summaryrefslogtreecommitdiffstats
path: root/src/main
diff options
context:
space:
mode:
authorKanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>2017-08-11 10:16:29 +0530
committerKanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>2017-08-11 10:27:22 +0530
commit29674db1d9b7a062402b2e0a7a94b3be775c7cb9 (patch)
treea212fbe8389c53835c37494ab86d8f8cb13513fc /src/main
parenta36ade39de9a97b318bf9a64441571d71affac5f (diff)
Add seed code for swagger-sdk
Change-Id: I7471cdb03d1a958bf8e47ee6917b67ab560b9c16 Issue-Id: MSB-14 Signed-off-by: Kanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/org/onap/swagger/bean/BeanConfigPostProcessor.java80
-rw-r--r--src/main/java/org/onap/swagger/service/rest/SwaggerRoa.java49
-rw-r--r--src/main/java/org/onap/swagger/util/AdapterInfoUtil.java212
-rw-r--r--src/main/resources/META-INF/onap-swagger-sdk/swagger-config.xml75
-rw-r--r--src/main/resources/swagger.properties.sample51
5 files changed, 467 insertions, 0 deletions
diff --git a/src/main/java/org/onap/swagger/bean/BeanConfigPostProcessor.java b/src/main/java/org/onap/swagger/bean/BeanConfigPostProcessor.java
new file mode 100644
index 0000000..cc107bb
--- /dev/null
+++ b/src/main/java/org/onap/swagger/bean/BeanConfigPostProcessor.java
@@ -0,0 +1,80 @@
+/*
+ * Copyright 2017 Huawei Technologies Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onap.swagger.bean;
+
+import io.swagger.jaxrs.config.BeanConfig;
+import org.onap.swagger.util.AdapterInfoUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.config.BeanPostProcessor;
+
+/**
+ * <br>
+ * <p>
+ * Bean post processor for setting the ip and port into swaggerConfig bean from adapter file.
+ * </p>
+ *
+ * @author
+ */
+public class BeanConfigPostProcessor implements BeanPostProcessor {
+
+ private static final String SWAGGER_BEAN_ID = "swaggerConfig";
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(BeanConfigPostProcessor.class);
+
+ /**
+ * Post process after bean initialization.<br/>
+ *
+ * @param bean obj
+ * @param beanName string
+ * @return object
+ * @throws BeansException exception
+ */
+ @Override
+ public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
+ if (SWAGGER_BEAN_ID.equals(beanName)) {
+ BeanConfig config = (BeanConfig) bean;
+ AdapterInfoUtil util = AdapterInfoUtil.getInstance();
+ if (util.getIp() != null && !"".equals(util.getIp()) && util.getPort() != null
+ && !"".equals(util.getPort())) {
+ config.setHost(util.getIp() + ":" + util.getPort());
+ LOGGER.info("BeanConfigPostProcessor : Host Details=" + util.getIp() + ":" + util.getPort());
+ return config;
+ }
+ LOGGER.info("BeanConfigPostProcessor : Host Details=" + config.getHost());
+ }
+ return bean;
+ }
+
+ /**
+ * Post processor.<br/>
+ *
+ * @param bean
+ * obj
+ * @param beanName
+ * String
+ * @return object
+ * @throws BeansException
+ * exception
+ */
+ @Override
+ public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
+ return bean;
+ }
+
+}
diff --git a/src/main/java/org/onap/swagger/service/rest/SwaggerRoa.java b/src/main/java/org/onap/swagger/service/rest/SwaggerRoa.java
new file mode 100644
index 0000000..23c5d12
--- /dev/null
+++ b/src/main/java/org/onap/swagger/service/rest/SwaggerRoa.java
@@ -0,0 +1,49 @@
+/*
+ * Copyright 2017 Huawei Technologies Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onap.swagger.service.rest;
+
+import org.apache.commons.io.IOUtils;
+
+import java.io.IOException;
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+
+/**
+ * Swagger API Doc.<br/>
+ *
+ * @author
+ * @version CLIENT 1.0 Jan 24, 2017
+ */
+@Path("/")
+@Produces({ MediaType.APPLICATION_JSON })
+public class SwaggerRoa {
+
+ /**
+ * API doc.
+ *
+ * @return string
+ * @throws IOException exception
+ */
+ @GET
+ @Path("/swagger.json")
+ public String apidoc() throws IOException {
+ ClassLoader classLoader = this.getClass().getClassLoader();
+ return IOUtils.toString(classLoader.getResourceAsStream("swagger.json"));
+ }
+}
diff --git a/src/main/java/org/onap/swagger/util/AdapterInfoUtil.java b/src/main/java/org/onap/swagger/util/AdapterInfoUtil.java
new file mode 100644
index 0000000..128e6c9
--- /dev/null
+++ b/src/main/java/org/onap/swagger/util/AdapterInfoUtil.java
@@ -0,0 +1,212 @@
+/*
+ * Copyright 2017 Huawei Technologies Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onap.swagger.util;
+
+import net.sf.json.JSONArray;
+import net.sf.json.JSONObject;
+import org.apache.commons.io.IOUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Iterator;
+import java.util.Properties;
+
+/**
+ * Loads adapter ip and port.
+ *
+ */
+public class AdapterInfoUtil {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(AdapterInfoUtil.class);
+
+ private static final String SWAGGER = "swagger.properties";
+
+ private static final String SWAGGER_ADAPTER_PROP = "service-config-file-path";
+
+ private static final String HOST_IP = "ip";
+
+ private static final String HOST_PORT = "port";
+
+ private static final String CATALINA_BASE = "catalina.base";
+
+ private static AdapterInfoUtil util;
+
+ private String ip;
+
+ private String port;
+
+ private AdapterInfoUtil() {
+ loadJson();
+ }
+
+ /**
+ * Get AdapterInfoUtil instance.
+ *
+ * @return AdapterInfoUtil
+ */
+ public static AdapterInfoUtil getInstance() {
+ if (util == null) {
+ util = new AdapterInfoUtil();
+ }
+
+ return util;
+ }
+
+ /**
+ * Get IP.
+ *
+ * @return string
+ */
+ public String getIp() {
+ if (ip != null) {
+ return ip;
+ }
+ return "";
+ }
+
+ /**
+ * Get port.
+ *
+ * @return string
+ */
+ public String getPort() {
+ if (port != null) {
+ return port;
+ }
+ return "";
+ }
+
+ /**
+ * Loads the json file from given service-config-file-path, if not then from class path.
+ */
+ private void loadJson() {
+ LOGGER.info("Loading service json...");
+ InputStream is;
+ String jsonTxt = null;
+ try {
+
+ String catalinaDir = getAppRoot();
+ LOGGER.info("Catalina base dir = " + catalinaDir);
+
+ FileInputStream fin = new FileInputStream(
+ AdapterInfoUtil.class.getClassLoader().getResource(SWAGGER).getFile());
+ Properties properties = new Properties();
+ properties.load(fin);
+ String configFilePath = properties.getProperty(SWAGGER_ADAPTER_PROP);
+ LOGGER.info("adapterfile = " + configFilePath);
+ if (configFilePath == null || "".equals(configFilePath)) {
+ LOGGER.error(SWAGGER_ADAPTER_PROP + " is not set in swagger.properties. "
+ + "Default ip and port will be considered.");
+ return;
+ }
+ File file = new File(configFilePath);
+ if (file.exists()) {
+ LOGGER.info("Loading using given path...");
+ is = new FileInputStream(configFilePath);
+ } else {
+ is = this.getClass().getResourceAsStream(configFilePath);
+ if (is != null) {
+ LOGGER.info("Loading using classpath...");
+ is = new FileInputStream(
+ AdapterInfoUtil.class.getClassLoader().getResource(configFilePath).getFile());
+ } else {
+ LOGGER.info("Loading using relative path...");
+ if (!configFilePath.startsWith(File.separator)) {
+ configFilePath = File.separator + configFilePath;
+ }
+ configFilePath = catalinaDir + configFilePath;
+ file = new File(configFilePath);
+ if (file.exists()) {
+ is = new FileInputStream(new File(configFilePath));
+ } else {
+ LOGGER.error(SWAGGER_ADAPTER_PROP + " is incorrect in swagger.properties. "
+ + "Default ip and port will be considered.");
+ return;
+ }
+ }
+ }
+ jsonTxt = IOUtils.toString(is);
+ LOGGER.info("jsonTxt = " + jsonTxt);
+
+ } catch (FileNotFoundException e) {
+ LOGGER.error("File Exception", e);
+ } catch (IOException e) {
+ LOGGER.error("File Exception", e);
+ }
+ if (jsonTxt != null) {
+ JSONObject temp = JSONObject.fromObject(jsonTxt);
+ if (temp != null) {
+ Iterator it = temp.keys();
+ while (it.hasNext()) {
+ String key = (String) it.next();
+ if (key.endsWith("nodes")) {
+ JSONArray lineItems = (JSONArray) temp.get(key);
+ for (Object o : lineItems) {
+ JSONObject jsonLineItem = (JSONObject) o;
+ String ip1 = jsonLineItem.getString(HOST_IP);
+ String port1 = jsonLineItem.getString(HOST_PORT);
+ if (ip1 != null && port1 != null) {
+ this.ip = ip1;
+ this.port = port1;
+ }
+ }
+ }
+ }
+
+ }
+ }
+ }
+
+ /**
+ * Read catalina base.
+ *
+ * @return String
+ */
+ public String getAppRoot() {
+ String appRoot;
+ appRoot = System.getProperty(CATALINA_BASE);
+ if (appRoot != null) {
+ appRoot = getCanonicalPath(appRoot);
+ }
+ return appRoot;
+ }
+
+ /**
+ * Get canonical path.
+ *
+ * @param inPath
+ * string
+ * @return string
+ */
+ private String getCanonicalPath(String inPath) {
+ String path = null;
+ try {
+ if (inPath != null) {
+ File file = new File(inPath);
+ path = file.getCanonicalPath();
+ }
+ } catch (IOException e) {
+ LOGGER.error("IOException", e);
+ }
+ return path;
+ }
+}
diff --git a/src/main/resources/META-INF/onap-swagger-sdk/swagger-config.xml b/src/main/resources/META-INF/onap-swagger-sdk/swagger-config.xml
new file mode 100644
index 0000000..ee9be49
--- /dev/null
+++ b/src/main/resources/META-INF/onap-swagger-sdk/swagger-config.xml
@@ -0,0 +1,75 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Copyright (c) 2017 Huawei Technologies Co., Ltd. and others. All rights reserved.
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<beans xmlns="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxrs="http://cxf.apache.org/jaxrs"
+ xmlns:context="http://www.springframework.org/schema/context"
+ xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">
+
+
+ <!--<context:property-placeholder location="classpath:swagger.properties"
+ order="0" ignore-unresolvable="true" ignoreResourceNotFound="true" /> -->
+
+ <bean id="swaggerproperties"
+ class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
+ <property name="location" value="classpath:swagger.properties" />
+ <property name="order" value="0" />
+ <property name="ignoreUnresolvablePlaceholders" value="true" />
+ <property name="ignoreResourceNotFound" value="true" />
+ </bean>
+
+ <!-- Swagger writers -->
+ <bean id="swaggerWriter" class="io.swagger.jaxrs.listing.SwaggerSerializers" />
+
+ <!-- Swagger API listing resource -->
+ <bean id="swaggerResource" class="io.swagger.jaxrs.listing.ApiListingResource" />
+
+
+ <!-- this scans the classes for resources -->
+ <bean id="swaggerConfig" class="io.swagger.jaxrs.config.BeanConfig">
+ <property name="version" value="${api-version}" />
+ <property name="title" value="${api-title}" />
+ <property name="description" value="${api-description}" />
+ <property name="host" value="${api-host-ip}:${api-host-port}" />
+ <property name="basePath" value="${api-base-path}" />
+ <property name="resourcePackage" value="${api-rest-package}" />
+ <property name="scan" value="${api-rest-package-scan}" />
+ </bean>
+
+ <bean class="org.onap.swagger.bean.BeanConfigPostProcessor" />
+
+ <bean id="sRoa" class="org.onap.swagger.service.rest.SwaggerRoa" />
+
+ <jaxrs:server id="api-doc" address="${api-swagger-uri}">
+ <jaxrs:serviceBeans>
+ <ref bean="sRoa" />
+ </jaxrs:serviceBeans>
+ </jaxrs:server>
+
+<!-- TODO (mrkanag) : Remove below settings to redirect from /${api-swagger-uri} and if service already
+ having static swagger.json, then it will be used otherwise this bean will be used to generate
+ and respond to user request-->
+ <jaxrs:server id="swagger" address="${api-swagger-uri}/api-doc">
+ <jaxrs:serviceBeans>
+ <ref bean="swaggerResource" />
+ </jaxrs:serviceBeans>
+ <jaxrs:providers>
+ <ref bean="swaggerWriter" />
+ </jaxrs:providers>
+ </jaxrs:server>
+
+
+</beans>
diff --git a/src/main/resources/swagger.properties.sample b/src/main/resources/swagger.properties.sample
new file mode 100644
index 0000000..77128e2
--- /dev/null
+++ b/src/main/resources/swagger.properties.sample
@@ -0,0 +1,51 @@
+# Copyright 2017 Huawei Technologies Co., Ltd.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Used to do wwagger configuration.
+# ONAP Service REST API version
+api-version=v1
+
+# ONAP Service REST API swagger document title
+api-title=Swagger REST API
+
+# ONAP Service REST API swagger document description
+api-description=Swagger REST API
+
+# ONAP Service REST API supported protocols
+api-schemas=http,https
+
+# ONAP Service REST API ROOT URI
+api-base-path=/onapapi/
+
+# swagger.json URI on top of api-base-path. so final URI would as follows
+#<api-base-path>/<api-swagger-uri>/swagger.json
+api-swagger-uri=/v1
+
+# Set the root level java package path, where REST API implemented.
+api-rest-package=
+
+# Enables swagger to scan the ROA defining the REST API
+api-rest-package-scan=true
+
+# License details emebeded in generated swagger.json
+api-license=https://wiki.onap.org/display/DW/Apache+2.0+License
+
+# if service-config-file-path does not have api-host-ip and api-host-port,
+# then ip and port defined here will be used as default
+api-host-ip=127.0.0.1
+api-host-port=8480
+
+# Set the below file path to service specific configuration file path
+service-config-file-path=etc/adapterInfo/service.json
+