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;
23 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
25 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
26 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
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.netvirt.vpnmanager.api.VpnHelper;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanDpnInterfaces;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesList;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.elan.dpn.interfaces.list.DpnInterfaces;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.op.data.vpn.instance.op.data.entry.VpnToDpnList;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 public class VpnServiceElanDpnInterfacesListener
43 extends AsyncDataTreeChangeListenerBase<DpnInterfaces, VpnServiceElanDpnInterfacesListener> {
45 private static final Logger LOG = LoggerFactory.getLogger(VpnServiceElanDpnInterfacesListener.class);
46 private final DataBroker dataBroker;
47 private final IInterfaceManager interfaceManager;
48 private final IFibManager fibManager;
49 private final JobCoordinator jobCoordinator;
50 private final VpnUtil vpnUtil;
53 public VpnServiceElanDpnInterfacesListener(final DataBroker dataBroker, final IInterfaceManager interfaceManager,
54 final IFibManager fibManager,final JobCoordinator jobCoordinator, VpnUtil vpnUtil) {
55 this.dataBroker = dataBroker;
56 this.interfaceManager = interfaceManager;
57 this.fibManager = fibManager;
58 this.jobCoordinator = jobCoordinator;
59 this.vpnUtil = vpnUtil;
64 registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
68 public InstanceIdentifier<DpnInterfaces> getWildCardPath() {
69 return InstanceIdentifier.builder(ElanDpnInterfaces.class).child(ElanDpnInterfacesList.class)
70 .child(DpnInterfaces.class).build();
74 protected void remove(InstanceIdentifier<DpnInterfaces> identifier, DpnInterfaces dpnInterfaces) {
79 protected void update(InstanceIdentifier<DpnInterfaces> identifier, DpnInterfaces original,
80 DpnInterfaces update) {
81 LOG.info("received Dpninterfaces update event for dpn {}", update.getDpId());
82 BigInteger dpnId = update.getDpId();
83 String elanInstanceName = identifier.firstKeyOf(ElanDpnInterfacesList.class).getElanInstanceName();
84 ElanInstance elanInstance = vpnUtil.getElanInstanceByName(elanInstanceName);
85 String vpnName = vpnUtil.getVpnNameFromElanIntanceName(elanInstanceName);
86 if (vpnName == null) {
89 String primaryRd = vpnUtil.getPrimaryRd(vpnName);
90 if (elanInstance != null && !elanInstance.isExternal() && VpnUtil.isVlan(elanInstance)) {
91 jobCoordinator.enqueueJob(elanInstance.getElanInstanceName(), () -> {
92 List<String> addedInterfaces = getUpdatedInterfaceList(update.getInterfaces(),
93 original.getInterfaces());
94 for (String addedInterface : addedInterfaces) {
95 if (interfaceManager.isExternalInterface(addedInterface)) {
96 InstanceIdentifier<VpnToDpnList> id = VpnHelper.getVpnToDpnListIdentifier(primaryRd, dpnId);
97 Optional<VpnToDpnList> dpnInVpn = SingleTransactionDataBroker.syncReadOptional(dataBroker,
98 LogicalDatastoreType.OPERATIONAL, id);
99 if (!dpnInVpn.isPresent() || (dpnInVpn.get().getVpnInterfaces() != null
100 && dpnInVpn.get().getVpnInterfaces().size() != 1)) {
101 return Collections.emptyList();
103 if (!vpnUtil.shouldPopulateFibForVlan(vpnName, elanInstanceName, dpnId)) {
104 return Collections.emptyList();
106 long vpnId = vpnUtil.getVpnId(vpnName);
107 fibManager.populateFibOnNewDpn(dpnId, vpnId, primaryRd, null);
111 List<String> deletedInterfaces = getUpdatedInterfaceList(original.getInterfaces(),
112 update.getInterfaces());
113 if (!deletedInterfaces.isEmpty()) {
114 String routerPortUuid = vpnUtil.getRouterPordIdFromElanInstance(elanInstanceName);
115 if (update.getInterfaces().size() == 2 && update.getInterfaces().contains(routerPortUuid)) {
116 vpnUtil.removeRouterPortFromElanForVlanInDpn(vpnName, dpnId);
119 return Collections.emptyList();
125 protected void add(InstanceIdentifier<DpnInterfaces> identifier, DpnInterfaces dpnInterfaces) {
126 BigInteger dpnId = dpnInterfaces.getDpId();
127 String elanInstanceName = identifier.firstKeyOf(ElanDpnInterfacesList.class).getElanInstanceName();
128 ElanInstance elanInstance = vpnUtil.getElanInstanceByName(elanInstanceName);
129 if (!VpnUtil.isVlan(elanInstance)) {
132 String vpnName = vpnUtil.getVpnNameFromElanIntanceName(elanInstanceName);
133 if (vpnName == null) {
136 vpnUtil.addRouterPortToElanForVlanInDpn(vpnName, dpnId);
140 protected VpnServiceElanDpnInterfacesListener getDataTreeChangeListener() {
141 return VpnServiceElanDpnInterfacesListener.this;
145 public static List<String> getUpdatedInterfaceList(List<String> updatedInterfaceList,
146 List<String> currentInterfaceList) {
147 if (updatedInterfaceList == null) {
148 return Collections.<String>emptyList();
150 List<String> newInterfaceList = new ArrayList<>(updatedInterfaceList);
151 if (currentInterfaceList == null) {
152 return newInterfaceList;
154 List<String> origInterfaceList = new ArrayList<>(currentInterfaceList);
155 for (Iterator<String> iterator = newInterfaceList.iterator(); iterator.hasNext();) {
156 String updatedInterface = iterator.next();
157 for (String currentInterface :origInterfaceList) {
158 if (updatedInterface.equals(currentInterface)) {
163 return newInterfaceList;