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 static java.util.Collections.emptyList;
13 import com.google.common.base.Optional;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import java.util.ArrayList;
16 import java.util.Collections;
17 import java.util.List;
18 import javax.annotation.PostConstruct;
19 import javax.inject.Inject;
20 import javax.inject.Singleton;
22 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
23 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
24 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
25 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
26 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
27 import org.opendaylight.genius.interfacemanager.globals.InterfaceInfo;
28 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
29 import org.opendaylight.genius.mdsalutil.MDSALUtil;
30 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
31 import org.opendaylight.infrautils.utils.concurrent.ListenableFutures;
32 import org.opendaylight.netvirt.elan.ElanException;
33 import org.opendaylight.netvirt.elan.cache.ElanInterfaceCache;
34 import org.opendaylight.netvirt.elan.utils.ElanConstants;
35 import org.opendaylight.netvirt.elan.utils.ElanUtils;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInstance;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanDpnInterfaces;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInstances;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesList;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesListKey;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterface;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.state.Elan;
45 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
50 public class ElanInstanceManager extends AsyncDataTreeChangeListenerBase<ElanInstance, ElanInstanceManager> {
52 private static final Logger LOG = LoggerFactory.getLogger(ElanInstanceManager.class);
54 private final DataBroker broker;
55 private final ManagedNewTransactionRunner txRunner;
56 private final IdManagerService idManager;
57 private final IInterfaceManager interfaceManager;
58 private final ElanInterfaceManager elanInterfaceManager;
59 private final JobCoordinator jobCoordinator;
60 private final ElanInterfaceCache elanInterfaceCache;
63 public ElanInstanceManager(final DataBroker dataBroker, final IdManagerService managerService,
64 final ElanInterfaceManager elanInterfaceManager,
65 final IInterfaceManager interfaceManager, final JobCoordinator jobCoordinator,
66 final ElanInterfaceCache elanInterfaceCache) {
67 super(ElanInstance.class, ElanInstanceManager.class);
68 this.broker = dataBroker;
69 this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
70 this.idManager = managerService;
71 this.elanInterfaceManager = elanInterfaceManager;
72 this.interfaceManager = interfaceManager;
73 this.jobCoordinator = jobCoordinator;
74 this.elanInterfaceCache = elanInterfaceCache;
80 registerListener(LogicalDatastoreType.CONFIGURATION, broker);
84 protected void remove(InstanceIdentifier<ElanInstance> identifier, ElanInstance deletedElan) {
85 LOG.trace("Remove ElanInstance - Key: {}, value: {}", identifier, deletedElan);
86 String elanName = deletedElan.getElanInstanceName();
87 // check the elan Instance present in the Operational DataStore
88 Elan existingElan = ElanUtils.getElanByName(broker, elanName);
89 long elanTag = deletedElan.getElanTag();
90 // Cleaning up the existing Elan Instance
91 if (existingElan != null) {
92 List<String> elanInterfaces = existingElan.getElanInterfaces();
93 if (elanInterfaces != null && !elanInterfaces.isEmpty()) {
94 List<ListenableFuture<Void>> futureList = new ArrayList<>();
95 elanInterfaces.forEach(elanInterfaceName -> {
96 jobCoordinator.enqueueJob(ElanUtils.getElanInterfaceJobKey(elanInterfaceName), () -> {
97 InstanceIdentifier<ElanInterface> elanInterfaceId = ElanUtils
98 .getElanInterfaceConfigurationDataPathId(elanInterfaceName);
99 InterfaceInfo interfaceInfo = interfaceManager.getInterfaceInfo(elanInterfaceName);
100 futureList.addAll(elanInterfaceManager.removeElanInterface(deletedElan, elanInterfaceName,
102 ElanUtils.delete(broker, LogicalDatastoreType.CONFIGURATION,
105 },ElanConstants.JOB_MAX_RETRIES);
108 ElanUtils.delete(broker, LogicalDatastoreType.OPERATIONAL,
109 ElanUtils.getElanInstanceOperationalDataPath(elanName));
110 Optional<ElanDpnInterfacesList> elanDpnInterfaceList = MDSALUtil.read(broker,
111 LogicalDatastoreType.OPERATIONAL,
112 ElanUtils.getElanDpnOperationDataPath(elanName));
113 if (elanDpnInterfaceList.isPresent()) {
114 ElanUtils.delete(broker, LogicalDatastoreType.OPERATIONAL,
115 getElanDpnOperationDataPath(elanName));
117 ElanUtils.delete(broker, LogicalDatastoreType.OPERATIONAL,
118 ElanUtils.getElanInfoEntriesOperationalDataPath(elanTag));
120 elanInterfaceCache.getInterfaceNames(elanName).forEach(
121 elanInterfaceName -> jobCoordinator.enqueueJob(ElanUtils.getElanInterfaceJobKey(elanInterfaceName),
122 () -> Collections.singletonList(txRunner.callWithNewReadWriteTransactionAndSubmit(tx -> {
123 LOG.info("Deleting the elanInterface present under ConfigDS:{}", elanInterfaceName);
124 ElanUtils.delete(broker, LogicalDatastoreType.CONFIGURATION,
125 ElanUtils.getElanInterfaceConfigurationDataPathId(elanInterfaceName));
126 elanInterfaceManager.unbindService(elanInterfaceName, tx);
127 LOG.info("unbind the Interface:{} service bounded to Elan:{}", elanInterfaceName, elanName);
128 })), ElanConstants.JOB_MAX_RETRIES));
130 ElanUtils.releaseId(idManager, ElanConstants.ELAN_ID_POOL_NAME, elanName);
131 if (deletedElan.getAugmentation(EtreeInstance.class) != null) {
132 removeEtreeInstance(deletedElan);
136 private void removeEtreeInstance(ElanInstance deletedElan) {
137 // Release leaves tag
138 ElanUtils.releaseId(idManager, ElanConstants.ELAN_ID_POOL_NAME,
139 deletedElan.getElanInstanceName() + ElanConstants.LEAVES_POSTFIX);
141 ElanUtils.delete(broker, LogicalDatastoreType.OPERATIONAL,
142 ElanUtils.getElanInfoEntriesOperationalDataPath(
143 deletedElan.getAugmentation(EtreeInstance.class).getEtreeLeafTagVal().getValue()));
147 protected void update(InstanceIdentifier<ElanInstance> identifier, ElanInstance original, ElanInstance update) {
148 Long existingElanTag = original.getElanTag();
149 String elanName = update.getElanInstanceName();
150 if (existingElanTag == null || !existingElanTag.equals(update.getElanTag())) {
151 if (update.getElanTag() == null) {
152 // update the elan-Instance with new properties
153 ListenableFutures.addErrorLogging(txRunner.callWithNewWriteOnlyTransactionAndSubmit(
154 tx -> ElanUtils.updateOperationalDataStore(idManager, update, new ArrayList<>(), tx)),
155 LOG, "Error updating ELAN tag in ELAN instance");
157 jobCoordinator.enqueueJob(elanName, () -> {
159 return elanInterfaceManager.handleunprocessedElanInterfaces(update);
160 } catch (ElanException e) {
161 LOG.error("update() failed for ElanInstance: {}", identifier.toString(), e);
164 }, ElanConstants.JOB_MAX_RETRIES);
170 protected void add(InstanceIdentifier<ElanInstance> identifier, ElanInstance elanInstanceAdded) {
171 ListenableFutures.addErrorLogging(txRunner.callWithNewReadWriteTransactionAndSubmit(tx -> {
172 String elanInstanceName = elanInstanceAdded.getElanInstanceName();
173 Elan elanInfo = ElanUtils.getElanByName(tx, elanInstanceName);
174 if (elanInfo == null) {
175 ElanUtils.updateOperationalDataStore(idManager, elanInstanceAdded, new ArrayList<>(), tx);
177 }), LOG, "Error adding an ELAN instance");
180 private static InstanceIdentifier<ElanDpnInterfacesList> getElanDpnOperationDataPath(String elanInstanceName) {
181 return InstanceIdentifier.builder(ElanDpnInterfaces.class)
182 .child(ElanDpnInterfacesList.class, new ElanDpnInterfacesListKey(elanInstanceName)).build();
186 protected InstanceIdentifier<ElanInstance> getWildCardPath() {
187 return InstanceIdentifier.create(ElanInstances.class).child(ElanInstance.class);
191 protected ElanInstanceManager getDataTreeChangeListener() {