Bug 6110: Fixed bugs in statistics manager due to race condition.
[openflowplugin.git] / extension / openflowplugin-extension-nicira / src / main / java / org / opendaylight / openflowplugin / extension / vendor / nicira / convertor / action / MultipathConvertor.java
1 /*
2  * Copyright (c) 2014, 2015 SDN Hub, LLC. 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.extension.vendor.nicira.convertor.action;
10
11 import com.google.common.base.Preconditions;
12 import org.opendaylight.openflowplugin.extension.api.ConvertorActionFromOFJava;
13 import org.opendaylight.openflowplugin.extension.api.ConvertorActionToOFJava;
14 import org.opendaylight.openflowplugin.extension.api.path.ActionPath;
15 import org.opendaylight.openflowplugin.extension.vendor.nicira.convertor.CodecPreconditionException;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.action.container.action.choice.ActionMultipath;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.action.container.action.choice.ActionMultipathBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.ofj.nx.action.multipath.grouping.NxActionMultipath;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.ofj.nx.action.multipath.grouping.NxActionMultipathBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.NxActionMultipathGrouping;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.flows.statistics.update.flow.and.statistics.map.list.instructions.instruction.instruction.apply.actions._case.apply.actions.action.action.NxActionMultipathNotifFlowsStatisticsUpdateApplyActionsCaseBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.flows.statistics.update.flow.and.statistics.map.list.instructions.instruction.instruction.write.actions._case.write.actions.action.action.NxActionMultipathNotifFlowsStatisticsUpdateWriteActionsCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.group.desc.stats.updated.group.desc.stats.buckets.bucket.action.action.NxActionMultipathNotifGroupDescStatsUpdatedCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nodes.node.table.flow.instructions.instruction.instruction.write.actions._case.write.actions.action.action.NxActionMultipathNodesNodeTableFlowWriteActionsCaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.multipath.grouping.NxMultipath;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.multipath.grouping.NxMultipathBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.multipath.grouping.nx.multipath.Dst;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.multipath.grouping.nx.multipath.DstBuilder;
30
31 public class MultipathConvertor implements
32         ConvertorActionToOFJava<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action, Action>,
33         ConvertorActionFromOFJava<Action, ActionPath> {
34
35     @Override
36     public org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action convert(final Action input, final ActionPath path) {
37         NxActionMultipath action = ((ActionMultipath) input.getActionChoice()).getNxActionMultipath();
38         DstBuilder dstBuilder = new DstBuilder();
39         dstBuilder.setDstChoice(RegMoveConvertor.resolveDst(action.getDst()));
40         dstBuilder.setStart(resolveStart(action.getOfsNbits()));
41         dstBuilder.setEnd(resolveEnd(action.getOfsNbits()));
42         NxMultipathBuilder builder = new NxMultipathBuilder();
43         builder.setBasis(action.getBasis());
44         builder.setAlgorithm(action.getAlgorithm());
45         builder.setMaxLink(action.getMaxLink());
46         builder.setArg(action.getArg());
47         builder.setDst(dstBuilder.build());
48         return resolveAction(builder.build(), path);
49     }
50
51     private static int resolveStart(final int ofsNBints) {
52         return extractSub(ofsNBints, 10, 6);
53     }
54
55     private static int resolveEnd(final int ofsNBints) {
56         int ofs = extractSub(ofsNBints, 10, 6);
57         int nBits = extractSub(ofsNBints, 6, 0);
58         return ofs + nBits;
59     }
60
61     private static int extractSub(final int l, final int nrBits, final int offset) {
62         final int rightShifted = l >>> offset;
63         final int mask = (1 << nrBits) - 1;
64         return rightShifted & mask;
65     }
66
67     private static org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action resolveAction(final NxMultipath value, final ActionPath path) {
68         switch (path) {
69             case NODES_NODE_TABLE_FLOW_INSTRUCTIONS_INSTRUCTION_WRITEACTIONSCASE_WRITEACTIONS_ACTION_ACTION_EXTENSIONLIST_EXTENSION:
70                 return new NxActionMultipathNodesNodeTableFlowWriteActionsCaseBuilder().setNxMultipath(value).build();
71             case FLOWSSTATISTICSUPDATE_FLOWANDSTATISTICSMAPLIST_INSTRUCTIONS_INSTRUCTION_INSTRUCTION_WRITEACTIONSCASE_WRITEACTIONS_ACTION_ACTION:
72                 return new NxActionMultipathNotifFlowsStatisticsUpdateWriteActionsCaseBuilder().setNxMultipath(value).build();
73             case FLOWSSTATISTICSUPDATE_FLOWANDSTATISTICSMAPLIST_INSTRUCTIONS_INSTRUCTION_INSTRUCTION_APPLYACTIONSCASE_APPLYACTIONS_ACTION_ACTION:
74                 return new NxActionMultipathNotifFlowsStatisticsUpdateApplyActionsCaseBuilder().setNxMultipath(value).build();
75             case GROUPDESCSTATSUPDATED_GROUPDESCSTATS_BUCKETS_BUCKET_ACTION:
76                 return new NxActionMultipathNotifGroupDescStatsUpdatedCaseBuilder().setNxMultipath(value).build();
77             default:
78                 throw new CodecPreconditionException(path);
79         }
80     }
81
82     @Override
83     public Action convert(final org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action nxActionArg) {
84         Preconditions.checkArgument(nxActionArg instanceof NxActionMultipathGrouping);
85         NxActionMultipathGrouping nxAction = (NxActionMultipathGrouping) nxActionArg;
86
87         NxActionMultipathBuilder nxActionMultipathBuilder = new NxActionMultipathBuilder();
88         nxActionMultipathBuilder.setFields(nxAction.getNxMultipath().getFields());
89         nxActionMultipathBuilder.setBasis(nxAction.getNxMultipath().getBasis());
90         nxActionMultipathBuilder.setAlgorithm(nxAction.getNxMultipath().getAlgorithm());
91         nxActionMultipathBuilder.setMaxLink(nxAction.getNxMultipath().getMaxLink());
92         nxActionMultipathBuilder.setArg(nxAction.getNxMultipath().getArg());
93         Dst dst = nxAction.getNxMultipath().getDst();
94         nxActionMultipathBuilder.setOfsNbits((dst.getStart() << 6) | (dst.getEnd() - dst.getStart()));
95         nxActionMultipathBuilder.setDst(RegMoveConvertor.resolveDst(dst.getDstChoice()));
96         ActionMultipathBuilder actionMultipathBuilder = new ActionMultipathBuilder();
97         actionMultipathBuilder.setNxActionMultipath(nxActionMultipathBuilder.build());
98         return ActionUtil.createAction(actionMultipathBuilder.build());
99     }
100
101 }