2 * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
9 package org.opendaylight.netvirt.elan.internal;
11 import com.google.common.base.Optional;
12 import com.google.common.util.concurrent.ListenableFuture;
14 import java.util.ArrayList;
15 import java.util.List;
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;
37 public class ElanInstanceManager extends AsyncDataTreeChangeListenerBase<ElanInstance, ElanInstanceManager>
38 implements AutoCloseable {
40 private ElanServiceProvider elanServiceProvider = null;
41 private static volatile ElanInstanceManager elanInstanceManager = null;
42 private static final Logger logger = LoggerFactory.getLogger(ElanInstanceManager.class);
44 private ElanInstanceManager(ElanServiceProvider elanServiceProvider) {
45 super(ElanInstance.class, ElanInstanceManager.class);
46 this.elanServiceProvider = elanServiceProvider;
49 public static ElanInstanceManager getElanInstanceManager(ElanServiceProvider elanServiceProvider) {
50 if (elanInstanceManager == null) {
51 synchronized (ElanInstanceManager.class) {
52 if (elanInstanceManager == null) {
53 elanInstanceManager = new ElanInstanceManager(elanServiceProvider);
57 return elanInstanceManager;
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,
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));
91 ElanUtils.releaseId(elanServiceProvider.getIdManager(), ElanConstants.ELAN_ID_POOL_NAME, elanName);
92 if (deletedElan.getAugmentation(EtreeInstance.class) != null) {
93 removeEtreeInstance(deletedElan);
97 private void removeEtreeInstance(ElanInstance deletedElan) {
99 ElanUtils.releaseId(elanServiceProvider.getIdManager(), ElanConstants.ELAN_ID_POOL_NAME,
100 deletedElan.getElanInstanceName() + ElanConstants.LEAVES_POSTFIX);
102 ElanUtils.delete(elanServiceProvider.getBroker(), LogicalDatastoreType.OPERATIONAL,
103 ElanUtils.getElanInfoEntriesOperationalDataPath(
104 deletedElan.getAugmentation(EtreeInstance.class).getEtreeLeafTagVal().getValue()));
108 protected void update(InstanceIdentifier<ElanInstance> identifier, ElanInstance original, ElanInstance update) {
109 Long existingElanTag = original.getElanTag();
110 if (existingElanTag != null && existingElanTag == update.getElanTag()) {
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);
120 elanServiceProvider.getElanInterfaceManager().handleunprocessedElanInterfaces(update);
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);
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();
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();
155 private InstanceIdentifier<ElanDpnInterfacesList> getElanDpnOperationDataPath(String elanInstanceName) {
156 return InstanceIdentifier.builder(ElanDpnInterfaces.class)
157 .child(ElanDpnInterfacesList.class, new ElanDpnInterfacesListKey(elanInstanceName)).build();
160 private InstanceIdentifier<ElanInstance> getElanInstanceConfigurationDataPath(String elanInstanceName) {
161 return InstanceIdentifier.builder(ElanInstances.class)
162 .child(ElanInstance.class, new ElanInstanceKey(elanInstanceName)).build();
166 protected InstanceIdentifier<ElanInstance> getWildCardPath() {
167 return InstanceIdentifier.create(ElanInstances.class).child(ElanInstance.class);
171 protected ElanInstanceManager getDataTreeChangeListener() {