Refactoring: shortening of match methods
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / TableFeaturesReplyConvertor.java
1 /**
2  * Copyright (c) 2013 Ericsson. 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
9 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor;
10
11 import com.google.common.collect.ImmutableMap;
12 import com.google.common.collect.ImmutableMap.Builder;
13 import java.math.BigInteger;
14 import java.util.ArrayList;
15 import java.util.Collections;
16 import java.util.List;
17 import java.util.Map;
18 import org.opendaylight.openflowplugin.api.OFConstants;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.CopyTtlInCaseBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.CopyTtlOutCaseBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.DecMplsTtlCaseBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.DecNwTtlCaseBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.GroupActionCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.OutputActionCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PopMplsActionCaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PopPbbActionCaseBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PopVlanActionCaseBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PushMplsActionCaseBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PushPbbActionCaseBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PushVlanActionCaseBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetFieldCaseBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetMplsTtlActionCaseBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNwDstActionCaseBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNwSrcActionCaseBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNwTtlActionCaseBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetQueueActionCaseBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ClearActionsCaseBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.GoToTableCaseBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.MeterCaseBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.WriteActionsCaseBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.WriteMetadataCaseBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.ActionRelatedTableFeatureProperty;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.InstructionRelatedTableFeatureProperty;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.NextTableRelatedTableFeatureProperty;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.OxmRelatedTableFeatureProperty;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.instruction.container.instruction.choice.ExperimenterIdCase;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.table.features.properties.container.table.feature.properties.NextTableIds;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.ActionChoice;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.CopyTtlInCase;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.CopyTtlOutCase;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.DecMplsTtlCase;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.DecNwTtlCase;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.GroupCase;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.OutputActionCase;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.PopMplsCase;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.PopPbbCase;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.PopVlanCase;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.PushMplsCase;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.PushPbbCase;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.PushVlanCase;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.SetFieldCase;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.SetMplsTtlCase;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.SetNwDstCase;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.SetNwSrcCase;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.SetNwTtlCase;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.SetQueueCase;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.InstructionChoice;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.ApplyActionsCase;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.ClearActionsCase;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.GotoTableCase;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.MeterCase;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.WriteActionsCase;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.WriteMetadataCase;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.TableFeaturesPropType;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.ArpOp;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.ArpSha;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.ArpSpa;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.ArpTha;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.ArpTpa;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.EthDst;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.EthSrc;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.EthType;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Icmpv4Code;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Icmpv4Type;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Icmpv6Code;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Icmpv6Type;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.InPhyPort;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.InPort;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.IpDscp;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.IpEcn;
94 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.IpProto;
95 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Ipv4Dst;
96 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Ipv4Src;
97 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Ipv6Dst;
98 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Ipv6Exthdr;
99 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Ipv6Flabel;
100 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Ipv6NdSll;
101 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Ipv6NdTarget;
102 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Ipv6NdTll;
103 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Ipv6Src;
104 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.MatchField;
105 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Metadata;
106 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.MplsBos;
107 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.MplsLabel;
108 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.MplsTc;
109 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.PbbIsid;
110 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.SctpDst;
111 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.SctpSrc;
112 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.TcpDst;
113 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.TcpSrc;
114 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.TunnelId;
115 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.UdpDst;
116 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.UdpSrc;
117 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.VlanPcp;
118 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.VlanVid;
119 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
120 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.table.features._case.MultipartReplyTableFeatures;
121 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.table.features.properties.grouping.TableFeatureProperties;
122 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TableConfig;
123 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.set.field.match.SetFieldMatch;
124 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.set.field.match.SetFieldMatchBuilder;
125 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.table.feature.prop.type.MatchBuilder;
126 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.table.feature.prop.type.WildcardsBuilder;
127 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.table.feature.prop.type.apply.setfield.ApplySetfieldBuilder;
128 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.table.feature.prop.type.apply.setfield.miss.ApplySetfieldMissBuilder;
129 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.table.feature.prop.type.match.MatchSetfieldBuilder;
130 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.table.feature.prop.type.next.table.miss.TablesMissBuilder;
131 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.table.feature.prop.type.wildcards.WildcardSetfieldBuilder;
132 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.table.feature.prop.type.write.setfield.WriteSetfieldBuilder;
133 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.table.feature.prop.type.write.setfield.miss.WriteSetfieldMissBuilder;
134 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures;
135 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeaturesBuilder;
136 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.table.features.TableProperties;
137 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.table.features.TablePropertiesBuilder;
138 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.table.features.table.properties.TableFeaturePropertiesBuilder;
139 import org.slf4j.Logger;
140 import org.slf4j.LoggerFactory;
141
142 /**
143  * Utility class for converting a OF library table features to MD-SAL table
144  * features.
145  */
146 public class TableFeaturesReplyConvertor {
147     private static final Logger LOG = LoggerFactory.getLogger(TableFeaturesReplyConvertor.class);
148
149     private TableFeaturesReplyConvertor() {
150         //hiding implicit constructor
151     }
152
153     private interface ActionExecutor {
154         public void execute(final TableFeatureProperties property, final TableFeaturePropertiesBuilder propBuilder);
155     }
156
157     public static List<TableFeatures> toTableFeaturesReply(
158             final MultipartReplyTableFeatures ofTableFeaturesList) {
159         if (ofTableFeaturesList == null || ofTableFeaturesList.getTableFeatures() == null) {
160             return Collections.<TableFeatures>emptyList();
161         }
162         List<TableFeatures> salTableFeaturesList = new ArrayList<>();
163         TableFeaturesBuilder salTableFeatures = new TableFeaturesBuilder();
164         for (org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.
165                 multipart.reply.table.features._case.multipart.reply.table.features.TableFeatures ofTableFeatures : ofTableFeaturesList
166                 .getTableFeatures()) {
167             salTableFeatures.setTableId(ofTableFeatures.getTableId());
168             salTableFeatures.setName(ofTableFeatures.getName());
169             if (ofTableFeatures.getMetadataMatch() != null) {
170                 salTableFeatures.setMetadataMatch(new BigInteger(OFConstants.SIGNUM_UNSIGNED, ofTableFeatures.getMetadataMatch()));
171             }
172             if (ofTableFeatures.getMetadataWrite() != null) {
173                 salTableFeatures.setMetadataWrite(new BigInteger(OFConstants.SIGNUM_UNSIGNED, ofTableFeatures.getMetadataWrite()));
174             }
175             if (ofTableFeatures.getConfig() != null) {
176                 salTableFeatures.setConfig(new TableConfig(ofTableFeatures.getConfig().isOFPTCDEPRECATEDMASK()));
177             }
178             salTableFeatures.setMaxEntries(ofTableFeatures.getMaxEntries());
179             salTableFeatures.setTableProperties(toTableProperties(ofTableFeatures.getTableFeatureProperties()));
180             salTableFeaturesList.add(salTableFeatures.build());
181         }
182         return salTableFeaturesList;
183     }
184
185     private static final Map<TableFeaturesPropType, ActionExecutor> TABLE_FEATURE_PROPERTY_TYPE_TO_ACTION;
186
187     static {
188         final Builder<TableFeaturesPropType, ActionExecutor> builder = ImmutableMap.builder();
189
190         builder.put(TableFeaturesPropType.OFPTFPTINSTRUCTIONS, new ActionExecutor() {
191
192             @Override
193             public void execute(final TableFeatureProperties property, final TableFeaturePropertiesBuilder propBuilder) {
194                 org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.table.feature.prop.type.InstructionsBuilder instructionBuilder = new org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.table.feature.prop.type.InstructionsBuilder();
195                 instructionBuilder
196                         .setInstructions(new org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.table.feature.prop.type.instructions.InstructionsBuilder()
197                                 .setInstruction(setInstructionTableFeatureProperty(property)).build());
198                 propBuilder.setTableFeaturePropType(instructionBuilder.build());
199             }
200         });
201         builder.put(TableFeaturesPropType.OFPTFPTINSTRUCTIONSMISS, new ActionExecutor() {
202
203             @Override
204             public void execute(final TableFeatureProperties property, final TableFeaturePropertiesBuilder propBuilder) {
205                 org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.table.feature.prop.type.InstructionsMissBuilder instructionMissBuilder = new org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.table.feature.prop.type.InstructionsMissBuilder();
206                 instructionMissBuilder
207                         .setInstructionsMiss(new org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.table.feature.prop.type.instructions.miss.InstructionsMissBuilder()
208                                 .setInstruction(setInstructionTableFeatureProperty(property)).build());
209                 propBuilder.setTableFeaturePropType(instructionMissBuilder.build());
210             }
211         });
212         builder.put(TableFeaturesPropType.OFPTFPTNEXTTABLES, new ActionExecutor() {
213
214             @Override
215             public void execute(final TableFeatureProperties property, final TableFeaturePropertiesBuilder propBuilder) {
216                 org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.table.feature.prop.type.NextTableBuilder nextTableBuilder = new org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.table.feature.prop.type.NextTableBuilder();
217                 nextTableBuilder
218                         .setTables(new org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.table.feature.prop.type.next.table.TablesBuilder()
219                                 .setTableIds(setNextTableFeatureProperty(property)).build());
220                 propBuilder.setTableFeaturePropType(nextTableBuilder.build());
221             }
222         });
223         builder.put(TableFeaturesPropType.OFPTFPTNEXTTABLESMISS, new ActionExecutor() {
224
225             @Override
226             public void execute(final TableFeatureProperties property, final TableFeaturePropertiesBuilder propBuilder) {
227                 org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.table.feature.prop.type.NextTableMissBuilder nextTableMissBuilder = new org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.table.feature.prop.type.NextTableMissBuilder();
228                 nextTableMissBuilder
229                         .setTablesMiss(new TablesMissBuilder()
230                                 .setTableIds(setNextTableFeatureProperty(property)).build());
231                 propBuilder.setTableFeaturePropType(nextTableMissBuilder.build());
232             }
233         });
234         builder.put(TableFeaturesPropType.OFPTFPTWRITEACTIONS, new ActionExecutor() {
235
236             @Override
237             public void execute(final TableFeatureProperties property, final TableFeaturePropertiesBuilder propBuilder) {
238                 org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.table.feature.prop.type.WriteActionsBuilder writeActionsBuilder = new org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.table.feature.prop.type.WriteActionsBuilder();
239                 writeActionsBuilder
240                         .setWriteActions(new org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.table.feature.prop.type.write.actions.WriteActionsBuilder()
241                                 .setAction(setActionTableFeatureProperty(property)).build());
242                 propBuilder.setTableFeaturePropType(writeActionsBuilder.build());
243             }
244         });
245         builder.put(TableFeaturesPropType.OFPTFPTWRITEACTIONSMISS, new ActionExecutor() {
246
247             @Override
248             public void execute(final TableFeatureProperties property, final TableFeaturePropertiesBuilder propBuilder) {
249                 org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.table.feature.prop.type.WriteActionsMissBuilder writeActionsMissBuilder = new org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.table.feature.prop.type.WriteActionsMissBuilder();
250                 writeActionsMissBuilder
251                         .setWriteActionsMiss(new org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.table.feature.prop.type.write.actions.miss.WriteActionsMissBuilder()
252                                 .setAction(setActionTableFeatureProperty(property)).build());
253                 propBuilder.setTableFeaturePropType(writeActionsMissBuilder.build());
254             }
255         });
256         builder.put(TableFeaturesPropType.OFPTFPTAPPLYACTIONS, new ActionExecutor() {
257
258             @Override
259             public void execute(final TableFeatureProperties property, final TableFeaturePropertiesBuilder propBuilder) {
260                 org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.table.feature.prop.type.ApplyActionsBuilder applyActionsBuilder = new org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.table.feature.prop.type.ApplyActionsBuilder();
261                 applyActionsBuilder
262                         .setApplyActions(new org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.table.feature.prop.type.apply.actions.ApplyActionsBuilder()
263                                 .setAction(setActionTableFeatureProperty(property)).build());
264                 propBuilder.setTableFeaturePropType(applyActionsBuilder.build());
265             }
266         });
267         builder.put(TableFeaturesPropType.OFPTFPTAPPLYACTIONSMISS, new ActionExecutor() {
268
269             @Override
270             public void execute(final TableFeatureProperties property, final TableFeaturePropertiesBuilder propBuilder) {
271                 org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.table.feature.prop.type.ApplyActionsMissBuilder applyActionsMissBuilder = new org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.table.feature.prop.type.ApplyActionsMissBuilder();
272                 applyActionsMissBuilder
273                         .setApplyActionsMiss(new org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.table.feature.prop.type.apply.actions.miss.ApplyActionsMissBuilder()
274                                 .setAction(setActionTableFeatureProperty(property)).build());
275                 propBuilder.setTableFeaturePropType(applyActionsMissBuilder.build());
276             }
277         });
278         builder.put(TableFeaturesPropType.OFPTFPTMATCH, new ActionExecutor() {
279
280             @Override
281             public void execute(final TableFeatureProperties property, final TableFeaturePropertiesBuilder propBuilder) {
282                 MatchSetfieldBuilder matchBuilder = new MatchSetfieldBuilder();
283
284                 matchBuilder.setSetFieldMatch(setSetFieldTableFeatureProperty(property, true));
285                 propBuilder.setTableFeaturePropType(new MatchBuilder().setMatchSetfield(matchBuilder.build()).build());
286             }
287         });
288         builder.put(TableFeaturesPropType.OFPTFPTWILDCARDS, new ActionExecutor() {
289
290             @Override
291             public void execute(final TableFeatureProperties property, final TableFeaturePropertiesBuilder propBuilder) {
292                 WildcardSetfieldBuilder wildcardsBuilder = new WildcardSetfieldBuilder();
293                 wildcardsBuilder.setSetFieldMatch(setSetFieldTableFeatureProperty(property, false));
294                 propBuilder.setTableFeaturePropType(new WildcardsBuilder().setWildcardSetfield(wildcardsBuilder.build()).build());
295             }
296         });
297         builder.put(TableFeaturesPropType.OFPTFPTWRITESETFIELD, new ActionExecutor() {
298
299             @Override
300             public void execute(final TableFeatureProperties property, final TableFeaturePropertiesBuilder propBuilder) {
301                 WriteSetfieldBuilder writeSetfieldBuilder = new WriteSetfieldBuilder();
302                 writeSetfieldBuilder.setSetFieldMatch(setSetFieldTableFeatureProperty(property, false));
303                 propBuilder.setTableFeaturePropType(new
304                         org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.table.feature.prop.type.WriteSetfieldBuilder().setWriteSetfield(writeSetfieldBuilder.build()).build());
305             }
306         });
307         builder.put(TableFeaturesPropType.OFPTFPTWRITESETFIELDMISS, new ActionExecutor() {
308
309             @Override
310             public void execute(final TableFeatureProperties property, final TableFeaturePropertiesBuilder propBuilder) {
311                 WriteSetfieldMissBuilder writeSetfieldMissBuilder = new WriteSetfieldMissBuilder();
312                 writeSetfieldMissBuilder.setSetFieldMatch(setSetFieldTableFeatureProperty(property, false));
313                 propBuilder.setTableFeaturePropType(new
314                         org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.table.feature.prop.type.WriteSetfieldMissBuilder().setWriteSetfieldMiss(writeSetfieldMissBuilder.build()).build());
315             }
316         });
317         builder.put(TableFeaturesPropType.OFPTFPTAPPLYSETFIELD, new ActionExecutor() {
318
319             @Override
320             public void execute(final TableFeatureProperties property, final TableFeaturePropertiesBuilder propBuilder) {
321                 ApplySetfieldBuilder applySetfieldBuilder = new ApplySetfieldBuilder();
322                 applySetfieldBuilder.setSetFieldMatch(setSetFieldTableFeatureProperty(property, false));
323                 propBuilder.setTableFeaturePropType(new
324                         org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.table.feature.prop.type.ApplySetfieldBuilder().setApplySetfield(applySetfieldBuilder.build()).build());
325             }
326         });
327         builder.put(TableFeaturesPropType.OFPTFPTAPPLYSETFIELDMISS, new ActionExecutor() {
328
329             @Override
330             public void execute(final TableFeatureProperties property, final TableFeaturePropertiesBuilder propBuilder) {
331                 ApplySetfieldMissBuilder applySetfieldMissBuilder = new ApplySetfieldMissBuilder();
332                 applySetfieldMissBuilder.setSetFieldMatch(setSetFieldTableFeatureProperty(property, false));
333                 propBuilder.setTableFeaturePropType(new
334                         org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.table.feature.prop.type.ApplySetfieldMissBuilder().setApplySetfieldMiss(applySetfieldMissBuilder.build()).build());
335             }
336         });
337
338         builder.put(TableFeaturesPropType.OFPTFPTEXPERIMENTER, new ActionExecutor() {
339
340             @Override
341             public void execute(final TableFeatureProperties property, final TableFeaturePropertiesBuilder propBuilder) {
342                 LOG.debug("Experimenter Table features is unhandled");
343             }
344         });
345
346         builder.put(TableFeaturesPropType.OFPTFPTEXPERIMENTERMISS, new ActionExecutor() {
347
348             @Override
349             public void execute(final TableFeatureProperties property, final TableFeaturePropertiesBuilder propBuilder) {
350                 LOG.debug("Experimenter miss Table features is unhandled");
351             }
352         });
353
354         TABLE_FEATURE_PROPERTY_TYPE_TO_ACTION = builder.build();
355
356     }
357
358     private static TableProperties toTableProperties(final List<TableFeatureProperties> ofTablePropertiesList) {
359         if (ofTablePropertiesList == null) {
360             return new TablePropertiesBuilder()
361                     .setTableFeatureProperties(
362                             Collections
363                                     .<org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.table.features.table.properties.TableFeatureProperties>emptyList())
364                     .build();
365         }
366         List<org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.table.features.table.properties.TableFeatureProperties> salTablePropertiesList = new ArrayList<>();
367         TableFeaturePropertiesBuilder propBuilder = new TableFeaturePropertiesBuilder();
368         int index = 0;
369         for (TableFeatureProperties property : ofTablePropertiesList) {
370             TableFeaturesPropType propType = property.getType();
371
372             ActionExecutor actionExecutor = TABLE_FEATURE_PROPERTY_TYPE_TO_ACTION.get(propType);
373             if (actionExecutor != null) {
374                 actionExecutor.execute(property, propBuilder);
375             } else {
376                 LOG.error("Unsupported table feature property : " + propType);
377             }
378             propBuilder.setOrder(index);
379
380             salTablePropertiesList.add(propBuilder.build());
381             index += 1;
382         }
383
384         return new TablePropertiesBuilder().setTableFeatureProperties(salTablePropertiesList).build();
385     }
386
387     private static List<Instruction> setInstructionTableFeatureProperty(final TableFeatureProperties properties) {
388         List<Instruction> instructionList = new ArrayList<>();
389         org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder builder = new org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder();
390
391         int index = 0;
392         for (org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731
393                 .instructions.grouping.Instruction currInstruction : properties
394                 .getAugmentation(InstructionRelatedTableFeatureProperty.class).getInstruction()) {
395             InstructionChoice currInstructionType = currInstruction.getInstructionChoice();
396
397             if (currInstructionType instanceof GotoTableCase) {
398                 builder.setInstruction((new GoToTableCaseBuilder()
399                         .build()));
400             } else if (currInstructionType instanceof WriteMetadataCase) {
401                 builder.setInstruction((new WriteMetadataCaseBuilder()
402                         .build()));
403             } else if (currInstructionType instanceof WriteActionsCase) {
404                 builder.setInstruction((new WriteActionsCaseBuilder()
405                         .build()));
406             } else if (currInstructionType instanceof ApplyActionsCase) {
407                 builder.setInstruction((new ApplyActionsCaseBuilder()
408                         .build()));
409             } else if (currInstructionType instanceof ClearActionsCase) {
410                 builder.setInstruction((new ClearActionsCaseBuilder()
411                         .build()));
412             } else if (currInstructionType instanceof MeterCase) {
413                 builder.setInstruction((new MeterCaseBuilder()
414                         .build()));
415             } else if (currInstructionType instanceof ExperimenterIdCase) {
416                 // TODO: Experimenter instructions are unhandled
417             }
418
419             builder.setOrder(index);
420             index += 1;
421
422             instructionList.add(builder.build());
423         }
424         return instructionList;
425     }
426
427     private static List<Short> setNextTableFeatureProperty(final TableFeatureProperties properties) {
428         List<Short> nextTableIdsList = new ArrayList<>();
429         for (NextTableIds tableId : properties.getAugmentation(NextTableRelatedTableFeatureProperty.class)
430                 .getNextTableIds()) {
431             nextTableIdsList.add(tableId.getTableId());
432         }
433         return nextTableIdsList;
434     }
435
436     private static final Map<Class<?>, org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action> OF_TO_SAL_ACTION;
437
438     static {
439         Builder<Class<?>, org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action> builder = ImmutableMap.builder();
440
441         builder.put(OutputActionCase.class, new OutputActionCaseBuilder().build());
442         builder.put(GroupCase.class, new GroupActionCaseBuilder().build());
443         builder.put(CopyTtlOutCase.class, new CopyTtlOutCaseBuilder().build());
444         builder.put(CopyTtlInCase.class, new CopyTtlInCaseBuilder().build());
445         builder.put(SetMplsTtlCase.class, new SetMplsTtlActionCaseBuilder().build());
446         builder.put(DecMplsTtlCase.class, new DecMplsTtlCaseBuilder().build());
447         builder.put(PushVlanCase.class, new PushVlanActionCaseBuilder().build());
448         builder.put(PopVlanCase.class, new PopVlanActionCaseBuilder().build());
449         builder.put(PushMplsCase.class, new PushMplsActionCaseBuilder().build());
450         builder.put(PopMplsCase.class, new PopMplsActionCaseBuilder().build());
451         builder.put(SetQueueCase.class, new SetQueueActionCaseBuilder().build());
452         builder.put(SetNwTtlCase.class, new SetNwTtlActionCaseBuilder().build());
453         builder.put(DecNwTtlCase.class, new DecNwTtlCaseBuilder().build());
454         builder.put(SetFieldCase.class, new SetFieldCaseBuilder().build());
455         builder.put(PushPbbCase.class, new PushPbbActionCaseBuilder().build());
456         builder.put(PopPbbCase.class, new PopPbbActionCaseBuilder().build());
457         builder.put(SetNwSrcCase.class, new SetNwSrcActionCaseBuilder().build());
458         builder.put(SetNwDstCase.class, new SetNwDstActionCaseBuilder().build());
459
460         OF_TO_SAL_ACTION = builder.build();
461     }
462
463     private static List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action> setActionTableFeatureProperty(
464             final TableFeatureProperties properties) {
465         List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action> actionList = new ArrayList<>();
466         int order = 0;
467         for (Action action : properties
468                 .getAugmentation(ActionRelatedTableFeatureProperty.class).getAction()) {
469             if (action != null && null != action.getActionChoice()) {
470                 org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder actionBuilder = new org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder();
471
472                 actionBuilder.setOrder(order++);
473                 ActionChoice actionType = action.getActionChoice();
474                 org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action salAction = OF_TO_SAL_ACTION.get(actionType.getImplementedInterface());
475
476                 actionBuilder.setAction(salAction);
477                 actionList.add(actionBuilder.build());
478             }
479         }
480         return actionList;
481     }
482
483     private static final Map<Class<? extends MatchField>, Class<? extends org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.MatchField>> OF_TO_SAL_TABLE_FEATURE_PROPERTIES;
484
485     static {
486         final Builder<Class<? extends MatchField>, Class<? extends org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.MatchField>> builder = ImmutableMap.builder();
487
488         builder.put(ArpOp.class, org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.ArpOp.class);
489         builder.put(ArpSha.class, org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.ArpSha.class);
490         builder.put(ArpSpa.class, org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.ArpSpa.class);
491         builder.put(ArpTha.class, org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.ArpTha.class);
492         builder.put(ArpTpa.class, org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.ArpTpa.class);
493         builder.put(EthDst.class, org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.EthDst.class);
494         builder.put(EthSrc.class, org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.EthSrc.class);
495         builder.put(EthType.class, org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.EthType.class);
496         builder.put(Icmpv4Code.class, org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.Icmpv4Code.class);
497         builder.put(Icmpv4Type.class, org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.Icmpv4Type.class);
498         builder.put(Icmpv6Code.class, org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.Icmpv6Code.class);
499         builder.put(Icmpv6Type.class, org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.Icmpv6Type.class);
500         builder.put(InPhyPort.class, org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.InPhyPort.class);
501         builder.put(InPort.class, org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.InPort.class);
502         builder.put(IpDscp.class, org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.IpDscp.class);
503         builder.put(IpEcn.class, org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.IpEcn.class);
504         builder.put(IpProto.class, org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.IpProto.class);
505         builder.put(Ipv4Dst.class, org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.Ipv4Dst.class);
506         builder.put(Ipv4Src.class, org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.Ipv4Src.class);
507         builder.put(Ipv6Dst.class, org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.Ipv6Dst.class);
508         builder.put(Ipv6Exthdr.class, org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.Ipv6Exthdr.class);
509         builder.put(Ipv6Flabel.class, org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.Ipv6Flabel.class);
510         builder.put(Ipv6NdSll.class, org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.Ipv6NdSll.class);
511         builder.put(Ipv6NdTarget.class, org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.Ipv6NdTarget.class);
512         builder.put(Ipv6NdTll.class, org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.Ipv6NdTll.class);
513         builder.put(Ipv6Src.class, org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.Ipv6Src.class);
514         builder.put(Metadata.class, org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.Metadata.class);
515         builder.put(MplsBos.class, org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.MplsBos.class);
516         builder.put(MplsLabel.class, org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.MplsLabel.class);
517         builder.put(MplsTc.class, org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.MplsTc.class);
518         builder.put(PbbIsid.class, org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.PbbIsid.class);
519         builder.put(SctpDst.class, org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.SctpDst.class);
520         builder.put(SctpSrc.class, org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.SctpSrc.class);
521         builder.put(TcpDst.class, org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TcpDst.class);
522         builder.put(TcpSrc.class, org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TcpSrc.class);
523         builder.put(TunnelId.class, org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TunnelId.class);
524         builder.put(UdpDst.class, org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.UdpDst.class);
525         builder.put(UdpSrc.class, org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.UdpSrc.class);
526         builder.put(VlanPcp.class, org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.VlanPcp.class);
527         builder.put(VlanVid.class, org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.VlanVid.class);
528
529         OF_TO_SAL_TABLE_FEATURE_PROPERTIES = builder.build();
530     }
531
532     private static List<SetFieldMatch> setSetFieldTableFeatureProperty(final TableFeatureProperties properties,
533                                                                        final boolean setHasMask) {
534         List<SetFieldMatch> setFieldMatchList = new ArrayList<>();
535         SetFieldMatchBuilder setFieldMatchBuilder = new SetFieldMatchBuilder();
536
537         Class<? extends MatchField> ofMatchField = null;
538
539         // This handles only OpenflowBasicClass oxm class.
540         for (MatchEntry currMatch : properties.getAugmentation(OxmRelatedTableFeatureProperty.class)
541                 .getMatchEntry()) {
542             ofMatchField = currMatch.getOxmMatchField();
543             Class<? extends org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.MatchField> salMatchField = null;
544             setFieldMatchBuilder.setMatchType(salMatchField);
545             if (setHasMask) {
546                 setFieldMatchBuilder.setHasMask(currMatch.isHasMask());
547             }
548             setFieldMatchBuilder.setMatchType(OF_TO_SAL_TABLE_FEATURE_PROPERTIES.get(ofMatchField));
549             setFieldMatchList.add(setFieldMatchBuilder.build());
550         }
551         return setFieldMatchList;
552     }
553
554 }