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);
165 private void handleNeutronPortDeleted(Port port) {
166 LOG.debug("Of-port-interface removal");
167 LOG.debug("Remove port from subnet");
168 // remove port from local Subnets DS
169 Uuid vpnId = removePortFromSubnets(port);
172 // remove vpn-interface for this neutron port
173 LOG.debug("removing VPN Interface");
174 nvpnManager.deleteVpnInterface(port);
176 // Remove of-port interface for this neutron port
177 // ELAN interface is also implicitly deleted as part of this operation
178 deleteOfPortInterface(port);
182 private void handleNeutronPortUpdated(Port portoriginal, Port portupdate) {
183 LOG.debug("Add port to subnet");
184 // add port FixedIP to local Subnets DS
185 Uuid vpnIdup = addPortToSubnets(portupdate);
187 if (vpnIdup != null) {
188 nvpnManager.createVpnInterface(vpnIdup, portupdate);
191 // remove port FixedIP from local Subnets DS
192 Uuid vpnIdor = removePortFromSubnets(portoriginal);
194 if (vpnIdor != null) {
195 nvpnManager.deleteVpnInterface(portoriginal);
199 private String createOfPortInterface(Port port) {
200 Interface inf = createInterface(port);
201 String infName = inf.getName();
203 LOG.debug("Creating OFPort Interface {}", infName);
204 InstanceIdentifier interfaceIdentifier = NeutronvpnUtils.buildVlanInterfaceIdentifier(infName);
206 Optional<Interface> optionalInf = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
207 interfaceIdentifier);
208 if (!optionalInf.isPresent()) {
209 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, interfaceIdentifier, inf);
211 LOG.error("Interface {} is already present", infName);
213 } catch (Exception e) {
214 LOG.error("failed to create interface {} due to the exception {} ", infName, e.getMessage());
219 private Interface createInterface(Port port) {
220 String parentRefName = NeutronvpnUtils.uuidToTapPortName(port.getUuid());;
221 String interfaceName = port.getUuid().getValue();
222 IfL2vlan.L2vlanMode l2VlanMode = IfL2vlan.L2vlanMode.Trunk;
223 InterfaceBuilder interfaceBuilder = new InterfaceBuilder();
224 IfL2vlanBuilder ifL2vlanBuilder = new IfL2vlanBuilder();
225 ifL2vlanBuilder.setL2vlanMode(l2VlanMode);
226 ParentRefsBuilder parentRefsBuilder = new ParentRefsBuilder().setParentInterface(parentRefName);
227 interfaceBuilder.setEnabled(true).setName(interfaceName).setType(L2vlan.class).addAugmentation(IfL2vlan
228 .class, ifL2vlanBuilder.build()).addAugmentation(ParentRefs.class, parentRefsBuilder.build());
229 return interfaceBuilder.build();
232 private void deleteOfPortInterface(Port port) {
233 String name = port.getUuid().getValue();
234 LOG.debug("Removing OFPort Interface {}", name);
235 InstanceIdentifier interfaceIdentifier = NeutronvpnUtils.buildVlanInterfaceIdentifier(name);
237 Optional<Interface> optionalInf = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
238 interfaceIdentifier);
239 if (optionalInf.isPresent()) {
240 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, interfaceIdentifier);
242 LOG.error("Interface {} is not present", name);
244 } catch (Exception e) {
245 LOG.error("Failed to delete interface {} due to the exception {}", name, e.getMessage());
249 private void createElanInterface(Port port, String name) {
250 String elanInstanceName = port.getNetworkId().getValue();
251 List<PhysAddress> physAddresses = new ArrayList<>();
252 physAddresses.add(new PhysAddress(port.getMacAddress()));
254 InstanceIdentifier<ElanInterface> id = InstanceIdentifier.builder(ElanInterfaces.class).child(ElanInterface
255 .class, new ElanInterfaceKey(name)).build();
256 ElanInterface elanInterface = new ElanInterfaceBuilder().setElanInstanceName(elanInstanceName)
257 .setName(name).setStaticMacEntries(physAddresses).setKey(new ElanInterfaceKey(name)).build();
258 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, id, elanInterface);
259 LOG.debug("Creating new ELan Interface {}", elanInterface);
262 // adds port to subnet list and creates vpnInterface
263 private Uuid addPortToSubnets(Port port) {
264 Uuid subnetId = null;
266 Subnetmap subnetmap = null;
267 String infName = port.getUuid().getValue();
268 boolean isLockAcquired = false;
269 String lockName = port.getUuid().getValue();
271 // find the subnet to which this port is associated
272 FixedIps ip = port.getFixedIps().get(0);
273 String ipValue = ip.getIpAddress().getIpv4Address().getValue();
274 InstanceIdentifier id = NeutronvpnUtils.buildFixedIpToPortNameIdentifier(ipValue);
275 PortFixedipToPortNameBuilder builder = new PortFixedipToPortNameBuilder().setPortFixedip(ipValue)
276 .setPortName(infName);
277 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, id, builder.build());
278 LOG.debug("fixedIp-name map for neutron port with fixedIp: {}, name: {} added to NeutronPortData DS",
280 subnetId = ip.getSubnetId();
281 subnetmap = nvpnManager.updateSubnetNode(subnetId, null, null, null, null, null, port.getUuid());
282 if (subnetmap != null) {
283 vpnId = subnetmap.getVpnId();
287 isLockAcquired = NeutronvpnUtils.lock(lockManager, lockName);
288 checkAndPublishPortAddNotification(subnetmap.getSubnetIp(), subnetId, port.getUuid());
289 LOG.debug("Port added to subnet notification sent");
290 } catch (Exception e) {
291 LOG.error("Port added to subnet notification failed", e);
293 if (isLockAcquired) {
294 NeutronvpnUtils.unlock(lockManager, lockName);
301 private Uuid removePortFromSubnets(Port port) {
302 Uuid subnetId = null;
304 Subnetmap subnetmap = null;
305 boolean isLockAcquired = false;
306 String lockName = port.getUuid().getValue();
308 // find the subnet to which this port is associated
309 FixedIps ip = port.getFixedIps().get(0);
310 String ipValue = ip.getIpAddress().getIpv4Address().getValue();
311 InstanceIdentifier id = NeutronvpnUtils.buildFixedIpToPortNameIdentifier(ipValue);
312 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, id);
313 LOG.debug("fixedIp-name map for neutron port with fixedIp: {} deleted from NeutronPortData DS", ipValue);
314 subnetId = ip.getSubnetId();
315 subnetmap = nvpnManager.removeFromSubnetNode(subnetId, null, null, null, port.getUuid());
316 if (subnetmap != null) {
317 vpnId = subnetmap.getVpnId();
321 isLockAcquired = NeutronvpnUtils.lock(lockManager, lockName);
322 checkAndPublishPortRemoveNotification(subnetmap.getSubnetIp(), subnetId, port.getUuid());
323 LOG.debug("Port removed from subnet notification sent");
324 } catch (Exception e) {
325 LOG.error("Port removed from subnet notification failed", e);
327 if (isLockAcquired) {
328 NeutronvpnUtils.unlock(lockManager, lockName);
335 private void checkAndPublishPortAddNotification(String subnetIp, Uuid subnetId, Uuid portId)throws InterruptedException{
336 PortAddedToSubnetBuilder builder = new PortAddedToSubnetBuilder();
338 LOG.info("publish notification called");
340 builder.setSubnetIp(subnetIp);
341 builder.setSubnetId(subnetId);
342 builder.setPortId(portId);
344 notificationPublishService.putNotification(builder.build());
347 private void checkAndPublishPortRemoveNotification(String subnetIp, Uuid subnetId, Uuid portId)throws InterruptedException{
348 PortRemovedFromSubnetBuilder builder = new PortRemovedFromSubnetBuilder();
350 LOG.info("publish notification called");
352 builder.setPortId(portId);
353 builder.setSubnetIp(subnetIp);
354 builder.setSubnetId(subnetId);
356 notificationPublishService.putNotification(builder.build());