diff --git a/nucleus/cli-handling.go b/nucleus/cli-handling.go index 432b9d4eb98932cbebdeaeb3e4f3d052f82358d5..2749fbd12473d0b563ba6a78e9ff3dc3eded6407 100644 --- a/nucleus/cli-handling.go +++ b/nucleus/cli-handling.go @@ -162,7 +162,6 @@ func (s *server) CreatePND(ctx context.Context, in *pb.CreatePNDRequest) (*pb.Cr sbi := s.core.southboundInterfaces[in.GetSbi()] id := uuid.New() s.core.principalNetworkDomains[id] = NewPND(in.GetName(), in.GetDescription(), sbi) - s.core.MarshallPNDs() return &pb.CreatePNDReply{Message: "Created new PND: " + id.String()}, nil } @@ -172,7 +171,7 @@ func (s *server) GetAllPNDs(ctx context.Context, in *emptypb.Empty) (*pb.AllPNDs var pnds []*pb.PND for uuidPND, pnd := range s.core.principalNetworkDomains { var devices []*pb.Device - for uuidDevice, device := range pnd.(*pndImplementation).Devices { + for uuidDevice, device := range pnd.(*pndImplementation).devices { tmpDevice := pb.Device{ Uuid: uuidDevice.String(), Address: device.Config.Address, @@ -230,9 +229,9 @@ func (s *server) AddDevice(ctx context.Context, in *pb.AddDeviceRequest) (*pb.Ad }, Transport: transport, } + newDevice.GoStruct = sbi.Schema().Root devicesMap := pnd.GetDevices() devicesMap[newDevice.Config.Uuid] = &newDevice - s.core.MarshallPNDs() return &pb.AddDeviceReply{Message: "Added new Device: " + newDevice.Config.Uuid.String()}, err } @@ -262,11 +261,12 @@ func (s *server) HandleDeviceGetRequest(ctx context.Context, in *pb.DeviceGetReq if err != nil { return &pb.DeviceGetReply{Message: err.Error()}, err } + log.Info(device) d, err := json.MarshalIndent(device, "", "\t") if err != nil { return &pb.DeviceGetReply{Message: err.Error()}, err } - return &pb.DeviceGetReply{Message: string(d)}, nil + return &pb.DeviceGetReply{Message: string(string(d))}, nil } diff --git a/nucleus/controller.go b/nucleus/controller.go index bc10fad0ae2a4c7552af80a6d39ccfb625c43544..bbf3dbd95533e75bad0d76f9829ff6c8f19f7a6d 100644 --- a/nucleus/controller.go +++ b/nucleus/controller.go @@ -1,12 +1,11 @@ package nucleus import ( - "encoding/json" "fmt" - "io/ioutil" "os" "code.fbi.h-da.de/cocsn/gosdn/database" + "github.com/google/uuid" log "github.com/sirupsen/logrus" "github.com/spf13/viper" ) @@ -14,7 +13,7 @@ import ( // Core is the representation of the controllers core type Core struct { southboundInterfaces map[string]SouthboundInterface - principalNetworkDomains PrincipalNetworkDomainMap + principalNetworkDomains map[uuid.UUID]PrincipalNetworkDomain database database.Database IsRunning chan bool } @@ -39,11 +38,6 @@ func (c *Core) Initialize(IsRunningChannel chan bool) { log.Info(sbi) } - // err = c.UnMarshallPNDs() - // if err != nil { - // log.Info(err) - // } - c.IsRunning = IsRunningChannel } @@ -52,33 +46,6 @@ func (c *Core) AttachDatabase() { c.database = database.NewDatabaseClient() } -func (c *Core) MarshallPNDs() error { - b, err := json.MarshalIndent(c.principalNetworkDomains, "", "\t") - if err != nil { - return err - } - err = ioutil.WriteFile("./backup.json", b, 0644) - if err != nil { - return err - } - return nil -} - -func (c *Core) UnMarshallPNDs() error { - backup, err := ioutil.ReadFile("./backup.json") - if err != nil { - return err - } - err = json.Unmarshal(backup, &c.principalNetworkDomains) - if err != nil { - return err - } - for _, v := range c.principalNetworkDomains { - log.Info(v.GetSBIs()) - } - return nil -} - // CreateSouthboundInterfaces initializes the controller with his SBIs func (c *Core) CreateSouthboundInterfaces() { arista := &AristaOC{} diff --git a/nucleus/device.go b/nucleus/device.go index 2539413479aa0c1ce87eae96a8c653642f397e72..86ea4a08b204b1d13579a082754e28d937d4a700 100644 --- a/nucleus/device.go +++ b/nucleus/device.go @@ -6,17 +6,17 @@ import ( ) type Device struct { - // Device inherits properies of ygot.GoStruct + // Device inherits properties of ygot.GoStruct ygot.GoStruct // SBI is the device's southbound interface implementation - SBI SouthboundInterface + SBI SouthboundInterface `json:"-"` // Config is the device's config. Under revision - Config DeviceConfig + Config DeviceConfig `json:"-"` // Transport is the device's Transport implementation - Transport Transport + Transport Transport `json:"-"` } type DeviceConfig struct { diff --git a/nucleus/json_Helper.go b/nucleus/json_Helper.go deleted file mode 100644 index dc9c13816d5f7066f15b5ce5870f1d35450f9de2..0000000000000000000000000000000000000000 --- a/nucleus/json_Helper.go +++ /dev/null @@ -1,57 +0,0 @@ -package nucleus - -import ( - "encoding/json" - "github.com/google/uuid" -) - -type PrincipalNetworkDomainMap map[uuid.UUID]PrincipalNetworkDomain - -func (m PrincipalNetworkDomainMap) UnmarshalJSON(b []byte) error { - data := make(map[string]json.RawMessage) - if err := json.Unmarshal(b, &data); err != nil { - return err - } - - for k, v := range data { - dest := &pndImplementation{} - if err := json.Unmarshal(v, dest); err != nil { - return err - } - id, err := uuid.Parse(k) - if err != nil { - return err - } - m[id] = dest - } - return nil -} - -type SouthboundInterfaceMap map[string]SouthboundInterface - -func (m *SouthboundInterfaceMap) UnmarshalJSON(b []byte) error { - m2 := make(map[string]SouthboundInterface) - data := make(map[string]interface{}) - if err := json.Unmarshal(b, &data); err != nil { - return err - } - - for k, v := range data { - m2[k] = getSouthboundInterfaceFromJSONMap(v.(map[string]interface{})) - } - *m = m2 - return nil -} - -func getSouthboundInterfaceFromJSONMap(m map[string]interface{}) SouthboundInterface { - var newSBI SouthboundInterface - - switch sbi := m["name"]; sbi { - case "arista": - newSBI = &AristaOC{} - - case "openconfig": - newSBI = &OpenConfig{} - } - return newSBI -} diff --git a/nucleus/principalNetworkDomain.go b/nucleus/principalNetworkDomain.go index 5661cf19cd1130483c6cd30edab64cf0125ec2b3..c304f8ae399d2278b576c2f36daebafd161f8195 100644 --- a/nucleus/principalNetworkDomain.go +++ b/nucleus/principalNetworkDomain.go @@ -22,10 +22,10 @@ type PrincipalNetworkDomain interface { } type pndImplementation struct { - Name string `json:"name"` - Description string `json:"description"` - Sbi SouthboundInterfaceMap `json:"sbis"` - Devices map[uuid.UUID]*Device `json:"devices"` + name string + description string + sbi map[string]SouthboundInterface + devices map[uuid.UUID]*Device } //NewPND creates a Principle Network Domain @@ -34,27 +34,27 @@ func NewPND(name, description string, sbi SouthboundInterface) PrincipalNetworkD sbic["default"] = sbi devices := make(map[uuid.UUID]*Device) return &pndImplementation{ - Name: name, - Description: description, - Sbi: sbic, - Devices: devices, + name: name, + description: description, + sbi: sbic, + devices: devices, } } func (pnd *pndImplementation) GetName() string { - return pnd.Name + return pnd.name } func (pnd *pndImplementation) GetDevices() map[uuid.UUID]*Device { - return pnd.Devices + return pnd.devices } func (pnd *pndImplementation) GetDescription() string { - return pnd.Description + return pnd.description } func (pnd *pndImplementation) GetSBIs() map[string]SouthboundInterface { - return pnd.Sbi + return pnd.sbi } // Interface satisfaction @@ -85,29 +85,33 @@ func destroy() error { } func (pnd *pndImplementation) addSbi(sbi SouthboundInterface) error { - pnd.Sbi[sbi.SbiIdentifier()] = sbi + pnd.sbi[sbi.SbiIdentifier()] = sbi return nil } func (pnd *pndImplementation) removeSbi(sbiIdentifier string) error { - delete(pnd.Sbi, sbiIdentifier) + delete(pnd.sbi, sbiIdentifier) return nil } func (pnd *pndImplementation) addDevice(device *Device) error { - pnd.Devices[device.Config.Uuid] = device + pnd.devices[device.Config.Uuid] = device return nil } func (pnd *pndImplementation) removeDevice(uuid uuid.UUID) error { - delete(pnd.Devices, uuid) + delete(pnd.devices, uuid) return nil } func (pnd *pndImplementation) Request(uuid uuid.UUID, path string) error { - d := pnd.Devices[uuid] + d := pnd.devices[uuid] ctx := context.Background() - _, err := d.Transport.Get(ctx, path) + res, err := d.Transport.Get(ctx, path) + if err != nil { + return err + } + err = d.Transport.ProcessResponse(res, d.GoStruct, d.SBI.Schema()) if err != nil { return err } @@ -115,7 +119,7 @@ func (pnd *pndImplementation) Request(uuid uuid.UUID, path string) error { } func (pnd *pndImplementation) RequestAll(path string) error { - for k := range pnd.Devices { + for k := range pnd.devices { if err := pnd.Request(k, path); err != nil { return err }