551540023b694c22cc8972442bc4dae5f69aa0a7
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / servlets / ComponentInstanceServlet.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.openecomp.sdc.be.servlets;
21
22 import com.fasterxml.jackson.core.JsonProcessingException;
23 import com.fasterxml.jackson.databind.ObjectMapper;
24 import com.fasterxml.jackson.databind.SerializationFeature;
25 import com.google.gson.Gson;
26 import com.google.gson.GsonBuilder;
27 import com.google.gson.reflect.TypeToken;
28 import com.jcabi.aspects.Loggable;
29 import fj.data.Either;
30 import io.swagger.v3.oas.annotations.Operation;
31 import io.swagger.v3.oas.annotations.Parameter;
32 import io.swagger.v3.oas.annotations.media.ArraySchema;
33 import io.swagger.v3.oas.annotations.media.Content;
34 import io.swagger.v3.oas.annotations.media.Schema;
35 import io.swagger.v3.oas.annotations.responses.ApiResponse;
36 import io.swagger.v3.oas.annotations.servers.Server;
37 import io.swagger.v3.oas.annotations.tags.Tag;
38 import io.swagger.v3.oas.annotations.tags.Tags;
39 import java.io.IOException;
40 import java.io.InputStream;
41 import java.lang.reflect.Type;
42 import java.util.ArrayList;
43 import java.util.Arrays;
44 import java.util.List;
45 import java.util.Map;
46 import java.util.Optional;
47 import java.util.Set;
48 import javax.inject.Inject;
49 import javax.servlet.http.HttpServletRequest;
50 import javax.ws.rs.Consumes;
51 import javax.ws.rs.DELETE;
52 import javax.ws.rs.GET;
53 import javax.ws.rs.HeaderParam;
54 import javax.ws.rs.POST;
55 import javax.ws.rs.PUT;
56 import javax.ws.rs.Path;
57 import javax.ws.rs.PathParam;
58 import javax.ws.rs.Produces;
59 import javax.ws.rs.QueryParam;
60 import javax.ws.rs.core.Context;
61 import javax.ws.rs.core.MediaType;
62 import javax.ws.rs.core.Response;
63 import org.apache.commons.collections.CollectionUtils;
64 import org.apache.commons.io.IOUtils;
65 import org.apache.commons.lang.StringUtils;
66 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
67 import org.openecomp.sdc.be.components.impl.ComponentNodeFilterBusinessLogic;
68 import org.openecomp.sdc.be.components.impl.GroupBusinessLogic;
69 import org.openecomp.sdc.be.components.impl.ResourceImportManager;
70 import org.openecomp.sdc.be.components.impl.aaf.AafPermission;
71 import org.openecomp.sdc.be.components.impl.aaf.PermissionAllowed;
72 import org.openecomp.sdc.be.components.impl.exceptions.BusinessLogicException;
73 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
74 import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException;
75 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
76 import org.openecomp.sdc.be.config.BeEcompErrorManager;
77 import org.openecomp.sdc.be.dao.api.ActionStatus;
78 import org.openecomp.sdc.be.datamodel.ForwardingPaths;
79 import org.openecomp.sdc.be.datatypes.elements.CINodeFilterDataDefinition;
80 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
81 import org.openecomp.sdc.be.datatypes.enums.CreatedFrom;
82 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
83 import org.openecomp.sdc.be.externalapi.servlet.representation.ReplaceVNFInfo;
84 import org.openecomp.sdc.be.impl.ComponentsUtils;
85 import org.openecomp.sdc.be.impl.ServletUtils;
86 import org.openecomp.sdc.be.info.CreateAndAssotiateInfo;
87 import org.openecomp.sdc.be.info.GroupDefinitionInfo;
88 import org.openecomp.sdc.be.model.ComponentInstance;
89 import org.openecomp.sdc.be.model.ComponentInstanceAttribute;
90 import org.openecomp.sdc.be.model.ComponentInstanceInput;
91 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
92 import org.openecomp.sdc.be.model.PropertyConstraint;
93 import org.openecomp.sdc.be.model.RequirementCapabilityRelDef;
94 import org.openecomp.sdc.be.model.RequirementDefinition;
95 import org.openecomp.sdc.be.model.Service;
96 import org.openecomp.sdc.be.model.User;
97 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation.PropertyConstraintDeserialiser;
98 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
99 import org.openecomp.sdc.be.user.UserBusinessLogic;
100 import org.openecomp.sdc.common.api.Constants;
101 import org.openecomp.sdc.common.datastructure.Wrapper;
102 import org.openecomp.sdc.common.log.elements.LoggerSupportability;
103 import org.openecomp.sdc.common.log.enums.LoggerSupportabilityActions;
104 import org.openecomp.sdc.common.log.enums.StatusCode;
105 import org.openecomp.sdc.common.log.wrappers.Logger;
106 import org.openecomp.sdc.exception.ResponseFormat;
107 import org.springframework.stereotype.Controller;
108
109 /**
110  * Root resource (exposed at "/" path) .json.
111  */
112 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
113 @Path("/v1/catalog")
114 @Tags({@Tag(name = "SDCE-2 APIs")})
115 @Server(url = "/sdc2/rest")
116 @Controller
117 public class ComponentInstanceServlet extends AbstractValidationsServlet {
118
119     private static final String FAILED_TO_GET_PROPERTIES_OF_COMPONENT_INSTANCE_ID_IN_WITH_ID = "Failed to get properties of component instance ID: {} in {} with ID: {}";
120     private static final String GET_GROUP_ARTIFACT_BY_ID = "getGroupArtifactById";
121     private static final String GET_GROUP_ARTIFACT_BY_ID_UNEXPECTED_EXCEPTION = "getGroupArtifactById unexpected exception";
122     private static final String GET_START_HANDLE_REQUEST_OF = "(GET) Start handle request of {}";
123     private static final String START_HANDLE_REQUEST_OF_UPDATE_RESOURCE_INSTANCE_PROPERTY_RECEIVED_PROPERTY_IS = "Start handle request of updateResourceInstanceProperty. Received property is {}";
124     private static final String UPDATE_RESOURCE_INSTANCE = "Update Resource Instance";
125     private static final String RESOURCE_INSTANCE_UPDATE_RESOURCE_INSTANCE = "Resource Instance - updateResourceInstance";
126     private static final String UPDATE_RESOURCE_INSTANCE_WITH_EXCEPTION = "update resource instance with exception";
127     private static final String FAILED_TO_CONVERT_RECEIVED_DATA_TO_BE_FORMAT = "Failed to convert received data to BE format.";
128     private static final String EMPTY_BODY_WAS_SENT = "Empty body was sent.";
129     private static final String START_HANDLE_REQUEST_OF = "Start handle request of {}";
130     private static final String UNSUPPORTED_COMPONENT_TYPE = "Unsupported component type {}";
131     private static final String CREATE_AND_ASSOCIATE_RI_FAILED_WITH_EXCEPTION = "create and associate RI failed with exception: {}";
132     private static final Logger log = Logger.getLogger(ComponentInstanceServlet.class);
133     private static final Type PROPERTY_CONSTRAINT_TYPE = new TypeToken<PropertyConstraint>() {
134     }.getType();
135     private static final Gson gsonDeserializer = new GsonBuilder().registerTypeAdapter(PROPERTY_CONSTRAINT_TYPE, new PropertyConstraintDeserialiser())
136         .create();
137     private static final LoggerSupportability loggerSupportability = LoggerSupportability.getLogger(ComponentInstanceServlet.class.getName());
138     private static final String SERVICES = "services";
139     private final GroupBusinessLogic groupBL;
140     private final ComponentNodeFilterBusinessLogic nodeFilterBusinessLogic;
141
142     @Inject
143     public ComponentInstanceServlet(UserBusinessLogic userBusinessLogic, GroupBusinessLogic groupBL,
144                                     ComponentInstanceBusinessLogic componentInstanceBL, ComponentsUtils componentsUtils, ServletUtils servletUtils,
145                                     ResourceImportManager resourceImportManager, ComponentNodeFilterBusinessLogic nodeFilterBusinessLogic) {
146         super(userBusinessLogic, componentInstanceBL, componentsUtils, servletUtils, resourceImportManager);
147         this.groupBL = groupBL;
148         this.nodeFilterBusinessLogic = nodeFilterBusinessLogic;
149     }
150
151     @POST
152     @Path("/{containerComponentType}/{componentId}/resourceInstance")
153     @Consumes(MediaType.APPLICATION_JSON)
154     @Produces(MediaType.APPLICATION_JSON)
155     @Operation(description = "Create ComponentInstance", method = "POST", summary = "Returns created ComponentInstance", responses = {
156         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
157         @ApiResponse(responseCode = "201", description = "Component created"),
158         @ApiResponse(responseCode = "403", description = "Restricted operation"),
159         @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
160         @ApiResponse(responseCode = "409", description = "Component instance already exist")})
161     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
162     public Response createComponentInstance(@Parameter(description = "RI object to be created", required = true) String data,
163                                             @PathParam("componentId") final String containerComponentId,
164                                             @Parameter(description = "valid values: resources / services", schema = @Schema(allowableValues = {
165                                                 ComponentTypeEnum.RESOURCE_PARAM_NAME,
166                                                 ComponentTypeEnum.SERVICE_PARAM_NAME})) @PathParam("containerComponentType") final String containerComponentType,
167                                             @HeaderParam(value = Constants.USER_ID_HEADER) @Parameter(description = "USER_ID of modifier user", required = true) String userId,
168                                             @Context final HttpServletRequest request) {
169         validateNotEmptyBody(data);
170         final ComponentInstance componentInstance;
171         try {
172             componentInstance = RepresentationUtils.fromRepresentation(data, ComponentInstance.class);
173             componentInstance.setInvariantName(null);
174             componentInstance.setCreatedFrom(CreatedFrom.UI);
175         } catch (final Exception e) {
176             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Create Component Instance");
177             log.debug("create component instance failed with exception", e);
178             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT, containerComponentType));
179         }
180         loggerSupportability
181             .log(LoggerSupportabilityActions.CREATE_INSTANCE, StatusCode.STARTED, "Starting to create component instance by {}", userId);
182         if (componentInstanceBusinessLogic == null) {
183             log.debug(UNSUPPORTED_COMPONENT_TYPE, containerComponentType);
184             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
185         }
186         final ComponentInstance actionResponse = componentInstanceBusinessLogic
187             .createComponentInstance(containerComponentType, containerComponentId, userId, componentInstance);
188         loggerSupportability.log(LoggerSupportabilityActions.CREATE_INSTANCE, actionResponse.getComponentMetadataForSupportLog(), StatusCode.COMPLETE,
189             "Ending to create component instance by user {}", userId);
190         return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.CREATED), actionResponse);
191     }
192
193     @POST
194     @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}")
195     @Consumes(MediaType.APPLICATION_JSON)
196     @Produces(MediaType.APPLICATION_JSON)
197     @Operation(description = "Update resource instance", method = "POST", summary = "Returns updated resource instance", responses = {
198         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
199         @ApiResponse(responseCode = "200", description = "Resource instance updated"),
200         @ApiResponse(responseCode = "403", description = "Restricted operation"),
201         @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
202     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
203     public Response updateComponentInstanceMetadata(@PathParam("componentId") final String componentId,
204                                                     @PathParam("componentInstanceId") final String componentInstanceId,
205                                                     @Parameter(description = "valid values: resources / services / products", schema = @Schema(allowableValues = {
206                                                         ComponentTypeEnum.RESOURCE_PARAM_NAME, ComponentTypeEnum.SERVICE_PARAM_NAME,
207                                                         ComponentTypeEnum.PRODUCT_PARAM_NAME})) @PathParam("containerComponentType") final String containerComponentType,
208                                                     @Context final HttpServletRequest request) throws IOException, BusinessLogicException {
209         final String url = request.getMethod() + " " + request.getRequestURI();
210         log.debug(START_HANDLE_REQUEST_OF, url);
211         loggerSupportability.log(LoggerSupportabilityActions.UPDATE_COMPONENT_INSTANCE, StatusCode.STARTED, "update Component Instance Metadata");
212         final String userId = request.getHeader(Constants.USER_ID_HEADER);
213         try {
214             if (componentInstanceBusinessLogic == null) {
215                 log.debug(UNSUPPORTED_COMPONENT_TYPE, containerComponentType);
216                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
217             }
218             componentInstanceBusinessLogic.validateUser(userId);
219             log.debug(START_HANDLE_REQUEST_OF, url);
220             final byte[] bytes = IOUtils.toByteArray(request.getInputStream());
221             if (bytes == null || bytes.length == 0) {
222                 log.info(EMPTY_BODY_WAS_SENT);
223                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
224             }
225             final String data = new String(bytes);
226             final ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
227             final Either<ComponentInstance, ResponseFormat> convertResponse = convertToResourceInstance(data);
228             if (convertResponse.isRight()) {
229                 BeEcompErrorManager.getInstance().logBeSystemError(RESOURCE_INSTANCE_UPDATE_RESOURCE_INSTANCE);
230                 log.debug(FAILED_TO_CONVERT_RECEIVED_DATA_TO_BE_FORMAT);
231                 return buildErrorResponse(convertResponse.right().value());
232             }
233             final ComponentInstance componentInstance = convertResponse.left().value();
234             final Either<ComponentInstance, ResponseFormat> actionResponse = componentInstanceBusinessLogic
235                 .updateComponentInstanceMetadata(containerComponentType, componentId, componentInstanceId, userId, componentInstance);
236             loggerSupportability
237                 .log(LoggerSupportabilityActions.UPDATE_COMPONENT_INSTANCE, actionResponse.left().value().getComponentMetadataForSupportLog(),
238                     StatusCode.COMPLETE, "update Component Instance Metadata by {}", userId);
239             if (actionResponse.isRight()) {
240                 return buildErrorResponse(actionResponse.right().value());
241             }
242             final ComponentInstance resultValue = actionResponse.left().value();
243             if (ComponentTypeEnum.SERVICE.equals(componentTypeEnum) || ComponentTypeEnum.RESOURCE.equals(componentTypeEnum)) {
244                 if (CollectionUtils.isNotEmpty(componentInstance.getDirectives())) {
245                     final Optional<CINodeFilterDataDefinition> nodeFilterDataDefinition = nodeFilterBusinessLogic
246                         .createNodeFilterIfNotExist(componentId, componentInstanceId, true, componentTypeEnum);
247                     if (!nodeFilterDataDefinition.isPresent()) {
248                         BeEcompErrorManager.getInstance().logBeSystemError("Failed to create node filter.");
249                         log.error("Failed to create node filter.");
250                         return buildErrorResponse(convertResponse.right().value());
251                     }
252                     resultValue.setNodeFilter(nodeFilterDataDefinition.get());
253                 } else {
254                     final Optional<String> result = nodeFilterBusinessLogic
255                         .deleteNodeFilterIfExists(componentId, componentInstanceId, true, componentTypeEnum);
256                     if (!result.isPresent()) {
257                         BeEcompErrorManager.getInstance().logBeSystemError("Failed to delete node filter.");
258                         log.error("Failed to delete node filter.");
259                         return buildErrorResponse(convertResponse.right().value());
260                     }
261                     resultValue.setNodeFilter(null);
262                 }
263             }
264             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
265         } catch (final Exception e) {
266             BeEcompErrorManager.getInstance().logBeRestApiGeneralError(UPDATE_RESOURCE_INSTANCE);
267             log.debug(UPDATE_RESOURCE_INSTANCE_WITH_EXCEPTION, e);
268             throw e;
269         }
270     }
271
272     @POST
273     @Path("/{containerComponentType}/{componentId}/resourceInstance/multipleComponentInstance")
274     @Consumes(MediaType.APPLICATION_JSON)
275     @Produces(MediaType.APPLICATION_JSON)
276     @Operation(description = "Update resource instance multiple component", method = "POST", summary = "Returns updated resource instance", responses = {
277         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
278         @ApiResponse(responseCode = "200", description = "Resource instance updated"),
279         @ApiResponse(responseCode = "403", description = "Restricted operation"),
280         @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
281     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
282     public Response updateMultipleComponentInstance(@PathParam("componentId") final String componentId,
283                                                     @Parameter(description = "valid values: resources / services / products", schema = @Schema(allowableValues = {
284                                                         ComponentTypeEnum.RESOURCE_PARAM_NAME, ComponentTypeEnum.SERVICE_PARAM_NAME,
285                                                         ComponentTypeEnum.PRODUCT_PARAM_NAME})) @PathParam("containerComponentType") final String containerComponentType,
286                                                     @Context final HttpServletRequest request,
287                                                     @Parameter(description = "Component Instance JSON Array", required = true) final String componentInstanceJsonArray) {
288         String url = request.getMethod() + " " + request.getRequestURI();
289         log.debug(START_HANDLE_REQUEST_OF, url);
290         try {
291             log.debug(START_HANDLE_REQUEST_OF, url);
292             if (componentInstanceJsonArray == null || componentInstanceJsonArray.length() == 0) {
293                 log.info("Empty JSON list was sent.");
294                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
295             }
296             String userId = request.getHeader(Constants.USER_ID_HEADER);
297             if (componentInstanceBusinessLogic == null) {
298                 log.debug(UNSUPPORTED_COMPONENT_TYPE, containerComponentType);
299                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
300             }
301             Either<List<ComponentInstance>, ResponseFormat> convertResponse = convertToMultipleResourceInstance(componentInstanceJsonArray);
302             if (convertResponse.isRight()) {
303                 // Using both ECOMP error methods, show to Sofer
304                 BeEcompErrorManager.getInstance().logBeSystemError(RESOURCE_INSTANCE_UPDATE_RESOURCE_INSTANCE);
305                 log.debug(FAILED_TO_CONVERT_RECEIVED_DATA_TO_BE_FORMAT);
306                 return buildErrorResponse(convertResponse.right().value());
307             }
308             List<ComponentInstance> componentInstanceList = convertResponse.left().value();
309             List<ComponentInstance> actionResponse = componentInstanceBusinessLogic
310                 .updateComponentInstance(containerComponentType, null, componentId, userId, componentInstanceList, true);
311             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse);
312         } catch (Exception e) {
313             BeEcompErrorManager.getInstance().logBeRestApiGeneralError(UPDATE_RESOURCE_INSTANCE);
314             log.debug(UPDATE_RESOURCE_INSTANCE_WITH_EXCEPTION, e);
315             throw e;
316         }
317     }
318
319     @DELETE
320     @Path("/{containerComponentType}/{componentId}/resourceInstance/{resourceInstanceId}")
321     @Consumes(MediaType.APPLICATION_JSON)
322     @Produces(MediaType.APPLICATION_JSON)
323     @Operation(description = "Delete ResourceInstance", method = "DELETE", summary = "Returns delete resourceInstance", responses = {
324         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
325         @ApiResponse(responseCode = "201", description = "ResourceInstance deleted"),
326         @ApiResponse(responseCode = "403", description = "Restricted operation"),
327         @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
328     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
329     public Response deleteResourceInstance(@PathParam("componentId") final String componentId,
330                                            @PathParam("resourceInstanceId") final String resourceInstanceId,
331                                            @Parameter(description = "valid values: resources / services / products", schema = @Schema(allowableValues = {
332                                                ComponentTypeEnum.RESOURCE_PARAM_NAME, ComponentTypeEnum.SERVICE_PARAM_NAME,
333                                                ComponentTypeEnum.PRODUCT_PARAM_NAME})) @PathParam("containerComponentType") final String containerComponentType,
334                                            @Context final HttpServletRequest request) {
335         String url = request.getMethod() + " " + request.getRequestURI();
336         try {
337             log.debug(START_HANDLE_REQUEST_OF, url);
338             if (componentInstanceBusinessLogic == null) {
339                 log.debug(UNSUPPORTED_COMPONENT_TYPE, containerComponentType);
340                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
341             }
342             String userId = request.getHeader(Constants.USER_ID_HEADER);
343             ComponentInstance actionResponse = componentInstanceBusinessLogic
344                 .deleteComponentInstance(containerComponentType, componentId, resourceInstanceId, userId);
345             loggerSupportability
346                 .log(LoggerSupportabilityActions.DELETE_COMPONENT_INSTANCE, actionResponse.getComponentMetadataForSupportLog(), StatusCode.STARTED,
347                     "DELETE_COMPONENT_INSTANCE by user {}", userId);
348             loggerSupportability
349                 .log(LoggerSupportabilityActions.DELETE_COMPONENT_INSTANCE, actionResponse.getComponentMetadataForSupportLog(), StatusCode.COMPLETE,
350                     "DELETE_COMPONENT_INSTANCE by user {}", userId);
351             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse);
352         } catch (Exception e) {
353             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Delete Resource Instance");
354             log.debug("delete resource instance with exception", e);
355             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
356         }
357     }
358
359     @Parameter(description = "allowed values are resources /services / products", schema = @Schema(allowableValues = {
360         ComponentTypeEnum.RESOURCE_PARAM_NAME, ComponentTypeEnum.SERVICE_PARAM_NAME, ComponentTypeEnum.PRODUCT_PARAM_NAME}), required = true)
361     @POST
362     @Path("/{containerComponentType}/{componentId}/resourceInstance/associate")
363     @Consumes(MediaType.APPLICATION_JSON)
364     @Produces(MediaType.APPLICATION_JSON)
365     @Operation(description = "Associate RI to RI", method = "POST", summary = "Returns created RelationshipInfo", responses = {
366         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
367         @ApiResponse(responseCode = "201", description = "Relationship created"),
368         @ApiResponse(responseCode = "403", description = "Missing information"),
369         @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
370         @ApiResponse(responseCode = "409", description = "Relationship already exist")})
371     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
372     public Response associateRIToRI(
373         @Parameter(description = "unique id of the container component") @PathParam("componentId") final String componentId,
374         @Parameter(description = "allowed values are resources /services / products", schema = @Schema(allowableValues = {
375             ComponentTypeEnum.RESOURCE_PARAM_NAME, ComponentTypeEnum.SERVICE_PARAM_NAME,
376             ComponentTypeEnum.PRODUCT_PARAM_NAME}), required = true) @PathParam("containerComponentType") final String containerComponentType,
377         @HeaderParam(value = Constants.USER_ID_HEADER) String userId, @Parameter(description = "RelationshipInfo", required = true) String data,
378         @Context final HttpServletRequest request) {
379         String url = request.getMethod() + " " + request.getRequestURI();
380         log.debug(START_HANDLE_REQUEST_OF, url);
381         loggerSupportability
382             .log(LoggerSupportabilityActions.ASSOCIATE_RI_TO_RI, StatusCode.STARTED, "Starting to associate RI To RI for component {} ",
383                 componentId + " by " + userId);
384         try {
385             log.debug(START_HANDLE_REQUEST_OF, url);
386             ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
387             if (componentInstanceBusinessLogic == null) {
388                 log.debug(UNSUPPORTED_COMPONENT_TYPE, containerComponentType);
389                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
390             }
391             RequirementCapabilityRelDef requirementDef = convertToRequirementCapabilityRelDef(data);
392             requirementDef.setOriginUI(true);
393             RequirementCapabilityRelDef actionResponse = componentInstanceBusinessLogic
394                 .associateRIToRI(componentId, userId, requirementDef, componentTypeEnum);
395             loggerSupportability
396                 .log(LoggerSupportabilityActions.ASSOCIATE_RI_TO_RI, StatusCode.COMPLETE, "Ended associate RI To RI for component {} ",
397                     componentId + " by " + userId);
398             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse);
399         } catch (Exception e) {
400             if (!e.getClass().equals(ComponentException.class)) {
401                 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Associate Resource Instance");
402                 log.debug("associate resource instance to another RI with exception", e);
403                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
404             }
405             throw e;
406         }
407     }
408
409     @PUT
410     @Path("/{containerComponentType}/{componentId}/resourceInstance/dissociate")
411     @Consumes(MediaType.APPLICATION_JSON)
412     @Produces(MediaType.APPLICATION_JSON)
413     @Operation(description = "Dissociate RI from RI", method = "PUT", summary = "Returns deleted RelationshipInfo", responses = {
414         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
415         @ApiResponse(responseCode = "201", description = "Relationship deleted"),
416         @ApiResponse(responseCode = "403", description = "Missing information"),
417         @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
418     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
419     public Response dissociateRIFromRI(
420         @Parameter(description = "allowed values are resources /services / products", schema = @Schema(allowableValues = {
421             ComponentTypeEnum.RESOURCE_PARAM_NAME, ComponentTypeEnum.SERVICE_PARAM_NAME,
422             ComponentTypeEnum.PRODUCT_PARAM_NAME}), required = true) @PathParam("containerComponentType") final String containerComponentType,
423         @Parameter(description = "unique id of the container component") @PathParam("componentId") final String componentId,
424         @HeaderParam(value = Constants.USER_ID_HEADER) String userId, @Parameter(description = "RelationshipInfo", required = true) String data,
425         @Context final HttpServletRequest request) {
426         String url = request.getMethod() + " " + request.getRequestURI();
427         log.debug(START_HANDLE_REQUEST_OF, url);
428         loggerSupportability
429             .log(LoggerSupportabilityActions.UN_ASSOCIATE_RI_TO_RI, StatusCode.STARTED, "Starting to undo associate RI To RI for component {} ",
430                 componentId + " by " + userId);
431         try {
432             log.debug(START_HANDLE_REQUEST_OF, url);
433             ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
434             if (componentInstanceBusinessLogic == null) {
435                 log.debug(UNSUPPORTED_COMPONENT_TYPE, containerComponentType);
436                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
437             }
438             RequirementCapabilityRelDef requirementDef = convertToRequirementCapabilityRelDef(data);
439             RequirementCapabilityRelDef actionResponse = componentInstanceBusinessLogic
440                 .dissociateRIFromRI(componentId, userId, requirementDef, componentTypeEnum);
441             loggerSupportability
442                 .log(LoggerSupportabilityActions.UN_ASSOCIATE_RI_TO_RI, StatusCode.COMPLETE, "Ended undo associate RI To RI for component {} ",
443                     componentId + " by " + userId);
444             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse);
445         } catch (Exception e) {
446             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Dissociate Resource Instance");
447             log.debug("dissociate resource instance from service failed with exception", e);
448             throw e;
449         }
450     }
451
452     @POST
453     @Path("/{containerComponentType}/{componentId}/resourceInstance/createAndAssociate")
454     @Consumes(MediaType.APPLICATION_JSON)
455     @Produces(MediaType.APPLICATION_JSON)
456     @Operation(description = "Create RI and associate RI to RI", method = "POST", summary = "Returns created RI and RelationshipInfo", responses = {
457         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
458         @ApiResponse(responseCode = "201", description = "RI created"), @ApiResponse(responseCode = "403", description = "Restricted operation"),
459         @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
460         @ApiResponse(responseCode = "409", description = "Relationship already exist")})
461     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
462     public Response createAndAssociateRIToRI(@PathParam("componentId") final String componentId,
463                                              @Parameter(description = "valid values: resources / services", schema = @Schema(allowableValues = {
464                                                  ComponentTypeEnum.RESOURCE_PARAM_NAME, ComponentTypeEnum.SERVICE_PARAM_NAME,
465                                                  ComponentTypeEnum.PRODUCT_PARAM_NAME})) @PathParam("containerComponentType") final String containerComponentType,
466                                              @Context final HttpServletRequest request) throws IOException {
467         String url = request.getMethod() + " " + request.getRequestURI();
468         log.debug(START_HANDLE_REQUEST_OF, url);
469         try {
470             log.debug(START_HANDLE_REQUEST_OF, url);
471             InputStream inputStream = request.getInputStream();
472             byte[] bytes = IOUtils.toByteArray(inputStream);
473             if (bytes == null || bytes.length == 0) {
474                 log.info(EMPTY_BODY_WAS_SENT);
475                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
476             }
477             String userId = request.getHeader(Constants.USER_ID_HEADER);
478             String data = new String(bytes);
479             if (componentInstanceBusinessLogic == null) {
480                 log.debug(UNSUPPORTED_COMPONENT_TYPE, containerComponentType);
481                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
482             }
483             Either<CreateAndAssotiateInfo, ActionStatus> convertStatus = convertJsonToObject(data, CreateAndAssotiateInfo.class);
484             if (convertStatus.isRight()) {
485                 BeEcompErrorManager.getInstance().logBeSystemError("Resource Instance - createAndAssociateRIToRI");
486                 log.debug(FAILED_TO_CONVERT_RECEIVED_DATA_TO_BE_FORMAT);
487                 Either<Object, ResponseFormat> formattedResponse = Either
488                     .right(getComponentsUtils().getResponseFormat(convertStatus.right().value()));
489                 return buildErrorResponse(formattedResponse.right().value());
490             }
491             CreateAndAssotiateInfo createAndAssotiateInfo = convertStatus.left().value();
492             RequirementCapabilityRelDef requirementDef = createAndAssotiateInfo.getAssociate();
493             requirementDef.setOriginUI(true);
494             Either<CreateAndAssotiateInfo, ResponseFormat> actionResponse = componentInstanceBusinessLogic
495                 .createAndAssociateRIToRI(containerComponentType, componentId, userId, createAndAssotiateInfo);
496             if (actionResponse.isRight()) {
497                 return buildErrorResponse(actionResponse.right().value());
498             }
499             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.CREATED), actionResponse.left().value());
500         } catch (Exception e) {
501             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Create and Associate Resource Instance");
502             log.debug("create and associate RI failed with exception", e);
503             throw e;
504         }
505     }
506
507     @POST
508     @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}/properties")
509     @Consumes(MediaType.APPLICATION_JSON)
510     @Produces(MediaType.APPLICATION_JSON)
511     @Operation(description = "Update resource instance property", method = "POST", summary = "Returns updated resource instance property", responses = {
512         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
513         @ApiResponse(responseCode = "201", description = "Resource instance created"),
514         @ApiResponse(responseCode = "403", description = "Restricted operation"),
515         @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
516     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
517     public Response updateResourceInstanceProperties(@Parameter(description = "service id") @PathParam("componentId") final String componentId,
518                                                      @Parameter(description = "valid values: resources / services", schema = @Schema(allowableValues = {
519                                                          ComponentTypeEnum.RESOURCE_PARAM_NAME,
520                                                          ComponentTypeEnum.SERVICE_PARAM_NAME})) @PathParam("containerComponentType") final String containerComponentType,
521                                                      @Parameter(description = "resource instance id") @PathParam("componentInstanceId") final String componentInstanceId,
522                                                      @Parameter(description = "id of user initiating the operation") @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
523                                                      @Context final HttpServletRequest request,
524                                                      @Parameter(description = "Component Instance Properties JSON Array", required = true) final String componentInstancePropertiesJsonArray) {
525         String url = request.getMethod() + " " + request.getRequestURI();
526         log.debug(START_HANDLE_REQUEST_OF, url);
527         loggerSupportability.log(LoggerSupportabilityActions.UPDATE_COMPONENT_INSTANCE, StatusCode.STARTED,
528             "Starting to update Resource Instance Properties for component {} ", componentId + " by " + userId);
529         Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
530         List<ComponentInstanceProperty> propertiesToUpdate = new ArrayList<>();
531         if (errorWrapper.isEmpty()) {
532             Either<List<ComponentInstanceProperty>, ResponseFormat> propertiesToUpdateEither = convertMultipleProperties(
533                 componentInstancePropertiesJsonArray);
534             if (propertiesToUpdateEither.isRight()) {
535                 errorWrapper.setInnerElement(propertiesToUpdateEither.right().value());
536             } else {
537                 propertiesToUpdate = propertiesToUpdateEither.left().value();
538             }
539         }
540         if (!errorWrapper.isEmpty()) {
541             return buildErrorResponse(errorWrapper.getInnerElement());
542         }
543         log.debug(START_HANDLE_REQUEST_OF_UPDATE_RESOURCE_INSTANCE_PROPERTY_RECEIVED_PROPERTY_IS, propertiesToUpdate);
544         ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
545         if (componentInstanceBusinessLogic == null) {
546             log.debug(UNSUPPORTED_COMPONENT_TYPE, containerComponentType);
547             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
548         }
549         Either<List<ComponentInstanceProperty>, ResponseFormat> actionResponse = componentInstanceBusinessLogic
550             .createOrUpdatePropertiesValues(componentTypeEnum, componentId, componentInstanceId, propertiesToUpdate, userId);
551         if (actionResponse.isRight()) {
552             return buildErrorResponse(actionResponse.right().value());
553         }
554         List<ComponentInstanceProperty> resourceInstanceProperties = actionResponse.left().value();
555         ObjectMapper mapper = new ObjectMapper();
556         String result;
557         loggerSupportability.log(LoggerSupportabilityActions.UPDATE_COMPONENT_INSTANCE, StatusCode.COMPLETE,
558             "Ended update Resource Instance Properties for component {} ", componentId + " by " + userId);
559         try {
560             result = mapper.writeValueAsString(resourceInstanceProperties);
561         } catch (JsonProcessingException e) {
562             log.error(UPDATE_RESOURCE_INSTANCE_WITH_EXCEPTION, e.getMessage(), e);
563             throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR);
564         }
565         loggerSupportability.log(LoggerSupportabilityActions.UPDATE_COMPONENT_INSTANCE, StatusCode.COMPLETE,
566             "Ended update Resource Instance Properties for component {} ", componentId + " by user " + userId);
567         return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result);
568     }
569
570     @POST
571     @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}/inputs")
572     @Consumes(MediaType.APPLICATION_JSON)
573     @Produces(MediaType.APPLICATION_JSON)
574     @Operation(description = "Update resource instance property", method = "POST", summary = "Returns updated resource instance property", responses = {
575         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
576         @ApiResponse(responseCode = "201", description = "Resource instance created"),
577         @ApiResponse(responseCode = "403", description = "Restricted operation"),
578         @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
579     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
580     public Response updateResourceInstanceInput(@Parameter(description = "service id") @PathParam("componentId") final String componentId,
581                                                 @Parameter(description = "valid values: resources / services", schema = @Schema(allowableValues = {
582                                                     ComponentTypeEnum.RESOURCE_PARAM_NAME,
583                                                     ComponentTypeEnum.SERVICE_PARAM_NAME})) @PathParam("containerComponentType") final String containerComponentType,
584                                                 @Parameter(description = "resource instance id") @PathParam("componentInstanceId") final String componentInstanceId,
585                                                 @Parameter(description = "id of user initiating the operation") @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
586                                                 @Context final HttpServletRequest request,
587                                                 @Parameter(description = "Component Instance Properties JSON Array", required = true) final String componentInstanceInputsJsonArray) {
588         String url = request.getMethod() + " " + request.getRequestURI();
589         log.debug(START_HANDLE_REQUEST_OF, url);
590         Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
591         List<ComponentInstanceInput> inputsToUpdate = new ArrayList<>();
592         if (errorWrapper.isEmpty()) {
593             Either<List<ComponentInstanceInput>, ResponseFormat> inputsToUpdateEither = convertMultipleInputs(componentInstanceInputsJsonArray);
594             if (inputsToUpdateEither.isRight()) {
595                 errorWrapper.setInnerElement(inputsToUpdateEither.right().value());
596             } else {
597                 inputsToUpdate = inputsToUpdateEither.left().value();
598             }
599         }
600         if (!errorWrapper.isEmpty()) {
601             return buildErrorResponse(errorWrapper.getInnerElement());
602         }
603         log.debug(START_HANDLE_REQUEST_OF_UPDATE_RESOURCE_INSTANCE_PROPERTY_RECEIVED_PROPERTY_IS, inputsToUpdate);
604         ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
605         if (componentInstanceBusinessLogic == null) {
606             log.debug(UNSUPPORTED_COMPONENT_TYPE, containerComponentType);
607             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
608         }
609         Either<List<ComponentInstanceInput>, ResponseFormat> actionResponse = componentInstanceBusinessLogic
610             .createOrUpdateInstanceInputValues(componentTypeEnum, componentId, componentInstanceId, inputsToUpdate, userId);
611         if (actionResponse.isRight()) {
612             return buildErrorResponse(actionResponse.right().value());
613         }
614         List<ComponentInstanceInput> resourceInstanceInput = actionResponse.left().value();
615         ObjectMapper mapper = new ObjectMapper();
616         mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
617         String result;
618         loggerSupportability
619             .log(LoggerSupportabilityActions.UPDATE_PROPERTIES, StatusCode.COMPLETE, "Ending update Resource Instance Input for component {} ",
620                 componentId + " by " + userId);
621         try {
622             result = mapper.writeValueAsString(resourceInstanceInput);
623         } catch (JsonProcessingException e) {
624             log.error(UPDATE_RESOURCE_INSTANCE_WITH_EXCEPTION, e.getMessage(), e);
625             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
626         }
627         loggerSupportability
628             .log(LoggerSupportabilityActions.UPDATE_PROPERTIES, StatusCode.COMPLETE, "Ending update Resource Instance Input for component {} ",
629                 componentId + " by user " + userId);
630         return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result);
631     }
632
633     /**
634      * Updates ResourceInstance Attribute
635      *
636      * @param componentId
637      * @param containerComponentType
638      * @param componentInstanceId
639      * @param userId
640      * @param request
641      * @return
642      */
643     @POST
644     @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}/attribute")
645     @Consumes(MediaType.APPLICATION_JSON)
646     @Produces(MediaType.APPLICATION_JSON)
647     @Operation(description = "Update resource instance attribute", method = "POST", summary = "Returns updated resource instance attribute", responses = {
648         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
649         @ApiResponse(responseCode = "201", description = "Resource instance created"),
650         @ApiResponse(responseCode = "403", description = "Restricted operation"),
651         @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
652     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
653     public Response updateResourceInstanceAttribute(@Parameter(description = "service id") @PathParam("componentId") final String componentId,
654                                                     @Parameter(description = "valid values: resources / services", schema = @Schema(allowableValues = {
655                                                         ComponentTypeEnum.RESOURCE_PARAM_NAME,
656                                                         ComponentTypeEnum.SERVICE_PARAM_NAME})) @PathParam("containerComponentType") final String containerComponentType,
657                                                     @Parameter(description = "resource instance id") @PathParam("componentInstanceId") final String componentInstanceId,
658                                                     @Parameter(description = "id of user initiating the operation") @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
659                                                     @Context final HttpServletRequest request) throws IOException {
660         String url = request.getMethod() + " " + request.getRequestURI();
661         log.debug(START_HANDLE_REQUEST_OF, url);
662         loggerSupportability
663             .log(LoggerSupportabilityActions.UPDATE_RESOURCE, StatusCode.STARTED, "Starting to update Resource Instance Attribute for component {} ",
664                 componentId + " by " + userId);
665         try {
666             Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
667             Wrapper<String> dataWrapper = new Wrapper<>();
668             Wrapper<ComponentInstanceProperty> attributeWrapper = new Wrapper<>();
669             Wrapper<ComponentInstanceBusinessLogic> blWrapper = new Wrapper<>();
670             validateInputStream(request, dataWrapper, errorWrapper);
671             if (errorWrapper.isEmpty()) {
672                 validateClassParse(dataWrapper.getInnerElement(), attributeWrapper, () -> ComponentInstanceProperty.class, errorWrapper);
673             }
674             if (errorWrapper.isEmpty()) {
675                 validateComponentInstanceBusinessLogic(request, containerComponentType, blWrapper, errorWrapper);
676             }
677             if (errorWrapper.isEmpty()) {
678                 ComponentInstanceBusinessLogic componentInstanceLogic = blWrapper.getInnerElement();
679                 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
680                 log.debug("Start handle request of ComponentInstanceAttribute. Received attribute is {}", attributeWrapper.getInnerElement());
681                 Either<ComponentInstanceProperty, ResponseFormat> eitherAttribute = componentInstanceLogic
682                     .createOrUpdateAttributeValue(componentTypeEnum, componentId, componentInstanceId, attributeWrapper.getInnerElement(), userId);
683                 if (eitherAttribute.isRight()) {
684                     errorWrapper.setInnerElement(eitherAttribute.right().value());
685                 } else {
686                     attributeWrapper.setInnerElement(eitherAttribute.left().value());
687                 }
688             }
689             loggerSupportability
690                 .log(LoggerSupportabilityActions.UPDATE_RESOURCE, StatusCode.COMPLETE, "Ended update Resource Instance Attribute for component {} ",
691                     componentId + " by " + userId);
692             return buildResponseFromElement(errorWrapper, attributeWrapper);
693         } catch (Exception e) {
694             log.error(CREATE_AND_ASSOCIATE_RI_FAILED_WITH_EXCEPTION, e.getMessage(), e);
695             throw e;
696         }
697     }
698
699     @DELETE
700     @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}/property/{propertyId}")
701     @Consumes(MediaType.APPLICATION_JSON)
702     @Produces(MediaType.APPLICATION_JSON)
703     @Operation(description = "Update resource instance", method = "DELETE", summary = "Returns deleted resource instance property", responses = {
704         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
705         @ApiResponse(responseCode = "201", description = "Resource instance created"),
706         @ApiResponse(responseCode = "403", description = "Restricted operation"),
707         @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
708     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
709     public Response deleteResourceInstanceProperty(@Parameter(description = "service id") @PathParam("componentId") final String componentId,
710                                                    @Parameter(description = "valid values: resources / services", schema = @Schema(allowableValues = {
711                                                        ComponentTypeEnum.RESOURCE_PARAM_NAME,
712                                                        ComponentTypeEnum.SERVICE_PARAM_NAME})) @PathParam("containerComponentType") final String containerComponentType,
713                                                    @Parameter(description = "resource instance id") @PathParam("componentInstanceId") final String componentInstanceId,
714                                                    @Parameter(description = "property id") @PathParam("propertyId") final String propertyId,
715                                                    @Parameter(description = "id of user initiating the operation") @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
716                                                    @Context final HttpServletRequest request) {
717         loggerSupportability
718             .log(LoggerSupportabilityActions.UPDATE_PROPERTIES, StatusCode.STARTED, "Starting to delete Resource Instance Property for component {} ",
719                 componentId + " by " + userId);
720         String url = request.getMethod() + " " + request.getRequestURI();
721         log.debug(START_HANDLE_REQUEST_OF, url);
722         try {
723             ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
724             loggerSupportability
725                 .log(LoggerSupportabilityActions.UPDATE_PROPERTIES, StatusCode.COMPLETE, "Ended delete Resource Instance Property for component {} ",
726                     componentId + " by " + userId);
727             if (componentInstanceBusinessLogic == null) {
728                 log.debug(UNSUPPORTED_COMPONENT_TYPE, containerComponentType);
729                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
730             }
731             Either<ComponentInstanceProperty, ResponseFormat> actionResponse = componentInstanceBusinessLogic
732                 .deletePropertyValue(componentTypeEnum, componentId, componentInstanceId, propertyId, userId);
733             if (actionResponse.isRight()) {
734                 return buildErrorResponse(actionResponse.right().value());
735             }
736             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.NO_CONTENT), null);
737         } catch (Exception e) {
738             log.error(CREATE_AND_ASSOCIATE_RI_FAILED_WITH_EXCEPTION, e.getMessage(), e);
739             throw e;
740         }
741     }
742
743     @POST
744     @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}/changeVersion")
745     @Consumes(MediaType.APPLICATION_JSON)
746     @Produces(MediaType.APPLICATION_JSON)
747     @Operation(description = "Update resource instance", method = "POST", summary = "Returns updated resource instance", responses = {
748         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
749         @ApiResponse(responseCode = "201", description = "Resource instance created"),
750         @ApiResponse(responseCode = "403", description = "Restricted operation"),
751         @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
752     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
753     public Response changeResourceInstanceVersion(@PathParam("componentId") final String componentId,
754                                                   @PathParam("componentInstanceId") final String componentInstanceId,
755                                                   @Parameter(description = "valid values: resources / services", schema = @Schema(allowableValues = {
756                                                       ComponentTypeEnum.RESOURCE_PARAM_NAME,
757                                                       ComponentTypeEnum.SERVICE_PARAM_NAME})) @PathParam("containerComponentType") final String containerComponentType,
758                                                   @Context final HttpServletRequest request) throws IOException {
759         String url = request.getMethod() + " " + request.getRequestURI();
760         log.debug(START_HANDLE_REQUEST_OF, url);
761         try (InputStream inputStream = request.getInputStream()) {
762             byte[] bytes = IOUtils.toByteArray(inputStream);
763             if (bytes == null || bytes.length == 0) {
764                 log.info(EMPTY_BODY_WAS_SENT);
765                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
766             }
767             String userId = request.getHeader(Constants.USER_ID_HEADER);
768             String data = new String(bytes);
769             if (componentInstanceBusinessLogic == null) {
770                 log.debug(UNSUPPORTED_COMPONENT_TYPE, containerComponentType);
771                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
772             }
773             Either<ComponentInstance, ResponseFormat> convertResponse = convertToResourceInstance(data);
774             if (convertResponse.isRight()) {
775                 BeEcompErrorManager.getInstance().logBeSystemError(RESOURCE_INSTANCE_UPDATE_RESOURCE_INSTANCE);
776                 log.debug(FAILED_TO_CONVERT_RECEIVED_DATA_TO_BE_FORMAT);
777                 return buildErrorResponse(convertResponse.right().value());
778             }
779             ComponentInstance newResourceInstance = convertResponse.left().value();
780             ComponentInstance actionResponse = componentInstanceBusinessLogic
781                 .changeComponentInstanceVersion(containerComponentType, componentId, componentInstanceId, userId, newResourceInstance);
782             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse);
783         } catch (Exception e) {
784             BeEcompErrorManager.getInstance().logBeRestApiGeneralError(UPDATE_RESOURCE_INSTANCE);
785             log.debug(UPDATE_RESOURCE_INSTANCE_WITH_EXCEPTION, e);
786             throw e;
787         }
788     }
789
790     @POST
791     @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}/groupInstance/{groupInstanceId}/property")
792     @Consumes(MediaType.APPLICATION_JSON)
793     @Produces(MediaType.APPLICATION_JSON)
794     @Operation(description = "Update resource instance property", method = "POST", summary = "Returns updated resource instance property", responses = {
795         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
796         @ApiResponse(responseCode = "201", description = "Resource instance created"),
797         @ApiResponse(responseCode = "403", description = "Restricted operation"),
798         @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
799     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
800     public Response updateGroupInstanceProperty(@Parameter(description = "service id") @PathParam("componentId") final String componentId,
801                                                 @Parameter(description = "valid values: resources / services", schema = @Schema(allowableValues = {
802                                                     ComponentTypeEnum.RESOURCE_PARAM_NAME,
803                                                     ComponentTypeEnum.SERVICE_PARAM_NAME})) @PathParam("containerComponentType") final String containerComponentType,
804                                                 @Parameter(description = "resource instance id") @PathParam("componentInstanceId") final String componentInstanceId,
805                                                 @Parameter(description = "group instance id") @PathParam("groupInstanceId") final String groupInstanceId,
806                                                 @Parameter(description = "id of user initiating the operation") @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
807                                                 @Context final HttpServletRequest request) throws IOException {
808         String url = request.getMethod() + " " + request.getRequestURI();
809         log.debug(START_HANDLE_REQUEST_OF, url);
810         try {
811             loggerSupportability
812                 .log(LoggerSupportabilityActions.UPDATE_PROPERTIES, StatusCode.STARTED, "Starting update Group Instance Property for component {} ",
813                     componentId + " by " + userId);
814             Wrapper<String> dataWrapper = new Wrapper<>();
815             Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
816             Wrapper<ComponentInstanceProperty> propertyWrapper = new Wrapper<>();
817             validateInputStream(request, dataWrapper, errorWrapper);
818             if (errorWrapper.isEmpty()) {
819                 validateClassParse(dataWrapper.getInnerElement(), propertyWrapper, () -> ComponentInstanceProperty.class, errorWrapper);
820             }
821             if (!errorWrapper.isEmpty()) {
822                 return buildErrorResponse(errorWrapper.getInnerElement());
823             }
824             ComponentInstanceProperty property = propertyWrapper.getInnerElement();
825             log.debug(START_HANDLE_REQUEST_OF_UPDATE_RESOURCE_INSTANCE_PROPERTY_RECEIVED_PROPERTY_IS, property);
826             ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
827             if (componentInstanceBusinessLogic == null) {
828                 log.debug(UNSUPPORTED_COMPONENT_TYPE, containerComponentType);
829                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
830             }
831             Either<ComponentInstanceProperty, ResponseFormat> actionResponse = componentInstanceBusinessLogic
832                 .createOrUpdateGroupInstancePropertyValue(componentTypeEnum, componentId, componentInstanceId, groupInstanceId, property, userId);
833             if (actionResponse.isRight()) {
834                 return buildErrorResponse(actionResponse.right().value());
835             }
836             ComponentInstanceProperty resourceInstanceProperty = actionResponse.left().value();
837             ObjectMapper mapper = new ObjectMapper();
838             String result = mapper.writeValueAsString(resourceInstanceProperty);
839             loggerSupportability
840                 .log(LoggerSupportabilityActions.UPDATE_PROPERTIES, StatusCode.COMPLETE, "Ended update Group Instance Property for component {} ",
841                     componentId + " by " + userId);
842             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result);
843         } catch (Exception e) {
844             log.error(CREATE_AND_ASSOCIATE_RI_FAILED_WITH_EXCEPTION, e.getMessage(), e);
845             throw e;
846         }
847     }
848
849     @GET
850     @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}/groupInstance/{groupInstId}")
851     @Consumes(MediaType.APPLICATION_JSON)
852     @Produces(MediaType.APPLICATION_JSON)
853     @Operation(description = "Get group artifacts ", method = "GET", summary = "Returns artifacts metadata according to groupInstId", responses = {
854         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
855         @ApiResponse(responseCode = "200", description = "group found"), @ApiResponse(responseCode = "403", description = "Restricted operation"),
856         @ApiResponse(responseCode = "404", description = "Group not found")})
857     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
858     public Response getGroupArtifactById(@PathParam("containerComponentType") final String containerComponentType,
859                                          @PathParam("componentId") final String componentId,
860                                          @PathParam("componentInstanceId") final String componentInstanceId,
861                                          @PathParam("groupInstId") final String groupInstId, @Context final HttpServletRequest request,
862                                          @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
863         String url = request.getMethod() + " " + request.getRequestURI();
864         log.debug(GET_START_HANDLE_REQUEST_OF, url);
865         try {
866             ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
867             Either<GroupDefinitionInfo, ResponseFormat> actionResponse = groupBL
868                 .getGroupInstWithArtifactsById(componentTypeEnum, componentId, componentInstanceId, groupInstId, userId, false);
869             if (actionResponse.isRight()) {
870                 log.debug("failed to get all non abstract {}", containerComponentType);
871                 return buildErrorResponse(actionResponse.right().value());
872             }
873             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
874         } catch (Exception e) {
875             BeEcompErrorManager.getInstance().logBeRestApiGeneralError(GET_GROUP_ARTIFACT_BY_ID);
876             log.debug(GET_GROUP_ARTIFACT_BY_ID_UNEXPECTED_EXCEPTION, e);
877             throw e;
878         }
879     }
880
881     // US831698
882     @GET
883     @Path("/{containerComponentType}/{containerComponentId}/componentInstances/{componentInstanceUniqueId}/properties")
884     @Consumes(MediaType.APPLICATION_JSON)
885     @Produces(MediaType.APPLICATION_JSON)
886     @Operation(description = "Get component instance properties", method = "GET", summary = "Returns component instance properties", responses = {
887         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
888         @ApiResponse(responseCode = "200", description = "Properties found"),
889         @ApiResponse(responseCode = "403", description = "Restricted operation"),
890         @ApiResponse(responseCode = "404", description = "Component/Component Instance - not found")})
891     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
892     public Response getInstancePropertiesById(@PathParam("containerComponentType") final String containerComponentType,
893                                               @PathParam("containerComponentId") final String containerComponentId,
894                                               @PathParam("componentInstanceUniqueId") final String componentInstanceUniqueId,
895                                               @Context final HttpServletRequest request,
896                                               @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
897         String url = request.getMethod() + " " + request.getRequestURI();
898         log.debug(GET_START_HANDLE_REQUEST_OF, url);
899         List<ComponentInstanceProperty> componentInstancePropertiesById = componentInstanceBusinessLogic
900             .getComponentInstancePropertiesById(containerComponentType, containerComponentId, componentInstanceUniqueId, userId);
901         return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), componentInstancePropertiesById);
902     }
903
904     @GET
905     @Path("/{containerComponentType}/{containerComponentId}/componentInstances/{componentInstanceUniqueId}/attributes")
906     @Consumes(MediaType.APPLICATION_JSON)
907     @Produces(MediaType.APPLICATION_JSON)
908     @Operation(description = "Get component instance attributes", method = "GET", summary = "Returns component instance attributes", responses = {
909         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
910         @ApiResponse(responseCode = "200", description = "Attributes found"),
911         @ApiResponse(responseCode = "403", description = "Restricted operation"),
912         @ApiResponse(responseCode = "404", description = "Component/Component Instance - not found")})
913     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
914     public Response getInstanceAttributesById(@PathParam("containerComponentType") final String containerComponentType,
915                                               @PathParam("containerComponentId") final String containerComponentId,
916                                               @PathParam("componentInstanceUniqueId") final String componentInstanceUniqueId,
917                                               @Context final HttpServletRequest request,
918                                               @HeaderParam(value = Constants.USER_ID_HEADER) final String userId) {
919         final String url = request.getMethod() + " " + request.getRequestURI();
920         log.debug(GET_START_HANDLE_REQUEST_OF, url);
921         final List<ComponentInstanceAttribute> componentInstanceAttributesById = componentInstanceBusinessLogic
922             .getComponentInstanceAttributesById(containerComponentType, containerComponentId, componentInstanceUniqueId, userId);
923         return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), componentInstanceAttributesById);
924     }
925
926     // US330353
927     @GET
928     @Path("/{containerComponentType}/{containerComponentId}/componentInstances/{componentInstanceUniqueId}/capability/{capabilityType}/capabilityName/{capabilityName}/ownerId/{ownerId}/properties")
929     @Consumes(MediaType.APPLICATION_JSON)
930     @Produces(MediaType.APPLICATION_JSON)
931     @Operation(description = "Get component instance capability properties", method = "GET", summary = "Returns component instance capability properties", responses = {
932         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
933         @ApiResponse(responseCode = "200", description = "Properties found"),
934         @ApiResponse(responseCode = "403", description = "Restricted operation"),
935         @ApiResponse(responseCode = "404", description = "Component/Component Instance/Capability - not found")})
936     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
937     public Response getInstanceCapabilityPropertiesById(@PathParam("containerComponentType") final String containerComponentType,
938                                                         @PathParam("containerComponentId") final String containerComponentId,
939                                                         @PathParam("componentInstanceUniqueId") final String componentInstanceUniqueId,
940                                                         @PathParam("capabilityType") final String capabilityType,
941                                                         @PathParam("capabilityName") final String capabilityName,
942                                                         @PathParam("ownerId") final String ownerId, @Context final HttpServletRequest request,
943                                                         @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
944         String url = request.getMethod() + " " + request.getRequestURI();
945         log.debug(GET_START_HANDLE_REQUEST_OF, url);
946         try {
947             List<ComponentInstanceProperty> componentInstancePropertiesById = componentInstanceBusinessLogic
948                 .getComponentInstanceCapabilityPropertiesById(containerComponentType, containerComponentId, componentInstanceUniqueId, capabilityType,
949                     capabilityName, ownerId, userId);
950             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), componentInstancePropertiesById);
951         } catch (Exception e) {
952             BeEcompErrorManager.getInstance().logBeRestApiGeneralError(GET_GROUP_ARTIFACT_BY_ID);
953             log.debug(GET_GROUP_ARTIFACT_BY_ID_UNEXPECTED_EXCEPTION, e);
954             throw e;
955         }
956     }
957
958     //US 331281
959     @PUT
960     @Path("/{containerComponentType}/{containerComponentId}/componentInstances/{componentInstanceUniqueId}/capability/{capabilityType}/capabilityName/{capabilityName}/ownerId/{ownerId}/properties")
961     @Consumes(MediaType.APPLICATION_JSON)
962     @Produces(MediaType.APPLICATION_JSON)
963     @Operation(description = "Update Instance Capabilty  Property", method = "PUT", summary = "Returns updated property", responses = {
964         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
965         @ApiResponse(responseCode = "200", description = "Resource instance capabilty property updated"),
966         @ApiResponse(responseCode = "403", description = "Restricted operation"),
967         @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
968         @ApiResponse(responseCode = "404", description = "Component/Component Instance/Capability - not found")})
969     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
970     public Response updateInstanceCapabilityProperty(@PathParam("containerComponentType") final String containerComponentType,
971                                                      @PathParam("containerComponentId") final String containerComponentId,
972                                                      @PathParam("componentInstanceUniqueId") final String componentInstanceUniqueId,
973                                                      @PathParam("capabilityType") final String capabilityType,
974                                                      @PathParam("capabilityName") final String capabilityName,
975                                                      @PathParam("ownerId") final String ownerId,
976                                                      @Parameter(description = "Instance capabilty property to update", required = true) String data,
977                                                      @Context final HttpServletRequest request,
978                                                      @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
979         String url = request.getMethod() + " " + request.getRequestURI();
980         log.debug("(PUT) Start handle request of {}", url);
981         loggerSupportability.log(LoggerSupportabilityActions.UPDATE_INSTANCE_CAPABILITY_PROPERTY, StatusCode.STARTED,
982             " Starting to update Instance Capability Property for component instance {} ", componentInstanceUniqueId + " by " + userId);
983         try {
984             Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
985             List<ComponentInstanceProperty> propertiesToUpdate = new ArrayList<>();
986             if (errorWrapper.isEmpty()) {
987                 Either<List<ComponentInstanceProperty>, ResponseFormat> propertiesToUpdateEither = convertMultipleProperties(data);
988                 if (propertiesToUpdateEither.isRight()) {
989                     errorWrapper.setInnerElement(propertiesToUpdateEither.right().value());
990                 } else {
991                     propertiesToUpdate = propertiesToUpdateEither.left().value();
992                 }
993             }
994             if (!errorWrapper.isEmpty()) {
995                 return buildErrorResponse(errorWrapper.getInnerElement());
996             }
997             ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
998             Either<List<ComponentInstanceProperty>, ResponseFormat> updateCICapProperty = componentInstanceBusinessLogic
999                 .updateInstanceCapabilityProperties(componentTypeEnum, containerComponentId, componentInstanceUniqueId, capabilityType,
1000                     capabilityName, propertiesToUpdate, userId);
1001             if (updateCICapProperty.isRight()) {
1002                 log.debug(FAILED_TO_GET_PROPERTIES_OF_COMPONENT_INSTANCE_ID_IN_WITH_ID, componentInstanceUniqueId, containerComponentType,
1003                     containerComponentId);
1004                 return buildErrorResponse(updateCICapProperty.right().value());
1005             }
1006             loggerSupportability.log(LoggerSupportabilityActions.UPDATE_INSTANCE_CAPABILITY_PROPERTY, StatusCode.COMPLETE,
1007                 " Ended to update Instance Capability Property for component instance {} ", componentInstanceUniqueId + " by " + userId);
1008             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), updateCICapProperty.left().value());
1009         } catch (Exception e) {
1010             BeEcompErrorManager.getInstance().logBeRestApiGeneralError(GET_GROUP_ARTIFACT_BY_ID);
1011             log.debug(GET_GROUP_ARTIFACT_BY_ID_UNEXPECTED_EXCEPTION, e);
1012             throw e;
1013         }
1014     }
1015
1016     @PUT
1017     @Path("/{containerComponentType}/{containerComponentId}/componentInstances/{componentInstanceUniqueId}/requirement/{capabilityType}/requirementName/{requirementName}")
1018     @Consumes(MediaType.APPLICATION_JSON)
1019     @Produces(MediaType.APPLICATION_JSON)
1020     @Operation(description = "Update Instance Requirement", method = "PUT", summary = "Returns updated requirement", responses = {
1021         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
1022         @ApiResponse(responseCode = "200", description = "Resource instance requirement updated"),
1023         @ApiResponse(responseCode = "403", description = "Restricted operation"),
1024         @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
1025         @ApiResponse(responseCode = "404", description = "Component/Component Instance/Requirement - not found")})
1026     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
1027     public Response updateInstanceRequirement(@PathParam("containerComponentType") final String containerComponentType,
1028                                               @PathParam("containerComponentId") final String containerComponentId,
1029                                               @PathParam("componentInstanceUniqueId") final String componentInstanceUniqueId,
1030                                               @PathParam("capabilityType") final String capabilityType,
1031                                               @PathParam("requirementName") final String requirementName,
1032                                               @Parameter(description = "Instance capabilty requirement to update", required = true) String data,
1033                                               @Context final HttpServletRequest request,
1034                                               @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
1035         String url = request.getMethod() + " " + request.getRequestURI();
1036         log.debug(START_HANDLE_REQUEST_OF, url);
1037         loggerSupportability.log(LoggerSupportabilityActions.UPDATE_INSTANCE_REQUIREMENT, StatusCode.STARTED,
1038             "Starting to update requirement {} in component instance {} by {}", requirementName, componentInstanceUniqueId, userId);
1039         try {
1040             log.debug(START_HANDLE_REQUEST_OF, url);
1041             ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
1042             if (componentInstanceBusinessLogic == null) {
1043                 log.debug(UNSUPPORTED_COMPONENT_TYPE, containerComponentType);
1044                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
1045             }
1046             Either<RequirementDefinition, ResponseFormat> mappedRequirementDataEither = getMappedRequirementData(data, new User(userId),
1047                 componentTypeEnum);
1048             if (mappedRequirementDataEither.isRight()) {
1049                 log.debug("Failed to update requirements");
1050                 return buildErrorResponse(mappedRequirementDataEither.right().value());
1051             }
1052             RequirementDefinition requirementDef = mappedRequirementDataEither.left().value();
1053             Either<RequirementDefinition, ResponseFormat> response = componentInstanceBusinessLogic
1054                 .updateInstanceRequirement(componentTypeEnum, containerComponentId, componentInstanceUniqueId, requirementDef, userId);
1055             if (response.isRight()) {
1056                 return buildErrorResponse(response.right().value());
1057             }
1058             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), response.left().value());
1059         } catch (ComponentException e) {
1060             throw e;
1061         } catch (Exception e) {
1062             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Update component instance requirement");
1063             log.debug("Update component instance requirement with exception", e);
1064             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
1065         }
1066     }
1067
1068     private Either<RequirementDefinition, ResponseFormat> getMappedRequirementData(String inputJson, User user, ComponentTypeEnum componentTypeEnum) {
1069         return getComponentsUtils()
1070             .convertJsonToObjectUsingObjectMapper(inputJson, user, RequirementDefinition.class, AuditingActionEnum.GET_TOSCA_MODEL,
1071                 componentTypeEnum);
1072     }
1073
1074     @POST
1075     @Path("/{containerComponentType}/{containerComponentId}/serviceProxy")
1076     @Consumes(MediaType.APPLICATION_JSON)
1077     @Produces(MediaType.APPLICATION_JSON)
1078     @Operation(description = "Create service proxy", method = "POST", summary = "Returns created service proxy", responses = {
1079         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
1080         @ApiResponse(responseCode = "201", description = "Service proxy created"),
1081         @ApiResponse(responseCode = "403", description = "Restricted operation"),
1082         @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
1083         @ApiResponse(responseCode = "409", description = "Service proxy already exist")})
1084     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
1085     public Response createServiceProxy(@Parameter(description = "RI object to be created", required = true) String data,
1086                                        @PathParam("containerComponentId") final String containerComponentId,
1087                                        @Parameter(description = "valid values: resources / services", schema = @Schema(allowableValues = {
1088                                            ComponentTypeEnum.RESOURCE_PARAM_NAME,
1089                                            ComponentTypeEnum.SERVICE_PARAM_NAME})) @PathParam("containerComponentType") final String containerComponentType,
1090                                        @HeaderParam(value = Constants.USER_ID_HEADER) @Parameter(description = "USER_ID of modifier user", required = true) String userId,
1091                                        @Context final HttpServletRequest request) {
1092         try {
1093             ComponentInstance componentInstance = RepresentationUtils.fromRepresentation(data, ComponentInstance.class);
1094             componentInstance.setInvariantName(null);
1095             ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
1096             if (componentTypeEnum != ComponentTypeEnum.SERVICE) {
1097                 log.debug("Unsupported container component type {}", containerComponentType);
1098                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
1099             }
1100             if (componentInstanceBusinessLogic == null) {
1101                 log.debug(UNSUPPORTED_COMPONENT_TYPE, containerComponentType);
1102                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
1103             }
1104             Either<ComponentInstance, ResponseFormat> actionResponse = componentInstanceBusinessLogic.createServiceProxy();
1105             if (actionResponse.isRight()) {
1106                 return buildErrorResponse(actionResponse.right().value());
1107             }
1108             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.CREATED), actionResponse.left().value());
1109         } catch (Exception e) {
1110             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Create service proxy");
1111             log.debug("Create service proxy failed with exception", e);
1112             throw e;
1113         }
1114     }
1115
1116     @DELETE
1117     @Path("/{containerComponentType}/{containerComponentId}/serviceProxy/{serviceProxyId}")
1118     @Consumes(MediaType.APPLICATION_JSON)
1119     @Produces(MediaType.APPLICATION_JSON)
1120     @Operation(description = "Delete service proxy", method = "DELETE", summary = "Returns delete service proxy", responses = {
1121         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
1122         @ApiResponse(responseCode = "201", description = "Service proxy deleted"),
1123         @ApiResponse(responseCode = "403", description = "Restricted operation"),
1124         @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
1125     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
1126     public Response deleteServiceProxy(@PathParam("containerComponentId") final String containerComponentId,
1127                                        @PathParam("serviceProxyId") final String serviceProxyId,
1128                                        @Parameter(description = "valid values: resources / services / products", schema = @Schema(allowableValues = {
1129                                            ComponentTypeEnum.RESOURCE_PARAM_NAME,
1130                                            ComponentTypeEnum.SERVICE_PARAM_NAME})) @PathParam("containerComponentType") final String containerComponentType,
1131                                        @Context final HttpServletRequest request) {
1132         String url = request.getMethod() + " " + request.getRequestURI();
1133         try {
1134             log.debug(START_HANDLE_REQUEST_OF, url);
1135             if (componentInstanceBusinessLogic == null) {
1136                 log.debug(UNSUPPORTED_COMPONENT_TYPE, containerComponentType);
1137                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
1138             }
1139             Either<ComponentInstance, ResponseFormat> actionResponse = componentInstanceBusinessLogic.deleteServiceProxy();
1140             if (actionResponse.isRight()) {
1141                 return buildErrorResponse(actionResponse.right().value());
1142             } else {
1143                 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.NO_CONTENT), null);
1144             }
1145         } catch (Exception e) {
1146             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Delete service proxy");
1147             log.debug("Delete service proxy failed with exception", e);
1148             throw e;
1149         }
1150     }
1151
1152     @POST
1153     @Path("/{containerComponentType}/{containerComponentId}/serviceProxy/{serviceProxyId}/changeVersion/{newServiceId}")
1154     @Consumes(MediaType.APPLICATION_JSON)
1155     @Produces(MediaType.APPLICATION_JSON)
1156     @Operation(description = "Update service proxy with new version", method = "POST", summary = "Returns updated service proxy", responses = {
1157         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
1158         @ApiResponse(responseCode = "201", description = "Service proxy created"),
1159         @ApiResponse(responseCode = "403", description = "Restricted operation"),
1160         @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
1161     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
1162     public Response changeServiceProxyVersion(@PathParam("containerComponentId") final String containerComponentId,
1163                                               @PathParam("serviceProxyId") final String serviceProxyId,
1164                                               @Parameter(description = "valid values: resources / services", schema = @Schema(allowableValues = {
1165                                                   ComponentTypeEnum.RESOURCE_PARAM_NAME,
1166                                                   ComponentTypeEnum.SERVICE_PARAM_NAME})) @PathParam("containerComponentType") final String containerComponentType,
1167                                               @Context final HttpServletRequest request) {
1168         String url = request.getMethod() + " " + request.getRequestURI();
1169         log.debug(START_HANDLE_REQUEST_OF, url);
1170         try {
1171             if (componentInstanceBusinessLogic == null) {
1172                 log.debug(UNSUPPORTED_COMPONENT_TYPE, containerComponentType);
1173                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
1174             }
1175             Either<ComponentInstance, ResponseFormat> actionResponse = componentInstanceBusinessLogic.changeServiceProxyVersion();
1176             if (actionResponse.isRight()) {
1177                 return buildErrorResponse(actionResponse.right().value());
1178             }
1179             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
1180         } catch (Exception e) {
1181             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Update service proxy with new version");
1182             log.debug("Update service proxy with new version failed with exception", e);
1183             throw e;
1184         }
1185     }
1186
1187     /**
1188      * REST API GET relation by Id Allows to get relation contained in specified component according to received Id
1189      *
1190      * @param containerComponentType
1191      * @param componentId
1192      * @param relationId
1193      * @param request
1194      * @param userId
1195      * @return Response
1196      */
1197     @GET
1198     @Path("/{containerComponentType}/{componentId}/relationId/{relationId}")
1199     @Consumes(MediaType.APPLICATION_JSON)
1200     @Produces(MediaType.APPLICATION_JSON)
1201     @Operation(description = "Get relation", method = "GET", summary = "Returns relation metadata according to relationId", responses = {
1202         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
1203         @ApiResponse(responseCode = "200", description = "relation found"), @ApiResponse(responseCode = "403", description = "Restricted operation"),
1204         @ApiResponse(responseCode = "404", description = "Relation not found")})
1205     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
1206     public Response getRelationById(@PathParam("containerComponentType") final String containerComponentType,
1207                                     @PathParam("componentId") final String componentId, @PathParam("relationId") final String relationId,
1208                                     @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
1209         String url = request.getMethod() + " " + request.getRequestURI();
1210         log.debug(GET_START_HANDLE_REQUEST_OF, url);
1211         try {
1212             ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
1213             if (componentTypeEnum == null) {
1214                 log.debug(UNSUPPORTED_COMPONENT_TYPE, containerComponentType);
1215                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
1216             }
1217             Either<RequirementCapabilityRelDef, ResponseFormat> actionResponse = componentInstanceBusinessLogic
1218                 .getRelationById(componentId, relationId, userId, componentTypeEnum);
1219             if (actionResponse.isRight()) {
1220                 return buildErrorResponse(actionResponse.right().value());
1221             }
1222             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
1223         } catch (Exception e) {
1224             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("getRelationById");
1225             log.debug("getRelationById unexpected exception", e);
1226             throw e;
1227         }
1228     }
1229
1230     private Either<ComponentInstance, ResponseFormat> convertToResourceInstance(String data) {
1231         Either<ComponentInstance, ResponseFormat> convertStatus = getComponentsUtils()
1232             .convertJsonToObjectUsingObjectMapper(data, new User(), ComponentInstance.class, null, ComponentTypeEnum.RESOURCE_INSTANCE);
1233         if (convertStatus.isRight()) {
1234             return Either.right(convertStatus.right().value());
1235         }
1236         ComponentInstance resourceInstanceInfo = convertStatus.left().value();
1237         return Either.left(resourceInstanceInfo);
1238     }
1239
1240     private Either<List<ComponentInstance>, ResponseFormat> convertToMultipleResourceInstance(String dataList) {
1241         Either<ComponentInstance[], ResponseFormat> convertStatus = getComponentsUtils()
1242             .convertJsonToObjectUsingObjectMapper(dataList, new User(), ComponentInstance[].class, null, ComponentTypeEnum.RESOURCE_INSTANCE);
1243         if (convertStatus.isRight()) {
1244             return Either.right(convertStatus.right().value());
1245         }
1246         return Either.left(Arrays.asList(convertStatus.left().value()));
1247     }
1248
1249     private Either<List<ComponentInstanceProperty>, ResponseFormat> convertMultipleProperties(String dataList) {
1250         if (StringUtils.isEmpty(dataList)) {
1251             return Either.right(getComponentsUtils().getResponseFormat(ActionStatus.MISSING_BODY));
1252         }
1253         Either<ComponentInstanceProperty[], ResponseFormat> convertStatus = getComponentsUtils()
1254             .convertJsonToObjectUsingObjectMapper(dataList, new User(), ComponentInstanceProperty[].class, null, ComponentTypeEnum.RESOURCE_INSTANCE);
1255         if (convertStatus.isRight()) {
1256             return Either.right(convertStatus.right().value());
1257         }
1258         return Either.left(Arrays.asList(convertStatus.left().value()));
1259     }
1260
1261     private Either<List<ComponentInstanceInput>, ResponseFormat> convertMultipleInputs(String dataList) {
1262         if (StringUtils.isEmpty(dataList)) {
1263             return Either.right(getComponentsUtils().getResponseFormat(ActionStatus.MISSING_BODY));
1264         }
1265         Either<ComponentInstanceInput[], ResponseFormat> convertStatus = getComponentsUtils()
1266             .convertJsonToObjectUsingObjectMapper(dataList, new User(), ComponentInstanceInput[].class, null, ComponentTypeEnum.RESOURCE_INSTANCE);
1267         if (convertStatus.isRight()) {
1268             return Either.right(convertStatus.right().value());
1269         }
1270         return Either.left(Arrays.asList(convertStatus.left().value()));
1271     }
1272
1273     private RequirementCapabilityRelDef convertToRequirementCapabilityRelDef(String data) {
1274         Either<RequirementCapabilityRelDef, ActionStatus> convertStatus = convertJsonToObject(data, RequirementCapabilityRelDef.class);
1275         if (convertStatus.isRight()) {
1276             throw new ByActionStatusComponentException(convertStatus.right().value());
1277         }
1278         return convertStatus.left().value();
1279     }
1280
1281     @Override
1282     public <T> Either<T, ActionStatus> convertJsonToObject(String data, Class<T> clazz) {
1283         try {
1284             log.trace("convert json to object. json=\n {}", data);
1285             T t;
1286             t = gsonDeserializer.fromJson(data, clazz);
1287             if (t == null) {
1288                 BeEcompErrorManager.getInstance().logBeInvalidJsonInput("convertJsonToObject");
1289                 log.debug("object is null after converting from json");
1290                 return Either.right(ActionStatus.INVALID_CONTENT);
1291             }
1292             return Either.left(t);
1293         } catch (Exception e) {
1294             // INVALID JSON
1295             BeEcompErrorManager.getInstance().logBeInvalidJsonInput("convertJsonToObject");
1296             log.debug("failed to convert from json", e);
1297             return Either.right(ActionStatus.INVALID_CONTENT);
1298         }
1299     }
1300
1301     @GET
1302     @Path("/{containerComponentType}/{componentId}/paths-to-delete")
1303     @Produces(MediaType.APPLICATION_JSON)
1304     @Operation(description = "Check if forwarding path to delete on version change", method = "GET", summary = "Returns forwarding paths to delete", responses = {
1305         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class))))})
1306     public Response changeResourceInstanceVersion(@PathParam("componentId") String componentId,
1307                                                   @QueryParam("componentInstanceId") final String oldComponentInstanceId,
1308                                                   @QueryParam("newComponentInstanceId") final String newComponentInstanceId,
1309                                                   @Parameter(description = "valid values: resources / services", schema = @Schema(allowableValues = {
1310                                                       ComponentTypeEnum.RESOURCE_PARAM_NAME,
1311                                                       ComponentTypeEnum.SERVICE_PARAM_NAME})) @PathParam("containerComponentType") final String containerComponentType,
1312                                                   @Context final HttpServletRequest request) {
1313         if (oldComponentInstanceId == null) {
1314             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.MISSING_OLD_COMPONENT_INSTANCE));
1315         }
1316         if (newComponentInstanceId == null) {
1317             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.MISSING_NEW_COMPONENT_INSTANCE));
1318         }
1319         String url = request.getMethod() + " " + request.getRequestURI();
1320         log.debug(START_HANDLE_REQUEST_OF, url);
1321         if (componentInstanceBusinessLogic == null) {
1322             log.debug(UNSUPPORTED_COMPONENT_TYPE, containerComponentType);
1323             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
1324         }
1325         ComponentInstance newComponentInstance;
1326         if (StringUtils.isNotEmpty(newComponentInstanceId)) {
1327             newComponentInstance = new ComponentInstance();
1328             newComponentInstance.setToscaPresentationValue(JsonPresentationFields.CI_COMPONENT_UID, newComponentInstanceId);
1329         } else {
1330             log.error("missing component id");
1331             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.MISSING_DATA));
1332         }
1333         Either<Set<String>, ResponseFormat> actionResponse = componentInstanceBusinessLogic
1334             .forwardingPathOnVersionChange(containerComponentType, componentId, oldComponentInstanceId, newComponentInstance);
1335         if (actionResponse.isRight()) {
1336             return buildErrorResponse(actionResponse.right().value());
1337         }
1338         ForwardingPaths forwardingPaths = new ForwardingPaths();
1339         forwardingPaths.setForwardingPathToDelete(actionResponse.left().value());
1340         return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), forwardingPaths);
1341     }
1342
1343     @POST
1344     @Path("/services/{componentId}/copyComponentInstance/{componentInstanceId}")
1345     @Consumes(MediaType.APPLICATION_JSON)
1346     @Produces((MediaType.APPLICATION_JSON))
1347     @Operation(description = "Copy Component Instance", method = "POST", summary = "Returns updated service information", responses = {
1348         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Service.class)))),
1349         @ApiResponse(responseCode = "201", description = "Copy and Paste Success"),
1350         @ApiResponse(responseCode = "403", description = "Restricted Operation"),
1351         @ApiResponse(responseCode = "400", description = "Invalid Content / Missing content")})
1352     public Response copyComponentInstance(
1353         @Parameter(description = "service unique id in pasted canvas") @PathParam("componentId") final String containerComponentId,
1354         @Parameter(description = "Data for copying", required = true) String data, @PathParam("componentInstanceId") final String componentInstanceId,
1355         @Context final HttpServletRequest request) {
1356         log.info("Start to copy component instance");
1357         String userId = request.getHeader(Constants.USER_ID_HEADER);
1358         final String CNTAINER_CMPT_TYPE = SERVICES;
1359         try {
1360             ComponentInstance inputComponentInstance = RepresentationUtils.fromRepresentation(data, ComponentInstance.class);
1361             inputComponentInstance.setInvariantName(null);
1362             ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(CNTAINER_CMPT_TYPE);
1363             if (componentInstanceBusinessLogic == null) {
1364                 log.debug(UNSUPPORTED_COMPONENT_TYPE, componentTypeEnum);
1365                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, SERVICES));
1366             }
1367             Either<Map<String, ComponentInstance>, ResponseFormat> copyComponentInstance = componentInstanceBusinessLogic
1368                 .copyComponentInstance(inputComponentInstance, containerComponentId, componentInstanceId, userId);
1369             if (copyComponentInstance.isRight()) {
1370                 log.error("Failed to copy ComponentInstance {}", copyComponentInstance.right().value());
1371                 return buildErrorResponse(copyComponentInstance.right().value());
1372             }
1373             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), copyComponentInstance.left().value());
1374         } catch (Exception e) {
1375             log.error("Failed to convert json to Map { }", data, e);
1376             return buildErrorResponse(
1377                 getComponentsUtils().getResponseFormat(ActionStatus.USER_DEFINED, "Failed to get the copied component instance information"));
1378         }
1379     }
1380
1381     @POST
1382     @Path("/{containerComponentType}/{componentId}/batchDeleteResourceInstances/")
1383     @Consumes(MediaType.APPLICATION_JSON)
1384     @Produces(MediaType.APPLICATION_JSON)
1385     @Operation(description = "Batch Delete ResourceInstances", method = "POST", responses = {
1386         @ApiResponse(responseCode = "203", description = "ResourceInstances deleted"),
1387         @ApiResponse(responseCode = "403", description = "Restricted Operation"),
1388         @ApiResponse(responseCode = "400", description = "Invalid Content / Missing Content")})
1389     public Response batchDeleteResourceInstances(
1390         @Parameter(description = "valid values: resources / services / products", schema = @Schema(allowableValues = {
1391             ComponentTypeEnum.RESOURCE_PARAM_NAME, ComponentTypeEnum.SERVICE_PARAM_NAME,
1392             ComponentTypeEnum.PRODUCT_PARAM_NAME})) @PathParam("containerComponentType") final String containerComponentType,
1393         @PathParam("componentId") final String componentId, @Context final HttpServletRequest request,
1394         @Parameter(description = "Component Instance Id List", required = true) final String componentInstanceIdLisStr) {
1395         try {
1396             if (componentInstanceIdLisStr == null || componentInstanceIdLisStr.isEmpty()) {
1397                 log.error("Empty JSON List was sent", componentInstanceIdLisStr);
1398                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
1399             }
1400             if (componentInstanceBusinessLogic == null) {
1401                 log.error(UNSUPPORTED_COMPONENT_TYPE, containerComponentType);
1402                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
1403             }
1404             Either<List<String>, ResponseFormat> convertResponse = convertToStringList(componentInstanceIdLisStr);
1405             if (convertResponse.isRight()) {
1406                 BeEcompErrorManager.getInstance().logBeSystemError("Resource Instance - batchDeleteResourceInstances");
1407                 log.error(FAILED_TO_CONVERT_RECEIVED_DATA_TO_BE_FORMAT);
1408                 return buildErrorResponse(convertResponse.right().value());
1409             }
1410             String userId = request.getHeader(Constants.USER_ID_HEADER);
1411             List<String> componentInstanceIdList = convertResponse.left().value();
1412             log.debug("batchDeleteResourceInstances componentInstanceIdList is {}", componentInstanceIdList);
1413             Map<String, List<String>> deleteErrorMap = componentInstanceBusinessLogic
1414                 .batchDeleteComponentInstance(containerComponentType, componentId, componentInstanceIdList, userId);
1415             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), deleteErrorMap);
1416         } catch (Exception e) {
1417             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Batch Delete ResourceInstances");
1418             log.error("batch delete resource instances with exception", e);
1419             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
1420         }
1421     }
1422
1423     @PUT
1424     @Path("/{containerComponentType}/{componentId}/resourceInstance/batchDissociate")
1425     @Consumes(MediaType.APPLICATION_JSON)
1426     @Produces(MediaType.APPLICATION_JSON)
1427     @Operation(description = "Batch Dissociate RI from RI", method = "PUT", summary = "Returns deleted RelationShip Info", responses = {
1428         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
1429         @ApiResponse(responseCode = "201", description = "Relationship deleted"),
1430         @ApiResponse(responseCode = "403", description = "Missing Information"),
1431         @ApiResponse(responseCode = "400", description = "Invalid Content / Missing Content")})
1432     public Response batchDissociateRIFromRI(
1433         @Parameter(description = "allowed values are resources/services/products", schema = @Schema(allowableValues = {
1434             ComponentTypeEnum.RESOURCE_PARAM_NAME, ComponentTypeEnum.SERVICE_PARAM_NAME,
1435             ComponentTypeEnum.PRODUCT_PARAM_NAME}), required = true) @PathParam("containerComponentType") final String containerComponentType,
1436         @Parameter(description = "unique id of the container component") @PathParam("componentId") final String componentId,
1437         @HeaderParam(value = Constants.USER_ID_HEADER) String userId, @Parameter(description = "RelationshipInfo", required = true) String data,
1438         @Context final HttpServletRequest request) {
1439         try {
1440             if (data == null || data.length() == 0) {
1441                 log.info("Empty JSON list was sent");
1442                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
1443             }
1444             ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
1445             if (componentInstanceBusinessLogic == null) {
1446                 log.debug(UNSUPPORTED_COMPONENT_TYPE, containerComponentType);
1447                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
1448             }
1449             Either<List<RequirementCapabilityRelDef>, ResponseFormat> regInfoWs = convertToRequirementCapabilityRelDefList(data);
1450             if (regInfoWs.isRight()) {
1451                 BeEcompErrorManager.getInstance().logBeSystemError("Resource Instance - batch dissociateRIFromRI");
1452                 log.debug("Failed to convert received data to BE format");
1453                 return buildErrorResponse(regInfoWs.right().value());
1454             }
1455             List<RequirementCapabilityRelDef> requirementDefList = regInfoWs.left().value();
1456             List<RequirementCapabilityRelDef> delOkResult = componentInstanceBusinessLogic
1457                 .batchDissociateRIFromRI(componentId, userId, requirementDefList, componentTypeEnum);
1458             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), delOkResult);
1459         } catch (Exception e) {
1460             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Batch Dissociate Resource Instance");
1461             log.debug("batch dissociate resource instance from service failed with exception", e);
1462             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
1463         }
1464     }
1465
1466     private Either<List<String>, ResponseFormat> convertToStringList(String datalist) {
1467         Either<String[], ResponseFormat> convertStatus = getComponentsUtils()
1468             .convertJsonToObjectUsingObjectMapper(datalist, new User(), String[].class, null, null);
1469         if (convertStatus.isRight()) {
1470             return Either.right(convertStatus.right().value());
1471         }
1472         return Either.left(Arrays.asList(convertStatus.left().value()));
1473     }
1474
1475     private Either<List<RequirementCapabilityRelDef>, ResponseFormat> convertToRequirementCapabilityRelDefList(String data) {
1476         Either<RequirementCapabilityRelDef[], ResponseFormat> convertStatus = getComponentsUtils()
1477             .convertJsonToObjectUsingObjectMapper(data, new User(), RequirementCapabilityRelDef[].class, null, null);
1478         if (convertStatus.isRight()) {
1479             return Either.right(convertStatus.right().value());
1480         }
1481         return Either.left(Arrays.asList(convertStatus.left().value()));
1482     }
1483
1484     @POST
1485     @Path("/services/replaceVNF")
1486     @Operation(description = "Replace new VNF based on the existing VNF", method = "POST", summary = "Return whether the replace VNF is successful", responses = {
1487         @ApiResponse(responseCode = "200", description = "ECOMP component is authenticated and list of Catalog Assets Metadata is returned", content = @Content(array = @ArraySchema(schema = @Schema(implementation = ReplaceVNFInfo.class)))),
1488         @ApiResponse(responseCode = "400", description = "Missing  'X-ECOMP-InstanceID'  HTTP header - POL5001"),
1489         @ApiResponse(responseCode = "401", description = "ECOMP component  should authenticate itself  and  to  re-send  again  HTTP  request  with its Basic Authentication credentials - POL5002"),
1490         @ApiResponse(responseCode = "403", description = "ECOMP component is not authorized - POL5003"),
1491         @ApiResponse(responseCode = "404", description = "Error: Requested '%1' (uuid) resource was not found - SVC4063"),
1492         @ApiResponse(responseCode = "405", description = "Method  Not Allowed  :  Invalid HTTP method type used ( PUT,DELETE,POST will be rejected) - POL4050"),
1493         @ApiResponse(responseCode = "500", description = "The GET request failed either due to internal SDC problem. ECOMP Component should continue the attempts to get the needed information - POL5000"),
1494         @ApiResponse(responseCode = "409", description = "Service already exist")})
1495     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
1496     public Response replaceVNF(@Parameter(description = "The user id", required = true) @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
1497                                @Parameter(description = "X-ECOMP-RequestID header", required = false) @HeaderParam(value = Constants.X_ECOMP_REQUEST_ID_HEADER) String requestId,
1498                                @Parameter(description = "X-ECOMP-InstanceID header", required = true) @HeaderParam(value = Constants.X_ECOMP_INSTANCE_ID_HEADER) final String instanceIdHeader,
1499                                @Parameter(description = "Determines the format of the body of the response", required = false) @HeaderParam(value = Constants.ACCEPT_HEADER) String accept,
1500                                @Parameter(description = "The username and password", required = true) @HeaderParam(value = Constants.AUTHORIZATION_HEADER) String authorization,
1501                                @Context final HttpServletRequest request,
1502                                @Parameter(description = "Resource object to be created", required = true) String data) {
1503         String url = request.getMethod() + " " + request.getRequestURI();
1504         log.debug("replaceVNF: Start handle request of {}", url);
1505         User modifier = new User();
1506         modifier.setUserId(userId);
1507         log.debug("replaceVNF:modifier id is {}", userId);
1508         validateNotEmptyBody(data);
1509         Either<ReplaceVNFInfo, ResponseFormat> convertResponse = parseToReplaceVNFInfo(data, modifier);
1510         if (convertResponse.isRight()) {
1511             throw new ByResponseFormatComponentException(convertResponse.right().value());
1512         }
1513         log.debug("replaceVNF:get ReplaceVNFInfo success");
1514         String containerComponentType = SERVICES;
1515         ReplaceVNFInfo replaceVNFInfo = convertResponse.left().value();
1516         String serviceUniqueId = replaceVNFInfo.getServiceUniqueId();
1517         String abstractResourceUniqueId = replaceVNFInfo.getAbstractResourceUniqueId();
1518         ComponentInstance componentInstance = replaceVNFInfo.getRealVNFComponentInstance();
1519         log.debug("replaceVNF:get ReplaceVNFInfo,serviceUniqueId:{},abstractResourceUniqueId:{}", serviceUniqueId, abstractResourceUniqueId);
1520         try {
1521             /**
1522              * delete vnf
1523              */
1524             if (componentInstanceBusinessLogic == null) {
1525                 log.debug("replaceVNF:Unsupported component type {}", containerComponentType);
1526                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
1527             }
1528             Either<ComponentInstance, ResponseFormat> componentInstanceEither = componentInstanceBusinessLogic
1529                 .deleteAbstractComponentInstance(containerComponentType, serviceUniqueId, abstractResourceUniqueId, userId);
1530             if (componentInstanceEither.isRight()) {
1531                 log.debug("replaceVNF:delete Abstract ComponentInstance field");
1532                 return buildErrorResponse(componentInstanceEither.right().value());
1533             }
1534             /**
1535              * add vnf
1536              */
1537             log.debug("replaceVNF,start add vnf");
1538             componentInstance.setInvariantName(null);
1539             componentInstance.setCreatedFrom(CreatedFrom.UI);
1540             Either<ComponentInstance, ResponseFormat> realComponentInstance = componentInstanceBusinessLogic
1541                 .createRealComponentInstance(containerComponentType, serviceUniqueId, userId, componentInstance);
1542             if (realComponentInstance.isRight()) {
1543                 log.debug("replaceVNF:filed to add vnf");
1544                 return buildErrorResponse(realComponentInstance.right().value());
1545             }
1546             ComponentInstance newComponentInstance = realComponentInstance.left().value();
1547             log.debug("replaceVNF:success to add vnf");
1548             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), newComponentInstance);
1549         } catch (Exception e) {
1550             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("replaceVNF");
1551             log.debug("replaceVNF with exception", e);
1552             throw e;
1553         }
1554     }
1555
1556     private Either<ReplaceVNFInfo, ResponseFormat> parseToReplaceVNFInfo(String serviceJson, User user) {
1557         log.debug("enter parseToReplaceVNFInfo,get serviceJson:{}", serviceJson);
1558         return getComponentsUtils().convertJsonToObjectUsingObjectMapper(serviceJson, user, ReplaceVNFInfo.class, AuditingActionEnum.CREATE_RESOURCE,
1559             ComponentTypeEnum.SERVICE);
1560     }
1561 }