Support Neighbor Advertisement functionality for router iface
[netvirt.git] / vpnservice / ipv6service / impl / src / main / java / org / opendaylight / netvirt / ipv6service / NeutronPortChangeListener.java
1 /*
2  * Copyright (c) 2016 Red Hat, Inc. 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.netvirt.ipv6service;
9
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;
27
28 public class NeutronPortChangeListener extends AbstractDataChangeListener<Port> implements AutoCloseable {
29     private static final Logger LOG = LoggerFactory.getLogger(NeutronPortChangeListener.class);
30
31     private ListenerRegistration<DataChangeListener> listenerRegistration;
32     private IfMgr ifMgr;
33     private NotificationService notificationService;
34
35     public NeutronPortChangeListener(final DataBroker db) {
36         super(Port.class);
37         this.ifMgr = IfMgr.getIfMgrInstance();
38         registerListener(db);
39     }
40
41     @Override
42     public void close() throws Exception {
43         if (listenerRegistration != null) {
44             listenerRegistration.close();
45             listenerRegistration = null;
46         }
47         LOG.info("Neutron Port listener Closed");
48     }
49
50
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);
55     }
56
57     @Override
58     protected void add(InstanceIdentifier<Port> identifier, Port port) {
59         LOG.info("Add port notification handler is invoked...");
60         List<FixedIps> ipList = port.getFixedIps();
61
62         for (FixedIps fixedip : ipList) {
63             if (port.getDeviceOwner().equalsIgnoreCase(Ipv6Constants.NETWORK_ROUTER_INTERFACE)) {
64
65                 // Add router interface
66                 ifMgr.addRouterIntf(port.getUuid(),
67                         new Uuid(port.getDeviceId()),
68                         fixedip.getSubnetId(),
69                         port.getNetworkId(),
70                         fixedip.getIpAddress(),
71                         port.getMacAddress().getValue(),
72                         port.getDeviceOwner());
73             } else {
74                 // Add host interface
75                 ifMgr.addHostIntf(port.getUuid(),
76                         fixedip.getSubnetId(),
77                         port.getNetworkId(),
78                         fixedip.getIpAddress(),
79                         port.getMacAddress().getValue(),
80                         port.getDeviceOwner());
81             }
82         }
83     }
84
85     @Override
86     protected void remove(InstanceIdentifier<Port> identifier, Port port) {
87         LOG.info("remove port notification handler is invoked...");
88         ifMgr.removePort(port.getUuid());
89     }
90
91     @Override
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());
96         } else {
97             ifMgr.updateHostIntf(update.getUuid(), update.getFixedIps());
98         }
99     }
100 }