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.servers.Servers;
25 import io.swagger.v3.oas.annotations.tags.Tag;
26 import io.swagger.v3.oas.annotations.tags.Tags;
27 import java.util.List;
29 import javax.inject.Inject;
30 import javax.ws.rs.Consumes;
31 import javax.ws.rs.DELETE;
32 import javax.ws.rs.GET;
33 import javax.ws.rs.HeaderParam;
34 import javax.ws.rs.POST;
35 import javax.ws.rs.PUT;
36 import javax.ws.rs.Path;
37 import javax.ws.rs.PathParam;
38 import javax.ws.rs.Produces;
39 import javax.ws.rs.WebApplicationException;
40 import javax.ws.rs.core.MediaType;
41 import javax.ws.rs.core.Response;
42 import org.openecomp.sdc.be.components.impl.ExternalRefsBusinessLogic;
43 import org.openecomp.sdc.be.components.impl.aaf.AafPermission;
44 import org.openecomp.sdc.be.components.impl.aaf.PermissionAllowed;
45 import org.openecomp.sdc.be.dao.api.ActionStatus;
46 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
47 import org.openecomp.sdc.be.dto.ExternalRefDTO;
48 import org.openecomp.sdc.be.impl.ComponentsUtils;
49 import org.openecomp.sdc.be.servlets.BeGenericServlet;
50 import org.openecomp.sdc.be.user.UserBusinessLogic;
51 import org.openecomp.sdc.common.datastructure.Wrapper;
52 import org.openecomp.sdc.common.log.wrappers.Logger;
53 import org.springframework.stereotype.Controller;
56 @Tags({@Tag(name = "SDC External APIs")})
57 @Servers({@Server(url = "/sdc")})
59 public class ExternalRefsServlet extends BeGenericServlet {
61 private static final Logger log = Logger.getLogger(ExternalRefsServlet.class);
62 private final ExternalRefsBusinessLogic businessLogic;
65 public ExternalRefsServlet(UserBusinessLogic userBusinessLogic, ComponentsUtils componentsUtils,
66 ExternalRefsBusinessLogic externalRefsBusinessLogic) {
67 super(userBusinessLogic, componentsUtils);
68 this.businessLogic = externalRefsBusinessLogic;
72 @Path("/{assetType}/{uuid}/version/{version}/resourceInstances/{componentInstanceName}/externalReferences/{objectType}")
73 @Produces(MediaType.APPLICATION_JSON)
74 @PermissionAllowed({AafPermission.PermNames.READ_VALUE})
75 public Response getComponentInstanceExternalRef(@PathParam("assetType") String assetType, @PathParam("uuid") String uuid,
76 @PathParam("version") String version,
77 @PathParam("componentInstanceName") String componentInstanceName,
78 @PathParam("objectType") String objectType, @HeaderParam("USER_ID") String userId,
79 @HeaderParam("X-ECOMP-InstanceID") String xEcompInstanceId) {
80 log.debug("GET component instance external interfaces {} {} {} {}", assetType, uuid, componentInstanceName, objectType);
81 Response r = validateRequest(xEcompInstanceId);
85 Either<List<String>, ActionStatus> refsResult = this.businessLogic.getExternalReferences(uuid, version, componentInstanceName, objectType);
86 if (refsResult.isLeft()) {
87 return this.buildOkResponse(refsResult.left().value());
89 return this.buildExtRefErrorResponse(refsResult.right().value(), uuid, version, componentInstanceName, objectType, "");
94 @Path("/{assetType}/{uuid}/version/{version}/externalReferences/{objectType}")
95 @Produces(MediaType.APPLICATION_JSON)
96 @PermissionAllowed({AafPermission.PermNames.READ_VALUE})
97 public Map<String, List<String>> getAssetExternalRefByObjectType(@PathParam("assetType") String assetType, @PathParam("uuid") String uuid,
98 @PathParam("version") String version, @PathParam("objectType") String objectType,
99 @HeaderParam("USER_ID") String userId,
100 @HeaderParam("X-ECOMP-InstanceID") String xEcompInstanceId) {
101 log.debug("GET asset external references {} {} {}", assetType, uuid, objectType);
102 Response r = validateRequest(xEcompInstanceId);
104 throw new WebApplicationException(r);
106 Either<Map<String, List<String>>, ActionStatus> refsResult = this.businessLogic.getExternalReferences(uuid, version, objectType);
107 if (refsResult.isLeft()) {
108 return refsResult.left().value();
110 throw new WebApplicationException(this.buildExtRefErrorResponse(refsResult.right().value(), uuid, version, "", objectType, ""));
115 @Path("/{assetType}/{uuid}/resourceInstances/{componentInstanceName}/externalReferences/{objectType}")
116 @Consumes(MediaType.APPLICATION_JSON)
117 @Produces(MediaType.APPLICATION_JSON)
118 @PermissionAllowed({AafPermission.PermNames.WRITE_VALUE})
119 public Response addComponentInstanceExternalRef(@PathParam("assetType") String assetType, @PathParam("uuid") String uuid,
120 @PathParam("componentInstanceName") String componentInstanceName,
121 @PathParam("objectType") String objectType, ExternalRefDTO ref,
122 @HeaderParam("USER_ID") String userId,
123 @HeaderParam("X-ECOMP-InstanceID") String xEcompInstanceId) {
124 log.debug("POST component instance external interfaces {} {} {} {} {}", assetType, uuid, componentInstanceName, objectType, ref);
125 Response r = validateRequest(xEcompInstanceId);
129 ComponentTypeEnum componentType = ComponentTypeEnum.findByParamName(assetType);
130 String uid = this.businessLogic.fetchComponentUniqueIdByUuid(uuid, componentType);
131 Either<String, ActionStatus> addResult = this.businessLogic
132 .addExternalReference(uid, componentType, userId, componentInstanceName, objectType, ref);
133 if (addResult.isLeft()) {
134 return Response.status(Response.Status.CREATED).entity(ref).build();
136 return this.buildExtRefErrorResponse(addResult.right().value(), uuid, "", componentInstanceName, objectType, ref.getReferenceUUID());
141 @Path("/{assetType}/{uuid}/resourceInstances/{componentInstanceName}/externalReferences/{objectType}/{reference}")
142 @Produces(MediaType.APPLICATION_JSON)
143 @PermissionAllowed({AafPermission.PermNames.DELETE_VALUE})
144 public Response deleteComponentInstanceReference(@PathParam("assetType") String assetType, @PathParam("uuid") String uuid,
145 @PathParam("componentInstanceName") String componentInstanceName,
146 @PathParam("objectType") String objectType, @PathParam("reference") String reference,
147 @HeaderParam("USER_ID") String userId,
148 @HeaderParam("X-ECOMP-InstanceID") String xEcompInstanceId) {
149 log.debug("DELETE component instance external interfaces {} {} {} {}", assetType, uuid, componentInstanceName, objectType);
150 Response r = validateRequest(xEcompInstanceId);
154 ComponentTypeEnum componentType = ComponentTypeEnum.findByParamName(assetType);
155 String uid = this.businessLogic.fetchComponentUniqueIdByUuid(uuid, componentType);
156 Either<String, ActionStatus> deleteStatus = this.businessLogic
157 .deleteExternalReference(uid, componentType, userId, componentInstanceName, objectType, reference);
158 if (deleteStatus.isLeft()) {
159 return this.buildOkResponse(new ExternalRefDTO(reference));
161 return this.buildExtRefErrorResponse(deleteStatus.right().value(), uuid, "", componentInstanceName, objectType, reference);
166 @Path("/{assetType}/{uuid}/resourceInstances/{componentInstanceName}/externalReferences/{objectType}/{oldRefValue}")
167 @Produces(MediaType.APPLICATION_JSON)
168 @Consumes(MediaType.APPLICATION_JSON)
169 @PermissionAllowed({AafPermission.PermNames.WRITE_VALUE})
170 public Response updateComponentInstanceReference(@PathParam("assetType") String assetType, @PathParam("uuid") String uuid,
171 @PathParam("componentInstanceName") String componentInstanceName,
172 @PathParam("objectType") String objectType, @PathParam("oldRefValue") String oldRefValue,
173 ExternalRefDTO newRefValueDTO, @HeaderParam("USER_ID") String userId,
174 @HeaderParam("X-ECOMP-InstanceID") String xEcompInstanceId) {
175 log.debug("PUT component instance external interfaces {} {} {} {}", assetType, uuid, componentInstanceName, objectType);
176 Response r = validateRequest(xEcompInstanceId);
180 String newRefValue = newRefValueDTO.getReferenceUUID();
181 ComponentTypeEnum componentType = ComponentTypeEnum.findByParamName(assetType);
182 String uid = this.businessLogic.fetchComponentUniqueIdByUuid(uuid, componentType);
183 Either<String, ActionStatus> updateResult = this.businessLogic
184 .updateExternalReference(uid, componentType, userId, componentInstanceName, objectType, oldRefValue, newRefValue);
185 if (updateResult.isLeft()) {
186 return this.buildOkResponse(new ExternalRefDTO(newRefValue));
188 return this.buildExtRefErrorResponse(updateResult.right().value(), uuid, "", componentInstanceName, objectType, oldRefValue);
192 private Response validateRequest(String xEcompInstanceIdHeader) {
193 Wrapper<Response> responseWrapper = new Wrapper<>();
194 //Validate X-ECOMP_INSTANCE_ID_HEADER
195 if (xEcompInstanceIdHeader == null || xEcompInstanceIdHeader.isEmpty()) {
196 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,
204 case RESOURCE_NOT_FOUND:
205 return buildErrorResponse(componentsUtils.getResponseFormat(status, uuid));
206 case COMPONENT_VERSION_NOT_FOUND:
207 return buildErrorResponse(componentsUtils.getResponseFormat(status, uuid, version));
208 case COMPONENT_INSTANCE_NOT_FOUND:
209 return buildErrorResponse(componentsUtils.getResponseFormat(status, componentInstanceName, uuid));
210 case EXT_REF_ALREADY_EXIST:
211 return Response.status(Response.Status.OK).entity(new ExternalRefDTO(ref)).build();
212 case EXT_REF_NOT_FOUND:
213 return buildErrorResponse(componentsUtils.getResponseFormat(status, objectType + "/" + ref));
214 case MISSING_X_ECOMP_INSTANCE_ID:
215 return buildErrorResponse(componentsUtils.getResponseFormat(status));
217 return this.buildGeneralErrorResponse();