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.Subnetmaps;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.subnetmaps.Subnetmap;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.networks.rev150712.networks.attributes.networks.Network;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 public class SubnetmapChangeListener extends AsyncDataTreeChangeListenerBase<Subnetmap, SubnetmapChangeListener> {
33 private static final Logger LOG = LoggerFactory.getLogger(SubnetmapChangeListener.class);
34 private final DataBroker dataBroker;
35 private final VpnSubnetRouteHandler vpnSubnetRouteHandler;
38 public SubnetmapChangeListener(final DataBroker dataBroker, final VpnSubnetRouteHandler vpnSubnetRouteHandler) {
39 super(Subnetmap.class, SubnetmapChangeListener.class);
40 this.dataBroker = dataBroker;
41 this.vpnSubnetRouteHandler = vpnSubnetRouteHandler;
46 LOG.info("{} start", getClass().getSimpleName());
47 registerListener(dataBroker);
51 protected InstanceIdentifier<Subnetmap> getWildCardPath() {
52 return InstanceIdentifier.create(Subnetmaps.class).child(Subnetmap.class);
55 // TODO Clean up the exception handling
56 @SuppressWarnings("checkstyle:IllegalCatch")
57 private void registerListener(final DataBroker db) {
59 registerListener(LogicalDatastoreType.CONFIGURATION, db);
60 } catch (final Exception e) {
61 LOG.error("VPNManager subnetMap config DataChange listener registration fail!", e);
62 throw new IllegalStateException("VPNManager subnetMap config DataChange listener registration failed.", e);
67 protected void add(InstanceIdentifier<Subnetmap> identifier, Subnetmap subnetmap) {
68 LOG.trace("add:SubnetmapChangeListener add subnetmap method - key: {}, value: {}", identifier, subnetmap);
69 Uuid subnetId = subnetmap.getId();
70 Uuid vpnId = subnetmap.getVpnId();
72 if (subnetmap.getVpnId() != null) {
73 // SubnetRoute for ExternalSubnets is handled in ExternalSubnetVpnInstanceListener.
74 // Here we must handle only InternalVpnSubnetRoute and BGPVPNBasedSubnetRoute
75 Network network = VpnUtil.getNeutronNetwork(dataBroker, subnetmap.getNetworkId());
76 if (network == null) {
77 LOG.info("update: vpnId {}, networkId: {}, subnetId: {}: network was not found",
78 vpnId, subnetmap.getNetworkId(), subnetId);
81 boolean isExtNetwork = VpnUtil.getIsExternal(VpnUtil.getNeutronNetwork(dataBroker,
82 subnetmap.getNetworkId()));
83 // SubnetRoute for ExternalNetwork is not impacting for internetvpn
87 boolean isBgpVpn = !vpnId.equals(subnetmap.getRouterId());
88 String elanInstanceName = subnetmap.getNetworkId().getValue();
89 Long elanTag = getElanTag(elanInstanceName);
90 if (elanTag.equals(0L)) {
91 LOG.error("add:Unable to fetch elantag from ElanInstance {} and hence not proceeding with "
92 + "subnetmapListener add for subnet {}", elanInstanceName, subnetId.getValue());
95 // subnet added to VPN case upon config DS replay after reboot
96 // ports added to subnet upon config DS replay after reboot are handled implicitly by subnetAddedToVpn
97 // in SubnetRouteHandler
98 vpnSubnetRouteHandler.onSubnetAddedToVpn(subnetmap, isBgpVpn , elanTag);
100 if (subnetmap.getInternetVpnId() != null) {
101 boolean isBgpVpn = !subnetmap.getInternetVpnId().equals(subnetmap.getRouterId());
102 String elanInstanceName = subnetmap.getNetworkId().getValue();
103 Long elanTag = getElanTag(elanInstanceName);
104 if (elanTag.equals(0L)) {
105 LOG.error("add:Unable to fetch elantag from ElanInstance {} and hence not proceeding with "
106 + "subnetmapListener add for subnet {}", elanInstanceName, subnetId);
109 // subnet added to VPN case upon config DS replay after reboot
110 // ports added to subnet upon config DS replay after reboot are handled implicitly by subnetAddedToVpn
111 // in SubnetRouteHandler
112 vpnSubnetRouteHandler.onSubnetAddedToVpn(subnetmap, isBgpVpn , elanTag);
117 protected void remove(InstanceIdentifier<Subnetmap> identifier, Subnetmap subnetmap) {
118 LOG.trace("remove:SubnetmapListener remove subnetmap method - key: {}, value: {}", identifier, subnetmap);
122 // TODO Clean up the exception handling
123 @SuppressWarnings("checkstyle:IllegalCatch")
124 protected void update(InstanceIdentifier<Subnetmap> identifier, Subnetmap subnetmapOriginal, Subnetmap
126 LOG.trace("update:SubnetmapListener update subnetmap method - key {}, original {}, update {}", identifier,
127 subnetmapOriginal, subnetmapUpdate);
128 Uuid vpnIdNew = subnetmapUpdate.getVpnId();
129 Uuid vpnIdOld = subnetmapOriginal.getVpnId();
130 Uuid subnetId = subnetmapUpdate.getId();
131 if ((subnetmapUpdate.getNetworkId() == null)
132 && subnetmapOriginal.getNetworkId() == null) {
133 // transition: subnetmap is removed with syncwrite.
134 // wait next write to do the update
135 LOG.error("subnetmap has no network for subnetmap {}", subnetmapUpdate);
138 boolean updateCapableForCreation = false;
139 String elanInstanceName = null;
140 Long elanTag = Long.valueOf(0L);
141 if (subnetmapUpdate.getNetworkId() != null) {
142 updateCapableForCreation = true;
143 elanInstanceName = subnetmapUpdate.getNetworkId().getValue();
144 elanTag = getElanTag(elanInstanceName);
145 if (elanTag.equals(0L)) {
146 LOG.error("update:Unable to fetch elantag from ElanInstance {} and "
147 + "hence not proceeding with "
148 + "subnetmapListener update for subnet {}", elanInstanceName, subnetId.getValue());
152 // SubnetRoute for ExternalSubnets is handled in ExternalSubnetVpnInstanceListener.
153 // Here we must handle only InternalVpnSubnetRoute and BGPVPNBasedSubnetRoute
154 Network network = VpnUtil.getNeutronNetwork(dataBroker, subnetmapUpdate.getNetworkId());
155 if (VpnUtil.getIsExternal(network)) {
159 Uuid vpnIdInternetNew = subnetmapUpdate.getInternetVpnId();
160 Uuid vpnIdInternetOld = subnetmapOriginal.getInternetVpnId();
161 boolean returnValue1 = updateSubnetmapOpDataEntry(vpnIdInternetOld, vpnIdInternetNew,
162 subnetmapUpdate, subnetmapOriginal, elanTag, updateCapableForCreation);
163 boolean returnValue2 = updateSubnetmapOpDataEntry(vpnIdOld, vpnIdNew,
164 subnetmapUpdate, subnetmapOriginal, elanTag, updateCapableForCreation);
165 if (!returnValue2 || !returnValue1) {
168 // port added/removed to/from subnet case
169 List<Uuid> oldPortList;
170 List<Uuid> newPortList;
171 newPortList = subnetmapUpdate.getPortList() != null ? subnetmapUpdate.getPortList() : new ArrayList<>();
172 oldPortList = subnetmapOriginal.getPortList() != null ? subnetmapOriginal.getPortList() : new ArrayList<>();
173 if (newPortList.size() == oldPortList.size()) {
176 if (newPortList.size() > oldPortList.size()) {
177 for (Uuid portId : newPortList) {
178 if (! oldPortList.contains(portId)) {
179 vpnSubnetRouteHandler.onPortAddedToSubnet(subnetmapUpdate, portId);
184 for (Uuid portId : oldPortList) {
185 if (! newPortList.contains(portId)) {
186 vpnSubnetRouteHandler.onPortRemovedFromSubnet(subnetmapUpdate, portId);
193 private boolean updateSubnetmapOpDataEntry(Uuid vpnIdOld, Uuid vpnIdNew, Subnetmap subnetmapUpdate,
194 Subnetmap subnetmapOriginal, Long elanTag, boolean updateCapableForCreation) {
195 // subnet added to VPN case
196 if (vpnIdNew != null && vpnIdOld == null && updateCapableForCreation) {
197 boolean isBgpVpn = !vpnIdNew.equals(subnetmapUpdate.getRouterId());
201 vpnSubnetRouteHandler.onSubnetAddedToVpn(subnetmapUpdate, true, elanTag);
204 // subnet removed from VPN case
205 if (vpnIdOld != null && vpnIdNew == null) {
206 boolean isBgpVpn = vpnIdOld.equals(subnetmapOriginal.getRouterId()) ? false : true;
210 vpnSubnetRouteHandler.onSubnetDeletedFromVpn(subnetmapOriginal, true);
213 // subnet updated in VPN case
214 if (vpnIdOld != null && vpnIdNew != null && (!vpnIdNew.equals(vpnIdOld))) {
215 vpnSubnetRouteHandler.onSubnetUpdatedInVpn(subnetmapUpdate, elanTag);
222 protected SubnetmapChangeListener getDataTreeChangeListener() {
226 // TODO Clean up the exception handling
227 @SuppressWarnings("checkstyle:IllegalCatch")
228 protected long getElanTag(String elanInstanceName) {
229 InstanceIdentifier<ElanInstance> elanIdentifierId = InstanceIdentifier.builder(ElanInstances.class)
230 .child(ElanInstance.class, new ElanInstanceKey(elanInstanceName)).build();
233 Optional<ElanInstance> elanInstance = VpnUtil.read(dataBroker, LogicalDatastoreType
234 .CONFIGURATION, elanIdentifierId);
235 if (elanInstance.isPresent()) {
236 if (elanInstance.get().getElanTag() != null) {
237 elanTag = elanInstance.get().getElanTag();
239 LOG.error("Notification failed because of failure in fetching elanTag for ElanInstance {}",
243 LOG.error("Notification failed because of failure in reading ELANInstance {}", elanInstanceName);
245 } catch (Exception e) {
246 LOG.error("Notification failed because of failure in fetching elanTag for ElanInstance {}",
247 elanInstanceName, e);