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 org.openecomp.sdc.be.components.impl.ExternalRefsBusinessLogic;
25 import org.openecomp.sdc.be.components.impl.aaf.AafPermission;
26 import org.openecomp.sdc.be.components.impl.aaf.PermissionAllowed;
27 import org.openecomp.sdc.be.dao.api.ActionStatus;
28 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
29 import org.openecomp.sdc.be.dto.ExternalRefDTO;
30 import org.openecomp.sdc.be.impl.ComponentsUtils;
31 import org.openecomp.sdc.be.servlets.BeGenericServlet;
32 import org.openecomp.sdc.be.user.UserBusinessLogic;
33 import org.openecomp.sdc.common.datastructure.Wrapper;
34 import org.openecomp.sdc.common.log.wrappers.Logger;
35 import org.springframework.stereotype.Controller;
37 import javax.inject.Inject;
38 import javax.ws.rs.Consumes;
39 import javax.ws.rs.DELETE;
40 import javax.ws.rs.GET;
41 import javax.ws.rs.HeaderParam;
42 import javax.ws.rs.POST;
43 import javax.ws.rs.PUT;
44 import javax.ws.rs.Path;
45 import javax.ws.rs.PathParam;
46 import javax.ws.rs.Produces;
47 import javax.ws.rs.WebApplicationException;
48 import javax.ws.rs.core.MediaType;
49 import javax.ws.rs.core.Response;
50 import java.util.List;
55 public class ExternalRefsServlet extends BeGenericServlet {
57 private static final Logger log = Logger.getLogger(ExternalRefsServlet.class);
58 private final ExternalRefsBusinessLogic businessLogic;
61 public ExternalRefsServlet(UserBusinessLogic userBusinessLogic,
62 ComponentsUtils componentsUtils,
63 ExternalRefsBusinessLogic externalRefsBusinessLogic) {
64 super(userBusinessLogic, componentsUtils);
65 this.businessLogic = externalRefsBusinessLogic;
69 @Path("/{assetType}/{uuid}/version/{version}/resourceInstances/{componentInstanceName}/externalReferences/{objectType}")
70 @Produces(MediaType.APPLICATION_JSON)
71 @PermissionAllowed({AafPermission.PermNames.READ_VALUE})
72 public Response getComponentInstanceExternalRef(
73 @PathParam("assetType") String assetType,
74 @PathParam("uuid") String uuid,
75 @PathParam("version") String version,
76 @PathParam("componentInstanceName") String componentInstanceName,
77 @PathParam("objectType") String objectType, @HeaderParam("USER_ID") String userId, @HeaderParam("X-ECOMP-InstanceID") String xEcompInstanceId) {
79 log.debug("GET component instance external interfaces {} {} {} {}", assetType, uuid, componentInstanceName, objectType);
81 Response r = validateRequest(xEcompInstanceId);
86 Either<List<String>, ActionStatus> refsResult = this.businessLogic.getExternalReferences(uuid, version, componentInstanceName, objectType);
87 if (refsResult.isLeft()){
88 return this.buildOkResponse(refsResult.left().value());
90 return this.buildExtRefErrorResponse(refsResult.right().value(), uuid, version, componentInstanceName, objectType, "");
95 @Path("/{assetType}/{uuid}/version/{version}/externalReferences/{objectType}")
96 @Produces(MediaType.APPLICATION_JSON)
97 @PermissionAllowed({AafPermission.PermNames.READ_VALUE})
98 public Map<String, List<String>> getAssetExternalRefByObjectType(
99 @PathParam("assetType") String assetType,
100 @PathParam("uuid") String uuid,
101 @PathParam("version") String version,
102 @PathParam("objectType") String objectType, @HeaderParam("USER_ID") String userId, @HeaderParam("X-ECOMP-InstanceID") String xEcompInstanceId) {
104 log.debug("GET asset external references {} {} {}", assetType, uuid, objectType);
106 Response r = validateRequest(xEcompInstanceId);
108 throw new WebApplicationException(r);
111 Either<Map<String, List<String>>, ActionStatus> refsResult = this.businessLogic.getExternalReferences(uuid, version, objectType);
112 if (refsResult.isLeft()){
113 return refsResult.left().value();
115 throw new WebApplicationException(this.buildExtRefErrorResponse(refsResult.right().value(), uuid, version, "", objectType, ""));
120 @Path("/{assetType}/{uuid}/resourceInstances/{componentInstanceName}/externalReferences/{objectType}")
121 @Consumes(MediaType.APPLICATION_JSON)
122 @Produces(MediaType.APPLICATION_JSON)
123 @PermissionAllowed({AafPermission.PermNames.WRITE_VALUE})
124 public Response addComponentInstanceExternalRef(
125 @PathParam("assetType") String assetType,
126 @PathParam("uuid") String uuid,
127 @PathParam("componentInstanceName") String componentInstanceName,
128 @PathParam("objectType") String objectType, ExternalRefDTO ref, @HeaderParam("USER_ID") String userId, @HeaderParam("X-ECOMP-InstanceID") String xEcompInstanceId) {
130 log.debug("POST component instance external interfaces {} {} {} {} {}", assetType, uuid, componentInstanceName, objectType, ref);
132 Response r = validateRequest(xEcompInstanceId);
137 ComponentTypeEnum componentType = ComponentTypeEnum.findByParamName(assetType);
138 String uid = this.businessLogic.fetchComponentUniqueIdByUuid(uuid, componentType);
139 Either<String, ActionStatus> addResult = this.businessLogic.addExternalReference(uid, componentType, userId, componentInstanceName, objectType, ref);
140 if (addResult.isLeft()) {
141 return Response.status(Response.Status.CREATED)
145 return this.buildExtRefErrorResponse(addResult.right().value(), uuid, "", componentInstanceName, objectType, ref.getReferenceUUID());
151 @Path("/{assetType}/{uuid}/resourceInstances/{componentInstanceName}/externalReferences/{objectType}/{reference}")
152 @Produces(MediaType.APPLICATION_JSON)
153 @PermissionAllowed({AafPermission.PermNames.DELETE_VALUE})
154 public Response deleteComponentInstanceReference(
155 @PathParam("assetType") String assetType,
156 @PathParam("uuid") String uuid,
157 @PathParam("componentInstanceName") String componentInstanceName,
158 @PathParam("objectType") String objectType,
159 @PathParam("reference") String reference, @HeaderParam("USER_ID") String userId, @HeaderParam("X-ECOMP-InstanceID") String xEcompInstanceId) {
161 log.debug("DELETE component instance external interfaces {} {} {} {}", assetType, uuid, componentInstanceName, objectType);
163 Response r = validateRequest(xEcompInstanceId);
167 ComponentTypeEnum componentType = ComponentTypeEnum.findByParamName(assetType);
168 String uid = this.businessLogic.fetchComponentUniqueIdByUuid(uuid, componentType);
169 Either<String, ActionStatus> deleteStatus = this.businessLogic.deleteExternalReference(uid, componentType, userId, componentInstanceName, objectType, reference);
170 if (deleteStatus.isLeft()){
171 return this.buildOkResponse(new ExternalRefDTO(reference));
173 return this.buildExtRefErrorResponse(deleteStatus.right().value(), uuid, "", componentInstanceName, objectType, reference);
178 @Path("/{assetType}/{uuid}/resourceInstances/{componentInstanceName}/externalReferences/{objectType}/{oldRefValue}")
179 @Produces(MediaType.APPLICATION_JSON)
180 @Consumes(MediaType.APPLICATION_JSON)
181 @PermissionAllowed({AafPermission.PermNames.WRITE_VALUE})
182 public Response updateComponentInstanceReference(
183 @PathParam("assetType") String assetType,
184 @PathParam("uuid") String uuid,
185 @PathParam("componentInstanceName") String componentInstanceName,
186 @PathParam("objectType") String objectType,
187 @PathParam("oldRefValue") String oldRefValue,
188 ExternalRefDTO newRefValueDTO, @HeaderParam("USER_ID") String userId, @HeaderParam("X-ECOMP-InstanceID") String xEcompInstanceId) {
190 log.debug("PUT component instance external interfaces {} {} {} {}", assetType, uuid, componentInstanceName, objectType);
192 Response r = validateRequest(xEcompInstanceId);
197 String newRefValue = newRefValueDTO.getReferenceUUID();
198 ComponentTypeEnum componentType = ComponentTypeEnum.findByParamName(assetType);
199 String uid = this.businessLogic.fetchComponentUniqueIdByUuid(uuid, componentType);
200 Either<String, ActionStatus> updateResult = this.businessLogic.updateExternalReference(uid, componentType, userId, componentInstanceName, objectType, oldRefValue, newRefValue);
201 if (updateResult.isLeft()){
202 return this.buildOkResponse(new ExternalRefDTO(newRefValue));
204 return this.buildExtRefErrorResponse(updateResult.right().value(), uuid, "", componentInstanceName, objectType, oldRefValue);
209 private Response validateRequest(String xEcompInstanceIdHeader) {
210 Wrapper<Response> responseWrapper = new Wrapper<>();
212 //Validate X-ECOMP_INSTANCE_ID_HEADER
213 if (xEcompInstanceIdHeader == null || xEcompInstanceIdHeader.isEmpty()){
214 return this.buildExtRefErrorResponse(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID, "", "", "", "", "");
217 return responseWrapper.getInnerElement();
220 private Response buildExtRefErrorResponse(ActionStatus status, String uuid, String version, String componentInstanceName, String objectType, String ref){
222 case RESOURCE_NOT_FOUND:
223 return buildErrorResponse(componentsUtils.getResponseFormat(status, uuid));
224 case COMPONENT_VERSION_NOT_FOUND:
225 return buildErrorResponse(componentsUtils.getResponseFormat(status, uuid, version));
226 case COMPONENT_INSTANCE_NOT_FOUND:
227 return buildErrorResponse(componentsUtils.getResponseFormat(status, componentInstanceName, uuid));
228 case EXT_REF_ALREADY_EXIST:
229 return Response.status(Response.Status.OK)
230 .entity(new ExternalRefDTO(ref))
232 case EXT_REF_NOT_FOUND:
233 return buildErrorResponse(componentsUtils.getResponseFormat(status, objectType + "/" + ref));
234 case MISSING_X_ECOMP_INSTANCE_ID:
235 return buildErrorResponse(componentsUtils.getResponseFormat(status));
237 return this.buildGeneralErrorResponse();