Merge "Topology Manager changes for cross checking Production switches"
[controller.git] / opendaylight / md-sal / compatibility / sal-compatibility / src / main / java / org / opendaylight / controller / sal / compatibility / FlowProgrammerAdapter.xtend
1 package org.opendaylight.controller.sal.compatibility
2
3 import java.util.concurrent.ExecutionException
4 import org.opendaylight.controller.sal.core.Node
5 import org.opendaylight.controller.sal.flowprogrammer.Flow
6 import org.opendaylight.controller.sal.flowprogrammer.IPluginInFlowProgrammerService
7 import org.opendaylight.controller.sal.flowprogrammer.IPluginOutFlowProgrammerService
8 import org.opendaylight.controller.sal.utils.Status
9 import org.opendaylight.controller.sal.utils.StatusCode
10 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowAdded
11 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowRemoved
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SwitchFlowRemoved
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowUpdated
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowListener
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeErrorNotification
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeExperimenterErrorNotification
18 import org.opendaylight.yangtools.yang.common.RpcResult
19 import org.slf4j.LoggerFactory
20
21 import static org.opendaylight.controller.sal.compatibility.MDFlowMapping.*
22
23 import static extension org.opendaylight.controller.sal.compatibility.NodeMapping.*
24 import static extension org.opendaylight.controller.sal.compatibility.ToSalConversionsUtils.*
25
26 class FlowProgrammerAdapter implements IPluginInFlowProgrammerService, SalFlowListener {
27
28     private static val LOG = LoggerFactory.getLogger(FlowProgrammerAdapter);
29
30     @Property
31     private SalFlowService delegate;
32     
33     @Property
34     private IPluginOutFlowProgrammerService flowProgrammerPublisher;
35
36     override addFlow(Node node, Flow flow) {
37         val input = addFlowInput(node, flow);
38         val future = delegate.addFlow(input);
39         try {
40             val result = future.get();
41             return toStatus(result); // how get status from result? conversion?
42         } catch (Exception e) {
43             return processException(e);
44         }
45     }
46
47     override modifyFlow(Node node, Flow oldFlow, Flow newFlow) {
48         val input = updateFlowInput(node, oldFlow, newFlow);
49         val future = delegate.updateFlow(input);
50         try {
51             val result = future.get();
52             return toStatus(result);
53         } catch (Exception e) {
54             return processException(e);
55         }
56     }
57
58     override removeFlow(Node node, Flow flow) {
59         val input = removeFlowInput(node, flow);
60         val future = delegate.removeFlow(input);
61
62         try {
63             val result = future.get();
64             return toStatus(result);
65         } catch (Exception e) {
66             return processException(e);
67         }
68     }
69
70     override addFlowAsync(Node node, Flow flow, long rid) {
71         val input = addFlowInput(node, flow);
72         delegate.addFlow(input);
73         return new Status(StatusCode.SUCCESS);
74     }
75
76     override modifyFlowAsync(Node node, Flow oldFlow, Flow newFlow, long rid) {
77         val input = updateFlowInput(node, oldFlow, newFlow);
78         delegate.updateFlow(input);
79         return new Status(StatusCode.SUCCESS);
80     }
81
82     override removeFlowAsync(Node node, Flow flow, long rid) {
83         val input = removeFlowInput(node, flow);
84         delegate.removeFlow(input);
85         return new Status(StatusCode.SUCCESS);
86     }
87
88     override removeAllFlows(Node node) {
89         throw new UnsupportedOperationException("Not present in MD-SAL");
90     }
91
92     override syncSendBarrierMessage(Node node) {
93
94         // FIXME: Update YANG model
95         return null;
96     }
97
98     override asyncSendBarrierMessage(Node node) {
99
100         // FIXME: Update YANG model
101         return null;
102     }
103
104     public static def toStatus(RpcResult<?> result) {
105         if (result.isSuccessful()) {
106             return new Status(StatusCode.SUCCESS);
107         } else {
108             return new Status(StatusCode.INTERNALERROR);
109         }
110     }
111     
112     private static dispatch def Status processException(InterruptedException e) {
113         LOG.error("Interruption occured during processing flow",e);
114         return new Status(StatusCode.INTERNALERROR);
115     }
116     
117     private static dispatch def Status processException(ExecutionException e) {
118         LOG.error("Execution exception occured during processing flow",e.cause);
119         return new Status(StatusCode.INTERNALERROR);
120     }
121     
122     private static dispatch def Status processException(Exception e) {
123         throw new RuntimeException(e);
124     }
125     
126     override onFlowAdded(FlowAdded notification) {
127         // NOOP : Not supported by AD SAL
128     }
129     
130     override onFlowRemoved(FlowRemoved notification) {
131         flowProgrammerPublisher.flowRemoved(notification.node.toADNode,notification.toFlow());
132     }
133     
134     override onFlowUpdated(FlowUpdated notification) {
135         // NOOP : Not supported by AD SAL
136     }
137     
138     override onSwitchFlowRemoved(SwitchFlowRemoved notification) {
139         // NOOP : Not supported by AD SAL
140     }
141     
142      override onNodeErrorNotification(NodeErrorNotification notification) {
143         // NOOP : Not supported by AD SAL
144     }
145     
146      override onNodeExperimenterErrorNotification(
147                 NodeExperimenterErrorNotification notification) {
148         // NOOP : Not supported by AD SAL
149     }
150 }