Ayaka Koshibe
Committed by Gerrit Code Review

Misc device/optical config features:

o devices and optical ports are updated with network configs
o refactored AllowedEntity checks in DeviceManager
o "basic" -> "optical" for OpticalPort config key
o Device and OpticalPort operators can create descriptions

Change-Id: I59edc368f3a8ff931954b174d4a898f6b4add5d5
...@@ -84,7 +84,7 @@ public class BasicNetworkConfigs { ...@@ -84,7 +84,7 @@ public class BasicNetworkConfigs {
84 }, 84 },
85 new ConfigFactory<ConnectPoint, OpticalPortConfig>(CONNECT_POINT_SUBJECT_FACTORY, 85 new ConfigFactory<ConnectPoint, OpticalPortConfig>(CONNECT_POINT_SUBJECT_FACTORY,
86 OpticalPortConfig.class, 86 OpticalPortConfig.class,
87 - "basic") { 87 + "optical") {
88 @Override 88 @Override
89 public OpticalPortConfig createConfig() { 89 public OpticalPortConfig createConfig() {
90 return new OpticalPortConfig(); 90 return new OpticalPortConfig();
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
16 package org.onosproject.net.device.impl; 16 package org.onosproject.net.device.impl;
17 17
18 import static org.slf4j.LoggerFactory.getLogger; 18 import static org.slf4j.LoggerFactory.getLogger;
19 +import static com.google.common.base.Preconditions.checkNotNull;
19 20
20 import org.onosproject.net.config.ConfigOperator; 21 import org.onosproject.net.config.ConfigOperator;
21 import org.onosproject.net.config.basics.BasicDeviceConfig; 22 import org.onosproject.net.config.basics.BasicDeviceConfig;
...@@ -93,4 +94,12 @@ public final class BasicDeviceOperator implements ConfigOperator { ...@@ -93,4 +94,12 @@ public final class BasicDeviceOperator implements ConfigOperator {
93 DefaultAnnotations newAnnotations = newBuilder.build(); 94 DefaultAnnotations newAnnotations = newBuilder.build();
94 return DefaultAnnotations.union(an, newAnnotations); 95 return DefaultAnnotations.union(an, newAnnotations);
95 } 96 }
97 +
98 + public static DeviceDescription descriptionOf(Device device) {
99 + checkNotNull(device, "Must supply non-null Device");
100 + return new DefaultDeviceDescription(device.id().uri(), device.type(),
101 + device.manufacturer(), device.hwVersion(),
102 + device.swVersion(), device.serialNumber(),
103 + device.chassisId(), (SparseAnnotations) device.annotations());
104 + }
96 } 105 }
......
...@@ -31,11 +31,13 @@ import org.onosproject.net.config.NetworkConfigEvent; ...@@ -31,11 +31,13 @@ import org.onosproject.net.config.NetworkConfigEvent;
31 import org.onosproject.net.config.NetworkConfigListener; 31 import org.onosproject.net.config.NetworkConfigListener;
32 import org.onosproject.net.config.NetworkConfigService; 32 import org.onosproject.net.config.NetworkConfigService;
33 import org.onosproject.net.config.basics.BasicDeviceConfig; 33 import org.onosproject.net.config.basics.BasicDeviceConfig;
34 +import org.onosproject.net.config.basics.OpticalPortConfig;
34 import org.onosproject.mastership.MastershipEvent; 35 import org.onosproject.mastership.MastershipEvent;
35 import org.onosproject.mastership.MastershipListener; 36 import org.onosproject.mastership.MastershipListener;
36 import org.onosproject.mastership.MastershipService; 37 import org.onosproject.mastership.MastershipService;
37 import org.onosproject.mastership.MastershipTerm; 38 import org.onosproject.mastership.MastershipTerm;
38 import org.onosproject.mastership.MastershipTermService; 39 import org.onosproject.mastership.MastershipTermService;
40 +import org.onosproject.net.ConnectPoint;
39 import org.onosproject.net.Device; 41 import org.onosproject.net.Device;
40 import org.onosproject.net.Device.Type; 42 import org.onosproject.net.Device.Type;
41 import org.onosproject.net.DeviceId; 43 import org.onosproject.net.DeviceId;
...@@ -64,13 +66,13 @@ import java.util.HashSet; ...@@ -64,13 +66,13 @@ import java.util.HashSet;
64 import java.util.List; 66 import java.util.List;
65 import java.util.Objects; 67 import java.util.Objects;
66 import java.util.Set; 68 import java.util.Set;
69 +import java.util.stream.Collectors;
67 import java.util.concurrent.CompletableFuture; 70 import java.util.concurrent.CompletableFuture;
68 import java.util.concurrent.ExecutionException; 71 import java.util.concurrent.ExecutionException;
69 import java.util.concurrent.ScheduledExecutorService; 72 import java.util.concurrent.ScheduledExecutorService;
70 import java.util.concurrent.TimeUnit; 73 import java.util.concurrent.TimeUnit;
71 74
72 import static com.google.common.base.Preconditions.checkNotNull; 75 import static com.google.common.base.Preconditions.checkNotNull;
73 -import static com.google.common.base.Preconditions.checkState;
74 import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; 76 import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
75 import static org.onlab.util.Tools.groupedThreads; 77 import static org.onlab.util.Tools.groupedThreads;
76 import static org.onosproject.net.MastershipRole.*; 78 import static org.onosproject.net.MastershipRole.*;
...@@ -78,7 +80,6 @@ import static org.onosproject.security.AppGuard.checkPermission; ...@@ -78,7 +80,6 @@ import static org.onosproject.security.AppGuard.checkPermission;
78 import static org.slf4j.LoggerFactory.getLogger; 80 import static org.slf4j.LoggerFactory.getLogger;
79 import static org.onosproject.security.AppPermission.Type.*; 81 import static org.onosproject.security.AppPermission.Type.*;
80 82
81 -
82 /** 83 /**
83 * Provides implementation of the device SB &amp; NB APIs. 84 * Provides implementation of the device SB &amp; NB APIs.
84 */ 85 */
...@@ -92,6 +93,7 @@ public class DeviceManager ...@@ -92,6 +93,7 @@ public class DeviceManager
92 private static final String PORT_NUMBER_NULL = "Port number cannot be null"; 93 private static final String PORT_NUMBER_NULL = "Port number cannot be null";
93 private static final String DEVICE_DESCRIPTION_NULL = "Device description cannot be null"; 94 private static final String DEVICE_DESCRIPTION_NULL = "Device description cannot be null";
94 private static final String PORT_DESCRIPTION_NULL = "Port description cannot be null"; 95 private static final String PORT_DESCRIPTION_NULL = "Port description cannot be null";
96 + private static final String PORT_DESC_LIST_NULL = "Port description list cannot be null";
95 97
96 private final Logger log = getLogger(getClass()); 98 private final Logger log = getLogger(getClass());
97 99
...@@ -265,8 +267,7 @@ public class DeviceManager ...@@ -265,8 +267,7 @@ public class DeviceManager
265 continue; 267 continue;
266 } 268 }
267 269
268 - log.info("{} is reachable but did not have a valid role, reasserting", 270 + log.info("{} is reachable but did not have a valid role, reasserting", deviceId);
269 - deviceId);
270 271
271 // isReachable but was not MASTER or STANDBY, get a role and apply 272 // isReachable but was not MASTER or STANDBY, get a role and apply
272 // Note: NONE triggers request to MastershipService 273 // Note: NONE triggers request to MastershipService
...@@ -309,16 +310,20 @@ public class DeviceManager ...@@ -309,16 +310,20 @@ public class DeviceManager
309 return true; 310 return true;
310 } 311 }
311 312
312 -
313 @Override 313 @Override
314 public void deviceConnected(DeviceId deviceId, 314 public void deviceConnected(DeviceId deviceId,
315 DeviceDescription deviceDescription) { 315 DeviceDescription deviceDescription) {
316 checkNotNull(deviceId, DEVICE_ID_NULL); 316 checkNotNull(deviceId, DEVICE_ID_NULL);
317 checkNotNull(deviceDescription, DEVICE_DESCRIPTION_NULL); 317 checkNotNull(deviceDescription, DEVICE_DESCRIPTION_NULL);
318 checkValidity(); 318 checkValidity();
319 - deviceDescription = validateDevice(deviceDescription, deviceId);
320 319
321 - // Establish my Role 320 + BasicDeviceConfig cfg = networkConfigService.getConfig(deviceId, BasicDeviceConfig.class);
321 + if (!isAllowed(cfg)) {
322 + log.warn("Device {} is not allowed", deviceId);
323 + return;
324 + }
325 + // Generate updated description and establish my Role
326 + deviceDescription = BasicDeviceOperator.combine(cfg, deviceDescription);
322 Futures.getUnchecked(mastershipService.requestRoleFor(deviceId) 327 Futures.getUnchecked(mastershipService.requestRoleFor(deviceId)
323 .thenAccept(role -> { 328 .thenAccept(role -> {
324 log.info("Local role is {} for {}", role, deviceId); 329 log.info("Local role is {} for {}", role, deviceId);
...@@ -327,22 +332,13 @@ public class DeviceManager ...@@ -327,22 +332,13 @@ public class DeviceManager
327 332
328 DeviceEvent event = store.createOrUpdateDevice(provider().id(), deviceId, 333 DeviceEvent event = store.createOrUpdateDevice(provider().id(), deviceId,
329 deviceDescription); 334 deviceDescription);
335 + log.info("Device {} connected", deviceId);
330 if (event != null) { 336 if (event != null) {
331 log.trace("event: {} {}", event.type(), event); 337 log.trace("event: {} {}", event.type(), event);
332 post(event); 338 post(event);
333 } 339 }
334 } 340 }
335 341
336 - // returns a DeviceDescription made from the union of the BasicDeviceConfig
337 - // annotations if it exists
338 - private DeviceDescription validateDevice(DeviceDescription deviceDescription, DeviceId deviceId) {
339 - BasicDeviceConfig cfg = networkConfigService.getConfig(deviceId, BasicDeviceConfig.class);
340 - checkState(cfg == null || cfg.isAllowed(), "Device " + deviceId + " is not allowed");
341 - log.info("Device {} connected", deviceId);
342 -
343 - return BasicDeviceOperator.combine(cfg, deviceDescription);
344 - }
345 -
346 @Override 342 @Override
347 public void deviceDisconnected(DeviceId deviceId) { 343 public void deviceDisconnected(DeviceId deviceId) {
348 checkNotNull(deviceId, DEVICE_ID_NULL); 344 checkNotNull(deviceId, DEVICE_ID_NULL);
...@@ -402,8 +398,7 @@ public class DeviceManager ...@@ -402,8 +398,7 @@ public class DeviceManager
402 public void updatePorts(DeviceId deviceId, 398 public void updatePorts(DeviceId deviceId,
403 List<PortDescription> portDescriptions) { 399 List<PortDescription> portDescriptions) {
404 checkNotNull(deviceId, DEVICE_ID_NULL); 400 checkNotNull(deviceId, DEVICE_ID_NULL);
405 - checkNotNull(portDescriptions, 401 + checkNotNull(portDescriptions, PORT_DESC_LIST_NULL);
406 - "Port descriptions list cannot be null");
407 checkValidity(); 402 checkValidity();
408 if (!mastershipService.isLocalMaster(deviceId)) { 403 if (!mastershipService.isLocalMaster(deviceId)) {
409 // Never been a master for this device 404 // Never been a master for this device
...@@ -411,7 +406,9 @@ public class DeviceManager ...@@ -411,7 +406,9 @@ public class DeviceManager
411 log.trace("Ignoring {} port updates on standby node. {}", deviceId, portDescriptions); 406 log.trace("Ignoring {} port updates on standby node. {}", deviceId, portDescriptions);
412 return; 407 return;
413 } 408 }
414 - 409 + portDescriptions = portDescriptions.stream()
410 + .map(e -> consolidate(deviceId, e))
411 + .collect(Collectors.toList());
415 List<DeviceEvent> events = store.updatePorts(this.provider().id(), 412 List<DeviceEvent> events = store.updatePorts(this.provider().id(),
416 deviceId, portDescriptions); 413 deviceId, portDescriptions);
417 for (DeviceEvent event : events) { 414 for (DeviceEvent event : events) {
...@@ -433,16 +430,28 @@ public class DeviceManager ...@@ -433,16 +430,28 @@ public class DeviceManager
433 portDescription); 430 portDescription);
434 return; 431 return;
435 } 432 }
436 - 433 + portDescription = consolidate(deviceId, portDescription);
437 final DeviceEvent event = store.updatePortStatus(this.provider().id(), 434 final DeviceEvent event = store.updatePortStatus(this.provider().id(),
438 deviceId, portDescription); 435 deviceId, portDescription);
439 if (event != null) { 436 if (event != null) {
440 - log.info("Device {} port {} status changed", deviceId, event 437 + log.info("Device {} port {} status changed", deviceId, event.port().number());
441 - .port().number());
442 post(event); 438 post(event);
443 } 439 }
444 } 440 }
445 441
442 + // merges the appropriate PortConfig with the description.
443 + private PortDescription consolidate(DeviceId did, PortDescription desc) {
444 + switch (desc.type()) {
445 + case COPPER:
446 + case VIRTUAL:
447 + return desc;
448 + default:
449 + OpticalPortConfig opc = networkConfigService.getConfig(
450 + new ConnectPoint(did, desc.portNumber()), OpticalPortConfig.class);
451 + return OpticalPortOperator.combine(opc, desc);
452 + }
453 + }
454 +
446 @Override 455 @Override
447 public void receivedRoleReply(DeviceId deviceId, MastershipRole requested, 456 public void receivedRoleReply(DeviceId deviceId, MastershipRole requested,
448 MastershipRole response) { 457 MastershipRole response) {
...@@ -498,6 +507,11 @@ public class DeviceManager ...@@ -498,6 +507,11 @@ public class DeviceManager
498 } 507 }
499 } 508 }
500 509
510 + // by default allowed, otherwise check flag
511 + private boolean isAllowed(BasicDeviceConfig cfg) {
512 + return (cfg == null || cfg.isAllowed());
513 + }
514 +
501 // Applies the specified role to the device; ignores NONE 515 // Applies the specified role to the device; ignores NONE
502 516
503 /** 517 /**
...@@ -617,7 +631,6 @@ public class DeviceManager ...@@ -617,7 +631,6 @@ public class DeviceManager
617 myNextRole = NONE; 631 myNextRole = NONE;
618 } 632 }
619 633
620 -
621 final boolean isReachable = isReachable(did); 634 final boolean isReachable = isReachable(did);
622 if (!isReachable) { 635 if (!isReachable) {
623 // device is not connected to this node 636 // device is not connected to this node
...@@ -695,23 +708,54 @@ public class DeviceManager ...@@ -695,23 +708,54 @@ public class DeviceManager
695 private class InternalNetworkConfigListener implements NetworkConfigListener { 708 private class InternalNetworkConfigListener implements NetworkConfigListener {
696 @Override 709 @Override
697 public boolean isRelevant(NetworkConfigEvent event) { 710 public boolean isRelevant(NetworkConfigEvent event) {
698 - return (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED || 711 + return (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED
699 - event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) 712 + || event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED)
700 - && event.configClass().equals(BasicDeviceConfig.class); 713 + && (event.configClass().equals(BasicDeviceConfig.class)
714 + || event.configClass().equals(OpticalPortConfig.class));
701 } 715 }
702 716
703 @Override 717 @Override
704 public void event(NetworkConfigEvent event) { 718 public void event(NetworkConfigEvent event) {
719 + DeviceEvent de = null;
720 + if (event.configClass().equals(BasicDeviceConfig.class)) {
705 log.info("Detected Device network config event {}", event.type()); 721 log.info("Detected Device network config event {}", event.type());
706 - kickOutBadDevice(((DeviceId) event.subject())); 722 + DeviceId did = (DeviceId) event.subject();
723 + BasicDeviceConfig cfg = networkConfigService.getConfig(did, BasicDeviceConfig.class);
724 +
725 + if (!isAllowed(cfg)) {
726 + kickOutBadDevice(did);
727 + } else {
728 + Device dev = getDevice(did);
729 + DeviceDescription desc = (dev == null) ? null : BasicDeviceOperator.descriptionOf(dev);
730 + desc = BasicDeviceOperator.combine(cfg, desc);
731 + if (getProvider(did) != null) {
732 + de = store.createOrUpdateDevice(getProvider(did).id(), did, desc);
733 + }
734 + }
735 + }
736 + if (event.configClass().equals(OpticalPortConfig.class)) {
737 + ConnectPoint cpt = (ConnectPoint) event.subject();
738 + DeviceId did = cpt.deviceId();
739 + Port dpt = getPort(did, cpt.port());
740 +
741 + if (dpt != null) {
742 + OpticalPortConfig opc = networkConfigService.getConfig(cpt, OpticalPortConfig.class);
743 + PortDescription desc = OpticalPortOperator.descriptionOf(dpt);
744 + desc = OpticalPortOperator.combine(opc, desc);
745 + if (getProvider(did) != null) {
746 + de = store.updatePortStatus(getProvider(did).id(), did, desc);
747 + }
748 + }
749 + }
750 +
751 + if (de != null) {
752 + post(de);
707 } 753 }
708 } 754 }
709 755
710 // checks if the specified device is allowed by the BasicDeviceConfig 756 // checks if the specified device is allowed by the BasicDeviceConfig
711 // and if not, removes it 757 // and if not, removes it
712 private void kickOutBadDevice(DeviceId deviceId) { 758 private void kickOutBadDevice(DeviceId deviceId) {
713 - BasicDeviceConfig cfg = networkConfigService.getConfig(deviceId, BasicDeviceConfig.class);
714 - if (!cfg.isAllowed()) {
715 Device badDevice = getDevice(deviceId); 759 Device badDevice = getDevice(deviceId);
716 if (badDevice != null) { 760 if (badDevice != null) {
717 removeDevice(deviceId); 761 removeDevice(deviceId);
......
...@@ -16,11 +16,16 @@ ...@@ -16,11 +16,16 @@
16 package org.onosproject.net.device.impl; 16 package org.onosproject.net.device.impl;
17 17
18 import static org.slf4j.LoggerFactory.getLogger; 18 import static org.slf4j.LoggerFactory.getLogger;
19 +import static com.google.common.base.Preconditions.checkNotNull;
19 20
20 import org.onosproject.net.config.ConfigOperator; 21 import org.onosproject.net.config.ConfigOperator;
21 import org.onosproject.net.config.basics.OpticalPortConfig; 22 import org.onosproject.net.config.basics.OpticalPortConfig;
22 import org.onosproject.net.AnnotationKeys; 23 import org.onosproject.net.AnnotationKeys;
23 import org.onosproject.net.DefaultAnnotations; 24 import org.onosproject.net.DefaultAnnotations;
25 +import org.onosproject.net.OchPort;
26 +import org.onosproject.net.OduCltPort;
27 +import org.onosproject.net.OmsPort;
28 +import org.onosproject.net.Port;
24 import org.onosproject.net.PortNumber; 29 import org.onosproject.net.PortNumber;
25 import org.onosproject.net.SparseAnnotations; 30 import org.onosproject.net.SparseAnnotations;
26 import org.onosproject.net.device.DefaultPortDescription; 31 import org.onosproject.net.device.DefaultPortDescription;
...@@ -137,4 +142,32 @@ public final class OpticalPortOperator implements ConfigOperator { ...@@ -137,4 +142,32 @@ public final class OpticalPortOperator implements ConfigOperator {
137 } 142 }
138 return DefaultAnnotations.union(an, b.build()); 143 return DefaultAnnotations.union(an, b.build());
139 } 144 }
145 +
146 + /**
147 + * Returns a description built from an existing port.
148 + *
149 + * @param port the device port
150 + * @return a PortDescription based on the port
151 + */
152 + public static PortDescription descriptionOf(Port port) {
153 + checkNotNull(port, "Must supply non-null Port");
154 + final PortNumber ptn = port.number();
155 + final boolean isup = port.isEnabled();
156 + final SparseAnnotations an = (SparseAnnotations) port.annotations();
157 + switch (port.type()) {
158 + case OMS:
159 + OmsPort oms = (OmsPort) port;
160 + return new OmsPortDescription(ptn, isup, oms.minFrequency(),
161 + oms.maxFrequency(), oms.grid(), an);
162 + case OCH:
163 + OchPort och = (OchPort) port;
164 + return new OchPortDescription(ptn, isup, och.signalType(),
165 + och.isTunable(), och.lambda(), an);
166 + case ODUCLT:
167 + OduCltPort odu = (OduCltPort) port;
168 + return new OduCltPortDescription(ptn, isup, odu.signalType(), an);
169 + default:
170 + return new DefaultPortDescription(ptn, isup, port.type(), port.portSpeed(), an);
171 + }
172 + }
140 } 173 }
......