BUG-4118: Li:backward compatibility - rpcs - transformers
[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 org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
15 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
16 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.FlowStatsResponseConvertor;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdate;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdateBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.TransactionId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyFlowCase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.MultipartReplyFlow;
25
26 /**
27  * pulled out flow stats to notification transformation
28  */
29 public class FlowStatisticsToNotificationTransformer {
30
31     private static FlowStatsResponseConvertor flowStatsConvertor = new FlowStatsResponseConvertor();
32
33     /**
34      * @param mpResult      raw multipart response from device
35      * @param deviceContext device context
36      * @param ofVersion device version
37      * @param emulatedTxId
38      * @return notification containing flow stats
39      */
40     public static FlowsStatisticsUpdate transformToNotification(final List<MultipartReply> mpResult,
41                                                                 final DeviceContext deviceContext,
42                                                                 final OpenflowVersion ofVersion,
43                                                                 final TransactionId emulatedTxId) {
44         final FlowsStatisticsUpdateBuilder notification = new FlowsStatisticsUpdateBuilder();
45         final List<FlowAndStatisticsMapList> statsList = new ArrayList<>();
46         notification.setFlowAndStatisticsMapList(statsList);
47         notification.setMoreReplies(Boolean.FALSE);
48         notification.setTransactionId(emulatedTxId);
49
50         for (MultipartReply mpRawReply : mpResult) {
51             Preconditions.checkArgument(MultipartType.OFPMPFLOW.equals(mpRawReply.getType()));
52
53             MultipartReplyFlowCase caseBody = (MultipartReplyFlowCase) mpRawReply.getMultipartReplyBody();
54             MultipartReplyFlow replyBody = caseBody.getMultipartReplyFlow();
55             List<FlowAndStatisticsMapList> outStatsItem = flowStatsConvertor.toSALFlowStatsList(
56                     replyBody.getFlowStats(),
57                     deviceContext.getDeviceState().getFeatures().getDatapathId(),
58                     ofVersion);
59             statsList.addAll(outStatsItem);
60         }
61
62         return notification.build();
63     }
64 }