Merge "Add blueprint wiring for elanmanager"
[netvirt.git] / vpnservice / elanmanager / 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.controller.md.sal.binding.api.ClusteredDataChangeListener;
11 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
12 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
13 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
14 import org.opendaylight.genius.datastoreutils.AsyncClusteredDataChangeListenerBase;
15 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundUtils;
16 import org.opendaylight.netvirt.elan.l2gw.jobs.LogicalSwitchAddedJob;
17 import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayMulticastUtils;
18 import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayUtils;
19 import org.opendaylight.netvirt.elan.l2gw.utils.L2GatewayConnectionUtils;
20 import org.opendaylight.netvirt.elan.utils.ElanClusterUtils;
21 import org.opendaylight.netvirt.elan.utils.ElanUtils;
22 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.attributes.Devices;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepNodeName;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * The listener class for listening to {@code LogicalSwitches}
34  * add/delete/update.
35  *
36  * @see LogicalSwitches
37  */
38 public class HwvtepLogicalSwitchListener extends
39     AsyncClusteredDataChangeListenerBase<LogicalSwitches, HwvtepLogicalSwitchListener> {
40
41     /** The Constant LOG. */
42     private static final Logger LOG = LoggerFactory.getLogger(HwvtepLogicalSwitchListener.class);
43
44     /** The node id. */
45     private NodeId nodeId;
46
47     /** The logical switch name. */
48     private String logicalSwitchName;
49
50     /** The physical device. */
51     private Devices physicalDevice;
52
53     /** The l2 gateway device. */
54     private L2GatewayDevice l2GatewayDevice;
55
56     /** The default vlan id. */
57     private Integer defaultVlanId;
58
59     /** Id of L2 Gateway connection responsible for this logical switch creation */
60     private Uuid l2GwConnId;
61
62     private final DataBroker broker;
63     private final ElanL2GatewayUtils elanL2GatewayUtils;
64     private final EntityOwnershipService entityOwnershipService;
65     private final ElanUtils elanUtils;
66     private final ElanL2GatewayMulticastUtils elanL2GatewayMulticastUtils;
67
68     /**
69      * Instantiates a new hardware vtep logical switch listener.
70      *
71      * @param entityOwnershipService the entity ownership service
72      * @param elanUtils the ELAN utilities
73      * @param elanL2GatewayMulticastUtils
74      * @param l2GatewayDevice the l2 gateway device
75      * @param logicalSwitchName the logical switch name
76      * @param physicalDevice the physical device
77      * @param defaultVlanId the default vlan id
78      * @param l2GwConnId
79      */
80     public HwvtepLogicalSwitchListener(DataBroker dataBroker, ElanL2GatewayUtils elanL2GatewayUtils,
81                                        EntityOwnershipService entityOwnershipService, ElanUtils elanUtils,
82                                        ElanL2GatewayMulticastUtils elanL2GatewayMulticastUtils,
83                                        L2GatewayDevice l2GatewayDevice,
84                                        String logicalSwitchName,
85                                        Devices physicalDevice, Integer defaultVlanId, Uuid l2GwConnId) {
86         super(LogicalSwitches.class, HwvtepLogicalSwitchListener.class);
87         this.broker = dataBroker;
88         this.elanL2GatewayUtils = elanL2GatewayUtils;
89         this.entityOwnershipService = entityOwnershipService;
90         this.elanUtils = elanUtils;
91         this.elanL2GatewayMulticastUtils = elanL2GatewayMulticastUtils;
92         this.nodeId = new NodeId(l2GatewayDevice.getHwvtepNodeId());
93         this.logicalSwitchName = logicalSwitchName;
94         this.physicalDevice = physicalDevice;
95         this.l2GatewayDevice = l2GatewayDevice;
96         this.defaultVlanId = defaultVlanId;
97         this.l2GwConnId = l2GwConnId;
98     }
99
100     /*
101      * (non-Javadoc)
102      *
103      * @see
104      * org.opendaylight.genius.datastoreutils.AsyncClusteredDataChangeListenerBase#
105      * getWildCardPath()
106      */
107     @Override
108         public InstanceIdentifier<LogicalSwitches> getWildCardPath() {
109         return HwvtepSouthboundUtils.createLogicalSwitchesInstanceIdentifier(nodeId,
110                 new HwvtepNodeName(logicalSwitchName));
111     }
112
113     /*
114      * (non-Javadoc)
115      *
116      * @see
117      * org.opendaylight.genius.datastoreutils.AsyncClusteredDataChangeListenerBase#
118      * getDataChangeListener()
119      */
120     @Override
121     protected ClusteredDataChangeListener getDataChangeListener() {
122         return HwvtepLogicalSwitchListener.this;
123     }
124
125     /*
126      * (non-Javadoc)
127      *
128      * @see
129      * org.opendaylight.genius.datastoreutils.AsyncClusteredDataChangeListenerBase#
130      * getDataChangeScope()
131      */
132     @Override
133     protected AsyncDataBroker.DataChangeScope getDataChangeScope() {
134         return AsyncDataBroker.DataChangeScope.BASE;
135     }
136
137     /*
138      * (non-Javadoc)
139      *
140      * @see
141      * org.opendaylight.genius.datastoreutils.AsyncClusteredDataChangeListenerBase#
142      * remove(org.opendaylight.yangtools.yang.binding.InstanceIdentifier,
143      * org.opendaylight.yangtools.yang.binding.DataObject)
144      */
145     @Override
146     protected void remove(InstanceIdentifier<LogicalSwitches> identifier, LogicalSwitches deletedLogicalSwitch) {
147         LOG.trace("Received Remove DataChange Notification for identifier: {}, LogicalSwitches: {}", identifier,
148                 deletedLogicalSwitch);
149     }
150
151     /*
152      * (non-Javadoc)
153      *
154      * @see
155      * org.opendaylight.genius.datastoreutils.AsyncClusteredDataChangeListenerBase#
156      * update(org.opendaylight.yangtools.yang.binding.InstanceIdentifier,
157      * org.opendaylight.yangtools.yang.binding.DataObject,
158      * org.opendaylight.yangtools.yang.binding.DataObject)
159      */
160     @Override
161     protected void update(InstanceIdentifier<LogicalSwitches> identifier, LogicalSwitches logicalSwitchOld,
162             LogicalSwitches logicalSwitchNew) {
163         LOG.trace("Received Update DataChange Notification for identifier: {}, LogicalSwitches old: {}, new: {}."
164                 + "No Action Performed.", identifier, logicalSwitchOld, logicalSwitchNew);
165     }
166
167     /*
168      * (non-Javadoc)
169      *
170      * @see
171      * org.opendaylight.genius.datastoreutils.AsyncClusteredDataChangeListenerBase#
172      * add(org.opendaylight.yangtools.yang.binding.InstanceIdentifier,
173      * org.opendaylight.yangtools.yang.binding.DataObject)
174      */
175     @Override
176     protected void add(InstanceIdentifier<LogicalSwitches> identifier, LogicalSwitches logicalSwitchNew) {
177         LOG.debug("Received Add DataChange Notification for identifier: {}, LogicalSwitches: {}", identifier,
178                 logicalSwitchNew);
179         try {
180             L2GatewayDevice elanDevice = L2GatewayConnectionUtils.addL2DeviceToElanL2GwCache(
181                     logicalSwitchNew.getHwvtepNodeName().getValue(), l2GatewayDevice, l2GwConnId);
182
183             LogicalSwitchAddedJob logicalSwitchAddedWorker = new LogicalSwitchAddedJob(broker, elanL2GatewayUtils,
184                     elanUtils, elanL2GatewayMulticastUtils, logicalSwitchName, physicalDevice, elanDevice, defaultVlanId);
185             ElanClusterUtils.runOnlyInLeaderNode(entityOwnershipService, logicalSwitchAddedWorker.getJobKey() ,
186                     "create vlan mappings and mcast configurations",
187                     logicalSwitchAddedWorker);
188         } catch (Exception e) {
189             LOG.error("Failed to handle HwVTEPLogicalSwitch - add: {}", e);
190         } finally {
191             try {
192                 // This listener is specific to handle a specific logical
193                 // switch, hence closing it.
194                 LOG.trace("Closing LogicalSwitches listener for node: {}, logicalSwitch: {}", nodeId.getValue(),
195                         logicalSwitchName);
196                 close();
197             } catch (Exception e) {
198                 LOG.warn("Failed to close HwVTEPLogicalSwitchListener: {}", e);
199             }
200         }
201     }
202 }