2 * Copyright (c) 2013, 2015 IBM Corporation and others. All rights reserved.
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
9 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.flow;
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;
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;
47 * Converts Openflow 1.3+ specific instructions to MD-SAL format flow instruction.
53 * VersionConvertorData data = new VersionConvertorData(version);
54 * Optional<Instructions> salFlowInstruction = convertorManager.convert(ofFlowInstructions, data);
58 public final class FlowInstructionResponseConvertor extends Convertor<
59 List<org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions
60 .grouping.Instruction>,
62 VersionConvertorData> {
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);
68 public Collection<Class<?>> getTypes() {
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();
77 List<Instruction> salInstructionList = new ArrayList<>();
78 int instructionTreeNodekey = 0;
79 org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction salInstruction;
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();
88 final ActionResponseConvertorData actionResponseConvertorData =
89 new ActionResponseConvertorData(data.getVersion());
90 actionResponseConvertorData.setActionPath(ActionPath.FLOWS_STATISTICS_UPDATE_APPLY_ACTIONS);
92 final Optional<List<Action>> actions = getConvertorExecutor().convert(
93 actionsInstruction.getApplyActions().getAction(), actionResponseConvertorData);
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();
106 GoToTableCaseBuilder goToTableCaseBuilder = new GoToTableCaseBuilder();
107 GoToTableBuilder goToTableBuilder = new GoToTableBuilder();
108 goToTableBuilder.setTableId(gotoTableCase.getGotoTable().getTableId());
109 goToTableCaseBuilder.setGoToTable(goToTableBuilder.build());
111 salInstruction = goToTableCaseBuilder.build();
112 } else if (switchInst.getInstructionChoice() instanceof MeterCase) {
113 MeterCase meterIdInstruction = (MeterCase) switchInst.getInstructionChoice();
115 MeterCaseBuilder meterCaseBuilder = new MeterCaseBuilder();
116 MeterBuilder meterBuilder = new MeterBuilder();
117 meterBuilder.setMeterId(new MeterId(meterIdInstruction.getMeter().getMeterId()));
118 meterCaseBuilder.setMeter(meterBuilder.build());
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();
126 final ActionResponseConvertorData actionResponseConvertorData =
127 new ActionResponseConvertorData(data.getVersion());
128 actionResponseConvertorData.setActionPath(ActionPath.FLOWS_STATISTICS_UPDATE_WRITE_ACTIONS);
130 final Optional<List<Action>> actions = getConvertorExecutor().convert(
131 writeActionsCase.getWriteActions().getAction(), actionResponseConvertorData);
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();
156 InstructionBuilder instBuilder = new InstructionBuilder();
157 instBuilder.setInstruction(salInstruction);
158 instBuilder.setKey(new InstructionKey(instructionTreeNodekey));
159 instBuilder.setOrder(instructionTreeNodekey);
160 instructionTreeNodekey++;
161 salInstructionList.add(instBuilder.build());
165 instructionsBuilder.setInstruction(salInstructionList);
166 return instructionsBuilder.build();