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;
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
14 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.vpnservice.mdsalutil.AbstractDataChangeListener;
17 import org.opendaylight.vpnservice.elan.utils.ElanConstants;
18 import org.opendaylight.vpnservice.elan.utils.ElanUtils;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.ElanDpnInterfaces;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.ElanInstances;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesList;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesListKey;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.elan.dpn.interfaces.elan.dpn.interfaces.list.DpnInterfaces;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.elan.instances.ElanInstance;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.elan.instances.ElanInstanceKey;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.elan.interfaces.ElanInterface;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.elan.state.Elan;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.IdManagerService;
29 import org.opendaylight.yangtools.concepts.ListenerRegistration;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 import java.util.List;
36 public class ElanInstanceManager extends AbstractDataChangeListener<ElanInstance> implements AutoCloseable {
37 private DataBroker broker;
38 private static ElanInstanceManager elanInstanceManager = new ElanInstanceManager();
39 private ListenerRegistration<DataChangeListener> elanInstanceListenerRegistration;
40 private IdManagerService idManager;
41 private ElanInterfaceManager elanInterfaceManager;
43 private static final Logger logger = LoggerFactory.getLogger(ElanInstanceManager.class);
45 private ElanInstanceManager() {
46 super(ElanInstance.class);
50 public static ElanInstanceManager getElanInstanceManager() {
51 return elanInstanceManager;
54 public void setIdManager(IdManagerService idManager) {
55 this.idManager = idManager;
58 public void setDataBroker(DataBroker broker) {
62 public void setElanInterfaceManager(ElanInterfaceManager elanInterfaceManager) {
63 this.elanInterfaceManager = elanInterfaceManager;
67 public void registerListener() {
69 elanInstanceListenerRegistration = broker.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
70 getElanInstanceWildcardPath(), ElanInstanceManager.this, DataChangeScope.SUBTREE);
71 } catch (final Exception e) {
72 logger.error("ELAN Instance DataChange listener registration failed !", e);
73 throw new IllegalStateException("ELAN Instance registration Listener failed.", e);
77 private InstanceIdentifier<?> getElanInstanceWildcardPath() {
78 return InstanceIdentifier.create(ElanInstances.class).child(ElanInstance.class);
82 public void close() throws Exception {
83 if (elanInstanceListenerRegistration != null) {
84 elanInstanceListenerRegistration.close();
89 protected void remove(InstanceIdentifier<ElanInstance> identifier, ElanInstance del) {
90 logger.trace("Remove ElanInstance - Key: {}, value: {}", identifier, del);
91 String elanName = del.getElanInstanceName();
92 //check the elan Instance present in the Operational DataStore
93 Elan existingElan = ElanUtils.getElanByName(elanName);
94 long elanTag = del.getElanTag();
95 //Cleaning up the existing Elan Instance
96 if(existingElan != null) {
97 List<String> elanInterfaces = existingElan.getElanInterfaces();
98 if(elanInterfaces != null && !elanInterfaces.isEmpty()) {
99 for (String elanInterfaceName : elanInterfaces) {
100 InstanceIdentifier<ElanInterface> elanInterfaceId = ElanUtils.getElanInterfaceConfigurationDataPathId(elanInterfaceName);
101 elanInterfaceManager.removeElanInterface(del, elanInterfaceName);
102 ElanUtils.delete(broker, LogicalDatastoreType.CONFIGURATION, elanInterfaceId);
105 ElanUtils.delete(broker, LogicalDatastoreType.OPERATIONAL, ElanUtils.getElanInstanceOperationalDataPath(elanName));
106 ElanUtils.delete(broker, LogicalDatastoreType.OPERATIONAL, getElanDpnOperationDataPath(elanName));
107 ElanUtils.delete(broker, LogicalDatastoreType.OPERATIONAL, ElanUtils.getElanInfoEntriesOperationalDataPath(elanTag));
110 ElanUtils.releaseId(idManager, ElanConstants.ELAN_ID_POOL_NAME, elanName);
115 protected void update(InstanceIdentifier<ElanInstance> identifier, ElanInstance original, ElanInstance update) {
116 Long existingElanTag = original.getElanTag();
117 if (existingElanTag != null && existingElanTag == update.getElanTag()) {
119 } else if (update.getElanTag() == null) {
120 // update the elan-Instance with new properties
121 if(original.getMacTimeout().equals(update.getMacTimeout()) && original.getDescription().equalsIgnoreCase(update.getDescription())) {
124 ElanUtils.UpdateOperationalDataStore(broker, idManager, update);
127 elanInterfaceManager.handleunprocessedElanInterfaces(update);
131 protected void add(InstanceIdentifier<ElanInstance> identifier, ElanInstance elanInstanceAdded) {
132 Elan elanInfo = ElanUtils.getElanByName(elanInstanceAdded.getElanInstanceName());
133 if(elanInfo == null) {
134 ElanUtils.UpdateOperationalDataStore(broker, idManager, elanInstanceAdded);
138 public ElanInstance getElanInstanceByName(String elanInstanceName) {
139 InstanceIdentifier<ElanInstance> elanIdentifierId = getElanInstanceConfigurationDataPath(elanInstanceName);
140 Optional<ElanInstance> elanInstance = ElanUtils.read(broker, LogicalDatastoreType.CONFIGURATION, elanIdentifierId);
141 if(elanInstance.isPresent()) {
142 return elanInstance.get();
147 public List<DpnInterfaces> getElanDPNByName(String elanInstanceName) {
148 InstanceIdentifier<ElanDpnInterfacesList> elanIdentifier = getElanDpnOperationDataPath(elanInstanceName);
149 Optional<ElanDpnInterfacesList> elanInstance = ElanUtils.read(broker, LogicalDatastoreType.OPERATIONAL, elanIdentifier);
150 if(elanInstance.isPresent()) {
151 ElanDpnInterfacesList elanDPNs = elanInstance.get();
152 return elanDPNs.getDpnInterfaces();
157 private InstanceIdentifier<ElanDpnInterfacesList> getElanDpnOperationDataPath(String elanInstanceName) {
158 return InstanceIdentifier.builder(ElanDpnInterfaces.class).child(ElanDpnInterfacesList.class, new ElanDpnInterfacesListKey(elanInstanceName)).build();
161 private InstanceIdentifier<ElanInstance> getElanInstanceConfigurationDataPath(String elanInstanceName) {
162 return InstanceIdentifier.builder(ElanInstances.class).child(ElanInstance.class, new ElanInstanceKey(elanInstanceName)).build();