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