2 * Copyright (c) 2015 - 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.vpnservice.neutronvpn;
11 import com.google.common.base.Optional;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
15 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
16 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
17 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.vpnservice.mdsalutil.AbstractDataChangeListener;
20 import org.opendaylight.vpnservice.mdsalutil.MDSALUtil;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.L2vlan;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceBuilder;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.port.attributes.FixedIps;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.Ports;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.ElanInterfaces;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.elan.interfaces.ElanInterface;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.elan.interfaces.ElanInterfaceBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.elan.interfaces.ElanInterfaceKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfL2vlan;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfL2vlanBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.ParentRefs;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.ParentRefsBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.lockmanager.rev150819.LockManagerService;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.PortAddedToSubnetBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.PortRemovedFromSubnetBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.neutron.port.data
42 .PortFixedipToPortNameBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.subnetmaps.Subnetmap;
44 import org.opendaylight.yangtools.concepts.ListenerRegistration;
45 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
49 import java.util.ArrayList;
50 import java.util.Iterator;
51 import java.util.List;
54 public class NeutronPortChangeListener extends AbstractDataChangeListener<Port> implements AutoCloseable {
55 private static final Logger LOG = LoggerFactory.getLogger(NeutronPortChangeListener.class);
57 private ListenerRegistration<DataChangeListener> listenerRegistration;
58 private final DataBroker broker;
59 private NeutronvpnManager nvpnManager;
60 private LockManagerService lockManager;
61 private NotificationPublishService notificationPublishService;
62 private NotificationService notificationService;
65 public NeutronPortChangeListener(final DataBroker db, NeutronvpnManager nVpnMgr,NotificationPublishService notiPublishService, NotificationService notiService) {
68 nvpnManager = nVpnMgr;
69 notificationPublishService = notiPublishService;
70 notificationService = notiService;
74 public void setLockManager(LockManagerService lockManager) {
75 this.lockManager = lockManager;
79 public void close() throws Exception {
80 if (listenerRegistration != null) {
82 listenerRegistration.close();
83 } catch (final Exception e) {
84 LOG.error("Error when cleaning up DataChangeListener.", e);
86 listenerRegistration = null;
88 LOG.info("N_Port listener Closed");
92 private void registerListener(final DataBroker db) {
94 listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
95 InstanceIdentifier.create(Neutron.class).child(Ports.class).child(Port.class),
96 NeutronPortChangeListener.this, DataChangeScope.SUBTREE);
97 } catch (final Exception e) {
98 LOG.error("Neutron Manager Port DataChange listener registration fail!", e);
99 throw new IllegalStateException("Neutron Manager Port DataChange listener registration failed.", e);
104 protected void add(InstanceIdentifier<Port> identifier, Port input) {
105 if (LOG.isTraceEnabled()) {
106 LOG.trace("Adding Port : key: " + identifier + ", value=" + input);
108 handleNeutronPortCreated(input);
113 protected void remove(InstanceIdentifier<Port> identifier, Port input) {
114 if (LOG.isTraceEnabled()) {
115 LOG.trace("Removing Port : key: " + identifier + ", value=" + input);
117 handleNeutronPortDeleted(input);
122 protected void update(InstanceIdentifier<Port> identifier, Port original, Port update) {
123 if (LOG.isTraceEnabled()) {
124 LOG.trace("Updating Port : key: " + identifier + ", original value=" + original + ", update value=" +
127 List<FixedIps> oldIPs = (original.getFixedIps() != null) ? original.getFixedIps() : new ArrayList<FixedIps>();
128 List<FixedIps> newIPs = (update.getFixedIps() != null) ? update.getFixedIps() : new ArrayList<FixedIps>();
130 if (!oldIPs.equals(newIPs)) {
131 Iterator<FixedIps> iterator = newIPs.iterator();
132 while (iterator.hasNext()) {
133 FixedIps ip = iterator.next();
134 if (oldIPs.remove(ip)) {
138 handleNeutronPortUpdated(original, update);
142 private void handleNeutronPortCreated(Port port) {
143 LOG.info("Of-port-interface creation");
144 // Create of-port interface for this neutron port
145 String portInterfaceName = createOfPortInterface(port);
146 LOG.debug("Creating ELAN Interface");
147 createElanInterface(port, portInterfaceName);
148 LOG.debug("Add port to subnet");
149 // add port to local Subnets DS
150 Uuid vpnId = addPortToSubnets(port);
153 // create vpn-interface on this neutron port
154 LOG.debug("Adding VPN Interface");
155 nvpnManager.createVpnInterface(vpnId, port);
159 private void handleNeutronPortDeleted(Port port) {
160 LOG.debug("Of-port-interface removal");
161 LOG.debug("Remove port from subnet");
162 // remove port from local Subnets DS
163 Uuid vpnId = removePortFromSubnets(port);
166 // remove vpn-interface for this neutron port
167 LOG.debug("removing VPN Interface");
168 nvpnManager.deleteVpnInterface(port);
170 // Remove of-port interface for this neutron port
171 // ELAN interface is also implicitly deleted as part of this operation
172 deleteOfPortInterface(port);
176 private void handleNeutronPortUpdated(Port portoriginal, Port portupdate) {
177 LOG.debug("Add port to subnet");
178 // add port FixedIP to local Subnets DS
179 Uuid vpnIdup = addPortToSubnets(portupdate);
181 if (vpnIdup != null) {
182 nvpnManager.createVpnInterface(vpnIdup, portupdate);
185 // remove port FixedIP from local Subnets DS
186 Uuid vpnIdor = removePortFromSubnets(portoriginal);
188 if (vpnIdor != null) {
189 nvpnManager.deleteVpnInterface(portoriginal);
193 private String createOfPortInterface(Port port) {
194 Interface inf = createInterface(port);
195 String infName = inf.getName();
197 LOG.debug("Creating OFPort Interface {}", infName);
198 InstanceIdentifier interfaceIdentifier = NeutronvpnUtils.buildVlanInterfaceIdentifier(infName);
200 Optional<Interface> optionalInf = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
201 interfaceIdentifier);
202 if (!optionalInf.isPresent()) {
203 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, interfaceIdentifier, inf);
205 LOG.error("Interface {} is already present", infName);
207 } catch (Exception e) {
208 LOG.error("failed to create interface {} due to the exception {} ", infName, e.getMessage());
213 private Interface createInterface(Port port) {
214 String parentRefName = NeutronvpnUtils.uuidToTapPortName(port.getUuid());;
215 String interfaceName = port.getUuid().getValue();
216 IfL2vlan.L2vlanMode l2VlanMode = IfL2vlan.L2vlanMode.Trunk;
217 InterfaceBuilder interfaceBuilder = new InterfaceBuilder();
218 IfL2vlanBuilder ifL2vlanBuilder = new IfL2vlanBuilder();
219 ifL2vlanBuilder.setL2vlanMode(l2VlanMode);
220 ParentRefsBuilder parentRefsBuilder = new ParentRefsBuilder().setParentInterface(parentRefName);
221 interfaceBuilder.setEnabled(true).setName(interfaceName).setType(L2vlan.class).addAugmentation(IfL2vlan
222 .class, ifL2vlanBuilder.build()).addAugmentation(ParentRefs.class, parentRefsBuilder.build());
223 return interfaceBuilder.build();
226 private void deleteOfPortInterface(Port port) {
227 String name = port.getUuid().getValue();
228 LOG.debug("Removing OFPort Interface {}", name);
229 InstanceIdentifier interfaceIdentifier = NeutronvpnUtils.buildVlanInterfaceIdentifier(name);
231 Optional<Interface> optionalInf = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
232 interfaceIdentifier);
233 if (optionalInf.isPresent()) {
234 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, interfaceIdentifier);
236 LOG.error("Interface {} is not present", name);
238 } catch (Exception e) {
239 LOG.error("Failed to delete interface {} due to the exception {}", name, e.getMessage());
243 private void createElanInterface(Port port, String name) {
244 String elanInstanceName = port.getNetworkId().getValue();
245 List<PhysAddress> physAddresses = new ArrayList<>();
246 physAddresses.add(new PhysAddress(port.getMacAddress()));
248 InstanceIdentifier<ElanInterface> id = InstanceIdentifier.builder(ElanInterfaces.class).child(ElanInterface
249 .class, new ElanInterfaceKey(name)).build();
250 ElanInterface elanInterface = new ElanInterfaceBuilder().setElanInstanceName(elanInstanceName)
251 .setName(name).setStaticMacEntries(physAddresses).setKey(new ElanInterfaceKey(name)).build();
252 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, id, elanInterface);
253 LOG.debug("Creating new ELan Interface {}", elanInterface);
256 // adds port to subnet list and creates vpnInterface
257 private Uuid addPortToSubnets(Port port) {
258 Uuid subnetId = null;
260 Subnetmap subnetmap = null;
261 String infName = port.getUuid().getValue();
262 boolean isLockAcquired = false;
263 String lockName = port.getUuid().getValue();
265 // find the subnet to which this port is associated
266 FixedIps ip = port.getFixedIps().get(0);
267 String ipValue = ip.getIpAddress().getIpv4Address().getValue();
268 InstanceIdentifier id = NeutronvpnUtils.buildFixedIpToPortNameIdentifier(ipValue);
269 PortFixedipToPortNameBuilder builder = new PortFixedipToPortNameBuilder().setPortFixedip(ipValue)
270 .setPortName(infName);
271 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, id, builder.build());
272 LOG.debug("fixedIp-name map for neutron port with fixedIp: {}, name: {} added to NeutronPortData DS",
274 subnetId = ip.getSubnetId();
275 subnetmap = nvpnManager.updateSubnetNode(subnetId, null, null, null, null, null, port.getUuid());
276 if (subnetmap != null) {
277 vpnId = subnetmap.getVpnId();
281 isLockAcquired = NeutronvpnUtils.lock(lockManager, lockName);
282 checkAndPublishPortAddNotification(subnetmap.getSubnetIp(), subnetId, port.getUuid());
283 LOG.debug("Port added to subnet notification sent");
284 } catch (Exception e) {
285 LOG.error("Port added to subnet notification failed", e);
287 if (isLockAcquired) {
288 NeutronvpnUtils.unlock(lockManager, lockName);
295 private Uuid removePortFromSubnets(Port port) {
296 Uuid subnetId = null;
298 Subnetmap subnetmap = null;
299 boolean isLockAcquired = false;
300 String lockName = port.getUuid().getValue();
302 // find the subnet to which this port is associated
303 FixedIps ip = port.getFixedIps().get(0);
304 String ipValue = ip.getIpAddress().getIpv4Address().getValue();
305 InstanceIdentifier id = NeutronvpnUtils.buildFixedIpToPortNameIdentifier(ipValue);
306 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, id);
307 LOG.debug("fixedIp-name map for neutron port with fixedIp: {} deleted from NeutronPortData DS", ipValue);
308 subnetId = ip.getSubnetId();
309 subnetmap = nvpnManager.removeFromSubnetNode(subnetId, null, null, null, port.getUuid());
310 if (subnetmap != null) {
311 vpnId = subnetmap.getVpnId();
315 isLockAcquired = NeutronvpnUtils.lock(lockManager, lockName);
316 checkAndPublishPortRemoveNotification(subnetmap.getSubnetIp(), subnetId, port.getUuid());
317 LOG.debug("Port removed from subnet notification sent");
318 } catch (Exception e) {
319 LOG.error("Port removed from subnet notification failed", e);
321 if (isLockAcquired) {
322 NeutronvpnUtils.unlock(lockManager, lockName);
329 private void checkAndPublishPortAddNotification(String subnetIp, Uuid subnetId, Uuid portId)throws InterruptedException{
330 PortAddedToSubnetBuilder builder = new PortAddedToSubnetBuilder();
332 LOG.info("publish notification called");
334 builder.setSubnetIp(subnetIp);
335 builder.setSubnetId(subnetId);
336 builder.setPortId(portId);
338 notificationPublishService.putNotification(builder.build());
341 private void checkAndPublishPortRemoveNotification(String subnetIp, Uuid subnetId, Uuid portId)throws InterruptedException{
342 PortRemovedFromSubnetBuilder builder = new PortRemovedFromSubnetBuilder();
344 LOG.info("publish notification called");
346 builder.setPortId(portId);
347 builder.setSubnetIp(subnetIp);
348 builder.setSubnetId(subnetId);
350 notificationPublishService.putNotification(builder.build());