Bump versions by 0.1.0 for next dev cycle
[vpnservice.git] / elanmanager / elanmanager-impl / src / main / java / org / opendaylight / vpnservice / elan / internal / ElanInstanceManager.java
1 /*
2  * Copyright (c) 2016 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.vpnservice.elan.internal;
9
10 import com.google.common.base.Optional;
11
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
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.vpnservice.interfacemgr.globals.InterfaceInfo;
17 import org.opendaylight.vpnservice.interfacemgr.interfaces.IInterfaceManager;
18 import org.opendaylight.vpnservice.mdsalutil.AbstractDataChangeListener;
19 import org.opendaylight.vpnservice.elan.utils.ElanConstants;
20 import org.opendaylight.vpnservice.elan.utils.ElanUtils;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.ElanDpnInterfaces;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.ElanInstances;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesList;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesListKey;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.elan.dpn.interfaces.elan.dpn.interfaces.list.DpnInterfaces;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.elan.instances.ElanInstance;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.elan.instances.ElanInstanceKey;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.elan.interfaces.ElanInterface;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.elan.state.Elan;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.IdManagerService;
31 import org.opendaylight.yangtools.concepts.ListenerRegistration;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 import java.util.List;
37
38 public class ElanInstanceManager extends AbstractDataChangeListener<ElanInstance> implements AutoCloseable {
39     private DataBroker broker;
40     private static ElanInstanceManager elanInstanceManager = new ElanInstanceManager();
41     private ListenerRegistration<DataChangeListener> elanInstanceListenerRegistration;
42     private IdManagerService idManager;
43     private ElanInterfaceManager elanInterfaceManager;
44     private IInterfaceManager interfaceManager;
45
46     private static final Logger logger = LoggerFactory.getLogger(ElanInstanceManager.class);
47
48     private ElanInstanceManager() {
49         super(ElanInstance.class);
50
51     }
52
53     public static ElanInstanceManager getElanInstanceManager() {
54         return elanInstanceManager;
55     }
56
57     public void setIdManager(IdManagerService idManager) {
58         this.idManager = idManager;
59     }
60
61     public void setDataBroker(DataBroker broker) {
62         this.broker = broker;
63     }
64
65     public void setElanInterfaceManager(ElanInterfaceManager elanInterfaceManager) {
66         this.elanInterfaceManager = elanInterfaceManager;
67     }
68
69     public void setInterfaceManager(IInterfaceManager interfaceManager) {
70         this.interfaceManager = interfaceManager;
71     }
72
73
74     /**
75      * Starts listening for changes in elan.yang:elan-instance container
76      */
77     public void registerListener() {
78         try {
79             elanInstanceListenerRegistration = broker.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
80                     getElanInstanceWildcardPath(), ElanInstanceManager.this, DataChangeScope.SUBTREE);
81         } catch (final Exception e) {
82             logger.error("ELAN Instance DataChange listener registration failed !", e);
83             throw new IllegalStateException("ELAN Instance registration Listener failed.", e);
84         }
85     }
86
87     private InstanceIdentifier<?> getElanInstanceWildcardPath() {
88         return InstanceIdentifier.create(ElanInstances.class).child(ElanInstance.class);
89     }
90
91     @Override
92     public void close() throws Exception {
93         if (elanInstanceListenerRegistration != null) {
94             elanInstanceListenerRegistration.close();
95         }
96     }
97
98     @Override
99     protected void remove(InstanceIdentifier<ElanInstance> identifier, ElanInstance deletedElan) {
100         logger.trace("Remove ElanInstance - Key: {}, value: {}", identifier, deletedElan);
101         String elanName = deletedElan.getElanInstanceName();
102         //check the elan Instance present in the Operational DataStore
103         Elan existingElan = ElanUtils.getElanByName(elanName);
104         long elanTag = deletedElan.getElanTag();
105         //Cleaning up the existing Elan Instance
106         if(existingElan != null) {
107             List<String> elanInterfaces =  existingElan.getElanInterfaces();
108             if(elanInterfaces != null && !elanInterfaces.isEmpty()) {
109                 for (String elanInterfaceName : elanInterfaces) {
110                     InstanceIdentifier<ElanInterface> elanInterfaceId = ElanUtils.getElanInterfaceConfigurationDataPathId(elanInterfaceName);
111                     InterfaceInfo interfaceInfo = interfaceManager.getInterfaceInfo(elanInterfaceName);
112                     elanInterfaceManager.removeElanInterface(deletedElan, elanInterfaceName, interfaceInfo);
113                     ElanUtils.delete(broker, LogicalDatastoreType.CONFIGURATION, elanInterfaceId);
114                 }
115             }
116             ElanUtils.delete(broker, LogicalDatastoreType.OPERATIONAL, ElanUtils.getElanInstanceOperationalDataPath(elanName));
117             ElanUtils.delete(broker, LogicalDatastoreType.OPERATIONAL, getElanDpnOperationDataPath(elanName));
118             ElanUtils.delete(broker, LogicalDatastoreType.OPERATIONAL, ElanUtils.getElanInfoEntriesOperationalDataPath(elanTag));
119         }
120         // Release tag
121         ElanUtils.releaseId(idManager, ElanConstants.ELAN_ID_POOL_NAME, elanName);
122
123     }
124
125     @Override
126     protected void update(InstanceIdentifier<ElanInstance> identifier, ElanInstance original, ElanInstance update) {
127         Long existingElanTag = original.getElanTag();
128         if (existingElanTag != null && existingElanTag == update.getElanTag()) {
129             return;
130         } else if (update.getElanTag() == null) {
131             // update the elan-Instance with new properties
132             ElanUtils.updateOperationalDataStore(broker, idManager, update);
133             return;
134         }
135         elanInterfaceManager.handleunprocessedElanInterfaces(update);
136     }
137
138     @Override
139     protected void add(InstanceIdentifier<ElanInstance> identifier, ElanInstance elanInstanceAdded) {
140         Elan elanInfo = ElanUtils.getElanByName(elanInstanceAdded.getElanInstanceName());
141         if(elanInfo == null) {
142             ElanUtils.updateOperationalDataStore(broker, idManager, elanInstanceAdded);
143         }
144     }
145
146     public ElanInstance getElanInstanceByName(String elanInstanceName) {
147         InstanceIdentifier<ElanInstance> elanIdentifierId = getElanInstanceConfigurationDataPath(elanInstanceName);
148         Optional<ElanInstance> elanInstance = ElanUtils.read(broker, LogicalDatastoreType.CONFIGURATION, elanIdentifierId);
149         if(elanInstance.isPresent()) {
150             return elanInstance.get();
151         }
152         return null;
153     }
154
155     public List<DpnInterfaces> getElanDPNByName(String elanInstanceName) {
156         InstanceIdentifier<ElanDpnInterfacesList> elanIdentifier = getElanDpnOperationDataPath(elanInstanceName);
157         Optional<ElanDpnInterfacesList> elanInstance = ElanUtils.read(broker, LogicalDatastoreType.OPERATIONAL, elanIdentifier);
158         if(elanInstance.isPresent()) {
159             ElanDpnInterfacesList elanDPNs =  elanInstance.get();
160             return elanDPNs.getDpnInterfaces();
161         }
162         return null;
163     }
164
165     private InstanceIdentifier<ElanDpnInterfacesList> getElanDpnOperationDataPath(String elanInstanceName) {
166         return InstanceIdentifier.builder(ElanDpnInterfaces.class).child(ElanDpnInterfacesList.class, new ElanDpnInterfacesListKey(elanInstanceName)).build();
167     }
168
169     private InstanceIdentifier<ElanInstance> getElanInstanceConfigurationDataPath(String elanInstanceName) {
170         return InstanceIdentifier.builder(ElanInstances.class).child(ElanInstance.class, new ElanInstanceKey(elanInstanceName)).build();
171     }
172 }