Open Kilda Java Documentation
SwitchOperationsBolt.java
Go to the documentation of this file.
1 /* Copyright 2017 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.wfm.topology.nbworker.bolts;
17 
25 
26 import org.apache.storm.topology.OutputFieldsDeclarer;
27 import org.apache.storm.tuple.Fields;
28 import org.apache.storm.tuple.Tuple;
29 import org.neo4j.driver.v1.Record;
30 import org.neo4j.driver.v1.Session;
31 import org.neo4j.driver.v1.StatementResult;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 
35 import java.util.ArrayList;
36 import java.util.List;
37 
39 
40  private static final Logger LOGGER = LoggerFactory.getLogger(SwitchOperationsBolt.class);
41 
42  public SwitchOperationsBolt(Auth neoAuth) {
43  super(neoAuth);
44  }
45 
46  @Override
47  List<? extends InfoData> processRequest(Tuple tuple, BaseRequest request, Session session) {
48  List<? extends InfoData> result = null;
49  if (request instanceof GetSwitchesRequest) {
50  result = getSwitches(session);
51  } else {
52  unhandledInput(tuple);
53  }
54 
55  return result;
56  }
57 
58  private List<SwitchInfoData> getSwitches(Session session) {
59  StatementResult result = session.run("MATCH (sw:switch) "
60  + "RETURN "
61  + "sw.name as name, "
62  + "sw.address as address, "
63  + "sw.hostname as hostname, "
64  + "sw.description as description, "
65  + "sw.controller as controller, "
66  + "sw.state as state");
67  List<SwitchInfoData> results = new ArrayList<>();
68  for (Record record : result.list()) {
69  SwitchInfoData sw = new SwitchInfoData();
70  sw.setSwitchId(new SwitchId(record.get("name").asString()));
71  sw.setAddress(record.get("address").asString());
72  sw.setController(record.get("controller").asString());
73  sw.setDescription(record.get("description").asString());
74  sw.setHostname(record.get("hostname").asString());
75 
76  String status = record.get("state").asString();
78  sw.setState(st);
79 
80  results.add(sw);
81  }
82  LOGGER.debug("Found switches: {}", results.size());
83 
84  return results;
85  }
86 
87  @Override
88  public void declareOutputFields(OutputFieldsDeclarer declarer) {
89  declarer.declare(new Fields("response", "correlationId"));
90  }
91 
92  @Override
93  Logger getLogger() {
94  return LOGGER;
95  }
96 }
def status()
Definition: rest.py:593
list result
Definition: plan-d.py:72
void unhandledInput(Tuple input)