re base code
[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.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;
13
14 import javax.ws.rs.*;
15 import javax.ws.rs.core.MediaType;
16 import javax.ws.rs.core.Response;
17 import java.util.List;
18 import java.util.Map;
19
20 @Path("/v1/catalog")
21 @Controller
22 public class ExternalRefsServlet extends BeGenericServlet {
23
24     private static final Logger log = Logger.getLogger(ExternalRefsServlet.class);
25     private final ComponentsUtils componentsUtils;
26     private final ExternalRefsBusinessLogic businessLogic;
27
28     public ExternalRefsServlet(ExternalRefsBusinessLogic businessLogic, ComponentsUtils componentsUtils){
29         this.businessLogic = businessLogic;
30         this.componentsUtils = componentsUtils;
31     }
32
33     @GET
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) {
42
43         log.debug("GET component instance external interfaces {} {} {} {}", assetType, uuid, componentInstanceName, objectType);
44
45         Response r = validateRequest(xEcompInstanceId);
46         if (r != null){
47             return r;
48         }
49
50         Either<List<String>, ActionStatus> refsResult = this.businessLogic.getExternalReferences(uuid, version, componentInstanceName, objectType);
51         if (refsResult.isLeft()){
52             return this.buildOkResponse(refsResult.left().value());
53         } else {
54             return this.buildExtRefErrorResponse(refsResult.right().value(), uuid, version, componentInstanceName, objectType, "");
55         }
56     }
57
58     @GET
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) {
66
67         log.debug("GET asset external references {} {} {}", assetType, uuid, objectType);
68
69         Response r = validateRequest(xEcompInstanceId);
70         if (r != null){
71             throw new WebApplicationException(r);
72         }
73
74         Either<Map<String, List<String>>, ActionStatus> refsResult = this.businessLogic.getExternalReferences(uuid, version, objectType);
75         if (refsResult.isLeft()){
76             return refsResult.left().value();
77         } else {
78             throw new WebApplicationException(this.buildExtRefErrorResponse(refsResult.right().value(), uuid, version, "", objectType, ""));
79         }
80     }
81
82     @POST
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) {
91
92         log.debug("POST component instance external interfaces {} {} {} {} {}", assetType, uuid, componentInstanceName, objectType, ref);
93
94         Response r = validateRequest(xEcompInstanceId);
95         if (r != null){
96             return r;
97         }
98
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)
102                     .entity(ref)
103                     .build();
104         } else {
105             return this.buildExtRefErrorResponse(addResult.right().value(), uuid, "", componentInstanceName, objectType, ref.getReferenceUUID());
106         }
107
108     }
109
110     @DELETE
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) {
119
120         log.debug("DELETE component instance external interfaces {} {} {} {}", assetType, uuid, componentInstanceName, objectType);
121
122         Response r = validateRequest(xEcompInstanceId);
123         if (r != null){
124             return r;
125         }
126
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));
130         } else {
131             return this.buildExtRefErrorResponse(deleteStatus.right().value(), uuid, "", componentInstanceName, objectType, reference);
132         }
133     }
134
135     @PUT
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) {
146
147         log.debug("PUT component instance external interfaces {} {} {} {}", assetType, uuid, componentInstanceName, objectType);
148
149         Response r = validateRequest(xEcompInstanceId);
150         if (r != null){
151             return r;
152         }
153
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));
158         } else {
159             return this.buildExtRefErrorResponse(updateResult.right().value(), uuid, "", componentInstanceName, objectType, oldRefValue);
160         }
161
162     }
163
164     private Response validateRequest(String xEcompInstanceIdHeader) {
165         Wrapper<Response> responseWrapper = new Wrapper<>();
166
167         //Validate X-ECOMP_INSTANCE_ID_HEADER
168         if (xEcompInstanceIdHeader == null || xEcompInstanceIdHeader.isEmpty()){
169             return this.buildExtRefErrorResponse(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID, "", "", "", "", "");
170         }
171
172         return responseWrapper.getInnerElement();
173     }
174
175     private Response buildExtRefErrorResponse(ActionStatus status, String uuid, String version, String componentInstanceName, String objectType, String ref){
176         switch (status) {
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))
186                         .build();
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));
191             default:
192                 return this.buildGeneralErrorResponse();
193         }
194     }
195 }