67fa6fac2459bbeef60578e71b700e07319c5dbf
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / common / MultipartReplyTranslatorUtil.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.common;
10
11 import java.math.BigInteger;
12 import java.util.ArrayList;
13 import java.util.Collections;
14 import java.util.List;
15 import java.util.Optional;
16 import java.util.stream.Collectors;
17 import javax.annotation.Nullable;
18 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
19 import org.opendaylight.openflowplugin.api.openflow.device.MessageTranslator;
20 import org.opendaylight.openflowplugin.api.openflow.device.TranslatorLibrary;
21 import org.opendaylight.openflowplugin.api.openflow.md.core.TranslatorKey;
22 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
23 import org.opendaylight.openflowplugin.extension.api.path.MatchPath;
24 import org.opendaylight.openflowplugin.impl.util.GroupUtil;
25 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
26 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.data.FlowStatsResponseConvertorData;
27 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.data.VersionConvertorData;
28 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.data.VersionDatapathIdConvertorData;
29 import org.opendaylight.openflowplugin.openflow.md.util.InventoryDataServiceUtil;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Counter32;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Counter64;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.multipart.reply.multipart.reply.body.MultipartReplyDescBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.multipart.reply.multipart.reply.body.MultipartReplyFlowAggregateStats;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.multipart.reply.multipart.reply.body.MultipartReplyFlowAggregateStatsBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.multipart.reply.multipart.reply.body.MultipartReplyFlowStats;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.multipart.reply.multipart.reply.body.MultipartReplyFlowStatsBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.and.statistics.map.FlowTableAndStatisticsMap;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.and.statistics.map.FlowTableAndStatisticsMapBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.multipart.reply.multipart.reply.body.MultipartReplyFlowTableStats;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.multipart.reply.multipart.reply.body.MultipartReplyFlowTableStatsBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.multipart.reply.multipart.reply.body.MultipartReplyPortDesc;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.multipart.reply.multipart.reply.body.MultipartReplyPortDescBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.multipart.reply.multipart.reply.body.multipart.reply.port.desc.PortsBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.queue.rev130925.QueueId;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.multipart.reply.multipart.reply.body.MultipartReplyGroupDescBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.multipart.reply.multipart.reply.body.MultipartReplyGroupFeaturesBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.multipart.reply.multipart.reply.body.MultipartReplyGroupStats;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.multipart.reply.multipart.reply.body.MultipartReplyGroupStatsBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.Chaining;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.ChainingChecks;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupAll;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupCapability;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupFf;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupIndirect;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupSelect;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupType;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.SelectLiveness;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.SelectWeight;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.desc.stats.reply.GroupDescStats;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.statistics.reply.GroupStats;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.multipart.reply.multipart.reply.body.MultipartReplyMeterConfigBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.multipart.reply.multipart.reply.body.MultipartReplyMeterFeaturesBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.multipart.reply.multipart.reply.body.MultipartReplyMeterStats;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.multipart.reply.multipart.reply.body.MultipartReplyMeterStatsBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterBand;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterBandDrop;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterBandDscpRemark;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterBurst;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterCapability;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterKbps;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterPktps;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterStats;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.meter.config.stats.reply.MeterConfigStats;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.statistics.types.rev130925.duration.DurationBuilder;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.statistics.types.rev130925.node.connector.statistics.BytesBuilder;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.statistics.types.rev130925.node.connector.statistics.PacketsBuilder;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.multipart.types.rev170112.multipart.reply.MultipartReplyBody;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortGrouping;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyAggregateCase;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyDescCase;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyFlowCase;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyGroupCase;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyGroupDescCase;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyGroupFeaturesCase;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyMeterCase;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyMeterConfigCase;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyMeterFeaturesCase;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyPortDescCase;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyPortStatsCase;
94 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyQueueCase;
95 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyTableCase;
96 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyTableFeaturesCase;
97 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.aggregate._case.MultipartReplyAggregate;
98 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.desc._case.MultipartReplyDesc;
99 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.MultipartReplyFlow;
100 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.group._case.MultipartReplyGroup;
101 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.group.desc._case.MultipartReplyGroupDesc;
102 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.group.features._case.MultipartReplyGroupFeatures;
103 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.meter._case.MultipartReplyMeter;
104 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.meter.config._case.MultipartReplyMeterConfig;
105 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.meter.features._case.MultipartReplyMeterFeatures;
106 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.port.stats._case.MultipartReplyPortStats;
107 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.port.stats._case.multipart.reply.port.stats.PortStats;
108 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.queue._case.MultipartReplyQueue;
109 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.queue._case.multipart.reply.queue.QueueStats;
110 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.table._case.MultipartReplyTable;
111 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.table._case.multipart.reply.table.TableStats;
112 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.table.features._case.MultipartReplyTableFeatures;
113 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.multipart.reply.multipart.reply.body.MultipartReplyPortStatsBuilder;
114 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.node.connector.statistics.and.port.number.map.NodeConnectorStatisticsAndPortNumberMap;
115 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.node.connector.statistics.and.port.number.map.NodeConnectorStatisticsAndPortNumberMapBuilder;
116 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.node.connector.statistics.and.port.number.map.NodeConnectorStatisticsAndPortNumberMapKey;
117 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.multipart.reply.multipart.reply.body.MultipartReplyQueueStats;
118 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.multipart.reply.multipart.reply.body.MultipartReplyQueueStatsBuilder;
119 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.queue.id.and.statistics.map.QueueIdAndStatisticsMap;
120 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.queue.id.and.statistics.map.QueueIdAndStatisticsMapBuilder;
121 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TableId;
122 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.multipart.reply.multipart.reply.body.MultipartReplyTableFeaturesBuilder;
123 import org.slf4j.Logger;
124 import org.slf4j.LoggerFactory;
125
126 /**
127  * Class converts multipart reply messages to the objects that can be then written to datastore using
128  * multipart writers.
129  */
130 public class MultipartReplyTranslatorUtil {
131     private static final Logger LOG = LoggerFactory.getLogger(MultipartReplyTranslatorUtil.class);
132
133     public static Optional<? extends MultipartReplyBody> translate(
134             final OfHeader message,
135             final DeviceInfo deviceInfo,
136             @Nullable final ConvertorExecutor convertorExecutor,
137             @Nullable final TranslatorLibrary translatorLibrary) {
138
139         if (message instanceof MultipartReply) {
140             final Optional<ConvertorExecutor> convertor = Optional.ofNullable(convertorExecutor);
141             final Optional<TranslatorLibrary> translator = Optional.ofNullable(translatorLibrary);
142             final MultipartReply msg = MultipartReply.class.cast(message);
143             final OpenflowVersion ofVersion = OpenflowVersion.get(deviceInfo.getVersion());
144             final VersionDatapathIdConvertorData data = new VersionDatapathIdConvertorData(deviceInfo.getVersion());
145             data.setDatapathId(deviceInfo.getDatapathId());
146
147             switch (msg.getType()) {
148                 case OFPMPFLOW:
149                     return convertor.flatMap(c -> Optional.of(translateFlow(msg, data, c)));
150                 case OFPMPAGGREGATE:
151                     return Optional.of(translateAggregate(msg));
152                 case OFPMPPORTSTATS:
153                     return Optional.of(translatePortStats(msg, ofVersion, deviceInfo.getDatapathId()));
154                 case OFPMPGROUP:
155                     return convertor.flatMap(c -> Optional.of(translateGroup(msg, data, c)));
156                 case OFPMPGROUPDESC:
157                     return convertor.flatMap(c -> Optional.of(translateGroupDesc(msg, data, c)));
158                 case OFPMPGROUPFEATURES:
159                     return Optional.of(translateGroupFeatures(msg));
160                 case OFPMPMETER:
161                     return convertor.flatMap(c -> Optional.of(translateMeter(msg, data, c)));
162                 case OFPMPMETERCONFIG:
163                     return convertor.flatMap(c -> Optional.of(translateMeterConfig(msg, data, c)));
164                 case OFPMPMETERFEATURES:
165                     return Optional.of(translateMeterFeatures(msg));
166                 case OFPMPTABLE:
167                     return Optional.of(translateTable(msg));
168                 case OFPMPQUEUE:
169                     return Optional.of(translateQueue(msg, ofVersion, deviceInfo.getDatapathId()));
170                 case OFPMPDESC:
171                     return Optional.of(translateDesc(msg));
172                 case OFPMPTABLEFEATURES:
173                     return convertor.flatMap(c -> Optional.of(translateTableFeatures(msg, deviceInfo.getVersion(), c)));
174                 case OFPMPPORTDESC:
175                     return translator.flatMap(t -> Optional.of(translatePortDesc(msg, deviceInfo, t)));
176                 default:
177             }
178         } else if (message instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.multipart.types.rev170112
179             .MultipartReply) {
180             return Optional.of(org.opendaylight.yang.gen.v1.urn.opendaylight.multipart.types.rev170112
181                 .MultipartReply.class.cast(message).getMultipartReplyBody());
182         }
183
184         LOG.debug("Failed to translate {} for node {}.", message.getImplementedInterface(), deviceInfo);
185         return Optional.empty();
186     }
187
188     private static MultipartReplyPortDesc translatePortDesc(final MultipartReply msg,
189                                                             final DeviceInfo deviceInfo,
190                                                             final TranslatorLibrary translatorLibrary) {
191         return new MultipartReplyPortDescBuilder()
192             .setPorts(((MultipartReplyPortDescCase) msg.getMultipartReplyBody())
193                 .getMultipartReplyPortDesc()
194                 .getPorts()
195                 .stream()
196                 .map(port -> {
197                     final TranslatorKey translatorKey = new TranslatorKey(
198                         deviceInfo.getVersion(),
199                         PortGrouping.class.getName());
200
201                     final MessageTranslator<PortGrouping, FlowCapableNodeConnector> translator = translatorLibrary
202                         .lookupTranslator(translatorKey);
203
204                     return new PortsBuilder(translator
205                         .translate(port, deviceInfo, null))
206                         .build();
207                 })
208                 .collect(Collectors.toList()))
209             .build();
210     }
211
212     private static org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.multipart.reply.multipart
213             .reply.body.MultipartReplyTableFeatures translateTableFeatures(
214                     final MultipartReply msg,
215                     final short version,
216                     final ConvertorExecutor convertorExecutor) {
217         MultipartReplyTableFeaturesCase caseBody = (MultipartReplyTableFeaturesCase) msg.getMultipartReplyBody();
218         final MultipartReplyTableFeatures multipartReplyTableFeatures = caseBody.getMultipartReplyTableFeatures();
219         final Optional<List<org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features
220             .TableFeatures>> tableFeaturesList = convertorExecutor
221             .convert(multipartReplyTableFeatures, new VersionConvertorData(version));
222
223         return new MultipartReplyTableFeaturesBuilder()
224             .setTableFeatures(tableFeaturesList.orElse(Collections.emptyList()))
225             .build();
226     }
227
228     private static org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.multipart.reply.multipart
229             .reply.body.MultipartReplyDesc translateDesc(final MultipartReply msg) {
230         final MultipartReplyDesc desc = ((MultipartReplyDescCase) msg.getMultipartReplyBody()).getMultipartReplyDesc();
231
232         return new MultipartReplyDescBuilder()
233             .setDescription(desc.getDpDesc())
234             .setHardware(desc.getHwDesc())
235             .setManufacturer(desc.getMfrDesc())
236             .setSoftware(desc.getSwDesc())
237             .setSerialNumber(desc.getSerialNum())
238             .build();
239     }
240
241     private static MultipartReplyFlowStats translateFlow(final MultipartReply msg,
242                                                          final VersionDatapathIdConvertorData data,
243                                                          final ConvertorExecutor convertorExecutor) {
244         FlowStatsResponseConvertorData flowData = new FlowStatsResponseConvertorData(data.getVersion());
245         flowData.setDatapathId(data.getDatapathId());
246         flowData.setMatchPath(MatchPath.FLOWSSTATISTICSUPDATE_FLOWANDSTATISTICSMAPLIST_MATCH);
247         MultipartReplyFlowStatsBuilder message = new MultipartReplyFlowStatsBuilder();
248         MultipartReplyFlowCase caseBody = (MultipartReplyFlowCase) msg.getMultipartReplyBody();
249         MultipartReplyFlow replyBody = caseBody.getMultipartReplyFlow();
250         final Optional<List<FlowAndStatisticsMapList>> flowAndStatisticsMapLists =
251             convertorExecutor.convert(replyBody.getFlowStats(), flowData);
252
253         message.setFlowAndStatisticsMapList(flowAndStatisticsMapLists.orElse(Collections.emptyList()));
254         return message.build();
255     }
256
257     private static MultipartReplyFlowAggregateStats translateAggregate(final MultipartReply msg) {
258         MultipartReplyFlowAggregateStatsBuilder message = new MultipartReplyFlowAggregateStatsBuilder();
259         MultipartReplyAggregateCase caseBody = (MultipartReplyAggregateCase) msg.getMultipartReplyBody();
260         MultipartReplyAggregate replyBody = caseBody.getMultipartReplyAggregate();
261         message.setByteCount(new Counter64(replyBody.getByteCount()));
262         message.setPacketCount(new Counter64(replyBody.getPacketCount()));
263         message.setFlowCount(new Counter32(replyBody.getFlowCount()));
264         return message.build();
265     }
266
267     private static org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.multipart.reply.multipart
268             .reply.body.MultipartReplyPortStats translatePortStats(
269                     final MultipartReply msg,
270                     final OpenflowVersion ofVersion,
271                     final BigInteger datapathId) {
272         MultipartReplyPortStatsBuilder message = new MultipartReplyPortStatsBuilder();
273         MultipartReplyPortStatsCase caseBody = (MultipartReplyPortStatsCase) msg.getMultipartReplyBody();
274         MultipartReplyPortStats replyBody = caseBody.getMultipartReplyPortStats();
275
276         List<NodeConnectorStatisticsAndPortNumberMap> statsMap =
277             new ArrayList<>();
278         for (PortStats portStats : replyBody.getPortStats()) {
279
280             NodeConnectorStatisticsAndPortNumberMapBuilder statsBuilder =
281                 new NodeConnectorStatisticsAndPortNumberMapBuilder();
282             statsBuilder.setNodeConnectorId(
283                 InventoryDataServiceUtil.nodeConnectorIdfromDatapathPortNo(datapathId,
284                     portStats.getPortNo(), ofVersion));
285
286             BytesBuilder bytesBuilder = new BytesBuilder();
287             bytesBuilder.setReceived(portStats.getRxBytes());
288             bytesBuilder.setTransmitted(portStats.getTxBytes());
289             statsBuilder.setBytes(bytesBuilder.build());
290
291             PacketsBuilder packetsBuilder = new PacketsBuilder();
292             packetsBuilder.setReceived(portStats.getRxPackets());
293             packetsBuilder.setTransmitted(portStats.getTxPackets());
294             statsBuilder.setPackets(packetsBuilder.build());
295
296             DurationBuilder durationBuilder = new DurationBuilder();
297             if (portStats.getDurationSec() != null) {
298                 durationBuilder.setSecond(new Counter32(portStats.getDurationSec()));
299             }
300             if (portStats.getDurationNsec() != null) {
301                 durationBuilder.setNanosecond(new Counter32(portStats.getDurationNsec()));
302             }
303             statsBuilder.setDuration(durationBuilder.build());
304             statsBuilder.setCollisionCount(portStats.getCollisions());
305             statsBuilder.setKey(new NodeConnectorStatisticsAndPortNumberMapKey(statsBuilder.getNodeConnectorId()));
306             statsBuilder.setReceiveCrcError(portStats.getRxCrcErr());
307             statsBuilder.setReceiveDrops(portStats.getRxDropped());
308             statsBuilder.setReceiveErrors(portStats.getRxErrors());
309             statsBuilder.setReceiveFrameError(portStats.getRxFrameErr());
310             statsBuilder.setReceiveOverRunError(portStats.getRxOverErr());
311             statsBuilder.setTransmitDrops(portStats.getTxDropped());
312             statsBuilder.setTransmitErrors(portStats.getTxErrors());
313
314             statsMap.add(statsBuilder.build());
315         }
316         message.setNodeConnectorStatisticsAndPortNumberMap(statsMap);
317
318
319         return message.build();
320     }
321
322     private static MultipartReplyGroupStats translateGroup(final MultipartReply msg,
323                                                            final VersionDatapathIdConvertorData data,
324                                                            final ConvertorExecutor convertorExecutor) {
325         MultipartReplyGroupStatsBuilder message = new MultipartReplyGroupStatsBuilder();
326         MultipartReplyGroupCase caseBody = (MultipartReplyGroupCase) msg.getMultipartReplyBody();
327         MultipartReplyGroup replyBody = caseBody.getMultipartReplyGroup();
328         final Optional<List<GroupStats>> groupStatsList = convertorExecutor.convert(
329             replyBody.getGroupStats(), data);
330
331         message.setGroupStats(groupStatsList.orElse(Collections.emptyList()));
332
333         return message.build();
334     }
335
336     private static org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.multipart.reply.multipart
337             .reply.body.MultipartReplyGroupDesc translateGroupDesc(
338                     final MultipartReply msg,
339                     final VersionDatapathIdConvertorData data,
340                     final ConvertorExecutor convertorExecutor) {
341         MultipartReplyGroupDescBuilder message = new MultipartReplyGroupDescBuilder();
342         MultipartReplyGroupDescCase caseBody = (MultipartReplyGroupDescCase) msg.getMultipartReplyBody();
343         MultipartReplyGroupDesc replyBody = caseBody.getMultipartReplyGroupDesc();
344
345         final Optional<List<GroupDescStats>> groupDescStatsList = convertorExecutor.convert(
346             replyBody.getGroupDesc(), data);
347
348         message.setGroupDescStats(groupDescStatsList.orElse(Collections.emptyList()));
349
350         return message.build();
351     }
352
353     private static org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.multipart.reply.multipart
354             .reply.body.MultipartReplyGroupFeatures translateGroupFeatures(final MultipartReply msg) {
355         final MultipartReplyGroupFeaturesBuilder message = new MultipartReplyGroupFeaturesBuilder();
356         final MultipartReplyGroupFeaturesCase caseBody = (MultipartReplyGroupFeaturesCase) msg.getMultipartReplyBody();
357         final MultipartReplyGroupFeatures replyBody = caseBody.getMultipartReplyGroupFeatures();
358         List<Class<? extends GroupType>> supportedGroups =
359             new ArrayList<>();
360
361         if (replyBody.getTypes().isOFPGTALL()) {
362             supportedGroups.add(GroupAll.class);
363         }
364         if (replyBody.getTypes().isOFPGTSELECT()) {
365             supportedGroups.add(GroupSelect.class);
366         }
367         if (replyBody.getTypes().isOFPGTINDIRECT()) {
368             supportedGroups.add(GroupIndirect.class);
369         }
370         if (replyBody.getTypes().isOFPGTFF()) {
371             supportedGroups.add(GroupFf.class);
372         }
373         message.setGroupTypesSupported(supportedGroups);
374         message.setMaxGroups(replyBody.getMaxGroups());
375
376         List<Class<? extends GroupCapability>> supportedCapabilities =
377             new ArrayList<>();
378
379         if (replyBody.getCapabilities().isOFPGFCCHAINING()) {
380             supportedCapabilities.add(Chaining.class);
381         }
382         if (replyBody.getCapabilities().isOFPGFCCHAININGCHECKS()) {
383             supportedCapabilities.add(ChainingChecks.class);
384         }
385         if (replyBody.getCapabilities().isOFPGFCSELECTLIVENESS()) {
386             supportedCapabilities.add(SelectLiveness.class);
387         }
388         if (replyBody.getCapabilities().isOFPGFCSELECTWEIGHT()) {
389             supportedCapabilities.add(SelectWeight.class);
390         }
391
392         message.setGroupCapabilitiesSupported(supportedCapabilities);
393
394         message.setActions(GroupUtil.extractGroupActionsSupportBitmap(replyBody.getActionsBitmap()));
395         return message.build();
396     }
397
398     private static MultipartReplyMeterStats translateMeter(final MultipartReply msg,
399                                                            final VersionDatapathIdConvertorData data,
400                                                            final ConvertorExecutor convertorExecutor) {
401         MultipartReplyMeterStatsBuilder message = new MultipartReplyMeterStatsBuilder();
402         MultipartReplyMeterCase caseBody = (MultipartReplyMeterCase) msg.getMultipartReplyBody();
403         MultipartReplyMeter replyBody = caseBody.getMultipartReplyMeter();
404         final Optional<List<
405                 org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.meter.statistics.reply.MeterStats
406                 >>
407                 meterStatsList = convertorExecutor.convert(replyBody.getMeterStats(), data);
408
409         message.setMeterStats(meterStatsList.orElse(Collections.emptyList()));
410
411         return message.build();
412     }
413
414     private static org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.multipart.reply.multipart
415             .reply.body.MultipartReplyMeterConfig translateMeterConfig(
416                     final MultipartReply msg,
417                     final VersionDatapathIdConvertorData data,
418                     final ConvertorExecutor convertorExecutor) {
419         MultipartReplyMeterConfigBuilder message = new MultipartReplyMeterConfigBuilder();
420         MultipartReplyMeterConfigCase caseBody = (MultipartReplyMeterConfigCase) msg.getMultipartReplyBody();
421         MultipartReplyMeterConfig replyBody = caseBody.getMultipartReplyMeterConfig();
422         final Optional<List<MeterConfigStats>> meterConfigStatsList
423                 = convertorExecutor.convert(replyBody.getMeterConfig(), data);
424
425         message.setMeterConfigStats(meterConfigStatsList.orElse(Collections.emptyList()));
426
427         return message.build();
428     }
429
430     private static org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.multipart.reply.multipart
431             .reply.body.MultipartReplyMeterFeatures translateMeterFeatures(final MultipartReply msg) {
432         MultipartReplyMeterFeaturesBuilder message = new MultipartReplyMeterFeaturesBuilder();
433         MultipartReplyMeterFeaturesCase caseBody = (MultipartReplyMeterFeaturesCase) msg.getMultipartReplyBody();
434         MultipartReplyMeterFeatures replyBody = caseBody.getMultipartReplyMeterFeatures();
435         message.setMaxBands(replyBody.getMaxBands());
436         message.setMaxColor(replyBody.getMaxColor());
437         message.setMaxMeter(new Counter32(replyBody.getMaxMeter()));
438
439         List<Class<? extends MeterCapability>> supportedCapabilities =
440             new ArrayList<>();
441         if (replyBody.getCapabilities().isOFPMFBURST()) {
442             supportedCapabilities.add(MeterBurst.class);
443         }
444         if (replyBody.getCapabilities().isOFPMFKBPS()) {
445             supportedCapabilities.add(MeterKbps.class);
446
447         }
448         if (replyBody.getCapabilities().isOFPMFPKTPS()) {
449             supportedCapabilities.add(MeterPktps.class);
450
451         }
452         if (replyBody.getCapabilities().isOFPMFSTATS()) {
453             supportedCapabilities.add(MeterStats.class);
454
455         }
456         message.setMeterCapabilitiesSupported(supportedCapabilities);
457
458         List<Class<? extends MeterBand>> supportedMeterBand =
459             new ArrayList<>();
460         if (replyBody.getBandTypes().isOFPMBTDROP()) {
461             supportedMeterBand.add(MeterBandDrop.class);
462         }
463         if (replyBody.getBandTypes().isOFPMBTDSCPREMARK()) {
464             supportedMeterBand.add(MeterBandDscpRemark.class);
465         }
466         message.setMeterBandSupported(supportedMeterBand);
467         return message.build();
468     }
469
470     private static MultipartReplyFlowTableStats translateTable(final MultipartReply msg) {
471         MultipartReplyFlowTableStatsBuilder message = new MultipartReplyFlowTableStatsBuilder();
472         MultipartReplyTableCase caseBody = (MultipartReplyTableCase) msg.getMultipartReplyBody();
473         MultipartReplyTable replyBody = caseBody.getMultipartReplyTable();
474         List<TableStats> swTablesStats = replyBody.getTableStats();
475
476         List<FlowTableAndStatisticsMap> salFlowStats = new ArrayList<>();
477         //TODO: Duplicate code: look at OpendaylightFlowTableStatisticsServiceImpl method transformToNotification
478         for (TableStats swTableStats : swTablesStats) {
479             FlowTableAndStatisticsMapBuilder statisticsBuilder = new FlowTableAndStatisticsMapBuilder();
480
481             statisticsBuilder.setActiveFlows(new Counter32(swTableStats.getActiveCount()));
482             statisticsBuilder.setPacketsLookedUp(new Counter64(swTableStats.getLookupCount()));
483             statisticsBuilder.setPacketsMatched(new Counter64(swTableStats.getMatchedCount()));
484             statisticsBuilder.setTableId(new TableId(swTableStats.getTableId()));
485             salFlowStats.add(statisticsBuilder.build());
486         }
487
488         message.setFlowTableAndStatisticsMap(salFlowStats);
489         return message.build();
490     }
491
492     private static MultipartReplyQueueStats translateQueue(final MultipartReply msg,
493                                                            final OpenflowVersion ofVersion,
494                                                            final BigInteger datapathId) {
495         MultipartReplyQueueStatsBuilder message = new MultipartReplyQueueStatsBuilder();
496         MultipartReplyQueueCase caseBody = (MultipartReplyQueueCase) msg.getMultipartReplyBody();
497         MultipartReplyQueue replyBody = caseBody.getMultipartReplyQueue();
498
499         List<QueueIdAndStatisticsMap> statsMap =
500             new ArrayList<>();
501
502         for (QueueStats queueStats : replyBody.getQueueStats()) {
503
504             QueueIdAndStatisticsMapBuilder statsBuilder =
505                 new QueueIdAndStatisticsMapBuilder();
506             statsBuilder.setNodeConnectorId(
507                 InventoryDataServiceUtil.nodeConnectorIdfromDatapathPortNo(datapathId,
508                     queueStats.getPortNo(), ofVersion));
509             statsBuilder.setTransmissionErrors(new Counter64(queueStats.getTxErrors()));
510             statsBuilder.setTransmittedBytes(new Counter64(queueStats.getTxBytes()));
511             statsBuilder.setTransmittedPackets(new Counter64(queueStats.getTxPackets()));
512
513             DurationBuilder durationBuilder = new DurationBuilder();
514             durationBuilder.setSecond(new Counter32(queueStats.getDurationSec()));
515             durationBuilder.setNanosecond(new Counter32(queueStats.getDurationNsec()));
516             statsBuilder.setDuration(durationBuilder.build());
517
518             statsBuilder.setQueueId(new QueueId(queueStats.getQueueId()));
519             statsBuilder.setNodeConnectorId(InventoryDataServiceUtil.nodeConnectorIdfromDatapathPortNo(datapathId,
520                 queueStats.getPortNo(), ofVersion));
521
522             statsMap.add(statsBuilder.build());
523         }
524         message.setQueueIdAndStatisticsMap(statsMap);
525
526         return message.build();
527     }
528
529 }