Merge "ipv6service: Register listeners and configure handlers"
[netvirt.git] / vpnservice / elanmanager / elanmanager-impl / src / main / java / org / opendaylight / netvirt / elan / internal / ElanInstanceManager.java
1 /*
2  * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.netvirt.elan.internal;
10
11 import java.util.ArrayList;
12 import java.util.List;
13
14 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
17 import org.opendaylight.netvirt.elan.utils.ElanConstants;
18 import org.opendaylight.netvirt.elan.utils.ElanUtils;
19 import org.opendaylight.genius.interfacemanager.globals.InterfaceInfo;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanDpnInterfaces;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInstances;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesList;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesListKey;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.elan.dpn.interfaces.list.DpnInterfaces;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstanceKey;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterface;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.state.Elan;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 import com.google.common.base.Optional;
34
35 public class ElanInstanceManager extends AsyncDataTreeChangeListenerBase<ElanInstance,ElanInstanceManager> implements AutoCloseable {
36
37     private  ElanServiceProvider elanServiceProvider = null;
38     private static volatile ElanInstanceManager elanInstanceManager = null;
39
40     private static final Logger logger = LoggerFactory.getLogger(ElanInstanceManager.class);
41
42     private ElanInstanceManager(ElanServiceProvider elanServiceProvider) {
43         super(ElanInstance.class,ElanInstanceManager.class);
44         this.elanServiceProvider = elanServiceProvider;
45     }
46
47     public static ElanInstanceManager getElanInstanceManager(ElanServiceProvider elanServiceProvider) {
48         if (elanInstanceManager == null) {
49             synchronized (ElanInstanceManager.class) {
50                 if (elanInstanceManager == null) {
51                     elanInstanceManager = new ElanInstanceManager(elanServiceProvider);
52                 }
53             }
54         }
55         return elanInstanceManager;
56     }
57
58     @Override
59     protected void remove(InstanceIdentifier<ElanInstance> identifier, ElanInstance deletedElan) {
60         logger.trace("Remove ElanInstance - Key: {}, value: {}", identifier, deletedElan);
61         String elanName = deletedElan.getElanInstanceName();
62         //check the elan Instance present in the Operational DataStore
63         Elan existingElan = ElanUtils.getElanByName(elanName);
64         long elanTag = deletedElan.getElanTag();
65         //Cleaning up the existing Elan Instance
66         if (existingElan != null) {
67             List<String> elanInterfaces =  existingElan.getElanInterfaces();
68             if (elanInterfaces != null && !elanInterfaces.isEmpty()) {
69                 for (String elanInterfaceName : elanInterfaces) {
70                     InstanceIdentifier<ElanInterface> elanInterfaceId = ElanUtils.getElanInterfaceConfigurationDataPathId(elanInterfaceName);
71                     InterfaceInfo interfaceInfo = elanServiceProvider.getInterfaceManager().getInterfaceInfo(elanInterfaceName);
72                     elanServiceProvider.getElanInterfaceManager().removeElanInterface(deletedElan, elanInterfaceName, interfaceInfo, false);
73                     ElanUtils.delete(elanServiceProvider.getBroker(), LogicalDatastoreType.CONFIGURATION, elanInterfaceId);
74                 }
75             }
76             ElanUtils.delete(elanServiceProvider.getBroker(), LogicalDatastoreType.OPERATIONAL, ElanUtils.getElanInstanceOperationalDataPath(elanName));
77             ElanUtils.delete(elanServiceProvider.getBroker(), LogicalDatastoreType.OPERATIONAL, getElanDpnOperationDataPath(elanName));
78             ElanUtils.delete(elanServiceProvider.getBroker(), LogicalDatastoreType.OPERATIONAL, ElanUtils.getElanInfoEntriesOperationalDataPath(elanTag));
79         }
80         // Release tag
81         ElanUtils.releaseId( elanServiceProvider.getIdManager(), ElanConstants.ELAN_ID_POOL_NAME, elanName);
82
83     }
84
85     @Override
86     protected void update(InstanceIdentifier<ElanInstance> identifier, ElanInstance original, ElanInstance update) {
87         Long existingElanTag = original.getElanTag();
88         if (existingElanTag != null && existingElanTag == update.getElanTag()) {
89             return;
90         } else if (update.getElanTag() == null) {
91             // update the elan-Instance with new properties
92             WriteTransaction tx = elanServiceProvider.getBroker().newWriteOnlyTransaction();
93             ElanUtils.updateOperationalDataStore(elanServiceProvider.getBroker(), elanServiceProvider.getIdManager(),
94                 update, new ArrayList<String>(), tx);
95             ElanUtils.waitForTransactionToComplete(tx);
96             return;
97         }
98         elanServiceProvider.getElanInterfaceManager().handleunprocessedElanInterfaces(update);
99     }
100
101     @Override
102     protected void add(InstanceIdentifier<ElanInstance> identifier, ElanInstance elanInstanceAdded) {
103         Elan elanInfo = ElanUtils.getElanByName(elanInstanceAdded.getElanInstanceName());
104         if (elanInfo == null) {
105             WriteTransaction tx = elanServiceProvider.getBroker().newWriteOnlyTransaction();
106             ElanUtils.updateOperationalDataStore(elanServiceProvider.getBroker(), elanServiceProvider.getIdManager(), elanInstanceAdded,
107                 new ArrayList<String>(), tx);
108             ElanUtils.waitForTransactionToComplete(tx);
109         }
110     }
111
112     public ElanInstance getElanInstanceByName(String elanInstanceName) {
113         InstanceIdentifier<ElanInstance> elanIdentifierId = getElanInstanceConfigurationDataPath(elanInstanceName);
114         Optional<ElanInstance> elanInstance = ElanUtils.read(elanServiceProvider.getBroker(), LogicalDatastoreType.CONFIGURATION, elanIdentifierId);
115         if (elanInstance.isPresent()) {
116             return elanInstance.get();
117         }
118         return null;
119     }
120
121     public List<DpnInterfaces> getElanDPNByName(String elanInstanceName) {
122         InstanceIdentifier<ElanDpnInterfacesList> elanIdentifier = getElanDpnOperationDataPath(elanInstanceName);
123         Optional<ElanDpnInterfacesList> elanInstance = ElanUtils.read(elanServiceProvider.getBroker(), LogicalDatastoreType.OPERATIONAL, elanIdentifier);
124         if (elanInstance.isPresent()) {
125             ElanDpnInterfacesList elanDPNs =  elanInstance.get();
126             return elanDPNs.getDpnInterfaces();
127         }
128         return null;
129     }
130
131     private InstanceIdentifier<ElanDpnInterfacesList> getElanDpnOperationDataPath(String elanInstanceName) {
132         return InstanceIdentifier.builder(ElanDpnInterfaces.class).child(ElanDpnInterfacesList.class, new ElanDpnInterfacesListKey(elanInstanceName)).build();
133     }
134
135     private InstanceIdentifier<ElanInstance> getElanInstanceConfigurationDataPath(String elanInstanceName) {
136         return InstanceIdentifier.builder(ElanInstances.class).child(ElanInstance.class, new ElanInstanceKey(elanInstanceName)).build();
137     }
138
139     @Override
140     protected InstanceIdentifier<ElanInstance> getWildCardPath() {
141         return InstanceIdentifier.create(ElanInstances.class).child(ElanInstance.class);
142     }
143
144     @Override
145     protected ElanInstanceManager getDataTreeChangeListener() {
146         return this;
147     }
148
149 }