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