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 org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInstances;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstanceKey;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.Subnetmaps;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.subnetmaps.Subnetmap;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.networks.rev150712.networks.attributes.networks.Network;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 public class SubnetmapChangeListener extends AsyncDataTreeChangeListenerBase<Subnetmap, SubnetmapChangeListener> {
30 private static final Logger LOG = LoggerFactory.getLogger(SubnetmapChangeListener.class);
31 private final DataBroker dataBroker;
32 private final VpnSubnetRouteHandler vpnSubnetRouteHandler;
34 public SubnetmapChangeListener(final DataBroker dataBroker, final VpnSubnetRouteHandler vpnSubnetRouteHandler) {
35 super(Subnetmap.class, SubnetmapChangeListener.class);
36 this.dataBroker = dataBroker;
37 this.vpnSubnetRouteHandler = vpnSubnetRouteHandler;
41 LOG.info("{} start", getClass().getSimpleName());
42 registerListener(dataBroker);
46 protected InstanceIdentifier<Subnetmap> getWildCardPath() {
47 return InstanceIdentifier.create(Subnetmaps.class).child(Subnetmap.class);
50 // TODO Clean up the exception handling
51 @SuppressWarnings("checkstyle:IllegalCatch")
52 private void registerListener(final DataBroker db) {
54 registerListener(LogicalDatastoreType.CONFIGURATION, db);
55 } catch (final Exception e) {
56 LOG.error("VPNManager subnetMap config DataChange listener registration fail!", e);
57 throw new IllegalStateException("VPNManager subnetMap config DataChange listener registration failed.", e);
62 protected void add(InstanceIdentifier<Subnetmap> identifier, Subnetmap subnetmap) {
63 LOG.trace("add:SubnetmapChangeListener add subnetmap method - key: {}, value: {}", identifier, subnetmap);
64 Uuid subnetId = subnetmap.getId();
65 Uuid vpnId = subnetmap.getVpnId();
66 if (subnetmap.getVpnId() != null) {
67 // SubnetRoute for ExternalSubnets is handled in ExternalSubnetVpnInstanceListener.
68 // Here we must handle only InternalVpnSubnetRoute and BGPVPNBasedSubnetRoute
69 Network network = VpnUtil.getNeutronNetwork(dataBroker, subnetmap.getNetworkId());
70 if (network == null) {
71 LOG.info("update: vpnId {}, networkId: {}, subnetId: {}: network was not found",
72 vpnId.getValue(), subnetmap.getNetworkId().getValue(), subnetId.getValue());
75 if (VpnUtil.getIsExternal(network)) {
78 boolean isBgpVpn = !vpnId.equals(subnetmap.getRouterId());
79 String elanInstanceName = subnetmap.getNetworkId().getValue();
80 Long elanTag = getElanTag(elanInstanceName);
81 if (elanTag.equals(0L)) {
82 LOG.error("add:Unable to fetch elantag from ElanInstance {} and hence not proceeding with "
83 + "subnetmapListener add for subnet {}", elanInstanceName, subnetId.getValue());
86 // subnet added to VPN case upon config DS replay after reboot
87 // ports added to subnet upon config DS replay after reboot are handled implicitly by subnetAddedToVpn
88 // in SubnetRouteHandler
89 vpnSubnetRouteHandler.onSubnetAddedToVpn(subnetmap, isBgpVpn , elanTag);
94 protected void remove(InstanceIdentifier<Subnetmap> identifier, Subnetmap subnetmap) {
95 LOG.trace("remove:SubnetmapListener remove subnetmap method - key: {}, value: {}", identifier, subnetmap);
99 // TODO Clean up the exception handling
100 @SuppressWarnings("checkstyle:IllegalCatch")
101 protected void update(InstanceIdentifier<Subnetmap> identifier, Subnetmap subnetmapOriginal, Subnetmap
103 LOG.trace("update:SubnetmapListener update subnetmap method - key {}, original {}, update {}", identifier,
104 subnetmapOriginal, subnetmapUpdate);
105 Uuid vpnIdNew = subnetmapUpdate.getVpnId();
106 Uuid vpnIdOld = subnetmapOriginal.getVpnId();
107 Uuid subnetId = subnetmapUpdate.getId();
108 String elanInstanceName = subnetmapUpdate.getNetworkId().getValue();
109 // SubnetRoute for ExternalSubnets is handled in ExternalSubnetVpnInstanceListener.
110 // Here we must handle only InternalVpnSubnetRoute and BGPVPNBasedSubnetRoute
111 Network network = VpnUtil.getNeutronNetwork(dataBroker, subnetmapUpdate.getNetworkId());
112 if (network == null) {
113 LOG.info("update: vpnIdNew: {}, vpnIdOld: {}, networkId: {}, subnetId: {}: network was not found",
114 vpnIdNew.getValue(), vpnIdOld.getValue(), elanInstanceName, subnetId.getValue());
117 if (VpnUtil.getIsExternal(network)) {
120 Long elanTag = getElanTag(elanInstanceName);
121 if (elanTag.equals(0L)) {
122 LOG.error("update:Unable to fetch elantag from ElanInstance {} and hence not proceeding with "
123 + "subnetmapListener update for subnet {}", elanInstanceName, subnetId.getValue());
126 // subnet added to VPN case
127 if (vpnIdNew != null && vpnIdOld == null) {
128 boolean isBgpVpn = !vpnIdNew.equals(subnetmapUpdate.getRouterId());
132 vpnSubnetRouteHandler.onSubnetAddedToVpn(subnetmapUpdate, true, elanTag);
135 // subnet removed from VPN case
136 if (vpnIdOld != null && vpnIdNew == null) {
137 Boolean isBgpVpn = vpnIdOld.equals(subnetmapOriginal.getRouterId()) ? false : true;
141 vpnSubnetRouteHandler.onSubnetDeletedFromVpn(subnetmapOriginal, true);
144 // subnet updated in VPN case
145 if (vpnIdOld != null && vpnIdNew != null && (!vpnIdNew.equals(vpnIdOld))) {
146 vpnSubnetRouteHandler.onSubnetUpdatedInVpn(subnetmapUpdate, elanTag);
149 // port added/removed to/from subnet case
150 List<Uuid> oldPortList;
151 List<Uuid> newPortList;
152 newPortList = subnetmapUpdate.getPortList() != null ? subnetmapUpdate.getPortList() : new ArrayList<>();
153 oldPortList = subnetmapOriginal.getPortList() != null ? subnetmapOriginal.getPortList() : new ArrayList<>();
154 if (newPortList.size() == oldPortList.size()) {
157 if (newPortList.size() > oldPortList.size()) {
158 for (Uuid portId : newPortList) {
159 if (! oldPortList.contains(portId)) {
160 vpnSubnetRouteHandler.onPortAddedToSubnet(subnetmapUpdate, portId);
165 for (Uuid portId : oldPortList) {
166 if (! newPortList.contains(portId)) {
167 vpnSubnetRouteHandler.onPortRemovedFromSubnet(subnetmapUpdate, portId);
175 protected SubnetmapChangeListener getDataTreeChangeListener() {
179 // TODO Clean up the exception handling
180 @SuppressWarnings("checkstyle:IllegalCatch")
181 protected long getElanTag(String elanInstanceName) {
182 InstanceIdentifier<ElanInstance> elanIdentifierId = InstanceIdentifier.builder(ElanInstances.class)
183 .child(ElanInstance.class, new ElanInstanceKey(elanInstanceName)).build();
186 Optional<ElanInstance> elanInstance = VpnUtil.read(dataBroker, LogicalDatastoreType
187 .CONFIGURATION, elanIdentifierId);
188 if (elanInstance.isPresent()) {
189 if (elanInstance.get().getElanTag() != null) {
190 elanTag = elanInstance.get().getElanTag();
192 LOG.error("Notification failed because of failure in fetching elanTag for ElanInstance {}",
196 LOG.error("Notification failed because of failure in reading ELANInstance {}", elanInstanceName);
198 } catch (Exception e) {
199 LOG.error("Notification failed because of failure in fetching elanTag for ElanInstance {}",
200 elanInstanceName, e);