Use ByteBuf.readRetainedSlice()
[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 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action;
9
10 import java.util.ArrayList;
11 import java.util.Collection;
12 import java.util.List;
13 import java.util.Set;
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.OfToSalSetDlDstCase;
29 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.OfToSalSetDlSrcCase;
30 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.OfToSalSetFieldCase;
31 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.OfToSalSetMplsTtlCase;
32 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.OfToSalSetNwDstCase;
33 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.OfToSalSetNwSrcCase;
34 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.OfToSalSetNwTosCase;
35 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.OfToSalSetNwTtlCase;
36 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.OfToSalSetQueueCase;
37 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.OfToSalSetTpDstCase;
38 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.OfToSalSetTpSrcCase;
39 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.OfToSalSetVlanIdCase;
40 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.OfToSalSetVlanPcpCase;
41 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases.OfToSalStripVlanCase;
42 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.data.ActionResponseConvertorData;
43 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.Convertor;
44 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.ConvertorProcessor;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.ActionChoice;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action;
47
48 /**
49  * Converts OF actions associated with bucket to SAL Actions.
50  *
51  * <p>
52  * Example usage:
53  * <pre>
54  * {@code
55  * ActionResponseConvertorData data = new ActionResponseConvertorData(version);
56  * data.setActionPath(actionPath);
57  * Optional<List<Action>> salActions = convertorManager.convert(ofActions, data);
58  * }
59  * </pre>
60  */
61 public final class ActionResponseConvertor extends Convertor<
62         List<Action>,
63         List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action>,
64         ActionResponseConvertorData> {
65
66     private static final ConvertorProcessor<ActionChoice, org.opendaylight.yang.gen.v1.urn.opendaylight.action.types
67         .rev131112.action.Action, ActionResponseConvertorData> PROCESSOR = new ConvertorProcessor<
68             ActionChoice,
69             org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action,
70             ActionResponseConvertorData>()
71             // Add rules for each action type
72             .addCase(new OfToSalCopyTtlInCase())
73             .addCase(new OfToSalCopyTtlOutCase())
74             .addCase(new OfToSalDecMplsTtlCase())
75             .addCase(new OfToSalDecNwTtlCase())
76             .addCase(new OfToSalGroupCase())
77             .addCase(new OfToSalOutputActionCase())
78             .addCase(new OfToSalPopMplsCase())
79             .addCase(new OfToSalPopVlanCase())
80             .addCase(new OfToSalPushMplsCase())
81             .addCase(new OfToSalPushPbbCase())
82             .addCase(new OfToSalPushVlanCase())
83             .addCase(new OfToSalSetMplsTtlCase())
84             .addCase(new OfToSalSetNwTtlCase())
85             .addCase(new OfToSalSetQueueCase())
86             // OpenFlow 1.3 specific actions
87             .addCase(new OfToSalPopPbbCase())
88             .addCase(new OfToSalSetFieldCase())
89             // OpenFlow 1.0 specific actions
90             .addCase(new OfToSalSetNwSrcCase())
91             .addCase(new OfToSalSetNwDstCase())
92             .addCase(new OfToSalSetNwTosCase())
93             .addCase(new OfToSalSetDlSrcCase())
94             .addCase(new OfToSalSetDlDstCase())
95             .addCase(new OfToSalSetTpSrcCase())
96             .addCase(new OfToSalSetTpDstCase())
97             .addCase(new OfToSalSetVlanPcpCase())
98             .addCase(new OfToSalSetVlanIdCase())
99             .addCase(new OfToSalStripVlanCase());
100     private static final Set<Class<?>> TYPES = Set.of(Action.class);
101
102     @Override
103     public Collection<Class<?>> getTypes() {
104         return TYPES;
105     }
106
107     @Override
108     public List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action>
109             convert(final List<Action> source, final ActionResponseConvertorData data) {
110         final var result =
111             new ArrayList<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action>();
112
113         // Iterate over Openflow actions, run them through tokenizer and then add them to list of converted actions
114         if (source != null) {
115             final var ofVersion = OpenflowVersion.get(data.getVersion());
116             for (var action : source) {
117                 final var convertedAction = PROCESSOR.process(action.getActionChoice(), data, getConvertorExecutor());
118                 if (convertedAction.isPresent()) {
119                     result.add(convertedAction.orElseThrow());
120                 } else {
121                     /*
122                      * TODO: EXTENSION PROPOSAL (action, OFJava to MD-SAL)
123                      * - we might also need a way on how to identify exact type of augmentation to be
124                      *   used as match can be bound to multiple models
125                      */
126                     var processedAction = ActionExtensionHelper.processAlienAction(action, ofVersion,
127                         data.getActionPath());
128                     if (processedAction != null) {
129                         result.add(processedAction);
130                     }
131                 }
132             }
133         }
134
135         return result;
136     }
137 }