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;
13 import java.util.ArrayList;
14 import java.util.Collections;
15 import java.util.List;
16 import javax.annotation.PostConstruct;
17 import javax.inject.Inject;
18 import javax.inject.Singleton;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
23 import org.opendaylight.genius.interfacemanager.globals.InterfaceInfo;
24 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
25 import org.opendaylight.genius.mdsalutil.MDSALUtil;
26 import org.opendaylight.netvirt.elan.ElanException;
27 import org.opendaylight.netvirt.elan.utils.ElanConstants;
28 import org.opendaylight.netvirt.elan.utils.ElanUtils;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInstance;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanDpnInterfaces;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInstances;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesList;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesListKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.elan.dpn.interfaces.list.DpnInterfaces;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstanceKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterface;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.state.Elan;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 public class ElanInstanceManager extends AsyncDataTreeChangeListenerBase<ElanInstance, ElanInstanceManager>
46 implements AutoCloseable {
48 private static final Logger LOG = LoggerFactory.getLogger(ElanInstanceManager.class);
50 private final DataBroker broker;
51 private final IdManagerService idManager;
52 private final IInterfaceManager interfaceManager;
53 private final ElanInterfaceManager elanInterfaceManager;
56 public ElanInstanceManager(final DataBroker dataBroker, final IdManagerService managerService,
57 final ElanInterfaceManager elanInterfaceManager,
58 final IInterfaceManager interfaceManager) {
59 super(ElanInstance.class, ElanInstanceManager.class);
60 this.broker = dataBroker;
61 this.idManager = managerService;
62 this.elanInterfaceManager = elanInterfaceManager;
63 this.interfaceManager = interfaceManager;
69 registerListener(LogicalDatastoreType.CONFIGURATION, broker);
73 protected void remove(InstanceIdentifier<ElanInstance> identifier, ElanInstance deletedElan) {
74 LOG.trace("Remove ElanInstance - Key: {}, value: {}", identifier, deletedElan);
75 List<ListenableFuture<Void>> futures = new ArrayList<>();
76 String elanName = deletedElan.getElanInstanceName();
77 // check the elan Instance present in the Operational DataStore
78 Elan existingElan = ElanUtils.getElanByName(broker, elanName);
79 long elanTag = deletedElan.getElanTag();
80 // Cleaning up the existing Elan Instance
81 if (existingElan != null) {
82 List<String> elanInterfaces = existingElan.getElanInterfaces();
83 if (elanInterfaces != null && !elanInterfaces.isEmpty()) {
84 for (String elanInterfaceName : elanInterfaces) {
85 InstanceIdentifier<ElanInterface> elanInterfaceId = ElanUtils
86 .getElanInterfaceConfigurationDataPathId(elanInterfaceName);
87 InterfaceInfo interfaceInfo = interfaceManager.getInterfaceInfo(elanInterfaceName);
88 elanInterfaceManager.removeElanInterface(futures, deletedElan, elanInterfaceName,
89 interfaceInfo, false);
90 ElanUtils.delete(broker, LogicalDatastoreType.CONFIGURATION,
94 ElanUtils.delete(broker, LogicalDatastoreType.OPERATIONAL,
95 ElanUtils.getElanInstanceOperationalDataPath(elanName));
96 Optional<ElanDpnInterfacesList> elanDpnInterfaceList = MDSALUtil.read(broker,
97 LogicalDatastoreType.OPERATIONAL,
98 ElanUtils.getElanDpnOperationDataPath(elanName));
99 if (elanDpnInterfaceList.isPresent()) {
100 ElanUtils.delete(broker, LogicalDatastoreType.OPERATIONAL,
101 getElanDpnOperationDataPath(elanName));
103 ElanUtils.delete(broker, LogicalDatastoreType.OPERATIONAL,
104 ElanUtils.getElanInfoEntriesOperationalDataPath(elanTag));
107 ElanUtils.releaseId(idManager, ElanConstants.ELAN_ID_POOL_NAME, elanName);
108 if (deletedElan.getAugmentation(EtreeInstance.class) != null) {
109 removeEtreeInstance(deletedElan);
113 private void removeEtreeInstance(ElanInstance deletedElan) {
114 // Release leaves tag
115 ElanUtils.releaseId(idManager, ElanConstants.ELAN_ID_POOL_NAME,
116 deletedElan.getElanInstanceName() + ElanConstants.LEAVES_POSTFIX);
118 ElanUtils.delete(broker, LogicalDatastoreType.OPERATIONAL,
119 ElanUtils.getElanInfoEntriesOperationalDataPath(
120 deletedElan.getAugmentation(EtreeInstance.class).getEtreeLeafTagVal().getValue()));
124 protected void update(InstanceIdentifier<ElanInstance> identifier, ElanInstance original, ElanInstance update) {
125 Long existingElanTag = original.getElanTag();
126 if (existingElanTag != null && existingElanTag == update.getElanTag()) {
128 } else if (update.getElanTag() == null) {
129 // update the elan-Instance with new properties
130 WriteTransaction tx = broker.newWriteOnlyTransaction();
131 ElanUtils.updateOperationalDataStore(broker, idManager,
132 update, new ArrayList<String>(), tx);
133 ElanUtils.waitForTransactionToComplete(tx);
137 elanInterfaceManager.handleunprocessedElanInterfaces(update);
138 } catch (ElanException e) {
139 LOG.error("update() failed for ElanInstance: " + identifier.toString(), e);
144 protected void add(InstanceIdentifier<ElanInstance> identifier, ElanInstance elanInstanceAdded) {
145 String elanInstanceName = elanInstanceAdded.getElanInstanceName();
146 Elan elanInfo = ElanUtils.getElanByName(broker, elanInstanceName);
147 if (elanInfo == null) {
148 WriteTransaction tx = broker.newWriteOnlyTransaction();
149 ElanUtils.updateOperationalDataStore(broker, idManager,
150 elanInstanceAdded, new ArrayList<String>(), tx);
151 ElanUtils.waitForTransactionToComplete(tx);
155 public ElanInstance getElanInstanceByName(String elanInstanceName) {
156 InstanceIdentifier<ElanInstance> elanIdentifierId = getElanInstanceConfigurationDataPath(elanInstanceName);
157 return MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, elanIdentifierId).orNull();
160 public List<DpnInterfaces> getElanDPNByName(String elanInstanceName) {
161 InstanceIdentifier<ElanDpnInterfacesList> elanIdentifier = getElanDpnOperationDataPath(elanInstanceName);
162 return MDSALUtil.read(broker, LogicalDatastoreType.OPERATIONAL, elanIdentifier).transform(
163 ElanDpnInterfacesList::getDpnInterfaces).or(Collections.emptyList());
166 private InstanceIdentifier<ElanDpnInterfacesList> getElanDpnOperationDataPath(String elanInstanceName) {
167 return InstanceIdentifier.builder(ElanDpnInterfaces.class)
168 .child(ElanDpnInterfacesList.class, new ElanDpnInterfacesListKey(elanInstanceName)).build();
171 private InstanceIdentifier<ElanInstance> getElanInstanceConfigurationDataPath(String elanInstanceName) {
172 return InstanceIdentifier.builder(ElanInstances.class)
173 .child(ElanInstance.class, new ElanInstanceKey(elanInstanceName)).build();
177 protected InstanceIdentifier<ElanInstance> getWildCardPath() {
178 return InstanceIdentifier.create(ElanInstances.class).child(ElanInstance.class);
182 protected ElanInstanceManager getDataTreeChangeListener() {