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.genius.itm.listeners;
10 import com.google.common.base.Optional;
11 import javax.annotation.PostConstruct;
12 import javax.annotation.PreDestroy;
13 import javax.inject.Inject;
14 import javax.inject.Singleton;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
18 import org.opendaylight.genius.datastoreutils.DataStoreJobCoordinator;
19 import org.opendaylight.genius.itm.confighelpers.ItmMonitorToggleWorker;
20 import org.opendaylight.genius.itm.globals.ITMConstants;
21 import org.opendaylight.genius.itm.impl.ItmUtils;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelMonitoringTypeBase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.config.rev160406.TunnelMonitorParams;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.TransportZones;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.TransportZone;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 public class TunnelMonitorChangeListener extends AsyncDataTreeChangeListenerBase<TunnelMonitorParams, TunnelMonitorChangeListener>
32 implements AutoCloseable {
33 private static final Logger LOG = LoggerFactory.getLogger(TunnelMonitorChangeListener.class);
34 private final DataBroker broker;
35 // private final IInterfaceManager interfaceManager;
38 public TunnelMonitorChangeListener(final DataBroker dataBroker) {
39 super(TunnelMonitorParams.class, TunnelMonitorChangeListener.class);
40 this.broker = dataBroker;
41 // interfaceManager = ifManager;
42 // registerListener(db);
46 public void start() throws Exception {
47 registerListener(LogicalDatastoreType.CONFIGURATION, this.broker);
48 LOG.info("Tunnel Monitor listeners Started");
51 /* private void registerListener(final DataBroker db) {
53 TunnelMonitorChangeListener monitorEnabledChangeListener = new TunnelMonitorChangeListener();
54 monitorEnabledListenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
55 monitorEnabledChangeListener.getWildCardPath(), monitorEnabledChangeListener, DataChangeScope.SUBTREE);
56 } catch (final Exception e) {
57 LOG.error("ITM Monitor Interfaces DataChange listener registration fail!", e);
58 throw new IllegalStateException("ITM Monitor registration Listener failed.", e);
65 /* if (monitorEnabledListenerRegistration != null) {
67 monitorEnabledListenerRegistration.close();
68 } catch (final Exception e) {
69 LOG.error("Error when cleaning up DataChangeListener.", e);
71 monitorEnabledListenerRegistration = null;
74 if (monitorIntervalListenerRegistration != null) {
76 monitorIntervalListenerRegistration.close();
77 } catch (final Exception e) {
78 LOG.error("Error when cleaning up DataChangeListener.", e);
80 monitorIntervalListenerRegistration = null;
83 LOG.info("Tunnel Monitor listeners Closed");
86 @Override protected InstanceIdentifier<TunnelMonitorParams> getWildCardPath() {
87 return InstanceIdentifier.create(TunnelMonitorParams.class);
91 protected void remove(InstanceIdentifier<TunnelMonitorParams> key, TunnelMonitorParams dataObjectModification) {
92 DataStoreJobCoordinator coordinator = DataStoreJobCoordinator.getInstance();
93 InstanceIdentifier<TransportZones> path = InstanceIdentifier.builder(TransportZones.class).build();
94 Optional<TransportZones> tZonesOptional = ItmUtils.read(LogicalDatastoreType.CONFIGURATION, path, broker);
95 Class<? extends TunnelMonitoringTypeBase> monitorProtocol = dataObjectModification.getMonitorProtocol();
96 if (monitorProtocol == null) {
97 monitorProtocol = ITMConstants.DEFAULT_MONITOR_PROTOCOL;
99 if (tZonesOptional.isPresent()) {
100 TransportZones tZones = tZonesOptional.get();
101 for (TransportZone tzone : tZones.getTransportZone()) {
102 LOG.debug("Remove - TunnelMonitorToggleWorker with tzone = {}, Enable = {}, MonitorProtocol = {}",tzone.getZoneName(),dataObjectModification.isEnabled(), monitorProtocol);
103 ItmMonitorToggleWorker toggleWorker = new ItmMonitorToggleWorker(tzone.getZoneName(),
104 false,monitorProtocol, broker);
105 coordinator.enqueueJob(tzone.getZoneName(), toggleWorker);
111 @Override protected void update(InstanceIdentifier<TunnelMonitorParams> key,
112 TunnelMonitorParams dataObjectModificationBefore,
113 TunnelMonitorParams dataObjectModificationAfter) {
114 LOG.debug("update TunnelMonitorChangeListener called with {}",dataObjectModificationAfter.isEnabled());
115 Class<? extends TunnelMonitoringTypeBase> monitorProtocol_before = dataObjectModificationBefore.getMonitorProtocol();
116 Class<? extends TunnelMonitoringTypeBase> monitorProtocol_after = dataObjectModificationAfter.getMonitorProtocol();
117 Class<? extends TunnelMonitoringTypeBase> monitorProtocol = ITMConstants.DEFAULT_MONITOR_PROTOCOL;
118 DataStoreJobCoordinator coordinator = DataStoreJobCoordinator.getInstance();
119 InstanceIdentifier<TransportZones> path = InstanceIdentifier.builder(TransportZones.class).build();
120 Optional<TransportZones> tZonesOptional = ItmUtils.read(LogicalDatastoreType.CONFIGURATION, path, broker);
121 if(monitorProtocol_after!=null ) {
122 monitorProtocol = dataObjectModificationAfter.getMonitorProtocol();
124 if(monitorProtocol_before!=null && monitorProtocol_after!=null)
126 LOG.debug("TunnelMonitorChangeListener Update : Existing_MonitorProtocol {}, New_MonitorProtocol {} ",monitorProtocol_before.getName(), monitorProtocol_after.getName());
127 if(!monitorProtocol_after.getName().equalsIgnoreCase(monitorProtocol_before.getName())) {
128 LOG.error("Updation of monitor protocol not allowed");
132 if (tZonesOptional.isPresent()) {
133 TransportZones tZones = tZonesOptional.get();
134 for (TransportZone tzone : tZones.getTransportZone()) {
135 LOG.debug("Update - TunnelMonitorToggleWorker with tzone = {}, Enable = {}, MonitorProtocol = {}",tzone.getZoneName(),dataObjectModificationAfter.isEnabled(), monitorProtocol);
136 ItmMonitorToggleWorker toggleWorker = new ItmMonitorToggleWorker(tzone.getZoneName(),
137 dataObjectModificationAfter.isEnabled(), monitorProtocol, broker);
138 coordinator.enqueueJob(tzone.getZoneName(), toggleWorker);
144 protected void add(InstanceIdentifier<TunnelMonitorParams> key, TunnelMonitorParams dataObjectModification) {
145 LOG.debug("Add - TunnelMonitorToggleWorker with Enable = {}, MonitorProtocol = {}",dataObjectModification.isEnabled(), dataObjectModification.getMonitorProtocol());
146 Class<? extends TunnelMonitoringTypeBase> monitorProtocol = dataObjectModification.getMonitorProtocol();
147 DataStoreJobCoordinator coordinator = DataStoreJobCoordinator.getInstance();
148 InstanceIdentifier<TransportZones> path = InstanceIdentifier.builder(TransportZones.class).build();
149 Optional<TransportZones> tZonesOptional = ItmUtils.read(LogicalDatastoreType.CONFIGURATION, path, broker);
150 if(monitorProtocol==null) {
151 monitorProtocol = ITMConstants.DEFAULT_MONITOR_PROTOCOL;
153 if (tZonesOptional.isPresent()) {
154 TransportZones tZones = tZonesOptional.get();
155 for (TransportZone tzone : tZones.getTransportZone()) {
156 LOG.debug("Add: TunnelMonitorToggleWorker with tzone = {} monitoringEnabled {} and monitoringProtocol {}",tzone.getZoneName(),dataObjectModification.isEnabled(), dataObjectModification.getMonitorProtocol());
157 ItmMonitorToggleWorker toggleWorker = new ItmMonitorToggleWorker(tzone.getZoneName(),
158 dataObjectModification.isEnabled(), monitorProtocol, broker);
159 coordinator.enqueueJob(tzone.getZoneName(), toggleWorker);
164 @Override protected TunnelMonitorChangeListener getDataTreeChangeListener() {
165 return TunnelMonitorChangeListener.this;