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