Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
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 | |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
Carmelo Cascone | 1da7a4d | 2018-06-27 18:03:11 +0200 | [diff] [blame] | 19 | import com.google.common.collect.Lists; |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 20 | import com.google.common.collect.Maps; |
| 21 | import com.google.common.collect.Multimap; |
Jonghwan Hyun | 6ecf56d | 2017-08-01 16:07:44 -0700 | [diff] [blame] | 22 | import org.onlab.util.KryoNamespace; |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 23 | import org.onlab.util.Tools; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 24 | import org.onosproject.cluster.ClusterService; |
| 25 | import org.onosproject.cluster.NodeId; |
Thomas Vachuska | 52f2cd1 | 2018-11-08 21:20:04 -0800 | [diff] [blame] | 26 | import org.onosproject.net.config.basics.PortDescriptionsConfig; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 27 | import org.onosproject.mastership.MastershipEvent; |
| 28 | import org.onosproject.mastership.MastershipListener; |
| 29 | import org.onosproject.mastership.MastershipService; |
| 30 | import org.onosproject.mastership.MastershipTerm; |
| 31 | import org.onosproject.mastership.MastershipTermService; |
Carmelo Cascone | 0761cd3 | 2018-08-29 19:22:50 -0700 | [diff] [blame] | 32 | import org.onosproject.net.AnnotationKeys; |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 33 | import org.onosproject.net.ConnectPoint; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 34 | import org.onosproject.net.Device; |
samuel | 738dfaf | 2015-07-11 11:08:57 +0800 | [diff] [blame] | 35 | import org.onosproject.net.Device.Type; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 36 | import org.onosproject.net.DeviceId; |
| 37 | import org.onosproject.net.MastershipRole; |
| 38 | import org.onosproject.net.Port; |
| 39 | import org.onosproject.net.PortNumber; |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 40 | import org.onosproject.net.config.Config; |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 41 | import org.onosproject.net.config.NetworkConfigEvent; |
| 42 | import org.onosproject.net.config.NetworkConfigListener; |
| 43 | import org.onosproject.net.config.NetworkConfigService; |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 44 | import org.onosproject.net.config.PortConfigOperator; |
| 45 | import org.onosproject.net.config.PortConfigOperatorRegistry; |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 46 | import org.onosproject.net.config.basics.BasicDeviceConfig; |
Palash Kala | a06a616 | 2017-11-15 20:42:40 +0900 | [diff] [blame] | 47 | import org.onosproject.net.config.basics.DeviceAnnotationConfig; |
Yuta HIGUCHI | 7438f5a | 2017-02-15 22:09:46 -0800 | [diff] [blame] | 48 | import org.onosproject.net.config.basics.PortAnnotationConfig; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 49 | import org.onosproject.net.device.DefaultPortDescription; |
| 50 | import org.onosproject.net.device.DeviceAdminService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 51 | import org.onosproject.net.device.DeviceDescription; |
| 52 | import org.onosproject.net.device.DeviceEvent; |
| 53 | import org.onosproject.net.device.DeviceListener; |
| 54 | import org.onosproject.net.device.DeviceProvider; |
| 55 | import org.onosproject.net.device.DeviceProviderRegistry; |
| 56 | import org.onosproject.net.device.DeviceProviderService; |
| 57 | import org.onosproject.net.device.DeviceService; |
| 58 | import org.onosproject.net.device.DeviceStore; |
| 59 | import org.onosproject.net.device.DeviceStoreDelegate; |
| 60 | import org.onosproject.net.device.PortDescription; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 61 | import org.onosproject.net.device.PortStatistics; |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 62 | import org.onosproject.net.provider.AbstractListenerProviderRegistry; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 63 | import org.onosproject.net.provider.AbstractProviderService; |
Yuta HIGUCHI | 3a2a987 | 2016-11-29 20:24:23 -0800 | [diff] [blame] | 64 | import org.onosproject.net.provider.Provider; |
| 65 | import org.onosproject.net.provider.ProviderId; |
Jonghwan Hyun | 6ecf56d | 2017-08-01 16:07:44 -0700 | [diff] [blame] | 66 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
| 67 | import org.onosproject.store.cluster.messaging.MessageSubject; |
| 68 | import org.onosproject.store.serializers.KryoNamespaces; |
| 69 | import org.onosproject.store.service.Serializer; |
Jordan Halterman | 9416aea | 2017-11-17 12:40:21 -0800 | [diff] [blame] | 70 | import org.onosproject.upgrade.UpgradeService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 71 | import org.osgi.service.component.annotations.Activate; |
| 72 | import org.osgi.service.component.annotations.Component; |
| 73 | import org.osgi.service.component.annotations.Deactivate; |
| 74 | import org.osgi.service.component.annotations.Reference; |
| 75 | import org.osgi.service.component.annotations.ReferenceCardinality; |
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 | 0c47d53 | 2017-08-18 23:16:35 -0700 | [diff] [blame] | 78 | import java.time.Instant; |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 79 | import java.util.Collection; |
| 80 | import java.util.HashSet; |
| 81 | import java.util.List; |
| 82 | import java.util.Map; |
| 83 | import java.util.Objects; |
| 84 | import java.util.Optional; |
| 85 | import java.util.Set; |
| 86 | import java.util.concurrent.CompletableFuture; |
| 87 | import java.util.concurrent.ConcurrentHashMap; |
| 88 | import java.util.concurrent.CopyOnWriteArrayList; |
| 89 | import java.util.concurrent.ExecutionException; |
Jonghwan Hyun | 6ecf56d | 2017-08-01 16:07:44 -0700 | [diff] [blame] | 90 | import java.util.concurrent.ExecutorService; |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 91 | import java.util.concurrent.ScheduledExecutorService; |
| 92 | import java.util.concurrent.TimeUnit; |
| 93 | import java.util.stream.Collectors; |
Jonathan Hart | 2f66936 | 2015-02-11 16:19:20 -0800 | [diff] [blame] | 94 | |
Ray Milkey | 9ef2223 | 2016-07-14 12:42:37 -0700 | [diff] [blame] | 95 | import static com.google.common.base.Preconditions.checkNotNull; |
Jonghwan Hyun | 6ecf56d | 2017-08-01 16:07:44 -0700 | [diff] [blame] | 96 | import static com.google.common.base.Preconditions.checkState; |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 97 | import static com.google.common.collect.Multimaps.newListMultimap; |
| 98 | import static com.google.common.collect.Multimaps.synchronizedListMultimap; |
Jonghwan Hyun | 6ecf56d | 2017-08-01 16:07:44 -0700 | [diff] [blame] | 99 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
Ray Milkey | 9ef2223 | 2016-07-14 12:42:37 -0700 | [diff] [blame] | 100 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
| 101 | import static org.onlab.util.Tools.groupedThreads; |
| 102 | import static org.onosproject.net.MastershipRole.MASTER; |
| 103 | import static org.onosproject.net.MastershipRole.NONE; |
| 104 | import static org.onosproject.net.MastershipRole.STANDBY; |
Ray Milkey | 9ef2223 | 2016-07-14 12:42:37 -0700 | [diff] [blame] | 105 | import static org.onosproject.security.AppGuard.checkPermission; |
| 106 | import static org.onosproject.security.AppPermission.Type.DEVICE_READ; |
| 107 | import static org.slf4j.LoggerFactory.getLogger; |
| 108 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 109 | /** |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 110 | * Provides implementation of the device SB & NB APIs. |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 111 | */ |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 112 | @Component(immediate = true, |
| 113 | service = {DeviceService.class, DeviceAdminService.class, |
| 114 | DeviceProviderRegistry.class, PortConfigOperatorRegistry.class }) |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 115 | public class DeviceManager |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 116 | extends AbstractListenerProviderRegistry<DeviceEvent, DeviceListener, DeviceProvider, DeviceProviderService> |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 117 | implements DeviceService, DeviceAdminService, DeviceProviderRegistry, PortConfigOperatorRegistry { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 118 | |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 119 | private static final String DEVICE_ID_NULL = "Device ID cannot be null"; |
| 120 | private static final String PORT_NUMBER_NULL = "Port number cannot be null"; |
| 121 | private static final String DEVICE_DESCRIPTION_NULL = "Device description cannot be null"; |
| 122 | private static final String PORT_DESCRIPTION_NULL = "Port description cannot be null"; |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 123 | private static final String PORT_DESC_LIST_NULL = "Port description list cannot be null"; |
Jonghwan Hyun | 6ecf56d | 2017-08-01 16:07:44 -0700 | [diff] [blame] | 124 | private static final String EVENT_NON_MASTER = "Non-master node cannot handle this event"; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 125 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 126 | private final Logger log = getLogger(getClass()); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 127 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 128 | private final DeviceStoreDelegate delegate = new InternalStoreDelegate(); |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 129 | |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 130 | private final MastershipListener mastershipListener = new InternalMastershipListener(); |
Madan Jampani | de003d9 | 2015-05-11 17:14:20 -0700 | [diff] [blame] | 131 | private NodeId localNodeId; |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 132 | |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 133 | private ScheduledExecutorService backgroundService; |
| 134 | |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 135 | private final NetworkConfigListener networkConfigListener = new InternalNetworkConfigListener(); |
| 136 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 137 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 138 | protected DeviceStore store; |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 139 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 140 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 141 | protected ClusterService clusterService; |
| 142 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 143 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 144 | protected MastershipService mastershipService; |
| 145 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 146 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Ayaka Koshibe | 3de43ca | 2014-09-26 16:40:23 -0700 | [diff] [blame] | 147 | protected MastershipTermService termService; |
| 148 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 149 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jordan Halterman | 9416aea | 2017-11-17 12:40:21 -0800 | [diff] [blame] | 150 | protected UpgradeService upgradeService; |
| 151 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 152 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 153 | protected NetworkConfigService networkConfigService; |
| 154 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 155 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jonghwan Hyun | 6ecf56d | 2017-08-01 16:07:44 -0700 | [diff] [blame] | 156 | protected ClusterCommunicationService communicationService; |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 157 | |
Jonghwan Hyun | 6ecf56d | 2017-08-01 16:07:44 -0700 | [diff] [blame] | 158 | private ExecutorService portReqeustExecutor; |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 159 | /** |
| 160 | * List of all registered PortConfigOperator. |
| 161 | */ |
| 162 | private final List<PortConfigOperator> portOps = new CopyOnWriteArrayList<>(); |
| 163 | |
| 164 | /** |
| 165 | * Index to look up PortConfigOperator from Config each PortConfigOperator uses. |
| 166 | */ |
| 167 | private final Multimap<Class<? extends Config<ConnectPoint>>, PortConfigOperator> portOpsIndex |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 168 | = synchronizedListMultimap( |
| 169 | newListMultimap(new ConcurrentHashMap<>(), CopyOnWriteArrayList::new)); |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 170 | |
Yuta HIGUCHI | 7438f5a | 2017-02-15 22:09:46 -0800 | [diff] [blame] | 171 | // not part of portOps. must be executed at the end |
| 172 | private PortAnnotationOperator portAnnotationOp; |
Palash Kala | a06a616 | 2017-11-15 20:42:40 +0900 | [diff] [blame] | 173 | private DeviceAnnotationOperator deviceAnnotationOp; |
Yuta HIGUCHI | 7438f5a | 2017-02-15 22:09:46 -0800 | [diff] [blame] | 174 | |
Jonghwan Hyun | 6ecf56d | 2017-08-01 16:07:44 -0700 | [diff] [blame] | 175 | private static final MessageSubject PORT_UPDOWN_SUBJECT = |
| 176 | new MessageSubject("port-updown-req"); |
| 177 | |
| 178 | private static final Serializer SERIALIZER = Serializer.using( |
| 179 | KryoNamespace.newBuilder() |
| 180 | .register(KryoNamespaces.API) |
| 181 | .register(InternalPortUpDownEvent.class) |
| 182 | .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID) |
| 183 | .build("DeviceManager")); |
| 184 | |
Saurav Das | d5ec9e9 | 2017-01-17 10:40:18 -0800 | [diff] [blame] | 185 | /** |
| 186 | * Local storage for connectivity status of devices. |
| 187 | */ |
| 188 | private class LocalStatus { |
| 189 | boolean connected; |
Yuta HIGUCHI | 0c47d53 | 2017-08-18 23:16:35 -0700 | [diff] [blame] | 190 | Instant dateTime; |
Saurav Das | d5ec9e9 | 2017-01-17 10:40:18 -0800 | [diff] [blame] | 191 | |
Yuta HIGUCHI | 0c47d53 | 2017-08-18 23:16:35 -0700 | [diff] [blame] | 192 | public LocalStatus(boolean b, Instant now) { |
Saurav Das | d5ec9e9 | 2017-01-17 10:40:18 -0800 | [diff] [blame] | 193 | connected = b; |
| 194 | dateTime = now; |
| 195 | } |
| 196 | } |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 197 | |
Saurav Das | d5ec9e9 | 2017-01-17 10:40:18 -0800 | [diff] [blame] | 198 | private final Map<DeviceId, LocalStatus> deviceLocalStatus = |
| 199 | Maps.newConcurrentMap(); |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 200 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 201 | @Activate |
| 202 | public void activate() { |
Yuta HIGUCHI | 7438f5a | 2017-02-15 22:09:46 -0800 | [diff] [blame] | 203 | portAnnotationOp = new PortAnnotationOperator(networkConfigService); |
Palash Kala | a06a616 | 2017-11-15 20:42:40 +0900 | [diff] [blame] | 204 | deviceAnnotationOp = new DeviceAnnotationOperator(networkConfigService); |
Yuta HIGUCHI | 7438f5a | 2017-02-15 22:09:46 -0800 | [diff] [blame] | 205 | portOpsIndex.put(PortAnnotationConfig.class, portAnnotationOp); |
| 206 | |
HIGUCHI Yuta | 060da9a | 2016-03-11 19:16:35 -0800 | [diff] [blame] | 207 | backgroundService = newSingleThreadScheduledExecutor( |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 208 | groupedThreads("onos/device", "manager-background", log)); |
Madan Jampani | de003d9 | 2015-05-11 17:14:20 -0700 | [diff] [blame] | 209 | localNodeId = clusterService.getLocalNode().id(); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 210 | |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 211 | store.setDelegate(delegate); |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 212 | eventDispatcher.addSink(DeviceEvent.class, listenerRegistry); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 213 | mastershipService.addListener(mastershipListener); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 214 | networkConfigService.addListener(networkConfigListener); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 215 | |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 216 | backgroundService.scheduleWithFixedDelay(() -> { |
| 217 | try { |
| 218 | mastershipCheck(); |
| 219 | } catch (Exception e) { |
| 220 | log.error("Exception thrown during integrity check", e); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 221 | } |
| 222 | }, 1, 1, TimeUnit.MINUTES); |
Jonghwan Hyun | 6ecf56d | 2017-08-01 16:07:44 -0700 | [diff] [blame] | 223 | |
| 224 | portReqeustExecutor = newSingleThreadExecutor(); |
| 225 | |
| 226 | communicationService.<InternalPortUpDownEvent>addSubscriber( |
| 227 | PORT_UPDOWN_SUBJECT, |
| 228 | SERIALIZER::decode, |
| 229 | this::handlePortRequest, |
| 230 | portReqeustExecutor); |
| 231 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 232 | log.info("Started"); |
| 233 | } |
| 234 | |
| 235 | @Deactivate |
| 236 | public void deactivate() { |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 237 | backgroundService.shutdown(); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 238 | networkConfigService.removeListener(networkConfigListener); |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 239 | store.unsetDelegate(delegate); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 240 | mastershipService.removeListener(mastershipListener); |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 241 | eventDispatcher.removeSink(DeviceEvent.class); |
Jonghwan Hyun | 6ecf56d | 2017-08-01 16:07:44 -0700 | [diff] [blame] | 242 | communicationService.removeSubscriber(PORT_UPDOWN_SUBJECT); |
| 243 | portReqeustExecutor.shutdown(); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 244 | log.info("Stopped"); |
| 245 | } |
| 246 | |
| 247 | @Override |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 248 | public int getDeviceCount() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 249 | checkPermission(DEVICE_READ); |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 250 | return store.getDeviceCount(); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | @Override |
mskala | 32000d3 | 2017-07-14 16:27:06 +0200 | [diff] [blame] | 254 | public int getAvailableDeviceCount() { |
| 255 | checkPermission(DEVICE_READ); |
| 256 | return store.getAvailableDeviceCount(); |
| 257 | } |
| 258 | |
| 259 | @Override |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 260 | public Iterable<Device> getDevices() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 261 | checkPermission(DEVICE_READ); |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 262 | return store.getDevices(); |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 263 | } |
| 264 | |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 265 | @Override |
Yuta HIGUCHI | f1f2ac0 | 2014-11-26 14:02:22 -0800 | [diff] [blame] | 266 | public Iterable<Device> getAvailableDevices() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 267 | checkPermission(DEVICE_READ); |
Yuta HIGUCHI | f1f2ac0 | 2014-11-26 14:02:22 -0800 | [diff] [blame] | 268 | return store.getAvailableDevices(); |
| 269 | } |
| 270 | |
| 271 | @Override |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 272 | public Device getDevice(DeviceId deviceId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 273 | checkPermission(DEVICE_READ); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 274 | checkNotNull(deviceId, DEVICE_ID_NULL); |
tom | 132b58a | 2014-08-28 16:11:28 -0700 | [diff] [blame] | 275 | return store.getDevice(deviceId); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | @Override |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 279 | public MastershipRole getRole(DeviceId deviceId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 280 | checkPermission(DEVICE_READ); |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 281 | checkNotNull(deviceId, DEVICE_ID_NULL); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 282 | return mastershipService.getLocalRole(deviceId); |
tom | ad2d209 | 2014-09-06 23:24:20 -0700 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | @Override |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 286 | public List<Port> getPorts(DeviceId deviceId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 287 | checkPermission(DEVICE_READ); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 288 | checkNotNull(deviceId, DEVICE_ID_NULL); |
tom | 132b58a | 2014-08-28 16:11:28 -0700 | [diff] [blame] | 289 | return store.getPorts(deviceId); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | @Override |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 293 | public List<PortStatistics> getPortStatistics(DeviceId deviceId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 294 | checkPermission(DEVICE_READ); |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 295 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 296 | return store.getPortStatistics(deviceId); |
| 297 | } |
| 298 | |
| 299 | @Override |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 300 | public List<PortStatistics> getPortDeltaStatistics(DeviceId deviceId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 301 | checkPermission(DEVICE_READ); |
Dusan Pajin | 11ff4a8 | 2015-08-20 18:03:05 +0200 | [diff] [blame] | 302 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 303 | return store.getPortDeltaStatistics(deviceId); |
| 304 | } |
| 305 | |
| 306 | @Override |
Viswanath KSP | 22774cd | 2016-08-20 20:06:30 +0530 | [diff] [blame] | 307 | public PortStatistics getStatisticsForPort(DeviceId deviceId, PortNumber portNumber) { |
| 308 | checkPermission(DEVICE_READ); |
| 309 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 310 | checkNotNull(portNumber, PORT_NUMBER_NULL); |
| 311 | return store.getStatisticsForPort(deviceId, portNumber); |
| 312 | } |
| 313 | |
| 314 | @Override |
| 315 | public PortStatistics getDeltaStatisticsForPort(DeviceId deviceId, PortNumber portNumber) { |
| 316 | checkPermission(DEVICE_READ); |
| 317 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 318 | checkNotNull(portNumber, PORT_NUMBER_NULL); |
| 319 | return store.getDeltaStatisticsForPort(deviceId, portNumber); |
| 320 | } |
| 321 | |
| 322 | @Override |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 323 | public Port getPort(DeviceId deviceId, PortNumber portNumber) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 324 | checkPermission(DEVICE_READ); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 325 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 326 | checkNotNull(portNumber, PORT_NUMBER_NULL); |
tom | 132b58a | 2014-08-28 16:11:28 -0700 | [diff] [blame] | 327 | return store.getPort(deviceId, portNumber); |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | @Override |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 331 | public boolean isAvailable(DeviceId deviceId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 332 | checkPermission(DEVICE_READ); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 333 | |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 334 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 335 | return store.isAvailable(deviceId); |
| 336 | } |
| 337 | |
Saurav Das | d5ec9e9 | 2017-01-17 10:40:18 -0800 | [diff] [blame] | 338 | @Override |
| 339 | public String localStatus(DeviceId deviceId) { |
| 340 | LocalStatus ls = deviceLocalStatus.get(deviceId); |
| 341 | if (ls == null) { |
| 342 | return "No Record"; |
| 343 | } |
Yuta HIGUCHI | 0c47d53 | 2017-08-18 23:16:35 -0700 | [diff] [blame] | 344 | String timeAgo = Tools.timeAgo(ls.dateTime.toEpochMilli()); |
Saurav Das | d5ec9e9 | 2017-01-17 10:40:18 -0800 | [diff] [blame] | 345 | return (ls.connected) ? "connected " + timeAgo : "disconnected " + timeAgo; |
| 346 | } |
| 347 | |
Palash Kala | 0d817b0 | 2018-03-23 18:09:45 +0900 | [diff] [blame] | 348 | private boolean isLocallyConnected(DeviceId deviceId) { |
| 349 | LocalStatus ls = deviceLocalStatus.get(deviceId); |
| 350 | if (ls == null) { |
| 351 | return false; |
| 352 | } |
| 353 | return ls.connected; |
| 354 | } |
| 355 | |
Ray Milkey | 054e23d | 2018-03-22 13:37:11 -0700 | [diff] [blame] | 356 | @Override |
| 357 | public long getLastUpdatedInstant(DeviceId deviceId) { |
| 358 | LocalStatus ls = deviceLocalStatus.get(deviceId); |
| 359 | if (ls == null) { |
| 360 | return 0; |
| 361 | } |
| 362 | return ls.dateTime.toEpochMilli(); |
| 363 | } |
| 364 | |
Palash Kala | 0d817b0 | 2018-03-23 18:09:45 +0900 | [diff] [blame] | 365 | // Check a device for control channel connectivity |
| 366 | // and changes local-status appropriately. |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 367 | private boolean isReachable(DeviceId deviceId) { |
Ayaka Koshibe | 78bcbc1 | 2014-11-19 14:28:58 -0800 | [diff] [blame] | 368 | if (deviceId == null) { |
| 369 | return false; |
| 370 | } |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 371 | DeviceProvider provider = getProvider(deviceId); |
| 372 | if (provider != null) { |
Palash Kala | 0d817b0 | 2018-03-23 18:09:45 +0900 | [diff] [blame] | 373 | boolean reachable = provider.isReachable(deviceId); |
| 374 | if (reachable && !isLocallyConnected(deviceId)) { |
| 375 | deviceLocalStatus.put(deviceId, new LocalStatus(true, Instant.now())); |
| 376 | } else if (!reachable && isLocallyConnected(deviceId)) { |
| 377 | deviceLocalStatus.put(deviceId, new LocalStatus(false, Instant.now())); |
| 378 | } |
| 379 | return reachable; |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 380 | } else { |
Yuta HIGUCHI | 72669c4 | 2014-11-13 14:48:17 -0800 | [diff] [blame] | 381 | log.debug("Provider not found for {}", deviceId); |
Ayaka Koshibe | e60d452 | 2014-10-28 15:07:00 -0700 | [diff] [blame] | 382 | return false; |
| 383 | } |
Ayaka Koshibe | e8708e3 | 2014-10-22 13:40:18 -0700 | [diff] [blame] | 384 | } |
| 385 | |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 386 | @Override |
| 387 | public void removeDevice(DeviceId deviceId) { |
| 388 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 389 | DeviceEvent event = store.removeDevice(deviceId); |
tom | 0efbb1d | 2014-09-09 11:54:28 -0700 | [diff] [blame] | 390 | if (event != null) { |
| 391 | log.info("Device {} administratively removed", deviceId); |
| 392 | post(event); |
| 393 | } |
tom | e5ec3fd | 2014-09-04 15:18:06 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Jonghwan Hyun | 6ecf56d | 2017-08-01 16:07:44 -0700 | [diff] [blame] | 396 | private void handlePortRequest(InternalPortUpDownEvent event) { |
| 397 | DeviceId deviceId = event.deviceId(); |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 398 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 399 | checkNotNull(deviceId, PORT_NUMBER_NULL); |
Jonghwan Hyun | 6ecf56d | 2017-08-01 16:07:44 -0700 | [diff] [blame] | 400 | checkState(mastershipService.isLocalMaster(deviceId), EVENT_NON_MASTER); |
| 401 | changePortStateAtMaster(event.deviceId(), event.portNumber(), event.isEnable()); |
| 402 | } |
| 403 | |
| 404 | private void changePortStateAtMaster(DeviceId deviceId, PortNumber portNumber, |
| 405 | boolean enable) { |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 406 | DeviceProvider provider = getProvider(deviceId); |
| 407 | if (provider != null) { |
Jonghwan Hyun | 6ecf56d | 2017-08-01 16:07:44 -0700 | [diff] [blame] | 408 | log.info("Port {} on device {} being administratively brought {}", |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 409 | portNumber, deviceId, |
| 410 | (enable) ? "UP" : "DOWN"); |
| 411 | provider.changePortState(deviceId, portNumber, enable); |
| 412 | } else { |
| 413 | log.warn("Provider not found for {}", deviceId); |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | @Override |
Jonghwan Hyun | 6ecf56d | 2017-08-01 16:07:44 -0700 | [diff] [blame] | 418 | public void changePortState(DeviceId deviceId, PortNumber portNumber, |
| 419 | boolean enable) { |
| 420 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 421 | checkNotNull(deviceId, PORT_NUMBER_NULL); |
| 422 | NodeId masterId = mastershipService.getMasterFor(deviceId); |
| 423 | |
Ray Milkey | 4ef245e | 2018-05-10 15:41:16 -0700 | [diff] [blame] | 424 | if (masterId == null) { |
| 425 | // No master found; device is offline |
| 426 | log.info("No master found for port state change for {}", deviceId); |
| 427 | return; |
| 428 | } |
| 429 | |
Jonghwan Hyun | 6ecf56d | 2017-08-01 16:07:44 -0700 | [diff] [blame] | 430 | if (!masterId.equals(localNodeId)) { |
| 431 | //Send the request to the master node for the device |
| 432 | log.info("Device {} is managed by {}, forwarding the request to the MASTER", |
| 433 | deviceId, masterId); |
| 434 | communicationService.unicast( |
| 435 | new InternalPortUpDownEvent(deviceId, portNumber, enable), |
| 436 | PORT_UPDOWN_SUBJECT, |
| 437 | SERIALIZER::encode, |
| 438 | masterId).whenComplete((r, error) -> { |
| 439 | if (error != null) { |
| 440 | log.warn("Failed to send packet-updown-req to {}", masterId, error); |
| 441 | } |
| 442 | }); |
| 443 | } else { |
| 444 | changePortStateAtMaster(deviceId, portNumber, enable); |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | @Override |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 449 | protected DeviceProviderService createProviderService( |
| 450 | DeviceProvider provider) { |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 451 | return new InternalDeviceProviderService(provider); |
| 452 | } |
| 453 | |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 454 | /** |
| 455 | * Checks if all the reachable devices have a valid mastership role. |
| 456 | */ |
| 457 | private void mastershipCheck() { |
| 458 | log.debug("Checking mastership"); |
| 459 | for (Device device : getDevices()) { |
| 460 | final DeviceId deviceId = device.id(); |
Ray Milkey | c710467 | 2016-08-31 12:04:34 -0700 | [diff] [blame] | 461 | MastershipRole myRole = mastershipService.getLocalRole(deviceId); |
| 462 | log.trace("Checking device {}. Current role is {}", deviceId, myRole); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 463 | if (!isReachable(deviceId)) { |
Ray Milkey | c710467 | 2016-08-31 12:04:34 -0700 | [diff] [blame] | 464 | if (myRole != NONE) { |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 465 | // can't be master if device is not reachable |
| 466 | try { |
Ray Milkey | c710467 | 2016-08-31 12:04:34 -0700 | [diff] [blame] | 467 | if (myRole == MASTER) { |
Andrea Campanella | e319dc2 | 2018-01-18 12:51:04 +0100 | [diff] [blame] | 468 | log.info("Local Role {}, Marking unreachable device {} offline", MASTER, deviceId); |
Ray Milkey | c710467 | 2016-08-31 12:04:34 -0700 | [diff] [blame] | 469 | post(store.markOffline(deviceId)); |
| 470 | } |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 471 | //relinquish master role and ability to be backup. |
| 472 | mastershipService.relinquishMastership(deviceId).get(); |
| 473 | } catch (InterruptedException e) { |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 474 | log.warn("Interrupted while relinquishing role for {}", deviceId); |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 475 | Thread.currentThread().interrupt(); |
| 476 | } catch (ExecutionException e) { |
| 477 | log.error("Exception thrown while relinquishing role for {}", deviceId, e); |
| 478 | } |
Bharath Thiruveedula | 651a7da | 2016-12-13 02:52:50 +0530 | [diff] [blame] | 479 | } else { |
Andrea Campanella | e319dc2 | 2018-01-18 12:51:04 +0100 | [diff] [blame] | 480 | // check if the device has master and is available to the store, if not, mark it offline |
Bharath Thiruveedula | 651a7da | 2016-12-13 02:52:50 +0530 | [diff] [blame] | 481 | // only the nodes which has mastership role can mark any device offline. |
Andrea Campanella | e319dc2 | 2018-01-18 12:51:04 +0100 | [diff] [blame] | 482 | // This condition should never be hit unless in a device removed phase for NONE mastership roles. |
Jordan Halterman | bc0308f | 2017-11-29 15:37:34 -0800 | [diff] [blame] | 483 | NodeId master = mastershipService.getMasterFor(deviceId); |
Andrea Campanella | e319dc2 | 2018-01-18 12:51:04 +0100 | [diff] [blame] | 484 | if (master == null && isAvailable(deviceId)) { |
Jordan Halterman | bc0308f | 2017-11-29 15:37:34 -0800 | [diff] [blame] | 485 | CompletableFuture<MastershipRole> roleFuture = mastershipService.requestRoleFor(deviceId); |
| 486 | roleFuture.thenAccept(role -> { |
| 487 | MastershipTerm term = termService.getMastershipTerm(deviceId); |
| 488 | if (term != null && localNodeId.equals(term.master())) { |
| 489 | log.info("Marking unreachable device {} offline", deviceId); |
| 490 | post(store.markOffline(deviceId)); |
| 491 | } else { |
| 492 | log.info("Failed marking {} offline. {}", deviceId, role); |
| 493 | } |
| 494 | mastershipService.relinquishMastership(deviceId); |
| 495 | }); |
| 496 | } |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 497 | } |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 498 | continue; |
| 499 | } |
| 500 | |
Ray Milkey | c710467 | 2016-08-31 12:04:34 -0700 | [diff] [blame] | 501 | if (myRole != NONE) { |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 502 | continue; |
| 503 | } |
| 504 | |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 505 | 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] | 506 | |
| 507 | // isReachable but was not MASTER or STANDBY, get a role and apply |
| 508 | // Note: NONE triggers request to MastershipService |
Jordan Halterman | 9416aea | 2017-11-17 12:40:21 -0800 | [diff] [blame] | 509 | reassertRole(deviceId, NONE); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 510 | } |
| 511 | } |
| 512 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 513 | // Personalized device provider service issued to the supplied provider. |
tom | dc361b6 | 2014-09-09 20:36:52 -0700 | [diff] [blame] | 514 | private class InternalDeviceProviderService |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 515 | extends AbstractProviderService<DeviceProvider> |
| 516 | implements DeviceProviderService { |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 517 | |
tom | cfde062 | 2014-09-09 11:02:42 -0700 | [diff] [blame] | 518 | InternalDeviceProviderService(DeviceProvider provider) { |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 519 | super(provider); |
| 520 | } |
| 521 | |
Ray Milkey | 33306ba | 2018-09-20 13:27:25 -0700 | [diff] [blame] | 522 | /** |
| 523 | * Apply role in reaction to provider event. |
| 524 | * |
| 525 | * @param deviceId device identifier |
| 526 | * @param newRole new role to apply to the device |
| 527 | * @return true if the request was sent to provider |
| 528 | */ |
| 529 | private boolean applyRole(DeviceId deviceId, MastershipRole newRole) { |
| 530 | |
| 531 | if (newRole.equals(MastershipRole.NONE)) { |
| 532 | //no-op |
| 533 | return true; |
| 534 | } |
| 535 | |
| 536 | DeviceProvider provider = provider(); |
| 537 | if (provider == null) { |
| 538 | log.warn("Provider for {} was not found. Cannot apply role {}", |
| 539 | deviceId, newRole); |
| 540 | return false; |
| 541 | } |
| 542 | provider.roleChanged(deviceId, newRole); |
| 543 | // not triggering probe when triggered by provider service event |
| 544 | return true; |
| 545 | } |
| 546 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 547 | @Override |
alshabib | b7b4063 | 2014-09-28 21:30:00 -0700 | [diff] [blame] | 548 | public void deviceConnected(DeviceId deviceId, |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 549 | DeviceDescription deviceDescription) { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 550 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 551 | checkNotNull(deviceDescription, DEVICE_DESCRIPTION_NULL); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 552 | checkValidity(); |
Yuta HIGUCHI | 24b2e2a | 2014-10-07 15:53:57 -0700 | [diff] [blame] | 553 | |
Yuta HIGUCHI | 0c47d53 | 2017-08-18 23:16:35 -0700 | [diff] [blame] | 554 | deviceLocalStatus.put(deviceId, new LocalStatus(true, Instant.now())); |
Saurav Das | d5ec9e9 | 2017-01-17 10:40:18 -0800 | [diff] [blame] | 555 | |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 556 | BasicDeviceConfig cfg = networkConfigService.getConfig(deviceId, BasicDeviceConfig.class); |
| 557 | if (!isAllowed(cfg)) { |
| 558 | log.warn("Device {} is not allowed", deviceId); |
| 559 | return; |
| 560 | } |
Andrea Campanella | 75ef9f5 | 2017-07-27 20:14:32 +0200 | [diff] [blame] | 561 | PortDescriptionsConfig portConfig = networkConfigService.getConfig(deviceId, PortDescriptionsConfig.class); |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 562 | // Generate updated description and establish my Role |
| 563 | deviceDescription = BasicDeviceOperator.combine(cfg, deviceDescription); |
jaegonkim | 1c1f0c2 | 2018-01-28 22:20:42 +0900 | [diff] [blame] | 564 | DeviceAnnotationConfig annoConfig = networkConfigService.getConfig(deviceId, DeviceAnnotationConfig.class); |
| 565 | if (annoConfig != null) { |
| 566 | deviceDescription = deviceAnnotationOp.combine(deviceId, deviceDescription, Optional.of(annoConfig)); |
| 567 | } |
| 568 | |
jaegonkim | 8eabcec | 2018-07-27 23:40:43 +0900 | [diff] [blame] | 569 | MastershipRole role = mastershipService.requestRoleForSync(deviceId); |
| 570 | log.info("Local role is {} for {}", role, deviceId); |
Yi Tseng | 7be4e67 | 2017-12-05 15:03:26 -0800 | [diff] [blame] | 571 | DeviceEvent event = store.createOrUpdateDevice(provider().id(), deviceId, |
jaegonkim | 8eabcec | 2018-07-27 23:40:43 +0900 | [diff] [blame] | 572 | deviceDescription); |
Ray Milkey | 33306ba | 2018-09-20 13:27:25 -0700 | [diff] [blame] | 573 | applyRole(deviceId, role); |
HIGUCHI Yuta | 11530fb | 2015-05-27 13:10:20 -0700 | [diff] [blame] | 574 | |
Andrea Campanella | 75ef9f5 | 2017-07-27 20:14:32 +0200 | [diff] [blame] | 575 | if (portConfig != null) { |
| 576 | //updating the ports if configration exists |
| 577 | List<PortDescription> complete = store.getPortDescriptions(provider().id(), deviceId) |
| 578 | .collect(Collectors.toList()); |
| 579 | complete.addAll(portConfig.portDescriptions()); |
| 580 | List<PortDescription> portDescriptions = complete.stream() |
| 581 | .map(e -> applyAllPortOps(deviceId, e)) |
| 582 | .collect(Collectors.toList()); |
| 583 | store.updatePorts(provider().id(), deviceId, portDescriptions); |
| 584 | } |
| 585 | |
helenyrwu | fd296b6 | 2016-06-22 17:43:02 -0700 | [diff] [blame] | 586 | if (deviceDescription.isDefaultAvailable()) { |
| 587 | log.info("Device {} connected", deviceId); |
| 588 | } else { |
| 589 | log.info("Device {} registered", deviceId); |
| 590 | } |
Andrea Campanella | 75ef9f5 | 2017-07-27 20:14:32 +0200 | [diff] [blame] | 591 | |
tom | 80c0e5e | 2014-09-08 18:08:58 -0700 | [diff] [blame] | 592 | if (event != null) { |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 593 | log.trace("event: {} {}", event.type(), event); |
tom | 568581d | 2014-09-08 20:13:36 -0700 | [diff] [blame] | 594 | post(event); |
tom | 80c0e5e | 2014-09-08 18:08:58 -0700 | [diff] [blame] | 595 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 596 | } |
| 597 | |
Yuta HIGUCHI | 6eb00cc | 2016-06-10 11:55:12 -0700 | [diff] [blame] | 598 | private PortDescription ensurePortEnabledState(PortDescription desc, boolean enabled) { |
| 599 | if (desc.isEnabled() != enabled) { |
Yuta HIGUCHI | 2b1935d | 2018-03-01 21:41:06 -0800 | [diff] [blame] | 600 | return DefaultPortDescription.builder(desc) |
| 601 | .isEnabled(enabled) |
| 602 | .build(); |
Yuta HIGUCHI | 6eb00cc | 2016-06-10 11:55:12 -0700 | [diff] [blame] | 603 | } |
| 604 | return desc; |
| 605 | } |
| 606 | |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 607 | @Override |
| 608 | public void deviceDisconnected(DeviceId deviceId) { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 609 | checkNotNull(deviceId, DEVICE_ID_NULL); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 610 | checkValidity(); |
Yuta HIGUCHI | 0c47d53 | 2017-08-18 23:16:35 -0700 | [diff] [blame] | 611 | deviceLocalStatus.put(deviceId, new LocalStatus(false, Instant.now())); |
Yuta HIGUCHI | 2d3cd31 | 2014-10-31 11:38:04 -0700 | [diff] [blame] | 612 | log.info("Device {} disconnected from this node", deviceId); |
Ayaka Koshibe | d9f693e | 2014-09-29 18:04:54 -0700 | [diff] [blame] | 613 | |
Yuta HIGUCHI | 6eb00cc | 2016-06-10 11:55:12 -0700 | [diff] [blame] | 614 | List<PortDescription> descs = store.getPortDescriptions(provider().id(), deviceId) |
| 615 | .map(desc -> ensurePortEnabledState(desc, false)) |
| 616 | .collect(Collectors.toList()); |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 617 | |
alshabib | afc514a | 2014-12-01 14:44:05 -0800 | [diff] [blame] | 618 | store.updatePorts(this.provider().id(), deviceId, descs); |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 619 | try { |
Madan Jampani | 565a66a | 2015-07-25 17:01:13 -0700 | [diff] [blame] | 620 | if (mastershipService.isLocalMaster(deviceId)) { |
Thomas Vachuska | 5f429d6 | 2015-05-28 15:34:36 -0700 | [diff] [blame] | 621 | post(store.markOffline(deviceId)); |
| 622 | } |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 623 | } catch (IllegalStateException e) { |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 624 | log.warn("Failed to mark {} offline", deviceId); |
| 625 | // only the MASTER should be marking off-line in normal cases, |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 626 | // but if I was the last STANDBY connection, etc. and no one else |
| 627 | // was there to mark the device offline, this instance may need to |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 628 | // temporarily request for Master Role and mark offline. |
| 629 | |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 630 | //there are times when this node will correctly have mastership, BUT |
| 631 | //that isn't reflected in the ClockManager before the device disconnects. |
| 632 | //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] | 633 | |
Yuta HIGUCHI | eb24e9d | 2014-10-26 19:34:20 -0700 | [diff] [blame] | 634 | // FIXME: Store semantics leaking out as IllegalStateException. |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 635 | // Consider revising store API to handle this scenario. |
| 636 | CompletableFuture<MastershipRole> roleFuture = mastershipService.requestRoleFor(deviceId); |
Madan Jampani | de003d9 | 2015-05-11 17:14:20 -0700 | [diff] [blame] | 637 | roleFuture.whenComplete((role, error) -> { |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 638 | MastershipTerm term = termService.getMastershipTerm(deviceId); |
| 639 | // TODO: Move this type of check inside device clock manager, etc. |
| 640 | if (term != null && localNodeId.equals(term.master())) { |
| 641 | log.info("Retry marking {} offline", deviceId); |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 642 | post(store.markOffline(deviceId)); |
| 643 | } else { |
| 644 | log.info("Failed again marking {} offline. {}", deviceId, role); |
| 645 | } |
| 646 | }); |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 647 | } finally { |
Madan Jampani | c6e574f | 2015-05-29 13:41:52 -0700 | [diff] [blame] | 648 | try { |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 649 | //relinquish master role and ability to be backup. |
Madan Jampani | c6e574f | 2015-05-29 13:41:52 -0700 | [diff] [blame] | 650 | mastershipService.relinquishMastership(deviceId).get(); |
| 651 | } catch (InterruptedException e) { |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 652 | log.warn("Interrupted while reliquishing role for {}", deviceId); |
Madan Jampani | c6e574f | 2015-05-29 13:41:52 -0700 | [diff] [blame] | 653 | Thread.currentThread().interrupt(); |
| 654 | } catch (ExecutionException e) { |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 655 | log.error("Exception thrown while relinquishing role for {}", deviceId, e); |
Madan Jampani | c6e574f | 2015-05-29 13:41:52 -0700 | [diff] [blame] | 656 | } |
tom | 0efbb1d | 2014-09-09 11:54:28 -0700 | [diff] [blame] | 657 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | @Override |
alshabib | b7b4063 | 2014-09-28 21:30:00 -0700 | [diff] [blame] | 661 | public void updatePorts(DeviceId deviceId, |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 662 | List<PortDescription> portDescriptions) { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 663 | checkNotNull(deviceId, DEVICE_ID_NULL); |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 664 | checkNotNull(portDescriptions, PORT_DESC_LIST_NULL); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 665 | checkValidity(); |
Madan Jampani | 565a66a | 2015-07-25 17:01:13 -0700 | [diff] [blame] | 666 | if (!mastershipService.isLocalMaster(deviceId)) { |
Yuta HIGUCHI | 13c0b87 | 2014-10-30 18:09:22 -0700 | [diff] [blame] | 667 | // Never been a master for this device |
| 668 | // any update will be ignored. |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 669 | log.trace("Ignoring {} port updates on standby node. {}", deviceId, portDescriptions); |
Yuta HIGUCHI | 13c0b87 | 2014-10-30 18:09:22 -0700 | [diff] [blame] | 670 | return; |
| 671 | } |
Andrea Campanella | 75ef9f5 | 2017-07-27 20:14:32 +0200 | [diff] [blame] | 672 | PortDescriptionsConfig portConfig = networkConfigService.getConfig(deviceId, PortDescriptionsConfig.class); |
| 673 | if (portConfig != null) { |
Carmelo Cascone | 1da7a4d | 2018-06-27 18:03:11 +0200 | [diff] [blame] | 674 | // Updating the ports if configuration exists (on new lists as |
| 675 | // the passed one might be immutable) |
| 676 | portDescriptions = Lists.newArrayList(portDescriptions); |
Andrea Campanella | 75ef9f5 | 2017-07-27 20:14:32 +0200 | [diff] [blame] | 677 | portDescriptions.addAll(portConfig.portDescriptions()); |
| 678 | } |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 679 | portDescriptions = portDescriptions.stream() |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 680 | .map(e -> applyAllPortOps(deviceId, e)) |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 681 | .collect(Collectors.toList()); |
Yuta HIGUCHI | 5f6739c | 2014-10-01 14:04:01 -0700 | [diff] [blame] | 682 | List<DeviceEvent> events = store.updatePorts(this.provider().id(), |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 683 | deviceId, portDescriptions); |
Thomas Vachuska | 5923b9a | 2016-01-26 10:52:57 -0800 | [diff] [blame] | 684 | if (events != null) { |
| 685 | for (DeviceEvent event : events) { |
| 686 | post(event); |
| 687 | } |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 688 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | @Override |
alshabib | b7b4063 | 2014-09-28 21:30:00 -0700 | [diff] [blame] | 692 | public void portStatusChanged(DeviceId deviceId, |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 693 | PortDescription portDescription) { |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 694 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 695 | checkNotNull(portDescription, PORT_DESCRIPTION_NULL); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 696 | checkValidity(); |
Ayaka Koshibe | b5c63a0 | 2014-10-18 18:42:27 -0700 | [diff] [blame] | 697 | |
Madan Jampani | 565a66a | 2015-07-25 17:01:13 -0700 | [diff] [blame] | 698 | if (!mastershipService.isLocalMaster(deviceId)) { |
Yuta HIGUCHI | 13c0b87 | 2014-10-30 18:09:22 -0700 | [diff] [blame] | 699 | // Never been a master for this device |
| 700 | // any update will be ignored. |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 701 | log.trace("Ignoring {} port update on standby node. {}", deviceId, |
| 702 | portDescription); |
Yuta HIGUCHI | 13c0b87 | 2014-10-30 18:09:22 -0700 | [diff] [blame] | 703 | return; |
| 704 | } |
Ray Milkey | 9ef2223 | 2016-07-14 12:42:37 -0700 | [diff] [blame] | 705 | Device device = getDevice(deviceId); |
| 706 | if (device == null) { |
| 707 | log.trace("Device not found: {}", deviceId); |
nitinanand | ddfa8c9 | 2017-03-24 16:14:23 +0530 | [diff] [blame] | 708 | return; |
Ray Milkey | 9ef2223 | 2016-07-14 12:42:37 -0700 | [diff] [blame] | 709 | } |
Rimon Ashkenazy | 8ebfff0 | 2016-02-01 11:56:36 +0200 | [diff] [blame] | 710 | if ((Device.Type.ROADM.equals(device.type())) || |
Andrea Campanella | fa0f6cc | 2018-12-18 15:17:53 +0100 | [diff] [blame^] | 711 | (Device.Type.OTN.equals(device.type())) || (Device.Type.OLS.equals(device.type()))) { |
HIGUCHI Yuta | 6972ae6 | 2016-05-12 19:57:46 -0700 | [diff] [blame] | 712 | // FIXME This is ignoring all other info in portDescription given as input?? |
Yuta HIGUCHI | 6eb00cc | 2016-06-10 11:55:12 -0700 | [diff] [blame] | 713 | PortDescription storedPortDesc = store.getPortDescription(provider().id(), |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 714 | deviceId, |
| 715 | portDescription.portNumber()); |
Yuta HIGUCHI | 6eb00cc | 2016-06-10 11:55:12 -0700 | [diff] [blame] | 716 | portDescription = ensurePortEnabledState(storedPortDesc, |
| 717 | portDescription.isEnabled()); |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 718 | } |
| 719 | |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 720 | portDescription = applyAllPortOps(deviceId, portDescription); |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 721 | final DeviceEvent event = store.updatePortStatus(this.provider().id(), |
HIGUCHI Yuta | 6972ae6 | 2016-05-12 19:57:46 -0700 | [diff] [blame] | 722 | deviceId, |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 723 | portDescription); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 724 | if (event != null) { |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 725 | log.info("Device {} port {} status changed", deviceId, event.port().number()); |
tom | 0efbb1d | 2014-09-09 11:54:28 -0700 | [diff] [blame] | 726 | post(event); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 727 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 728 | } |
tom | 3f2bbd7 | 2014-09-24 12:07:58 -0700 | [diff] [blame] | 729 | |
| 730 | @Override |
Michal Mach | ce77433 | 2017-01-25 11:02:55 +0100 | [diff] [blame] | 731 | public void deletePort(DeviceId deviceId, PortDescription basePortDescription) { |
| 732 | |
| 733 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 734 | checkNotNull(basePortDescription, PORT_DESCRIPTION_NULL); |
| 735 | checkValidity(); |
| 736 | |
| 737 | if (!mastershipService.isLocalMaster(deviceId)) { |
| 738 | // Never been a master for this device |
| 739 | // any update will be ignored. |
| 740 | log.trace("Ignoring {} port update on standby node. {}", deviceId, |
| 741 | basePortDescription); |
| 742 | return; |
| 743 | } |
| 744 | |
| 745 | Device device = getDevice(deviceId); |
| 746 | if (device == null) { |
| 747 | log.trace("Device not found: {}", deviceId); |
| 748 | } |
| 749 | |
Yuta HIGUCHI | 53e4796 | 2018-03-01 23:50:48 -0800 | [diff] [blame] | 750 | PortDescription newPortDescription = DefaultPortDescription.builder(basePortDescription) |
| 751 | .isRemoved(true) |
| 752 | .build(); |
| 753 | |
Michal Mach | ce77433 | 2017-01-25 11:02:55 +0100 | [diff] [blame] | 754 | final DeviceEvent event = store.updatePortStatus(this.provider().id(), |
| 755 | deviceId, |
| 756 | newPortDescription); |
| 757 | if (event != null) { |
| 758 | log.info("Device {} port {} status changed", deviceId, event.port().number()); |
| 759 | post(event); |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | @Override |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 764 | public void receivedRoleReply(DeviceId deviceId, MastershipRole requested, |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame] | 765 | MastershipRole response) { |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 766 | // Several things can happen here: |
| 767 | // 1. request and response match |
| 768 | // 2. request and response don't match |
| 769 | // 3. MastershipRole and requested match (and 1 or 2 are true) |
| 770 | // 4. MastershipRole and requested don't match (and 1 or 2 are true) |
| 771 | // |
| 772 | // 2, 4, and 3 with case 2 are failure modes. |
| 773 | |
tom | 3f2bbd7 | 2014-09-24 12:07:58 -0700 | [diff] [blame] | 774 | // FIXME: implement response to this notification |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 775 | |
Madan Jampani | f2af771 | 2015-05-29 18:43:52 -0700 | [diff] [blame] | 776 | 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] | 777 | deviceId, requested, response); |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 778 | |
| 779 | if (requested == null && response == null) { |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 780 | // something was off with DeviceProvider, maybe check channel too? |
Shashikanth VH | 387a1ca | 2016-02-09 20:35:21 +0530 | [diff] [blame] | 781 | log.warn("Failed to assert role onto Device {}", deviceId); |
Yuta HIGUCHI | cf60390 | 2014-10-07 23:04:32 -0700 | [diff] [blame] | 782 | mastershipService.relinquishMastership(deviceId); |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 783 | return; |
Yuta HIGUCHI | cf60390 | 2014-10-07 23:04:32 -0700 | [diff] [blame] | 784 | } |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 785 | |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame] | 786 | if (Objects.equals(requested, response)) { |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 787 | if (Objects.equals(requested, mastershipService.getLocalRole(deviceId))) { |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 788 | return; |
| 789 | } else { |
HIGUCHI Yuta | 1979f55 | 2015-12-28 21:24:26 -0800 | [diff] [blame] | 790 | log.warn("Role mismatch on {}. set to {}, but store demands {}", |
| 791 | deviceId, response, mastershipService.getLocalRole(deviceId)); |
| 792 | // roleManager got the device to comply, but doesn't agree with |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 793 | // the store; use the store's view, then try to reassert. |
HIGUCHI Yuta | 060da9a | 2016-03-11 19:16:35 -0800 | [diff] [blame] | 794 | backgroundService.execute(() -> reassertRole(deviceId, mastershipService.getLocalRole(deviceId))); |
HIGUCHI Yuta | 1979f55 | 2015-12-28 21:24:26 -0800 | [diff] [blame] | 795 | return; |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 796 | } |
| 797 | } else { |
| 798 | // we didn't get back what we asked for. Reelect someone else. |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 799 | log.warn("Failed to assert role onto device {}. requested={}, response={}", |
| 800 | deviceId, requested, response); |
Carmelo Cascone | b37bdde | 2018-06-22 17:25:46 +0200 | [diff] [blame] | 801 | if (requested == MastershipRole.MASTER) { |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 802 | mastershipService.relinquishMastership(deviceId); |
| 803 | // TODO: Shouldn't we be triggering event? |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 804 | //final Device device = getDevice(deviceId); |
| 805 | //post(new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device)); |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 806 | } |
| 807 | } |
tom | 3f2bbd7 | 2014-09-24 12:07:58 -0700 | [diff] [blame] | 808 | } |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 809 | |
| 810 | @Override |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 811 | public void updatePortStatistics(DeviceId deviceId, Collection<PortStatistics> portStatistics) { |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 812 | checkNotNull(deviceId, DEVICE_ID_NULL); |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame] | 813 | checkNotNull(portStatistics, "Port statistics list cannot be null"); |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 814 | checkValidity(); |
| 815 | |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 816 | DeviceEvent event = store.updatePortStatistics(this.provider().id(), |
| 817 | deviceId, portStatistics); |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 818 | post(event); |
| 819 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 820 | } |
tom | 32f6684 | 2014-08-27 19:27:47 -0700 | [diff] [blame] | 821 | |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 822 | // by default allowed, otherwise check flag |
| 823 | private boolean isAllowed(BasicDeviceConfig cfg) { |
| 824 | return (cfg == null || cfg.isAllowed()); |
| 825 | } |
| 826 | |
Carmelo Cascone | 0761cd3 | 2018-08-29 19:22:50 -0700 | [diff] [blame] | 827 | private boolean canMarkOnline(Device device) { |
| 828 | final boolean providerMarkOnline = Boolean.parseBoolean( |
| 829 | device.annotations().value(AnnotationKeys.PROVIDER_MARK_ONLINE)); |
| 830 | return !providerMarkOnline; |
| 831 | } |
| 832 | |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 833 | // Applies the specified role to the device; ignores NONE |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 834 | |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 835 | /** |
| 836 | * Apply role to device and send probe if MASTER. |
| 837 | * |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 838 | * @param deviceId device identifier |
| 839 | * @param newRole new role to apply to the device |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 840 | * @return true if the request was sent to provider |
| 841 | */ |
| 842 | private boolean applyRoleAndProbe(DeviceId deviceId, MastershipRole newRole) { |
| 843 | if (newRole.equals(MastershipRole.NONE)) { |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 844 | //no-op |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 845 | return true; |
| 846 | } |
Ayaka Koshibe | 317245a | 2014-10-29 00:34:43 -0700 | [diff] [blame] | 847 | |
Ayaka Koshibe | 78bcbc1 | 2014-11-19 14:28:58 -0800 | [diff] [blame] | 848 | DeviceProvider provider = getProvider(deviceId); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 849 | if (provider == null) { |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 850 | log.warn("Provider for {} was not found. Cannot apply role {}", deviceId, newRole); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 851 | return false; |
| 852 | } |
| 853 | provider.roleChanged(deviceId, newRole); |
| 854 | |
| 855 | if (newRole.equals(MastershipRole.MASTER)) { |
HIGUCHI Yuta | 1979f55 | 2015-12-28 21:24:26 -0800 | [diff] [blame] | 856 | log.debug("sent TriggerProbe({})", deviceId); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 857 | // only trigger event when request was sent to provider |
Ayaka Koshibe | 78bcbc1 | 2014-11-19 14:28:58 -0800 | [diff] [blame] | 858 | provider.triggerProbe(deviceId); |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 859 | } |
| 860 | return true; |
| 861 | } |
| 862 | |
| 863 | /** |
Harold Huang | ff6e1e6 | 2017-11-09 16:25:36 +0800 | [diff] [blame] | 864 | * Reassert role for specified device connected to this node. |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 865 | * |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 866 | * @param did device identifier |
| 867 | * @param nextRole role to apply. If NONE is specified, |
| 868 | * it will ask mastership service for a role and apply it. |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 869 | */ |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 870 | private void reassertRole(final DeviceId did, |
| 871 | final MastershipRole nextRole) { |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 872 | |
Jordan Halterman | 9416aea | 2017-11-17 12:40:21 -0800 | [diff] [blame] | 873 | MastershipRole myNextRole = nextRole; |
| 874 | if (myNextRole == NONE && upgradeService.isLocalActive()) { |
| 875 | try { |
| 876 | mastershipService.requestRoleFor(did).get(); |
| 877 | MastershipTerm term = termService.getMastershipTerm(did); |
| 878 | if (term != null && localNodeId.equals(term.master())) { |
| 879 | myNextRole = MASTER; |
| 880 | } else { |
| 881 | myNextRole = STANDBY; |
| 882 | } |
| 883 | } catch (InterruptedException e) { |
| 884 | Thread.currentThread().interrupt(); |
| 885 | log.error("Interrupted waiting for Mastership", e); |
| 886 | } catch (ExecutionException e) { |
| 887 | log.error("Encountered an error waiting for Mastership", e); |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | switch (myNextRole) { |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 892 | case MASTER: |
| 893 | final Device device = getDevice(did); |
Carmelo Cascone | 0761cd3 | 2018-08-29 19:22:50 -0700 | [diff] [blame] | 894 | if (device != null && !isAvailable(did) && canMarkOnline(device)) { |
Palash Kala | 6c52606 | 2018-04-03 18:25:11 +0900 | [diff] [blame] | 895 | post(store.markOnline(did)); |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 896 | } |
| 897 | // TODO: should apply role only if there is mismatch |
Jordan Halterman | 9416aea | 2017-11-17 12:40:21 -0800 | [diff] [blame] | 898 | log.debug("Applying role {} to {}", myNextRole, did); |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 899 | if (!applyRoleAndProbe(did, MASTER)) { |
Jordan Halterman | 9416aea | 2017-11-17 12:40:21 -0800 | [diff] [blame] | 900 | log.warn("Unsuccessful applying role {} to {}", myNextRole, did); |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 901 | // immediately failed to apply role |
| 902 | mastershipService.relinquishMastership(did); |
| 903 | // FIXME disconnect? |
| 904 | } |
| 905 | break; |
| 906 | case STANDBY: |
Jordan Halterman | 9416aea | 2017-11-17 12:40:21 -0800 | [diff] [blame] | 907 | log.debug("Applying role {} to {}", myNextRole, did); |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 908 | if (!applyRoleAndProbe(did, STANDBY)) { |
Jordan Halterman | 9416aea | 2017-11-17 12:40:21 -0800 | [diff] [blame] | 909 | log.warn("Unsuccessful applying role {} to {}", myNextRole, did); |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 910 | // immediately failed to apply role |
| 911 | mastershipService.relinquishMastership(did); |
| 912 | // FIXME disconnect? |
| 913 | } |
| 914 | break; |
| 915 | case NONE: |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 916 | break; |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 917 | default: |
| 918 | // should never reach here |
| 919 | log.error("You didn't see anything. I did not exist."); |
| 920 | break; |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 921 | } |
| 922 | } |
| 923 | |
Madan Jampani | 328371d | 2015-05-29 14:06:27 -0700 | [diff] [blame] | 924 | private void handleMastershipEvent(MastershipEvent event) { |
Jon Hall | 7a8bfc6 | 2016-05-26 17:59:04 -0700 | [diff] [blame] | 925 | if (event.type() == MastershipEvent.Type.BACKUPS_CHANGED) { |
Madan Jampani | 328371d | 2015-05-29 14:06:27 -0700 | [diff] [blame] | 926 | // Don't care if backup list changed. |
| 927 | return; |
| 928 | } |
Madan Jampani | 328371d | 2015-05-29 14:06:27 -0700 | [diff] [blame] | 929 | final DeviceId did = event.subject(); |
| 930 | |
| 931 | // myRole suggested by MastershipService |
| 932 | MastershipRole myNextRole; |
Jon Hall | 7a8bfc6 | 2016-05-26 17:59:04 -0700 | [diff] [blame] | 933 | if (event.type() == MastershipEvent.Type.SUSPENDED) { |
| 934 | myNextRole = NONE; // FIXME STANDBY OR NONE? |
| 935 | } else if (localNodeId.equals(event.roleInfo().master())) { |
Madan Jampani | 328371d | 2015-05-29 14:06:27 -0700 | [diff] [blame] | 936 | // confirm latest info |
| 937 | MastershipTerm term = termService.getMastershipTerm(did); |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 938 | final boolean iHaveControl = term != null && localNodeId.equals(term.master()); |
Madan Jampani | 328371d | 2015-05-29 14:06:27 -0700 | [diff] [blame] | 939 | if (iHaveControl) { |
Madan Jampani | 328371d | 2015-05-29 14:06:27 -0700 | [diff] [blame] | 940 | myNextRole = MASTER; |
| 941 | } else { |
| 942 | myNextRole = STANDBY; |
| 943 | } |
| 944 | } else if (event.roleInfo().backups().contains(localNodeId)) { |
| 945 | myNextRole = STANDBY; |
| 946 | } else { |
| 947 | myNextRole = NONE; |
| 948 | } |
| 949 | |
Madan Jampani | 328371d | 2015-05-29 14:06:27 -0700 | [diff] [blame] | 950 | final boolean isReachable = isReachable(did); |
| 951 | if (!isReachable) { |
| 952 | // device is not connected to this node |
Jon Hall | a90c44c | 2017-01-24 16:02:07 -0800 | [diff] [blame] | 953 | if (mastershipService.getLocalRole(did) == NONE) { |
| 954 | log.debug("Node was instructed to be {} role for {}, " |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 955 | + "but this node cannot reach the device " |
| 956 | + "and role is already None. Ignoring request.", |
| 957 | myNextRole, did); |
Jon Hall | a90c44c | 2017-01-24 16:02:07 -0800 | [diff] [blame] | 958 | } else if (myNextRole != NONE) { |
Madan Jampani | 328371d | 2015-05-29 14:06:27 -0700 | [diff] [blame] | 959 | log.warn("Node was instructed to be {} role for {}, " |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 960 | + "but this node cannot reach the device. " |
| 961 | + "Relinquishing role. ", |
samuel | e1fa732 | 2015-07-14 16:35:16 +0800 | [diff] [blame] | 962 | myNextRole, did); |
Madan Jampani | 328371d | 2015-05-29 14:06:27 -0700 | [diff] [blame] | 963 | mastershipService.relinquishMastership(did); |
| 964 | } |
| 965 | return; |
| 966 | } |
| 967 | |
| 968 | // device is connected to this node: |
| 969 | if (store.getDevice(did) != null) { |
| 970 | reassertRole(did, myNextRole); |
| 971 | } else { |
| 972 | log.debug("Device is not yet/no longer in the store: {}", did); |
| 973 | } |
| 974 | } |
| 975 | |
Yuta HIGUCHI | 63323fd | 2014-11-11 12:16:58 -0800 | [diff] [blame] | 976 | // Intercepts mastership events |
| 977 | private class InternalMastershipListener implements MastershipListener { |
| 978 | |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 979 | @Override |
| 980 | public void event(MastershipEvent event) { |
HIGUCHI Yuta | 060da9a | 2016-03-11 19:16:35 -0800 | [diff] [blame] | 981 | backgroundService.execute(() -> { |
Madan Jampani | 328371d | 2015-05-29 14:06:27 -0700 | [diff] [blame] | 982 | try { |
| 983 | handleMastershipEvent(event); |
| 984 | } catch (Exception e) { |
| 985 | log.warn("Failed to handle {}", event, e); |
Yuta HIGUCHI | d26354d | 2014-10-31 14:14:38 -0700 | [diff] [blame] | 986 | } |
Madan Jampani | 328371d | 2015-05-29 14:06:27 -0700 | [diff] [blame] | 987 | }); |
Ayaka Koshibe | 317245a | 2014-10-29 00:34:43 -0700 | [diff] [blame] | 988 | } |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 989 | } |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 990 | |
| 991 | // Store delegate to re-post events emitted from the store. |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame] | 992 | private class InternalStoreDelegate implements DeviceStoreDelegate { |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 993 | @Override |
| 994 | public void notify(DeviceEvent event) { |
| 995 | post(event); |
Rafał Szalecki | 9fb87f6 | 2017-12-06 15:06:09 +0100 | [diff] [blame] | 996 | if (event.type().equals(DeviceEvent.Type.DEVICE_REMOVED)) { |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 997 | // When device is administratively removed, force disconnect. |
| 998 | DeviceId deviceId = event.subject().id(); |
| 999 | deviceLocalStatus.remove(deviceId); |
| 1000 | |
| 1001 | DeviceProvider provider = getProvider(deviceId); |
| 1002 | if (provider != null) { |
| 1003 | log.info("Triggering disconnect for device {}", deviceId); |
| 1004 | try { |
| 1005 | provider.triggerDisconnect(deviceId); |
| 1006 | } catch (UnsupportedOperationException e) { |
| 1007 | log.warn("Unable to trigger disconnect due to {}", e.getMessage()); |
| 1008 | } |
| 1009 | } |
Rafał Szalecki | 9fb87f6 | 2017-12-06 15:06:09 +0100 | [diff] [blame] | 1010 | } |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 1011 | } |
| 1012 | } |
samuel | 738dfaf | 2015-07-11 11:08:57 +0800 | [diff] [blame] | 1013 | |
| 1014 | @Override |
| 1015 | public Iterable<Device> getDevices(Type type) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 1016 | checkPermission(DEVICE_READ); |
samuel | 738dfaf | 2015-07-11 11:08:57 +0800 | [diff] [blame] | 1017 | Set<Device> results = new HashSet<>(); |
| 1018 | Iterable<Device> devices = store.getDevices(); |
| 1019 | if (devices != null) { |
| 1020 | devices.forEach(d -> { |
| 1021 | if (type.equals(d.type())) { |
| 1022 | results.add(d); |
| 1023 | } |
| 1024 | }); |
| 1025 | } |
| 1026 | return results; |
| 1027 | } |
| 1028 | |
| 1029 | @Override |
| 1030 | public Iterable<Device> getAvailableDevices(Type type) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 1031 | checkPermission(DEVICE_READ); |
samuel | 738dfaf | 2015-07-11 11:08:57 +0800 | [diff] [blame] | 1032 | Set<Device> results = new HashSet<>(); |
| 1033 | Iterable<Device> availableDevices = store.getAvailableDevices(); |
| 1034 | if (availableDevices != null) { |
| 1035 | availableDevices.forEach(d -> { |
| 1036 | if (type.equals(d.type())) { |
| 1037 | results.add(d); |
| 1038 | } |
| 1039 | }); |
| 1040 | } |
| 1041 | return results; |
| 1042 | } |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 1043 | |
| 1044 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
| 1045 | @Override |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 1046 | public boolean isRelevant(NetworkConfigEvent event) { |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 1047 | return (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED |
| 1048 | || event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) |
| 1049 | && (event.configClass().equals(BasicDeviceConfig.class) |
Andrea Campanella | 75ef9f5 | 2017-07-27 20:14:32 +0200 | [diff] [blame] | 1050 | || portOpsIndex.containsKey(event.configClass()) |
Palash Kala | a06a616 | 2017-11-15 20:42:40 +0900 | [diff] [blame] | 1051 | || event.configClass().equals(PortDescriptionsConfig.class) |
Thomas Vachuska | f131e59 | 2018-05-07 11:52:14 -0700 | [diff] [blame] | 1052 | || event.configClass().equals(DeviceAnnotationConfig.class)) |
| 1053 | && mastershipService.isLocalMaster((DeviceId) event.subject()); |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 1054 | } |
| 1055 | |
| 1056 | @Override |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 1057 | public void event(NetworkConfigEvent event) { |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 1058 | DeviceEvent de = null; |
| 1059 | if (event.configClass().equals(BasicDeviceConfig.class)) { |
Thomas Vachuska | 138de8b | 2016-01-11 21:31:38 -0800 | [diff] [blame] | 1060 | log.debug("Detected device network config event {}", event.type()); |
Yuta HIGUCHI | 1d4b3aa | 2017-08-09 15:10:18 -0700 | [diff] [blame] | 1061 | DeviceId did = (DeviceId) event.subject(); |
| 1062 | DeviceProvider dp = getProvider(did); |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 1063 | BasicDeviceConfig cfg = |
| 1064 | networkConfigService.getConfig(did, BasicDeviceConfig.class); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 1065 | |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 1066 | if (!isAllowed(cfg)) { |
| 1067 | kickOutBadDevice(did); |
| 1068 | } else { |
| 1069 | Device dev = getDevice(did); |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 1070 | DeviceDescription desc = |
| 1071 | (dev == null) ? null : BasicDeviceOperator.descriptionOf(dev); |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 1072 | desc = BasicDeviceOperator.combine(cfg, desc); |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 1073 | if (desc != null && dp != null) { |
| 1074 | de = store.createOrUpdateDevice(dp.id(), did, desc); |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 1075 | } |
| 1076 | } |
Yuta HIGUCHI | 1d4b3aa | 2017-08-09 15:10:18 -0700 | [diff] [blame] | 1077 | } else if (event.configClass().equals(PortDescriptionsConfig.class)) { |
| 1078 | DeviceId did = (DeviceId) event.subject(); |
| 1079 | DeviceProvider dp = getProvider(did); |
| 1080 | if (!event.config().isPresent() || |
| 1081 | getDevice(did) == null || dp == null) { |
| 1082 | // sanity check failed, ignore |
| 1083 | return; |
| 1084 | } |
Andrea Campanella | 75ef9f5 | 2017-07-27 20:14:32 +0200 | [diff] [blame] | 1085 | PortDescriptionsConfig portConfig = (PortDescriptionsConfig) event.config().get(); |
Yuta HIGUCHI | 1d4b3aa | 2017-08-09 15:10:18 -0700 | [diff] [blame] | 1086 | //updating the ports if configuration exists |
Andrea Campanella | 75ef9f5 | 2017-07-27 20:14:32 +0200 | [diff] [blame] | 1087 | List<PortDescription> complete = store.getPortDescriptions(dp.id(), did) |
| 1088 | .collect(Collectors.toList()); |
| 1089 | complete.addAll(portConfig.portDescriptions()); |
| 1090 | store.updatePorts(dp.id(), did, complete); |
Palash Kala | a06a616 | 2017-11-15 20:42:40 +0900 | [diff] [blame] | 1091 | } else if (event.configClass().equals(DeviceAnnotationConfig.class)) { |
| 1092 | DeviceId did = (DeviceId) event.subject(); |
| 1093 | DeviceProvider dp = getProvider(did); |
| 1094 | Device dev = getDevice(did); |
| 1095 | DeviceDescription desc = |
| 1096 | (dev == null) ? null : BasicDeviceOperator.descriptionOf(dev); |
| 1097 | Optional<Config> prevConfig = event.prevConfig(); |
| 1098 | desc = deviceAnnotationOp.combine(did, desc, prevConfig); |
| 1099 | if (desc != null && dp != null) { |
| 1100 | de = store.createOrUpdateDevice(dp.id(), did, desc); |
| 1101 | } |
Yuta HIGUCHI | 1d4b3aa | 2017-08-09 15:10:18 -0700 | [diff] [blame] | 1102 | } else if (portOpsIndex.containsKey(event.configClass())) { |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 1103 | ConnectPoint cpt = (ConnectPoint) event.subject(); |
Yuta HIGUCHI | 1d4b3aa | 2017-08-09 15:10:18 -0700 | [diff] [blame] | 1104 | DeviceId did = cpt.deviceId(); |
| 1105 | DeviceProvider dp = getProvider(did); |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 1106 | |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 1107 | // Note: assuming PortOperator can modify existing port, |
| 1108 | // but cannot add new port purely from Config. |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 1109 | de = Optional.ofNullable(dp) |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 1110 | .map(provider -> store.getPortDescription(provider.id(), did, cpt.port())) |
| 1111 | .map(desc -> applyAllPortOps(cpt, desc)) |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 1112 | .map(desc -> store.updatePortStatus(dp.id(), did, desc)) |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 1113 | .orElse(null); |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 1114 | } |
| 1115 | |
| 1116 | if (de != null) { |
| 1117 | post(de); |
| 1118 | } |
| 1119 | } |
| 1120 | |
Simon Hunt | 8f60ff8 | 2017-04-24 17:19:30 -0700 | [diff] [blame] | 1121 | // removes the specified device if it exists |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 1122 | private void kickOutBadDevice(DeviceId deviceId) { |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 1123 | Device badDevice = getDevice(deviceId); |
| 1124 | if (badDevice != null) { |
| 1125 | removeDevice(deviceId); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 1126 | } |
| 1127 | } |
| 1128 | } |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 1129 | |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 1130 | @Override |
| 1131 | @SafeVarargs |
| 1132 | public final void registerPortConfigOperator(PortConfigOperator portOp, |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 1133 | Class<? extends Config<ConnectPoint>>... configs) { |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 1134 | checkNotNull(portOp); |
| 1135 | |
| 1136 | portOp.bindService(networkConfigService); |
| 1137 | |
| 1138 | // update both portOpsIndex and portOps |
| 1139 | synchronized (portOpsIndex) { |
| 1140 | for (Class<? extends Config<ConnectPoint>> config : configs) { |
| 1141 | portOpsIndex.put(config, portOp); |
| 1142 | } |
| 1143 | |
| 1144 | portOps.add(portOp); |
| 1145 | } |
| 1146 | |
| 1147 | // TODO: Should we be applying to all existing Ports? |
| 1148 | Tools.stream(store.getAvailableDevices()) |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 1149 | .map(Device::id) |
| 1150 | .filter(mastershipService::isLocalMaster) |
| 1151 | // for each locally managed Device, update all port descriptions |
| 1152 | .map(did -> { |
| 1153 | ProviderId pid = Optional.ofNullable(getProvider(did)) |
| 1154 | .map(Provider::id) |
| 1155 | .orElse(null); |
| 1156 | if (pid == null) { |
| 1157 | log.warn("Provider not found for {}", did); |
| 1158 | return ImmutableList.<DeviceEvent>of(); |
| 1159 | } |
| 1160 | List<PortDescription> pds |
| 1161 | = store.getPortDescriptions(pid, did) |
| 1162 | .map(pdesc -> applyAllPortOps(did, pdesc)) |
| 1163 | .collect(Collectors.toList()); |
| 1164 | return store.updatePorts(pid, did, pds); |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 1165 | }) |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 1166 | // ..and port port update event if necessary |
| 1167 | .forEach(evts -> evts.forEach(this::post)); |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 1168 | } |
| 1169 | |
| 1170 | @Override |
| 1171 | public void unregisterPortConfigOperator(PortConfigOperator portOp) { |
| 1172 | checkNotNull(portOp); |
| 1173 | |
| 1174 | |
| 1175 | // remove all portOp |
| 1176 | synchronized (portOpsIndex) { |
| 1177 | portOps.remove(portOp); |
| 1178 | |
| 1179 | // had to do this since COWArrayList iterator doesn't support remove |
| 1180 | portOpsIndex.keySet().forEach(key -> portOpsIndex.remove(key, portOp)); |
| 1181 | } |
| 1182 | |
| 1183 | } |
| 1184 | |
| 1185 | /** |
| 1186 | * Merges the appropriate PortConfig with the description. |
| 1187 | * |
| 1188 | * @param did ID of the Device where the port is attached |
| 1189 | * @param desc {@link PortDescription} |
| 1190 | * @return merged {@link PortDescription} |
| 1191 | */ |
| 1192 | private PortDescription applyAllPortOps(DeviceId did, PortDescription desc) { |
| 1193 | return applyAllPortOps(new ConnectPoint(did, desc.portNumber()), desc); |
| 1194 | } |
| 1195 | |
| 1196 | /** |
| 1197 | * Merges the appropriate PortConfig with the description. |
| 1198 | * |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 1199 | * @param cpt ConnectPoint where the port is attached |
| 1200 | * @param desc {@link PortDescription} |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 1201 | * @return merged {@link PortDescription} |
| 1202 | */ |
| 1203 | private PortDescription applyAllPortOps(ConnectPoint cpt, PortDescription desc) { |
| 1204 | PortDescription work = desc; |
| 1205 | for (PortConfigOperator portOp : portOps) { |
| 1206 | work = portOp.combine(cpt, work); |
| 1207 | } |
Yuta HIGUCHI | 7438f5a | 2017-02-15 22:09:46 -0800 | [diff] [blame] | 1208 | return portAnnotationOp.combine(cpt, work); |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 1209 | } |
| 1210 | |
Jonghwan Hyun | 6ecf56d | 2017-08-01 16:07:44 -0700 | [diff] [blame] | 1211 | /** |
| 1212 | * Port Enable/Disable message sent to the device's MASTER node. |
| 1213 | */ |
| 1214 | private class InternalPortUpDownEvent { |
| 1215 | private final DeviceId deviceId; |
| 1216 | private final PortNumber portNumber; |
| 1217 | private final boolean enable; |
| 1218 | |
| 1219 | protected InternalPortUpDownEvent( |
| 1220 | DeviceId deviceId, PortNumber portNumber, boolean enable) { |
| 1221 | this.deviceId = deviceId; |
| 1222 | this.portNumber = portNumber; |
| 1223 | this.enable = enable; |
| 1224 | } |
| 1225 | |
| 1226 | public DeviceId deviceId() { |
| 1227 | return deviceId; |
| 1228 | } |
| 1229 | public PortNumber portNumber() { |
| 1230 | return portNumber; |
| 1231 | } |
| 1232 | public boolean isEnable() { |
| 1233 | return enable; |
| 1234 | } |
| 1235 | |
| 1236 | protected InternalPortUpDownEvent() { |
| 1237 | this.deviceId = null; |
| 1238 | this.portNumber = null; |
| 1239 | this.enable = false; |
| 1240 | } |
| 1241 | } |
tom | d3097b0 | 2014-08-26 10:40:29 -0700 | [diff] [blame] | 1242 | } |