Skip to content

Commit

Permalink
Merge proto and cc into common structs and split typeinfo file (#499)
Browse files Browse the repository at this point in the history
* Merge proto and cc into common structs and split typeinfo file

* update code
  • Loading branch information
DanG100 authored Nov 18, 2024
1 parent 5f86cd5 commit 1384841
Show file tree
Hide file tree
Showing 7 changed files with 1,333 additions and 1,273 deletions.
48 changes: 27 additions & 21 deletions dataplane/apigen/ccgen/ccgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ extern "C" {
extern const {{ .APIType }} l_{{ .APIName }};
{{ range .Funcs }}
{{- if .Name }}
{{ .ReturnType }} l_{{ .Name }}({{ .Args }});
{{- end }}
{{ end }}
#endif // {{ .IncludeGuard }}
Expand Down Expand Up @@ -216,8 +218,10 @@ switch ({{ .Var }}) {
const {{ .APIType }} l_{{ .APIName }} = {
{{- range .Funcs }}
{{- if .Name }}
.{{ .Name }} = l_{{ .Name }},
{{- end }}
{{- end }}
};
{{ range .Globals }}
Expand Down Expand Up @@ -245,16 +249,17 @@ return msg;
{{ end }}
{{- range .Funcs }}
{{- if .Name }}
{{ .ReturnType }} l_{{ .Name }}({{ .Args }}) {
LOG(INFO) << "Func: " << __PRETTY_FUNCTION__;
{{- if .UseCommonAPI }}
{{ if eq .Operation "create" }}
lemming::dataplane::sai::{{ .ReqType }} req = {{.ConvertFunc}}({{.Vars}});
lemming::dataplane::sai::{{ .RespType }} resp;
lemming::dataplane::sai::{{ .ProtoRequestType }} req = {{.ConvertFunc}}({{.Vars}});
lemming::dataplane::sai::{{ .ProtoResponseType }} resp;
grpc::ClientContext context;
{{ if .SwitchScoped }} req.set_switch_(switch_id); {{ end }}
{{ if .EntryVar }} *req.mutable_entry() = {{ .EntryConversionFunc }}({{ .EntryVar }}); {{ end }}
grpc::Status status = {{ .Client }}->{{ .RPCMethod }}(&context, req, &resp);
grpc::Status status = {{ .Client }}->{{ .ProtoRPCName }}(&context, req, &resp);
if (!status.ok()) {
auto it = context.GetServerTrailingMetadata().find("traceparent");
if (it != context.GetServerTrailingMetadata().end()) {
Expand All @@ -270,8 +275,8 @@ return msg;
}
{{ end }}
{{ else if eq .Operation "create_bulk" }}
lemming::dataplane::sai::{{ .ReqType }} req;
lemming::dataplane::sai::{{ .RespType }} resp;
lemming::dataplane::sai::{{ .ProtoRequestType }} req;
lemming::dataplane::sai::{{ .ProtoResponseType }} resp;
grpc::ClientContext context;
for (uint32_t i = 0; i < object_count; i++) {
Expand All @@ -286,7 +291,7 @@ return msg;
*req.add_reqs() = r;
}
grpc::Status status = {{ .Client }}->{{ .RPCMethod }}(&context, req, &resp);
grpc::Status status = {{ .Client }}->{{ .ProtoRPCName }}(&context, req, &resp);
if (!status.ok()) {
auto it = context.GetServerTrailingMetadata().find("traceparent");
if (it != context.GetServerTrailingMetadata().end()) {
Expand All @@ -305,16 +310,16 @@ return msg;
}
{{ else if eq .Operation "get_attribute" }}
lemming::dataplane::sai::{{ .ReqType }} req;
lemming::dataplane::sai::{{ .RespType }} resp;
lemming::dataplane::sai::{{ .ProtoRequestType }} req;
lemming::dataplane::sai::{{ .ProtoResponseType }} resp;
grpc::ClientContext context;
{{ if .EntryVar }} *req.mutable_entry() = {{ .EntryConversionFunc }}({{ .EntryVar }}); {{ end }}
{{ if .OidVar -}} req.set_oid({{ .OidVar }}); {{ end }}
for (uint32_t i = 0; i < attr_count; i++) {
req.add_attr_type(convert_{{ .AttrType }}_to_proto(attr_list[i].id));
}
grpc::Status status = {{ .Client }}->{{ .RPCMethod }}(&context, req, &resp);
grpc::Status status = {{ .Client }}->{{ .ProtoRPCName }}(&context, req, &resp);
if (!status.ok()) {
auto it = context.GetServerTrailingMetadata().find("traceparent");
if (it != context.GetServerTrailingMetadata().end()) {
Expand All @@ -329,14 +334,14 @@ return msg;
{{ template "getattr" .AttrSwitch }}
}
{{ else if and (eq .Operation "set_attribute") (ne (len .AttrSwitch.Attrs) 0) }}
lemming::dataplane::sai::{{ .ReqType }} req;
lemming::dataplane::sai::{{ .RespType }} resp;
lemming::dataplane::sai::{{ .ProtoRequestType }} req;
lemming::dataplane::sai::{{ .ProtoResponseType }} resp;
grpc::ClientContext context;
{{ if .OidVar -}} req.set_oid({{ .OidVar }}); {{ end }}
{{ if .EntryVar }} *req.mutable_entry() = {{ .EntryConversionFunc }}({{ .EntryVar }}); {{ end }}
{{ .AttrConvertInsert }}
{{ template "setattr" .AttrSwitch }}
grpc::Status status = {{ .Client }}->{{ .RPCMethod }}(&context, req, &resp);
grpc::Status status = {{ .Client }}->{{ .ProtoRPCName }}(&context, req, &resp);
if (!status.ok()) {
auto it = context.GetServerTrailingMetadata().find("traceparent");
if (it != context.GetServerTrailingMetadata().end()) {
Expand All @@ -347,12 +352,12 @@ return msg;
return SAI_STATUS_FAILURE;
}
{{ else if eq .Operation "remove" }}
lemming::dataplane::sai::{{ .ReqType }} req;
lemming::dataplane::sai::{{ .RespType }} resp;
lemming::dataplane::sai::{{ .ProtoRequestType }} req;
lemming::dataplane::sai::{{ .ProtoResponseType }} resp;
grpc::ClientContext context;
{{ if .OidVar -}} req.set_oid({{ .OidVar }}); {{ end }}
{{ if .EntryVar }} *req.mutable_entry() = {{ .EntryConversionFunc }}({{ .EntryVar }}); {{ end }}
grpc::Status status = {{ .Client }}->{{ .RPCMethod }}(&context, req, &resp);
grpc::Status status = {{ .Client }}->{{ .ProtoRPCName }}(&context, req, &resp);
if (!status.ok()) {
auto it = context.GetServerTrailingMetadata().find("traceparent");
if (it != context.GetServerTrailingMetadata().end()) {
Expand All @@ -363,16 +368,16 @@ return msg;
return SAI_STATUS_FAILURE;
}
{{ else if eq .Operation "remove_bulk" }}
lemming::dataplane::sai::{{ .ReqType }} req;
lemming::dataplane::sai::{{ .RespType }} resp;
lemming::dataplane::sai::{{ .ProtoRequestType }} req;
lemming::dataplane::sai::{{ .ProtoResponseType }} resp;
grpc::ClientContext context;
for (uint32_t i = 0; i < object_count; i++) {
{{ if .OidVar -}} req.add_reqs()->set_oid(object_id[i]); {{ end }}
{{ if .EntryVar }} *req.add_reqs()->mutable_entry() = {{ .EntryConversionFunc }}({{ .EntryVar }}[i]); {{ end }}
}
grpc::Status status = {{ .Client }}->{{ .RPCMethod }}(&context, req, &resp);
grpc::Status status = {{ .Client }}->{{ .ProtoRPCName }}(&context, req, &resp);
if (!status.ok()) {
auto it = context.GetServerTrailingMetadata().find("traceparent");
if (it != context.GetServerTrailingMetadata().end()) {
Expand All @@ -389,15 +394,15 @@ return msg;
object_statuses[i] = SAI_STATUS_SUCCESS;
}
{{ else if eq .Operation "get_stats" }}
lemming::dataplane::sai::{{ .ReqType }} req;
lemming::dataplane::sai::{{ .RespType }} resp;
lemming::dataplane::sai::{{ .ProtoRequestType }} req;
lemming::dataplane::sai::{{ .ProtoResponseType }} resp;
grpc::ClientContext context;
{{ if .OidVar -}} req.set_oid({{ .OidVar }}); {{ end }}
{{ if .EntryVar }} *req.mutable_entry() = {{ .EntryConversionFunc }}({{ .EntryVar }}); {{ end }}
for (uint32_t i = 0; i < number_of_counters; i++) {
req.add_counter_ids(convert_{{ .AttrType }}_to_proto(counter_ids[i]));
}
grpc::Status status = {{ .Client }}->{{ .RPCMethod }}(&context, req, &resp);
grpc::Status status = {{ .Client }}->{{ .ProtoRPCName }}(&context, req, &resp);
if (!status.ok()) {
auto it = context.GetServerTrailingMetadata().find("traceparent");
if (it != context.GetServerTrailingMetadata().end()) {
Expand All @@ -416,6 +421,7 @@ return msg;
return SAI_STATUS_NOT_IMPLEMENTED;
{{- end }}
}
{{- end }}
{{ end }}
`))
enumHeaderTmpl = template.Must(template.New("enum").Parse(`
Expand Down
8 changes: 5 additions & 3 deletions dataplane/apigen/protogen/protogen.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ enum {{ .Name }} {
}
{{ end -}}
{{ range .Messages }}
{{ range .Types }}
message {{ .Name }} {
{{- if .Option }}
{{ .Option }};
Expand All @@ -169,8 +169,10 @@ message {{ .Name }} {
{{ end }}
service {{ .ServiceName }} {
{{- range .RPCs }}
rpc {{ .Name }} ({{ .RequestName }}) returns ({{ .ResponseName }}) {}
{{- range .Funcs }}
{{- if .ProtoRPCName }}
rpc {{ .ProtoRPCName }} ({{ .ProtoRequestType }}) returns ({{ .ProtoResponseType }}) {}
{{- end }}
{{- end }}
}
`))
Expand Down
5 changes: 4 additions & 1 deletion dataplane/apigen/typeinfo/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "typeinfo",
srcs = ["typeinfo.go"],
srcs = [
"typeinfo.go",
"types.go",
],
importpath = "github.com/openconfig/lemming/dataplane/apigen/typeinfo",
visibility = ["//visibility:public"],
deps = [
Expand Down
Loading

0 comments on commit 1384841

Please sign in to comment.