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