From fdd6a02d934205cd7b468b1c97be2dfed3293c8e Mon Sep 17 00:00:00 2001 From: Venu Nanditha Jampani Date: Wed, 22 Mar 2017 18:24:58 +0530 Subject: IMPLEMENTED THREE NEW INTERFACES. issue_id:VNFSDK-21 Change-Id: Ib04a6fcbe134ca365dcb1ff3bd4c4c9a0b1a2c9c Signed-off-by: Venu Nanditha Jampani --- .../externalservice/entity/Environment.java | 67 ++++++++++ .../externalservice/entity/EnvironmentMap.java | 49 +++++++ .../externalservice/entity/OperationStatus.java | 54 ++++++++ .../entity/OperationStatusHandler.java | 74 +++++++++++ .../vnfsdk/functest/resource/CommonManager.java | 68 ++++++++++ .../functest/responsehandler/TestResult.java | 54 ++++++++ .../functest/responsehandler/TestResultParser.java | 143 +++++++++++++++++++++ .../VnfFuncTestResponseHandler.java | 19 +++ 8 files changed, 528 insertions(+) create mode 100644 vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/externalservice/entity/Environment.java create mode 100644 vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/externalservice/entity/EnvironmentMap.java create mode 100644 vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/externalservice/entity/OperationStatus.java create mode 100644 vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/externalservice/entity/OperationStatusHandler.java create mode 100644 vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/responsehandler/TestResult.java create mode 100644 vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/responsehandler/TestResultParser.java (limited to 'vnf-sdk-function-test/src/main') diff --git a/vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/externalservice/entity/Environment.java b/vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/externalservice/entity/Environment.java new file mode 100644 index 0000000..aba3fc8 --- /dev/null +++ b/vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/externalservice/entity/Environment.java @@ -0,0 +1,67 @@ +/* + * 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.functest.externalservice.entity; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public class Environment { + + public String getRemoteIp() { + return RemoteIp; + } + + public void setRemoteIp(String remoteIp) { + RemoteIp = remoteIp; + } + + public String getUserName() { + return UserName; + } + + public void setUserName(String userName) { + UserName = userName; + } + + public String getPassword() { + return Password; + } + + public void setPassword(String password) { + Password = password; + } + + public String getPath() { + return Path; + } + + public void setPath(String path) { + Path = path; + } + + @JsonProperty("RemoteIp") + private String RemoteIp; + + @JsonProperty("UserName") + private String UserName; + + @JsonProperty("Password") + private String Password; + + @JsonProperty("Path") + private String Path; + +} diff --git a/vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/externalservice/entity/EnvironmentMap.java b/vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/externalservice/entity/EnvironmentMap.java new file mode 100644 index 0000000..4b347d3 --- /dev/null +++ b/vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/externalservice/entity/EnvironmentMap.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.functest.externalservice.entity; + +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +public class EnvironmentMap { + + private static Map envmap = new HashMap(); + + private static EnvironmentMap oInstance = new EnvironmentMap(); + + private EnvironmentMap() { + // Empty nothing to do + } + + public static synchronized EnvironmentMap getInstance() { + return oInstance; + } + + public synchronized Map getEnvmap() { + return envmap; + } + + public synchronized void addEnv(UUID uuid, Environment envobj) { + envmap.put(uuid, envobj); + } + + public synchronized void delEnv(UUID uuid) { + envmap.remove(uuid); + } + +} diff --git a/vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/externalservice/entity/OperationStatus.java b/vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/externalservice/entity/OperationStatus.java new file mode 100644 index 0000000..96b43af --- /dev/null +++ b/vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/externalservice/entity/OperationStatus.java @@ -0,0 +1,54 @@ +/* + * 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.functest.externalservice.entity; + +public class OperationStatus { + + private boolean operFinished = false; + + public enum operResultCode { + SUCCESS, FAILURE, NOTFOUND + }; + + operResultCode oResultCode; + + public operResultCode getoResultCode() { + return oResultCode; + } + + public void setoResultCode(operResultCode oResultCode) { + this.oResultCode = oResultCode; + } + + private String operResultMessage; + + public String getOperResultMessage() { + return operResultMessage; + } + + public void setOperResultMessage(String operResultMessage) { + this.operResultMessage = operResultMessage; + } + + public boolean isOperFinished() { + return operFinished; + } + + public void setOperFinished(boolean operFinished) { + this.operFinished = operFinished; + } +} diff --git a/vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/externalservice/entity/OperationStatusHandler.java b/vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/externalservice/entity/OperationStatusHandler.java new file mode 100644 index 0000000..fd93136 --- /dev/null +++ b/vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/externalservice/entity/OperationStatusHandler.java @@ -0,0 +1,74 @@ +/* + * 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.functest.externalservice.entity; + +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +import javax.ws.rs.core.Response; + +import org.openo.vnfsdk.functest.util.RestResponseUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class OperationStatusHandler { + + private static final Logger LOGGER = LoggerFactory.getLogger(OperationStatusHandler.class); + + private static Map operStatusMap = new HashMap(); + + private static OperationStatusHandler oInstance = new OperationStatusHandler(); + + private OperationStatusHandler() { + // Empty nothing to do + } + + public static synchronized OperationStatusHandler getInstance() { + return oInstance; + } + + public static synchronized Map getOperStatusMap() { + return operStatusMap; + } + + public static synchronized void setOperStatusMap(UUID uuid, OperationStatus InputOperStatusMap) { + operStatusMap.put(uuid, InputOperStatusMap); + } + + public Response operationStatusfunc(UUID uuid) { + + if(OperationStatusHandler.getOperStatusMap().containsKey(uuid)) { + + OperationStatus operstatus = OperationStatusHandler.getOperStatusMap().get(uuid); + LOGGER.info("Operation Finished?" + operstatus.isOperFinished()); + LOGGER.info("Operation Result Message" + operstatus.getOperResultMessage()); + + return RestResponseUtil.getSuccessResponse(operstatus); + } else { + OperationStatus operstatus = new OperationStatus(); + operstatus.setOperFinished(true); + operstatus.setoResultCode(OperationStatus.operResultCode.NOTFOUND); + LOGGER.error("uuid not found"); + + return RestResponseUtil.getSuccessResponse(operstatus); + + } + + } + +} diff --git a/vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/resource/CommonManager.java b/vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/resource/CommonManager.java index ee8ced0..9563f52 100644 --- a/vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/resource/CommonManager.java +++ b/vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/resource/CommonManager.java @@ -27,6 +27,7 @@ import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; @@ -38,12 +39,16 @@ import javax.ws.rs.core.Response; import org.eclipse.jetty.http.HttpStatus; import org.openo.vnfsdk.functest.FileUtil; import org.openo.vnfsdk.functest.TaskExecution; +import org.openo.vnfsdk.functest.externalservice.entity.Environment; +import org.openo.vnfsdk.functest.externalservice.entity.EnvironmentMap; +import org.openo.vnfsdk.functest.externalservice.entity.OperationStatusHandler; import org.openo.vnfsdk.functest.responsehandler.VnfFuncTestResponseHandler; import org.openo.vnfsdk.functest.util.RestResponseUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.codahale.metrics.annotation.Timed; +import com.fasterxml.jackson.databind.ObjectMapper; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; @@ -57,6 +62,40 @@ public class CommonManager { private static final Logger LOGGER = LoggerFactory.getLogger(CommonManager.class); + @POST + @Path("/setenv") + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + @Timed + public Response setEnvironment(String env) { + LOGGER.info("set Environment"); + + try { + + // Generate UUID for each environment + final UUID uniqueKey = UUID.randomUUID(); + + // Convert input string to Environment class + ObjectMapper mapper = new ObjectMapper(); + Environment envObj = mapper.readValue(env, Environment.class); + if(null == envObj) { + // Converting input to Env object failed + return null; + } + + // Set to the environment map + EnvironmentMap.getInstance().addEnv(uniqueKey, envObj); + + // Send REST response + return RestResponseUtil.getSuccessResponse(uniqueKey); + + } catch(Exception e) { + LOGGER.error("Setting the Environment Fail", e); + } + + return null; + } + @Path("") @POST @ApiOperation(value = "execute the function test") @@ -124,6 +163,35 @@ public class CommonManager { return VnfFuncTestResponseHandler.getInstance().getResponseByFuncTestId(instanceId); } + @Path("/status/{operationId}") + @GET + @ApiOperation(value = "get function test result by id") + @Produces(MediaType.APPLICATION_JSON) + @ApiResponses(value = { + @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found", response = String.class), + @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415, message = "Unprocessable MicroServiceInfo Entity ", response = String.class), + @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "internal server error", response = String.class)}) + @Timed + public Response getOperationResult(@ApiParam(value = "operationId") @PathParam("operationId") String operationId) { + LOGGER.info("Query functest status by id." + operationId); + + return OperationStatusHandler.getInstance().operationStatusfunc(UUID.fromString(operationId)); + } + + @Path("/download/{functestId}") + @GET + @ApiOperation(value = "get function test result by id") + @Produces(MediaType.APPLICATION_JSON) + @ApiResponses(value = { + @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found", response = String.class), + @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415, message = "Unprocessable MicroServiceInfo Entity ", response = String.class), + @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "internal server error", response = String.class)}) + @Timed + public Response downloadResults(@ApiParam(value = "functestId") @PathParam("functestId") String funcTestId) { + LOGGER.info("query functest result by id." + funcTestId); + return VnfFuncTestResponseHandler.getInstance().downloadResults(funcTestId); + } + /** * Convert the stream to File Name
* diff --git a/vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/responsehandler/TestResult.java b/vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/responsehandler/TestResult.java new file mode 100644 index 0000000..4704717 --- /dev/null +++ b/vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/responsehandler/TestResult.java @@ -0,0 +1,54 @@ +/* + * 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.functest.responsehandler; + +/** + * @author Administrator + */ +public class TestResult { + + private String name; + + private String description; + + private String status; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } + +} diff --git a/vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/responsehandler/TestResultParser.java b/vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/responsehandler/TestResultParser.java new file mode 100644 index 0000000..40a781a --- /dev/null +++ b/vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/responsehandler/TestResultParser.java @@ -0,0 +1,143 @@ +/* + * 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.functest.responsehandler; + +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.openo.vnfsdk.functest.FileUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.w3c.dom.Document; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + +public class TestResultParser { + + private static final String statusPass = "PASS"; + + private static final String ResultTag = "test"; + + private static final String NameTag = "name"; + + private static final String StatusTag = "status"; + + private static final String KwTag = "kw"; + + private static final String DocTag = "doc"; + + private static final Logger logger = LoggerFactory.getLogger(TestResultParser.class); + + public List populateResultList(String xmlFile) { + List resultData = new ArrayList<>(); + if(!FileUtil.checkFileExist(xmlFile)) { + logger.error("File Not Found !!! :" + xmlFile); + return resultData; + } + parseResultData(xmlFile, resultData); + return resultData; + } + + private void parseResultData(String xmlFile, List resultData) { + try { + Document doc = createDocument(xmlFile); + NodeList list = doc.getElementsByTagName(ResultTag); + for(int i = 0; i < list.getLength(); i++) { + Node node = list.item(i); + if(node.getNodeType() != Node.ELEMENT_NODE) { + continue; + } + + NamedNodeMap attr = node.getAttributes(); + if(null == attr) { + continue; + } + + String nameAttr = getNodeValue(attr.getNamedItem(NameTag)); + if(null == nameAttr) { + continue; + } + + String descriptionAttr = nameAttr; + String statusAttr = statusPass; + NodeList childlist = node.getChildNodes(); + for(int j = 0; j < childlist.getLength(); j++) { + Node childNode = childlist.item(j); + if(childNode.getNodeType() != Node.ELEMENT_NODE) { + continue; + } + + if(KwTag == childNode.getNodeName()) { + NodeList kwNodeList = childNode.getChildNodes(); + for(int k = 0; k < kwNodeList.getLength(); k++) { + Node descNode = kwNodeList.item(k); + if(descNode.getNodeType() != Node.ELEMENT_NODE) { + continue; + } + + if(DocTag == descNode.getNodeName()) { + if(null != descNode.getTextContent()) { + descriptionAttr = descNode.getTextContent(); + break; + } + } + } + } + + if(StatusTag == childNode.getNodeName()) { + NamedNodeMap statusAttrMap = childNode.getAttributes(); + if(null != statusAttrMap) { + statusAttr = getNodeValue(statusAttrMap.getNamedItem(StatusTag)); + } + } + } + + TestResult testData = new TestResult(); + testData.setName(nameAttr); + testData.setDescription(descriptionAttr); + testData.setStatus(statusAttr); + + resultData.add(testData); + } + } catch(ParserConfigurationException | SAXException | IOException e) { + logger.error("Exception while parsing file :" + xmlFile); + logger.error("Exception while parsing file :", e); + } + } + + private Document createDocument(String fileName) throws ParserConfigurationException, SAXException, IOException { + InputStream inputStream = new FileInputStream(fileName); + DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); + Document doc = docBuilder.parse(inputStream); + doc.getDocumentElement().normalize(); + return doc; + } + + private String getNodeValue(Node namedItem) { + return (null != namedItem) ? namedItem.getNodeValue() : null; + } +} diff --git a/vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/responsehandler/VnfFuncTestResponseHandler.java b/vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/responsehandler/VnfFuncTestResponseHandler.java index fe79027..7dd47d7 100644 --- a/vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/responsehandler/VnfFuncTestResponseHandler.java +++ b/vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/responsehandler/VnfFuncTestResponseHandler.java @@ -19,6 +19,7 @@ package org.openo.vnfsdk.functest.responsehandler; import java.io.File; import java.io.FileInputStream; import java.io.IOException; +import java.util.List; import java.util.Map; import javax.ws.rs.core.Response; @@ -37,6 +38,8 @@ public class VnfFuncTestResponseHandler { private static int error = 22; + private static String resultFileName = "output.xml"; + private static String resultpathkey = "DIR_RESULT"; private static Map mapConfigValues; @@ -110,6 +113,22 @@ public class VnfFuncTestResponseHandler { } } + public Response downloadResults(String funcTestId) { + + if((null == mapConfigValues) || (null == mapConfigValues.get(resultpathkey))) { + logger.warn("Result Store path not configfured !!!"); + return RestResponseUtil.getErrorResponse(error); + } + + String resultPath = mapConfigValues.get(resultpathkey); + String resultfileName = resultPath + File.separator + funcTestId + File.separator + resultFileName; + + TestResultParser oTestResultParser = new TestResultParser(); + List resultList = oTestResultParser.populateResultList(resultfileName); + return (!resultList.isEmpty()) ? RestResponseUtil.getSuccessResponse(resultList) + : RestResponseUtil.getErrorResponse(error); + } + @SuppressWarnings("unchecked") private static void loadConfigurations() { String curDir = System.getProperty("user.dir"); -- cgit 1.2.3-korg