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