diff --git a/controller/interfaces/plugin/plugin.go b/controller/interfaces/plugin/plugin.go deleted file mode 100644 index f61bda8a8ec589486a6452141d4e07727790e2c3..0000000000000000000000000000000000000000 --- a/controller/interfaces/plugin/plugin.go +++ /dev/null @@ -1,199 +0,0 @@ -package plugin - -import ( - "encoding/json" - "fmt" - "net" - "os" - "path/filepath" - "regexp" - - "code.fbi.h-da.de/danet/gosdn/controller/customerrs" - "code.fbi.h-da.de/danet/gosdn/controller/nucleus/util" - "code.fbi.h-da.de/danet/gosdn/controller/plugin/shared" - "github.com/google/uuid" - hcplugin "github.com/hashicorp/go-plugin" - "go.mongodb.org/mongo-driver/bson" - "gopkg.in/yaml.v3" -) - -// State represents the current state of a plugin within the controller. Since -// the plugins used within the controller are basic go plugins, they can be -// CREATED, BUILT, LOADED or FAULTY. A plugin can not be unloaded (this is a -// limitation of go plugins in general). -type State int64 - -const ( - //CREATED state describes a plugin which has been created but is not yet - //built. - CREATED State = iota - // INITIALIZED state describes a plugin which is running and has been - // initialized with the model data of the associated network element. - INITIALIZED - // FAULTY state describes a plugin which couldn't be built or loaded. - FAULTY -) - -// Plugin describes an interface for a plugin within the controller. A plugin -// is based on hashicorp's `go plugin`. -type Plugin interface { - ID() uuid.UUID - GetClient() *hcplugin.Client - State() State - Manifest() *Manifest - ExecPath() string - Update() error - Ping() error - Restart() error - Close() - shared.DeviceModel -} - -// Manifest represents the manifest of a plugin. -type Manifest struct { - // Name of the plugin - Name string `yaml:"name" json:"name" bson:"name"` - // Name of the plugin - Firmware string `yaml:"firmware" json:"firmware" bson:"firmware"` - // Author of the plugin - Author string `yaml:"author" json:"author" bson:"author"` - // Version of the plugin - Version string `yaml:"version" json:"version" bson:"version"` -} - -// Validate is a method to check if the manifest is valid and is compliant with -// the requirements. -func (m *Manifest) Validate() error { - errs := []error{} - if m.Name == "" { - errs = append(errs, fmt.Errorf("Name is required")) - } - if m.Firmware == "" { - errs = append(errs, fmt.Errorf("Firmware is required")) - } - if m.Author == "" { - errs = append(errs, fmt.Errorf("Author is required")) - } - if m.Version == "" { - errs = append(errs, fmt.Errorf("Version is required")) - } - // regex from: https://stackoverflow.com/a/68921827 - validVersion, err := regexp.MatchString(`^([1-9]\d*|0)(\.(([1-9]\d*)|0)){2}$`, - m.Version) - if err != nil { - errs = append(errs, err) - } - if !validVersion { - errs = append(errs, fmt.Errorf("Version has to be of form: X.X.X")) - } - if len(errs) != 0 { - return customerrs.CombinedErrListError{Errors: errs} - } - return nil -} - -// ReadManifestFromFile reads a manifest file and returns a pointer to a newly -// created Manifest. -func ReadManifestFromFile(path string) (*Manifest, error) { - manifest := &Manifest{} - - manifestFile, err := os.ReadFile(filepath.Join(path, util.ManifestFileName)) - if err != nil { - return nil, err - } - - err = yaml.Unmarshal(manifestFile, manifest) - if err != nil { - return nil, err - } - - // validate the loaded manifest - if err := manifest.Validate(); err != nil { - return nil, err - } - - return manifest, nil -} - -type LoadedPlugin struct { - // ID represents the UUID of the LoadedPlugin. - ID string `json:"id" bson:"_id"` - // Manifest represents the manifest of the LoadedPlugin. - Manifest Manifest `json:"manifest" bson:"manifest"` - // State represents the state of the LoadedPlugin. - State State `json:"state,omitempty" bson:"state"` - // ExecPath represents the path to the executable of the plugin. - ExecPath string `json:"exec_path,omitempty" bson:"exec_path"` - // ReattachConfig represents the configuration to reattach to a already - // running plugin. - ReattachConfig hcplugin.ReattachConfig `json:"reattatch_config,omitempty" bson:"reattatch_config"` -} - -func (lp *LoadedPlugin) UnmarshalBSON(data []byte) error { - loadedPluginHelper := new(LoadedPluginHelper) - if err := bson.Unmarshal(data, loadedPluginHelper); err != nil { - return err - } - - lp.ID = loadedPluginHelper.ID - lp.Manifest = loadedPluginHelper.Manifest - lp.State = loadedPluginHelper.State - lp.ExecPath = loadedPluginHelper.ExecPath - lp.ReattachConfig = hcplugin.ReattachConfig{ - Protocol: hcplugin.Protocol(loadedPluginHelper.ReattachConfig.Protocol), - ProtocolVersion: loadedPluginHelper.ReattachConfig.ProtocolVersion, - Addr: &net.UnixAddr{ - Name: loadedPluginHelper.ReattachConfig.Addr.Name, - Net: loadedPluginHelper.ReattachConfig.Addr.Net, - }, - Pid: loadedPluginHelper.ReattachConfig.Pid, - Test: loadedPluginHelper.ReattachConfig.Test, - } - - return nil -} - -func (lp *LoadedPlugin) UnmarshalJSON(data []byte) error { - loadedPluginHelper := new(LoadedPluginHelper) - if err := json.Unmarshal(data, loadedPluginHelper); err != nil { - return err - } - - lp.ID = loadedPluginHelper.ID - lp.Manifest = loadedPluginHelper.Manifest - lp.State = loadedPluginHelper.State - lp.ExecPath = loadedPluginHelper.ExecPath - lp.ReattachConfig = hcplugin.ReattachConfig{ - Protocol: hcplugin.Protocol(loadedPluginHelper.ReattachConfig.Protocol), - ProtocolVersion: loadedPluginHelper.ReattachConfig.ProtocolVersion, - Addr: &net.UnixAddr{ - Name: loadedPluginHelper.ReattachConfig.Addr.Name, - Net: loadedPluginHelper.ReattachConfig.Addr.Net, - }, - Pid: loadedPluginHelper.ReattachConfig.Pid, - Test: loadedPluginHelper.ReattachConfig.Test, - } - - return nil -} - -type LoadedPluginHelper struct { - ID string `json:"id" bson:"_id"` - Manifest Manifest `json:"manifest" bson:"manifest"` - State State `json:"state,omitempty" bson:"state"` - ExecPath string `json:"exec_path,omitempty" bson:"exec_path"` - ReattachConfig LoadedReattachConfig `json:"reattatch_config,omitempty" bson:"reattatch_config"` -} - -type LoadedReattachConfig struct { - Protocol string - ProtocolVersion int - Addr LoadedAddress - Pid int - Test bool -} - -type LoadedAddress struct { - Name string - Net string -} diff --git a/controller/interfaces/plugin/pluginService.go b/controller/interfaces/plugin/pluginService.go deleted file mode 100644 index 801acaa9e6f06e02337ef0442b1975e7b9b1007f..0000000000000000000000000000000000000000 --- a/controller/interfaces/plugin/pluginService.go +++ /dev/null @@ -1,10 +0,0 @@ -package plugin - -// // Service describes an interface for plugin service implementations. -// type Service interface { -// Add(Plugin) error -// Delete(Plugin) error -// Get(model.Query) (Plugin, error) -// GetAll() ([]Plugin, error) -// RequestPlugin(uuid.UUID) (Plugin, error) -// } diff --git a/controller/interfaces/plugin/pluginStore.go b/controller/interfaces/plugin/pluginStore.go deleted file mode 100644 index 1f25a3c79f29ce41401045b5c5aa42c7b9df9ed1..0000000000000000000000000000000000000000 --- a/controller/interfaces/plugin/pluginStore.go +++ /dev/null @@ -1,10 +0,0 @@ -package plugin - -// // Store describes an interface for plugin store implementations. -// type Store interface { -// Add(Plugin) error -// Update(Plugin) error -// Delete(Plugin) error -// Get(model.Query) (LoadedPlugin, error) -// GetAll() ([]LoadedPlugin, error) -// } diff --git a/controller/northbound/server/configurationmanagement.go b/controller/northbound/server/configurationmanagement.go index 3c6ba847cfd0aa47883db604d70abf76400790ef..38746bd83e9263e2e42503ab4f59babfccf52a23 100644 --- a/controller/northbound/server/configurationmanagement.go +++ b/controller/northbound/server/configurationmanagement.go @@ -9,7 +9,6 @@ import ( spb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/southbound" tpb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/transport" "code.fbi.h-da.de/danet/gosdn/controller/conflict" - "code.fbi.h-da.de/danet/gosdn/controller/interfaces/plugin" "code.fbi.h-da.de/danet/gosdn/controller/nucleus/domain/model" "code.fbi.h-da.de/danet/gosdn/controller/nucleus/domain/ports" "code.fbi.h-da.de/danet/gosdn/controller/topology" @@ -62,7 +61,7 @@ type sdnConfig struct { Nodes []nodes.Node `json:"nodes"` Ports []topoPorts.Port `json:"ports"` Links []links.Link `json:"links"` - Plugins []plugin.LoadedPlugin `json:"plugins"` + Plugins []model.LoadedPlugin `json:"plugins"` NetworkElements []model.NetworkElement `json:"networkelements"` } @@ -72,12 +71,15 @@ type loadedSDNConfig struct { Nodes []nodes.Node `json:"nodes"` Ports []topoPorts.Port `json:"ports"` Links []links.Link `json:"links"` - Plugins []plugin.LoadedPlugin `json:"plugins"` + Plugins []model.LoadedPlugin `json:"plugins"` NetworkElements []model.LoadedNetworkElement `json:"networkelements"` } // ExportSDNConfig returns the SDN configuration. -func (c ConfigurationManagementServer) ExportSDNConfig(ctx context.Context, request *cmpb.ExportSDNConfigRequest) (*cmpb.ExportSDNConfigResponse, error) { +func (c ConfigurationManagementServer) ExportSDNConfig( + ctx context.Context, + request *cmpb.ExportSDNConfigRequest, +) (*cmpb.ExportSDNConfigResponse, error) { if err := c.protoValidator.Validate(request); err != nil { return nil, status.Errorf(codes.Aborted, "%v", err) } diff --git a/controller/nucleus/domain/model/plugin.go b/controller/nucleus/domain/model/plugin.go index f2a2c72fba4fbc68296eab3e8a77e8cf7384860f..1bd6391fccfdc646aa5bb734c42557a4cc086c92 100644 --- a/controller/nucleus/domain/model/plugin.go +++ b/controller/nucleus/domain/model/plugin.go @@ -7,10 +7,10 @@ import ( "os" "os/exec" "path/filepath" + "plugin" "regexp" "code.fbi.h-da.de/danet/gosdn/controller/customerrs" - "code.fbi.h-da.de/danet/gosdn/controller/interfaces/plugin" "code.fbi.h-da.de/danet/gosdn/controller/nucleus/util" "code.fbi.h-da.de/danet/gosdn/controller/plugin/shared" "github.com/google/uuid" diff --git a/plugin-registry/main.go b/plugin-registry/main.go index 95d113f5a377b03d8677a27dec233c073915ab11..83a4d5cb4b09232a34651accde7af4421b47fc7b 100644 --- a/plugin-registry/main.go +++ b/plugin-registry/main.go @@ -8,7 +8,7 @@ import ( "path/filepath" pb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/plugin-registry" - "code.fbi.h-da.de/danet/gosdn/controller/interfaces/plugin" + "code.fbi.h-da.de/danet/gosdn/controller/nucleus/domain/model" "github.com/google/uuid" "google.golang.org/grpc" ) @@ -52,7 +52,7 @@ func registerPlugins() *PluginRegistry { fmt.Printf("File %+v\n", file) if file.IsDir() { - manifest, err := plugin.ReadManifestFromFile(filepath.Join(pluginFilePath, file.Name())) + manifest, err := model.ReadManifestFromFile(filepath.Join(pluginFilePath, file.Name())) if err != nil { panic(err) } diff --git a/plugin-registry/registry.go b/plugin-registry/registry.go index b658b3142ce2f42f0475388d377d938fb9e9aa65..743b188496d8c58bf67a72c3439742d47b757cbd 100644 --- a/plugin-registry/registry.go +++ b/plugin-registry/registry.go @@ -4,14 +4,14 @@ import ( "fmt" pb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/plugin-registry" - "code.fbi.h-da.de/danet/gosdn/controller/interfaces/plugin" + "code.fbi.h-da.de/danet/gosdn/controller/nucleus/domain/model" "github.com/google/uuid" ) type Plugin struct { - ID uuid.UUID `json:"id,omitempty"` - Path string `json:"path,omitempty"` - Manifest *plugin.Manifest `json:"manifest,omitempty"` + ID uuid.UUID `json:"id,omitempty"` + Path string `json:"path,omitempty"` + Manifest *model.Manifest `json:"manifest,omitempty"` } type PluginRegistry struct {