From 29674db1d9b7a062402b2e0a7a94b3be775c7cb9 Mon Sep 17 00:00:00 2001 From: Kanagaraj Manickam k00365106 Date: Fri, 11 Aug 2017 10:16:29 +0530 Subject: Add seed code for swagger-sdk Change-Id: I7471cdb03d1a958bf8e47ee6917b67ab560b9c16 Issue-Id: MSB-14 Signed-off-by: Kanagaraj Manickam k00365106 --- .gitignore | 8 + pom.xml | 73 +++++++ .../onap/swagger/bean/BeanConfigPostProcessor.java | 80 ++++++++ .../org/onap/swagger/service/rest/SwaggerRoa.java | 49 +++++ .../org/onap/swagger/util/AdapterInfoUtil.java | 212 +++++++++++++++++++++ .../META-INF/onap-swagger-sdk/swagger-config.xml | 75 ++++++++ src/main/resources/swagger.properties.sample | 51 +++++ 7 files changed, 548 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/java/org/onap/swagger/bean/BeanConfigPostProcessor.java create mode 100644 src/main/java/org/onap/swagger/service/rest/SwaggerRoa.java create mode 100644 src/main/java/org/onap/swagger/util/AdapterInfoUtil.java create mode 100644 src/main/resources/META-INF/onap-swagger-sdk/swagger-config.xml create mode 100644 src/main/resources/swagger.properties.sample diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..97767e1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +bin/ +target/ +coverage-report/ +.project +.settings +.classpath +.class +.checkstyle diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..945727e --- /dev/null +++ b/pom.xml @@ -0,0 +1,73 @@ + + + + 4.0.0 + + + org.onap.oparent + oparent + 1.0.0-SNAPSHOT + + + org.onap.msb.swagger-sdk + swagger-sdk + 1.0.0-SNAPSHOT + jar + + swagger-sdk + + + + io.swagger + swagger-core + 1.5.3 + + + javax.ws.rs + jsr311-api + + + + + io.swagger + swagger-jaxrs + 1.5.3 + + + org.slf4j + slf4j-log4j12 + 1.6.1 + + + org.springframework + spring-beans + 3.1.0.RELEASE + + + net.sf.json-lib + json-lib + 2.4 + jdk15 + + + org.apache.commons + commons-io + 1.3.2 + + + 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; + +/** + *
+ *

+ * Bean post processor for setting the ip and port into swaggerConfig bean from adapter file. + *

+ * + * @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.
+ * + * @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.
+ * + * @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.
+ * + * @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 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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 +#//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 + -- cgit 1.2.3-korg