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

added functionality to load backup.json for pnd

parent 1a8c30d4
No related branches found
No related tags found
3 merge requests!97Resolve "PND handling via CLI and database",!91"Overhaul Architecture",!90Develop
This commit is part of merge request !91. Comments created here will be created in the context of that merge request.
...@@ -160,9 +160,6 @@ func (s *server) TAPIGetLink(ctx context.Context, in *pb.TAPIRequest) (*pb.TAPIR ...@@ -160,9 +160,6 @@ func (s *server) TAPIGetLink(ctx context.Context, in *pb.TAPIRequest) (*pb.TAPIR
func (s *server) CreatePND(ctx context.Context, in *pb.CreatePNDRequest) (*pb.CreatePNDReply, error) { func (s *server) CreatePND(ctx context.Context, in *pb.CreatePNDRequest) (*pb.CreatePNDReply, error) {
log.Info("Received: Create a PND with the name", in.GetName()) log.Info("Received: Create a PND with the name", in.GetName())
sbi := s.core.southboundInterfaces[in.GetSbi()] sbi := s.core.southboundInterfaces[in.GetSbi()]
if sbi == nil {
log.Info("lul")
}
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)
//TODO: export //TODO: export
...@@ -226,7 +223,6 @@ func (s *server) AddDevice(ctx context.Context, in *pb.AddDeviceRequest) (*pb.Ad ...@@ -226,7 +223,6 @@ func (s *server) AddDevice(ctx context.Context, in *pb.AddDeviceRequest) (*pb.Ad
} }
devicesMap := pnd.GetDevices() devicesMap := pnd.GetDevices()
devicesMap[newDevice.Uuid] = newDevice devicesMap[newDevice.Uuid] = newDevice
//TODO: export
s.core.MarshallPNDs() s.core.MarshallPNDs()
return &pb.AddDeviceReply{Message: "Added new Device: " + newDevice.Uuid.String()}, err return &pb.AddDeviceReply{Message: "Added new Device: " + newDevice.Uuid.String()}, err
...@@ -269,5 +265,6 @@ func (s *server) HandleDeviceGetRequest(ctx context.Context, in *pb.DeviceGetReq ...@@ -269,5 +265,6 @@ func (s *server) HandleDeviceGetRequest(ctx context.Context, in *pb.DeviceGetReq
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(d)}, nil
} }
...@@ -7,7 +7,6 @@ import ( ...@@ -7,7 +7,6 @@ import (
"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"
) )
...@@ -15,7 +14,7 @@ import ( ...@@ -15,7 +14,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 map[uuid.UUID]PrincipalNetworkDomain principalNetworkDomains PrincipalNetworkDomainMap
database database.Database database database.Database
IsRunning chan bool IsRunning chan bool
} }
...@@ -37,6 +36,11 @@ func (c *Core) Initialize(IsRunningChannel chan bool) { ...@@ -37,6 +36,11 @@ func (c *Core) Initialize(IsRunningChannel chan bool) {
c.AttachDatabase() c.AttachDatabase()
c.CreateSouthboundInterfaces() c.CreateSouthboundInterfaces()
err = c.UnMarshallPNDs()
if err != nil {
log.Info(err)
}
c.IsRunning = IsRunningChannel c.IsRunning = IsRunningChannel
} }
...@@ -57,6 +61,21 @@ func (c *Core) MarshallPNDs() error { ...@@ -57,6 +61,21 @@ func (c *Core) MarshallPNDs() error {
return nil 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() {
if len(c.southboundInterfaces) == 0 { if len(c.southboundInterfaces) == 0 {
......
package nucleus package nucleus
import ( import (
"encoding/json"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/openconfig/ygot/ygot" "github.com/openconfig/ygot/ygot"
) )
type Device struct { type Device struct {
Uuid uuid.UUID `json:"id"` Uuid uuid.UUID `json:"id"`
Device ygot.GoStruct `json:"device"` Device ygot.GoStruct `json:"-"`
SBI SouthboundInterface `json:"sbi"` SBI SouthboundInterface `json:"sbi"`
Config DeviceConfig `json:"config"` Config DeviceConfig `json:"config"`
Transport Transport `json:"-"` Transport Transport `json:"-"`
...@@ -16,12 +17,41 @@ type Device struct { ...@@ -16,12 +17,41 @@ type Device struct {
// Add adds a property to a device. Please // Add adds a property to a device. Please
// use better naming in further develop // use better naming in further develop
// Also all that Interface Call specific logic belongs to SBI! // Also all that Interface Call specific logic belongs to SBI!
func (d Device) Add(resp interface{}) error { func (d *Device) Add(resp interface{}) error {
return d.Transport.ProcessResponse(resp, d.Device, d.SBI.Schema()) return d.Transport.ProcessResponse(resp, d.Device, d.SBI.Schema())
} }
func (d *Device) UnmarshalJSON(b []byte) error {
data := make(map[string]interface{})
if err := json.Unmarshal(b, &data); err != nil {
return err
}
id, err := uuid.Parse(data["id"].(string))
if err != nil {
return err
}
device := Device{
Uuid: id,
Device: nil,
SBI: getSouthboundInterfaceFromJSONMap(data["sbi"].(map[string]interface{})),
Config: DeviceConfig{
Address: data["config"].(map[string]interface{})["address"].(string),
Username: data["config"].(map[string]interface{})["username"].(string),
Password: data["config"].(map[string]interface{})["password"].(string),
},
Transport: nil,
}
device.SBI.SetDefaults()
device.Transport = device.SBI.GetTransport()
device.Device = device.SBI.Schema().Root
*d = device
return nil
}
type DeviceConfig struct { type DeviceConfig struct {
Address string Address string `json:"address"`
Username string Username string `json:"username"`
Password string Password string `json:"password"`
} }
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{}
newSBI.SetDefaults()
case "openconfig":
newSBI = &OpenConfig{}
newSBI.SetDefaults()
}
return newSBI
}
...@@ -19,10 +19,10 @@ type PrincipalNetworkDomain interface { ...@@ -19,10 +19,10 @@ type PrincipalNetworkDomain interface {
} }
type pndImplementation struct { type pndImplementation struct {
Name string `json:"name"` Name string `json:"name"`
Description string `json:"description"` Description string `json:"description"`
Sbi map[string]SouthboundInterface `json:"sbis"` Sbi SouthboundInterfaceMap `json:"sbis"`
Devices map[uuid.UUID]Device `json:"devices"` Devices map[uuid.UUID]Device `json:"devices"`
} }
//NewPND creates a Principle Network Domain //NewPND creates a Principle Network Domain
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment