From 9797ba0f3aa46ee9267f6e8de1d7f871464f4b99 Mon Sep 17 00:00:00 2001 From: subhash kumar singh Date: Sat, 20 Apr 2019 15:41:18 +0530 Subject: Enhancement for query Enhancemenv for query to include group and vnfccustomization. { "serviceResources": { "modelInfo": { "modelName": "NSService", "modelUuid": "0bad8c92-7d22-49f0-b092-b64e6ca564a7", "modelInvariantUuid": "69161960-515b-4bf3-91f1-313b813f5e1d", "modelVersion": "1.0" }, "serviceType": "", "serviceRole": "", "environmentContext": "General_Revenue-Bearing", "resourceOrder": "NF", "workloadContext": "Production", "serviceVnfs": [ { "modelInfo": { "modelName": "", "modelUuid": "123", "modelInvariantUuid": "", "modelVersion": "", "modelCustomizationUuid": "1234", "modelInstanceName": "test" }, "toscaNodeType": "", "nfFunction": "", "nfType": "", "nfRole": "", "nfNamingCode": "", "multiStageDesign": "", "resourceInput": "", "vfModules": [], "groups": [ { "modelInfo": { "modelName": "test", "modelUuid": "11", "modelInvariantUuid": "11", "modelVersion": "2" }, "vnfcs": [ { "modelInfo": { "modelName": "xfs", "modelUuid": "22", "modelInvariantUuid": "2222", "modelVersion": "22222", "modelCustomizationUuid": "2222" } } ] } ] } ], "serviceNetworks": [], "serviceAllottedResources": [] } } Issue-ID: SO-1393 Change-Id: Iee7f79337f73276f857d6d42b5b2416e4e8f4536 Signed-off-by: subhash kumar singh --- .../catalogdb/catalogrest/QueryGroups.java | 98 +++++++++++++++++ .../catalogdb/catalogrest/QueryServiceVnfs.java | 12 ++- .../adapters/catalogdb/catalogrest/QueryVnfcs.java | 119 +++++++++++++++++++++ .../V5.12__Add_Relation_VnfcCustomization.sql | 2 + .../catalogdb/catalogrest/QueryGroupsTest.java | 74 +++++++++++++ .../catalogdb/catalogrest/QueryVnfcsTest.java | 65 +++++++++++ 6 files changed, 369 insertions(+), 1 deletion(-) create mode 100644 adapters/mso-catalog-db-adapter/src/main/java/org/onap/so/adapters/catalogdb/catalogrest/QueryGroups.java create mode 100644 adapters/mso-catalog-db-adapter/src/main/java/org/onap/so/adapters/catalogdb/catalogrest/QueryVnfcs.java create mode 100644 adapters/mso-catalog-db-adapter/src/main/resources/db/migration/V5.12__Add_Relation_VnfcCustomization.sql create mode 100644 adapters/mso-catalog-db-adapter/src/test/java/org/onap/so/adapters/catalogdb/catalogrest/QueryGroupsTest.java create mode 100644 adapters/mso-catalog-db-adapter/src/test/java/org/onap/so/adapters/catalogdb/catalogrest/QueryVnfcsTest.java (limited to 'adapters/mso-catalog-db-adapter') diff --git a/adapters/mso-catalog-db-adapter/src/main/java/org/onap/so/adapters/catalogdb/catalogrest/QueryGroups.java b/adapters/mso-catalog-db-adapter/src/main/java/org/onap/so/adapters/catalogdb/catalogrest/QueryGroups.java new file mode 100644 index 0000000000..c15b0d9df1 --- /dev/null +++ b/adapters/mso-catalog-db-adapter/src/main/java/org/onap/so/adapters/catalogdb/catalogrest/QueryGroups.java @@ -0,0 +1,98 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2019 Huawei Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.so.adapters.catalogdb.catalogrest; + +import org.onap.so.db.catalog.beans.InstanceGroup; +import org.onap.so.db.catalog.beans.VFCInstanceGroup; +import org.onap.so.db.catalog.beans.VnfcInstanceGroupCustomization; +import javax.xml.bind.annotation.XmlRootElement; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@XmlRootElement(name = "groups") +public class QueryGroups extends CatalogQuery { + + private List vnfcInstanceGroupCustomizations; + private static final String TEMPLATE = "\n" + "\t{ \"modelInfo\" : {\n" + + "\t\t\"modelName\" : ,\n" + "\t\t\"modelUuid\" : ,\n" + + "\t\t\"modelInvariantUuid\" : ,\n" + + "\t\t\"modelVersion\" : \n" + "\t\t},\n" + "<_VNFCS_>\n" + "\t}"; + + public QueryGroups() { + super(); + vnfcInstanceGroupCustomizations = new ArrayList<>(); + + } + + public QueryGroups(List vnfcInstanceGroupCustomizations) { + this.vnfcInstanceGroupCustomizations = new ArrayList<>(); + if (vnfcInstanceGroupCustomizations != null) { + for (VnfcInstanceGroupCustomization g : vnfcInstanceGroupCustomizations) { + if (logger.isDebugEnabled()) { + logger.debug(g.toString()); + } + this.vnfcInstanceGroupCustomizations.add(g); + } + } + } + + @Override + public String JSON2(boolean isArray, boolean isEmbed) { + StringBuilder sb = new StringBuilder(); + if (!isEmbed && isArray) + sb.append("{ "); + if (isArray) + sb.append("\"groups\": ["); + Map valueMap = new HashMap<>(); + String sep = ""; + boolean first = true; + + for (VnfcInstanceGroupCustomization o : vnfcInstanceGroupCustomizations) { + if (first) + sb.append("\n"); + first = false; + + boolean vnfcCustomizationNull = o.getVnfcCustomizations() == null; + InstanceGroup instanceGroup = o.getInstanceGroup(); + + if (instanceGroup != null) { + put(valueMap, "MODEL_NAME", instanceGroup.getModelName()); + put(valueMap, "MODEL_UUID", instanceGroup.getModelInvariantUUID()); + put(valueMap, "MODEL_INVARIANT_ID", instanceGroup.getModelInvariantUUID()); + put(valueMap, "MODEL_VERSION", instanceGroup.getModelUUID()); + } + + String subItem = new QueryVnfcs(vnfcCustomizationNull ? null : o.getVnfcCustomizations()).JSON2(true, true); + valueMap.put("_VNFCS_", subItem.replaceAll("(?m)^", "\t\t")); + sb.append(sep).append(this.setTemplate(TEMPLATE, valueMap)); + sep = ",\n"; + } + if (!first) + sb.append("\n"); + if (isArray) + sb.append("]"); + if (!isEmbed && isArray) + sb.append("}"); + return sb.toString(); + } +} diff --git a/adapters/mso-catalog-db-adapter/src/main/java/org/onap/so/adapters/catalogdb/catalogrest/QueryServiceVnfs.java b/adapters/mso-catalog-db-adapter/src/main/java/org/onap/so/adapters/catalogdb/catalogrest/QueryServiceVnfs.java index f786f25361..e5241a4e95 100644 --- a/adapters/mso-catalog-db-adapter/src/main/java/org/onap/so/adapters/catalogdb/catalogrest/QueryServiceVnfs.java +++ b/adapters/mso-catalog-db-adapter/src/main/java/org/onap/so/adapters/catalogdb/catalogrest/QueryServiceVnfs.java @@ -25,8 +25,12 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; import javax.xml.bind.annotation.XmlRootElement; +import org.onap.so.db.catalog.beans.InstanceGroup; +import org.onap.so.db.catalog.beans.VFCInstanceGroup; import org.onap.so.db.catalog.beans.VnfResourceCustomization; +import org.onap.so.db.catalog.beans.VnfcInstanceGroupCustomization; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -45,7 +49,7 @@ public class QueryServiceVnfs extends CatalogQuery { + "\t\"nfFunction\" : ,\n" + "\t\"nfType\" : ,\n" + "\t\"nfRole\" : ,\n" + "\t\"nfNamingCode\" : ,\n" + "\t\"multiStageDesign\" : ,\n" - + "\t\"resourceInput\" : ,\n" + "<_VFMODULES_>\n" + "\t}"; + + "\t\"resourceInput\" : ,\n" + "<_VFMODULES_>,\n" + "<_GROUPS_>\n" + "\t}"; public QueryServiceVnfs() { super(); @@ -120,6 +124,12 @@ public class QueryServiceVnfs extends CatalogQuery { String subitem = new QueryVfModule(vrNull ? null : o.getVfModuleCustomizations()).JSON2(true, true); valueMap.put("_VFMODULES_", subitem.replaceAll("(?m)^", "\t\t")); + List vnfcInstanceGroupCustomizations = + o.getVnfcInstanceGroupCustomizations(); + + String grpSubItem = new QueryGroups(vrNull ? null : vnfcInstanceGroupCustomizations).JSON2(true, true); + valueMap.put("_GROUPS_", grpSubItem.replaceAll("(?m)^", "\t\t")); + sb.append(sep).append(this.setTemplate(TEMPLATE, valueMap)); sep = ",\n"; } diff --git a/adapters/mso-catalog-db-adapter/src/main/java/org/onap/so/adapters/catalogdb/catalogrest/QueryVnfcs.java b/adapters/mso-catalog-db-adapter/src/main/java/org/onap/so/adapters/catalogdb/catalogrest/QueryVnfcs.java new file mode 100644 index 0000000000..0bf82fd8b7 --- /dev/null +++ b/adapters/mso-catalog-db-adapter/src/main/java/org/onap/so/adapters/catalogdb/catalogrest/QueryVnfcs.java @@ -0,0 +1,119 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2019 Huawei Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.so.adapters.catalogdb.catalogrest; + +import org.onap.so.db.catalog.beans.VnfcCustomization; +import javax.xml.bind.annotation.XmlRootElement; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@XmlRootElement(name = "vnfcs") +public class QueryVnfcs extends CatalogQuery { + private List vnfcCustomizations; + private static final String TEMPLATE = + "\t{\n" + "\t\t\"modelInfo\" : { \n" + "\t\t\t\"modelName\" : ,\n" + + "\t\t\t\"modelUuid\" : ,\n" + + "\t\t\t\"modelInvariantUuid\" : ,\n" + + "\t\t\t\"modelVersion\" : ,\n" + + "\t\t\t\"modelCustomizationUuid\" : \n" + "\t\t}" + "\t}"; + + public QueryVnfcs() { + super(); + vnfcCustomizations = new ArrayList(); + } + + public QueryVnfcs(List vnfcCustomizations) { + this.vnfcCustomizations = new ArrayList(); + if (vnfcCustomizations != null) { + for (VnfcCustomization vnfcCustomization : vnfcCustomizations) { + if (logger.isDebugEnabled()) { + logger.debug(vnfcCustomization.toString()); + } + this.vnfcCustomizations.add(vnfcCustomization); + } + } + } + + public List getVnfcCustomizations() { + return vnfcCustomizations; + } + + public void setVnfcCustomizations(List vnfcCustomizations) { + this.vnfcCustomizations = vnfcCustomizations; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + + boolean first = true; + int i = 1; + for (VnfcCustomization o : vnfcCustomizations) { + sb.append(i).append("\t"); + if (!first) { + sb.append("\n"); + } + first = false; + sb.append(o); + } + return sb.toString(); + } + + @Override + public String JSON2(boolean isArray, boolean isEmbed) { + StringBuilder sb = new StringBuilder(); + if (!isEmbed && isArray) { + sb.append("{"); + } + + if (isArray) { + sb.append("\"vnfcs\": ["); + } + + Map valueMap = new HashMap<>(); + String sep = ""; + boolean first = true; + + for (VnfcCustomization o : vnfcCustomizations) { + if (first) + sb.append("\n"); + first = false; + + put(valueMap, "MODEL_NAME", o.getModelName()); + put(valueMap, "MODEL_UUID", o.getModelUUID()); + put(valueMap, "MODEL_INVARIANT_ID", o.getModelInvariantUUID()); + put(valueMap, "MODEL_VERSION", o.getModelVersion()); + put(valueMap, "MODEL_CUSTOMIZATION_UUID", o.getModelCustomizationUUID()); + + sb.append(sep).append(this.setTemplate(TEMPLATE, valueMap)); + sep = ",\n"; + } + if (!first) + sb.append("\n"); + if (isArray) + sb.append("]"); + if (!isEmbed && isArray) + sb.append("}"); + return sb.toString(); + } +} diff --git a/adapters/mso-catalog-db-adapter/src/main/resources/db/migration/V5.12__Add_Relation_VnfcCustomization.sql b/adapters/mso-catalog-db-adapter/src/main/resources/db/migration/V5.12__Add_Relation_VnfcCustomization.sql new file mode 100644 index 0000000000..95a2c25eb8 --- /dev/null +++ b/adapters/mso-catalog-db-adapter/src/main/resources/db/migration/V5.12__Add_Relation_VnfcCustomization.sql @@ -0,0 +1,2 @@ +use catalogdb; +ALTER TABLE vnfc_customization ADD vnfc_instance_group_customization_id INTEGER NULL; \ No newline at end of file diff --git a/adapters/mso-catalog-db-adapter/src/test/java/org/onap/so/adapters/catalogdb/catalogrest/QueryGroupsTest.java b/adapters/mso-catalog-db-adapter/src/test/java/org/onap/so/adapters/catalogdb/catalogrest/QueryGroupsTest.java new file mode 100644 index 0000000000..00db6d5938 --- /dev/null +++ b/adapters/mso-catalog-db-adapter/src/test/java/org/onap/so/adapters/catalogdb/catalogrest/QueryGroupsTest.java @@ -0,0 +1,74 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2019 Huawei Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.so.adapters.catalogdb.catalogrest; + +import org.assertj.core.api.Assertions; +import org.junit.Test; +import org.onap.so.db.catalog.beans.Service; +import org.onap.so.db.catalog.beans.VFCInstanceGroup; +import org.onap.so.db.catalog.beans.VnfResource; +import org.onap.so.db.catalog.beans.VnfResourceCustomization; +import org.onap.so.db.catalog.beans.VnfcCustomization; +import org.onap.so.db.catalog.beans.VnfcInstanceGroupCustomization; +import org.onap.so.db.catalog.rest.beans.ServiceMacroHolder; +import org.onap.so.jsonpath.JsonPathUtil; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class QueryGroupsTest { + + @Test + public void convertToJson_successful() { + QueryGroups queryGroups = new QueryGroups(createList()); + String jsonResult = queryGroups.JSON2(true, false); + + Assertions.assertThat(JsonPathUtil.getInstance().locateResult(jsonResult, "$.groups[0].modelInfo.modelName")) + .contains("test"); + Assertions + .assertThat( + JsonPathUtil.getInstance().locateResult(jsonResult, "$.groups[0].vnfcs[0].modelInfo.modelName")) + .contains("test"); + } + + private List createList() { + + VnfcCustomization vnfcCustomization = new VnfcCustomization(); + vnfcCustomization.setModelCustomizationUUID("test"); + vnfcCustomization.setModelVersion("test"); + vnfcCustomization.setModelInvariantUUID("test"); + vnfcCustomization.setModelName("test"); + + VFCInstanceGroup vfcInstanceGroup = new VFCInstanceGroup(); + vfcInstanceGroup.setModelName("test"); + vfcInstanceGroup.setModelUUID("test"); + vfcInstanceGroup.setModelInvariantUUID("test"); + vfcInstanceGroup.setModelVersion("test"); + + VnfcInstanceGroupCustomization vnfcInstanceGroupCustomization = new VnfcInstanceGroupCustomization(); + vnfcInstanceGroupCustomization.setVnfcCustomizations(Arrays.asList(vnfcCustomization)); + vnfcInstanceGroupCustomization.setInstanceGroup(vfcInstanceGroup); + + + vfcInstanceGroup.setVnfcInstanceGroupCustomizations(Arrays.asList(vnfcInstanceGroupCustomization)); + return Arrays.asList(vnfcInstanceGroupCustomization); + } +} diff --git a/adapters/mso-catalog-db-adapter/src/test/java/org/onap/so/adapters/catalogdb/catalogrest/QueryVnfcsTest.java b/adapters/mso-catalog-db-adapter/src/test/java/org/onap/so/adapters/catalogdb/catalogrest/QueryVnfcsTest.java new file mode 100644 index 0000000000..1fd4f19e29 --- /dev/null +++ b/adapters/mso-catalog-db-adapter/src/test/java/org/onap/so/adapters/catalogdb/catalogrest/QueryVnfcsTest.java @@ -0,0 +1,65 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2019 Huawei Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.so.adapters.catalogdb.catalogrest; + +import static org.assertj.core.api.Assertions.assertThat; +import org.junit.Test; +import org.onap.so.db.catalog.beans.VnfcCustomization; +import org.onap.so.jsonpath.JsonPathUtil; +import java.util.ArrayList; +import java.util.List; + +public class QueryVnfcsTest { + + @Test + public void convertToJson_successful() { + QueryVnfcs queryVnfcs = new QueryVnfcs(createList()); + String jsonResult = queryVnfcs.JSON2(true, false); + System.out.println(jsonResult); + assertThat(JsonPathUtil.getInstance().locateResult(jsonResult, "$.vnfcs[0].modelInfo.modelName")) + .contains("model1"); + assertThat(JsonPathUtil.getInstance().locateResult(jsonResult, "$.vnfcs[1].modelInfo.modelName")) + .contains("model2"); + + } + + private List createList() { + List customizations = new ArrayList(); + + VnfcCustomization c1 = new VnfcCustomization(); + c1.setModelName("model1"); + c1.setModelUUID("uuid1"); + c1.setModelInvariantUUID("inv1"); + c1.setModelVersion("v1"); + c1.setModelCustomizationUUID("cust1"); + + VnfcCustomization c2 = new VnfcCustomization(); + c2.setModelName("model2"); + c2.setModelUUID("uuid2"); + c2.setModelInvariantUUID("inv2"); + c2.setModelVersion("v2"); + c2.setModelCustomizationUUID("cust2"); + + customizations.add(c1); + customizations.add(c2); + return customizations; + } +} -- cgit 1.2.3-korg