Fixup Augmentable and Identifiable methods changing
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / flow / FlowInstructionResponseConvertor.java
1 /*
2  * Copyright (c) 2013, 2015 IBM Corporation 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.flow;
10
11 import java.math.BigInteger;
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.Collections;
15 import java.util.List;
16 import java.util.Optional;
17 import java.util.Set;
18 import org.opendaylight.openflowplugin.api.OFConstants;
19 import org.opendaylight.openflowplugin.extension.api.path.ActionPath;
20 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.data.ActionResponseConvertorData;
21 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.Convertor;
22 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.data.VersionConvertorData;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ClearActionsCaseBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.GoToTableCaseBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.MeterCaseBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.WriteActionsCaseBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.WriteMetadataCaseBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.go.to.table._case.GoToTableBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.meter._case.MeterBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.write.actions._case.WriteActionsBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.write.metadata._case.WriteMetadataBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterId;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.ApplyActionsCase;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.GotoTableCase;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.MeterCase;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.WriteActionsCase;
45
46 /**
47  * Converts Openflow 1.3+ specific instructions to MD-SAL format flow instruction.
48  *
49  * <p>
50  * Example usage:
51  * <pre>
52  * {@code
53  * VersionConvertorData data = new VersionConvertorData(version);
54  * Optional<Instructions> salFlowInstruction = convertorManager.convert(ofFlowInstructions, data);
55  * }
56  * </pre>
57  */
58 public final class FlowInstructionResponseConvertor extends Convertor<
59         List<org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions
60             .grouping.Instruction>,
61         Instructions,
62         VersionConvertorData> {
63
64     private static final Set<Class<?>> TYPES = Collections.singleton(org.opendaylight.yang.gen.v1.urn.opendaylight
65             .openflow.common.instruction.rev130731.instructions.grouping.Instruction.class);
66
67     @Override
68     public Collection<Class<?>> getTypes() {
69         return TYPES;
70     }
71
72     @Override
73     public Instructions convert(List<org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction
74             .rev130731.instructions.grouping.Instruction> source, VersionConvertorData data) {
75         InstructionsBuilder instructionsBuilder = new InstructionsBuilder();
76
77         List<Instruction> salInstructionList = new ArrayList<>();
78         int instructionTreeNodekey = 0;
79         org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction salInstruction;
80
81         for (org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping
82                 .Instruction switchInst : source) {
83             if (switchInst.getInstructionChoice() instanceof ApplyActionsCase) {
84                 ApplyActionsCase actionsInstruction = (ApplyActionsCase) switchInst.getInstructionChoice();
85                 ApplyActionsCaseBuilder applyActionsCaseBuilder = new ApplyActionsCaseBuilder();
86                 ApplyActionsBuilder applyActionsBuilder = new ApplyActionsBuilder();
87
88                 final ActionResponseConvertorData actionResponseConvertorData =
89                         new ActionResponseConvertorData(data.getVersion());
90                 actionResponseConvertorData.setActionPath(ActionPath.FLOWS_STATISTICS_UPDATE_APPLY_ACTIONS);
91
92                 final Optional<List<Action>> actions = getConvertorExecutor().convert(
93                         actionsInstruction.getApplyActions().getAction(), actionResponseConvertorData);
94
95                 applyActionsBuilder.setAction(FlowConvertorUtil.wrapActionList(
96                         actions.orElse(Collections.emptyList())));
97                 applyActionsCaseBuilder.setApplyActions(applyActionsBuilder.build());
98                 salInstruction = applyActionsCaseBuilder.build();
99             } else if (switchInst.getInstructionChoice() instanceof org.opendaylight.yang.gen.v1.urn.opendaylight
100                     .openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.ClearActionsCase) {
101                 ClearActionsCaseBuilder clearActionsCaseBuilder = new ClearActionsCaseBuilder();
102                 salInstruction = clearActionsCaseBuilder.build();
103             } else if (switchInst.getInstructionChoice() instanceof GotoTableCase) {
104                 GotoTableCase gotoTableCase = (GotoTableCase) switchInst.getInstructionChoice();
105
106                 GoToTableCaseBuilder goToTableCaseBuilder = new GoToTableCaseBuilder();
107                 GoToTableBuilder goToTableBuilder = new GoToTableBuilder();
108                 goToTableBuilder.setTableId(gotoTableCase.getGotoTable().getTableId());
109                 goToTableCaseBuilder.setGoToTable(goToTableBuilder.build());
110
111                 salInstruction = goToTableCaseBuilder.build();
112             } else if (switchInst.getInstructionChoice() instanceof MeterCase) {
113                 MeterCase meterIdInstruction = (MeterCase) switchInst.getInstructionChoice();
114
115                 MeterCaseBuilder meterCaseBuilder = new MeterCaseBuilder();
116                 MeterBuilder meterBuilder = new MeterBuilder();
117                 meterBuilder.setMeterId(new MeterId(meterIdInstruction.getMeter().getMeterId()));
118                 meterCaseBuilder.setMeter(meterBuilder.build());
119
120                 salInstruction = meterCaseBuilder.build();
121             } else if (switchInst.getInstructionChoice() instanceof WriteActionsCase) {
122                 WriteActionsCase writeActionsCase = (WriteActionsCase) switchInst.getInstructionChoice();
123                 WriteActionsCaseBuilder writeActionsCaseBuilder = new WriteActionsCaseBuilder();
124                 WriteActionsBuilder writeActionsBuilder = new WriteActionsBuilder();
125
126                 final ActionResponseConvertorData actionResponseConvertorData =
127                         new ActionResponseConvertorData(data.getVersion());
128                 actionResponseConvertorData.setActionPath(ActionPath.FLOWS_STATISTICS_UPDATE_WRITE_ACTIONS);
129
130                 final Optional<List<Action>> actions = getConvertorExecutor().convert(
131                         writeActionsCase.getWriteActions().getAction(), actionResponseConvertorData);
132
133                 writeActionsBuilder.setAction(FlowConvertorUtil.wrapActionList(
134                         actions.orElse(Collections.emptyList())));
135                 writeActionsCaseBuilder.setWriteActions(writeActionsBuilder.build());
136                 salInstruction = writeActionsCaseBuilder.build();
137             } else if (switchInst.getInstructionChoice() instanceof org.opendaylight.yang.gen.v1.urn.opendaylight
138                     .openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.WriteMetadataCase) {
139                 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction
140                     .grouping.instruction.choice.WriteMetadataCase writeMetadataCase =
141                         (org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731
142                             .instruction.grouping.instruction.choice.WriteMetadataCase) switchInst
143                                 .getInstructionChoice();
144                 WriteMetadataCaseBuilder writeMetadataCaseBuilder = new WriteMetadataCaseBuilder();
145                 WriteMetadataBuilder writeMetadataBuilder = new WriteMetadataBuilder();
146                 writeMetadataBuilder.setMetadata(new BigInteger(OFConstants.SIGNUM_UNSIGNED,
147                         writeMetadataCase.getWriteMetadata().getMetadata()));
148                 writeMetadataBuilder.setMetadataMask(new BigInteger(OFConstants.SIGNUM_UNSIGNED,
149                         writeMetadataCase.getWriteMetadata().getMetadataMask()));
150                 writeMetadataCaseBuilder.setWriteMetadata(writeMetadataBuilder.build());
151                 salInstruction = writeMetadataCaseBuilder.build();
152             } else {
153                 continue;
154             }
155
156             InstructionBuilder instBuilder = new InstructionBuilder();
157             instBuilder.setInstruction(salInstruction);
158             instBuilder.withKey(new InstructionKey(instructionTreeNodekey));
159             instBuilder.setOrder(instructionTreeNodekey);
160             instructionTreeNodekey++;
161             salInstructionList.add(instBuilder.build());
162
163         }
164
165         instructionsBuilder.setInstruction(salInstructionList);
166         return instructionsBuilder.build();
167     }
168 }