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