Merge changes I029118f9,I4a0320b9
[controller.git] / opendaylight / clustering / integrationtest / src / test / java / org / opendaylight / controller / clustering / services_implementation / internal / ClusteringServicesIT.java
1 package org.opendaylight.controller.clustering.services_implementation.internal;\r
2 \r
3 import static org.junit.Assert.assertEquals;\r
4 import static org.junit.Assert.assertFalse;\r
5 import static org.junit.Assert.assertNotNull;\r
6 import static org.junit.Assert.assertNull;\r
7 import static org.junit.Assert.assertTrue;\r
8 import static org.ops4j.pax.exam.CoreOptions.junitBundles;\r
9 import static org.ops4j.pax.exam.CoreOptions.mavenBundle;\r
10 import static org.ops4j.pax.exam.CoreOptions.options;\r
11 import static org.ops4j.pax.exam.CoreOptions.systemPackages;\r
12 import static org.ops4j.pax.exam.CoreOptions.systemProperty;\r
13 \r
14 import java.net.InetAddress;\r
15 import java.util.HashSet;\r
16 import java.util.List;\r
17 import java.util.concurrent.ConcurrentMap;\r
18 \r
19 import javax.inject.Inject;\r
20 \r
21 import org.junit.Before;\r
22 import org.junit.Test;\r
23 import org.junit.runner.RunWith;\r
24 import org.opendaylight.controller.clustering.services.CacheConfigException;\r
25 import org.opendaylight.controller.clustering.services.CacheExistException;\r
26 import org.opendaylight.controller.clustering.services.CacheListenerAddException;\r
27 import org.opendaylight.controller.clustering.services.IClusterServices;\r
28 import org.opendaylight.controller.clustering.services.IClusterServices.cacheMode;\r
29 import org.opendaylight.controller.clustering.services.IGetUpdates;\r
30 import org.ops4j.pax.exam.Option;\r
31 import org.ops4j.pax.exam.junit.Configuration;\r
32 import org.ops4j.pax.exam.junit.PaxExam;\r
33 import org.ops4j.pax.exam.util.PathUtils;\r
34 import org.osgi.framework.Bundle;\r
35 import org.osgi.framework.BundleContext;\r
36 import org.osgi.framework.ServiceReference;\r
37 import org.slf4j.Logger;\r
38 import org.slf4j.LoggerFactory;\r
39 \r
40 @RunWith(PaxExam.class)\r
41 public class ClusteringServicesIT {\r
42     private Logger log = LoggerFactory\r
43             .getLogger(ClusteringServicesIT.class);\r
44     // get the OSGI bundle context\r
45     @Inject\r
46     private BundleContext bc;\r
47 \r
48     private IClusterServices clusterServices = null;\r
49 \r
50     // Configure the OSGi container\r
51     @Configuration\r
52     public Option[] config() {\r
53         return options(\r
54                 //\r
55                 systemProperty("logback.configurationFile").value(\r
56                         "file:" + PathUtils.getBaseDir()\r
57                                 + "/src/test/resources/logback.xml"),\r
58                 // To start OSGi console for inspection remotely\r
59                 systemProperty("osgi.console").value("2401"),\r
60                 // Set the systemPackages (used by clustering)\r
61                 systemPackages("sun.reflect", "sun.reflect.misc", "sun.misc"),\r
62                 // List framework bundles\r
63                 mavenBundle("equinoxSDK381", "org.eclipse.equinox.console",\r
64                         "1.0.0.v20120522-1841"),\r
65                 mavenBundle("equinoxSDK381", "org.eclipse.equinox.util",\r
66                         "1.0.400.v20120522-2049"),\r
67                 mavenBundle("equinoxSDK381", "org.eclipse.osgi.services",\r
68                         "3.3.100.v20120522-1822"),\r
69                 mavenBundle("equinoxSDK381", "org.eclipse.equinox.ds",\r
70                         "1.4.0.v20120522-1841"),\r
71                 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.command",\r
72                         "0.8.0.v201108120515"),\r
73                 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.runtime",\r
74                         "0.8.0.v201108120515"),\r
75                 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.shell",\r
76                         "0.8.0.v201110170705"),\r
77                 // List logger bundles\r
78                 mavenBundle("org.slf4j", "slf4j-api").versionAsInProject(),\r
79                 mavenBundle("org.slf4j", "log4j-over-slf4j").versionAsInProject(),\r
80                 mavenBundle("ch.qos.logback", "logback-core").versionAsInProject(),\r
81                 mavenBundle("ch.qos.logback", "logback-classic").versionAsInProject(),\r
82                 // List all the bundles on which the test case depends\r
83                 mavenBundle("org.opendaylight.controller",\r
84                         "clustering.services").versionAsInProject(),\r
85                 mavenBundle("org.opendaylight.controller",\r
86                         "clustering.services-implementation").versionAsInProject(),\r
87                 mavenBundle("org.opendaylight.controller", "sal").versionAsInProject(),\r
88                 mavenBundle("org.opendaylight.controller",\r
89                         "sal.implementation").versionAsInProject(),\r
90                 mavenBundle("org.jboss.spec.javax.transaction",\r
91                         "jboss-transaction-api_1.1_spec").versionAsInProject(),\r
92                 mavenBundle("org.apache.commons", "commons-lang3").versionAsInProject(),\r
93                 mavenBundle("org.apache.felix",\r
94                         "org.apache.felix.dependencymanager").versionAsInProject(),\r
95                 junitBundles());\r
96     }\r
97 \r
98     private String stateToString(int state) {\r
99         switch (state) {\r
100         case Bundle.ACTIVE:\r
101             return "ACTIVE";\r
102         case Bundle.INSTALLED:\r
103             return "INSTALLED";\r
104         case Bundle.RESOLVED:\r
105             return "RESOLVED";\r
106         case Bundle.UNINSTALLED:\r
107             return "UNINSTALLED";\r
108         default:\r
109             return "Not CONVERTED";\r
110         }\r
111     }\r
112 \r
113     @Before\r
114     public void areWeReady() {\r
115         assertNotNull(bc);\r
116         boolean debugit = false;\r
117         Bundle b[] = bc.getBundles();\r
118         for (int i = 0; i < b.length; i++) {\r
119             int state = b[i].getState();\r
120             if (state != Bundle.ACTIVE && state != Bundle.RESOLVED) {\r
121                 log.debug("Bundle:" + b[i].getSymbolicName() + " state:"\r
122                         + stateToString(state));\r
123                 debugit = true;\r
124             }\r
125         }\r
126         if (debugit) {\r
127             log.debug("Do some debugging because some bundle is "\r
128                     + "unresolved");\r
129         }\r
130 \r
131         // Assert if true, if false we are good to go!\r
132         assertFalse(debugit);\r
133 \r
134         ServiceReference r = bc.getServiceReference(IClusterServices.class\r
135                 .getName());\r
136         if (r != null) {\r
137             this.clusterServices = (IClusterServices) bc.getService(r);\r
138         }\r
139         assertNotNull(this.clusterServices);\r
140 \r
141     }\r
142 \r
143     @Test\r
144     public void clusterTest() throws CacheExistException, CacheConfigException,\r
145             CacheListenerAddException {\r
146 \r
147         String container1 = "Container1";\r
148         String container2 = "Container2";\r
149         String cache1 = "Cache1";\r
150         String cache2 = "Cache2";\r
151         String cache3 = "Cache3";\r
152 \r
153         HashSet<cacheMode> cacheModeSet = new HashSet<cacheMode>();\r
154         cacheModeSet.add(cacheMode.NON_TRANSACTIONAL);\r
155         ConcurrentMap cm11 = this.clusterServices.createCache(container1,\r
156                 cache1, cacheModeSet);\r
157         assertNotNull(cm11);\r
158 \r
159         assertNull(this.clusterServices.getCache(container2, cache2));\r
160         assertEquals(cm11, this.clusterServices.getCache(container1, cache1));\r
161 \r
162         assertFalse(this.clusterServices.existCache(container2, cache2));\r
163         assertTrue(this.clusterServices.existCache(container1, cache1));\r
164 \r
165         ConcurrentMap cm12 = this.clusterServices.createCache(container1,\r
166                 cache2, cacheModeSet);\r
167         ConcurrentMap cm23 = this.clusterServices.createCache(container2,\r
168                 cache3, cacheModeSet);\r
169 \r
170         HashSet<String> cacheList = (HashSet<String>) this.clusterServices\r
171                 .getCacheList(container1);\r
172         assertEquals(2, cacheList.size());\r
173         assertTrue(cacheList.contains(cache1));\r
174         assertTrue(cacheList.contains(cache2));\r
175         assertFalse(cacheList.contains(cache3));\r
176 \r
177         assertNotNull(this.clusterServices.getCacheProperties(container1,\r
178                 cache1));\r
179 \r
180         HashSet<IGetUpdates<?, ?>> listeners = (HashSet<IGetUpdates<?, ?>>) this.clusterServices\r
181                 .getListeners(container1, cache1);\r
182         assertEquals(0, listeners.size());\r
183 \r
184         IGetUpdates<?, ?> getUpdate1 = new GetUpdates();\r
185         this.clusterServices.addListener(container1, cache1, getUpdate1);\r
186         listeners = (HashSet<IGetUpdates<?, ?>>) this.clusterServices\r
187                 .getListeners(container1, cache1);\r
188         assertEquals(1, listeners.size());\r
189         this.clusterServices.addListener(container1, cache1, new GetUpdates());\r
190         listeners = (HashSet<IGetUpdates<?, ?>>) this.clusterServices\r
191                 .getListeners(container1, cache1);\r
192         assertEquals(2, listeners.size());\r
193 \r
194         listeners = (HashSet<IGetUpdates<?, ?>>) this.clusterServices\r
195                 .getListeners(container2, cache3);\r
196         assertEquals(0, listeners.size());\r
197 \r
198         this.clusterServices.removeListener(container1, cache1, getUpdate1);\r
199         listeners = (HashSet<IGetUpdates<?, ?>>) this.clusterServices\r
200                 .getListeners(container1, cache1);\r
201         assertEquals(1, listeners.size());\r
202 \r
203         InetAddress addr = this.clusterServices.getMyAddress();\r
204         assertNotNull(addr);\r
205 \r
206         List<InetAddress> addrList = this.clusterServices\r
207                 .getClusteredControllers();\r
208 \r
209         this.clusterServices.destroyCache(container1, cache1);\r
210         assertFalse(this.clusterServices.existCache(container1, cache1));\r
211 \r
212     }\r
213 \r
214     private class GetUpdates implements IGetUpdates<Integer, String> {\r
215 \r
216         @Override\r
217         public void entryCreated(Integer key, String containerName,\r
218                 String cacheName, boolean originLocal) {\r
219             return;\r
220         }\r
221 \r
222         @Override\r
223         public void entryUpdated(Integer key, String new_value,\r
224                 String containerName, String cacheName, boolean originLocal) {\r
225             return;\r
226         }\r
227 \r
228         @Override\r
229         public void entryDeleted(Integer key, String containerName,\r
230                 String cacheName, boolean originLocal) {\r
231             return;\r
232         }\r
233     }\r
234 }\r