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 java.util.ArrayList;
11 import java.util.Arrays;
12 import java.util.List;
13 import java.util.concurrent.*;
15 import com.google.common.util.concurrent.CheckedFuture;
16 import org.opendaylight.bgpmanager.api.IBgpManager;
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
19 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
22 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
23 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.af.config.vpntargets.VpnTarget;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.VpnInstanceToVpnId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.VpnInstanceToVpnIdBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.VpnRouteList;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.vpn.instance.op.data.VpnInstanceOpDataEntry;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.vpn.instance.op.data.VpnInstanceOpDataEntryBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.vpn.instance.op.data.VpnInstanceOpDataEntryKey;
30 import org.opendaylight.yangtools.concepts.ListenerRegistration;
31 import org.opendaylight.yangtools.yang.binding.DataObject;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
34 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
35 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.VpnAfConfig;
36 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.VpnInterfaces;
37 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.instances.VpnInstance;
38 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.instances.VpnInstanceKey;
39 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.VpnInstances;
40 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.instances.VpnInstanceBuilder;
41 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterface;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.VpnInstanceOpData;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.VpnInstanceOpDataBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.fibmanager.rev150330.FibEntries;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.fibmanager.rev150330.fibentries.VrfTables;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.fibmanager.rev150330.fibentries.VrfTablesKey;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.fibmanager.rev150330.vrfentries.VrfEntry;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.IdManagerService;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
52 import com.google.common.base.Optional;
53 import com.google.common.util.concurrent.FutureCallback;
54 import com.google.common.util.concurrent.Futures;
56 public class VpnManager extends AbstractDataChangeListener<VpnInstance> implements AutoCloseable {
57 private static final Logger LOG = LoggerFactory.getLogger(VpnManager.class);
58 private ListenerRegistration<DataChangeListener> listenerRegistration, fibListenerRegistration, opListenerRegistration;
59 private ConcurrentMap<String, Runnable> vpnOpMap = new ConcurrentHashMap<String, Runnable>();
60 private ExecutorService executorService = Executors.newSingleThreadExecutor();
61 private final DataBroker broker;
62 private final IBgpManager bgpManager;
63 private IdManagerService idManager;
64 private VpnInterfaceManager vpnInterfaceManager;
65 private final FibEntriesListener fibListener;
66 private final VpnInstanceOpListener vpnInstOpListener;
67 private NotificationService notificationService;
69 private static final FutureCallback<Void> DEFAULT_CALLBACK =
70 new FutureCallback<Void>() {
71 public void onSuccess(Void result) {
72 LOG.debug("Success in Datastore operation");
75 public void onFailure(Throwable error) {
76 LOG.error("Error in Datastore operation", error);
81 * Listens for data change related to VPN Instance
82 * Informs the BGP about VRF information
84 * @param db - dataBroker reference
86 public VpnManager(final DataBroker db, final IBgpManager bgpManager) {
87 super(VpnInstance.class);
89 this.bgpManager = bgpManager;
90 this.fibListener = new FibEntriesListener();
91 this.vpnInstOpListener = new VpnInstanceOpListener();
95 private void registerListener(final DataBroker db) {
97 listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
98 getWildCardPath(), VpnManager.this, DataChangeScope.SUBTREE);
99 fibListenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
100 getFibEntryListenerPath(), fibListener, DataChangeScope.BASE);
101 opListenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
102 getVpnInstanceOpListenerPath(), vpnInstOpListener, DataChangeScope.SUBTREE);
104 } catch (final Exception e) {
105 LOG.error("VPN Service DataChange listener registration fail !", e);
106 throw new IllegalStateException("VPN Service registration Listener failed.", e);
110 public void setIdManager(IdManagerService idManager) {
111 this.idManager = idManager;
114 public void setVpnInterfaceManager(VpnInterfaceManager vpnInterfaceManager) {
115 this.vpnInterfaceManager = vpnInterfaceManager;
118 private void waitForOpDataRemoval(String id, long timeout) {
119 //wait till DCN for update on VPN Instance Op Data signals that vpn interfaces linked to this vpn instance is zero
120 Runnable notifyTask = new VpnNotifyTask();
121 synchronized (id.intern()) {
122 vpnOpMap.put(id, notifyTask);
123 synchronized (notifyTask) {
125 notifyTask.wait(timeout);
126 } catch (InterruptedException e) {
134 protected void remove(InstanceIdentifier<VpnInstance> identifier, VpnInstance del) {
135 LOG.trace("Remove VPN event key: {}, value: {}", identifier, del);
136 String vpnName = del.getVpnInstanceName();
138 String rd = del.getIpv4Family().getRouteDistinguisher();
140 // //Clean up vpn Interface
141 // InstanceIdentifier<VpnInterfaces> vpnInterfacesId = InstanceIdentifier.builder(VpnInterfaces.class).build();
142 // Optional<VpnInterfaces> optionalVpnInterfaces = read(LogicalDatastoreType.OPERATIONAL, vpnInterfacesId);
144 // if(optionalVpnInterfaces.isPresent()) {
145 // List<VpnInterface> vpnInterfaces = optionalVpnInterfaces.get().getVpnInterface();
146 // for(VpnInterface vpnInterface : vpnInterfaces) {
147 // if(vpnInterface.getVpnInstanceName().equals(vpnName)) {
148 // LOG.debug("VpnInterface {} will be removed from VPN {}", vpnInterface.getName(), vpnName);
149 // vpnInterfaceManager.remove(
150 // VpnUtil.getVpnInterfaceIdentifier(vpnInterface.getName()), vpnInterface);
155 //TODO(vpnteam): Entire code would need refactoring to listen only on the parent object - VPNInstance
156 Optional<VpnInstanceOpDataEntry> vpnOpValue = null;
157 if ((rd != null) && (!rd.isEmpty())) {
158 vpnOpValue = VpnUtil.read(broker, LogicalDatastoreType.OPERATIONAL,
159 VpnUtil.getVpnInstanceOpDataIdentifier(rd));
161 vpnOpValue = VpnUtil.read(broker, LogicalDatastoreType.OPERATIONAL,
162 VpnUtil.getVpnInstanceOpDataIdentifier(vpnName));
165 if ((vpnOpValue != null) && (vpnOpValue.isPresent())) {
166 VpnInstanceOpDataEntry vpnOpEntry = null;
167 long timeout = VpnConstants.MIN_WAIT_TIME_IN_MILLISECONDS;
170 vpnOpEntry = vpnOpValue.get();
171 intfCount = vpnOpEntry.getVpnInterfaceCount();
172 if (intfCount != null && intfCount > 0) {
173 // Minimum wait time of 10 seconds for one VPN Interface clearance (inclusive of full trace on)
174 timeout = intfCount * 10000;
175 // Maximum wait time of 90 seconds for all VPN Interfaces clearance (inclusive of full trace on)
176 if (timeout > VpnConstants.MAX_WAIT_TIME_IN_MILLISECONDS) {
177 timeout = VpnConstants.MAX_WAIT_TIME_IN_MILLISECONDS;
179 LOG.trace("VPNInstance removal interface count at {} for for rd {}, vpnname {}",
180 intfCount, rd, vpnName);
182 LOG.trace("VPNInstance removal thread waiting for {} seconds for rd {}, vpnname {}",
183 (timeout/1000), rd, vpnName);
185 if ((rd != null) && (!rd.isEmpty())) {
186 waitForOpDataRemoval(rd, timeout);
188 waitForOpDataRemoval(vpnName, timeout);
191 LOG.trace("Returned out of waiting for Op Data removal for rd {}, vpnname {}", rd, vpnName);
194 InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.vpn.instance.to.vpn.id.VpnInstance>
195 vpnIdentifier = VpnUtil.getVpnInstanceToVpnIdIdentifier(vpnName);
196 delete(LogicalDatastoreType.CONFIGURATION, vpnIdentifier);
197 LOG.trace("Removed vpnIdentifier for rd{} vpnname {}", rd, vpnName);
201 bgpManager.deleteVrf(rd);
202 } catch (Exception e) {
203 LOG.error("Exception when removing VRF from BGP", e);
206 delete(LogicalDatastoreType.OPERATIONAL, VpnUtil.getVpnInstanceOpDataIdentifier(rd));
209 delete(LogicalDatastoreType.OPERATIONAL, VpnUtil.getVpnInstanceOpDataIdentifier(vpnName));
212 VpnUtil.releaseId(idManager, VpnConstants.VPN_IDPOOL_NAME, vpnName);
216 protected void update(InstanceIdentifier<VpnInstance> identifier,
217 VpnInstance original, VpnInstance update) {
218 LOG.trace("Update VPN event key: {}, value: {}", identifier, update);
222 protected void add(InstanceIdentifier<VpnInstance> identifier,
224 LOG.trace("Add VPN event key: {}, value: {}", identifier, value);
225 VpnAfConfig config = value.getIpv4Family();
226 String rd = config.getRouteDistinguisher();
228 long vpnId = VpnUtil.getUniqueId(idManager, VpnConstants.VPN_IDPOOL_NAME, value.getVpnInstanceName());
229 LOG.trace("VPN instance to ID generated.");
230 org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.vpn.instance.to.vpn.id.VpnInstance
231 vpnInstanceToVpnId = VpnUtil.getVpnInstanceToVpnId(value.getVpnInstanceName(), vpnId,
232 (rd != null) ? rd : value.getVpnInstanceName());
234 syncWrite(LogicalDatastoreType.CONFIGURATION,
235 VpnUtil.getVpnInstanceToVpnIdIdentifier(value.getVpnInstanceName()),
236 vpnInstanceToVpnId, DEFAULT_CALLBACK);
240 syncWrite(LogicalDatastoreType.OPERATIONAL,
241 VpnUtil.getVpnInstanceOpDataIdentifier(value.getVpnInstanceName()),
242 VpnUtil.getVpnInstanceOpDataBuilder(value.getVpnInstanceName(), vpnId), DEFAULT_CALLBACK);
245 syncWrite(LogicalDatastoreType.OPERATIONAL,
246 VpnUtil.getVpnInstanceOpDataIdentifier(rd),
247 VpnUtil.getVpnInstanceOpDataBuilder(rd, vpnId), DEFAULT_CALLBACK);
249 List<VpnTarget> vpnTargetList = config.getVpnTargets().getVpnTarget();
251 List<String> ertList = new ArrayList<String>();
252 List<String> irtList = new ArrayList<String>();
254 for (VpnTarget vpnTarget : vpnTargetList) {
255 if (vpnTarget.getVrfRTType() == VpnTarget.VrfRTType.ExportExtcommunity) {
256 ertList.add(vpnTarget.getVrfRTValue());
258 if (vpnTarget.getVrfRTType() == VpnTarget.VrfRTType.ImportExtcommunity) {
259 irtList.add(vpnTarget.getVrfRTValue());
261 if (vpnTarget.getVrfRTType() == VpnTarget.VrfRTType.Both) {
262 ertList.add(vpnTarget.getVrfRTValue());
263 irtList.add(vpnTarget.getVrfRTValue());
268 bgpManager.addVrf(rd, irtList, ertList);
269 } catch(Exception e) {
270 LOG.error("Exception when adding VRF to BGP", e);
273 //Try to add up vpn Interfaces if already in Operational Datastore
274 InstanceIdentifier<VpnInterfaces> vpnInterfacesId = InstanceIdentifier.builder(VpnInterfaces.class).build();
275 Optional<VpnInterfaces> optionalVpnInterfaces = read(LogicalDatastoreType.CONFIGURATION, vpnInterfacesId);
277 if(optionalVpnInterfaces.isPresent()) {
278 List<VpnInterface> vpnInterfaces = optionalVpnInterfaces.get().getVpnInterface();
279 for(VpnInterface vpnInterface : vpnInterfaces) {
280 if(vpnInterface.getVpnInstanceName().equals(value.getVpnInstanceName())) {
281 LOG.debug("VpnInterface {} will be added from VPN {}", vpnInterface.getName(), value.getVpnInstanceName());
282 vpnInterfaceManager.add(
283 VpnUtil.getVpnInterfaceIdentifier(vpnInterface.getName()), vpnInterface);
290 private InstanceIdentifier<?> getWildCardPath() {
291 return InstanceIdentifier.create(VpnInstances.class).child(VpnInstance.class);
294 private InstanceIdentifier<?> getFibEntryListenerPath() {
295 return InstanceIdentifier.create(FibEntries.class).child(VrfTables.class)
296 .child(VrfEntry.class);
299 private InstanceIdentifier<?> getVpnInstanceOpListenerPath() {
300 return InstanceIdentifier.create(VpnInstanceOpData.class).child(VpnInstanceOpDataEntry.class);
305 public void close() throws Exception {
306 if (listenerRegistration != null) {
308 listenerRegistration.close();
309 } catch (final Exception e) {
310 LOG.error("Error when cleaning up Vpn DataChangeListener.", e);
312 listenerRegistration = null;
314 if (fibListenerRegistration != null) {
316 fibListenerRegistration.close();
317 } catch (final Exception e) {
318 LOG.error("Error when cleaning up Fib entries DataChangeListener.", e);
320 fibListenerRegistration = null;
322 if (opListenerRegistration != null) {
324 opListenerRegistration.close();
325 } catch (final Exception e) {
326 LOG.error("Error when cleaning up VPN Instance Operational entries DataChangeListener.", e);
328 opListenerRegistration = null;
331 LOG.trace("VPN Manager Closed");
334 private <T extends DataObject> Optional<T> read(LogicalDatastoreType datastoreType,
335 InstanceIdentifier<T> path) {
337 ReadOnlyTransaction tx = broker.newReadOnlyTransaction();
339 Optional<T> result = Optional.absent();
341 result = tx.read(datastoreType, path).get();
342 } catch (Exception e) {
343 throw new RuntimeException(e);
349 private <T extends DataObject> void asyncWrite(LogicalDatastoreType datastoreType,
350 InstanceIdentifier<T> path, T data, FutureCallback<Void> callback) {
351 WriteTransaction tx = broker.newWriteOnlyTransaction();
352 tx.put(datastoreType, path, data, true);
353 Futures.addCallback(tx.submit(), callback);
356 private <T extends DataObject> void syncWrite(LogicalDatastoreType datastoreType,
357 InstanceIdentifier<T> path, T data, FutureCallback<Void> callback) {
358 WriteTransaction tx = broker.newWriteOnlyTransaction();
359 tx.put(datastoreType, path, data, true);
360 CheckedFuture<Void, TransactionCommitFailedException> futures = tx.submit();
363 } catch (InterruptedException | ExecutionException e) {
364 LOG.error("Error writing VPN instance to ID info to datastore (path, data) : ({}, {})", path, data);
365 throw new RuntimeException(e.getMessage());
369 protected VpnInstanceOpDataEntry getVpnInstanceOpData(String rd) {
370 InstanceIdentifier<VpnInstanceOpDataEntry> id = VpnUtil.getVpnInstanceOpDataIdentifier(rd);
371 Optional<VpnInstanceOpDataEntry> vpnInstanceOpData = read(LogicalDatastoreType.OPERATIONAL, id);
372 if(vpnInstanceOpData.isPresent()) {
373 return vpnInstanceOpData.get();
378 private <T extends DataObject> void delete(LogicalDatastoreType datastoreType, InstanceIdentifier<T> path) {
379 WriteTransaction tx = broker.newWriteOnlyTransaction();
380 tx.delete(datastoreType, path);
381 Futures.addCallback(tx.submit(), DEFAULT_CALLBACK);
384 private class FibEntriesListener extends AbstractDataChangeListener<VrfEntry> {
386 public FibEntriesListener() {
387 super(VrfEntry.class);
391 protected void remove(InstanceIdentifier<VrfEntry> identifier,
393 LOG.trace("Remove Fib event - Key : {}, value : {} ", identifier, del);
394 final VrfTablesKey key = identifier.firstKeyOf(VrfTables.class, VrfTablesKey.class);
395 String rd = key.getRouteDistinguisher();
396 Long label = del.getLabel();
397 VpnInstanceOpDataEntry vpnInstanceOpData = getVpnInstanceOpData(rd);
398 if(vpnInstanceOpData != null) {
399 List<Long> routeIds = vpnInstanceOpData.getRouteEntryId();
400 if(routeIds == null) {
401 LOG.debug("Fib Route entry is empty.");
404 LOG.debug("Removing label from vpn info - {}", label);
405 routeIds.remove(label);
406 asyncWrite(LogicalDatastoreType.OPERATIONAL, VpnUtil.getVpnInstanceOpDataIdentifier(rd),
407 new VpnInstanceOpDataEntryBuilder(vpnInstanceOpData).setRouteEntryId(routeIds).build(), DEFAULT_CALLBACK);
409 LOG.warn("No VPN Instance found for RD: {}", rd);
414 protected void update(InstanceIdentifier<VrfEntry> identifier,
415 VrfEntry original, VrfEntry update) {
416 // TODO Auto-generated method stub
421 protected void add(InstanceIdentifier<VrfEntry> identifier,
423 LOG.trace("Add Vrf Entry event - Key : {}, value : {}", identifier, add);
424 final VrfTablesKey key = identifier.firstKeyOf(VrfTables.class, VrfTablesKey.class);
425 String rd = key.getRouteDistinguisher();
426 Long label = add.getLabel();
427 VpnInstanceOpDataEntry vpn = getVpnInstanceOpData(rd);
429 List<Long> routeIds = vpn.getRouteEntryId();
430 if(routeIds == null) {
431 routeIds = new ArrayList<>();
433 LOG.debug("Adding label to vpn info - {}", label);
435 asyncWrite(LogicalDatastoreType.OPERATIONAL, VpnUtil.getVpnInstanceOpDataIdentifier(rd),
436 new VpnInstanceOpDataEntryBuilder(vpn).setRouteEntryId(routeIds).build(), DEFAULT_CALLBACK);
438 LOG.warn("No VPN Instance found for RD: {}", rd);
443 class VpnInstanceOpListener extends org.opendaylight.vpnservice.mdsalutil.AbstractDataChangeListener<VpnInstanceOpDataEntry> {
445 public VpnInstanceOpListener() {
446 super(VpnInstanceOpDataEntry.class);
450 protected void remove(InstanceIdentifier<VpnInstanceOpDataEntry> identifier, VpnInstanceOpDataEntry del) {
455 protected void update(InstanceIdentifier<VpnInstanceOpDataEntry> identifier, VpnInstanceOpDataEntry original, VpnInstanceOpDataEntry update) {
456 final VpnInstanceOpDataEntryKey key = identifier.firstKeyOf(VpnInstanceOpDataEntry.class, VpnInstanceOpDataEntryKey.class);
457 String vpnName = key.getVrfId();
459 LOG.trace("VpnInstanceOpListener update: vpn name {} interface count in Old VpnOp Instance {} in New VpnOp Instance {}" ,
460 vpnName, original.getVpnInterfaceCount(), update.getVpnInterfaceCount() );
462 //if((original.getVpnToDpnList().size() != update.getVpnToDpnList().size()) && (update.getVpnToDpnList().size() == 0)) {
463 if((original.getVpnInterfaceCount() != update.getVpnInterfaceCount()) && (update.getVpnInterfaceCount() == 0)) {
464 notifyTaskIfRequired(vpnName);
468 private void notifyTaskIfRequired(String vpnName) {
469 Runnable notifyTask = vpnOpMap.remove(vpnName);
470 if (notifyTask == null) {
473 executorService.execute(notifyTask);
477 protected void add(InstanceIdentifier<VpnInstanceOpDataEntry> identifier, VpnInstanceOpDataEntry add) {