Adding BC group update job.
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / l2gw / listeners / HwvtepLogicalSwitchListener.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.netvirt.elan.l2gw.listeners;
9
10 import org.opendaylight.genius.datastoreutils.hwvtep.HwvtepClusteredDataTreeChangeListener;
11 import org.opendaylight.genius.utils.hwvtep.HwvtepNodeHACache;
12 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundUtils;
13 import org.opendaylight.netvirt.elan.cache.ElanInstanceCache;
14 import org.opendaylight.netvirt.elan.l2gw.jobs.LogicalSwitchAddedJob;
15 import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayMulticastUtils;
16 import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayUtils;
17 import org.opendaylight.netvirt.elan.l2gw.utils.L2GatewayConnectionUtils;
18 import org.opendaylight.netvirt.elan.utils.ElanClusterUtils;
19 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
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.neutron.l2gateways.rev150712.l2gateway.attributes.Devices;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepNodeName;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
24 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * The listener class for listening to {@code LogicalSwitches}
31  * add/delete/update.
32  *
33  * @see LogicalSwitches
34  */
35 public class HwvtepLogicalSwitchListener extends
36         HwvtepClusteredDataTreeChangeListener<LogicalSwitches, HwvtepLogicalSwitchListener> {
37
38     /** The Constant LOG. */
39     private static final Logger LOG = LoggerFactory.getLogger(HwvtepLogicalSwitchListener.class);
40
41     /** The node id. */
42     private final NodeId nodeId;
43
44     /** The logical switch name. */
45     private final String logicalSwitchName;
46
47     /** The physical device. */
48     private final Devices physicalDevice;
49
50     /** The l2 gateway device. */
51     private final L2GatewayDevice l2GatewayDevice;
52
53     // The default vlan id
54     private final Integer defaultVlanId;
55
56     // Id of L2 Gateway connection responsible for this logical switch creation
57     private final Uuid l2GwConnId;
58
59     private final ElanInstanceCache elanInstanceCache;
60     private final ElanL2GatewayUtils elanL2GatewayUtils;
61     private final ElanClusterUtils elanClusterUtils;
62     private final ElanL2GatewayMulticastUtils elanL2GatewayMulticastUtils;
63     private final L2GatewayConnectionUtils l2GatewayConnectionUtils;
64
65     /**
66      * Instantiates a new hardware vtep logical switch listener.
67      */
68     public HwvtepLogicalSwitchListener(ElanInstanceCache elanInstanceCache, ElanL2GatewayUtils elanL2GatewayUtils,
69             ElanClusterUtils elanClusterUtils, ElanL2GatewayMulticastUtils elanL2GatewayMulticastUtils,
70             L2GatewayConnectionUtils l2GatewayConnectionUtils, L2GatewayDevice l2GatewayDevice,
71             String logicalSwitchName, Devices physicalDevice, Integer defaultVlanId, Uuid l2GwConnId,
72             HwvtepNodeHACache hwvtepNodeHACache) {
73         super(LogicalSwitches.class, HwvtepLogicalSwitchListener.class, hwvtepNodeHACache);
74         this.elanInstanceCache = elanInstanceCache;
75         this.elanL2GatewayUtils = elanL2GatewayUtils;
76         this.elanClusterUtils = elanClusterUtils;
77         this.elanL2GatewayMulticastUtils = elanL2GatewayMulticastUtils;
78         this.l2GatewayConnectionUtils = l2GatewayConnectionUtils;
79         this.nodeId = new NodeId(l2GatewayDevice.getHwvtepNodeId());
80         this.logicalSwitchName = logicalSwitchName;
81         this.physicalDevice = physicalDevice;
82         this.l2GatewayDevice = l2GatewayDevice;
83         this.defaultVlanId = defaultVlanId;
84         this.l2GwConnId = l2GwConnId;
85     }
86
87     /*
88      * (non-Javadoc)
89      *
90      * @see
91      * org.opendaylight.genius.datastoreutils.AsyncClusteredDataChangeListenerBase#
92      * getWildCardPath()
93      */
94     @Override
95     public InstanceIdentifier<LogicalSwitches> getWildCardPath() {
96         return HwvtepSouthboundUtils.createLogicalSwitchesInstanceIdentifier(nodeId,
97                 new HwvtepNodeName(logicalSwitchName));
98     }
99
100     @Override
101     protected HwvtepLogicalSwitchListener getDataTreeChangeListener() {
102         return this;
103     }
104
105     /*
106      * (non-Javadoc)
107      *
108      * @see
109      * org.opendaylight.genius.datastoreutils.AsyncClusteredDataChangeListenerBase#
110      * remove(org.opendaylight.yangtools.yang.binding.InstanceIdentifier,
111      * org.opendaylight.yangtools.yang.binding.DataObject)
112      */
113     @Override
114     protected void removed(InstanceIdentifier<LogicalSwitches> identifier, LogicalSwitches deletedLogicalSwitch) {
115         LOG.trace("Received Remove DataChange Notification for identifier: {}, LogicalSwitches: {}", identifier,
116                 deletedLogicalSwitch);
117     }
118
119     /*
120      * (non-Javadoc)
121      *
122      * @see
123      * org.opendaylight.genius.datastoreutils.AsyncClusteredDataChangeListenerBase#
124      * update(org.opendaylight.yangtools.yang.binding.InstanceIdentifier,
125      * org.opendaylight.yangtools.yang.binding.DataObject,
126      * org.opendaylight.yangtools.yang.binding.DataObject)
127      */
128     @Override
129     protected void updated(InstanceIdentifier<LogicalSwitches> identifier, LogicalSwitches logicalSwitchOld,
130             LogicalSwitches logicalSwitchNew) {
131         LOG.trace("Received Update DataChange Notification for identifier: {}, LogicalSwitches old: {}, new: {}."
132                 + "No Action Performed.", identifier, logicalSwitchOld, logicalSwitchNew);
133     }
134
135     @Override
136     @SuppressWarnings("checkstyle:IllegalCatch")
137     protected void added(InstanceIdentifier<LogicalSwitches> identifier, LogicalSwitches logicalSwitchNew) {
138         LOG.debug("Received Add DataChange Notification for identifier: {}, LogicalSwitches: {}", identifier,
139                 logicalSwitchNew);
140         try {
141             L2GatewayDevice elanDevice = l2GatewayConnectionUtils.addL2DeviceToElanL2GwCache(
142                     logicalSwitchNew.getHwvtepNodeName().getValue(), l2GatewayDevice, l2GwConnId, physicalDevice);
143
144             LogicalSwitchAddedJob logicalSwitchAddedWorker = new LogicalSwitchAddedJob(
145                     elanL2GatewayUtils, elanL2GatewayMulticastUtils, logicalSwitchName, physicalDevice, elanDevice,
146                     defaultVlanId);
147             elanClusterUtils.runOnlyInOwnerNode(logicalSwitchAddedWorker.getJobKey(),
148                     "create vlan mappings and mcast configurations", logicalSwitchAddedWorker);
149         } catch (RuntimeException e) {
150             LOG.error("Failed to handle HwVTEPLogicalSwitch - add for: {}", identifier, e);
151         } finally {
152             try {
153                 // This listener is specific to handle a specific logical
154                 // switch, hence closing it.
155                 LOG.trace("Closing LogicalSwitches listener for node: {}, logicalSwitch: {}", nodeId.getValue(),
156                         logicalSwitchName);
157                 // TODO use https://git.opendaylight.org/gerrit/#/c/44145/ when merged, and remove @SuppressWarnings
158                 close();
159             } catch (Exception e) {
160                 LOG.warn("Failed to close HwVTEPLogicalSwitchListener", e);
161             }
162         }
163     }
164 }