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