2 * Copyright (c) 2015 - 2016 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;
10 import org.opendaylight.vpnservice.utilities.InterfaceUtils;
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.JdkFutureAdapters;
16 import org.opendaylight.controller.md.sal.binding.api.*;
17 import org.opendaylight.vpnservice.mdsalutil.*;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.OpState;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.OpStateBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.adjacency.list.AdjacencyKey;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.vpn.instance.op.data.VpnInstanceOpDataEntry;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.vpn.instance.op.data.vpn.instance.op.data.entry.VpnToDpnList;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.vpn.instance.op.data.vpn.instance.op.data.entry.VpnToDpnListBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.vpn.instance.op.data.vpn.instance.op.data.entry.vpn.to.dpn.list.VpnInterfacesBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.vpn.instance.op.data.vpn.instance.op.data.entry.vpn.to.dpn.list.VpnInterfacesKey;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.arputil.rev151126.OdlArputilService;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.arputil.rev151126.SendArpResponseInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.arputil.rev151126.SendArpResponseInputBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.servicebinding.rev151015.service.bindings.services.info.BoundServices;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.fibmanager.rev150330.FibEntries;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.fibmanager.rev150330.fibentries.VrfTables;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.fibmanager.rev150330.fibentries.VrfTablesBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.fibmanager.rev150330.fibentries.VrfTablesKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.fibmanager.rev150330.vrfentries.VrfEntry;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.fibmanager.rev150330.vrfentries.VrfEntryBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.fibmanager.rev150330.vrfentries.VrfEntryKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.IdManagerService;
42 import java.math.BigInteger;
43 import java.util.Collection;
44 import java.util.Iterator;
45 import java.util.List;
46 import java.util.ArrayList;
47 import java.util.concurrent.*;
49 import com.google.common.base.Optional;
51 import org.opendaylight.bgpmanager.api.IBgpManager;
52 import org.opendaylight.fibmanager.api.IFibManager;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rpcs.rev151003.OdlInterfaceRpcService;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rpcs.rev151217.ItmRpcService;
55 import org.opendaylight.vpnservice.mdsalutil.interfaces.IMdsalApiManager;
56 import org.opendaylight.yangtools.concepts.ListenerRegistration;
57 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
58 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
59 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
60 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
61 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.adjacency.list.Adjacency;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.adjacency.list.AdjacencyBuilder;
64 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.VpnInterfaces;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.Adjacencies;
66 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.VpnAfConfig;
67 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.VpnInstances;
68 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.instances.VpnInstance;
69 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.instances.VpnInstanceKey;
70 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterface;
71 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterfaceKey;
72 import org.opendaylight.yangtools.yang.common.RpcError;
73 import org.opendaylight.yangtools.yang.common.RpcResult;
74 import org.slf4j.Logger;
75 import org.slf4j.LoggerFactory;
77 public class VpnInterfaceManager extends AbstractDataChangeListener<VpnInterface> implements AutoCloseable {
78 private static final Logger LOG = LoggerFactory.getLogger(VpnInterfaceManager.class);
79 private ListenerRegistration<DataChangeListener> listenerRegistration, opListenerRegistration;
80 private ConcurrentMap<String, Runnable> vpnIntfMap = new ConcurrentHashMap<String, Runnable>();
81 private ExecutorService executorService = Executors.newSingleThreadExecutor();
82 private final DataBroker broker;
83 private final IBgpManager bgpManager;
84 private IFibManager fibManager;
85 private IMdsalApiManager mdsalManager;
86 private OdlInterfaceRpcService interfaceManager;
87 private ItmRpcService itmProvider;
88 private IdManagerService idManager;
89 private OdlArputilService arpManager;
90 private InterfaceStateChangeListener interfaceListener;
91 private VpnInterfaceOpListener vpnInterfaceOpListener;
92 private ArpNotificationHandler arpNotificationHandler;
93 protected enum UpdateRouteAction {
94 ADVERTISE_ROUTE, WITHDRAW_ROUTE
97 * Responsible for listening to data change related to VPN Interface
98 * Bind VPN Service on the interface and informs the BGP service
100 * @param db - dataBroker service reference
102 public VpnInterfaceManager(final DataBroker db, final IBgpManager bgpManager, NotificationService notificationService) {
103 super(VpnInterface.class);
105 this.bgpManager = bgpManager;
106 interfaceListener = new InterfaceStateChangeListener(db, this);
107 vpnInterfaceOpListener = new VpnInterfaceOpListener();
108 arpNotificationHandler = new ArpNotificationHandler(this, broker);
109 notificationService.registerNotificationListener(arpNotificationHandler);
110 registerListener(db);
113 public void setMdsalManager(IMdsalApiManager mdsalManager) {
114 this.mdsalManager = mdsalManager;
117 public void setInterfaceManager(OdlInterfaceRpcService interfaceManager) {
118 this.interfaceManager = interfaceManager;
119 interfaceListener.setInterfaceManager(interfaceManager);
122 public void setITMProvider(ItmRpcService itmProvider) {
123 this.itmProvider = itmProvider;
126 public void setFibManager(IFibManager fibManager) {
127 this.fibManager = fibManager;
130 public void setIdManager(IdManagerService idManager) {
131 this.idManager = idManager;
134 public void setArpManager(OdlArputilService arpManager) {
135 this.arpManager = arpManager;
139 public void close() throws Exception {
140 if (listenerRegistration != null) {
142 listenerRegistration.close();
143 opListenerRegistration.close();
144 } catch (final Exception e) {
145 LOG.error("Error when cleaning up DataChangeListener.", e);
147 listenerRegistration = null;
148 opListenerRegistration = null;
150 LOG.info("VPN Interface Manager Closed");
153 private void registerListener(final DataBroker db) {
155 listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
156 getWildCardPath(), VpnInterfaceManager.this, DataChangeScope.SUBTREE);
157 opListenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
158 getWildCardPath(), vpnInterfaceOpListener, DataChangeScope.SUBTREE);
159 } catch (final Exception e) {
160 LOG.error("VPN Service DataChange listener registration fail!", e);
161 throw new IllegalStateException("VPN Service registration Listener failed.", e);
165 private InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> getInterfaceListenerPath() {
166 return InstanceIdentifier.create(InterfacesState.class)
167 .child(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.class);
171 protected void add(final InstanceIdentifier<VpnInterface> identifier,
172 final VpnInterface vpnInterface) {
173 LOG.trace("VPN Interface key: {} , value: {}", identifier, vpnInterface );
174 addInterface(identifier, vpnInterface);
177 private void addInterface(final InstanceIdentifier<VpnInterface> identifier,
178 final VpnInterface vpnInterface) {
179 LOG.trace("VPN Interface add event - key: {}, value: {}" ,identifier, vpnInterface );
180 final VpnInterfaceKey key = identifier.firstKeyOf(VpnInterface.class, VpnInterfaceKey.class);
181 String interfaceName = key.getName();
183 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface interfaceState =
184 InterfaceUtils.getInterfaceStateFromOperDS(broker, interfaceName);
185 if (interfaceState != null) {
186 // Interface state is up
187 processVpnInterfaceUp(InterfaceUtils.getDpIdFromInterface(interfaceState), interfaceName, interfaceState.getIfIndex());
189 LOG.trace("VPN interfaces are not yet operational.");
193 protected void processVpnInterfaceUp(BigInteger dpId, String interfaceName, int lPortTag) {
195 VpnInterface vpnInterface = VpnUtil.getConfiguredVpnInterface(broker, interfaceName);
196 if(vpnInterface == null) {
197 LOG.info("Unable to process add/up for interface {} as it is not configured", interfaceName);
200 String vpnName = vpnInterface.getVpnInstanceName();
201 LOG.info("Binding vpn service to interface {} ", interfaceName);
202 long vpnId = VpnUtil.getVpnId(broker, vpnName);
203 if (vpnId == VpnConstants.INVALID_ID) {
204 LOG.trace("VpnInstance to VPNId mapping is not yet available, bailing out now.");
207 synchronized (interfaceName.intern()) {
208 if (VpnUtil.getOperationalVpnInterface(broker, vpnInterface.getName()) != null) {
209 LOG.trace("VPN Interface already provisioned , bailing out from here.");
212 bindService(dpId, vpnName, interfaceName, lPortTag);
213 updateDpnDbs(vpnName, interfaceName, true);
214 processVpnInterfaceAdjacencies(VpnUtil.getVpnInterfaceIdentifier(vpnInterface.getName()), vpnInterface, true);
219 private void updateDpnDbs(String vpnName, String interfaceName, boolean add) {
220 long vpnId = VpnUtil.getVpnId(broker, vpnName);
221 BigInteger dpId = InterfaceUtils.getDpnForInterface(interfaceManager, interfaceName);
222 if(!dpId.equals(BigInteger.ZERO)) {
224 updateMappingDbs(vpnId, dpId, interfaceName, vpnName);
226 removeFromMappingDbs(vpnId, dpId, interfaceName, vpnName);
231 private void bindService(BigInteger dpId, String vpnInstanceName, String vpnInterfaceName, int lPortTag) {
232 int priority = VpnConstants.DEFAULT_FLOW_PRIORITY;
233 long vpnId = VpnUtil.getVpnId(broker, vpnInstanceName);
235 int instructionKey = 0;
236 List<Instruction> instructions = new ArrayList<Instruction>();
238 instructions.add(MDSALUtil.buildAndGetWriteMetadaInstruction(BigInteger.valueOf(vpnId), MetaDataUtil.METADATA_MASK_VRFID, ++instructionKey));
239 instructions.add(MDSALUtil.buildAndGetGotoTableInstruction(NwConstants.L3_FIB_TABLE, ++instructionKey));
243 InterfaceUtils.getBoundServices(String.format("%s.%s.%s", "vpn",vpnInstanceName, vpnInterfaceName),
244 VpnConstants.L3VPN_SERVICE_IDENTIFIER, priority,
245 VpnConstants.COOKIE_VM_INGRESS_TABLE, instructions);
246 VpnUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
247 InterfaceUtils.buildServiceId(vpnInterfaceName, VpnConstants.L3VPN_SERVICE_IDENTIFIER), serviceInfo);
248 makeArpFlow(dpId, VpnConstants.L3VPN_SERVICE_IDENTIFIER, lPortTag, vpnInterfaceName,
249 vpnId, ArpReplyOrRequest.REQUEST, NwConstants.ADD_FLOW);
253 private void processVpnInterfaceAdjacencies(final InstanceIdentifier<VpnInterface> identifier, VpnInterface intf,
254 boolean vpnInterfaceState) {
255 String intfName = intf.getName();
257 synchronized (intfName) {
259 InstanceIdentifier<Adjacencies> path = identifier.augmentation(Adjacencies.class);
260 Optional<Adjacencies> adjacencies = VpnUtil.read(broker, LogicalDatastoreType.CONFIGURATION, path);
262 if (adjacencies.isPresent()) {
263 List<Adjacency> nextHops = adjacencies.get().getAdjacency();
264 List<Adjacency> value = new ArrayList<>();
266 // Get the rd of the vpn instance
267 String rd = getRouteDistinguisher(intf.getVpnInstanceName());
269 BigInteger dpnId = InterfaceUtils.getDpnForInterface(interfaceManager, intfName);
270 String nextHopIp = InterfaceUtils.getEndpointIpAddressForDPN(broker, dpnId);
271 if (nextHopIp == null){
272 LOG.error("NextHop for interface {} is null", intfName);
275 LOG.trace("NextHops are {}", nextHops);
276 for (Adjacency nextHop : nextHops) {
277 String prefix = VpnUtil.getIpPrefix(nextHop.getIpAddress());
278 long label = VpnUtil.getUniqueId(idManager, VpnConstants.VPN_IDPOOL_NAME, VpnUtil
279 .getNextHopLabelKey((rd == null) ? intf.getVpnInstanceName() : rd, prefix));
280 String adjNextHop = nextHop.getNextHopIp();
281 value.add(new AdjacencyBuilder(nextHop).setLabel(label).setNextHopIp((adjNextHop != null && !adjNextHop.isEmpty()) ? adjNextHop : nextHopIp)
282 .setIpAddress(prefix).setKey(new AdjacencyKey(prefix)).build());
283 if(nextHop.getMacAddress() != null && !nextHop.getMacAddress().isEmpty()) {
286 LogicalDatastoreType.OPERATIONAL,
287 VpnUtil.getPrefixToInterfaceIdentifier(
288 VpnUtil.getVpnId(broker, intf.getVpnInstanceName()), prefix),
289 VpnUtil.getPrefixToInterface(dpnId, intf.getName(), prefix));
293 Adjacencies aug = VpnUtil.getVpnInterfaceAugmentation(value);
294 VpnInterface opInterface = VpnUtil.getVpnInterface(intfName, intf.getVpnInstanceName(),
295 aug, vpnInterfaceState);
296 InstanceIdentifier<VpnInterface> interfaceId = VpnUtil.getVpnInterfaceIdentifier(intfName);
297 VpnUtil.syncWrite(broker, LogicalDatastoreType.OPERATIONAL, interfaceId, opInterface);
298 for (Adjacency nextHop : aug.getAdjacency()) {
299 long label = nextHop.getLabel();
300 String adjNextHop = nextHop.getNextHopIp();
302 addPrefixToBGP(rd, nextHop.getIpAddress(),
303 (adjNextHop != null && !adjNextHop.isEmpty()) ? adjNextHop : nextHopIp, label);
305 // ### add FIB route directly
306 addFibEntryToDS(intf.getVpnInstanceName(), nextHop.getIpAddress(),
307 (adjNextHop != null && !adjNextHop.isEmpty()) ? adjNextHop : nextHopIp, (int) label);
314 private void makeArpFlow(BigInteger dpId,short sIndex, int lPortTag, String vpnInterfaceName,
315 long vpnId, ArpReplyOrRequest replyOrRequest, int addOrRemoveFlow){
316 List<MatchInfo> matches = new ArrayList<MatchInfo>();
317 BigInteger metadata = MetaDataUtil.getMetaDataForLPortDispatcher(lPortTag, ++sIndex, BigInteger.valueOf(vpnId));
318 BigInteger metadataMask = MetaDataUtil.getMetaDataMaskForLPortDispatcher(MetaDataUtil.METADATA_MASK_SERVICE_INDEX,
319 MetaDataUtil.METADATA_MASK_LPORT_TAG, MetaDataUtil.METADATA_MASK_VRFID);
321 // Matching Arp reply flows
322 matches.add(new MatchInfo(MatchFieldType.eth_type, new long[] { NwConstants.ETHTYPE_ARP }));
323 matches.add(new MatchInfo(MatchFieldType.metadata, new BigInteger[] {
324 metadata, metadataMask }));
326 matches.add(new MatchInfo(MatchFieldType.arp_op, new long[] { replyOrRequest.getArpOperation() }));
328 // Instruction to punt to controller
329 List<InstructionInfo> instructions = new ArrayList<InstructionInfo>();
330 List<ActionInfo> actionsInfos = new ArrayList<ActionInfo>();
331 actionsInfos.add(new ActionInfo(ActionType.punt_to_controller, new String[] {}));
332 instructions.add(new InstructionInfo(InstructionType.write_actions, actionsInfos));
334 // Install the flow entry in L3_INTERFACE_TABLE
335 String flowRef = VpnUtil.getFlowRef(dpId, NwConstants.L3_INTERFACE_TABLE,
336 NwConstants.ETHTYPE_ARP, lPortTag, replyOrRequest.getArpOperation());
337 FlowEntity flowEntity;
338 flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.L3_INTERFACE_TABLE, flowRef,
339 NwConstants.DEFAULT_ARP_FLOW_PRIORITY, replyOrRequest.getName(), 0, 0,
340 VpnUtil.getCookieArpFlow(lPortTag), matches, instructions);
342 if (addOrRemoveFlow == NwConstants.ADD_FLOW) {
343 LOG.debug("Creating ARP Flow for interface {}",vpnInterfaceName);
344 mdsalManager.installFlow(flowEntity);
346 LOG.debug("Deleting ARP Flow for interface {}",vpnInterfaceName);
347 mdsalManager.removeFlow(flowEntity);
351 private String getRouteDistinguisher(String vpnName) {
352 InstanceIdentifier<VpnInstance> id = InstanceIdentifier.builder(VpnInstances.class)
353 .child(VpnInstance.class, new VpnInstanceKey(vpnName)).build();
354 Optional<VpnInstance> vpnInstance = VpnUtil.read(broker, LogicalDatastoreType.CONFIGURATION, id);
356 if(vpnInstance.isPresent()) {
357 VpnInstance instance = vpnInstance.get();
358 VpnAfConfig config = instance.getIpv4Family();
359 rd = config.getRouteDistinguisher();
364 private synchronized void updateMappingDbs(long vpnId, BigInteger dpnId, String intfName, String vpnName) {
365 String routeDistinguisher = getRouteDistinguisher(vpnName);
366 String rd = (routeDistinguisher == null) ? vpnName : routeDistinguisher;
367 InstanceIdentifier<VpnToDpnList> id = VpnUtil.getVpnToDpnListIdentifier(rd, dpnId);
368 Optional<VpnToDpnList> dpnInVpn = VpnUtil.read(broker, LogicalDatastoreType.OPERATIONAL, id);
369 org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.vpn.instance.op.data.vpn.instance.op.data.entry.vpn.to.dpn.list.VpnInterfaces
370 vpnInterface = new VpnInterfacesBuilder().setInterfaceName(intfName).build();
372 if (dpnInVpn.isPresent()) {
373 VpnUtil.syncWrite(broker, LogicalDatastoreType.OPERATIONAL, id.child(
374 org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.vpn.instance
375 .op.data.vpn.instance.op.data.entry.vpn.to.dpn.list.VpnInterfaces.class,
376 new VpnInterfacesKey(intfName)), vpnInterface);
378 VpnUtil.syncUpdate(broker, LogicalDatastoreType.OPERATIONAL,
379 VpnUtil.getVpnInstanceOpDataIdentifier(rd),
380 VpnUtil.getVpnInstanceOpDataBuilder(rd, vpnId));
381 VpnToDpnListBuilder vpnToDpnList = new VpnToDpnListBuilder().setDpnId(dpnId);
382 List<org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.vpn.instance.op.data
383 .vpn.instance.op.data.entry.vpn.to.dpn.list.VpnInterfaces> vpnInterfaces = new ArrayList<>();
384 vpnInterfaces.add(vpnInterface);
385 VpnUtil.syncWrite(broker, LogicalDatastoreType.OPERATIONAL, id,
386 vpnToDpnList.setVpnInterfaces(vpnInterfaces).build());
389 * FIXME: DC Gateway tunnel should be built dynamically
390 //this is the first VM in this VPN on the DPN, may be a new DPN has come up,
391 //if tunnel to DC GW does not exist, create it
392 //if(!tunnelExists(dpnID, bgpManager.getDCGWIP()))
393 String dcGW = bgpManager.getDCGwIP();
394 if(dcGW != null && !dcGW.isEmpty())
396 LOG.debug("Building tunnel from DPN {} to DC GW {}", dpnId, dcGW);
397 itmProvider.buildTunnelFromDPNToDCGW(dpnId, new IpAddress(dcGW.toCharArray()));
399 fibManager.populateFibOnNewDpn(dpnId, vpnId, (rd == null) ? vpnName : rd);
403 private synchronized void removeFromMappingDbs(long vpnId, BigInteger dpnId, String intfName, String vpnName) {
404 //TODO: Delay 'DPN' removal so that other services can cleanup the entries for this dpn
405 String rd = VpnUtil.getVpnRd(broker, vpnName);
406 InstanceIdentifier<VpnToDpnList> id = VpnUtil.getVpnToDpnListIdentifier(rd, dpnId);
407 Optional<VpnToDpnList> dpnInVpn = VpnUtil.read(broker, LogicalDatastoreType.OPERATIONAL, id);
408 if (dpnInVpn.isPresent()) {
409 List<org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.vpn.instance.op.data
410 .vpn.instance.op.data.entry.vpn.to.dpn.list.VpnInterfaces> vpnInterfaces = dpnInVpn.get().getVpnInterfaces();
411 org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.vpn.instance.op.data.vpn.instance.op.data.entry.vpn.to.dpn.list.VpnInterfaces
412 currVpnInterface = new VpnInterfacesBuilder().setInterfaceName(intfName).build();
414 if (vpnInterfaces.remove(currVpnInterface)) {
415 if (vpnInterfaces.isEmpty()) {
416 VpnUtil.delete(broker, LogicalDatastoreType.OPERATIONAL, id, VpnUtil.DEFAULT_CALLBACK);
417 fibManager.cleanUpDpnForVpn(dpnId, vpnId, rd);
419 VpnUtil.delete(broker, LogicalDatastoreType.OPERATIONAL, id.child(
420 org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.vpn.instance.op.data
421 .vpn.instance.op.data.entry.vpn.to.dpn.list.VpnInterfaces.class,
422 new VpnInterfacesKey(intfName)), VpnUtil.DEFAULT_CALLBACK);
428 private void addPrefixToBGP(String rd, String prefix, String nextHopIp, long label) {
430 bgpManager.addPrefix(rd, prefix, nextHopIp, (int)label);
431 } catch(Exception e) {
432 LOG.error("Add prefix failed", e);
437 private InstanceIdentifier<VpnInterface> getWildCardPath() {
438 return InstanceIdentifier.create(VpnInterfaces.class).child(VpnInterface.class);
442 protected void remove( InstanceIdentifier<VpnInterface> identifier, VpnInterface vpnInterface) {
443 LOG.trace("Remove event - key: {}, value: {}" ,identifier, vpnInterface );
444 final VpnInterfaceKey key = identifier.firstKeyOf(VpnInterface.class, VpnInterfaceKey.class);
445 String interfaceName = key.getName();
447 InstanceIdentifier<VpnInterface> interfaceId = VpnUtil.getVpnInterfaceIdentifier(interfaceName);
448 Optional<VpnInterface> existingVpnInterface = VpnUtil.read(broker, LogicalDatastoreType.OPERATIONAL, interfaceId);
449 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface interfaceState =
450 InterfaceUtils.getInterfaceStateFromOperDS(broker, interfaceName);
452 if (existingVpnInterface.isPresent() && interfaceState != null) {
453 processVpnInterfaceDown(InterfaceUtils.getDpIdFromInterface(interfaceState), interfaceName, interfaceState.getIfIndex(), false);
455 LOG.warn("VPN interface {} was unavailable in operational data store to handle remove event", interfaceName);
459 protected void processVpnInterfaceDown(BigInteger dpId, String interfaceName, int lPortTag, boolean isInterfaceStateDown) {
460 VpnInterface vpnInterface = VpnUtil.getOperationalVpnInterface(broker, interfaceName);
461 if(vpnInterface == null) {
462 LOG.info("Unable to process delete/down for interface {} as it is not available in operational data store", interfaceName);
465 String vpnName = vpnInterface.getVpnInstanceName();
466 InstanceIdentifier<VpnInterface> identifier = VpnUtil.getVpnInterfaceIdentifier(interfaceName);
468 synchronized (interfaceName.intern()) {
469 removeAdjacenciesFromVpn(identifier, vpnInterface);
470 LOG.info("Unbinding vpn service from interface {} ", interfaceName);
471 unbindService(dpId, vpnName, interfaceName, lPortTag, isInterfaceStateDown);
473 //wait till DCN for removal of vpn interface in operational DS arrives
474 Runnable notifyTask = new VpnNotifyTask();
475 synchronized (interfaceName.intern()) {
476 vpnIntfMap.put(interfaceName, notifyTask);
477 synchronized (notifyTask) {
479 notifyTask.wait(VpnConstants.WAIT_TIME_IN_MILLISECONDS);
480 } catch (InterruptedException e) {
488 private void removeAdjacenciesFromVpn(final InstanceIdentifier<VpnInterface> identifier, VpnInterface intf) {
490 InstanceIdentifier<Adjacencies> path = identifier.augmentation(Adjacencies.class);
491 Optional<Adjacencies> adjacencies = VpnUtil.read(broker, LogicalDatastoreType.OPERATIONAL, path);
493 String rd = VpnUtil.getVpnRd(broker, intf.getVpnInstanceName());
494 if (adjacencies.isPresent()) {
495 VpnUtil.syncUpdate(broker, LogicalDatastoreType.OPERATIONAL, identifier.augmentation(OpState.class),
496 new OpStateBuilder().setStateUp(false).build());
497 List<Adjacency> nextHops = adjacencies.get().getAdjacency();
499 if (!nextHops.isEmpty()) {
500 LOG.trace("NextHops are " + nextHops);
501 for (Adjacency nextHop : nextHops) {
502 VpnUtil.releaseId(idManager, VpnConstants.VPN_IDPOOL_NAME,
503 VpnUtil.getNextHopLabelKey(rd, nextHop.getIpAddress()));
504 /*VpnUtil.delete(broker, LogicalDatastoreType.OPERATIONAL,
505 VpnUtil.getPrefixToInterfaceIdentifier(
506 VpnUtil.getVpnId(broker, intf.getVpnInstanceName()),
507 nextHop.getIpAddress()),
508 VpnUtil.DEFAULT_CALLBACK);*/
509 if (rd.equals(intf.getVpnInstanceName())) {
510 //this is an internal vpn - the rd is assigned to the vpn instance name;
511 //remove from FIB directly
512 removeFibEntryFromDS(intf.getVpnInstanceName(), nextHop.getIpAddress());
514 removePrefixFromBGP(rd, nextHop.getIpAddress());
522 private void unbindService(BigInteger dpId, String vpnInstanceName, String vpnInterfaceName,
523 int lPortTag, boolean isInterfaceStateDown) {
524 if (!isInterfaceStateDown) {
525 VpnUtil.delete(broker, LogicalDatastoreType.CONFIGURATION,
526 InterfaceUtils.buildServiceId(vpnInterfaceName,
527 VpnConstants.L3VPN_SERVICE_IDENTIFIER),
528 VpnUtil.DEFAULT_CALLBACK);
530 long vpnId = VpnUtil.getVpnId(broker, vpnInstanceName);
531 makeArpFlow(dpId, VpnConstants.L3VPN_SERVICE_IDENTIFIER, lPortTag, vpnInterfaceName,
532 vpnId, ArpReplyOrRequest.REQUEST, NwConstants.DEL_FLOW);
536 private void removePrefixFromBGP(String rd, String prefix) {
538 bgpManager.deletePrefix(rd, prefix);
539 } catch(Exception e) {
540 LOG.error("Delete prefix failed", e);
545 protected void update(InstanceIdentifier<VpnInterface> identifier, VpnInterface original, VpnInterface update) {
546 if (LOG.isTraceEnabled()) {
547 LOG.trace("Updating VPN Interface : key " + identifier + ", original value=" + original + ", update " +
550 String oldVpnName = original.getVpnInstanceName();
551 String newVpnName = update.getVpnInstanceName();
552 List<Adjacency> oldAdjs = original.getAugmentation(Adjacencies.class).getAdjacency();
553 List<Adjacency> newAdjs = update.getAugmentation(Adjacencies.class).getAdjacency();
554 if (oldAdjs == null) {
555 oldAdjs = new ArrayList<>();
557 if (newAdjs == null) {
558 newAdjs = new ArrayList<>();
560 //handles switching between <internal VPN - external VPN>
561 if (!oldVpnName.equals(newVpnName)) {
562 remove(identifier, original);
563 add(identifier, update);
565 //handle both addition and removal of adjacencies
566 //currently, new adjacency may be an extra route
567 if (!oldAdjs.equals(newAdjs)) {
568 for (Adjacency adj : newAdjs) {
569 if (oldAdjs.contains(adj)) {
572 // add new adjacency - right now only extra route will hit this path
573 addNewAdjToVpnInterface(identifier, adj);
576 for (Adjacency adj : oldAdjs) {
577 delAdjFromVpnInterface(identifier, adj);
582 public void processArpRequest(IpAddress srcIP, PhysAddress srcMac, IpAddress targetIP, String srcInterface){
583 SendArpResponseInput input = new SendArpResponseInputBuilder().setInterface(srcInterface)
584 .setIpaddress(srcIP).setSrcIpAddress(targetIP).setMacaddress(srcMac).build();
585 final String msgFormat = String.format("Send ARP Response on interface %s to destination %s", srcInterface, srcIP);
586 Future<RpcResult<Void>> future = arpManager.sendArpResponse(input);
587 Futures.addCallback(JdkFutureAdapters.listenInPoolThread(future), new FutureCallback<RpcResult<Void>>() {
589 public void onFailure(Throwable error) {
590 LOG.error("Error - {}", msgFormat, error);
594 public void onSuccess(RpcResult<Void> result) {
595 if(!result.isSuccessful()) {
596 LOG.warn("Rpc call to {} failed", msgFormat, getErrorText(result.getErrors()));
598 LOG.debug("Successful RPC Result - {}", msgFormat);
604 private String getErrorText(Collection<RpcError> errors) {
605 StringBuilder errorText = new StringBuilder();
606 for(RpcError error : errors) {
607 errorText.append(",").append(error.getErrorType()).append("-")
608 .append(error.getMessage());
610 return errorText.toString();
613 private String getTunnelInterfaceFlowRef(BigInteger dpnId, short tableId, String ifName) {
614 return new StringBuilder().append(dpnId).append(tableId).append(ifName).toString();
619 public synchronized void addFibEntryToDS(String rd, String prefix,
620 String nexthop, int label) {
622 VrfEntry vrfEntry = new VrfEntryBuilder().setDestPrefix(prefix).
623 setNextHopAddress(nexthop).setLabel((long)label).build();
624 LOG.debug("Created vrfEntry for {} nexthop {} label {}", prefix, nexthop, label);
626 List<VrfEntry> vrfEntryList = new ArrayList<VrfEntry>();
627 vrfEntryList.add(vrfEntry);
629 InstanceIdentifierBuilder<VrfTables> idBuilder =
630 InstanceIdentifier.builder(FibEntries.class).child(VrfTables.class, new VrfTablesKey(rd));
631 InstanceIdentifier<VrfTables> vrfTableId = idBuilder.build();
633 VrfTables vrfTableNew = new VrfTablesBuilder().setRouteDistinguisher(rd).
634 setVrfEntry(vrfEntryList).build();
636 VpnUtil.syncUpdate(broker, LogicalDatastoreType.CONFIGURATION, vrfTableId, vrfTableNew);
639 public synchronized void removeFibEntryFromDS(String rd, String prefix) {
641 LOG.debug("Removing fib entry with destination prefix {} from vrf table for rd {}", prefix, rd);
643 InstanceIdentifierBuilder<VrfEntry> idBuilder =
644 InstanceIdentifier.builder(FibEntries.class).child(VrfTables.class, new VrfTablesKey(rd)).child(VrfEntry.class, new VrfEntryKey(prefix));
645 InstanceIdentifier<VrfEntry> vrfEntryId = idBuilder.build();
646 VpnUtil.delete(broker, LogicalDatastoreType.CONFIGURATION, vrfEntryId, VpnUtil.DEFAULT_CALLBACK);
650 public synchronized void removeVrfFromDS(String rd) {
651 LOG.debug("Removing vrf table for rd {}", rd);
653 InstanceIdentifierBuilder<VrfTables> idBuilder =
654 InstanceIdentifier.builder(FibEntries.class).child(VrfTables.class, new VrfTablesKey(rd));
655 InstanceIdentifier<VrfTables> vrfTableId = idBuilder.build();
657 VpnUtil.delete(broker, LogicalDatastoreType.CONFIGURATION, vrfTableId, VpnUtil.DEFAULT_CALLBACK);
661 protected void addNewAdjToVpnInterface(InstanceIdentifier<VpnInterface> identifier, Adjacency adj) {
663 Optional<VpnInterface> optVpnInterface = VpnUtil.read(broker, LogicalDatastoreType.OPERATIONAL, identifier);
665 if (optVpnInterface.isPresent()) {
666 VpnInterface currVpnIntf = optVpnInterface.get();
667 String prefix = VpnUtil.getIpPrefix(adj.getIpAddress());
668 String rd = getRouteDistinguisher(currVpnIntf.getVpnInstanceName());
669 InstanceIdentifier<Adjacencies> adjPath = identifier.augmentation(Adjacencies.class);
670 Optional<Adjacencies> optAdjacencies = VpnUtil.read(broker, LogicalDatastoreType.OPERATIONAL, adjPath);
672 VpnUtil.getUniqueId(idManager, VpnConstants.VPN_IDPOOL_NAME,
673 VpnUtil.getNextHopLabelKey((rd != null) ? rd : currVpnIntf.getVpnInstanceName(), prefix));
675 List<Adjacency> adjacencies;
676 if (optAdjacencies.isPresent()) {
677 adjacencies = optAdjacencies.get().getAdjacency();
679 //This code will not be hit since VM adjacency will always be there
680 adjacencies = new ArrayList<>();
683 adjacencies.add(new AdjacencyBuilder(adj).setLabel(label).setNextHopIp(adj.getNextHopIp())
684 .setIpAddress(prefix).setKey(new AdjacencyKey(prefix)).build());
686 Adjacencies aug = VpnUtil.getVpnInterfaceAugmentation(adjacencies);
687 VpnInterface newVpnIntf = VpnUtil.getVpnInterface(currVpnIntf.getName(), currVpnIntf.getVpnInstanceName(),
688 aug, currVpnIntf.getAugmentation(OpState.class).isStateUp());
690 VpnUtil.syncUpdate(broker, LogicalDatastoreType.OPERATIONAL, identifier, newVpnIntf);
691 addExtraRoute(adj.getIpAddress(), adj.getNextHopIp(), rd, currVpnIntf.getVpnInstanceName(), (int) label, currVpnIntf.getName());
697 protected void delAdjFromVpnInterface(InstanceIdentifier<VpnInterface> identifier, Adjacency adj) {
698 Optional<VpnInterface> optVpnInterface = VpnUtil.read(broker, LogicalDatastoreType.OPERATIONAL, identifier);
700 if (optVpnInterface.isPresent()) {
701 VpnInterface currVpnIntf = optVpnInterface.get();
703 InstanceIdentifier<Adjacencies> path = identifier.augmentation(Adjacencies.class);
704 Optional<Adjacencies> optAdjacencies = VpnUtil.read(broker, LogicalDatastoreType.OPERATIONAL, path);
705 if (optAdjacencies.isPresent()) {
706 List<Adjacency> adjacencies = optAdjacencies.get().getAdjacency();
708 if (!adjacencies.isEmpty()) {
709 String rd = getRouteDistinguisher(currVpnIntf.getVpnInstanceName());
710 LOG.trace("Adjacencies are " + adjacencies);
711 Iterator<Adjacency> adjIt = adjacencies.iterator();
712 while (adjIt.hasNext()) {
713 Adjacency adjElem = adjIt.next();
714 if (adjElem.getIpAddress().equals(adj.getIpAddress())) {
715 VpnUtil.releaseId(idManager, VpnConstants.VPN_IDPOOL_NAME,
716 VpnUtil.getNextHopLabelKey(rd, adj.getIpAddress()));
719 Adjacencies aug = VpnUtil.getVpnInterfaceAugmentation(adjacencies);
720 VpnInterface newVpnIntf = VpnUtil.getVpnInterface(currVpnIntf.getName(),
721 currVpnIntf.getVpnInstanceName(),
723 currVpnIntf.getAugmentation(OpState.class).isStateUp());
725 VpnUtil.syncUpdate(broker, LogicalDatastoreType.OPERATIONAL, identifier, newVpnIntf);
727 delExtraRoute(adj.getIpAddress(), rd, currVpnIntf.getVpnInstanceName());
738 protected void addExtraRoute(String destination, String nextHop, String rd, String routerID, int label, String intfName) {
740 //add extra route to vpn mapping; advertise with nexthop as tunnel ip
743 LogicalDatastoreType.OPERATIONAL,
744 VpnUtil.getVpnToExtrarouteIdentifier(
745 (rd != null) ? rd : routerID, destination),
746 VpnUtil.getVpnToExtraroute(destination, nextHop));
748 if(intfName != null && !intfName.isEmpty()) {
749 BigInteger dpnId = InterfaceUtils.getDpnForInterface(interfaceManager, intfName);
750 String nextHopIp = InterfaceUtils.getEndpointIpAddressForDPN(broker, dpnId);
751 if (nextHopIp == null && !nextHopIp.isEmpty()) {
752 LOG.error("NextHop for interface {} is null. Adding extra route {} without nextHop", intfName,
758 addPrefixToBGP(rd, destination, nextHop, label);
760 // ### add FIB route directly
761 addFibEntryToDS(routerID, destination, nextHop, label);
765 protected void delExtraRoute(String destination, String rd, String routerID) {
767 removePrefixFromBGP(rd, destination);
769 // ### add FIB route directly
770 removeFibEntryFromDS(routerID, destination);
774 class VpnInterfaceOpListener extends org.opendaylight.vpnservice.mdsalutil.AbstractDataChangeListener<VpnInterface> {
776 public VpnInterfaceOpListener() {
777 super(VpnInterface.class);
781 protected void remove(InstanceIdentifier<VpnInterface> identifier, VpnInterface del) {
782 final VpnInterfaceKey key = identifier.firstKeyOf(VpnInterface.class, VpnInterfaceKey.class);
783 String interfaceName = key.getName();
785 //increment the vpn interface count in Vpn Instance Op Data
787 String rd = getRouteDistinguisher(del.getVpnInstanceName());
788 if(rd.isEmpty()) rd = del.getVpnInstanceName();
789 VpnInstanceOpDataEntry vpnInstOp = VpnUtil.getVpnInstanceOpData(broker, rd);
790 if(vpnInstOp != null && vpnInstOp.getVpnInterfaceCount() != null) {
791 ifCnt = vpnInstOp.getVpnInterfaceCount();
794 LOG.trace("VpnInterfaceOpListener remove: interface name {} rd {} interface count in Vpn Op Instance {}", interfaceName, rd, ifCnt);
796 VpnUtil.asyncUpdate(broker, LogicalDatastoreType.OPERATIONAL,
797 VpnUtil.getVpnInstanceOpDataIdentifier(rd),
798 VpnUtil.updateIntfCntInVpnInstOpData(ifCnt - 1, rd), VpnUtil.DEFAULT_CALLBACK);
800 //TODO: Clean up the DPN List in Vpn Instance Op if ifCnt is zero
802 notifyTaskIfRequired(interfaceName);
805 private void notifyTaskIfRequired(String intfName) {
806 Runnable notifyTask = vpnIntfMap.remove(intfName);
807 if (notifyTask == null) {
810 executorService.execute(notifyTask);
814 protected void update(InstanceIdentifier<VpnInterface> identifier, VpnInterface original, VpnInterface update) {
818 protected void add(InstanceIdentifier<VpnInterface> identifier, VpnInterface add) {
819 final VpnInterfaceKey key = identifier.firstKeyOf(VpnInterface.class, VpnInterfaceKey.class);
820 String interfaceName = key.getName();
822 //increment the vpn interface count in Vpn Instance Op Data
824 String rd = getRouteDistinguisher(add.getVpnInstanceName());
825 if(rd.isEmpty()) rd = add.getVpnInstanceName();
826 VpnInstanceOpDataEntry vpnInstOp = VpnUtil.getVpnInstanceOpData(broker, rd);
827 if(vpnInstOp != null && vpnInstOp.getVpnInterfaceCount() != null) {
828 ifCnt = vpnInstOp.getVpnInterfaceCount();
831 LOG.trace("VpnInterfaceOpListener add: interface name {} rd {} interface count in Vpn Op Instance {}", interfaceName, rd, ifCnt);
833 VpnUtil.asyncUpdate(broker, LogicalDatastoreType.OPERATIONAL,
834 VpnUtil.getVpnInstanceOpDataIdentifier(rd),
835 VpnUtil.updateIntfCntInVpnInstOpData(ifCnt + 1, rd), VpnUtil.DEFAULT_CALLBACK);
841 protected void updatePrefixesForDPN(BigInteger dpnId, UpdateRouteAction action) {
843 LOG.info("Tunnel event triggered {} for Dpn:{} ", action.name(), dpnId);
844 InstanceIdentifierBuilder<VpnInstances> idBuilder = InstanceIdentifier.builder(VpnInstances.class);
845 InstanceIdentifier<VpnInstances> vpnInstancesId = idBuilder.build();
846 Optional<VpnInstances> vpnInstances = VpnUtil.read(broker, LogicalDatastoreType.CONFIGURATION, vpnInstancesId);
848 if (vpnInstances.isPresent()) {
849 List<VpnInstance> vpnInstanceList = vpnInstances.get().getVpnInstance();
850 Iterator<VpnInstance> vpnInstIter = vpnInstanceList.iterator();
851 while (vpnInstIter.hasNext()) {
852 VpnInstance vpnInstance = vpnInstIter.next();
853 VpnAfConfig vpnConfig = vpnInstance.getIpv4Family();
854 String rd = vpnConfig.getRouteDistinguisher();
856 InstanceIdentifier<VpnToDpnList> id = VpnUtil.getVpnToDpnListIdentifier(rd, dpnId);
857 Optional<VpnToDpnList> dpnInVpn = VpnUtil.read(broker, LogicalDatastoreType.OPERATIONAL, id);
858 if (dpnInVpn.isPresent()) {
859 if (action == UpdateRouteAction.ADVERTISE_ROUTE) {
860 fibManager.populateFibOnNewDpn(dpnId, VpnUtil
861 .getVpnId(broker, vpnInstance.getVpnInstanceName()), rd);
863 List<org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.vpn.instance.op.data
864 .vpn.instance.op.data.entry.vpn.to.dpn.list.VpnInterfaces> vpnInterfaces = dpnInVpn.get().getVpnInterfaces();
865 for(org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.vpn.instance.op.data
866 .vpn.instance.op.data.entry.vpn.to.dpn.list.VpnInterfaces vpnInterface : vpnInterfaces) {
867 InstanceIdentifier<VpnInterface> vpnIntfId = VpnUtil.getVpnInterfaceIdentifier(vpnInterface.getInterfaceName());
868 InstanceIdentifier<Adjacencies> path = vpnIntfId.augmentation(Adjacencies.class);
869 Optional<Adjacencies> adjacencies = VpnUtil.read(broker, LogicalDatastoreType.OPERATIONAL, path);
871 if (adjacencies.isPresent()) {
872 List<Adjacency> adjacencyList = adjacencies.get().getAdjacency();
873 Iterator<Adjacency> adjacencyIterator = adjacencyList.iterator();
875 while (adjacencyIterator.hasNext()) {
876 Adjacency adjacency = adjacencyIterator.next();
878 if(action == UpdateRouteAction.ADVERTISE_ROUTE)
879 bgpManager.addPrefix(rd, adjacency.getIpAddress(), adjacency.getNextHopIp(), adjacency.getLabel().intValue());
880 else if(action == UpdateRouteAction.WITHDRAW_ROUTE)
881 bgpManager.deletePrefix(rd, adjacency.getIpAddress());
882 } catch (Exception e) {
883 LOG.error("Exception when updating prefix {} in vrf {} to BGP", adjacency.getIpAddress(), rd);
889 if (action == UpdateRouteAction.WITHDRAW_ROUTE) {
890 fibManager.cleanUpDpnForVpn(dpnId, VpnUtil
891 .getVpnId(broker, vpnInstance.getVpnInstanceName()), rd);