diff --git a/database/client.go b/database/client.go index 6d745bbb6014123fd774ae0cfc4605e6798cd17b..8b91dc3567bc46cd123104d2f5fd529f22e8c17d 100644 --- a/database/client.go +++ b/database/client.go @@ -7,7 +7,7 @@ import ( log "github.com/sirupsen/logrus" ) -//Database is a database +//Client is a database client type Client struct { driver neo4j.Driver tapiClient *tapiDatabaseClient @@ -91,10 +91,35 @@ func (c Client) StorePND(pnd *PND) 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) { 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: return nil, errors.New("unsupported DeviceType") diff --git a/database/tapiDatabaseClient.go b/database/tapiDatabaseClient.go index 4a0e343bec3d1d2f690a08c596e1fafab11651e0..e805edbb23080e366d8f58ccc11a105c9764db2a 100644 --- a/database/tapiDatabaseClient.go +++ b/database/tapiDatabaseClient.go @@ -20,7 +20,7 @@ func newTapiDatabaseClient(d neo4j.Driver) *tapiDatabaseClient { //storeNodeTxFunc transaction to store devices from a json. //relates them to a specific pnd id. //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) { var nodelist []neo4j.Node query := @@ -29,7 +29,7 @@ func storeNodeTxFunc(id, name string, pndId int64) neo4j.TransactionWork { ON CREATE SET device.name = $name WITH device MATCH (pnd:PND) - WHERE id(pnd) = $pndId + WHERE id(pnd) = $pndID MERGE (device)-[:BELONGS_TO]->(pnd) RETURN device ` @@ -37,7 +37,7 @@ func storeNodeTxFunc(id, name string, pndId int64) neo4j.TransactionWork { result, err := tx.Run(query, map[string]interface{}{ "id": id, "name": name, - "pndId": pndId, + "pndID": pndID, }) if err != nil { @@ -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 //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) { - //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() - +func (d tapiDatabaseClient) storeNode(id, name string, pndID int64) ([]neo4j.Node, error) { session := createSession(d.driver, true) defer session.Close() - result, err := session.WriteTransaction(storeNodeTxFunc(id, name, pndId)) + result, err := session.WriteTransaction(storeNodeTxFunc(id, name, pndID)) if err != nil { log.Info(err) } @@ -88,6 +84,7 @@ func storeLinkTxFunc(nep1Id, nep2Id string) neo4j.TransactionWork { WHERE d.id = $nep1Id AND d2.id = $nep2Id MERGE (d)-[r:connected]->(d2) + RETURN r ` result, err := tx.Run(query, map[string]interface{}{ @@ -113,7 +110,7 @@ func storeLinkTxFunc(nep1Id, nep2Id string) neo4j.TransactionWork { } //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) defer session.Close() @@ -124,7 +121,7 @@ func (d tapiDatabaseClient) StoreLink(nep1Id, nep2Id string) []neo4j.Relationshi 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. @@ -208,3 +205,43 @@ func (d tapiDatabaseClient) StoreNodeEdgePoints(json string) { 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() {} diff --git a/sbi/restconf/client/ciena/client.go b/sbi/restconf/client/ciena/client.go index 2fd4d737dfdb433936eba1905da55d0e1e7dfd16..9a61caad1b1777732aa9f0429f13cf2867730f89 100644 --- a/sbi/restconf/client/ciena/client.go +++ b/sbi/restconf/client/ciena/client.go @@ -89,9 +89,6 @@ func (c *MCPClient) GetConnections() error { // 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 func (c *MCPClient) GetLinks() error { - var nep1 string - var nep2 string - defer c.buffer.Reset() _, err := c.client.TapiTopologyCore.GetTapiCoreContextTopologyMcpBaseTopologyLink(nil) if err != nil { @@ -108,19 +105,7 @@ func (c *MCPClient) GetLinks() error { //lot of them. log.Info(err) } - log.Info(*dest.Uuid) - - 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) + c.database.StoreLink(dest) } log.Debug(c.buffer.Next(25)) @@ -137,7 +122,7 @@ func (c *MCPClient) GetNodes() error { } 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) @@ -149,8 +134,7 @@ func (c *MCPClient) GetNodes() error { //lot of them. log.Info(err) } - c.database.StoreDevice(dest, pndId) - // c.database.StoreNode(*dest.Uuid, *dest.Name["nativeName"].Value) + c.database.StoreDevice(dest, pndID) } log.Debug(c.buffer.Next(25))