blob: 67929cf1d569d637928e9a9c729ce6aef661b729 [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;
Sho SHIMIZU70e76d82016-01-05 16:26:26 -080028import org.onlab.util.Spectrum;
Ayaka Koshibe74b55272015-05-28 15:16:04 -070029import org.onosproject.net.AnnotationKeys;
Ayaka Koshibeae541732015-05-19 13:37:27 -070030import org.onosproject.net.ChannelSpacing;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import org.onosproject.net.ConnectPoint;
32import org.onosproject.net.DefaultAnnotations;
33import org.onosproject.net.Device;
34import org.onosproject.net.DeviceId;
Ayaka Koshibeae541732015-05-19 13:37:27 -070035import org.onosproject.net.GridType;
Brian O'Connorabafb502014-12-02 22:26:20 -080036import org.onosproject.net.Host;
37import org.onosproject.net.HostId;
38import org.onosproject.net.HostLocation;
39import org.onosproject.net.Link;
40import org.onosproject.net.MastershipRole;
Ayaka Koshibeae541732015-05-19 13:37:27 -070041import org.onosproject.net.OchPort;
42import org.onosproject.net.OchSignal;
43import org.onosproject.net.OduCltPort;
44import org.onosproject.net.OduSignalType;
45import org.onosproject.net.OmsPort;
Brian O'Connorabafb502014-12-02 22:26:20 -080046import org.onosproject.net.Port;
Ayaka Koshibe74b55272015-05-28 15:16:04 -070047import org.onosproject.net.PortNumber;
Brian O'Connorabafb502014-12-02 22:26:20 -080048import org.onosproject.net.SparseAnnotations;
49import org.onosproject.net.device.DefaultDeviceDescription;
50import org.onosproject.net.device.DefaultPortDescription;
51import org.onosproject.net.device.DeviceDescription;
Thomas Vachuskac40d4632015-04-09 16:55:03 -070052import org.onosproject.net.device.DeviceEvent;
53import org.onosproject.net.device.DeviceListener;
Brian O'Connorabafb502014-12-02 22:26:20 -080054import org.onosproject.net.device.DeviceProvider;
55import org.onosproject.net.device.DeviceProviderRegistry;
56import org.onosproject.net.device.DeviceProviderService;
Thomas Vachuskac40d4632015-04-09 16:55:03 -070057import org.onosproject.net.device.DeviceService;
Ayaka Koshibeae541732015-05-19 13:37:27 -070058import org.onosproject.net.device.OchPortDescription;
59import org.onosproject.net.device.OduCltPortDescription;
60import org.onosproject.net.device.OmsPortDescription;
Brian O'Connorabafb502014-12-02 22:26:20 -080061import org.onosproject.net.device.PortDescription;
62import org.onosproject.net.host.DefaultHostDescription;
63import org.onosproject.net.host.HostProvider;
64import org.onosproject.net.host.HostProviderRegistry;
65import org.onosproject.net.host.HostProviderService;
66import org.onosproject.net.link.DefaultLinkDescription;
67import org.onosproject.net.link.LinkProvider;
68import org.onosproject.net.link.LinkProviderRegistry;
69import org.onosproject.net.link.LinkProviderService;
70import org.onosproject.net.provider.ProviderId;
Thomas Vachuskac40d4632015-04-09 16:55:03 -070071import org.slf4j.Logger;
72import org.slf4j.LoggerFactory;
Thomas Vachuska9252bc32014-10-23 02:33:25 -070073
74import java.net.URI;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070075import java.util.ArrayList;
Thomas Vachuskaece59ee2014-11-19 19:06:11 -080076import java.util.HashSet;
Thomas Vachuska9252bc32014-10-23 02:33:25 -070077import java.util.Iterator;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070078import java.util.List;
Ayaka Koshibe74b55272015-05-28 15:16:04 -070079import java.util.Map;
Thomas Vachuskaece59ee2014-11-19 19:06:11 -080080import java.util.Set;
Thomas Vachuskac40d4632015-04-09 16:55:03 -070081import java.util.concurrent.CountDownLatch;
82import java.util.concurrent.TimeUnit;
83import java.util.stream.Collectors;
Sho SHIMIZUbb825d72015-06-11 10:56:32 -070084import java.util.stream.Stream;
Thomas Vachuska9252bc32014-10-23 02:33:25 -070085
86import static com.google.common.base.Preconditions.checkNotNull;
Brian O'Connorabafb502014-12-02 22:26:20 -080087import static org.onosproject.net.DeviceId.deviceId;
88import static org.onosproject.net.PortNumber.portNumber;
Thomas Vachuskac40d4632015-04-09 16:55:03 -070089import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_ADDED;
90import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED;
Thomas Vachuska9252bc32014-10-23 02:33:25 -070091
92/**
93 * Provider of devices and links parsed from a JSON configuration structure.
94 */
95class ConfigProvider implements DeviceProvider, LinkProvider, HostProvider {
96
Thomas Vachuskac40d4632015-04-09 16:55:03 -070097 private final Logger log = LoggerFactory.getLogger(getClass());
98
Thomas Vachuska9252bc32014-10-23 02:33:25 -070099 private static final ProviderId PID =
Brian O'Connorabafb502014-12-02 22:26:20 -0800100 new ProviderId("cfg", "org.onosproject.rest", true);
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700101
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700102 private static final String UNKNOWN = "unknown";
103
Ayaka Koshibeae541732015-05-19 13:37:27 -0700104 // C-band has 4.4 THz (4,400 GHz) total bandwidth
Sho SHIMIZU00762ee2016-01-05 16:32:24 -0800105 private static final Frequency TOTAL = Frequency.ofGHz(4_400);
Ayaka Koshibeae541732015-05-19 13:37:27 -0700106
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700107 private CountDownLatch deviceLatch;
108
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700109 private final JsonNode cfg;
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700110 private final DeviceService deviceService;
111
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700112 private final DeviceProviderRegistry deviceProviderRegistry;
113 private final LinkProviderRegistry linkProviderRegistry;
114 private final HostProviderRegistry hostProviderRegistry;
115
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700116 private DeviceProviderService deviceProviderService;
117 private LinkProviderService linkProviderService;
118 private HostProviderService hostProviderService;
119
120 private DeviceListener deviceEventCounter = new DeviceEventCounter();
121 private List<ConnectPoint> connectPoints = Lists.newArrayList();
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700122 private Map<ConnectPoint, PortDescription> descriptions = Maps.newHashMap();
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700123
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700124 /**
125 * Creates a new configuration provider.
126 *
127 * @param cfg JSON configuration
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700128 * @param deviceService device service
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700129 * @param deviceProviderRegistry device provider registry
130 * @param linkProviderRegistry link provider registry
131 * @param hostProviderRegistry host provider registry
132 */
133 ConfigProvider(JsonNode cfg,
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700134 DeviceService deviceService,
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700135 DeviceProviderRegistry deviceProviderRegistry,
136 LinkProviderRegistry linkProviderRegistry,
137 HostProviderRegistry hostProviderRegistry) {
138 this.cfg = checkNotNull(cfg, "Configuration cannot be null");
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700139 this.deviceService = checkNotNull(deviceService, "Device service cannot be null");
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700140 this.deviceProviderRegistry = checkNotNull(deviceProviderRegistry, "Device provider registry cannot be null");
141 this.linkProviderRegistry = checkNotNull(linkProviderRegistry, "Link provider registry cannot be null");
142 this.hostProviderRegistry = checkNotNull(hostProviderRegistry, "Host provider registry cannot be null");
143 }
144
145 /**
146 * Parses the given JSON and provides links as configured.
147 */
148 void parse() {
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700149 try {
150 register();
151 parseDevices();
152 parseLinks();
153 parseHosts();
154 addMissingPorts();
155 } finally {
156 unregister();
157 }
158 }
159
160 private void register() {
161 deviceProviderService = deviceProviderRegistry.register(this);
162 linkProviderService = linkProviderRegistry.register(this);
163 hostProviderService = hostProviderRegistry.register(this);
164 }
165
166 private void unregister() {
167 deviceProviderRegistry.unregister(this);
168 linkProviderRegistry.unregister(this);
169 hostProviderRegistry.unregister(this);
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700170 }
171
172 // Parses the given JSON and provides devices.
173 private void parseDevices() {
174 try {
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700175 JsonNode nodes = cfg.get("devices");
176 if (nodes != null) {
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700177 prepareForDeviceEvents(nodes.size());
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700178 for (JsonNode node : nodes) {
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700179 parseDevice(node);
Thomas Vachuska3b84c862015-04-28 12:09:48 -0700180
181 // FIXME: hack to make sure device attributes take
182 // This will be fixed when GossipDeviceStore uses ECM
183 parseDevice(node);
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700184 }
185 }
186 } finally {
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700187 waitForDeviceEvents();
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700188 }
189 }
190
191 // Parses the given node with device data and supplies the device.
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700192 private void parseDevice(JsonNode node) {
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700193 URI uri = URI.create(get(node, "uri"));
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700194 Device.Type type = Device.Type.valueOf(get(node, "type", "SWITCH"));
195 String mfr = get(node, "mfr", UNKNOWN);
196 String hw = get(node, "hw", UNKNOWN);
197 String sw = get(node, "sw", UNKNOWN);
198 String serial = get(node, "serial", UNKNOWN);
199 ChassisId cid = new ChassisId(get(node, "mac", "000000000000"));
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700200 SparseAnnotations annotations = annotations(node.get("annotations"));
201
202 DeviceDescription desc =
203 new DefaultDeviceDescription(uri, type, mfr, hw, sw, serial,
204 cid, annotations);
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700205 DeviceId deviceId = deviceId(uri);
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700206 deviceProviderService.deviceConnected(deviceId, desc);
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700207
208 JsonNode ports = node.get("ports");
209 if (ports != null) {
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700210 parsePorts(deviceId, ports);
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700211 }
212 }
213
214 // Parses the given node with list of device ports.
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700215 private void parsePorts(DeviceId deviceId, JsonNode nodes) {
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700216 List<PortDescription> ports = new ArrayList<>();
217 for (JsonNode node : nodes) {
Marc De Leenheer723f5532015-06-03 20:16:17 -0700218 ports.add(parsePort(deviceId, node));
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700219 }
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700220 deviceProviderService.updatePorts(deviceId, ports);
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700221 }
222
223 // Parses the given node with port information.
Marc De Leenheer723f5532015-06-03 20:16:17 -0700224 private PortDescription parsePort(DeviceId deviceId, JsonNode node) {
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700225 Port.Type type = Port.Type.valueOf(node.path("type").asText("COPPER"));
Marc De Leenheer723f5532015-06-03 20:16:17 -0700226 // TL1-based ports have a name
227 PortNumber port = null;
228 if (node.has("name")) {
229 for (Port p : deviceService.getPorts(deviceId)) {
230 if (p.number().name().equals(node.get("name").asText())) {
231 port = p.number();
232 break;
233 }
234 }
235 } else {
236 port = portNumber(node.path("port").asLong(0));
237 }
238
Marc De Leenheerc9733082015-06-04 12:22:38 -0700239 if (port == null) {
240 log.error("Cannot find port given in node {}", node);
241 return null;
242 }
243
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700244 String portName = Strings.emptyToNull(port.name());
245 SparseAnnotations annotations = null;
246 if (portName != null) {
247 annotations = DefaultAnnotations.builder()
248 .set(AnnotationKeys.PORT_NAME, portName).build();
249 }
Ayaka Koshibeae541732015-05-19 13:37:27 -0700250 switch (type) {
251 case COPPER:
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700252 return new DefaultPortDescription(port, node.path("enabled").asBoolean(true),
253 type, node.path("speed").asLong(1_000),
254 annotations);
Ayaka Koshibeae541732015-05-19 13:37:27 -0700255 case FIBER:
256 // Currently, assume OMS when FIBER. Provide sane defaults.
Marc De Leenheerfc913dd2015-07-30 16:04:55 -0700257 annotations = annotations(node.get("annotations"));
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700258 return new OmsPortDescription(port, node.path("enabled").asBoolean(true),
Sho SHIMIZU70e76d82016-01-05 16:26:26 -0800259 Spectrum.CENTER_FREQUENCY, Spectrum.CENTER_FREQUENCY.add(TOTAL),
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700260 Frequency.ofGHz(100), annotations);
Marc De Leenheer723f5532015-06-03 20:16:17 -0700261 case ODUCLT:
262 annotations = annotations(node.get("annotations"));
263 OduCltPort oduCltPort = (OduCltPort) deviceService.getPort(deviceId, port);
264 return new OduCltPortDescription(port, node.path("enabled").asBoolean(true),
265 oduCltPort.signalType(), annotations);
266 case OCH:
267 annotations = annotations(node.get("annotations"));
268 OchPort ochPort = (OchPort) deviceService.getPort(deviceId, port);
269 return new OchPortDescription(port, node.path("enabled").asBoolean(true),
270 ochPort.signalType(), ochPort.isTunable(),
271 ochPort.lambda(), annotations);
272 case OMS:
273 annotations = annotations(node.get("annotations"));
274 OmsPort omsPort = (OmsPort) deviceService.getPort(deviceId, port);
275 return new OmsPortDescription(port, node.path("enabled").asBoolean(true),
276 omsPort.minFrequency(), omsPort.maxFrequency(), omsPort.grid(), annotations);
Ayaka Koshibeae541732015-05-19 13:37:27 -0700277 default:
278 log.warn("{}: Unsupported Port Type");
279 }
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700280 return new DefaultPortDescription(port, node.path("enabled").asBoolean(true),
281 type, node.path("speed").asLong(1_000),
282 annotations);
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700283 }
284
285 // Parses the given JSON and provides links as configured.
286 private void parseLinks() {
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700287 JsonNode nodes = cfg.get("links");
288 if (nodes != null) {
289 for (JsonNode node : nodes) {
290 parseLink(node, false);
291 if (!node.has("halfplex")) {
292 parseLink(node, true);
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700293 }
294 }
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700295 }
296 }
297
298 // Parses the given node with link data and supplies the link.
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700299 private void parseLink(JsonNode node, boolean reverse) {
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700300 ConnectPoint src = connectPoint(get(node, "src"));
301 ConnectPoint dst = connectPoint(get(node, "dst"));
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700302 Link.Type type = Link.Type.valueOf(get(node, "type", "DIRECT"));
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700303 SparseAnnotations annotations = annotations(node.get("annotations"));
Ayaka Koshibeae541732015-05-19 13:37:27 -0700304 // take annotations to update optical ports with correct attributes.
305 updatePorts(src, dst, annotations);
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700306 DefaultLinkDescription desc = reverse ?
307 new DefaultLinkDescription(dst, src, type, annotations) :
308 new DefaultLinkDescription(src, dst, type, annotations);
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700309 linkProviderService.linkDetected(desc);
310
311 connectPoints.add(src);
312 connectPoints.add(dst);
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700313 }
314
Ayaka Koshibeae541732015-05-19 13:37:27 -0700315 private void updatePorts(ConnectPoint src, ConnectPoint dst, SparseAnnotations annotations) {
Ayaka Koshibeae541732015-05-19 13:37:27 -0700316 final String linkType = annotations.value("optical.type");
317 if ("cross-connect".equals(linkType)) {
318 String value = annotations.value("bandwidth").trim();
319 try {
320 double bw = Double.parseDouble(value);
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700321 updateOchPort(bw, src, dst);
Ayaka Koshibeae541732015-05-19 13:37:27 -0700322 } catch (NumberFormatException e) {
323 log.warn("Invalid bandwidth ({}), can't configure port(s)", value);
324 return;
325 }
326 } else if ("WDM".equals(linkType)) {
327 String value = annotations.value("optical.waves").trim();
328 try {
329 int numChls = Integer.parseInt(value);
Jonathan Hartd9df7bd2015-11-10 17:10:25 -0800330 updateOmsPorts(numChls, src, dst);
Ayaka Koshibeae541732015-05-19 13:37:27 -0700331 } catch (NumberFormatException e) {
332 log.warn("Invalid channel ({}), can't configure port(s)", value);
333 return;
334 }
335 }
336 }
337
338 // uses 'bandwidth' annotation to determine the channel spacing.
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700339 private void updateOchPort(double bw, ConnectPoint srcCp, ConnectPoint dstCp) {
340 Device src = deviceService.getDevice(srcCp.deviceId());
341 Device dst = deviceService.getDevice(dstCp.deviceId());
342 // bandwidth in MHz (assuming Hz - linc is not clear if that or Mb).
Ayaka Koshibeae541732015-05-19 13:37:27 -0700343 Frequency spacing = Frequency.ofMHz(bw);
344 // channel bandwidth is smaller than smallest standard channel spacing.
345 ChannelSpacing chsp = null;
346 if (spacing.compareTo(ChannelSpacing.CHL_6P25GHZ.frequency()) <= 0) {
347 chsp = ChannelSpacing.CHL_6P25GHZ;
348 }
349 for (int i = 1; i < ChannelSpacing.values().length; i++) {
350 Frequency val = ChannelSpacing.values()[i].frequency();
351 // pick the next highest or equal channel interval.
352 if (val.isLessThan(spacing)) {
353 chsp = ChannelSpacing.values()[i - 1];
354 break;
355 }
356 }
357 if (chsp == null) {
358 log.warn("Invalid channel spacing ({}), can't configure port(s)", spacing);
359 return;
360 }
361 OchSignal signal = new OchSignal(GridType.DWDM, chsp, 1, 1);
362 if (src.type() == Device.Type.ROADM) {
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700363 PortDescription portDesc = new OchPortDescription(srcCp.port(), true,
Ayaka Koshibeae541732015-05-19 13:37:27 -0700364 OduSignalType.ODU4, true, signal);
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700365 descriptions.put(srcCp, portDesc);
366 deviceProviderService.portStatusChanged(srcCp.deviceId(), portDesc);
Ayaka Koshibeae541732015-05-19 13:37:27 -0700367 }
368 if (dst.type() == Device.Type.ROADM) {
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700369 PortDescription portDesc = new OchPortDescription(dstCp.port(), true,
Ayaka Koshibeae541732015-05-19 13:37:27 -0700370 OduSignalType.ODU4, true, signal);
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700371 descriptions.put(dstCp, portDesc);
372 deviceProviderService.portStatusChanged(dstCp.deviceId(), portDesc);
Ayaka Koshibeae541732015-05-19 13:37:27 -0700373 }
374 }
375
Jonathan Hartd9df7bd2015-11-10 17:10:25 -0800376 private void updateOmsPorts(int numChls, ConnectPoint srcCp, ConnectPoint dstCp) {
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700377 // round down to largest slot that allows numChl channels to fit into C band range
Ayaka Koshibeae541732015-05-19 13:37:27 -0700378 ChannelSpacing chl = null;
379 Frequency perChl = TOTAL.floorDivision(numChls);
380 for (int i = 0; i < ChannelSpacing.values().length; i++) {
381 Frequency val = ChannelSpacing.values()[i].frequency();
382 if (val.isLessThan(perChl)) {
383 chl = ChannelSpacing.values()[i];
384 break;
385 }
386 }
387 if (chl == null) {
388 chl = ChannelSpacing.CHL_6P25GHZ;
389 }
390
391 // if true, there was less channels than can be tightly packed.
Sho SHIMIZUf6501c02015-06-10 19:30:24 -0700392 Frequency grid = chl.frequency();
Ayaka Koshibeae541732015-05-19 13:37:27 -0700393 // say Linc's 1st slot starts at CENTER and goes up from there.
Sho SHIMIZU70e76d82016-01-05 16:26:26 -0800394 Frequency min = Spectrum.CENTER_FREQUENCY.add(grid);
395 Frequency max = Spectrum.CENTER_FREQUENCY.add(grid.multiply(numChls));
Ayaka Koshibeae541732015-05-19 13:37:27 -0700396
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700397 PortDescription srcPortDesc = new OmsPortDescription(srcCp.port(), true, min, max, grid);
398 PortDescription dstPortDesc = new OmsPortDescription(dstCp.port(), true, min, max, grid);
399 descriptions.put(srcCp, srcPortDesc);
400 descriptions.put(dstCp, dstPortDesc);
401 deviceProviderService.portStatusChanged(srcCp.deviceId(), srcPortDesc);
402 deviceProviderService.portStatusChanged(dstCp.deviceId(), dstPortDesc);
Ayaka Koshibeae541732015-05-19 13:37:27 -0700403 }
404
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700405 // Parses the given JSON and provides hosts as configured.
406 private void parseHosts() {
407 try {
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700408 JsonNode nodes = cfg.get("hosts");
409 if (nodes != null) {
410 for (JsonNode node : nodes) {
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700411 parseHost(node);
Thomas Vachuska3b84c862015-04-28 12:09:48 -0700412
413 // FIXME: hack to make sure host attributes take
414 // This will be fixed when GossipHostStore uses ECM
415 parseHost(node);
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700416 }
417 }
418 } finally {
419 hostProviderRegistry.unregister(this);
420 }
421 }
422
423 // Parses the given node with host data and supplies the host.
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700424 private void parseHost(JsonNode node) {
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700425 MacAddress mac = MacAddress.valueOf(get(node, "mac"));
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700426 VlanId vlanId = VlanId.vlanId((short) node.get("vlan").asInt(VlanId.UNTAGGED));
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700427 HostId hostId = HostId.hostId(mac, vlanId);
428 SparseAnnotations annotations = annotations(node.get("annotations"));
429 HostLocation location = new HostLocation(connectPoint(get(node, "location")), 0);
Thomas Vachuskaece59ee2014-11-19 19:06:11 -0800430
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700431 String[] ipStrings = get(node, "ip", "").split(",");
Thomas Vachuskaece59ee2014-11-19 19:06:11 -0800432 Set<IpAddress> ips = new HashSet<>();
433 for (String ip : ipStrings) {
434 ips.add(IpAddress.valueOf(ip.trim()));
435 }
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700436
437 DefaultHostDescription desc =
Thomas Vachuskaece59ee2014-11-19 19:06:11 -0800438 new DefaultHostDescription(mac, vlanId, location, ips, annotations);
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700439 hostProviderService.hostDetected(hostId, desc);
440
441 connectPoints.add(location);
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700442 }
443
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700444 // Adds any missing device ports for configured links and host locations.
445 private void addMissingPorts() {
446 deviceService.getDevices().forEach(this::addMissingPorts);
447 }
448
449 // Adds any missing device ports.
450 private void addMissingPorts(Device device) {
Jonathan Harteb431c12015-07-27 10:43:50 -0700451 try {
452 List<Port> ports = deviceService.getPorts(device.id());
453 Set<ConnectPoint> existing = ports.stream()
454 .map(p -> new ConnectPoint(device.id(), p.number()))
455 .collect(Collectors.toSet());
456 Set<ConnectPoint> missing = connectPoints.stream()
457 .filter(cp -> cp.deviceId().equals(device.id()))
458 .filter(cp -> !existing.contains(cp))
459 .collect(Collectors.toSet());
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700460
Jonathan Harteb431c12015-07-27 10:43:50 -0700461 if (!missing.isEmpty()) {
462 List<PortDescription> newPorts = Stream.concat(
463 ports.stream().map(this::description),
464 missing.stream().map(this::description)
465 ).collect(Collectors.toList());
466 deviceProviderService.updatePorts(device.id(), newPorts);
467 }
468 } catch (IllegalArgumentException e) {
469 log.warn("Error pushing ports: {}", e.getMessage());
470 }
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700471 }
472
473 // Creates a port description from the specified port.
474 private PortDescription description(Port p) {
Ayaka Koshibeae541732015-05-19 13:37:27 -0700475 switch (p.type()) {
476 case OMS:
477 OmsPort op = (OmsPort) p;
478 return new OmsPortDescription(
479 op.number(), op.isEnabled(), op.minFrequency(), op.maxFrequency(), op.grid());
480 case OCH:
481 OchPort ochp = (OchPort) p;
482 return new OchPortDescription(
483 ochp.number(), ochp.isEnabled(), ochp.signalType(), ochp.isTunable(), ochp.lambda());
484 case ODUCLT:
485 OduCltPort odup = (OduCltPort) p;
486 return new OduCltPortDescription(
487 odup.number(), odup.isEnabled(), odup.signalType());
488 default:
489 return new DefaultPortDescription(p.number(), p.isEnabled(), p.type(), p.portSpeed());
490 }
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700491 }
492
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700493 // Creates a port description from the specified connection point if none created earlier.
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700494 private PortDescription description(ConnectPoint cp) {
Ayaka Koshibe74b55272015-05-28 15:16:04 -0700495 PortDescription saved = descriptions.get(cp);
496 if (saved != null) {
497 return saved;
498 }
Ayaka Koshibeae541732015-05-19 13:37:27 -0700499 Port p = deviceService.getPort(cp.deviceId(), cp.port());
500 if (p == null) {
501 return new DefaultPortDescription(cp.port(), true);
502 }
503 return description(p);
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700504 }
505
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700506 // Produces set of annotations from the given JSON node.
507 private SparseAnnotations annotations(JsonNode node) {
508 if (node == null) {
Sho SHIMIZUa3d67cd2015-06-04 11:46:48 -0700509 return DefaultAnnotations.EMPTY;
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700510 }
511
512 DefaultAnnotations.Builder builder = DefaultAnnotations.builder();
513 Iterator<String> it = node.fieldNames();
514 while (it.hasNext()) {
515 String k = it.next();
516 builder.set(k, node.get(k).asText());
517 }
518 return builder.build();
519 }
520
521 // Produces a connection point from the specified uri/port text.
522 private ConnectPoint connectPoint(String text) {
523 int i = text.lastIndexOf("/");
Ayaka Koshibec10d1512015-06-04 15:17:58 -0700524 String portName = text.substring(i + 1);
525 DeviceId deviceId = deviceId(text.substring(0, i));
526
Ayaka Koshibec10d1512015-06-04 15:17:58 -0700527 for (Port port : deviceService.getPorts(deviceId)) {
528 PortNumber pn = port.number();
529 if (pn.name().equals(portName)) {
530 return new ConnectPoint(deviceId, pn);
531 }
532 }
Sho SHIMIZUccf40c72015-06-11 16:55:20 -0700533
534 long portNum;
535 try {
536 portNum = Long.parseLong(portName);
537 } catch (NumberFormatException e) {
538 portNum = 0;
539 }
540
Ayaka Koshibec10d1512015-06-04 15:17:58 -0700541 return new ConnectPoint(deviceId, portNumber(portNum, portName));
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700542 }
543
544 // Returns string form of the named property in the given JSON object.
545 private String get(JsonNode node, String name) {
546 return node.path(name).asText();
547 }
548
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700549 // Returns string form of the named property in the given JSON object.
550 private String get(JsonNode node, String name, String defaultValue) {
551 return node.path(name).asText(defaultValue);
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700552 }
553
554 @Override
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700555 public void roleChanged(DeviceId device, MastershipRole newRole) {
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700556 deviceProviderService.receivedRoleReply(device, newRole, newRole);
557 }
558
559 @Override
560 public void triggerProbe(DeviceId deviceId) {
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700561 }
562
563 @Override
564 public void triggerProbe(Host host) {
565 }
566
567 @Override
568 public ProviderId id() {
569 return PID;
570 }
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700571
572 @Override
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700573 public boolean isReachable(DeviceId device) {
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700574 return true;
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700575 }
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700576
577 /**
578 * Prepares to count device added/available/removed events.
579 *
580 * @param count number of events to count
581 */
582 protected void prepareForDeviceEvents(int count) {
583 deviceLatch = new CountDownLatch(count);
584 deviceService.addListener(deviceEventCounter);
585 }
586
587 /**
588 * Waits for all expected device added/available/removed events.
589 */
590 protected void waitForDeviceEvents() {
591 try {
592 deviceLatch.await(2, TimeUnit.SECONDS);
593 } catch (InterruptedException e) {
594 log.warn("Device events did not arrive in time");
595 }
596 deviceService.removeListener(deviceEventCounter);
597 }
598
599 // Counts down number of device added/available/removed events.
600 private class DeviceEventCounter implements DeviceListener {
601 @Override
602 public void event(DeviceEvent event) {
603 DeviceEvent.Type type = event.type();
604 if (type == DEVICE_ADDED || type == DEVICE_AVAILABILITY_CHANGED) {
605 deviceLatch.countDown();
606 }
607 }
608 }
609
Thomas Vachuska9252bc32014-10-23 02:33:25 -0700610}