Merge "Bump openflowplugin to use yangtools 1.2.0"
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / lifecycle / ContextChainHolderImpl.java
1 /*
2  * Copyright (c) 2016 Pantheon Technologies s.r.o. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.openflowplugin.impl.lifecycle;
9
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.util.concurrent.FutureCallback;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.MoreExecutors;
14 import java.util.HashMap;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Objects;
18 import java.util.Optional;
19 import java.util.concurrent.ConcurrentHashMap;
20 import java.util.concurrent.ExecutionException;
21 import java.util.concurrent.ExecutorService;
22 import java.util.concurrent.TimeUnit;
23 import java.util.concurrent.TimeoutException;
24 import java.util.stream.Collectors;
25 import javax.annotation.Nonnull;
26 import javax.annotation.Nullable;
27 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipChange;
28 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListenerRegistration;
29 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
30 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
31 import org.opendaylight.openflowplugin.api.openflow.OFPManager;
32 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
33 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionStatus;
34 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
35 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
36 import org.opendaylight.openflowplugin.api.openflow.device.DeviceManager;
37 import org.opendaylight.openflowplugin.api.openflow.lifecycle.ContextChain;
38 import org.opendaylight.openflowplugin.api.openflow.lifecycle.ContextChainHolder;
39 import org.opendaylight.openflowplugin.api.openflow.lifecycle.ContextChainMastershipState;
40 import org.opendaylight.openflowplugin.api.openflow.lifecycle.MasterChecker;
41 import org.opendaylight.openflowplugin.api.openflow.lifecycle.OwnershipChangeListener;
42 import org.opendaylight.openflowplugin.api.openflow.role.RoleContext;
43 import org.opendaylight.openflowplugin.api.openflow.role.RoleManager;
44 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
45 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcManager;
46 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext;
47 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsManager;
48 import org.opendaylight.openflowplugin.impl.util.DeviceStateUtil;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.rf.state.rev170713.ResultState;
54 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
55 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
58
59 public class ContextChainHolderImpl implements ContextChainHolder, MasterChecker {
60     private static final Logger LOG = LoggerFactory.getLogger(ContextChainHolderImpl.class);
61
62     private static final String CONTEXT_CREATED_FOR_CONNECTION = " context created for connection: {}";
63     private static final long REMOVE_DEVICE_FROM_DS_TIMEOUT = 5000L;
64     private static final String ASYNC_SERVICE_ENTITY_TYPE = "org.opendaylight.mdsal.AsyncServiceCloseEntityType";
65
66     private final Map<DeviceInfo, ContextChain> contextChainMap = new ConcurrentHashMap<>();
67     private final Map<DeviceInfo, ? super ConnectionContext> connectingDevices = new ConcurrentHashMap<>();
68     private final EntityOwnershipListenerRegistration eosListenerRegistration;
69     private final ClusterSingletonServiceProvider singletonServiceProvider;
70     private final ExecutorService executorService;
71     private final OwnershipChangeListener ownershipChangeListener;
72     private DeviceManager deviceManager;
73     private RpcManager rpcManager;
74     private StatisticsManager statisticsManager;
75     private RoleManager roleManager;
76
77     public ContextChainHolderImpl(final ExecutorService executorService,
78                                   final ClusterSingletonServiceProvider singletonServiceProvider,
79                                   final EntityOwnershipService entityOwnershipService,
80                                   final OwnershipChangeListener ownershipChangeListener) {
81         this.singletonServiceProvider = singletonServiceProvider;
82         this.executorService = executorService;
83         this.ownershipChangeListener = ownershipChangeListener;
84         this.ownershipChangeListener.setMasterChecker(this);
85         this.eosListenerRegistration = Objects.requireNonNull(entityOwnershipService
86                 .registerListener(ASYNC_SERVICE_ENTITY_TYPE, this));
87     }
88
89     @Override
90     public <T extends OFPManager> void addManager(final T manager) {
91         if (Objects.isNull(deviceManager) && manager instanceof DeviceManager) {
92             LOG.trace("Context chain holder: Device manager OK.");
93             deviceManager = (DeviceManager) manager;
94         } else if (Objects.isNull(rpcManager) && manager instanceof RpcManager) {
95             LOG.trace("Context chain holder: RPC manager OK.");
96             rpcManager = (RpcManager) manager;
97         } else if (Objects.isNull(statisticsManager) && manager instanceof StatisticsManager) {
98             LOG.trace("Context chain holder: Statistics manager OK.");
99             statisticsManager = (StatisticsManager) manager;
100         } else if (Objects.isNull(roleManager) && manager instanceof RoleManager) {
101             LOG.trace("Context chain holder: Role manager OK.");
102             roleManager = (RoleManager) manager;
103         }
104     }
105
106     @VisibleForTesting
107     void createContextChain(final ConnectionContext connectionContext) {
108         final DeviceInfo deviceInfo = connectionContext.getDeviceInfo();
109
110         final DeviceContext deviceContext = deviceManager.createContext(connectionContext);
111         deviceContext.registerMastershipWatcher(this);
112         LOG.debug("Device" + CONTEXT_CREATED_FOR_CONNECTION, deviceInfo);
113
114         final RpcContext rpcContext = rpcManager.createContext(deviceContext);
115         rpcContext.registerMastershipWatcher(this);
116         LOG.debug("RPC" + CONTEXT_CREATED_FOR_CONNECTION, deviceInfo);
117
118         final StatisticsContext statisticsContext = statisticsManager.createContext(deviceContext,
119                 ownershipChangeListener.isReconciliationFrameworkRegistered());
120         statisticsContext.registerMastershipWatcher(this);
121         LOG.debug("Statistics" + CONTEXT_CREATED_FOR_CONNECTION, deviceInfo);
122
123         final RoleContext roleContext = roleManager.createContext(deviceContext);
124         roleContext.registerMastershipWatcher(this);
125         LOG.debug("Role" + CONTEXT_CREATED_FOR_CONNECTION, deviceInfo);
126
127         final ContextChain contextChain = new ContextChainImpl(this, connectionContext,
128                 executorService);
129         contextChain.registerDeviceRemovedHandler(deviceManager);
130         contextChain.registerDeviceRemovedHandler(rpcManager);
131         contextChain.registerDeviceRemovedHandler(statisticsManager);
132         contextChain.registerDeviceRemovedHandler(roleManager);
133         contextChain.registerDeviceRemovedHandler(this);
134         contextChain.addContext(deviceContext);
135         contextChain.addContext(rpcContext);
136         contextChain.addContext(statisticsContext);
137         contextChain.addContext(roleContext);
138         contextChainMap.put(deviceInfo, contextChain);
139         connectingDevices.remove(deviceInfo);
140         LOG.debug("Context chain" + CONTEXT_CREATED_FOR_CONNECTION, deviceInfo);
141
142         deviceContext.onPublished();
143         contextChain.registerServices(singletonServiceProvider);
144     }
145
146     @Override
147     public ConnectionStatus deviceConnected(final ConnectionContext connectionContext) throws Exception {
148         final DeviceInfo deviceInfo = connectionContext.getDeviceInfo();
149         final ContextChain contextChain = contextChainMap.get(deviceInfo);
150         final FeaturesReply featuresReply = connectionContext.getFeatures();
151         final Short auxiliaryId = featuresReply != null ? featuresReply.getAuxiliaryId() : null;
152
153         if (auxiliaryId != null && auxiliaryId != 0) {
154             if (contextChain == null) {
155                 LOG.warn("An auxiliary connection for device {}, but no primary connection. Refusing connection.",
156                         deviceInfo);
157                 return ConnectionStatus.REFUSING_AUXILIARY_CONNECTION;
158             } else {
159                 if (contextChain.addAuxiliaryConnection(connectionContext)) {
160                     LOG.info("An auxiliary connection was added to device: {}", deviceInfo);
161                     return ConnectionStatus.MAY_CONTINUE;
162                 } else {
163                     LOG.warn("Not able to add auxiliary connection to the device {}", deviceInfo);
164                     return ConnectionStatus.REFUSING_AUXILIARY_CONNECTION;
165                 }
166             }
167         } else {
168             LOG.info("Device {} connected.", deviceInfo);
169             final boolean contextExists = contextChain != null;
170             final boolean isClosing = contextExists && contextChain.isClosing();
171
172             if (!isClosing && connectingDevices.putIfAbsent(deviceInfo, connectionContext) != null) {
173                 LOG.warn("Device {} is already trying to connect, wait until succeeded or disconnected.", deviceInfo);
174                 return ConnectionStatus.ALREADY_CONNECTED;
175             }
176
177             if (contextExists) {
178                 if (isClosing) {
179                     LOG.warn("Device {} is already in termination state, closing all incoming connections.",
180                             deviceInfo);
181                     return ConnectionStatus.CLOSING;
182                 }
183
184                 LOG.warn("Device {} already connected. Closing previous connection", deviceInfo);
185                 destroyContextChain(deviceInfo);
186                 LOG.info("Old connection dropped, creating new context chain for device {}", deviceInfo);
187                 createContextChain(connectionContext);
188             } else {
189                 LOG.info("No context chain found for device: {}, creating new.", deviceInfo);
190                 createContextChain(connectionContext);
191             }
192
193             return ConnectionStatus.MAY_CONTINUE;
194         }
195
196     }
197
198     @Override
199     public void onNotAbleToStartMastership(@Nonnull final DeviceInfo deviceInfo,
200                                            @Nonnull final String reason,
201                                            final boolean mandatory) {
202         LOG.warn("Not able to set MASTER role on device {}, reason: {}", deviceInfo, reason);
203
204         if (!mandatory) {
205             return;
206         }
207
208         Optional.ofNullable(contextChainMap.get(deviceInfo)).ifPresent(contextChain -> {
209             LOG.warn("This mastering is mandatory, destroying context chain and closing connection for device {}.",
210                     deviceInfo);
211             destroyContextChain(deviceInfo);
212         });
213     }
214
215     @Override
216     public void onMasterRoleAcquired(@Nonnull final DeviceInfo deviceInfo,
217                                      @Nonnull final ContextChainMastershipState mastershipState) {
218         Optional.ofNullable(contextChainMap.get(deviceInfo)).ifPresent(contextChain -> {
219             if (ownershipChangeListener.isReconciliationFrameworkRegistered() &&
220                     !ContextChainMastershipState.INITIAL_SUBMIT.equals(mastershipState)) {
221                 if (contextChain.isMastered(mastershipState, true)) {
222                     Futures.addCallback(
223                             ownershipChangeListener.becomeMasterBeforeSubmittedDS(deviceInfo),
224                             reconciliationFrameworkCallback(deviceInfo, contextChain),
225                             MoreExecutors.directExecutor());
226                 }
227             } else if (contextChain.isMastered(mastershipState, false)) {
228                 LOG.info("Role MASTER was granted to device {}", deviceInfo);
229                 ownershipChangeListener.becomeMaster(deviceInfo);
230                 deviceManager.sendNodeAddedNotification(deviceInfo.getNodeInstanceIdentifier());
231             }
232         });
233     }
234
235     @Override
236     public void onSlaveRoleAcquired(final DeviceInfo deviceInfo) {
237         ownershipChangeListener.becomeSlaveOrDisconnect(deviceInfo);
238         LOG.info("Role SLAVE was granted to device {}", deviceInfo);
239         Optional.ofNullable(contextChainMap.get(deviceInfo)).ifPresent(ContextChain::makeContextChainStateSlave);
240     }
241
242     @Override
243     public void onSlaveRoleNotAcquired(final DeviceInfo deviceInfo, final String reason) {
244         LOG.warn("Not able to set SLAVE role on device {}, reason: {}", deviceInfo, reason);
245         Optional.ofNullable(contextChainMap.get(deviceInfo)).ifPresent(contextChain -> destroyContextChain(deviceInfo));
246     }
247
248     @Override
249     public void onDeviceDisconnected(final ConnectionContext connectionContext) {
250         final DeviceInfo deviceInfo = connectionContext.getDeviceInfo();
251
252         Optional.ofNullable(connectionContext.getDeviceInfo())
253                 .map(contextChainMap::get)
254                 .ifPresent(contextChain -> {
255                     if (contextChain.auxiliaryConnectionDropped(connectionContext)) {
256                         LOG.info("Auxiliary connection from device {} disconnected.", deviceInfo);
257                     } else {
258                         LOG.info("Device {} disconnected.", deviceInfo);
259                         destroyContextChain(deviceInfo);
260                     }
261                 });
262     }
263
264     @VisibleForTesting
265     boolean checkAllManagers() {
266         return Objects.nonNull(deviceManager)
267                 && Objects.nonNull(rpcManager)
268                 && Objects.nonNull(statisticsManager)
269                 && Objects.nonNull(roleManager);
270     }
271
272     @Override
273     public void close() throws Exception {
274         Map<DeviceInfo, ContextChain> copyOfChains = new HashMap<>(contextChainMap);
275         copyOfChains.keySet().forEach(this::destroyContextChain);
276         copyOfChains.clear();
277         contextChainMap.clear();
278         eosListenerRegistration.close();
279     }
280
281     @Override
282     public void ownershipChanged(EntityOwnershipChange entityOwnershipChange) {
283         if (entityOwnershipChange.hasOwner()) {
284             return;
285         }
286
287         final String entityName = getEntityNameFromOwnershipChange(entityOwnershipChange);
288
289         if (Objects.nonNull(entityName)) {
290             LOG.debug("Entity {} has no owner", entityName);
291             final NodeId nodeId = new NodeId(entityName);
292
293             try {
294                 final KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier =
295                         DeviceStateUtil.createNodeInstanceIdentifier(nodeId);
296
297                 deviceManager.sendNodeRemovedNotification(nodeInstanceIdentifier);
298
299                 LOG.info("Try to remove device {} from operational DS", nodeId);
300                 deviceManager
301                         .removeDeviceFromOperationalDS(nodeInstanceIdentifier)
302                         .get(REMOVE_DEVICE_FROM_DS_TIMEOUT, TimeUnit.MILLISECONDS);
303                 LOG.info("Removing device from operational DS {} was successful", nodeId);
304             } catch (TimeoutException | ExecutionException | NullPointerException | InterruptedException e) {
305                 LOG.warn("Not able to remove device {} from operational DS. ",nodeId, e);
306             }
307         }
308     }
309
310     private void destroyContextChain(final DeviceInfo deviceInfo) {
311         ownershipChangeListener.becomeSlaveOrDisconnect(deviceInfo);
312         Optional.ofNullable(contextChainMap.get(deviceInfo)).ifPresent(contextChain -> {
313             deviceManager.sendNodeRemovedNotification(deviceInfo.getNodeInstanceIdentifier());
314             contextChain.close();
315         });
316     }
317
318     @Override
319     public List<DeviceInfo> listOfMasteredDevices() {
320         return contextChainMap
321                 .entrySet()
322                 .stream()
323                 .filter(deviceInfoContextChainEntry -> deviceInfoContextChainEntry
324                         .getValue()
325                         .isMastered(ContextChainMastershipState.CHECK, false))
326                 .map(Map.Entry::getKey)
327                 .collect(Collectors.toList());
328     }
329
330     @Override
331     public boolean isAnyDeviceMastered() {
332         return contextChainMap
333                 .entrySet()
334                 .stream()
335                 .findAny()
336                 .filter(deviceInfoContextChainEntry -> deviceInfoContextChainEntry.getValue()
337                         .isMastered(ContextChainMastershipState.CHECK, false))
338                 .isPresent();
339     }
340
341     private String getEntityNameFromOwnershipChange(final EntityOwnershipChange entityOwnershipChange) {
342         final YangInstanceIdentifier.NodeIdentifierWithPredicates lastIdArgument =
343                 (YangInstanceIdentifier.NodeIdentifierWithPredicates) entityOwnershipChange
344                         .getEntity()
345                         .getId()
346                         .getLastPathArgument();
347
348         return lastIdArgument
349                 .getKeyValues()
350                 .values()
351                 .iterator()
352                 .next()
353                 .toString();
354     }
355
356     @Override
357     public void onDeviceRemoved(final DeviceInfo deviceInfo) {
358         contextChainMap.remove(deviceInfo);
359         LOG.debug("Context chain removed for node {}", deviceInfo);
360     }
361
362     private FutureCallback<ResultState> reconciliationFrameworkCallback(
363             @Nonnull DeviceInfo deviceInfo,
364             ContextChain contextChain) {
365         return new FutureCallback<ResultState>() {
366             @Override
367             public void onSuccess(@Nullable ResultState result) {
368                 if (ResultState.DONOTHING == result) {
369                     LOG.info("Device {} connection is enabled by reconciliation framework.", deviceInfo);
370                     contextChain.continueInitializationAfterReconciliation();
371                 } else {
372                     LOG.warn("Reconciliation framework failure for device {}", deviceInfo);
373                     destroyContextChain(deviceInfo);
374                 }
375             }
376
377             @Override
378             public void onFailure(@Nonnull Throwable throwable) {
379                 LOG.warn("Reconciliation framework failure.");
380                 destroyContextChain(deviceInfo);
381             }
382         };
383     }
384 }