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