Extension support - easy lookup for toOFJava
[openflowplugin.git] / extension / openflowplugin-extension-nicira / src / main / java / org / opendaylight / openflowplugin / extension / vendor / nicira / convertor / action / RegLoadConvertor.java
1 /**
2  * Copyright (c) 2014 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 package org.opendaylight.openflowplugin.extension.vendor.nicira.convertor.action;
9
10 import org.opendaylight.openflowplugin.extension.api.ConvertorActionFromOFJava;
11 import org.opendaylight.openflowplugin.extension.api.ConvertorActionToOFJava;
12 import org.opendaylight.openflowplugin.extension.api.path.ActionPath;
13 import org.opendaylight.openflowplugin.extension.vendor.nicira.convertor.CodecPreconditionException;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.Action;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.NxmNxRegLoad;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.OfjAugNxAction;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.OfjAugNxActionBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.ofj.nx.action.reg.load.grouping.ActionRegLoad;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.ofj.nx.action.reg.load.grouping.ActionRegLoadBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.NxActionRegLoadGrouping;
21 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.NxActionRegLoadNotifFlowsStatisticsUpdateApplyActionsCaseBuilder;
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.write.actions._case.write.actions.action.action.NxActionRegLoadNotifFlowsStatisticsUpdateWriteActionsCaseBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.group.desc.stats.updated.group.desc.stats.buckets.bucket.action.action.NxActionRegLoadNotifGroupDescStatsUpdatedCaseBuilder;
24 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.NxActionRegLoadNodesNodeTableFlowWriteActionsCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.reg.load.grouping.NxRegLoad;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.reg.load.grouping.NxRegLoadBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.reg.load.grouping.nx.reg.load.Dst;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.reg.load.grouping.nx.reg.load.DstBuilder;
29
30 import com.google.common.base.Preconditions;
31
32 /**
33  * @author msunal
34  *
35  */
36 public class RegLoadConvertor implements 
37 ConvertorActionToOFJava<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action, Action>,
38 ConvertorActionFromOFJava<Action, ActionPath> {
39
40     @Override
41     public org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action convert(Action input, ActionPath path) {
42         ActionRegLoad actionRegLoad = input.getAugmentation(OfjAugNxAction.class).getActionRegLoad();
43         DstBuilder dstBuilder = new DstBuilder();
44         dstBuilder.setDstChoice(RegMoveConvertor.resolveDst(actionRegLoad.getDst()));
45         dstBuilder.setStart(resolveStart(actionRegLoad.getOfsNbits()));
46         dstBuilder.setEnd(resolveEnd(actionRegLoad.getOfsNbits()));
47         NxRegLoadBuilder nxRegLoadBuilder = new NxRegLoadBuilder();
48         nxRegLoadBuilder.setDst(dstBuilder.build());
49         nxRegLoadBuilder.setValue(actionRegLoad.getValue());
50         return resolveAction(nxRegLoadBuilder.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(NxRegLoad value,
70             ActionPath path) {
71         switch (path) {
72         case NODES_NODE_TABLE_FLOW_INSTRUCTIONS_INSTRUCTION_WRITEACTIONSCASE_WRITEACTIONS_ACTION_ACTION_EXTENSIONLIST_EXTENSION:
73             return new NxActionRegLoadNodesNodeTableFlowWriteActionsCaseBuilder().setNxRegLoad(value).build();
74         case FLOWSSTATISTICSUPDATE_FLOWANDSTATISTICSMAPLIST_INSTRUCTIONS_INSTRUCTION_INSTRUCTION_WRITEACTIONSCASE_WRITEACTIONS_ACTION_ACTION:
75             return new NxActionRegLoadNotifFlowsStatisticsUpdateWriteActionsCaseBuilder().setNxRegLoad(value).build();
76         case FLOWSSTATISTICSUPDATE_FLOWANDSTATISTICSMAPLIST_INSTRUCTIONS_INSTRUCTION_INSTRUCTION_APPLYACTIONSCASE_APPLYACTIONS_ACTION_ACTION:
77             return new NxActionRegLoadNotifFlowsStatisticsUpdateApplyActionsCaseBuilder().setNxRegLoad(value).build();
78         case GROUPDESCSTATSUPDATED_GROUPDESCSTATS_BUCKETS_BUCKET_ACTION:
79             return new NxActionRegLoadNotifGroupDescStatsUpdatedCaseBuilder().setNxRegLoad(value).build();
80         default:
81             throw new CodecPreconditionException(path);
82         }
83     }
84     
85     @Override
86     public Action convert(org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action nxActionArg) {
87         Preconditions.checkArgument(nxActionArg instanceof NxActionRegLoadGrouping);
88         
89         NxActionRegLoadGrouping nxAction = (NxActionRegLoadGrouping) nxActionArg;
90         Dst dst = nxAction.getNxRegLoad().getDst();
91         ActionRegLoadBuilder actionRegLoadBuilder = new ActionRegLoadBuilder();
92         actionRegLoadBuilder.setDst(RegMoveConvertor.resolveDst(dst.getDstChoice()));
93         actionRegLoadBuilder.setOfsNbits((dst.getStart() << 6) | (dst.getEnd() - dst.getStart()));
94         actionRegLoadBuilder.setValue(nxAction.getNxRegLoad().getValue());
95         OfjAugNxActionBuilder augNxActionBuilder = new OfjAugNxActionBuilder();
96         augNxActionBuilder.setActionRegLoad(actionRegLoadBuilder.build());
97         return ActionUtil.createNiciraAction(augNxActionBuilder.build(), NxmNxRegLoad.class);
98     }
99
100    
101 }