e30988e1aa3d0e6014eaf190e60c66358d9ab639
[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 org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
14 import org.opendaylight.openflowplugin.api.OFConstants;
15 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
16 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
17 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
18 import org.opendaylight.openflowplugin.impl.registry.flow.FlowRegistryKeyFactory;
19 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.FlowStatsResponseConvertor;
20 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match.MatchReactor;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.GetFlowStatisticsInput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.GetFlowStatisticsOutput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.GetFlowStatisticsOutputBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsData;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsDataBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapListBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapListKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.statistics.FlowStatisticsBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyFlowCase;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.MultipartReplyFlow;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.MultipartRequestBody;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestFlowCaseBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.flow._case.MultipartRequestFlowBuilder;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45
46 /**
47  * The Flow direct statistics service.
48  */
49 public class FlowDirectStatisticsService extends AbstractDirectStatisticsService<GetFlowStatisticsInput, GetFlowStatisticsOutput> {
50     private final FlowStatsResponseConvertor flowStatsConvertor = new FlowStatsResponseConvertor();
51
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
107         if (success) {
108             for (final MultipartReply mpReply : input) {
109                 final MultipartReplyFlowCase caseBody = (MultipartReplyFlowCase) mpReply.getMultipartReplyBody();
110                 final MultipartReplyFlow replyBody = caseBody.getMultipartReplyFlow();
111
112                 final List<FlowAndStatisticsMapList> statsListPart = flowStatsConvertor.toSALFlowStatsList(replyBody.getFlowStats(), getDatapathId(), getOfVersion());
113
114                 for (final FlowAndStatisticsMapList part : statsListPart) {
115                     final org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowId flowId =
116                             new org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowId(generateFlowId(part).getValue());
117
118                     statsList.add(new FlowAndStatisticsMapListBuilder(part)
119                             .setKey(new FlowAndStatisticsMapListKey(flowId))
120                             .setFlowId(flowId)
121                             .build());
122                 }
123             }
124         }
125
126         return new GetFlowStatisticsOutputBuilder()
127                 .setFlowAndStatisticsMapList(statsList)
128                 .build();
129     }
130
131     @Override
132     protected void storeStatistics(GetFlowStatisticsOutput output) throws Exception {
133         final InstanceIdentifier<FlowCapableNode> nodePath = getDeviceInfo().getNodeInstanceIdentifier().augmentation(FlowCapableNode.class);
134
135         for (final FlowAndStatisticsMapList flowStatistics : output.getFlowAndStatisticsMapList()) {
136             final FlowId flowId = generateFlowId(flowStatistics);
137             final FlowKey flowKey = new FlowKey(flowId);
138
139             final FlowStatisticsDataBuilder flowStatisticsDataBld = new FlowStatisticsDataBuilder()
140                     .setFlowStatistics(new FlowStatisticsBuilder(flowStatistics).build());
141
142             final FlowBuilder flowBuilder = new FlowBuilder(flowStatistics)
143                     .addAugmentation(FlowStatisticsData.class, flowStatisticsDataBld.build())
144                     .setKey(flowKey);
145
146             final InstanceIdentifier<Flow> flowStatisticsPath = nodePath
147                     .child(Table.class, new TableKey(flowStatistics.getTableId()))
148                     .child(Flow.class, flowKey);
149
150             getTxFacade().writeToTransactionWithParentsSlow(LogicalDatastoreType.OPERATIONAL, flowStatisticsPath, flowBuilder.build());
151         }
152     }
153
154     private FlowId generateFlowId(FlowAndStatisticsMapList flowStatistics) {
155         final FlowStatisticsDataBuilder flowStatisticsDataBld = new FlowStatisticsDataBuilder()
156                 .setFlowStatistics(new FlowStatisticsBuilder(flowStatistics).build());
157
158         final FlowBuilder flowBuilder = new FlowBuilder(flowStatistics)
159                 .addAugmentation(FlowStatisticsData.class, flowStatisticsDataBld.build());
160
161         final FlowRegistryKey flowRegistryKey = FlowRegistryKeyFactory.create(flowBuilder.build());
162         return getDeviceRegistry().getDeviceFlowRegistry().storeIfNecessary(flowRegistryKey);
163     }
164 }