Open Kilda Java Documentation
PortConverter.java
Go to the documentation of this file.
1 package org.openkilda.integration.converter;
2 
3 import com.fasterxml.jackson.core.JsonParseException;
4 import com.fasterxml.jackson.databind.JsonMappingException;
5 
6 import java.io.IOException;
7 import java.util.ArrayList;
8 import java.util.Collections;
9 import java.util.List;
10 
11 import org.json.simple.JSONObject;
12 import org.json.simple.JSONValue;
17 
18 public final class PortConverter {
19 
20  private PortConverter() {}
21 
22  public static List<PortInfo> toPortsInfo(final JSONObject jsonObject, final String switchId)
23  throws JsonParseException, JsonMappingException, IOException {
24  List<PortInfo> ports = new ArrayList<PortInfo>();
25  if (jsonObject != null) {
26  Object object = jsonObject.get(switchId);
27  if (object != null) {
28  String val = JSONValue.toJSONString(object);
29  PortsDetail portsDetail =
30  JsonUtil.toObject(val, PortsDetail.class);
31 
32  List<PortDetail> portDetailList = portsDetail.getPortDetail();
33  if (portDetailList != null && !portDetailList.isEmpty()) {
34  ports = getPortsInfo(portDetailList, switchId, ports);
35  }
36  }
37  }
38  return ports;
39  }
40 
41  private static List<PortInfo> getPortsInfo(final List<PortDetail> portsDetail,
42  final String key, final List<PortInfo> switchPortsInfoList) {
43  for (PortDetail portDetail : portsDetail) {
44 
45  if (!portDetail.getPortNumber().equalsIgnoreCase("local")) {
46  PortInfo info = setPortInfo(key, portDetail);
47  switchPortsInfoList.add(info);
48  }
49 
50  }
51  Collections.sort(switchPortsInfoList);
52  return switchPortsInfoList;
53  }
54 
55  private static PortInfo setPortInfo(final String key, final PortDetail portDetail) {
56  PortInfo portInfo = new PortInfo();
57  StringBuilder currentFeatures = new StringBuilder();
58 
59  if (portDetail.getCurrentFeatures() != null
60  && portDetail.getCurrentFeatures().size() > 0) {
61 
62  for (int i = 0; i < portDetail.getCurrentFeatures().size(); i++) {
63  if (currentFeatures.length() == 0) {
64  currentFeatures = currentFeatures.append(portDetail
65  .getCurrentFeatures().get(i));
66  } else {
67  currentFeatures = currentFeatures.append(","+portDetail
68  .getCurrentFeatures().get(i));
69  }
70  }
71  portInfo.setInterfacetype(currentFeatures.toString());
72  }
73  portInfo.setSwitchName(key);
74  portInfo.setPortNumber(portDetail.getPortNumber());
75  portInfo.setPortName(portDetail.getName());
76  if (portDetail.getState() != null && !portDetail.getState().isEmpty()) {
77  portInfo.setStatus(portDetail.getState().get(0));
78  }
79 
80  return portInfo;
81  }
82 }
static List< PortInfo > toPortsInfo(final JSONObject jsonObject, final String switchId)
static< T > T toObject(final String data, final Class< T > objClass)
Definition: JsonUtil.java:29