Bump mdsal to 5.0.2
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / statistics / services / AggregateFlowsInTableService.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowplugin.impl.statistics.services;
9
10 import com.google.common.base.Preconditions;
11 import java.util.List;
12 import java.util.concurrent.atomic.AtomicLong;
13 import org.opendaylight.openflowplugin.api.OFConstants;
14 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
15 import org.opendaylight.openflowplugin.api.openflow.device.MessageTranslator;
16 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
17 import org.opendaylight.openflowplugin.api.openflow.device.TranslatorLibrary;
18 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
19 import org.opendaylight.openflowplugin.api.openflow.md.core.TranslatorKey;
20 import org.opendaylight.openflowplugin.impl.services.util.RequestInputUtils;
21 import org.opendaylight.openflowplugin.impl.statistics.services.compatibility.AbstractCompatibleStatService;
22 import org.opendaylight.openflowplugin.impl.util.FlowCreatorUtil;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.AggregateFlowStatisticsUpdate;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.AggregateFlowStatisticsUpdateBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForAllFlowsInput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutputBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.get.aggregate.flow.statistics.from.flow.table._for.given.match.output.AggregatedFlowStatistics;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.TransactionId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInputBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyAggregateCase;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestAggregateCaseBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.aggregate._case.MultipartRequestAggregateBuilder;
37
38 public final class AggregateFlowsInTableService extends
39         AbstractCompatibleStatService<GetAggregateFlowStatisticsFromFlowTableForAllFlowsInput,
40                                       GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput,
41                                       AggregateFlowStatisticsUpdate> {
42
43     final TranslatorLibrary translatorLibrary;
44
45     public static AggregateFlowsInTableService createWithOook(final RequestContextStack requestContextStack,
46                                                               final DeviceContext deviceContext,
47                                                               final AtomicLong compatibilityXidSeed) {
48         return new AggregateFlowsInTableService(requestContextStack,
49                                                 deviceContext,
50                                                 compatibilityXidSeed,
51                                                 deviceContext.oook());
52     }
53
54     public AggregateFlowsInTableService(final RequestContextStack requestContextStack,
55                                         final DeviceContext deviceContext,
56                                         final AtomicLong compatibilityXidSeed,
57                                         final TranslatorLibrary translatorLibrary) {
58         super(requestContextStack, deviceContext, compatibilityXidSeed);
59
60         this.translatorLibrary = translatorLibrary;
61     }
62
63     @Override
64     protected OfHeader buildRequest(final Xid xid,
65             final GetAggregateFlowStatisticsFromFlowTableForAllFlowsInput input) {
66         // Create multipart request body for fetch all the group stats
67         final MultipartRequestAggregateCaseBuilder multipartRequestAggregateCaseBuilder =
68                 new MultipartRequestAggregateCaseBuilder();
69         final MultipartRequestAggregateBuilder mprAggregateRequestBuilder = new MultipartRequestAggregateBuilder();
70         mprAggregateRequestBuilder.setTableId(input.getTableId().getValue());
71         mprAggregateRequestBuilder.setOutPort(OFConstants.OFPP_ANY);
72         mprAggregateRequestBuilder.setOutGroup(OFConstants.OFPG_ANY);
73         mprAggregateRequestBuilder.setCookie(OFConstants.DEFAULT_COOKIE);
74         mprAggregateRequestBuilder.setCookieMask(OFConstants.DEFAULT_COOKIE_MASK);
75         final short version = getVersion();
76         FlowCreatorUtil.setWildcardedFlowMatch(version, mprAggregateRequestBuilder);
77
78         // Set request body to main multipart request
79         multipartRequestAggregateCaseBuilder.setMultipartRequestAggregate(mprAggregateRequestBuilder
80                 .build());
81         final MultipartRequestInputBuilder mprInput = RequestInputUtils.createMultipartHeader(
82                 MultipartType.OFPMPAGGREGATE, xid.getValue(), version);
83
84         mprInput.setMultipartRequestBody(multipartRequestAggregateCaseBuilder.build());
85
86         return mprInput.build();
87     }
88
89     @Override
90     public GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput buildTxCapableResult(
91             final TransactionId emulatedTxId) {
92         return new GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutputBuilder().setTransactionId(emulatedTxId)
93                 .build();
94     }
95
96     @Override
97     public AggregateFlowStatisticsUpdate transformToNotification(final List<MultipartReply> result,
98                                                                  final TransactionId emulatedTxId) {
99         final int mpSize = result.size();
100         Preconditions.checkArgument(mpSize == 1, "unexpected (!=1) mp-reply size received: {}", mpSize);
101
102         MultipartReply mpReply = result.get(0);
103         final TranslatorKey translatorKey =
104                 new TranslatorKey(mpReply.getVersion().toJava(), MultipartReplyAggregateCase.class.getName());
105         final MessageTranslator<MultipartReply, AggregatedFlowStatistics> messageTranslator =
106                 translatorLibrary.lookupTranslator(translatorKey);
107
108         final AggregatedFlowStatistics flowStatistics = messageTranslator.translate(mpReply, getDeviceInfo(), null);
109         final AggregateFlowStatisticsUpdateBuilder notification =
110                 new AggregateFlowStatisticsUpdateBuilder(flowStatistics)
111                 .setId(getDeviceInfo().getNodeId())
112                 .setMoreReplies(Boolean.FALSE)
113                 .setTransactionId(emulatedTxId);
114
115         return notification.build();
116     }
117 }