Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.device.impl; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 17 | |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
| 20 | import static org.onlab.util.Tools.groupedThreads; |
Thomas Vachuska | fa8aa2f | 2015-10-13 11:56:59 -0700 | [diff] [blame] | 21 | import static org.onlab.util.Tools.nullIsNotFound; |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 22 | import static org.onosproject.net.MastershipRole.MASTER; |
| 23 | import static org.onosproject.net.MastershipRole.NONE; |
| 24 | import static org.onosproject.net.MastershipRole.STANDBY; |
HIGUCHI Yuta | 6972ae6 | 2016-05-12 19:57:46 -0700 | [diff] [blame^] | 25 | import static org.onosproject.net.optical.device.OchPortHelper.ochPortDescription; |
| 26 | import static org.onosproject.net.optical.device.OduCltPortHelper.oduCltPortDescription; |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 27 | import static org.onosproject.security.AppGuard.checkPermission; |
| 28 | import static org.onosproject.security.AppPermission.Type.DEVICE_READ; |
| 29 | import static org.slf4j.LoggerFactory.getLogger; |
| 30 | |
| 31 | import java.util.Collection; |
| 32 | import java.util.HashSet; |
| 33 | import java.util.List; |
| 34 | import java.util.Objects; |
| 35 | import java.util.Set; |
| 36 | import java.util.concurrent.CompletableFuture; |
| 37 | import java.util.concurrent.ExecutionException; |
| 38 | import java.util.concurrent.ScheduledExecutorService; |
| 39 | import java.util.concurrent.TimeUnit; |
| 40 | import java.util.stream.Collectors; |
Madan Jampani | 565a66a | 2015-07-25 17:01:13 -0700 | [diff] [blame] | 41 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 42 | import org.apache.felix.scr.annotations.Activate; |
| 43 | import org.apache.felix.scr.annotations.Component; |
| 44 | import org.apache.felix.scr.annotations.Deactivate; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 45 | import org.apache.felix.scr.annotations.Reference; |
| 46 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 47 | import org.apache.felix.scr.annotations.Service; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 48 | import org.onosproject.cluster.ClusterService; |
| 49 | import org.onosproject.cluster.NodeId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 50 | import org.onosproject.mastership.MastershipEvent; |
| 51 | import org.onosproject.mastership.MastershipListener; |
| 52 | import org.onosproject.mastership.MastershipService; |
| 53 | import org.onosproject.mastership.MastershipTerm; |
| 54 | import org.onosproject.mastership.MastershipTermService; |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 55 | import org.onosproject.net.ConnectPoint; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 56 | import org.onosproject.net.Device; |
samuel | 738dfaf | 2015-07-11 11:08:57 +0800 | [diff] [blame] | 57 | import org.onosproject.net.Device.Type; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 58 | import org.onosproject.net.DeviceId; |
| 59 | import org.onosproject.net.MastershipRole; |
| 60 | import org.onosproject.net.Port; |
| 61 | import org.onosproject.net.PortNumber; |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 62 | import org.onosproject.net.config.NetworkConfigEvent; |
| 63 | import org.onosproject.net.config.NetworkConfigListener; |
| 64 | import org.onosproject.net.config.NetworkConfigService; |
| 65 | import org.onosproject.net.config.basics.BasicDeviceConfig; |
| 66 | import org.onosproject.net.config.basics.OpticalPortConfig; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 67 | import org.onosproject.net.device.DefaultDeviceDescription; |
| 68 | import org.onosproject.net.device.DefaultPortDescription; |
| 69 | import org.onosproject.net.device.DeviceAdminService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 70 | import org.onosproject.net.device.DeviceDescription; |
| 71 | import org.onosproject.net.device.DeviceEvent; |
| 72 | import org.onosproject.net.device.DeviceListener; |
| 73 | import org.onosproject.net.device.DeviceProvider; |
| 74 | import org.onosproject.net.device.DeviceProviderRegistry; |
| 75 | import org.onosproject.net.device.DeviceProviderService; |
| 76 | import org.onosproject.net.device.DeviceService; |
| 77 | import org.onosproject.net.device.DeviceStore; |
| 78 | import org.onosproject.net.device.DeviceStoreDelegate; |
HIGUCHI Yuta | 6972ae6 | 2016-05-12 19:57:46 -0700 | [diff] [blame^] | 79 | import org.onosproject.net.device.OchPortDescription; |
| 80 | import org.onosproject.net.device.OduCltPortDescription; |
| 81 | import org.onosproject.net.device.OmsPortDescription; |
| 82 | import org.onosproject.net.device.OtuPortDescription; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 83 | import org.onosproject.net.device.PortDescription; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 84 | import org.onosproject.net.device.PortStatistics; |
HIGUCHI Yuta | 6972ae6 | 2016-05-12 19:57:46 -0700 | [diff] [blame^] | 85 | import org.onosproject.net.optical.device.OmsPortHelper; |
| 86 | import org.onosproject.net.optical.device.OtuPortHelper; |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 87 | import org.onosproject.net.provider.AbstractListenerProviderRegistry; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 88 | import org.onosproject.net.provider.AbstractProviderService; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 89 | import org.slf4j.Logger; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 90 | |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 91 | import com.google.common.util.concurrent.Futures; |
Jonathan Hart | 2f66936 | 2015-02-11 16:19:20 -0800 | [diff] [blame] | 92 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 93 | /** |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 94 | * Provides implementation of the device SB & NB APIs. |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 95 | */ |
| 96 | @Component(immediate = true) |
| 97 | @Service |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 98 | public class DeviceManager |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 99 | extends AbstractListenerProviderRegistry<DeviceEvent, DeviceListener, DeviceProvider, DeviceProviderService> |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 100 | implements DeviceService, DeviceAdminService, DeviceProviderRegistry { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 101 | |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 102 | private static final String DEVICE_ID_NULL = "Device ID cannot be null"; |
| 103 | private static final String PORT_NUMBER_NULL = "Port number cannot be null"; |
| 104 | private static final String DEVICE_DESCRIPTION_NULL = "Device description cannot be null"; |
| 105 | private static final String PORT_DESCRIPTION_NULL = "Port description cannot be null"; |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 106 | private static final String PORT_DESC_LIST_NULL = "Port description list cannot be null"; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 107 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 108 | private final Logger log = getLogger(getClass()); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 109 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 110 | private final DeviceStoreDelegate delegate = new InternalStoreDelegate(); |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 111 | |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 112 | private final MastershipListener mastershipListener = new InternalMastershipListener(); |
Madan Jampani | de003d9 | 2015-05-11 17:14:20 -0700 | [diff] [blame] | 113 | private NodeId localNodeId; |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 114 | |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 115 | private ScheduledExecutorService backgroundService; |
| 116 | |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 117 | private final NetworkConfigListener networkConfigListener = new InternalNetworkConfigListener(); |
| 118 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 119 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 120 | protected DeviceStore store; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 121 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 122 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 123 | protected ClusterService clusterService; |
| 124 | |
| 125 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 126 | protected MastershipService mastershipService; |
| 127 | |
Yuta HIGUCHI | bcac499 | 2014-11-22 19:27:57 -0800 | [diff] [blame] | 128 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Ayaka Koshibe | 3de43ca | 2014-09-26 16:40:23 -0700 | [diff] [blame] | 129 | protected MastershipTermService termService; |
| 130 | |
Madan Jampani | 61056bc | 2014-09-27 09:07:26 -0700 | [diff] [blame] | 131 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 132 | protected NetworkConfigService networkConfigService; |
| 133 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 134 | @Activate |
| 135 | public void activate() { |
HIGUCHI Yuta | 060da9a | 2016-03-11 19:16:35 -0800 | [diff] [blame] | 136 | backgroundService = newSingleThreadScheduledExecutor( |
| 137 | groupedThreads("onos/device", "manager-background", log)); |
Madan Jampani | de003d9 | 2015-05-11 17:14:20 -0700 | [diff] [blame] | 138 | localNodeId = clusterService.getLocalNode().id(); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 139 | |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 140 | store.setDelegate(delegate); |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 141 | eventDispatcher.addSink(DeviceEvent.class, listenerRegistry); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 142 | mastershipService.addListener(mastershipListener); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 143 | networkConfigService.addListener(networkConfigListener); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 144 | |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 145 | backgroundService.scheduleWithFixedDelay(() -> { |
| 146 | try { |
| 147 | mastershipCheck(); |
| 148 | } catch (Exception e) { |
| 149 | log.error("Exception thrown during integrity check", e); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 150 | } |
| 151 | }, 1, 1, TimeUnit.MINUTES); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 152 | log.info("Started"); |
| 153 | } |
| 154 | |
| 155 | @Deactivate |
| 156 | public void deactivate() { |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 157 | backgroundService.shutdown(); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 158 | networkConfigService.removeListener(networkConfigListener); |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 159 | store.unsetDelegate(delegate); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 160 | mastershipService.removeListener(mastershipListener); |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 161 | eventDispatcher.removeSink(DeviceEvent.class); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 162 | log.info("Stopped"); |
| 163 | } |
| 164 | |
| 165 | @Override |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 166 | public int getDeviceCount() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 167 | checkPermission(DEVICE_READ); |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 168 | return store.getDeviceCount(); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | @Override |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 172 | public Iterable<Device> getDevices() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 173 | checkPermission(DEVICE_READ); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 174 | return store.getDevices(); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 175 | } |
| 176 | |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 177 | @Override |
Yuta HIGUCHI | f1f2ac0 | 2014-11-26 14:02:22 -0800 | [diff] [blame] | 178 | public Iterable<Device> getAvailableDevices() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 179 | checkPermission(DEVICE_READ); |
Yuta HIGUCHI | f1f2ac0 | 2014-11-26 14:02:22 -0800 | [diff] [blame] | 180 | return store.getAvailableDevices(); |
| 181 | } |
| 182 | |
| 183 | @Override |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 184 | public Device getDevice(DeviceId deviceId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 185 | checkPermission(DEVICE_READ); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 186 | checkNotNull(deviceId, DEVICE_ID_NULL); |
tom | 132b58a | 2014-08-28 16:11:28 -0700 | [diff] [blame] | 187 | return store.getDevice(deviceId); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | @Override |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 191 | public MastershipRole getRole(DeviceId deviceId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 192 | checkPermission(DEVICE_READ); |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 193 | checkNotNull(deviceId, DEVICE_ID_NULL); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 194 | return mastershipService.getLocalRole(deviceId); |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | @Override |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 198 | public List<Port> getPorts(DeviceId deviceId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 199 | checkPermission(DEVICE_READ); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 200 | checkNotNull(deviceId, DEVICE_ID_NULL); |
tom | 132b58a | 2014-08-28 16:11:28 -0700 | [diff] [blame] | 201 | return store.getPorts(deviceId); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | @Override |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 205 | public List<PortStatistics> getPortStatistics(DeviceId deviceId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 206 | checkPermission(DEVICE_READ); |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 207 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 208 | return store.getPortStatistics(deviceId); |
| 209 | } |
| 210 | |
| 211 | @Override |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 212 | public List<PortStatistics> getPortDeltaStatistics(DeviceId deviceId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 213 | checkPermission(DEVICE_READ); |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 214 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 215 | return store.getPortDeltaStatistics(deviceId); |
| 216 | } |
| 217 | |
| 218 | @Override |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 219 | public Port getPort(DeviceId deviceId, PortNumber portNumber) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 220 | checkPermission(DEVICE_READ); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 221 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 222 | checkNotNull(portNumber, PORT_NUMBER_NULL); |
tom | 132b58a | 2014-08-28 16:11:28 -0700 | [diff] [blame] | 223 | return store.getPort(deviceId, portNumber); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | @Override |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 227 | public boolean isAvailable(DeviceId deviceId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 228 | checkPermission(DEVICE_READ); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 229 | |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 230 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 231 | return store.isAvailable(deviceId); |
| 232 | } |
| 233 | |
Ayaka Koshibe | e60d452 | 2014-10-28 15:07:00 -0700 | [diff] [blame] | 234 | // Check a device for control channel connectivity. |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 235 | private boolean isReachable(DeviceId deviceId) { |
Ayaka Koshibe | 78bcbc1 | 2014-11-19 14:28:58 -0800 | [diff] [blame] | 236 | if (deviceId == null) { |
| 237 | return false; |
| 238 | } |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 239 | DeviceProvider provider = getProvider(deviceId); |
| 240 | if (provider != null) { |
| 241 | return provider.isReachable(deviceId); |
| 242 | } else { |
Yuta HIGUCHI | 72669c4 | 2014-11-13 14:48:17 -0800 | [diff] [blame] | 243 | log.debug("Provider not found for {}", deviceId); |
Ayaka Koshibe | e60d452 | 2014-10-28 15:07:00 -0700 | [diff] [blame] | 244 | return false; |
| 245 | } |
Ayaka Koshibe | e8708e3 | 2014-10-22 13:40:18 -0700 | [diff] [blame] | 246 | } |
| 247 | |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 248 | @Override |
| 249 | public void removeDevice(DeviceId deviceId) { |
| 250 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 251 | DeviceEvent event = store.removeDevice(deviceId); |
tom | 0efbb1d | 2014-09-09 11:54:28 -0700 | [diff] [blame] | 252 | if (event != null) { |
| 253 | log.info("Device {} administratively removed", deviceId); |
| 254 | post(event); |
| 255 | } |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 256 | } |
| 257 | |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 258 | @Override |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 259 | public void changePortState(DeviceId deviceId, PortNumber portNumber, |
| 260 | boolean enable) { |
| 261 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 262 | checkNotNull(deviceId, PORT_NUMBER_NULL); |
| 263 | DeviceProvider provider = getProvider(deviceId); |
| 264 | if (provider != null) { |
| 265 | log.warn("Port {} on device {} being administratively brought {}", |
| 266 | portNumber, deviceId, |
| 267 | (enable) ? "UP" : "DOWN"); |
| 268 | provider.changePortState(deviceId, portNumber, enable); |
| 269 | } else { |
| 270 | log.warn("Provider not found for {}", deviceId); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | @Override |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 275 | protected DeviceProviderService createProviderService( |
| 276 | DeviceProvider provider) { |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 277 | return new InternalDeviceProviderService(provider); |
| 278 | } |
| 279 | |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 280 | /** |
| 281 | * Checks if all the reachable devices have a valid mastership role. |
| 282 | */ |
| 283 | private void mastershipCheck() { |
| 284 | log.debug("Checking mastership"); |
| 285 | for (Device device : getDevices()) { |
| 286 | final DeviceId deviceId = device.id(); |
Jonathan Hart | 2f66936 | 2015-02-11 16:19:20 -0800 | [diff] [blame] | 287 | log.trace("Checking device {}", deviceId); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 288 | |
| 289 | if (!isReachable(deviceId)) { |
| 290 | continue; |
| 291 | } |
| 292 | |
| 293 | if (mastershipService.getLocalRole(deviceId) != NONE) { |
| 294 | continue; |
| 295 | } |
| 296 | |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 297 | log.info("{} is reachable but did not have a valid role, reasserting", deviceId); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 298 | |
| 299 | // isReachable but was not MASTER or STANDBY, get a role and apply |
| 300 | // Note: NONE triggers request to MastershipService |
| 301 | reassertRole(deviceId, NONE); |
| 302 | } |
| 303 | } |
| 304 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 305 | // Personalized device provider service issued to the supplied provider. |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 306 | private class InternalDeviceProviderService |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 307 | extends AbstractProviderService<DeviceProvider> |
| 308 | implements DeviceProviderService { |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 309 | |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 310 | InternalDeviceProviderService(DeviceProvider provider) { |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 311 | super(provider); |
| 312 | } |
| 313 | |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 314 | /** |
| 315 | * Apply role in reaction to provider event. |
| 316 | * |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 317 | * @param deviceId device identifier |
| 318 | * @param newRole new role to apply to the device |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 319 | * @return true if the request was sent to provider |
| 320 | */ |
| 321 | private boolean applyRole(DeviceId deviceId, MastershipRole newRole) { |
| 322 | |
| 323 | if (newRole.equals(MastershipRole.NONE)) { |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 324 | //no-op |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 325 | return true; |
| 326 | } |
| 327 | |
| 328 | DeviceProvider provider = provider(); |
| 329 | if (provider == null) { |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 330 | log.warn("Provider for {} was not found. Cannot apply role {}", |
| 331 | deviceId, newRole); |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 332 | return false; |
| 333 | } |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 334 | provider.roleChanged(deviceId, newRole); |
| 335 | // not triggering probe when triggered by provider service event |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 336 | |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 337 | return true; |
| 338 | } |
| 339 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 340 | @Override |
alshabib | b7b4063 | 2014-09-28 21:30:00 -0700 | [diff] [blame] | 341 | public void deviceConnected(DeviceId deviceId, |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 342 | DeviceDescription deviceDescription) { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 343 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 344 | checkNotNull(deviceDescription, DEVICE_DESCRIPTION_NULL); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 345 | checkValidity(); |
Yuta HIGUCHI | 24b2e2a | 2014-10-07 15:53:57 -0700 | [diff] [blame] | 346 | |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 347 | BasicDeviceConfig cfg = networkConfigService.getConfig(deviceId, BasicDeviceConfig.class); |
| 348 | if (!isAllowed(cfg)) { |
| 349 | log.warn("Device {} is not allowed", deviceId); |
| 350 | return; |
| 351 | } |
| 352 | // Generate updated description and establish my Role |
| 353 | deviceDescription = BasicDeviceOperator.combine(cfg, deviceDescription); |
Madan Jampani | 565a66a | 2015-07-25 17:01:13 -0700 | [diff] [blame] | 354 | Futures.getUnchecked(mastershipService.requestRoleFor(deviceId) |
| 355 | .thenAccept(role -> { |
| 356 | log.info("Local role is {} for {}", role, deviceId); |
| 357 | applyRole(deviceId, role); |
| 358 | })); |
HIGUCHI Yuta | 11530fb | 2015-05-27 13:10:20 -0700 | [diff] [blame] | 359 | |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 360 | DeviceEvent event = store.createOrUpdateDevice(provider().id(), deviceId, |
| 361 | deviceDescription); |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 362 | log.info("Device {} connected", deviceId); |
tom | 80c0e5e | 2014-09-08 18:08:58 -0700 | [diff] [blame] | 363 | if (event != null) { |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 364 | log.trace("event: {} {}", event.type(), event); |
tom | 568581d | 2014-09-08 20:13:36 -0700 | [diff] [blame] | 365 | post(event); |
tom | 80c0e5e | 2014-09-08 18:08:58 -0700 | [diff] [blame] | 366 | } |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 367 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | @Override |
| 371 | public void deviceDisconnected(DeviceId deviceId) { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 372 | checkNotNull(deviceId, DEVICE_ID_NULL); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 373 | checkValidity(); |
Yuta HIGUCHI | cf60390 | 2014-10-07 23:04:32 -0700 | [diff] [blame] | 374 | |
Yuta HIGUCHI | 2d3cd31 | 2014-10-31 11:38:04 -0700 | [diff] [blame] | 375 | log.info("Device {} disconnected from this node", deviceId); |
Ayaka Koshibe | d9f693e | 2014-09-29 18:04:54 -0700 | [diff] [blame] | 376 | |
alshabib | afc514a | 2014-12-01 14:44:05 -0800 | [diff] [blame] | 377 | List<Port> ports = store.getPorts(deviceId); |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 378 | final Device device = getDevice(deviceId); |
| 379 | |
| 380 | List<PortDescription> descs = ports.stream().map( |
Rimon Ashkenazy | 8ebfff0 | 2016-02-01 11:56:36 +0200 | [diff] [blame] | 381 | port -> (!(Device.Type.ROADM.equals(device.type()) || |
Aneesha Pailla | d5a8f01 | 2016-05-04 11:45:17 -0700 | [diff] [blame] | 382 | (Device.Type.OTN.equals(device.type())) || |
| 383 | (Device.Type.FIBER_SWITCH.equals(device.type())))) ? |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 384 | new DefaultPortDescription(port.number(), false, |
| 385 | port.type(), port.portSpeed()) : |
| 386 | OpticalPortOperator.descriptionOf(port, false) |
| 387 | ).collect(Collectors.toList()); |
| 388 | |
alshabib | afc514a | 2014-12-01 14:44:05 -0800 | [diff] [blame] | 389 | store.updatePorts(this.provider().id(), deviceId, descs); |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 390 | try { |
Madan Jampani | 565a66a | 2015-07-25 17:01:13 -0700 | [diff] [blame] | 391 | if (mastershipService.isLocalMaster(deviceId)) { |
Thomas Vachuska | 5f429d6 | 2015-05-28 15:34:36 -0700 | [diff] [blame] | 392 | post(store.markOffline(deviceId)); |
| 393 | } |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 394 | } catch (IllegalStateException e) { |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 395 | log.warn("Failed to mark {} offline", deviceId); |
| 396 | // only the MASTER should be marking off-line in normal cases, |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 397 | // but if I was the last STANDBY connection, etc. and no one else |
| 398 | // was there to mark the device offline, this instance may need to |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 399 | // temporarily request for Master Role and mark offline. |
| 400 | |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 401 | //there are times when this node will correctly have mastership, BUT |
| 402 | //that isn't reflected in the ClockManager before the device disconnects. |
| 403 | //we want to let go of the device anyways, so make sure this happens. |
Yuta HIGUCHI | 0722fb2 | 2014-10-19 01:16:33 -0700 | [diff] [blame] | 404 | |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 405 | // FIXME: Store semantics leaking out as IllegalStateException. |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 406 | // Consider revising store API to handle this scenario. |
| 407 | CompletableFuture<MastershipRole> roleFuture = mastershipService.requestRoleFor(deviceId); |
Madan Jampani | de003d9 | 2015-05-11 17:14:20 -0700 | [diff] [blame] | 408 | roleFuture.whenComplete((role, error) -> { |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 409 | MastershipTerm term = termService.getMastershipTerm(deviceId); |
| 410 | // TODO: Move this type of check inside device clock manager, etc. |
| 411 | if (term != null && localNodeId.equals(term.master())) { |
| 412 | log.info("Retry marking {} offline", deviceId); |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 413 | post(store.markOffline(deviceId)); |
| 414 | } else { |
| 415 | log.info("Failed again marking {} offline. {}", deviceId, role); |
| 416 | } |
| 417 | }); |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 418 | } finally { |
Madan Jampani | c6e574f | 2015-05-29 13:41:52 -0700 | [diff] [blame] | 419 | try { |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 420 | //relinquish master role and ability to be backup. |
Madan Jampani | c6e574f | 2015-05-29 13:41:52 -0700 | [diff] [blame] | 421 | mastershipService.relinquishMastership(deviceId).get(); |
| 422 | } catch (InterruptedException e) { |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 423 | log.warn("Interrupted while reliquishing role for {}", deviceId); |
Madan Jampani | c6e574f | 2015-05-29 13:41:52 -0700 | [diff] [blame] | 424 | Thread.currentThread().interrupt(); |
| 425 | } catch (ExecutionException e) { |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 426 | log.error("Exception thrown while relinquishing role for {}", deviceId, e); |
Madan Jampani | c6e574f | 2015-05-29 13:41:52 -0700 | [diff] [blame] | 427 | } |
tom | 0efbb1d | 2014-09-09 11:54:28 -0700 | [diff] [blame] | 428 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 429 | } |
| 430 | |
HIGUCHI Yuta | 6972ae6 | 2016-05-12 19:57:46 -0700 | [diff] [blame^] | 431 | /** |
| 432 | * Transforms optical specific PortDescription to generic PortDescription. |
| 433 | * |
| 434 | * @param descr PortDescription |
| 435 | * @return generic PortDescription |
| 436 | * @deprecated in Goldeneye (1.6.0) |
| 437 | */ |
| 438 | @Deprecated |
| 439 | private PortDescription ensureGeneric(PortDescription descr) { |
| 440 | switch (descr.type()) { |
| 441 | case OCH: |
| 442 | if (descr instanceof OchPortDescription) { |
| 443 | OchPortDescription och = (OchPortDescription) descr; |
| 444 | return ochPortDescription(och, |
| 445 | och.signalType(), |
| 446 | och.isTunable(), |
| 447 | och.lambda(), |
| 448 | och.annotations()); |
| 449 | } |
| 450 | break; |
| 451 | case ODUCLT: |
| 452 | if (descr instanceof OduCltPortDescription) { |
| 453 | OduCltPortDescription clt = (OduCltPortDescription) descr; |
| 454 | return oduCltPortDescription(clt, |
| 455 | clt.signalType(), |
| 456 | clt.annotations()); |
| 457 | } |
| 458 | break; |
| 459 | case OMS: |
| 460 | if (descr instanceof OmsPortDescription) { |
| 461 | OmsPortDescription oms = (OmsPortDescription) descr; |
| 462 | return OmsPortHelper.omsPortDescription(oms, |
| 463 | oms.minFrequency(), |
| 464 | oms.maxFrequency(), |
| 465 | oms.grid(), |
| 466 | oms.annotations()); |
| 467 | } |
| 468 | break; |
| 469 | case OTU: |
| 470 | if (descr instanceof OtuPortDescription) { |
| 471 | OtuPortDescription otu = (OtuPortDescription) descr; |
| 472 | return OtuPortHelper.otuPortDescription(otu, |
| 473 | otu.signalType(), |
| 474 | otu.annotations()); |
| 475 | } |
| 476 | break; |
| 477 | |
| 478 | default: |
| 479 | // no-op |
| 480 | break; |
| 481 | } |
| 482 | return descr; |
| 483 | } |
| 484 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 485 | @Override |
alshabib | b7b4063 | 2014-09-28 21:30:00 -0700 | [diff] [blame] | 486 | public void updatePorts(DeviceId deviceId, |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 487 | List<PortDescription> portDescriptions) { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 488 | checkNotNull(deviceId, DEVICE_ID_NULL); |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 489 | checkNotNull(portDescriptions, PORT_DESC_LIST_NULL); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 490 | checkValidity(); |
Madan Jampani | 565a66a | 2015-07-25 17:01:13 -0700 | [diff] [blame] | 491 | if (!mastershipService.isLocalMaster(deviceId)) { |
Yuta HIGUCHI | 13c0b87 | 2014-10-30 18:09:22 -0700 | [diff] [blame] | 492 | // Never been a master for this device |
| 493 | // any update will be ignored. |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 494 | log.trace("Ignoring {} port updates on standby node. {}", deviceId, portDescriptions); |
Yuta HIGUCHI | 13c0b87 | 2014-10-30 18:09:22 -0700 | [diff] [blame] | 495 | return; |
| 496 | } |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 497 | portDescriptions = portDescriptions.stream() |
| 498 | .map(e -> consolidate(deviceId, e)) |
HIGUCHI Yuta | 6972ae6 | 2016-05-12 19:57:46 -0700 | [diff] [blame^] | 499 | .map(this::ensureGeneric) |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 500 | .collect(Collectors.toList()); |
Yuta HIGUCHI | 5f6739c | 2014-10-01 14:04:01 -0700 | [diff] [blame] | 501 | List<DeviceEvent> events = store.updatePorts(this.provider().id(), |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 502 | deviceId, portDescriptions); |
Thomas Vachuska | 5923b9a | 2016-01-26 10:52:57 -0800 | [diff] [blame] | 503 | if (events != null) { |
| 504 | for (DeviceEvent event : events) { |
| 505 | post(event); |
| 506 | } |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 507 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | @Override |
alshabib | b7b4063 | 2014-09-28 21:30:00 -0700 | [diff] [blame] | 511 | public void portStatusChanged(DeviceId deviceId, |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 512 | PortDescription portDescription) { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 513 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 514 | checkNotNull(portDescription, PORT_DESCRIPTION_NULL); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 515 | checkValidity(); |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 516 | |
Madan Jampani | 565a66a | 2015-07-25 17:01:13 -0700 | [diff] [blame] | 517 | if (!mastershipService.isLocalMaster(deviceId)) { |
Yuta HIGUCHI | 13c0b87 | 2014-10-30 18:09:22 -0700 | [diff] [blame] | 518 | // Never been a master for this device |
| 519 | // any update will be ignored. |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 520 | log.trace("Ignoring {} port update on standby node. {}", deviceId, |
| 521 | portDescription); |
Yuta HIGUCHI | 13c0b87 | 2014-10-30 18:09:22 -0700 | [diff] [blame] | 522 | return; |
| 523 | } |
Thomas Vachuska | fa8aa2f | 2015-10-13 11:56:59 -0700 | [diff] [blame] | 524 | Device device = nullIsNotFound(getDevice(deviceId), "Device not found"); |
Rimon Ashkenazy | 8ebfff0 | 2016-02-01 11:56:36 +0200 | [diff] [blame] | 525 | if ((Device.Type.ROADM.equals(device.type())) || |
| 526 | (Device.Type.OTN.equals(device.type()))) { |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 527 | Port port = getPort(deviceId, portDescription.portNumber()); |
HIGUCHI Yuta | 6972ae6 | 2016-05-12 19:57:46 -0700 | [diff] [blame^] | 528 | // FIXME This is ignoring all other info in portDescription given as input?? |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 529 | portDescription = OpticalPortOperator.descriptionOf(port, portDescription.isEnabled()); |
| 530 | } |
| 531 | |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 532 | portDescription = consolidate(deviceId, portDescription); |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 533 | final DeviceEvent event = store.updatePortStatus(this.provider().id(), |
HIGUCHI Yuta | 6972ae6 | 2016-05-12 19:57:46 -0700 | [diff] [blame^] | 534 | deviceId, |
| 535 | ensureGeneric(portDescription)); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 536 | if (event != null) { |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 537 | log.info("Device {} port {} status changed", deviceId, event.port().number()); |
tom | 0efbb1d | 2014-09-09 11:54:28 -0700 | [diff] [blame] | 538 | post(event); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 539 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 540 | } |
tom | 3f2bbd7 | 2014-09-24 12:07:58 -0700 | [diff] [blame] | 541 | |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 542 | // merges the appropriate PortConfig with the description. |
| 543 | private PortDescription consolidate(DeviceId did, PortDescription desc) { |
| 544 | switch (desc.type()) { |
| 545 | case COPPER: |
| 546 | case VIRTUAL: |
| 547 | return desc; |
| 548 | default: |
| 549 | OpticalPortConfig opc = networkConfigService.getConfig( |
| 550 | new ConnectPoint(did, desc.portNumber()), OpticalPortConfig.class); |
| 551 | return OpticalPortOperator.combine(opc, desc); |
| 552 | } |
| 553 | } |
| 554 | |
tom | 3f2bbd7 | 2014-09-24 12:07:58 -0700 | [diff] [blame] | 555 | @Override |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 556 | public void receivedRoleReply(DeviceId deviceId, MastershipRole requested, |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame] | 557 | MastershipRole response) { |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 558 | // Several things can happen here: |
| 559 | // 1. request and response match |
| 560 | // 2. request and response don't match |
| 561 | // 3. MastershipRole and requested match (and 1 or 2 are true) |
| 562 | // 4. MastershipRole and requested don't match (and 1 or 2 are true) |
| 563 | // |
| 564 | // 2, 4, and 3 with case 2 are failure modes. |
| 565 | |
tom | 3f2bbd7 | 2014-09-24 12:07:58 -0700 | [diff] [blame] | 566 | // FIXME: implement response to this notification |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 567 | |
Madan Jampani | f2af771 | 2015-05-29 18:43:52 -0700 | [diff] [blame] | 568 | log.debug("got reply to a role request for {}: asked for {}, and got {}", |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 569 | deviceId, requested, response); |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 570 | |
| 571 | if (requested == null && response == null) { |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 572 | // something was off with DeviceProvider, maybe check channel too? |
Shashikanth VH | 387a1ca | 2016-02-09 20:35:21 +0530 | [diff] [blame] | 573 | log.warn("Failed to assert role onto Device {}", deviceId); |
Yuta HIGUCHI | cf60390 | 2014-10-07 23:04:32 -0700 | [diff] [blame] | 574 | mastershipService.relinquishMastership(deviceId); |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 575 | return; |
Yuta HIGUCHI | cf60390 | 2014-10-07 23:04:32 -0700 | [diff] [blame] | 576 | } |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 577 | |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame] | 578 | if (Objects.equals(requested, response)) { |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 579 | if (Objects.equals(requested, mastershipService.getLocalRole(deviceId))) { |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 580 | return; |
| 581 | } else { |
HIGUCHI Yuta | 1979f55 | 2015-12-28 21:24:26 -0800 | [diff] [blame] | 582 | log.warn("Role mismatch on {}. set to {}, but store demands {}", |
| 583 | deviceId, response, mastershipService.getLocalRole(deviceId)); |
| 584 | // roleManager got the device to comply, but doesn't agree with |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 585 | // the store; use the store's view, then try to reassert. |
HIGUCHI Yuta | 060da9a | 2016-03-11 19:16:35 -0800 | [diff] [blame] | 586 | backgroundService.execute(() -> reassertRole(deviceId, mastershipService.getLocalRole(deviceId))); |
HIGUCHI Yuta | 1979f55 | 2015-12-28 21:24:26 -0800 | [diff] [blame] | 587 | return; |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 588 | } |
| 589 | } else { |
| 590 | // we didn't get back what we asked for. Reelect someone else. |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 591 | log.warn("Failed to assert role [{}] onto Device {}", response, deviceId); |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 592 | if (response == MastershipRole.MASTER) { |
| 593 | mastershipService.relinquishMastership(deviceId); |
| 594 | // TODO: Shouldn't we be triggering event? |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 595 | //final Device device = getDevice(deviceId); |
| 596 | //post(new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device)); |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 597 | } |
| 598 | } |
tom | 3f2bbd7 | 2014-09-24 12:07:58 -0700 | [diff] [blame] | 599 | } |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 600 | |
| 601 | @Override |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 602 | public void updatePortStatistics(DeviceId deviceId, Collection<PortStatistics> portStatistics) { |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 603 | checkNotNull(deviceId, DEVICE_ID_NULL); |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame] | 604 | checkNotNull(portStatistics, "Port statistics list cannot be null"); |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 605 | checkValidity(); |
| 606 | |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 607 | DeviceEvent event = store.updatePortStatistics(this.provider().id(), |
| 608 | deviceId, portStatistics); |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 609 | post(event); |
| 610 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 611 | } |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 612 | |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 613 | // by default allowed, otherwise check flag |
| 614 | private boolean isAllowed(BasicDeviceConfig cfg) { |
| 615 | return (cfg == null || cfg.isAllowed()); |
| 616 | } |
| 617 | |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 618 | // Applies the specified role to the device; ignores NONE |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 619 | |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 620 | /** |
| 621 | * Apply role to device and send probe if MASTER. |
| 622 | * |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 623 | * @param deviceId device identifier |
| 624 | * @param newRole new role to apply to the device |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 625 | * @return true if the request was sent to provider |
| 626 | */ |
| 627 | private boolean applyRoleAndProbe(DeviceId deviceId, MastershipRole newRole) { |
| 628 | if (newRole.equals(MastershipRole.NONE)) { |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 629 | //no-op |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 630 | return true; |
| 631 | } |
Ayaka Koshibe | 317245a | 2014-10-29 00:34:43 -0700 | [diff] [blame] | 632 | |
Ayaka Koshibe | 78bcbc1 | 2014-11-19 14:28:58 -0800 | [diff] [blame] | 633 | DeviceProvider provider = getProvider(deviceId); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 634 | if (provider == null) { |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 635 | log.warn("Provider for {} was not found. Cannot apply role {}", deviceId, newRole); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 636 | return false; |
| 637 | } |
| 638 | provider.roleChanged(deviceId, newRole); |
| 639 | |
| 640 | if (newRole.equals(MastershipRole.MASTER)) { |
HIGUCHI Yuta | 1979f55 | 2015-12-28 21:24:26 -0800 | [diff] [blame] | 641 | log.debug("sent TriggerProbe({})", deviceId); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 642 | // only trigger event when request was sent to provider |
Ayaka Koshibe | 78bcbc1 | 2014-11-19 14:28:58 -0800 | [diff] [blame] | 643 | provider.triggerProbe(deviceId); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 644 | } |
| 645 | return true; |
| 646 | } |
| 647 | |
| 648 | /** |
| 649 | * Reaasert role for specified device connected to this node. |
| 650 | * |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 651 | * @param did device identifier |
| 652 | * @param nextRole role to apply. If NONE is specified, |
| 653 | * it will ask mastership service for a role and apply it. |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 654 | */ |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 655 | private void reassertRole(final DeviceId did, |
| 656 | final MastershipRole nextRole) { |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 657 | |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 658 | MastershipRole myNextRole = nextRole; |
| 659 | if (myNextRole == NONE) { |
HIGUCHI Yuta | 1979f55 | 2015-12-28 21:24:26 -0800 | [diff] [blame] | 660 | try { |
| 661 | mastershipService.requestRoleFor(did).get(); |
| 662 | MastershipTerm term = termService.getMastershipTerm(did); |
| 663 | if (term != null && localNodeId.equals(term.master())) { |
| 664 | myNextRole = MASTER; |
| 665 | } else { |
| 666 | myNextRole = STANDBY; |
| 667 | } |
| 668 | } catch (InterruptedException e) { |
| 669 | Thread.currentThread().interrupt(); |
| 670 | log.error("Interrupted waiting for Mastership", e); |
| 671 | } catch (ExecutionException e) { |
| 672 | log.error("Encountered an error waiting for Mastership", e); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 673 | } |
| 674 | } |
| 675 | |
| 676 | switch (myNextRole) { |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 677 | case MASTER: |
| 678 | final Device device = getDevice(did); |
| 679 | if ((device != null) && !isAvailable(did)) { |
| 680 | //flag the device as online. Is there a better way to do this? |
| 681 | DefaultDeviceDescription deviceDescription |
| 682 | = new DefaultDeviceDescription(did.uri(), |
| 683 | device.type(), |
| 684 | device.manufacturer(), |
| 685 | device.hwVersion(), |
| 686 | device.swVersion(), |
| 687 | device.serialNumber(), |
| 688 | device.chassisId()); |
| 689 | DeviceEvent devEvent = |
| 690 | store.createOrUpdateDevice(device.providerId(), did, |
| 691 | deviceDescription); |
| 692 | post(devEvent); |
| 693 | } |
| 694 | // TODO: should apply role only if there is mismatch |
| 695 | log.debug("Applying role {} to {}", myNextRole, did); |
| 696 | if (!applyRoleAndProbe(did, MASTER)) { |
| 697 | log.warn("Unsuccessful applying role {} to {}", myNextRole, did); |
| 698 | // immediately failed to apply role |
| 699 | mastershipService.relinquishMastership(did); |
| 700 | // FIXME disconnect? |
| 701 | } |
| 702 | break; |
| 703 | case STANDBY: |
| 704 | log.debug("Applying role {} to {}", myNextRole, did); |
| 705 | if (!applyRoleAndProbe(did, STANDBY)) { |
| 706 | log.warn("Unsuccessful applying role {} to {}", myNextRole, did); |
| 707 | // immediately failed to apply role |
| 708 | mastershipService.relinquishMastership(did); |
| 709 | // FIXME disconnect? |
| 710 | } |
| 711 | break; |
| 712 | case NONE: |
| 713 | default: |
| 714 | // should never reach here |
| 715 | log.error("You didn't see anything. I did not exist."); |
| 716 | break; |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 717 | } |
| 718 | } |
| 719 | |
Madan Jampani | 328371d | 2015-05-29 14:06:27 -0700 | [diff] [blame] | 720 | private void handleMastershipEvent(MastershipEvent event) { |
| 721 | if (event.type() != MastershipEvent.Type.MASTER_CHANGED) { |
| 722 | // Don't care if backup list changed. |
| 723 | return; |
| 724 | } |
| 725 | |
| 726 | final DeviceId did = event.subject(); |
| 727 | |
| 728 | // myRole suggested by MastershipService |
| 729 | MastershipRole myNextRole; |
| 730 | if (localNodeId.equals(event.roleInfo().master())) { |
| 731 | // confirm latest info |
| 732 | MastershipTerm term = termService.getMastershipTerm(did); |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 733 | final boolean iHaveControl = term != null && localNodeId.equals(term.master()); |
Madan Jampani | 328371d | 2015-05-29 14:06:27 -0700 | [diff] [blame] | 734 | if (iHaveControl) { |
Madan Jampani | 328371d | 2015-05-29 14:06:27 -0700 | [diff] [blame] | 735 | myNextRole = MASTER; |
| 736 | } else { |
| 737 | myNextRole = STANDBY; |
| 738 | } |
| 739 | } else if (event.roleInfo().backups().contains(localNodeId)) { |
| 740 | myNextRole = STANDBY; |
| 741 | } else { |
| 742 | myNextRole = NONE; |
| 743 | } |
| 744 | |
Madan Jampani | 328371d | 2015-05-29 14:06:27 -0700 | [diff] [blame] | 745 | final boolean isReachable = isReachable(did); |
| 746 | if (!isReachable) { |
| 747 | // device is not connected to this node |
| 748 | if (myNextRole != NONE) { |
| 749 | log.warn("Node was instructed to be {} role for {}, " |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 750 | + "but this node cannot reach the device. " |
| 751 | + "Relinquishing role. ", |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 752 | myNextRole, did); |
Madan Jampani | 328371d | 2015-05-29 14:06:27 -0700 | [diff] [blame] | 753 | mastershipService.relinquishMastership(did); |
| 754 | } |
| 755 | return; |
| 756 | } |
| 757 | |
| 758 | // device is connected to this node: |
| 759 | if (store.getDevice(did) != null) { |
| 760 | reassertRole(did, myNextRole); |
| 761 | } else { |
| 762 | log.debug("Device is not yet/no longer in the store: {}", did); |
| 763 | } |
| 764 | } |
| 765 | |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 766 | // Intercepts mastership events |
| 767 | private class InternalMastershipListener implements MastershipListener { |
| 768 | |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 769 | @Override |
| 770 | public void event(MastershipEvent event) { |
HIGUCHI Yuta | 060da9a | 2016-03-11 19:16:35 -0800 | [diff] [blame] | 771 | backgroundService.execute(() -> { |
Madan Jampani | 328371d | 2015-05-29 14:06:27 -0700 | [diff] [blame] | 772 | try { |
| 773 | handleMastershipEvent(event); |
| 774 | } catch (Exception e) { |
| 775 | log.warn("Failed to handle {}", event, e); |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 776 | } |
Madan Jampani | 328371d | 2015-05-29 14:06:27 -0700 | [diff] [blame] | 777 | }); |
Ayaka Koshibe | 317245a | 2014-10-29 00:34:43 -0700 | [diff] [blame] | 778 | } |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 779 | } |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 780 | |
| 781 | // Store delegate to re-post events emitted from the store. |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame] | 782 | private class InternalStoreDelegate implements DeviceStoreDelegate { |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 783 | @Override |
| 784 | public void notify(DeviceEvent event) { |
| 785 | post(event); |
| 786 | } |
| 787 | } |
samuel | 738dfaf | 2015-07-11 11:08:57 +0800 | [diff] [blame] | 788 | |
| 789 | @Override |
| 790 | public Iterable<Device> getDevices(Type type) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 791 | checkPermission(DEVICE_READ); |
samuel | 738dfaf | 2015-07-11 11:08:57 +0800 | [diff] [blame] | 792 | Set<Device> results = new HashSet<>(); |
| 793 | Iterable<Device> devices = store.getDevices(); |
| 794 | if (devices != null) { |
| 795 | devices.forEach(d -> { |
| 796 | if (type.equals(d.type())) { |
| 797 | results.add(d); |
| 798 | } |
| 799 | }); |
| 800 | } |
| 801 | return results; |
| 802 | } |
| 803 | |
| 804 | @Override |
| 805 | public Iterable<Device> getAvailableDevices(Type type) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 806 | checkPermission(DEVICE_READ); |
samuel | 738dfaf | 2015-07-11 11:08:57 +0800 | [diff] [blame] | 807 | Set<Device> results = new HashSet<>(); |
| 808 | Iterable<Device> availableDevices = store.getAvailableDevices(); |
| 809 | if (availableDevices != null) { |
| 810 | availableDevices.forEach(d -> { |
| 811 | if (type.equals(d.type())) { |
| 812 | results.add(d); |
| 813 | } |
| 814 | }); |
| 815 | } |
| 816 | return results; |
| 817 | } |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 818 | |
| 819 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
| 820 | @Override |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 821 | public boolean isRelevant(NetworkConfigEvent event) { |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 822 | return (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED |
| 823 | || event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) |
| 824 | && (event.configClass().equals(BasicDeviceConfig.class) |
| 825 | || event.configClass().equals(OpticalPortConfig.class)); |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | @Override |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 829 | public void event(NetworkConfigEvent event) { |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 830 | DeviceEvent de = null; |
| 831 | if (event.configClass().equals(BasicDeviceConfig.class)) { |
Thomas Vachuska | 138de8b | 2016-01-11 21:31:38 -0800 | [diff] [blame] | 832 | log.debug("Detected device network config event {}", event.type()); |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 833 | DeviceId did = (DeviceId) event.subject(); |
| 834 | BasicDeviceConfig cfg = networkConfigService.getConfig(did, BasicDeviceConfig.class); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 835 | |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 836 | if (!isAllowed(cfg)) { |
| 837 | kickOutBadDevice(did); |
| 838 | } else { |
| 839 | Device dev = getDevice(did); |
| 840 | DeviceDescription desc = (dev == null) ? null : BasicDeviceOperator.descriptionOf(dev); |
| 841 | desc = BasicDeviceOperator.combine(cfg, desc); |
Thomas Vachuska | 1627dc8 | 2015-11-13 12:22:14 -0800 | [diff] [blame] | 842 | if (desc != null && getProvider(did) != null) { |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 843 | de = store.createOrUpdateDevice(getProvider(did).id(), did, desc); |
| 844 | } |
| 845 | } |
| 846 | } |
| 847 | if (event.configClass().equals(OpticalPortConfig.class)) { |
| 848 | ConnectPoint cpt = (ConnectPoint) event.subject(); |
| 849 | DeviceId did = cpt.deviceId(); |
| 850 | Port dpt = getPort(did, cpt.port()); |
| 851 | |
| 852 | if (dpt != null) { |
| 853 | OpticalPortConfig opc = networkConfigService.getConfig(cpt, OpticalPortConfig.class); |
| 854 | PortDescription desc = OpticalPortOperator.descriptionOf(dpt); |
| 855 | desc = OpticalPortOperator.combine(opc, desc); |
Thomas Vachuska | 1627dc8 | 2015-11-13 12:22:14 -0800 | [diff] [blame] | 856 | if (desc != null && getProvider(did) != null) { |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 857 | de = store.updatePortStatus(getProvider(did).id(), did, desc); |
| 858 | } |
| 859 | } |
| 860 | } |
| 861 | |
| 862 | if (de != null) { |
| 863 | post(de); |
| 864 | } |
| 865 | } |
| 866 | |
| 867 | // checks if the specified device is allowed by the BasicDeviceConfig |
| 868 | // and if not, removes it |
| 869 | private void kickOutBadDevice(DeviceId deviceId) { |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 870 | Device badDevice = getDevice(deviceId); |
| 871 | if (badDevice != null) { |
| 872 | removeDevice(deviceId); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 873 | } |
| 874 | } |
| 875 | } |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 876 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 877 | } |