Open Kilda Java Documentation
FlowAdapter.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.pce.api;
17 
23 
24 import org.neo4j.driver.v1.Record;
25 
26 import java.io.IOException;
27 
28 public class FlowAdapter {
29  private final Flow flow;
30 
31  public FlowAdapter(Record dbRecord) {
32  String pathJson = dbRecord.get("path").asString().trim();
33 
34  if (pathJson.equals("null")) {
35  pathJson = "{\"path\": [], \"latency_ns\": 0, \"timestamp\": 0}";
36  }
37 
38  /*
39  * The 'clazz' value is stripped when storing in the database, but we need it in the string
40  * in order for MAPPER to do its thing. So, let's add it back in at the very beginning.
41  */
42  String start = pathJson.substring(0, pathJson.length() - 1);
44  pathJson = start + ", \"clazz\":\"org.openkilda.messaging.info.event.PathInfoData\"}";
45 
46  try {
47  path = Utils.MAPPER.readValue(pathJson, PathInfoData.class);
48  } catch (IOException e) {
49  throw new IllegalArgumentException(String.format(
50  "Can\'t deserialize flow path: json=%s", pathJson), e);
51  }
52 
53  flow = new Flow(
54  dbRecord.get(Utils.FLOW_ID).asString(),
55  dbRecord.get("bandwidth").asInt(),
56  dbRecord.get("ignore_bandwidth").asBoolean(),
57  dbRecord.get("cookie").asLong(),
58  dbRecord.get("description").asString(),
59  dbRecord.get("last_updated").asString(),
60  new SwitchId(dbRecord.get("src_switch").asString()),
61  new SwitchId(dbRecord.get("dst_switch").asString()),
62  dbRecord.get("src_port").asInt(),
63  dbRecord.get("dst_port").asInt(),
64  dbRecord.get("src_vlan").asInt(),
65  dbRecord.get("dst_vlan").asInt(),
66  dbRecord.get("meter_id").asInt(),
67  dbRecord.get("transit_vlan").asInt(),
69  );
70  }
71 
72  public Flow getFlow() {
73  return flow;
74  }
75 }
static final ObjectMapper MAPPER
Definition: Utils.java:31
static final String FLOW_ID
Definition: Utils.java:61