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