Bug 5540 - ConvertorManager base
[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 java.util.List;
11 import java.util.concurrent.atomic.AtomicLong;
12 import org.opendaylight.openflowplugin.api.OFConstants;
13 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
14 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
15 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
16 import org.opendaylight.openflowplugin.impl.services.RequestInputUtils;
17 import org.opendaylight.openflowplugin.impl.statistics.services.compatibility.AbstractCompatibleStatService;
18 import org.opendaylight.openflowplugin.impl.statistics.services.compatibility.FlowStatisticsToNotificationTransformer;
19 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match.MatchReactor;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdate;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetFlowStatisticsFromFlowTableInput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetFlowStatisticsFromFlowTableOutput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetFlowStatisticsFromFlowTableOutputBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.TransactionId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInputBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestFlowCaseBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.flow._case.MultipartRequestFlowBuilder;
31
32 public final class FlowsInTableService extends AbstractCompatibleStatService<GetFlowStatisticsFromFlowTableInput,
33         GetFlowStatisticsFromFlowTableOutput, FlowsStatisticsUpdate> {
34
35     public FlowsInTableService(final RequestContextStack requestContextStack, final DeviceContext deviceContext, AtomicLong compatibilityXidSeed) {
36         super(requestContextStack, deviceContext, compatibilityXidSeed);
37     }
38
39     @Override
40     protected OfHeader buildRequest(final Xid xid, final GetFlowStatisticsFromFlowTableInput input) {
41         final MultipartRequestFlowCaseBuilder multipartRequestFlowCaseBuilder = new MultipartRequestFlowCaseBuilder();
42         final MultipartRequestFlowBuilder mprFlowRequestBuilder = new MultipartRequestFlowBuilder();
43
44         if (input.getTableId() != null) {
45             mprFlowRequestBuilder.setTableId(input.getTableId());
46         } else {
47             mprFlowRequestBuilder.setTableId(OFConstants.OFPTT_ALL);
48         }
49
50         if (input.getOutPort() != null) {
51             mprFlowRequestBuilder.setOutPort(input.getOutPort().longValue());
52         } else {
53             mprFlowRequestBuilder.setOutPort(OFConstants.OFPP_ANY);
54         }
55
56         if (input.getOutGroup() != null) {
57             mprFlowRequestBuilder.setOutGroup(input.getOutGroup());
58         } else {
59             mprFlowRequestBuilder.setOutGroup(OFConstants.OFPG_ANY);
60         }
61
62         if (input.getCookie() != null) {
63             mprFlowRequestBuilder.setCookie(input.getCookie().getValue());
64         } else {
65             mprFlowRequestBuilder.setCookie(OFConstants.DEFAULT_COOKIE);
66         }
67
68         if (input.getCookieMask() != null) {
69             mprFlowRequestBuilder.setCookieMask(input.getCookieMask().getValue());
70         } else {
71             mprFlowRequestBuilder.setCookieMask(OFConstants.DEFAULT_COOKIE_MASK);
72         }
73
74         // convert and inject match
75         final short version = getVersion();
76         MatchReactor.getInstance().convert(input.getMatch(), version, mprFlowRequestBuilder);
77
78         // Set request body to main multipart request
79         multipartRequestFlowCaseBuilder.setMultipartRequestFlow(mprFlowRequestBuilder.build());
80         final MultipartRequestInputBuilder mprInput = RequestInputUtils.createMultipartHeader(
81                 MultipartType.OFPMPFLOW, xid.getValue(), version);
82         mprInput.setMultipartRequestBody(multipartRequestFlowCaseBuilder.build());
83
84         return mprInput.build();
85     }
86
87     @Override
88     public GetFlowStatisticsFromFlowTableOutput buildTxCapableResult(TransactionId emulatedTxId) {
89         return new GetFlowStatisticsFromFlowTableOutputBuilder().setTransactionId(emulatedTxId).build();
90     }
91
92     @Override
93     public FlowsStatisticsUpdate transformToNotification(List<MultipartReply> result, TransactionId emulatedTxId) {
94         return FlowStatisticsToNotificationTransformer.transformToNotification(result, getDeviceInfo(), getOfVersion(), emulatedTxId);
95     }
96 }