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.Collection;
13 import java.util.Collections;
14 import java.util.List;
15 import java.util.Optional;
17 import org.opendaylight.openflowplugin.api.OFConstants;
18 import org.opendaylight.openflowplugin.extension.api.path.ActionPath;
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.util.BindingMap;
45 import org.opendaylight.yangtools.yang.common.Uint64;
48 * Converts Openflow 1.3+ specific instructions to MD-SAL format flow instruction.
54 * VersionConvertorData data = new VersionConvertorData(version);
55 * Optional<Instructions> salFlowInstruction = convertorManager.convert(ofFlowInstructions, data);
59 public final class FlowInstructionResponseConvertor extends Convertor<
60 List<org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions
61 .grouping.Instruction>,
63 VersionConvertorData> {
65 private static final Set<Class<?>> TYPES = Collections.singleton(org.opendaylight.yang.gen.v1.urn.opendaylight
66 .openflow.common.instruction.rev130731.instructions.grouping.Instruction.class);
69 public Collection<Class<?>> getTypes() {
74 public Instructions convert(final List<org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction
75 .rev130731.instructions.grouping.Instruction> source, final VersionConvertorData data) {
77 BindingMap.Builder<InstructionKey, Instruction> salInstructionList = BindingMap.builder();
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(List.of())));
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(Uint64.valueOf(new BigInteger(OFConstants.SIGNUM_UNSIGNED,
147 writeMetadataCase.getWriteMetadata().getMetadata())));
148 writeMetadataBuilder.setMetadataMask(Uint64.valueOf(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.withKey(new InstructionKey(instructionTreeNodekey));
159 instBuilder.setOrder(instructionTreeNodekey);
160 instructionTreeNodekey++;
161 salInstructionList.add(instBuilder.build());
165 return new InstructionsBuilder()
166 .setInstruction(salInstructionList.build())