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;
68 * Starts listening for changes in elan.yang:elan-instance container
70 public void registerListener() {
72 elanInstanceListenerRegistration = broker.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
73 getElanInstanceWildcardPath(), ElanInstanceManager.this, DataChangeScope.SUBTREE);
74 } catch (final Exception e) {
75 logger.error("ELAN Instance DataChange listener registration failed !", e);
76 throw new IllegalStateException("ELAN Instance registration Listener failed.", e);
80 private InstanceIdentifier<?> getElanInstanceWildcardPath() {
81 return InstanceIdentifier.create(ElanInstances.class).child(ElanInstance.class);
85 public void close() throws Exception {
86 if (elanInstanceListenerRegistration != null) {
87 elanInstanceListenerRegistration.close();
92 protected void remove(InstanceIdentifier<ElanInstance> identifier, ElanInstance deletedElan) {
93 logger.trace("Remove ElanInstance - Key: {}, value: {}", identifier, deletedElan);
94 String elanName = deletedElan.getElanInstanceName();
95 //check the elan Instance present in the Operational DataStore
96 Elan existingElan = ElanUtils.getElanByName(elanName);
97 long elanTag = deletedElan.getElanTag();
98 //Cleaning up the existing Elan Instance
99 if(existingElan != null) {
100 List<String> elanInterfaces = existingElan.getElanInterfaces();
101 if(elanInterfaces != null && !elanInterfaces.isEmpty()) {
102 for (String elanInterfaceName : elanInterfaces) {
103 InstanceIdentifier<ElanInterface> elanInterfaceId = ElanUtils.getElanInterfaceConfigurationDataPathId(elanInterfaceName);
104 elanInterfaceManager.removeElanInterface(deletedElan, elanInterfaceName);
105 ElanUtils.delete(broker, LogicalDatastoreType.CONFIGURATION, elanInterfaceId);
108 ElanUtils.delete(broker, LogicalDatastoreType.OPERATIONAL, ElanUtils.getElanInstanceOperationalDataPath(elanName));
109 ElanUtils.delete(broker, LogicalDatastoreType.OPERATIONAL, getElanDpnOperationDataPath(elanName));
110 ElanUtils.delete(broker, LogicalDatastoreType.OPERATIONAL, ElanUtils.getElanInfoEntriesOperationalDataPath(elanTag));
113 ElanUtils.releaseId(idManager, ElanConstants.ELAN_ID_POOL_NAME, elanName);
118 protected void update(InstanceIdentifier<ElanInstance> identifier, ElanInstance original, ElanInstance update) {
119 Long existingElanTag = original.getElanTag();
120 if (existingElanTag != null && existingElanTag == update.getElanTag()) {
122 } else if (update.getElanTag() == null) {
123 // update the elan-Instance with new properties
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();