Fix errors in serializers and deserializers
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / util / DeviceInitializationUtils.java
index ad5599f1cebf2bffdfa96d0112df8f5e09370bbb..0bdbe08a9c82e01be756b0cabec21d027f6a2863 100644 (file)
@@ -20,6 +20,7 @@ import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
+import java.util.Objects;
 import java.util.concurrent.ExecutionException;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
@@ -37,9 +38,10 @@ import org.opendaylight.openflowplugin.api.openflow.md.core.TranslatorKey;
 import org.opendaylight.openflowplugin.impl.common.MultipartRequestInputFactory;
 import org.opendaylight.openflowplugin.impl.common.NodeStaticReplyTranslatorUtil;
 import org.opendaylight.openflowplugin.impl.rpc.AbstractRequestContext;
+import org.opendaylight.openflowplugin.openflow.md.core.sal.SwitchFeaturesUtil;
+import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
-import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
@@ -58,6 +60,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.Capabilities;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.CapabilitiesV10;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutputBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortGrouping;
@@ -99,16 +102,15 @@ public class DeviceInitializationUtils {
      * @param deviceContext
      * @param switchFeaturesMandatory
      * @param convertorExecutor
-     * @return future - recommended to have blocking call for this future
      */
-    public static ListenableFuture<Void> initializeNodeInformation(final DeviceContext deviceContext, final boolean switchFeaturesMandatory, final ConvertorExecutor convertorExecutor) {
+    public static void initializeNodeInformation(final DeviceContext deviceContext, final boolean switchFeaturesMandatory, final ConvertorExecutor convertorExecutor) throws ExecutionException, InterruptedException {
         Preconditions.checkArgument(deviceContext != null);
         final DeviceState deviceState = Preconditions.checkNotNull(deviceContext.getDeviceState());
         final DeviceInfo deviceInfo = deviceContext.getDeviceInfo();
         final ConnectionContext connectionContext = Preconditions.checkNotNull(deviceContext.getPrimaryConnectionContext());
         final short version = deviceInfo.getVersion();
         LOG.trace("initalizeNodeInformation for node {}", deviceInfo.getNodeId());
-        final SettableFuture<Void> returnFuture = SettableFuture.<Void>create();
+        final SettableFuture<Void> returnFuture = SettableFuture.create();
         addNodeToOperDS(deviceContext, returnFuture);
         final ListenableFuture<List<RpcResult<List<MultipartReply>>>> deviceFeaturesFuture;
         if (OFConstants.OFP_VERSION_1_0 == version) {
@@ -150,35 +152,17 @@ public class DeviceInitializationUtils {
             final Capabilities capabilities = connectionContext.getFeatures().getCapabilities();
             LOG.debug("Setting capabilities for device {}", deviceInfo.getNodeId());
             DeviceStateUtil.setDeviceStateBasedOnV13Capabilities(deviceState, capabilities);
-            deviceFeaturesFuture = createDeviceFeaturesForOF13(deviceContext, switchFeaturesMandatory, convertorExecutor);
+            createDeviceFeaturesForOF13(deviceContext, switchFeaturesMandatory, convertorExecutor).get();
         } else {
-            deviceFeaturesFuture = Futures.immediateFailedFuture(new ConnectionException("Unsupported version "
-                    + version));
+            throw new ExecutionException(new ConnectionException("Unsupported version " + version));
         }
 
-        Futures.addCallback(deviceFeaturesFuture, new FutureCallback<List<RpcResult<List<MultipartReply>>>>() {
-            @Override
-            public void onSuccess(final List<RpcResult<List<MultipartReply>>> result) {
-                LOG.debug("All init data for node {} is in submitted.", deviceInfo.getNodeId());
-                returnFuture.set(null);
-            }
-
-            @Override
-            public void onFailure(final Throwable t) {
-                // FIXME : remove session
-                LOG.trace("Device capabilities gathering future failed.");
-                LOG.trace("more info in exploration failure..", t);
-                LOG.debug("All init data for node {} was not submited correctly - connection has to go down.", deviceInfo.getNodeId());
-                returnFuture.setException(t);
-            }
-        });
-        return returnFuture;
     }
 
     private static void addNodeToOperDS(final DeviceContext deviceContext, final SettableFuture<Void> future) {
         Preconditions.checkArgument(deviceContext != null);
         final NodeBuilder nodeBuilder = new NodeBuilder().setId(deviceContext.getDeviceInfo().getNodeId()).setNodeConnector(
-                Collections.<NodeConnector>emptyList());
+                Collections.emptyList());
         try {
             deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, deviceContext.getDeviceInfo().getNodeInstanceIdentifier(),
                     nodeBuilder.build());
@@ -225,9 +209,15 @@ public class DeviceInitializationUtils {
                         createSuccessProcessingCallback(MultipartType.OFPMPGROUPFEATURES, deviceContext,
                                 deviceContext.getDeviceInfo().getNodeInstanceIdentifier(), replyGroupFeatures, convertorExecutor);
 
-                        final ListenableFuture<RpcResult<List<MultipartReply>>> replyTableFeatures = getNodeStaticInfo(
-                                MultipartType.OFPMPTABLEFEATURES, deviceContext,
-                                deviceContext.getDeviceInfo().getNodeInstanceIdentifier(), deviceContext.getDeviceInfo().getVersion());
+                        final ListenableFuture<RpcResult<List<MultipartReply>>> replyTableFeatures;
+
+                        if (deviceContext.isSkipTableFeatures()) {
+                            replyTableFeatures = RpcResultBuilder.<List<MultipartReply>>success().buildFuture();
+                        } else {
+                            replyTableFeatures = getNodeStaticInfo(
+                                    MultipartType.OFPMPTABLEFEATURES, deviceContext,
+                                    deviceContext.getDeviceInfo().getNodeInstanceIdentifier(), deviceContext.getDeviceInfo().getVersion());
+                        }
                         createSuccessProcessingCallback(MultipartType.OFPMPTABLEFEATURES, deviceContext,
                                 deviceContext.getDeviceInfo().getNodeInstanceIdentifier(), replyTableFeatures, convertorExecutor);
 
@@ -251,20 +241,26 @@ public class DeviceInitializationUtils {
     static void translateAndWriteReply(final MultipartType type, final DeviceContext dContext,
                                        final InstanceIdentifier<Node> nodeII, final Collection<MultipartReply> result,
                                        final ConvertorExecutor convertorExecutor) {
-        try {
-            result.stream()
-                    .map(MultipartReply::getMultipartReplyBody)
-                    .forEach(multipartReplyBody -> {
-                        if (!(writeDesc(type, multipartReplyBody, dContext, nodeII)
-                                || writeTableFeatures(type, multipartReplyBody, dContext, nodeII, convertorExecutor)
-                                || writeMeterFeatures(type, multipartReplyBody, dContext, nodeII)
-                                || writeGroupFeatures(type, multipartReplyBody, dContext, nodeII)
-                                || writePortDesc(type, multipartReplyBody, dContext, nodeII))) {
-                            throw new IllegalArgumentException("Unexpected MultipartType " + type);
-                        }
-                    });
-        } catch (final Exception e) {
-            LOG.debug("translateAndWriteReply: Failed to write node {} to DS ", dContext.getDeviceInfo().getNodeId().toString(), e);
+        if (Objects.nonNull(result)) {
+            try {
+                result.stream()
+                        .map(MultipartReply::getMultipartReplyBody)
+                        .forEach(multipartReplyBody -> {
+                            if (!(writeDesc(type, multipartReplyBody, dContext, nodeII)
+                                    || writeTableFeatures(type, multipartReplyBody, dContext, nodeII, convertorExecutor)
+                                    || writeMeterFeatures(type, multipartReplyBody, dContext, nodeII)
+                                    || writeGroupFeatures(type, multipartReplyBody, dContext, nodeII)
+                                    || writePortDesc(type, multipartReplyBody, dContext, nodeII))) {
+                                throw new IllegalArgumentException("Unexpected MultipartType " + type);
+                            }
+                        });
+            } catch (final Exception e) {
+                LOG.debug("translateAndWriteReply: Failed to write node {} to DS ", dContext.getDeviceInfo().getNodeId().toString(), e);
+            }
+        } else {
+            LOG.debug("translateAndWriteReply: Failed to write node {} to DS because we failed to gather device" +
+                            "info.",
+                    dContext.getDeviceInfo().getNodeId().toString());
         }
     }
 
@@ -278,8 +274,12 @@ public class DeviceInitializationUtils {
 
         Preconditions.checkArgument(body instanceof MultipartReplyDescCase);
         final MultipartReplyDesc replyDesc = ((MultipartReplyDescCase) body).getMultipartReplyDesc();
-        final FlowCapableNode fcNode = NodeStaticReplyTranslatorUtil.nodeDescTranslator(replyDesc,
-                getIpAddressOf(dContext));
+        final FlowCapableNode fcNode = NodeStaticReplyTranslatorUtil
+                .nodeDescTranslator(replyDesc, getIpAddressOf(dContext))
+                .setSwitchFeatures(SwitchFeaturesUtil.getInstance().buildSwitchFeatures(
+                        new GetFeaturesOutputBuilder(dContext.getPrimaryConnectionContext().getFeatures()).build()))
+                .build();
+
         final InstanceIdentifier<FlowCapableNode> fNodeII = nodeII.augmentation(FlowCapableNode.class);
         dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, fNodeII, fcNode);
         return true;
@@ -429,7 +429,9 @@ public class DeviceInitializationUtils {
     // FIXME : remove after ovs tableFeatures fix
     private static void makeEmptyTables(final DeviceContext dContext, final InstanceIdentifier<Node> nodeII,
                                         final Short nrOfTables) {
-        LOG.debug("About to create {} empty tables.", nrOfTables);
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("About to create {} empty tables.", nrOfTables);
+        }
         for (int i = 0; i < nrOfTables; i++) {
             final short tId = (short) i;
             final InstanceIdentifier<Table> tableII = nodeII.augmentation(FlowCapableNode.class).child(Table.class,
@@ -460,7 +462,7 @@ public class DeviceInitializationUtils {
                 } else {
                     for (RpcError rpcError : rpcResult.getErrors()) {
                         LOG.info("Failed to retrieve static node {} info: {}", type, rpcError.getMessage());
-                        if (null != rpcError.getCause()) {
+                        if (LOG.isTraceEnabled() && Objects.nonNull(rpcError.getCause())) {
                             LOG.trace("Detailed error:", rpcError.getCause());
                         }
                     }
@@ -479,11 +481,13 @@ public class DeviceInitializationUtils {
     }
 
     private static ListenableFuture<RpcResult<List<MultipartReply>>> getNodeStaticInfo(final MultipartType type,
-                                                                                       final DeviceContext deviceContext, final InstanceIdentifier<Node> nodeII, final short version) {
+                                                                                       final DeviceContext deviceContext,
+                                                                                       final InstanceIdentifier<Node> nodeII,
+                                                                                       final short version) {
 
         final OutboundQueue queue = deviceContext.getPrimaryConnectionContext().getOutboundQueueProvider();
 
-        final Long reserved = deviceContext.reserveXidForDeviceMessage();
+        final Long reserved = deviceContext.getDeviceInfo().reserveXidForDeviceMessage();
         final RequestContext<List<MultipartReply>> requestContext = new AbstractRequestContext<List<MultipartReply>>(
                 reserved) {
             @Override
@@ -494,6 +498,11 @@ public class DeviceInitializationUtils {
 
         final Xid xid = requestContext.getXid();
 
+        if (Objects.isNull(xid)) {
+            LOG.debug("Xid is not present, so cancelling node static info gathering.");
+            return Futures.immediateCancelledFuture();
+        }
+
         LOG.trace("Hooking xid {} to device context - precaution.", reserved);
 
         final MultiMsgCollector multiMsgCollector = deviceContext.getMultiMsgCollector(requestContext);
@@ -534,21 +543,30 @@ public class DeviceInitializationUtils {
                                          final ListenableFuture<List<RpcResult<List<MultipartReply>>>> deviceFeaturesFuture) {
 
         try {
-            LOG.trace("Waiting for protocol version 1.0");
+            if (LOG.isTraceEnabled()) {
+                LOG.trace("Waiting for protocol version 1.0");
+            }
             List<RpcResult<List<MultipartReply>>> results = deviceFeaturesFuture.get();
             boolean allSucceeded = true;
             for (final RpcResult<List<MultipartReply>> rpcResult : results) {
                 allSucceeded &= rpcResult.isSuccessful();
             }
             if (allSucceeded) {
-                LOG.debug("Creating emtpy flow capable node: {}", deviceContext.getDeviceInfo().getNodeId().getValue());
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug("Creating empty flow capable node: {}", deviceContext.getDeviceInfo().getLOGValue());
+                }
                 createEmptyFlowCapableNodeInDs(deviceContext);
-                LOG.debug("Creating emtpy tables for {}", deviceContext.getDeviceInfo().getNodeId().getValue());
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug("Creating empty tables for {}", deviceContext.getDeviceInfo().getLOGValue());
+                }
                 makeEmptyTables(deviceContext, deviceContext.getDeviceInfo().getNodeInstanceIdentifier(),
                         deviceContext.getPrimaryConnectionContext().getFeatures().getTables());
             }
         } catch (InterruptedException | ExecutionException e) {
-            LOG.warn("Error occurred in preparation node {} for protocol 1.0", deviceContext.getDeviceInfo().getNodeId().getValue());
+            LOG.warn("Error occurred in preparation node {} for protocol 1.0", deviceContext.getDeviceInfo().getLOGValue());
+            if (LOG.isTraceEnabled()) {
+                LOG.trace("Error for node {} : ", deviceContext.getDeviceInfo().getLOGValue(), e);
+            }
         }
     }
 }