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 java.util.ArrayList;
12 import java.util.List;
14 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
17 import org.opendaylight.netvirt.elan.utils.ElanConstants;
18 import org.opendaylight.netvirt.elan.utils.ElanUtils;
19 import org.opendaylight.genius.interfacemanager.globals.InterfaceInfo;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanDpnInterfaces;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInstances;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesList;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesListKey;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.elan.dpn.interfaces.list.DpnInterfaces;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstanceKey;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterface;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.state.Elan;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 import com.google.common.base.Optional;
35 public class ElanInstanceManager extends AsyncDataTreeChangeListenerBase<ElanInstance,ElanInstanceManager> implements AutoCloseable {
37 private ElanServiceProvider elanServiceProvider = null;
38 private static volatile ElanInstanceManager elanInstanceManager = null;
40 private static final Logger logger = LoggerFactory.getLogger(ElanInstanceManager.class);
42 private ElanInstanceManager(ElanServiceProvider elanServiceProvider) {
43 super(ElanInstance.class,ElanInstanceManager.class);
44 this.elanServiceProvider = elanServiceProvider;
47 public static ElanInstanceManager getElanInstanceManager(ElanServiceProvider elanServiceProvider) {
48 if (elanInstanceManager == null) {
49 synchronized (ElanInstanceManager.class) {
50 if (elanInstanceManager == null) {
51 elanInstanceManager = new ElanInstanceManager(elanServiceProvider);
55 return elanInstanceManager;
59 protected void remove(InstanceIdentifier<ElanInstance> identifier, ElanInstance deletedElan) {
60 logger.trace("Remove ElanInstance - Key: {}, value: {}", identifier, deletedElan);
61 String elanName = deletedElan.getElanInstanceName();
62 //check the elan Instance present in the Operational DataStore
63 Elan existingElan = ElanUtils.getElanByName(elanName);
64 long elanTag = deletedElan.getElanTag();
65 //Cleaning up the existing Elan Instance
66 if (existingElan != null) {
67 List<String> elanInterfaces = existingElan.getElanInterfaces();
68 if (elanInterfaces != null && !elanInterfaces.isEmpty()) {
69 for (String elanInterfaceName : elanInterfaces) {
70 InstanceIdentifier<ElanInterface> elanInterfaceId = ElanUtils.getElanInterfaceConfigurationDataPathId(elanInterfaceName);
71 InterfaceInfo interfaceInfo = elanServiceProvider.getInterfaceManager().getInterfaceInfo(elanInterfaceName);
72 elanServiceProvider.getElanInterfaceManager().removeElanInterface(deletedElan, elanInterfaceName, interfaceInfo, false);
73 ElanUtils.delete(elanServiceProvider.getBroker(), LogicalDatastoreType.CONFIGURATION, elanInterfaceId);
76 ElanUtils.delete(elanServiceProvider.getBroker(), LogicalDatastoreType.OPERATIONAL, ElanUtils.getElanInstanceOperationalDataPath(elanName));
77 ElanUtils.delete(elanServiceProvider.getBroker(), LogicalDatastoreType.OPERATIONAL, getElanDpnOperationDataPath(elanName));
78 ElanUtils.delete(elanServiceProvider.getBroker(), LogicalDatastoreType.OPERATIONAL, ElanUtils.getElanInfoEntriesOperationalDataPath(elanTag));
81 ElanUtils.releaseId( elanServiceProvider.getIdManager(), ElanConstants.ELAN_ID_POOL_NAME, elanName);
86 protected void update(InstanceIdentifier<ElanInstance> identifier, ElanInstance original, ElanInstance update) {
87 Long existingElanTag = original.getElanTag();
88 if (existingElanTag != null && existingElanTag == update.getElanTag()) {
90 } else if (update.getElanTag() == null) {
91 // update the elan-Instance with new properties
92 WriteTransaction tx = elanServiceProvider.getBroker().newWriteOnlyTransaction();
93 ElanUtils.updateOperationalDataStore(elanServiceProvider.getBroker(), elanServiceProvider.getIdManager(),
94 update, new ArrayList<String>(), tx);
95 ElanUtils.waitForTransactionToComplete(tx);
98 elanServiceProvider.getElanInterfaceManager().handleunprocessedElanInterfaces(update);
102 protected void add(InstanceIdentifier<ElanInstance> identifier, ElanInstance elanInstanceAdded) {
103 Elan elanInfo = ElanUtils.getElanByName(elanInstanceAdded.getElanInstanceName());
104 if (elanInfo == null) {
105 WriteTransaction tx = elanServiceProvider.getBroker().newWriteOnlyTransaction();
106 ElanUtils.updateOperationalDataStore(elanServiceProvider.getBroker(), elanServiceProvider.getIdManager(), elanInstanceAdded,
107 new ArrayList<String>(), tx);
108 ElanUtils.waitForTransactionToComplete(tx);
112 public ElanInstance getElanInstanceByName(String elanInstanceName) {
113 InstanceIdentifier<ElanInstance> elanIdentifierId = getElanInstanceConfigurationDataPath(elanInstanceName);
114 Optional<ElanInstance> elanInstance = ElanUtils.read(elanServiceProvider.getBroker(), LogicalDatastoreType.CONFIGURATION, elanIdentifierId);
115 if (elanInstance.isPresent()) {
116 return elanInstance.get();
121 public List<DpnInterfaces> getElanDPNByName(String elanInstanceName) {
122 InstanceIdentifier<ElanDpnInterfacesList> elanIdentifier = getElanDpnOperationDataPath(elanInstanceName);
123 Optional<ElanDpnInterfacesList> elanInstance = ElanUtils.read(elanServiceProvider.getBroker(), LogicalDatastoreType.OPERATIONAL, elanIdentifier);
124 if (elanInstance.isPresent()) {
125 ElanDpnInterfacesList elanDPNs = elanInstance.get();
126 return elanDPNs.getDpnInterfaces();
131 private InstanceIdentifier<ElanDpnInterfacesList> getElanDpnOperationDataPath(String elanInstanceName) {
132 return InstanceIdentifier.builder(ElanDpnInterfaces.class).child(ElanDpnInterfacesList.class, new ElanDpnInterfacesListKey(elanInstanceName)).build();
135 private InstanceIdentifier<ElanInstance> getElanInstanceConfigurationDataPath(String elanInstanceName) {
136 return InstanceIdentifier.builder(ElanInstances.class).child(ElanInstance.class, new ElanInstanceKey(elanInstanceName)).build();
140 protected InstanceIdentifier<ElanInstance> getWildCardPath() {
141 return InstanceIdentifier.create(ElanInstances.class).child(ElanInstance.class);
145 protected ElanInstanceManager getDataTreeChangeListener() {