c299772f6efcbd8f2f054afbcb63c1ab0f9bd4c5
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / device / DeviceManagerImpl.java
1 /**
2  * Copyright (c) 2015, 2017 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.collect.Iterators;
11 import com.google.common.util.concurrent.CheckedFuture;
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.Futures;
14 import io.netty.util.HashedWheelTimer;
15 import io.netty.util.internal.ConcurrentSet;
16 import java.util.Collections;
17 import java.util.Iterator;
18 import java.util.Optional;
19 import java.util.Set;
20 import java.util.concurrent.ConcurrentHashMap;
21 import java.util.concurrent.ConcurrentMap;
22 import java.util.concurrent.ExecutionException;
23 import java.util.concurrent.ScheduledThreadPoolExecutor;
24 import java.util.concurrent.TimeUnit;
25 import javax.annotation.Nonnull;
26 import javax.annotation.Nullable;
27 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
28 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
29 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
30 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
31 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
32 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueueHandlerRegistration;
33 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
34 import org.opendaylight.openflowplugin.api.openflow.connection.OutboundQueueProvider;
35 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
36 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
37 import org.opendaylight.openflowplugin.api.openflow.device.DeviceManager;
38 import org.opendaylight.openflowplugin.api.openflow.device.TranslatorLibrary;
39 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
40 import org.opendaylight.openflowplugin.extension.api.ExtensionConverterProviderKeeper;
41 import org.opendaylight.openflowplugin.extension.api.core.extension.ExtensionConverterProvider;
42 import org.opendaylight.openflowplugin.impl.connection.OutboundQueueProviderImpl;
43 import org.opendaylight.openflowplugin.impl.device.initialization.DeviceInitializerProvider;
44 import org.opendaylight.openflowplugin.impl.device.listener.OpenflowProtocolListenerFullImpl;
45 import org.opendaylight.openflowplugin.impl.services.sal.SalRoleServiceImpl;
46 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRemovedBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdatedBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodesBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
54 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
55 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
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 long globalNotificationQuota;
67     private boolean switchFeaturesMandatory;
68     private boolean isFlowRemovedNotificationOn;
69     private boolean skipTableFeatures;
70     private static final int SPY_RATE = 10;
71
72     private final DataBroker dataBroker;
73     private final DeviceInitializerProvider deviceInitializerProvider;
74     private final ConvertorExecutor convertorExecutor;
75     private TranslatorLibrary translatorLibrary;
76
77     private final ConcurrentMap<DeviceInfo, DeviceContext> deviceContexts = new ConcurrentHashMap<>();
78     private final Set<DeviceInfo> notificationCreateNodeSend = new ConcurrentSet<>();
79
80     private long barrierIntervalNanos;
81     private int barrierCountLimit;
82
83     private ExtensionConverterProvider extensionConverterProvider;
84     private ScheduledThreadPoolExecutor spyPool;
85     private final NotificationPublishService notificationPublishService;
86     private final MessageSpy messageSpy;
87     private final HashedWheelTimer hashedWheelTimer;
88     private final boolean useSingleLayerSerialization;
89
90     public DeviceManagerImpl(@Nonnull final DataBroker dataBroker,
91                              @Nonnull final MessageSpy messageSpy,
92                              @Nullable final NotificationPublishService notificationPublishService,
93                              @Nonnull final HashedWheelTimer hashedWheelTimer,
94                              @Nonnull final ConvertorExecutor convertorExecutor,
95                              @Nonnull final DeviceInitializerProvider deviceInitializerProvider,
96                              final boolean useSingleLayerSerialization) {
97
98         this.dataBroker = dataBroker;
99         this.deviceInitializerProvider = deviceInitializerProvider;
100         this.useSingleLayerSerialization = useSingleLayerSerialization;
101
102         /* merge empty nodes to oper DS to predict any problems with missing parent for Node */
103         final WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
104         final NodesBuilder nodesBuilder = new NodesBuilder();
105         nodesBuilder.setNode(Collections.<Node>emptyList());
106         tx.merge(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(Nodes.class), nodesBuilder.build());
107         try {
108             tx.submit().get();
109         } catch (ExecutionException | InterruptedException e) {
110             LOG.error("Creation of node failed.", e);
111             throw new IllegalStateException(e);
112         }
113
114         this.convertorExecutor = convertorExecutor;
115         this.hashedWheelTimer = hashedWheelTimer;
116         this.spyPool = new ScheduledThreadPoolExecutor(1);
117         this.notificationPublishService = notificationPublishService;
118         this.messageSpy = messageSpy;
119     }
120
121     @Override
122     public TranslatorLibrary oook() {
123         return translatorLibrary;
124     }
125
126     @Override
127     public void setTranslatorLibrary(final TranslatorLibrary translatorLibrary) {
128         this.translatorLibrary = translatorLibrary;
129     }
130
131     @Override
132     public void close() {
133         for (final Iterator<DeviceContext> iterator = Iterators.consumingIterator(deviceContexts.values().iterator());
134                 iterator.hasNext();) {
135             final DeviceContext deviceCtx = iterator.next();
136             deviceCtx.shutdownConnection();
137             deviceCtx.shuttingDownDataStoreTransactions();
138         }
139
140         Optional.ofNullable(spyPool).ifPresent(ScheduledThreadPoolExecutor::shutdownNow);
141         spyPool = null;
142
143     }
144
145     @Override
146     public void initialize() {
147         spyPool.scheduleAtFixedRate(messageSpy, SPY_RATE, SPY_RATE, TimeUnit.SECONDS);
148     }
149
150     @Override
151     public void setExtensionConverterProvider(final ExtensionConverterProvider extensionConverterProvider) {
152         this.extensionConverterProvider = extensionConverterProvider;
153     }
154
155     @Override
156     public ExtensionConverterProvider getExtensionConverterProvider() {
157         return extensionConverterProvider;
158     }
159
160     @Override
161     public void setFlowRemovedNotificationOn(boolean isNotificationFlowRemovedOff) {
162         this.isFlowRemovedNotificationOn = isNotificationFlowRemovedOff;
163     }
164
165     @Override
166     public boolean isFlowRemovedNotificationOn() {
167         return this.isFlowRemovedNotificationOn;
168     }
169
170     @Override
171     public void setGlobalNotificationQuota(final long globalNotificationQuota) {
172         this.globalNotificationQuota = globalNotificationQuota;
173     }
174
175     @Override
176     public void setSwitchFeaturesMandatory(final boolean switchFeaturesMandatory) {
177         this.switchFeaturesMandatory = switchFeaturesMandatory;
178     }
179
180     @Override
181     public void setSkipTableFeatures(boolean skipTableFeaturesValue) {
182         skipTableFeatures = skipTableFeaturesValue;
183     }
184
185     @Override
186     public void setBarrierCountLimit(final int barrierCountLimit) {
187         this.barrierCountLimit = barrierCountLimit;
188     }
189
190     @Override
191     public void setBarrierInterval(final long barrierTimeoutLimit) {
192         this.barrierIntervalNanos = TimeUnit.MILLISECONDS.toNanos(barrierTimeoutLimit);
193     }
194
195     @Override
196     public CheckedFuture<Void, TransactionCommitFailedException> removeDeviceFromOperationalDS(final KeyedInstanceIdentifier<Node, NodeKey> ii) {
197         final WriteTransaction delWtx = dataBroker.newWriteOnlyTransaction();
198         delWtx.delete(LogicalDatastoreType.OPERATIONAL, ii);
199         final CheckedFuture<Void, TransactionCommitFailedException> delFuture = delWtx.submit();
200
201         Futures.addCallback(delFuture, new FutureCallback<Void>() {
202             @Override
203             public void onSuccess(final Void result) {
204                 if (LOG.isDebugEnabled()) {
205                     LOG.debug("Delete Node {} was successful", ii);
206                 }
207             }
208
209             @Override
210             public void onFailure(@Nonnull final Throwable t) {
211                 LOG.warn("Delete node {} failed with exception {}", ii, t);
212             }
213         });
214
215         return delFuture;
216     }
217
218     @Override
219     public CheckedFuture<Void, TransactionCommitFailedException> removeDeviceFromOperationalDS(final DeviceInfo deviceInfo) {
220         return this.removeDeviceFromOperationalDS(deviceInfo.getNodeInstanceIdentifier());
221     }
222
223     public DeviceContext createContext(@Nonnull final ConnectionContext connectionContext) {
224
225         LOG.info("ConnectionEvent: Device connected to controller, Device:{}, NodeId:{}",
226                 connectionContext.getConnectionAdapter().getRemoteAddress(),
227                 connectionContext.getDeviceInfo().getNodeId());
228
229         connectionContext.getConnectionAdapter().setPacketInFiltering(true);
230
231         final OutboundQueueProvider outboundQueueProvider
232                 = new OutboundQueueProviderImpl(connectionContext.getDeviceInfo().getVersion());
233
234         connectionContext.setOutboundQueueProvider(outboundQueueProvider);
235         final OutboundQueueHandlerRegistration<OutboundQueueProvider> outboundQueueHandlerRegistration =
236                 connectionContext.getConnectionAdapter().registerOutboundQueueHandler(
237                         outboundQueueProvider,
238                         barrierCountLimit,
239                         barrierIntervalNanos);
240         connectionContext.setOutboundQueueHandleRegistration(outboundQueueHandlerRegistration);
241
242
243         final DeviceContext deviceContext = new DeviceContextImpl(
244                 connectionContext,
245                 dataBroker,
246                 messageSpy,
247                 translatorLibrary,
248                 this,
249                 convertorExecutor,
250                 skipTableFeatures,
251                 hashedWheelTimer,
252                 useSingleLayerSerialization,
253                 deviceInitializerProvider);
254
255         deviceContext.setSalRoleService(new SalRoleServiceImpl(deviceContext, deviceContext));
256         deviceContext.setSwitchFeaturesMandatory(switchFeaturesMandatory);
257         ((ExtensionConverterProviderKeeper) deviceContext).setExtensionConverterProvider(extensionConverterProvider);
258         deviceContext.setNotificationPublishService(notificationPublishService);
259
260         deviceContexts.put(connectionContext.getDeviceInfo(), deviceContext);
261         updatePacketInRateLimiters();
262
263         final OpenflowProtocolListenerFullImpl messageListener = new OpenflowProtocolListenerFullImpl(
264                 connectionContext.getConnectionAdapter(), deviceContext);
265
266         connectionContext.getConnectionAdapter().setMessageListener(messageListener);
267
268         return deviceContext;
269     }
270
271     private void updatePacketInRateLimiters() {
272         synchronized (deviceContexts) {
273             final int deviceContextsSize = deviceContexts.size();
274             if (deviceContextsSize > 0) {
275                 long freshNotificationLimit = globalNotificationQuota / deviceContextsSize;
276                 if (freshNotificationLimit < 100) {
277                     freshNotificationLimit = 100;
278                 }
279                 if (LOG.isDebugEnabled()) {
280                     LOG.debug("fresh notification limit = {}", freshNotificationLimit);
281                 }
282                 for (final DeviceContext deviceContext : deviceContexts.values()) {
283                     deviceContext.updatePacketInRateLimit(freshNotificationLimit);
284                 }
285             }
286         }
287     }
288
289     @Override
290     public void sendNodeRemovedNotification(@Nonnull final DeviceInfo deviceInfo) {
291         notificationCreateNodeSend.remove(deviceInfo);
292         NodeRemovedBuilder builder = new NodeRemovedBuilder();
293         builder.setNodeRef(new NodeRef(deviceInfo.getNodeInstanceIdentifier()));
294         if (LOG.isDebugEnabled()) {
295             LOG.debug("Publishing node removed notification for {}", deviceInfo.getLOGValue());
296         }
297         notificationPublishService.offerNotification(builder.build());
298     }
299
300
301     @Override
302     public void onDeviceRemoved(final DeviceInfo deviceInfo) {
303         deviceContexts.remove(deviceInfo);
304         if (LOG.isDebugEnabled()) {
305             LOG.debug("Device context removed for node {}", deviceInfo.getLOGValue());
306         }
307         if (deviceContexts.size() > 0) {
308             this.updatePacketInRateLimiters();
309         }
310     }
311
312     @Override
313     public void sendNodeAddedNotification(@Nonnull final DeviceInfo deviceInfo) {
314         if (!notificationCreateNodeSend.contains(deviceInfo)) {
315             notificationCreateNodeSend.add(deviceInfo);
316             NodeUpdatedBuilder builder = new NodeUpdatedBuilder();
317             builder.setId(deviceInfo.getNodeId());
318             builder.setNodeRef(new NodeRef(deviceInfo.getNodeInstanceIdentifier()));
319             if (LOG.isDebugEnabled()) {
320                 LOG.debug("Publishing node added notification for {}", deviceInfo.getLOGValue());
321             }
322             notificationPublishService.offerNotification(builder.build());
323         }
324     }
325 }