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