2 * Copyright (c) 2016, 2017 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
9 package org.opendaylight.netvirt.vpnmanager;
11 import com.google.common.base.Optional;
12 import java.util.ArrayList;
13 import java.util.List;
14 import javax.annotation.PostConstruct;
15 import javax.inject.Inject;
16 import javax.inject.Singleton;
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInstances;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstanceKey;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.NetworkAttributes.NetworkType;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.Subnetmaps;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.subnetmaps.Subnetmap;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.networks.rev150712.networks.attributes.networks.Network;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 public class SubnetmapChangeListener extends AsyncDataTreeChangeListenerBase<Subnetmap, SubnetmapChangeListener> {
34 private static final Logger LOG = LoggerFactory.getLogger(SubnetmapChangeListener.class);
35 private final DataBroker dataBroker;
36 private final VpnSubnetRouteHandler vpnSubnetRouteHandler;
39 public SubnetmapChangeListener(final DataBroker dataBroker, final VpnSubnetRouteHandler vpnSubnetRouteHandler) {
40 super(Subnetmap.class, SubnetmapChangeListener.class);
41 this.dataBroker = dataBroker;
42 this.vpnSubnetRouteHandler = vpnSubnetRouteHandler;
47 LOG.info("{} start", getClass().getSimpleName());
48 registerListener(dataBroker);
52 protected InstanceIdentifier<Subnetmap> getWildCardPath() {
53 return InstanceIdentifier.create(Subnetmaps.class).child(Subnetmap.class);
56 // TODO Clean up the exception handling
57 @SuppressWarnings("checkstyle:IllegalCatch")
58 private void registerListener(final DataBroker db) {
60 registerListener(LogicalDatastoreType.CONFIGURATION, db);
61 } catch (final Exception e) {
62 LOG.error("VPNManager subnetMap config DataChange listener registration fail!", e);
63 throw new IllegalStateException("VPNManager subnetMap config DataChange listener registration failed.", e);
68 protected void add(InstanceIdentifier<Subnetmap> identifier, Subnetmap subnetmap) {
69 LOG.trace("add:SubnetmapChangeListener add subnetmap method - key: {}, value: {}", identifier, subnetmap);
70 Uuid subnetId = subnetmap.getId();
71 Uuid vpnId = subnetmap.getVpnId();
73 if (subnetmap.getVpnId() != null) {
74 // SubnetRoute for ExternalSubnets is handled in ExternalSubnetVpnInstanceListener.
75 // Here we must handle only InternalVpnSubnetRoute and BGPVPNBasedSubnetRoute
76 Network network = VpnUtil.getNeutronNetwork(dataBroker, subnetmap.getNetworkId());
77 if (network == null) {
78 LOG.info("update: vpnId {}, networkId: {}, subnetId: {}: network was not found",
79 vpnId, subnetmap.getNetworkId(), subnetId);
82 boolean isExtNetwork = VpnUtil.getIsExternal(VpnUtil.getNeutronNetwork(dataBroker,
83 subnetmap.getNetworkId()));
84 // SubnetRoute for ExternalNetwork is not impacting for internetvpn
88 boolean isBgpVpn = !vpnId.equals(subnetmap.getRouterId());
89 String elanInstanceName = subnetmap.getNetworkId().getValue();
90 Long elanTag = getElanTag(elanInstanceName);
91 if (elanTag.equals(0L)) {
92 LOG.error("add:Unable to fetch elantag from ElanInstance {} and hence not proceeding with "
93 + "subnetmapListener add for subnet {}", elanInstanceName, subnetId.getValue());
96 if (subnetmap.getNetworkType().equals(NetworkType.VLAN)) {
97 VpnUtil.addRouterPortToElanDpnListForVlaninAllDpn(vpnId.getValue(), dataBroker);
99 // subnet added to VPN case upon config DS replay after reboot
100 // ports added to subnet upon config DS replay after reboot are handled implicitly by subnetAddedToVpn
101 // in SubnetRouteHandler
102 vpnSubnetRouteHandler.onSubnetAddedToVpn(subnetmap, isBgpVpn , elanTag);
104 if (subnetmap.getInternetVpnId() != null) {
105 boolean isBgpVpn = !subnetmap.getInternetVpnId().equals(subnetmap.getRouterId());
106 String elanInstanceName = subnetmap.getNetworkId().getValue();
107 Long elanTag = getElanTag(elanInstanceName);
108 if (elanTag.equals(0L)) {
109 LOG.error("add:Unable to fetch elantag from ElanInstance {} and hence not proceeding with "
110 + "subnetmapListener add for subnet {}", elanInstanceName, subnetId);
113 // subnet added to VPN case upon config DS replay after reboot
114 // ports added to subnet upon config DS replay after reboot are handled implicitly by subnetAddedToVpn
115 // in SubnetRouteHandler
116 vpnSubnetRouteHandler.onSubnetAddedToVpn(subnetmap, isBgpVpn , elanTag);
121 protected void remove(InstanceIdentifier<Subnetmap> identifier, Subnetmap subnetmap) {
122 LOG.trace("remove:SubnetmapListener remove subnetmap method - key: {}, value: {}", identifier, subnetmap);
126 // TODO Clean up the exception handling
127 @SuppressWarnings("checkstyle:IllegalCatch")
128 protected void update(InstanceIdentifier<Subnetmap> identifier, Subnetmap subnetmapOriginal, Subnetmap
130 LOG.trace("update:SubnetmapListener update subnetmap method - key {}, original {}, update {}", identifier,
131 subnetmapOriginal, subnetmapUpdate);
132 Uuid vpnIdNew = subnetmapUpdate.getVpnId();
133 Uuid vpnIdOld = subnetmapOriginal.getVpnId();
134 Uuid subnetId = subnetmapUpdate.getId();
135 if ((subnetmapUpdate.getNetworkId() == null)
136 && subnetmapOriginal.getNetworkId() == null) {
137 // transition: subnetmap is removed with syncwrite.
138 // wait next write to do the update
139 LOG.error("subnetmap has no network for subnetmap {}", subnetmapUpdate);
142 boolean updateCapableForCreation = false;
143 String elanInstanceName = null;
144 Long elanTag = Long.valueOf(0L);
145 if (subnetmapUpdate.getNetworkId() != null) {
146 updateCapableForCreation = true;
147 elanInstanceName = subnetmapUpdate.getNetworkId().getValue();
148 elanTag = getElanTag(elanInstanceName);
149 if (elanTag.equals(0L)) {
150 LOG.error("update:Unable to fetch elantag from ElanInstance {} and "
151 + "hence not proceeding with "
152 + "subnetmapListener update for subnet {}", elanInstanceName, subnetId.getValue());
156 // SubnetRoute for ExternalSubnets is handled in ExternalSubnetVpnInstanceListener.
157 // Here we must handle only InternalVpnSubnetRoute and BGPVPNBasedSubnetRoute
158 Network network = VpnUtil.getNeutronNetwork(dataBroker, subnetmapUpdate.getNetworkId());
159 if (VpnUtil.getIsExternal(network)) {
163 Uuid vpnIdInternetNew = subnetmapUpdate.getInternetVpnId();
164 Uuid vpnIdInternetOld = subnetmapOriginal.getInternetVpnId();
165 boolean returnValue1 = updateSubnetmapOpDataEntry(vpnIdInternetOld, vpnIdInternetNew,
166 subnetmapUpdate, subnetmapOriginal, elanTag, null, updateCapableForCreation);
167 boolean returnValue2 = updateSubnetmapOpDataEntry(vpnIdOld, vpnIdNew,
168 subnetmapUpdate, subnetmapOriginal, elanTag, elanInstanceName, updateCapableForCreation);
169 if (!returnValue2 || !returnValue1) {
172 // port added/removed to/from subnet case
173 List<Uuid> oldPortList;
174 List<Uuid> newPortList;
175 newPortList = subnetmapUpdate.getPortList() != null ? subnetmapUpdate.getPortList() : new ArrayList<>();
176 oldPortList = subnetmapOriginal.getPortList() != null ? subnetmapOriginal.getPortList() : new ArrayList<>();
177 if (newPortList.size() == oldPortList.size()) {
180 if (newPortList.size() > oldPortList.size()) {
181 for (Uuid portId : newPortList) {
182 if (! oldPortList.contains(portId)) {
183 vpnSubnetRouteHandler.onPortAddedToSubnet(subnetmapUpdate, portId);
188 for (Uuid portId : oldPortList) {
189 if (! newPortList.contains(portId)) {
190 vpnSubnetRouteHandler.onPortRemovedFromSubnet(subnetmapUpdate, portId);
197 private boolean updateSubnetmapOpDataEntry(Uuid vpnIdOld, Uuid vpnIdNew, Subnetmap subnetmapUpdate,
198 Subnetmap subnetmapOriginal, Long elanTag, String elanInstanceName,
199 boolean updateCapableForCreation) {
200 // subnet added to VPN case
201 if (vpnIdNew != null && vpnIdOld == null && updateCapableForCreation) {
202 if (elanInstanceName != null && subnetmapUpdate.getNetworkType().equals(NetworkType.VLAN)) {
203 VpnUtil.addRouterPortToElanDpnListForVlaninAllDpn(vpnIdNew.getValue(), dataBroker);
205 boolean isBgpVpn = !vpnIdNew.equals(subnetmapUpdate.getRouterId());
209 vpnSubnetRouteHandler.onSubnetAddedToVpn(subnetmapUpdate, true, elanTag);
212 // subnet removed from VPN case
213 if (vpnIdOld != null && vpnIdNew == null) {
214 if (subnetmapOriginal.getNetworkType().equals(NetworkType.VLAN)) {
215 VpnUtil.removeRouterPortFromElanDpnListForVlanInAllDpn(elanInstanceName, subnetmapOriginal
216 .getRouterInterfacePortId().getValue(), vpnIdOld.getValue(), dataBroker);
218 boolean isBgpVpn = vpnIdOld.equals(subnetmapOriginal.getRouterId()) ? false : true;
222 vpnSubnetRouteHandler.onSubnetDeletedFromVpn(subnetmapOriginal, true);
225 // subnet updated in VPN case
226 if (vpnIdOld != null && vpnIdNew != null && (!vpnIdNew.equals(vpnIdOld))) {
227 vpnSubnetRouteHandler.onSubnetUpdatedInVpn(subnetmapUpdate, elanTag);
234 protected SubnetmapChangeListener getDataTreeChangeListener() {
238 // TODO Clean up the exception handling
239 @SuppressWarnings("checkstyle:IllegalCatch")
240 protected long getElanTag(String elanInstanceName) {
241 InstanceIdentifier<ElanInstance> elanIdentifierId = InstanceIdentifier.builder(ElanInstances.class)
242 .child(ElanInstance.class, new ElanInstanceKey(elanInstanceName)).build();
245 Optional<ElanInstance> elanInstance = VpnUtil.read(dataBroker, LogicalDatastoreType
246 .CONFIGURATION, elanIdentifierId);
247 if (elanInstance.isPresent()) {
248 if (elanInstance.get().getElanTag() != null) {
249 elanTag = elanInstance.get().getElanTag();
251 LOG.error("Notification failed because of failure in fetching elanTag for ElanInstance {}",
255 LOG.error("Notification failed because of failure in reading ELANInstance {}", elanInstanceName);
257 } catch (Exception e) {
258 LOG.error("Notification failed because of failure in fetching elanTag for ElanInstance {}",
259 elanInstanceName, e);