blob: 536e91e0b23e0d04b12a257d7d8aea03120e4fd5 [file] [log] [blame]
Thomas Vachuska18867bf2014-10-27 19:56:15 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19package org.onlab.onos.oecfg;
20
21import com.fasterxml.jackson.databind.JsonNode;
22import com.fasterxml.jackson.databind.ObjectMapper;
23import com.fasterxml.jackson.databind.node.ArrayNode;
24import com.fasterxml.jackson.databind.node.ObjectNode;
25
26import java.io.IOException;
27import java.io.InputStream;
28import java.util.HashMap;
29import java.util.Map;
30
31/**
32 * Utility program to convert standard ONOS config JSON to format expected
33 * by the OE Link switch.
34 */
35public final class OELinkConfig {
36
37 private ObjectMapper mapper = new ObjectMapper();
38 private Map<String, String> dpidToName = new HashMap<>();
39
40 public static void main(String[] args) {
41 try {
42 OELinkConfig config = new OELinkConfig();
43 JsonNode json = config.convert(System.in);
44 System.out.println(json.toString());
45 } catch (IOException e) {
46 System.err.println("Unable to convert JSON due to: " + e.getMessage());
47 e.printStackTrace();
48 }
49 }
50
51 private OELinkConfig() {
52 }
53
54 private JsonNode convert(InputStream input) throws IOException {
55 JsonNode json = mapper.readTree(input);
56 ObjectNode result = mapper.createObjectNode();
57 result.set("opticalSwitches", opticalSwitches(json));
58 result.set("opticalLinks", opticalLinks(json));
59 return result;
60 }
61
62 private JsonNode opticalSwitches(JsonNode json) {
63 ArrayNode result = mapper.createArrayNode();
64 for (JsonNode node : json.get("devices")) {
65 String dpid = dpid(node.path("uri"));
66 String name = node.path("name").asText("none");
67 dpidToName.put(dpid, name);
68 if (node.path("type").asText("none").equals("ROADM")) {
69 result.add(opticalSwitch(dpid, name, (ObjectNode) node));
70 }
71 }
72 return result;
73 }
74
75 private ObjectNode opticalSwitch(String dpid, String name, ObjectNode node) {
76 ObjectNode result = mapper.createObjectNode();
77 ObjectNode annot = (ObjectNode) node.path("annotations");
78 result.put("allowed", true).put("type", "Roadm")
79 .put("name", name).put("nodeDpid", dpid)
80 .put("latitude", annot.path("latitude").asDouble(0.0))
81 .put("longitude", annot.path("longitude").asDouble(0.0))
82 .set("params", switchParams(annot));
83 return result;
84 }
85
86 private ObjectNode switchParams(ObjectNode annot) {
87 return mapper.createObjectNode()
88 .put("numRegen", annot.path("optical.regens").asInt(0));
89 }
90
91 private JsonNode opticalLinks(JsonNode json) {
92 ArrayNode result = mapper.createArrayNode();
93 for (JsonNode node : json.get("links")) {
94 if (node.path("type").asText("none").equals("OPTICAL")) {
95 result.add(opticalLink((ObjectNode) node));
96 }
97 }
98 return result;
99 }
100
101 private ObjectNode opticalLink(ObjectNode node) {
102 ObjectNode result = mapper.createObjectNode();
103 ObjectNode annot = (ObjectNode) node.path("annotations");
104 String src = dpid(node.path("src"));
105 String dst = dpid(node.path("dst"));
106 result.put("allowed", true).put("type", linkType(annot))
107 .put("nodeDpid1", src).put("nodeDpid2", dst)
108 .set("params", linkParams(src, dst, node, annot));
109 return result;
110 }
111
112 private String linkType(ObjectNode annot) {
113 return annot.path("optical.type").asText("cross-connect").equals("WDM") ?
114 "wdmLink" : "pktOptLink";
115 }
116
117 private ObjectNode linkParams(String src, String dst,
118 ObjectNode node, ObjectNode annot) {
119 ObjectNode result = mapper.createObjectNode()
120 .put("nodeName1", dpidToName.get(src))
121 .put("nodeName2", dpidToName.get(dst))
122 .put("port1", port(node.path("src")))
123 .put("port2", port(node.path("dst")));
124 if (annot.has("bandwidth")) {
125 result.put("bandwidth", annot.path("bandwidth").asInt());
126 }
127 if (annot.has("optical.waves")) {
128 result.put("numWaves", annot.path("optical.waves").asInt());
129 }
130 return result;
131 }
132
133 private String dpid(JsonNode node) {
134 String s = node.asText("of:0000000000000000").substring(3);
135 return s.substring(0, 2) + ":" + s.substring(2, 4) + ":" +
136 s.substring(4, 6) + ":" + s.substring(6, 8) + ":" +
137 s.substring(8, 10) + ":" + s.substring(10, 12) + ":" +
138 s.substring(12, 14) + ":" + s.substring(14, 16);
139 }
140
141 private int port(JsonNode node) {
142 return Integer.parseInt(node.asText("of:0000000000000000/0").substring(20));
143 }
144
145}