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.globals.ITMConstants;
23 import org.opendaylight.vpnservice.mdsalutil.interfaces.IMdsalApiManager;
24 import org.opendaylight.vpnservice.itm.impl.ItmUtils;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.ExternalTunnelList;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.TunnelList;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.dpn.endpoints.DPNTEPsInfo;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.external.tunnel.list.ExternalTunnel;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.external.tunnel.list.ExternalTunnelKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.tunnel.list.Tunnel;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.tunnel.list.TunnelKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rpcs.rev151217.AddExternalTunnelEndpointInput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rpcs.rev151217.BuildExternalTunnelFromDpnsInput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rpcs.rev151217.CreateTerminatingServiceActionsInput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rpcs.rev151217.GetExternalTunnelInterfaceNameInput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rpcs.rev151217.GetExternalTunnelInterfaceNameOutput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rpcs.rev151217.GetExternalTunnelInterfaceNameOutputBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rpcs.rev151217.GetTunnelInterfaceNameInput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rpcs.rev151217.GetTunnelInterfaceNameOutput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rpcs.rev151217.GetTunnelInterfaceNameOutputBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rpcs.rev151217.ItmRpcService;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rpcs.rev151217.RemoveExternalTunnelEndpointInput;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rpcs.rev151217.RemoveExternalTunnelFromDpnsInput;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rpcs.rev151217.RemoveTerminatingServiceActionsInput;
47 import org.opendaylight.yangtools.yang.common.RpcResult;
48 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
50 import com.google.common.util.concurrent.FutureCallback;
51 import com.google.common.util.concurrent.Futures;
52 import com.google.common.util.concurrent.ListenableFuture;
53 import com.google.common.util.concurrent.SettableFuture;
55 import org.opendaylight.yangtools.yang.common.RpcError;
56 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
57 import org.opendaylight.vpnservice.mdsalutil.MDSALUtil;
58 import org.opendaylight.vpnservice.mdsalutil.MatchFieldType;
59 import org.opendaylight.vpnservice.mdsalutil.MatchInfo;
60 import org.opendaylight.vpnservice.mdsalutil.MetaDataUtil;
61 import org.opendaylight.vpnservice.mdsalutil.ActionInfo;
62 import org.opendaylight.vpnservice.mdsalutil.ActionType;
64 import com.google.common.base.Optional;
66 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
68 public class ItmManagerRpcService implements ItmRpcService {
70 private static final Logger LOG = LoggerFactory.getLogger(ItmManagerRpcService.class);
71 DataBroker dataBroker;
72 private IMdsalApiManager mdsalManager;
75 public void setMdsalManager(IMdsalApiManager mdsalManager) {
76 this.mdsalManager = mdsalManager;
79 IdManagerService idManagerService;
81 public ItmManagerRpcService(DataBroker dataBroker, IdManagerService idManagerService) {
82 this.dataBroker = dataBroker;
83 this.idManagerService = idManagerService;
87 public Future<RpcResult<GetTunnelInterfaceNameOutput>> getTunnelInterfaceName(GetTunnelInterfaceNameInput input) {
88 RpcResultBuilder<GetTunnelInterfaceNameOutput> resultBld = null;
89 BigInteger sourceDpn = input.getSourceDpid() ;
90 BigInteger destinationDpn = input.getDestinationDpid() ;
91 InstanceIdentifier<Tunnel> path = InstanceIdentifier.create(
93 .child(Tunnel.class, new TunnelKey(destinationDpn, sourceDpn));
95 Optional<Tunnel> tnl = ItmUtils.read(LogicalDatastoreType.CONFIGURATION, path, dataBroker);
97 if( tnl != null && tnl.isPresent())
99 Tunnel tunnel = tnl.get();
100 GetTunnelInterfaceNameOutputBuilder output = new GetTunnelInterfaceNameOutputBuilder() ;
101 output.setInterfaceName(tunnel.getTunnelInterfaceName()) ;
102 resultBld = RpcResultBuilder.success();
103 resultBld.withResult(output.build()) ;
105 resultBld = RpcResultBuilder.failed();
108 return Futures.immediateFuture(resultBld.build());
113 public Future<RpcResult<Void>> removeExternalTunnelEndpoint(
114 RemoveExternalTunnelEndpointInput input) {
115 // TODO Auto-generated method stub
120 public Future<RpcResult<Void>> removeExternalTunnelFromDpns(
121 RemoveExternalTunnelFromDpnsInput input) {
122 //Ignore the Futures for now
123 final SettableFuture<RpcResult<Void>> result = SettableFuture.create();
124 // ItmExternalTunnelDeleteWorker.buildTunnelsFromDpnToExternalEndPoint(dataBroker, input.getDpnId(), null, input.getDestinationIp());
125 result.set(RpcResultBuilder.<Void>success().build());
130 public Future<RpcResult<Void>> buildExternalTunnelFromDpns(
131 BuildExternalTunnelFromDpnsInput input) {
132 //Ignore the Futures for now
133 final SettableFuture<RpcResult<Void>> result = SettableFuture.create();
134 List<ListenableFuture<Void>> extTunnelResultList = ItmExternalTunnelAddWorker.buildTunnelsFromDpnToExternalEndPoint(dataBroker, idManagerService, input.getDpnId(), input.getDestinationIp(), input.getTunnelType());
135 for (ListenableFuture<Void> extTunnelResult : extTunnelResultList) {
136 Futures.addCallback(extTunnelResult, new FutureCallback<Void>(){
139 public void onSuccess(Void aVoid) {
140 result.set(RpcResultBuilder.<Void>success().build());
144 public void onFailure(Throwable error) {
145 String msg = String.format("Unable to create ext tunnel");
146 LOG.error("create ext tunnel failed. {}. {}", msg, error);
147 result.set(RpcResultBuilder.<Void>failed().withError(RpcError.ErrorType.APPLICATION, msg, error).build());
151 result.set(RpcResultBuilder.<Void>success().build());
156 public Future<RpcResult<Void>> addExternalTunnelEndpoint(
157 AddExternalTunnelEndpointInput input) {
158 // TODO Auto-generated method stub
160 //Ignore the Futures for now
161 final SettableFuture<RpcResult<Void>> result = SettableFuture.create();
162 // ItmExternalTunnelAddWorker.buildTunnelsToExternalEndPoint(dataBroker, null, input.getDestinationIp()) ;
163 result.set(RpcResultBuilder.<Void>success().build());
168 public Future<RpcResult<GetExternalTunnelInterfaceNameOutput>> getExternalTunnelInterfaceName(
169 GetExternalTunnelInterfaceNameInput input) {
170 final SettableFuture<RpcResult<GetExternalTunnelInterfaceNameOutput>> result = SettableFuture.create() ;
171 RpcResultBuilder<GetExternalTunnelInterfaceNameOutput> resultBld;
172 BigInteger sourceDpn = input.getSourceDpid() ;
173 IpAddress destinationIp = input.getDestinationIp() ;
174 InstanceIdentifier<ExternalTunnel> path = InstanceIdentifier.create(
175 ExternalTunnelList.class)
176 .child(ExternalTunnel.class, new ExternalTunnelKey(destinationIp, sourceDpn));
178 Optional<ExternalTunnel> ext = ItmUtils.read(LogicalDatastoreType.CONFIGURATION, path, dataBroker);
180 if( ext != null && ext.isPresent())
182 ExternalTunnel exTunnel = ext.get();
183 GetExternalTunnelInterfaceNameOutputBuilder output = new GetExternalTunnelInterfaceNameOutputBuilder() ;
184 output.setInterfaceName(exTunnel.getTunnelInterfaceName()) ;
185 resultBld = RpcResultBuilder.success();
186 resultBld.withResult(output.build()) ;
188 resultBld = RpcResultBuilder.failed();
191 return Futures.immediateFuture(resultBld.build());
195 public Future<RpcResult<java.lang.Void>> createTerminatingServiceActions(final CreateTerminatingServiceActionsInput input) {
196 LOG.info("create terminatingServiceAction on DpnId = {} for service id {} and instructions {}", input.getDpnId() , input.getServiceId(), input.getInstruction());
197 final SettableFuture<RpcResult<Void>> result = SettableFuture.create();
198 int serviceId = input.getServiceId() ;
199 List<MatchInfo> mkMatches = new ArrayList<MatchInfo>();
200 byte[] vxLANHeader = new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
202 byte Flags = (byte) 0x08;
203 vxLANHeader[0] = Flags;
205 // Extract the serviceId details and imprint on the VxLAN Header
206 vxLANHeader[4] = (byte) (serviceId >> 16);
207 vxLANHeader[5] = (byte) (serviceId >> 8);
208 vxLANHeader[6] = (byte) (serviceId >> 0);
211 mkMatches.add(new MatchInfo(MatchFieldType.tunnel_id, new BigInteger[] {
212 new BigInteger(1, vxLANHeader),
213 MetaDataUtil.METADA_MASK_VALID_TUNNEL_ID_BIT_AND_TUNNEL_ID }));
215 Flow terminatingServiceTableFlow = MDSALUtil.buildFlowNew(ITMConstants.INTERNAL_TUNNEL_TABLE,
216 getFlowRef(ITMConstants.INTERNAL_TUNNEL_TABLE,serviceId), 5, String.format("%s:%d","ITM Flow Entry ",serviceId),
217 0, 0, ITMConstants.COOKIE_ITM.add(BigInteger.valueOf(serviceId)),mkMatches, input.getInstruction());
219 ListenableFuture<Void> installFlowResult = mdsalManager.installFlow(input.getDpnId(), terminatingServiceTableFlow);
220 Futures.addCallback(installFlowResult, new FutureCallback<Void>(){
223 public void onSuccess(Void aVoid) {
224 result.set(RpcResultBuilder.<Void>success().build());
228 public void onFailure(Throwable error) {
229 String msg = String.format("Unable to install terminating service flow for %s", input.getDpnId());
230 LOG.error("create terminating service actions failed. {}. {}", msg, error);
231 result.set(RpcResultBuilder.<Void>failed().withError(RpcError.ErrorType.APPLICATION, msg, error).build());
234 result.set(RpcResultBuilder.<Void>success().build());
239 public Future<RpcResult<java.lang.Void>> removeTerminatingServiceActions(final RemoveTerminatingServiceActionsInput input) {
240 LOG.info("remove terminatingServiceActions called with DpnId = {} and serviceId = {}", input.getDpnId(), input.getServiceId());
241 final SettableFuture<RpcResult<Void>> result = SettableFuture.create();
242 Flow terminatingServiceTableFlow = MDSALUtil.buildFlowNew(ITMConstants.INTERNAL_TUNNEL_TABLE,
243 getFlowRef(ITMConstants.INTERNAL_TUNNEL_TABLE,input.getServiceId()), 5, String.format("%s:%d","ITM Flow Entry ",input.getServiceId()),
244 0, 0, ITMConstants.COOKIE_ITM.add(BigInteger.valueOf(input.getServiceId())),getTunnelMatchesForServiceId(input.getServiceId()), null );
246 ListenableFuture<Void> installFlowResult = mdsalManager.installFlow(input.getDpnId(), terminatingServiceTableFlow);
247 Futures.addCallback(installFlowResult, new FutureCallback<Void>(){
250 public void onSuccess(Void aVoid) {
251 result.set(RpcResultBuilder.<Void>success().build());
255 public void onFailure(Throwable error) {
256 String msg = String.format("Unable to remove terminating service flow for %s", input.getDpnId());
257 LOG.error("remove terminating service actions failed. {}. {}", msg, error);
258 result.set(RpcResultBuilder.<Void>failed().withError(RpcError.ErrorType.APPLICATION, msg, error).build());
261 result.set(RpcResultBuilder.<Void>success().build());
266 public List<MatchInfo> getTunnelMatchesForServiceId(int serviceId) {
267 List<MatchInfo> mkMatches = new ArrayList<MatchInfo>();
268 byte[] vxLANHeader = new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
271 byte Flags = (byte) 0x08;
272 vxLANHeader[0] = Flags;
274 // Extract the serviceId details and imprint on the VxLAN Header
275 vxLANHeader[4] = (byte) (serviceId >> 16);
276 vxLANHeader[5] = (byte) (serviceId >> 8);
277 vxLANHeader[6] = (byte) (serviceId >> 0);
280 mkMatches.add(new MatchInfo(MatchFieldType.tunnel_id, new BigInteger[]{
281 new BigInteger(1, vxLANHeader),
282 MetaDataUtil.METADA_MASK_VALID_TUNNEL_ID_BIT_AND_TUNNEL_ID}));
287 private String getFlowRef(long termSvcTable, int svcId) {
288 return new StringBuffer().append(termSvcTable).append(svcId).toString();