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