c13d9e09adbd0991704f6842d0ff4acd0a62b848
[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.base.Preconditions;
11 import com.google.common.base.Verify;
12 import com.google.common.collect.Iterators;
13 import io.netty.util.HashedWheelTimer;
14 import java.util.Collections;
15 import java.util.Iterator;
16 import java.util.Map.Entry;
17 import java.util.concurrent.ConcurrentHashMap;
18 import java.util.concurrent.ConcurrentMap;
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.ScheduledThreadPoolExecutor;
21 import java.util.concurrent.TimeUnit;
22 import javax.annotation.CheckForNull;
23 import javax.annotation.Nonnull;
24 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
25 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
26 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
27 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
28 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
29 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
30 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueueHandlerRegistration;
31 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
32 import org.opendaylight.openflowplugin.api.openflow.connection.OutboundQueueProvider;
33 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
34 import org.opendaylight.openflowplugin.api.openflow.device.DeviceManager;
35 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
36 import org.opendaylight.openflowplugin.api.openflow.device.TranslatorLibrary;
37 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceInitializationPhaseHandler;
38 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageIntelligenceAgency;
39 import org.opendaylight.openflowplugin.extension.api.ExtensionConverterProviderKeeper;
40 import org.opendaylight.openflowplugin.extension.api.core.extension.ExtensionConverterProvider;
41 import org.opendaylight.openflowplugin.impl.connection.OutboundQueueProviderImpl;
42 import org.opendaylight.openflowplugin.impl.device.listener.OpenflowProtocolListenerFullImpl;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodesBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50
51 /**
52  *
53  */
54 public class DeviceManagerImpl implements DeviceManager, ExtensionConverterProviderKeeper {
55
56     private static final Logger LOG = LoggerFactory.getLogger(DeviceManagerImpl.class);
57
58     private static final long TICK_DURATION = 10; // 0.5 sec.
59     private final long globalNotificationQuota;
60     private final boolean switchFeaturesMandatory;
61     private ScheduledThreadPoolExecutor spyPool;
62     private final int spyRate = 10;
63
64     private final DataBroker dataBroker;
65     private final HashedWheelTimer hashedWheelTimer;
66     private TranslatorLibrary translatorLibrary;
67     private DeviceInitializationPhaseHandler deviceInitPhaseHandler;
68     private NotificationService notificationService;
69     private NotificationPublishService notificationPublishService;
70
71     private final ConcurrentMap<NodeId, DeviceContext> deviceContexts = new ConcurrentHashMap<>();
72     private final MessageIntelligenceAgency messageIntelligenceAgency;
73
74     private final long barrierNanos = TimeUnit.MILLISECONDS.toNanos(500);
75     private final int maxQueueDepth = 25600;
76     private ExtensionConverterProvider extensionConverterProvider;
77
78     public DeviceManagerImpl(@Nonnull final DataBroker dataBroker,
79                              @Nonnull final MessageIntelligenceAgency messageIntelligenceAgency,
80                              final long globalNotificationQuota, final boolean switchFeaturesMandatory) {
81         this.switchFeaturesMandatory = switchFeaturesMandatory;
82         this.globalNotificationQuota = globalNotificationQuota;
83         this.dataBroker = Preconditions.checkNotNull(dataBroker);
84         hashedWheelTimer = new HashedWheelTimer(TICK_DURATION, TimeUnit.MILLISECONDS, 500);
85         /* merge empty nodes to oper DS to predict any problems with missing parent for Node */
86         final WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
87
88         final NodesBuilder nodesBuilder = new NodesBuilder();
89         nodesBuilder.setNode(Collections.<Node>emptyList());
90         tx.merge(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(Nodes.class), nodesBuilder.build());
91         try {
92             tx.submit().get();
93         } catch (ExecutionException | InterruptedException e) {
94             LOG.error("Creation of node failed.", e);
95             throw new IllegalStateException(e);
96         }
97
98         this.messageIntelligenceAgency = messageIntelligenceAgency;
99     }
100
101
102     @Override
103     public void setDeviceInitializationPhaseHandler(final DeviceInitializationPhaseHandler handler) {
104         deviceInitPhaseHandler = handler;
105     }
106
107     @Override
108     public void onDeviceContextLevelUp(final DeviceContext deviceContext) throws Exception {
109         // final phase - we have to add new Device to MD-SAL DataStore
110         LOG.debug("Final phase of DeviceContextLevelUp for Node: {} ", deviceContext.getDeviceState().getNodeId());
111         Preconditions.checkNotNull(deviceContext);
112         ((DeviceContextImpl) deviceContext).initialSubmitTransaction();
113         deviceContext.onPublished();
114     }
115
116     @Override
117     public void deviceConnected(@CheckForNull final ConnectionContext connectionContext) throws Exception {
118         Preconditions.checkArgument(connectionContext != null);
119         Preconditions.checkState(!deviceContexts.containsKey(connectionContext.getNodeId()),
120                 "Rejecting connection from node which is already connected and there exist deviceContext for it: {}",
121                 connectionContext.getNodeId()
122         );
123         LOG.info("Initializing New Connection DeviceContext for node:{}", connectionContext.getNodeId());
124
125         // Cache this for clarity
126         final ConnectionAdapter connectionAdapter = connectionContext.getConnectionAdapter();
127
128         //FIXME: as soon as auxiliary connection are fully supported then this is needed only before device context published
129         connectionAdapter.setPacketInFiltering(true);
130
131         final Short version = connectionContext.getFeatures().getVersion();
132         final OutboundQueueProvider outboundQueueProvider = new OutboundQueueProviderImpl(version);
133
134         connectionContext.setOutboundQueueProvider(outboundQueueProvider);
135         final OutboundQueueHandlerRegistration<OutboundQueueProvider> outboundQueueHandlerRegistration =
136                 connectionAdapter.registerOutboundQueueHandler(outboundQueueProvider, maxQueueDepth, barrierNanos);
137         connectionContext.setOutboundQueueHandleRegistration(outboundQueueHandlerRegistration);
138
139         final DeviceState deviceState = createDeviceState(connectionContext);
140         final DeviceContext deviceContext = new DeviceContextImpl(connectionContext, deviceState, dataBroker,
141                 hashedWheelTimer, messageIntelligenceAgency, outboundQueueProvider, translatorLibrary, switchFeaturesMandatory);
142
143         Verify.verify(deviceContexts.putIfAbsent(connectionContext.getNodeId(), deviceContext) == null, "DeviceCtx still not closed.");
144         deviceContext.addDeviceContextClosedHandler(this);
145
146         ((ExtensionConverterProviderKeeper) deviceContext).setExtensionConverterProvider(extensionConverterProvider);
147         deviceContext.setNotificationService(notificationService);
148         deviceContext.setNotificationPublishService(notificationPublishService);
149
150         updatePacketInRateLimiters();
151
152         final OpenflowProtocolListenerFullImpl messageListener = new OpenflowProtocolListenerFullImpl(
153                 connectionAdapter, deviceContext);
154         connectionAdapter.setMessageListener(messageListener);
155
156         deviceCtxLevelUp(deviceContext);
157     }
158
159     private static DeviceStateImpl createDeviceState(final @Nonnull ConnectionContext connectionContext) {
160         return new DeviceStateImpl(connectionContext.getFeatures(), connectionContext.getNodeId());
161     }
162
163     private void updatePacketInRateLimiters() {
164         synchronized (deviceContexts) {
165             final int deviceContextsSize = deviceContexts.size();
166             if (deviceContextsSize > 0) {
167                 long freshNotificationLimit = globalNotificationQuota / deviceContextsSize;
168                 if (freshNotificationLimit < 100) {
169                     freshNotificationLimit = 100;
170                 }
171                 LOG.debug("fresh notification limit = {}", freshNotificationLimit);
172                 for (final DeviceContext deviceContext : deviceContexts.values()) {
173                     deviceContext.updatePacketInRateLimit(freshNotificationLimit);
174                 }
175             }
176         }
177     }
178
179     private void deviceCtxLevelUp(final DeviceContext deviceContext) throws Exception {
180         deviceContext.getDeviceState().setValid(true);
181         LOG.trace("Device context level up called.");
182         deviceInitPhaseHandler.onDeviceContextLevelUp(deviceContext);
183     }
184
185     @Override
186     public TranslatorLibrary oook() {
187         return translatorLibrary;
188     }
189
190     @Override
191     public void setTranslatorLibrary(final TranslatorLibrary translatorLibrary) {
192         this.translatorLibrary = translatorLibrary;
193     }
194
195     @Override
196     public void setNotificationService(final NotificationService notificationServiceParam) {
197         notificationService = notificationServiceParam;
198     }
199
200     @Override
201     public void setNotificationPublishService(final NotificationPublishService notificationService) {
202         notificationPublishService = notificationService;
203     }
204
205     @Override
206     public void close() {
207         for (final Iterator<Entry<NodeId, DeviceContext>> iterator = Iterators
208                 .consumingIterator(deviceContexts.entrySet().iterator()); iterator.hasNext();) {
209             iterator.next().getValue().close();
210         }
211     }
212
213     @Override
214     public void onDeviceContextClosed(final DeviceContext deviceContext) {
215         LOG.trace("onDeviceContextClosed for Node {}", deviceContext.getDeviceState().getNodeId());
216         deviceContexts.remove(deviceContext.getPrimaryConnectionContext().getNodeId());
217         updatePacketInRateLimiters();
218     }
219
220     @Override
221     public void initialize() {
222         spyPool = new ScheduledThreadPoolExecutor(1);
223         spyPool.scheduleAtFixedRate(messageIntelligenceAgency, spyRate, spyRate, TimeUnit.SECONDS);
224     }
225
226     @Override
227     public void setExtensionConverterProvider(final ExtensionConverterProvider extensionConverterProvider) {
228         this.extensionConverterProvider = extensionConverterProvider;
229     }
230
231     @Override
232     public ExtensionConverterProvider getExtensionConverterProvider() {
233         return extensionConverterProvider;
234     }
235 }