2 * Copyright (c) 2018 Redhat 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.netvirt.vpnmanager;
11 import com.google.common.base.Optional;
12 import com.google.common.util.concurrent.ListenableFuture;
14 import java.math.BigInteger;
15 import java.util.ArrayList;
16 import java.util.Collections;
17 import java.util.Iterator;
18 import java.util.List;
20 import javax.annotation.PostConstruct;
21 import javax.inject.Inject;
22 import javax.inject.Singleton;
24 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
25 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
26 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
27 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
28 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
29 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
30 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
31 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
32 import org.opendaylight.infrautils.utils.concurrent.ListenableFutures;
33 import org.opendaylight.netvirt.fibmanager.api.IFibManager;
34 import org.opendaylight.netvirt.vpnmanager.api.VpnHelper;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanDpnInterfaces;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesList;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.elan.dpn.interfaces.list.DpnInterfaces;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.op.data.vpn.instance.op.data.entry.VpnToDpnList;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 public class VpnServiceElanDpnInterfacesListener
47 extends AsyncDataTreeChangeListenerBase<DpnInterfaces, VpnServiceElanDpnInterfacesListener> {
49 private static final Logger LOG = LoggerFactory.getLogger(VpnServiceElanDpnInterfacesListener.class);
50 private final DataBroker dataBroker;
51 private final IInterfaceManager interfaceManager;
52 private final IFibManager fibManager;
53 private final JobCoordinator jobCoordinator;
54 private final ManagedNewTransactionRunner txRunner;
57 public VpnServiceElanDpnInterfacesListener(final DataBroker dataBroker, final IInterfaceManager interfaceManager,
58 final IFibManager fibManager,final JobCoordinator jobCoordinator) {
59 this.dataBroker = dataBroker;
60 this.interfaceManager = interfaceManager;
61 this.fibManager = fibManager;
62 this.jobCoordinator = jobCoordinator;
63 this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
68 registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
72 public InstanceIdentifier<DpnInterfaces> getWildCardPath() {
73 return InstanceIdentifier.builder(ElanDpnInterfaces.class).child(ElanDpnInterfacesList.class)
74 .child(DpnInterfaces.class).build();
78 protected void remove(InstanceIdentifier<DpnInterfaces> identifier, DpnInterfaces dpnInterfaces) {
83 protected void update(InstanceIdentifier<DpnInterfaces> identifier, DpnInterfaces original,
84 DpnInterfaces update) {
85 LOG.info("received Dpninterfaces update event for dpn {}", update.getDpId());
86 BigInteger dpnId = update.getDpId();
87 String elanInstanceName = identifier.firstKeyOf(ElanDpnInterfacesList.class).getElanInstanceName();
88 ElanInstance elanInstance = VpnUtil.getElanInstanceByName(dataBroker, elanInstanceName);
89 String vpnName = VpnUtil.getVpnNameFromElanIntanceName(dataBroker,elanInstanceName);
90 if (vpnName == null) {
93 String primaryRd = VpnUtil.getPrimaryRd(dataBroker, vpnName);
94 if (elanInstance != null && !elanInstance.isExternal() && VpnUtil.isVlan(elanInstance)) {
95 jobCoordinator.enqueueJob(elanInstance.getElanInstanceName(), () -> {
96 ListenableFuture<Void> future = txRunner.callWithNewWriteOnlyTransactionAndSubmit(writeConfigTxn -> {
97 List<String> addedInterfaces = getUpdatedInterfaceList(update.getInterfaces(),
98 original.getInterfaces());
99 for (String addedInterface: addedInterfaces) {
100 if (interfaceManager.isExternalInterface(addedInterface)) {
101 InstanceIdentifier<VpnToDpnList> id = VpnHelper.getVpnToDpnListIdentifier(primaryRd,
103 Optional<VpnToDpnList> dpnInVpn = SingleTransactionDataBroker.syncReadOptional(dataBroker,
104 LogicalDatastoreType.OPERATIONAL, id);
105 if (!dpnInVpn.isPresent() || (dpnInVpn.get().getVpnInterfaces() != null
106 && dpnInVpn.get().getVpnInterfaces().size() != 1)) {
109 if (!VpnUtil.shouldPopulateFibForVlan(dataBroker, vpnName, elanInstanceName,
110 dpnId, interfaceManager)) {
113 long vpnId = VpnUtil.getVpnId(dataBroker, vpnName);
114 fibManager.populateFibOnNewDpn(dpnId, vpnId, primaryRd, null);
118 List<String> deletedInterfaces = getUpdatedInterfaceList(original.getInterfaces(),
119 update.getInterfaces());
120 if (!deletedInterfaces.isEmpty()) {
121 String routerPortUuid = VpnUtil.getRouterPordIdFromElanInstance(dataBroker, elanInstanceName);
122 if (update.getInterfaces().size() == 2 && update.getInterfaces().contains(routerPortUuid)) {
123 VpnUtil.removeRouterPortFromElanForVlanInDpn(vpnName, dpnId, dataBroker);
127 ListenableFutures.addErrorLogging(future, LOG, "Failed to process DpnInterfaces update event for"
128 + "dpn {} elan {} vpn {}", dpnId, elanInstanceName, vpnName);
129 return Collections.singletonList(future);
135 protected void add(InstanceIdentifier<DpnInterfaces> identifier, DpnInterfaces dpnInterfaces) {
136 BigInteger dpnId = dpnInterfaces.getDpId();
137 String elanInstanceName = identifier.firstKeyOf(ElanDpnInterfacesList.class).getElanInstanceName();
138 ElanInstance elanInstance = VpnUtil.getElanInstanceByName(dataBroker, elanInstanceName);
139 if (!VpnUtil.isVlan(elanInstance)) {
142 String vpnName = VpnUtil.getVpnNameFromElanIntanceName(dataBroker,elanInstanceName);
143 if (vpnName == null) {
146 VpnUtil.addRouterPortToElanForVlanInDpn(vpnName, dpnId, dataBroker);
150 protected VpnServiceElanDpnInterfacesListener getDataTreeChangeListener() {
151 return VpnServiceElanDpnInterfacesListener.this;
155 public static List<String> getUpdatedInterfaceList(List<String> updatedInterfaceList,
156 List<String> currentInterfaceList) {
157 if (updatedInterfaceList == null) {
158 return Collections.<String>emptyList();
160 List<String> newInterfaceList = new ArrayList<>(updatedInterfaceList);
161 if (currentInterfaceList == null) {
162 return newInterfaceList;
164 List<String> origInterfaceList = new ArrayList<>(currentInterfaceList);
165 for (Iterator<String> iterator = newInterfaceList.iterator(); iterator.hasNext();) {
166 String updatedInterface = iterator.next();
167 for (String currentInterface :origInterfaceList) {
168 if (updatedInterface.equals(currentInterface)) {
173 return newInterfaceList;