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 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;
39 import javax.ws.rs.core.MediaType;
40 import javax.ws.rs.core.Response;
41 import java.util.List;
46 public class ExternalRefsServlet extends BeGenericServlet {
48 private static final Logger log = Logger.getLogger(ExternalRefsServlet.class);
49 private final ExternalRefsBusinessLogic businessLogic;
52 public ExternalRefsServlet(UserBusinessLogic userBusinessLogic,
53 ComponentsUtils componentsUtils,
54 ExternalRefsBusinessLogic externalRefsBusinessLogic) {
55 super(userBusinessLogic, componentsUtils);
56 this.businessLogic = externalRefsBusinessLogic;
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) {
69 log.debug("GET component instance external interfaces {} {} {} {}", assetType, uuid, componentInstanceName, objectType);
71 Response r = validateRequest(xEcompInstanceId);
76 Either<List<String>, ActionStatus> refsResult = this.businessLogic.getExternalReferences(uuid, version, componentInstanceName, objectType);
77 if (refsResult.isLeft()){
78 return this.buildOkResponse(refsResult.left().value());
80 return this.buildExtRefErrorResponse(refsResult.right().value(), uuid, version, componentInstanceName, objectType, "");
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) {
93 log.debug("GET asset external references {} {} {}", assetType, uuid, objectType);
95 Response r = validateRequest(xEcompInstanceId);
97 throw new WebApplicationException(r);
100 Either<Map<String, List<String>>, ActionStatus> refsResult = this.businessLogic.getExternalReferences(uuid, version, objectType);
101 if (refsResult.isLeft()){
102 return refsResult.left().value();
104 throw new WebApplicationException(this.buildExtRefErrorResponse(refsResult.right().value(), uuid, version, "", objectType, ""));
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) {
118 log.debug("POST component instance external interfaces {} {} {} {} {}", assetType, uuid, componentInstanceName, objectType, ref);
120 Response r = validateRequest(xEcompInstanceId);
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)
131 return this.buildExtRefErrorResponse(addResult.right().value(), uuid, "", componentInstanceName, objectType, ref.getReferenceUUID());
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) {
146 log.debug("DELETE component instance external interfaces {} {} {} {}", assetType, uuid, componentInstanceName, objectType);
148 Response r = validateRequest(xEcompInstanceId);
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));
157 return this.buildExtRefErrorResponse(deleteStatus.right().value(), uuid, "", componentInstanceName, objectType, reference);
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) {
173 log.debug("PUT component instance external interfaces {} {} {} {}", assetType, uuid, componentInstanceName, objectType);
175 Response r = validateRequest(xEcompInstanceId);
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));
185 return this.buildExtRefErrorResponse(updateResult.right().value(), uuid, "", componentInstanceName, objectType, oldRefValue);
190 private Response validateRequest(String xEcompInstanceIdHeader) {
191 Wrapper<Response> responseWrapper = new Wrapper<>();
193 //Validate X-ECOMP_INSTANCE_ID_HEADER
194 if (xEcompInstanceIdHeader == null || xEcompInstanceIdHeader.isEmpty()){
195 return this.buildExtRefErrorResponse(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID, "", "", "", "", "");
198 return responseWrapper.getInnerElement();
201 private Response buildExtRefErrorResponse(ActionStatus status, String uuid, String version, String componentInstanceName, String objectType, String ref){
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))
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));
218 return this.buildGeneralErrorResponse();