Unsupported operation exception handling
[netvirt.git] / dhcpservice / impl / src / main / java / org / opendaylight / netvirt / dhcpservice / DhcpInterfaceEventListener.java
1 /*
2  * Copyright (c) 2016, 2017 Ericsson India Global Services Pvt Ltd. 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.dhcpservice;
9
10 import java.util.List;
11 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
12 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
13 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
14 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
15 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
16 import org.opendaylight.netvirt.dhcpservice.api.DhcpMConstants;
17 import org.opendaylight.netvirt.dhcpservice.jobs.DhcpInterfaceAddJob;
18 import org.opendaylight.netvirt.dhcpservice.jobs.DhcpInterfaceRemoveJob;
19 import org.opendaylight.netvirt.dhcpservice.jobs.DhcpInterfaceUpdateJob;
20 import org.opendaylight.netvirt.elanmanager.api.IElanService;
21 import org.opendaylight.netvirt.neutronvpn.api.utils.NeutronConstants;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev170119.L2vlan;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev170119.Tunnel;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.ItmRpcService;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.opendaylight.yangtools.yang.common.Uint64;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public class DhcpInterfaceEventListener
36         extends AsyncDataTreeChangeListenerBase<Interface, DhcpInterfaceEventListener> {
37
38     private static final Logger LOG = LoggerFactory.getLogger(DhcpInterfaceEventListener.class);
39
40     private final DataBroker dataBroker;
41     private final DhcpManager dhcpManager;
42     private final DhcpExternalTunnelManager dhcpExternalTunnelManager;
43     private final JobCoordinator jobCoordinator;
44     private final IInterfaceManager interfaceManager;
45     private final IElanService elanService;
46     private final DhcpPortCache dhcpPortCache;
47     private final ItmRpcService itmRpcService;
48
49     public DhcpInterfaceEventListener(DhcpManager dhcpManager, DataBroker dataBroker,
50                                       DhcpExternalTunnelManager dhcpExternalTunnelManager,
51                                       IInterfaceManager interfaceManager, IElanService elanService,
52                                       DhcpPortCache dhcpPortCache, JobCoordinator jobCoordinator,
53                                       ItmRpcService itmRpcService) {
54         super(Interface.class, DhcpInterfaceEventListener.class);
55         this.dhcpManager = dhcpManager;
56         this.dataBroker = dataBroker;
57         this.dhcpExternalTunnelManager = dhcpExternalTunnelManager;
58         this.interfaceManager = interfaceManager;
59         this.elanService = elanService;
60         this.dhcpPortCache = dhcpPortCache;
61         this.jobCoordinator = jobCoordinator;
62         this.itmRpcService = itmRpcService;
63         registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
64     }
65
66     @Override
67     public void close() {
68         super.close();
69         LOG.info("DhcpInterfaceEventListener Closed");
70     }
71
72     @Override
73     protected void remove(InstanceIdentifier<Interface> identifier, Interface del) {
74         if (!L2vlan.class.equals(del.getType()) && !Tunnel.class.equals(del.getType())) {
75             return;
76         }
77         List<String> ofportIds = del.getLowerLayerIf();
78         if (ofportIds == null || ofportIds.isEmpty()) {
79             return;
80         }
81         String interfaceName = del.getName();
82         Port port = dhcpPortCache.get(interfaceName);
83         if (NeutronConstants.IS_DHCP_PORT.test(port)) {
84             return;
85         }
86         NodeConnectorId nodeConnectorId = new NodeConnectorId(ofportIds.get(0));
87         Uint64 dpnId = DhcpServiceUtils.getDpnIdFromNodeConnectorId(nodeConnectorId);
88
89         DhcpInterfaceRemoveJob job = new DhcpInterfaceRemoveJob(dhcpManager, dhcpExternalTunnelManager,
90                 dataBroker, del, dpnId, interfaceManager, elanService, port);
91         jobCoordinator.enqueueJob(DhcpServiceUtils.getJobKey(interfaceName), job, DhcpMConstants.RETRY_COUNT);
92         dhcpPortCache.remove(interfaceName);
93     }
94
95     @Override
96     protected void update(InstanceIdentifier<Interface> identifier,
97             Interface original, Interface update) {
98         // We're only interested in Vlan and Tunnel ports
99         if (!L2vlan.class.equals(update.getType()) && !Tunnel.class.equals(update.getType())) {
100             return;
101         }
102         if ((original.getOperStatus().getIntValue() ^ update.getOperStatus().getIntValue()) == 0) {
103             LOG.trace("Interface operstatus is same orig {} updated {}", original, update);
104             return;
105         }
106         List<String> ofportIds = update.getLowerLayerIf();
107         if (ofportIds == null || ofportIds.isEmpty()) {
108             return;
109         }
110         NodeConnectorId nodeConnectorId = new NodeConnectorId(ofportIds.get(0));
111         Uint64 dpnId = DhcpServiceUtils.getDpnIdFromNodeConnectorId(nodeConnectorId);
112         String interfaceName = update.getName();
113         OperStatus updatedOperStatus = update.getOperStatus();
114         if (original.getOperStatus().equals(OperStatus.Up) && updatedOperStatus.equals(OperStatus.Unknown)) {
115             updatedOperStatus = OperStatus.Down;
116         }
117         DhcpInterfaceUpdateJob job = new DhcpInterfaceUpdateJob(dhcpExternalTunnelManager, dataBroker,
118                 interfaceName, dpnId, updatedOperStatus, interfaceManager);
119         jobCoordinator.enqueueJob(DhcpServiceUtils.getJobKey(interfaceName), job, DhcpMConstants.RETRY_COUNT);
120     }
121
122     @Override
123     protected void add(InstanceIdentifier<Interface> identifier, Interface add) {
124         // We're only interested in Vlan and Tunnel ports
125         if (!L2vlan.class.equals(add.getType()) && !Tunnel.class.equals(add.getType())) {
126             return;
127         }
128         String interfaceName = add.getName();
129         LOG.trace("DhcpInterfaceAddJob to be created for interface {}", interfaceName);
130         List<String> ofportIds = add.getLowerLayerIf();
131         if (ofportIds == null || ofportIds.isEmpty()) {
132             return;
133         }
134         if (!Tunnel.class.equals(add.getType())) {
135             Port port = dhcpManager.getNeutronPort(interfaceName);
136             if (NeutronConstants.IS_DHCP_PORT.test(port)) {
137                 return;
138             }
139             dhcpPortCache.put(interfaceName, port);
140         }
141         NodeConnectorId nodeConnectorId = new NodeConnectorId(ofportIds.get(0));
142         Uint64 dpnId = DhcpServiceUtils.getDpnIdFromNodeConnectorId(nodeConnectorId);
143         DhcpInterfaceAddJob job = new DhcpInterfaceAddJob(dhcpManager, dhcpExternalTunnelManager, dataBroker,
144                 add, dpnId, interfaceManager, elanService, itmRpcService);
145         jobCoordinator.enqueueJob(DhcpServiceUtils.getJobKey(interfaceName), job, DhcpMConstants.RETRY_COUNT);
146     }
147
148     @Override
149     protected InstanceIdentifier<Interface> getWildCardPath() {
150         return InstanceIdentifier.create(InterfacesState.class).child(Interface.class);
151     }
152
153     @Override
154     protected DhcpInterfaceEventListener getDataTreeChangeListener() {
155         return DhcpInterfaceEventListener.this;
156     }
157 }