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

nodes and links can be stored

using the facade pattern for the database
parent f37d49c8
No related branches found
No related tags found
1 merge request!81Draft: Resolve "update database for ygot"
Pipeline #56130 passed
...@@ -7,7 +7,7 @@ import ( ...@@ -7,7 +7,7 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
//Database is a database //Client is a database client
type Client struct { type Client struct {
driver neo4j.Driver driver neo4j.Driver
tapiClient *tapiDatabaseClient tapiClient *tapiDatabaseClient
...@@ -91,10 +91,35 @@ func (c Client) StorePND(pnd *PND) neo4j.Node { ...@@ -91,10 +91,35 @@ func (c Client) StorePND(pnd *PND) neo4j.Node {
return result.(neo4j.Node) return result.(neo4j.Node)
} }
func (c Client) StoreDevice(device interface{}, pndId int64) ([]neo4j.Node, error) { //StoreDevice stores a network device into the neo4j database as a node
func (c Client) StoreDevice(device interface{}, pndID int64) ([]neo4j.Node, error) {
switch device := device.(type) { switch device := device.(type) {
case *tapi.TapiCommon_Context_TopologyContext_Topology_Node: case *tapi.TapiCommon_Context_TopologyContext_Topology_Node:
return c.tapiClient.StoreNode(*device.Uuid, *device.Name["nativeName"].Value, pndId) return c.tapiClient.storeNode(*device.Uuid, *device.Name["nativeName"].Value, pndID)
default:
return nil, errors.New("unsupported DeviceType")
}
}
//StoreLink stores a link between network devices into the neo4j database as a
//relationship
func (c Client) StoreLink(device interface{}) ([]neo4j.Relationship, error) {
switch device := device.(type) {
case *tapi.TapiCommon_Context_TopologyContext_Topology_Link:
var nep1 string
var nep2 string
count := 0
for _, nep := range device.NodeEdgePoint {
if count == 0 {
nep1 = *nep.NodeUuid
} else {
nep2 = *nep.NodeUuid
}
count++
}
return c.tapiClient.storeLink(nep1, nep2)
default: default:
return nil, errors.New("unsupported DeviceType") return nil, errors.New("unsupported DeviceType")
......
...@@ -20,7 +20,7 @@ func newTapiDatabaseClient(d neo4j.Driver) *tapiDatabaseClient { ...@@ -20,7 +20,7 @@ func newTapiDatabaseClient(d neo4j.Driver) *tapiDatabaseClient {
//storeNodeTxFunc transaction to store devices from a json. //storeNodeTxFunc transaction to store devices from a json.
//relates them to a specific pnd id. //relates them to a specific pnd id.
//returns a slice of added devices //returns a slice of added devices
func storeNodeTxFunc(id, name string, pndId int64) neo4j.TransactionWork { func storeNodeTxFunc(id, name string, pndID int64) neo4j.TransactionWork {
return func(tx neo4j.Transaction) (interface{}, error) { return func(tx neo4j.Transaction) (interface{}, error) {
var nodelist []neo4j.Node var nodelist []neo4j.Node
query := query :=
...@@ -29,7 +29,7 @@ func storeNodeTxFunc(id, name string, pndId int64) neo4j.TransactionWork { ...@@ -29,7 +29,7 @@ func storeNodeTxFunc(id, name string, pndId int64) neo4j.TransactionWork {
ON CREATE SET device.name = $name ON CREATE SET device.name = $name
WITH device WITH device
MATCH (pnd:PND) MATCH (pnd:PND)
WHERE id(pnd) = $pndId WHERE id(pnd) = $pndID
MERGE (device)-[:BELONGS_TO]->(pnd) MERGE (device)-[:BELONGS_TO]->(pnd)
RETURN device RETURN device
` `
...@@ -37,7 +37,7 @@ func storeNodeTxFunc(id, name string, pndId int64) neo4j.TransactionWork { ...@@ -37,7 +37,7 @@ func storeNodeTxFunc(id, name string, pndId int64) neo4j.TransactionWork {
result, err := tx.Run(query, map[string]interface{}{ result, err := tx.Run(query, map[string]interface{}{
"id": id, "id": id,
"name": name, "name": name,
"pndId": pndId, "pndID": pndID,
}) })
if err != nil { if err != nil {
...@@ -59,15 +59,11 @@ func storeNodeTxFunc(id, name string, pndId int64) neo4j.TransactionWork { ...@@ -59,15 +59,11 @@ func storeNodeTxFunc(id, name string, pndId int64) neo4j.TransactionWork {
//StoreNode stores the given nodes to the database and adds them to a //StoreNode stores the given nodes to the database and adds them to a
//principle networt domain (PND). It is required for a node to belong to a PND. //principle networt domain (PND). It is required for a node to belong to a PND.
func (d tapiDatabaseClient) StoreNode(id, name string, pndId int64) ([]neo4j.Node, error) { func (d tapiDatabaseClient) storeNode(id, name string, pndID int64) ([]neo4j.Node, error) {
//TODO: remove this after testing and add own gRPC call for it
// testPND := PND{name: "test_PND", description: "very interesting", interfaces: []string{"TAPI", "RESTCONF"}}
// pndId := d.StorePND(&testPND).Id()
session := createSession(d.driver, true) session := createSession(d.driver, true)
defer session.Close() defer session.Close()
result, err := session.WriteTransaction(storeNodeTxFunc(id, name, pndId)) result, err := session.WriteTransaction(storeNodeTxFunc(id, name, pndID))
if err != nil { if err != nil {
log.Info(err) log.Info(err)
} }
...@@ -88,6 +84,7 @@ func storeLinkTxFunc(nep1Id, nep2Id string) neo4j.TransactionWork { ...@@ -88,6 +84,7 @@ func storeLinkTxFunc(nep1Id, nep2Id string) neo4j.TransactionWork {
WHERE d.id = $nep1Id WHERE d.id = $nep1Id
AND d2.id = $nep2Id AND d2.id = $nep2Id
MERGE (d)-[r:connected]->(d2) MERGE (d)-[r:connected]->(d2)
RETURN r
` `
result, err := tx.Run(query, map[string]interface{}{ result, err := tx.Run(query, map[string]interface{}{
...@@ -113,7 +110,7 @@ func storeLinkTxFunc(nep1Id, nep2Id string) neo4j.TransactionWork { ...@@ -113,7 +110,7 @@ func storeLinkTxFunc(nep1Id, nep2Id string) neo4j.TransactionWork {
} }
//StoreLink stores the links between nodes //StoreLink stores the links between nodes
func (d tapiDatabaseClient) StoreLink(nep1Id, nep2Id string) []neo4j.Relationship { func (d tapiDatabaseClient) storeLink(nep1Id, nep2Id string) ([]neo4j.Relationship, error) {
session := createSession(d.driver, true) session := createSession(d.driver, true)
defer session.Close() defer session.Close()
...@@ -124,7 +121,7 @@ func (d tapiDatabaseClient) StoreLink(nep1Id, nep2Id string) []neo4j.Relationshi ...@@ -124,7 +121,7 @@ func (d tapiDatabaseClient) StoreLink(nep1Id, nep2Id string) []neo4j.Relationshi
log.Info("added/updated links (count): ", len(result.([]neo4j.Relationship))) log.Info("added/updated links (count): ", len(result.([]neo4j.Relationship)))
return result.([]neo4j.Relationship) return result.([]neo4j.Relationship), err
} }
//storeNodeEdgePointsTxFunc transaction to store interfaces from a json. //storeNodeEdgePointsTxFunc transaction to store interfaces from a json.
...@@ -208,3 +205,43 @@ func (d tapiDatabaseClient) StoreNodeEdgePoints(json string) { ...@@ -208,3 +205,43 @@ func (d tapiDatabaseClient) StoreNodeEdgePoints(json string) {
log.Info("added/updated nodeEdgePoints (count): ", result) log.Info("added/updated nodeEdgePoints (count): ", result)
} }
//TODO: not implemented yet
//RemovePND removes the given principle network domain by id.
func (d tapiDatabaseClient) RemovePND(id string) {}
//GetPNDByID gets a specific PND by the given ID.
func (d tapiDatabaseClient) GetPNDByID(id string) {}
//GetNodesByLabel gets all nodes that belong to a specific label.
func (d tapiDatabaseClient) GetNodesByLabel(label string) {}
//GetNodeByID gets a specific node by ID.
func (d tapiDatabaseClient) GetNodeByID(id string) {}
//RemoveNodes removes the given nodes and their relationships
func (d tapiDatabaseClient) RemoveNodes(json string) {}
//RemoveSingleNode removes the given node and their relationship by id.
func (d tapiDatabaseClient) RemoveSingleNode(id string) {}
//StoreConnections stores relations between nodes
func (d tapiDatabaseClient) StoreConnections(json string) {}
//StoreTopology creates a new network topology node. Can also create a relation
//the new node and a existing one if desired
func StoreTopology() {}
//RemoveTopology removes the given network topology. This includes the node itself
//aswell as the containing links and relations
func RemoveTopology() {}
//CreateTopologyRelation creates a relation between two given topologies
func CreateTopologyRelation() {}
//CreateLink creates a link between two network elements
func CreateLink() {}
//RemoveLink removes a link between two network elements
func RemoveLink() {}
...@@ -89,9 +89,6 @@ func (c *MCPClient) GetConnections() error { ...@@ -89,9 +89,6 @@ func (c *MCPClient) GetConnections() error {
// GetLinks implements the TAPI Topology GetLinks call with a grain of // GetLinks implements the TAPI Topology GetLinks call with a grain of
// Ciena salt. The response is written to the client's buffer and passed to the database // Ciena salt. The response is written to the client's buffer and passed to the database
func (c *MCPClient) GetLinks() error { func (c *MCPClient) GetLinks() error {
var nep1 string
var nep2 string
defer c.buffer.Reset() defer c.buffer.Reset()
_, err := c.client.TapiTopologyCore.GetTapiCoreContextTopologyMcpBaseTopologyLink(nil) _, err := c.client.TapiTopologyCore.GetTapiCoreContextTopologyMcpBaseTopologyLink(nil)
if err != nil { if err != nil {
...@@ -108,19 +105,7 @@ func (c *MCPClient) GetLinks() error { ...@@ -108,19 +105,7 @@ func (c *MCPClient) GetLinks() error {
//lot of them. //lot of them.
log.Info(err) log.Info(err)
} }
log.Info(*dest.Uuid) c.database.StoreLink(dest)
count := 0
for _, nep := range dest.NodeEdgePoint {
if count == 0 {
nep1 = *nep.NodeUuid
} else {
nep2 = *nep.NodeUuid
}
count++
}
//c.database.StoreLink(nep1, nep2)
log.Info(nep1, nep2)
} }
log.Debug(c.buffer.Next(25)) log.Debug(c.buffer.Next(25))
...@@ -137,7 +122,7 @@ func (c *MCPClient) GetNodes() error { ...@@ -137,7 +122,7 @@ func (c *MCPClient) GetNodes() error {
} }
testPND := database.PND{Name: "test_PND", Description: "very interesting", Interfaces: []string{"TAPI", "RESTCONF"}} testPND := database.PND{Name: "test_PND", Description: "very interesting", Interfaces: []string{"TAPI", "RESTCONF"}}
pndId := c.database.StorePND(&testPND).Id() pndID := c.database.StorePND(&testPND).Id()
json := gjson.Get(c.buffer.String(), c.config.GjsonDefaultPath) json := gjson.Get(c.buffer.String(), c.config.GjsonDefaultPath)
...@@ -149,8 +134,7 @@ func (c *MCPClient) GetNodes() error { ...@@ -149,8 +134,7 @@ func (c *MCPClient) GetNodes() error {
//lot of them. //lot of them.
log.Info(err) log.Info(err)
} }
c.database.StoreDevice(dest, pndId) c.database.StoreDevice(dest, pndID)
// c.database.StoreNode(*dest.Uuid, *dest.Name["nativeName"].Value)
} }
log.Debug(c.buffer.Next(25)) log.Debug(c.buffer.Next(25))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment