natservice: drop nullToEmpty and reqNonNullOrElse
[netvirt.git] / natservice / impl / src / main / java / org / opendaylight / netvirt / natservice / internal / NatInterfaceStateChangeListener.java
1 /*
2  * Copyright (c) 2016 - 2018 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.natservice.internal;
9
10 import com.google.common.base.Optional;
11 import java.math.BigInteger;
12 import javax.annotation.PostConstruct;
13 import javax.inject.Inject;
14 import javax.inject.Singleton;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
18 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
19 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterface;
20 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.vpn._interface.VpnInstanceNames;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev170119.L2vlan;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.router.interfaces.RouterInterface;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn._interface.op.data.VpnInterfaceOpDataEntry;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 @Singleton
31 public class NatInterfaceStateChangeListener
32     extends AsyncDataTreeChangeListenerBase<Interface, NatInterfaceStateChangeListener> {
33
34     private static final Logger LOG = LoggerFactory.getLogger(NatInterfaceStateChangeListener.class);
35     private final DataBroker dataBroker;
36     private final NatSouthboundEventHandlers southboundEventHandlers;
37
38     @Inject
39     public NatInterfaceStateChangeListener(final DataBroker dataBroker,
40                                            final NatSouthboundEventHandlers southboundEventHandlers) {
41         super(Interface.class, NatInterfaceStateChangeListener.class);
42         this.dataBroker = dataBroker;
43         this.southboundEventHandlers = southboundEventHandlers;
44     }
45
46     @Override
47     @PostConstruct
48     public void init() {
49         LOG.info("{} init", getClass().getSimpleName());
50         registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
51     }
52
53     @Override
54     protected InstanceIdentifier<Interface> getWildCardPath() {
55         return InstanceIdentifier.create(InterfacesState.class).child(Interface.class);
56     }
57
58     @Override
59     protected NatInterfaceStateChangeListener getDataTreeChangeListener() {
60         return NatInterfaceStateChangeListener.this;
61     }
62
63     @Override
64     // TODO Clean up the exception handling
65     @SuppressWarnings("checkstyle:IllegalCatch")
66     protected void add(InstanceIdentifier<Interface> identifier, Interface intrf) {
67         LOG.trace("add : Interface {} up event received", intrf);
68         if (!L2vlan.class.equals(intrf.getType())) {
69             LOG.debug("add : Interface {} is not Vlan Interface.Ignoring", intrf.getName());
70             return;
71         }
72         String interfaceName = intrf.getName();
73         BigInteger intfDpnId;
74         try {
75             intfDpnId = NatUtil.getDpIdFromInterface(intrf);
76         } catch (Exception e) {
77             LOG.error("add : Exception occured while retriving dpnid for interface {}", intrf.getName(), e);
78             return;
79         }
80         if (BigInteger.ZERO.equals(intfDpnId)) {
81             LOG.warn("add : Could not retrieve dp id for interface {} ", interfaceName);
82             return;
83         }
84         // We service only VM interfaces. We do not service Tunnel Interfaces here.
85         // Tunnel events are directly serviced by TunnelInterfacesStateListener present as part of
86         // VpnInterfaceManager
87         RouterInterface routerInterface = NatUtil.getConfiguredRouterInterface(dataBroker, interfaceName);
88         if (routerInterface != null) {
89             this.southboundEventHandlers.handleAdd(interfaceName, intfDpnId, routerInterface);
90         } else {
91             LOG.info("add : Router-Interface Mapping not found for Interface : {}", interfaceName);
92         }
93     }
94
95     @Override
96     // TODO Clean up the exception handling
97     @SuppressWarnings("checkstyle:IllegalCatch")
98     protected void remove(InstanceIdentifier<Interface> identifier, Interface intrf) {
99         LOG.trace("remove : Interface {} removed event received", intrf);
100         if (!L2vlan.class.equals(intrf.getType())) {
101             LOG.debug("remove : Interface {} is not Vlan Interface.Ignoring", intrf.getName());
102             return;
103         }
104         String interfaceName = intrf.getName();
105         BigInteger intfDpnId = BigInteger.ZERO;
106         try {
107             intfDpnId = NatUtil.getDpIdFromInterface(intrf);
108         } catch (Exception e) {
109             LOG.error("remove : Exception occured while retriving dpnid for interface {}",  intrf.getName(), e);
110             InstanceIdentifier<VpnInterface> id = NatUtil.getVpnInterfaceIdentifier(interfaceName);
111             Optional<VpnInterface> cfgVpnInterface =
112                     SingleTransactionDataBroker.syncReadOptionalAndTreatReadFailedExceptionAsAbsentOptional(
113                             dataBroker, LogicalDatastoreType.CONFIGURATION, id);
114             if (!cfgVpnInterface.isPresent()) {
115                 LOG.warn("remove : Interface {} is not a VPN Interface, ignoring.", interfaceName);
116                 return;
117             }
118             for (VpnInstanceNames vpnInterfaceVpnInstance : cfgVpnInterface.get().nonnullVpnInstanceNames()) {
119                 String vpnName  = vpnInterfaceVpnInstance.getVpnName();
120                 InstanceIdentifier<VpnInterfaceOpDataEntry> idOper = NatUtil
121                       .getVpnInterfaceOpDataEntryIdentifier(interfaceName, vpnName);
122                 Optional<VpnInterfaceOpDataEntry> optVpnInterface =
123                       SingleTransactionDataBroker.syncReadOptionalAndTreatReadFailedExceptionAsAbsentOptional(
124                             dataBroker, LogicalDatastoreType.OPERATIONAL, idOper);
125                 if (optVpnInterface.isPresent()) {
126                     intfDpnId = optVpnInterface.get().getDpnId();
127                     break;
128                 }
129             }
130         }
131         if (BigInteger.ZERO.equals(intfDpnId)) {
132             LOG.warn("remove : Could not retrieve dpnid for interface {} ", interfaceName);
133             return;
134         }
135         RouterInterface routerInterface = NatUtil.getConfiguredRouterInterface(dataBroker, interfaceName);
136         if (routerInterface != null) {
137             this.southboundEventHandlers.handleRemove(intrf.getName(), intfDpnId, routerInterface);
138         } else {
139             LOG.info("remove : Router-Interface Mapping not found for Interface : {}", interfaceName);
140         }
141     }
142
143     @Override
144     // TODO Clean up the exception handling
145     @SuppressWarnings("checkstyle:IllegalCatch")
146     protected void update(InstanceIdentifier<Interface> identifier, Interface original, Interface update) {
147         LOG.trace("update : Operation Interface update event - Old: {}, New: {}", original, update);
148         if (!L2vlan.class.equals(update.getType())) {
149             LOG.debug("update : Interface {} is not Vlan Interface.Ignoring", update.getName());
150             return;
151         }
152         BigInteger intfDpnId = BigInteger.ZERO;
153         String interfaceName = update.getName();
154         try {
155             intfDpnId = NatUtil.getDpIdFromInterface(update);
156         } catch (Exception e) {
157             LOG.error("update : Exception occured while retriving dpnid for interface {}",  update.getName(), e);
158             InstanceIdentifier<VpnInterface> id = NatUtil.getVpnInterfaceIdentifier(interfaceName);
159             Optional<VpnInterface> cfgVpnInterface =
160                     SingleTransactionDataBroker.syncReadOptionalAndTreatReadFailedExceptionAsAbsentOptional(
161                             dataBroker, LogicalDatastoreType.CONFIGURATION, id);
162             if (!cfgVpnInterface.isPresent()) {
163                 LOG.warn("update : Interface {} is not a VPN Interface, ignoring.", interfaceName);
164                 return;
165             }
166             for (VpnInstanceNames vpnInterfaceVpnInstance : cfgVpnInterface.get().nonnullVpnInstanceNames()) {
167                 String vpnName  = vpnInterfaceVpnInstance.getVpnName();
168                 InstanceIdentifier<VpnInterfaceOpDataEntry> idOper = NatUtil
169                       .getVpnInterfaceOpDataEntryIdentifier(interfaceName, vpnName);
170                 Optional<VpnInterfaceOpDataEntry> optVpnInterface =
171                       SingleTransactionDataBroker.syncReadOptionalAndTreatReadFailedExceptionAsAbsentOptional(
172                             dataBroker, LogicalDatastoreType.OPERATIONAL, idOper);
173                 if (optVpnInterface.isPresent()) {
174                     intfDpnId = optVpnInterface.get().getDpnId();
175                     break;
176                 }
177             }
178         }
179         if (BigInteger.ZERO.equals(intfDpnId)) {
180             LOG.warn("remove : Could not retrieve dpnid for interface {} ", interfaceName);
181             return;
182         }
183         RouterInterface routerInterface = NatUtil.getConfiguredRouterInterface(dataBroker, interfaceName);
184         if (routerInterface != null) {
185             this.southboundEventHandlers.handleUpdate(original, update, intfDpnId, routerInterface);
186         } else {
187             LOG.info("update : Router-Interface Mapping not found for Interface : {}", interfaceName);
188         }
189     }
190 }