From 6c4ef0760ad023e8a01790eab54a8a427565a19b Mon Sep 17 00:00:00 2001 From: zm330 Date: Fri, 28 Feb 2020 11:51:17 +0800 Subject: Add Catalog DB query methods Issue-ID: SO-2368 Signed-off-by: zm330 Change-Id: I99f3241d4726d3d334b1eeccf30022c939b71b6f --- .../catalogrest/QueryServiceArtifact.java | 118 +++++++++++++++++++++ .../catalogdb/catalogrest/QueryServiceInfo.java | 82 ++++++++++++++ 2 files changed, 200 insertions(+) create mode 100644 adapters/mso-catalog-db-adapter/src/main/java/org/onap/so/adapters/catalogdb/catalogrest/QueryServiceArtifact.java create mode 100644 adapters/mso-catalog-db-adapter/src/main/java/org/onap/so/adapters/catalogdb/catalogrest/QueryServiceInfo.java (limited to 'adapters') diff --git a/adapters/mso-catalog-db-adapter/src/main/java/org/onap/so/adapters/catalogdb/catalogrest/QueryServiceArtifact.java b/adapters/mso-catalog-db-adapter/src/main/java/org/onap/so/adapters/catalogdb/catalogrest/QueryServiceArtifact.java new file mode 100644 index 0000000000..ce39b9713a --- /dev/null +++ b/adapters/mso-catalog-db-adapter/src/main/java/org/onap/so/adapters/catalogdb/catalogrest/QueryServiceArtifact.java @@ -0,0 +1,118 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (c) 2019, CMCC 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.so.adapters.catalogdb.catalogrest; + +import org.onap.so.db.catalog.beans.ServiceArtifact; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import javax.xml.bind.annotation.XmlRootElement; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@XmlRootElement(name = "serviceArtifacts") +public class QueryServiceArtifact extends CatalogQuery { + + protected static Logger logger = LoggerFactory.getLogger(QueryServiceArtifact.class); + + private List serviceArtifactList; + + private static final String TEMPLATE = "\t{\n" + "\t\t\"artifactUUID\" : ,\n" + + "\t\t\"name\" : ,\n" + "\t\t\"version\" : ,\n" + + "\t\t\"checksum\" : ,\n" + "\t\t\"type\" : ,\n" + + "\t\t\"content\" : ,\n" + "\t\t\"description\" : \n" + "\t}"; + + public QueryServiceArtifact() { + super(); + serviceArtifactList = new ArrayList<>(); + } + + public QueryServiceArtifact(List alist) { + serviceArtifactList = new ArrayList<>(); + for (ServiceArtifact o : alist) { + if (logger.isDebugEnabled()) + logger.debug(o.toString()); + serviceArtifactList.add(o); + } + } + + public List getServiceArtifact() { + return this.serviceArtifactList; + } + + public void setServiceArtifact(List a) { + this.serviceArtifactList = a; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + + boolean first = true; + int i = 1; + for (ServiceArtifact o : serviceArtifactList) { + 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("\"serviceArtifact\": ["); + Map valueMap = new HashMap<>(); + String sep = ""; + boolean first = true; + + for (ServiceArtifact o : serviceArtifactList) { + if (first) + sb.append("\n"); + first = false; + + boolean vrNull = o == null; + + put(valueMap, "ARTIFACT_UUID", vrNull ? null : o.getArtifactUUID()); + put(valueMap, "TYPE", vrNull ? null : o.getType()); + put(valueMap, "NAME", vrNull ? null : o.getName()); + put(valueMap, "VERSION", vrNull ? null : o.getVersion()); + put(valueMap, "DESCRIPTION", vrNull ? null : o.getDescription()); + put(valueMap, "CONTENT", vrNull ? null : o.getContent()); + put(valueMap, "CHECKSUM", vrNull ? null : o.getChecksum()); + 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/QueryServiceInfo.java b/adapters/mso-catalog-db-adapter/src/main/java/org/onap/so/adapters/catalogdb/catalogrest/QueryServiceInfo.java new file mode 100644 index 0000000000..b1911654c4 --- /dev/null +++ b/adapters/mso-catalog-db-adapter/src/main/java/org/onap/so/adapters/catalogdb/catalogrest/QueryServiceInfo.java @@ -0,0 +1,82 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (c) 2019, CMCC 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.so.adapters.catalogdb.catalogrest; + +import org.onap.so.db.catalog.beans.Service; +import org.onap.so.db.catalog.beans.ServiceInfo; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import javax.xml.bind.annotation.XmlRootElement; +import java.util.HashMap; +import java.util.Map; + +@XmlRootElement(name = "serviceInfo") +public class QueryServiceInfo extends CatalogQuery { + + protected static Logger logger = LoggerFactory.getLogger(QueryServiceInfo.class); + + private ServiceInfo serviceInfo; + + private static final String TEMPLATE = + "\n" + "\t{" + "\t\t\"id\" : ,\n" + "\t\t\"serviceInput\" : ,\n" + + "\t\"serviceProperties\" : ,\n" + "<_SERVICEARTIFACT_>\n"; + + + public QueryServiceInfo() { + super(); + this.serviceInfo = new ServiceInfo(); + } + + public QueryServiceInfo(ServiceInfo serviceInfo) { + this.serviceInfo = serviceInfo; + } + + public ServiceInfo getServiceInfo() { + return this.serviceInfo; + } + + public void setServiceInfo(ServiceInfo serviceInfo) { + this.serviceInfo = serviceInfo; + } + + @Override + public String toString() { + + return serviceInfo.toString(); + } + + @Override + public String JSON2(boolean isArray, boolean isEmbed) { + StringBuilder sb = new StringBuilder(); + sb.append("\"serviceInfo\": "); + sb.append("\n"); + Map valueMap = new HashMap<>(); + Service service = serviceInfo.getService(); + put(valueMap, "ID", null == serviceInfo ? null : serviceInfo.getId()); + put(valueMap, "SERVICE_INPUT", null == serviceInfo ? null : serviceInfo.getServiceInput()); + put(valueMap, "SERVICE_PROPERTIES", null == serviceInfo ? null : serviceInfo.getServiceProperties()); + // String subitem = new QueryServiceArtifact(service.getServiceArtifactList()).JSON2(true, true); + // valueMap.put("_SERVICEARTIFACT_", subitem.replaceAll("(?m)^", "\t\t"));m + sb.append(this.setTemplate(TEMPLATE, valueMap)); + sb.append("}"); + return sb.toString(); + } +} -- cgit 1.2.3-korg