0c3595e05dc9c5f268d54df50745d3bc6fce62bb
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / externalapi / servlet / ExternalRefsServlet.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 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.sdc.be.externalapi.servlet;
21
22 import fj.data.Either;
23 import io.swagger.v3.oas.annotations.servers.Server;
24 import io.swagger.v3.oas.annotations.tags.Tag;
25 import java.util.List;
26 import java.util.Map;
27 import javax.inject.Inject;
28 import javax.ws.rs.Consumes;
29 import javax.ws.rs.DELETE;
30 import javax.ws.rs.GET;
31 import javax.ws.rs.HeaderParam;
32 import javax.ws.rs.POST;
33 import javax.ws.rs.PUT;
34 import javax.ws.rs.Path;
35 import javax.ws.rs.PathParam;
36 import javax.ws.rs.Produces;
37 import javax.ws.rs.WebApplicationException;
38 import javax.ws.rs.core.MediaType;
39 import javax.ws.rs.core.Response;
40 import org.openecomp.sdc.be.components.impl.ExternalRefsBusinessLogic;
41 import org.openecomp.sdc.be.components.impl.aaf.AafPermission;
42 import org.openecomp.sdc.be.components.impl.aaf.PermissionAllowed;
43 import org.openecomp.sdc.be.dao.api.ActionStatus;
44 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
45 import org.openecomp.sdc.be.dto.ExternalRefDTO;
46 import org.openecomp.sdc.be.impl.ComponentsUtils;
47 import org.openecomp.sdc.be.servlets.BeGenericServlet;
48 import org.openecomp.sdc.be.user.UserBusinessLogic;
49 import org.openecomp.sdc.common.datastructure.Wrapper;
50 import org.openecomp.sdc.common.log.wrappers.Logger;
51 import org.springframework.stereotype.Controller;
52
53 @Path("/v1/catalog")
54 @Tag(name = "SDCE-7 APIs")
55 @Server(url = "/sdc")
56 @Controller
57 public class ExternalRefsServlet extends BeGenericServlet {
58
59     private static final Logger log = Logger.getLogger(ExternalRefsServlet.class);
60     private final ExternalRefsBusinessLogic businessLogic;
61
62     @Inject
63     public ExternalRefsServlet(UserBusinessLogic userBusinessLogic, ComponentsUtils componentsUtils,
64                                ExternalRefsBusinessLogic externalRefsBusinessLogic) {
65         super(userBusinessLogic, componentsUtils);
66         this.businessLogic = externalRefsBusinessLogic;
67     }
68
69     @GET
70     @Path("/{assetType}/{uuid}/version/{version}/resourceInstances/{componentInstanceName}/externalReferences/{objectType}")
71     @Produces(MediaType.APPLICATION_JSON)
72     @PermissionAllowed({AafPermission.PermNames.READ_VALUE})
73     public Response getComponentInstanceExternalRef(@PathParam("assetType") String assetType, @PathParam("uuid") String uuid,
74                                                     @PathParam("version") String version,
75                                                     @PathParam("componentInstanceName") String componentInstanceName,
76                                                     @PathParam("objectType") String objectType, @HeaderParam("USER_ID") String userId,
77                                                     @HeaderParam("X-ECOMP-InstanceID") String xEcompInstanceId) {
78         log.debug("GET component instance external interfaces {} {} {} {}", assetType, uuid, componentInstanceName, objectType);
79         Response r = validateRequest(xEcompInstanceId);
80         if (r != null) {
81             return r;
82         }
83         Either<List<String>, ActionStatus> refsResult = this.businessLogic.getExternalReferences(uuid, version, componentInstanceName, objectType);
84         if (refsResult.isLeft()) {
85             return this.buildOkResponse(refsResult.left().value());
86         } else {
87             return this.buildExtRefErrorResponse(refsResult.right().value(), uuid, version, componentInstanceName, objectType, "");
88         }
89     }
90
91     @GET
92     @Path("/{assetType}/{uuid}/version/{version}/externalReferences/{objectType}")
93     @Produces(MediaType.APPLICATION_JSON)
94     @PermissionAllowed({AafPermission.PermNames.READ_VALUE})
95     public Map<String, List<String>> getAssetExternalRefByObjectType(@PathParam("assetType") String assetType, @PathParam("uuid") String uuid,
96                                                                      @PathParam("version") String version, @PathParam("objectType") String objectType,
97                                                                      @HeaderParam("USER_ID") String userId,
98                                                                      @HeaderParam("X-ECOMP-InstanceID") String xEcompInstanceId) {
99         log.debug("GET asset external references {} {} {}", assetType, uuid, objectType);
100         Response r = validateRequest(xEcompInstanceId);
101         if (r != null) {
102             throw new WebApplicationException(r);
103         }
104         Either<Map<String, List<String>>, ActionStatus> refsResult = this.businessLogic.getExternalReferences(uuid, version, objectType);
105         if (refsResult.isLeft()) {
106             return refsResult.left().value();
107         } else {
108             throw new WebApplicationException(this.buildExtRefErrorResponse(refsResult.right().value(), uuid, version, "", objectType, ""));
109         }
110     }
111
112     @POST
113     @Path("/{assetType}/{uuid}/resourceInstances/{componentInstanceName}/externalReferences/{objectType}")
114     @Consumes(MediaType.APPLICATION_JSON)
115     @Produces(MediaType.APPLICATION_JSON)
116     @PermissionAllowed({AafPermission.PermNames.WRITE_VALUE})
117     public Response addComponentInstanceExternalRef(@PathParam("assetType") String assetType, @PathParam("uuid") String uuid,
118                                                     @PathParam("componentInstanceName") String componentInstanceName,
119                                                     @PathParam("objectType") String objectType, ExternalRefDTO ref,
120                                                     @HeaderParam("USER_ID") String userId,
121                                                     @HeaderParam("X-ECOMP-InstanceID") String xEcompInstanceId) {
122         log.debug("POST component instance external interfaces {} {} {} {} {}", assetType, uuid, componentInstanceName, objectType, ref);
123         Response r = validateRequest(xEcompInstanceId);
124         if (r != null) {
125             return r;
126         }
127         ComponentTypeEnum componentType = ComponentTypeEnum.findByParamName(assetType);
128         String uid = this.businessLogic.fetchComponentUniqueIdByUuid(uuid, componentType);
129         Either<String, ActionStatus> addResult = this.businessLogic
130             .addExternalReference(uid, componentType, userId, componentInstanceName, objectType, ref);
131         if (addResult.isLeft()) {
132             return Response.status(Response.Status.CREATED).entity(ref).build();
133         } else {
134             return this.buildExtRefErrorResponse(addResult.right().value(), uuid, "", componentInstanceName, objectType, ref.getReferenceUUID());
135         }
136     }
137
138     @DELETE
139     @Path("/{assetType}/{uuid}/resourceInstances/{componentInstanceName}/externalReferences/{objectType}/{reference}")
140     @Produces(MediaType.APPLICATION_JSON)
141     @PermissionAllowed({AafPermission.PermNames.DELETE_VALUE})
142     public Response deleteComponentInstanceReference(@PathParam("assetType") String assetType, @PathParam("uuid") String uuid,
143                                                      @PathParam("componentInstanceName") String componentInstanceName,
144                                                      @PathParam("objectType") String objectType, @PathParam("reference") String reference,
145                                                      @HeaderParam("USER_ID") String userId,
146                                                      @HeaderParam("X-ECOMP-InstanceID") String xEcompInstanceId) {
147         log.debug("DELETE component instance external interfaces {} {} {} {}", assetType, uuid, componentInstanceName, objectType);
148         Response r = validateRequest(xEcompInstanceId);
149         if (r != null) {
150             return r;
151         }
152         ComponentTypeEnum componentType = ComponentTypeEnum.findByParamName(assetType);
153         String uid = this.businessLogic.fetchComponentUniqueIdByUuid(uuid, componentType);
154         Either<String, ActionStatus> deleteStatus = this.businessLogic
155             .deleteExternalReference(uid, componentType, userId, componentInstanceName, objectType, reference);
156         if (deleteStatus.isLeft()) {
157             return this.buildOkResponse(new ExternalRefDTO(reference));
158         } else {
159             return this.buildExtRefErrorResponse(deleteStatus.right().value(), uuid, "", componentInstanceName, objectType, reference);
160         }
161     }
162
163     @PUT
164     @Path("/{assetType}/{uuid}/resourceInstances/{componentInstanceName}/externalReferences/{objectType}/{oldRefValue}")
165     @Produces(MediaType.APPLICATION_JSON)
166     @Consumes(MediaType.APPLICATION_JSON)
167     @PermissionAllowed({AafPermission.PermNames.WRITE_VALUE})
168     public Response updateComponentInstanceReference(@PathParam("assetType") String assetType, @PathParam("uuid") String uuid,
169                                                      @PathParam("componentInstanceName") String componentInstanceName,
170                                                      @PathParam("objectType") String objectType, @PathParam("oldRefValue") String oldRefValue,
171                                                      ExternalRefDTO newRefValueDTO, @HeaderParam("USER_ID") String userId,
172                                                      @HeaderParam("X-ECOMP-InstanceID") String xEcompInstanceId) {
173         log.debug("PUT component instance external interfaces {} {} {} {}", assetType, uuid, componentInstanceName, objectType);
174         Response r = validateRequest(xEcompInstanceId);
175         if (r != null) {
176             return r;
177         }
178         String newRefValue = newRefValueDTO.getReferenceUUID();
179         ComponentTypeEnum componentType = ComponentTypeEnum.findByParamName(assetType);
180         String uid = this.businessLogic.fetchComponentUniqueIdByUuid(uuid, componentType);
181         Either<String, ActionStatus> updateResult = this.businessLogic
182             .updateExternalReference(uid, componentType, userId, componentInstanceName, objectType, oldRefValue, newRefValue);
183         if (updateResult.isLeft()) {
184             return this.buildOkResponse(new ExternalRefDTO(newRefValue));
185         } else {
186             return this.buildExtRefErrorResponse(updateResult.right().value(), uuid, "", componentInstanceName, objectType, oldRefValue);
187         }
188     }
189
190     private Response validateRequest(String xEcompInstanceIdHeader) {
191         Wrapper<Response> responseWrapper = new Wrapper<>();
192         //Validate X-ECOMP_INSTANCE_ID_HEADER
193         if (xEcompInstanceIdHeader == null || xEcompInstanceIdHeader.isEmpty()) {
194             return this.buildExtRefErrorResponse(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID, "", "", "", "", "");
195         }
196         return responseWrapper.getInnerElement();
197     }
198
199     private Response buildExtRefErrorResponse(ActionStatus status, String uuid, String version, String componentInstanceName, String objectType,
200                                               String ref) {
201         switch (status) {
202             case RESOURCE_NOT_FOUND:
203                 return buildErrorResponse(componentsUtils.getResponseFormat(status, uuid));
204             case COMPONENT_VERSION_NOT_FOUND:
205                 return buildErrorResponse(componentsUtils.getResponseFormat(status, uuid, version));
206             case COMPONENT_INSTANCE_NOT_FOUND:
207                 return buildErrorResponse(componentsUtils.getResponseFormat(status, componentInstanceName, uuid));
208             case EXT_REF_ALREADY_EXIST:
209                 return Response.status(Response.Status.OK).entity(new ExternalRefDTO(ref)).build();
210             case EXT_REF_NOT_FOUND:
211                 return buildErrorResponse(componentsUtils.getResponseFormat(status, objectType + "/" + ref));
212             case MISSING_X_ECOMP_INSTANCE_ID:
213                 return buildErrorResponse(componentsUtils.getResponseFormat(status));
214             default:
215                 return this.buildGeneralErrorResponse();
216         }
217     }
218 }