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.collect.Sets;
12 import com.google.common.util.concurrent.AsyncFunction;
13 import com.google.common.util.concurrent.FutureCallback;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import io.netty.util.HashedWheelTimer;
17 import java.math.BigInteger;
18 import java.net.Inet4Address;
19 import java.net.Inet6Address;
20 import java.net.InetAddress;
21 import java.net.InetSocketAddress;
22 import java.util.Arrays;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.Iterator;
26 import java.util.List;
28 import java.util.concurrent.ExecutionException;
29 import java.util.concurrent.ScheduledThreadPoolExecutor;
30 import java.util.concurrent.TimeUnit;
31 import javax.annotation.CheckForNull;
32 import javax.annotation.Nonnull;
33 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
34 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
35 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
36 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
37 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
38 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
39 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
40 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueueHandlerRegistration;
41 import org.opendaylight.openflowplugin.api.ConnectionException;
42 import org.opendaylight.openflowplugin.api.OFConstants;
43 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
44 import org.opendaylight.openflowplugin.api.openflow.connection.OutboundQueueProvider;
45 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
46 import org.opendaylight.openflowplugin.api.openflow.device.DeviceManager;
47 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
48 import org.opendaylight.openflowplugin.api.openflow.device.MessageTranslator;
49 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
50 import org.opendaylight.openflowplugin.api.openflow.device.TranslatorLibrary;
51 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
52 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceInitializationPhaseHandler;
53 import org.opendaylight.openflowplugin.api.openflow.device.handlers.MultiMsgCollector;
54 import org.opendaylight.openflowplugin.api.openflow.md.core.TranslatorKey;
55 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageIntelligenceAgency;
56 import org.opendaylight.openflowplugin.impl.common.MultipartRequestInputFactory;
57 import org.opendaylight.openflowplugin.impl.common.NodeStaticReplyTranslatorUtil;
58 import org.opendaylight.openflowplugin.impl.connection.OutboundQueueProviderImpl;
59 import org.opendaylight.openflowplugin.impl.device.listener.OpenflowProtocolListenerFullImpl;
60 import org.opendaylight.openflowplugin.impl.rpc.AbstractRequestContext;
61 import org.opendaylight.openflowplugin.impl.util.DeviceStateUtil;
62 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
63 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
64 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsData;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsDataBuilder;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupFeatures;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodesBuilder;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorBuilder;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterFeatures;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.Capabilities;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.CapabilitiesV10;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortGrouping;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.MultipartReplyBody;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyDescCase;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyGroupFeaturesCase;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyMeterFeaturesCase;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyPortDescCase;
94 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyTableFeaturesCase;
95 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.desc._case.MultipartReplyDesc;
96 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.group.features._case.MultipartReplyGroupFeatures;
97 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.meter.features._case.MultipartReplyMeterFeatures;
98 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.port.desc._case.MultipartReplyPortDesc;
99 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.table.features._case.MultipartReplyTableFeatures;
100 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.FlowCapableNodeConnectorStatisticsData;
101 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.FlowCapableNodeConnectorStatisticsDataBuilder;
102 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures;
103 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
104 import org.opendaylight.yangtools.yang.common.RpcError;
105 import org.opendaylight.yangtools.yang.common.RpcResult;
106 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
107 import org.slf4j.Logger;
108 import org.slf4j.LoggerFactory;
113 public class DeviceManagerImpl implements DeviceManager, AutoCloseable {
115 private static final Logger LOG = LoggerFactory.getLogger(DeviceManagerImpl.class);
117 private static final long TICK_DURATION = 10; // 0.5 sec.
118 private final long globalNotificationQuota;
119 private ScheduledThreadPoolExecutor spyPool;
120 private final int spyRate = 10;
122 private final DataBroker dataBroker;
123 private final HashedWheelTimer hashedWheelTimer;
124 private TranslatorLibrary translatorLibrary;
125 private DeviceInitializationPhaseHandler deviceInitPhaseHandler;
126 private NotificationService notificationService;
127 private NotificationPublishService notificationPublishService;
129 private final Set<DeviceContext> deviceContexts = Sets.newConcurrentHashSet();
130 private final MessageIntelligenceAgency messageIntelligenceAgency;
132 private final long barrierNanos = TimeUnit.MILLISECONDS.toNanos(500);
133 private final int maxQueueDepth = 25600;
134 private final boolean switchFeaturesMandatory;
135 private final DeviceTransactionChainManagerProvider deviceTransactionChainManagerProvider;
137 public DeviceManagerImpl(@Nonnull final DataBroker dataBroker,
138 @Nonnull final MessageIntelligenceAgency messageIntelligenceAgency,
139 final boolean switchFeaturesMandatory,
140 final long globalNotificationQuota) {
141 this.globalNotificationQuota = globalNotificationQuota;
142 this.dataBroker = Preconditions.checkNotNull(dataBroker);
143 hashedWheelTimer = new HashedWheelTimer(TICK_DURATION, TimeUnit.MILLISECONDS, 500);
144 /* merge empty nodes to oper DS to predict any problems with missing parent for Node */
145 final WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
147 final NodesBuilder nodesBuilder = new NodesBuilder();
148 nodesBuilder.setNode(Collections.<Node>emptyList());
149 tx.merge(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(Nodes.class), nodesBuilder.build());
152 } catch (ExecutionException | InterruptedException e) {
153 LOG.error("Creation of node failed.", e);
154 throw new IllegalStateException(e);
157 this.messageIntelligenceAgency = messageIntelligenceAgency;
158 this.switchFeaturesMandatory = switchFeaturesMandatory;
159 deviceTransactionChainManagerProvider = new DeviceTransactionChainManagerProvider(dataBroker);
164 public void setDeviceInitializationPhaseHandler(final DeviceInitializationPhaseHandler handler) {
165 deviceInitPhaseHandler = handler;
169 public void onDeviceContextLevelUp(final DeviceContext deviceContext) {
170 // final phase - we have to add new Device to MD-SAL DataStore
171 Preconditions.checkNotNull(deviceContext);
173 ((DeviceContextImpl) deviceContext).initialSubmitTransaction();
174 deviceContext.onPublished();
175 } catch (final Exception e) {
176 LOG.warn("Node {} can not be add to OPERATIONAL DataStore yet because {} ", deviceContext.getDeviceState().getNodeId(), e.getMessage());
177 LOG.trace("Problem with add node {} to OPERATIONAL DataStore", deviceContext.getDeviceState().getNodeId(), e);
179 deviceContext.close();
180 } catch (final Exception e1) {
181 LOG.warn("Device context close FAIL - " + deviceContext.getDeviceState().getNodeId());
187 public void deviceConnected(@CheckForNull final ConnectionContext connectionContext) {
188 Preconditions.checkArgument(connectionContext != null);
190 ReadyForNewTransactionChainHandler readyForNewTransactionChainHandler = new ReadyForNewTransactionChainHandlerImpl(this, connectionContext);
191 DeviceTransactionChainManagerProvider.TransactionChainManagerRegistration transactionChainManagerRegistration = deviceTransactionChainManagerProvider.provideTransactionChainManager(connectionContext);
192 TransactionChainManager transactionChainManager = transactionChainManagerRegistration.getTransactionChainManager();
194 //this actually is new registration for currently processed connection context
195 if (transactionChainManagerRegistration.ownedByInvokingConnectionContext()) {
196 initializeDeviceContext(connectionContext, transactionChainManager);
198 //this means there already exists connection described by same NodeId and it is not current connection contexts' registration
199 else if (TransactionChainManager.TransactionChainManagerStatus.WORKING.equals(transactionChainManager.getTransactionChainManagerStatus())) {
200 connectionContext.closeConnection(false);
202 //previous connection is shutting down, we will try to register handler listening on new transaction chain ready
203 else if (!transactionChainManager.attemptToRegisterHandler(readyForNewTransactionChainHandler)) {
204 // new connection wil be closed if handler registration fails
205 connectionContext.closeConnection(false);
209 private void initializeDeviceContext(final ConnectionContext connectionContext, final TransactionChainManager transactionChainManager) {
211 // Cache this for clarity
212 final ConnectionAdapter connectionAdapter = connectionContext.getConnectionAdapter();
214 //FIXME: as soon as auxiliary connection are fully supported then this is needed only before device context published
215 connectionAdapter.setPacketInFiltering(true);
217 final Short version = connectionContext.getFeatures().getVersion();
218 final OutboundQueueProvider outboundQueueProvider = new OutboundQueueProviderImpl(version);
220 connectionContext.setOutboundQueueProvider(outboundQueueProvider);
221 final OutboundQueueHandlerRegistration<OutboundQueueProvider> outboundQueueHandlerRegistration =
222 connectionAdapter.registerOutboundQueueHandler(outboundQueueProvider, maxQueueDepth, barrierNanos);
223 connectionContext.setOutboundQueueHandleRegistration(outboundQueueHandlerRegistration);
225 final NodeId nodeId = connectionContext.getNodeId();
226 final DeviceState deviceState = new DeviceStateImpl(connectionContext.getFeatures(), nodeId);
228 final DeviceContext deviceContext = new DeviceContextImpl(connectionContext, deviceState, dataBroker,
229 hashedWheelTimer, messageIntelligenceAgency, outboundQueueProvider, translatorLibrary, transactionChainManager);
230 deviceContext.setNotificationService(notificationService);
231 deviceContext.setNotificationPublishService(notificationPublishService);
232 final NodeBuilder nodeBuilder = new NodeBuilder().setId(deviceState.getNodeId()).setNodeConnector(Collections.<NodeConnector>emptyList());
234 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, deviceState.getNodeInstanceIdentifier(), nodeBuilder.build());
235 } catch (final Exception e) {
236 LOG.debug("Failed to write node to DS ", e);
239 connectionContext.setDeviceDisconnectedHandler(deviceContext);
240 deviceContext.addDeviceContextClosedHandler(this);
241 deviceContexts.add(deviceContext);
243 updatePacketInRateLimiters();
245 final OpenflowProtocolListenerFullImpl messageListener = new OpenflowProtocolListenerFullImpl(
246 connectionAdapter, deviceContext);
247 connectionAdapter.setMessageListener(messageListener);
249 final ListenableFuture<List<RpcResult<List<MultipartReply>>>> deviceFeaturesFuture;
250 if (OFConstants.OFP_VERSION_1_0 == version) {
251 final CapabilitiesV10 capabilitiesV10 = connectionContext.getFeatures().getCapabilitiesV10();
253 DeviceStateUtil.setDeviceStateBasedOnV10Capabilities(deviceState, capabilitiesV10);
255 deviceFeaturesFuture = createDeviceFeaturesForOF10(deviceContext, deviceState);
256 // create empty tables after device description is processed
257 chainTableTrunkWriteOF10(deviceContext, deviceFeaturesFuture);
259 final short ofVersion = deviceContext.getDeviceState().getVersion();
260 final TranslatorKey translatorKey = new TranslatorKey(ofVersion, PortGrouping.class.getName());
261 final MessageTranslator<PortGrouping, FlowCapableNodeConnector> translator = deviceContext.oook().lookupTranslator(translatorKey);
262 final BigInteger dataPathId = deviceContext.getPrimaryConnectionContext().getFeatures().getDatapathId();
264 for (final PortGrouping port : connectionContext.getFeatures().getPhyPort()) {
265 final FlowCapableNodeConnector fcNodeConnector = translator.translate(port, deviceContext, null);
267 final NodeConnectorId nodeConnectorId = NodeStaticReplyTranslatorUtil.nodeConnectorId(dataPathId.toString(), port.getPortNo(), ofVersion);
268 final NodeConnectorBuilder ncBuilder = new NodeConnectorBuilder().setId(nodeConnectorId);
269 ncBuilder.addAugmentation(FlowCapableNodeConnector.class, fcNodeConnector);
270 ncBuilder.addAugmentation(FlowCapableNodeConnectorStatisticsData.class, new FlowCapableNodeConnectorStatisticsDataBuilder().build());
271 final NodeConnector connector = ncBuilder.build();
272 final InstanceIdentifier<NodeConnector> connectorII = deviceState.getNodeInstanceIdentifier().child(NodeConnector.class, connector.getKey());
274 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, connectorII, connector);
275 } catch (final Exception e) {
276 LOG.debug("Failed to write node {} to DS ", deviceContext.getDeviceState().getNodeId().toString(), e);
280 } else if (OFConstants.OFP_VERSION_1_3 == version) {
281 final Capabilities capabilities = connectionContext.getFeatures().getCapabilities();
282 LOG.debug("Setting capabilities for device {}", deviceContext.getDeviceState().getNodeId());
283 DeviceStateUtil.setDeviceStateBasedOnV13Capabilities(deviceState, capabilities);
284 deviceFeaturesFuture = createDeviceFeaturesForOF13(deviceContext, deviceState);
286 deviceFeaturesFuture = Futures.immediateFailedFuture(new ConnectionException("Unsupported version " + version));
289 Futures.addCallback(deviceFeaturesFuture, new FutureCallback<List<RpcResult<List<MultipartReply>>>>() {
291 public void onSuccess(final List<RpcResult<List<MultipartReply>>> result) {
292 deviceCtxLevelUp(deviceContext);
296 public void onFailure(final Throwable t) {
297 // FIXME : remove session
298 LOG.trace("Device capabilities gathering future failed.");
299 LOG.trace("more info in exploration failure..", t);
304 private void updatePacketInRateLimiters() {
305 synchronized (deviceContexts) {
306 final int deviceContextsSize = deviceContexts.size();
307 if (deviceContextsSize > 0) {
308 long freshNotificationLimit = globalNotificationQuota / deviceContextsSize;
309 if (freshNotificationLimit < 100) {
310 freshNotificationLimit = 100;
312 LOG.debug("fresh notification limit = {}", freshNotificationLimit);
313 for (DeviceContext deviceContext : deviceContexts) {
314 deviceContext.updatePacketInRateLimit(freshNotificationLimit);
320 void deviceCtxLevelUp(final DeviceContext deviceContext) {
321 deviceContext.getDeviceState().setValid(true);
322 deviceInitPhaseHandler.onDeviceContextLevelUp(deviceContext);
323 LOG.trace("Device context level up called.");
326 static void chainTableTrunkWriteOF10(final DeviceContext deviceContext, final ListenableFuture<List<RpcResult<List<MultipartReply>>>> deviceFeaturesFuture) {
327 Futures.addCallback(deviceFeaturesFuture, new FutureCallback<List<RpcResult<List<MultipartReply>>>>() {
329 public void onSuccess(final List<RpcResult<List<MultipartReply>>> results) {
330 boolean allSucceeded = true;
331 for (final RpcResult<List<MultipartReply>> rpcResult : results) {
332 allSucceeded &= rpcResult.isSuccessful();
335 createEmptyFlowCapableNodeInDs(deviceContext);
336 makeEmptyTables(deviceContext, deviceContext.getDeviceState().getNodeInstanceIdentifier(),
337 deviceContext.getDeviceState().getFeatures().getTables());
342 public void onFailure(final Throwable t) {
349 static ListenableFuture<List<RpcResult<List<MultipartReply>>>> createDeviceFeaturesForOF10(final DeviceContext deviceContext,
350 final DeviceState deviceState) {
351 final ListenableFuture<RpcResult<List<MultipartReply>>> replyDesc = getNodeStaticInfo(MultipartType.OFPMPDESC,
353 deviceState.getNodeInstanceIdentifier(),
354 deviceState.getVersion());
356 return Futures.allAsList(Arrays.asList(replyDesc));
359 ListenableFuture<List<RpcResult<List<MultipartReply>>>> createDeviceFeaturesForOF13(final DeviceContext deviceContext,
360 final DeviceState deviceState) {
362 final ListenableFuture<RpcResult<List<MultipartReply>>> replyDesc = getNodeStaticInfo(MultipartType.OFPMPDESC,
364 deviceState.getNodeInstanceIdentifier(),
365 deviceState.getVersion());
367 //first process description reply, write data to DS and write consequent data if successful
368 return Futures.transform(replyDesc, new AsyncFunction<RpcResult<List<MultipartReply>>, List<RpcResult<List<MultipartReply>>>>() {
370 public ListenableFuture<List<RpcResult<List<MultipartReply>>>> apply(final RpcResult<List<MultipartReply>> rpcResult) throws Exception {
372 translateAndWriteReply(MultipartType.OFPMPDESC, deviceContext, deviceState.getNodeInstanceIdentifier(), rpcResult.getResult());
374 final ListenableFuture<RpcResult<List<MultipartReply>>> replyMeterFeature = getNodeStaticInfo(MultipartType.OFPMPMETERFEATURES,
376 deviceState.getNodeInstanceIdentifier(),
377 deviceState.getVersion());
379 createSuccessProcessingCallback(MultipartType.OFPMPMETERFEATURES,
381 deviceState.getNodeInstanceIdentifier(),
384 final ListenableFuture<RpcResult<List<MultipartReply>>> replyGroupFeatures = getNodeStaticInfo(MultipartType.OFPMPGROUPFEATURES,
386 deviceState.getNodeInstanceIdentifier(),
387 deviceState.getVersion());
388 createSuccessProcessingCallback(MultipartType.OFPMPGROUPFEATURES,
390 deviceState.getNodeInstanceIdentifier(),
393 final ListenableFuture<RpcResult<List<MultipartReply>>> replyTableFeatures = getNodeStaticInfo(MultipartType.OFPMPTABLEFEATURES,
395 deviceState.getNodeInstanceIdentifier(),
396 deviceState.getVersion());
397 createSuccessProcessingCallback(MultipartType.OFPMPTABLEFEATURES,
399 deviceState.getNodeInstanceIdentifier(),
402 final ListenableFuture<RpcResult<List<MultipartReply>>> replyPortDescription = getNodeStaticInfo(MultipartType.OFPMPPORTDESC,
404 deviceState.getNodeInstanceIdentifier(),
405 deviceState.getVersion());
406 createSuccessProcessingCallback(MultipartType.OFPMPPORTDESC,
408 deviceState.getNodeInstanceIdentifier(),
409 replyPortDescription);
410 if (switchFeaturesMandatory) {
411 return Futures.allAsList(Arrays.asList(
415 replyPortDescription));
417 return Futures.successfulAsList(Arrays.asList(
421 replyPortDescription));
429 public TranslatorLibrary oook() {
430 return translatorLibrary;
434 public void setTranslatorLibrary(final TranslatorLibrary translatorLibrary) {
435 this.translatorLibrary = translatorLibrary;
438 static ListenableFuture<RpcResult<List<MultipartReply>>> getNodeStaticInfo(final MultipartType type, final DeviceContext deviceContext,
439 final InstanceIdentifier<Node> nodeII, final short version) {
441 final OutboundQueue queue = deviceContext.getPrimaryConnectionContext().getOutboundQueueProvider();
443 final Long reserved = deviceContext.getReservedXid();
444 final RequestContext<List<MultipartReply>> requestContext = new AbstractRequestContext<List<MultipartReply>>(reserved) {
446 public void close() {
451 final Xid xid = requestContext.getXid();
453 LOG.trace("Hooking xid {} to device context - precaution.", reserved);
455 final MultiMsgCollector multiMsgCollector = deviceContext.getMultiMsgCollector(requestContext);
456 queue.commitEntry(xid.getValue(), MultipartRequestInputFactory.makeMultipartRequestInput(xid.getValue(), version, type), new FutureCallback<OfHeader>() {
458 public void onSuccess(final OfHeader ofHeader) {
459 if (ofHeader instanceof MultipartReply) {
460 final MultipartReply multipartReply = (MultipartReply) ofHeader;
461 multiMsgCollector.addMultipartMsg(multipartReply);
462 } else if (null != ofHeader) {
463 LOG.info("Unexpected response type received {}.", ofHeader.getClass());
465 multiMsgCollector.endCollecting();
466 LOG.info("Response received is null.");
471 public void onFailure(final Throwable t) {
472 LOG.info("Fail response from OutboundQueue for multipart type {}.", type);
473 final RpcResult<List<MultipartReply>> rpcResult = RpcResultBuilder.<List<MultipartReply>>failed().build();
474 requestContext.setResult(rpcResult);
475 if (MultipartType.OFPMPTABLEFEATURES.equals(type)) {
476 makeEmptyTables(deviceContext, nodeII, deviceContext.getPrimaryConnectionContext().getFeatures().getTables());
478 requestContext.close();
482 return requestContext.getFuture();
485 static void createSuccessProcessingCallback(final MultipartType type, final DeviceContext deviceContext, final InstanceIdentifier<Node> nodeII, final ListenableFuture<RpcResult<List<MultipartReply>>> requestContextFuture) {
486 Futures.addCallback(requestContextFuture, new FutureCallback<RpcResult<List<MultipartReply>>>() {
488 public void onSuccess(final RpcResult<List<MultipartReply>> rpcResult) {
489 final List<MultipartReply> result = rpcResult.getResult();
490 if (result != null) {
491 LOG.info("Static node {} info: {} collected", deviceContext.getDeviceState().getNodeId(), type);
492 translateAndWriteReply(type, deviceContext, nodeII, result);
494 final Iterator<RpcError> rpcErrorIterator = rpcResult.getErrors().iterator();
495 while (rpcErrorIterator.hasNext()) {
496 final RpcError rpcError = rpcErrorIterator.next();
497 LOG.info("Failed to retrieve static node {} info: {}", type, rpcError.getMessage());
498 if (null != rpcError.getCause()) {
499 LOG.trace("Detailed error:", rpcError.getCause());
502 if (MultipartType.OFPMPTABLEFEATURES.equals(type)) {
503 makeEmptyTables(deviceContext, nodeII, deviceContext.getPrimaryConnectionContext().getFeatures().getTables());
509 public void onFailure(final Throwable throwable) {
510 LOG.info("Request of type {} for static info of node {} failed.", type, nodeII);
515 // FIXME : remove after ovs tableFeatures fix
516 static void makeEmptyTables(final DeviceContext dContext, final InstanceIdentifier<Node> nodeII, final Short nrOfTables) {
517 LOG.debug("About to create {} empty tables.", nrOfTables);
518 for (int i = 0; i < nrOfTables; i++) {
519 final short tId = (short) i;
520 final InstanceIdentifier<Table> tableII = nodeII.augmentation(FlowCapableNode.class).child(Table.class, new TableKey(tId));
521 final TableBuilder tableBuilder = new TableBuilder().setId(tId).addAugmentation(FlowTableStatisticsData.class, new FlowTableStatisticsDataBuilder().build());
524 dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, tableII, tableBuilder.build());
525 } catch (final Exception e) {
526 LOG.debug("Failed to write node {} to DS ", dContext.getDeviceState().getNodeId().toString(), e);
532 private static IpAddress getIpAddressOf(final DeviceContext deviceContext) {
534 InetSocketAddress remoteAddress = deviceContext.getPrimaryConnectionContext().getConnectionAdapter().getRemoteAddress();
536 if (remoteAddress == null) {
537 LOG.warn("IP address of the node {} cannot be obtained. No connection with switch.", deviceContext.getDeviceState().getNodeId());
540 LOG.info("IP address of switch is :"+remoteAddress);
542 final InetAddress address = remoteAddress.getAddress();
543 String hostAddress = address.getHostAddress();
544 if (address instanceof Inet4Address) {
545 return new IpAddress(new Ipv4Address(hostAddress));
547 if (address instanceof Inet6Address) {
548 return new IpAddress(new Ipv6Address(hostAddress));
550 LOG.info("Illegal IP address {} of switch:{} ", address, deviceContext.getDeviceState().getNodeId());
555 static void translateAndWriteReply(final MultipartType type, final DeviceContext dContext,
556 final InstanceIdentifier<Node> nodeII, final Collection<MultipartReply> result) {
558 for (final MultipartReply reply : result) {
559 final MultipartReplyBody body = reply.getMultipartReplyBody();
562 Preconditions.checkArgument(body instanceof MultipartReplyDescCase);
563 final MultipartReplyDesc replyDesc = ((MultipartReplyDescCase) body).getMultipartReplyDesc();
564 final FlowCapableNode fcNode = NodeStaticReplyTranslatorUtil.nodeDescTranslator(replyDesc, getIpAddressOf(dContext));
565 final InstanceIdentifier<FlowCapableNode> fNodeII = nodeII.augmentation(FlowCapableNode.class);
566 dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, fNodeII, fcNode);
569 case OFPMPTABLEFEATURES:
570 Preconditions.checkArgument(body instanceof MultipartReplyTableFeaturesCase);
571 final MultipartReplyTableFeatures tableFeatures = ((MultipartReplyTableFeaturesCase) body).getMultipartReplyTableFeatures();
572 final List<TableFeatures> tables = NodeStaticReplyTranslatorUtil.nodeTableFeatureTranslator(tableFeatures);
573 for (final TableFeatures table : tables) {
574 final Short tableId = table.getTableId();
575 final InstanceIdentifier<Table> tableII = nodeII.augmentation(FlowCapableNode.class).child(Table.class, new TableKey(tableId));
576 final TableBuilder tableBuilder = new TableBuilder().setId(tableId).setTableFeatures(Collections.singletonList(table));
577 tableBuilder.addAugmentation(FlowTableStatisticsData.class, new FlowTableStatisticsDataBuilder().build());
578 dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, tableII, tableBuilder.build());
582 case OFPMPMETERFEATURES:
583 Preconditions.checkArgument(body instanceof MultipartReplyMeterFeaturesCase);
584 final MultipartReplyMeterFeatures meterFeatures = ((MultipartReplyMeterFeaturesCase) body).getMultipartReplyMeterFeatures();
585 final NodeMeterFeatures mFeature = NodeStaticReplyTranslatorUtil.nodeMeterFeatureTranslator(meterFeatures);
586 final InstanceIdentifier<NodeMeterFeatures> mFeatureII = nodeII.augmentation(NodeMeterFeatures.class);
587 dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, mFeatureII, mFeature);
588 if (0L < mFeature.getMeterFeatures().getMaxMeter().getValue()) {
589 dContext.getDeviceState().setMeterAvailable(true);
593 case OFPMPGROUPFEATURES:
594 Preconditions.checkArgument(body instanceof MultipartReplyGroupFeaturesCase);
595 final MultipartReplyGroupFeatures groupFeatures = ((MultipartReplyGroupFeaturesCase) body).getMultipartReplyGroupFeatures();
596 final NodeGroupFeatures gFeature = NodeStaticReplyTranslatorUtil.nodeGroupFeatureTranslator(groupFeatures);
597 final InstanceIdentifier<NodeGroupFeatures> gFeatureII = nodeII.augmentation(NodeGroupFeatures.class);
598 dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, gFeatureII, gFeature);
602 Preconditions.checkArgument(body instanceof MultipartReplyPortDescCase);
603 final MultipartReplyPortDesc portDesc = ((MultipartReplyPortDescCase) body).getMultipartReplyPortDesc();
604 for (final PortGrouping port : portDesc.getPorts()) {
605 final short ofVersion = dContext.getDeviceState().getVersion();
606 final TranslatorKey translatorKey = new TranslatorKey(ofVersion, PortGrouping.class.getName());
607 final MessageTranslator<PortGrouping, FlowCapableNodeConnector> translator = dContext.oook().lookupTranslator(translatorKey);
608 final FlowCapableNodeConnector fcNodeConnector = translator.translate(port, dContext, null);
610 final BigInteger dataPathId = dContext.getPrimaryConnectionContext().getFeatures().getDatapathId();
611 final NodeConnectorId nodeConnectorId = NodeStaticReplyTranslatorUtil.nodeConnectorId(dataPathId.toString(), port.getPortNo(), ofVersion);
612 final NodeConnectorBuilder ncBuilder = new NodeConnectorBuilder().setId(nodeConnectorId);
613 ncBuilder.addAugmentation(FlowCapableNodeConnector.class, fcNodeConnector);
615 ncBuilder.addAugmentation(FlowCapableNodeConnectorStatisticsData.class, new FlowCapableNodeConnectorStatisticsDataBuilder().build());
616 final NodeConnector connector = ncBuilder.build();
618 final InstanceIdentifier<NodeConnector> connectorII = nodeII.child(NodeConnector.class, connector.getKey());
619 dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, connectorII, connector);
625 throw new IllegalArgumentException("Unnexpected MultipartType " + type);
628 } catch (final Exception e) {
629 LOG.debug("Failed to write node {} to DS ", dContext.getDeviceState().getNodeId().toString(), e);
634 public void setNotificationService(final NotificationService notificationServiceParam) {
635 notificationService = notificationServiceParam;
639 public void setNotificationPublishService(final NotificationPublishService notificationService) {
640 notificationPublishService = notificationService;
644 public void close() throws Exception {
645 for (final DeviceContext deviceContext : deviceContexts) {
646 deviceContext.close();
650 static void createEmptyFlowCapableNodeInDs(final DeviceContext deviceContext) {
651 final FlowCapableNodeBuilder flowCapableNodeBuilder = new FlowCapableNodeBuilder();
652 final InstanceIdentifier<FlowCapableNode> fNodeII = deviceContext.getDeviceState().getNodeInstanceIdentifier().augmentation(FlowCapableNode.class);
654 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, fNodeII, flowCapableNodeBuilder.build());
655 } catch (final Exception e) {
656 LOG.debug("Failed to write node {} to DS ", deviceContext.getDeviceState().getNodeId().toString(), e);
661 public void onDeviceContextClosed(final DeviceContext deviceContext) {
662 deviceContexts.remove(deviceContext);
663 updatePacketInRateLimiters();
667 public void initialize() {
668 spyPool = new ScheduledThreadPoolExecutor(1);
669 spyPool.scheduleAtFixedRate(messageIntelligenceAgency, spyRate, spyRate, TimeUnit.SECONDS);