1 package org.openecomp.sdc.be.externalapi.servlet;
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;
16 import javax.servlet.http.HttpServletRequest;
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;
27 public class ExternalRefsServlet extends AbstractValidationsServlet {
29 private static final Logger log = LoggerFactory.getLogger(ExternalRefsServlet.class);
31 private ExternalRefsBusinessLogic businessLogic;
33 public ExternalRefsServlet(ExternalRefsBusinessLogic businessLogic, ComponentsUtils componentsUtils){
34 this.businessLogic = businessLogic;
35 this.componentsUtils = componentsUtils;
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) {
49 log.debug("GET component instance external interfaces {} {} {} {}", assetType, uuid, componentInstanceName, objectType);
51 Response r = authorizeAndValidateRequest(request, userId);
56 Either<List<String>, ActionStatus> refsResult = this.businessLogic.getExternalReferences(uuid, version, componentInstanceName, objectType);
57 if (refsResult.isLeft()){
58 return this.buildOkResponse(refsResult.left().value());
60 return this.buildExtRefErrorResponse(refsResult.right().value(), uuid, version, componentInstanceName, objectType, "");
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) {
74 log.debug("GET asset external references {} {} {}", assetType, uuid, objectType);
76 Response r = authorizeAndValidateRequest(request, userId);
78 throw new WebApplicationException(r);
81 Either<Map<String, List<String>>, ActionStatus> refsResult = this.businessLogic.getExternalReferences(uuid, version, objectType);
82 if (refsResult.isLeft()){
83 return refsResult.left().value();
85 throw new WebApplicationException(this.buildExtRefErrorResponse(refsResult.right().value(), uuid, version, "", objectType, ""));
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) {
100 log.debug("POST component instance external interfaces {} {} {} {} {}", assetType, uuid, componentInstanceName, objectType, ref);
102 Response r = authorizeAndValidateRequest(request, userId);
107 Either<String, ActionStatus> addResult = this.businessLogic.addExternalReference(uuid, componentInstanceName, objectType, ref);
108 if (addResult.isLeft()) {
109 return Response.status(Response.Status.CREATED)
113 return this.buildExtRefErrorResponse(addResult.right().value(), uuid, "", componentInstanceName, objectType, ref.getReferenceUUID());
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) {
129 log.debug("DELETE component instance external interfaces {} {} {} {}", assetType, uuid, componentInstanceName, objectType);
131 Response r = authorizeAndValidateRequest(request, userId);
136 Either<String, ActionStatus> deleteStatus = this.businessLogic.deleteExternalReference(uuid, componentInstanceName, objectType, reference);
137 if (deleteStatus.isLeft()){
138 return this.buildOkResponse(new ExternalRefDTO(reference));
140 return this.buildExtRefErrorResponse(deleteStatus.right().value(), uuid, "", componentInstanceName, objectType, reference);
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) {
157 log.debug("PUT component instance external interfaces {} {} {} {}", assetType, uuid, componentInstanceName, objectType);
159 Response r = authorizeAndValidateRequest(request, userId);
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));
169 return this.buildExtRefErrorResponse(updateResult.right().value(), uuid, "", componentInstanceName, objectType, oldRefValue);
174 private Response authorizeAndValidateRequest(final HttpServletRequest request, String userId) {
177 Wrapper<Response> responseWrapper = new Wrapper<>();
178 Wrapper<User> userWrapper = new Wrapper<>();
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, "", "", "", "", "");
185 String method = request.getMethod();
186 if (responseWrapper.isEmpty() && !"GET".equals(method)) {
187 validateUserExist(responseWrapper, userWrapper, userId);
188 validateUserRole(responseWrapper, userWrapper.getInnerElement());
191 return responseWrapper.getInnerElement();
194 private Response buildExtRefErrorResponse(ActionStatus status, String uuid, String version, String componentInstanceName, String objectType, String ref){
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))
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));
211 return this.buildGeneralErrorResponse();