Merge "Populating future object used by DataStoreJobCoordinator"
[netvirt.git] / vpnservice / elanmanager / elanmanager-impl / src / main / java / org / opendaylight / netvirt / 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
9 package org.opendaylight.netvirt.elan.internal;
10
11 import com.google.common.base.Optional;
12 import com.google.common.util.concurrent.ListenableFuture;
13
14 import java.util.ArrayList;
15 import java.util.List;
16
17 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
20 import org.opendaylight.genius.interfacemanager.globals.InterfaceInfo;
21 import org.opendaylight.netvirt.elan.utils.ElanConstants;
22 import org.opendaylight.netvirt.elan.utils.ElanUtils;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInstance;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanDpnInterfaces;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInstances;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesList;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesListKey;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.elan.dpn.interfaces.list.DpnInterfaces;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstanceKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterface;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.state.Elan;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 public class ElanInstanceManager extends AsyncDataTreeChangeListenerBase<ElanInstance, ElanInstanceManager>
38         implements AutoCloseable {
39
40     private ElanServiceProvider elanServiceProvider = null;
41     private static volatile ElanInstanceManager elanInstanceManager = null;
42     private static final Logger logger = LoggerFactory.getLogger(ElanInstanceManager.class);
43
44     private ElanInstanceManager(ElanServiceProvider elanServiceProvider) {
45         super(ElanInstance.class, ElanInstanceManager.class);
46         this.elanServiceProvider = elanServiceProvider;
47     }
48
49     public static ElanInstanceManager getElanInstanceManager(ElanServiceProvider elanServiceProvider) {
50         if (elanInstanceManager == null) {
51             synchronized (ElanInstanceManager.class) {
52                 if (elanInstanceManager == null) {
53                     elanInstanceManager = new ElanInstanceManager(elanServiceProvider);
54                 }
55             }
56         }
57         return elanInstanceManager;
58     }
59
60     @Override
61     protected void remove(InstanceIdentifier<ElanInstance> identifier, ElanInstance deletedElan) {
62         logger.trace("Remove ElanInstance - Key: {}, value: {}", identifier, deletedElan);
63         List<ListenableFuture<Void>> futures = new ArrayList<>();
64         String elanName = deletedElan.getElanInstanceName();
65         // check the elan Instance present in the Operational DataStore
66         Elan existingElan = ElanUtils.getElanByName(elanName);
67         long elanTag = deletedElan.getElanTag();
68         // Cleaning up the existing Elan Instance
69         if (existingElan != null) {
70             List<String> elanInterfaces = existingElan.getElanInterfaces();
71             if (elanInterfaces != null && !elanInterfaces.isEmpty()) {
72                 for (String elanInterfaceName : elanInterfaces) {
73                     InstanceIdentifier<ElanInterface> elanInterfaceId = ElanUtils
74                             .getElanInterfaceConfigurationDataPathId(elanInterfaceName);
75                     InterfaceInfo interfaceInfo = elanServiceProvider.getInterfaceManager()
76                             .getInterfaceInfo(elanInterfaceName);
77                     elanServiceProvider.getElanInterfaceManager().removeElanInterface(futures, deletedElan, elanInterfaceName,
78                             interfaceInfo, false);
79                     ElanUtils.delete(elanServiceProvider.getBroker(), LogicalDatastoreType.CONFIGURATION,
80                             elanInterfaceId);
81                 }
82             }
83             ElanUtils.delete(elanServiceProvider.getBroker(), LogicalDatastoreType.OPERATIONAL,
84                     ElanUtils.getElanInstanceOperationalDataPath(elanName));
85             ElanUtils.delete(elanServiceProvider.getBroker(), LogicalDatastoreType.OPERATIONAL,
86                     getElanDpnOperationDataPath(elanName));
87             ElanUtils.delete(elanServiceProvider.getBroker(), LogicalDatastoreType.OPERATIONAL,
88                     ElanUtils.getElanInfoEntriesOperationalDataPath(elanTag));
89         }
90         // Release tag
91         ElanUtils.releaseId(elanServiceProvider.getIdManager(), ElanConstants.ELAN_ID_POOL_NAME, elanName);
92         if (deletedElan.getAugmentation(EtreeInstance.class) != null) {
93             removeEtreeInstance(deletedElan);
94         }
95     }
96
97     private void removeEtreeInstance(ElanInstance deletedElan) {
98         // Release leaves tag
99         ElanUtils.releaseId(elanServiceProvider.getIdManager(), ElanConstants.ELAN_ID_POOL_NAME,
100                 deletedElan.getElanInstanceName() + ElanConstants.LEAVES_POSTFIX);
101
102         ElanUtils.delete(elanServiceProvider.getBroker(), LogicalDatastoreType.OPERATIONAL,
103                 ElanUtils.getElanInfoEntriesOperationalDataPath(
104                 deletedElan.getAugmentation(EtreeInstance.class).getEtreeLeafTagVal().getValue()));
105     }
106
107     @Override
108     protected void update(InstanceIdentifier<ElanInstance> identifier, ElanInstance original, ElanInstance update) {
109         Long existingElanTag = original.getElanTag();
110         if (existingElanTag != null && existingElanTag == update.getElanTag()) {
111             return;
112         } else if (update.getElanTag() == null) {
113             // update the elan-Instance with new properties
114             WriteTransaction tx = elanServiceProvider.getBroker().newWriteOnlyTransaction();
115             ElanUtils.updateOperationalDataStore(elanServiceProvider.getBroker(), elanServiceProvider.getIdManager(),
116                     update, new ArrayList<String>(), tx);
117             ElanUtils.waitForTransactionToComplete(tx);
118             return;
119         }
120         elanServiceProvider.getElanInterfaceManager().handleunprocessedElanInterfaces(update);
121     }
122
123     @Override
124     protected void add(InstanceIdentifier<ElanInstance> identifier, ElanInstance elanInstanceAdded) {
125         Elan elanInfo = ElanUtils.getElanByName(elanInstanceAdded.getElanInstanceName());
126         if (elanInfo == null) {
127             WriteTransaction tx = elanServiceProvider.getBroker().newWriteOnlyTransaction();
128             ElanUtils.updateOperationalDataStore(elanServiceProvider.getBroker(), elanServiceProvider.getIdManager(),
129                     elanInstanceAdded, new ArrayList<String>(), tx);
130             ElanUtils.waitForTransactionToComplete(tx);
131         }
132     }
133
134     public ElanInstance getElanInstanceByName(String elanInstanceName) {
135         InstanceIdentifier<ElanInstance> elanIdentifierId = getElanInstanceConfigurationDataPath(elanInstanceName);
136         Optional<ElanInstance> elanInstance = ElanUtils.read(elanServiceProvider.getBroker(),
137                 LogicalDatastoreType.CONFIGURATION, elanIdentifierId);
138         if (elanInstance.isPresent()) {
139             return elanInstance.get();
140         }
141         return null;
142     }
143
144     public List<DpnInterfaces> getElanDPNByName(String elanInstanceName) {
145         InstanceIdentifier<ElanDpnInterfacesList> elanIdentifier = getElanDpnOperationDataPath(elanInstanceName);
146         Optional<ElanDpnInterfacesList> elanInstance = ElanUtils.read(elanServiceProvider.getBroker(),
147                 LogicalDatastoreType.OPERATIONAL, elanIdentifier);
148         if (elanInstance.isPresent()) {
149             ElanDpnInterfacesList elanDPNs = elanInstance.get();
150             return elanDPNs.getDpnInterfaces();
151         }
152         return null;
153     }
154
155     private InstanceIdentifier<ElanDpnInterfacesList> getElanDpnOperationDataPath(String elanInstanceName) {
156         return InstanceIdentifier.builder(ElanDpnInterfaces.class)
157                 .child(ElanDpnInterfacesList.class, new ElanDpnInterfacesListKey(elanInstanceName)).build();
158     }
159
160     private InstanceIdentifier<ElanInstance> getElanInstanceConfigurationDataPath(String elanInstanceName) {
161         return InstanceIdentifier.builder(ElanInstances.class)
162                 .child(ElanInstance.class, new ElanInstanceKey(elanInstanceName)).build();
163     }
164
165     @Override
166     protected InstanceIdentifier<ElanInstance> getWildCardPath() {
167         return InstanceIdentifier.create(ElanInstances.class).child(ElanInstance.class);
168     }
169
170     @Override
171     protected ElanInstanceManager getDataTreeChangeListener() {
172         return this;
173     }
174
175 }