Merge "Refactor nsh fields to new encoding"
[openflowplugin.git] / extension / openflowplugin-extension-nicira / src / main / java / org / opendaylight / openflowplugin / extension / vendor / nicira / convertor / action / RegLoad2Convertor.java
1 /*
2  * Copyright (c) 2018 SUSE LINUX GmbH.  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 java.math.BigInteger;
13 import java.util.Collections;
14 import javax.annotation.Nullable;
15 import org.opendaylight.openflowjava.nx.codec.match.NiciraMatchCodecs;
16 import org.opendaylight.openflowplugin.extension.api.ConvertorActionFromOFJava;
17 import org.opendaylight.openflowplugin.extension.api.ConvertorActionToOFJava;
18 import org.opendaylight.openflowplugin.extension.api.path.ActionPath;
19 import org.opendaylight.openflowplugin.extension.vendor.nicira.convertor.CodecPreconditionException;
20 import org.opendaylight.openflowplugin.extension.vendor.nicira.convertor.match.NshFlagsConvertor;
21 import org.opendaylight.openflowplugin.extension.vendor.nicira.convertor.match.NshTtlConvertor;
22 import org.opendaylight.openflowplugin.extension.vendor.nicira.convertor.match.Nshc1Convertor;
23 import org.opendaylight.openflowplugin.extension.vendor.nicira.convertor.match.Nshc2Convertor;
24 import org.opendaylight.openflowplugin.extension.vendor.nicira.convertor.match.Nshc3Convertor;
25 import org.opendaylight.openflowplugin.extension.vendor.nicira.convertor.match.Nshc4Convertor;
26 import org.opendaylight.openflowplugin.extension.vendor.nicira.convertor.match.NsiConvertor;
27 import org.opendaylight.openflowplugin.extension.vendor.nicira.convertor.match.NspConvertor;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.oxm.container.match.entry.value.ExperimenterIdCase;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.MatchField;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.action.container.action.choice.ActionRegLoad2;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.action.container.action.choice.ActionRegLoad2Builder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.ofj.nx.action.reg.load2.grouping.NxActionRegLoad2;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.ofj.nx.action.reg.load2.grouping.NxActionRegLoad2Builder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxNshFlags;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxNshTtl;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxNshc1;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxNshc2;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxNshc3;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxNshc4;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxNsi;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxNsp;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.OfjAugNxExpMatch;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.nx.match.nsh.flags.grouping.NshFlagsValues;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.nx.match.nsh.ttl.grouping.NshTtlValues;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.nx.match.nsi.grouping.NsiValues;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.nx.match.nsp.grouping.NspValues;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.experimenter.id._case.NxExpMatchEntryValue;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.experimenter.id._case.nx.exp.match.entry.value.NshFlagsCaseValue;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.experimenter.id._case.nx.exp.match.entry.value.NshTtlCaseValue;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.experimenter.id._case.nx.exp.match.entry.value.NshcCaseValue;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.experimenter.id._case.nx.exp.match.entry.value.NsiCaseValue;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.experimenter.id._case.nx.exp.match.entry.value.NspCaseValue;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.NxActionRegLoadGrouping;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.DstChoice;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxNshFlagsCase;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxNshFlagsCaseBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxNshTtlCase;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxNshTtlCaseBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxNshc1Case;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxNshc1CaseBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxNshc2Case;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxNshc2CaseBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxNshc3Case;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxNshc3CaseBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxNshc4Case;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxNshc4CaseBuilder;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxNsiCase;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxNsiCaseBuilder;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxNspCase;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxNspCaseBuilder;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.reg.load.grouping.NxRegLoad;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.reg.load.grouping.NxRegLoadBuilder;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.reg.load.grouping.nx.reg.load.Dst;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.reg.load.grouping.nx.reg.load.DstBuilder;
77
78 /**
79  * Convert between RegLoad SAL action and RegLoad2 nicira action.
80  */
81 public class RegLoad2Convertor implements
82         ConvertorActionToOFJava<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action,
83                 Action>,
84         ConvertorActionFromOFJava<Action, ActionPath> {
85
86     @Override
87     public org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action convert(Action input,
88                                                                                                       ActionPath path) {
89         NxActionRegLoad2 actionRegLoad2 = ((ActionRegLoad2) input.getActionChoice()).getNxActionRegLoad2();
90         MatchEntry matchEntry = actionRegLoad2.getMatchEntry().get(0);
91         NxRegLoad nxRegLoad = resolveRegLoad(matchEntry);
92         return RegLoadConvertor.resolveAction(nxRegLoad, path);
93     }
94
95     @Override
96     public Action convert(
97             org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action actionCase) {
98         Preconditions.checkArgument(actionCase instanceof NxActionRegLoadGrouping);
99
100         NxActionRegLoadGrouping nxAction = (NxActionRegLoadGrouping) actionCase;
101         MatchEntry matchEntry = resolveMatchEntry(nxAction.getNxRegLoad());
102         NxActionRegLoad2 nxActionRegLoad2 = new NxActionRegLoad2Builder()
103                 .setMatchEntry(Collections.singletonList(matchEntry))
104                 .build();
105         ActionRegLoad2 actionRegLoad2 = new ActionRegLoad2Builder().setNxActionRegLoad2(nxActionRegLoad2).build();
106         return ActionUtil.createAction(actionRegLoad2);
107     }
108
109     private static MatchEntry resolveMatchEntry(NxRegLoad nxRegLoad) {
110         Dst dst = nxRegLoad.getDst();
111         BigInteger value = nxRegLoad.getValue();
112         int start = dst.getStart();
113         int end = dst.getEnd();
114         BigInteger[] valueMask = resolveValueMask(value, start, end);
115         value = valueMask[0];
116         BigInteger mask = valueMask[1];
117         DstChoice dstChoice = dst.getDstChoice();
118         return resolveMatchEntry(dstChoice, value, mask);
119     }
120
121     private static MatchEntry resolveMatchEntry(DstChoice dstChoice, BigInteger value, BigInteger mask) {
122         try {
123             if (dstChoice instanceof DstNxNshFlagsCase) {
124                 return NshFlagsConvertor.buildMatchEntry(value.shortValueExact(), mask.shortValueExact());
125             }
126             if (dstChoice instanceof DstNxNspCase) {
127                 return NspConvertor.buildMatchEntry(value.longValueExact(), mask.longValueExact());
128             }
129             if (dstChoice instanceof DstNxNsiCase) {
130                 return NsiConvertor.buildMatchEntry(value.shortValueExact(), mask.shortValueExact());
131             }
132             if (dstChoice instanceof DstNxNshc1Case) {
133                 return Nshc1Convertor.buildMatchEntry(value.longValueExact(), mask.longValueExact());
134             }
135             if (dstChoice instanceof DstNxNshc2Case) {
136                 return Nshc2Convertor.buildMatchEntry(value.longValueExact(), mask.longValueExact());
137             }
138             if (dstChoice instanceof DstNxNshc3Case) {
139                 return Nshc3Convertor.buildMatchEntry(value.longValueExact(), mask.longValueExact());
140             }
141             if (dstChoice instanceof DstNxNshc4Case) {
142                 return Nshc4Convertor.buildMatchEntry(value.longValueExact(), mask.longValueExact());
143             }
144             if (dstChoice instanceof DstNxNshTtlCase) {
145                 return NshTtlConvertor.buildMatchEntry(value.shortValueExact(), mask.shortValueExact());
146             }
147         } catch (ArithmeticException e) {
148             throw new IllegalArgumentException("Value or bit range too big for destination choice", e);
149         }
150
151         throw new CodecPreconditionException("Missing implementation of a case in dst-choice? " + dstChoice.getClass());
152     }
153
154     private static NxRegLoad resolveRegLoad(MatchEntry matchEntry) {
155         Class<? extends MatchField> oxmMatchField = matchEntry.getOxmMatchField();
156         ExperimenterIdCase experimenterIdCase = (ExperimenterIdCase) matchEntry.getMatchEntryValue();
157         OfjAugNxExpMatch ofjAugNxExpMatch = experimenterIdCase.augmentation(OfjAugNxExpMatch.class);
158         NxExpMatchEntryValue nxExpMatchEntryValue = ofjAugNxExpMatch.getNxExpMatchEntryValue();
159         DstBuilder dstBuilder = new DstBuilder();
160         return resolveRegLoad(oxmMatchField, nxExpMatchEntryValue, dstBuilder);
161     }
162
163     private static NxRegLoad resolveRegLoad(
164             Class<? extends MatchField> oxmMatchField,
165             NxExpMatchEntryValue value,
166             DstBuilder dstBuilder) {
167
168         if (NxmNxNshFlags.class.equals(oxmMatchField)) {
169             int valueLength = NiciraMatchCodecs.NSH_FLAGS_CODEC.getValueLength();
170             dstBuilder.setDstChoice(new DstNxNshFlagsCaseBuilder().setNxNshFlags(true).build());
171             NshFlagsValues nshFlagsValues = ((NshFlagsCaseValue) value).getNshFlagsValues();
172             return resolveRegLoad(nshFlagsValues.getNshFlags(), nshFlagsValues.getMask(), valueLength, dstBuilder);
173         }
174         if (NxmNxNsp.class.equals(oxmMatchField)) {
175             int valueLength = NiciraMatchCodecs.NSP_CODEC.getValueLength();
176             dstBuilder.setDstChoice(new DstNxNspCaseBuilder().setNxNspDst(true).build());
177             NspValues nspValues = ((NspCaseValue) value).getNspValues();
178             return resolveRegLoad(nspValues.getNsp(), nspValues.getMask(), valueLength, dstBuilder);
179         }
180         if (NxmNxNsi.class.equals(oxmMatchField)) {
181             int valueLength = NiciraMatchCodecs.NSI_CODEC.getValueLength();
182             dstBuilder.setDstChoice(new DstNxNsiCaseBuilder().setNxNsiDst(true).build());
183             NsiValues nsiValues = ((NsiCaseValue) value).getNsiValues();
184             return resolveRegLoad(nsiValues.getNsi(), nsiValues.getMask(), valueLength, dstBuilder);
185         }
186         if (NxmNxNshc1.class.equals(oxmMatchField)) {
187             int valueLength = NiciraMatchCodecs.NSC1_CODEC.getValueLength();
188             dstBuilder.setDstChoice(new DstNxNshc1CaseBuilder().setNxNshc1Dst(true).build());
189             NshcCaseValue nshcCaseValue = ((NshcCaseValue) value);
190             return resolveRegLoad(nshcCaseValue.getNshc(), nshcCaseValue.getMask(), valueLength, dstBuilder);
191         }
192         if (NxmNxNshc2.class.equals(oxmMatchField)) {
193             int valueLength = NiciraMatchCodecs.NSC2_CODEC.getValueLength();
194             dstBuilder.setDstChoice(new DstNxNshc2CaseBuilder().setNxNshc2Dst(true).build());
195             NshcCaseValue nshcCaseValue = ((NshcCaseValue) value);
196             return resolveRegLoad(nshcCaseValue.getNshc(), nshcCaseValue.getMask(), valueLength, dstBuilder);
197         }
198         if (NxmNxNshc3.class.equals(oxmMatchField)) {
199             int valueLength = NiciraMatchCodecs.NSC3_CODEC.getValueLength();
200             dstBuilder.setDstChoice(new DstNxNshc3CaseBuilder().setNxNshc3Dst(true).build());
201             NshcCaseValue nshcCaseValue = ((NshcCaseValue) value);
202             return resolveRegLoad(nshcCaseValue.getNshc(), nshcCaseValue.getMask(), valueLength, dstBuilder);
203         }
204         if (NxmNxNshc4.class.equals(oxmMatchField)) {
205             int valueLength = NiciraMatchCodecs.NSC4_CODEC.getValueLength();
206             dstBuilder.setDstChoice(new DstNxNshc4CaseBuilder().setNxNshc4Dst(true).build());
207             NshcCaseValue nshcCaseValue = ((NshcCaseValue) value);
208             return resolveRegLoad(nshcCaseValue.getNshc(), nshcCaseValue.getMask(), valueLength, dstBuilder);
209         }
210         if (NxmNxNshTtl.class.equals(oxmMatchField)) {
211             int valueLength = NiciraMatchCodecs.NSH_TTL_CODEC.getValueLength();
212             dstBuilder.setDstChoice(new DstNxNshTtlCaseBuilder().setNxNshTtl(true).build());
213             NshTtlValues nshTtlValues = ((NshTtlCaseValue) value).getNshTtlValues();
214             return resolveRegLoad(nshTtlValues.getNshTtl(), nshTtlValues.getMask(), valueLength, dstBuilder);
215         }
216
217         throw new CodecPreconditionException("Missing codec for " + value.getImplementedInterface());
218     }
219
220     private static NxRegLoad resolveRegLoad(Short value, @Nullable Short mask, int valueLength, DstBuilder dstBuilder) {
221         return resolveRegLoad(
222                 BigInteger.valueOf(value),
223                 mask == null ? null : BigInteger.valueOf(mask),
224                 valueLength,
225                 dstBuilder);
226     }
227
228     private static NxRegLoad resolveRegLoad(Long value, @Nullable Long mask, int valueLength, DstBuilder dstBuilder) {
229         return resolveRegLoad(
230                 BigInteger.valueOf(value),
231                 mask == null ? null : BigInteger.valueOf(mask),
232                 valueLength,
233                 dstBuilder);
234     }
235
236     // Convert the value/mask pair of the openflowjava reg_load2 action to the
237     // value/bit range pair of the openfloplugin reg_load action.
238     private static NxRegLoad resolveRegLoad(BigInteger value,
239                                             @Nullable BigInteger mask,
240                                             int length,
241                                             DstBuilder dstBuilder) {
242         final int start;
243         final int end;
244         if (mask == null) {
245             start = 0;
246             end = length * 8;
247         } else {
248             start = mask.getLowestSetBit();
249             end = start + mask.shiftRight(start).not().getLowestSetBit();
250             value = value.and(mask).shiftRight(start);
251
252             if (value.bitLength() > end - start) {
253                 // We cannot map a REG_LOAD2 to a single REG_LOAD if the mask
254                 // has multiple 1-bit segments (i.e. 0xFF00FF)
255                 throw new IllegalArgumentException("Value does not fit in the first 1-bit segment of the mask");
256             }
257         }
258
259         dstBuilder.setStart(start);
260         dstBuilder.setEnd(end - 1);
261         NxRegLoadBuilder nxRegLoadBuilder = new NxRegLoadBuilder();
262         nxRegLoadBuilder.setDst(dstBuilder.build());
263         nxRegLoadBuilder.setValue(value);
264         return nxRegLoadBuilder.build();
265     }
266
267     // Convert value/bit range pair of the openfloplugin reg_load action to the
268     // value/mask pair of the openflowjava reg_load2 action.
269     private static BigInteger[] resolveValueMask(BigInteger value, int start, int end) {
270         int bits = end - start + 1;
271         if (value.bitLength() > bits) {
272             throw new IllegalArgumentException("Value does not fit the bit range");
273         }
274
275         BigInteger mask = BigInteger.ONE.shiftLeft(bits).subtract(BigInteger.ONE).shiftLeft(start);
276         value = value.shiftLeft(start);
277
278         return new BigInteger[] {value, mask};
279     }
280
281 }