DeviceState changes
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / statistics / services / AggregateFlowsInTableService.java
index baaedcbd7b949b10b8cab3175dd1521dd7114c09..4c822cd55cd7623790fe34a5b775e901a09a63ad 100644 (file)
@@ -7,24 +7,50 @@
  */
 package org.opendaylight.openflowplugin.impl.statistics.services;
 
+import com.google.common.base.Preconditions;
+import java.util.List;
+import java.util.concurrent.atomic.AtomicLong;
 import org.opendaylight.openflowplugin.api.OFConstants;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
+import org.opendaylight.openflowplugin.api.openflow.device.MessageTranslator;
 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
+import org.opendaylight.openflowplugin.api.openflow.device.TranslatorLibrary;
 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
-import org.opendaylight.openflowplugin.impl.services.AbstractSimpleService;
+import org.opendaylight.openflowplugin.api.openflow.md.core.TranslatorKey;
 import org.opendaylight.openflowplugin.impl.services.RequestInputUtils;
+import org.opendaylight.openflowplugin.impl.statistics.services.compatibility.AbstractCompatibleStatService;
 import org.opendaylight.openflowplugin.openflow.md.util.FlowCreatorUtil;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.AggregateFlowStatisticsUpdate;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.AggregateFlowStatisticsUpdateBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForAllFlowsInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutputBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.get.aggregate.flow.statistics.from.flow.table._for.given.match.output.AggregatedFlowStatistics;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.TransactionId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInputBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyAggregateCase;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestAggregateCaseBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.aggregate._case.MultipartRequestAggregateBuilder;
 
-final class AggregateFlowsInTableService extends AbstractSimpleService<GetAggregateFlowStatisticsFromFlowTableForAllFlowsInput, GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput> {
-    public AggregateFlowsInTableService(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
-        super(requestContextStack, deviceContext, GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput.class);
+public final class AggregateFlowsInTableService extends AbstractCompatibleStatService<GetAggregateFlowStatisticsFromFlowTableForAllFlowsInput,
+        GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput, AggregateFlowStatisticsUpdate> {
+
+    final TranslatorLibrary translatorLibrary;
+
+    public static AggregateFlowsInTableService createWithOook(final RequestContextStack requestContextStack,
+                                                                                  final DeviceContext deviceContext,
+                                                                                  AtomicLong compatibilityXidSeed) {
+        return new AggregateFlowsInTableService(requestContextStack, deviceContext, compatibilityXidSeed, deviceContext.oook());
+    }
+
+    public AggregateFlowsInTableService(final RequestContextStack requestContextStack, final DeviceContext deviceContext,
+                                        AtomicLong compatibilityXidSeed, TranslatorLibrary translatorLibrary) {
+        super(requestContextStack, deviceContext, compatibilityXidSeed);
+
+        this.translatorLibrary = translatorLibrary;
     }
 
     @Override
@@ -50,4 +76,27 @@ final class AggregateFlowsInTableService extends AbstractSimpleService<GetAggreg
 
         return mprInput.build();
     }
+
+    @Override
+    public GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput buildTxCapableResult(TransactionId emulatedTxId) {
+        return new GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutputBuilder().setTransactionId(emulatedTxId).build();
+    }
+
+    @Override
+    public AggregateFlowStatisticsUpdate transformToNotification(List<MultipartReply> result, TransactionId emulatedTxId) {
+        final int mpSize = result.size();
+        Preconditions.checkArgument(mpSize == 1, "unexpected (!=1) mp-reply size received: {}", mpSize);
+
+        MultipartReply mpReply = result.get(0);
+        final TranslatorKey translatorKey = new TranslatorKey(mpReply.getVersion(), MultipartReplyAggregateCase.class.getName());
+        final MessageTranslator<MultipartReply, AggregatedFlowStatistics> messageTranslator = translatorLibrary.lookupTranslator(translatorKey);
+
+        final AggregatedFlowStatistics flowStatistics = messageTranslator.translate(mpReply, getDeviceContext().getDeviceInfo(), null);
+        final AggregateFlowStatisticsUpdateBuilder notification = new AggregateFlowStatisticsUpdateBuilder(flowStatistics)
+                .setId(getDeviceContext().getDeviceInfo().getNodeId())
+                .setMoreReplies(Boolean.FALSE)
+                .setTransactionId(emulatedTxId);
+
+        return notification.build();
+    }
 }