Thomas Vachuska | 18867bf | 2014-10-27 19:56:15 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 18867bf | 2014-10-27 19:56:15 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * 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 Vachuska | 18867bf | 2014-10-27 19:56:15 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * 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 Vachuska | 18867bf | 2014-10-27 19:56:15 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.oecfg; |
Thomas Vachuska | 18867bf | 2014-10-27 19:56:15 -0700 | [diff] [blame] | 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 22 | |
| 23 | import java.io.IOException; |
| 24 | import java.io.InputStream; |
| 25 | import java.util.HashMap; |
| 26 | import java.util.Map; |
| 27 | |
| 28 | /** |
| 29 | * Utility program to convert standard ONOS config JSON to format expected |
| 30 | * by the OE Link switch. |
| 31 | */ |
| 32 | public 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 Balakrishnan | bf78cf7 | 2014-11-12 12:14:50 -0800 | [diff] [blame] | 54 | result.set("switchConfig", opticalSwitches(json)); |
| 55 | result.set("linkConfig", opticalLinks(json)); |
Thomas Vachuska | 18867bf | 2014-10-27 19:56:15 -0700 | [diff] [blame] | 56 | 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 | } |