2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.openflowplugin.impl.device;
10 import com.google.common.base.Preconditions;
11 import com.google.common.base.Verify;
12 import io.netty.util.HashedWheelTimer;
13 import java.util.Collections;
14 import java.util.concurrent.ConcurrentHashMap;
15 import java.util.concurrent.ConcurrentMap;
16 import java.util.concurrent.ExecutionException;
17 import java.util.concurrent.ScheduledThreadPoolExecutor;
18 import java.util.concurrent.TimeUnit;
19 import javax.annotation.CheckForNull;
20 import javax.annotation.Nonnull;
21 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
22 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
23 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
24 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
25 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
26 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
27 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueueHandlerRegistration;
28 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
29 import org.opendaylight.openflowplugin.api.openflow.connection.OutboundQueueProvider;
30 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
31 import org.opendaylight.openflowplugin.api.openflow.device.DeviceManager;
32 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
33 import org.opendaylight.openflowplugin.api.openflow.device.TranslatorLibrary;
34 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceInitializationPhaseHandler;
35 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageIntelligenceAgency;
36 import org.opendaylight.openflowplugin.extension.api.ExtensionConverterProviderKeeper;
37 import org.opendaylight.openflowplugin.extension.api.core.extension.ExtensionConverterProvider;
38 import org.opendaylight.openflowplugin.impl.connection.OutboundQueueProviderImpl;
39 import org.opendaylight.openflowplugin.impl.device.listener.OpenflowProtocolListenerFullImpl;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodesBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
51 public class DeviceManagerImpl implements DeviceManager, ExtensionConverterProviderKeeper {
53 private static final Logger LOG = LoggerFactory.getLogger(DeviceManagerImpl.class);
55 private static final long TICK_DURATION = 10; // 0.5 sec.
56 private final long globalNotificationQuota;
57 private final boolean switchFeaturesMandatory;
58 private ScheduledThreadPoolExecutor spyPool;
59 private final int spyRate = 10;
61 private final DataBroker dataBroker;
62 private final HashedWheelTimer hashedWheelTimer;
63 private TranslatorLibrary translatorLibrary;
64 private DeviceInitializationPhaseHandler deviceInitPhaseHandler;
65 private NotificationService notificationService;
66 private NotificationPublishService notificationPublishService;
68 private final ConcurrentMap<NodeId, DeviceContext> deviceContexts = new ConcurrentHashMap<>();
69 private final MessageIntelligenceAgency messageIntelligenceAgency;
71 private final long barrierNanos = TimeUnit.MILLISECONDS.toNanos(500);
72 private final int maxQueueDepth = 25600;
73 private ExtensionConverterProvider extensionConverterProvider;
75 public DeviceManagerImpl(@Nonnull final DataBroker dataBroker,
76 @Nonnull final MessageIntelligenceAgency messageIntelligenceAgency,
77 final long globalNotificationQuota, final boolean switchFeaturesMandatory) {
78 this.switchFeaturesMandatory = switchFeaturesMandatory;
79 this.globalNotificationQuota = globalNotificationQuota;
80 this.dataBroker = Preconditions.checkNotNull(dataBroker);
81 hashedWheelTimer = new HashedWheelTimer(TICK_DURATION, TimeUnit.MILLISECONDS, 500);
82 /* merge empty nodes to oper DS to predict any problems with missing parent for Node */
83 final WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
85 final NodesBuilder nodesBuilder = new NodesBuilder();
86 nodesBuilder.setNode(Collections.<Node>emptyList());
87 tx.merge(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(Nodes.class), nodesBuilder.build());
90 } catch (ExecutionException | InterruptedException e) {
91 LOG.error("Creation of node failed.", e);
92 throw new IllegalStateException(e);
95 this.messageIntelligenceAgency = messageIntelligenceAgency;
100 public void setDeviceInitializationPhaseHandler(final DeviceInitializationPhaseHandler handler) {
101 deviceInitPhaseHandler = handler;
105 public void onDeviceContextLevelUp(final DeviceContext deviceContext) {
106 // final phase - we have to add new Device to MD-SAL DataStore
107 LOG.debug("Final phase of DeviceContextLevelUp for Node: {} ", deviceContext.getDeviceState().getNodeId());
108 Preconditions.checkNotNull(deviceContext);
110 ((DeviceContextImpl) deviceContext).initialSubmitTransaction();
111 deviceContext.onPublished();
113 } catch (final Exception e) {
114 LOG.warn("Node {} can not be add to OPERATIONAL DataStore yet because {} ", deviceContext.getDeviceState().getNodeId(), e.getMessage());
115 LOG.trace("Problem with add node {} to OPERATIONAL DataStore", deviceContext.getDeviceState().getNodeId(), e);
117 deviceContext.close();
118 } catch (final Exception e1) {
119 LOG.warn("Exception on device context close. ", e);
126 public void deviceConnected(@CheckForNull final ConnectionContext connectionContext) throws Exception {
127 Preconditions.checkArgument(connectionContext != null);
128 Preconditions.checkState(!deviceContexts.containsKey(connectionContext.getNodeId()),
129 "Rejecting connection from node which is already connected and there exist deviceContext for it: {}",
130 connectionContext.getNodeId()
132 LOG.info("Initializing New Connection DeviceContext for node:{}", connectionContext.getNodeId());
134 // Cache this for clarity
135 final ConnectionAdapter connectionAdapter = connectionContext.getConnectionAdapter();
137 //FIXME: as soon as auxiliary connection are fully supported then this is needed only before device context published
138 connectionAdapter.setPacketInFiltering(true);
140 final Short version = connectionContext.getFeatures().getVersion();
141 final OutboundQueueProvider outboundQueueProvider = new OutboundQueueProviderImpl(version);
143 connectionContext.setOutboundQueueProvider(outboundQueueProvider);
144 final OutboundQueueHandlerRegistration<OutboundQueueProvider> outboundQueueHandlerRegistration =
145 connectionAdapter.registerOutboundQueueHandler(outboundQueueProvider, maxQueueDepth, barrierNanos);
146 connectionContext.setOutboundQueueHandleRegistration(outboundQueueHandlerRegistration);
148 final DeviceState deviceState = createDeviceState(connectionContext);
149 final DeviceContext deviceContext = new DeviceContextImpl(connectionContext, deviceState, dataBroker,
150 hashedWheelTimer, messageIntelligenceAgency, outboundQueueProvider, translatorLibrary, switchFeaturesMandatory);
152 Verify.verify(deviceContexts.putIfAbsent(connectionContext.getNodeId(), deviceContext) == null, "DeviceCtx still not closed.");
153 deviceContext.addDeviceContextClosedHandler(this);
155 ((ExtensionConverterProviderKeeper) deviceContext).setExtensionConverterProvider(extensionConverterProvider);
156 deviceContext.setNotificationService(notificationService);
157 deviceContext.setNotificationPublishService(notificationPublishService);
159 updatePacketInRateLimiters();
161 final OpenflowProtocolListenerFullImpl messageListener = new OpenflowProtocolListenerFullImpl(
162 connectionAdapter, deviceContext);
163 connectionAdapter.setMessageListener(messageListener);
165 deviceCtxLevelUp(deviceContext);
168 private static DeviceStateImpl createDeviceState(final @Nonnull ConnectionContext connectionContext) {
169 return new DeviceStateImpl(connectionContext.getFeatures(), connectionContext.getNodeId());
172 private void updatePacketInRateLimiters() {
173 synchronized (deviceContexts) {
174 final int deviceContextsSize = deviceContexts.size();
175 if (deviceContextsSize > 0) {
176 long freshNotificationLimit = globalNotificationQuota / deviceContextsSize;
177 if (freshNotificationLimit < 100) {
178 freshNotificationLimit = 100;
180 LOG.debug("fresh notification limit = {}", freshNotificationLimit);
181 for (final DeviceContext deviceContext : deviceContexts.values()) {
182 deviceContext.updatePacketInRateLimit(freshNotificationLimit);
188 private void deviceCtxLevelUp(final DeviceContext deviceContext) throws Exception {
189 deviceContext.getDeviceState().setValid(true);
190 LOG.trace("Device context level up called.");
191 deviceInitPhaseHandler.onDeviceContextLevelUp(deviceContext);
195 public TranslatorLibrary oook() {
196 return translatorLibrary;
200 public void setTranslatorLibrary(final TranslatorLibrary translatorLibrary) {
201 this.translatorLibrary = translatorLibrary;
205 public void setNotificationService(final NotificationService notificationServiceParam) {
206 notificationService = notificationServiceParam;
210 public void setNotificationPublishService(final NotificationPublishService notificationService) {
211 notificationPublishService = notificationService;
215 public void close() throws Exception {
216 for (final DeviceContext deviceContext : deviceContexts.values()) {
217 deviceContext.close();
222 public void onDeviceContextClosed(final DeviceContext deviceContext) {
223 LOG.trace("onDeviceContextClosed for Node {}", deviceContext.getDeviceState().getNodeId());
224 deviceContexts.remove(deviceContext.getPrimaryConnectionContext().getNodeId());
225 updatePacketInRateLimiters();
229 public void initialize() {
230 spyPool = new ScheduledThreadPoolExecutor(1);
231 spyPool.scheduleAtFixedRate(messageIntelligenceAgency, spyRate, spyRate, TimeUnit.SECONDS);
235 public void setExtensionConverterProvider(final ExtensionConverterProvider extensionConverterProvider) {
236 this.extensionConverterProvider = extensionConverterProvider;
240 public ExtensionConverterProvider getExtensionConverterProvider() {
241 return extensionConverterProvider;