Yang changes for l3vpn
[vpnservice.git] / interfacemgr / interfacemgr-impl / src / main / java / org / opendaylight / vpnservice / interfacemgr / rpcservice / InterfaceManagerRpcService.java
1 /*
2  * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.vpnservice.interfacemgr.rpcservice;
10
11 import com.google.common.base.Optional;
12 import com.google.common.util.concurrent.Futures;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.vpnservice.interfacemgr.IfmUtil;
16 import org.opendaylight.vpnservice.interfacemgr.commons.InterfaceManagerCommonUtils;
17 import org.opendaylight.vpnservice.interfacemgr.commons.InterfaceMetaUtils;
18 import org.opendaylight.vpnservice.mdsalutil.ActionInfo;
19 import org.opendaylight.vpnservice.mdsalutil.ActionType;
20 import org.opendaylight.vpnservice.mdsalutil.InstructionInfo;
21 import org.opendaylight.vpnservice.mdsalutil.InstructionType;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.L2vlan;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.Tunnel;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfaceType;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.InterfaceChildInfo;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info.InterfaceParentEntry;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info.InterfaceParentEntryKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info._interface.parent.entry.InterfaceChildEntry;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info._interface.parent.entry.InterfaceChildEntryKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge._interface.info.BridgeEntry;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge._interface.info.BridgeEntryKey;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge._interface.info.bridge.entry.BridgeInterfaceEntry;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfL2vlan;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfTunnel;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.ParentRefs;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.TunnelTypeBase;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rpcs.rev151003.*;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48 import org.opendaylight.yangtools.yang.common.RpcResult;
49 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52
53 import java.math.BigInteger;
54 import java.util.ArrayList;
55 import java.util.Arrays;
56 import java.util.List;
57 import java.util.concurrent.Future;
58
59 public class InterfaceManagerRpcService implements OdlInterfaceRpcService {
60     private static final Logger LOG = LoggerFactory.getLogger(InterfaceManagerRpcService.class);
61     DataBroker dataBroker;
62     public InterfaceManagerRpcService(DataBroker dataBroker) {
63         this.dataBroker = dataBroker;
64     }
65
66     @Override
67     public Future<RpcResult<GetDpidFromInterfaceOutput>> getDpidFromInterface(GetDpidFromInterfaceInput input) {
68         String interfaceName = input.getIntfName();
69         RpcResultBuilder<GetDpidFromInterfaceOutput> rpcResultBuilder;
70         try {
71             BigInteger dpId = null;
72             InterfaceKey interfaceKey = new InterfaceKey(interfaceName);
73             Interface interfaceInfo = InterfaceManagerCommonUtils.getInterfaceFromConfigDS(interfaceKey, dataBroker);
74             if (Tunnel.class.equals(interfaceInfo.getType())) {
75                 ParentRefs parentRefs = interfaceInfo.getAugmentation(ParentRefs.class);
76                 dpId = parentRefs.getDatapathNodeIdentifier();
77             } else {
78                 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState =
79                         InterfaceManagerCommonUtils.getInterfaceStateFromOperDS(interfaceName, dataBroker);
80                 String lowerLayerIf = ifState.getLowerLayerIf().get(0);
81                 NodeConnectorId nodeConnectorId = new NodeConnectorId(lowerLayerIf);
82                 dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
83             }
84             GetDpidFromInterfaceOutputBuilder output = new GetDpidFromInterfaceOutputBuilder().setDpid(
85                     (dpId));
86             rpcResultBuilder = RpcResultBuilder.success();
87             rpcResultBuilder.withResult(output.build());
88         } catch (Exception e) {
89             LOG.error("Retrieval of datapath id for the key {} failed due to {}", interfaceName, e);
90             rpcResultBuilder = RpcResultBuilder.failed();
91         }
92         return Futures.immediateFuture(rpcResultBuilder.build());
93     }
94
95     @Override
96     public Future<RpcResult<GetEndpointIpForDpnOutput>> getEndpointIpForDpn(GetEndpointIpForDpnInput input) {
97         RpcResultBuilder<GetEndpointIpForDpnOutput> rpcResultBuilder;
98         try {
99             BridgeEntryKey bridgeEntryKey = new BridgeEntryKey(input.getDpid());
100             InstanceIdentifier<BridgeEntry> bridgeEntryInstanceIdentifier =
101                     InterfaceMetaUtils.getBridgeEntryIdentifier(bridgeEntryKey);
102             BridgeEntry bridgeEntry =
103                     InterfaceMetaUtils.getBridgeEntryFromConfigDS(bridgeEntryInstanceIdentifier,
104                             dataBroker);
105             // local ip of any of the bridge interface entry will be the dpn end point ip
106             BridgeInterfaceEntry bridgeInterfaceEntry = bridgeEntry.getBridgeInterfaceEntry().get(0);
107             InterfaceKey interfaceKey = new InterfaceKey(bridgeInterfaceEntry.getInterfaceName());
108             Interface interfaceInfo = InterfaceManagerCommonUtils.getInterfaceFromConfigDS(interfaceKey, dataBroker);
109             IfTunnel tunnel = interfaceInfo.getAugmentation(IfTunnel.class);
110             GetEndpointIpForDpnOutputBuilder endpointIpForDpnOutput = new GetEndpointIpForDpnOutputBuilder().setLocalIps(Arrays.asList(tunnel.getTunnelSource()));
111             rpcResultBuilder = RpcResultBuilder.success();
112             rpcResultBuilder.withResult(endpointIpForDpnOutput.build());
113         }catch(Exception e){
114             LOG.error("Retrieval of endpoint of for dpn {} failed due to {}" ,input.getDpid(), e);
115             rpcResultBuilder = RpcResultBuilder.failed();
116         }
117         return Futures.immediateFuture(rpcResultBuilder.build());
118     }
119
120     @Override
121     public Future<RpcResult<GetEgressInstructionsForInterfaceOutput>> getEgressInstructionsForInterface(GetEgressInstructionsForInterfaceInput input) {
122         RpcResultBuilder<GetEgressInstructionsForInterfaceOutput> rpcResultBuilder;
123         try {
124             List<InstructionInfo> instructionInfo = new ArrayList<InstructionInfo>();
125             List<ActionInfo> actionInfo = getEgressActionInfosForInterface(input.getIntfName());
126             instructionInfo.add(new InstructionInfo(InstructionType.write_actions, actionInfo));
127                     GetEgressInstructionsForInterfaceOutputBuilder output = new GetEgressInstructionsForInterfaceOutputBuilder().
128                     setInstruction(buildInstructions(instructionInfo));
129             rpcResultBuilder = RpcResultBuilder.success();
130             rpcResultBuilder.withResult(output.build());
131         }catch(Exception e){
132             LOG.error("Retrieval of egress actions for the key {} failed due to {}" ,input.getIntfName(), e);
133             rpcResultBuilder = RpcResultBuilder.failed();
134         }
135         return Futures.immediateFuture(rpcResultBuilder.build());
136     }
137
138     @Override
139     public Future<RpcResult<GetInterfaceTypeOutput>> getInterfaceType(GetInterfaceTypeInput input) {
140         String interfaceName = input.getIntfName();
141         RpcResultBuilder<GetInterfaceTypeOutput> rpcResultBuilder;
142         try {
143             InterfaceKey interfaceKey = new InterfaceKey(interfaceName);
144             Interface interfaceInfo = InterfaceManagerCommonUtils.getInterfaceFromConfigDS(interfaceKey, dataBroker);
145
146             GetInterfaceTypeOutputBuilder output = new GetInterfaceTypeOutputBuilder().setInterfaceType(interfaceInfo.getType());
147             rpcResultBuilder = RpcResultBuilder.success();
148             rpcResultBuilder.withResult(output.build());
149         } catch (Exception e) {
150             LOG.error("Retrieval of interface type for the key {} failed due to {}", interfaceName, e);
151             rpcResultBuilder = RpcResultBuilder.failed();
152         }
153         return Futures.immediateFuture(rpcResultBuilder.build());
154     }
155
156     @Override
157     public Future<RpcResult<GetEgressActionsForInterfaceOutput>> getEgressActionsForInterface(GetEgressActionsForInterfaceInput input) {
158         RpcResultBuilder<GetEgressActionsForInterfaceOutput> rpcResultBuilder;
159         try {
160             List<Action> actionsList = getEgressActionsForInterface(input.getIntfName());
161             GetEgressActionsForInterfaceOutputBuilder output = new GetEgressActionsForInterfaceOutputBuilder().
162                     setAction(actionsList);
163             rpcResultBuilder = RpcResultBuilder.success();
164             rpcResultBuilder.withResult(output.build());
165         }catch(Exception e){
166             LOG.error("Retrieval of egress actions for the key {} failed due to {}" ,input.getIntfName(), e);
167             rpcResultBuilder = RpcResultBuilder.failed();
168         }
169         return Futures.immediateFuture(rpcResultBuilder.build());
170     }
171
172     @Override
173     public Future<RpcResult<GetPortFromInterfaceOutput>> getPortFromInterface(GetPortFromInterfaceInput input) {
174         RpcResultBuilder<GetPortFromInterfaceOutput> rpcResultBuilder;
175         String interfaceName = input.getIntfName();
176         try {
177             BigInteger dpId = null;
178             long portNo = 0;
179             org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState =
180                         InterfaceManagerCommonUtils.getInterfaceStateFromOperDS(interfaceName, dataBroker);
181             String lowerLayerIf = ifState.getLowerLayerIf().get(0);
182             NodeConnectorId nodeConnectorId = new NodeConnectorId(lowerLayerIf);
183             dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
184             portNo = Long.valueOf(IfmUtil.getPortNoFromNodeConnectorId(nodeConnectorId));
185             // FIXME Assuming portName and interfaceName are same
186             GetPortFromInterfaceOutputBuilder output = new GetPortFromInterfaceOutputBuilder().setDpid(dpId).
187                     setPortname(interfaceName).setPortno(Long.valueOf(portNo));
188             rpcResultBuilder = RpcResultBuilder.success();
189             rpcResultBuilder.withResult(output.build());
190         }catch(Exception e){
191             LOG.error("Retrieval of lport tag for the key {} failed due to {}" ,input.getIntfName(), e);
192             rpcResultBuilder = RpcResultBuilder.failed();
193         }
194         return Futures.immediateFuture(rpcResultBuilder.build());
195     }
196
197     @Override
198     public Future<RpcResult<GetNodeconnectorIdFromInterfaceOutput>> getNodeconnectorIdFromInterface(GetNodeconnectorIdFromInterfaceInput input) {
199         String interfaceName = input.getIntfName();
200         RpcResultBuilder<GetNodeconnectorIdFromInterfaceOutput> rpcResultBuilder;
201         try {
202             org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState =
203                     InterfaceManagerCommonUtils.getInterfaceStateFromOperDS(interfaceName, dataBroker);
204             String lowerLayerIf = ifState.getLowerLayerIf().get(0);
205             NodeConnectorId nodeConnectorId = new NodeConnectorId(lowerLayerIf);
206
207             GetNodeconnectorIdFromInterfaceOutputBuilder output = new GetNodeconnectorIdFromInterfaceOutputBuilder().setNodeconnectorId(nodeConnectorId);
208             rpcResultBuilder = RpcResultBuilder.success();
209             rpcResultBuilder.withResult(output.build());
210         } catch (Exception e) {
211             LOG.error("Retrieval of nodeconnector id for the key {} failed due to {}", interfaceName, e);
212             rpcResultBuilder = RpcResultBuilder.failed();
213         }
214         return Futures.immediateFuture(rpcResultBuilder.build());
215     }
216
217     public List<ActionInfo> getEgressActionInfosForInterface(String interfaceName) {
218         Interface interfaceInfo = InterfaceManagerCommonUtils.getInterfaceFromConfigDS(new InterfaceKey(interfaceName),
219                 dataBroker);
220         List<ActionInfo> listActionInfo = new ArrayList<ActionInfo>();
221         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState =
222                 InterfaceManagerCommonUtils.getInterfaceStateFromOperDS(interfaceName, dataBroker);
223
224         String lowerLayerIf = ifState.getLowerLayerIf().get(0);
225         NodeConnectorId nodeConnectorId = new NodeConnectorId(lowerLayerIf);
226         String portNo = IfmUtil.getPortNoFromNodeConnectorId(nodeConnectorId);
227         Class<? extends InterfaceType> ifType = interfaceInfo.getType();
228         if(L2vlan.class.equals(ifType)){
229             IfL2vlan vlanIface = interfaceInfo.getAugmentation(IfL2vlan.class);
230             LOG.trace("L2Vlan: {}",vlanIface);
231             long vlanVid = (vlanIface == null) ? 0 : vlanIface.getVlanId().getValue();
232             if (vlanVid != 0) {
233                 listActionInfo.add(new ActionInfo(ActionType.push_vlan, new String[] {}));
234                 listActionInfo.add(new ActionInfo(ActionType.set_field_vlan_vid,
235                         new String[] { Long.toString(vlanVid) }));
236             }
237             listActionInfo.add(new ActionInfo(ActionType.output, new String[] {portNo}));
238         }else if(Tunnel.class.equals(ifType)){
239             listActionInfo.add(new ActionInfo(ActionType.output, new String[] { portNo}));
240         }
241         return listActionInfo;
242     }
243
244     public List<Action> getEgressActionsForInterface(String interfaceName) {
245             List<ActionInfo> listActionInfo = getEgressActionInfosForInterface(interfaceName);
246             List<Action> actionsList = new ArrayList<>();
247             for (ActionInfo actionInfo : listActionInfo) {
248                 actionsList.add(actionInfo.buildAction());
249             }
250             return actionsList;
251     }
252
253     protected static List<Instruction> buildInstructions(List<InstructionInfo> listInstructionInfo) {
254         if (listInstructionInfo != null) {
255             List<Instruction> instructions = new ArrayList<Instruction>();
256             int instructionKey = 0;
257
258             for (InstructionInfo instructionInfo : listInstructionInfo) {
259                 instructions.add(instructionInfo.buildInstruction(instructionKey));
260                 instructionKey++;
261             }
262             return instructions;
263         }
264
265         return null;
266     }
267
268 }