Merge "Flow removed switch event. Group and Meter update RPC"
[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.yangtools.yang.common.RpcResult
17 import org.slf4j.LoggerFactory
18
19 import static org.opendaylight.controller.sal.compatibility.MDFlowMapping.*
20
21 import static extension org.opendaylight.controller.sal.compatibility.NodeMapping.*
22 import static extension org.opendaylight.controller.sal.compatibility.ToSalConversionsUtils.*
23
24 class FlowProgrammerAdapter implements IPluginInFlowProgrammerService, SalFlowListener {
25
26     private static val LOG = LoggerFactory.getLogger(FlowProgrammerAdapter);
27
28     @Property
29     private SalFlowService delegate;
30     
31     @Property
32     private IPluginOutFlowProgrammerService flowProgrammerPublisher;
33
34     override addFlow(Node node, Flow flow) {
35         val input = addFlowInput(node, flow);
36         val future = delegate.addFlow(input);
37         try {
38             val result = future.get();
39             return toStatus(result); // how get status from result? conversion?
40         } catch (Exception e) {
41             return processException(e);
42         }
43     }
44
45     override modifyFlow(Node node, Flow oldFlow, Flow newFlow) {
46         val input = updateFlowInput(node, oldFlow, newFlow);
47         val future = delegate.updateFlow(input);
48         try {
49             val result = future.get();
50             return toStatus(result);
51         } catch (Exception e) {
52             return processException(e);
53         }
54     }
55
56     override removeFlow(Node node, Flow flow) {
57         val input = removeFlowInput(node, flow);
58         val future = delegate.removeFlow(input);
59
60         try {
61             val result = future.get();
62             return toStatus(result);
63         } catch (Exception e) {
64             return processException(e);
65         }
66     }
67
68     override addFlowAsync(Node node, Flow flow, long rid) {
69         val input = addFlowInput(node, flow);
70         delegate.addFlow(input);
71         return new Status(StatusCode.SUCCESS);
72     }
73
74     override modifyFlowAsync(Node node, Flow oldFlow, Flow newFlow, long rid) {
75         val input = updateFlowInput(node, oldFlow, newFlow);
76         delegate.updateFlow(input);
77         return new Status(StatusCode.SUCCESS);
78     }
79
80     override removeFlowAsync(Node node, Flow flow, long rid) {
81         val input = removeFlowInput(node, flow);
82         delegate.removeFlow(input);
83         return new Status(StatusCode.SUCCESS);
84     }
85
86     override removeAllFlows(Node node) {
87         throw new UnsupportedOperationException("Not present in MD-SAL");
88     }
89
90     override syncSendBarrierMessage(Node node) {
91
92         // FIXME: Update YANG model
93         return null;
94     }
95
96     override asyncSendBarrierMessage(Node node) {
97
98         // FIXME: Update YANG model
99         return null;
100     }
101
102     public static def toStatus(RpcResult<?> result) {
103         if (result.isSuccessful()) {
104             return new Status(StatusCode.SUCCESS);
105         } else {
106             return new Status(StatusCode.INTERNALERROR);
107         }
108     }
109     
110     private static dispatch def Status processException(InterruptedException e) {
111         LOG.error("Interruption occured during processing flow",e);
112         return new Status(StatusCode.INTERNALERROR);
113     }
114     
115     private static dispatch def Status processException(ExecutionException e) {
116         LOG.error("Execution exception occured during processing flow",e.cause);
117         return new Status(StatusCode.INTERNALERROR);
118     }
119     
120     private static dispatch def Status processException(Exception e) {
121         throw new RuntimeException(e);
122     }
123     
124     override onFlowAdded(FlowAdded notification) {
125         // NOOP : Not supported by AD SAL
126     }
127     
128     override onFlowRemoved(FlowRemoved notification) {
129         flowProgrammerPublisher.flowRemoved(notification.node.toADNode,notification.toFlow());
130     }
131     
132     override onFlowUpdated(FlowUpdated notification) {
133         // NOOP : Not supported by AD SAL
134     }
135     
136     override onSwitchFlowRemoved(SwitchFlowRemoved notification) {
137         // NOOP : Not supported by AD SAL
138     }
139     
140 }