Merge "Bug 6372 table-miss-enforcer - DTCL instead of DTL"
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / SalTableServiceImpl.java
index 1a3c104b39079b2d55406839b479c8e3e20603f5..25ccd243ae49e1b9a44628d91b963349e881bd56 100644 (file)
@@ -13,17 +13,19 @@ import com.google.common.util.concurrent.ListenableFuture;
 import com.google.common.util.concurrent.SettableFuture;
 import java.math.BigInteger;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
+import java.util.Optional;
 import java.util.concurrent.Future;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
+import org.opendaylight.openflowplugin.api.openflow.device.TxFacade;
 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
-import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.TableFeaturesConvertor;
-import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.TableFeaturesReplyConvertor;
+import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
+import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.data.VersionConvertorData;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.TransactionId;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
@@ -52,9 +54,15 @@ import org.slf4j.Logger;
 
 public final class SalTableServiceImpl extends AbstractMultipartService<UpdateTableInput> implements SalTableService {
     private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(SalTableServiceImpl.class);
+    private final TxFacade txFacade;
+    private final ConvertorExecutor convertorExecutor;
+    private final VersionConvertorData data;
 
-    public SalTableServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
+    public SalTableServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext, final ConvertorExecutor convertorExecutor) {
         super(requestContextStack, deviceContext);
+        this.txFacade = deviceContext;
+        this.convertorExecutor = convertorExecutor;
+        data = new VersionConvertorData(getVersion());
     }
 
     @Override
@@ -80,7 +88,11 @@ public final class SalTableServiceImpl extends AbstractMultipartService<UpdateTa
                         final UpdateTableOutputBuilder updateTableOutputBuilder = new UpdateTableOutputBuilder();
                         updateTableOutputBuilder.setTransactionId(new TransactionId(BigInteger.valueOf(xid)));
                         finalFuture.set(RpcResultBuilder.success(updateTableOutputBuilder.build()).build());
-                        writeResponseToOperationalDatastore(multipartReplies);
+                        try {
+                            writeResponseToOperationalDatastore(multipartReplies);
+                        } catch (Exception e) {
+                            LOG.warn("Not able to write to operational datastore: {}", e.getMessage());
+                        }
                     }
                 } else {
                     LOG.debug("OnSuccess, rpc result unsuccessful, multipart response for rpc update-table was unsuccessful.");
@@ -91,7 +103,7 @@ public final class SalTableServiceImpl extends AbstractMultipartService<UpdateTa
 
             @Override
             public void onFailure(final Throwable t) {
-                LOG.debug("Failure multipart response for table features request. Exception: {}", t);
+                LOG.error("Failure multipart response for table features request. Exception: {}", t);
                 finalFuture.set(RpcResultBuilder.<UpdateTableOutput>failed()
                         .withError(ErrorType.RPC, "Future error", t).build());
             }
@@ -105,26 +117,25 @@ public final class SalTableServiceImpl extends AbstractMultipartService<UpdateTa
     /**
      * @param multipartReplies
      */
-    private void writeResponseToOperationalDatastore(final List<MultipartReply> multipartReplies) {
+    private void writeResponseToOperationalDatastore(final List<MultipartReply> multipartReplies) throws Exception {
 
         final List<org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures> salTableFeatures = convertToSalTableFeatures(multipartReplies);
 
-        final DeviceContext deviceContext = getDeviceContext();
-        final NodeId nodeId = deviceContext.getPrimaryConnectionContext().getNodeId();
         final InstanceIdentifier<FlowCapableNode> flowCapableNodeII = InstanceIdentifier.create(Nodes.class)
-                .child(Node.class, new NodeKey(nodeId)).augmentation(FlowCapableNode.class);
+                .child(Node.class, new NodeKey(getDeviceInfo().getNodeId())).augmentation(FlowCapableNode.class);
         for (final org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures tableFeatureData : salTableFeatures) {
             final Short tableId = tableFeatureData.getTableId();
             final KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures, TableFeaturesKey> tableFeaturesII = flowCapableNodeII
                     .child(org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures.class,
                             new TableFeaturesKey(tableId));
-            deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, tableFeaturesII,
+            txFacade.writeToTransaction(LogicalDatastoreType.OPERATIONAL, tableFeaturesII,
                     tableFeatureData);
         }
-        deviceContext.submitTransaction();
+
+        txFacade.submitTransaction();
     }
 
-    protected static List<org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures> convertToSalTableFeatures(
+    protected List<org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures> convertToSalTableFeatures(
             final List<MultipartReply> multipartReplies) {
         final List<org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures> salTableFeaturesAll = new ArrayList<>();
         for (final MultipartReply multipartReply : multipartReplies) {
@@ -134,9 +145,14 @@ public final class SalTableServiceImpl extends AbstractMultipartService<UpdateTa
                     final MultipartReplyTableFeaturesCase tableFeaturesCase = ((MultipartReplyTableFeaturesCase) multipartReplyBody);
                     final MultipartReplyTableFeatures salTableFeatures = tableFeaturesCase
                             .getMultipartReplyTableFeatures();
-                    final List<org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures> salTableFeaturesPartial = TableFeaturesReplyConvertor
-                            .toTableFeaturesReply(salTableFeatures);
-                    salTableFeaturesAll.addAll(salTableFeaturesPartial);
+
+                    final Optional<List<org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures>> salTableFeaturesPartial =
+                            convertorExecutor.convert(salTableFeatures, data);
+
+                    if (salTableFeaturesPartial.isPresent()) {
+                        salTableFeaturesAll.addAll(salTableFeaturesPartial.get());
+                    }
+
                     LOG.debug("TableFeature {} for xid {}.", salTableFeatures, multipartReply.getXid());
                 }
             }
@@ -157,9 +173,9 @@ public final class SalTableServiceImpl extends AbstractMultipartService<UpdateTa
     protected OfHeader buildRequest(final Xid xid, final UpdateTableInput input) {
         final MultipartRequestTableFeaturesCaseBuilder caseBuilder = new MultipartRequestTableFeaturesCaseBuilder();
         final MultipartRequestTableFeaturesBuilder requestBuilder = new MultipartRequestTableFeaturesBuilder();
-        final List<TableFeatures> ofTableFeatureList = TableFeaturesConvertor.toTableFeaturesRequest(input
-            .getUpdatedTable());
-        requestBuilder.setTableFeatures(ofTableFeatureList);
+
+        final Optional<List<TableFeatures>> ofTableFeatureList = convertorExecutor.convert(input.getUpdatedTable(), data);
+        requestBuilder.setTableFeatures(ofTableFeatureList.orElse(Collections.emptyList()));
         caseBuilder.setMultipartRequestTableFeatures(requestBuilder.build());
 
         // Set request body to main multipart request