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.components.impl.aaf.AafPermission;
42 import org.openecomp.sdc.be.components.impl.aaf.PermissionAllowed;
43 import org.openecomp.sdc.be.dao.api.ActionStatus;
44 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
45 import org.openecomp.sdc.be.dto.ExternalRefDTO;
46 import org.openecomp.sdc.be.impl.ComponentsUtils;
47 import org.openecomp.sdc.be.servlets.BeGenericServlet;
48 import org.openecomp.sdc.be.user.UserBusinessLogic;
49 import org.openecomp.sdc.common.datastructure.Wrapper;
50 import org.openecomp.sdc.common.log.wrappers.Logger;
51 import org.springframework.stereotype.Controller;
54 @Tag(name = "SDCE-7 APIs")
57 public class ExternalRefsServlet extends BeGenericServlet {
59 private static final Logger log = Logger.getLogger(ExternalRefsServlet.class);
60 private final ExternalRefsBusinessLogic businessLogic;
63 public ExternalRefsServlet(UserBusinessLogic userBusinessLogic, ComponentsUtils componentsUtils,
64 ExternalRefsBusinessLogic externalRefsBusinessLogic) {
65 super(userBusinessLogic, componentsUtils);
66 this.businessLogic = externalRefsBusinessLogic;
70 @Path("/{assetType}/{uuid}/version/{version}/resourceInstances/{componentInstanceName}/externalReferences/{objectType}")
71 @Produces(MediaType.APPLICATION_JSON)
72 @PermissionAllowed({AafPermission.PermNames.READ_VALUE})
73 public Response getComponentInstanceExternalRef(@PathParam("assetType") String assetType, @PathParam("uuid") String uuid,
74 @PathParam("version") String version,
75 @PathParam("componentInstanceName") String componentInstanceName,
76 @PathParam("objectType") String objectType, @HeaderParam("USER_ID") String userId,
77 @HeaderParam("X-ECOMP-InstanceID") String xEcompInstanceId) {
78 log.debug("GET component instance external interfaces {} {} {} {}", assetType, uuid, componentInstanceName, objectType);
79 Response r = validateRequest(xEcompInstanceId);
83 Either<List<String>, ActionStatus> refsResult = this.businessLogic.getExternalReferences(uuid, version, componentInstanceName, objectType);
84 if (refsResult.isLeft()) {
85 return this.buildOkResponse(refsResult.left().value());
87 return this.buildExtRefErrorResponse(refsResult.right().value(), uuid, version, componentInstanceName, objectType, "");
92 @Path("/{assetType}/{uuid}/version/{version}/externalReferences/{objectType}")
93 @Produces(MediaType.APPLICATION_JSON)
94 @PermissionAllowed({AafPermission.PermNames.READ_VALUE})
95 public Map<String, List<String>> getAssetExternalRefByObjectType(@PathParam("assetType") String assetType, @PathParam("uuid") String uuid,
96 @PathParam("version") String version, @PathParam("objectType") String objectType,
97 @HeaderParam("USER_ID") String userId,
98 @HeaderParam("X-ECOMP-InstanceID") String xEcompInstanceId) {
99 log.debug("GET asset external references {} {} {}", assetType, uuid, objectType);
100 Response r = validateRequest(xEcompInstanceId);
102 throw new WebApplicationException(r);
104 Either<Map<String, List<String>>, ActionStatus> refsResult = this.businessLogic.getExternalReferences(uuid, version, objectType);
105 if (refsResult.isLeft()) {
106 return refsResult.left().value();
108 throw new WebApplicationException(this.buildExtRefErrorResponse(refsResult.right().value(), uuid, version, "", objectType, ""));
113 @Path("/{assetType}/{uuid}/resourceInstances/{componentInstanceName}/externalReferences/{objectType}")
114 @Consumes(MediaType.APPLICATION_JSON)
115 @Produces(MediaType.APPLICATION_JSON)
116 @PermissionAllowed({AafPermission.PermNames.WRITE_VALUE})
117 public Response addComponentInstanceExternalRef(@PathParam("assetType") String assetType, @PathParam("uuid") String uuid,
118 @PathParam("componentInstanceName") String componentInstanceName,
119 @PathParam("objectType") String objectType, ExternalRefDTO ref,
120 @HeaderParam("USER_ID") String userId,
121 @HeaderParam("X-ECOMP-InstanceID") String xEcompInstanceId) {
122 log.debug("POST component instance external interfaces {} {} {} {} {}", assetType, uuid, componentInstanceName, objectType, ref);
123 Response r = validateRequest(xEcompInstanceId);
127 ComponentTypeEnum componentType = ComponentTypeEnum.findByParamName(assetType);
128 String uid = this.businessLogic.fetchComponentUniqueIdByUuid(uuid, componentType);
129 Either<String, ActionStatus> addResult = this.businessLogic
130 .addExternalReference(uid, componentType, userId, componentInstanceName, objectType, ref);
131 if (addResult.isLeft()) {
132 return Response.status(Response.Status.CREATED).entity(ref).build();
134 return this.buildExtRefErrorResponse(addResult.right().value(), uuid, "", componentInstanceName, objectType, ref.getReferenceUUID());
139 @Path("/{assetType}/{uuid}/resourceInstances/{componentInstanceName}/externalReferences/{objectType}/{reference}")
140 @Produces(MediaType.APPLICATION_JSON)
141 @PermissionAllowed({AafPermission.PermNames.DELETE_VALUE})
142 public Response deleteComponentInstanceReference(@PathParam("assetType") String assetType, @PathParam("uuid") String uuid,
143 @PathParam("componentInstanceName") String componentInstanceName,
144 @PathParam("objectType") String objectType, @PathParam("reference") String reference,
145 @HeaderParam("USER_ID") String userId,
146 @HeaderParam("X-ECOMP-InstanceID") String xEcompInstanceId) {
147 log.debug("DELETE component instance external interfaces {} {} {} {}", assetType, uuid, componentInstanceName, objectType);
148 Response r = validateRequest(xEcompInstanceId);
152 ComponentTypeEnum componentType = ComponentTypeEnum.findByParamName(assetType);
153 String uid = this.businessLogic.fetchComponentUniqueIdByUuid(uuid, componentType);
154 Either<String, ActionStatus> deleteStatus = this.businessLogic
155 .deleteExternalReference(uid, componentType, userId, componentInstanceName, objectType, reference);
156 if (deleteStatus.isLeft()) {
157 return this.buildOkResponse(new ExternalRefDTO(reference));
159 return this.buildExtRefErrorResponse(deleteStatus.right().value(), uuid, "", componentInstanceName, objectType, reference);
164 @Path("/{assetType}/{uuid}/resourceInstances/{componentInstanceName}/externalReferences/{objectType}/{oldRefValue}")
165 @Produces(MediaType.APPLICATION_JSON)
166 @Consumes(MediaType.APPLICATION_JSON)
167 @PermissionAllowed({AafPermission.PermNames.WRITE_VALUE})
168 public Response updateComponentInstanceReference(@PathParam("assetType") String assetType, @PathParam("uuid") String uuid,
169 @PathParam("componentInstanceName") String componentInstanceName,
170 @PathParam("objectType") String objectType, @PathParam("oldRefValue") String oldRefValue,
171 ExternalRefDTO newRefValueDTO, @HeaderParam("USER_ID") String userId,
172 @HeaderParam("X-ECOMP-InstanceID") String xEcompInstanceId) {
173 log.debug("PUT component instance external interfaces {} {} {} {}", assetType, uuid, componentInstanceName, objectType);
174 Response r = validateRequest(xEcompInstanceId);
178 String newRefValue = newRefValueDTO.getReferenceUUID();
179 ComponentTypeEnum componentType = ComponentTypeEnum.findByParamName(assetType);
180 String uid = this.businessLogic.fetchComponentUniqueIdByUuid(uuid, componentType);
181 Either<String, ActionStatus> updateResult = this.businessLogic
182 .updateExternalReference(uid, componentType, userId, componentInstanceName, objectType, oldRefValue, newRefValue);
183 if (updateResult.isLeft()) {
184 return this.buildOkResponse(new ExternalRefDTO(newRefValue));
186 return this.buildExtRefErrorResponse(updateResult.right().value(), uuid, "", componentInstanceName, objectType, oldRefValue);
190 private Response validateRequest(String xEcompInstanceIdHeader) {
191 Wrapper<Response> responseWrapper = new Wrapper<>();
192 //Validate X-ECOMP_INSTANCE_ID_HEADER
193 if (xEcompInstanceIdHeader == null || xEcompInstanceIdHeader.isEmpty()) {
194 return this.buildExtRefErrorResponse(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID, "", "", "", "", "");
196 return responseWrapper.getInnerElement();
199 private Response buildExtRefErrorResponse(ActionStatus status, String uuid, String version, String componentInstanceName, String objectType,
202 case RESOURCE_NOT_FOUND:
203 return buildErrorResponse(componentsUtils.getResponseFormat(status, uuid));
204 case COMPONENT_VERSION_NOT_FOUND:
205 return buildErrorResponse(componentsUtils.getResponseFormat(status, uuid, version));
206 case COMPONENT_INSTANCE_NOT_FOUND:
207 return buildErrorResponse(componentsUtils.getResponseFormat(status, componentInstanceName, uuid));
208 case EXT_REF_ALREADY_EXIST:
209 return Response.status(Response.Status.OK).entity(new ExternalRefDTO(ref)).build();
210 case EXT_REF_NOT_FOUND:
211 return buildErrorResponse(componentsUtils.getResponseFormat(status, objectType + "/" + ref));
212 case MISSING_X_ECOMP_INSTANCE_ID:
213 return buildErrorResponse(componentsUtils.getResponseFormat(status));
215 return this.buildGeneralErrorResponse();