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=========================================================
20 package org.openecomp.sdc.be.externalapi.servlet;
22 import fj.data.Either;
23 import io.swagger.v3.oas.annotations.servers.Server;
24 import io.swagger.v3.oas.annotations.tags.Tag;
25 import java.util.List;
27 import javax.inject.Inject;
28 import javax.ws.rs.Consumes;
29 import javax.ws.rs.DELETE;
30 import javax.ws.rs.GET;
31 import javax.ws.rs.HeaderParam;
32 import javax.ws.rs.POST;
33 import javax.ws.rs.PUT;
34 import javax.ws.rs.Path;
35 import javax.ws.rs.PathParam;
36 import javax.ws.rs.Produces;
37 import javax.ws.rs.WebApplicationException;
38 import javax.ws.rs.core.MediaType;
39 import javax.ws.rs.core.Response;
40 import org.openecomp.sdc.be.components.impl.ExternalRefsBusinessLogic;
41 import org.openecomp.sdc.be.dao.api.ActionStatus;
42 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
43 import org.openecomp.sdc.be.dto.ExternalRefDTO;
44 import org.openecomp.sdc.be.impl.ComponentsUtils;
45 import org.openecomp.sdc.be.servlets.BeGenericServlet;
46 import org.openecomp.sdc.common.datastructure.Wrapper;
47 import org.openecomp.sdc.common.log.wrappers.Logger;
48 import org.springframework.stereotype.Controller;
51 @Tag(name = "SDCE-7 APIs")
54 public class ExternalRefsServlet extends BeGenericServlet {
56 private static final Logger log = Logger.getLogger(ExternalRefsServlet.class);
57 private final ExternalRefsBusinessLogic businessLogic;
60 public ExternalRefsServlet(ComponentsUtils componentsUtils,
61 ExternalRefsBusinessLogic externalRefsBusinessLogic) {
62 super(componentsUtils);
63 this.businessLogic = externalRefsBusinessLogic;
67 @Path("/{assetType}/{uuid}/version/{version}/resourceInstances/{componentInstanceName}/externalReferences/{objectType}")
68 @Produces(MediaType.APPLICATION_JSON)
69 public Response getComponentInstanceExternalRef(@PathParam("assetType") String assetType, @PathParam("uuid") String uuid,
70 @PathParam("version") String version,
71 @PathParam("componentInstanceName") String componentInstanceName,
72 @PathParam("objectType") String objectType, @HeaderParam("USER_ID") String userId,
73 @HeaderParam("X-ECOMP-InstanceID") String xEcompInstanceId) {
74 log.debug("GET component instance external interfaces {} {} {} {}", assetType, uuid, componentInstanceName, objectType);
75 Response r = validateRequest(xEcompInstanceId);
79 Either<List<String>, ActionStatus> refsResult = this.businessLogic.getExternalReferences(uuid, version, componentInstanceName, objectType);
80 if (refsResult.isLeft()) {
81 return this.buildOkResponse(refsResult.left().value());
83 return this.buildExtRefErrorResponse(refsResult.right().value(), uuid, version, componentInstanceName, objectType, "");
88 @Path("/{assetType}/{uuid}/version/{version}/externalReferences/{objectType}")
89 @Produces(MediaType.APPLICATION_JSON)
90 public Map<String, List<String>> getAssetExternalRefByObjectType(@PathParam("assetType") String assetType, @PathParam("uuid") String uuid,
91 @PathParam("version") String version, @PathParam("objectType") String objectType,
92 @HeaderParam("USER_ID") String userId,
93 @HeaderParam("X-ECOMP-InstanceID") String xEcompInstanceId) {
94 log.debug("GET asset external references {} {} {}", assetType, uuid, objectType);
95 Response r = validateRequest(xEcompInstanceId);
97 throw new WebApplicationException(r);
99 Either<Map<String, List<String>>, ActionStatus> refsResult = this.businessLogic.getExternalReferences(uuid, version, objectType);
100 if (refsResult.isLeft()) {
101 return refsResult.left().value();
103 throw new WebApplicationException(this.buildExtRefErrorResponse(refsResult.right().value(), uuid, version, "", objectType, ""));
108 @Path("/{assetType}/{uuid}/resourceInstances/{componentInstanceName}/externalReferences/{objectType}")
109 @Consumes(MediaType.APPLICATION_JSON)
110 @Produces(MediaType.APPLICATION_JSON)
111 public Response addComponentInstanceExternalRef(@PathParam("assetType") String assetType, @PathParam("uuid") String uuid,
112 @PathParam("componentInstanceName") String componentInstanceName,
113 @PathParam("objectType") String objectType, ExternalRefDTO ref,
114 @HeaderParam("USER_ID") String userId,
115 @HeaderParam("X-ECOMP-InstanceID") String xEcompInstanceId) {
116 log.debug("POST component instance external interfaces {} {} {} {} {}", assetType, uuid, componentInstanceName, objectType, ref);
117 Response r = validateRequest(xEcompInstanceId);
121 ComponentTypeEnum componentType = ComponentTypeEnum.findByParamName(assetType);
122 String uid = this.businessLogic.fetchComponentUniqueIdByUuid(uuid, componentType);
123 Either<String, ActionStatus> addResult = this.businessLogic
124 .addExternalReference(uid, componentType, userId, componentInstanceName, objectType, ref);
125 if (addResult.isLeft()) {
126 return Response.status(Response.Status.CREATED).entity(ref).build();
128 return this.buildExtRefErrorResponse(addResult.right().value(), uuid, "", componentInstanceName, objectType, ref.getReferenceUUID());
133 @Path("/{assetType}/{uuid}/resourceInstances/{componentInstanceName}/externalReferences/{objectType}/{reference}")
134 @Produces(MediaType.APPLICATION_JSON)
135 public Response deleteComponentInstanceReference(@PathParam("assetType") String assetType, @PathParam("uuid") String uuid,
136 @PathParam("componentInstanceName") String componentInstanceName,
137 @PathParam("objectType") String objectType, @PathParam("reference") String reference,
138 @HeaderParam("USER_ID") String userId,
139 @HeaderParam("X-ECOMP-InstanceID") String xEcompInstanceId) {
140 log.debug("DELETE component instance external interfaces {} {} {} {}", assetType, uuid, componentInstanceName, objectType);
141 Response r = validateRequest(xEcompInstanceId);
145 ComponentTypeEnum componentType = ComponentTypeEnum.findByParamName(assetType);
146 String uid = this.businessLogic.fetchComponentUniqueIdByUuid(uuid, componentType);
147 Either<String, ActionStatus> deleteStatus = this.businessLogic
148 .deleteExternalReference(uid, componentType, userId, componentInstanceName, objectType, reference);
149 if (deleteStatus.isLeft()) {
150 return this.buildOkResponse(new ExternalRefDTO(reference));
152 return this.buildExtRefErrorResponse(deleteStatus.right().value(), uuid, "", componentInstanceName, objectType, reference);
157 @Path("/{assetType}/{uuid}/resourceInstances/{componentInstanceName}/externalReferences/{objectType}/{oldRefValue}")
158 @Produces(MediaType.APPLICATION_JSON)
159 @Consumes(MediaType.APPLICATION_JSON)
160 public Response updateComponentInstanceReference(@PathParam("assetType") String assetType, @PathParam("uuid") String uuid,
161 @PathParam("componentInstanceName") String componentInstanceName,
162 @PathParam("objectType") String objectType, @PathParam("oldRefValue") String oldRefValue,
163 ExternalRefDTO newRefValueDTO, @HeaderParam("USER_ID") String userId,
164 @HeaderParam("X-ECOMP-InstanceID") String xEcompInstanceId) {
165 log.debug("PUT component instance external interfaces {} {} {} {}", assetType, uuid, componentInstanceName, objectType);
166 Response r = validateRequest(xEcompInstanceId);
170 String newRefValue = newRefValueDTO.getReferenceUUID();
171 ComponentTypeEnum componentType = ComponentTypeEnum.findByParamName(assetType);
172 String uid = this.businessLogic.fetchComponentUniqueIdByUuid(uuid, componentType);
173 Either<String, ActionStatus> updateResult = this.businessLogic
174 .updateExternalReference(uid, componentType, userId, componentInstanceName, objectType, oldRefValue, newRefValue);
175 if (updateResult.isLeft()) {
176 return this.buildOkResponse(new ExternalRefDTO(newRefValue));
178 return this.buildExtRefErrorResponse(updateResult.right().value(), uuid, "", componentInstanceName, objectType, oldRefValue);
182 private Response validateRequest(String xEcompInstanceIdHeader) {
183 Wrapper<Response> responseWrapper = new Wrapper<>();
184 //Validate X-ECOMP_INSTANCE_ID_HEADER
185 if (xEcompInstanceIdHeader == null || xEcompInstanceIdHeader.isEmpty()) {
186 return this.buildExtRefErrorResponse(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID, "", "", "", "", "");
188 return responseWrapper.getInnerElement();
191 private Response buildExtRefErrorResponse(ActionStatus status, String uuid, String version, String componentInstanceName, String objectType,
194 case RESOURCE_NOT_FOUND:
195 return buildErrorResponse(componentsUtils.getResponseFormat(status, uuid));
196 case COMPONENT_VERSION_NOT_FOUND:
197 return buildErrorResponse(componentsUtils.getResponseFormat(status, uuid, version));
198 case COMPONENT_INSTANCE_NOT_FOUND:
199 return buildErrorResponse(componentsUtils.getResponseFormat(status, componentInstanceName, uuid));
200 case EXT_REF_ALREADY_EXIST:
201 return Response.status(Response.Status.OK).entity(new ExternalRefDTO(ref)).build();
202 case EXT_REF_NOT_FOUND:
203 return buildErrorResponse(componentsUtils.getResponseFormat(status, objectType + "/" + ref));
204 case MISSING_X_ECOMP_INSTANCE_ID:
205 return buildErrorResponse(componentsUtils.getResponseFormat(status));
207 return this.buildGeneralErrorResponse();