2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.be.servlets;
23 import com.fasterxml.jackson.databind.ObjectMapper;
24 import com.google.gson.Gson;
25 import com.google.gson.GsonBuilder;
26 import com.google.gson.reflect.TypeToken;
27 import com.jcabi.aspects.Loggable;
28 import fj.data.Either;
29 import io.swagger.annotations.*;
30 import org.apache.commons.io.IOUtils;
31 import org.apache.commons.lang.StringUtils;
32 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
33 import org.openecomp.sdc.be.components.impl.GroupBusinessLogic;
34 import org.openecomp.sdc.be.components.impl.ServiceBusinessLogic;
35 import org.openecomp.sdc.be.components.impl.utils.DirectivesUtils;
36 import org.openecomp.sdc.be.config.BeEcompErrorManager;
37 import org.openecomp.sdc.be.dao.api.ActionStatus;
38 import org.openecomp.sdc.be.datamodel.ForwardingPaths;
39 import org.openecomp.sdc.be.datatypes.elements.CINodeFilterDataDefinition;
40 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
41 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
42 import org.openecomp.sdc.be.info.CreateAndAssotiateInfo;
43 import org.openecomp.sdc.be.info.GroupDefinitionInfo;
44 import org.openecomp.sdc.be.model.*;
45 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation.PropertyConstraintDeserialiser;
46 import org.openecomp.sdc.common.api.Constants;
47 import org.openecomp.sdc.common.datastructure.Wrapper;
48 import org.openecomp.sdc.common.log.wrappers.Logger;
49 import org.openecomp.sdc.exception.ResponseFormat;
51 import javax.inject.Singleton;
52 import javax.servlet.ServletContext;
53 import javax.servlet.http.HttpServletRequest;
55 import javax.ws.rs.core.Context;
56 import javax.ws.rs.core.MediaType;
57 import javax.ws.rs.core.Response;
58 import java.io.InputStream;
59 import java.lang.reflect.Type;
60 import java.util.ArrayList;
61 import java.util.Arrays;
62 import java.util.List;
67 * Root resource (exposed at "/" path) .json
69 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
71 @Api(value = "Resource Instance Servlet")
73 public class ComponentInstanceServlet extends AbstractValidationsServlet {
75 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: {}";
76 private static final String GET_GROUP_ARTIFACT_BY_ID = "getGroupArtifactById";
77 private static final String GET_GROUP_ARTIFACT_BY_ID_UNEXPECTED_EXCEPTION = "getGroupArtifactById unexpected exception";
78 private static final String GET_START_HANDLE_REQUEST_OF = "(GET) Start handle request of {}";
79 private static final String START_HANDLE_REQUEST_OF_UPDATE_RESOURCE_INSTANCE_PROPERTY_RECEIVED_PROPERTY_IS = "Start handle request of updateResourceInstanceProperty. Received property is {}";
80 private static final String UPDATE_RESOURCE_INSTANCE = "Update Resource Instance";
81 private static final String RESOURCE_INSTANCE_UPDATE_RESOURCE_INSTANCE = "Resource Instance - updateResourceInstance";
82 private static final String UPDATE_RESOURCE_INSTANCE_WITH_EXCEPTION = "update resource instance with exception";
83 private static final String FAILED_TO_CONVERT_RECEIVED_DATA_TO_BE_FORMAT = "Failed to convert received data to BE format.";
84 private static final String EMPTY_BODY_WAS_SENT = "Empty body was sent.";
85 private static final String START_HANDLE_REQUEST_OF = "Start handle request of {}";
86 private static final String UNSUPPORTED_COMPONENT_TYPE = "Unsupported component type {}";
87 private static final Logger log = Logger.getLogger(ComponentInstanceServlet.class);
88 private static final Type PROPERTY_CONSTRAINT_TYPE = new TypeToken<PropertyConstraint>() {}.getType();
89 private static final Gson gsonDeserializer = new GsonBuilder().registerTypeAdapter(PROPERTY_CONSTRAINT_TYPE, new PropertyConstraintDeserialiser()).create();
92 @Path("/{containerComponentType}/{componentId}/resourceInstance")
93 @Consumes(MediaType.APPLICATION_JSON)
94 @Produces(MediaType.APPLICATION_JSON)
95 @ApiOperation(value = "Create ComponentInstance", httpMethod = "POST", notes = "Returns created ComponentInstance", response = Response.class)
96 @ApiResponses(value = { @ApiResponse(code = 201, message = "Component created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
97 @ApiResponse(code = 409, message = "Component instance already exist") })
98 public Response createComponentInstance(@ApiParam(value = "RI object to be created", required = true) String data, @PathParam("componentId") final String containerComponentId,
99 @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
100 @HeaderParam(value = Constants.USER_ID_HEADER) @ApiParam(value = "USER_ID of modifier user", required = true) String userId, @Context final HttpServletRequest request) {
101 ServletContext context = request.getSession().getServletContext();
105 ComponentInstance componentInstance = RepresentationUtils.fromRepresentation(data, ComponentInstance.class);
106 componentInstance.setInvariantName(null);
107 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
108 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context);
109 if (componentInstanceLogic == null) {
110 log.debug(UNSUPPORTED_COMPONENT_TYPE, containerComponentType);
111 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
113 Either<ComponentInstance, ResponseFormat> actionResponse = componentInstanceLogic.createComponentInstance(containerComponentType, containerComponentId, userId, componentInstance);
115 if (actionResponse.isRight()) {
116 return buildErrorResponse(actionResponse.right().value());
118 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.CREATED), actionResponse.left().value());
120 } catch (Exception e) {
121 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Create Component Instance");
122 log.debug("create component instance failed with exception", e);
123 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
128 @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}")
129 @Consumes(MediaType.APPLICATION_JSON)
130 @Produces(MediaType.APPLICATION_JSON)
131 @ApiOperation(value = "Update resource instance", httpMethod = "POST", notes = "Returns updated resource instance", response = Response.class)
132 @ApiResponses(value = { @ApiResponse(code = 200, message = "Resource instance updated"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
133 public Response updateComponentInstanceMetadata(@PathParam("componentId") final String componentId, @PathParam("componentInstanceId") final String componentInstanceId,
134 @ApiParam(value = "valid values: resources / services / products", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME + ","
135 + ComponentTypeEnum.PRODUCT_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
136 @Context final HttpServletRequest request) {
137 ServletContext context = request.getSession().getServletContext();
139 String url = request.getMethod() + " " + request.getRequestURI();
140 log.debug(START_HANDLE_REQUEST_OF, url);
143 log.debug(START_HANDLE_REQUEST_OF, url);
145 InputStream inputStream = request.getInputStream();
147 byte[] bytes = IOUtils.toByteArray(inputStream);
149 if (bytes == null || bytes.length == 0) {
150 log.info(EMPTY_BODY_WAS_SENT);
151 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
154 String userId = request.getHeader(Constants.USER_ID_HEADER);
156 String data = new String(bytes);
157 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
158 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context);
159 if (componentInstanceLogic == null) {
160 log.debug(UNSUPPORTED_COMPONENT_TYPE, containerComponentType);
161 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
163 Either<ComponentInstance, ResponseFormat> convertResponse = convertToResourceInstance(data);
165 if (convertResponse.isRight()) {
166 BeEcompErrorManager.getInstance().logBeSystemError(RESOURCE_INSTANCE_UPDATE_RESOURCE_INSTANCE);
167 log.debug(FAILED_TO_CONVERT_RECEIVED_DATA_TO_BE_FORMAT);
168 return buildErrorResponse(convertResponse.right().value());
171 ComponentInstance resourceInstance = convertResponse.left().value();
172 Either<ComponentInstance, ResponseFormat> actionResponse = componentInstanceLogic.updateComponentInstanceMetadata(containerComponentType, componentId, componentInstanceId, userId, resourceInstance);
174 if (actionResponse.isRight()) {
175 return buildErrorResponse(actionResponse.right().value());
177 ComponentInstance resultValue = actionResponse.left().value();
178 if (componentTypeEnum.equals(ComponentTypeEnum.SERVICE)){
179 boolean shouldCreateServiceFilter = resourceInstance.getDirectives() != null && resourceInstance.getDirectives().contains(
180 DirectivesUtils.SELECTABLE);
182 serviceBusinessLogic = (ServiceBusinessLogic) getComponentBL(componentTypeEnum, context);
184 if(shouldCreateServiceFilter) {
185 Either<CINodeFilterDataDefinition, ResponseFormat> either =
186 serviceBusinessLogic.createIfNotAlreadyExistServiceFilter(componentId, componentInstanceId, userId,
188 if (either.isRight()){
189 BeEcompErrorManager.getInstance().logBeSystemError("Resource Instance - updateResourceInstance Failed to create service filter.");
190 log.debug("Failed to create service filter.");
191 return buildErrorResponse(convertResponse.right().value());
193 resultValue.setNodeFilter(either.left().value());
195 Either<String, ResponseFormat> either = serviceBusinessLogic.deleteIfNotAlreadyDeletedServiceFilter(componentId, componentInstanceId, userId,true);
196 if (either.isRight()){
197 BeEcompErrorManager.getInstance().logBeSystemError("Resource Instance - updateResourceInstance Failed to delete service filter.");
198 log.debug("Failed to delete service filter.");
199 return buildErrorResponse(convertResponse.right().value());
201 resultValue.setNodeFilter(null);
205 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
207 } catch (Exception e) {
208 BeEcompErrorManager.getInstance().logBeRestApiGeneralError(UPDATE_RESOURCE_INSTANCE);
209 log.debug(UPDATE_RESOURCE_INSTANCE_WITH_EXCEPTION, e);
210 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
216 @Path("/{containerComponentType}/{componentId}/resourceInstance/multipleComponentInstance")
217 @Consumes(MediaType.APPLICATION_JSON)
218 @Produces(MediaType.APPLICATION_JSON)
219 @ApiOperation(value = "Update resource instance multiple component", httpMethod = "POST", notes = "Returns updated resource instance", response = Response.class)
220 @ApiResponses(value = { @ApiResponse(code = 200, message = "Resource instance updated"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
221 public Response updateMultipleComponentInstance(@PathParam("componentId") final String componentId,
222 @ApiParam(value = "valid values: resources / services / products", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME + ","
223 + ComponentTypeEnum.PRODUCT_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
224 @Context final HttpServletRequest request, @ApiParam(value = "Component Instance JSON Array", required = true) final String componentInstanceJsonArray) {
226 ServletContext context = request.getSession().getServletContext();
227 String url = request.getMethod() + " " + request.getRequestURI();
228 log.debug(START_HANDLE_REQUEST_OF, url);
231 log.debug(START_HANDLE_REQUEST_OF, url);
233 if (componentInstanceJsonArray == null || componentInstanceJsonArray.length() == 0) {
234 log.info("Empty JSON list was sent.");
235 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
238 String userId = request.getHeader(Constants.USER_ID_HEADER);
240 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
241 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context);
242 if (componentInstanceLogic == null) {
243 log.debug(UNSUPPORTED_COMPONENT_TYPE, containerComponentType);
244 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
247 Either<List<ComponentInstance>, ResponseFormat> convertResponse = convertToMultipleResourceInstance(componentInstanceJsonArray);
249 if (convertResponse.isRight()) {
250 // Using both ECOMP error methods, show to Sofer
251 BeEcompErrorManager.getInstance().logBeSystemError(RESOURCE_INSTANCE_UPDATE_RESOURCE_INSTANCE);
252 log.debug(FAILED_TO_CONVERT_RECEIVED_DATA_TO_BE_FORMAT);
253 return buildErrorResponse(convertResponse.right().value());
256 List<ComponentInstance> componentInstanceList = convertResponse.left().value();
258 Either<List<ComponentInstance>, ResponseFormat> actionResponse = componentInstanceLogic.updateComponentInstance(containerComponentType, componentId, userId, componentInstanceList, true);
260 if (actionResponse.isRight()) {
261 return buildErrorResponse(actionResponse.right().value());
264 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
266 } catch (Exception e) {
267 BeEcompErrorManager.getInstance().logBeRestApiGeneralError(UPDATE_RESOURCE_INSTANCE);
268 log.debug(UPDATE_RESOURCE_INSTANCE_WITH_EXCEPTION, e);
269 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
275 @Path("/{containerComponentType}/{componentId}/resourceInstance/{resourceInstanceId}")
276 @Consumes(MediaType.APPLICATION_JSON)
277 @Produces(MediaType.APPLICATION_JSON)
278 @ApiOperation(value = "Delete ResourceInstance", httpMethod = "DELETE", notes = "Returns delete resourceInstance", response = Response.class)
279 @ApiResponses(value = { @ApiResponse(code = 201, message = "ResourceInstance deleted"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
280 public Response deleteResourceInstance(@PathParam("componentId") final String componentId, @PathParam("resourceInstanceId") final String resourceInstanceId,
281 @ApiParam(value = "valid values: resources / services / products", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME + ","
282 + ComponentTypeEnum.PRODUCT_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
283 @Context final HttpServletRequest request) {
284 ServletContext context = request.getSession().getServletContext();
285 String url = request.getMethod() + " " + request.getRequestURI();
286 Response response = null;
288 log.debug(START_HANDLE_REQUEST_OF, url);
289 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
290 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context);
291 if (componentInstanceLogic == null) {
292 log.debug(UNSUPPORTED_COMPONENT_TYPE, containerComponentType);
293 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
295 String userId = request.getHeader(Constants.USER_ID_HEADER);
296 Either<ComponentInstance, ResponseFormat> actionResponse = componentInstanceLogic.deleteComponentInstance(containerComponentType, componentId, resourceInstanceId, userId);
298 if (actionResponse.isRight()) {
299 response = buildErrorResponse(actionResponse.right().value());
301 response = buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.NO_CONTENT), null);
304 } catch (Exception e) {
305 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Delete Resource Instance");
306 log.debug("delete resource instance with exception", e);
307 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
311 @ApiParam(value = "allowed values are resources /services / products", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME + "," + ComponentTypeEnum.PRODUCT_PARAM_NAME, required = true)
313 @Path("/{containerComponentType}/{componentId}/resourceInstance/associate")
314 @Consumes(MediaType.APPLICATION_JSON)
315 @Produces(MediaType.APPLICATION_JSON)
316 @ApiOperation(value = "Associate RI to RI", httpMethod = "POST", notes = "Returns created RelationshipInfo", response = Response.class)
317 @ApiResponses(value = { @ApiResponse(code = 201, message = "Relationship created"), @ApiResponse(code = 403, message = "Missing information"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
318 @ApiResponse(code = 409, message = "Relationship already exist") })
319 public Response associateRIToRI(@ApiParam(value = "unique id of the container component") @PathParam("componentId") final String componentId,
320 @ApiParam(value = "allowed values are resources /services / products", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME + ","
321 + ComponentTypeEnum.PRODUCT_PARAM_NAME, required = true) @PathParam("containerComponentType") final String containerComponentType,
322 @HeaderParam(value = Constants.USER_ID_HEADER) String userId, @ApiParam(value = "RelationshipInfo", required = true) String data, @Context final HttpServletRequest request) {
323 ServletContext context = request.getSession().getServletContext();
325 String url = request.getMethod() + " " + request.getRequestURI();
326 log.debug(START_HANDLE_REQUEST_OF, url);
327 Response response = null;
331 log.debug(START_HANDLE_REQUEST_OF, url);
333 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
334 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context);
335 if (componentInstanceLogic == null) {
336 log.debug(UNSUPPORTED_COMPONENT_TYPE, containerComponentType);
337 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
340 Either<RequirementCapabilityRelDef, ResponseFormat> regInfoW = convertToRequirementCapabilityRelDef(data);
342 Either<RequirementCapabilityRelDef, ResponseFormat> resultOp;
343 if (regInfoW.isRight()) {
344 BeEcompErrorManager.getInstance().logBeSystemError("Resource Instance - associateRIToRI");
345 log.debug(FAILED_TO_CONVERT_RECEIVED_DATA_TO_BE_FORMAT);
346 resultOp = Either.right(regInfoW.right().value());
348 RequirementCapabilityRelDef requirementDef = regInfoW.left().value();
349 requirementDef.setOriginUI(true);
350 resultOp = componentInstanceLogic.associateRIToRI(componentId, userId, requirementDef, componentTypeEnum);
353 Either<RequirementCapabilityRelDef, ResponseFormat> actionResponse = resultOp;
355 if (actionResponse.isRight()) {
356 response = buildErrorResponse(actionResponse.right().value());
358 response = buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
362 } catch (Exception e) {
363 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Associate Resource Instance");
364 log.debug("associate resource instance to another RI with exception", e);
365 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
370 @Path("/{containerComponentType}/{componentId}/resourceInstance/dissociate")
371 @Consumes(MediaType.APPLICATION_JSON)
372 @Produces(MediaType.APPLICATION_JSON)
373 @ApiOperation(value = "Dissociate RI from RI", httpMethod = "PUT", notes = "Returns deleted RelationshipInfo", response = Response.class)
374 @ApiResponses(value = { @ApiResponse(code = 201, message = "Relationship deleted"), @ApiResponse(code = 403, message = "Missing information"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
375 public Response dissociateRIFromRI(
376 @ApiParam(value = "allowed values are resources /services / products", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME + ","
377 + ComponentTypeEnum.PRODUCT_PARAM_NAME, required = true) @PathParam("containerComponentType") final String containerComponentType,
378 @ApiParam(value = "unique id of the container component") @PathParam("componentId") final String componentId, @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
379 @ApiParam(value = "RelationshipInfo", required = true) String data, @Context final HttpServletRequest request) {
380 ServletContext context = request.getSession().getServletContext();
382 String url = request.getMethod() + " " + request.getRequestURI();
383 log.debug(START_HANDLE_REQUEST_OF, url);
387 log.debug(START_HANDLE_REQUEST_OF, url);
389 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
390 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context);
391 if (componentInstanceLogic == null) {
392 log.debug(UNSUPPORTED_COMPONENT_TYPE, containerComponentType);
393 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
396 Either<RequirementCapabilityRelDef, ResponseFormat> regInfoW = convertToRequirementCapabilityRelDef(data);
397 if (regInfoW.isRight()) {
398 BeEcompErrorManager.getInstance().logBeSystemError("Resource Instance - dissociateRIFromRI");
399 log.debug(FAILED_TO_CONVERT_RECEIVED_DATA_TO_BE_FORMAT);
400 return buildErrorResponse(regInfoW.right().value());
403 RequirementCapabilityRelDef requirementDef = regInfoW.left().value();
404 Either<RequirementCapabilityRelDef, ResponseFormat> actionResponse = componentInstanceLogic.dissociateRIFromRI(componentId, userId, requirementDef, componentTypeEnum);
406 if (actionResponse.isRight()) {
407 return buildErrorResponse(actionResponse.right().value());
409 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
411 } catch (Exception e) {
412 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Dissociate Resource Instance");
413 log.debug("dissociate resource instance from service failed with exception", e);
414 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
419 @Path("/{containerComponentType}/{componentId}/resourceInstance/createAndAssociate")
420 @Consumes(MediaType.APPLICATION_JSON)
421 @Produces(MediaType.APPLICATION_JSON)
422 @ApiOperation(value = "Create RI and associate RI to RI", httpMethod = "POST", notes = "Returns created RI and RelationshipInfo", response = Response.class)
423 @ApiResponses(value = { @ApiResponse(code = 201, message = "RI created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
424 @ApiResponse(code = 409, message = "Relationship already exist") })
425 public Response createAndAssociateRIToRI(@PathParam("componentId") final String componentId,
426 @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
427 @Context final HttpServletRequest request) {
428 ServletContext context = request.getSession().getServletContext();
430 String url = request.getMethod() + " " + request.getRequestURI();
431 log.debug(START_HANDLE_REQUEST_OF, url);
434 log.debug(START_HANDLE_REQUEST_OF, url);
436 InputStream inputStream = request.getInputStream();
438 byte[] bytes = IOUtils.toByteArray(inputStream);
440 if (bytes == null || bytes.length == 0) {
441 log.info(EMPTY_BODY_WAS_SENT);
442 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
445 String userId = request.getHeader(Constants.USER_ID_HEADER);
447 String data = new String(bytes);
449 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
450 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context);
451 if (componentInstanceLogic == null) {
452 log.debug(UNSUPPORTED_COMPONENT_TYPE, containerComponentType);
453 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
456 Either<CreateAndAssotiateInfo, ActionStatus> convertStatus = convertJsonToObject(data, CreateAndAssotiateInfo.class);
457 if (convertStatus.isRight()) {
458 BeEcompErrorManager.getInstance().logBeSystemError("Resource Instance - createAndAssociateRIToRI");
459 log.debug(FAILED_TO_CONVERT_RECEIVED_DATA_TO_BE_FORMAT);
460 Either<Object, ResponseFormat> formattedResponse = Either.right(getComponentsUtils().getResponseFormat(convertStatus.right().value()));
461 return buildErrorResponse(formattedResponse.right().value());
464 CreateAndAssotiateInfo createAndAssotiateInfo = convertStatus.left().value();
465 RequirementCapabilityRelDef requirementDef = createAndAssotiateInfo.getAssociate();
466 requirementDef.setOriginUI(true);
467 Either<CreateAndAssotiateInfo, ResponseFormat> actionResponse = componentInstanceLogic.createAndAssociateRIToRI(containerComponentType, componentId, userId, createAndAssotiateInfo);
469 if (actionResponse.isRight()) {
470 return buildErrorResponse(actionResponse.right().value());
472 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.CREATED), actionResponse.left().value());
473 } catch (Exception e) {
474 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Create and Associate Resource Instance");
475 log.debug("create and associate RI failed with exception", e);
476 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
481 @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}/properties")
482 @Consumes(MediaType.APPLICATION_JSON)
483 @Produces(MediaType.APPLICATION_JSON)
484 @ApiOperation(value = "Update resource instance property", httpMethod = "POST", notes = "Returns updated resource instance property", response = Response.class)
485 @ApiResponses(value = { @ApiResponse(code = 201, message = "Resource instance created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
486 public Response updateResourceInstanceProperties(@ApiParam(value = "service id") @PathParam("componentId") final String componentId,
487 @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
488 @ApiParam(value = "resource instance id") @PathParam("componentInstanceId") final String componentInstanceId, @ApiParam(value = "id of user initiating the operation") @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
489 @Context final HttpServletRequest request, @ApiParam(value = "Component Instance Properties JSON Array", required = true) final String componentInstancePropertiesJsonArray) {
491 String url = request.getMethod() + " " + request.getRequestURI();
492 log.debug(START_HANDLE_REQUEST_OF, url);
495 Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
496 List<ComponentInstanceProperty> propertiesToUpdate = new ArrayList<>();
497 if (errorWrapper.isEmpty()) {
498 Either<List<ComponentInstanceProperty>, ResponseFormat> propertiesToUpdateEither = convertMultipleProperties(componentInstancePropertiesJsonArray);
499 if (propertiesToUpdateEither.isRight()) {
500 errorWrapper.setInnerElement(propertiesToUpdateEither.right().value());
502 propertiesToUpdate = propertiesToUpdateEither.left().value();
506 if (!errorWrapper.isEmpty()) {
507 return buildErrorResponse(errorWrapper.getInnerElement());
510 log.debug(START_HANDLE_REQUEST_OF_UPDATE_RESOURCE_INSTANCE_PROPERTY_RECEIVED_PROPERTY_IS, propertiesToUpdate);
512 ServletContext context = request.getSession().getServletContext();
514 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
515 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context);
516 if (componentInstanceLogic == null) {
517 log.debug(UNSUPPORTED_COMPONENT_TYPE, containerComponentType);
518 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
521 Either<List<ComponentInstanceProperty>, ResponseFormat> actionResponse = componentInstanceLogic.createOrUpdatePropertiesValues(componentTypeEnum, componentId, componentInstanceId, propertiesToUpdate, userId);
523 if (actionResponse.isRight()) {
524 return buildErrorResponse(actionResponse.right().value());
527 List<ComponentInstanceProperty> resourceInstanceProperties = actionResponse.left().value();
528 ObjectMapper mapper = new ObjectMapper();
529 String result = mapper.writeValueAsString(resourceInstanceProperties);
530 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result);
532 } catch (Exception e) {
533 log.error("create and associate RI failed with exception: {}", e.getMessage(), e);
534 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
540 @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}/inputs")
541 @Consumes(MediaType.APPLICATION_JSON)
542 @Produces(MediaType.APPLICATION_JSON)
543 @ApiOperation(value = "Update resource instance property", httpMethod = "POST", notes = "Returns updated resource instance property", response = Response.class)
544 @ApiResponses(value = { @ApiResponse(code = 201, message = "Resource instance created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
545 public Response updateResourceInstanceInput(@ApiParam(value = "service id") @PathParam("componentId") final String componentId,
546 @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
547 @ApiParam(value = "resource instance id") @PathParam("componentInstanceId") final String componentInstanceId, @ApiParam(value = "id of user initiating the operation") @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
548 @Context final HttpServletRequest request, @ApiParam(value = "Component Instance Properties JSON Array", required = true) final String componentInstanceInputsJsonArray) {
550 String url = request.getMethod() + " " + request.getRequestURI();
551 log.debug(START_HANDLE_REQUEST_OF, url);
554 Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
555 List<ComponentInstanceInput> inputsToUpdate = new ArrayList<>();
556 if (errorWrapper.isEmpty()) {
557 Either<List<ComponentInstanceInput>, ResponseFormat> inputsToUpdateEither = convertMultipleInputs(componentInstanceInputsJsonArray);
558 if (inputsToUpdateEither.isRight()) {
559 errorWrapper.setInnerElement(inputsToUpdateEither.right().value());
561 inputsToUpdate = inputsToUpdateEither.left().value();
564 if (!errorWrapper.isEmpty()) {
565 return buildErrorResponse(errorWrapper.getInnerElement());
568 log.debug(START_HANDLE_REQUEST_OF_UPDATE_RESOURCE_INSTANCE_PROPERTY_RECEIVED_PROPERTY_IS, inputsToUpdate);
570 ServletContext context = request.getSession().getServletContext();
572 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
573 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context);
574 if (componentInstanceLogic == null) {
575 log.debug(UNSUPPORTED_COMPONENT_TYPE, containerComponentType);
576 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
579 Either<List<ComponentInstanceInput>, ResponseFormat> actionResponse =
580 componentInstanceLogic.createOrUpdateInstanceInputValues(componentTypeEnum, componentId, componentInstanceId, inputsToUpdate, userId);
582 if (actionResponse.isRight()) {
583 return buildErrorResponse(actionResponse.right().value());
586 List<ComponentInstanceInput> resourceInstanceInput = actionResponse.left().value();
587 ObjectMapper mapper = new ObjectMapper();
588 String result = mapper.writeValueAsString(resourceInstanceInput);
589 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result);
591 } catch (Exception e) {
592 log.error("create and associate RI failed with exception: {}", e.getMessage(), e);
593 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
599 * Updates ResourceInstance Attribute
602 * @param containerComponentType
603 * @param componentInstanceId
609 @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}/attribute")
610 @Consumes(MediaType.APPLICATION_JSON)
611 @Produces(MediaType.APPLICATION_JSON)
612 @ApiOperation(value = "Update resource instance attribute", httpMethod = "POST", notes = "Returns updated resource instance attribute", response = Response.class)
613 @ApiResponses(value = { @ApiResponse(code = 201, message = "Resource instance created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
614 public Response updateResourceInstanceAttribute(@ApiParam(value = "service id") @PathParam("componentId") final String componentId,
615 @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
616 @ApiParam(value = "resource instance id") @PathParam("componentInstanceId") final String componentInstanceId, @ApiParam(value = "id of user initiating the operation") @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
617 @Context final HttpServletRequest request) {
619 String url = request.getMethod() + " " + request.getRequestURI();
620 log.debug(START_HANDLE_REQUEST_OF, url);
624 Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
625 Wrapper<String> dataWrapper = new Wrapper<>();
626 Wrapper<ComponentInstanceProperty> attributeWrapper = new Wrapper<>();
627 Wrapper<ComponentInstanceBusinessLogic> blWrapper = new Wrapper<>();
629 validateInputStream(request, dataWrapper, errorWrapper);
631 if (errorWrapper.isEmpty()) {
632 validateClassParse(dataWrapper.getInnerElement(), attributeWrapper, () -> ComponentInstanceProperty.class, errorWrapper);
635 if (errorWrapper.isEmpty()) {
636 validateComponentInstanceBusinessLogic(request, containerComponentType, blWrapper, errorWrapper);
639 if (errorWrapper.isEmpty()) {
640 ComponentInstanceBusinessLogic componentInstanceLogic = blWrapper.getInnerElement();
641 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
642 log.debug("Start handle request of ComponentInstanceAttribute. Received attribute is {}", attributeWrapper.getInnerElement());
643 Either<ComponentInstanceProperty, ResponseFormat> eitherAttribute = componentInstanceLogic.createOrUpdateAttributeValue(componentTypeEnum, componentId, componentInstanceId, attributeWrapper.getInnerElement(), userId);
644 if (eitherAttribute.isRight()) {
645 errorWrapper.setInnerElement(eitherAttribute.right().value());
647 attributeWrapper.setInnerElement(eitherAttribute.left().value());
651 return buildResponseFromElement(errorWrapper, attributeWrapper);
653 } catch (Exception e) {
654 log.error("create and associate RI failed with exception: {}", e.getMessage(), e);
655 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
661 @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}/property/{propertyId}")
662 @Consumes(MediaType.APPLICATION_JSON)
663 @Produces(MediaType.APPLICATION_JSON)
664 @ApiOperation(value = "Update resource instance", httpMethod = "DELETE", notes = "Returns deleted resource instance property", response = Response.class)
665 @ApiResponses(value = { @ApiResponse(code = 201, message = "Resource instance created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
666 public Response deleteResourceInstanceProperty(@ApiParam(value = "service id") @PathParam("componentId") final String componentId,
667 @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
668 @ApiParam(value = "resource instance id") @PathParam("componentInstanceId") final String componentInstanceId, @ApiParam(value = "property id") @PathParam("propertyId") final String propertyId,
669 @ApiParam(value = "id of user initiating the operation") @HeaderParam(value = Constants.USER_ID_HEADER) String userId, @Context final HttpServletRequest request) {
671 ServletContext context = request.getSession().getServletContext();
673 String url = request.getMethod() + " " + request.getRequestURI();
674 log.debug(START_HANDLE_REQUEST_OF, url);
677 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
678 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context);
679 if (componentInstanceLogic == null) {
680 log.debug(UNSUPPORTED_COMPONENT_TYPE, containerComponentType);
681 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
684 Either<ComponentInstanceProperty, ResponseFormat> actionResponse = componentInstanceLogic.deletePropertyValue(componentTypeEnum, componentId, componentInstanceId, propertyId, userId);
685 if (actionResponse.isRight()) {
686 return buildErrorResponse(actionResponse.right().value());
688 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.NO_CONTENT), null);
689 } catch (Exception e) {
690 log.error("create and associate RI failed with exception: {}", e.getMessage(), e);
691 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
697 @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}/changeVersion")
698 @Consumes(MediaType.APPLICATION_JSON)
699 @Produces(MediaType.APPLICATION_JSON)
700 @ApiOperation(value = "Update resource instance", httpMethod = "POST", notes = "Returns updated resource instance", response = Response.class)
701 @ApiResponses(value = { @ApiResponse(code = 201, message = "Resource instance created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
702 public Response changeResourceInstanceVersion(@PathParam("componentId") final String componentId, @PathParam("componentInstanceId") final String componentInstanceId,
703 @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
704 @Context final HttpServletRequest request) {
705 ServletContext context = request.getSession().getServletContext();
707 String url = request.getMethod() + " " + request.getRequestURI();
708 log.debug(START_HANDLE_REQUEST_OF, url);
709 try ( InputStream inputStream = request.getInputStream()) {
711 byte[] bytes = IOUtils.toByteArray(inputStream);
713 if (bytes == null || bytes.length == 0) {
714 log.info(EMPTY_BODY_WAS_SENT);
715 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
718 String userId = request.getHeader(Constants.USER_ID_HEADER);
720 String data = new String(bytes);
722 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
723 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context);
724 if (componentInstanceLogic == null) {
725 log.debug(UNSUPPORTED_COMPONENT_TYPE, containerComponentType);
726 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
729 Either<ComponentInstance, ResponseFormat> convertResponse = convertToResourceInstance(data);
731 if (convertResponse.isRight()) {
732 BeEcompErrorManager.getInstance().logBeSystemError(RESOURCE_INSTANCE_UPDATE_RESOURCE_INSTANCE);
733 log.debug(FAILED_TO_CONVERT_RECEIVED_DATA_TO_BE_FORMAT);
734 return buildErrorResponse(convertResponse.right().value());
737 ComponentInstance newResourceInstance = convertResponse.left().value();
738 Either<ComponentInstance, ResponseFormat> actionResponse = componentInstanceLogic.changeComponentInstanceVersion(containerComponentType, componentId, componentInstanceId, userId, newResourceInstance);
740 if (actionResponse.isRight()) {
741 return buildErrorResponse(actionResponse.right().value());
743 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
745 } catch (Exception e) {
746 BeEcompErrorManager.getInstance().logBeRestApiGeneralError(UPDATE_RESOURCE_INSTANCE);
747 log.debug(UPDATE_RESOURCE_INSTANCE_WITH_EXCEPTION, e);
748 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
754 @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}/groupInstance/{groupInstanceId}/property")
755 @Consumes(MediaType.APPLICATION_JSON)
756 @Produces(MediaType.APPLICATION_JSON)
757 @ApiOperation(value = "Update resource instance property", httpMethod = "POST", notes = "Returns updated resource instance property", response = Response.class)
758 @ApiResponses(value = { @ApiResponse(code = 201, message = "Resource instance created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
759 public Response updateGroupInstanceProperty(@ApiParam(value = "service id") @PathParam("componentId") final String componentId,
760 @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
761 @ApiParam(value = "resource instance id") @PathParam("componentInstanceId") final String componentInstanceId, @ApiParam(value = "group instance id") @PathParam("groupInstanceId") final String groupInstanceId,
762 @ApiParam(value = "id of user initiating the operation") @HeaderParam(value = Constants.USER_ID_HEADER) String userId, @Context final HttpServletRequest request) {
764 String url = request.getMethod() + " " + request.getRequestURI();
765 log.debug(START_HANDLE_REQUEST_OF, url);
768 Wrapper<String> dataWrapper = new Wrapper<>();
769 Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
770 Wrapper<ComponentInstanceProperty> propertyWrapper = new Wrapper<>();
772 validateInputStream(request, dataWrapper, errorWrapper);
774 if (errorWrapper.isEmpty()) {
775 validateClassParse(dataWrapper.getInnerElement(), propertyWrapper, () -> ComponentInstanceProperty.class, errorWrapper);
778 if (!errorWrapper.isEmpty()) {
779 return buildErrorResponse(errorWrapper.getInnerElement());
782 ComponentInstanceProperty property = propertyWrapper.getInnerElement();
784 log.debug(START_HANDLE_REQUEST_OF_UPDATE_RESOURCE_INSTANCE_PROPERTY_RECEIVED_PROPERTY_IS, property);
786 ServletContext context = request.getSession().getServletContext();
788 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
789 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context);
790 if (componentInstanceLogic == null) {
791 log.debug(UNSUPPORTED_COMPONENT_TYPE, containerComponentType);
792 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
795 Either<ComponentInstanceProperty, ResponseFormat> actionResponse = componentInstanceLogic.createOrUpdateGroupInstancePropertyValue(componentTypeEnum, componentId, componentInstanceId, groupInstanceId, property, userId);
797 if (actionResponse.isRight()) {
798 return buildErrorResponse(actionResponse.right().value());
801 ComponentInstanceProperty resourceInstanceProperty = actionResponse.left().value();
802 ObjectMapper mapper = new ObjectMapper();
803 String result = mapper.writeValueAsString(resourceInstanceProperty);
804 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result);
806 } catch (Exception e) {
807 log.error("create and associate RI failed with exception: {}", e.getMessage(), e);
808 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
814 @Path("/{containerComponentType}/{componentId}/resourceInstance/{componentInstanceId}/groupInstance/{groupInstId}")
815 @Consumes(MediaType.APPLICATION_JSON)
816 @Produces(MediaType.APPLICATION_JSON)
817 @ApiOperation(value = "Get group artifacts ", httpMethod = "GET", notes = "Returns artifacts metadata according to groupInstId", response = Resource.class)
818 @ApiResponses(value = { @ApiResponse(code = 200, message = "group found"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 404, message = "Group not found") })
819 public Response getGroupArtifactById(@PathParam("containerComponentType") final String containerComponentType, @PathParam("componentId") final String componentId, @PathParam("componentInstanceId") final String componentInstanceId,
820 @PathParam("groupInstId") final String groupInstId, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
821 ServletContext context = request.getSession().getServletContext();
822 String url = request.getMethod() + " " + request.getRequestURI();
823 log.debug(GET_START_HANDLE_REQUEST_OF, url);
827 GroupBusinessLogic businessLogic = this.getGroupBL(context);
828 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
829 Either<GroupDefinitionInfo, ResponseFormat> actionResponse = businessLogic.getGroupInstWithArtifactsById(componentTypeEnum, componentId, componentInstanceId, groupInstId, userId, false);
831 if (actionResponse.isRight()) {
832 log.debug("failed to get all non abstract {}", containerComponentType);
833 return buildErrorResponse(actionResponse.right().value());
836 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
838 } catch (Exception e) {
839 BeEcompErrorManager.getInstance().logBeRestApiGeneralError(GET_GROUP_ARTIFACT_BY_ID);
840 log.debug(GET_GROUP_ARTIFACT_BY_ID_UNEXPECTED_EXCEPTION, e);
841 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
848 @Path("/{containerComponentType}/{containerComponentId}/componentInstances/{componentInstanceUniqueId}/properties")
849 @Consumes(MediaType.APPLICATION_JSON)
850 @Produces(MediaType.APPLICATION_JSON)
851 @ApiOperation(value = "Get component instance properties", httpMethod = "GET", notes = "Returns component instance properties", response = Response.class)
852 @ApiResponses(value = { @ApiResponse(code = 200, message = "Properties found"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 404, message = "Component/Component Instance - not found") })
853 public Response getInstancePropertiesById(@PathParam("containerComponentType") final String containerComponentType, @PathParam("containerComponentId") final String containerComponentId,
854 @PathParam("componentInstanceUniqueId") final String componentInstanceUniqueId, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
856 ServletContext context = request.getSession().getServletContext();
857 String url = request.getMethod() + " " + request.getRequestURI();
858 log.debug(GET_START_HANDLE_REQUEST_OF, url);
861 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
862 ComponentInstanceBusinessLogic componentInstanceBL = getComponentInstanceBL(context);
864 Either<List<ComponentInstanceProperty>, ResponseFormat> componentInstancePropertiesById = componentInstanceBL.getComponentInstancePropertiesById(containerComponentType, containerComponentId, componentInstanceUniqueId, userId);
866 if (componentInstancePropertiesById.isRight()) {
867 log.debug(FAILED_TO_GET_PROPERTIES_OF_COMPONENT_INSTANCE_ID_IN_WITH_ID, componentInstanceUniqueId, containerComponentType, containerComponentId);
868 return buildErrorResponse(componentInstancePropertiesById.right().value());
871 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), componentInstancePropertiesById.left().value());
872 } catch (Exception e) {
873 BeEcompErrorManager.getInstance().logBeRestApiGeneralError(GET_GROUP_ARTIFACT_BY_ID);
874 log.debug(GET_GROUP_ARTIFACT_BY_ID_UNEXPECTED_EXCEPTION, e);
875 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
882 @Path("/{containerComponentType}/{containerComponentId}/componentInstances/{componentInstanceUniqueId}/capability/{capabilityType}/capabilityName/{capabilityName}/ownerId/{ownerId}/properties")
883 @Consumes(MediaType.APPLICATION_JSON)
884 @Produces(MediaType.APPLICATION_JSON)
885 @ApiOperation(value = "Get component instance capability properties", httpMethod = "GET", notes = "Returns component instance capability properties", response = Response.class)
886 @ApiResponses(value = { @ApiResponse(code = 200, message = "Properties found"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 404, message = "Component/Component Instance/Capability - not found") })
887 public Response getInstanceCapabilityPropertiesById(@PathParam("containerComponentType") final String containerComponentType, @PathParam("containerComponentId") final String containerComponentId,
888 @PathParam("componentInstanceUniqueId") final String componentInstanceUniqueId, @PathParam("capabilityType") final String capabilityType, @PathParam("capabilityName") final String capabilityName, @PathParam("ownerId") final String ownerId, @Context final HttpServletRequest request,
889 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
891 ServletContext context = request.getSession().getServletContext();
892 String url = request.getMethod() + " " + request.getRequestURI();
893 log.debug(GET_START_HANDLE_REQUEST_OF, url);
896 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
897 ComponentInstanceBusinessLogic componentInstanceBL = getComponentInstanceBL(context);
899 Either<List<ComponentInstanceProperty>, ResponseFormat> componentInstancePropertiesById = componentInstanceBL.getComponentInstanceCapabilityPropertiesById(containerComponentType, containerComponentId, componentInstanceUniqueId,
900 capabilityType, capabilityName, ownerId, userId);
902 if (componentInstancePropertiesById.isRight()) {
903 log.debug(FAILED_TO_GET_PROPERTIES_OF_COMPONENT_INSTANCE_ID_IN_WITH_ID, componentInstanceUniqueId, containerComponentType, containerComponentId);
904 return buildErrorResponse(componentInstancePropertiesById.right().value());
907 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), componentInstancePropertiesById.left().value());
908 } catch (Exception e) {
909 BeEcompErrorManager.getInstance().logBeRestApiGeneralError(GET_GROUP_ARTIFACT_BY_ID);
910 log.debug(GET_GROUP_ARTIFACT_BY_ID_UNEXPECTED_EXCEPTION, e);
911 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
918 @Path("/{containerComponentType}/{containerComponentId}/componentInstances/{componentInstanceUniqueId}/capability/{capabilityType}/capabilityName/{capabilityName}/ownerId/{ownerId}/properties")
919 @Consumes(MediaType.APPLICATION_JSON)
920 @Produces(MediaType.APPLICATION_JSON)
921 @ApiOperation(value = "Update Instance Capabilty Property", httpMethod = "PUT", notes = "Returns updated property", response = Response.class)
922 @ApiResponses(value = { @ApiResponse(code = 200, message = "Resource instance capabilty property updated"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
923 @ApiResponse(code = 404, message = "Component/Component Instance/Capability - not found") })
924 public Response updateInstanceCapabilityProperty(@PathParam("containerComponentType") final String containerComponentType, @PathParam("containerComponentId") final String containerComponentId,
925 @PathParam("componentInstanceUniqueId") final String componentInstanceUniqueId, @PathParam("capabilityType") final String capabilityType, @PathParam("capabilityName") final String capabilityName, @PathParam("ownerId") final String ownerId,
926 @ApiParam(value = "Instance capabilty property to update", required = true) String data, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
927 ServletContext context = request.getSession().getServletContext();
928 String url = request.getMethod() + " " + request.getRequestURI();
929 log.debug("(PUT) Start handle request of {}", url);
931 Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
932 List<ComponentInstanceProperty> propertiesToUpdate = new ArrayList<>();
933 if (errorWrapper.isEmpty()) {
934 Either<List<ComponentInstanceProperty>, ResponseFormat> propertiesToUpdateEither = convertMultipleProperties(data);
935 if (propertiesToUpdateEither.isRight()) {
936 errorWrapper.setInnerElement(propertiesToUpdateEither.right().value());
938 propertiesToUpdate = propertiesToUpdateEither.left().value();
942 if (!errorWrapper.isEmpty()) {
943 return buildErrorResponse(errorWrapper.getInnerElement());
946 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
947 ComponentInstanceBusinessLogic componentInstanceBL = getComponentInstanceBL(context);
949 Either<List<ComponentInstanceProperty>, ResponseFormat> updateCICapProperty = componentInstanceBL.updateInstanceCapabilityProperties(componentTypeEnum, containerComponentId, componentInstanceUniqueId, capabilityType, capabilityName, propertiesToUpdate, userId);
951 if (updateCICapProperty.isRight()) {
952 log.debug(FAILED_TO_GET_PROPERTIES_OF_COMPONENT_INSTANCE_ID_IN_WITH_ID, componentInstanceUniqueId, containerComponentType, containerComponentId);
953 return buildErrorResponse(updateCICapProperty.right().value());
956 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), updateCICapProperty.left().value());
957 } catch (Exception e) {
958 BeEcompErrorManager.getInstance().logBeRestApiGeneralError(GET_GROUP_ARTIFACT_BY_ID);
959 log.debug(GET_GROUP_ARTIFACT_BY_ID_UNEXPECTED_EXCEPTION, e);
960 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
965 @Path("/{containerComponentType}/{containerComponentId}/serviceProxy")
966 @Consumes(MediaType.APPLICATION_JSON)
967 @Produces(MediaType.APPLICATION_JSON)
968 @ApiOperation(value = "Create service proxy", httpMethod = "POST", notes = "Returns created service proxy", response = Response.class)
969 @ApiResponses(value = { @ApiResponse(code = 201, message = "Service proxy created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
970 @ApiResponse(code = 409, message = "Service proxy already exist") })
971 public Response createServiceProxy(@ApiParam(value = "RI object to be created", required = true) String data, @PathParam("containerComponentId") final String containerComponentId,
972 @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
973 @HeaderParam(value = Constants.USER_ID_HEADER) @ApiParam(value = "USER_ID of modifier user", required = true) String userId, @Context final HttpServletRequest request) {
974 ServletContext context = request.getSession().getServletContext();
978 ComponentInstance componentInstance = RepresentationUtils.fromRepresentation(data, ComponentInstance.class);
979 componentInstance.setInvariantName(null);
980 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
981 if (componentTypeEnum != ComponentTypeEnum.SERVICE) {
982 log.debug("Unsupported container component type {}", containerComponentType);
983 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
985 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context);
986 if (componentInstanceLogic == null) {
987 log.debug(UNSUPPORTED_COMPONENT_TYPE, containerComponentType);
988 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
990 Either<ComponentInstance, ResponseFormat> actionResponse = componentInstanceLogic.createServiceProxy();
992 if (actionResponse.isRight()) {
993 return buildErrorResponse(actionResponse.right().value());
995 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.CREATED), actionResponse.left().value());
997 } catch (Exception e) {
998 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Create service proxy");
999 log.debug("Create service proxy failed with exception", e);
1000 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
1005 @Path("/{containerComponentType}/{containerComponentId}/serviceProxy/{serviceProxyId}")
1006 @Consumes(MediaType.APPLICATION_JSON)
1007 @Produces(MediaType.APPLICATION_JSON)
1008 @ApiOperation(value = "Delete service proxy", httpMethod = "DELETE", notes = "Returns delete service proxy", response = Response.class)
1009 @ApiResponses(value = { @ApiResponse(code = 201, message = "Service proxy deleted"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
1010 public Response deleteServiceProxy(@PathParam("containerComponentId") final String containerComponentId, @PathParam("serviceProxyId") final String serviceProxyId,
1011 @ApiParam(value = "valid values: resources / services / products", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME + ","
1012 + ComponentTypeEnum.PRODUCT_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
1013 @Context final HttpServletRequest request) {
1014 ServletContext context = request.getSession().getServletContext();
1015 String url = request.getMethod() + " " + request.getRequestURI();
1016 Response response = null;
1018 log.debug(START_HANDLE_REQUEST_OF, url);
1019 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
1020 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context);
1021 if (componentInstanceLogic == null) {
1022 log.debug(UNSUPPORTED_COMPONENT_TYPE, containerComponentType);
1023 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
1025 String userId = request.getHeader(Constants.USER_ID_HEADER);
1026 Either<ComponentInstance, ResponseFormat> actionResponse = componentInstanceLogic.deleteServiceProxy();
1028 if (actionResponse.isRight()) {
1029 response = buildErrorResponse(actionResponse.right().value());
1031 response = buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.NO_CONTENT), null);
1034 } catch (Exception e) {
1035 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Delete service proxy");
1036 log.debug("Delete service proxy failed with exception", e);
1037 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
1042 @Path("/{containerComponentType}/{containerComponentId}/serviceProxy/{serviceProxyId}/changeVersion/{newServiceId}")
1043 @Consumes(MediaType.APPLICATION_JSON)
1044 @Produces(MediaType.APPLICATION_JSON)
1045 @ApiOperation(value = "Update service proxy with new version", httpMethod = "POST", notes = "Returns updated service proxy", response = Response.class)
1046 @ApiResponses(value = { @ApiResponse(code = 201, message = "Service proxy created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
1047 public Response changeServiceProxyVersion(@PathParam("containerComponentId") final String containerComponentId, @PathParam("serviceProxyId") final String serviceProxyId,
1048 @ApiParam(value = "valid values: resources / services", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME) @PathParam("containerComponentType") final String containerComponentType,
1049 @Context final HttpServletRequest request) {
1050 ServletContext context = request.getSession().getServletContext();
1052 String url = request.getMethod() + " " + request.getRequestURI();
1053 log.debug(START_HANDLE_REQUEST_OF, url);
1056 String userId = request.getHeader(Constants.USER_ID_HEADER);
1058 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
1059 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context);
1060 if (componentInstanceLogic == null) {
1061 log.debug(UNSUPPORTED_COMPONENT_TYPE, containerComponentType);
1062 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
1064 Either<ComponentInstance, ResponseFormat> actionResponse = componentInstanceLogic.changeServiceProxyVersion();
1066 if (actionResponse.isRight()) {
1067 return buildErrorResponse(actionResponse.right().value());
1069 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
1071 } catch (Exception e) {
1072 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Update service proxy with new version");
1073 log.debug("Update service proxy with new version failed with exception", e);
1074 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
1078 * REST API GET relation by Id
1079 * Allows to get relation contained in specified component according to received Id
1080 * @param containerComponentType
1081 * @param componentId
1088 @Path("/{containerComponentType}/{componentId}/relationId/{relationId}")
1089 @Consumes(MediaType.APPLICATION_JSON)
1090 @Produces(MediaType.APPLICATION_JSON)
1091 @ApiOperation(value = "Get relation", httpMethod = "GET", notes = "Returns relation metadata according to relationId", response = Resource.class)
1092 @ApiResponses(value = { @ApiResponse(code = 200, message = "relation found"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 404, message = "Relation not found") })
1093 public Response getRelationById(@PathParam("containerComponentType") final String containerComponentType, @PathParam("componentId") final String componentId,
1094 @PathParam("relationId") final String relationId, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
1096 ServletContext context = request.getSession().getServletContext();
1097 String url = request.getMethod() + " " + request.getRequestURI();
1098 log.debug(GET_START_HANDLE_REQUEST_OF, url);
1100 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
1101 if (componentTypeEnum == null) {
1102 log.debug(UNSUPPORTED_COMPONENT_TYPE, containerComponentType);
1103 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
1105 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context);
1107 Either<RequirementCapabilityRelDef, ResponseFormat> actionResponse = componentInstanceLogic.getRelationById(componentId, relationId, userId, componentTypeEnum);
1108 if (actionResponse.isRight()) {
1109 return buildErrorResponse(actionResponse.right().value());
1111 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
1112 } catch (Exception e) {
1113 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("getRelationById");
1114 log.debug("getRelationById unexpected exception", e);
1115 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
1119 private Either<ComponentInstance, ResponseFormat> convertToResourceInstance(String data) {
1121 Either<ComponentInstance, ResponseFormat> convertStatus = getComponentsUtils().convertJsonToObjectUsingObjectMapper(data, new User(), ComponentInstance.class, null, ComponentTypeEnum.RESOURCE_INSTANCE);
1122 if (convertStatus.isRight()) {
1123 return Either.right(convertStatus.right().value());
1125 ComponentInstance resourceInstanceInfo = convertStatus.left().value();
1127 return Either.left(resourceInstanceInfo);
1130 private Either<List<ComponentInstance>, ResponseFormat> convertToMultipleResourceInstance(String dataList) {
1132 Either<ComponentInstance[], ResponseFormat> convertStatus = getComponentsUtils().convertJsonToObjectUsingObjectMapper(dataList, new User(), ComponentInstance[].class, null, ComponentTypeEnum.RESOURCE_INSTANCE);
1133 if (convertStatus.isRight()) {
1134 return Either.right(convertStatus.right().value());
1137 return Either.left(Arrays.asList(convertStatus.left().value()));
1140 private Either<List<ComponentInstanceProperty>, ResponseFormat> convertMultipleProperties(String dataList) {
1141 if (StringUtils.isEmpty(dataList)) {
1142 return Either.right(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
1144 Either<ComponentInstanceProperty[], ResponseFormat> convertStatus = getComponentsUtils().convertJsonToObjectUsingObjectMapper(dataList, new User(), ComponentInstanceProperty[].class, null, ComponentTypeEnum.RESOURCE_INSTANCE);
1145 if (convertStatus.isRight()) {
1146 return Either.right(convertStatus.right().value());
1148 return Either.left(Arrays.asList(convertStatus.left().value()));
1151 private Either<List<ComponentInstanceInput>, ResponseFormat> convertMultipleInputs(String dataList) {
1152 if (StringUtils.isEmpty(dataList)) {
1153 return Either.right(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
1155 Either<ComponentInstanceInput[], ResponseFormat> convertStatus = getComponentsUtils().convertJsonToObjectUsingObjectMapper(dataList, new User(), ComponentInstanceInput[].class, null, ComponentTypeEnum.RESOURCE_INSTANCE);
1156 if (convertStatus.isRight()) {
1157 return Either.right(convertStatus.right().value());
1159 return Either.left(Arrays.asList(convertStatus.left().value()));
1163 private Either<RequirementCapabilityRelDef, ResponseFormat> convertToRequirementCapabilityRelDef(String data) {
1165 Either<RequirementCapabilityRelDef, ActionStatus> convertStatus = convertJsonToObject(data, RequirementCapabilityRelDef.class);
1166 if (convertStatus.isRight()) {
1167 return Either.right(getComponentsUtils().getResponseFormat(convertStatus.right().value()));
1169 RequirementCapabilityRelDef requirementCapabilityRelDef = convertStatus.left().value();
1170 return Either.left(requirementCapabilityRelDef);
1173 public <T> Either<T, ActionStatus> convertJsonToObject(String data, Class<T> clazz) {
1175 log.trace("convert json to object. json=\n {}", data);
1177 t = gsonDeserializer.fromJson(data, clazz);
1179 BeEcompErrorManager.getInstance().logBeInvalidJsonInput("convertJsonToObject");
1180 log.debug("object is null after converting from json");
1181 return Either.right(ActionStatus.INVALID_CONTENT);
1183 return Either.left(t);
1184 } catch (Exception e) {
1186 BeEcompErrorManager.getInstance().logBeInvalidJsonInput("convertJsonToObject");
1187 log.debug("failed to convert from json", e);
1188 return Either.right(ActionStatus.INVALID_CONTENT);
1194 @Path("/{containerComponentType}/{componentId}/paths-to-delete")
1195 @Produces(MediaType.APPLICATION_JSON)
1196 @ApiOperation(value = "Check if forwarding path to delete on version change", httpMethod = "GET", notes = "Returns forwarding paths to delete",
1197 response = Response.class)
1198 public Response changeResourceInstanceVersion( @PathParam("componentId") String componentId,
1199 @QueryParam("componentInstanceId") final String oldComponentInstanceId,
1200 @QueryParam("newComponentInstanceId") final String newComponentInstanceId,
1201 @ApiParam(value = "valid values: resources / services",
1202 allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME)
1203 @PathParam("containerComponentType") final String containerComponentType,
1204 @Context final HttpServletRequest request) {
1205 if (oldComponentInstanceId == null){
1206 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.MISSING_OLD_COMPONENT_INSTANCE));
1208 if (newComponentInstanceId == null){
1209 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.MISSING_NEW_COMPONENT_INSTANCE));
1211 ServletContext context = request.getSession().getServletContext();
1213 String url = request.getMethod() + " " + request.getRequestURI();
1214 log.debug(START_HANDLE_REQUEST_OF, url);
1215 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
1216 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context);
1217 if (componentInstanceLogic == null) {
1218 log.debug(UNSUPPORTED_COMPONENT_TYPE, containerComponentType);
1219 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
1221 ComponentInstance newComponentInstance;
1222 if(StringUtils.isNotEmpty(newComponentInstanceId)){
1223 newComponentInstance=new ComponentInstance();
1224 newComponentInstance.setToscaPresentationValue(JsonPresentationFields.CI_COMPONENT_UID,newComponentInstanceId);
1226 log.error("missing component id");
1227 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.MISSING_DATA));
1229 Either<Set<String>,ResponseFormat> actionResponse= componentInstanceLogic.forwardingPathOnVersionChange(
1230 containerComponentType,componentId,oldComponentInstanceId,newComponentInstance);
1231 if (actionResponse.isRight()) {
1232 return buildErrorResponse(actionResponse.right().value());
1234 ForwardingPaths forwardingPaths=new ForwardingPaths();
1235 forwardingPaths.setForwardingPathToDelete(actionResponse.left().value());
1236 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), forwardingPaths);
1241 @Path("/services/{componentId}/copyComponentInstance/{componentInstanceId}")
1242 @Consumes(MediaType.APPLICATION_JSON)
1243 @Produces((MediaType.APPLICATION_JSON))
1244 @ApiOperation(value = "Copy Component Instance", httpMethod = "POST", notes = "Returns updated service information", response = Service.class)
1245 @ApiResponses(value = {
1246 @ApiResponse(code = 201, message = "Copy and Paste Success"),
1247 @ApiResponse(code = 403, message = "Restricted Operation"),
1248 @ApiResponse(code = 400, message = "Invalid Content / Missing content")})
1249 public Response copyComponentInstance(
1250 @ApiParam(value = "service unique id in pasted canvas") @PathParam("componentId") final String containerComponentId,
1251 @ApiParam(value = "Data for copying", required = true) String data, @PathParam("componentInstanceId") final String componentInstanceId,
1252 @Context final HttpServletRequest request) {
1253 log.info("Start to copy component instance");
1254 ServletContext context = request.getSession().getServletContext();
1255 String userId = request.getHeader(Constants.USER_ID_HEADER);
1256 final String CNTAINER_CMPT_TYPE = "services";
1259 ComponentInstance inputComponentInstance = RepresentationUtils.fromRepresentation(data, ComponentInstance.class);
1260 inputComponentInstance.setInvariantName(null);
1261 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(CNTAINER_CMPT_TYPE);
1262 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context);
1263 if (componentInstanceLogic == null) {
1264 log.debug(UNSUPPORTED_COMPONENT_TYPE, componentTypeEnum);
1265 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, "services"));
1267 Either<Map<String, ComponentInstance>, ResponseFormat> copyComponentInstance = componentInstanceLogic.copyComponentInstance(
1268 inputComponentInstance, containerComponentId, componentInstanceId, userId);
1270 if (copyComponentInstance.isRight()) {
1271 log.error("Failed to copy ComponentInstance {}", copyComponentInstance.right().value());
1272 return buildErrorResponse(copyComponentInstance.right().value());
1275 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK),
1276 copyComponentInstance.left().value());
1277 } catch (Exception e) {
1278 log.error("Failed to convert json to Map { }", data, e);
1279 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.USER_DEFINED,
1280 "Failed to get the copied component instance information"));
1285 @Path("/{containerComponentType}/{componentId}/batchDeleteResourceInstances/")
1286 @Consumes(MediaType.APPLICATION_JSON)
1287 @Produces(MediaType.APPLICATION_JSON)
1288 @ApiOperation(value = "Batch Delete ResourceInstances", httpMethod = "POST")
1289 @ApiResponses(value = {
1290 @ApiResponse(code = 203, message = "ResourceInstances deleted"),
1291 @ApiResponse(code = 403, message = "Restricted Operation"),
1292 @ApiResponse(code = 400, message = "Invalid Content / Missing Content")
1294 public Response batchDeleteResourceInstances(
1295 @ApiParam(value = "valid values: resources / services / products", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME + "," +
1296 ComponentTypeEnum.PRODUCT_PARAM_NAME)
1297 @PathParam("containerComponentType") final String containerComponentType,
1298 @PathParam("componentId") final String componentId,
1299 @Context final HttpServletRequest request,
1300 @ApiParam(value = "Component Instance Id List", required = true) final String componentInstanceIdLisStr) {
1301 ServletContext context = request.getSession().getServletContext();
1303 if (componentInstanceIdLisStr == null || componentInstanceIdLisStr.isEmpty()) {
1304 log.error("Empty JSON List was sent",componentInstanceIdLisStr);
1305 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
1309 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context);
1310 if (componentInstanceLogic == null) {
1311 log.error("Unsupported component type {}", containerComponentType);
1312 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
1315 Either<List<String>, ResponseFormat> convertResponse = convertToStringList(componentInstanceIdLisStr);
1317 if (convertResponse.isRight()) {
1318 BeEcompErrorManager.getInstance().logBeSystemError("Resource Instance - batchDeleteResourceInstances");
1319 log.error("Failed to convert received data to BE format.");
1320 return buildErrorResponse(convertResponse.right().value());
1323 String userId = request.getHeader(Constants.USER_ID_HEADER);
1324 List<String> componentInstanceIdList = convertResponse.left().value();
1325 log.debug("batchDeleteResourceInstances componentInstanceIdList is {}", componentInstanceIdList);
1326 Map<String, List<String>> deleteErrorMap = componentInstanceLogic.batchDeleteComponentInstance(containerComponentType,
1327 componentId, componentInstanceIdList, userId);
1329 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), deleteErrorMap);
1330 } catch (Exception e) {
1331 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Batch Delete ResourceInstances");
1332 log.error("batch delete resource instances with exception" , e);
1333 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
1339 @Path("/{containerComponentType}/{componentId}/resourceInstance/batchDissociate")
1340 @Consumes(MediaType.APPLICATION_JSON)
1341 @Produces(MediaType.APPLICATION_JSON)
1342 @ApiOperation(value = "Batch Dissociate RI from RI", httpMethod = "PUT", notes = "Returns deleted RelationShip Info", response = Response.class)
1343 @ApiResponses(value = {
1344 @ApiResponse(code = 201, message = "Relationship deleted"),
1345 @ApiResponse(code = 403, message = "Missing Information"),
1346 @ApiResponse(code = 400, message = "Invalid Content / Missing Content")
1348 public Response batchDissociateRIFromRI(
1349 @ApiParam(value = "allowed values are resources/services/products", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME + "," + ComponentTypeEnum.PRODUCT_PARAM_NAME, required = true)
1350 @PathParam("containerComponentType") final String containerComponentType,
1351 @ApiParam(value = "unique id of the container component")
1352 @PathParam("componentId") final String componentId,
1353 @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
1354 @ApiParam(value = "RelationshipInfo", required = true) String data,
1355 @Context final HttpServletRequest request) {
1356 ServletContext context = request.getSession().getServletContext();
1359 if (data == null || data.length() == 0) {
1360 log.info("Empty JSON list was sent");
1361 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
1364 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
1365 ComponentInstanceBusinessLogic componentInstanceLogic = getComponentInstanceBL(context);
1367 if (componentInstanceLogic == null) {
1368 log.debug("Unsupported component type {}", containerComponentType);
1369 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.UNSUPPORTED_ERROR, containerComponentType));
1372 Either<List<RequirementCapabilityRelDef>, ResponseFormat> regInfoWs = convertToRequirementCapabilityRelDefList(data);
1374 if (regInfoWs.isRight()) {
1375 BeEcompErrorManager.getInstance().logBeSystemError("Resource Instance - batch dissociateRIFromRI");
1376 log.debug("Failed to convert received data to BE format");
1377 return buildErrorResponse(regInfoWs.right().value());
1380 List<RequirementCapabilityRelDef> requirementDefList = regInfoWs.left().value();
1381 List<RequirementCapabilityRelDef> delOkResult = componentInstanceLogic.batchDissociateRIFromRI(
1382 componentId, userId, requirementDefList, componentTypeEnum);
1384 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), delOkResult);
1385 } catch (Exception e) {
1386 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Batch Dissociate Resource Instance");
1387 log.debug("batch dissociate resource instance from service failed with exception", e);
1388 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
1392 private Either<List<String>, ResponseFormat> convertToStringList(String datalist) {
1393 Either<String[], ResponseFormat> convertStatus = getComponentsUtils().convertJsonToObjectUsingObjectMapper(datalist, new User(), String[].class, null, null);
1395 if (convertStatus.isRight()) {
1396 return Either.right(convertStatus.right().value());
1399 return Either.left(Arrays.asList(convertStatus.left().value()));
1402 private Either<List<RequirementCapabilityRelDef>, ResponseFormat> convertToRequirementCapabilityRelDefList(String data) {
1403 Either<RequirementCapabilityRelDef[], ResponseFormat> convertStatus = getComponentsUtils().convertJsonToObjectUsingObjectMapper(data, new User(), RequirementCapabilityRelDef[].class, null, null);
1405 if (convertStatus.isRight()) {
1406 return Either.right(convertStatus.right().value());
1409 return Either.left(Arrays.asList(convertStatus.left().value()));