Merge "Use InetAddress.getLoopbackAddress in ClusterManagerCommon."
[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 org.opendaylight.controller.sal.binding.api.data.DataBrokerService
22 import org.opendaylight.controller.md.sal.common.api.TransactionStatus
23 import org.opendaylight.controller.md.sal.common.api.data.DataModification
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey
30 import org.opendaylight.yangtools.yang.binding.DataObject
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId
36
37
38 import static extension org.opendaylight.controller.sal.compatibility.MDFlowMapping.*
39
40 import static extension org.opendaylight.controller.sal.compatibility.NodeMapping.*
41 import static extension org.opendaylight.controller.sal.compatibility.ToSalConversionsUtils.*
42
43 class FlowProgrammerAdapter implements IPluginInFlowProgrammerService, SalFlowListener {
44
45     private static val LOG = LoggerFactory.getLogger(FlowProgrammerAdapter);
46
47     @Property
48     private SalFlowService delegate;
49
50     @Property
51     private DataBrokerService dataBrokerService;
52     
53     @Property
54     private IPluginOutFlowProgrammerService flowProgrammerPublisher;
55
56     override addFlow(Node node, Flow flow) {
57         return addFlowAsync(node,flow,0)
58     }
59
60     override modifyFlow(Node node, Flow oldFlow, Flow newFlow) {
61         return modifyFlowAsync(node, oldFlow,newFlow,0)
62     }
63
64     override removeFlow(Node node, Flow flow) {
65         return removeFlowAsync(node, flow,0);
66     }
67
68     override addFlowAsync(Node node, Flow flow, long rid) {
69         writeFlow(flow.toMDFlow, new NodeKey(new NodeId(node.getNodeIDString())));
70         return toStatus(true);
71     }
72
73     override modifyFlowAsync(Node node, Flow oldFlow, Flow newFlow, long rid) {
74         writeFlow(newFlow.toMDFlow, new NodeKey(new NodeId(node.getNodeIDString())));
75         return toStatus(true);
76     }
77
78     override removeFlowAsync(Node node, Flow adflow, long rid) {
79         val flow = adflow.toMDFlow;
80         val modification = this._dataBrokerService.beginTransaction();
81         val flowPath = InstanceIdentifier.builder(Nodes)
82                 .child(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node, new NodeKey(new NodeId(node.getNodeIDString())))
83                 .augmentation(FlowCapableNode)
84                 .child(Table, new TableKey(flow.getTableId()))
85                 .child(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow, new FlowKey(flow.id))
86                 .build;
87         modification.removeConfigurationData(flowPath);
88         val commitFuture = modification.commit();
89         return toStatus(true);
90     }
91
92     override removeAllFlows(Node node) {
93         // I know this looks like a copout... but its exactly what the legacy OFplugin did
94         return new Status(StatusCode.SUCCESS);
95     }
96
97     override syncSendBarrierMessage(Node node) {
98
99         // FIXME: Update YANG model
100         return null;
101     }
102
103     override asyncSendBarrierMessage(Node node) {
104
105         // FIXME: Update YANG model
106         return null;
107     }
108
109     private static def toStatus(boolean successful) {
110         if (successful) {
111             return new Status(StatusCode.SUCCESS);
112         } else {
113             return new Status(StatusCode.INTERNALERROR);
114         }
115     }
116
117
118     private def writeFlow(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow flow, NodeKey nodeKey) {
119         val modification = this._dataBrokerService.beginTransaction();
120         val flowPath = InstanceIdentifier.builder(Nodes)
121                 .child(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node, nodeKey)
122                 .augmentation(FlowCapableNode)
123                 .child(Table, new TableKey(flow.getTableId()))
124                 .child(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow, new FlowKey(flow.id))
125                 .build;
126         modification.putConfigurationData(flowPath, flow);
127         val commitFuture = modification.commit();
128         try {
129             val result = commitFuture.get();
130             val status = result.getResult();
131         } catch (InterruptedException e) {
132             LOG.error(e.getMessage(), e);
133         } catch (ExecutionException e) {
134             LOG.error(e.getMessage(), e);
135         }
136     }
137
138     public static def toStatus(RpcResult<?> result) {
139         return toStatus(result.isSuccessful());
140     }
141     
142     private static dispatch def Status processException(InterruptedException e) {
143         LOG.error("Interruption occured during processing flow",e);
144         return new Status(StatusCode.INTERNALERROR);
145     }
146     
147     private static dispatch def Status processException(ExecutionException e) {
148         LOG.error("Execution exception occured during processing flow",e.cause);
149         return new Status(StatusCode.INTERNALERROR);
150     }
151     
152     private static dispatch def Status processException(Exception e) {
153         throw new RuntimeException(e);
154     }
155     
156     override onFlowAdded(FlowAdded notification) {
157         // NOOP : Not supported by AD SAL
158     }
159     
160     override onFlowRemoved(FlowRemoved notification) {
161         flowProgrammerPublisher.flowRemoved(notification.node.toADNode,notification.toFlow());
162     }
163     
164     override onFlowUpdated(FlowUpdated notification) {
165         // NOOP : Not supported by AD SAL
166     }
167     
168     override onSwitchFlowRemoved(SwitchFlowRemoved notification) {
169         // NOOP : Not supported by AD SAL
170     }
171     
172      override onNodeErrorNotification(NodeErrorNotification notification) {
173         // NOOP : Not supported by AD SAL
174     }
175     
176      override onNodeExperimenterErrorNotification(
177                 NodeExperimenterErrorNotification notification) {
178         // NOOP : Not supported by AD SAL
179     }
180 }