|
17 | 17 | package restapi
|
18 | 18 |
|
19 | 19 | import (
|
| 20 | + "bytes" |
20 | 21 | "context"
|
21 | 22 | "fmt"
|
22 | 23 | "net/http"
|
23 | 24 | "runtime"
|
| 25 | + "strings" |
24 | 26 |
|
25 | 27 | "github.com/go-errors/errors"
|
| 28 | + "github.com/golang/protobuf/proto" |
| 29 | + protoc_plugin "github.com/golang/protobuf/protoc-gen-go/plugin" |
26 | 30 | "github.com/unrolled/render"
|
27 | 31 |
|
| 32 | + "go.ligato.io/cn-infra/v2/logging/logrus" |
| 33 | + "go.ligato.io/vpp-agent/v3/client" |
28 | 34 | "go.ligato.io/vpp-agent/v3/cmd/agentctl/api/types"
|
29 | 35 | "go.ligato.io/vpp-agent/v3/pkg/version"
|
30 | 36 | "go.ligato.io/vpp-agent/v3/plugins/configurator"
|
| 37 | + "go.ligato.io/vpp-agent/v3/plugins/restapi/jsonschema/converter" |
31 | 38 | "go.ligato.io/vpp-agent/v3/plugins/restapi/resturl"
|
32 | 39 | interfaces "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/interfaces"
|
| 40 | + "google.golang.org/protobuf/reflect/protodesc" |
| 41 | + "google.golang.org/protobuf/reflect/protoreflect" |
| 42 | + "google.golang.org/protobuf/types/descriptorpb" |
| 43 | +) |
| 44 | + |
| 45 | +const ( |
| 46 | + // URLFieldNamingParamName is URL parameter name for JSON schema http handler's setting |
| 47 | + // to output field names using proto/json/both names for fields |
| 48 | + URLFieldNamingParamName = "fieldnames" |
| 49 | + // OnlyProtoFieldNames is URL parameter value for JSON schema http handler to use only proto names as field names |
| 50 | + OnlyProtoFieldNames = "onlyproto" |
| 51 | + // OnlyJSONFieldNames is URL parameter value for JSON schema http handler to use only JSON names as field names |
| 52 | + OnlyJSONFieldNames = "onlyjson" |
| 53 | + |
| 54 | + internalErrorLogPrefix = "500 Internal server error: " |
33 | 55 | )
|
34 | 56 |
|
35 | 57 | var (
|
|
40 | 62 |
|
41 | 63 | func (p *Plugin) registerInfoHandlers() {
|
42 | 64 | p.HTTPHandlers.RegisterHTTPHandler(resturl.Version, p.versionHandler, GET)
|
| 65 | + p.HTTPHandlers.RegisterHTTPHandler(resturl.JSONSchema, p.jsonSchemaHandler, GET) |
43 | 66 | }
|
44 | 67 |
|
45 | 68 | // Registers ABF REST handler
|
@@ -315,6 +338,138 @@ func (p *Plugin) registerHTTPHandler(key, method string, f func() (interface{},
|
315 | 338 | p.HTTPHandlers.RegisterHTTPHandler(key, handlerFunc, method)
|
316 | 339 | }
|
317 | 340 |
|
| 341 | +// jsonSchemaHandler returns JSON schema of VPP-Agent configuration. |
| 342 | +// This handler also accepts URL query parameters changing the exported field names of proto messages. By default, |
| 343 | +// proto message fields are exported twice in JSON scheme. Once with proto name and once with JSON name. This should |
| 344 | +// allow to use any of the 2 forms in JSON/YAML configuration when used JSON schema for validation. However, |
| 345 | +// this behaviour can be modified by URLFieldNamingParamName URL query parameter, that force to export only |
| 346 | +// proto named fields (OnlyProtoFieldNames URL query parameter value) or JSON named fields (OnlyJSONFieldNames |
| 347 | +// URL query parameter value). |
| 348 | +func (p *Plugin) jsonSchemaHandler(formatter *render.Render) http.HandlerFunc { |
| 349 | + return func(w http.ResponseWriter, req *http.Request) { |
| 350 | + // create FileDescriptorProto for dynamic Config holding all VPP-Agent configuration |
| 351 | + knownModels, err := client.LocalClient.KnownModels("config") // locally registered models |
| 352 | + if err != nil { |
| 353 | + errMsg := fmt.Sprintf("can't get registered models: %v\n", err) |
| 354 | + p.Log.Error(internalErrorLogPrefix + errMsg) |
| 355 | + p.logError(formatter.JSON(w, http.StatusInternalServerError, errMsg)) |
| 356 | + return |
| 357 | + } |
| 358 | + config, err := client.NewDynamicConfig(knownModels) |
| 359 | + if err != nil { |
| 360 | + errMsg := fmt.Sprintf("can't create dynamic config due to: %v\n", err) |
| 361 | + p.Log.Error(internalErrorLogPrefix + errMsg) |
| 362 | + p.logError(formatter.JSON(w, http.StatusInternalServerError, errMsg)) |
| 363 | + return |
| 364 | + } |
| 365 | + dynConfigFileDescProto := protodesc.ToFileDescriptorProto(config.ProtoReflect().Descriptor().ParentFile()) |
| 366 | + |
| 367 | + // create list of all FileDescriptorProtos (imports should be before converted proto file -> dynConfig is last) |
| 368 | + fileDescriptorProtos := allFileDescriptorProtos(knownModels) |
| 369 | + fileDescriptorProtos = append(fileDescriptorProtos, dynConfigFileDescProto) |
| 370 | + |
| 371 | + // creating input for protoc's plugin (code extracted in plugins/restapi/jsonschema) that can convert |
| 372 | + // FileDescriptorProtos to JSONSchema |
| 373 | + params := []string{ |
| 374 | + "messages=[Dynamic_config]", // targeting only the main config message (proto file has also other messages) |
| 375 | + "disallow_additional_properties", // additional unknown json fields makes configuration applying fail |
| 376 | + } |
| 377 | + fieldNamesConverterParam := "proto_and_json_fieldnames" // create proto and json named fields by default |
| 378 | + if fieldNames, found := req.URL.Query()[URLFieldNamingParamName]; found && len(fieldNames) > 0 { |
| 379 | + // converting REST API request params to 3rd party tool params |
| 380 | + switch fieldNames[0] { |
| 381 | + case OnlyProtoFieldNames: |
| 382 | + fieldNamesConverterParam = "" |
| 383 | + case OnlyJSONFieldNames: |
| 384 | + fieldNamesConverterParam = "json_fieldnames" |
| 385 | + } |
| 386 | + } |
| 387 | + if fieldNamesConverterParam != "" { |
| 388 | + params = append(params, fieldNamesConverterParam) |
| 389 | + } |
| 390 | + paramsStr := strings.Join(params, ",") |
| 391 | + cgReq := &protoc_plugin.CodeGeneratorRequest{ |
| 392 | + ProtoFile: fileDescriptorProtos, |
| 393 | + FileToGenerate: []string{dynConfigFileDescProto.GetName()}, |
| 394 | + Parameter: ¶msStr, |
| 395 | + CompilerVersion: nil, // compiler version is not need in this protoc plugin |
| 396 | + } |
| 397 | + cgReqMarshalled, err := proto.Marshal(cgReq) |
| 398 | + if err != nil { |
| 399 | + errMsg := fmt.Sprintf("can't proto marshal CodeGeneratorRequest: %v\n", err) |
| 400 | + p.Log.Error(internalErrorLogPrefix + errMsg) |
| 401 | + p.logError(formatter.JSON(w, http.StatusInternalServerError, errMsg)) |
| 402 | + return |
| 403 | + } |
| 404 | + |
| 405 | + // use JSON schema converter and handle error cases |
| 406 | + p.Log.Debug("Processing code generator request") |
| 407 | + protoConverter := converter.New(logrus.DefaultLogger().StandardLogger()) |
| 408 | + res, err := protoConverter.ConvertFrom(bytes.NewReader(cgReqMarshalled)) |
| 409 | + if err != nil { |
| 410 | + if res == nil { |
| 411 | + errMsg := fmt.Sprintf("failed to read registered model configuration input: %v\n", err) |
| 412 | + p.Log.Error(internalErrorLogPrefix + errMsg) |
| 413 | + p.logError(formatter.JSON(w, http.StatusInternalServerError, errMsg)) |
| 414 | + return |
| 415 | + } |
| 416 | + errMsg := fmt.Sprintf("failed generate JSON schema: %v (%v)\n", res.Error, err) |
| 417 | + p.Log.Error(internalErrorLogPrefix + errMsg) |
| 418 | + p.logError(formatter.JSON(w, http.StatusInternalServerError, errMsg)) |
| 419 | + return |
| 420 | + } |
| 421 | + |
| 422 | + // extract json schema |
| 423 | + // (protoc_plugin.CodeGeneratorResponse could have cut the file content into multiple pieces |
| 424 | + // for performance optimization (due to godoc), but we know that all pieces are only one file |
| 425 | + // due to requesting one file -> join all content together) |
| 426 | + var sb strings.Builder |
| 427 | + for _, file := range res.File { |
| 428 | + sb.WriteString(file.GetContent()) |
| 429 | + } |
| 430 | + |
| 431 | + // writing response |
| 432 | + // (jsonschema is in raw form (string) and non of the available format renders supports raw data output |
| 433 | + // with customizable content type setting in header -> custom handling) |
| 434 | + w.Header().Set(render.ContentType, render.ContentJSON+"; charset=UTF-8") |
| 435 | + w.Write([]byte(sb.String())) // will also call WriteHeader(http.StatusOK) automatically |
| 436 | + } |
| 437 | +} |
| 438 | + |
| 439 | +// allImports retrieves all imports from given FileDescriptor including transitive imports (import |
| 440 | +// duplication can occur) |
| 441 | +func allImports(fileDesc protoreflect.FileDescriptor) []protoreflect.FileDescriptor { |
| 442 | + result := make([]protoreflect.FileDescriptor, 0) |
| 443 | + imports := fileDesc.Imports() |
| 444 | + for i := 0; i < imports.Len(); i++ { |
| 445 | + currentImport := imports.Get(i).FileDescriptor |
| 446 | + result = append(result, currentImport) |
| 447 | + result = append(result, allImports(currentImport)...) |
| 448 | + } |
| 449 | + return result |
| 450 | +} |
| 451 | + |
| 452 | +// allFileDescriptorProtos retrieves all FileDescriptorProtos related to given models (including |
| 453 | +// all imported proto files) |
| 454 | +func allFileDescriptorProtos(knownModels []*client.ModelInfo) []*descriptorpb.FileDescriptorProto { |
| 455 | + // extract all FileDescriptors for given known models (including direct and transitive file imports) |
| 456 | + fileDescriptors := make(map[string]protoreflect.FileDescriptor) // using map for deduplication |
| 457 | + for _, knownModel := range knownModels { |
| 458 | + protoFile := knownModel.MessageDescriptor.ParentFile() |
| 459 | + fileDescriptors[protoFile.Path()] = protoFile |
| 460 | + for _, importProtoFile := range allImports(protoFile) { |
| 461 | + fileDescriptors[importProtoFile.Path()] = importProtoFile |
| 462 | + } |
| 463 | + } |
| 464 | + |
| 465 | + // convert retrieved FileDescriptors to FileDescriptorProtos |
| 466 | + fileDescriptorProtos := make([]*descriptorpb.FileDescriptorProto, 0, len(knownModels)) |
| 467 | + for _, fileDescriptor := range fileDescriptors { |
| 468 | + fileDescriptorProtos = append(fileDescriptorProtos, protodesc.ToFileDescriptorProto(fileDescriptor)) |
| 469 | + } |
| 470 | + return fileDescriptorProtos |
| 471 | +} |
| 472 | + |
318 | 473 | // versionHandler returns version of Agent.
|
319 | 474 | func (p *Plugin) versionHandler(formatter *render.Render) http.HandlerFunc {
|
320 | 475 | return func(w http.ResponseWriter, req *http.Request) {
|
|
0 commit comments