Open Kilda Java Documentation
DatabaseNeoImpl.java
Go to the documentation of this file.
1 /* Copyright 2018 Telstra Open Source
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package org.openkilda.testing.service.database;
17 
19 
20 import com.google.common.collect.ImmutableMap;
21 import org.neo4j.driver.v1.Driver;
22 import org.neo4j.driver.v1.GraphDatabase;
23 import org.neo4j.driver.v1.Session;
24 import org.neo4j.driver.v1.StatementResult;
25 import org.springframework.beans.factory.DisposableBean;
26 import org.springframework.beans.factory.annotation.Value;
27 import org.springframework.stereotype.Component;
28 
29 import java.util.HashMap;
30 import java.util.Map;
31 
32 @Component
33 public class DatabaseNeoImpl implements DisposableBean, Database {
34  private Driver neo;
35 
36  public DatabaseNeoImpl(@Value("${neo.uri}") String neoUri) {
37  neo = GraphDatabase.driver(neoUri);
38  }
39 
40  @Override
41  public void destroy() {
42  if (neo != null) {
43  neo.close();
44  }
45  }
46 
52  @Override
53  public boolean updateLinkProperty(Isl isl, String property, Object value) {
54  String query = "MATCH ()-[link:isl {src_port:$srcPort, dst_port:$dstPort, src_switch:$srcSwitch, "
55  + "dst_switch:$dstSwitch}]->() SET link += {props}";
56  Map<String, Object> params = getParams(isl);
57  params.put("props", ImmutableMap.of(property, value));
58  StatementResult result;
59  try (Session session = neo.session()) {
60  result = session.run(query, params);
61  }
62  return result.summary().counters().propertiesSet() > 0;
63  }
64 
71  @Override
72  public boolean revertIslBandwidth(Isl isl) {
73  String query = "MATCH ()-[link:isl {src_port:$srcPort, dst_port:$dstPort, src_switch:$srcSwitch, "
74  + "dst_switch:$dstSwitch}]->() SET link.max_bandwidth=link.speed, link.available_bandwidth=link.speed";
75  Map<String, Object> params = getParams(isl);
76  StatementResult result;
77  try (Session session = neo.session()) {
78  result = session.run(query, params);
79  }
80  return result.summary().counters().propertiesSet() > 0;
81  }
82 
83  private Map<String, Object> getParams(Isl isl) {
84  Map<String, Object> params = new HashMap<>(4);
85  params.put("srcPort", isl.getSrcPort());
86  params.put("dstPort", isl.getDstPort());
87  params.put("srcSwitch", isl.getSrcSwitch().getDpId().toString());
88  params.put("dstSwitch", isl.getDstSwitch().getDpId().toString());
89  return params;
90  }
91 }
value
Definition: nodes.py:62
list result
Definition: plan-d.py:72
DatabaseNeoImpl(@Value("${neo.uri}") String neoUri)
boolean updateLinkProperty(Isl isl, String property, Object value)