Bug 5540 - FlowConvertor, FlowStatsResponseConvertor, FlowInstructionResponseConvertor
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / statistics / services / compatibility / FlowStatisticsToNotificationTransformer.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.compatibility;
10
11 import com.google.common.base.Preconditions;
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.Optional;
15 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
16 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
17 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManager;
18 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.data.VersionDatapathIdConvertorData;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdate;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdateBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.TransactionId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyFlowCase;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.MultipartReplyFlow;
27
28 /**
29  * pulled out flow stats to notification transformation
30  */
31 public class FlowStatisticsToNotificationTransformer {
32     /**
33      * @param mpResult      raw multipart response from device
34      * @param deviceInfo   device state
35      * @param ofVersion     device version
36      * @param emulatedTxId
37      * @return notification containing flow stats
38      */
39     public static FlowsStatisticsUpdate transformToNotification(final List<MultipartReply> mpResult,
40                                                                 final DeviceInfo deviceInfo,
41                                                                 final OpenflowVersion ofVersion,
42                                                                 final TransactionId emulatedTxId) {
43         final VersionDatapathIdConvertorData data = new VersionDatapathIdConvertorData(ofVersion.getVersion());
44         data.setDatapathId(deviceInfo.getDatapathId());
45         final FlowsStatisticsUpdateBuilder notification = new FlowsStatisticsUpdateBuilder();
46         final List<FlowAndStatisticsMapList> statsList = new ArrayList<>();
47         notification.setId(deviceInfo.getNodeId());
48         notification.setFlowAndStatisticsMapList(statsList);
49         notification.setMoreReplies(Boolean.FALSE);
50         notification.setTransactionId(emulatedTxId);
51
52         for (MultipartReply mpRawReply : mpResult) {
53             Preconditions.checkArgument(MultipartType.OFPMPFLOW.equals(mpRawReply.getType()));
54
55             MultipartReplyFlowCase caseBody = (MultipartReplyFlowCase) mpRawReply.getMultipartReplyBody();
56             MultipartReplyFlow replyBody = caseBody.getMultipartReplyFlow();
57             final Optional<List<FlowAndStatisticsMapList>> outStatsItem =
58                     ConvertorManager.getInstance().convert(replyBody.getFlowStats(), data);
59
60
61             if (outStatsItem.isPresent()) {
62                 statsList.addAll(outStatsItem.get());
63             }
64         }
65
66         return notification.build();
67     }
68 }