Merge "Move match converter to new converter system"
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / action / ActionConvertor.java
1 /*
2  * Copyright (c) 2014 Ericsson India Global Services Pvt Ltd. 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.action;
10
11 import com.google.common.collect.Ordering;
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.openflow.md.core.sal.convertor.action.cases.SalToOfCopyTtlInCase;
19 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.SalToOfCopyTtlOutCase;
20 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.SalToOfDecMplsTtlCase;
21 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.SalToOfDecNwTtlCase;
22 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.SalToOfDropActionCase;
23 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.SalToOfGeneralExtensionGroupingCase;
24 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.SalToOfGroupActionCase;
25 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.SalToOfOutputActionCase;
26 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.SalToOfPopMplsActionCase;
27 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.SalToOfPopPbbActionCase;
28 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.SalToOfPopVlanActionCase;
29 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.SalToOfPopVlanActionV10Case;
30 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.SalToOfPushMplsActionCase;
31 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.SalToOfPushPbbActionCase;
32 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.SalToOfPushVlanActionCase;
33 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.SalToOfSetDlDstActionCase;
34 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.SalToOfSetDlDstActionV10Case;
35 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.SalToOfSetDlSrcActionCase;
36 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.SalToOfSetDlSrcActionV10Case;
37 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.SalToOfSetFieldCase;
38 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.SalToOfSetFieldV10Case;
39 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.SalToOfSetMplsTtlActionCase;
40 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.SalToOfSetNwDstActionCase;
41 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.SalToOfSetNwDstActionV10Case;
42 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.SalToOfSetNwSrcActionCase;
43 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.SalToOfSetNwSrcActionV10Case;
44 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.SalToOfSetNwTosActionCase;
45 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.SalToOfSetNwTosActionV10Case;
46 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.SalToOfSetNwTtlActionCase;
47 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.SalToOfSetQueueActionCase;
48 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.SalToOfSetTpDstActionCase;
49 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.SalToOfSetTpDstActionV10Case;
50 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.SalToOfSetTpSrcActionCase;
51 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.SalToOfSetTpSrcActionV10Case;
52 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.SalToOfSetVlanIdActionCase;
53 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.SalToOfSetVlanIdActionV10Case;
54 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.SalToOfSetVlanPcpActionCase;
55 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.SalToOfSetVlanPcpActionV10Case;
56 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.SalToOfStripVlanActionCase;
57 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.SalToOfStripVlanActionV10Case;
58 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.SalToOfVendorCodecCase;
59 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.data.ActionConvertorData;
60 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.Convertor;
61 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.ConvertorProcessor;
62 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.OrderComparator;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action;
64 import org.opendaylight.yangtools.yang.binding.DataContainer;
65
66 /**
67  * Converts SAL actions into OF Library actions
68  *
69  * Example usage:
70  * <pre>
71  * {@code
72  * ActionConvertorData data = new ActionConvertorData(version);
73  * data.setDatapathId(datapathId);
74  * data.setIpProtocol(ipProtocol);
75  * Optional<List<Action>> ofActions = convertorManager.convert(salActions, data);
76  * }
77  * </pre>
78  */
79 public final class ActionConvertor extends Convertor<
80         List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action>,
81         List<Action>,
82         ActionConvertorData> {
83
84     private static final ConvertorProcessor<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action, Action, ActionConvertorData> PROCESSOR = new ConvertorProcessor<
85             org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action,
86             Action,
87             ActionConvertorData>()
88             // Set default rule, what will be used if no rule match is found
89             .setDefaultCase(new SalToOfVendorCodecCase())
90             // Add rules for each action type
91             .addCase(new SalToOfCopyTtlInCase())
92             .addCase(new SalToOfCopyTtlOutCase())
93             .addCase(new SalToOfDecMplsTtlCase())
94             .addCase(new SalToOfDecNwTtlCase())
95             .addCase(new SalToOfDropActionCase())
96             .addCase(new SalToOfGroupActionCase())
97             .addCase(new SalToOfOutputActionCase())
98             .addCase(new SalToOfPopMplsActionCase())
99             .addCase(new SalToOfPopPbbActionCase())
100             .addCase(new SalToOfPopVlanActionCase())
101             .addCase(new SalToOfPopVlanActionV10Case())
102             .addCase(new SalToOfPushMplsActionCase())
103             .addCase(new SalToOfPushPbbActionCase())
104             .addCase(new SalToOfPushVlanActionCase())
105             .addCase(new SalToOfSetFieldCase())
106             .addCase(new SalToOfSetFieldV10Case())
107             .addCase(new SalToOfSetMplsTtlActionCase())
108             .addCase(new SalToOfSetNwTtlActionCase())
109             .addCase(new SalToOfSetQueueActionCase())
110             // Openflow 1.0 actions, with support for Openflow 1.3
111             .addCase(new SalToOfSetVlanIdActionCase())
112             .addCase(new SalToOfSetVlanIdActionV10Case())
113             .addCase(new SalToOfSetVlanPcpActionCase())
114             .addCase(new SalToOfSetVlanPcpActionV10Case())
115             .addCase(new SalToOfStripVlanActionCase())
116             .addCase(new SalToOfStripVlanActionV10Case())
117             .addCase(new SalToOfSetDlSrcActionCase())
118             .addCase(new SalToOfSetDlSrcActionV10Case())
119             .addCase(new SalToOfSetDlDstActionCase())
120             .addCase(new SalToOfSetDlDstActionV10Case())
121             .addCase(new SalToOfSetNwSrcActionCase())
122             .addCase(new SalToOfSetNwSrcActionV10Case())
123             .addCase(new SalToOfSetNwDstActionCase())
124             .addCase(new SalToOfSetNwDstActionV10Case())
125             .addCase(new SalToOfSetTpSrcActionCase())
126             .addCase(new SalToOfSetTpSrcActionV10Case())
127             .addCase(new SalToOfSetTpDstActionCase())
128             .addCase(new SalToOfSetTpDstActionV10Case())
129             .addCase(new SalToOfSetNwTosActionCase())
130             .addCase(new SalToOfSetNwTosActionV10Case())
131             // Try to convert action grouping using converters from openflowplugin-extension
132             .addCase(new SalToOfGeneralExtensionGroupingCase());
133     private static final Set<Class<?>> TYPES = Collections.singleton(org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action.class);
134     private static final Ordering<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action> ACTION_ORDERING =
135             Ordering.from(OrderComparator.<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action>build());
136
137     @Override
138     public Collection<Class<?>> getTypes() {
139         return TYPES;
140     }
141
142     @Override
143     public List<Action> convert(List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action> source, ActionConvertorData data) {
144         // Prepare list of converted actions
145         final List<Action> result = new ArrayList<>();
146
147         // Iterate over SAL actions, run them through tokenizer and then add them to list of converted actions
148         if (source != null) {
149             final List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action> sortedActions =
150                     ACTION_ORDERING.sortedCopy(source);
151
152             for (final org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action action : sortedActions) {
153                 final Optional<Action> convertedAction = PROCESSOR.process(action.getAction(), data, getConvertorExecutor());
154
155                 if (convertedAction.isPresent()) {
156                     result.add(convertedAction.get());
157                 }
158             }
159         }
160
161         return result;
162     }
163 }