Merge "Table Id constant for L3VPN GW Table"
[genius.git] / itm / itm-impl / src / main / java / org / opendaylight / genius / itm / listeners / TunnelMonitorChangeListener.java
1 /*
2  * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.genius.itm.listeners;
9
10 import com.google.common.base.Optional;
11 import java.util.ArrayList;
12 import java.util.List;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
16 import org.opendaylight.genius.datastoreutils.DataStoreJobCoordinator;
17 import org.opendaylight.genius.itm.confighelpers.HwVtep;
18 import org.opendaylight.genius.itm.confighelpers.ItmMonitorToggleWorker;
19 import org.opendaylight.genius.itm.globals.ITMConstants;
20 import org.opendaylight.genius.itm.impl.ItmUtils;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelMonitoringTypeBase;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.config.rev160406.TunnelMonitorParams;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.TransportZones;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.TransportZone;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.transport.zone.Subnets;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.transport.zone.subnets.DeviceVteps;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
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;
36
37     public TunnelMonitorChangeListener(final DataBroker db) {
38         super(TunnelMonitorParams.class, TunnelMonitorChangeListener.class);
39         broker = db;
40         // interfaceManager = ifManager;
41         // registerListener(db);
42     }
43
44     /* private void registerListener(final DataBroker db) {
45          try {
46              TunnelMonitorChangeListener monitorEnabledChangeListener = new TunnelMonitorChangeListener();
47              monitorEnabledListenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
48                      monitorEnabledChangeListener.getWildCardPath(), monitorEnabledChangeListener, DataChangeScope.SUBTREE);
49          } catch (final Exception e) {
50              LOG.error("ITM Monitor Interfaces DataChange listener registration fail!", e);
51              throw new IllegalStateException("ITM Monitor registration Listener failed.", e);
52          }
53      }
54  */    @Override
55     public void close() throws Exception {
56        /* if (monitorEnabledListenerRegistration != null) {
57             try {
58                 monitorEnabledListenerRegistration.close();
59             } catch (final Exception e) {
60                 LOG.error("Error when cleaning up DataChangeListener.", e);
61             }
62             monitorEnabledListenerRegistration = null;
63         }
64
65         if (monitorIntervalListenerRegistration != null) {
66             try {
67                 monitorIntervalListenerRegistration.close();
68             } catch (final Exception e) {
69                 LOG.error("Error when cleaning up DataChangeListener.", e);
70             }
71             monitorIntervalListenerRegistration = null;
72         }
73 */
74         LOG.info("Tunnel Monitor listeners Closed");
75     }
76
77     @Override protected InstanceIdentifier<TunnelMonitorParams> getWildCardPath() {
78         return InstanceIdentifier.create(TunnelMonitorParams.class);
79     }
80
81     @Override
82     protected void remove(InstanceIdentifier<TunnelMonitorParams> key, TunnelMonitorParams dataObjectModification) {
83         List<HwVtep> hwVteps = new ArrayList<>();
84         Boolean hwVtepsExist = false;
85         DataStoreJobCoordinator coordinator = DataStoreJobCoordinator.getInstance();
86         InstanceIdentifier<TransportZones> path = InstanceIdentifier.builder(TransportZones.class).build();
87         Optional<TransportZones> tZonesOptional = ItmUtils.read(LogicalDatastoreType.CONFIGURATION, path, broker);
88         Class<? extends TunnelMonitoringTypeBase> monitorProtocol = dataObjectModification.getMonitorProtocol();
89         if (tZonesOptional.isPresent()) {
90             TransportZones tZones = tZonesOptional.get();
91             for (TransportZone tzone : tZones.getTransportZone()) {
92                 hwVtepsExist = false;
93                 hwVteps = new ArrayList<>();
94                 if (tzone.getSubnets() != null && !tzone.getSubnets().isEmpty()) {
95                     for (Subnets sub : tzone.getSubnets()) {
96                         if (sub.getDeviceVteps() != null && !sub.getDeviceVteps().isEmpty()) {
97                             hwVtepsExist = true;
98                             for (DeviceVteps deviceVtep : sub.getDeviceVteps()) {
99                                 HwVtep hwVtep = ItmUtils.createHwVtepObject(deviceVtep.getTopologyId(), deviceVtep.getNodeId(),
100                                         deviceVtep.getIpAddress(), sub.getPrefix(), sub.getGatewayIp(), sub.getVlanId(),
101                                         tzone.getTunnelType(), tzone);
102                                 hwVteps.add(hwVtep);
103                             }
104                         }
105                     }
106                 }
107                 if(monitorProtocol==null)
108                     monitorProtocol = ITMConstants.DEFAULT_MONITOR_PROTOCOL;
109                 LOG.debug("Remove:Calling TunnelMonitorToggleWorker with tzone = {} and {}",tzone.getZoneName(),dataObjectModification.isEnabled());
110                 LOG.debug("Update:Calling TunnelMonitorToggleWorker with monitor protocol = {} ",monitorProtocol);
111                 ItmMonitorToggleWorker toggleWorker = new ItmMonitorToggleWorker(hwVteps, tzone.getZoneName(),
112                         false,monitorProtocol, broker, hwVtepsExist);
113                 coordinator.enqueueJob(tzone.getZoneName(), toggleWorker);
114             }
115         }
116     }
117
118
119     @Override protected void update(InstanceIdentifier<TunnelMonitorParams> key,
120                                     TunnelMonitorParams dataObjectModificationBefore,
121                                     TunnelMonitorParams dataObjectModificationAfter) {
122         LOG.debug("update TunnelMonitorChangeListener called with {}",dataObjectModificationAfter.isEnabled());
123         List<HwVtep> hwVteps = new ArrayList<>();
124         Boolean hwVtepsExist = false;
125         Class<? extends TunnelMonitoringTypeBase> monitorProtocol_before = dataObjectModificationBefore.getMonitorProtocol();
126         Class<? extends TunnelMonitoringTypeBase> monitorProtocol_after = dataObjectModificationAfter.getMonitorProtocol();
127         Class<? extends TunnelMonitoringTypeBase> monitorProtocol = ITMConstants.DEFAULT_MONITOR_PROTOCOL;
128         DataStoreJobCoordinator coordinator = DataStoreJobCoordinator.getInstance();
129         InstanceIdentifier<TransportZones> path = InstanceIdentifier.builder(TransportZones.class).build();
130         Optional<TransportZones> tZonesOptional = ItmUtils.read(LogicalDatastoreType.CONFIGURATION, path, broker);
131         if(monitorProtocol_after!=null )
132             monitorProtocol = dataObjectModificationAfter.getMonitorProtocol();
133         if(monitorProtocol_before!=null && monitorProtocol_after!=null)
134         {
135             LOG.debug("Update TunnelMonitorChangeListener: Existing monitor protocol {}",monitorProtocol_before.getName());
136             LOG.debug("Update TunnelMonitorChangeListener: New monitor protocol {}",monitorProtocol_after.getName());
137             if(!monitorProtocol_after.getName().equalsIgnoreCase(monitorProtocol_before.getName()))
138                 LOG.error("Updation of monitor protocol not allowed");
139
140         }
141         if (tZonesOptional.isPresent()) {
142             TransportZones tZones = tZonesOptional.get();
143             for (TransportZone tzone : tZones.getTransportZone()) {
144                 hwVtepsExist = false;
145                 hwVteps = new ArrayList<>();
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);
155                                 hwVteps.add(hwVtep);
156                             }
157                         }
158                     }
159                 }
160                 LOG.debug("Update:Calling TunnelMonitorToggleWorker with tzone = {} and {}",tzone.getZoneName(),dataObjectModificationAfter.isEnabled());
161                 LOG.debug("Update:Calling TunnelMonitorToggleWorker with monitor protocol = {} ",monitorProtocol);
162                 ItmMonitorToggleWorker toggleWorker = new ItmMonitorToggleWorker(hwVteps, tzone.getZoneName(),
163                         dataObjectModificationAfter.isEnabled(), monitorProtocol, broker, hwVtepsExist);
164                 coordinator.enqueueJob(tzone.getZoneName(), toggleWorker);
165             }
166         }
167     }
168
169     @Override
170     protected void add(InstanceIdentifier<TunnelMonitorParams> key, TunnelMonitorParams dataObjectModification) {
171         LOG.debug("add TunnelMonitorChangeListener called with {}",dataObjectModification.isEnabled());
172         LOG.debug("add TunnelMonitorChangeListener called with monitorProtcol {}",dataObjectModification.getMonitorProtocol());
173         List<HwVtep> hwVteps = new ArrayList<>();
174         Boolean hwVtepsExist = false;
175         Class<? extends TunnelMonitoringTypeBase> monitorProtocol = dataObjectModification.getMonitorProtocol();
176         DataStoreJobCoordinator coordinator = DataStoreJobCoordinator.getInstance();
177         InstanceIdentifier<TransportZones> path = InstanceIdentifier.builder(TransportZones.class).build();
178         Optional<TransportZones> tZonesOptional = ItmUtils.read(LogicalDatastoreType.CONFIGURATION, path, broker);
179         if (tZonesOptional.isPresent()) {
180             TransportZones tZones = tZonesOptional.get();
181             for (TransportZone tzone : tZones.getTransportZone()) {
182                 hwVtepsExist = false;
183                 hwVteps = new ArrayList<>();
184                 if (tzone.getSubnets() != null && !tzone.getSubnets().isEmpty()) {
185                     for (Subnets sub : tzone.getSubnets()) {
186                         if (sub.getDeviceVteps() != null && !sub.getDeviceVteps().isEmpty()) {
187                             hwVtepsExist = true;
188                             for (DeviceVteps deviceVtep : sub.getDeviceVteps()) {
189                                 HwVtep hwVtep = ItmUtils.createHwVtepObject(deviceVtep.getTopologyId(), deviceVtep.getNodeId(),
190                                         deviceVtep.getIpAddress(), sub.getPrefix(), sub.getGatewayIp(), sub.getVlanId(),
191                                         tzone.getTunnelType(), tzone);
192                                 hwVteps.add(hwVtep);
193                             }
194                         }
195                     }
196                 }
197                 LOG.debug("Add:Calling TunnelMonitorToggleWorker with tzone = {} monitoringEnabled {} and monitoringProtocol {}",tzone.getZoneName(),dataObjectModification.isEnabled(), dataObjectModification.getMonitorProtocol());
198                 if(monitorProtocol==null)
199                     monitorProtocol = ITMConstants.DEFAULT_MONITOR_PROTOCOL;
200                 LOG.debug("Add:Calling TunnelMonitorToggleWorker with monitor protocol = {} ",monitorProtocol);
201                 ItmMonitorToggleWorker toggleWorker = new ItmMonitorToggleWorker(hwVteps, tzone.getZoneName(),
202                         dataObjectModification.isEnabled(), monitorProtocol, broker, hwVtepsExist);
203                 coordinator.enqueueJob(tzone.getZoneName(), toggleWorker);
204             }
205         }
206     }
207
208     @Override protected TunnelMonitorChangeListener getDataTreeChangeListener() {
209         return TunnelMonitorChangeListener.this;
210     }
211
212 }