From 870d2f5b2d6d7a8670453395dd59993ccccdc8af Mon Sep 17 00:00:00 2001 From: Kanagaraj Manickam Date: Tue, 4 Aug 2020 12:32:12 +0530 Subject: Add profile support Issue-ID: VNFSDK-533 Change-Id: I4f40cfadbd29a086ed1256e286229c4a569c3f8d Signed-off-by: Kanagaraj Manickam k00365106 --- .../org/onap/vtp/profile/VTPProfileResource.java | 340 +++++++++++++++++++++ .../org/onap/vtp/profile/model/VTPTestProfile.java | 102 +++++++ .../src/main/webapp/WEB-INF/web.xml | 3 +- 3 files changed, 444 insertions(+), 1 deletion(-) create mode 100644 vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/profile/VTPProfileResource.java create mode 100644 vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/profile/model/VTPTestProfile.java diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/profile/VTPProfileResource.java b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/profile/VTPProfileResource.java new file mode 100644 index 00000000..880bfefa --- /dev/null +++ b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/profile/VTPProfileResource.java @@ -0,0 +1,340 @@ +/** + * Copyright 2018 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.vtp.profile; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +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 javax.ws.rs.core.Response; + +import org.apache.commons.io.IOUtils; +import org.eclipse.jetty.http.HttpStatus; +import org.onap.vtp.VTPResource; +import org.onap.vtp.error.VTPError; +import org.onap.vtp.error.VTPError.VTPException; +import org.onap.vtp.profile.model.VTPTestProfile; +import org.onap.vtp.profile.model.VTPTestProfile.VTPTestProfileList; +import org.onap.vtp.profile.model.VTPTestProfile.VTPTestProfileProperty; + +import com.google.gson.Gson; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; + +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiParam; +import io.swagger.annotations.ApiResponse; +import io.swagger.annotations.ApiResponses; + +@Path("/vtp") +@Api(tags = {"VTP Envrionment Profile"}) +public class VTPProfileResource extends VTPResource { + + public VTPTestProfileList listTestProfilesHandler() throws VTPException, IOException{ + List args = new ArrayList<>(); + args.addAll(Arrays.asList(new String[] { + "--product", "open-cli", "profile-list", "--format", "json" + })); + + JsonObject results = this.makeRpcAndGetJson(args).getAsJsonObject(); + + VTPTestProfileList list = new VTPTestProfileList(); + + if (results != null && results.isJsonArray() && results.getAsJsonArray().size() > 0) { + JsonArray resultsArray = results.getAsJsonArray(); + for (Iterator it = resultsArray.iterator(); it.hasNext();) { + JsonElement jsonElement = it.next(); + JsonObject n = jsonElement.getAsJsonObject(); + list.getTestProfiles().add(new VTPTestProfile().setName(n.get("profile").getAsString())); + } + } + + return list; + } + + @Path("/profiles") + @GET + @ApiOperation(tags = "VTP Envrionment Profile", value = "List available profiles", response = VTPTestProfile.class, responseContainer = "List") + @Produces(MediaType.APPLICATION_JSON) + @ApiResponses(value = { + @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, + message = "Failed to perform the operation", + response = VTPError.class) }) + public Response listTestProfiles() throws VTPException, IOException { + return Response.ok(this.listTestProfilesHandler().getTestProfiles().toString(), MediaType.APPLICATION_JSON).build(); + } + + public VTPTestProfile getTestProfileHandler(String profileName) throws VTPException, IOException { + List args = new ArrayList<>(); + args.addAll(Arrays.asList(new String[] { + "--product", "open-cli", "profile-show", "--profile", profileName, "--format", "json" + })); + JsonObject results = this.makeRpcAndGetJson(args).getAsJsonObject(); + + VTPTestProfile profile = new VTPTestProfile(); + profile.setName(profileName); + + if (results != null && results.isJsonArray() && results.getAsJsonArray().size() > 0) { + JsonArray resultsArray = results.getAsJsonArray(); + for (Iterator it = resultsArray.iterator(); it.hasNext();) { + JsonElement jsonElement = it.next(); + JsonObject n = jsonElement.getAsJsonObject(); + VTPTestProfileProperty prp = new VTPTestProfileProperty(); + + prp.setInputParameterName(n.get("parameter").getAsString()); + prp.setValue(n.get("value").getAsString()); + + if (n.get("service") != null && !n.get("service").getAsString().equals("*")) + prp.setTestSuiteName(n.get("service").getAsString()); + + if (n.get("command") != null && !n.get("command").getAsString().equals("*")) + prp.setTestCaseName(n.get("command").getAsString()); + + if (n.get("product") != null && !n.get("product").getAsString().equals("*")) + prp.setScenario(n.get("product").getAsString()); + + profile.getProperties().add(prp); + } + } + + return profile; + } + + @Path("/profiles/{profileName}") + @GET + @ApiOperation(tags = "VTP Envrionment Profile", value = " Retrieve profile details", response = VTPTestProfile.class) + @Produces(MediaType.APPLICATION_JSON) + @ApiResponses(value = { + @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, + message = "Failed to perform the operation", response = VTPError.class), + @ApiResponse(code = HttpStatus.NOT_FOUND_404, + message = "Test profile does not exist", response = VTPError.class)}) + public Response getTestProfile( + @ApiParam(value = "Test profile name") @PathParam("profileName") String profileName) + throws IOException, VTPException { + return Response.ok(this.getTestProfileHandler(profileName).toString(), MediaType.APPLICATION_JSON).build(); + } + + public void setPofileHanlder(VTPTestProfile profile) throws VTPException, IOException { + List args = new ArrayList<>(); + + + args.addAll(Arrays.asList(new String[] { + "--product", "open-cli", "profile-set", "--format", "json", "--profile", profile.getName() + })); + + Map> productVsProfile = new HashMap<>(); + + for (VTPTestProfileProperty prp: profile.getProperties()) { + String scenario = prp.getScenario(); + + if (prp.getScenario() == null ) { + scenario = "__global__"; + } + + if (productVsProfile.get(scenario) == null) { + productVsProfile.put(scenario, new ArrayList()); + } + + if ((prp.getInputParameterName() != null) && !prp.getInputParameterName().isEmpty() && + (prp.getValue() != null) && !prp.getValue().isEmpty()) { + + String paramName = ""; + if (prp.getTestSuiteName() != null && !prp.getTestSuiteName().isEmpty()) { + paramName += prp.getTestSuiteName() + ":"; + + if (prp.getTestCaseName() != null && !prp.getTestCaseName().isEmpty()) { + paramName += prp.getTestCaseName() + ":"; + } + } + + paramName += prp.getInputParameterName(); + + productVsProfile.get(scenario).add("--parameter"); + productVsProfile.get(scenario).add(paramName + "=" + prp.getValue()); + } + } + + for (String scenario: productVsProfile.keySet()) { + List arguments = new ArrayList<>(); + arguments.addAll(args); + arguments.addAll(productVsProfile.get(scenario)); + + if (!scenario.equals("__global__")) { //profile is set across. + arguments.add("--product"); + arguments.add(scenario); + } + + this.makeRpcAndGetJson(arguments); + } + } + + public void unsetPofileHanlder(VTPTestProfile profile) throws VTPException, IOException { + List args = new ArrayList<>(); + + + args.addAll(Arrays.asList(new String[] { + "--product", "open-cli", "profile-unset", "--format", "json", "--profile", profile.getName() + })); + + Map> productVsProfile = new HashMap<>(); + + for (VTPTestProfileProperty prp: profile.getProperties()) { + String scenario = prp.getScenario(); + + if (prp.getScenario() == null ) { + scenario = "__global__"; + } + + if (productVsProfile.get(scenario) == null) { + productVsProfile.put(scenario, new ArrayList()); + } + + if ((prp.getInputParameterName() != null) && !prp.getInputParameterName().isEmpty() && + ((prp.getValue() == null) || prp.getValue().isEmpty())) { + + String paramName = ""; + if (prp.getTestSuiteName() != null && !prp.getTestSuiteName().isEmpty()) { + paramName += prp.getTestSuiteName() + ":"; + + if (prp.getTestCaseName() != null && !prp.getTestCaseName().isEmpty()) { + paramName += prp.getTestCaseName() + ":"; + } + } + + paramName += prp.getInputParameterName(); + + productVsProfile.get(scenario).add("--parameter"); + productVsProfile.get(scenario).add(paramName); + } + } + + for (String scenario: productVsProfile.keySet()) { + + if (productVsProfile.get(scenario).size() == 0) continue; + + List arguments = new ArrayList<>(); + arguments.addAll(args); + arguments.addAll(productVsProfile.get(scenario)); + + if (!scenario.equals("__global__")) { //profile is set across. + arguments.add("--product"); + arguments.add(scenario); + } + + this.makeRpcAndGetJson(arguments); + } + } + + @Path("/profiles") + @POST + @ApiOperation(tags = "VTP Envrionment Profile", value = "Create profile.") + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + @ApiResponses(value = { + @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, + message = "Failed to perform the operation", + response = VTPError.class), + @ApiResponse(code = HttpStatus.CONFLICT_409, + message = "Test profile does already exist with given name", response = VTPError.class) }) + public Response createProfile(@Context HttpServletRequest request) throws VTPException, IOException { + + VTPTestProfile profile = new Gson().fromJson(IOUtils.toString(request.getInputStream()), VTPTestProfile.class); + + for (VTPTestProfile p: this.listTestProfilesHandler().getTestProfiles()) { + if (p.getName().equalsIgnoreCase(profile.getName())) { + throw new VTPException( + new VTPError().setMessage("Test profile does already exist with given name").setHttpStatus(HttpStatus.CONFLICT_409)); + } + } + + this.setPofileHanlder(profile); + + return Response.ok().build(); + } + + @Path("/profiles/{profileName}") + @PUT + @ApiOperation(tags = "VTP Envrionment Profile", value = "Update profile. To remove a profile parameter, set its value to null or empty.") + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + @ApiResponses(value = { + @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, + message = "Failed to perform the operation", + response = VTPError.class) }) + public Response updateProfile( + @ApiParam(value = "Test profile name") @PathParam("profileName") String profileName, + @Context HttpServletRequest request) throws VTPException, IOException { + + VTPTestProfile profile = new Gson().fromJson(IOUtils.toString(request.getInputStream()), VTPTestProfile.class); + if (profile.getName() != null && !profile.getName().equalsIgnoreCase(profileName)) { + //TODO: rename profile + } + + profile.setName(profileName); + + //Unset those params which value is null or empty. + this.unsetPofileHanlder(profile); + + //Set profile + this.setPofileHanlder(profile); + + return Response.ok().build(); + } + + public void deleteProfileHandler(String profileName) throws VTPException, IOException { + List args = new ArrayList<>(); + args.addAll(Arrays.asList(new String[] { + "--product", "open-cli", "profile-delete", "--profile", profileName, "--format", "json" + })); + this.makeRpcAndGetJson(args); + } + + @Path("/profiles/{profileName}") + @DELETE + @ApiOperation(tags = "VTP Envrionment Profile", value = "Delete profile") + @ApiResponses(value = { + @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, + message = "Failed to perform the operation", response = VTPError.class), + @ApiResponse(code = HttpStatus.NOT_FOUND_404, + message = "Test profile does not exist", response = VTPError.class)}) + public Response deleteProfile( + @ApiParam(value = "Test profile name") @PathParam("profileName") String profileName) + throws IOException, VTPException { + + this.deleteProfileHandler(profileName); + + return Response.status(HttpStatus.NO_CONTENT_204).build(); + } +} diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/profile/model/VTPTestProfile.java b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/profile/model/VTPTestProfile.java new file mode 100644 index 00000000..a9604692 --- /dev/null +++ b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/profile/model/VTPTestProfile.java @@ -0,0 +1,102 @@ +/** + * Copyright 2019 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.vtp.profile.model; + +import java.util.ArrayList; +import java.util.List; + +import org.onap.vtp.VTPModelBase; + +public class VTPTestProfile extends VTPModelBase{ + private String name; + private List properties = new ArrayList<>(); + + public String getName() { + return name; + } + + public VTPTestProfile setName(String name) { + this.name = name; + return this; + } + + public List getProperties() { + return properties; + } + + public VTPTestProfile setProperties(List properties) { + this.properties = properties; + return this; + } + + public static class VTPTestProfileList extends VTPModelBase { + List testProfiles = new ArrayList<>(); + + public List getTestProfiles() { + return testProfiles; + } + + public VTPTestProfileList setTestProfiles(List testProfiles) { + this.testProfiles = testProfiles; + return this; + } + } + + public static class VTPTestProfileProperty extends VTPModelBase { + private String scenario; + private String testSuiteName; + private String testCaseName; + private String inputParameterName; + private String value; + public String getTestSuiteName() { + return testSuiteName; + } + public VTPTestProfileProperty setTestSuiteName(String testSuiteName) { + this.testSuiteName = testSuiteName; + return this; + } + public String getTestCaseName() { + return testCaseName; + } + public VTPTestProfileProperty setTestCaseName(String testCaseName) { + this.testCaseName = testCaseName; + return this; + } + public String getInputParameterName() { + return inputParameterName; + } + public VTPTestProfileProperty setInputParameterName(String inputParameterName) { + this.inputParameterName = inputParameterName; + return this; + } + public String getValue() { + return value; + } + public VTPTestProfileProperty setValue(String value) { + this.value = value; + return this; + } + + public String getScenario() { + return scenario; + } + public VTPTestProfileProperty setScenario(String scenario) { + this.scenario = scenario; + return this; + } + } +} diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/main/webapp/WEB-INF/web.xml b/vnfmarket-be/vnf-sdk-marketplace/src/main/webapp/WEB-INF/web.xml index da5ee65f..cd22a010 100644 --- a/vnfmarket-be/vnf-sdk-marketplace/src/main/webapp/WEB-INF/web.xml +++ b/vnfmarket-be/vnf-sdk-marketplace/src/main/webapp/WEB-INF/web.xml @@ -11,7 +11,8 @@ jersey.config.server.provider.packages io.swagger.jaxrs.listing, org.onap.vnfsdk.marketplace.resource, - org.onap.vtp.error, + org.onap.vtp.error, + org.onap.vtp.profile, org.onap.vtp.scenario, org.onap.vtp.execution -- cgit 1.2.3-korg