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