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
8 package org.opendaylight.vpnservice.itm.rpc;
10 import java.math.BigInteger;
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.concurrent.Future;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.IdManagerService;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
21 import org.opendaylight.vpnservice.itm.confighelpers.ItmExternalTunnelAddWorker;
22 import org.opendaylight.vpnservice.itm.confighelpers.ItmExternalTunnelDeleteWorker;
23 import org.opendaylight.vpnservice.itm.globals.ITMConstants;
24 import org.opendaylight.vpnservice.mdsalutil.interfaces.IMdsalApiManager;
25 import org.opendaylight.vpnservice.itm.impl.ItmUtils;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.ExternalTunnelList;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.TunnelList;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.dpn.endpoints.DPNTEPsInfo;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.dpn.endpoints.dpn.teps.info.TunnelEndPoints;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.external.tunnel.list.ExternalTunnel;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.external.tunnel.list.ExternalTunnelKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.tunnel.list.InternalTunnel;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.tunnel.list.InternalTunnelKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rpcs.rev151217.AddExternalTunnelEndpointInput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rpcs.rev151217.BuildExternalTunnelFromDpnsInput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rpcs.rev151217.CreateTerminatingServiceActionsInput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rpcs.rev151217.GetExternalTunnelInterfaceNameInput;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rpcs.rev151217.GetExternalTunnelInterfaceNameOutput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rpcs.rev151217.GetExternalTunnelInterfaceNameOutputBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rpcs.rev151217.GetInternalOrExternalInterfaceNameInput;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rpcs.rev151217.GetInternalOrExternalInterfaceNameOutput;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rpcs.rev151217.GetInternalOrExternalInterfaceNameOutputBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rpcs.rev151217.GetTunnelInterfaceNameInput;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rpcs.rev151217.GetTunnelInterfaceNameOutput;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rpcs.rev151217.GetTunnelInterfaceNameOutputBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rpcs.rev151217.ItmRpcService;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rpcs.rev151217.RemoveExternalTunnelEndpointInput;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rpcs.rev151217.RemoveExternalTunnelFromDpnsInput;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rpcs.rev151217.RemoveTerminatingServiceActionsInput;
52 import org.opendaylight.yangtools.yang.common.RpcResult;
53 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
55 import com.google.common.util.concurrent.FutureCallback;
56 import com.google.common.util.concurrent.Futures;
57 import com.google.common.util.concurrent.ListenableFuture;
58 import com.google.common.util.concurrent.SettableFuture;
60 import org.opendaylight.yangtools.yang.common.RpcError;
61 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
62 import org.opendaylight.vpnservice.mdsalutil.MDSALUtil;
63 import org.opendaylight.vpnservice.mdsalutil.MatchFieldType;
64 import org.opendaylight.vpnservice.mdsalutil.MatchInfo;
65 import org.opendaylight.vpnservice.mdsalutil.MetaDataUtil;
66 import org.opendaylight.vpnservice.mdsalutil.ActionInfo;
67 import org.opendaylight.vpnservice.mdsalutil.ActionType;
69 import com.google.common.base.Optional;
71 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
73 public class ItmManagerRpcService implements ItmRpcService {
75 private static final Logger LOG = LoggerFactory.getLogger(ItmManagerRpcService.class);
76 DataBroker dataBroker;
77 private IMdsalApiManager mdsalManager;
80 public void setMdsalManager(IMdsalApiManager mdsalManager) {
81 this.mdsalManager = mdsalManager;
84 IdManagerService idManagerService;
86 public ItmManagerRpcService(DataBroker dataBroker, IdManagerService idManagerService) {
87 this.dataBroker = dataBroker;
88 this.idManagerService = idManagerService;
92 public Future<RpcResult<GetTunnelInterfaceNameOutput>> getTunnelInterfaceName(GetTunnelInterfaceNameInput input) {
93 RpcResultBuilder<GetTunnelInterfaceNameOutput> resultBld = null;
94 BigInteger sourceDpn = input.getSourceDpid() ;
95 BigInteger destinationDpn = input.getDestinationDpid() ;
96 InstanceIdentifier<InternalTunnel> path = InstanceIdentifier.create(
98 .child(InternalTunnel.class, new InternalTunnelKey(destinationDpn, sourceDpn));
100 Optional<InternalTunnel> tnl = ItmUtils.read(LogicalDatastoreType.CONFIGURATION, path, dataBroker);
102 if( tnl != null && tnl.isPresent())
104 InternalTunnel tunnel = tnl.get();
105 GetTunnelInterfaceNameOutputBuilder output = new GetTunnelInterfaceNameOutputBuilder() ;
106 output.setInterfaceName(tunnel.getTunnelInterfaceName()) ;
107 resultBld = RpcResultBuilder.success();
108 resultBld.withResult(output.build()) ;
110 resultBld = RpcResultBuilder.failed();
113 return Futures.immediateFuture(resultBld.build());
118 public Future<RpcResult<Void>> removeExternalTunnelEndpoint(
119 RemoveExternalTunnelEndpointInput input) {
120 //Ignore the Futures for now
121 final SettableFuture<RpcResult<Void>> result = SettableFuture.create();
122 List<DPNTEPsInfo> meshedDpnList = ItmUtils.getTunnelMeshInfo(dataBroker) ;
123 ItmExternalTunnelDeleteWorker.deleteTunnels(dataBroker, idManagerService,meshedDpnList , input.getDestinationIp(), input.getTunnelType());
124 result.set(RpcResultBuilder.<Void>success().build());
129 public Future<RpcResult<Void>> removeExternalTunnelFromDpns(
130 RemoveExternalTunnelFromDpnsInput input) {
131 //Ignore the Futures for now
132 final SettableFuture<RpcResult<Void>> result = SettableFuture.create();
133 List<DPNTEPsInfo> cfgDpnList = ItmUtils.getDPNTEPListFromDPNId(dataBroker, input.getDpnId()) ;
134 ItmExternalTunnelDeleteWorker.deleteTunnels(dataBroker, idManagerService, cfgDpnList, input.getDestinationIp(), input.getTunnelType());
135 result.set(RpcResultBuilder.<Void>success().build());
140 public Future<RpcResult<Void>> buildExternalTunnelFromDpns(
141 BuildExternalTunnelFromDpnsInput input) {
142 //Ignore the Futures for now
143 final SettableFuture<RpcResult<Void>> result = SettableFuture.create();
144 List<ListenableFuture<Void>> extTunnelResultList = ItmExternalTunnelAddWorker.buildTunnelsFromDpnToExternalEndPoint(dataBroker, idManagerService,input.getDpnId(), input.getDestinationIp(), input.getTunnelType());
145 for (ListenableFuture<Void> extTunnelResult : extTunnelResultList) {
146 Futures.addCallback(extTunnelResult, new FutureCallback<Void>(){
149 public void onSuccess(Void aVoid) {
150 result.set(RpcResultBuilder.<Void>success().build());
154 public void onFailure(Throwable error) {
155 String msg = String.format("Unable to create ext tunnel");
156 LOG.error("create ext tunnel failed. {}. {}", msg, error);
157 result.set(RpcResultBuilder.<Void>failed().withError(RpcError.ErrorType.APPLICATION, msg, error).build());
161 result.set(RpcResultBuilder.<Void>success().build());
166 public Future<RpcResult<Void>> addExternalTunnelEndpoint(
167 AddExternalTunnelEndpointInput input) {
168 // TODO Auto-generated method stub
170 //Ignore the Futures for now
171 final SettableFuture<RpcResult<Void>> result = SettableFuture.create();
172 List<DPNTEPsInfo> meshedDpnList = ItmUtils.getTunnelMeshInfo(dataBroker) ;
173 ItmExternalTunnelAddWorker.buildTunnelsToExternalEndPoint(dataBroker, idManagerService,meshedDpnList, input.getDestinationIp(), input.getTunnelType()) ;
174 result.set(RpcResultBuilder.<Void>success().build());
179 public Future<RpcResult<GetExternalTunnelInterfaceNameOutput>> getExternalTunnelInterfaceName(
180 GetExternalTunnelInterfaceNameInput input) {
181 final SettableFuture<RpcResult<GetExternalTunnelInterfaceNameOutput>> result = SettableFuture.create() ;
182 RpcResultBuilder<GetExternalTunnelInterfaceNameOutput> resultBld;
183 BigInteger sourceDpn = input.getSourceDpid() ;
184 IpAddress destinationIp = input.getDestinationIp() ;
185 InstanceIdentifier<ExternalTunnel> path = InstanceIdentifier.create(
186 ExternalTunnelList.class)
187 .child(ExternalTunnel.class, new ExternalTunnelKey(destinationIp, sourceDpn));
189 Optional<ExternalTunnel> ext = ItmUtils.read(LogicalDatastoreType.CONFIGURATION, path, dataBroker);
191 if( ext != null && ext.isPresent())
193 ExternalTunnel exTunnel = ext.get();
194 GetExternalTunnelInterfaceNameOutputBuilder output = new GetExternalTunnelInterfaceNameOutputBuilder() ;
195 output.setInterfaceName(exTunnel.getTunnelInterfaceName()) ;
196 resultBld = RpcResultBuilder.success();
197 resultBld.withResult(output.build()) ;
199 resultBld = RpcResultBuilder.failed();
202 return Futures.immediateFuture(resultBld.build());
206 public Future<RpcResult<java.lang.Void>> createTerminatingServiceActions(final CreateTerminatingServiceActionsInput input) {
207 LOG.info("create terminatingServiceAction on DpnId = {} for service id {} and instructions {}", input.getDpnId() , input.getServiceId(), input.getInstruction());
208 final SettableFuture<RpcResult<Void>> result = SettableFuture.create();
209 int serviceId = input.getServiceId() ;
210 List<MatchInfo> mkMatches = new ArrayList<MatchInfo>();
211 byte[] vxLANHeader = new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
213 byte Flags = (byte) 0x08;
214 vxLANHeader[0] = Flags;
216 // Extract the serviceId details and imprint on the VxLAN Header
217 vxLANHeader[4] = (byte) (serviceId >> 16);
218 vxLANHeader[5] = (byte) (serviceId >> 8);
219 vxLANHeader[6] = (byte) (serviceId >> 0);
222 mkMatches.add(new MatchInfo(MatchFieldType.tunnel_id, new BigInteger[] {
223 new BigInteger(1, vxLANHeader),
224 MetaDataUtil.METADA_MASK_VALID_TUNNEL_ID_BIT_AND_TUNNEL_ID }));
226 Flow terminatingServiceTableFlow = MDSALUtil.buildFlowNew(ITMConstants.INTERNAL_TUNNEL_TABLE,
227 getFlowRef(ITMConstants.INTERNAL_TUNNEL_TABLE,serviceId), 5, String.format("%s:%d","ITM Flow Entry ",serviceId),
228 0, 0, ITMConstants.COOKIE_ITM.add(BigInteger.valueOf(serviceId)),mkMatches, input.getInstruction());
230 ListenableFuture<Void> installFlowResult = mdsalManager.installFlow(input.getDpnId(), terminatingServiceTableFlow);
231 Futures.addCallback(installFlowResult, new FutureCallback<Void>(){
234 public void onSuccess(Void aVoid) {
235 result.set(RpcResultBuilder.<Void>success().build());
239 public void onFailure(Throwable error) {
240 String msg = String.format("Unable to install terminating service flow for %s", input.getDpnId());
241 LOG.error("create terminating service actions failed. {}. {}", msg, error);
242 result.set(RpcResultBuilder.<Void>failed().withError(RpcError.ErrorType.APPLICATION, msg, error).build());
245 result.set(RpcResultBuilder.<Void>success().build());
250 public Future<RpcResult<java.lang.Void>> removeTerminatingServiceActions(final RemoveTerminatingServiceActionsInput input) {
251 LOG.info("remove terminatingServiceActions called with DpnId = {} and serviceId = {}", input.getDpnId(), input.getServiceId());
252 final SettableFuture<RpcResult<Void>> result = SettableFuture.create();
253 Flow terminatingServiceTableFlow = MDSALUtil.buildFlowNew(ITMConstants.INTERNAL_TUNNEL_TABLE,
254 getFlowRef(ITMConstants.INTERNAL_TUNNEL_TABLE,input.getServiceId()), 5, String.format("%s:%d","ITM Flow Entry ",input.getServiceId()),
255 0, 0, ITMConstants.COOKIE_ITM.add(BigInteger.valueOf(input.getServiceId())),getTunnelMatchesForServiceId(input.getServiceId()), null );
257 ListenableFuture<Void> installFlowResult = mdsalManager.installFlow(input.getDpnId(), terminatingServiceTableFlow);
258 Futures.addCallback(installFlowResult, new FutureCallback<Void>(){
261 public void onSuccess(Void aVoid) {
262 result.set(RpcResultBuilder.<Void>success().build());
266 public void onFailure(Throwable error) {
267 String msg = String.format("Unable to remove terminating service flow for %s", input.getDpnId());
268 LOG.error("remove terminating service actions failed. {}. {}", msg, error);
269 result.set(RpcResultBuilder.<Void>failed().withError(RpcError.ErrorType.APPLICATION, msg, error).build());
272 result.set(RpcResultBuilder.<Void>success().build());
277 public List<MatchInfo> getTunnelMatchesForServiceId(int serviceId) {
278 List<MatchInfo> mkMatches = new ArrayList<MatchInfo>();
279 byte[] vxLANHeader = new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
282 byte Flags = (byte) 0x08;
283 vxLANHeader[0] = Flags;
285 // Extract the serviceId details and imprint on the VxLAN Header
286 vxLANHeader[4] = (byte) (serviceId >> 16);
287 vxLANHeader[5] = (byte) (serviceId >> 8);
288 vxLANHeader[6] = (byte) (serviceId >> 0);
291 mkMatches.add(new MatchInfo(MatchFieldType.tunnel_id, new BigInteger[]{
292 new BigInteger(1, vxLANHeader),
293 MetaDataUtil.METADA_MASK_VALID_TUNNEL_ID_BIT_AND_TUNNEL_ID}));
298 private String getFlowRef(long termSvcTable, int svcId) {
299 return new StringBuffer().append(termSvcTable).append(svcId).toString();
303 public Future<RpcResult<GetInternalOrExternalInterfaceNameOutput>> getInternalOrExternalInterfaceName(
304 GetInternalOrExternalInterfaceNameInput input) {
305 RpcResultBuilder<GetInternalOrExternalInterfaceNameOutput> resultBld = null;
306 BigInteger srcDpn = input.getSourceDpid() ;
307 IpAddress dstIp = input.getDestinationIp() ;
308 List<DPNTEPsInfo> meshedDpnList = ItmUtils.getTunnelMeshInfo(dataBroker) ;
309 // Look for external tunnels if not look for internal tunnel
310 for( DPNTEPsInfo teps : meshedDpnList) {
311 TunnelEndPoints firstEndPt = teps.getTunnelEndPoints().get(0) ;
312 if( dstIp.equals(firstEndPt.getIpAddress())) {
313 InstanceIdentifier<InternalTunnel> path = InstanceIdentifier.create(
315 .child(InternalTunnel.class, new InternalTunnelKey(srcDpn, teps.getDPNID()));
317 Optional<InternalTunnel> tnl = ItmUtils.read(LogicalDatastoreType.CONFIGURATION, path, dataBroker);
318 if( tnl != null && tnl.isPresent())
320 InternalTunnel tunnel = tnl.get();
321 GetInternalOrExternalInterfaceNameOutputBuilder output = new GetInternalOrExternalInterfaceNameOutputBuilder().setInterfaceName(tunnel.getTunnelInterfaceName() );
322 resultBld = RpcResultBuilder.success();
323 resultBld.withResult(output.build()) ;
325 //resultBld = RpcResultBuilder.failed();
326 InstanceIdentifier<ExternalTunnel> path1 = InstanceIdentifier.create(
327 ExternalTunnelList.class)
328 .child(ExternalTunnel.class, new ExternalTunnelKey(dstIp, srcDpn));
330 Optional<ExternalTunnel> ext = ItmUtils.read(LogicalDatastoreType.CONFIGURATION, path1, dataBroker);
332 if( ext != null && ext.isPresent())
334 ExternalTunnel extTunnel = ext.get();
335 GetInternalOrExternalInterfaceNameOutputBuilder output = new GetInternalOrExternalInterfaceNameOutputBuilder().setInterfaceName(extTunnel.getTunnelInterfaceName() );
336 resultBld = RpcResultBuilder.success();
337 resultBld.withResult(output.build()) ;
339 resultBld = RpcResultBuilder.failed();
344 return Futures.immediateFuture(resultBld.build());