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