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
Branches
Tags
2 merge requests!43Resolve "neo4j",!18Develop
Pipeline #52469 passed with warnings
......@@ -7,8 +7,7 @@ import (
//Database is a database
type Database struct {
driver neo4j.Driver
session neo4j.Session
driver neo4j.Driver
}
//PND is a principle network domain
......@@ -21,11 +20,9 @@ type PND struct {
//NewDatabaseClient creates a database client
func NewDatabaseClient(uri, username, password string, encrypted bool) Database {
driver := createDriver(uri, username, password, encrypted)
session := createSession(driver)
return Database{
driver: driver,
session: session,
driver: driver,
}
}
......@@ -41,15 +38,22 @@ func createDriver(uri, username, password string, encrypted bool) neo4j.Driver {
)
if err != nil {
log.Info("failed creating database Driver:", err)
log.Info("failed creating database driver:", err)
}
return driver
}
//createSession creates a neo4j.Session
func createSession(driver neo4j.Driver) neo4j.Session {
sessionConfig := neo4j.SessionConfig{AccessMode: neo4j.AccessModeWrite}
func createSession(driver neo4j.Driver, write bool) neo4j.Session {
var sessionConfig neo4j.SessionConfig
if write {
sessionConfig = neo4j.SessionConfig{AccessMode: neo4j.AccessModeWrite}
} else {
sessionConfig = neo4j.SessionConfig{AccessMode: neo4j.AccessModeRead}
}
session, err := driver.NewSession(sessionConfig)
if err != nil {
......@@ -61,6 +65,9 @@ func createSession(driver neo4j.Driver) neo4j.Session {
//StorePND stores the given principle network domain
func (d Database) StorePND(pnd *PND) {
session := createSession(d.driver, true)
defer session.Close()
query :=
`
MERGE (pnd:PND {name: $name})
......@@ -68,7 +75,7 @@ func (d Database) StorePND(pnd *PND) {
pnd.southboundInterfaces = $southboundInterfaces
`
//refactor map[string]interface... in own function
_, err := d.session.Run(
_, err := session.Run(
query, map[string]interface{}{
"name": pnd.name,
"description": pnd.description,
......@@ -85,6 +92,12 @@ func (d Database) StorePND(pnd *PND) {
//RemovePND removes the given principle network domain by id.
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
//principle networt domain (PND). It is required for a node to belong to a PND.
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"}}
d.StorePND(&testPND)
session := createSession(d.driver, true)
defer session.Close()
query :=
`
WITH apoc.convert.fromJsonMap($stringToAdd)
......@@ -109,7 +125,7 @@ func (d Database) StoreNodes(json string) {
MERGE (device)-[:BELONGS_TO]->(pnd)
`
_, err := d.session.Run(
_, err := session.Run(
query, map[string]interface{}{
"stringToAdd": json,
"pnd": testPND.name,
......@@ -130,9 +146,11 @@ func (d Database) RemoveSingleNode(id string) {}
//StoreLinks stores the links between nodes
func (d Database) StoreLinks(json string) {
session := createSession(d.driver, true)
defer session.Close()
query :=
`
WITH apoc.convert.fromJsonMap($stringToAdd)
` WITH apoc.convert.fromJsonMap($stringToAdd)
AS value
UNWIND value.data as l
MATCH (d:Device), (d2:Device)
......@@ -143,7 +161,7 @@ func (d Database) StoreLinks(json string) {
RETURN rel
`
_, err := d.session.Run(
_, err := session.Run(
query, map[string]interface{}{
"stringToAdd": json,
})
......@@ -157,6 +175,9 @@ func (d Database) StoreLinks(json string) {
//StoreNodeEdgePoints stores the given node edge points (interfaces)
func (d Database) StoreNodeEdgePoints(json string) {
session := createSession(d.driver, true)
defer session.Close()
query :=
`
WITH apoc.convert.fromJsonMap($stringToAdd)
......@@ -168,7 +189,7 @@ func (d Database) StoreNodeEdgePoints(json string) {
interface.location = i.object_data.` + "`tapi-object-data`.name[1].value," + `
interface.` + "`containing-node` = i.object_data.`tapi-object-data`.`containing-node`"
_, err := d.session.Run(
_, err := session.Run(
query, map[string]interface{}{
"stringToAdd": json,
})
......@@ -177,7 +198,7 @@ func (d Database) StoreNodeEdgePoints(json string) {
log.Info("failed storing NodeEdgePoints into database:", err)
}
setNodeNodeEdgePointsRelation(d.session)
setNodeNodeEdgePointsRelation(session)
log.Info("successfully added NodeEdgePoints into database")
......@@ -205,12 +226,25 @@ func setNodeNodeEdgePointsRelation(session neo4j.Session) {
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
func (d Database) Shutdown() {
//TODO: add logger
if err := d.session.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