Simplify CommonService interface
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / statistics / services / MatchingFlowsInTableService.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.MoreObjects;
11 import org.opendaylight.openflowplugin.api.OFConstants;
12 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
13 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
14 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
15 import org.opendaylight.openflowplugin.impl.services.AbstractMultipartService;
16 import org.opendaylight.openflowplugin.impl.services.RequestInputUtils;
17 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match.MatchReactor;
18 import org.opendaylight.openflowplugin.openflow.md.util.FlowCreatorUtil;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForGivenMatchInput;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInputBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestAggregateCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.aggregate._case.MultipartRequestAggregateBuilder;
25
26 final class MatchingFlowsInTableService extends AbstractMultipartService<GetAggregateFlowStatisticsFromFlowTableForGivenMatchInput> {
27
28     public MatchingFlowsInTableService(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
29         super(requestContextStack, deviceContext);
30     }
31
32     @Override
33     protected OfHeader buildRequest(final Xid xid, final GetAggregateFlowStatisticsFromFlowTableForGivenMatchInput input) {
34         final DeviceContext deviceContext = getDeviceContext();
35         final MultipartRequestAggregateCaseBuilder multipartRequestAggregateCaseBuilder = new MultipartRequestAggregateCaseBuilder();
36         final MultipartRequestAggregateBuilder mprAggregateRequestBuilder = new MultipartRequestAggregateBuilder();
37         final short tableId = MoreObjects.firstNonNull(input.getTableId(), OFConstants.OFPTT_ALL).shortValue();
38         mprAggregateRequestBuilder.setTableId(tableId);
39         long outputPortValue = MoreObjects.firstNonNull(input.getOutPort(), OFConstants.OFPP_ANY).longValue();
40         mprAggregateRequestBuilder.setOutPort(outputPortValue);
41         // TODO: repeating code
42
43         final short version = getVersion();
44         if (version == OFConstants.OFP_VERSION_1_3) {
45
46             if (input.getCookie() == null) {
47                 mprAggregateRequestBuilder.setCookie(OFConstants.DEFAULT_COOKIE);
48             } else {
49                 mprAggregateRequestBuilder.setCookie(MoreObjects.firstNonNull(input.getCookie().getValue(), OFConstants.DEFAULT_COOKIE));
50             }
51
52             if (input.getCookieMask() == null) {
53                 mprAggregateRequestBuilder.setCookieMask(OFConstants.DEFAULT_COOKIE_MASK);
54             } else {
55                 mprAggregateRequestBuilder.setCookieMask(MoreObjects.firstNonNull(input.getCookieMask().getValue(), OFConstants.DEFAULT_COOKIE_MASK));
56             }
57             long outGroup = MoreObjects.firstNonNull(input.getOutGroup(), OFConstants.OFPG_ANY).longValue();
58             mprAggregateRequestBuilder.setOutGroup(outGroup);
59         } else {
60             mprAggregateRequestBuilder.setOutGroup(OFConstants.OFPG_ANY);
61             mprAggregateRequestBuilder.setCookie(OFConstants.DEFAULT_COOKIE);
62             mprAggregateRequestBuilder.setCookieMask(OFConstants.DEFAULT_COOKIE_MASK);
63         }
64
65         MatchReactor.getInstance().convert(input.getMatch(), version, mprAggregateRequestBuilder,
66                 deviceContext.getPrimaryConnectionContext().getFeatures().getDatapathId());
67
68         FlowCreatorUtil.setWildcardedFlowMatch(version, mprAggregateRequestBuilder);
69
70         // Set request body to main multipart request
71         multipartRequestAggregateCaseBuilder.setMultipartRequestAggregate(mprAggregateRequestBuilder
72                 .build());
73
74         final MultipartRequestInputBuilder mprInput = RequestInputUtils.createMultipartHeader(
75                 MultipartType.OFPMPAGGREGATE, xid.getValue(), version);
76
77         mprInput.setMultipartRequestBody(multipartRequestAggregateCaseBuilder.build());
78
79         return mprInput.build();
80     }
81 }