2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.externalapi.servlet;
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;
35 import javax.ws.rs.core.MediaType;
36 import javax.ws.rs.core.Response;
37 import java.util.List;
42 public class ExternalRefsServlet extends BeGenericServlet {
44 private static final Logger log = Logger.getLogger(ExternalRefsServlet.class);
45 private final ComponentsUtils componentsUtils;
46 private final ExternalRefsBusinessLogic businessLogic;
48 public ExternalRefsServlet(ExternalRefsBusinessLogic businessLogic, ComponentsUtils componentsUtils){
49 this.businessLogic = businessLogic;
50 this.componentsUtils = componentsUtils;
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) {
63 log.debug("GET component instance external interfaces {} {} {} {}", assetType, uuid, componentInstanceName, objectType);
65 Response r = validateRequest(xEcompInstanceId);
70 Either<List<String>, ActionStatus> refsResult = this.businessLogic.getExternalReferences(uuid, version, componentInstanceName, objectType);
71 if (refsResult.isLeft()){
72 return this.buildOkResponse(refsResult.left().value());
74 return this.buildExtRefErrorResponse(refsResult.right().value(), uuid, version, componentInstanceName, objectType, "");
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) {
87 log.debug("GET asset external references {} {} {}", assetType, uuid, objectType);
89 Response r = validateRequest(xEcompInstanceId);
91 throw new WebApplicationException(r);
94 Either<Map<String, List<String>>, ActionStatus> refsResult = this.businessLogic.getExternalReferences(uuid, version, objectType);
95 if (refsResult.isLeft()){
96 return refsResult.left().value();
98 throw new WebApplicationException(this.buildExtRefErrorResponse(refsResult.right().value(), uuid, version, "", objectType, ""));
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) {
112 log.debug("POST component instance external interfaces {} {} {} {} {}", assetType, uuid, componentInstanceName, objectType, ref);
114 Response r = validateRequest(xEcompInstanceId);
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)
125 return this.buildExtRefErrorResponse(addResult.right().value(), uuid, "", componentInstanceName, objectType, ref.getReferenceUUID());
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) {
140 log.debug("DELETE component instance external interfaces {} {} {} {}", assetType, uuid, componentInstanceName, objectType);
142 Response r = validateRequest(xEcompInstanceId);
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));
151 return this.buildExtRefErrorResponse(deleteStatus.right().value(), uuid, "", componentInstanceName, objectType, reference);
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) {
167 log.debug("PUT component instance external interfaces {} {} {} {}", assetType, uuid, componentInstanceName, objectType);
169 Response r = validateRequest(xEcompInstanceId);
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));
179 return this.buildExtRefErrorResponse(updateResult.right().value(), uuid, "", componentInstanceName, objectType, oldRefValue);
184 private Response validateRequest(String xEcompInstanceIdHeader) {
185 Wrapper<Response> responseWrapper = new Wrapper<>();
187 //Validate X-ECOMP_INSTANCE_ID_HEADER
188 if (xEcompInstanceIdHeader == null || xEcompInstanceIdHeader.isEmpty()){
189 return this.buildExtRefErrorResponse(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID, "", "", "", "", "");
192 return responseWrapper.getInnerElement();
195 private Response buildExtRefErrorResponse(ActionStatus status, String uuid, String version, String componentInstanceName, String objectType, String ref){
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))
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));
212 return this.buildGeneralErrorResponse();