Open Kilda Java Documentation
LinksConverter.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.converters;
17 
18 import static java.lang.String.format;
19 
27 
28 import org.apache.commons.lang3.math.NumberUtils;
29 import org.neo4j.driver.v1.Value;
30 import org.neo4j.driver.v1.types.Node;
31 import org.neo4j.driver.v1.types.Relationship;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 
35 import java.util.ArrayList;
36 import java.util.List;
37 import java.util.Map;
38 import java.util.Optional;
39 import java.util.stream.Collectors;
40 
41 public final class LinksConverter {
42 
43  private static final String SRC_SWITCH_FIELD = "src_switch";
44  private static final String SRC_PORT_FIELD = "src_port";
45  private static final String DST_SWITCH_FIELD = "dst_switch";
46  private static final String DST_PORT_FIELD = "dst_port";
47  private static final String CREATED_FIELD = "time_create";
48  private static final String MODIFIED_FIELD = "time_modify";
49 
50  private static final Logger logger = LoggerFactory.getLogger(LinksConverter.class);
51 
57  public static IslInfoData toIslInfoData(Relationship relationship) {
58  // max_bandwidth not used in IslInfoData
59  PathNode src = new PathNode();
60  src.setSwitchId(new SwitchId(relationship.get(SRC_SWITCH_FIELD).asString()));
61  src.setPortNo(parseIntValue(relationship.get(SRC_PORT_FIELD)));
62  src.setSegLatency(parseIntValue(relationship.get("latency")));
63  src.setSeqId(0);
64 
65  PathNode dst = new PathNode();
66  dst.setSwitchId(new SwitchId(relationship.get(DST_SWITCH_FIELD).asString()));
67  dst.setPortNo(parseIntValue(relationship.get(DST_PORT_FIELD)));
68  dst.setSegLatency(parseIntValue(relationship.get("latency")));
69  dst.setSeqId(1);
70 
71  List<PathNode> pathNodes = new ArrayList<>();
72  pathNodes.add(src);
73  pathNodes.add(dst);
74 
75  IslChangeType state = getStatus(relationship.get("status").asString());
76  IslInfoData isl = new IslInfoData(
77  parseIntValue(relationship.get("latency")),
78  pathNodes,
79  parseIntValue(relationship.get("speed")),
80  state,
81  parseIntValue(relationship.get("available_bandwidth"))
82  );
83  isl.setTimestamp(System.currentTimeMillis());
84 
85  return isl;
86  }
87 
94  public static LinkPropsData toLinkPropsData(Node node) {
95  // create mutable map from immutable returned by neo4j to be able to delete items from it.
96  Map<String, String> properties = node.asMap().entrySet()
97  .stream()
98  .collect(Collectors.toMap(Map.Entry::getKey, el -> String.valueOf(el.getValue())));
99 
100  // remove known fields to leave only props in that map
101  SwitchId srcSwitch = new SwitchId(properties.remove(SRC_SWITCH_FIELD));
102  int srcPort = NumberUtils.toInt(properties.remove(SRC_PORT_FIELD));
103  SwitchId dstSwitch = new SwitchId(properties.remove(DST_SWITCH_FIELD));
104  int dstPort = NumberUtils.toInt(properties.remove(DST_PORT_FIELD));
105 
106  // remove created/updated fields as they aren't part of link properties
107  properties.remove(CREATED_FIELD);
108  properties.remove(MODIFIED_FIELD);
109 
110  LinkProps linkProps = new LinkProps(new NetworkEndpoint(srcSwitch, srcPort),
111  new NetworkEndpoint(dstSwitch, dstPort), properties);
112  return new LinkPropsData(linkProps);
113  }
114 
115  private static int parseIntValue(Value value) {
116  int intValue = 0;
117  try {
118  intValue = Optional.ofNullable(value)
119  .filter(val -> !val.isNull())
120  .map(Value::asObject)
121  .map(Object::toString)
122  .filter(NumberUtils::isCreatable)
123  .map(Integer::parseInt)
124  .orElse(0);
125  } catch (Exception e) {
126  logger.warn(format("Exception trying to get an Integer; the String isn't parseable. Value: %s", value), e);
127  }
128  return intValue;
129  }
130 
131  private static IslChangeType getStatus(String status) {
132  switch (status) {
133  case "active":
134  return IslChangeType.DISCOVERED;
135  case "inactive":
136  return IslChangeType.FAILED;
137  case "moved":
138  return IslChangeType.MOVED;
139  default:
140  logger.warn("Found incorrect ISL status: {}", status);
141  return IslChangeType.FAILED;
142  }
143  }
144 
145  private LinksConverter() {
146  }
147 
148 }
value
Definition: nodes.py:62
def status()
Definition: rest.py:593
void setSegLatency(final long latency)
Definition: PathNode.java:207
void setSwitchId(final SwitchId switchId)
Definition: PathNode.java:147