Simplify CommonService interface
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / statistics / services / FlowsInTableService.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 org.opendaylight.openflowplugin.api.OFConstants;
11 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
12 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
13 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
14 import org.opendaylight.openflowplugin.impl.services.AbstractSimpleService;
15 import org.opendaylight.openflowplugin.impl.services.RequestInputUtils;
16 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match.MatchReactor;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetFlowStatisticsFromFlowTableInput;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetFlowStatisticsFromFlowTableOutput;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInputBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestFlowCaseBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.flow._case.MultipartRequestFlowBuilder;
24
25 final class FlowsInTableService extends AbstractSimpleService<GetFlowStatisticsFromFlowTableInput, GetFlowStatisticsFromFlowTableOutput> {
26     FlowsInTableService(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
27         super(requestContextStack, deviceContext, GetFlowStatisticsFromFlowTableOutput.class);
28     }
29
30     @Override
31     protected OfHeader buildRequest(final Xid xid, final GetFlowStatisticsFromFlowTableInput input) {
32         final MultipartRequestFlowCaseBuilder multipartRequestFlowCaseBuilder = new MultipartRequestFlowCaseBuilder();
33         final MultipartRequestFlowBuilder mprFlowRequestBuilder = new MultipartRequestFlowBuilder();
34         mprFlowRequestBuilder.setTableId(input.getTableId());
35
36         if (input.getOutPort() != null) {
37             mprFlowRequestBuilder.setOutPort(input.getOutPort().longValue());
38         } else {
39             mprFlowRequestBuilder.setOutPort(OFConstants.OFPP_ANY);
40         }
41
42         if (input.getOutGroup() != null) {
43             mprFlowRequestBuilder.setOutGroup(input.getOutGroup());
44         } else {
45             mprFlowRequestBuilder.setOutGroup(OFConstants.OFPG_ANY);
46         }
47
48         if (input.getCookie() != null) {
49             mprFlowRequestBuilder.setCookie(input.getCookie().getValue());
50         } else {
51             mprFlowRequestBuilder.setCookie(OFConstants.DEFAULT_COOKIE);
52         }
53
54         if (input.getCookieMask() != null) {
55             mprFlowRequestBuilder.setCookieMask(input.getCookieMask().getValue());
56         } else {
57             mprFlowRequestBuilder.setCookieMask(OFConstants.DEFAULT_COOKIE_MASK);
58         }
59
60         // convert and inject match
61         final short version = getVersion();
62         final DeviceContext deviceContext = getDeviceContext();
63         MatchReactor.getInstance().convert(input.getMatch(), version, mprFlowRequestBuilder,
64                 deviceContext.getPrimaryConnectionContext().getFeatures().getDatapathId());
65
66         // Set request body to main multipart request
67         multipartRequestFlowCaseBuilder.setMultipartRequestFlow(mprFlowRequestBuilder.build());
68         final MultipartRequestInputBuilder mprInput = RequestInputUtils.createMultipartHeader(
69                 MultipartType.OFPMPFLOW, xid.getValue(), version);
70         mprInput.setMultipartRequestBody(multipartRequestFlowCaseBuilder.build());
71
72         return mprInput.build();
73     }
74 }