[MSO-8] Update the maven dependency
[so.git] / adapters / mso-catalog-db-adapter / src / main / java / org.openecomp.mso / adapters / catalogdb / CatalogDbAdapterRest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - MSO
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.openecomp.mso.adapters.catalogdb;
21
22 /*
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
26
27 Returns a structure (JSON?) containing VNF RESOURCE attributes, plus a list of VF Module structures.
28
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.
31
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
34
35 Update the MSO Catalog DB schema to include the new fields defined in this user story.
36
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.
41
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.
44
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).
47
48 min and initial counts can be 0. max can be null to indicate no maximum.
49
50 Once the network-level distribution artifacts are defined, similar updates can be made to the NETWORK_RESOURCE table.
51 */
52
53 import java.util.Map;
54 import java.util.List;
55
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;
71
72 import org.apache.http.HttpStatus;
73
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;
87
88 /**
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
92  */
93 @Path("/v1")
94 public class CatalogDbAdapterRest {
95         private static MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);
96
97         @HEAD
98         @GET
99         @Path("healthcheck")
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();
104         }
105
106         /*
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
109          * RESP:
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}}
111          */
112         @GET
113         @Path("serviceVnfs")
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
121                         ) {
122                 QueryServiceVnfs qryResp;
123                 int respStatus = HttpStatus.SC_OK;
124                 String uuid = "";
125                 List<VnfResource> ret;
126
127                 try {
128
129                         if (vnfUuid != null && !vnfUuid.equals("")) {
130                                 uuid = vnfUuid;
131                                 LOGGER.debug ("Query serviceVnfs getAllVnfsByVnfModelCustomizationUuid vnfModelCustomizationUuid: " + uuid);
132                                 ret = (new CatalogDatabase ()).getAllVnfsByVnfModelCustomizationUuid(uuid);
133                         }
134                         else if (smUuid != null && !smUuid.equals("")) {
135                                 uuid = smUuid;
136                                 LOGGER.debug ("Query serviceVnfs getAllVnfsByServiceModelUuid serviceModelUuid: " + uuid);
137                                 ret = (new CatalogDatabase ()).getAllVnfsByServiceModelUuid(uuid);
138                         }
139                         else if (smiUuid != null && !smiUuid.equals("")) {
140                                 uuid = smiUuid;
141                                 if (smVer != null && !smVer.equals("")) {
142                                         LOGGER.debug ("Query serviceVnfs getAllNetworksByServiceModelInvariantUuid serviceModelInvariantUuid: " + uuid+ " serviceModelVersion: "+ smVer);
143                                         ret = (new CatalogDatabase ()).getAllVnfsByServiceModelInvariantUuid(uuid, smVer);
144                                 }
145                                 else {
146                                         LOGGER.debug ("Query serviceVnfs getAllNetworksByServiceModelInvariantUuid serviceModelUuid: " + uuid);
147                                         ret = (new CatalogDatabase ()).getAllVnfsByServiceModelInvariantUuid(uuid);
148                                 }
149                         }
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);
154                                 }
155                                 else {
156                                         LOGGER.debug ("Query serviceVnfs getAllVnfsByServiceName serviceModelName: " + smName);
157                                         ret = (new CatalogDatabase ()).getAllVnfsByServiceName(smName);
158                                 }
159                         }
160                         else {
161                                 throw(new Exception("no matching parameters"));
162                         }
163
164                         if (ret == null || ret.isEmpty()) {
165                                 LOGGER.debug ("serviceVnfs not found");
166                                 respStatus = HttpStatus.SC_NOT_FOUND;
167                                 qryResp = new QueryServiceVnfs();
168                         } else {
169                                 LOGGER.debug ("serviceVnfs found");
170                                 qryResp = new QueryServiceVnfs(ret);
171                                 LOGGER.debug ("serviceVnfs qryResp="+ qryResp);
172                                 LOGGER.debug ("serviceVnfs tojsonstring="+ qryResp.toJsonString());
173                         }
174                         LOGGER.debug ("Query serviceVnfs exit");
175                         return Response
176                                 .status(respStatus)
177                                 //.entity(new GenericEntity<QueryServiceVnfs>(qryResp) {})
178                                 .entity(qryResp.toJsonString())
179                                 .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
180                                 .build();
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);
184                         return Response
185                                 .status(HttpStatus.SC_INTERNAL_SERVER_ERROR)
186                                 .entity(new GenericEntity<VfModuleExceptionResponse>(excResp) {})
187                                 .build();
188                 }
189         }
190
191         @GET
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
200                         ) {
201                 QueryServiceNetworks qryResp;
202                 int respStatus = HttpStatus.SC_OK;
203                 String uuid = "";
204                 List<NetworkResourceCustomization> ret;
205
206                 try {
207                         if (nUuid != null && !nUuid.equals("")) {
208                                 uuid = nUuid;
209                                 LOGGER.debug ("Query serviceNetworks getAllNetworksByNetworkModelCustomizationUuid networkModelCustomizationUuid: " + uuid);
210                                 ret = (new CatalogDatabase ()).getAllNetworksByNetworkModelCustomizationUuid(uuid);
211                         }
212                         else if (smUuid != null && !smUuid.equals("")) {
213                                 uuid = smUuid;
214                                 LOGGER.debug ("Query serviceNetworks getAllNetworksByServiceModelUuid serviceModelUuid: " + uuid);
215                                 ret = (new CatalogDatabase ()).getAllNetworksByServiceModelUuid(uuid);
216                         }
217                         else if (nType != null && !nType.equals("")) {
218                                 uuid = nType;
219                                 LOGGER.debug ("Query serviceNetworks getAllNetworksByNetworkType serviceModelUuid: " + uuid);
220                                 ret = (new CatalogDatabase ()).getAllNetworksByNetworkType(uuid);
221                         }
222                         else if (smiUuid != null && !smiUuid.equals("")) {
223                                 uuid = smiUuid;
224                                 if (smVer != null && !smVer.equals("")) {
225                                         LOGGER.debug ("Query serviceNetworks getAllNetworksByServiceModelInvariantUuid serviceModelInvariantUuid: " + uuid+ " serviceModelVersion: "+ smVer);
226                                         ret = (new CatalogDatabase ()).getAllNetworksByServiceModelInvariantUuid(uuid, smVer);
227                                 }
228                                 else {
229                                         LOGGER.debug ("Query serviceNetworks getAllNetworksByServiceModelInvariantUuid serviceModelUuid: " + uuid);
230                                         ret = (new CatalogDatabase ()).getAllNetworksByServiceModelInvariantUuid(uuid);
231                                 }
232                         }
233                         else {
234                                 throw(new Exception("no matching parameters"));
235                         }
236
237                         if (ret == null || ret.isEmpty()) {
238                                 LOGGER.debug ("serviceNetworks not found");
239                                 respStatus = HttpStatus.SC_NOT_FOUND;
240                                 qryResp = new QueryServiceNetworks();
241                         } else {
242                                 LOGGER.debug ("serviceNetworks found");
243                                 qryResp = new QueryServiceNetworks(ret);
244                                 LOGGER.debug ("serviceNetworks qryResp="+ qryResp);
245                                 LOGGER.debug ("serviceNetworks tojsonstring="+ qryResp.toJsonString());
246                         }
247                         LOGGER.debug ("Query serviceNetworks exit");
248                         return Response
249                                 .status(respStatus)
250                                 //.entity(new GenericEntity<QueryServiceNetworks>(qryResp) {})
251                                 .entity(qryResp.toJsonString())
252                                 .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
253                                 .build();
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);
257                         return Response
258                                 .status(HttpStatus.SC_INTERNAL_SERVER_ERROR)
259                                 .entity(new GenericEntity<VfModuleExceptionResponse>(excResp) {})
260                                 .build();
261                 }
262         }
263
264         @GET
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
271                         ) {
272                 QueryServiceMacroHolder qryResp;
273                 int respStatus = HttpStatus.SC_OK;
274                 String uuid = "";
275                 ServiceMacroHolder ret;
276
277                 try {
278
279                         if (smUuid != null && !smUuid.equals("")) {
280                                 uuid = smUuid;
281                                 LOGGER.debug ("Query serviceMacroHolder getAllResourcesByServiceModelUuid serviceModelUuid: " + uuid);
282                                 ret = (new CatalogDatabase ()).getAllResourcesByServiceModelUuid(uuid);
283                         }
284                         else if (smiUuid != null && !smiUuid.equals("")) {
285                                 uuid = smiUuid;
286                                 if (smVer != null && !smVer.equals("")) {
287                                         LOGGER.debug ("Query serviceMacroHolder getAllResourcesByServiceModelInvariantUuid serviceModelInvariantUuid: " + uuid+ " serviceModelVersion: "+ smVer);
288                                         ret = (new CatalogDatabase ()).getAllResourcesByServiceModelInvariantUuid(uuid, smVer);
289                                 }
290                                 else {
291                                         LOGGER.debug ("Query serviceMacroHolder getAllResourcesByServiceModelInvariantUuid serviceModelUuid: " + uuid);
292                                         ret = (new CatalogDatabase ()).getAllResourcesByServiceModelInvariantUuid(uuid);
293                                 }
294                         }
295                         else {
296                                 throw(new Exception("no matching parameters"));
297                         }
298
299                         if (ret == null) {
300                                 LOGGER.debug ("serviceMacroHolder not found");
301                                 respStatus = HttpStatus.SC_NOT_FOUND;
302                                 qryResp = new QueryServiceMacroHolder();
303                         } else {
304                                 LOGGER.debug ("serviceMacroHolder found");
305                                 qryResp = new QueryServiceMacroHolder(ret);
306                                 LOGGER.debug ("serviceMacroHolder qryResp="+ qryResp);
307                                 LOGGER.debug ("serviceMacroHolder tojsonstring="+ qryResp.toJsonString());
308                         }
309                         LOGGER.debug ("Query serviceMacroHolder exit");
310                         return Response
311                                 .status(respStatus)
312                                 //.entity(new GenericEntity<QueryServiceMacroHolder>(qryResp) {})
313                                 .entity(qryResp.toJsonString())
314                                 .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
315                                 .build();
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);
319                         return Response
320                                 .status(HttpStatus.SC_INTERNAL_SERVER_ERROR)
321                                 .entity(new GenericEntity<VfModuleExceptionResponse>(excResp) {})
322                                 .build();
323                 }
324         }
325
326         @GET
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
334                         ) {
335                 QueryAllottedResourceCustomization qryResp;
336                 int respStatus = HttpStatus.SC_OK;
337                 String uuid = "";
338                 List<AllottedResourceCustomization > ret;
339
340                 try {
341
342                         if (smUuid != null && !smUuid.equals("")) {
343                                 uuid = smUuid;
344                                 LOGGER.debug ("Query AllottedResourceCustomization getAllAllottedResourcesByServiceModelUuid serviceModelUuid: " + uuid);
345                                 ret = (new CatalogDatabase ()).getAllAllottedResourcesByServiceModelUuid(uuid);
346                         }
347                         else if (smiUuid != null && !smiUuid.equals("")) {
348                                 uuid = smiUuid;
349                                 if (smVer != null && !smVer.equals("")) {
350                                         LOGGER.debug ("Query AllottedResourceCustomization getAllAllottedResourcesByServiceModelInvariantUuid serviceModelInvariantUuid: " + uuid+ " serviceModelVersion: "+ smVer);
351                                         ret = (new CatalogDatabase ()).getAllAllottedResourcesByServiceModelInvariantUuid(uuid, smVer);
352                                 }
353                                 else {
354                                         LOGGER.debug ("Query AllottedResourceCustomization getAllAllottedResourcesByServiceModelInvariantUuid serviceModelUuid: " + uuid);
355                                         ret = (new CatalogDatabase ()).getAllAllottedResourcesByServiceModelInvariantUuid(uuid);
356                                 }
357                         }
358                         else if (aUuid != null && !aUuid.equals("")) {
359                                 uuid = aUuid;
360                                 LOGGER.debug ("Query AllottedResourceCustomization getAllAllottedResourcesByArModelCustomizationUuid serviceModelUuid: " + uuid);
361                                 ret = (new CatalogDatabase ()).getAllAllottedResourcesByArModelCustomizationUuid(uuid);
362                         }
363                         else {
364                                 throw(new Exception("no matching parameters"));
365                         }
366
367                         if (ret == null || ret.isEmpty()) {
368                                 LOGGER.debug ("AllottedResourceCustomization not found");
369                                 respStatus = HttpStatus.SC_NOT_FOUND;
370                                 qryResp = new QueryAllottedResourceCustomization();
371                         } else {
372                                 LOGGER.debug ("AllottedResourceCustomization found");
373                                 qryResp = new QueryAllottedResourceCustomization(ret);
374                                 LOGGER.debug ("AllottedResourceCustomization qryResp="+ qryResp);
375                                 LOGGER.debug ("AllottedResourceCustomization tojsonstring="+ qryResp.toJsonString());
376                         }
377                         LOGGER.debug ("Query AllottedResourceCustomization exit");
378                         return Response
379                                 .status(respStatus)
380                                 //.entity(new GenericEntity<QueryAllottedResourceCustomization>(qryResp) {})
381                                 .entity(qryResp.toJsonString())
382                                 .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
383                                 .build();
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);
387                         return Response
388                                 .status(HttpStatus.SC_INTERNAL_SERVER_ERROR)
389                                 .entity(new GenericEntity<VfModuleExceptionResponse>(excResp) {})
390                                 .build();
391                 }
392         }
393 }