2 * Copyright (c) 2016, 2017 Ericsson India Global Services Pvt Ltd. 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
8 package org.opendaylight.genius.interfacemanager.servicebindings.flowbased.utilities;
10 import java.lang.reflect.InvocationTargetException;
11 import java.lang.reflect.Method;
12 import java.util.ArrayList;
13 import java.util.HashMap;
14 import java.util.List;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.bound.services.instruction.instruction.apply.actions._case.apply.actions.action.action.ServiceBindingNxActionConntrackApplyActionsCase;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.bound.services.instruction.instruction.apply.actions._case.apply.actions.action.action.ServiceBindingNxActionLearnApplyActionsCase;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.bound.services.instruction.instruction.apply.actions._case.apply.actions.action.action.ServiceBindingNxActionMultipathApplyActionsCase;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.bound.services.instruction.instruction.apply.actions._case.apply.actions.action.action.ServiceBindingNxActionOutputRegApplyActionsCase;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.bound.services.instruction.instruction.apply.actions._case.apply.actions.action.action.ServiceBindingNxActionPopNshApplyActionsCase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.bound.services.instruction.instruction.apply.actions._case.apply.actions.action.action.ServiceBindingNxActionPushNshApplyActionsCase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.bound.services.instruction.instruction.apply.actions._case.apply.actions.action.action.ServiceBindingNxActionRegLoadApplyActionsCase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.bound.services.instruction.instruction.apply.actions._case.apply.actions.action.action.ServiceBindingNxActionRegMoveApplyActionsCase;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.bound.services.instruction.instruction.apply.actions._case.apply.actions.action.action.ServiceBindingNxActionResubmitApplyActionsCase;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.bound.services.instruction.instruction.write.actions._case.write.actions.action.action.ServiceBindingNxActionConntrackWriteActionsCase;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.bound.services.instruction.instruction.write.actions._case.write.actions.action.action.ServiceBindingNxActionLearnWriteActionsCase;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.bound.services.instruction.instruction.write.actions._case.write.actions.action.action.ServiceBindingNxActionMultipathWriteActionsCase;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.bound.services.instruction.instruction.write.actions._case.write.actions.action.action.ServiceBindingNxActionOutputRegWriteActionsCase;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.bound.services.instruction.instruction.write.actions._case.write.actions.action.action.ServiceBindingNxActionPopNshWriteActionsCase;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.bound.services.instruction.instruction.write.actions._case.write.actions.action.action.ServiceBindingNxActionPushNshWriteActionsCase;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.bound.services.instruction.instruction.write.actions._case.write.actions.action.action.ServiceBindingNxActionRegLoadWriteActionsCase;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.bound.services.instruction.instruction.write.actions._case.write.actions.action.action.ServiceBindingNxActionRegMoveWriteActionsCase;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.bound.services.instruction.instruction.write.actions._case.write.actions.action.action.ServiceBindingNxActionResubmitWriteActionsCase;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nodes.node.table.flow.instructions.instruction.instruction.apply.actions._case.apply.actions.action.action.NxActionConntrackNodesNodeTableFlowApplyActionsCaseBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nodes.node.table.flow.instructions.instruction.instruction.apply.actions._case.apply.actions.action.action.NxActionLearnNodesNodeTableFlowApplyActionsCaseBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nodes.node.table.flow.instructions.instruction.instruction.apply.actions._case.apply.actions.action.action.NxActionMultipathNodesNodeTableFlowApplyActionsCaseBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nodes.node.table.flow.instructions.instruction.instruction.apply.actions._case.apply.actions.action.action.NxActionOutputRegNodesNodeTableFlowApplyActionsCaseBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nodes.node.table.flow.instructions.instruction.instruction.apply.actions._case.apply.actions.action.action.NxActionPopNshNodesNodeTableFlowApplyActionsCaseBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nodes.node.table.flow.instructions.instruction.instruction.apply.actions._case.apply.actions.action.action.NxActionPushNshNodesNodeTableFlowApplyActionsCaseBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nodes.node.table.flow.instructions.instruction.instruction.apply.actions._case.apply.actions.action.action.NxActionRegLoadNodesNodeTableFlowApplyActionsCaseBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nodes.node.table.flow.instructions.instruction.instruction.apply.actions._case.apply.actions.action.action.NxActionRegMoveNodesNodeTableFlowApplyActionsCaseBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nodes.node.table.flow.instructions.instruction.instruction.apply.actions._case.apply.actions.action.action.NxActionResubmitNodesNodeTableFlowApplyActionsCaseBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nodes.node.table.flow.instructions.instruction.instruction.write.actions._case.write.actions.action.action.NxActionConntrackNodesNodeTableFlowWriteActionsCaseBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nodes.node.table.flow.instructions.instruction.instruction.write.actions._case.write.actions.action.action.NxActionLearnNodesNodeTableFlowWriteActionsCaseBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nodes.node.table.flow.instructions.instruction.instruction.write.actions._case.write.actions.action.action.NxActionMultipathNodesNodeTableFlowWriteActionsCaseBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nodes.node.table.flow.instructions.instruction.instruction.write.actions._case.write.actions.action.action.NxActionOutputRegNodesNodeTableFlowWriteActionsCaseBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nodes.node.table.flow.instructions.instruction.instruction.write.actions._case.write.actions.action.action.NxActionPopNshNodesNodeTableFlowWriteActionsCaseBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nodes.node.table.flow.instructions.instruction.instruction.write.actions._case.write.actions.action.action.NxActionPushNshNodesNodeTableFlowWriteActionsCaseBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nodes.node.table.flow.instructions.instruction.instruction.write.actions._case.write.actions.action.action.NxActionRegLoadNodesNodeTableFlowWriteActionsCaseBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nodes.node.table.flow.instructions.instruction.instruction.write.actions._case.write.actions.action.action.NxActionRegMoveNodesNodeTableFlowWriteActionsCaseBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nodes.node.table.flow.instructions.instruction.instruction.write.actions._case.write.actions.action.action.NxActionResubmitNodesNodeTableFlowWriteActionsCaseBuilder;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
57 public class ActionConverterUtil {
59 private static final Logger LOG = LoggerFactory.getLogger(ActionConverterUtil.class);
60 private static final Map<Class<? extends Action>, Class<?>> SERVICE_ACTION_TO_OF_ACTION_MAP = new HashMap<>();
61 private static final String BUILD_METHOD = "build";
62 private static final String AUGMENTATION_METHOD = "getAugmentation";
63 private static final String GET_IMPL_METHOD = "getImplementedInterface";
66 SERVICE_ACTION_TO_OF_ACTION_MAP.put(ServiceBindingNxActionConntrackApplyActionsCase.class,
67 NxActionConntrackNodesNodeTableFlowApplyActionsCaseBuilder.class);
68 SERVICE_ACTION_TO_OF_ACTION_MAP.put(ServiceBindingNxActionLearnApplyActionsCase.class,
69 NxActionLearnNodesNodeTableFlowApplyActionsCaseBuilder.class);
70 SERVICE_ACTION_TO_OF_ACTION_MAP.put(ServiceBindingNxActionMultipathApplyActionsCase.class,
71 NxActionMultipathNodesNodeTableFlowApplyActionsCaseBuilder.class);
72 SERVICE_ACTION_TO_OF_ACTION_MAP.put(ServiceBindingNxActionOutputRegApplyActionsCase.class,
73 NxActionOutputRegNodesNodeTableFlowApplyActionsCaseBuilder.class);
74 SERVICE_ACTION_TO_OF_ACTION_MAP.put(ServiceBindingNxActionPopNshApplyActionsCase.class,
75 NxActionPopNshNodesNodeTableFlowApplyActionsCaseBuilder.class);
76 SERVICE_ACTION_TO_OF_ACTION_MAP.put(ServiceBindingNxActionPushNshApplyActionsCase.class,
77 NxActionPushNshNodesNodeTableFlowApplyActionsCaseBuilder.class);
78 SERVICE_ACTION_TO_OF_ACTION_MAP.put(ServiceBindingNxActionRegLoadApplyActionsCase.class,
79 NxActionRegLoadNodesNodeTableFlowApplyActionsCaseBuilder.class);
80 SERVICE_ACTION_TO_OF_ACTION_MAP.put(ServiceBindingNxActionRegMoveApplyActionsCase.class,
81 NxActionRegMoveNodesNodeTableFlowApplyActionsCaseBuilder.class);
82 SERVICE_ACTION_TO_OF_ACTION_MAP.put(ServiceBindingNxActionResubmitApplyActionsCase.class,
83 NxActionResubmitNodesNodeTableFlowApplyActionsCaseBuilder.class);
84 SERVICE_ACTION_TO_OF_ACTION_MAP.put(ServiceBindingNxActionConntrackWriteActionsCase.class,
85 NxActionConntrackNodesNodeTableFlowWriteActionsCaseBuilder.class);
86 SERVICE_ACTION_TO_OF_ACTION_MAP.put(ServiceBindingNxActionLearnWriteActionsCase.class,
87 NxActionLearnNodesNodeTableFlowWriteActionsCaseBuilder.class);
88 SERVICE_ACTION_TO_OF_ACTION_MAP.put(ServiceBindingNxActionMultipathWriteActionsCase.class,
89 NxActionMultipathNodesNodeTableFlowWriteActionsCaseBuilder.class);
90 SERVICE_ACTION_TO_OF_ACTION_MAP.put(ServiceBindingNxActionOutputRegWriteActionsCase.class,
91 NxActionOutputRegNodesNodeTableFlowWriteActionsCaseBuilder.class);
92 SERVICE_ACTION_TO_OF_ACTION_MAP.put(ServiceBindingNxActionPopNshWriteActionsCase.class,
93 NxActionPopNshNodesNodeTableFlowWriteActionsCaseBuilder.class);
94 SERVICE_ACTION_TO_OF_ACTION_MAP.put(ServiceBindingNxActionPushNshWriteActionsCase.class,
95 NxActionPushNshNodesNodeTableFlowWriteActionsCaseBuilder.class);
96 SERVICE_ACTION_TO_OF_ACTION_MAP.put(ServiceBindingNxActionRegLoadWriteActionsCase.class,
97 NxActionRegLoadNodesNodeTableFlowWriteActionsCaseBuilder.class);
98 SERVICE_ACTION_TO_OF_ACTION_MAP.put(ServiceBindingNxActionRegMoveWriteActionsCase.class,
99 NxActionRegMoveNodesNodeTableFlowWriteActionsCaseBuilder.class);
100 SERVICE_ACTION_TO_OF_ACTION_MAP.put(ServiceBindingNxActionResubmitWriteActionsCase.class,
101 NxActionResubmitNodesNodeTableFlowWriteActionsCaseBuilder.class);
104 public static List<org.opendaylight.yang.gen.v1.urn.opendaylight.action
105 .types.rev131112.action.list.Action> convertServiceActionToFlowAction(
106 List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112
107 .action.list.Action> inActionList) {
108 return convertServiceActionToFlowAction(inActionList, 0);
112 * Convert service binding actions to flow actions, applying an offset to
115 * @param inActionList the service binding actions.
116 * @param keyOffset the offset.
117 * @return the flow actions.
119 public static List<org.opendaylight.yang.gen.v1.urn.opendaylight.action
120 .types.rev131112.action.list.Action> convertServiceActionToFlowAction(
121 List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112
122 .action.list.Action> inActionList,
124 List<org.opendaylight.yang.gen.v1.urn.opendaylight
125 .action.types.rev131112.action.list.Action> outActionList = new ArrayList<>();
126 if (inActionList != null) {
127 for (org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112
128 .action.list.Action inAction : inActionList) {
130 new org.opendaylight.yang.gen.v1.urn.opendaylight
131 .action.types.rev131112.action.list.ActionBuilder()
132 .setAction(convertServiceActionToFlowAction(inAction.getAction()))
133 .setKey(new ActionKey(inAction.getKey().getOrder() + keyOffset))
137 return outActionList;
140 public static Action convertServiceActionToFlowAction(Action inAction) {
141 Class ofActionClass = SERVICE_ACTION_TO_OF_ACTION_MAP.get(inAction.getImplementedInterface());
142 if (ofActionClass != null) {
144 Method build = ofActionClass.getDeclaredMethod(BUILD_METHOD);
145 Object ofActionObj = ofActionClass.newInstance();
146 ofActionObj = build.invoke(copy(inAction, ofActionObj));
147 LOG.debug("Converted {} action to {} action", inAction.getImplementedInterface(),
148 ((Action) ofActionObj).getImplementedInterface());
149 inAction = (Action) ofActionObj;
150 } catch (InstantiationException e) {
151 LOG.error("Failed to instantiate OF action class {}", ofActionClass);
152 } catch (IllegalAccessException e) {
153 LOG.error("Cannot access some fields in {}", ofActionClass);
154 } catch (NoSuchMethodException e) {
155 LOG.error("Method build does not exist in {}", ofActionClass);
156 } catch (InvocationTargetException e) {
157 LOG.error("Method build invocation failed in {}", ofActionClass);
163 private static Object copy(Object src, Object dest) {
164 Method[] gettersAndSetters = src.getClass().getDeclaredMethods();
165 for (Method gettersAndSetter : gettersAndSetters) {
166 String methodName = gettersAndSetter.getName();
167 gettersAndSetter.setAccessible(true);
169 if (methodName.equals(AUGMENTATION_METHOD) || methodName.equals(GET_IMPL_METHOD)) {
172 if (methodName.startsWith("get")) {
173 dest = dest.getClass()
174 .getMethod(methodName.replaceFirst("get", "set"), gettersAndSetter.getReturnType())
175 .invoke(dest, gettersAndSetter.invoke(src, null));
176 } else if (methodName.startsWith("is")) {
177 dest = dest.getClass()
178 .getMethod(methodName.replaceFirst("is", "set"), gettersAndSetter.getReturnType())
179 .invoke(dest, gettersAndSetter.invoke(src, null));
181 } catch (NoSuchMethodException e) {
182 LOG.error("Method {} does not exist in {}", methodName, src.getClass(), e);
183 } catch (IllegalArgumentException e) {
184 LOG.error("Method {} invocation failed in {} due to illegal argument.", methodName, dest.getClass(), e);
185 } catch (InvocationTargetException e) {
186 LOG.error("Method {} invocation failed in {}", methodName, dest.getClass(), e);
187 } catch (IllegalAccessException e) {
188 LOG.error("Cannot access method {} in {}", methodName, dest.getClass(), e);