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.vpnservice.neutronvpn.api.utils.NeutronUtils;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.L2vlan;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceBuilder;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.port.attributes.FixedIps;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.Ports;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.ElanInterfaces;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.elan.interfaces.ElanInterface;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.elan.interfaces.ElanInterfaceBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.elan.interfaces.ElanInterfaceKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfL2vlan;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfL2vlanBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.ParentRefs;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.ParentRefsBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.lockmanager.rev150819.LockManagerService;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.PortAddedToSubnetBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.PortRemovedFromSubnetBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.neutron.port.data
43 .PortFixedipToPortNameBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.subnetmaps.Subnetmap;
45 import org.opendaylight.yangtools.concepts.ListenerRegistration;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
50 import java.util.ArrayList;
51 import java.util.Iterator;
52 import java.util.List;
55 public class NeutronPortChangeListener extends AbstractDataChangeListener<Port> implements AutoCloseable {
56 private static final Logger LOG = LoggerFactory.getLogger(NeutronPortChangeListener.class);
58 private ListenerRegistration<DataChangeListener> listenerRegistration;
59 private final DataBroker broker;
60 private NeutronvpnManager nvpnManager;
61 private LockManagerService lockManager;
62 private NotificationPublishService notificationPublishService;
63 private NotificationService notificationService;
66 public NeutronPortChangeListener(final DataBroker db, NeutronvpnManager nVpnMgr,NotificationPublishService notiPublishService, NotificationService notiService) {
69 nvpnManager = nVpnMgr;
70 notificationPublishService = notiPublishService;
71 notificationService = notiService;
75 public void setLockManager(LockManagerService lockManager) {
76 this.lockManager = lockManager;
80 public void close() throws Exception {
81 if (listenerRegistration != null) {
83 listenerRegistration.close();
84 } catch (final Exception e) {
85 LOG.error("Error when cleaning up DataChangeListener.", e);
87 listenerRegistration = null;
89 LOG.info("N_Port listener Closed");
93 private void registerListener(final DataBroker db) {
95 listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
96 InstanceIdentifier.create(Neutron.class).child(Ports.class).child(Port.class),
97 NeutronPortChangeListener.this, DataChangeScope.SUBTREE);
98 } catch (final Exception e) {
99 LOG.error("Neutron Manager Port DataChange listener registration fail!", e);
100 throw new IllegalStateException("Neutron Manager Port DataChange listener registration failed.", e);
105 protected void add(InstanceIdentifier<Port> identifier, Port input) {
106 if (LOG.isTraceEnabled()) {
107 LOG.trace("Adding Port : key: " + identifier + ", value=" + input);
109 handleNeutronPortCreated(input);
114 protected void remove(InstanceIdentifier<Port> identifier, Port input) {
115 if (LOG.isTraceEnabled()) {
116 LOG.trace("Removing Port : key: " + identifier + ", value=" + input);
118 handleNeutronPortDeleted(input);
123 protected void update(InstanceIdentifier<Port> identifier, Port original, Port update) {
124 if (LOG.isTraceEnabled()) {
125 LOG.trace("Updating Port : key: " + identifier + ", original value=" + original + ", update value=" +
128 List<FixedIps> oldIPs = (original.getFixedIps() != null) ? original.getFixedIps() : new ArrayList<FixedIps>();
129 List<FixedIps> newIPs = (update.getFixedIps() != null) ? update.getFixedIps() : new ArrayList<FixedIps>();
131 if (!oldIPs.equals(newIPs)) {
132 Iterator<FixedIps> iterator = newIPs.iterator();
133 while (iterator.hasNext()) {
134 FixedIps ip = iterator.next();
135 if (oldIPs.remove(ip)) {
139 handleNeutronPortUpdated(original, update);
143 private void handleNeutronPortCreated(Port port) {
144 if (!NeutronUtils.isPortVnicTypeNormal(port)) {
145 LOG.info("Port {} is not a NORMAL VNIC Type port; OF Port interfaces are not created",
146 port.getUuid().getValue());
149 LOG.info("Of-port-interface creation");
150 // Create of-port interface for this neutron port
151 String portInterfaceName = createOfPortInterface(port);
152 LOG.debug("Creating ELAN Interface");
153 createElanInterface(port, portInterfaceName);
154 LOG.debug("Add port to subnet");
155 // add port to local Subnets DS
156 Uuid vpnId = addPortToSubnets(port);
159 // create vpn-interface on this neutron port
160 LOG.debug("Adding VPN Interface");
161 nvpnManager.createVpnInterface(vpnId, port);
162 Uuid routerId = NeutronvpnUtils.getVpnMap(broker, vpnId).getRouterId();
163 if(routerId != null) {
164 nvpnManager.addToNeutronRouterInterfacesMap(routerId, port.getUuid().getValue());
169 private void handleNeutronPortDeleted(Port port) {
170 LOG.debug("Of-port-interface removal");
171 LOG.debug("Remove port from subnet");
172 // remove port from local Subnets DS
173 Uuid vpnId = removePortFromSubnets(port);
176 // remove vpn-interface for this neutron port
177 LOG.debug("removing VPN Interface");
178 nvpnManager.deleteVpnInterface(port);
180 // Remove of-port interface for this neutron port
181 // ELAN interface is also implicitly deleted as part of this operation
182 deleteOfPortInterface(port);
184 Uuid routerId = NeutronvpnUtils.getVpnMap(broker, vpnId).getRouterId();
185 if(routerId != null) {
186 nvpnManager.removeFromNeutronRouterInterfacesMap(routerId, port.getUuid().getValue());
191 private void handleNeutronPortUpdated(Port portoriginal, Port portupdate) {
192 LOG.debug("Add port to subnet");
193 // add port FixedIP to local Subnets DS
194 Uuid vpnIdup = addPortToSubnets(portupdate);
196 if (vpnIdup != null) {
197 nvpnManager.createVpnInterface(vpnIdup, portupdate);
198 Uuid routerId = NeutronvpnUtils.getVpnMap(broker, vpnIdup).getRouterId();
199 if(routerId != null) {
200 nvpnManager.addToNeutronRouterInterfacesMap(routerId, portupdate.getUuid().getValue());
204 // remove port FixedIP from local Subnets DS
205 Uuid vpnIdor = removePortFromSubnets(portoriginal);
207 if (vpnIdor != null) {
208 nvpnManager.deleteVpnInterface(portoriginal);
209 Uuid routerId = NeutronvpnUtils.getVpnMap(broker, vpnIdor).getRouterId();
210 if(routerId != null) {
211 nvpnManager.removeFromNeutronRouterInterfacesMap(routerId, portoriginal.getUuid().getValue());
216 private String createOfPortInterface(Port port) {
217 Interface inf = createInterface(port);
218 String infName = inf.getName();
220 LOG.debug("Creating OFPort Interface {}", infName);
221 InstanceIdentifier interfaceIdentifier = NeutronvpnUtils.buildVlanInterfaceIdentifier(infName);
223 Optional<Interface> optionalInf = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
224 interfaceIdentifier);
225 if (!optionalInf.isPresent()) {
226 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, interfaceIdentifier, inf);
228 LOG.error("Interface {} is already present", infName);
230 } catch (Exception e) {
231 LOG.error("failed to create interface {} due to the exception {} ", infName, e.getMessage());
236 private Interface createInterface(Port port) {
237 String parentRefName = NeutronvpnUtils.uuidToTapPortName(port.getUuid());;
238 String interfaceName = port.getUuid().getValue();
239 IfL2vlan.L2vlanMode l2VlanMode = IfL2vlan.L2vlanMode.Trunk;
240 InterfaceBuilder interfaceBuilder = new InterfaceBuilder();
241 IfL2vlanBuilder ifL2vlanBuilder = new IfL2vlanBuilder();
242 ifL2vlanBuilder.setL2vlanMode(l2VlanMode);
243 ParentRefsBuilder parentRefsBuilder = new ParentRefsBuilder().setParentInterface(parentRefName);
244 interfaceBuilder.setEnabled(true).setName(interfaceName).setType(L2vlan.class).addAugmentation(IfL2vlan
245 .class, ifL2vlanBuilder.build()).addAugmentation(ParentRefs.class, parentRefsBuilder.build());
246 return interfaceBuilder.build();
249 private void deleteOfPortInterface(Port port) {
250 String name = port.getUuid().getValue();
251 LOG.debug("Removing OFPort Interface {}", name);
252 InstanceIdentifier interfaceIdentifier = NeutronvpnUtils.buildVlanInterfaceIdentifier(name);
254 Optional<Interface> optionalInf = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
255 interfaceIdentifier);
256 if (optionalInf.isPresent()) {
257 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, interfaceIdentifier);
259 LOG.error("Interface {} is not present", name);
261 } catch (Exception e) {
262 LOG.error("Failed to delete interface {} due to the exception {}", name, e.getMessage());
266 private void createElanInterface(Port port, String name) {
267 String elanInstanceName = port.getNetworkId().getValue();
268 List<PhysAddress> physAddresses = new ArrayList<>();
269 physAddresses.add(new PhysAddress(port.getMacAddress()));
271 InstanceIdentifier<ElanInterface> id = InstanceIdentifier.builder(ElanInterfaces.class).child(ElanInterface
272 .class, new ElanInterfaceKey(name)).build();
273 ElanInterface elanInterface = new ElanInterfaceBuilder().setElanInstanceName(elanInstanceName)
274 .setName(name).setStaticMacEntries(physAddresses).setKey(new ElanInterfaceKey(name)).build();
275 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, id, elanInterface);
276 LOG.debug("Creating new ELan Interface {}", elanInterface);
279 // adds port to subnet list and creates vpnInterface
280 private Uuid addPortToSubnets(Port port) {
281 Uuid subnetId = null;
283 Subnetmap subnetmap = null;
284 String infName = port.getUuid().getValue();
285 boolean isLockAcquired = false;
286 String lockName = port.getUuid().getValue();
288 // find the subnet to which this port is associated
289 FixedIps ip = port.getFixedIps().get(0);
290 String ipValue = ip.getIpAddress().getIpv4Address().getValue();
291 InstanceIdentifier id = NeutronvpnUtils.buildFixedIpToPortNameIdentifier(ipValue);
292 PortFixedipToPortNameBuilder builder = new PortFixedipToPortNameBuilder().setPortFixedip(ipValue)
293 .setPortName(infName);
294 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, id, builder.build());
295 LOG.debug("fixedIp-name map for neutron port with fixedIp: {}, name: {} added to NeutronPortData DS",
297 subnetId = ip.getSubnetId();
298 subnetmap = nvpnManager.updateSubnetNode(subnetId, null, null, null, null, null, port.getUuid());
299 if (subnetmap != null) {
300 vpnId = subnetmap.getVpnId();
304 isLockAcquired = NeutronvpnUtils.lock(lockManager, lockName);
305 checkAndPublishPortAddNotification(subnetmap.getSubnetIp(), subnetId, port.getUuid());
306 LOG.debug("Port added to subnet notification sent");
307 } catch (Exception e) {
308 LOG.error("Port added to subnet notification failed", e);
310 if (isLockAcquired) {
311 NeutronvpnUtils.unlock(lockManager, lockName);
318 private Uuid removePortFromSubnets(Port port) {
319 Uuid subnetId = null;
321 Subnetmap subnetmap = null;
322 boolean isLockAcquired = false;
323 String lockName = port.getUuid().getValue();
325 // find the subnet to which this port is associated
326 FixedIps ip = port.getFixedIps().get(0);
327 String ipValue = ip.getIpAddress().getIpv4Address().getValue();
328 InstanceIdentifier id = NeutronvpnUtils.buildFixedIpToPortNameIdentifier(ipValue);
329 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, id);
330 LOG.debug("fixedIp-name map for neutron port with fixedIp: {} deleted from NeutronPortData DS", ipValue);
331 subnetId = ip.getSubnetId();
332 subnetmap = nvpnManager.removeFromSubnetNode(subnetId, null, null, null, port.getUuid());
333 if (subnetmap != null) {
334 vpnId = subnetmap.getVpnId();
338 isLockAcquired = NeutronvpnUtils.lock(lockManager, lockName);
339 checkAndPublishPortRemoveNotification(subnetmap.getSubnetIp(), subnetId, port.getUuid());
340 LOG.debug("Port removed from subnet notification sent");
341 } catch (Exception e) {
342 LOG.error("Port removed from subnet notification failed", e);
344 if (isLockAcquired) {
345 NeutronvpnUtils.unlock(lockManager, lockName);
352 private void checkAndPublishPortAddNotification(String subnetIp, Uuid subnetId, Uuid portId)throws InterruptedException{
353 PortAddedToSubnetBuilder builder = new PortAddedToSubnetBuilder();
355 LOG.info("publish notification called");
357 builder.setSubnetIp(subnetIp);
358 builder.setSubnetId(subnetId);
359 builder.setPortId(portId);
361 notificationPublishService.putNotification(builder.build());
364 private void checkAndPublishPortRemoveNotification(String subnetIp, Uuid subnetId, Uuid portId)throws InterruptedException{
365 PortRemovedFromSubnetBuilder builder = new PortRemovedFromSubnetBuilder();
367 LOG.info("publish notification called");
369 builder.setPortId(portId);
370 builder.setSubnetIp(subnetIp);
371 builder.setSubnetId(subnetId);
373 notificationPublishService.putNotification(builder.build());