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