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