go.ligato.io/vpp-agent/v3@v3.5.0/pkg/models/api.go (about) 1 // Copyright (c) 2020 Pantheon.tech 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at: 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package models 16 17 import ( 18 "reflect" 19 20 "google.golang.org/protobuf/proto" 21 "google.golang.org/protobuf/reflect/protoreflect" 22 "google.golang.org/protobuf/reflect/protoregistry" 23 24 "go.ligato.io/vpp-agent/v3/proto/ligato/generic" 25 ) 26 27 // ModelInfo represents model information retrieved using meta service 28 type ModelInfo struct { 29 *generic.ModelDetail 30 31 // MessageDescriptor is the proto message descriptor of the message represented by this ModelInfo struct 32 MessageDescriptor protoreflect.MessageDescriptor 33 } 34 35 // Registry defines model registry for managing registered models 36 type Registry interface { 37 // GetModel returns registered model for the given model name 38 // or error if model is not found. 39 GetModel(name string) (KnownModel, error) 40 41 // GetModelFor returns registered model for the given proto message. 42 GetModelFor(x interface{}) (KnownModel, error) 43 44 // GetModelForKey returns registered model for the given key or error. 45 GetModelForKey(key string) (KnownModel, error) 46 47 // MessageTypeRegistry creates new message type registry from registered proto messages 48 MessageTypeRegistry() *protoregistry.Types 49 50 // RegisteredModels returns all registered modules. 51 RegisteredModels() []KnownModel 52 53 // Register registers either a protobuf message known at compile-time together 54 // with the given model specification (for LocalRegistry), 55 // or a remote model represented by an instance of ModelInfo obtained via KnownModels RPC from MetaService 56 // (for RemoteRegistry or also for LocalRegistry but most likely just proxied to a remote agent). 57 // If spec.Class is unset, then it defaults to 'config'. 58 Register(x interface{}, spec Spec, opts ...ModelOption) (KnownModel, error) 59 } 60 61 // KnownModel represents a registered model 62 type KnownModel interface { 63 // Spec returns model specification for the model. 64 Spec() *Spec 65 66 // ModelDetail returns descriptor for the model. 67 ModelDetail() *generic.ModelDetail 68 69 // NewInstance creates new instance value for model type. 70 NewInstance() proto.Message 71 72 // ProtoName returns proto message name registered with the model. 73 ProtoName() string 74 75 // ProtoFile returns proto file name for the model. 76 ProtoFile() string 77 78 // NameTemplate returns name template for the model. 79 NameTemplate() string 80 81 // GoType returns go type for the model. 82 GoType() string 83 84 // LocalGoType returns reflect go type for the model. The reflect type can be retrieved only 85 // for locally registered model that provide locally known go types. The remotely retrieved model 86 // can't provide reflect type so if known model information is retrieved remotely, this method 87 // will return nil. 88 LocalGoType() reflect.Type 89 90 // PkgPath returns package import path for the model definition. 91 PkgPath() string 92 93 // Name returns name for the model. 94 Name() string 95 96 // KeyPrefix returns key prefix for the model. 97 KeyPrefix() string 98 99 // ParseKey parses the given key and returns item name 100 // or returns empty name and valid as false if the key is not valid. 101 ParseKey(key string) (name string, valid bool) 102 103 // IsKeyValid returns true if given key is valid for this model. 104 IsKeyValid(key string) bool 105 106 // StripKeyPrefix returns key with prefix stripped. 107 StripKeyPrefix(key string) string 108 109 // InstanceName computes message name for given proto message using name template (if present). 110 InstanceName(x interface{}) (string, error) 111 }