blob: 296337f221bf8a1d3102b4e760f2c8ce89220bc2 [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
238 checkNotNull(port);
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700239 String portName = Strings.emptyToNull(port.name());
240 SparseAnnotations annotations = null;
241 if (portName != null) {
242 annotations = DefaultAnnotations.builder()
243 .set(AnnotationKeys.PORT_NAME, portName).build();
244 }
Ayaka Koshibeae541732015-05-19 13:37:27 -0700245 switch (type) {
246 case COPPER:
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700247 return new DefaultPortDescription(port, node.path("enabled").asBoolean(true),
248 type, node.path("speed").asLong(1_000),
249 annotations);
Ayaka Koshibeae541732015-05-19 13:37:27 -0700250 case FIBER:
251 // Currently, assume OMS when FIBER. Provide sane defaults.
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700252 return new OmsPortDescription(port, node.path("enabled").asBoolean(true),
253 CENTER, CENTER.add(TOTAL),
254 Frequency.ofGHz(100), annotations);
Marc De Leenheer723f5532015-06-03 20:16:17 -0700255 case ODUCLT:
256 annotations = annotations(node.get("annotations"));
257 OduCltPort oduCltPort = (OduCltPort) deviceService.getPort(deviceId, port);
258 return new OduCltPortDescription(port, node.path("enabled").asBoolean(true),
259 oduCltPort.signalType(), annotations);
260 case OCH:
261 annotations = annotations(node.get("annotations"));
262 OchPort ochPort = (OchPort) deviceService.getPort(deviceId, port);
263 return new OchPortDescription(port, node.path("enabled").asBoolean(true),
264 ochPort.signalType(), ochPort.isTunable(),
265 ochPort.lambda(), annotations);
266 case OMS:
267 annotations = annotations(node.get("annotations"));
268 OmsPort omsPort = (OmsPort) deviceService.getPort(deviceId, port);
269 return new OmsPortDescription(port, node.path("enabled").asBoolean(true),
270 omsPort.minFrequency(), omsPort.maxFrequency(), omsPort.grid(), annotations);
Ayaka Koshibeae541732015-05-19 13:37:27 -0700271 default:
272 log.warn("{}: Unsupported Port Type");
273 }
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700274 return new DefaultPortDescription(port, node.path("enabled").asBoolean(true),
275 type, node.path("speed").asLong(1_000),
276 annotations);
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700277 }
278
279 // Parses the given JSON and provides links as configured.
280 private void parseLinks() {
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700281 JsonNode nodes = cfg.get("links");
282 if (nodes != null) {
283 for (JsonNode node : nodes) {
284 parseLink(node, false);
285 if (!node.has("halfplex")) {
286 parseLink(node, true);
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700287 }
288 }
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700289 }
290 }
291
292 // Parses the given node with link data and supplies the link.
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700293 private void parseLink(JsonNode node, boolean reverse) {
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700294 ConnectPoint src = connectPoint(get(node, "src"));
295 ConnectPoint dst = connectPoint(get(node, "dst"));
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700296 Link.Type type = Link.Type.valueOf(get(node, "type", "DIRECT"));
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700297 SparseAnnotations annotations = annotations(node.get("annotations"));
Ayaka Koshibeae541732015-05-19 13:37:27 -0700298 // take annotations to update optical ports with correct attributes.
299 updatePorts(src, dst, annotations);
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700300 DefaultLinkDescription desc = reverse ?
301 new DefaultLinkDescription(dst, src, type, annotations) :
302 new DefaultLinkDescription(src, dst, type, annotations);
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700303 linkProviderService.linkDetected(desc);
304
305 connectPoints.add(src);
306 connectPoints.add(dst);
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700307 }
308
Ayaka Koshibeae541732015-05-19 13:37:27 -0700309 private void updatePorts(ConnectPoint src, ConnectPoint dst, SparseAnnotations annotations) {
Ayaka Koshibeae541732015-05-19 13:37:27 -0700310 final String linkType = annotations.value("optical.type");
311 if ("cross-connect".equals(linkType)) {
312 String value = annotations.value("bandwidth").trim();
313 try {
314 double bw = Double.parseDouble(value);
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700315 updateOchPort(bw, src, dst);
Ayaka Koshibeae541732015-05-19 13:37:27 -0700316 } catch (NumberFormatException e) {
317 log.warn("Invalid bandwidth ({}), can't configure port(s)", value);
318 return;
319 }
320 } else if ("WDM".equals(linkType)) {
321 String value = annotations.value("optical.waves").trim();
322 try {
323 int numChls = Integer.parseInt(value);
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700324 updateOMSPorts(numChls, src, dst);
Ayaka Koshibeae541732015-05-19 13:37:27 -0700325 } catch (NumberFormatException e) {
326 log.warn("Invalid channel ({}), can't configure port(s)", value);
327 return;
328 }
329 }
330 }
331
332 // uses 'bandwidth' annotation to determine the channel spacing.
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700333 private void updateOchPort(double bw, ConnectPoint srcCp, ConnectPoint dstCp) {
334 Device src = deviceService.getDevice(srcCp.deviceId());
335 Device dst = deviceService.getDevice(dstCp.deviceId());
336 // bandwidth in MHz (assuming Hz - linc is not clear if that or Mb).
Ayaka Koshibeae541732015-05-19 13:37:27 -0700337 Frequency spacing = Frequency.ofMHz(bw);
338 // channel bandwidth is smaller than smallest standard channel spacing.
339 ChannelSpacing chsp = null;
340 if (spacing.compareTo(ChannelSpacing.CHL_6P25GHZ.frequency()) <= 0) {
341 chsp = ChannelSpacing.CHL_6P25GHZ;
342 }
343 for (int i = 1; i < ChannelSpacing.values().length; i++) {
344 Frequency val = ChannelSpacing.values()[i].frequency();
345 // pick the next highest or equal channel interval.
346 if (val.isLessThan(spacing)) {
347 chsp = ChannelSpacing.values()[i - 1];
348 break;
349 }
350 }
351 if (chsp == null) {
352 log.warn("Invalid channel spacing ({}), can't configure port(s)", spacing);
353 return;
354 }
355 OchSignal signal = new OchSignal(GridType.DWDM, chsp, 1, 1);
356 if (src.type() == Device.Type.ROADM) {
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700357 PortDescription portDesc = new OchPortDescription(srcCp.port(), true,
Ayaka Koshibeae541732015-05-19 13:37:27 -0700358 OduSignalType.ODU4, true, signal);
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700359 descriptions.put(srcCp, portDesc);
360 deviceProviderService.portStatusChanged(srcCp.deviceId(), portDesc);
Ayaka Koshibeae541732015-05-19 13:37:27 -0700361 }
362 if (dst.type() == Device.Type.ROADM) {
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700363 PortDescription portDesc = new OchPortDescription(dstCp.port(), true,
Ayaka Koshibeae541732015-05-19 13:37:27 -0700364 OduSignalType.ODU4, true, signal);
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700365 descriptions.put(dstCp, portDesc);
366 deviceProviderService.portStatusChanged(dstCp.deviceId(), portDesc);
Ayaka Koshibeae541732015-05-19 13:37:27 -0700367 }
368 }
369
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700370 private void updateOMSPorts(int numChls, ConnectPoint srcCp, ConnectPoint dstCp) {
371 // round down to largest slot that allows numChl channels to fit into C band range
Ayaka Koshibeae541732015-05-19 13:37:27 -0700372 ChannelSpacing chl = null;
373 Frequency perChl = TOTAL.floorDivision(numChls);
374 for (int i = 0; i < ChannelSpacing.values().length; i++) {
375 Frequency val = ChannelSpacing.values()[i].frequency();
376 if (val.isLessThan(perChl)) {
377 chl = ChannelSpacing.values()[i];
378 break;
379 }
380 }
381 if (chl == null) {
382 chl = ChannelSpacing.CHL_6P25GHZ;
383 }
384
385 // if true, there was less channels than can be tightly packed.
386 Frequency grid = (chl == null) ? Frequency.ofGHz(100) : chl.frequency();
387 // say Linc's 1st slot starts at CENTER and goes up from there.
388 Frequency min = CENTER.add(grid);
389 Frequency max = CENTER.add(grid.multiply(numChls));
390
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700391 PortDescription srcPortDesc = new OmsPortDescription(srcCp.port(), true, min, max, grid);
392 PortDescription dstPortDesc = new OmsPortDescription(dstCp.port(), true, min, max, grid);
393 descriptions.put(srcCp, srcPortDesc);
394 descriptions.put(dstCp, dstPortDesc);
395 deviceProviderService.portStatusChanged(srcCp.deviceId(), srcPortDesc);
396 deviceProviderService.portStatusChanged(dstCp.deviceId(), dstPortDesc);
Ayaka Koshibeae541732015-05-19 13:37:27 -0700397 }
398
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700399 // Parses the given JSON and provides hosts as configured.
400 private void parseHosts() {
401 try {
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700402 JsonNode nodes = cfg.get("hosts");
403 if (nodes != null) {
404 for (JsonNode node : nodes) {
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700405 parseHost(node);
Thomas Vachuska3b84c862015-04-28 12:09:48 -0700406
407 // FIXME: hack to make sure host attributes take
408 // This will be fixed when GossipHostStore uses ECM
409 parseHost(node);
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700410 }
411 }
412 } finally {
413 hostProviderRegistry.unregister(this);
414 }
415 }
416
417 // Parses the given node with host data and supplies the host.
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700418 private void parseHost(JsonNode node) {
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700419 MacAddress mac = MacAddress.valueOf(get(node, "mac"));
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700420 VlanId vlanId = VlanId.vlanId((short) node.get("vlan").asInt(VlanId.UNTAGGED));
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700421 HostId hostId = HostId.hostId(mac, vlanId);
422 SparseAnnotations annotations = annotations(node.get("annotations"));
423 HostLocation location = new HostLocation(connectPoint(get(node, "location")), 0);
Thomas Vachuskaece59ee2014-11-19 19:06:11 -0800424
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700425 String[] ipStrings = get(node, "ip", "").split(",");
Thomas Vachuskaece59ee2014-11-19 19:06:11 -0800426 Set<IpAddress> ips = new HashSet<>();
427 for (String ip : ipStrings) {
428 ips.add(IpAddress.valueOf(ip.trim()));
429 }
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700430
431 DefaultHostDescription desc =
Thomas Vachuskaece59ee2014-11-19 19:06:11 -0800432 new DefaultHostDescription(mac, vlanId, location, ips, annotations);
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700433 hostProviderService.hostDetected(hostId, desc);
434
435 connectPoints.add(location);
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700436 }
437
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700438 // Adds any missing device ports for configured links and host locations.
439 private void addMissingPorts() {
440 deviceService.getDevices().forEach(this::addMissingPorts);
441 }
442
443 // Adds any missing device ports.
444 private void addMissingPorts(Device device) {
445 List<Port> ports = deviceService.getPorts(device.id());
446 Set<ConnectPoint> existing = ports.stream()
447 .map(p -> new ConnectPoint(device.id(), p.number()))
448 .collect(Collectors.toSet());
449 Set<ConnectPoint> missing = connectPoints.stream()
450 .filter(cp -> !existing.contains(cp))
451 .collect(Collectors.toSet());
452
453 if (!missing.isEmpty()) {
454 List<PortDescription> newPorts = Lists.newArrayList();
455 ports.forEach(p -> newPorts.add(description(p)));
456 missing.forEach(cp -> newPorts.add(description(cp)));
457 deviceProviderService.updatePorts(device.id(), newPorts);
458 }
459 }
460
461 // Creates a port description from the specified port.
462 private PortDescription description(Port p) {
Ayaka Koshibeae541732015-05-19 13:37:27 -0700463 switch (p.type()) {
464 case OMS:
465 OmsPort op = (OmsPort) p;
466 return new OmsPortDescription(
467 op.number(), op.isEnabled(), op.minFrequency(), op.maxFrequency(), op.grid());
468 case OCH:
469 OchPort ochp = (OchPort) p;
470 return new OchPortDescription(
471 ochp.number(), ochp.isEnabled(), ochp.signalType(), ochp.isTunable(), ochp.lambda());
472 case ODUCLT:
473 OduCltPort odup = (OduCltPort) p;
474 return new OduCltPortDescription(
475 odup.number(), odup.isEnabled(), odup.signalType());
476 default:
477 return new DefaultPortDescription(p.number(), p.isEnabled(), p.type(), p.portSpeed());
478 }
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700479 }
480
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700481 // Creates a port description from the specified connection point if none created earlier.
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700482 private PortDescription description(ConnectPoint cp) {
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700483 PortDescription saved = descriptions.get(cp);
484 if (saved != null) {
485 return saved;
486 }
Ayaka Koshibeae541732015-05-19 13:37:27 -0700487 Port p = deviceService.getPort(cp.deviceId(), cp.port());
488 if (p == null) {
489 return new DefaultPortDescription(cp.port(), true);
490 }
491 return description(p);
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700492 }
493
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700494 // Produces set of annotations from the given JSON node.
495 private SparseAnnotations annotations(JsonNode node) {
496 if (node == null) {
Sho SHIMIZUa3d67cd2015-06-04 11:46:48 -0700497 return DefaultAnnotations.EMPTY;
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700498 }
499
500 DefaultAnnotations.Builder builder = DefaultAnnotations.builder();
501 Iterator<String> it = node.fieldNames();
502 while (it.hasNext()) {
503 String k = it.next();
504 builder.set(k, node.get(k).asText());
505 }
506 return builder.build();
507 }
508
509 // Produces a connection point from the specified uri/port text.
510 private ConnectPoint connectPoint(String text) {
511 int i = text.lastIndexOf("/");
Ayaka Koshibe236e9cd2015-06-03 18:18:13 -0700512 String portStr = text.substring(i + 1);
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700513 return new ConnectPoint(deviceId(text.substring(0, i)),
Ayaka Koshibe236e9cd2015-06-03 18:18:13 -0700514 portNumber(portStr.matches(".*[a-zA-Z].*") ? "0" : portStr, portStr));
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700515 }
516
517 // Returns string form of the named property in the given JSON object.
518 private String get(JsonNode node, String name) {
519 return node.path(name).asText();
520 }
521
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700522 // Returns string form of the named property in the given JSON object.
523 private String get(JsonNode node, String name, String defaultValue) {
524 return node.path(name).asText(defaultValue);
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700525 }
526
527 @Override
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700528 public void roleChanged(DeviceId device, MastershipRole newRole) {
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700529 deviceProviderService.receivedRoleReply(device, newRole, newRole);
530 }
531
532 @Override
533 public void triggerProbe(DeviceId deviceId) {
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700534 }
535
536 @Override
537 public void triggerProbe(Host host) {
538 }
539
540 @Override
541 public ProviderId id() {
542 return PID;
543 }
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700544
545 @Override
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700546 public boolean isReachable(DeviceId device) {
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700547 return true;
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700548 }
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700549
550 /**
551 * Prepares to count device added/available/removed events.
552 *
553 * @param count number of events to count
554 */
555 protected void prepareForDeviceEvents(int count) {
556 deviceLatch = new CountDownLatch(count);
557 deviceService.addListener(deviceEventCounter);
558 }
559
560 /**
561 * Waits for all expected device added/available/removed events.
562 */
563 protected void waitForDeviceEvents() {
564 try {
565 deviceLatch.await(2, TimeUnit.SECONDS);
566 } catch (InterruptedException e) {
567 log.warn("Device events did not arrive in time");
568 }
569 deviceService.removeListener(deviceEventCounter);
570 }
571
572 // Counts down number of device added/available/removed events.
573 private class DeviceEventCounter implements DeviceListener {
574 @Override
575 public void event(DeviceEvent event) {
576 DeviceEvent.Type type = event.type();
577 if (type == DEVICE_ADDED || type == DEVICE_AVAILABILITY_CHANGED) {
578 deviceLatch.countDown();
579 }
580 }
581 }
582
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700583}