diff options
Diffstat (limited to 'sdnr/wireless-transport/code-Carbon-SR1/apps/dlux/loader')
8 files changed, 566 insertions, 0 deletions
diff --git a/sdnr/wireless-transport/code-Carbon-SR1/apps/dlux/loader/impl/README.md b/sdnr/wireless-transport/code-Carbon-SR1/apps/dlux/loader/impl/README.md new file mode 100644 index 00000000..a906405a --- /dev/null +++ b/sdnr/wireless-transport/code-Carbon-SR1/apps/dlux/loader/impl/README.md @@ -0,0 +1,11 @@ + +The loader uses the following files from build: + +cp ../../dlux-web/build/index.html ./src/main/resources/index/ +cp ../../dlux-web/build/src/main.js ./src/main/resources/dlux/src/main.js +cp ../../dlux-web/build/src/app/app.controller.js ./src/main/resources/dlux/src/app/app.controller.js +cp ../../dlux-web/build/src/app/app.module.js ./src/main/resources/dlux/src/app/app.module.js +cp ../../dlux-web/build/src/app/routingConfig.js ./src/main/resources/dlux/src/app/routingConfig.js +cp -r ../../dlux-web/build/vendor/* ./src/main/resources/dlux/vendor/ +cp -r ../../dlux-web/build/assets/* ./src/main/resources/dlux/assets/ +rm -rf ./src/main/resources/dlux/assets/yang2xml
\ No newline at end of file diff --git a/sdnr/wireless-transport/code-Carbon-SR1/apps/dlux/loader/impl/pom.xml b/sdnr/wireless-transport/code-Carbon-SR1/apps/dlux/loader/impl/pom.xml new file mode 100644 index 00000000..a7892d2c --- /dev/null +++ b/sdnr/wireless-transport/code-Carbon-SR1/apps/dlux/loader/impl/pom.xml @@ -0,0 +1,198 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.opendaylight.dlux</groupId> + <artifactId>dlux-parent</artifactId> + <version>0.5.1-Carbon</version> + <relativePath>../../</relativePath> + </parent> + <artifactId>loader.implementation</artifactId> + <packaging>bundle</packaging> + <dependencies> + <dependency> + <groupId>org.opendaylight.dlux</groupId> + <artifactId>dlux-web</artifactId> + <version>${project.parent.version}</version> + </dependency> + <dependency> + <groupId>org.opendaylight.dlux</groupId> + <artifactId>loader</artifactId> + <version>${project.parent.version}</version> + </dependency> + <dependency> + <groupId>org.osgi</groupId> + <artifactId>org.osgi.core</artifactId> + </dependency> + <dependency> + <groupId>org.osgi</groupId> + <artifactId>org.osgi.compendium</artifactId> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-api</artifactId> + </dependency> + <dependency> + <groupId>com.google.guava</groupId> + <artifactId>guava</artifactId> + </dependency> + <dependency> + <groupId>javax.servlet</groupId> + <artifactId>javax.servlet-api</artifactId> + <version>${javax.servlet-api.version}</version> + </dependency> + <dependency> + <groupId>org.opendaylight.dlux</groupId> + <artifactId>dlux.loader.resources</artifactId> + <version>${loader.resources.version}</version> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-simple</artifactId> + <scope>test</scope> + </dependency> + </dependencies> + <build> + <resources> + <resource> + <directory>target/generated-resources</directory> + </resource> + <resource> + <directory>src/main/resources</directory> + </resource> + </resources> + <plugins> + <!--Clean extracted resources--> + <plugin> + <artifactId>maven-clean-plugin</artifactId> + <version>2.5</version> + <configuration> + <filesets> + <fileset> + <directory>${project.basedir}/src/main/resources/</directory> + <includes> + <include>dlux/</include> + <include>index/</include> + </includes> + <excludes></excludes> + <followSymlinks>false</followSymlinks> + </fileset> + </filesets> + </configuration> + </plugin> + <!--Copy resources from the dlux-web/build dir--> + <plugin> + <artifactId>maven-resources-plugin</artifactId> + <version>2.6</version> + <executions> + <execution> + <id>copy-src-resources</id> + <phase>validate</phase> + <goals> + <goal>copy-resources</goal> + </goals> + <configuration> + <outputDirectory>${project.basedir}/target/generated-resources/dlux</outputDirectory> + <resources> + <resource> + <directory>${project.basedir}/../../dlux-web/build/</directory> + <includes> + <include>src/main.js</include> + <include>src/app/app.controller.js</include> + <include>src/app/app.module.js</include> + <include>src/app/routingConfig.js</include> + <include>src/common/config/env.module.js</include> + <include>vendor/**</include> + <include>assets/**</include> + </includes> + <excludes> + <exclude>assets/yang2xml/</exclude> + </excludes> + <filtering>false</filtering> + </resource> + </resources> + </configuration> + </execution> + <execution> + <id>copy-index</id> + <phase>validate</phase> + <goals> + <goal>copy-resources</goal> + </goals> + <configuration> + <outputDirectory>${project.basedir}/target/generated-resources/index</outputDirectory> + <resources> + <resource> + <directory>${project.basedir}/../../dlux-web/build/</directory> + <includes> + <include>index.html</include> + </includes> + <filtering>false</filtering> + </resource> + </resources> + </configuration> + </execution> + </executions> + </plugin> + <!-- <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-dependency-plugin</artifactId> + <version>2.6</version> + <executions> + <execution> + <id>unpack-loader-resources</id> + <goals> + <goal>unpack-dependencies</goal> + </goals> + <phase>generate-resources</phase> + <configuration> + <outputDirectory>${project.build.directory}/generated-resources</outputDirectory> + <groupId>org.opendaylight.dlux</groupId> + <includeArtifactIds>dlux.loader.resources</includeArtifactIds> + <excludes>META-INF\/**</excludes> + <excludeTransitive>true</excludeTransitive> + <ignorePermissions>false</ignorePermissions> + </configuration> + </execution> + </executions> + </plugin> --> + <plugin> + + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-checkstyle-plugin</artifactId> + <configuration> + <!-- Exclusion doesn't seem to work for the yang.xml files within the resources folders --> + <excludes>**/*</excludes> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.felix</groupId> + <artifactId>maven-bundle-plugin</artifactId> + <version>2.4.0</version> + <extensions>true</extensions> + <configuration> + <instructions> + <Import-Package>org.osgi.framework, + org.opendaylight.dlux.loader, + org.osgi.service.http, + javax.servlet, + javax.servlet.http,* + </Import-Package> + <Export-Package>org.opendaylight.dlux.loader.implementation</Export-Package> + </instructions> + </configuration> + </plugin> + </plugins> + </build> + <scm> + <connection>scm:git:ssh://git.opendaylight.org:29418/dlux.git</connection> + <developerConnection>scm:git:ssh://git.opendaylight.org:29418/dlux.git</developerConnection> + <tag>HEAD</tag> + <url>https://wiki.opendaylight.org/view/OpenDaylight_dlux:Main</url> + </scm> +</project> diff --git a/sdnr/wireless-transport/code-Carbon-SR1/apps/dlux/loader/impl/src/main/java/org/opendaylight/dlux/loader/exception/DluxLoaderException.java b/sdnr/wireless-transport/code-Carbon-SR1/apps/dlux/loader/impl/src/main/java/org/opendaylight/dlux/loader/exception/DluxLoaderException.java new file mode 100644 index 00000000..8f521a80 --- /dev/null +++ b/sdnr/wireless-transport/code-Carbon-SR1/apps/dlux/loader/impl/src/main/java/org/opendaylight/dlux/loader/exception/DluxLoaderException.java @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.dlux.loader.exception; + +public class DluxLoaderException extends Exception { + + public DluxLoaderException(final String message) { + super(message); + } + + public DluxLoaderException(final String message, final Throwable throwable) { + super(message, throwable); + } +} diff --git a/sdnr/wireless-transport/code-Carbon-SR1/apps/dlux/loader/impl/src/main/java/org/opendaylight/dlux/loader/implementation/DluxLoader.java b/sdnr/wireless-transport/code-Carbon-SR1/apps/dlux/loader/impl/src/main/java/org/opendaylight/dlux/loader/implementation/DluxLoader.java new file mode 100644 index 00000000..df49a5e9 --- /dev/null +++ b/sdnr/wireless-transport/code-Carbon-SR1/apps/dlux/loader/impl/src/main/java/org/opendaylight/dlux/loader/implementation/DluxLoader.java @@ -0,0 +1,71 @@ +/** + * Copyright (c) 2014 Inocybe Technologies, and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.dlux.loader.implementation; + +import javax.servlet.ServletException; + +import com.google.common.base.Preconditions; +import org.opendaylight.dlux.loader.DluxModuleLoader; +import org.opendaylight.dlux.loader.Module; +import org.opendaylight.dlux.loader.exception.DluxLoaderException; +import org.osgi.service.http.HttpService; +import org.osgi.service.http.NamespaceException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.List; + +public class DluxLoader implements DluxModuleLoader { + + private DluxLoaderIndexServlet index; + private static Logger logger = LoggerFactory.getLogger(DluxLoader.class); + + /** + * List of modules registered with dlux + */ + private List<Module> modules = new ArrayList<>(); + + private String RESOURCE_URL = "/"; + + private String RESOURCE_DIRECTORY = "/dlux"; + + private String SERVLET_URL = "/index.html"; + + @Override + public void addModule(Module module){ + modules.add(module); + } + + @Override + public void removeModule(Module module) { + modules.remove(module); + } + + public List<Module> getModules() { + return modules; + } + + public void onUnbindService(HttpService httpService) { + httpService.unregister(SERVLET_URL); + httpService.unregister(RESOURCE_URL); + index = null; + } + + public void onBindService(HttpService httpService) throws ServletException, NamespaceException, DluxLoaderException { + Preconditions.checkNotNull(httpService, + "Unable to inject HttpService into DluxLoader. dlux modules won't work without httpService"); + + index = new DluxLoaderIndexServlet(this); + httpService.registerServlet(SERVLET_URL, index, null, null); + httpService.registerResources(RESOURCE_URL, RESOURCE_DIRECTORY, null); + logger.info("DluxLoader Service initialization complete."); + } + +} diff --git a/sdnr/wireless-transport/code-Carbon-SR1/apps/dlux/loader/impl/src/main/java/org/opendaylight/dlux/loader/implementation/DluxLoaderIndexServlet.java b/sdnr/wireless-transport/code-Carbon-SR1/apps/dlux/loader/impl/src/main/java/org/opendaylight/dlux/loader/implementation/DluxLoaderIndexServlet.java new file mode 100644 index 00000000..55929c0b --- /dev/null +++ b/sdnr/wireless-transport/code-Carbon-SR1/apps/dlux/loader/impl/src/main/java/org/opendaylight/dlux/loader/implementation/DluxLoaderIndexServlet.java @@ -0,0 +1,184 @@ +/** + * Copyright (c) 2014 Inocybe Technologies, and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.dlux.loader.implementation; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Preconditions; +import org.opendaylight.dlux.loader.Module; +import org.opendaylight.dlux.loader.exception.DluxLoaderException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; + +public class DluxLoaderIndexServlet extends HttpServlet{ + + private static final long serialVersionUID = 1L; + private static Logger logger = LoggerFactory.getLogger(DluxLoaderIndexServlet.class); + + private String DEFINEJS_PROPERTY = "defineJS"; + + private String REQUIREJS_PROPERTY = "requireJS"; + + private String ANGULARJS_PROPERTY = "angularJS"; + + private final String DEFINEJS_START = "var module = ["; + + private final String REQUIREJS_START = "var deps = ["; + + private final String ANGULARJS_START = "var e = ["; + + private final String end = "];"; + + private final String COMMA_QUOTE = ",\'"; + + private final String QUOTE = "\'"; + + private final String NEWLINE = "\n"; + + private final DluxLoader loader; + + private final String UTF_CHARSET = "UTF-8"; + + private final String JAVASCRIPT_REPLACE_PROPERTY = "javascriptReplaceString"; + + private final String CSS_REPLACE_PROPERTY = "cssReplaceString"; + + private final String INDEX_HTML_LOC = "index/index.html"; + + private final String RESPONSE_CONTENT_TYPE = "text/html"; + + private final String CSS_LINK_START = "<link rel=\"stylesheet\" href=\""; + + private final String CSS_LINK_END = "\" />"; + + private final Properties prop; + + private final List<String> indexHTML; + + public DluxLoaderIndexServlet(final DluxLoader loader) throws DluxLoaderException { + Preconditions.checkNotNull(loader, "Loader service can not be null"); + this.loader = loader; + this.prop = loadProperties(); + this.indexHTML = loadStaticHTML(); + } + + private Properties loadProperties() throws DluxLoaderException { + + Properties prop = new Properties(); + + try(InputStream inputStream = DluxLoaderIndexServlet.class.getClassLoader().getResourceAsStream("CommonAppModules.properties")){ + Preconditions.checkNotNull(inputStream, "Could not load the module properties file"); + prop.load(inputStream); + + } catch (IOException e) { + logger.error("Could not load properties from input stream", e); + throw new DluxLoaderException("Dlux Loader Servlet initialization failed. ", e); + } + return prop; + } + + private List<String> loadStaticHTML() throws DluxLoaderException{ + List<String> indexHTMLContent = new ArrayList<>(); + try(InputStream indexHTMLStream = DluxLoaderIndexServlet.class.getClassLoader().getResourceAsStream(INDEX_HTML_LOC); + BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(indexHTMLStream, UTF_CHARSET))) { + String line = bufferedReader.readLine(); + while(line != null) { + indexHTMLContent.add(line); + line = bufferedReader.readLine(); + } + + } catch (IOException e) { + logger.error("Could not load index html from input stream", e); + throw new DluxLoaderException("Dlux Loader Servlet initialization failed. ", e); + } + return indexHTMLContent; + } + + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + String jsReplace = prop.getProperty(JAVASCRIPT_REPLACE_PROPERTY); + String cssReplace = prop.getProperty(CSS_REPLACE_PROPERTY); + Preconditions.checkNotNull(jsReplace, "Global requireJS replace string should be present in properties file"); + Preconditions.checkNotNull(cssReplace, "Global css replace string should be present in properties file"); + Preconditions.checkArgument(indexHTML.size() > 0, "HTML file was not loaded properly at servlet initialization"); + + StringBuilder inputStringBuilder = new StringBuilder(); + for (String line : this.indexHTML) { + // add global variables + if(line.contains(jsReplace)) { + inputStringBuilder.append(getModulesString()); + inputStringBuilder.append(NEWLINE); + } + + if(line.contains(cssReplace)) { + inputStringBuilder.append(getCSSString()); + inputStringBuilder.append(NEWLINE); + } + inputStringBuilder.append(line); + inputStringBuilder.append(NEWLINE); + + } + response.setContentType(RESPONSE_CONTENT_TYPE); + PrintWriter out = response.getWriter(); + out.print(inputStringBuilder.toString()); + } + + private String getModulesString() { + StringBuilder defineJsBuilder = new StringBuilder(); + StringBuilder requireJsBuilder = new StringBuilder(); + StringBuilder angularBuilder = new StringBuilder(); + defineJsBuilder.append(DEFINEJS_START).append(prop.getProperty(DEFINEJS_PROPERTY)); + requireJsBuilder.append(REQUIREJS_START).append(prop.getProperty(REQUIREJS_PROPERTY)); + angularBuilder.append(ANGULARJS_START).append(prop.getProperty(ANGULARJS_PROPERTY)); + for (Module module: loader.getModules()){ + requireJsBuilder.append(COMMA_QUOTE).append(module.getRequireJs()).append(QUOTE); + angularBuilder.append(COMMA_QUOTE).append(module.getAngularJs()).append(QUOTE); + } + defineJsBuilder.append(end).append(NEWLINE); + requireJsBuilder.append(end).append(NEWLINE); + angularBuilder.append(end); + + return defineJsBuilder.toString() + requireJsBuilder.toString() + angularBuilder.toString(); + } + + private String getCSSString() { + StringBuilder cssBuilder = new StringBuilder(); + for(Module module : loader.getModules()) { + if(module.getCssDependencies() == null) { + continue; + } + for(String cssDependency : module.getCssDependencies()) { + cssBuilder.append(CSS_LINK_START).append(cssDependency).append(CSS_LINK_END).append(NEWLINE); + } + } + + return cssBuilder.toString(); + } + + @VisibleForTesting + public Properties getProp() { + return this.prop; + } + + @VisibleForTesting + public List<String> getIndexHTML() { + return indexHTML; + } +} diff --git a/sdnr/wireless-transport/code-Carbon-SR1/apps/dlux/loader/impl/src/main/resources/CommonAppModules.properties b/sdnr/wireless-transport/code-Carbon-SR1/apps/dlux/loader/impl/src/main/resources/CommonAppModules.properties new file mode 100644 index 00000000..183e46d0 --- /dev/null +++ b/sdnr/wireless-transport/code-Carbon-SR1/apps/dlux/loader/impl/src/main/resources/CommonAppModules.properties @@ -0,0 +1,5 @@ +defineJS= 'angular','ocLazyLoad','angular-ui-router','angular-translate', 'angular-sanitize', 'angular-translate-loader-static-files', 'angular-translate-loader-partial', 'angular-css-injector' +requireJS='common/config/env.module' +angularJS='oc.lazyLoad', 'ui.router', 'pascalprecht.translate', 'ngSanitize', 'angular.css.injector', 'app' +javascriptReplaceString=global variables +cssReplaceString=application CSS
\ No newline at end of file diff --git a/sdnr/wireless-transport/code-Carbon-SR1/apps/dlux/loader/impl/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/sdnr/wireless-transport/code-Carbon-SR1/apps/dlux/loader/impl/src/main/resources/OSGI-INF/blueprint/blueprint.xml new file mode 100644 index 00000000..ee732dcc --- /dev/null +++ b/sdnr/wireless-transport/code-Carbon-SR1/apps/dlux/loader/impl/src/main/resources/OSGI-INF/blueprint/blueprint.xml @@ -0,0 +1,10 @@ +<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"> + <bean id="loaderImpl" class="org.opendaylight.dlux.loader.implementation.DluxLoader"/> + + <reference id="onBindService" availability="mandatory" activation="eager" interface="org.osgi.service.http.HttpService"> + <reference-listener ref="loaderImpl" bind-method="onBindService" unbind-method="onUnbindService"/> + </reference> + + <service id="serviceRegistration" interface="org.opendaylight.dlux.loader.DluxModuleLoader" ref="loaderImpl"/> + +</blueprint>
\ No newline at end of file diff --git a/sdnr/wireless-transport/code-Carbon-SR1/apps/dlux/loader/impl/src/test/java/org/opendaylight/dlux/loader/DluxLoaderIndexServletTest.java b/sdnr/wireless-transport/code-Carbon-SR1/apps/dlux/loader/impl/src/test/java/org/opendaylight/dlux/loader/DluxLoaderIndexServletTest.java new file mode 100644 index 00000000..eb1c9055 --- /dev/null +++ b/sdnr/wireless-transport/code-Carbon-SR1/apps/dlux/loader/impl/src/test/java/org/opendaylight/dlux/loader/DluxLoaderIndexServletTest.java @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.dlux.loader; + +import org.junit.Test; +import org.junit.Assert; +import org.opendaylight.dlux.loader.exception.DluxLoaderException; +import org.opendaylight.dlux.loader.implementation.DluxLoader; +import org.opendaylight.dlux.loader.implementation.DluxLoaderIndexServlet; + +import java.util.List; +import java.util.Properties; + + +public class DluxLoaderIndexServletTest { + + private DluxLoader dluxLoader = new DluxLoader(); + + @Test + public void testLoadModulePropertyFile() throws DluxLoaderException { + + DluxLoaderIndexServlet indexServlet = new DluxLoaderIndexServlet(dluxLoader); + Properties properties = indexServlet.getProp(); + String defineJS = properties.getProperty("defineJS"); + Assert.assertNotNull(defineJS); + Assert.assertTrue(defineJS.contains("'angular',")); + String requireJS = properties.getProperty("requireJS"); + Assert.assertNotNull(requireJS); + Assert.assertTrue(requireJS.contains("common/config/env.module")); + String angularJS = properties.getProperty("angularJS"); + Assert.assertNotNull(angularJS); + Assert.assertTrue(angularJS.contains("'ui.router',")); + } + + @Test + public void testLoadIndexHTML() throws DluxLoaderException { + DluxLoaderIndexServlet indexServlet = new DluxLoaderIndexServlet(dluxLoader); + List<String> indexHTML = indexServlet.getIndexHTML(); + Properties properties = indexServlet.getProp(); + + String jsReplace = properties.getProperty("javascriptReplaceString"); + String cssReplace = properties.getProperty("cssReplaceString"); + + boolean containsJSReplace = false; + boolean containsCssReplace = false; + + for(String line : indexHTML) { + if(line.contains(jsReplace)) { + containsJSReplace = true; + } + + if(line.contains(cssReplace)) { + containsCssReplace = true; + } + } + + Assert.assertTrue(containsCssReplace); + Assert.assertTrue(containsJSReplace); + } + +} |