Bug 5540 - ConvertorManager DataContainer source, one Convertor interface
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / action / ActionResponseConvertor.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 java.util.ArrayList;
12 import java.util.List;
13 import java.util.Optional;
14 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
15 import org.opendaylight.openflowplugin.openflow.md.core.extension.ActionExtensionHelper;
16 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.OfToSalCopyTtlInCase;
17 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.OfToSalCopyTtlOutCase;
18 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.OfToSalDecMplsTtlCase;
19 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.OfToSalDecNwTtlCase;
20 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.OfToSalGroupCase;
21 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.OfToSalOutputActionCase;
22 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.OfToSalPopMplsCase;
23 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.OfToSalPopPbbCase;
24 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.OfToSalPopVlanCase;
25 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.OfToSalPushMplsCase;
26 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.OfToSalPushPbbCase;
27 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.OfToSalPushVlanCase;
28 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.OfToSalSetFieldCase;
29 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.OfToSalSetMplsTtlCase;
30 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.OfToSalSetNwDstCase;
31 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.OfToSalSetNwTtlCase;
32 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.OfToSalSetQueueCase;
33 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.OfToSalStripVlanCase;
34 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.data.ActionResponseConvertorData;
35 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.Convertor;
36 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.ConvertorProcessor;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.ActionChoice;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action;
39 import org.opendaylight.yangtools.yang.binding.DataContainer;
40
41 /**
42  * Converts OF actions associated with bucket to SAL Actions.
43  *
44  * Example usage:
45  * <pre>
46  * {@code
47  * ActionResponseConvertorData data = new ActionResponseConvertorData(version);
48  * data.setActionPath(actionPath);
49  * Optional<List<Action>> salActions = ConvertorManager.getInstance().convert(ofActions, data);
50  * }
51  * </pre>
52  */
53 public final class ActionResponseConvertor implements Convertor<
54         List<Action>,
55         List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action>,
56         ActionResponseConvertorData> {
57
58     private static final ConvertorProcessor<ActionChoice, org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action, ActionResponseConvertorData> PROCESSOR = new ConvertorProcessor<
59             ActionChoice,
60             org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action,
61             ActionResponseConvertorData>()
62             // Add rules for each action type
63             .addCase(new OfToSalCopyTtlInCase())
64             .addCase(new OfToSalCopyTtlOutCase())
65             .addCase(new OfToSalDecMplsTtlCase())
66             .addCase(new OfToSalDecNwTtlCase())
67             .addCase(new OfToSalGroupCase())
68             .addCase(new OfToSalOutputActionCase())
69             .addCase(new OfToSalPopMplsCase())
70             .addCase(new OfToSalPopPbbCase())
71             .addCase(new OfToSalPopVlanCase())
72             .addCase(new OfToSalPushMplsCase())
73             .addCase(new OfToSalPushPbbCase())
74             .addCase(new OfToSalPushVlanCase())
75             .addCase(new OfToSalSetFieldCase())
76             .addCase(new OfToSalSetMplsTtlCase())
77             .addCase(new OfToSalSetNwDstCase())
78             .addCase(new OfToSalSetNwTtlCase())
79             .addCase(new OfToSalSetQueueCase())
80             .addCase(new OfToSalStripVlanCase());
81
82     @Override
83     public Class<? extends DataContainer> getType() {
84         return Action.class;
85     }
86
87     @Override
88     public List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action> convert(List<Action> source, ActionResponseConvertorData data) {
89         final List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action> result = new ArrayList<>();
90         final OpenflowVersion ofVersion = OpenflowVersion.get(data.getVersion());
91
92         // Iterate over Openflow actions, run them through tokenizer and then add them to list of converted actions
93         if (source != null) {
94             for (final Action action : source) {
95                 final Optional<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action> convertedAction = PROCESSOR.process(action.getActionChoice(), data);
96
97                 if (convertedAction.isPresent()) {
98                     result.add(convertedAction.get());
99                 } else {
100                     /**
101                      * TODO: EXTENSION PROPOSAL (action, OFJava to MD-SAL)
102                      * - we might also need a way on how to identify exact type of augmentation to be
103                      *   used as match can be bound to multiple models
104                      */
105                     org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action processedAction =
106                             ActionExtensionHelper.processAlienAction(action, ofVersion, data.getActionPath());
107
108                     if (processedAction != null) {
109                         result.add(processedAction);
110                     }
111                 }
112             }
113         }
114
115         return result;
116     }
117 }