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