From 7b63090190459364e2c4be3edf49ebb6256f6dab Mon Sep 17 00:00:00 2001 From: luxin Date: Wed, 15 Mar 2017 16:49:25 +0800 Subject: Onboarding Interface Validate&Lifecycle Test Onboarding Interface VNFSDK-78 Change-Id: Ic59ffb6d2b2ddbb128fa5634307ad16c5bb4f319 Signed-off-by: luxin --- .../vnfsdk/lctest/common/constant/Constant.java | 86 ++++++++ .../vnfsdk/lctest/common/constant/UrlConstant.java | 45 ++++ .../vnfsdk/lctest/common/util/RequestUtil.java | 128 +++++++++++ .../vnfsdk/lctest/common/util/RestfulUtil.java | 237 +++++++++++++++++++++ .../vnfsdk/lctest/resources/LifecycleResource.java | 47 ---- .../activator/LifecycleTestPostProcessor.java | 60 ++++++ .../impl/LifecycleTestAdapter2MSBManager.java | 79 +++++++ .../impl/LifecycleTestAdapterMgrService.java | 184 ++++++++++++++++ .../inf/ILifecycleTestAdapter2MSBManager.java | 52 +++++ .../inf/ILifecycleTestAdapterMgrService.java | 39 ++++ .../business/impl/VnfpackageServiceImpl.java | 83 ++++++++ .../service/business/inf/VnfpackageService.java | 49 +++++ .../vnfsdk/lctest/service/rest/SwaggerRoa.java | 53 +++++ .../vnfsdk/lctest/service/rest/VnfpackageRoa.java | 94 ++++++++ 14 files changed, 1189 insertions(+), 47 deletions(-) create mode 100644 lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/common/constant/Constant.java create mode 100644 lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/common/constant/UrlConstant.java create mode 100644 lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/common/util/RequestUtil.java create mode 100644 lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/common/util/RestfulUtil.java delete mode 100644 lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/resources/LifecycleResource.java create mode 100644 lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/service/activator/LifecycleTestPostProcessor.java create mode 100644 lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/service/adapter/impl/LifecycleTestAdapter2MSBManager.java create mode 100644 lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/service/adapter/impl/LifecycleTestAdapterMgrService.java create mode 100644 lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/service/adapter/inf/ILifecycleTestAdapter2MSBManager.java create mode 100644 lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/service/adapter/inf/ILifecycleTestAdapterMgrService.java create mode 100644 lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/service/business/impl/VnfpackageServiceImpl.java create mode 100644 lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/service/business/inf/VnfpackageService.java create mode 100644 lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/service/rest/SwaggerRoa.java create mode 100644 lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/service/rest/VnfpackageRoa.java (limited to 'lifecycle-test/src/main/java') diff --git a/lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/common/constant/Constant.java b/lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/common/constant/Constant.java new file mode 100644 index 0000000..732818e --- /dev/null +++ b/lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/common/constant/Constant.java @@ -0,0 +1,86 @@ +/* + * 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.openo.vnfsdk.lctest.common.constant; + +/** + *
+ *

+ *

+ * + * @author + * @version NFVO 0.5 Mar 14, 2017 + */ +public class Constant { + + private Constant() { + // private constants + } + + public static final int OK_CODE = 1; + + public static final int ERROR_CODE = -1; + + public static final String SEPARATOR = "file.separator"; + + public static final String REASON = "reason"; + + public static final String RETCODE = "retCode"; + + public static final int REPEAT_REG_TIME = 60 * 1000; + + public static final int HTTP_OK = 200; + + public static final int HTTP_CREATED = 201; + + public static final int HTTP_ACCEPTED = 202; + + public static final int HTTP_NOCONTENT = 204; + + public static final int HTTP_UNAUTHORIZED = 401; + + public static final int HTTP_BAD_REQUEST = 400; + + public static final int HTTP_NOTFOUND_CODE = 404; + + public static final int HTTP_CONFLICT_CODE = 409; + + public static final int HTTP_INVALID_PARAMETERS = 415; + + public static final int HTTP_INNERERROR_CODE = 500; + + public static final int INTERNAL_EXCEPTION_CODE = 600; + + public static final int TOKEN_HEAD_NULL_CODE = 601; + + public static final int TOKEN_USER_NULL_CODE = 602; + + public static final int SERVICE_URL_ERROR_CODE = 603; + + public static final int ACCESS_OBJ_NULL_CODE = 604; + + public static final int CONNECT_NOT_FOUND_CODE = 605; + + public static final int VCENTER_PARA_ERROR_CODE = 606; + + public static final int TYPE_PARA_ERROR_CODE = 607; + + public static final int CONNECT_FAIL_CODE = 608; + + public static final int DIS_CONNECT_FAIL_CODE = 609; + + public static final int HANDSHAKE_FAIL_CODE = 610; +} diff --git a/lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/common/constant/UrlConstant.java b/lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/common/constant/UrlConstant.java new file mode 100644 index 0000000..4acf225 --- /dev/null +++ b/lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/common/constant/UrlConstant.java @@ -0,0 +1,45 @@ +/* + * 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.openo.vnfsdk.lctest.common.constant; + +/** + *
+ *

+ *

+ * + * @author + * @version NFVO 0.5 Mar 14, 2017 + */ +public class UrlConstant { + + private UrlConstant() { + // private constructor + } + + /** + * MSB register url. + */ + public static final String REST_MSB_REGISTER = "/openoapi/microservices/v1/services"; + + /** + * NSLCM VNF package onboarding url. + */ + public static final String NSLCM_VNFPACKAGE_URL = "/openoapi/nslcm/v1/vnfpackage"; + + public static final String QUERY_VNFPACKAGE_URL = "/openoapi/nslcm/v1/vnfpackage/%s"; + +} diff --git a/lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/common/util/RequestUtil.java b/lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/common/util/RequestUtil.java new file mode 100644 index 0000000..788d904 --- /dev/null +++ b/lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/common/util/RequestUtil.java @@ -0,0 +1,128 @@ +/* + * 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.openo.vnfsdk.lctest.common.util; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; + +import org.apache.commons.io.IOUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import net.sf.json.JSONException; +import net.sf.json.JSONObject; + +/** + *
+ *

+ *

+ * + * @author + * @version NFVO 0.5 Mar 14, 2017 + */ +public class RequestUtil { + + private static final Logger LOGGER = LoggerFactory.getLogger(RequestUtil.class); + + /** + * Constructor
+ *

+ *

+ * + * @since NFVO 0.5 + */ + private RequestUtil() { + } + + /** + * Get context string from HttpServletRequest context + * + * @param context + * HttpServletRequest context + * @return the needed string in HttpServletRequest context + */ + public static String getStringRequestBody(HttpServletRequest context) { + try { + InputStream input = context.getInputStream(); + return IOUtils.toString(input); + } catch(IOException e) { + LOGGER.error("function=getStringRequestBody, get httpservletrequest body exception: {}", e); + return null; + } + } + + /** + * Get json parameter from HttpServletRequest context + * + * @param context + * HttpServletRequest context + * @return JSONObject + */ + public static JSONObject getJsonRequestBody(HttpServletRequest context) { + try { + String bodyStr = getStringRequestBody(context); + return JSONObject.fromObject(bodyStr); + } catch(JSONException e) { + LOGGER.error("function=getJsonRequestBody, maybe request param is not a jsonobject exception: {}", e); + return null; + } + } + + /** + * Get the body of all request in json format
+ * + * @param context + * The HttpServletRequest context + * @return JSONObject The body of all request in json format + * @since NFVO 0.5 + */ + public static JSONObject getAllJsonRequestBody(HttpServletRequest context) { + JSONObject requestBody = getJsonRequestBody(context); + if(null == requestBody) { + LOGGER.error("get httpservletrequest body exception"); + requestBody = new JSONObject(); + } + LOGGER.warn("function=getAllJsonRequestBody; msg=get request data is:[{}]", requestBody.toString()); + requestBody.put("header", getContextHeader(context)); + return requestBody; + } + + /** + * Get the context header
+ * + * @param context + * The http context + * @return Map context header + * @since NFVO 0.5 + */ + @SuppressWarnings("unchecked") + private static Map getContextHeader(HttpServletRequest context) { + Map header = new HashMap(); + Enumeration headerNames = context.getHeaderNames(); + while(headerNames.hasMoreElements()) { + String headerName = headerNames.nextElement(); + String value = context.getHeader(headerName); + header.put(headerName, value); + } + return header; + } +} diff --git a/lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/common/util/RestfulUtil.java b/lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/common/util/RestfulUtil.java new file mode 100644 index 0000000..c42c66a --- /dev/null +++ b/lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/common/util/RestfulUtil.java @@ -0,0 +1,237 @@ +/* + * 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.openo.vnfsdk.lctest.common.util; + +import java.util.HashMap; +import java.util.Map; + +import javax.servlet.http.HttpServletResponse; + +import org.apache.commons.lang3.StringUtils; +import org.openo.baseservice.remoteservice.exception.ServiceException; +import org.openo.baseservice.roa.util.restclient.Restful; +import org.openo.baseservice.roa.util.restclient.RestfulFactory; +import org.openo.baseservice.roa.util.restclient.RestfulOptions; +import org.openo.baseservice.roa.util.restclient.RestfulParametes; +import org.openo.baseservice.roa.util.restclient.RestfulResponse; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import net.sf.json.JSONException; +import net.sf.json.JSONObject; + +/** + *
+ *

+ *

+ * + * @author + * @version NFVO 0.5 Mar 14, 2017 + */ +public class RestfulUtil { + + public static final String TYPE_GET = "get"; + + public static final String TYPE_PUT = "put"; + + public static final String TYPE_POST = "post"; + + public static final String TYPE_DEL = "delete"; + + public static final String CONTENT_TYPE = "Content-type"; + + public static final String APPLICATION = "application/json"; + + private static final Logger LOGGER = LoggerFactory.getLogger(RestfulUtil.class); + + private static final Restful REST_CLIENT = RestfulFactory.getRestInstance(RestfulFactory.PROTO_HTTP); + + private RestfulUtil() { + } + + /** + * Get response object.
+ * + * @param url + * @param type + * @return + * @since NFVO 0.5 + */ + public static JSONObject getResponseObj(String url, String type) { + return getResponseObj(url, new RestfulParametes(), type); + } + + /** + * Get response object.
+ * + * @param url + * @param parametes + * @param type + * @return + * @since NFVO 0.5 + */ + public static JSONObject getResponseObj(String url, RestfulParametes parametes, String type) { + try { + String content = RestfulUtil.getResponseContent(url, parametes, null, type); + LOGGER.error("function=getResponseObj, content : {}", content); + if(StringUtils.isEmpty(content)) { + return null; + } + return JSONObject.fromObject(content); + } catch(JSONException e) { + LOGGER.error("function=getResponseObj, exception : {}", e); + return null; + } + } + + /** + * Get response content.
+ * + * @param url + * @param restParametes + * @param type + * @return + * @since NFVO 0.5 + */ + public static String getResponseContent(String url, RestfulParametes restParametes, String type) { + return getResponseContent(url, restParametes, null, type); + } + + /** + * Get response content.
+ * + * @param url + * @param restParametes + * @param opt + * @param type + * @return + * @since NFVO 0.5 + */ + public static String getResponseContent(String url, RestfulParametes restParametes, RestfulOptions opt, + String type) { + String responseContent = null; + RestfulResponse rsp = restfulResponse(url, restParametes, opt, type); + if(rsp != null) { + int httpStatus = rsp.getStatus(); + LOGGER.warn("function=getResponseContent, get response httpStatusCode : {} ", httpStatus); + if(httpStatus < HttpServletResponse.SC_BAD_REQUEST && httpStatus > 0) { + responseContent = rsp.getResponseContent(); + LOGGER.warn("function=getResponseContent, get response data success!responseContent={}", + responseContent); + } + } + return responseContent; + } + + /** + * Get restful response.
+ * + * @param url + * @param restParametes + * @param type + * @return + * @since NFVO 0.5 + */ + public static RestfulResponse getRestfulResponse(String url, RestfulParametes restParametes, String type) { + return restfulResponse(url, restParametes, null, type); + } + + private static RestfulResponse restfulResponse(String url, RestfulParametes restParametes, RestfulOptions opt, + String type) { + RestfulResponse rsp = new RestfulResponse(); + try { + + if(REST_CLIENT != null) { + if(TYPE_GET.equals(type)) { + rsp = REST_CLIENT.get(url, restParametes, opt); + } else if(TYPE_POST.equals(type)) { + rsp = REST_CLIENT.post(url, restParametes, opt); + } else if(TYPE_PUT.equals(type)) { + rsp = REST_CLIENT.put(url, restParametes, opt); + } else if(TYPE_DEL.equals(type)) { + rsp = REST_CLIENT.delete(url, restParametes, opt); + } + } + } catch(ServiceException e) { + LOGGER.error("function=restfulResponse, get restful response catch exception {} ", e); + } + LOGGER.warn("function=restfulResponse, response status is {} ", rsp.getStatus()); + return rsp; + } + + /** + *
+ * + * @param paramsMap + * @param params + * @return + * @since NFVO 0.5 + */ + public static String getRemoteResponseContent(Map paramsMap, String params) { + String responseContent = null; + RestfulResponse rsp = getRemoteResponse(paramsMap, params); + if(rsp != null) { + int httpStatus = rsp.getStatus(); + LOGGER.warn("function=getResponseContent, get response httpStatusCode : {} ", httpStatus); + if(httpStatus < HttpServletResponse.SC_BAD_REQUEST && httpStatus > 0) { + responseContent = rsp.getResponseContent(); + LOGGER.warn("function=getResponseContent, get response data success!responseContent={}", + responseContent); + } + } + return responseContent; + } + + /** + *
+ * + * @param paramsMap + * @param params + * @return + * @since NFVO 0.5 + */ + public static RestfulResponse getRemoteResponse(Map paramsMap, String params) { + String url = paramsMap.get("url"); + String methodType = paramsMap.get("methodType"); + + RestfulResponse rsp = null; + Restful rest = RestfulFactory.getRestInstance(RestfulFactory.PROTO_HTTP); + try { + + RestfulParametes restfulParametes = new RestfulParametes(); + Map headerMap = new HashMap<>(3); + headerMap.put(CONTENT_TYPE, APPLICATION); + restfulParametes.setHeaderMap(headerMap); + restfulParametes.setRawData(params); + + if(rest != null) { + if(TYPE_GET.equalsIgnoreCase(methodType)) { + rsp = rest.get(url, restfulParametes); + } else if(TYPE_POST.equalsIgnoreCase(methodType)) { + rsp = rest.post(url, restfulParametes); + } else if(TYPE_PUT.equalsIgnoreCase(methodType)) { + rsp = rest.put(url, restfulParametes); + } else if(TYPE_DEL.equalsIgnoreCase(methodType)) { + rsp = rest.delete(url, restfulParametes); + } + } + } catch(ServiceException e) { + LOGGER.error("function=getRemoteResponse, get restful response catch exception {}", e); + } + return rsp; + } +} diff --git a/lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/resources/LifecycleResource.java b/lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/resources/LifecycleResource.java deleted file mode 100644 index 4d60213..0000000 --- a/lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/resources/LifecycleResource.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright 2016-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.openo.vnfsdk.lctest.resources; - -import java.io.InputStream; - -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; - - - - - -/** - * csar package test service. - *

- *

- * - * @author 71221 - */ -@Path("/") -public class LifecycleResource { - - @Path("/csars") - @POST - @Produces(MediaType.APPLICATION_JSON) - public Response lctest(InputStream uploadedInputStream) { - //TODO code to be added for lctest , csar will be input in stream - return Response.ok().build(); - } -} diff --git a/lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/service/activator/LifecycleTestPostProcessor.java b/lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/service/activator/LifecycleTestPostProcessor.java new file mode 100644 index 0000000..f4eff0f --- /dev/null +++ b/lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/service/activator/LifecycleTestPostProcessor.java @@ -0,0 +1,60 @@ +/* + * 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.openo.vnfsdk.lctest.service.activator; + +import org.openo.vnfsdk.lctest.service.adapter.inf.ILifecycleTestAdapterMgrService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor; + +/** + *
+ *

+ *

+ * + * @author + * @version NFVO 0.5 Mar 14, 2017 + */ +public class LifecycleTestPostProcessor implements DestructionAwareBeanPostProcessor { + + private static final Logger LOG = LoggerFactory.getLogger(LifecycleTestPostProcessor.class); + + @Override + public Object postProcessAfterInitialization(Object bean, String name) throws BeansException { + if(bean instanceof ILifecycleTestAdapterMgrService) { + LOG.warn("Register to Microservice BUS!"); + ILifecycleTestAdapterMgrService lifecycleTestAdapterSvc = (ILifecycleTestAdapterMgrService)bean; + lifecycleTestAdapterSvc.register(); + } + + return bean; + } + + @Override + public Object postProcessBeforeInitialization(Object bean, String name) throws BeansException { + // TODO Auto-generated method stub + return bean; + } + + @Override + public void postProcessBeforeDestruction(Object bean, String name) throws BeansException { + // TODO Auto-generated method stub + + } + +} diff --git a/lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/service/adapter/impl/LifecycleTestAdapter2MSBManager.java b/lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/service/adapter/impl/LifecycleTestAdapter2MSBManager.java new file mode 100644 index 0000000..5e9a7b6 --- /dev/null +++ b/lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/service/adapter/impl/LifecycleTestAdapter2MSBManager.java @@ -0,0 +1,79 @@ +/* + * 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.openo.vnfsdk.lctest.service.adapter.impl; + +import java.util.Map; + +import org.openo.baseservice.roa.util.restclient.RestfulResponse; +import org.openo.vnfsdk.lctest.common.constant.Constant; +import org.openo.vnfsdk.lctest.common.util.RestfulUtil; +import org.openo.vnfsdk.lctest.service.adapter.inf.ILifecycleTestAdapter2MSBManager; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import net.sf.json.JSONObject; + +/** + *
+ *

+ *

+ * + * @author + * @version NFVO 0.5 Sep 22, 2016 + */ +public class LifecycleTestAdapter2MSBManager implements ILifecycleTestAdapter2MSBManager { + + private static final Logger LOG = LoggerFactory.getLogger(LifecycleTestAdapter2MSBManager.class); + + @Override + public JSONObject registerLifecycleTest(Map paramsMap, JSONObject driverInfo) { + JSONObject resultObj = new JSONObject(); + + RestfulResponse rsp = RestfulUtil.getRemoteResponse(paramsMap, driverInfo.toString()); + if(null == rsp) { + LOG.error("function=registerLifecycleTest, RestfulResponse is null"); + resultObj.put(Constant.REASON, "RestfulResponse is null."); + resultObj.put(Constant.RETCODE, Constant.ERROR_CODE); + return resultObj; + } + LOG.warn("function=registerLifecycleTest, status={}, content={}.", rsp.getStatus(), rsp.getResponseContent()); + String resultCreate = rsp.getResponseContent(); + + if(rsp.getStatus() == Constant.HTTP_CREATED) { + LOG.warn("function=registerLifecycleTest, result={}.", resultCreate); + resultObj = JSONObject.fromObject(resultCreate); + resultObj.put("retCode", Constant.HTTP_CREATED); + return resultObj; + } else if(rsp.getStatus() == Constant.HTTP_INVALID_PARAMETERS) { + LOG.error("function=registerLifecycleTest, msg=MSB return fail,invalid parameters,status={}, result={}.", + rsp.getStatus(), resultCreate); + resultObj.put("reason", "MSB return fail,invalid parameters."); + } else if(rsp.getStatus() == Constant.HTTP_INNERERROR_CODE) { + LOG.error("function=registerLifecycleTest, msg=MSB return fail,internal system error,status={}, result={}.", + rsp.getStatus(), resultCreate); + resultObj.put("reason", "MSB return fail,internal system error."); + } + resultObj.put("retCode", Constant.ERROR_CODE); + return resultObj; + } + + @Override + public JSONObject unregisterLifecycleTest(Map paramsMap) { + return new JSONObject(); + } + +} diff --git a/lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/service/adapter/impl/LifecycleTestAdapterMgrService.java b/lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/service/adapter/impl/LifecycleTestAdapterMgrService.java new file mode 100644 index 0000000..3c0b627 --- /dev/null +++ b/lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/service/adapter/impl/LifecycleTestAdapterMgrService.java @@ -0,0 +1,184 @@ +/* + * 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.openo.vnfsdk.lctest.service.adapter.impl; + +import java.io.BufferedInputStream; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.Executors; + +import org.openo.baseservice.util.impl.SystemEnvVariablesFactory; +import org.openo.vnfsdk.lctest.common.constant.Constant; +import org.openo.vnfsdk.lctest.common.constant.UrlConstant; +import org.openo.vnfsdk.lctest.common.util.RestfulUtil; +import org.openo.vnfsdk.lctest.service.adapter.inf.ILifecycleTestAdapter2MSBManager; +import org.openo.vnfsdk.lctest.service.adapter.inf.ILifecycleTestAdapterMgrService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Service; + +import net.sf.json.JSONObject; + +/** + *
+ *

+ *

+ * + * @author + * @version NFVO 0.5 Sep 22, 2016 + */ +@Service +public class LifecycleTestAdapterMgrService implements ILifecycleTestAdapterMgrService { + + private static final Logger LOG = LoggerFactory.getLogger(LifecycleTestAdapterMgrService.class); + + public static final String LIFECYCLETESTINFO = "lifecycletestinfo.json"; + + @Override + public void register() { + // set MSB URL and mothedtype + Map paramsMap = new HashMap<>(); + paramsMap.put("url", UrlConstant.REST_MSB_REGISTER); + paramsMap.put("methodType", RestfulUtil.TYPE_POST); + + // get LifecycleTest info and raise registration + try { + String lifecycleTest = readAdapterInfoFromJson(); + if(!"".equals(lifecycleTest)) { + JSONObject adapterObject = JSONObject.fromObject(lifecycleTest); + RegisterThread registerThread = new RegisterThread(paramsMap, adapterObject); + Executors.newSingleThreadExecutor().submit(registerThread); + } else { + LOG.error("LifecycleTest info is null,please check!"); + } + + } catch(IOException e) { + LOG.error("Failed to read LifecycleTest info! " + e.getMessage(), e); + } + + } + + /** + * Retrieve lifecycleTest information. + * + * @return + * @throws IOException + */ + public static String readAdapterInfoFromJson() throws IOException { + InputStream ins = null; + BufferedInputStream bins = null; + String fileContent = ""; + + String fileName = SystemEnvVariablesFactory.getInstance().getAppRoot() + System.getProperty(Constant.SEPARATOR) + + "etc" + System.getProperty(Constant.SEPARATOR) + "adapterInfo" + + System.getProperty(Constant.SEPARATOR) + LIFECYCLETESTINFO; + + try { + ins = new FileInputStream(fileName); + bins = new BufferedInputStream(ins); + + byte[] contentByte = new byte[ins.available()]; + int num = bins.read(contentByte); + + if(num > 0) { + fileContent = new String(contentByte); + } + } catch(FileNotFoundException e) { + LOG.error(fileName + "is not found!", e); + } finally { + if(ins != null) { + ins.close(); + } + if(bins != null) { + bins.close(); + } + } + + return fileContent; + } + + private static class RegisterThread implements Runnable { + + // Thread lock Object + private final Object lockObject = new Object(); + + private ILifecycleTestAdapter2MSBManager adapter2MSBMgr = new LifecycleTestAdapter2MSBManager(); + + // url and mothedtype + private Map paramsMap; + + // driver body + private JSONObject adapterInfo; + + public RegisterThread(Map paramsMap, JSONObject adapterInfo) { + this.paramsMap = paramsMap; + this.adapterInfo = adapterInfo; + } + + @Override + public void run() { + LOG.info("start register lifecycleTest", RegisterThread.class); + + if(paramsMap == null || adapterInfo == null) { + LOG.error("parameter is null,please check!", RegisterThread.class); + return; + } + + // catch Runtime Exception + try { + sendRequest(paramsMap, adapterInfo); + } catch(RuntimeException e) { + LOG.error(e.getMessage(), e); + } + + } + + private void sendRequest(Map paramsMap, JSONObject driverInfo) { + JSONObject resultObj = adapter2MSBMgr.registerLifecycleTest(paramsMap, driverInfo); + + if(Integer.valueOf(resultObj.get("retCode").toString()) == Constant.HTTP_CREATED) { + LOG.info("LifecycleTest has now Successfully Registered to the Microservice BUS!"); + } else { + LOG.error("LifecycleTest failed to Register to the Microservice BUS! Reason:" + + resultObj.get("reason").toString() + " retCode:" + resultObj.get("retCode").toString()); + + // if registration fails,wait one minute and try again + try { + synchronized(lockObject) { + lockObject.wait(Constant.REPEAT_REG_TIME); + } + } catch(InterruptedException e) { + LOG.error(e.getMessage(), e); + } + + sendRequest(this.paramsMap, this.adapterInfo); + } + + } + + } + + @Override + public void unregister() { + // unregister + } + +} diff --git a/lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/service/adapter/inf/ILifecycleTestAdapter2MSBManager.java b/lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/service/adapter/inf/ILifecycleTestAdapter2MSBManager.java new file mode 100644 index 0000000..ef1090a --- /dev/null +++ b/lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/service/adapter/inf/ILifecycleTestAdapter2MSBManager.java @@ -0,0 +1,52 @@ +/* + * 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.openo.vnfsdk.lctest.service.adapter.inf; + +import java.util.Map; + +import net.sf.json.JSONObject; + +/** + *
+ *

+ *

+ * + * @author + * @version NFVO 0.5 Mar 14, 2017 + */ +public interface ILifecycleTestAdapter2MSBManager { + + /** + *
+ * + * @param paramsMap + * @param driverInfo + * @return + * @since NFVO 0.5 + */ + JSONObject registerLifecycleTest(Map paramsMap, JSONObject driverInfo); + + /** + *
+ * + * @param paramsMap + * @return + * @since NFVO 0.5 + */ + JSONObject unregisterLifecycleTest(Map paramsMap); + +} diff --git a/lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/service/adapter/inf/ILifecycleTestAdapterMgrService.java b/lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/service/adapter/inf/ILifecycleTestAdapterMgrService.java new file mode 100644 index 0000000..399bb65 --- /dev/null +++ b/lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/service/adapter/inf/ILifecycleTestAdapterMgrService.java @@ -0,0 +1,39 @@ +/* + * 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.openo.vnfsdk.lctest.service.adapter.inf; + +/** + *
+ *

+ *

+ * + * @author + * @version NFVO 0.5 Mar 14, 2017 + */ +public interface ILifecycleTestAdapterMgrService { + + /** + * Lifecycle-test register interface. + */ + void register(); + + /** + * Lifecycle-test unregister interface. + */ + void unregister(); + +} diff --git a/lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/service/business/impl/VnfpackageServiceImpl.java b/lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/service/business/impl/VnfpackageServiceImpl.java new file mode 100644 index 0000000..43c418b --- /dev/null +++ b/lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/service/business/impl/VnfpackageServiceImpl.java @@ -0,0 +1,83 @@ +/* + * 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.openo.vnfsdk.lctest.service.business.impl; + +import java.util.HashMap; +import java.util.Map; + +import org.openo.vnfsdk.lctest.common.constant.UrlConstant; +import org.openo.vnfsdk.lctest.common.util.RestfulUtil; +import org.openo.vnfsdk.lctest.service.business.inf.VnfpackageService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import net.sf.json.JSONObject; + +/** + *
+ *

+ *

+ * + * @author + * @version NFVO 0.5 Mar 14, 2017 + */ +public class VnfpackageServiceImpl implements VnfpackageService { + + private static final Logger LOGGER = LoggerFactory.getLogger(VnfpackageServiceImpl.class); + + /** + *
+ * + * @param object + * @return + * @since NFVO 0.5 + */ + @Override + public JSONObject onboarding(JSONObject object) { + LOGGER.info("VnfpackageServiceImpl::onboarding:{}", object.toString()); + Map paramsMap = new HashMap<>(3); + paramsMap.put("url", UrlConstant.NSLCM_VNFPACKAGE_URL); + paramsMap.put("methodType", RestfulUtil.TYPE_POST); + String rsp = RestfulUtil.getRemoteResponseContent(paramsMap, object.toString()); + if(null == rsp) { + JSONObject resJson = new JSONObject(); + resJson.put("message", "NS LCM response Error."); + return resJson; + } + return JSONObject.fromObject(rsp); + } + + /** + *
+ * + * @param csarId + * @return + * @since NFVO 0.5 + */ + @Override + public JSONObject queryVnfpackage(String csarId) { + String url = String.format(UrlConstant.QUERY_VNFPACKAGE_URL, csarId); + JSONObject rsp = RestfulUtil.getResponseObj(url, RestfulUtil.TYPE_GET); + if(null == rsp) { + JSONObject resJson = new JSONObject(); + resJson.put("message", "NS LCM response Error."); + return resJson; + } + return rsp; + } + +} diff --git a/lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/service/business/inf/VnfpackageService.java b/lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/service/business/inf/VnfpackageService.java new file mode 100644 index 0000000..f7977ad --- /dev/null +++ b/lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/service/business/inf/VnfpackageService.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.openo.vnfsdk.lctest.service.business.inf; + +import net.sf.json.JSONObject; + +/** + *
+ *

+ *

+ * + * @author + * @version NFVO 0.5 Mar 14, 2017 + */ +public interface VnfpackageService { + + /** + *
+ * + * @param object + * @return + * @since NFVO 0.5 + */ + JSONObject onboarding(JSONObject object); + + /** + *
+ * + * @param csarId + * @return + * @since NFVO 0.5 + */ + JSONObject queryVnfpackage(String csarId); + +} diff --git a/lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/service/rest/SwaggerRoa.java b/lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/service/rest/SwaggerRoa.java new file mode 100644 index 0000000..2132d01 --- /dev/null +++ b/lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/service/rest/SwaggerRoa.java @@ -0,0 +1,53 @@ +/* + * 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.openo.vnfsdk.lctest.service.rest; + +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; + +import org.apache.commons.io.IOUtils; + +/** + *
+ *

+ *

+ * + * @author + * @version NFVO 0.5 Mar 14, 2017 + */ +@Path("/v1") +@Produces({MediaType.APPLICATION_JSON}) +public class SwaggerRoa { + + /** + * API doc. + * + * @param filename + * @return + * @throws IOException + */ + @GET + @Path("/swagger.json") + public String apidoc() throws IOException { + ClassLoader classLoader = getClass().getClassLoader(); + return IOUtils.toString(classLoader.getResourceAsStream("swagger.json")); + } +} diff --git a/lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/service/rest/VnfpackageRoa.java b/lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/service/rest/VnfpackageRoa.java new file mode 100644 index 0000000..4c909bd --- /dev/null +++ b/lifecycle-test/src/main/java/org/openo/vnfsdk/lctest/service/rest/VnfpackageRoa.java @@ -0,0 +1,94 @@ +/* + * 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.openo.vnfsdk.lctest.service.rest; + +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.MediaType; + +import org.openo.baseservice.remoteservice.exception.ServiceException; +import org.openo.vnfsdk.lctest.common.util.RequestUtil; +import org.openo.vnfsdk.lctest.service.business.inf.VnfpackageService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import net.sf.json.JSONObject; + +/** + *
+ *

+ *

+ * + * @author + * @version NFVO 0.5 Mar 14, 2017 + */ +@Path("/v1/vnfpackage") +@Produces(MediaType.APPLICATION_JSON) +@Consumes(MediaType.APPLICATION_JSON) +public class VnfpackageRoa { + + private static final Logger LOGGER = LoggerFactory.getLogger(VnfpackageRoa.class); + + private VnfpackageService vnfpackageService; + + /** + *
+ * + * @param context + * @return + * @throws ServiceException + * @since NFVO 0.5 + */ + @POST + public JSONObject onboarding(@Context HttpServletRequest context) throws ServiceException { + JSONObject object = RequestUtil.getJsonRequestBody(context); + if(null == object) { + LOGGER.error("function=onboarding; msg=context is null."); + throw new ServiceException("org.openo.vnfsdk.lctest.service.rest.VnfpackageRoa.onboarding.null"); + } + + LOGGER.info("VnfpackageRoa::onboarding:{}", object.toString()); + return vnfpackageService.onboarding(object); + } + + /** + *
+ * + * @param context + * @param csarId + * @return + * @throws ServiceException + * @since NFVO 0.5 + */ + @GET + @Path("/{csarId}") + public JSONObject queryVnfpackage(@Context HttpServletRequest context, @PathParam("csarId") String csarId) + throws ServiceException { + LOGGER.warn("function=queryVnfpackage, csarId={}", csarId); + return vnfpackageService.queryVnfpackage(csarId); + } + + public void setVnfpackageService(VnfpackageService vnfpackageService) { + this.vnfpackageService = vnfpackageService; + } +} -- cgit 1.2.3-korg