2 * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
9 package org.opendaylight.vpnservice.interfacemgr.rpcservice;
11 import com.google.common.base.Optional;
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import com.google.common.util.concurrent.SettableFuture;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.vpnservice.interfacemgr.IfmConstants;
20 import org.opendaylight.vpnservice.interfacemgr.IfmUtil;
21 import org.opendaylight.vpnservice.interfacemgr.commons.InterfaceManagerCommonUtils;
22 import org.opendaylight.vpnservice.interfacemgr.commons.InterfaceMetaUtils;
23 import org.opendaylight.vpnservice.interfacemgr.servicebindings.flowbased.utilities.FlowBasedServicesUtils;
24 import org.opendaylight.vpnservice.mdsalutil.*;
25 import org.opendaylight.vpnservice.mdsalutil.interfaces.IMdsalApiManager;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.L2vlan;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.Tunnel;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfaceType;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.IfIndexesInterfaceMap;
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.bridge._interface.info.BridgeEntry;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge._interface.info.BridgeEntryKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge._interface.info.bridge.entry.BridgeInterfaceEntry;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfL2vlan;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfTunnel;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.ParentRefs;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rpcs.rev151003.*;
45 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
46 import org.opendaylight.yangtools.yang.common.RpcError;
47 import org.opendaylight.yangtools.yang.common.RpcResult;
48 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
52 import java.math.BigInteger;
53 import java.util.ArrayList;
54 import java.util.Arrays;
55 import java.util.List;
56 import java.util.concurrent.Future;
58 public class InterfaceManagerRpcService implements OdlInterfaceRpcService {
59 private static final Logger LOG = LoggerFactory.getLogger(InterfaceManagerRpcService.class);
60 DataBroker dataBroker;
61 IMdsalApiManager mdsalMgr;
62 public InterfaceManagerRpcService(DataBroker dataBroker, IMdsalApiManager mdsalMgr) {
63 this.dataBroker = dataBroker;
64 this.mdsalMgr = mdsalMgr;
68 public Future<RpcResult<GetDpidFromInterfaceOutput>> getDpidFromInterface(GetDpidFromInterfaceInput input) {
69 String interfaceName = input.getIntfName();
70 RpcResultBuilder<GetDpidFromInterfaceOutput> rpcResultBuilder;
72 BigInteger dpId = null;
73 InterfaceKey interfaceKey = new InterfaceKey(interfaceName);
74 Interface interfaceInfo = InterfaceManagerCommonUtils.getInterfaceFromConfigDS(interfaceKey, dataBroker);
75 if (Tunnel.class.equals(interfaceInfo.getType())) {
76 ParentRefs parentRefs = interfaceInfo.getAugmentation(ParentRefs.class);
77 dpId = parentRefs.getDatapathNodeIdentifier();
79 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState =
80 InterfaceManagerCommonUtils.getInterfaceStateFromOperDS(interfaceName, dataBroker);
81 String lowerLayerIf = ifState.getLowerLayerIf().get(0);
82 NodeConnectorId nodeConnectorId = new NodeConnectorId(lowerLayerIf);
83 dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
85 GetDpidFromInterfaceOutputBuilder output = new GetDpidFromInterfaceOutputBuilder().setDpid(
87 rpcResultBuilder = RpcResultBuilder.success();
88 rpcResultBuilder.withResult(output.build());
89 } catch (Exception e) {
90 LOG.error("Retrieval of datapath id for the key {} failed due to {}", interfaceName, e);
91 rpcResultBuilder = RpcResultBuilder.failed();
93 return Futures.immediateFuture(rpcResultBuilder.build());
97 public Future<RpcResult<Void>> createTerminatingServiceActions(CreateTerminatingServiceActionsInput input) {
98 final SettableFuture<RpcResult<Void>> result = SettableFuture.create();
100 List<MatchInfo> mkMatches = new ArrayList<MatchInfo>();
101 WriteTransaction t = dataBroker.newWriteOnlyTransaction();
102 LOG.info("create terminatingServiceAction on DpnId = {} for tunnel-key {}", input.getDpid() , input.getTunnelKey());
105 mkMatches.add(new MatchInfo(MatchFieldType.tunnel_id, new BigInteger[] {input.getTunnelKey()}));
106 List<Instruction> instructions = input.getInstruction();
107 Interface interfaceInfo = InterfaceManagerCommonUtils.getInterfaceFromConfigDS(new InterfaceKey(input.getInterfaceName()),dataBroker);
108 IfTunnel tunnelInfo = interfaceInfo.getAugmentation(IfTunnel.class);
109 if(tunnelInfo != null) {
110 short tableId = tunnelInfo.isInternal() ? IfmConstants.INTERNAL_TUNNEL_TABLE :
111 IfmConstants.EXTERNAL_TUNNEL_TABLE;
112 List<Instruction> instructionSet = new ArrayList<Instruction>();
113 if (instructions != null && !instructions.isEmpty()) {
114 for (Instruction info : instructions) {
115 instructionSet.add(info);
118 final String flowRef = getFlowRef(input.getDpid(),tableId, input.getTunnelKey());
119 Flow terminatingSerFlow = MDSALUtil.buildFlowNew(tableId, flowRef,
120 5, "TST Flow Entry", 0, 0,
121 IfmConstants.TUNNEL_TABLE_COOKIE.add(input.getTunnelKey()), mkMatches, instructionSet);
123 ListenableFuture<Void> installFlowResult = mdsalMgr.installFlow(input.getDpid(), terminatingSerFlow);
124 Futures.addCallback(installFlowResult, new FutureCallback<Void>(){
127 public void onSuccess(Void aVoid) {
128 result.set(RpcResultBuilder.<Void>success().build());
132 public void onFailure(Throwable error) {
133 String msg = String.format("Unable to install terminating service flow %s", flowRef);
134 LOG.error("create terminating service actions failed. {}. {}", msg, error);
135 result.set(RpcResultBuilder.<Void>failed().withError(RpcError.ErrorType.APPLICATION, msg, error).build());
138 result.set(RpcResultBuilder.<Void>success().build());
140 String msg = String.format("Terminating Service Actions cannot be created for a non-tunnel interface %s",input.getInterfaceName());
142 result.set(RpcResultBuilder.<Void>failed().withError(RpcError.ErrorType.APPLICATION, msg).build());
145 String msg = String.format("create Terminating Service Actions for %s failed",input.getInterfaceName());
146 LOG.error("create Terminating Service Actions for {} failed due to {}" ,input.getDpid(), e);
147 result.set(RpcResultBuilder.<Void>failed().withError(RpcError.ErrorType.APPLICATION, msg, e.getMessage()).build());
153 public Future<RpcResult<Void>> removeTerminatingServiceActions(RemoveTerminatingServiceActionsInput input) {
154 final SettableFuture<RpcResult<Void>> result = SettableFuture.create();
156 WriteTransaction t = dataBroker.newWriteOnlyTransaction();
157 LOG.info("remove terminatingServiceAction on DpnId = {} for tunnel-key {}", input.getDpid() , input.getTunnelKey());
159 Interface interfaceInfo = InterfaceManagerCommonUtils.getInterfaceFromConfigDS(new InterfaceKey(input.getInterfaceName()),dataBroker);
160 IfTunnel tunnelInfo = interfaceInfo.getAugmentation(IfTunnel.class);
161 if(tunnelInfo != null) {
162 short tableId = tunnelInfo.isInternal() ? IfmConstants.INTERNAL_TUNNEL_TABLE :
163 IfmConstants.EXTERNAL_TUNNEL_TABLE;
164 final String flowRef = getFlowRef(input.getDpid(),tableId, input.getTunnelKey());
165 FlowEntity flowEntity = MDSALUtil.buildFlowEntity(input.getDpid(), tableId, flowRef,
168 ListenableFuture<Void> removeFlowResult = mdsalMgr.removeFlow(input.getDpid(), flowEntity);
169 Futures.addCallback(removeFlowResult, new FutureCallback<Void>(){
172 public void onSuccess(Void aVoid) {
173 result.set(RpcResultBuilder.<Void>success().build());
177 public void onFailure(Throwable error) {
178 String msg = String.format("Unable to install terminating service flow %s", flowRef);
179 LOG.error("create terminating service actions failed. {}. {}", msg, error);
180 result.set(RpcResultBuilder.<Void>failed().withError(RpcError.ErrorType.APPLICATION, msg, error).build());
183 result.set(RpcResultBuilder.<Void>success().build());
185 String msg = String.format("Terminating Service Actions cannot be removed for a non-tunnel interface %s",
186 input.getInterfaceName());
188 result.set(RpcResultBuilder.<Void>failed().withError(RpcError.ErrorType.APPLICATION, msg).build());
191 LOG.error("Remove Terminating Service Actions for {} failed due to {}" ,input.getDpid(), e);
192 String msg = String.format("Remove Terminating Service Actions for %d failed.", input.getDpid());
193 result.set(RpcResultBuilder.<Void>failed().withError(RpcError.ErrorType.APPLICATION, msg, e.getMessage()).build());
199 public Future<RpcResult<GetEndpointIpForDpnOutput>> getEndpointIpForDpn(GetEndpointIpForDpnInput input) {
200 RpcResultBuilder<GetEndpointIpForDpnOutput> rpcResultBuilder;
202 BridgeEntryKey bridgeEntryKey = new BridgeEntryKey(input.getDpid());
203 InstanceIdentifier<BridgeEntry> bridgeEntryInstanceIdentifier =
204 InterfaceMetaUtils.getBridgeEntryIdentifier(bridgeEntryKey);
205 BridgeEntry bridgeEntry =
206 InterfaceMetaUtils.getBridgeEntryFromConfigDS(bridgeEntryInstanceIdentifier,
208 // local ip of any of the bridge interface entry will be the dpn end point ip
209 BridgeInterfaceEntry bridgeInterfaceEntry = bridgeEntry.getBridgeInterfaceEntry().get(0);
210 InterfaceKey interfaceKey = new InterfaceKey(bridgeInterfaceEntry.getInterfaceName());
211 Interface interfaceInfo = InterfaceManagerCommonUtils.getInterfaceFromConfigDS(interfaceKey, dataBroker);
212 IfTunnel tunnel = interfaceInfo.getAugmentation(IfTunnel.class);
213 GetEndpointIpForDpnOutputBuilder endpointIpForDpnOutput = new GetEndpointIpForDpnOutputBuilder().setLocalIps(Arrays.asList(tunnel.getTunnelSource()));
214 rpcResultBuilder = RpcResultBuilder.success();
215 rpcResultBuilder.withResult(endpointIpForDpnOutput.build());
217 LOG.error("Retrieval of endpoint of for dpn {} failed due to {}" ,input.getDpid(), e);
218 rpcResultBuilder = RpcResultBuilder.failed();
220 return Futures.immediateFuture(rpcResultBuilder.build());
224 public Future<RpcResult<GetEgressInstructionsForInterfaceOutput>> getEgressInstructionsForInterface(GetEgressInstructionsForInterfaceInput input) {
225 RpcResultBuilder<GetEgressInstructionsForInterfaceOutput> rpcResultBuilder;
227 List<InstructionInfo> instructionInfo = new ArrayList<InstructionInfo>();
228 List<ActionInfo> actionInfo = getEgressActionInfosForInterface(input.getIntfName());
229 instructionInfo.add(new InstructionInfo(InstructionType.write_actions, actionInfo));
230 GetEgressInstructionsForInterfaceOutputBuilder output = new GetEgressInstructionsForInterfaceOutputBuilder().
231 setInstruction(buildInstructions(instructionInfo));
232 rpcResultBuilder = RpcResultBuilder.success();
233 rpcResultBuilder.withResult(output.build());
235 LOG.error("Retrieval of egress actions for the key {} failed due to {}" ,input.getIntfName(), e);
236 rpcResultBuilder = RpcResultBuilder.failed();
238 return Futures.immediateFuture(rpcResultBuilder.build());
242 public Future<RpcResult<GetInterfaceTypeOutput>> getInterfaceType(GetInterfaceTypeInput input) {
243 String interfaceName = input.getIntfName();
244 RpcResultBuilder<GetInterfaceTypeOutput> rpcResultBuilder;
246 InterfaceKey interfaceKey = new InterfaceKey(interfaceName);
247 Interface interfaceInfo = InterfaceManagerCommonUtils.getInterfaceFromConfigDS(interfaceKey, dataBroker);
249 GetInterfaceTypeOutputBuilder output = new GetInterfaceTypeOutputBuilder().setInterfaceType(interfaceInfo.getType());
250 rpcResultBuilder = RpcResultBuilder.success();
251 rpcResultBuilder.withResult(output.build());
252 } catch (Exception e) {
253 LOG.error("Retrieval of interface type for the key {} failed due to {}", interfaceName, e);
254 rpcResultBuilder = RpcResultBuilder.failed();
256 return Futures.immediateFuture(rpcResultBuilder.build());
260 public Future<RpcResult<GetEgressActionsForInterfaceOutput>> getEgressActionsForInterface(GetEgressActionsForInterfaceInput input) {
261 RpcResultBuilder<GetEgressActionsForInterfaceOutput> rpcResultBuilder;
263 List<Action> actionsList = getEgressActionsForInterface(input.getIntfName());
264 GetEgressActionsForInterfaceOutputBuilder output = new GetEgressActionsForInterfaceOutputBuilder().
265 setAction(actionsList);
266 rpcResultBuilder = RpcResultBuilder.success();
267 rpcResultBuilder.withResult(output.build());
269 LOG.error("Retrieval of egress actions for the key {} failed due to {}" ,input.getIntfName(), e);
270 rpcResultBuilder = RpcResultBuilder.failed();
272 return Futures.immediateFuture(rpcResultBuilder.build());
276 public Future<RpcResult<GetPortFromInterfaceOutput>> getPortFromInterface(GetPortFromInterfaceInput input) {
277 RpcResultBuilder<GetPortFromInterfaceOutput> rpcResultBuilder;
278 String interfaceName = input.getIntfName();
280 BigInteger dpId = null;
282 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState =
283 InterfaceManagerCommonUtils.getInterfaceStateFromOperDS(interfaceName, dataBroker);
284 String lowerLayerIf = ifState.getLowerLayerIf().get(0);
285 NodeConnectorId nodeConnectorId = new NodeConnectorId(lowerLayerIf);
286 dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
287 portNo = Long.valueOf(IfmUtil.getPortNoFromNodeConnectorId(nodeConnectorId));
288 // FIXME Assuming portName and interfaceName are same
289 GetPortFromInterfaceOutputBuilder output = new GetPortFromInterfaceOutputBuilder().setDpid(dpId).
290 setPortname(interfaceName).setPortno(Long.valueOf(portNo));
291 rpcResultBuilder = RpcResultBuilder.success();
292 rpcResultBuilder.withResult(output.build());
294 LOG.error("Retrieval of lport tag for the key {} failed due to {}" ,input.getIntfName(), e);
295 rpcResultBuilder = RpcResultBuilder.failed();
297 return Futures.immediateFuture(rpcResultBuilder.build());
301 public Future<RpcResult<GetNodeconnectorIdFromInterfaceOutput>> getNodeconnectorIdFromInterface(GetNodeconnectorIdFromInterfaceInput input) {
302 String interfaceName = input.getIntfName();
303 RpcResultBuilder<GetNodeconnectorIdFromInterfaceOutput> rpcResultBuilder;
305 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState =
306 InterfaceManagerCommonUtils.getInterfaceStateFromOperDS(interfaceName, dataBroker);
307 String lowerLayerIf = ifState.getLowerLayerIf().get(0);
308 NodeConnectorId nodeConnectorId = new NodeConnectorId(lowerLayerIf);
310 GetNodeconnectorIdFromInterfaceOutputBuilder output = new GetNodeconnectorIdFromInterfaceOutputBuilder().setNodeconnectorId(nodeConnectorId);
311 rpcResultBuilder = RpcResultBuilder.success();
312 rpcResultBuilder.withResult(output.build());
313 } catch (Exception e) {
314 LOG.error("Retrieval of nodeconnector id for the key {} failed due to {}", interfaceName, e);
315 rpcResultBuilder = RpcResultBuilder.failed();
317 return Futures.immediateFuture(rpcResultBuilder.build());
321 public Future<RpcResult<GetInterfaceFromIfIndexOutput>> getInterfaceFromIfIndex(GetInterfaceFromIfIndexInput input) {
322 Integer ifIndex = input.getIfIndex();
323 RpcResultBuilder<GetInterfaceFromIfIndexOutput> rpcResultBuilder = null;
325 InstanceIdentifier<IfIndexInterface> id = InstanceIdentifier.builder(IfIndexesInterfaceMap.class).child(IfIndexInterface.class, new IfIndexInterfaceKey(ifIndex)).build();
326 Optional<IfIndexInterface> ifIndexesInterface = IfmUtil.read(LogicalDatastoreType.OPERATIONAL, id, dataBroker);
327 if(ifIndexesInterface.isPresent()) {
328 String interfaceName = ifIndexesInterface.get().getInterfaceName();
329 GetInterfaceFromIfIndexOutputBuilder output = new GetInterfaceFromIfIndexOutputBuilder().setInterfaceName(interfaceName);
330 rpcResultBuilder = RpcResultBuilder.success();
331 rpcResultBuilder.withResult(output.build());
333 } catch (Exception e) {
334 LOG.error("Retrieval of interfaceName for the key {} failed due to {}", ifIndex, e);
335 rpcResultBuilder = RpcResultBuilder.failed();
337 return Futures.immediateFuture(rpcResultBuilder.build());
340 public List<ActionInfo> getEgressActionInfosForInterface(String interfaceName) {
341 Interface interfaceInfo = InterfaceManagerCommonUtils.getInterfaceFromConfigDS(new InterfaceKey(interfaceName),
343 List<ActionInfo> listActionInfo = new ArrayList<ActionInfo>();
344 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState =
345 InterfaceManagerCommonUtils.getInterfaceStateFromOperDS(interfaceName, dataBroker);
347 String lowerLayerIf = ifState.getLowerLayerIf().get(0);
348 NodeConnectorId nodeConnectorId = new NodeConnectorId(lowerLayerIf);
349 String portNo = IfmUtil.getPortNoFromNodeConnectorId(nodeConnectorId);
350 Class<? extends InterfaceType> ifType = interfaceInfo.getType();
351 if(L2vlan.class.equals(ifType)){
352 IfL2vlan vlanIface = interfaceInfo.getAugmentation(IfL2vlan.class);
353 LOG.trace("L2Vlan: {}",vlanIface);
354 long vlanVid = (vlanIface == null) ? 0 : vlanIface.getVlanId().getValue();
356 listActionInfo.add(new ActionInfo(ActionType.push_vlan, new String[] {}));
357 listActionInfo.add(new ActionInfo(ActionType.set_field_vlan_vid,
358 new String[] { Long.toString(vlanVid) }));
360 listActionInfo.add(new ActionInfo(ActionType.output, new String[] {portNo}));
361 }else if(Tunnel.class.equals(ifType)){
362 listActionInfo.add(new ActionInfo(ActionType.output, new String[] { portNo}));
364 return listActionInfo;
367 public List<Action> getEgressActionsForInterface(String interfaceName) {
368 List<ActionInfo> listActionInfo = getEgressActionInfosForInterface(interfaceName);
369 List<Action> actionsList = new ArrayList<>();
370 for (ActionInfo actionInfo : listActionInfo) {
371 actionsList.add(actionInfo.buildAction());
376 protected static List<Instruction> buildInstructions(List<InstructionInfo> listInstructionInfo) {
377 if (listInstructionInfo != null) {
378 List<Instruction> instructions = new ArrayList<Instruction>();
379 int instructionKey = 0;
381 for (InstructionInfo instructionInfo : listInstructionInfo) {
382 instructions.add(instructionInfo.buildInstruction(instructionKey));
391 private String getFlowRef(BigInteger dpnId, short tableId, BigInteger tunnelKey) {
392 return new StringBuffer().append(IfmConstants.TUNNEL_TABLE_FLOWID_PREFIX).append(dpnId).append(NwConstants.FLOWID_SEPARATOR)
393 .append(tableId).append(NwConstants.FLOWID_SEPARATOR).append(tunnelKey).toString();