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