DevManager functionality add
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / device / DeviceManagerImpl.java
1 /**
2  * Copyright (c) 2015 Cisco Systems, Inc. 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.device;
9
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.base.Function;
12 import com.google.common.base.Preconditions;
13 import com.google.common.base.Verify;
14 import com.google.common.collect.Iterators;
15 import com.google.common.util.concurrent.AsyncFunction;
16 import com.google.common.util.concurrent.FutureCallback;
17 import com.google.common.util.concurrent.Futures;
18 import com.google.common.util.concurrent.ListenableFuture;
19 import io.netty.util.TimerTask;
20 import java.util.Collections;
21 import java.util.Iterator;
22 import java.util.concurrent.ConcurrentHashMap;
23 import java.util.concurrent.ConcurrentMap;
24 import java.util.concurrent.ExecutionException;
25 import java.util.concurrent.ScheduledThreadPoolExecutor;
26 import java.util.concurrent.TimeUnit;
27 import javax.annotation.CheckForNull;
28 import javax.annotation.Nonnull;
29 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
30 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
31 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
32 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
33 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueueHandlerRegistration;
34 import org.opendaylight.openflowplugin.api.openflow.OFPContext;
35 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
36 import org.opendaylight.openflowplugin.api.openflow.connection.OutboundQueueProvider;
37 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
38 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
39 import org.opendaylight.openflowplugin.api.openflow.device.DeviceManager;
40 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
41 import org.opendaylight.openflowplugin.api.openflow.device.TranslatorLibrary;
42 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceInitializationPhaseHandler;
43 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceTerminationPhaseHandler;
44 import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleConductor;
45 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext;
46 import org.opendaylight.openflowplugin.extension.api.ExtensionConverterProviderKeeper;
47 import org.opendaylight.openflowplugin.extension.api.core.extension.ExtensionConverterProvider;
48 import org.opendaylight.openflowplugin.impl.connection.OutboundQueueProviderImpl;
49 import org.opendaylight.openflowplugin.impl.device.listener.OpenflowProtocolListenerFullImpl;
50 import org.opendaylight.openflowplugin.impl.util.DeviceInitializationUtils;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodesBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
55 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
58
59 /**
60  *
61  */
62 public class DeviceManagerImpl implements DeviceManager, ExtensionConverterProviderKeeper {
63
64     private static final Logger LOG = LoggerFactory.getLogger(DeviceManagerImpl.class);
65
66     private final long globalNotificationQuota;
67     private final boolean switchFeaturesMandatory;
68
69     private final int spyRate = 10;
70
71     private final DataBroker dataBroker;
72     private TranslatorLibrary translatorLibrary;
73     private DeviceInitializationPhaseHandler deviceInitPhaseHandler;
74     private DeviceTerminationPhaseHandler deviceTerminPhaseHandler;
75
76     private final ConcurrentMap<DeviceInfo, DeviceContext> deviceContexts = new ConcurrentHashMap<>();
77
78     private final long barrierIntervalNanos;
79     private final int barrierCountLimit;
80     private ExtensionConverterProvider extensionConverterProvider;
81     private ScheduledThreadPoolExecutor spyPool;
82
83     private final LifecycleConductor conductor;
84
85     public DeviceManagerImpl(@Nonnull final DataBroker dataBroker,
86                              final long globalNotificationQuota, final boolean switchFeaturesMandatory,
87                              final long barrierInterval, final int barrierCountLimit,
88                              final LifecycleConductor lifecycleConductor) {
89         this.switchFeaturesMandatory = switchFeaturesMandatory;
90         this.globalNotificationQuota = globalNotificationQuota;
91         this.dataBroker = Preconditions.checkNotNull(dataBroker);
92         /* merge empty nodes to oper DS to predict any problems with missing parent for Node */
93         final WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
94
95         final NodesBuilder nodesBuilder = new NodesBuilder();
96         nodesBuilder.setNode(Collections.<Node>emptyList());
97         tx.merge(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(Nodes.class), nodesBuilder.build());
98         try {
99             tx.submit().get();
100         } catch (ExecutionException | InterruptedException e) {
101             LOG.error("Creation of node failed.", e);
102             throw new IllegalStateException(e);
103         }
104
105         this.barrierIntervalNanos = TimeUnit.MILLISECONDS.toNanos(barrierInterval);
106         this.barrierCountLimit = barrierCountLimit;
107
108         this.conductor = lifecycleConductor;
109         spyPool = new ScheduledThreadPoolExecutor(1);
110     }
111
112
113     @Override
114     public void setDeviceInitializationPhaseHandler(final DeviceInitializationPhaseHandler handler) {
115         this.deviceInitPhaseHandler = handler;
116     }
117
118     @Override
119     public void onDeviceContextLevelUp(@CheckForNull DeviceInfo deviceInfo) throws Exception {
120         // final phase - we have to add new Device to MD-SAL DataStore
121         LOG.debug("Final phase of DeviceContextLevelUp for Node: {} ", deviceInfo.getNodeId());
122         DeviceContext deviceContext = Preconditions.checkNotNull(deviceContexts.get(deviceInfo));
123         ((DeviceContextImpl) deviceContext).initialSubmitTransaction();
124         deviceContext.onPublished();
125     }
126
127     @Override
128     public boolean deviceConnected(@CheckForNull final ConnectionContext connectionContext) throws Exception {
129         Preconditions.checkArgument(connectionContext != null);
130
131         DeviceInfo deviceInfo = connectionContext.getDeviceInfo();
132         /**
133          * This part prevent destroy another device context. Throwing here an exception result to propagate close connection
134          * in {@link org.opendaylight.openflowplugin.impl.connection.org.opendaylight.openflowplugin.impl.connection.HandshakeContextImpl}
135          * If context already exist we are in state closing process (connection flapping) and we should not propagate connection close
136          */
137          if (deviceContexts.containsKey(deviceInfo)) {
138             LOG.warn("Rejecting connection from node which is already connected and there exist deviceContext for it: {}", connectionContext.getNodeId());
139              return false;
140          }
141
142         LOG.info("ConnectionEvent: Device connected to controller, Device:{}, NodeId:{}",
143                 connectionContext.getConnectionAdapter().getRemoteAddress(), deviceInfo.getNodeId());
144
145         // Add Disconnect handler
146         connectionContext.setDeviceDisconnectedHandler(DeviceManagerImpl.this);
147         // Cache this for clarity
148         final ConnectionAdapter connectionAdapter = connectionContext.getConnectionAdapter();
149
150         //FIXME: as soon as auxiliary connection are fully supported then this is needed only before device context published
151         connectionAdapter.setPacketInFiltering(true);
152
153         final Short version = connectionContext.getFeatures().getVersion();
154         final OutboundQueueProvider outboundQueueProvider = new OutboundQueueProviderImpl(version);
155
156         connectionContext.setOutboundQueueProvider(outboundQueueProvider);
157         final OutboundQueueHandlerRegistration<OutboundQueueProvider> outboundQueueHandlerRegistration =
158                 connectionAdapter.registerOutboundQueueHandler(outboundQueueProvider, barrierCountLimit, barrierIntervalNanos);
159         connectionContext.setOutboundQueueHandleRegistration(outboundQueueHandlerRegistration);
160
161         final DeviceState deviceState = new DeviceStateImpl();
162         final DeviceContext deviceContext = new DeviceContextImpl(connectionContext,
163                 deviceState,
164                 dataBroker,
165                 conductor,
166                 outboundQueueProvider,
167                 translatorLibrary,
168                 switchFeaturesMandatory);
169
170         Verify.verify(deviceContexts.putIfAbsent(deviceInfo, deviceContext) == null, "DeviceCtx still not closed.");
171
172         ((ExtensionConverterProviderKeeper) deviceContext).setExtensionConverterProvider(extensionConverterProvider);
173         deviceContext.setNotificationPublishService(conductor.getNotificationPublishService());
174
175         updatePacketInRateLimiters();
176
177         final OpenflowProtocolListenerFullImpl messageListener = new OpenflowProtocolListenerFullImpl(
178                 connectionAdapter, deviceContext);
179         connectionAdapter.setMessageListener(messageListener);
180         deviceState.setValid(true);
181
182         deviceInitPhaseHandler.onDeviceContextLevelUp(connectionContext.getDeviceInfo());
183
184         return true;
185     }
186
187     private void updatePacketInRateLimiters() {
188         synchronized (deviceContexts) {
189             final int deviceContextsSize = deviceContexts.size();
190             if (deviceContextsSize > 0) {
191                 long freshNotificationLimit = globalNotificationQuota / deviceContextsSize;
192                 if (freshNotificationLimit < 100) {
193                     freshNotificationLimit = 100;
194                 }
195                 LOG.debug("fresh notification limit = {}", freshNotificationLimit);
196                 for (final DeviceContext deviceContext : deviceContexts.values()) {
197                     deviceContext.updatePacketInRateLimit(freshNotificationLimit);
198                 }
199             }
200         }
201     }
202
203     @Override
204     public TranslatorLibrary oook() {
205         return translatorLibrary;
206     }
207
208     @Override
209     public void setTranslatorLibrary(final TranslatorLibrary translatorLibrary) {
210         this.translatorLibrary = translatorLibrary;
211     }
212
213     @Override
214     public void close() {
215         for (final Iterator<DeviceContext> iterator = Iterators.consumingIterator(deviceContexts.values().iterator());
216                 iterator.hasNext();) {
217             final DeviceContext deviceCtx = iterator.next();
218             deviceCtx.shutdownConnection();
219             deviceCtx.shuttingDownDataStoreTransactions();
220         }
221
222         if (spyPool != null) {
223             spyPool.shutdownNow();
224             spyPool = null;
225         }
226     }
227
228     @Override
229     public void onDeviceContextLevelDown(final DeviceInfo deviceInfo) {
230         LOG.debug("onDeviceContextClosed for Node {}", deviceInfo.getNodeId());
231         deviceContexts.remove(deviceInfo);
232         updatePacketInRateLimiters();
233     }
234
235     @Override
236     public void initialize() {
237         spyPool.scheduleAtFixedRate(conductor.getMessageIntelligenceAgency(), spyRate, spyRate, TimeUnit.SECONDS);
238     }
239
240     @Override
241     public DeviceContext getDeviceContextFromNodeId(DeviceInfo deviceInfo) {
242         return deviceContexts.get(deviceInfo);
243     }
244
245     @Override
246     public void setExtensionConverterProvider(final ExtensionConverterProvider extensionConverterProvider) {
247         this.extensionConverterProvider = extensionConverterProvider;
248     }
249
250     @Override
251     public ExtensionConverterProvider getExtensionConverterProvider() {
252         return extensionConverterProvider;
253     }
254
255     @Override
256     public void setDeviceTerminationPhaseHandler(final DeviceTerminationPhaseHandler handler) {
257         this.deviceTerminPhaseHandler = handler;
258     }
259
260     @Override
261     public void onDeviceDisconnected(final ConnectionContext connectionContext) {
262         LOG.trace("onDeviceDisconnected method call for Node: {}", connectionContext.getNodeId());
263         final DeviceInfo deviceInfo = connectionContext.getDeviceInfo();
264         final DeviceContext deviceCtx = this.deviceContexts.get(deviceInfo);
265
266         if (null == deviceCtx) {
267             LOG.info("DeviceContext for Node {} was not found. Connection is terminated without OFP context suite.", deviceInfo.getNodeId());
268             return;
269         }
270
271         if (!connectionContext.equals(deviceCtx.getPrimaryConnectionContext())) {
272             /* Connection is not PrimaryConnection so try to remove from Auxiliary Connections */
273             deviceCtx.removeAuxiliaryConnectionContext(connectionContext);
274         } else {
275             /* Device is disconnected and so we need to close TxManager */
276             final ListenableFuture<Void> future = deviceCtx.shuttingDownDataStoreTransactions();
277             Futures.addCallback(future, new FutureCallback<Void>() {
278
279                 @Override
280                 public void onSuccess(final Void result) {
281                     LOG.debug("TxChainManager for device {} is closed successful.", deviceInfo.getNodeId());
282                     deviceTerminPhaseHandler.onDeviceContextLevelDown(deviceInfo);
283                 }
284
285                 @Override
286                 public void onFailure(final Throwable t) {
287                     LOG.warn("TxChainManager for device {} failed by closing.", deviceInfo.getNodeId(), t);
288                     deviceTerminPhaseHandler.onDeviceContextLevelDown(deviceInfo);
289                 }
290             });
291             /* Add timer for Close TxManager because it could fain ind cluster without notification */
292             final TimerTask timerTask = timeout -> {
293                 if (!future.isDone()) {
294                     LOG.info("Shutting down TxChain for node {} not completed during 10 sec. Continue anyway.", deviceInfo.getNodeId());
295                     future.cancel(false);
296                 }
297             };
298             conductor.newTimeout(timerTask, 10, TimeUnit.SECONDS);
299         }
300     }
301
302     @VisibleForTesting
303     void addDeviceContextToMap(final DeviceInfo deviceInfo, final DeviceContext deviceContext){
304         deviceContexts.put(deviceInfo, deviceContext);
305     }
306
307     @Override
308     public <T extends OFPContext> T gainContext(final DeviceInfo deviceInfo) {
309         return (T) deviceContexts.get(deviceInfo);
310     }
311
312     @Override
313     public ListenableFuture<Void> onClusterRoleChange(final DeviceInfo deviceInfo, final OfpRole role) {
314         DeviceContext deviceContext = conductor.getDeviceContext(deviceInfo);
315         LOG.trace("onClusterRoleChange {} for node:", role, deviceInfo.getNodeId());
316         if (OfpRole.BECOMEMASTER.equals(role)) {
317             return onDeviceTakeClusterLeadership(deviceInfo);
318         }
319         return ((DeviceContextImpl)deviceContext).getTransactionChainManager().deactivateTransactionManager();
320     }
321
322     private ListenableFuture<Void> onDeviceTakeClusterLeadership(final DeviceInfo deviceInfo) {
323         LOG.trace("onDeviceTakeClusterLeadership for node: {}", deviceInfo.getNodeId());
324         /* validation */
325         StatisticsContext statisticsContext = conductor.getStatisticsContext(deviceInfo);
326         if (statisticsContext == null) {
327             final String errMsg = String.format("DeviceCtx %s is up but we are missing StatisticsContext", deviceInfo.getDatapathId());
328             LOG.warn(errMsg);
329             return Futures.immediateFailedFuture(new IllegalStateException(errMsg));
330         }
331         DeviceContext deviceContext = conductor.getDeviceContext(deviceInfo);
332         /* Prepare init info collecting */
333         deviceContext.getDeviceState().setDeviceSynchronized(false);
334         ((DeviceContextImpl)deviceContext).getTransactionChainManager().activateTransactionManager();
335         /* Init Collecting NodeInfo */
336         final ListenableFuture<Void> initCollectingDeviceInfo = DeviceInitializationUtils.initializeNodeInformation(
337                 deviceContext, switchFeaturesMandatory);
338         /* Init Collecting StatInfo */
339         final ListenableFuture<Boolean> statPollFuture = Futures.transform(initCollectingDeviceInfo,
340                 new AsyncFunction<Void, Boolean>() {
341
342                     @Override
343                     public ListenableFuture<Boolean> apply(@Nonnull final Void input) throws Exception {
344                         statisticsContext.statListForCollectingInitialization();
345                         return statisticsContext.gatherDynamicData();
346                     }
347                 });
348
349         return Futures.transform(statPollFuture, new Function<Boolean, Void>() {
350
351             @Override
352             public Void apply(final Boolean input) {
353                 if (ConnectionContext.CONNECTION_STATE.RIP.equals(conductor.gainConnectionStateSafely(deviceInfo))) {
354                     final String errMsg = String.format("We lost connection for Device %s, context has to be closed.",
355                             deviceInfo.getNodeId());
356                     LOG.warn(errMsg);
357                     throw new IllegalStateException(errMsg);
358                 }
359                 if (!input) {
360                     final String errMsg = String.format("Get Initial Device %s information fails",
361                             deviceInfo.getNodeId());
362                     LOG.warn(errMsg);
363                     throw new IllegalStateException(errMsg);
364                 }
365                 LOG.debug("Get Initial Device {} information is successful", deviceInfo.getNodeId());
366                 deviceContext.getDeviceState().setDeviceSynchronized(true);
367                 ((DeviceContextImpl)deviceContext).getTransactionChainManager().initialSubmitWriteTransaction();
368                 deviceContext.getDeviceState().setStatisticsPollingEnabledProp(true);
369                 return null;
370             }
371         });
372     }
373
374 }