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