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;
45 import org.opendaylight.yangtools.yang.binding.DataContainer;
48 * 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.grouping.Instruction>,
61 VersionConvertorData> {
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);
66 public Collection<Class<? extends DataContainer>> getTypes() {
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();
74 List<Instruction> salInstructionList = new ArrayList<>();
75 int instructionTreeNodekey = 0;
76 org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction salInstruction;
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();
85 final ActionResponseConvertorData actionResponseConvertorData = new ActionResponseConvertorData(data.getVersion());
86 actionResponseConvertorData.setActionPath(ActionPath.FLOWSSTATISTICSUPDATE_FLOWANDSTATISTICSMAPLIST_INSTRUCTIONS_INSTRUCTION_INSTRUCTION_APPLYACTIONSCASE_APPLYACTIONS_ACTION_ACTION);
88 final Optional<List<Action>> actions = getConvertorExecutor().convert(
89 actionsInstruction.getApplyActions().getAction(), actionResponseConvertorData);
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());
100 GoToTableCaseBuilder goToTableCaseBuilder = new GoToTableCaseBuilder();
101 GoToTableBuilder goToTableBuilder = new GoToTableBuilder();
102 goToTableBuilder.setTableId(gotoTableCase.getGotoTable().getTableId());
103 goToTableCaseBuilder.setGoToTable(goToTableBuilder.build());
105 salInstruction = goToTableCaseBuilder.build();
106 } else if (switchInst.getInstructionChoice() instanceof MeterCase) {
107 MeterCase meterIdInstruction = ((MeterCase) switchInst.getInstructionChoice());
109 MeterCaseBuilder meterCaseBuilder = new MeterCaseBuilder();
110 MeterBuilder meterBuilder = new MeterBuilder();
111 meterBuilder.setMeterId(new MeterId(meterIdInstruction.getMeter().getMeterId()));
112 meterCaseBuilder.setMeter(meterBuilder.build());
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();
120 final ActionResponseConvertorData actionResponseConvertorData = new ActionResponseConvertorData(data.getVersion());
121 actionResponseConvertorData.setActionPath(ActionPath.FLOWSSTATISTICSUPDATE_FLOWANDSTATISTICSMAPLIST_INSTRUCTIONS_INSTRUCTION_INSTRUCTION_WRITEACTIONSCASE_WRITEACTIONS_ACTION_ACTION);
123 final Optional<List<Action>> actions = getConvertorExecutor().convert(
124 writeActionsCase.getWriteActions().getAction(), actionResponseConvertorData);
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();
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());
151 instructionsBuilder.setInstruction(salInstructionList);
152 return instructionsBuilder.build();