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
8 package org.opendaylight.vpnservice.elan.internal;
10 import com.google.common.base.Optional;
11 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
12 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
13 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.vpnservice.mdsalutil.AbstractDataChangeListener;
16 import org.opendaylight.vpnservice.elan.utils.ElanConstants;
17 import org.opendaylight.vpnservice.elan.utils.ElanUtils;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.ElanDpnInterfaces;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.ElanInstances;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesList;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesListKey;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.elan.dpn.interfaces.elan.dpn.interfaces.list.DpnInterfaces;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.elan.instances.ElanInstance;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.elan.instances.ElanInstanceKey;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.elan.interfaces.ElanInterface;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.elan.state.Elan;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.IdManagerService;
28 import org.opendaylight.yangtools.concepts.ListenerRegistration;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 import java.util.List;
35 public class ElanInstanceManager extends AbstractDataChangeListener<ElanInstance> implements AutoCloseable {
36 private DataBroker broker;
37 private static ElanInstanceManager elanInstanceManager = new ElanInstanceManager();
38 private ListenerRegistration<DataChangeListener> elanInstanceListenerRegistration;
39 private IdManagerService idManager;
40 private ElanInterfaceManager elanInterfaceManager;
42 private static final Logger logger = LoggerFactory.getLogger(ElanInstanceManager.class);
44 private ElanInstanceManager() {
45 super(ElanInstance.class);
49 public static ElanInstanceManager getElanInstanceManager() {
50 return elanInstanceManager;
53 public void setIdManager(IdManagerService idManager) {
54 this.idManager = idManager;
57 public void setDataBroker(DataBroker broker) {
61 public void setElanInterfaceManager(ElanInterfaceManager elanInterfaceManager) {
62 this.elanInterfaceManager = elanInterfaceManager;
66 public void registerListener() {
68 elanInstanceListenerRegistration = broker.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
69 getElanInstanceWildcardPath(), ElanInstanceManager.this, DataChangeScope.SUBTREE);
70 } catch (final Exception e) {
71 logger.error("ELAN Instance DataChange listener registration failed !", e);
72 throw new IllegalStateException("ELAN Instance registration Listener failed.", e);
76 private InstanceIdentifier<?> getElanInstanceWildcardPath() {
77 return InstanceIdentifier.create(ElanInstances.class).child(ElanInstance.class);
81 public void close() throws Exception {
82 if (elanInstanceListenerRegistration != null) {
83 elanInstanceListenerRegistration.close();
88 protected void remove(InstanceIdentifier<ElanInstance> identifier, ElanInstance del) {
89 logger.trace("Remove ElanInstance - Key: {}, value: {}", identifier, del);
90 String elanName = del.getElanInstanceName();
91 //check the elan Instance present in the Operational DataStore
92 Elan existingElan = ElanUtils.getElanByName(elanName);
93 long elanTag = del.getElanTag();
94 //Cleaning up the existing Elan Instance
95 if(existingElan != null) {
96 List<String> elanInterfaces = existingElan.getElanInterfaces();
97 if(elanInterfaces != null && !elanInterfaces.isEmpty()) {
98 for (String elanInterfaceName : elanInterfaces) {
99 InstanceIdentifier<ElanInterface> elanInterfaceId = ElanUtils.getElanInterfaceConfigurationDataPathId(elanInterfaceName);
100 elanInterfaceManager.removeElanInterface(del, elanInterfaceName);
101 ElanUtils.delete(broker, LogicalDatastoreType.CONFIGURATION, elanInterfaceId);
104 ElanUtils.delete(broker, LogicalDatastoreType.OPERATIONAL, ElanUtils.getElanInstanceOperationalDataPath(elanName));
105 ElanUtils.delete(broker, LogicalDatastoreType.OPERATIONAL, getElanDpnOperationDataPath(elanName));
106 ElanUtils.delete(broker, LogicalDatastoreType.OPERATIONAL, ElanUtils.getElanInfoEntriesOperationalDataPath(elanTag));
109 ElanUtils.releaseId(idManager, ElanConstants.ELAN_ID_POOL_NAME, elanName);
114 protected void update(InstanceIdentifier<ElanInstance> identifier, ElanInstance original, ElanInstance update) {
115 if (original.getElanTag() == update.getElanTag()) {
117 } else if (update.getElanTag() == null) {
118 // update the elan-Instance with new properties
119 if(original.getMacTimeout() == update.getMacTimeout() && original.getDescription().equalsIgnoreCase(update.getDescription())) {
122 ElanUtils.UpdateOperationalDataStore(broker, idManager, update);
125 elanInterfaceManager.handleunprocessedElanInterfaces(update);
129 protected void add(InstanceIdentifier<ElanInstance> identifier, ElanInstance elanInstanceAdded) {
130 Elan elanInfo = ElanUtils.getElanByName(elanInstanceAdded.getElanInstanceName());
131 if(elanInfo == null) {
132 ElanUtils.UpdateOperationalDataStore(broker, idManager, elanInstanceAdded);
136 public ElanInstance getElanInstanceByName(String elanInstanceName) {
137 InstanceIdentifier<ElanInstance> elanIdentifierId = getElanInstanceConfigurationDataPath(elanInstanceName);
138 Optional<ElanInstance> elanInstance = ElanUtils.read(broker, LogicalDatastoreType.CONFIGURATION, elanIdentifierId);
139 if(elanInstance.isPresent()) {
140 return elanInstance.get();
145 public List<DpnInterfaces> getElanDPNByName(String elanInstanceName) {
146 InstanceIdentifier<ElanDpnInterfacesList> elanIdentifier = getElanDpnOperationDataPath(elanInstanceName);
147 Optional<ElanDpnInterfacesList> elanInstance = ElanUtils.read(broker, 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).child(ElanDpnInterfacesList.class, new ElanDpnInterfacesListKey(elanInstanceName)).build();
159 private InstanceIdentifier<ElanInstance> getElanInstanceConfigurationDataPath(String elanInstanceName) {
160 return InstanceIdentifier.builder(ElanInstances.class).child(ElanInstance.class, new ElanInstanceKey(elanInstanceName)).build();