fix blue print to init InterfaceStateToTansportZoneListener
[netvirt.git] / vpnservice / neutronvpn / neutronvpn-impl / src / main / java / org / opendaylight / netvirt / neutronvpn / InterfaceStateToTransportZoneListener.java
1 /*
2  * Copyright (c) 2015 - 2016 HPE 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.neutronvpn;
9
10 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
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.common.api.data.LogicalDatastoreType;
14 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
15 import org.opendaylight.genius.mdsalutil.MDSALDataStoreUtils;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.config.rev160806.NeutronvpnConfig;
19 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 import com.google.common.base.Optional;
24
25 public class InterfaceStateToTransportZoneListener extends AsyncDataTreeChangeListenerBase<Interface, InterfaceStateToTransportZoneListener> implements ClusteredDataTreeChangeListener<Interface>, AutoCloseable{
26
27     private static final Logger LOG = LoggerFactory.getLogger(InterfaceStateToTransportZoneListener.class);
28     private InterfaceStateManager ism;
29
30     public InterfaceStateToTransportZoneListener(DataBroker dbx, NeutronvpnManager nvManager) {
31         super(Interface.class, InterfaceStateToTransportZoneListener.class);
32         ism = new InterfaceStateManager(dbx, nvManager);
33         Optional<NeutronvpnConfig> nvsConfig = MDSALDataStoreUtils.read(dbx,
34                 LogicalDatastoreType.CONFIGURATION, InstanceIdentifier
35                 .create(NeutronvpnConfig.class));
36         Boolean useTZ = true;
37         if (nvsConfig.isPresent()) {
38             useTZ = nvsConfig.get().isUseTransportZone() == null ? true : nvsConfig.get().isUseTransportZone();
39         }
40         if (isAutoTunnelConfigEnabled(useTZ)) {
41             registerListener(LogicalDatastoreType.OPERATIONAL, dbx);
42         }       
43
44     }
45
46     @Override
47     protected InstanceIdentifier<Interface> getWildCardPath() {
48         return InstanceIdentifier.create(InterfacesState.class).child(Interface.class);
49     }
50
51
52     @Override
53     protected void remove(InstanceIdentifier<Interface> identifier, Interface del) {
54         // FIXME: once the TZ is declared it will stay forever
55
56     }
57
58     @Override
59     protected void update(InstanceIdentifier<Interface> identifier, Interface original, Interface update) {
60         ism.updateTrasportZone(update);
61     }
62
63
64     @Override
65     protected void add(InstanceIdentifier<Interface> identifier, Interface add) {
66         ism.updateTrasportZone(add);
67     }
68     
69     @Override
70     protected InterfaceStateToTransportZoneListener getDataTreeChangeListener() {
71         return InterfaceStateToTransportZoneListener.this;
72     }
73     
74     private boolean isAutoTunnelConfigEnabled(Boolean useTZ) {
75         if (useTZ) {
76             LOG.info("using automatic tunnel configuration");
77         } else {
78             LOG.info("don't use automatic tunnel configuration");
79         }
80         return useTZ;
81     }
82
83 }