catalog-be servlets refactoring
[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
21 package org.openecomp.sdc.be.externalapi.servlet;
22
23 import fj.data.Either;
24 import javax.inject.Inject;
25 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
26 import org.openecomp.sdc.be.components.impl.ExternalRefsBusinessLogic;
27 import org.openecomp.sdc.be.components.impl.GroupBusinessLogic;
28 import org.openecomp.sdc.be.dao.api.ActionStatus;
29 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
30 import org.openecomp.sdc.be.dto.ExternalRefDTO;
31 import org.openecomp.sdc.be.impl.ComponentsUtils;
32 import org.openecomp.sdc.be.servlets.BeGenericServlet;
33 import org.openecomp.sdc.be.user.UserBusinessLogic;
34 import org.openecomp.sdc.common.datastructure.Wrapper;
35 import org.openecomp.sdc.common.log.wrappers.Logger;
36 import org.springframework.stereotype.Controller;
37
38 import javax.ws.rs.*;
39 import javax.ws.rs.core.MediaType;
40 import javax.ws.rs.core.Response;
41 import java.util.List;
42 import java.util.Map;
43
44 @Path("/v1/catalog")
45 @Controller
46 public class ExternalRefsServlet extends BeGenericServlet {
47
48     private static final Logger log = Logger.getLogger(ExternalRefsServlet.class);
49     private final ExternalRefsBusinessLogic businessLogic;
50
51     @Inject
52     public ExternalRefsServlet(UserBusinessLogic userBusinessLogic,
53         ComponentsUtils componentsUtils,
54         ExternalRefsBusinessLogic externalRefsBusinessLogic) {
55         super(userBusinessLogic, componentsUtils);
56         this.businessLogic = externalRefsBusinessLogic;
57     }
58
59     @GET
60     @Path("/{assetType}/{uuid}/version/{version}/resourceInstances/{componentInstanceName}/externalReferences/{objectType}")
61     @Produces(MediaType.APPLICATION_JSON)
62     public Response getComponentInstanceExternalRef(
63             @PathParam("assetType") String assetType,
64             @PathParam("uuid") String uuid,
65             @PathParam("version") String version,
66             @PathParam("componentInstanceName") String componentInstanceName,
67             @PathParam("objectType") String objectType, @HeaderParam("USER_ID") String userId, @HeaderParam("X-ECOMP-InstanceID") String xEcompInstanceId) {
68
69         log.debug("GET component instance external interfaces {} {} {} {}", assetType, uuid, componentInstanceName, objectType);
70
71         Response r = validateRequest(xEcompInstanceId);
72         if (r != null){
73             return r;
74         }
75
76         Either<List<String>, ActionStatus> refsResult = this.businessLogic.getExternalReferences(uuid, version, componentInstanceName, objectType);
77         if (refsResult.isLeft()){
78             return this.buildOkResponse(refsResult.left().value());
79         } else {
80             return this.buildExtRefErrorResponse(refsResult.right().value(), uuid, version, componentInstanceName, objectType, "");
81         }
82     }
83
84     @GET
85     @Path("/{assetType}/{uuid}/version/{version}/externalReferences/{objectType}")
86     @Produces(MediaType.APPLICATION_JSON)
87     public Map<String, List<String>> getAssetExternalRefByObjectType(
88             @PathParam("assetType") String assetType,
89             @PathParam("uuid") String uuid,
90             @PathParam("version") String version,
91             @PathParam("objectType") String objectType, @HeaderParam("USER_ID") String userId, @HeaderParam("X-ECOMP-InstanceID") String xEcompInstanceId) {
92
93         log.debug("GET asset external references {} {} {}", assetType, uuid, objectType);
94
95         Response r = validateRequest(xEcompInstanceId);
96         if (r != null){
97             throw new WebApplicationException(r);
98         }
99
100         Either<Map<String, List<String>>, ActionStatus> refsResult = this.businessLogic.getExternalReferences(uuid, version, objectType);
101         if (refsResult.isLeft()){
102             return refsResult.left().value();
103         } else {
104             throw new WebApplicationException(this.buildExtRefErrorResponse(refsResult.right().value(), uuid, version, "", objectType, ""));
105         }
106     }
107
108     @POST
109     @Path("/{assetType}/{uuid}/resourceInstances/{componentInstanceName}/externalReferences/{objectType}")
110     @Consumes(MediaType.APPLICATION_JSON)
111     @Produces(MediaType.APPLICATION_JSON)
112     public Response addComponentInstanceExternalRef(
113             @PathParam("assetType") String assetType,
114             @PathParam("uuid") String uuid,
115             @PathParam("componentInstanceName") String componentInstanceName,
116             @PathParam("objectType") String objectType, ExternalRefDTO ref, @HeaderParam("USER_ID") String userId, @HeaderParam("X-ECOMP-InstanceID") String xEcompInstanceId) {
117
118         log.debug("POST component instance external interfaces {} {} {} {} {}", assetType, uuid, componentInstanceName, objectType, ref);
119
120         Response r = validateRequest(xEcompInstanceId);
121         if (r != null){
122             return r;
123         }
124
125         Either<String, ActionStatus> addResult = this.businessLogic.addExternalReference(ComponentTypeEnum.findByParamName(assetType), userId, uuid, componentInstanceName, objectType, ref);
126         if (addResult.isLeft()) {
127             return Response.status(Response.Status.CREATED)
128                     .entity(ref)
129                     .build();
130         } else {
131             return this.buildExtRefErrorResponse(addResult.right().value(), uuid, "", componentInstanceName, objectType, ref.getReferenceUUID());
132         }
133
134     }
135
136     @DELETE
137     @Path("/{assetType}/{uuid}/resourceInstances/{componentInstanceName}/externalReferences/{objectType}/{reference}")
138     @Produces(MediaType.APPLICATION_JSON)
139     public Response deleteComponentInstanceReference(
140             @PathParam("assetType") String assetType,
141             @PathParam("uuid") String uuid,
142             @PathParam("componentInstanceName") String componentInstanceName,
143             @PathParam("objectType") String objectType,
144             @PathParam("reference") String reference, @HeaderParam("USER_ID") String userId, @HeaderParam("X-ECOMP-InstanceID") String xEcompInstanceId) {
145
146         log.debug("DELETE component instance external interfaces {} {} {} {}", assetType, uuid, componentInstanceName, objectType);
147
148         Response r = validateRequest(xEcompInstanceId);
149         if (r != null){
150             return r;
151         }
152
153         Either<String, ActionStatus> deleteStatus = this.businessLogic.deleteExternalReference(ComponentTypeEnum.findByParamName(assetType), userId, uuid, componentInstanceName, objectType, reference);
154         if (deleteStatus.isLeft()){
155             return this.buildOkResponse(new ExternalRefDTO(reference));
156         } else {
157             return this.buildExtRefErrorResponse(deleteStatus.right().value(), uuid, "", componentInstanceName, objectType, reference);
158         }
159     }
160
161     @PUT
162     @Path("/{assetType}/{uuid}/resourceInstances/{componentInstanceName}/externalReferences/{objectType}/{oldRefValue}")
163     @Produces(MediaType.APPLICATION_JSON)
164     @Consumes(MediaType.APPLICATION_JSON)
165     public Response updateComponentInstanceReference(
166             @PathParam("assetType") String assetType,
167             @PathParam("uuid") String uuid,
168             @PathParam("componentInstanceName") String componentInstanceName,
169             @PathParam("objectType") String objectType,
170             @PathParam("oldRefValue") String oldRefValue,
171             ExternalRefDTO newRefValueDTO, @HeaderParam("USER_ID") String userId, @HeaderParam("X-ECOMP-InstanceID") String xEcompInstanceId) {
172
173         log.debug("PUT component instance external interfaces {} {} {} {}", assetType, uuid, componentInstanceName, objectType);
174
175         Response r = validateRequest(xEcompInstanceId);
176         if (r != null){
177             return r;
178         }
179
180         String newRefValue = newRefValueDTO.getReferenceUUID();
181         Either<String, ActionStatus> updateResult = this.businessLogic.updateExternalReference(ComponentTypeEnum.findByParamName(assetType), userId, uuid, 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
190     private Response validateRequest(String xEcompInstanceIdHeader) {
191         Wrapper<Response> responseWrapper = new Wrapper<>();
192
193         //Validate X-ECOMP_INSTANCE_ID_HEADER
194         if (xEcompInstanceIdHeader == null || xEcompInstanceIdHeader.isEmpty()){
195             return this.buildExtRefErrorResponse(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID, "", "", "", "", "");
196         }
197
198         return responseWrapper.getInnerElement();
199     }
200
201     private Response buildExtRefErrorResponse(ActionStatus status, String uuid, String version, String componentInstanceName, String objectType, String ref){
202         switch (status) {
203             case RESOURCE_NOT_FOUND:
204                 return buildErrorResponse(componentsUtils.getResponseFormat(status, uuid));
205             case COMPONENT_VERSION_NOT_FOUND:
206                 return buildErrorResponse(componentsUtils.getResponseFormat(status, uuid, version));
207             case COMPONENT_INSTANCE_NOT_FOUND:
208                 return buildErrorResponse(componentsUtils.getResponseFormat(status, componentInstanceName, uuid));
209             case EXT_REF_ALREADY_EXIST:
210                 return Response.status(Response.Status.OK)
211                         .entity(new ExternalRefDTO(ref))
212                         .build();
213             case EXT_REF_NOT_FOUND:
214                 return buildErrorResponse(componentsUtils.getResponseFormat(status, objectType + "/" + ref));
215             case MISSING_X_ECOMP_INSTANCE_ID:
216                 return buildErrorResponse(componentsUtils.getResponseFormat(status));
217             default:
218                 return this.buildGeneralErrorResponse();
219         }
220     }
221 }