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