17b3414c91e174c168895502a63a4e9c8951eb67
[controller.git] / opendaylight / configuration / integrationtest / src / test / java / org / opendaylight / controller / configuration / internal / ConfigurationIT.java
1 package org.opendaylight.controller.configuration.internal;
2
3 import static org.junit.Assert.assertFalse;
4 import static org.junit.Assert.assertNotNull;
5 import static org.ops4j.pax.exam.CoreOptions.junitBundles;
6 import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
7 import static org.ops4j.pax.exam.CoreOptions.options;
8 import static org.ops4j.pax.exam.CoreOptions.systemPackages;
9 import static org.ops4j.pax.exam.CoreOptions.systemProperty;
10
11 import java.io.File;
12
13 import javax.inject.Inject;
14
15 import org.junit.Assert;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.junit.runner.RunWith;
19 import org.opendaylight.controller.clustering.services.IClusterServices;
20 import org.opendaylight.controller.configuration.IConfigurationService;
21 import org.opendaylight.controller.sal.utils.GlobalConstants;
22 import org.opendaylight.controller.sal.utils.Status;
23 import org.opendaylight.controller.sal.utils.StatusCode;
24 import org.ops4j.pax.exam.Option;
25 import org.ops4j.pax.exam.junit.Configuration;
26 import org.ops4j.pax.exam.junit.PaxExam;
27 import org.ops4j.pax.exam.util.PathUtils;
28 import org.osgi.framework.Bundle;
29 import org.osgi.framework.BundleContext;
30 import org.osgi.framework.ServiceReference;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 @RunWith(PaxExam.class)
35 public class ConfigurationIT {
36     private final Logger logger = LoggerFactory
37             .getLogger(ConfigurationIT.class);
38     // get the OSGI bundle context
39     @Inject
40     private BundleContext bc;
41     private IClusterServices clusterService = null;
42     private IConfigurationService configService = null;
43
44     // Configure the OSGi container
45     @Configuration
46     public Option[] config() {
47         return options(
48                 systemProperty("logback.configurationFile").value(
49                         "file:" + PathUtils.getBaseDir()
50                                 + "/src/test/resources/logback.xml"),
51                 // To start OSGi console for inspection remotely
52                 systemProperty("osgi.console").value("2401"),
53                 // Set the systemPackages (used by clustering)
54                 systemPackages("sun.reflect", "sun.reflect.misc", "sun.misc"),
55                 // List framework bundles
56                 mavenBundle("equinoxSDK381", "org.eclipse.equinox.console",
57                         "1.0.0.v20120522-1841"),
58                 mavenBundle("equinoxSDK381", "org.eclipse.equinox.util",
59                         "1.0.400.v20120522-2049"),
60                 mavenBundle("equinoxSDK381", "org.eclipse.osgi.services",
61                         "3.3.100.v20120522-1822"),
62                 mavenBundle("equinoxSDK381", "org.eclipse.equinox.ds",
63                         "1.4.0.v20120522-1841"),
64                 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.command",
65                         "0.8.0.v201108120515"),
66                 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.runtime",
67                         "0.8.0.v201108120515"),
68                 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.shell",
69                         "0.8.0.v201110170705"),
70                 // List logger bundles
71                 mavenBundle("org.slf4j", "slf4j-api", "1.7.2"),
72                 mavenBundle("org.slf4j", "log4j-over-slf4j", "1.7.2"),
73                 mavenBundle("ch.qos.logback", "logback-core", "1.0.9"),
74                 mavenBundle("ch.qos.logback", "logback-classic", "1.0.9"),
75                 mavenBundle("org.apache.commons", "commons-lang3", "3.1"),
76                 mavenBundle("org.jboss.spec.javax.transaction",
77                         "jboss-transaction-api_1.1_spec", "1.0.1.Final"),
78                 mavenBundle("eclipselink", "javax.resource").versionAsInProject(),
79                 mavenBundle("org.apache.felix",
80                         "org.apache.felix.dependencymanager", "3.1.0"),
81                 // List all the bundles on which the test case depends
82                 mavenBundle("org.opendaylight.controller", "sal",
83                         "0.5.0-SNAPSHOT"), // SAL connects the protocols
84                                            // plug-ins to other stuff
85                 mavenBundle("org.opendaylight.controller",
86                         "sal.implementation", "0.4.0-SNAPSHOT"),
87                 mavenBundle("org.opendaylight.controller",
88                         "protocol_plugins.stub", "0.4.0-SNAPSHOT"),
89                 // needed bundles by switchmanager
90                 mavenBundle("org.opendaylight.controller", "containermanager",
91                         "0.4.0-SNAPSHOT"),
92                 mavenBundle("org.opendaylight.controller",
93                         "containermanager.implementation", "0.4.0-SNAPSHOT"),
94                 mavenBundle("org.opendaylight.controller", "switchmanager",
95                         "0.5.0-SNAPSHOT"),
96                 mavenBundle("org.opendaylight.controller",
97                         "switchmanager.implementation", "0.4.0-SNAPSHOT"),
98                 // needed bundles by configuration
99                 mavenBundle("org.opendaylight.controller",
100                         "clustering.services", "0.4.0-SNAPSHOT"), // what are
101                                                                   // the
102                                                                   // clustering
103                                                                   // services
104                                                                   // for
105                 mavenBundle("org.opendaylight.controller", "clustering.stub",
106                         "0.4.0-SNAPSHOT"),
107                 mavenBundle("org.opendaylight.controller",
108                         "clustering.services-implementation", "0.4.0-SNAPSHOT"),
109                 mavenBundle("org.opendaylight.controller", "configuration",
110                         "0.4.0-SNAPSHOT"),
111                 mavenBundle("org.opendaylight.controller",
112                         "configuration.implementation", "0.4.0-SNAPSHOT"),
113                 junitBundles());
114     }
115
116     private String stateToString(int state) {
117         switch (state) {
118         case Bundle.ACTIVE:
119             return "ACTIVE";
120         case Bundle.INSTALLED:
121             return "INSTALLED";
122         case Bundle.RESOLVED:
123             return "RESOLVED";
124         case Bundle.UNINSTALLED:
125             return "UNINSTALLED";
126         default:
127             return "Not CONVERTED";
128         }
129     }
130
131     @Before
132     public void areWeReadyForClustering() {
133         assertNotNull(bc);
134         boolean debugit = false;
135         Bundle b[] = bc.getBundles();
136         for (int i = 0; i < b.length; i++) {
137             int state = b[i].getState();
138             if (state != Bundle.ACTIVE && state != Bundle.RESOLVED) {
139                 logger.debug("Bundle:" + b[i].getSymbolicName() + " state:"
140                         + stateToString(state));
141                 debugit = true;
142             }
143         }
144         if (debugit) {
145             logger.debug("Do some debugging because some bundle is unresolved");
146         }
147
148         // Assert if true, if false we are good to go!
149         assertFalse(debugit);
150         ServiceReference r = bc.getServiceReference(IClusterServices.class
151                 .getName());
152         if (r != null) {
153             this.clusterService = (IClusterServices) bc.getService(r);
154         }
155         // If StatisticsManager is null, cannot run tests.
156         assertNotNull(this.clusterService);
157
158     }
159
160     @Before
161     public void areWeReadyForConfiguration() {
162         assertNotNull(bc);
163         boolean debugit = false;
164         Bundle b[] = bc.getBundles();
165         for (int i = 0; i < b.length; i++) {
166             int state = b[i].getState();
167             if (state != Bundle.ACTIVE && state != Bundle.RESOLVED) {
168                 logger.debug("Bundle:" + b[i].getSymbolicName() + " state:"
169                         + stateToString(state));
170                 debugit = true;
171             }
172         }
173         if (debugit) {
174             logger.debug("Do some debugging because some bundle is unresolved");
175         }
176
177         // Assert if true, if false we are good to go!
178         assertFalse(debugit);
179         ServiceReference r = bc.getServiceReference(IConfigurationService.class
180                 .getName());
181         if (r != null) {
182             this.configService = (IConfigurationService) bc.getService(r);
183         }
184         // If StatisticsManager is null, cannot run tests.
185         assertNotNull(this.configService);
186
187     }
188
189     @Test
190     public void saveConfiguration() {
191         File f = new File(GlobalConstants.STARTUPHOME.toString());
192         boolean success = f.exists();
193         if (!success) {
194             success = f.mkdirs();
195         }
196         if(success){
197             Status status = new Status(StatusCode.SUCCESS, null);
198             if (this.configService != null) {
199                 status = this.configService.saveConfigurations();
200             }
201             if (status.getCode().equals(StatusCode.SUCCESS)) {
202                 Assert.assertTrue("Successfully saved config for "
203                         + configService.getClass().getSimpleName(), status
204                         .getCode().equals(StatusCode.SUCCESS));
205             } else {
206                 Assert.assertFalse("Failed to save config for "
207                         + configService.getClass().getSimpleName(), status
208                         .getCode().equals(StatusCode.INTERNALERROR));
209             }
210         }
211     }
212
213 }