2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 
   6  * ================================================================================
 
   7  * Licensed under the Apache License, Version 2.0 (the "License");
 
   8  * you may not use this file except in compliance with the License.
 
   9  * You may obtain a copy of the License at
 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
 
  13  * Unless required by applicable law or agreed to in writing, software
 
  14  * distributed under the License is distributed on an "AS IS" BASIS,
 
  15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  16  * See the License for the specific language governing permissions and
 
  17  * limitations under the License.
 
  18  * ============LICENSE_END=========================================================
 
  20 package org.openecomp.mso.adapters.catalogdb;
 
  23 Create an initial query to retrieve a VNF Resource definition (including a list of possible module types)
 
  24 within the context of a given service. Input is a vnf resource model customization ID (new field for 1702),
 
  25 or a composite key (from 1610) of service name, service version, vnf instance name
 
  27 Returns a structure (JSON?) containing VNF RESOURCE attributes, plus a list of VF Module structures.
 
  29 Query a NETWORK_RESOURCE from the MSO Catalog, based on a networkModelCustomizationUUID (new for 1702),
 
  30 a network type (unique type identifier in 1610), or based on network role within a service.
 
  32 Create Adapter framework for access to Catalog DB, including connection management,
 
  33 login/password access, transaction logic, etc. This can be modeled after the Request DB Adapter
 
  35 Update the MSO Catalog DB schema to include the new fields defined in this user story.
 
  37 Note that the resourceModelCustomizationUUID (or vfModuleModelCustomizationUUID) will be unique keys (indexes)
 
  38 on the VNF_RESOURCE and VF_MODULE tables respectively.
 
  39 The previously constructed "vnf-type" and "vf-module-type" field may continue to be populated,
 
  40 but should no longer be needed and can deprecate in future release.
 
  42 For migration, a new randomly generated UUID field may be generated for the *ModelCustomizationUUID" fields
 
  43 until such time that the model is redistributed from ASDC.
 
  45 All other fields Check with Mike Z for appropriate value for the vfModuleLabel.
 
  46 We might be able to derive it's value from the current vnf-type (using the "middle" piece that identifies the module type).
 
  48 min and initial counts can be 0. max can be null to indicate no maximum.
 
  50 Once the network-level distribution artifacts are defined, similar updates can be made to the NETWORK_RESOURCE table.
 
  54 import java.util.List;
 
  56 import javax.ws.rs.Consumes;
 
  57 import javax.ws.rs.DELETE;
 
  58 import javax.ws.rs.GET;
 
  59 import javax.ws.rs.HEAD;
 
  60 import javax.ws.rs.POST;
 
  61 import javax.ws.rs.PUT;
 
  62 import javax.ws.rs.Path;
 
  63 import javax.ws.rs.PathParam;
 
  64 import javax.ws.rs.Produces;
 
  65 import javax.ws.rs.QueryParam;
 
  66 import javax.ws.rs.core.Response;
 
  67 import javax.ws.rs.core.GenericEntity;
 
  68 import javax.ws.rs.core.HttpHeaders;
 
  69 import javax.ws.rs.core.MediaType;
 
  70 import javax.xml.ws.Holder;
 
  72 import org.apache.http.HttpStatus;
 
  74 import org.openecomp.mso.logger.MessageEnum;
 
  75 import org.openecomp.mso.logger.MsoLogger;
 
  76 import org.openecomp.mso.openstack.exceptions.MsoExceptionCategory;
 
  77 import org.openecomp.mso.adapters.vnfrest.VfModuleExceptionResponse;
 
  78 import org.openecomp.mso.adapters.catalogrest.QueryServiceVnfs;
 
  79 import org.openecomp.mso.adapters.catalogrest.QueryServiceNetworks;
 
  80 import org.openecomp.mso.adapters.catalogrest.QueryServiceMacroHolder;
 
  81 import org.openecomp.mso.adapters.catalogrest.QueryAllottedResourceCustomization;
 
  82 import org.openecomp.mso.db.catalog.CatalogDatabase;
 
  83 import org.openecomp.mso.db.catalog.beans.VnfResource;
 
  84 import org.openecomp.mso.db.catalog.beans.NetworkResourceCustomization;
 
  85 import org.openecomp.mso.db.catalog.beans.ServiceMacroHolder;
 
  86 import org.openecomp.mso.db.catalog.beans.AllottedResourceCustomization;
 
  89  * This class services calls to the REST interface for VF Modules (http://host:port/ecomp/mso/catalog/v1)
 
  90  * Both XML and JSON can be produced/consumed.  Set Accept: and Content-Type: headers appropriately.  XML is the default.
 
  91  * Requests respond synchronously only
 
  94 public class CatalogDbAdapterRest {
 
  95         private static MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);
 
 100         @Produces(MediaType.TEXT_HTML)
 
 101         public Response healthcheck () {
 
 102                 String CHECK_HTML = "<!DOCTYPE html><html><head><meta charset=\"ISO-8859-1\"><title>Health Check</title></head><body>Application ready</body></html>";
 
 103                 return Response.ok().entity(CHECK_HTML).build();
 
 107          * GET {http-catalog-adapter-root}/v1/serviceVnfs?vnfModelCustomizationUuid=<vnf-model-customization-uuid>
 
 108          * URL:http://localhost:8080/ecomp/mso/catalog/v1/getVfModuleType?vnfType=Test/vSAMP10&vfModuleType=vSAMP10::base::module-0
 
 110          * {"queryVfModule":{"version":1,"asdcUuid":"MANUAL RECORD","created":{"nanos":0},"description":"vSAMP10","environmentId":15184,"id":2312,"isBase":1,"modelName":"vSAMP10::base::module-0","modelVersion":1,"templateId":15123,"type":"Test\/vSAMP10::vSAMP10::base::module-0","vnfResourceId":15187}}
 
 114         @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
 
 115         public Response serviceVnfs(
 
 116                         @QueryParam("vnfModelCustomizationUuid") String vnfUuid,
 
 117                         @QueryParam("serviceModelUuid") String smUuid,
 
 118                         @QueryParam("serviceModelInvariantUuid") String smiUuid,
 
 119                         @QueryParam("serviceModelVersion") String smVer,
 
 120                         @QueryParam("serviceModelName") String smName
 
 122                 QueryServiceVnfs qryResp;
 
 123                 int respStatus = HttpStatus.SC_OK;
 
 125                 List<VnfResource> ret;
 
 129                         if (vnfUuid != null && !vnfUuid.equals("")) {
 
 131                                 LOGGER.debug ("Query serviceVnfs getAllVnfsByVnfModelCustomizationUuid vnfModelCustomizationUuid: " + uuid);
 
 132                                 ret = (new CatalogDatabase ()).getAllVnfsByVnfModelCustomizationUuid(uuid);
 
 134                         else if (smUuid != null && !smUuid.equals("")) {
 
 136                                 LOGGER.debug ("Query serviceVnfs getAllVnfsByServiceModelUuid serviceModelUuid: " + uuid);
 
 137                                 ret = (new CatalogDatabase ()).getAllVnfsByServiceModelUuid(uuid);
 
 139                         else if (smiUuid != null && !smiUuid.equals("")) {
 
 141                                 if (smVer != null && !smVer.equals("")) {
 
 142                                         LOGGER.debug ("Query serviceVnfs getAllNetworksByServiceModelInvariantUuid serviceModelInvariantUuid: " + uuid+ " serviceModelVersion: "+ smVer);
 
 143                                         ret = (new CatalogDatabase ()).getAllVnfsByServiceModelInvariantUuid(uuid, smVer);
 
 146                                         LOGGER.debug ("Query serviceVnfs getAllNetworksByServiceModelInvariantUuid serviceModelUuid: " + uuid);
 
 147                                         ret = (new CatalogDatabase ()).getAllVnfsByServiceModelInvariantUuid(uuid);
 
 150                         else if (smName != null && !smName.equals("")) {
 
 151                                 if (smVer != null && !smVer.equals("")) {
 
 152                                         LOGGER.debug ("Query serviceVnfs getAllVnfsByServiceName serviceModelInvariantName: " + smName+ " serviceModelVersion: "+ smVer);
 
 153                                         ret = (new CatalogDatabase ()).getAllVnfsByServiceName(smName, smVer);
 
 156                                         LOGGER.debug ("Query serviceVnfs getAllVnfsByServiceName serviceModelName: " + smName);
 
 157                                         ret = (new CatalogDatabase ()).getAllVnfsByServiceName(smName);
 
 161                                 throw(new Exception("no matching parameters"));
 
 164                         if (ret == null || ret.isEmpty()) {
 
 165                                 LOGGER.debug ("serviceVnfs not found");
 
 166                                 respStatus = HttpStatus.SC_NOT_FOUND;
 
 167                                 qryResp = new QueryServiceVnfs();
 
 169                                 LOGGER.debug ("serviceVnfs found");
 
 170                                 qryResp = new QueryServiceVnfs(ret);
 
 171                                 LOGGER.debug ("serviceVnfs qryResp="+ qryResp);
 
 172                                 LOGGER.debug ("serviceVnfs tojsonstring="+ qryResp.toJsonString());
 
 174                         LOGGER.debug ("Query serviceVnfs exit");
 
 177                                 //.entity(new GenericEntity<QueryServiceVnfs>(qryResp) {})
 
 178                                 .entity(qryResp.toJsonString())
 
 179                                 .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
 
 181                 } catch (Exception e) {
 
 182                         LOGGER.error (MessageEnum.RA_QUERY_VNF_ERR,  uuid, "", "queryServiceVnfs", MsoLogger.ErrorCode.BusinessProcesssError, "Exception - queryServiceVnfs", e);
 
 183                         VfModuleExceptionResponse excResp = new VfModuleExceptionResponse(e.getMessage(), MsoExceptionCategory.INTERNAL, Boolean.FALSE, null);
 
 185                                 .status(HttpStatus.SC_INTERNAL_SERVER_ERROR)
 
 186                                 .entity(new GenericEntity<VfModuleExceptionResponse>(excResp) {})
 
 192         @Path("serviceNetworks")
 
 193         @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
 
 194         public Response serviceNetworks (
 
 195                         @QueryParam("networkModelCustomizationUuid") String nUuid,
 
 196                         @QueryParam("networkType") String nType,
 
 197                         @QueryParam("serviceModelUuid") String smUuid,
 
 198                         @QueryParam("serviceModelInvariantUuid") String smiUuid,
 
 199                         @QueryParam("serviceModelVersion") String smVer
 
 201                 QueryServiceNetworks qryResp;
 
 202                 int respStatus = HttpStatus.SC_OK;
 
 204                 List<NetworkResourceCustomization> ret;
 
 207                         if (nUuid != null && !nUuid.equals("")) {
 
 209                                 LOGGER.debug ("Query serviceNetworks getAllNetworksByNetworkModelCustomizationUuid networkModelCustomizationUuid: " + uuid);
 
 210                                 ret = (new CatalogDatabase ()).getAllNetworksByNetworkModelCustomizationUuid(uuid);
 
 212                         else if (smUuid != null && !smUuid.equals("")) {
 
 214                                 LOGGER.debug ("Query serviceNetworks getAllNetworksByServiceModelUuid serviceModelUuid: " + uuid);
 
 215                                 ret = (new CatalogDatabase ()).getAllNetworksByServiceModelUuid(uuid);
 
 217                         else if (nType != null && !nType.equals("")) {
 
 219                                 LOGGER.debug ("Query serviceNetworks getAllNetworksByNetworkType serviceModelUuid: " + uuid);
 
 220                                 ret = (new CatalogDatabase ()).getAllNetworksByNetworkType(uuid);
 
 222                         else if (smiUuid != null && !smiUuid.equals("")) {
 
 224                                 if (smVer != null && !smVer.equals("")) {
 
 225                                         LOGGER.debug ("Query serviceNetworks getAllNetworksByServiceModelInvariantUuid serviceModelInvariantUuid: " + uuid+ " serviceModelVersion: "+ smVer);
 
 226                                         ret = (new CatalogDatabase ()).getAllNetworksByServiceModelInvariantUuid(uuid, smVer);
 
 229                                         LOGGER.debug ("Query serviceNetworks getAllNetworksByServiceModelInvariantUuid serviceModelUuid: " + uuid);
 
 230                                         ret = (new CatalogDatabase ()).getAllNetworksByServiceModelInvariantUuid(uuid);
 
 234                                 throw(new Exception("no matching parameters"));
 
 237                         if (ret == null || ret.isEmpty()) {
 
 238                                 LOGGER.debug ("serviceNetworks not found");
 
 239                                 respStatus = HttpStatus.SC_NOT_FOUND;
 
 240                                 qryResp = new QueryServiceNetworks();
 
 242                                 LOGGER.debug ("serviceNetworks found");
 
 243                                 qryResp = new QueryServiceNetworks(ret);
 
 244                                 LOGGER.debug ("serviceNetworks qryResp="+ qryResp);
 
 245                                 LOGGER.debug ("serviceNetworks tojsonstring="+ qryResp.toJsonString());
 
 247                         LOGGER.debug ("Query serviceNetworks exit");
 
 250                                 //.entity(new GenericEntity<QueryServiceNetworks>(qryResp) {})
 
 251                                 .entity(qryResp.toJsonString())
 
 252                                 .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
 
 254                 } catch (Exception e) {
 
 255                         LOGGER.error (MessageEnum.RA_QUERY_VNF_ERR,  uuid, "", "queryServiceNetworks", MsoLogger.ErrorCode.BusinessProcesssError, "Exception - queryServiceNetworks", e);
 
 256                         VfModuleExceptionResponse excResp = new VfModuleExceptionResponse(e.getMessage(), MsoExceptionCategory.INTERNAL, Boolean.FALSE, null);
 
 258                                 .status(HttpStatus.SC_INTERNAL_SERVER_ERROR)
 
 259                                 .entity(new GenericEntity<VfModuleExceptionResponse>(excResp) {})
 
 265         @Path("serviceResources")
 
 266         @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
 
 267         public Response serviceResources(
 
 268                         @QueryParam("serviceModelUuid") String smUuid,
 
 269                         @QueryParam("serviceModelInvariantUuid") String smiUuid,
 
 270                         @QueryParam("serviceModelVersion") String smVer
 
 272                 QueryServiceMacroHolder qryResp;
 
 273                 int respStatus = HttpStatus.SC_OK;
 
 275                 ServiceMacroHolder ret;
 
 279                         if (smUuid != null && !smUuid.equals("")) {
 
 281                                 LOGGER.debug ("Query serviceMacroHolder getAllResourcesByServiceModelUuid serviceModelUuid: " + uuid);
 
 282                                 ret = (new CatalogDatabase ()).getAllResourcesByServiceModelUuid(uuid);
 
 284                         else if (smiUuid != null && !smiUuid.equals("")) {
 
 286                                 if (smVer != null && !smVer.equals("")) {
 
 287                                         LOGGER.debug ("Query serviceMacroHolder getAllResourcesByServiceModelInvariantUuid serviceModelInvariantUuid: " + uuid+ " serviceModelVersion: "+ smVer);
 
 288                                         ret = (new CatalogDatabase ()).getAllResourcesByServiceModelInvariantUuid(uuid, smVer);
 
 291                                         LOGGER.debug ("Query serviceMacroHolder getAllResourcesByServiceModelInvariantUuid serviceModelUuid: " + uuid);
 
 292                                         ret = (new CatalogDatabase ()).getAllResourcesByServiceModelInvariantUuid(uuid);
 
 296                                 throw(new Exception("no matching parameters"));
 
 300                                 LOGGER.debug ("serviceMacroHolder not found");
 
 301                                 respStatus = HttpStatus.SC_NOT_FOUND;
 
 302                                 qryResp = new QueryServiceMacroHolder();
 
 304                                 LOGGER.debug ("serviceMacroHolder found");
 
 305                                 qryResp = new QueryServiceMacroHolder(ret);
 
 306                                 LOGGER.debug ("serviceMacroHolder qryResp="+ qryResp);
 
 307                                 LOGGER.debug ("serviceMacroHolder tojsonstring="+ qryResp.toJsonString());
 
 309                         LOGGER.debug ("Query serviceMacroHolder exit");
 
 312                                 //.entity(new GenericEntity<QueryServiceMacroHolder>(qryResp) {})
 
 313                                 .entity(qryResp.toJsonString())
 
 314                                 .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
 
 316                 } catch (Exception e) {
 
 317                         LOGGER.error (MessageEnum.RA_QUERY_VNF_ERR,  uuid, "", "queryServiceMacroHolder", MsoLogger.ErrorCode.BusinessProcesssError, "Exception - queryServiceMacroHolder", e);
 
 318                         VfModuleExceptionResponse excResp = new VfModuleExceptionResponse(e.getMessage(), MsoExceptionCategory.INTERNAL, Boolean.FALSE, null);
 
 320                                 .status(HttpStatus.SC_INTERNAL_SERVER_ERROR)
 
 321                                 .entity(new GenericEntity<VfModuleExceptionResponse>(excResp) {})
 
 327         @Path("serviceAllottedResources")
 
 328         @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
 
 329         public Response serviceAllottedResources(
 
 330                         @QueryParam("serviceModelUuid") String smUuid,
 
 331                         @QueryParam("serviceModelInvariantUuid") String smiUuid,
 
 332                         @QueryParam("serviceModelVersion") String smVer,
 
 333                         @QueryParam("arModelCustomizationUuid") String aUuid
 
 335                 QueryAllottedResourceCustomization qryResp;
 
 336                 int respStatus = HttpStatus.SC_OK;
 
 338                 List<AllottedResourceCustomization > ret;
 
 342                         if (smUuid != null && !smUuid.equals("")) {
 
 344                                 LOGGER.debug ("Query AllottedResourceCustomization getAllAllottedResourcesByServiceModelUuid serviceModelUuid: " + uuid);
 
 345                                 ret = (new CatalogDatabase ()).getAllAllottedResourcesByServiceModelUuid(uuid);
 
 347                         else if (smiUuid != null && !smiUuid.equals("")) {
 
 349                                 if (smVer != null && !smVer.equals("")) {
 
 350                                         LOGGER.debug ("Query AllottedResourceCustomization getAllAllottedResourcesByServiceModelInvariantUuid serviceModelInvariantUuid: " + uuid+ " serviceModelVersion: "+ smVer);
 
 351                                         ret = (new CatalogDatabase ()).getAllAllottedResourcesByServiceModelInvariantUuid(uuid, smVer);
 
 354                                         LOGGER.debug ("Query AllottedResourceCustomization getAllAllottedResourcesByServiceModelInvariantUuid serviceModelUuid: " + uuid);
 
 355                                         ret = (new CatalogDatabase ()).getAllAllottedResourcesByServiceModelInvariantUuid(uuid);
 
 358                         else if (aUuid != null && !aUuid.equals("")) {
 
 360                                 LOGGER.debug ("Query AllottedResourceCustomization getAllAllottedResourcesByArModelCustomizationUuid serviceModelUuid: " + uuid);
 
 361                                 ret = (new CatalogDatabase ()).getAllAllottedResourcesByArModelCustomizationUuid(uuid);
 
 364                                 throw(new Exception("no matching parameters"));
 
 367                         if (ret == null || ret.isEmpty()) {
 
 368                                 LOGGER.debug ("AllottedResourceCustomization not found");
 
 369                                 respStatus = HttpStatus.SC_NOT_FOUND;
 
 370                                 qryResp = new QueryAllottedResourceCustomization();
 
 372                                 LOGGER.debug ("AllottedResourceCustomization found");
 
 373                                 qryResp = new QueryAllottedResourceCustomization(ret);
 
 374                                 LOGGER.debug ("AllottedResourceCustomization qryResp="+ qryResp);
 
 375                                 LOGGER.debug ("AllottedResourceCustomization tojsonstring="+ qryResp.toJsonString());
 
 377                         LOGGER.debug ("Query AllottedResourceCustomization exit");
 
 380                                 //.entity(new GenericEntity<QueryAllottedResourceCustomization>(qryResp) {})
 
 381                                 .entity(qryResp.toJsonString())
 
 382                                 .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
 
 384                 } catch (Exception e) {
 
 385                         LOGGER.error (MessageEnum.RA_QUERY_VNF_ERR,  uuid, "", "queryAllottedResourceCustomization", MsoLogger.ErrorCode.BusinessProcesssError, "Exception - queryAllottedResourceCustomization", e);
 
 386                         VfModuleExceptionResponse excResp = new VfModuleExceptionResponse(e.getMessage(), MsoExceptionCategory.INTERNAL, Boolean.FALSE, null);
 
 388                                 .status(HttpStatus.SC_INTERNAL_SERVER_ERROR)
 
 389                                 .entity(new GenericEntity<VfModuleExceptionResponse>(excResp) {})