Skip to content
Snippets Groups Projects
Commit 57b8bb9e authored by Malte Bauch's avatar Malte Bauch
Browse files

device is now filled after a GET request

additional the JSON un-/marshalling was removed. -> therefore there is
currently no persistance of pnds,sbis and devices.
in the future we want to handle this via a database anyways.
parent e0fae3f7
Branches
Tags
Loading
This commit is part of merge request !91. Comments created here will be created in the context of that merge request.
...@@ -162,7 +162,6 @@ func (s *server) CreatePND(ctx context.Context, in *pb.CreatePNDRequest) (*pb.Cr ...@@ -162,7 +162,6 @@ func (s *server) CreatePND(ctx context.Context, in *pb.CreatePNDRequest) (*pb.Cr
sbi := s.core.southboundInterfaces[in.GetSbi()] sbi := s.core.southboundInterfaces[in.GetSbi()]
id := uuid.New() id := uuid.New()
s.core.principalNetworkDomains[id] = NewPND(in.GetName(), in.GetDescription(), sbi) s.core.principalNetworkDomains[id] = NewPND(in.GetName(), in.GetDescription(), sbi)
s.core.MarshallPNDs()
return &pb.CreatePNDReply{Message: "Created new PND: " + id.String()}, nil 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 ...@@ -172,7 +171,7 @@ func (s *server) GetAllPNDs(ctx context.Context, in *emptypb.Empty) (*pb.AllPNDs
var pnds []*pb.PND var pnds []*pb.PND
for uuidPND, pnd := range s.core.principalNetworkDomains { for uuidPND, pnd := range s.core.principalNetworkDomains {
var devices []*pb.Device var devices []*pb.Device
for uuidDevice, device := range pnd.(*pndImplementation).Devices { for uuidDevice, device := range pnd.(*pndImplementation).devices {
tmpDevice := pb.Device{ tmpDevice := pb.Device{
Uuid: uuidDevice.String(), Uuid: uuidDevice.String(),
Address: device.Config.Address, Address: device.Config.Address,
...@@ -230,9 +229,9 @@ func (s *server) AddDevice(ctx context.Context, in *pb.AddDeviceRequest) (*pb.Ad ...@@ -230,9 +229,9 @@ func (s *server) AddDevice(ctx context.Context, in *pb.AddDeviceRequest) (*pb.Ad
}, },
Transport: transport, Transport: transport,
} }
newDevice.GoStruct = sbi.Schema().Root
devicesMap := pnd.GetDevices() devicesMap := pnd.GetDevices()
devicesMap[newDevice.Config.Uuid] = &newDevice devicesMap[newDevice.Config.Uuid] = &newDevice
s.core.MarshallPNDs()
return &pb.AddDeviceReply{Message: "Added new Device: " + newDevice.Config.Uuid.String()}, err 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 ...@@ -262,11 +261,12 @@ func (s *server) HandleDeviceGetRequest(ctx context.Context, in *pb.DeviceGetReq
if err != nil { if err != nil {
return &pb.DeviceGetReply{Message: err.Error()}, err return &pb.DeviceGetReply{Message: err.Error()}, err
} }
log.Info(device)
d, err := json.MarshalIndent(device, "", "\t") d, err := json.MarshalIndent(device, "", "\t")
if err != nil { if err != nil {
return &pb.DeviceGetReply{Message: err.Error()}, err return &pb.DeviceGetReply{Message: err.Error()}, err
} }
return &pb.DeviceGetReply{Message: string(d)}, nil return &pb.DeviceGetReply{Message: string(string(d))}, nil
} }
package nucleus package nucleus
import ( import (
"encoding/json"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"code.fbi.h-da.de/cocsn/gosdn/database" "code.fbi.h-da.de/cocsn/gosdn/database"
"github.com/google/uuid"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
...@@ -14,7 +13,7 @@ import ( ...@@ -14,7 +13,7 @@ import (
// Core is the representation of the controllers core // Core is the representation of the controllers core
type Core struct { type Core struct {
southboundInterfaces map[string]SouthboundInterface southboundInterfaces map[string]SouthboundInterface
principalNetworkDomains PrincipalNetworkDomainMap principalNetworkDomains map[uuid.UUID]PrincipalNetworkDomain
database database.Database database database.Database
IsRunning chan bool IsRunning chan bool
} }
...@@ -39,11 +38,6 @@ func (c *Core) Initialize(IsRunningChannel chan bool) { ...@@ -39,11 +38,6 @@ func (c *Core) Initialize(IsRunningChannel chan bool) {
log.Info(sbi) log.Info(sbi)
} }
// err = c.UnMarshallPNDs()
// if err != nil {
// log.Info(err)
// }
c.IsRunning = IsRunningChannel c.IsRunning = IsRunningChannel
} }
...@@ -52,33 +46,6 @@ func (c *Core) AttachDatabase() { ...@@ -52,33 +46,6 @@ func (c *Core) AttachDatabase() {
c.database = database.NewDatabaseClient() 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 // CreateSouthboundInterfaces initializes the controller with his SBIs
func (c *Core) CreateSouthboundInterfaces() { func (c *Core) CreateSouthboundInterfaces() {
arista := &AristaOC{} arista := &AristaOC{}
......
...@@ -6,17 +6,17 @@ import ( ...@@ -6,17 +6,17 @@ import (
) )
type Device struct { type Device struct {
// Device inherits properies of ygot.GoStruct // Device inherits properties of ygot.GoStruct
ygot.GoStruct ygot.GoStruct
// SBI is the device's southbound interface implementation // SBI is the device's southbound interface implementation
SBI SouthboundInterface SBI SouthboundInterface `json:"-"`
// Config is the device's config. Under revision // Config is the device's config. Under revision
Config DeviceConfig Config DeviceConfig `json:"-"`
// Transport is the device's Transport implementation // Transport is the device's Transport implementation
Transport Transport Transport Transport `json:"-"`
} }
type DeviceConfig struct { type DeviceConfig struct {
......
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
}
...@@ -22,10 +22,10 @@ type PrincipalNetworkDomain interface { ...@@ -22,10 +22,10 @@ type PrincipalNetworkDomain interface {
} }
type pndImplementation struct { type pndImplementation struct {
Name string `json:"name"` name string
Description string `json:"description"` description string
Sbi SouthboundInterfaceMap `json:"sbis"` sbi map[string]SouthboundInterface
Devices map[uuid.UUID]*Device `json:"devices"` devices map[uuid.UUID]*Device
} }
//NewPND creates a Principle Network Domain //NewPND creates a Principle Network Domain
...@@ -34,27 +34,27 @@ func NewPND(name, description string, sbi SouthboundInterface) PrincipalNetworkD ...@@ -34,27 +34,27 @@ func NewPND(name, description string, sbi SouthboundInterface) PrincipalNetworkD
sbic["default"] = sbi sbic["default"] = sbi
devices := make(map[uuid.UUID]*Device) devices := make(map[uuid.UUID]*Device)
return &pndImplementation{ return &pndImplementation{
Name: name, name: name,
Description: description, description: description,
Sbi: sbic, sbi: sbic,
Devices: devices, devices: devices,
} }
} }
func (pnd *pndImplementation) GetName() string { func (pnd *pndImplementation) GetName() string {
return pnd.Name return pnd.name
} }
func (pnd *pndImplementation) GetDevices() map[uuid.UUID]*Device { func (pnd *pndImplementation) GetDevices() map[uuid.UUID]*Device {
return pnd.Devices return pnd.devices
} }
func (pnd *pndImplementation) GetDescription() string { func (pnd *pndImplementation) GetDescription() string {
return pnd.Description return pnd.description
} }
func (pnd *pndImplementation) GetSBIs() map[string]SouthboundInterface { func (pnd *pndImplementation) GetSBIs() map[string]SouthboundInterface {
return pnd.Sbi return pnd.sbi
} }
// Interface satisfaction // Interface satisfaction
...@@ -85,29 +85,33 @@ func destroy() error { ...@@ -85,29 +85,33 @@ func destroy() error {
} }
func (pnd *pndImplementation) addSbi(sbi SouthboundInterface) error { func (pnd *pndImplementation) addSbi(sbi SouthboundInterface) error {
pnd.Sbi[sbi.SbiIdentifier()] = sbi pnd.sbi[sbi.SbiIdentifier()] = sbi
return nil return nil
} }
func (pnd *pndImplementation) removeSbi(sbiIdentifier string) error { func (pnd *pndImplementation) removeSbi(sbiIdentifier string) error {
delete(pnd.Sbi, sbiIdentifier) delete(pnd.sbi, sbiIdentifier)
return nil return nil
} }
func (pnd *pndImplementation) addDevice(device *Device) error { func (pnd *pndImplementation) addDevice(device *Device) error {
pnd.Devices[device.Config.Uuid] = device pnd.devices[device.Config.Uuid] = device
return nil return nil
} }
func (pnd *pndImplementation) removeDevice(uuid uuid.UUID) error { func (pnd *pndImplementation) removeDevice(uuid uuid.UUID) error {
delete(pnd.Devices, uuid) delete(pnd.devices, uuid)
return nil return nil
} }
func (pnd *pndImplementation) Request(uuid uuid.UUID, path string) error { func (pnd *pndImplementation) Request(uuid uuid.UUID, path string) error {
d := pnd.Devices[uuid] d := pnd.devices[uuid]
ctx := context.Background() 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 { if err != nil {
return err return err
} }
...@@ -115,7 +119,7 @@ func (pnd *pndImplementation) Request(uuid uuid.UUID, path string) error { ...@@ -115,7 +119,7 @@ func (pnd *pndImplementation) Request(uuid uuid.UUID, path string) error {
} }
func (pnd *pndImplementation) RequestAll(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 { if err := pnd.Request(k, path); err != nil {
return err return err
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment