2 * Copyright (c) 2016 Red Hat, Inc. 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.netvirt.ipv6service;
10 import java.util.List;
11 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
12 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
13 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
14 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.genius.mdsalutil.AbstractDataChangeListener;
17 import org.opendaylight.netvirt.ipv6service.utils.Ipv6Constants;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.port.attributes.FixedIps;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.Ports;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
23 import org.opendaylight.yangtools.concepts.ListenerRegistration;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 public class NeutronPortChangeListener extends AbstractDataChangeListener<Port> implements AutoCloseable {
29 private static final Logger LOG = LoggerFactory.getLogger(NeutronPortChangeListener.class);
31 private ListenerRegistration<DataChangeListener> listenerRegistration;
33 private NotificationService notificationService;
35 public NeutronPortChangeListener(final DataBroker db) {
37 this.ifMgr = IfMgr.getIfMgrInstance();
42 public void close() throws Exception {
43 if (listenerRegistration != null) {
44 listenerRegistration.close();
45 listenerRegistration = null;
47 LOG.info("Neutron Port listener Closed");
51 private void registerListener(final DataBroker db) {
52 listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
53 InstanceIdentifier.create(Neutron.class).child(Ports.class).child(Port.class),
54 NeutronPortChangeListener.this, DataChangeScope.SUBTREE);
58 protected void add(InstanceIdentifier<Port> identifier, Port port) {
59 LOG.info("Add port notification handler is invoked...");
60 List<FixedIps> ipList = port.getFixedIps();
62 for (FixedIps fixedip : ipList) {
63 if (port.getDeviceOwner().equalsIgnoreCase(Ipv6Constants.NETWORK_ROUTER_INTERFACE)) {
65 // Add router interface
66 ifMgr.addRouterIntf(port.getUuid(),
67 new Uuid(port.getDeviceId()),
68 fixedip.getSubnetId(),
70 fixedip.getIpAddress(),
71 port.getMacAddress().getValue(),
72 port.getDeviceOwner());
75 ifMgr.addHostIntf(port.getUuid(),
76 fixedip.getSubnetId(),
78 fixedip.getIpAddress(),
79 port.getMacAddress().getValue(),
80 port.getDeviceOwner());
86 protected void remove(InstanceIdentifier<Port> identifier, Port port) {
87 LOG.info("remove port notification handler is invoked...");
88 ifMgr.removePort(port.getUuid());
92 protected void update(InstanceIdentifier<Port> identifier, Port original, Port update) {
93 LOG.info("update port notification handler is invoked...");
94 if (update.getDeviceOwner().equalsIgnoreCase(Ipv6Constants.NETWORK_ROUTER_INTERFACE)) {
95 ifMgr.updateRouterIntf(update.getUuid(), new Uuid(update.getDeviceId()), update.getFixedIps());
97 ifMgr.updateHostIntf(update.getUuid(), update.getFixedIps());