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