blob: 978484e2fdada9968c5cf1300f34469768d1517c [file] [log] [blame]
Thomas Vachuska9252bc32014-10-23 02:33:25 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Thomas Vachuska9252bc32014-10-23 02:33:25 -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 Vachuska9252bc32014-10-23 02:33:25 -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 Vachuska9252bc32014-10-23 02:33:25 -070015 */
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070016package org.onosproject.rest.resources;
Thomas Vachuska9252bc32014-10-23 02:33:25 -070017
18import com.fasterxml.jackson.databind.JsonNode;
Ayaka Koshibe74b55272015-05-28 15:16:04 -070019import com.google.common.base.Strings;
Thomas Vachuskac40d4632015-04-09 16:55:03 -070020import com.google.common.collect.Lists;
Ayaka Koshibe74b55272015-05-28 15:16:04 -070021import com.google.common.collect.Maps;
Ayaka Koshibeae541732015-05-19 13:37:27 -070022
Thomas Vachuskac40d4632015-04-09 16:55:03 -070023import org.onlab.packet.ChassisId;
24import org.onlab.packet.IpAddress;
25import org.onlab.packet.MacAddress;
26import org.onlab.packet.VlanId;
Ayaka Koshibeae541732015-05-19 13:37:27 -070027import org.onlab.util.Frequency;
Ayaka Koshibe74b55272015-05-28 15:16:04 -070028import org.onosproject.net.AnnotationKeys;
Ayaka Koshibeae541732015-05-19 13:37:27 -070029import org.onosproject.net.ChannelSpacing;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.net.ConnectPoint;
31import org.onosproject.net.DefaultAnnotations;
32import org.onosproject.net.Device;
33import org.onosproject.net.DeviceId;
Ayaka Koshibeae541732015-05-19 13:37:27 -070034import org.onosproject.net.GridType;
Brian O'Connorabafb502014-12-02 22:26:20 -080035import org.onosproject.net.Host;
36import org.onosproject.net.HostId;
37import org.onosproject.net.HostLocation;
38import org.onosproject.net.Link;
39import org.onosproject.net.MastershipRole;
Ayaka Koshibeae541732015-05-19 13:37:27 -070040import org.onosproject.net.OchPort;
41import org.onosproject.net.OchSignal;
42import org.onosproject.net.OduCltPort;
43import org.onosproject.net.OduSignalType;
44import org.onosproject.net.OmsPort;
Brian O'Connorabafb502014-12-02 22:26:20 -080045import org.onosproject.net.Port;
Ayaka Koshibe74b55272015-05-28 15:16:04 -070046import org.onosproject.net.PortNumber;
Brian O'Connorabafb502014-12-02 22:26:20 -080047import org.onosproject.net.SparseAnnotations;
48import org.onosproject.net.device.DefaultDeviceDescription;
49import org.onosproject.net.device.DefaultPortDescription;
50import org.onosproject.net.device.DeviceDescription;
Thomas Vachuskac40d4632015-04-09 16:55:03 -070051import org.onosproject.net.device.DeviceEvent;
52import org.onosproject.net.device.DeviceListener;
Brian O'Connorabafb502014-12-02 22:26:20 -080053import org.onosproject.net.device.DeviceProvider;
54import org.onosproject.net.device.DeviceProviderRegistry;
55import org.onosproject.net.device.DeviceProviderService;
Thomas Vachuskac40d4632015-04-09 16:55:03 -070056import org.onosproject.net.device.DeviceService;
Ayaka Koshibeae541732015-05-19 13:37:27 -070057import org.onosproject.net.device.OchPortDescription;
58import org.onosproject.net.device.OduCltPortDescription;
59import org.onosproject.net.device.OmsPortDescription;
Brian O'Connorabafb502014-12-02 22:26:20 -080060import org.onosproject.net.device.PortDescription;
61import org.onosproject.net.host.DefaultHostDescription;
62import org.onosproject.net.host.HostProvider;
63import org.onosproject.net.host.HostProviderRegistry;
64import org.onosproject.net.host.HostProviderService;
65import org.onosproject.net.link.DefaultLinkDescription;
66import org.onosproject.net.link.LinkProvider;
67import org.onosproject.net.link.LinkProviderRegistry;
68import org.onosproject.net.link.LinkProviderService;
69import org.onosproject.net.provider.ProviderId;
Thomas Vachuskac40d4632015-04-09 16:55:03 -070070import org.slf4j.Logger;
71import org.slf4j.LoggerFactory;
Thomas Vachuska9252bc32014-10-23 02:33:25 -070072
73import java.net.URI;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070074import java.util.ArrayList;
Thomas Vachuskaece59ee2014-11-19 19:06:11 -080075import java.util.HashSet;
Thomas Vachuska9252bc32014-10-23 02:33:25 -070076import java.util.Iterator;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070077import java.util.List;
Ayaka Koshibe74b55272015-05-28 15:16:04 -070078import java.util.Map;
Thomas Vachuskaece59ee2014-11-19 19:06:11 -080079import java.util.Set;
Thomas Vachuskac40d4632015-04-09 16:55:03 -070080import java.util.concurrent.CountDownLatch;
81import java.util.concurrent.TimeUnit;
82import java.util.stream.Collectors;
Thomas Vachuska9252bc32014-10-23 02:33:25 -070083
84import static com.google.common.base.Preconditions.checkNotNull;
Brian O'Connorabafb502014-12-02 22:26:20 -080085import static org.onosproject.net.DeviceId.deviceId;
86import static org.onosproject.net.PortNumber.portNumber;
Thomas Vachuskac40d4632015-04-09 16:55:03 -070087import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_ADDED;
88import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED;
Thomas Vachuska9252bc32014-10-23 02:33:25 -070089
90/**
91 * Provider of devices and links parsed from a JSON configuration structure.
92 */
93class ConfigProvider implements DeviceProvider, LinkProvider, HostProvider {
94
Thomas Vachuskac40d4632015-04-09 16:55:03 -070095 private final Logger log = LoggerFactory.getLogger(getClass());
96
Thomas Vachuska9252bc32014-10-23 02:33:25 -070097 private static final ProviderId PID =
Brian O'Connorabafb502014-12-02 22:26:20 -080098 new ProviderId("cfg", "org.onosproject.rest", true);
Thomas Vachuska9252bc32014-10-23 02:33:25 -070099
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700100 private static final String UNKNOWN = "unknown";
101
Ayaka Koshibeae541732015-05-19 13:37:27 -0700102 private static final Frequency CENTER = Frequency.ofTHz(193.1);
103 // C-band has 4.4 THz (4,400 GHz) total bandwidth
104 private static final Frequency TOTAL = Frequency.ofTHz(4.4);
105
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700106 private CountDownLatch deviceLatch;
107
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700108 private final JsonNode cfg;
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700109 private final DeviceService deviceService;
110
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700111 private final DeviceProviderRegistry deviceProviderRegistry;
112 private final LinkProviderRegistry linkProviderRegistry;
113 private final HostProviderRegistry hostProviderRegistry;
114
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700115 private DeviceProviderService deviceProviderService;
116 private LinkProviderService linkProviderService;
117 private HostProviderService hostProviderService;
118
119 private DeviceListener deviceEventCounter = new DeviceEventCounter();
120 private List<ConnectPoint> connectPoints = Lists.newArrayList();
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700121 private Map<ConnectPoint, PortDescription> descriptions = Maps.newHashMap();
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700122
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700123 /**
124 * Creates a new configuration provider.
125 *
126 * @param cfg JSON configuration
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700127 * @param deviceService device service
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700128 * @param deviceProviderRegistry device provider registry
129 * @param linkProviderRegistry link provider registry
130 * @param hostProviderRegistry host provider registry
131 */
132 ConfigProvider(JsonNode cfg,
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700133 DeviceService deviceService,
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700134 DeviceProviderRegistry deviceProviderRegistry,
135 LinkProviderRegistry linkProviderRegistry,
136 HostProviderRegistry hostProviderRegistry) {
137 this.cfg = checkNotNull(cfg, "Configuration cannot be null");
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700138 this.deviceService = checkNotNull(deviceService, "Device service cannot be null");
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700139 this.deviceProviderRegistry = checkNotNull(deviceProviderRegistry, "Device provider registry cannot be null");
140 this.linkProviderRegistry = checkNotNull(linkProviderRegistry, "Link provider registry cannot be null");
141 this.hostProviderRegistry = checkNotNull(hostProviderRegistry, "Host provider registry cannot be null");
142 }
143
144 /**
145 * Parses the given JSON and provides links as configured.
146 */
147 void parse() {
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700148 try {
149 register();
150 parseDevices();
151 parseLinks();
152 parseHosts();
153 addMissingPorts();
154 } finally {
155 unregister();
156 }
157 }
158
159 private void register() {
160 deviceProviderService = deviceProviderRegistry.register(this);
161 linkProviderService = linkProviderRegistry.register(this);
162 hostProviderService = hostProviderRegistry.register(this);
163 }
164
165 private void unregister() {
166 deviceProviderRegistry.unregister(this);
167 linkProviderRegistry.unregister(this);
168 hostProviderRegistry.unregister(this);
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700169 }
170
171 // Parses the given JSON and provides devices.
172 private void parseDevices() {
173 try {
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700174 JsonNode nodes = cfg.get("devices");
175 if (nodes != null) {
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700176 prepareForDeviceEvents(nodes.size());
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700177 for (JsonNode node : nodes) {
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700178 parseDevice(node);
Thomas Vachuska3b84c862015-04-28 12:09:48 -0700179
180 // FIXME: hack to make sure device attributes take
181 // This will be fixed when GossipDeviceStore uses ECM
182 parseDevice(node);
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700183 }
184 }
185 } finally {
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700186 waitForDeviceEvents();
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700187 }
188 }
189
190 // Parses the given node with device data and supplies the device.
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700191 private void parseDevice(JsonNode node) {
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700192 URI uri = URI.create(get(node, "uri"));
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700193 Device.Type type = Device.Type.valueOf(get(node, "type", "SWITCH"));
194 String mfr = get(node, "mfr", UNKNOWN);
195 String hw = get(node, "hw", UNKNOWN);
196 String sw = get(node, "sw", UNKNOWN);
197 String serial = get(node, "serial", UNKNOWN);
198 ChassisId cid = new ChassisId(get(node, "mac", "000000000000"));
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700199 SparseAnnotations annotations = annotations(node.get("annotations"));
200
201 DeviceDescription desc =
202 new DefaultDeviceDescription(uri, type, mfr, hw, sw, serial,
203 cid, annotations);
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700204 DeviceId deviceId = deviceId(uri);
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700205 deviceProviderService.deviceConnected(deviceId, desc);
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700206
207 JsonNode ports = node.get("ports");
208 if (ports != null) {
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700209 parsePorts(deviceId, ports);
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700210 }
211 }
212
213 // Parses the given node with list of device ports.
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700214 private void parsePorts(DeviceId deviceId, JsonNode nodes) {
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700215 List<PortDescription> ports = new ArrayList<>();
216 for (JsonNode node : nodes) {
Marc De Leenheer723f5532015-06-03 20:16:17 -0700217 ports.add(parsePort(deviceId, node));
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700218 }
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700219 deviceProviderService.updatePorts(deviceId, ports);
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700220 }
221
222 // Parses the given node with port information.
Marc De Leenheer723f5532015-06-03 20:16:17 -0700223 private PortDescription parsePort(DeviceId deviceId, JsonNode node) {
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700224 Port.Type type = Port.Type.valueOf(node.path("type").asText("COPPER"));
Marc De Leenheer723f5532015-06-03 20:16:17 -0700225 // TL1-based ports have a name
226 PortNumber port = null;
227 if (node.has("name")) {
228 for (Port p : deviceService.getPorts(deviceId)) {
229 if (p.number().name().equals(node.get("name").asText())) {
230 port = p.number();
231 break;
232 }
233 }
234 } else {
235 port = portNumber(node.path("port").asLong(0));
236 }
237
Marc De Leenheerc9733082015-06-04 12:22:38 -0700238 if (port == null) {
239 log.error("Cannot find port given in node {}", node);
240 return null;
241 }
242
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700243 String portName = Strings.emptyToNull(port.name());
244 SparseAnnotations annotations = null;
245 if (portName != null) {
246 annotations = DefaultAnnotations.builder()
247 .set(AnnotationKeys.PORT_NAME, portName).build();
248 }
Ayaka Koshibeae541732015-05-19 13:37:27 -0700249 switch (type) {
250 case COPPER:
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700251 return new DefaultPortDescription(port, node.path("enabled").asBoolean(true),
252 type, node.path("speed").asLong(1_000),
253 annotations);
Ayaka Koshibeae541732015-05-19 13:37:27 -0700254 case FIBER:
255 // Currently, assume OMS when FIBER. Provide sane defaults.
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700256 return new OmsPortDescription(port, node.path("enabled").asBoolean(true),
257 CENTER, CENTER.add(TOTAL),
258 Frequency.ofGHz(100), annotations);
Marc De Leenheer723f5532015-06-03 20:16:17 -0700259 case ODUCLT:
260 annotations = annotations(node.get("annotations"));
261 OduCltPort oduCltPort = (OduCltPort) deviceService.getPort(deviceId, port);
262 return new OduCltPortDescription(port, node.path("enabled").asBoolean(true),
263 oduCltPort.signalType(), annotations);
264 case OCH:
265 annotations = annotations(node.get("annotations"));
266 OchPort ochPort = (OchPort) deviceService.getPort(deviceId, port);
267 return new OchPortDescription(port, node.path("enabled").asBoolean(true),
268 ochPort.signalType(), ochPort.isTunable(),
269 ochPort.lambda(), annotations);
270 case OMS:
271 annotations = annotations(node.get("annotations"));
272 OmsPort omsPort = (OmsPort) deviceService.getPort(deviceId, port);
273 return new OmsPortDescription(port, node.path("enabled").asBoolean(true),
274 omsPort.minFrequency(), omsPort.maxFrequency(), omsPort.grid(), annotations);
Ayaka Koshibeae541732015-05-19 13:37:27 -0700275 default:
276 log.warn("{}: Unsupported Port Type");
277 }
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700278 return new DefaultPortDescription(port, node.path("enabled").asBoolean(true),
279 type, node.path("speed").asLong(1_000),
280 annotations);
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700281 }
282
283 // Parses the given JSON and provides links as configured.
284 private void parseLinks() {
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700285 JsonNode nodes = cfg.get("links");
286 if (nodes != null) {
287 for (JsonNode node : nodes) {
288 parseLink(node, false);
289 if (!node.has("halfplex")) {
290 parseLink(node, true);
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700291 }
292 }
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700293 }
294 }
295
296 // Parses the given node with link data and supplies the link.
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700297 private void parseLink(JsonNode node, boolean reverse) {
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700298 ConnectPoint src = connectPoint(get(node, "src"));
299 ConnectPoint dst = connectPoint(get(node, "dst"));
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700300 Link.Type type = Link.Type.valueOf(get(node, "type", "DIRECT"));
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700301 SparseAnnotations annotations = annotations(node.get("annotations"));
Ayaka Koshibeae541732015-05-19 13:37:27 -0700302 // take annotations to update optical ports with correct attributes.
303 updatePorts(src, dst, annotations);
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700304 DefaultLinkDescription desc = reverse ?
305 new DefaultLinkDescription(dst, src, type, annotations) :
306 new DefaultLinkDescription(src, dst, type, annotations);
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700307 linkProviderService.linkDetected(desc);
308
309 connectPoints.add(src);
310 connectPoints.add(dst);
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700311 }
312
Ayaka Koshibeae541732015-05-19 13:37:27 -0700313 private void updatePorts(ConnectPoint src, ConnectPoint dst, SparseAnnotations annotations) {
Ayaka Koshibeae541732015-05-19 13:37:27 -0700314 final String linkType = annotations.value("optical.type");
315 if ("cross-connect".equals(linkType)) {
316 String value = annotations.value("bandwidth").trim();
317 try {
318 double bw = Double.parseDouble(value);
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700319 updateOchPort(bw, src, dst);
Ayaka Koshibeae541732015-05-19 13:37:27 -0700320 } catch (NumberFormatException e) {
321 log.warn("Invalid bandwidth ({}), can't configure port(s)", value);
322 return;
323 }
324 } else if ("WDM".equals(linkType)) {
325 String value = annotations.value("optical.waves").trim();
326 try {
327 int numChls = Integer.parseInt(value);
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700328 updateOMSPorts(numChls, src, dst);
Ayaka Koshibeae541732015-05-19 13:37:27 -0700329 } catch (NumberFormatException e) {
330 log.warn("Invalid channel ({}), can't configure port(s)", value);
331 return;
332 }
333 }
334 }
335
336 // uses 'bandwidth' annotation to determine the channel spacing.
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700337 private void updateOchPort(double bw, ConnectPoint srcCp, ConnectPoint dstCp) {
338 Device src = deviceService.getDevice(srcCp.deviceId());
339 Device dst = deviceService.getDevice(dstCp.deviceId());
340 // bandwidth in MHz (assuming Hz - linc is not clear if that or Mb).
Ayaka Koshibeae541732015-05-19 13:37:27 -0700341 Frequency spacing = Frequency.ofMHz(bw);
342 // channel bandwidth is smaller than smallest standard channel spacing.
343 ChannelSpacing chsp = null;
344 if (spacing.compareTo(ChannelSpacing.CHL_6P25GHZ.frequency()) <= 0) {
345 chsp = ChannelSpacing.CHL_6P25GHZ;
346 }
347 for (int i = 1; i < ChannelSpacing.values().length; i++) {
348 Frequency val = ChannelSpacing.values()[i].frequency();
349 // pick the next highest or equal channel interval.
350 if (val.isLessThan(spacing)) {
351 chsp = ChannelSpacing.values()[i - 1];
352 break;
353 }
354 }
355 if (chsp == null) {
356 log.warn("Invalid channel spacing ({}), can't configure port(s)", spacing);
357 return;
358 }
359 OchSignal signal = new OchSignal(GridType.DWDM, chsp, 1, 1);
360 if (src.type() == Device.Type.ROADM) {
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700361 PortDescription portDesc = new OchPortDescription(srcCp.port(), true,
Ayaka Koshibeae541732015-05-19 13:37:27 -0700362 OduSignalType.ODU4, true, signal);
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700363 descriptions.put(srcCp, portDesc);
364 deviceProviderService.portStatusChanged(srcCp.deviceId(), portDesc);
Ayaka Koshibeae541732015-05-19 13:37:27 -0700365 }
366 if (dst.type() == Device.Type.ROADM) {
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700367 PortDescription portDesc = new OchPortDescription(dstCp.port(), true,
Ayaka Koshibeae541732015-05-19 13:37:27 -0700368 OduSignalType.ODU4, true, signal);
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700369 descriptions.put(dstCp, portDesc);
370 deviceProviderService.portStatusChanged(dstCp.deviceId(), portDesc);
Ayaka Koshibeae541732015-05-19 13:37:27 -0700371 }
372 }
373
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700374 private void updateOMSPorts(int numChls, ConnectPoint srcCp, ConnectPoint dstCp) {
375 // round down to largest slot that allows numChl channels to fit into C band range
Ayaka Koshibeae541732015-05-19 13:37:27 -0700376 ChannelSpacing chl = null;
377 Frequency perChl = TOTAL.floorDivision(numChls);
378 for (int i = 0; i < ChannelSpacing.values().length; i++) {
379 Frequency val = ChannelSpacing.values()[i].frequency();
380 if (val.isLessThan(perChl)) {
381 chl = ChannelSpacing.values()[i];
382 break;
383 }
384 }
385 if (chl == null) {
386 chl = ChannelSpacing.CHL_6P25GHZ;
387 }
388
389 // if true, there was less channels than can be tightly packed.
390 Frequency grid = (chl == null) ? Frequency.ofGHz(100) : chl.frequency();
391 // say Linc's 1st slot starts at CENTER and goes up from there.
392 Frequency min = CENTER.add(grid);
393 Frequency max = CENTER.add(grid.multiply(numChls));
394
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700395 PortDescription srcPortDesc = new OmsPortDescription(srcCp.port(), true, min, max, grid);
396 PortDescription dstPortDesc = new OmsPortDescription(dstCp.port(), true, min, max, grid);
397 descriptions.put(srcCp, srcPortDesc);
398 descriptions.put(dstCp, dstPortDesc);
399 deviceProviderService.portStatusChanged(srcCp.deviceId(), srcPortDesc);
400 deviceProviderService.portStatusChanged(dstCp.deviceId(), dstPortDesc);
Ayaka Koshibeae541732015-05-19 13:37:27 -0700401 }
402
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700403 // Parses the given JSON and provides hosts as configured.
404 private void parseHosts() {
405 try {
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700406 JsonNode nodes = cfg.get("hosts");
407 if (nodes != null) {
408 for (JsonNode node : nodes) {
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700409 parseHost(node);
Thomas Vachuska3b84c862015-04-28 12:09:48 -0700410
411 // FIXME: hack to make sure host attributes take
412 // This will be fixed when GossipHostStore uses ECM
413 parseHost(node);
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700414 }
415 }
416 } finally {
417 hostProviderRegistry.unregister(this);
418 }
419 }
420
421 // Parses the given node with host data and supplies the host.
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700422 private void parseHost(JsonNode node) {
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700423 MacAddress mac = MacAddress.valueOf(get(node, "mac"));
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700424 VlanId vlanId = VlanId.vlanId((short) node.get("vlan").asInt(VlanId.UNTAGGED));
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700425 HostId hostId = HostId.hostId(mac, vlanId);
426 SparseAnnotations annotations = annotations(node.get("annotations"));
427 HostLocation location = new HostLocation(connectPoint(get(node, "location")), 0);
Thomas Vachuskaece59ee2014-11-19 19:06:11 -0800428
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700429 String[] ipStrings = get(node, "ip", "").split(",");
Thomas Vachuskaece59ee2014-11-19 19:06:11 -0800430 Set<IpAddress> ips = new HashSet<>();
431 for (String ip : ipStrings) {
432 ips.add(IpAddress.valueOf(ip.trim()));
433 }
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700434
435 DefaultHostDescription desc =
Thomas Vachuskaece59ee2014-11-19 19:06:11 -0800436 new DefaultHostDescription(mac, vlanId, location, ips, annotations);
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700437 hostProviderService.hostDetected(hostId, desc);
438
439 connectPoints.add(location);
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700440 }
441
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700442 // Adds any missing device ports for configured links and host locations.
443 private void addMissingPorts() {
444 deviceService.getDevices().forEach(this::addMissingPorts);
445 }
446
447 // Adds any missing device ports.
448 private void addMissingPorts(Device device) {
449 List<Port> ports = deviceService.getPorts(device.id());
450 Set<ConnectPoint> existing = ports.stream()
451 .map(p -> new ConnectPoint(device.id(), p.number()))
452 .collect(Collectors.toSet());
453 Set<ConnectPoint> missing = connectPoints.stream()
454 .filter(cp -> !existing.contains(cp))
455 .collect(Collectors.toSet());
456
457 if (!missing.isEmpty()) {
458 List<PortDescription> newPorts = Lists.newArrayList();
459 ports.forEach(p -> newPorts.add(description(p)));
460 missing.forEach(cp -> newPorts.add(description(cp)));
461 deviceProviderService.updatePorts(device.id(), newPorts);
462 }
463 }
464
465 // Creates a port description from the specified port.
466 private PortDescription description(Port p) {
Ayaka Koshibeae541732015-05-19 13:37:27 -0700467 switch (p.type()) {
468 case OMS:
469 OmsPort op = (OmsPort) p;
470 return new OmsPortDescription(
471 op.number(), op.isEnabled(), op.minFrequency(), op.maxFrequency(), op.grid());
472 case OCH:
473 OchPort ochp = (OchPort) p;
474 return new OchPortDescription(
475 ochp.number(), ochp.isEnabled(), ochp.signalType(), ochp.isTunable(), ochp.lambda());
476 case ODUCLT:
477 OduCltPort odup = (OduCltPort) p;
478 return new OduCltPortDescription(
479 odup.number(), odup.isEnabled(), odup.signalType());
480 default:
481 return new DefaultPortDescription(p.number(), p.isEnabled(), p.type(), p.portSpeed());
482 }
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700483 }
484
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700485 // Creates a port description from the specified connection point if none created earlier.
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700486 private PortDescription description(ConnectPoint cp) {
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700487 PortDescription saved = descriptions.get(cp);
488 if (saved != null) {
489 return saved;
490 }
Ayaka Koshibeae541732015-05-19 13:37:27 -0700491 Port p = deviceService.getPort(cp.deviceId(), cp.port());
492 if (p == null) {
493 return new DefaultPortDescription(cp.port(), true);
494 }
495 return description(p);
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700496 }
497
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700498 // Produces set of annotations from the given JSON node.
499 private SparseAnnotations annotations(JsonNode node) {
500 if (node == null) {
Sho SHIMIZUa3d67cd2015-06-04 11:46:48 -0700501 return DefaultAnnotations.EMPTY;
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700502 }
503
504 DefaultAnnotations.Builder builder = DefaultAnnotations.builder();
505 Iterator<String> it = node.fieldNames();
506 while (it.hasNext()) {
507 String k = it.next();
508 builder.set(k, node.get(k).asText());
509 }
510 return builder.build();
511 }
512
513 // Produces a connection point from the specified uri/port text.
514 private ConnectPoint connectPoint(String text) {
515 int i = text.lastIndexOf("/");
Ayaka Koshibec10d1512015-06-04 15:17:58 -0700516 String portName = text.substring(i + 1);
517 DeviceId deviceId = deviceId(text.substring(0, i));
518
519 long portNum = 0L;
520 for (Port port : deviceService.getPorts(deviceId)) {
521 PortNumber pn = port.number();
522 if (pn.name().equals(portName)) {
523 return new ConnectPoint(deviceId, pn);
524 }
525 }
526 return new ConnectPoint(deviceId, portNumber(portNum, portName));
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700527 }
528
529 // Returns string form of the named property in the given JSON object.
530 private String get(JsonNode node, String name) {
531 return node.path(name).asText();
532 }
533
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700534 // Returns string form of the named property in the given JSON object.
535 private String get(JsonNode node, String name, String defaultValue) {
536 return node.path(name).asText(defaultValue);
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700537 }
538
539 @Override
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700540 public void roleChanged(DeviceId device, MastershipRole newRole) {
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700541 deviceProviderService.receivedRoleReply(device, newRole, newRole);
542 }
543
544 @Override
545 public void triggerProbe(DeviceId deviceId) {
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700546 }
547
548 @Override
549 public void triggerProbe(Host host) {
550 }
551
552 @Override
553 public ProviderId id() {
554 return PID;
555 }
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700556
557 @Override
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700558 public boolean isReachable(DeviceId device) {
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700559 return true;
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700560 }
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700561
562 /**
563 * Prepares to count device added/available/removed events.
564 *
565 * @param count number of events to count
566 */
567 protected void prepareForDeviceEvents(int count) {
568 deviceLatch = new CountDownLatch(count);
569 deviceService.addListener(deviceEventCounter);
570 }
571
572 /**
573 * Waits for all expected device added/available/removed events.
574 */
575 protected void waitForDeviceEvents() {
576 try {
577 deviceLatch.await(2, TimeUnit.SECONDS);
578 } catch (InterruptedException e) {
579 log.warn("Device events did not arrive in time");
580 }
581 deviceService.removeListener(deviceEventCounter);
582 }
583
584 // Counts down number of device added/available/removed events.
585 private class DeviceEventCounter implements DeviceListener {
586 @Override
587 public void event(DeviceEvent event) {
588 DeviceEvent.Type type = event.type();
589 if (type == DEVICE_ADDED || type == DEVICE_AVAILABILITY_CHANGED) {
590 deviceLatch.countDown();
591 }
592 }
593 }
594
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700595}