diff --git a/database/client.go b/database/client.go index 5910b12c7a8b60c54e6089305b348312adfff59e..d1365a19f3d94763ae8cc360fd62c53f5de85314 100644 --- a/database/client.go +++ b/database/client.go @@ -119,33 +119,27 @@ func (d Database) GetNodesByLabel(label string) {} //GetNodeByID gets a specific node by ID. func (d Database) GetNodeByID(id string) {} -//storeNodesTxFunc transaction to store devices from a json. +//storeNodeTxFunc transaction to store devices from a json. //relates them to a specific pnd id. //returns a slice of added devices -func storeNodesTxFunc(json string, id int64) neo4j.TransactionWork { +func storeNodeTxFunc(id, name string, pndId int64) neo4j.TransactionWork { return func(tx neo4j.Transaction) (interface{}, error) { var nodelist []neo4j.Node query := ` - WITH apoc.convert.fromJsonMap($stringToAdd) - AS value - UNWIND value.data as d - MERGE (device:Device {id: d.object_id}) - ON CREATE SET device.nativeName = d.object_data.` + "`tapi-object-data`.name[0].value," + ` - device.deviceType = d.object_data.` + "`tapi-object-data`.name[1].value," + ` - device.serialNumber = d.object_data.` + "`tapi-object-data`.name[2].value," + ` - device.softwareVersion = d.object_data.` + "`tapi-object-data`.name[3].value," + ` - device.` + "`operational-state` = d.object_data.`tapi-object-data`.`operational-state`" + ` + MERGE (device:Device {id: $id}) + ON CREATE SET device.name = $name WITH device MATCH (pnd:PND) - WHERE id(pnd) = $pnd + WHERE id(pnd) = $pndId MERGE (device)-[:BELONGS_TO]->(pnd) RETURN device ` result, err := tx.Run(query, map[string]interface{}{ - "stringToAdd": json, - "pnd": id, + "id": id, + "name": name, + "pndId": pndId, }) if err != nil { @@ -165,17 +159,17 @@ func storeNodesTxFunc(json string, id int64) neo4j.TransactionWork { } } -//StoreNodes 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. -func (d Database) StoreNodes(json string) []neo4j.Node { +func (d Database) StoreNode(id, name string) []neo4j.Node { //TODO: remove this after testing and add own gRPC call for it testPND := PND{name: "test_PND", description: "very interesting", interfaces: []string{"TAPI", "RESTCONF"}} - pnd := d.StorePND(&testPND).Id() + pndId := d.StorePND(&testPND).Id() session := createSession(d.driver, true) defer session.Close() - result, err := session.WriteTransaction(storeNodesTxFunc(json, pnd)) + result, err := session.WriteTransaction(storeNodeTxFunc(id, name, pndId)) if err != nil { log.Info(err) } @@ -190,27 +184,31 @@ func (d Database) RemoveNodes(json string) {} //RemoveSingleNode removes the given node and their relationship by id. func (d Database) RemoveSingleNode(id string) {} -//storeLinksTxFunc transaction to store links from a json. +//storeLinkTxFunc transaction to store links from a json. //creates relation between different devices. //returns a slice of those created relations. -func storeLinksTxFunc(json string) neo4j.TransactionWork { +func storeLinkTxFunc(nep1Id, nep2Id string) neo4j.TransactionWork { return func(tx neo4j.Transaction) (interface{}, error) { var relationsList []neo4j.Relationship query := + // ` + // MATCH (d:Device), (d2:Device) + // WHERE d.id = $nep1Id + // AND d2.id = $nep2Id + // CALL apoc.merge.relationship(d,$layerQualifier,{},{}, d2,{}) + // YIELD rel + // RETURN rel + // ` ` - WITH apoc.convert.fromJsonMap($stringToAdd) - AS value - UNWIND value.data as l MATCH (d:Device), (d2:Device) - WHERE d.id = l.object_data.` + "`tapi-object-data`.`node-edge-point`[0].`node-uuid`" + ` - AND d2.id = l.object_data.` + "`tapi-object-data`.`node-edge-point`[1].`node-uuid`" + ` - CALL apoc.merge.relationship(d,l.object_data.` + "`tapi-object-data`.`layer-qualifier`,{},{}, d2,{})" + ` - YIELD rel - RETURN rel + WHERE d.id = $nep1Id + AND d2.id = $nep2Id + MERGE (d)-[r:connected]->(d2) ` result, err := tx.Run(query, map[string]interface{}{ - "stringToAdd": json, + "nep1Id": nep1Id, + "nep2Id": nep2Id, }) if err != nil { @@ -230,12 +228,12 @@ func storeLinksTxFunc(json string) neo4j.TransactionWork { } } -//StoreLinks stores the links between nodes -func (d Database) StoreLinks(json string) []neo4j.Relationship { +//StoreLink stores the links between nodes +func (d Database) StoreLink(nep1Id, nep2Id string) []neo4j.Relationship { session := createSession(d.driver, true) defer session.Close() - result, err := session.WriteTransaction(storeLinksTxFunc(json)) + result, err := session.WriteTransaction(storeLinkTxFunc(nep1Id, nep2Id)) if err != nil { log.Info(err) } diff --git a/sbi/restconf/client/ciena/client.go b/sbi/restconf/client/ciena/client.go index 4ff48a9460ca8e4185f0f57257a40f861f6ac7aa..82a1efe954af2999e87cfbd66f7ee2422fc12e8c 100644 --- a/sbi/restconf/client/ciena/client.go +++ b/sbi/restconf/client/ciena/client.go @@ -89,6 +89,9 @@ 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 { @@ -106,10 +109,20 @@ func (c *MCPClient) GetLinks() error { 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) } - // c.database.StoreLinks(c.buffer.String()) - // log.Debug(c.buffer.Next(25)) + log.Debug(c.buffer.Next(25)) return err } @@ -122,7 +135,7 @@ func (c *MCPClient) GetNodes() error { return err } - json := preformatJSON(c.buffer.String(), c.config.GjsonDefaultPath) + json := gjson.Get(c.buffer.String(), c.config.GjsonDefaultPath) for _, jsonEntry := range json.Array() { dest := &tapi.TapiCommon_Context_TopologyContext_Topology_Node{} @@ -132,11 +145,10 @@ func (c *MCPClient) GetNodes() error { //lot of them. log.Info(err) } - log.Info(*dest.Uuid) + c.database.StoreNode(*dest.Uuid, *dest.Name["nativeName"].Value) } - // c.database.StoreNodes(c.buffer.String()) - // log.Debug(c.buffer.Next(25)) + log.Debug(c.buffer.Next(25)) return err } @@ -200,7 +212,7 @@ func uppercaseJSONValues(jsonMap interface{}) interface{} { for k, v := range jsonMap { //TODO: maybe we can uppercase them too, but for now: //DO NOT uppercase uuid's - if strings.Contains(k, "uuid") { + if strings.Contains(k, "uuid") || strings.Contains(k, "name") { tmpInterfaceMap[k] = v } else { tmpInterfaceMap[k] = uppercaseJSONValues(v)