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 io.swagger.v3.oas.annotations.servers.Server;
25 import io.swagger.v3.oas.annotations.servers.Servers;
26 import io.swagger.v3.oas.annotations.tags.Tag;
27 import io.swagger.v3.oas.annotations.tags.Tags;
28 import org.openecomp.sdc.be.components.impl.ExternalRefsBusinessLogic;
29 import org.openecomp.sdc.be.components.impl.aaf.AafPermission;
30 import org.openecomp.sdc.be.components.impl.aaf.PermissionAllowed;
31 import org.openecomp.sdc.be.dao.api.ActionStatus;
32 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
33 import org.openecomp.sdc.be.dto.ExternalRefDTO;
34 import org.openecomp.sdc.be.impl.ComponentsUtils;
35 import org.openecomp.sdc.be.servlets.BeGenericServlet;
36 import org.openecomp.sdc.be.user.UserBusinessLogic;
37 import org.openecomp.sdc.common.datastructure.Wrapper;
38 import org.openecomp.sdc.common.log.wrappers.Logger;
39 import org.springframework.stereotype.Controller;
41 import javax.inject.Inject;
42 import javax.ws.rs.Consumes;
43 import javax.ws.rs.DELETE;
44 import javax.ws.rs.GET;
45 import javax.ws.rs.HeaderParam;
46 import javax.ws.rs.POST;
47 import javax.ws.rs.PUT;
48 import javax.ws.rs.Path;
49 import javax.ws.rs.PathParam;
50 import javax.ws.rs.Produces;
51 import javax.ws.rs.WebApplicationException;
52 import javax.ws.rs.core.MediaType;
53 import javax.ws.rs.core.Response;
54 import java.util.List;
58 @Tags({@Tag(name = "SDCE-7 APIs")})
59 @Servers({@Server(url = "/sdc")})
61 public class ExternalRefsServlet extends BeGenericServlet {
63 private static final Logger log = Logger.getLogger(ExternalRefsServlet.class);
64 private final ExternalRefsBusinessLogic businessLogic;
67 public ExternalRefsServlet(UserBusinessLogic userBusinessLogic,
68 ComponentsUtils componentsUtils,
69 ExternalRefsBusinessLogic externalRefsBusinessLogic) {
70 super(userBusinessLogic, componentsUtils);
71 this.businessLogic = externalRefsBusinessLogic;
75 @Path("/{assetType}/{uuid}/version/{version}/resourceInstances/{componentInstanceName}/externalReferences/{objectType}")
76 @Produces(MediaType.APPLICATION_JSON)
77 @PermissionAllowed({AafPermission.PermNames.READ_VALUE})
78 public Response getComponentInstanceExternalRef(
79 @PathParam("assetType") String assetType,
80 @PathParam("uuid") String uuid,
81 @PathParam("version") String version,
82 @PathParam("componentInstanceName") String componentInstanceName,
83 @PathParam("objectType") String objectType, @HeaderParam("USER_ID") String userId, @HeaderParam("X-ECOMP-InstanceID") String xEcompInstanceId) {
85 log.debug("GET component instance external interfaces {} {} {} {}", assetType, uuid, componentInstanceName, objectType);
87 Response r = validateRequest(xEcompInstanceId);
92 Either<List<String>, ActionStatus> refsResult = this.businessLogic.getExternalReferences(uuid, version, componentInstanceName, objectType);
93 if (refsResult.isLeft()){
94 return this.buildOkResponse(refsResult.left().value());
96 return this.buildExtRefErrorResponse(refsResult.right().value(), uuid, version, componentInstanceName, objectType, "");
101 @Path("/{assetType}/{uuid}/version/{version}/externalReferences/{objectType}")
102 @Produces(MediaType.APPLICATION_JSON)
103 @PermissionAllowed({AafPermission.PermNames.READ_VALUE})
104 public Map<String, List<String>> getAssetExternalRefByObjectType(
105 @PathParam("assetType") String assetType,
106 @PathParam("uuid") String uuid,
107 @PathParam("version") String version,
108 @PathParam("objectType") String objectType, @HeaderParam("USER_ID") String userId, @HeaderParam("X-ECOMP-InstanceID") String xEcompInstanceId) {
110 log.debug("GET asset external references {} {} {}", assetType, uuid, objectType);
112 Response r = validateRequest(xEcompInstanceId);
114 throw new WebApplicationException(r);
117 Either<Map<String, List<String>>, ActionStatus> refsResult = this.businessLogic.getExternalReferences(uuid, version, objectType);
118 if (refsResult.isLeft()){
119 return refsResult.left().value();
121 throw new WebApplicationException(this.buildExtRefErrorResponse(refsResult.right().value(), uuid, version, "", objectType, ""));
126 @Path("/{assetType}/{uuid}/resourceInstances/{componentInstanceName}/externalReferences/{objectType}")
127 @Consumes(MediaType.APPLICATION_JSON)
128 @Produces(MediaType.APPLICATION_JSON)
129 @PermissionAllowed({AafPermission.PermNames.WRITE_VALUE})
130 public Response addComponentInstanceExternalRef(
131 @PathParam("assetType") String assetType,
132 @PathParam("uuid") String uuid,
133 @PathParam("componentInstanceName") String componentInstanceName,
134 @PathParam("objectType") String objectType, ExternalRefDTO ref, @HeaderParam("USER_ID") String userId, @HeaderParam("X-ECOMP-InstanceID") String xEcompInstanceId) {
136 log.debug("POST component instance external interfaces {} {} {} {} {}", assetType, uuid, componentInstanceName, objectType, ref);
138 Response r = validateRequest(xEcompInstanceId);
143 ComponentTypeEnum componentType = ComponentTypeEnum.findByParamName(assetType);
144 String uid = this.businessLogic.fetchComponentUniqueIdByUuid(uuid, componentType);
145 Either<String, ActionStatus> addResult = this.businessLogic.addExternalReference(uid, componentType, userId, componentInstanceName, objectType, ref);
146 if (addResult.isLeft()) {
147 return Response.status(Response.Status.CREATED)
151 return this.buildExtRefErrorResponse(addResult.right().value(), uuid, "", componentInstanceName, objectType, ref.getReferenceUUID());
157 @Path("/{assetType}/{uuid}/resourceInstances/{componentInstanceName}/externalReferences/{objectType}/{reference}")
158 @Produces(MediaType.APPLICATION_JSON)
159 @PermissionAllowed({AafPermission.PermNames.DELETE_VALUE})
160 public Response deleteComponentInstanceReference(
161 @PathParam("assetType") String assetType,
162 @PathParam("uuid") String uuid,
163 @PathParam("componentInstanceName") String componentInstanceName,
164 @PathParam("objectType") String objectType,
165 @PathParam("reference") String reference, @HeaderParam("USER_ID") String userId, @HeaderParam("X-ECOMP-InstanceID") String xEcompInstanceId) {
167 log.debug("DELETE component instance external interfaces {} {} {} {}", assetType, uuid, componentInstanceName, objectType);
169 Response r = validateRequest(xEcompInstanceId);
173 ComponentTypeEnum componentType = ComponentTypeEnum.findByParamName(assetType);
174 String uid = this.businessLogic.fetchComponentUniqueIdByUuid(uuid, componentType);
175 Either<String, ActionStatus> deleteStatus = this.businessLogic.deleteExternalReference(uid, componentType, userId, componentInstanceName, objectType, reference);
176 if (deleteStatus.isLeft()){
177 return this.buildOkResponse(new ExternalRefDTO(reference));
179 return this.buildExtRefErrorResponse(deleteStatus.right().value(), uuid, "", componentInstanceName, objectType, reference);
184 @Path("/{assetType}/{uuid}/resourceInstances/{componentInstanceName}/externalReferences/{objectType}/{oldRefValue}")
185 @Produces(MediaType.APPLICATION_JSON)
186 @Consumes(MediaType.APPLICATION_JSON)
187 @PermissionAllowed({AafPermission.PermNames.WRITE_VALUE})
188 public Response updateComponentInstanceReference(
189 @PathParam("assetType") String assetType,
190 @PathParam("uuid") String uuid,
191 @PathParam("componentInstanceName") String componentInstanceName,
192 @PathParam("objectType") String objectType,
193 @PathParam("oldRefValue") String oldRefValue,
194 ExternalRefDTO newRefValueDTO, @HeaderParam("USER_ID") String userId, @HeaderParam("X-ECOMP-InstanceID") String xEcompInstanceId) {
196 log.debug("PUT component instance external interfaces {} {} {} {}", assetType, uuid, componentInstanceName, objectType);
198 Response r = validateRequest(xEcompInstanceId);
203 String newRefValue = newRefValueDTO.getReferenceUUID();
204 ComponentTypeEnum componentType = ComponentTypeEnum.findByParamName(assetType);
205 String uid = this.businessLogic.fetchComponentUniqueIdByUuid(uuid, componentType);
206 Either<String, ActionStatus> updateResult = this.businessLogic.updateExternalReference(uid, componentType, userId, componentInstanceName, objectType, oldRefValue, newRefValue);
207 if (updateResult.isLeft()){
208 return this.buildOkResponse(new ExternalRefDTO(newRefValue));
210 return this.buildExtRefErrorResponse(updateResult.right().value(), uuid, "", componentInstanceName, objectType, oldRefValue);
215 private Response validateRequest(String xEcompInstanceIdHeader) {
216 Wrapper<Response> responseWrapper = new Wrapper<>();
218 //Validate X-ECOMP_INSTANCE_ID_HEADER
219 if (xEcompInstanceIdHeader == null || xEcompInstanceIdHeader.isEmpty()){
220 return this.buildExtRefErrorResponse(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID, "", "", "", "", "");
223 return responseWrapper.getInnerElement();
226 private Response buildExtRefErrorResponse(ActionStatus status, String uuid, String version, String componentInstanceName, String objectType, String ref){
228 case RESOURCE_NOT_FOUND:
229 return buildErrorResponse(componentsUtils.getResponseFormat(status, uuid));
230 case COMPONENT_VERSION_NOT_FOUND:
231 return buildErrorResponse(componentsUtils.getResponseFormat(status, uuid, version));
232 case COMPONENT_INSTANCE_NOT_FOUND:
233 return buildErrorResponse(componentsUtils.getResponseFormat(status, componentInstanceName, uuid));
234 case EXT_REF_ALREADY_EXIST:
235 return Response.status(Response.Status.OK)
236 .entity(new ExternalRefDTO(ref))
238 case EXT_REF_NOT_FOUND:
239 return buildErrorResponse(componentsUtils.getResponseFormat(status, objectType + "/" + ref));
240 case MISSING_X_ECOMP_INSTANCE_ID:
241 return buildErrorResponse(componentsUtils.getResponseFormat(status));
243 return this.buildGeneralErrorResponse();