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