Merge "BUG-2329 Add test for anyxmls inside rpc resonse for netcfon-connector"
[controller.git] / opendaylight / clustering / stub / src / main / java / org / opendaylight / controller / clustering / stub / internal / ClusterManagerCommon.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9
10 package org.opendaylight.controller.clustering.stub.internal;
11
12 import java.net.InetAddress;
13 import java.net.UnknownHostException;
14 import java.util.ArrayList;
15 import java.util.Dictionary;
16 import java.util.List;
17 import java.util.Properties;
18 import java.util.Set;
19 import java.util.concurrent.ConcurrentHashMap;
20 import java.util.concurrent.ConcurrentMap;
21 import java.util.concurrent.TimeUnit;
22
23 import javax.transaction.HeuristicMixedException;
24 import javax.transaction.HeuristicRollbackException;
25 import javax.transaction.NotSupportedException;
26 import javax.transaction.RollbackException;
27 import javax.transaction.SystemException;
28 import javax.transaction.Transaction;
29
30 import org.apache.felix.dm.Component;
31 import org.opendaylight.controller.clustering.services.CacheConfigException;
32 import org.opendaylight.controller.clustering.services.CacheExistException;
33 import org.opendaylight.controller.clustering.services.IClusterServices;
34 import org.opendaylight.controller.clustering.services.IClusterServicesCommon;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 public abstract class ClusterManagerCommon implements IClusterServicesCommon {
39     protected String containerName = "";
40     protected static final Logger logger = LoggerFactory
41             .getLogger(ClusterManagerCommon.class);
42     private InetAddress loopbackAddress;
43     private ConcurrentMap<String, ConcurrentMap<?, ?>> caches = new ConcurrentHashMap<String, ConcurrentMap<?, ?>>();
44
45     protected ClusterManagerCommon() throws UnknownHostException {
46         loopbackAddress = InetAddress.getLoopbackAddress();
47     }
48
49     /**
50      * Function called by the dependency manager when all the required
51      * dependencies are satisfied
52      *
53      */
54     void init(Component c) {
55         Dictionary props = c.getServiceProperties();
56         if (props != null) {
57             this.containerName = (String) props.get("containerName");
58             logger.debug("Running containerName: {}", this.containerName);
59         } else {
60             // In the Global instance case the containerName is empty
61             this.containerName = "";
62         }
63     }
64
65     /**
66      * Function called by the dependency manager when any of the required
67      * dependencies are going away
68      *
69      */
70     void destroy() {
71         // Clear the caches, will restart on the new life
72         this.caches.clear();
73     }
74
75     @Override
76     public ConcurrentMap<?, ?> createCache(String cacheName,
77             Set<IClusterServices.cacheMode> cMode) throws CacheExistException,
78             CacheConfigException {
79         ConcurrentMap<?, ?> res = this.caches.get(cacheName);
80         if (res == null) {
81             res = new ConcurrentHashMap<Object, Object>();
82             this.caches.put(cacheName, res);
83             return res;
84         }
85         throw new CacheExistException();
86     }
87
88     @Override
89     public ConcurrentMap<?, ?> getCache(String cacheName) {
90         return this.caches.get(cacheName);
91     }
92
93     @Override
94     public void destroyCache(String cacheName) {
95         this.caches.remove(cacheName);
96     }
97
98     @Override
99     public boolean existCache(String cacheName) {
100         return (this.caches.get(cacheName) != null);
101     }
102
103     @Override
104     public Set<String> getCacheList() {
105         return this.caches.keySet();
106     }
107
108     @Override
109     public Properties getCacheProperties(String cacheName) {
110         return null;
111     }
112
113     @Override
114     public void tbegin() throws NotSupportedException, SystemException {
115     }
116
117     @Override
118     public void tcommit() throws RollbackException, HeuristicMixedException,
119             HeuristicRollbackException, java.lang.SecurityException,
120             java.lang.IllegalStateException, SystemException {
121     }
122
123     @Override
124     public void tbegin(long timeout, TimeUnit unit) throws NotSupportedException, SystemException {
125
126     }
127
128     @Override
129     public void trollback() throws java.lang.IllegalStateException,
130             java.lang.SecurityException, SystemException {
131     }
132
133     @Override
134     public Transaction tgetTransaction() throws SystemException {
135         return null;
136     }
137
138     @Override
139     public List<InetAddress> getClusteredControllers() {
140         List<InetAddress> res = new ArrayList<InetAddress>();
141         res.add(loopbackAddress);
142         return res;
143     }
144
145     @Override
146     public InetAddress getMyAddress() {
147         return loopbackAddress;
148     }
149
150     @Override
151     public InetAddress getCoordinatorAddress() {
152         return loopbackAddress;
153     }
154
155     @Override
156     public boolean amICoordinator() {
157         return true;
158     }
159 }