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;
13 import java.math.BigInteger;
14 import java.util.ArrayList;
15 import java.util.Collections;
16 import java.util.Iterator;
17 import java.util.List;
19 import javax.annotation.PostConstruct;
20 import javax.inject.Inject;
21 import javax.inject.Singleton;
22 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
23 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
24 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
25 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
26 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
27 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
28 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
29 import org.opendaylight.netvirt.fibmanager.api.IFibManager;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanDpnInterfaces;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesList;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.elan.dpn.interfaces.list.DpnInterfaces;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.op.data.vpn.instance.op.data.entry.VpnToDpnList;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 public class VpnServiceElanDpnInterfacesListener
42 extends AsyncDataTreeChangeListenerBase<DpnInterfaces, VpnServiceElanDpnInterfacesListener> {
44 private static final Logger LOG = LoggerFactory.getLogger(VpnServiceElanDpnInterfacesListener.class);
45 private final DataBroker dataBroker;
46 private final IInterfaceManager interfaceManager;
47 private final IFibManager fibManager;
48 private final JobCoordinator jobCoordinator;
49 private final ManagedNewTransactionRunner txRunner;
52 public VpnServiceElanDpnInterfacesListener(final DataBroker dataBroker, final IInterfaceManager interfaceManager,
53 final IFibManager fibManager,final JobCoordinator jobCoordinator) {
54 this.dataBroker = dataBroker;
55 this.interfaceManager = interfaceManager;
56 this.fibManager = fibManager;
57 this.jobCoordinator = jobCoordinator;
58 this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
63 registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
67 public InstanceIdentifier<DpnInterfaces> getWildCardPath() {
68 return InstanceIdentifier.builder(ElanDpnInterfaces.class).child(ElanDpnInterfacesList.class)
69 .child(DpnInterfaces.class).build();
73 protected void remove(InstanceIdentifier<DpnInterfaces> identifier, DpnInterfaces dpnInterfaces) {
78 protected void update(InstanceIdentifier<DpnInterfaces> identifier, DpnInterfaces original,
79 DpnInterfaces update) {
80 LOG.info("received Dpninterfaces update event for dpn {}", update.getDpId());
81 BigInteger dpnId = update.getDpId();
82 String elanInstanceName = identifier.firstKeyOf(ElanDpnInterfacesList.class).getElanInstanceName();
83 ElanInstance elanInstance = VpnUtil.getElanInstanceByName(dataBroker, elanInstanceName);
84 String vpnName = VpnUtil.getVpnNameFromElanIntanceName(dataBroker,elanInstanceName);
85 if (vpnName == null) {
88 String primaryRd = VpnUtil.getPrimaryRd(dataBroker, vpnName);
89 if (elanInstance != null && !elanInstance.isExternal() && VpnUtil.isVlan(elanInstance)) {
90 jobCoordinator.enqueueJob(elanInstance.getElanInstanceName(), () -> {
91 return Collections.singletonList(txRunner.callWithNewWriteOnlyTransactionAndSubmit(writeConfigTxn -> {
92 List<String> addedInterfaces = getUpdatedInterfaceList(update.getInterfaces(),
93 original.getInterfaces());
94 for (String addedInterface: addedInterfaces) {
95 if (interfaceManager.isExternalInterface(addedInterface)) {
97 InstanceIdentifier<VpnToDpnList> id = VpnUtil.getVpnToDpnListIdentifier(primaryRd, dpnId);
98 Optional<VpnToDpnList> dpnInVpn = VpnUtil.read(dataBroker, LogicalDatastoreType.OPERATIONAL,
100 if (!dpnInVpn.isPresent() || (dpnInVpn.get().getVpnInterfaces() != null
101 && dpnInVpn.get().getVpnInterfaces().size() != 1)) {
104 if (!VpnUtil.shouldPopulateFibForVlan(dataBroker, vpnName, elanInstanceName,
105 dpnId, interfaceManager)) {
108 long vpnId = VpnUtil.getVpnId(dataBroker, vpnName);
109 fibManager.populateFibOnNewDpn(dpnId, vpnId, primaryRd, null);
113 List<String> deletedInterfaces = getUpdatedInterfaceList(original.getInterfaces(),
114 update.getInterfaces());
115 if (!deletedInterfaces.isEmpty()) {
116 String routerPortUuid = VpnUtil.getRouterPordIdFromElanInstance(dataBroker, elanInstanceName);
117 if (update.getInterfaces().size() == 2 && update.getInterfaces().contains(routerPortUuid)) {
118 VpnUtil.removeRouterPortFromElanForVlanInDpn(vpnName, dpnId, dataBroker);
128 protected void add(InstanceIdentifier<DpnInterfaces> identifier, DpnInterfaces dpnInterfaces) {
129 BigInteger dpnId = dpnInterfaces.getDpId();
130 String elanInstanceName = identifier.firstKeyOf(ElanDpnInterfacesList.class).getElanInstanceName();
131 ElanInstance elanInstance = VpnUtil.getElanInstanceByName(dataBroker, elanInstanceName);
132 if (!VpnUtil.isVlan(elanInstance)) {
135 String vpnName = VpnUtil.getVpnNameFromElanIntanceName(dataBroker,elanInstanceName);
136 if (vpnName == null) {
139 VpnUtil.addRouterPortToElanForVlanInDpn(vpnName, dpnId, dataBroker);
143 protected VpnServiceElanDpnInterfacesListener getDataTreeChangeListener() {
144 return VpnServiceElanDpnInterfacesListener.this;
148 public static List<String> getUpdatedInterfaceList(List<String> updatedInterfaceList,
149 List<String> currentInterfaceList) {
150 if (updatedInterfaceList == null) {
151 return Collections.<String>emptyList();
153 List<String> newInterfaceList = new ArrayList<>(updatedInterfaceList);
154 if (currentInterfaceList == null) {
155 return newInterfaceList;
157 List<String> origInterfaceList = new ArrayList<>(currentInterfaceList);
158 for (Iterator<String> iterator = newInterfaceList.iterator(); iterator.hasNext();) {
159 String updatedInterface = iterator.next();
160 for (String currentInterface :origInterfaceList) {
161 if (updatedInterface.equals(currentInterface)) {
166 return newInterfaceList;