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.util.ArrayList;
14 import java.util.Collections;
15 import java.util.Iterator;
16 import java.util.List;
18 import javax.annotation.PostConstruct;
19 import javax.inject.Inject;
20 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.datastoreutils.SingleTransactionDataBroker;
26 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
27 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
28 import org.opendaylight.netvirt.fibmanager.api.IFibManager;
29 import org.opendaylight.netvirt.vpnmanager.api.VpnHelper;
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.opendaylight.yangtools.yang.common.Uint32;
38 import org.opendaylight.yangtools.yang.common.Uint64;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 public class VpnServiceElanDpnInterfacesListener
44 extends AsyncDataTreeChangeListenerBase<DpnInterfaces, VpnServiceElanDpnInterfacesListener> {
46 private static final Logger LOG = LoggerFactory.getLogger(VpnServiceElanDpnInterfacesListener.class);
47 private final DataBroker dataBroker;
48 private final IInterfaceManager interfaceManager;
49 private final IFibManager fibManager;
50 private final JobCoordinator jobCoordinator;
51 private final VpnUtil vpnUtil;
54 public VpnServiceElanDpnInterfacesListener(final DataBroker dataBroker, final IInterfaceManager interfaceManager,
55 final IFibManager fibManager,final JobCoordinator jobCoordinator, VpnUtil vpnUtil) {
56 this.dataBroker = dataBroker;
57 this.interfaceManager = interfaceManager;
58 this.fibManager = fibManager;
59 this.jobCoordinator = jobCoordinator;
60 this.vpnUtil = vpnUtil;
65 registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
69 public InstanceIdentifier<DpnInterfaces> getWildCardPath() {
70 return InstanceIdentifier.builder(ElanDpnInterfaces.class).child(ElanDpnInterfacesList.class)
71 .child(DpnInterfaces.class).build();
75 protected void remove(InstanceIdentifier<DpnInterfaces> identifier, DpnInterfaces dpnInterfaces) {
80 protected void update(InstanceIdentifier<DpnInterfaces> identifier, DpnInterfaces original,
81 DpnInterfaces update) {
82 LOG.info("received Dpninterfaces update event for dpn {}", update.getDpId());
83 Uint64 dpnId = update.getDpId();
84 String elanInstanceName = identifier.firstKeyOf(ElanDpnInterfacesList.class).getElanInstanceName();
85 ElanInstance elanInstance = vpnUtil.getElanInstanceByName(elanInstanceName);
86 String vpnName = vpnUtil.getVpnNameFromElanIntanceName(elanInstanceName);
87 if (vpnName == null) {
90 String primaryRd = vpnUtil.getPrimaryRd(vpnName);
91 if (elanInstance != null && !elanInstance.isExternal() && VpnUtil.isVlan(elanInstance)) {
92 jobCoordinator.enqueueJob(elanInstance.getElanInstanceName(), () -> {
93 List<String> addedInterfaces = getUpdatedInterfaceList(update.getInterfaces(),
94 original.getInterfaces());
95 for (String addedInterface : addedInterfaces) {
96 if (interfaceManager.isExternalInterface(addedInterface)) {
97 InstanceIdentifier<VpnToDpnList> id = VpnHelper.getVpnToDpnListIdentifier(primaryRd, dpnId);
98 Optional<VpnToDpnList> dpnInVpn = SingleTransactionDataBroker.syncReadOptional(dataBroker,
99 LogicalDatastoreType.OPERATIONAL, id);
100 if (!dpnInVpn.isPresent() || (dpnInVpn.get().getVpnInterfaces() != null
101 && dpnInVpn.get().getVpnInterfaces().size() != 1)) {
102 return Collections.emptyList();
104 if (!vpnUtil.shouldPopulateFibForVlan(vpnName, elanInstanceName, dpnId)) {
105 return Collections.emptyList();
107 Uint32 vpnId = vpnUtil.getVpnId(vpnName);
108 fibManager.populateFibOnNewDpn(dpnId, vpnId, primaryRd, null);
112 List<String> deletedInterfaces = getUpdatedInterfaceList(original.getInterfaces(),
113 update.getInterfaces());
114 if (!deletedInterfaces.isEmpty()) {
115 String routerPortUuid = vpnUtil.getRouterPordIdFromElanInstance(elanInstanceName);
116 if (update.getInterfaces().size() == 2 && update.getInterfaces().contains(routerPortUuid)) {
117 vpnUtil.removeRouterPortFromElanForVlanInDpn(vpnName, dpnId);
120 return Collections.emptyList();
126 protected void add(InstanceIdentifier<DpnInterfaces> identifier, DpnInterfaces dpnInterfaces) {
127 Uint64 dpnId = dpnInterfaces.getDpId();
128 String elanInstanceName = identifier.firstKeyOf(ElanDpnInterfacesList.class).getElanInstanceName();
129 ElanInstance elanInstance = vpnUtil.getElanInstanceByName(elanInstanceName);
130 if (!VpnUtil.isVlan(elanInstance)) {
133 String vpnName = vpnUtil.getVpnNameFromElanIntanceName(elanInstanceName);
134 if (vpnName == null) {
137 vpnUtil.addRouterPortToElanForVlanInDpn(vpnName, dpnId);
141 protected VpnServiceElanDpnInterfacesListener getDataTreeChangeListener() {
142 return VpnServiceElanDpnInterfacesListener.this;
146 public static List<String> getUpdatedInterfaceList(List<String> updatedInterfaceList,
147 List<String> currentInterfaceList) {
148 if (updatedInterfaceList == null) {
149 return Collections.<String>emptyList();
151 List<String> newInterfaceList = new ArrayList<>(updatedInterfaceList);
152 if (currentInterfaceList == null) {
153 return newInterfaceList;
155 List<String> origInterfaceList = new ArrayList<>(currentInterfaceList);
156 for (Iterator<String> iterator = newInterfaceList.iterator(); iterator.hasNext();) {
157 String updatedInterface = iterator.next();
158 for (String currentInterface :origInterfaceList) {
159 if (updatedInterface.equals(currentInterface)) {
164 return newInterfaceList;