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

changed usage of sessions

since sessions should be considered as a container for a sequence of
transactions the usage of one single session for the whole database was
removed.
Now a session is created for each specific transaction (write/read) and
closed after the transaction is done.
parent c285ebf9
No related branches found
No related tags found
2 merge requests!43Resolve "neo4j",!18Develop
Pipeline #52469 passed with warnings
...@@ -7,8 +7,7 @@ import ( ...@@ -7,8 +7,7 @@ import (
//Database is a database //Database is a database
type Database struct { type Database struct {
driver neo4j.Driver driver neo4j.Driver
session neo4j.Session
} }
//PND is a principle network domain //PND is a principle network domain
...@@ -21,11 +20,9 @@ type PND struct { ...@@ -21,11 +20,9 @@ type PND struct {
//NewDatabaseClient creates a database client //NewDatabaseClient creates a database client
func NewDatabaseClient(uri, username, password string, encrypted bool) Database { func NewDatabaseClient(uri, username, password string, encrypted bool) Database {
driver := createDriver(uri, username, password, encrypted) driver := createDriver(uri, username, password, encrypted)
session := createSession(driver)
return Database{ return Database{
driver: driver, driver: driver,
session: session,
} }
} }
...@@ -41,15 +38,22 @@ func createDriver(uri, username, password string, encrypted bool) neo4j.Driver { ...@@ -41,15 +38,22 @@ func createDriver(uri, username, password string, encrypted bool) neo4j.Driver {
) )
if err != nil { if err != nil {
log.Info("failed creating database Driver:", err) log.Info("failed creating database driver:", err)
} }
return driver return driver
} }
//createSession creates a neo4j.Session //createSession creates a neo4j.Session
func createSession(driver neo4j.Driver) neo4j.Session { func createSession(driver neo4j.Driver, write bool) neo4j.Session {
sessionConfig := neo4j.SessionConfig{AccessMode: neo4j.AccessModeWrite} var sessionConfig neo4j.SessionConfig
if write {
sessionConfig = neo4j.SessionConfig{AccessMode: neo4j.AccessModeWrite}
} else {
sessionConfig = neo4j.SessionConfig{AccessMode: neo4j.AccessModeRead}
}
session, err := driver.NewSession(sessionConfig) session, err := driver.NewSession(sessionConfig)
if err != nil { if err != nil {
...@@ -61,6 +65,9 @@ func createSession(driver neo4j.Driver) neo4j.Session { ...@@ -61,6 +65,9 @@ func createSession(driver neo4j.Driver) neo4j.Session {
//StorePND stores the given principle network domain //StorePND stores the given principle network domain
func (d Database) StorePND(pnd *PND) { func (d Database) StorePND(pnd *PND) {
session := createSession(d.driver, true)
defer session.Close()
query := query :=
` `
MERGE (pnd:PND {name: $name}) MERGE (pnd:PND {name: $name})
...@@ -68,7 +75,7 @@ func (d Database) StorePND(pnd *PND) { ...@@ -68,7 +75,7 @@ func (d Database) StorePND(pnd *PND) {
pnd.southboundInterfaces = $southboundInterfaces pnd.southboundInterfaces = $southboundInterfaces
` `
//refactor map[string]interface... in own function //refactor map[string]interface... in own function
_, err := d.session.Run( _, err := session.Run(
query, map[string]interface{}{ query, map[string]interface{}{
"name": pnd.name, "name": pnd.name,
"description": pnd.description, "description": pnd.description,
...@@ -85,6 +92,12 @@ func (d Database) StorePND(pnd *PND) { ...@@ -85,6 +92,12 @@ func (d Database) StorePND(pnd *PND) {
//RemovePND removes the given principle network domain by id. //RemovePND removes the given principle network domain by id.
func (d Database) RemovePND(id string) {} func (d Database) RemovePND(id string) {}
func (d Database) GetPNDByID(id string) {}
func (d Database) GetNodesByLabel(label string) {}
func (d Database) GetDeviceByID(id string) {}
//StoreNodes stores the given nodes to the database and adds them to a //StoreNodes 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 Database) StoreNodes(json string) { func (d Database) StoreNodes(json string) {
...@@ -92,6 +105,9 @@ func (d Database) StoreNodes(json string) { ...@@ -92,6 +105,9 @@ func (d Database) StoreNodes(json string) {
testPND := PND{name: "test_PND", description: "very, interesting", southboundInterfaces: []string{"TAPI", "RESTCONF"}} testPND := PND{name: "test_PND", description: "very, interesting", southboundInterfaces: []string{"TAPI", "RESTCONF"}}
d.StorePND(&testPND) d.StorePND(&testPND)
session := createSession(d.driver, true)
defer session.Close()
query := query :=
` `
WITH apoc.convert.fromJsonMap($stringToAdd) WITH apoc.convert.fromJsonMap($stringToAdd)
...@@ -109,7 +125,7 @@ func (d Database) StoreNodes(json string) { ...@@ -109,7 +125,7 @@ func (d Database) StoreNodes(json string) {
MERGE (device)-[:BELONGS_TO]->(pnd) MERGE (device)-[:BELONGS_TO]->(pnd)
` `
_, err := d.session.Run( _, err := session.Run(
query, map[string]interface{}{ query, map[string]interface{}{
"stringToAdd": json, "stringToAdd": json,
"pnd": testPND.name, "pnd": testPND.name,
...@@ -130,9 +146,11 @@ func (d Database) RemoveSingleNode(id string) {} ...@@ -130,9 +146,11 @@ func (d Database) RemoveSingleNode(id string) {}
//StoreLinks stores the links between nodes //StoreLinks stores the links between nodes
func (d Database) StoreLinks(json string) { func (d Database) StoreLinks(json string) {
session := createSession(d.driver, true)
defer session.Close()
query := query :=
` ` WITH apoc.convert.fromJsonMap($stringToAdd)
WITH apoc.convert.fromJsonMap($stringToAdd)
AS value AS value
UNWIND value.data as l UNWIND value.data as l
MATCH (d:Device), (d2:Device) MATCH (d:Device), (d2:Device)
...@@ -143,7 +161,7 @@ func (d Database) StoreLinks(json string) { ...@@ -143,7 +161,7 @@ func (d Database) StoreLinks(json string) {
RETURN rel RETURN rel
` `
_, err := d.session.Run( _, err := session.Run(
query, map[string]interface{}{ query, map[string]interface{}{
"stringToAdd": json, "stringToAdd": json,
}) })
...@@ -157,6 +175,9 @@ func (d Database) StoreLinks(json string) { ...@@ -157,6 +175,9 @@ func (d Database) StoreLinks(json string) {
//StoreNodeEdgePoints stores the given node edge points (interfaces) //StoreNodeEdgePoints stores the given node edge points (interfaces)
func (d Database) StoreNodeEdgePoints(json string) { func (d Database) StoreNodeEdgePoints(json string) {
session := createSession(d.driver, true)
defer session.Close()
query := query :=
` `
WITH apoc.convert.fromJsonMap($stringToAdd) WITH apoc.convert.fromJsonMap($stringToAdd)
...@@ -168,7 +189,7 @@ func (d Database) StoreNodeEdgePoints(json string) { ...@@ -168,7 +189,7 @@ func (d Database) StoreNodeEdgePoints(json string) {
interface.location = i.object_data.` + "`tapi-object-data`.name[1].value," + ` interface.location = i.object_data.` + "`tapi-object-data`.name[1].value," + `
interface.` + "`containing-node` = i.object_data.`tapi-object-data`.`containing-node`" interface.` + "`containing-node` = i.object_data.`tapi-object-data`.`containing-node`"
_, err := d.session.Run( _, err := session.Run(
query, map[string]interface{}{ query, map[string]interface{}{
"stringToAdd": json, "stringToAdd": json,
}) })
...@@ -177,7 +198,7 @@ func (d Database) StoreNodeEdgePoints(json string) { ...@@ -177,7 +198,7 @@ func (d Database) StoreNodeEdgePoints(json string) {
log.Info("failed storing NodeEdgePoints into database:", err) log.Info("failed storing NodeEdgePoints into database:", err)
} }
setNodeNodeEdgePointsRelation(d.session) setNodeNodeEdgePointsRelation(session)
log.Info("successfully added NodeEdgePoints into database") log.Info("successfully added NodeEdgePoints into database")
...@@ -205,12 +226,25 @@ func setNodeNodeEdgePointsRelation(session neo4j.Session) { ...@@ -205,12 +226,25 @@ func setNodeNodeEdgePointsRelation(session neo4j.Session) {
log.Info("successfully stored NodeNodeEdgePointsRelation into database") log.Info("successfully stored NodeNodeEdgePointsRelation into database")
} }
//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() {}
//Shutdown closes the connection to the database //Shutdown closes the connection to the database
func (d Database) Shutdown() { func (d Database) Shutdown() {
//TODO: add logger
if err := d.session.Close(); err != nil {
}
if err := d.driver.Close(); err != nil { if err := d.driver.Close(); err != nil {
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment