a39e72eab72ae98b36f2fa2b9593715342426a9c
[genius.git] / mdsalutil / mdsalutil-api / src / main / java / org / opendaylight / genius / mdsalutil / actions / ActionLearn.java
1 /*
2  * Copyright © 2016 Red Hat, Inc. and others.
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.genius.mdsalutil.actions;
9
10 import java.util.ArrayList;
11 import java.util.List;
12 import java.util.stream.Collectors;
13 import org.opendaylight.genius.mdsalutil.ActionInfo;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.flow.mod.spec.flow.mod.spec.FlowModAddMatchFromFieldCaseBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.flow.mod.spec.flow.mod.spec.FlowModAddMatchFromValueCaseBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.flow.mod.spec.flow.mod.spec.FlowModCopyFieldIntoFieldCaseBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.flow.mod.spec.flow.mod.spec.FlowModCopyValueIntoFieldCaseBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.flow.mod.spec.flow.mod.spec.FlowModOutputToPortCaseBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.flow.mod.spec.flow.mod.spec.flow.mod.add.match.from.field._case.FlowModAddMatchFromFieldBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.flow.mod.spec.flow.mod.spec.flow.mod.add.match.from.value._case.FlowModAddMatchFromValueBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.flow.mod.spec.flow.mod.spec.flow.mod.copy.field.into.field._case.FlowModCopyFieldIntoFieldBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.flow.mod.spec.flow.mod.spec.flow.mod.copy.value.into.field._case.FlowModCopyValueIntoFieldBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.flow.mod.spec.flow.mod.spec.flow.mod.output.to.port._case.FlowModOutputToPortBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nodes.node.table.flow.instructions.instruction.instruction.apply.actions._case.apply.actions.action.action.NxActionLearnNodesNodeTableFlowApplyActionsCaseBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.learn.grouping.NxLearnBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.learn.grouping.nx.learn.FlowMods;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.learn.grouping.nx.learn.FlowModsBuilder;
31 import org.opendaylight.yangtools.yang.common.Uint64;
32
33 /**
34  * Learn action.
35  */
36 public class ActionLearn extends ActionInfo {
37
38     private final int idleTimeout;
39     private final int hardTimeout;
40     private final int priority;
41     private final Uint64 cookie;
42     private final int flags;
43     private final short tableId;
44     private final int finIdleTimeout;
45     private final int finHardTimeout;
46     private final List<FlowMod> flowMods = new ArrayList<>();
47
48     public ActionLearn(int idleTimeout, int hardTimeout, int priority, Uint64 cookie, int flags, short tableId,
49         int finIdleTimeout, int finHardTimeout, List<FlowMod> flowMods) {
50         this(0, idleTimeout, hardTimeout, priority, cookie, flags, tableId, finIdleTimeout, finHardTimeout, flowMods);
51     }
52
53     public ActionLearn(int actionKey, int idleTimeout, int hardTimeout, int priority, Uint64 cookie, int flags,
54         short tableId, int finIdleTimeout, int finHardTimeout, List<FlowMod> flowMods) {
55         super(actionKey);
56         this.idleTimeout = idleTimeout;
57         this.hardTimeout = hardTimeout;
58         this.priority = priority;
59         this.cookie = cookie;
60         this.flags = flags;
61         this.tableId = tableId;
62         this.finIdleTimeout = finIdleTimeout;
63         this.finHardTimeout = finHardTimeout;
64         this.flowMods.addAll(flowMods);
65     }
66
67     @Override
68     public Action buildAction() {
69         return buildAction(getActionKey());
70     }
71
72     @Override
73     public Action buildAction(int newActionKey) {
74         NxLearnBuilder learnBuilder = new NxLearnBuilder();
75         learnBuilder
76             .setIdleTimeout(idleTimeout)
77             .setHardTimeout(hardTimeout)
78             .setPriority(priority)
79             .setCookie(cookie)
80             .setFlags(flags)
81             .setTableId(tableId)
82             .setFinIdleTimeout(finIdleTimeout)
83             .setFinHardTimeout(finHardTimeout);
84
85         learnBuilder.setFlowMods(this.flowMods.stream().map(FlowMod::buildFlowMod).collect(Collectors.toList()));
86
87         return new ActionBuilder()
88             .withKey(new ActionKey(newActionKey))
89             .setAction(new NxActionLearnNodesNodeTableFlowApplyActionsCaseBuilder()
90                 .setNxLearn(learnBuilder.build()).build())
91             .build();
92     }
93
94     public int getIdleTimeout() {
95         return idleTimeout;
96     }
97
98     public int getHardTimeout() {
99         return hardTimeout;
100     }
101
102     public int getPriority() {
103         return priority;
104     }
105
106     public Uint64 getCookie() {
107         return cookie;
108     }
109
110     public int getFlags() {
111         return flags;
112     }
113
114     public short getTableId() {
115         return tableId;
116     }
117
118     public int getFinIdleTimeout() {
119         return finIdleTimeout;
120     }
121
122     public int getFinHardTimeout() {
123         return finHardTimeout;
124     }
125
126     public List<FlowMod> getFlowMods() {
127         return flowMods;
128     }
129
130     @Override
131     public boolean equals(Object other) {
132         if (this == other) {
133             return true;
134         }
135         if (other == null || getClass() != other.getClass()) {
136             return false;
137         }
138         if (!super.equals(other)) {
139             return false;
140         }
141
142         ActionLearn that = (ActionLearn) other;
143
144         if (idleTimeout != that.idleTimeout) {
145             return false;
146         }
147         if (hardTimeout != that.hardTimeout) {
148             return false;
149         }
150         if (priority != that.priority) {
151             return false;
152         }
153         if (flags != that.flags) {
154             return false;
155         }
156         if (tableId != that.tableId) {
157             return false;
158         }
159         if (finIdleTimeout != that.finIdleTimeout) {
160             return false;
161         }
162         if (finHardTimeout != that.finHardTimeout) {
163             return false;
164         }
165         if (cookie != null ? !cookie.equals(that.cookie) : that.cookie != null) {
166             return false;
167         }
168         return flowMods.equals(that.flowMods);
169     }
170
171     @Override
172     public int hashCode() {
173         int result = super.hashCode();
174         result = 31 * result + idleTimeout;
175         result = 31 * result + hardTimeout;
176         result = 31 * result + priority;
177         result = 31 * result + (cookie != null ? cookie.hashCode() : 0);
178         result = 31 * result + flags;
179         result = 31 * result + tableId;
180         result = 31 * result + finIdleTimeout;
181         result = 31 * result + finHardTimeout;
182         result = 31 * result + flowMods.hashCode();
183         return result;
184     }
185
186     @Override
187     public String toString() {
188         return "ActionLearn [actionKey=" + getActionKey() + ", idleTimeout=" + idleTimeout + ", hardTimeout="
189                 + hardTimeout + ", priority=" + priority + ", cookie=" + cookie + ", flags=" + flags + ", tableId="
190                 + tableId + ", finIdleTimeout=" + finIdleTimeout + ", finHardTimeout=" + finHardTimeout + ", flowMods="
191                 + flowMods + "]";
192     }
193
194     public interface FlowMod {
195         FlowMods buildFlowMod();
196     }
197
198     public static class MatchFromField implements FlowMod {
199         private final long sourceField;
200         private final int srcOffset;
201         private final long destField;
202         private final int dstOffset;
203         private final int bits;
204
205         public MatchFromField(long sourceField, int srcOffset, long destField, int dstOffset, int bits) {
206             this.sourceField = sourceField;
207             this.srcOffset = srcOffset;
208             this.destField = destField;
209             this.dstOffset = dstOffset;
210             this.bits = bits;
211         }
212
213         public MatchFromField(long sourceField, long destField, int bits) {
214             this(sourceField, 0, destField, 0, bits);
215         }
216
217         public long getSourceField() {
218             return sourceField;
219         }
220
221         public long getDestField() {
222             return destField;
223         }
224
225         public int getBits() {
226             return bits;
227         }
228
229         public int getSrcOffset() {
230             return srcOffset;
231         }
232
233         public int getDstOffset() {
234             return dstOffset;
235         }
236
237         @Override
238         public FlowMods buildFlowMod() {
239             FlowModAddMatchFromFieldBuilder builder = new FlowModAddMatchFromFieldBuilder();
240             builder.setSrcField(sourceField);
241             builder.setSrcOfs(srcOffset);
242             builder.setDstField(destField);
243             builder.setDstOfs(dstOffset);
244             builder.setFlowModNumBits(bits);
245
246             FlowModsBuilder flowModsBuilder = new FlowModsBuilder();
247             FlowModAddMatchFromFieldCaseBuilder caseBuilder = new FlowModAddMatchFromFieldCaseBuilder();
248             caseBuilder.setFlowModAddMatchFromField(builder.build());
249             flowModsBuilder.setFlowModSpec(caseBuilder.build());
250             return flowModsBuilder.build();
251         }
252
253         @Override
254         public boolean equals(Object other) {
255             if (this == other) {
256                 return true;
257             }
258             if (other == null || getClass() != other.getClass()) {
259                 return false;
260             }
261
262             MatchFromField that = (MatchFromField) other;
263
264             if (sourceField != that.sourceField) {
265                 return false;
266             }
267             if (destField != that.destField) {
268                 return false;
269             }
270             if (srcOffset != that.srcOffset) {
271                 return false;
272             }
273             if (dstOffset != that.dstOffset) {
274                 return false;
275             }
276             return bits == that.bits;
277         }
278
279         @Override
280         public int hashCode() {
281             int result = (int) (sourceField ^ sourceField >>> 32);
282             result = 31 * result + (int) (destField ^ destField >>> 32);
283             result = 31 * result + srcOffset;
284             result = 31 * result + dstOffset;
285             result = 31 * result + bits;
286             return result;
287         }
288
289         @Override
290         public String toString() {
291             return "MatchFromField [sourceField=" + sourceField + ", srcOffset=" + srcOffset + ", destField="
292                     + destField + ", dstOffset=" + dstOffset + ", bits=" + bits + "]";
293         }
294     }
295
296     public static class MatchFromValue implements FlowMod {
297         private final int value;
298         private final long sourceField;
299         private final int srcOffset;
300         private final int bits;
301
302         public MatchFromValue(int value, long sourceField, int srcOffset, int bits) {
303             this.value = value;
304             this.sourceField = sourceField;
305             this.srcOffset = srcOffset;
306             this.bits = bits;
307         }
308
309         public MatchFromValue(int value, long sourceField, int bits) {
310             this(value, sourceField, 0, bits);
311         }
312
313         public int getValue() {
314             return value;
315         }
316
317         public long getSourceField() {
318             return sourceField;
319         }
320
321         public int getBits() {
322             return bits;
323         }
324
325         public int getSrcOffset() {
326             return srcOffset;
327         }
328
329         @Override
330         public FlowMods buildFlowMod() {
331             FlowModAddMatchFromValueBuilder builder = new FlowModAddMatchFromValueBuilder();
332             builder.setValue(value);
333             builder.setSrcField(sourceField);
334             builder.setSrcOfs(srcOffset);
335             builder.setFlowModNumBits(bits);
336
337             FlowModsBuilder flowModsBuilder = new FlowModsBuilder();
338             FlowModAddMatchFromValueCaseBuilder caseBuilder = new FlowModAddMatchFromValueCaseBuilder();
339             caseBuilder.setFlowModAddMatchFromValue(builder.build());
340             flowModsBuilder.setFlowModSpec(caseBuilder.build());
341             return flowModsBuilder.build();
342         }
343
344         @Override
345         public boolean equals(Object other) {
346             if (this == other) {
347                 return true;
348             }
349             if (other == null || getClass() != other.getClass()) {
350                 return false;
351             }
352
353             MatchFromValue that = (MatchFromValue) other;
354
355             if (value != that.value) {
356                 return false;
357             }
358             if (sourceField != that.sourceField) {
359                 return false;
360             }
361             if (srcOffset != that.srcOffset) {
362                 return false;
363             }
364             return bits == that.bits;
365         }
366
367         @Override
368         public int hashCode() {
369             int result = value;
370             result = 31 * result + (int) (sourceField ^ sourceField >>> 32);
371             result = 31 * result + srcOffset;
372             result = 31 * result + bits;
373             return result;
374         }
375
376         @Override
377         public String toString() {
378             return "MatchFromValue [value=" + value + ", sourceField=" + sourceField + ", srcOffset="
379                     + srcOffset + ", bits=" + bits + "]";
380         }
381     }
382
383     public static class CopyFromField implements FlowMod {
384         private final long sourceField;
385         private final int srcOffset;
386         private final long destField;
387         private final int dstOffset;
388         private final int bits;
389
390         public CopyFromField(long sourceField, int srcOffset, long destField, int dstOffset, int bits) {
391             this.sourceField = sourceField;
392             this.srcOffset = srcOffset;
393             this.destField = destField;
394             this.dstOffset = dstOffset;
395             this.bits = bits;
396         }
397
398         public CopyFromField(long sourceField, long destField, int bits) {
399             this(sourceField, 0, destField, 0, bits);
400         }
401
402         public long getSourceField() {
403             return sourceField;
404         }
405
406         public long getDestField() {
407             return destField;
408         }
409
410         public int getBits() {
411             return bits;
412         }
413
414         public int getSrcOffset() {
415             return srcOffset;
416         }
417
418         public int getDstOffset() {
419             return dstOffset;
420         }
421
422         @Override
423         public FlowMods buildFlowMod() {
424             FlowModCopyFieldIntoFieldBuilder builder = new FlowModCopyFieldIntoFieldBuilder();
425             builder.setSrcField(sourceField);
426             builder.setSrcOfs(srcOffset);
427             builder.setDstField(destField);
428             builder.setDstOfs(dstOffset);
429             builder.setFlowModNumBits(bits);
430
431             FlowModsBuilder flowModsBuilder = new FlowModsBuilder();
432             FlowModCopyFieldIntoFieldCaseBuilder caseBuilder = new FlowModCopyFieldIntoFieldCaseBuilder();
433             caseBuilder.setFlowModCopyFieldIntoField(builder.build());
434             flowModsBuilder.setFlowModSpec(caseBuilder.build());
435             return flowModsBuilder.build();
436         }
437
438         @Override
439         public boolean equals(Object other) {
440             if (this == other) {
441                 return true;
442             }
443             if (other == null || getClass() != other.getClass()) {
444                 return false;
445             }
446
447             CopyFromField that = (CopyFromField) other;
448
449             if (sourceField != that.sourceField) {
450                 return false;
451             }
452             if (destField != that.destField) {
453                 return false;
454             }
455             if (srcOffset != that.srcOffset) {
456                 return false;
457             }
458             if (dstOffset != that.dstOffset) {
459                 return false;
460             }
461             return bits == that.bits;
462         }
463
464         @Override
465         public int hashCode() {
466             int result = (int) (sourceField ^ sourceField >>> 32);
467             result = 31 * result + (int) (destField ^ destField >>> 32);
468             result = 31 * result + srcOffset;
469             result = 31 * result + dstOffset;
470             result = 31 * result + bits;
471             return result;
472         }
473
474         @Override
475         public String toString() {
476             return "CopyFromField [sourceField=" + sourceField + ", srcOffset=" + srcOffset + ", destField="
477                     + destField + ", dstOffset=" + dstOffset + ", bits=" + bits + "]";
478         }
479     }
480
481     public static class CopyFromValue implements FlowMod {
482         private final int value;
483         private final long destField;
484         private final int dstOffset;
485         private final int bits;
486
487         public CopyFromValue(int value, long destField, int bits) {
488             this(value, destField, 0, bits);
489         }
490
491         public CopyFromValue(int value, long destField, int dstOffset, int bits) {
492             this.value = value;
493             this.destField = destField;
494             this.dstOffset = dstOffset;
495             this.bits = bits;
496         }
497
498         public int getValue() {
499             return value;
500         }
501
502         public long getDestField() {
503             return destField;
504         }
505
506         public int getBits() {
507             return bits;
508         }
509
510         public int getDstOffset() {
511             return dstOffset;
512         }
513
514         @Override
515         public FlowMods buildFlowMod() {
516             FlowModCopyValueIntoFieldBuilder builder = new FlowModCopyValueIntoFieldBuilder();
517             builder.setValue(value);
518             builder.setDstField(destField);
519             builder.setDstOfs(dstOffset);
520             builder.setFlowModNumBits(bits);
521
522             FlowModsBuilder flowModsBuilder = new FlowModsBuilder();
523             FlowModCopyValueIntoFieldCaseBuilder caseBuilder = new FlowModCopyValueIntoFieldCaseBuilder();
524             caseBuilder.setFlowModCopyValueIntoField(builder.build());
525             flowModsBuilder.setFlowModSpec(caseBuilder.build());
526             return flowModsBuilder.build();
527         }
528
529         @Override
530         public boolean equals(Object other) {
531             if (this == other) {
532                 return true;
533             }
534             if (other == null || getClass() != other.getClass()) {
535                 return false;
536             }
537
538             CopyFromValue that = (CopyFromValue) other;
539
540             if (value != that.value) {
541                 return false;
542             }
543             if (destField != that.destField) {
544                 return false;
545             }
546             if (dstOffset != that.dstOffset) {
547                 return false;
548             }
549             return bits == that.bits;
550         }
551
552         @Override
553         public int hashCode() {
554             int result = value;
555             result = 31 * result + (int) (destField ^ destField >>> 32);
556             result = 31 * result +  dstOffset;
557             result = 31 * result + bits;
558             return result;
559         }
560
561         @Override
562         public String toString() {
563             return "CopyFromValue [value=" + value + ", destField=" + destField + ", dstOffset=" + dstOffset
564                     + ", bits=" + bits + "]";
565         }
566     }
567
568     public static class OutputToPort implements FlowMod {
569         private final long sourceField;
570         private final int bits;
571
572         public OutputToPort(long sourceField, int bits) {
573             this.sourceField = sourceField;
574             this.bits = bits;
575         }
576
577         public long getSourceField() {
578             return sourceField;
579         }
580
581         public int getBits() {
582             return bits;
583         }
584
585         @Override
586         public FlowMods buildFlowMod() {
587             FlowModOutputToPortBuilder builder = new FlowModOutputToPortBuilder();
588             builder.setSrcField(sourceField);
589             builder.setSrcOfs(0);
590             builder.setFlowModNumBits(bits);
591
592             FlowModsBuilder flowModsBuilder = new FlowModsBuilder();
593             FlowModOutputToPortCaseBuilder caseBuilder = new FlowModOutputToPortCaseBuilder();
594             caseBuilder.setFlowModOutputToPort(builder.build());
595             flowModsBuilder.setFlowModSpec(caseBuilder.build());
596             return flowModsBuilder.build();
597         }
598
599         @Override
600         public boolean equals(Object other) {
601             if (this == other) {
602                 return true;
603             }
604             if (other == null || getClass() != other.getClass()) {
605                 return false;
606             }
607
608             OutputToPort that = (OutputToPort) other;
609
610             if (sourceField != that.sourceField) {
611                 return false;
612             }
613             return bits == that.bits;
614         }
615
616         @Override
617         public int hashCode() {
618             int result = (int) (sourceField ^ sourceField >>> 32);
619             result = 31 * result + bits;
620             return result;
621         }
622
623         @Override
624         public String toString() {
625             return "OutputToPort [sourceField=" + sourceField + ", bits=" + bits + "]";
626         }
627     }
628 }