Bug 5540 - FlowConvertor, FlowStatsResponseConvertor, FlowInstructionResponseConvertor
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / statistics / services / direct / FlowDirectStatisticsService.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
9 package org.opendaylight.openflowplugin.impl.statistics.services.direct;
10
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.Optional;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.openflowplugin.api.OFConstants;
16 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
17 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
18 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
19 import org.opendaylight.openflowplugin.impl.registry.flow.FlowRegistryKeyFactory;
20 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManager;
21 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.data.VersionDatapathIdConvertorData;
22 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match.MatchReactor;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.GetFlowStatisticsInput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.GetFlowStatisticsOutput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.GetFlowStatisticsOutputBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsData;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsDataBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapListBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapListKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.statistics.FlowStatisticsBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyFlowCase;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.MultipartReplyFlow;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.MultipartRequestBody;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestFlowCaseBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.flow._case.MultipartRequestFlowBuilder;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
47
48 /**
49  * The Flow direct statistics service.
50  */
51 public class FlowDirectStatisticsService extends AbstractDirectStatisticsService<GetFlowStatisticsInput, GetFlowStatisticsOutput> {
52     /**
53      * Instantiates a new Flow direct statistics service.
54      *
55      * @param requestContextStack the request context stack
56      * @param deviceContext       the device context
57      */
58     public FlowDirectStatisticsService(RequestContextStack requestContextStack, DeviceContext deviceContext) {
59         super(MultipartType.OFPMPFLOW, requestContextStack, deviceContext);
60     }
61
62     @Override
63     protected MultipartRequestBody buildRequestBody(GetFlowStatisticsInput input) {
64         final MultipartRequestFlowBuilder mprFlowRequestBuilder = new MultipartRequestFlowBuilder();
65
66         if (input.getTableId() != null) {
67             mprFlowRequestBuilder.setTableId(input.getTableId());
68         } else {
69             mprFlowRequestBuilder.setTableId(OFConstants.OFPTT_ALL);
70         }
71
72         if (input.getOutPort() != null) {
73             mprFlowRequestBuilder.setOutPort(input.getOutPort().longValue());
74         } else {
75             mprFlowRequestBuilder.setOutPort(OFConstants.OFPP_ANY);
76         }
77
78         if (input.getOutGroup() != null) {
79             mprFlowRequestBuilder.setOutGroup(input.getOutGroup());
80         } else {
81             mprFlowRequestBuilder.setOutGroup(OFConstants.OFPG_ANY);
82         }
83
84         if (input.getCookie() != null) {
85             mprFlowRequestBuilder.setCookie(input.getCookie().getValue());
86         } else {
87             mprFlowRequestBuilder.setCookie(OFConstants.DEFAULT_COOKIE);
88         }
89
90         if (input.getCookieMask() != null) {
91             mprFlowRequestBuilder.setCookieMask(input.getCookieMask().getValue());
92         } else {
93             mprFlowRequestBuilder.setCookieMask(OFConstants.DEFAULT_COOKIE_MASK);
94         }
95
96         MatchReactor.getInstance().convert(input.getMatch(), getVersion(), mprFlowRequestBuilder);
97
98         return new MultipartRequestFlowCaseBuilder()
99                 .setMultipartRequestFlow(mprFlowRequestBuilder.build())
100                 .build();
101     }
102
103     @Override
104     protected GetFlowStatisticsOutput buildReply(List<MultipartReply> input, boolean success) {
105         final List<FlowAndStatisticsMapList> statsList = new ArrayList<>();
106         final VersionDatapathIdConvertorData data = new VersionDatapathIdConvertorData(getVersion());
107         data.setDatapathId(getDatapathId());
108
109         if (success) {
110             for (final MultipartReply mpReply : input) {
111                 final MultipartReplyFlowCase caseBody = (MultipartReplyFlowCase) mpReply.getMultipartReplyBody();
112                 final MultipartReplyFlow replyBody = caseBody.getMultipartReplyFlow();
113                 final Optional<List<FlowAndStatisticsMapList>> statsListPart = ConvertorManager.getInstance().convert(
114                         replyBody.getFlowStats(), data);
115
116                 if (statsListPart.isPresent()) {
117                     for (final FlowAndStatisticsMapList part : statsListPart.get()) {
118                         final org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowId flowId =
119                                 new org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowId(generateFlowId(part).getValue());
120
121                         statsList.add(new FlowAndStatisticsMapListBuilder(part)
122                                 .setKey(new FlowAndStatisticsMapListKey(flowId))
123                                 .setFlowId(flowId)
124                                 .build());
125                     }
126                 }
127             }
128         }
129
130         return new GetFlowStatisticsOutputBuilder()
131                 .setFlowAndStatisticsMapList(statsList)
132                 .build();
133     }
134
135     @Override
136     protected void storeStatistics(GetFlowStatisticsOutput output) throws Exception {
137         final InstanceIdentifier<FlowCapableNode> nodePath = getDeviceInfo().getNodeInstanceIdentifier().augmentation(FlowCapableNode.class);
138
139         for (final FlowAndStatisticsMapList flowStatistics : output.getFlowAndStatisticsMapList()) {
140             final FlowId flowId = generateFlowId(flowStatistics);
141             final FlowKey flowKey = new FlowKey(flowId);
142
143             final FlowStatisticsDataBuilder flowStatisticsDataBld = new FlowStatisticsDataBuilder()
144                     .setFlowStatistics(new FlowStatisticsBuilder(flowStatistics).build());
145
146             final FlowBuilder flowBuilder = new FlowBuilder(flowStatistics)
147                     .addAugmentation(FlowStatisticsData.class, flowStatisticsDataBld.build())
148                     .setKey(flowKey);
149
150             final InstanceIdentifier<Flow> flowStatisticsPath = nodePath
151                     .child(Table.class, new TableKey(flowStatistics.getTableId()))
152                     .child(Flow.class, flowKey);
153
154             getTxFacade().writeToTransactionWithParentsSlow(LogicalDatastoreType.OPERATIONAL, flowStatisticsPath, flowBuilder.build());
155         }
156     }
157
158     private FlowId generateFlowId(FlowAndStatisticsMapList flowStatistics) {
159         final FlowStatisticsDataBuilder flowStatisticsDataBld = new FlowStatisticsDataBuilder()
160                 .setFlowStatistics(new FlowStatisticsBuilder(flowStatistics).build());
161
162         final FlowBuilder flowBuilder = new FlowBuilder(flowStatistics)
163                 .addAugmentation(FlowStatisticsData.class, flowStatisticsDataBld.build());
164
165         final FlowRegistryKey flowRegistryKey = FlowRegistryKeyFactory.create(flowBuilder.build());
166         return getDeviceRegistry().getDeviceFlowRegistry().storeIfNecessary(flowRegistryKey);
167     }
168 }