2 * Copyright (c) 2016 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
8 package org.opendaylight.vpnservice.itm.listeners;
10 import com.google.common.base.Optional;
11 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
12 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
13 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
16 import org.opendaylight.vpnservice.datastoreutils.AsyncDataTreeChangeListenerBase;
17 import org.opendaylight.vpnservice.datastoreutils.DataStoreJobCoordinator;
18 import org.opendaylight.vpnservice.interfacemgr.interfaces.IInterfaceManager;
19 import org.opendaylight.vpnservice.itm.confighelpers.HwVtep;
20 import org.opendaylight.vpnservice.itm.confighelpers.ItmMonitorToggleWorker;
21 import org.opendaylight.vpnservice.itm.confighelpers.ItmTepAddWorker;
22 import org.opendaylight.vpnservice.itm.globals.ITMConstants;
23 import org.opendaylight.vpnservice.itm.impl.ItmUtils;
24 import org.opendaylight.vpnservice.mdsalutil.AbstractDataChangeListener;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceBuilder;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfTunnel;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfTunnelBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.config.rev151102.*;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.ExternalTunnelList;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.TunnelList;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.external.tunnel.list.ExternalTunnel;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.tunnel.list.InternalTunnel;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rev150701.TransportZones;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rev150701.transport.zones.TransportZone;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rev150701.transport.zones.transport.zone.Subnets;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rev150701.transport.zones.transport.zone.subnets.DeviceVteps;
39 import org.opendaylight.yangtools.concepts.ListenerRegistration;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 import java.util.ArrayList;
46 import java.util.List;
48 public class TunnelMonitorChangeListener extends AsyncDataTreeChangeListenerBase<TunnelMonitorEnabled, TunnelMonitorChangeListener>
49 implements AutoCloseable {
50 private static final Logger LOG = LoggerFactory.getLogger(TunnelMonitorChangeListener.class);
51 private final DataBroker broker;
52 // private final IInterfaceManager interfaceManager;
54 public TunnelMonitorChangeListener(final DataBroker db) {
55 super(TunnelMonitorEnabled.class, TunnelMonitorChangeListener.class);
57 // interfaceManager = ifManager;
58 // registerListener(db);
61 /* private void registerListener(final DataBroker db) {
63 TunnelMonitorChangeListener monitorEnabledChangeListener = new TunnelMonitorChangeListener();
64 monitorEnabledListenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
65 monitorEnabledChangeListener.getWildCardPath(), monitorEnabledChangeListener, DataChangeScope.SUBTREE);
66 } catch (final Exception e) {
67 LOG.error("ITM Monitor Interfaces DataChange listener registration fail!", e);
68 throw new IllegalStateException("ITM Monitor registration Listener failed.", e);
72 public void close() throws Exception {
73 /* if (monitorEnabledListenerRegistration != null) {
75 monitorEnabledListenerRegistration.close();
76 } catch (final Exception e) {
77 LOG.error("Error when cleaning up DataChangeListener.", e);
79 monitorEnabledListenerRegistration = null;
82 if (monitorIntervalListenerRegistration != null) {
84 monitorIntervalListenerRegistration.close();
85 } catch (final Exception e) {
86 LOG.error("Error when cleaning up DataChangeListener.", e);
88 monitorIntervalListenerRegistration = null;
91 LOG.info("Tunnel Monitor listeners Closed");
94 @Override protected InstanceIdentifier<TunnelMonitorEnabled> getWildCardPath() {
95 return InstanceIdentifier.create(TunnelMonitorEnabled.class);
99 protected void remove(InstanceIdentifier<TunnelMonitorEnabled> key, TunnelMonitorEnabled dataObjectModification) {
100 List<HwVtep> hwVteps = new ArrayList<HwVtep>();
101 Boolean hwVtepsExist = false;
102 DataStoreJobCoordinator coordinator = DataStoreJobCoordinator.getInstance();
103 InstanceIdentifier<TransportZones> path = InstanceIdentifier.builder(TransportZones.class).build();
104 Optional<TransportZones> tZonesOptional = ItmUtils.read(LogicalDatastoreType.CONFIGURATION, path, broker);
105 if (tZonesOptional.isPresent()) {
106 TransportZones tZones = tZonesOptional.get();
107 for (TransportZone tzone : tZones.getTransportZone()) {
108 hwVtepsExist = false;
109 hwVteps = new ArrayList<HwVtep>();
110 if (tzone.getSubnets() != null && !tzone.getSubnets().isEmpty()) {
111 for (Subnets sub : tzone.getSubnets()) {
112 if (sub.getDeviceVteps() != null && !sub.getDeviceVteps().isEmpty()) {
114 for (DeviceVteps deviceVtep : sub.getDeviceVteps()) {
115 HwVtep hwVtep = ItmUtils.createHwVtepObject(deviceVtep.getTopologyId(), deviceVtep.getNodeId(),
116 deviceVtep.getIpAddress(), sub.getPrefix(), sub.getGatewayIp(), sub.getVlanId(),
117 tzone.getTunnelType(), tzone);
123 LOG.debug("Remove:Calling TunnelMonitorToggleWorker with tzone = {} and {}",tzone.getZoneName(),dataObjectModification.isEnabled());
124 ItmMonitorToggleWorker toggleWorker = new ItmMonitorToggleWorker(hwVteps, tzone.getZoneName(),
125 false, broker, hwVtepsExist);
126 coordinator.enqueueJob(tzone.getZoneName(), toggleWorker);
132 @Override protected void update(InstanceIdentifier<TunnelMonitorEnabled> key,
133 TunnelMonitorEnabled dataObjectModificationBefore,
134 TunnelMonitorEnabled dataObjectModificationAfter) {
135 LOG.debug("update TunnelMonitorChangeListener called with {}",dataObjectModificationAfter.isEnabled());
136 List<HwVtep> hwVteps = new ArrayList<HwVtep>();
137 Boolean hwVtepsExist = false;
138 DataStoreJobCoordinator coordinator = DataStoreJobCoordinator.getInstance();
139 InstanceIdentifier<TransportZones> path = InstanceIdentifier.builder(TransportZones.class).build();
140 Optional<TransportZones> tZonesOptional = ItmUtils.read(LogicalDatastoreType.CONFIGURATION, path, broker);
141 if (tZonesOptional.isPresent()) {
142 TransportZones tZones = tZonesOptional.get();
143 for (TransportZone tzone : tZones.getTransportZone()) {
144 hwVtepsExist = false;
145 hwVteps = new ArrayList<HwVtep>();
146 if (tzone.getSubnets() != null && !tzone.getSubnets().isEmpty()) {
147 for (Subnets sub : tzone.getSubnets()) {
148 if (sub.getDeviceVteps() != null && !sub.getDeviceVteps().isEmpty()) {
149 hwVtepsExist = true;//gets set to true only if this particular tzone has
150 LOG.debug("Update:Calling TunnelMonitorToggleWorker with tzone = {} and hwtepExist",tzone.getZoneName());
151 for (DeviceVteps deviceVtep : sub.getDeviceVteps()) {
152 HwVtep hwVtep = ItmUtils.createHwVtepObject(deviceVtep.getTopologyId(), deviceVtep.getNodeId(),
153 deviceVtep.getIpAddress(), sub.getPrefix(), sub.getGatewayIp(), sub.getVlanId(),
154 tzone.getTunnelType(), tzone);
160 LOG.debug("Update:Calling TunnelMonitorToggleWorker with tzone = {} and {}",tzone.getZoneName(),dataObjectModificationAfter.isEnabled());
161 ItmMonitorToggleWorker toggleWorker = new ItmMonitorToggleWorker(hwVteps, tzone.getZoneName(),
162 dataObjectModificationAfter.isEnabled(), broker, hwVtepsExist);
163 coordinator.enqueueJob(tzone.getZoneName(), toggleWorker);
169 protected void add(InstanceIdentifier<TunnelMonitorEnabled> key, TunnelMonitorEnabled dataObjectModification) {
170 LOG.debug("add TunnelMonitorChangeListener called with {}",dataObjectModification.isEnabled());
171 List<HwVtep> hwVteps = new ArrayList<HwVtep>();
172 Boolean hwVtepsExist = false;
173 DataStoreJobCoordinator coordinator = DataStoreJobCoordinator.getInstance();
174 InstanceIdentifier<TransportZones> path = InstanceIdentifier.builder(TransportZones.class).build();
175 Optional<TransportZones> tZonesOptional = ItmUtils.read(LogicalDatastoreType.CONFIGURATION, path, broker);
176 if (tZonesOptional.isPresent()) {
177 TransportZones tZones = tZonesOptional.get();
178 for (TransportZone tzone : tZones.getTransportZone()) {
179 hwVtepsExist = false;
180 hwVteps = new ArrayList<HwVtep>();
181 if (tzone.getSubnets() != null && !tzone.getSubnets().isEmpty()) {
182 for (Subnets sub : tzone.getSubnets()) {
183 if (sub.getDeviceVteps() != null && !sub.getDeviceVteps().isEmpty()) {
185 for (DeviceVteps deviceVtep : sub.getDeviceVteps()) {
186 HwVtep hwVtep = ItmUtils.createHwVtepObject(deviceVtep.getTopologyId(), deviceVtep.getNodeId(),
187 deviceVtep.getIpAddress(), sub.getPrefix(), sub.getGatewayIp(), sub.getVlanId(),
188 tzone.getTunnelType(), tzone);
194 LOG.debug("Add:Calling TunnelMonitorToggleWorker with tzone = {} and {}",tzone.getZoneName(),dataObjectModification.isEnabled());
195 ItmMonitorToggleWorker toggleWorker = new ItmMonitorToggleWorker(hwVteps, tzone.getZoneName(),
196 dataObjectModification.isEnabled(), broker, hwVtepsExist);
197 coordinator.enqueueJob(tzone.getZoneName(), toggleWorker);
202 @Override protected TunnelMonitorChangeListener getDataTreeChangeListener() {
203 return TunnelMonitorChangeListener.this;