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.datatypes.enums.ComponentTypeEnum;
7 import org.openecomp.sdc.be.dto.ExternalRefDTO;
8 import org.openecomp.sdc.be.impl.ComponentsUtils;
9 import org.openecomp.sdc.be.servlets.BeGenericServlet;
10 import org.openecomp.sdc.common.datastructure.Wrapper;
11 import org.openecomp.sdc.common.log.wrappers.Logger;
12 import org.springframework.stereotype.Controller;
15 import javax.ws.rs.core.MediaType;
16 import javax.ws.rs.core.Response;
17 import java.util.List;
22 public class ExternalRefsServlet extends BeGenericServlet {
24 private static final Logger log = Logger.getLogger(ExternalRefsServlet.class);
25 private final ComponentsUtils componentsUtils;
26 private final ExternalRefsBusinessLogic businessLogic;
28 public ExternalRefsServlet(ExternalRefsBusinessLogic businessLogic, ComponentsUtils componentsUtils){
29 this.businessLogic = businessLogic;
30 this.componentsUtils = componentsUtils;
34 @Path("/{assetType}/{uuid}/version/{version}/resourceInstances/{componentInstanceName}/externalReferences/{objectType}")
35 @Produces(MediaType.APPLICATION_JSON)
36 public Response getComponentInstanceExternalRef(
37 @PathParam("assetType") String assetType,
38 @PathParam("uuid") String uuid,
39 @PathParam("version") String version,
40 @PathParam("componentInstanceName") String componentInstanceName,
41 @PathParam("objectType") String objectType, @HeaderParam("USER_ID") String userId, @HeaderParam("X-ECOMP-InstanceID") String xEcompInstanceId) {
43 log.debug("GET component instance external interfaces {} {} {} {}", assetType, uuid, componentInstanceName, objectType);
45 Response r = validateRequest(xEcompInstanceId);
50 Either<List<String>, ActionStatus> refsResult = this.businessLogic.getExternalReferences(uuid, version, componentInstanceName, objectType);
51 if (refsResult.isLeft()){
52 return this.buildOkResponse(refsResult.left().value());
54 return this.buildExtRefErrorResponse(refsResult.right().value(), uuid, version, componentInstanceName, objectType, "");
59 @Path("/{assetType}/{uuid}/version/{version}/externalReferences/{objectType}")
60 @Produces(MediaType.APPLICATION_JSON)
61 public Map<String, List<String>> getAssetExternalRefByObjectType(
62 @PathParam("assetType") String assetType,
63 @PathParam("uuid") String uuid,
64 @PathParam("version") String version,
65 @PathParam("objectType") String objectType, @HeaderParam("USER_ID") String userId, @HeaderParam("X-ECOMP-InstanceID") String xEcompInstanceId) {
67 log.debug("GET asset external references {} {} {}", assetType, uuid, objectType);
69 Response r = validateRequest(xEcompInstanceId);
71 throw new WebApplicationException(r);
74 Either<Map<String, List<String>>, ActionStatus> refsResult = this.businessLogic.getExternalReferences(uuid, version, objectType);
75 if (refsResult.isLeft()){
76 return refsResult.left().value();
78 throw new WebApplicationException(this.buildExtRefErrorResponse(refsResult.right().value(), uuid, version, "", objectType, ""));
83 @Path("/{assetType}/{uuid}/resourceInstances/{componentInstanceName}/externalReferences/{objectType}")
84 @Consumes(MediaType.APPLICATION_JSON)
85 @Produces(MediaType.APPLICATION_JSON)
86 public Response addComponentInstanceExternalRef(
87 @PathParam("assetType") String assetType,
88 @PathParam("uuid") String uuid,
89 @PathParam("componentInstanceName") String componentInstanceName,
90 @PathParam("objectType") String objectType, ExternalRefDTO ref, @HeaderParam("USER_ID") String userId, @HeaderParam("X-ECOMP-InstanceID") String xEcompInstanceId) {
92 log.debug("POST component instance external interfaces {} {} {} {} {}", assetType, uuid, componentInstanceName, objectType, ref);
94 Response r = validateRequest(xEcompInstanceId);
99 Either<String, ActionStatus> addResult = this.businessLogic.addExternalReference(ComponentTypeEnum.findByParamName(assetType), userId, uuid, componentInstanceName, objectType, ref);
100 if (addResult.isLeft()) {
101 return Response.status(Response.Status.CREATED)
105 return this.buildExtRefErrorResponse(addResult.right().value(), uuid, "", componentInstanceName, objectType, ref.getReferenceUUID());
111 @Path("/{assetType}/{uuid}/resourceInstances/{componentInstanceName}/externalReferences/{objectType}/{reference}")
112 @Produces(MediaType.APPLICATION_JSON)
113 public Response deleteComponentInstanceReference(
114 @PathParam("assetType") String assetType,
115 @PathParam("uuid") String uuid,
116 @PathParam("componentInstanceName") String componentInstanceName,
117 @PathParam("objectType") String objectType,
118 @PathParam("reference") String reference, @HeaderParam("USER_ID") String userId, @HeaderParam("X-ECOMP-InstanceID") String xEcompInstanceId) {
120 log.debug("DELETE component instance external interfaces {} {} {} {}", assetType, uuid, componentInstanceName, objectType);
122 Response r = validateRequest(xEcompInstanceId);
127 Either<String, ActionStatus> deleteStatus = this.businessLogic.deleteExternalReference(ComponentTypeEnum.findByParamName(assetType), userId, uuid, componentInstanceName, objectType, reference);
128 if (deleteStatus.isLeft()){
129 return this.buildOkResponse(new ExternalRefDTO(reference));
131 return this.buildExtRefErrorResponse(deleteStatus.right().value(), uuid, "", componentInstanceName, objectType, reference);
136 @Path("/{assetType}/{uuid}/resourceInstances/{componentInstanceName}/externalReferences/{objectType}/{oldRefValue}")
137 @Produces(MediaType.APPLICATION_JSON)
138 @Consumes(MediaType.APPLICATION_JSON)
139 public Response updateComponentInstanceReference(
140 @PathParam("assetType") String assetType,
141 @PathParam("uuid") String uuid,
142 @PathParam("componentInstanceName") String componentInstanceName,
143 @PathParam("objectType") String objectType,
144 @PathParam("oldRefValue") String oldRefValue,
145 ExternalRefDTO newRefValueDTO, @HeaderParam("USER_ID") String userId, @HeaderParam("X-ECOMP-InstanceID") String xEcompInstanceId) {
147 log.debug("PUT component instance external interfaces {} {} {} {}", assetType, uuid, componentInstanceName, objectType);
149 Response r = validateRequest(xEcompInstanceId);
154 String newRefValue = newRefValueDTO.getReferenceUUID();
155 Either<String, ActionStatus> updateResult = this.businessLogic.updateExternalReference(ComponentTypeEnum.findByParamName(assetType), userId, uuid, componentInstanceName, objectType, oldRefValue, newRefValue);
156 if (updateResult.isLeft()){
157 return this.buildOkResponse(new ExternalRefDTO(newRefValue));
159 return this.buildExtRefErrorResponse(updateResult.right().value(), uuid, "", componentInstanceName, objectType, oldRefValue);
164 private Response validateRequest(String xEcompInstanceIdHeader) {
165 Wrapper<Response> responseWrapper = new Wrapper<>();
167 //Validate X-ECOMP_INSTANCE_ID_HEADER
168 if (xEcompInstanceIdHeader == null || xEcompInstanceIdHeader.isEmpty()){
169 return this.buildExtRefErrorResponse(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID, "", "", "", "", "");
172 return responseWrapper.getInnerElement();
175 private Response buildExtRefErrorResponse(ActionStatus status, String uuid, String version, String componentInstanceName, String objectType, String ref){
177 case RESOURCE_NOT_FOUND:
178 return buildErrorResponse(componentsUtils.getResponseFormat(status, uuid));
179 case COMPONENT_VERSION_NOT_FOUND:
180 return buildErrorResponse(componentsUtils.getResponseFormat(status, uuid, version));
181 case COMPONENT_INSTANCE_NOT_FOUND:
182 return buildErrorResponse(componentsUtils.getResponseFormat(status, componentInstanceName, uuid));
183 case EXT_REF_ALREADY_EXIST:
184 return Response.status(Response.Status.OK)
185 .entity(new ExternalRefDTO(ref))
187 case EXT_REF_NOT_FOUND:
188 return buildErrorResponse(componentsUtils.getResponseFormat(status, objectType + "/" + ref));
189 case MISSING_X_ECOMP_INSTANCE_ID:
190 return buildErrorResponse(componentsUtils.getResponseFormat(status));
192 return this.buildGeneralErrorResponse();