2 * Copyright (c) 2016 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
9 package org.opendaylight.openflowplugin.impl.util;
11 import com.google.common.base.Preconditions;
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 com.google.common.util.concurrent.SettableFuture;
17 import java.math.BigInteger;
18 import java.net.InetSocketAddress;
19 import java.util.Arrays;
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.Iterator;
23 import java.util.List;
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
25 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
26 import org.opendaylight.openflowplugin.api.ConnectionException;
27 import org.opendaylight.openflowplugin.api.OFConstants;
28 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
29 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
30 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
31 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
32 import org.opendaylight.openflowplugin.api.openflow.device.MessageTranslator;
33 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
34 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
35 import org.opendaylight.openflowplugin.api.openflow.device.handlers.MultiMsgCollector;
36 import org.opendaylight.openflowplugin.api.openflow.md.core.TranslatorKey;
37 import org.opendaylight.openflowplugin.impl.common.MultipartRequestInputFactory;
38 import org.opendaylight.openflowplugin.impl.common.NodeStaticReplyTranslatorUtil;
39 import org.opendaylight.openflowplugin.impl.rpc.AbstractRequestContext;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IetfInetUtil;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsData;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsDataBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupFeatures;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterFeatures;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.Capabilities;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.CapabilitiesV10;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortGrouping;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.MultipartReplyBody;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyDescCase;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyGroupFeaturesCase;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyMeterFeaturesCase;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyPortDescCase;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyTableFeaturesCase;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.desc._case.MultipartReplyDesc;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.group.features._case.MultipartReplyGroupFeatures;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.meter.features._case.MultipartReplyMeterFeatures;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.port.desc._case.MultipartReplyPortDesc;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.table.features._case.MultipartReplyTableFeatures;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.FlowCapableNodeConnectorStatisticsData;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.FlowCapableNodeConnectorStatisticsDataBuilder;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeaturesKey;
78 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
79 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
80 import org.opendaylight.yangtools.yang.common.RpcError;
81 import org.opendaylight.yangtools.yang.common.RpcResult;
82 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
83 import org.slf4j.Logger;
84 import org.slf4j.LoggerFactory;
86 public class DeviceInitializationUtils {
88 private static final Logger LOG = LoggerFactory.getLogger(DeviceInitializationUtils.class);
91 * InitializationNodeInformation is good to call only for MASTER otherwise we will have not empty transaction
92 * for every Cluster Node (SLAVE too) and we will get race-condition by closing Connection.
94 * @param deviceContext
95 * @param switchFeaturesMandatory
96 * @return future - recommended to have blocking call for this future
98 public static ListenableFuture<Void> initializeNodeInformation(final DeviceContext deviceContext, final boolean switchFeaturesMandatory) {
99 Preconditions.checkArgument(deviceContext != null);
100 final DeviceState deviceState = Preconditions.checkNotNull(deviceContext.getDeviceState());
101 final DeviceInfo deviceInfo = deviceContext.getDeviceInfo();
102 final ConnectionContext connectionContext = Preconditions.checkNotNull(deviceContext.getPrimaryConnectionContext());
103 final short version = deviceInfo.getVersion();
104 LOG.trace("initalizeNodeInformation for node {}", deviceInfo.getNodeId());
105 final SettableFuture<Void> returnFuture = SettableFuture.<Void>create();
106 addNodeToOperDS(deviceContext, returnFuture);
107 final ListenableFuture<List<RpcResult<List<MultipartReply>>>> deviceFeaturesFuture;
108 if (OFConstants.OFP_VERSION_1_0 == version) {
109 final CapabilitiesV10 capabilitiesV10 = connectionContext.getFeatures().getCapabilitiesV10();
111 DeviceStateUtil.setDeviceStateBasedOnV10Capabilities(deviceState, capabilitiesV10);
113 deviceFeaturesFuture = createDeviceFeaturesForOF10(deviceContext, deviceState);
114 // create empty tables after device description is processed
115 chainTableTrunkWriteOF10(deviceContext, deviceFeaturesFuture);
117 final short ofVersion = deviceInfo.getVersion();
118 final TranslatorKey translatorKey = new TranslatorKey(ofVersion, PortGrouping.class.getName());
119 final MessageTranslator<PortGrouping, FlowCapableNodeConnector> translator = deviceContext.oook()
120 .lookupTranslator(translatorKey);
121 final BigInteger dataPathId = deviceContext.getPrimaryConnectionContext().getFeatures().getDatapathId();
123 for (final PortGrouping port : connectionContext.getFeatures().getPhyPort()) {
124 final FlowCapableNodeConnector fcNodeConnector = translator.translate(port, deviceContext.getDeviceInfo(), null);
126 final NodeConnectorId nodeConnectorId = NodeStaticReplyTranslatorUtil.nodeConnectorId(
127 dataPathId.toString(), port.getPortNo(), ofVersion);
128 final NodeConnectorBuilder ncBuilder = new NodeConnectorBuilder().setId(nodeConnectorId);
129 ncBuilder.addAugmentation(FlowCapableNodeConnector.class, fcNodeConnector);
130 ncBuilder.addAugmentation(FlowCapableNodeConnectorStatisticsData.class,
131 new FlowCapableNodeConnectorStatisticsDataBuilder().build());
132 final NodeConnector connector = ncBuilder.build();
133 final InstanceIdentifier<NodeConnector> connectorII = deviceInfo.getNodeInstanceIdentifier().child(
134 NodeConnector.class, connector.getKey());
136 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, connectorII, connector);
137 } catch (final Exception e) {
138 LOG.debug("Failed to write node {} to DS ", deviceInfo.getNodeId().toString(),
143 } else if (OFConstants.OFP_VERSION_1_3 == version) {
144 final Capabilities capabilities = connectionContext.getFeatures().getCapabilities();
145 LOG.debug("Setting capabilities for device {}", deviceInfo.getNodeId());
146 DeviceStateUtil.setDeviceStateBasedOnV13Capabilities(deviceState, capabilities);
147 deviceFeaturesFuture = createDeviceFeaturesForOF13(deviceContext, deviceState, switchFeaturesMandatory);
149 deviceFeaturesFuture = Futures.immediateFailedFuture(new ConnectionException("Unsupported version "
153 Futures.addCallback(deviceFeaturesFuture, new FutureCallback<List<RpcResult<List<MultipartReply>>>>() {
155 public void onSuccess(final List<RpcResult<List<MultipartReply>>> result) {
156 LOG.debug("All init data for node {} is in submited.", deviceInfo.getNodeId());
157 returnFuture.set(null);
161 public void onFailure(final Throwable t) {
162 // FIXME : remove session
163 LOG.trace("Device capabilities gathering future failed.");
164 LOG.trace("more info in exploration failure..", t);
165 LOG.debug("All init data for node {} was not submited correctly - connection has to go down.", deviceInfo.getNodeId());
166 returnFuture.setException(t);
172 private static void addNodeToOperDS(final DeviceContext deviceContext, final SettableFuture<Void> future) {
173 Preconditions.checkArgument(deviceContext != null);
174 final DeviceState deviceState = deviceContext.getDeviceState();
175 final NodeBuilder nodeBuilder = new NodeBuilder().setId(deviceContext.getDeviceInfo().getNodeId()).setNodeConnector(
176 Collections.<NodeConnector>emptyList());
178 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, deviceContext.getDeviceInfo().getNodeInstanceIdentifier(),
179 nodeBuilder.build());
180 } catch (final Exception e) {
181 LOG.warn("Failed to write node {} to DS ", deviceContext.getDeviceInfo().getNodeId(), e);
186 private static ListenableFuture<List<RpcResult<List<MultipartReply>>>> createDeviceFeaturesForOF10(
187 final DeviceContext deviceContext, final DeviceState deviceState) {
188 final ListenableFuture<RpcResult<List<MultipartReply>>> replyDesc = getNodeStaticInfo(MultipartType.OFPMPDESC,
189 deviceContext, deviceContext.getDeviceInfo().getNodeInstanceIdentifier(), deviceContext.getDeviceInfo().getVersion());
191 return Futures.allAsList(Arrays.asList(replyDesc));
194 private static ListenableFuture<List<RpcResult<List<MultipartReply>>>> createDeviceFeaturesForOF13(
195 final DeviceContext deviceContext, final DeviceState deviceState, final boolean switchFeaturesMandatory) {
197 final ListenableFuture<RpcResult<List<MultipartReply>>> replyDesc = getNodeStaticInfo(MultipartType.OFPMPDESC,
198 deviceContext, deviceContext.getDeviceInfo().getNodeInstanceIdentifier(), deviceContext.getDeviceInfo().getVersion());
200 //first process description reply, write data to DS and write consequent data if successful
201 return Futures.transform(replyDesc,
202 new AsyncFunction<RpcResult<List<MultipartReply>>, List<RpcResult<List<MultipartReply>>>>() {
204 public ListenableFuture<List<RpcResult<List<MultipartReply>>>> apply(
205 final RpcResult<List<MultipartReply>> rpcResult) throws Exception {
207 translateAndWriteReply(MultipartType.OFPMPDESC, deviceContext,
208 deviceContext.getDeviceInfo().getNodeInstanceIdentifier(), rpcResult.getResult());
210 final ListenableFuture<RpcResult<List<MultipartReply>>> replyMeterFeature = getNodeStaticInfo(
211 MultipartType.OFPMPMETERFEATURES, deviceContext,
212 deviceContext.getDeviceInfo().getNodeInstanceIdentifier(), deviceContext.getDeviceInfo().getVersion());
214 createSuccessProcessingCallback(MultipartType.OFPMPMETERFEATURES, deviceContext,
215 deviceContext.getDeviceInfo().getNodeInstanceIdentifier(), replyMeterFeature);
217 final ListenableFuture<RpcResult<List<MultipartReply>>> replyGroupFeatures = getNodeStaticInfo(
218 MultipartType.OFPMPGROUPFEATURES, deviceContext,
219 deviceContext.getDeviceInfo().getNodeInstanceIdentifier(), deviceContext.getDeviceInfo().getVersion());
220 createSuccessProcessingCallback(MultipartType.OFPMPGROUPFEATURES, deviceContext,
221 deviceContext.getDeviceInfo().getNodeInstanceIdentifier(), replyGroupFeatures);
223 final ListenableFuture<RpcResult<List<MultipartReply>>> replyTableFeatures = getNodeStaticInfo(
224 MultipartType.OFPMPTABLEFEATURES, deviceContext,
225 deviceContext.getDeviceInfo().getNodeInstanceIdentifier(), deviceContext.getDeviceInfo().getVersion());
226 createSuccessProcessingCallback(MultipartType.OFPMPTABLEFEATURES, deviceContext,
227 deviceContext.getDeviceInfo().getNodeInstanceIdentifier(), replyTableFeatures);
229 final ListenableFuture<RpcResult<List<MultipartReply>>> replyPortDescription = getNodeStaticInfo(
230 MultipartType.OFPMPPORTDESC, deviceContext, deviceContext.getDeviceInfo().getNodeInstanceIdentifier(),
231 deviceContext.getDeviceInfo().getVersion());
232 createSuccessProcessingCallback(MultipartType.OFPMPPORTDESC, deviceContext,
233 deviceContext.getDeviceInfo().getNodeInstanceIdentifier(), replyPortDescription);
234 if (switchFeaturesMandatory) {
235 return Futures.allAsList(Arrays.asList(replyMeterFeature, replyGroupFeatures,
236 replyTableFeatures, replyPortDescription));
238 return Futures.successfulAsList(Arrays.asList(replyMeterFeature, replyGroupFeatures,
239 replyTableFeatures, replyPortDescription));
246 static void translateAndWriteReply(final MultipartType type, final DeviceContext dContext,
247 final InstanceIdentifier<Node> nodeII, final Collection<MultipartReply> result) {
249 for (final MultipartReply reply : result) {
250 final MultipartReplyBody body = reply.getMultipartReplyBody();
253 Preconditions.checkArgument(body instanceof MultipartReplyDescCase);
254 final MultipartReplyDesc replyDesc = ((MultipartReplyDescCase) body).getMultipartReplyDesc();
255 final FlowCapableNode fcNode = NodeStaticReplyTranslatorUtil.nodeDescTranslator(replyDesc,
256 getIpAddressOf(dContext));
257 final InstanceIdentifier<FlowCapableNode> fNodeII = nodeII.augmentation(FlowCapableNode.class);
258 dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, fNodeII, fcNode);
261 case OFPMPTABLEFEATURES:
262 Preconditions.checkArgument(body instanceof MultipartReplyTableFeaturesCase);
263 final MultipartReplyTableFeatures tableFeaturesMP = ((MultipartReplyTableFeaturesCase) body)
264 .getMultipartReplyTableFeatures();
265 final List<TableFeatures> tableFeatures = NodeStaticReplyTranslatorUtil
266 .nodeTableFeatureTranslator(tableFeaturesMP);
267 for (final TableFeatures tableFeature : tableFeatures) {
268 final Short tableId = tableFeature.getTableId();
269 final KeyedInstanceIdentifier<TableFeatures, TableFeaturesKey> tableFeaturesII =
270 nodeII.augmentation(FlowCapableNode.class)
271 .child(TableFeatures.class, new TableFeaturesKey(tableId));
272 dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, tableFeaturesII, tableFeature);
274 // write parent for table statistics
275 final KeyedInstanceIdentifier<Table, TableKey> tableII =
276 nodeII.augmentation(FlowCapableNode.class)
277 .child(Table.class, new TableKey(tableId));
278 final TableBuilder tableBld = new TableBuilder().setId(tableId)
279 .addAugmentation(FlowTableStatisticsData.class,
280 new FlowTableStatisticsDataBuilder().build());
282 dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, tableII, tableBld.build());
286 case OFPMPMETERFEATURES:
287 Preconditions.checkArgument(body instanceof MultipartReplyMeterFeaturesCase);
288 final MultipartReplyMeterFeatures meterFeatures = ((MultipartReplyMeterFeaturesCase) body)
289 .getMultipartReplyMeterFeatures();
290 final NodeMeterFeatures mFeature = NodeStaticReplyTranslatorUtil
291 .nodeMeterFeatureTranslator(meterFeatures);
292 final InstanceIdentifier<NodeMeterFeatures> mFeatureII = nodeII
293 .augmentation(NodeMeterFeatures.class);
294 dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, mFeatureII, mFeature);
295 if (0L < mFeature.getMeterFeatures().getMaxMeter().getValue()) {
296 dContext.getDeviceState().setMeterAvailable(true);
300 case OFPMPGROUPFEATURES:
301 Preconditions.checkArgument(body instanceof MultipartReplyGroupFeaturesCase);
302 final MultipartReplyGroupFeatures groupFeatures = ((MultipartReplyGroupFeaturesCase) body)
303 .getMultipartReplyGroupFeatures();
304 final NodeGroupFeatures gFeature = NodeStaticReplyTranslatorUtil
305 .nodeGroupFeatureTranslator(groupFeatures);
306 final InstanceIdentifier<NodeGroupFeatures> gFeatureII = nodeII
307 .augmentation(NodeGroupFeatures.class);
308 dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, gFeatureII, gFeature);
312 Preconditions.checkArgument(body instanceof MultipartReplyPortDescCase);
313 final MultipartReplyPortDesc portDesc = ((MultipartReplyPortDescCase) body)
314 .getMultipartReplyPortDesc();
315 for (final PortGrouping port : portDesc.getPorts()) {
316 final short ofVersion = dContext.getDeviceInfo().getVersion();
317 final TranslatorKey translatorKey = new TranslatorKey(ofVersion, PortGrouping.class.getName());
318 final MessageTranslator<PortGrouping, FlowCapableNodeConnector> translator = dContext.oook()
319 .lookupTranslator(translatorKey);
320 final FlowCapableNodeConnector fcNodeConnector = translator.translate(port, dContext.getDeviceInfo(), null);
322 final BigInteger dataPathId = dContext.getPrimaryConnectionContext().getFeatures()
324 final NodeConnectorId nodeConnectorId = NodeStaticReplyTranslatorUtil.nodeConnectorId(
325 dataPathId.toString(), port.getPortNo(), ofVersion);
326 final NodeConnectorBuilder ncBuilder = new NodeConnectorBuilder().setId(nodeConnectorId);
327 ncBuilder.addAugmentation(FlowCapableNodeConnector.class, fcNodeConnector);
329 ncBuilder.addAugmentation(FlowCapableNodeConnectorStatisticsData.class,
330 new FlowCapableNodeConnectorStatisticsDataBuilder().build());
331 final NodeConnector connector = ncBuilder.build();
333 final InstanceIdentifier<NodeConnector> connectorII = nodeII.child(NodeConnector.class,
335 dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, connectorII, connector);
341 throw new IllegalArgumentException("Unnexpected MultipartType " + type);
344 } catch (final Exception e) {
345 LOG.debug("Failed to write node {} to DS ", dContext.getDeviceInfo().getNodeId().toString(), e);
349 private static void createEmptyFlowCapableNodeInDs(final DeviceContext deviceContext) {
350 final FlowCapableNodeBuilder flowCapableNodeBuilder = new FlowCapableNodeBuilder();
351 final InstanceIdentifier<FlowCapableNode> fNodeII = deviceContext.getDeviceInfo().getNodeInstanceIdentifier()
352 .augmentation(FlowCapableNode.class);
354 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, fNodeII, flowCapableNodeBuilder.build());
355 } catch (final Exception e) {
356 LOG.debug("Failed to write node {} to DS ", deviceContext.getDeviceInfo().getNodeId().toString(), e);
360 private static IpAddress getIpAddressOf(final DeviceContext deviceContext) {
362 final InetSocketAddress remoteAddress = deviceContext.getPrimaryConnectionContext().getConnectionAdapter()
365 if (remoteAddress == null) {
366 LOG.warn("IP address of the node {} cannot be obtained. No connection with switch.", deviceContext
367 .getDeviceInfo().getNodeId());
370 LOG.info("IP address of switch is: {}", remoteAddress);
372 return IetfInetUtil.INSTANCE.ipAddressFor(remoteAddress.getAddress());
375 // FIXME : remove after ovs tableFeatures fix
376 private static void makeEmptyTables(final DeviceContext dContext, final InstanceIdentifier<Node> nodeII,
377 final Short nrOfTables) {
378 LOG.debug("About to create {} empty tables.", nrOfTables);
379 for (int i = 0; i < nrOfTables; i++) {
380 final short tId = (short) i;
381 final InstanceIdentifier<Table> tableII = nodeII.augmentation(FlowCapableNode.class).child(Table.class,
383 final TableBuilder tableBuilder = new TableBuilder().setId(tId).addAugmentation(
384 FlowTableStatisticsData.class, new FlowTableStatisticsDataBuilder().build());
387 dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, tableII, tableBuilder.build());
388 } catch (final Exception e) {
389 LOG.debug("Failed to write node {} to DS ", dContext.getDeviceInfo().getNodeId().toString(), e);
395 static void createSuccessProcessingCallback(final MultipartType type, final DeviceContext deviceContext,
396 final InstanceIdentifier<Node> nodeII,
397 final ListenableFuture<RpcResult<List<MultipartReply>>> requestContextFuture) {
398 Futures.addCallback(requestContextFuture, new FutureCallback<RpcResult<List<MultipartReply>>>() {
400 public void onSuccess(final RpcResult<List<MultipartReply>> rpcResult) {
401 final List<MultipartReply> result = rpcResult.getResult();
402 if (result != null) {
403 LOG.info("Static node {} info: {} collected", deviceContext.getDeviceInfo().getNodeId(), type);
404 translateAndWriteReply(type, deviceContext, nodeII, result);
406 final Iterator<RpcError> rpcErrorIterator = rpcResult.getErrors().iterator();
407 while (rpcErrorIterator.hasNext()) {
408 final RpcError rpcError = rpcErrorIterator.next();
409 LOG.info("Failed to retrieve static node {} info: {}", type, rpcError.getMessage());
410 if (null != rpcError.getCause()) {
411 LOG.trace("Detailed error:", rpcError.getCause());
414 if (MultipartType.OFPMPTABLEFEATURES.equals(type)) {
415 makeEmptyTables(deviceContext, nodeII, deviceContext.getPrimaryConnectionContext()
416 .getFeatures().getTables());
422 public void onFailure(final Throwable throwable) {
423 LOG.info("Request of type {} for static info of node {} failed.", type, nodeII);
428 private static ListenableFuture<RpcResult<List<MultipartReply>>> getNodeStaticInfo(final MultipartType type,
429 final DeviceContext deviceContext, final InstanceIdentifier<Node> nodeII, final short version) {
431 final OutboundQueue queue = deviceContext.getPrimaryConnectionContext().getOutboundQueueProvider();
433 final Long reserved = deviceContext.reserveXidForDeviceMessage();
434 final RequestContext<List<MultipartReply>> requestContext = new AbstractRequestContext<List<MultipartReply>>(
437 public void close() {
442 final Xid xid = requestContext.getXid();
444 LOG.trace("Hooking xid {} to device context - precaution.", reserved);
446 final MultiMsgCollector multiMsgCollector = deviceContext.getMultiMsgCollector(requestContext);
447 queue.commitEntry(xid.getValue(),
448 MultipartRequestInputFactory.makeMultipartRequestInput(xid.getValue(), version, type),
449 new FutureCallback<OfHeader>() {
451 public void onSuccess(final OfHeader ofHeader) {
452 if (ofHeader instanceof MultipartReply) {
453 final MultipartReply multipartReply = (MultipartReply) ofHeader;
454 multiMsgCollector.addMultipartMsg(multipartReply);
455 } else if (null != ofHeader) {
456 LOG.info("Unexpected response type received {}.", ofHeader.getClass());
458 multiMsgCollector.endCollecting();
459 LOG.info("Response received is null.");
464 public void onFailure(final Throwable t) {
465 LOG.info("Fail response from OutboundQueue for multipart type {}.", type);
466 final RpcResult<List<MultipartReply>> rpcResult = RpcResultBuilder
467 .<List<MultipartReply>>failed().build();
468 requestContext.setResult(rpcResult);
469 if (MultipartType.OFPMPTABLEFEATURES.equals(type)) {
470 makeEmptyTables(deviceContext, nodeII, deviceContext.getPrimaryConnectionContext()
471 .getFeatures().getTables());
473 requestContext.close();
477 return requestContext.getFuture();
480 static void chainTableTrunkWriteOF10(final DeviceContext deviceContext,
481 final ListenableFuture<List<RpcResult<List<MultipartReply>>>> deviceFeaturesFuture) {
482 Futures.addCallback(deviceFeaturesFuture, new FutureCallback<List<RpcResult<List<MultipartReply>>>>() {
484 public void onSuccess(final List<RpcResult<List<MultipartReply>>> results) {
485 boolean allSucceeded = true;
486 for (final RpcResult<List<MultipartReply>> rpcResult : results) {
487 allSucceeded &= rpcResult.isSuccessful();
490 createEmptyFlowCapableNodeInDs(deviceContext);
491 makeEmptyTables(deviceContext, deviceContext.getDeviceInfo().getNodeInstanceIdentifier(),
492 deviceContext.getPrimaryConnectionContext().getFeatures().getTables());
497 public void onFailure(final Throwable t) {