Sync Integ to Master
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / externalapi / servlet / ExternalRefsServlet.java
1 package org.openecomp.sdc.be.externalapi.servlet;
2
3 import fj.data.Either;
4 import org.openecomp.sdc.be.components.impl.ExternalRefsBusinessLogic;
5 import org.openecomp.sdc.be.dao.api.ActionStatus;
6 import org.openecomp.sdc.be.dto.ExternalRefDTO;
7 import org.openecomp.sdc.be.impl.ComponentsUtils;
8 import org.openecomp.sdc.be.model.User;
9 import org.openecomp.sdc.be.servlets.AbstractValidationsServlet;
10 import org.openecomp.sdc.common.api.Constants;
11 import org.openecomp.sdc.common.datastructure.Wrapper;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14 import org.springframework.stereotype.Controller;
15
16 import javax.servlet.http.HttpServletRequest;
17 import javax.ws.rs.*;
18 import javax.ws.rs.core.Context;
19 import javax.ws.rs.core.MediaType;
20 import javax.ws.rs.core.Response;
21 import java.util.List;
22 import java.util.Map;
23
24
25 @Path("/v1/catalog")
26 @Controller
27 public class ExternalRefsServlet extends AbstractValidationsServlet {
28
29     private static final Logger log = LoggerFactory.getLogger(ExternalRefsServlet.class);
30
31     private ExternalRefsBusinessLogic businessLogic;
32
33     public ExternalRefsServlet(ExternalRefsBusinessLogic businessLogic, ComponentsUtils componentsUtils){
34         this.businessLogic = businessLogic;
35         this.componentsUtils = componentsUtils;
36     }
37
38     @GET
39     @Path("/{assetType}/{uuid}/version/{version}/resourceInstances/{componentInstanceName}/externalReferences/{objectType}")
40     @Produces(MediaType.APPLICATION_JSON)
41     public Response getComponentInstanceExternalRef(
42             @Context final HttpServletRequest request,
43             @PathParam("assetType") String assetType,
44             @PathParam("uuid") String uuid,
45             @PathParam("version") String version,
46             @PathParam("componentInstanceName") String componentInstanceName,
47             @PathParam("objectType") String objectType, @HeaderParam("USER_ID") String userId) {
48
49         log.debug("GET component instance external interfaces {} {} {} {}", assetType, uuid, componentInstanceName, objectType);
50
51         Response r = authorizeAndValidateRequest(request, userId);
52         if (r != null){
53             return r;
54         }
55
56         Either<List<String>, ActionStatus> refsResult = this.businessLogic.getExternalReferences(uuid, version, componentInstanceName, objectType);
57         if (refsResult.isLeft()){
58             return this.buildOkResponse(refsResult.left().value());
59         } else {
60             return this.buildExtRefErrorResponse(refsResult.right().value(), uuid, version, componentInstanceName, objectType, "");
61         }
62     }
63
64     @GET
65     @Path("/{assetType}/{uuid}/version/{version}/externalReferences/{objectType}")
66     @Produces(MediaType.APPLICATION_JSON)
67     public Map<String, List<String>> getAssetExternalRefByObjectType(
68             @Context final HttpServletRequest request,
69             @PathParam("assetType") String assetType,
70             @PathParam("uuid") String uuid,
71             @PathParam("version") String version,
72             @PathParam("objectType") String objectType, @HeaderParam("USER_ID") String userId) {
73
74         log.debug("GET asset external references {} {} {}", assetType, uuid, objectType);
75
76         Response r = authorizeAndValidateRequest(request, userId);
77         if (r != null){
78             throw new WebApplicationException(r);
79         }
80
81         Either<Map<String, List<String>>, ActionStatus> refsResult = this.businessLogic.getExternalReferences(uuid, version, objectType);
82         if (refsResult.isLeft()){
83             return refsResult.left().value();
84         } else {
85             throw new WebApplicationException(this.buildExtRefErrorResponse(refsResult.right().value(), uuid, version, "", objectType, ""));
86         }
87     }
88
89     @POST
90     @Path("/{assetType}/{uuid}/resourceInstances/{componentInstanceName}/externalReferences/{objectType}")
91     @Consumes(MediaType.APPLICATION_JSON)
92     @Produces(MediaType.APPLICATION_JSON)
93     public Response addComponentInstanceExternalRef(
94             @Context final HttpServletRequest request,
95             @PathParam("assetType") String assetType,
96             @PathParam("uuid") String uuid,
97             @PathParam("componentInstanceName") String componentInstanceName,
98             @PathParam("objectType") String objectType, ExternalRefDTO ref, @HeaderParam("USER_ID") String userId) {
99
100         log.debug("POST component instance external interfaces {} {} {} {} {}", assetType, uuid, componentInstanceName, objectType, ref);
101
102         Response r = authorizeAndValidateRequest(request, userId);
103         if (r != null){
104             return r;
105         }
106
107         Either<String, ActionStatus> addResult = this.businessLogic.addExternalReference(uuid, componentInstanceName, objectType, ref);
108         if (addResult.isLeft()) {
109             return Response.status(Response.Status.CREATED)
110                     .entity(ref)
111                     .build();
112         } else {
113             return this.buildExtRefErrorResponse(addResult.right().value(), uuid, "", componentInstanceName, objectType, ref.getReferenceUUID());
114         }
115
116     }
117
118     @DELETE
119     @Path("/{assetType}/{uuid}/resourceInstances/{componentInstanceName}/externalReferences/{objectType}/{reference}")
120     @Produces(MediaType.APPLICATION_JSON)
121     public Response deleteComponentInstanceReference(
122             @Context final HttpServletRequest request,
123             @PathParam("assetType") String assetType,
124             @PathParam("uuid") String uuid,
125             @PathParam("componentInstanceName") String componentInstanceName,
126             @PathParam("objectType") String objectType,
127             @PathParam("reference") String reference, @HeaderParam("USER_ID") String userId) {
128
129         log.debug("DELETE component instance external interfaces {} {} {} {}", assetType, uuid, componentInstanceName, objectType);
130
131         Response r = authorizeAndValidateRequest(request, userId);
132         if (r != null){
133             return r;
134         }
135
136         Either<String, ActionStatus> deleteStatus = this.businessLogic.deleteExternalReference(uuid, componentInstanceName, objectType, reference);
137         if (deleteStatus.isLeft()){
138             return this.buildOkResponse(new ExternalRefDTO(reference));
139         } else {
140             return this.buildExtRefErrorResponse(deleteStatus.right().value(), uuid, "", componentInstanceName, objectType, reference);
141         }
142     }
143
144     @PUT
145     @Path("/{assetType}/{uuid}/resourceInstances/{componentInstanceName}/externalReferences/{objectType}/{oldRefValue}")
146     @Produces(MediaType.APPLICATION_JSON)
147     @Consumes(MediaType.APPLICATION_JSON)
148     public Response updateComponentInstanceReference(
149             @Context final HttpServletRequest request,
150             @PathParam("assetType") String assetType,
151             @PathParam("uuid") String uuid,
152             @PathParam("componentInstanceName") String componentInstanceName,
153             @PathParam("objectType") String objectType,
154             @PathParam("oldRefValue") String oldRefValue,
155             ExternalRefDTO newRefValueDTO, @HeaderParam("USER_ID") String userId) {
156
157         log.debug("PUT component instance external interfaces {} {} {} {}", assetType, uuid, componentInstanceName, objectType);
158
159         Response r = authorizeAndValidateRequest(request, userId);
160         if (r != null){
161             return r;
162         }
163
164         String newRefValue = newRefValueDTO.getReferenceUUID();
165         Either<String, ActionStatus> updateResult = this.businessLogic.updateExternalReference(uuid, componentInstanceName, objectType, oldRefValue, newRefValue);
166         if (updateResult.isLeft()){
167             return this.buildOkResponse(new ExternalRefDTO(newRefValue));
168         } else {
169             return this.buildExtRefErrorResponse(updateResult.right().value(), uuid, "", componentInstanceName, objectType, oldRefValue);
170         }
171
172     }
173
174     private Response authorizeAndValidateRequest(final HttpServletRequest request, String userId) {
175         init(log);
176
177         Wrapper<Response> responseWrapper = new Wrapper<>();
178         Wrapper<User> userWrapper = new Wrapper<>();
179
180         //Validate X-ECOMP_INSTANCE_ID_HEADER
181         if (request.getHeader(Constants.X_ECOMP_INSTANCE_ID_HEADER) == null || request.getHeader(Constants.X_ECOMP_INSTANCE_ID_HEADER).isEmpty()){
182             return this.buildExtRefErrorResponse(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID, "", "", "", "", "");
183         }
184
185         String method = request.getMethod();
186         if (responseWrapper.isEmpty() && !"GET".equals(method)) {
187             validateUserExist(responseWrapper, userWrapper, userId);
188             validateUserRole(responseWrapper, userWrapper.getInnerElement());
189         }
190
191         return responseWrapper.getInnerElement();
192     }
193
194     private Response buildExtRefErrorResponse(ActionStatus status, String uuid, String version, String componentInstanceName, String objectType, String ref){
195         switch (status) {
196             case RESOURCE_NOT_FOUND:
197                 return buildErrorResponse(componentsUtils.getResponseFormat(status, uuid));
198             case COMPONENT_VERSION_NOT_FOUND:
199                 return buildErrorResponse(componentsUtils.getResponseFormat(status, uuid, version));
200             case COMPONENT_INSTANCE_NOT_FOUND:
201                 return buildErrorResponse(componentsUtils.getResponseFormat(status, componentInstanceName, uuid));
202             case EXT_REF_ALREADY_EXIST:
203                 return Response.status(Response.Status.OK)
204                         .entity(new ExternalRefDTO(ref))
205                         .build();
206             case EXT_REF_NOT_FOUND:
207                 return buildErrorResponse(componentsUtils.getResponseFormat(status, objectType + "/" + ref));
208             case MISSING_X_ECOMP_INSTANCE_ID:
209                 return buildErrorResponse(componentsUtils.getResponseFormat(status));
210             default:
211                 return this.buildGeneralErrorResponse();
212         }
213     }
214 }