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