+
+ public Either<Map<String, ComponentInstance>, ResponseFormat> copyComponentInstance(ComponentInstance inputComponentInstance,
+ String containerComponentId, String componentInstanceId,
+ String userId) {
+ Map<String, ComponentInstance> resultMap = new HashMap<>();
+ Either<Component, StorageOperationStatus> getOrigComponent = toscaOperationFacade.getToscaElement(containerComponentId);
+ if (getOrigComponent.isRight()) {
+ log.error("Failed to get the original component information");
+ return Either.right(componentsUtils.getResponseFormat(ActionStatus.USER_DEFINED, FAILED_TO_COPY_COMP_INSTANCE_TO_CANVAS));
+ }
+ Component origComponent = getOrigComponent.left().value();
+ try {
+ lockComponent(origComponent, "copyComponentInstance");
+ } catch (ComponentException e) {
+ log.error("destComponentInstance's data is {}", origComponent.toString());
+ return Either.right(componentsUtils
+ .getResponseFormat(ActionStatus.USER_DEFINED, "Failed to lock component destComponentInstance's data is {}",
+ origComponent.toString()));
+ }
+ boolean failed = false;
+ ComponentInstance actionResponse = null;
+ try {
+ actionResponse = createComponentInstance("services", containerComponentId, userId, inputComponentInstance, false);
+ } catch (ComponentException e) {
+ failed = true;
+ // on failure of the create instance unlock the resource and rollback the transaction.
+ return Either.right(componentsUtils.getResponseFormat(ActionStatus.USER_DEFINED, FAILED_TO_COPY_COMP_INSTANCE_TO_CANVAS));
+ } finally {
+ // on failure of the create instance unlock the resource and rollback the transaction.
+ if (null == actionResponse) {
+ log.error(FAILED_TO_COPY_COMP_INSTANCE_TO_CANVAS);
+ unlockComponent(failed, origComponent);
+ }
+ }
+ Either<String, ResponseFormat> resultOp = null;
+ try {
+ ComponentInstance destComponentInstance = actionResponse;
+ log.debug("destComponentInstance's data is {}", destComponentInstance);
+ resultOp = deepCopyComponentInstance(origComponent, containerComponentId, componentInstanceId, destComponentInstance, userId);
+ resultMap.put("componentInstance", destComponentInstance);
+ } finally {
+ // unlock resource
+ if (resultOp == null || resultOp.isRight()) {
+ unlockComponent(true, origComponent);
+ janusGraphDao.rollback();
+ log.error("Failed to deep copy component instance");
+ } else {
+ unlockComponent(false, origComponent);
+ janusGraphDao.commit();
+ log.debug("Success trasaction commit");
+ }
+ }
+ if (resultOp == null || resultOp.isRight()) {
+ return Either
+ .right(componentsUtils.getResponseFormat(ActionStatus.USER_DEFINED, "Failed to deep copy the component instance to the canvas"));
+ } else {
+ return Either.left(resultMap);
+ }
+ }
+
+ private Either<String, ResponseFormat> deepCopyComponentInstance(Component sourceComponent, String containerComponentId,
+ String sourceComponentInstanceId, ComponentInstance destComponentInstance,
+ String userId) {
+ Either<Component, StorageOperationStatus> getDestComponent = toscaOperationFacade.getToscaElement(containerComponentId);
+ if (getDestComponent.isRight()) {
+ log.error("Failed to get the dest component information");
+ return Either.right(componentsUtils.getResponseFormat(ActionStatus.USER_DEFINED, FAILED_TO_COPY_COMP_INSTANCE_TO_CANVAS));
+ }
+ Component destComponent = getDestComponent.left().value();
+ Either<String, ResponseFormat> copyComponentInstanceWithPropertiesAndInputs = copyComponentInstanceWithPropertiesAndInputs(sourceComponent,
+ destComponent, sourceComponentInstanceId, destComponentInstance);
+ if (copyComponentInstanceWithPropertiesAndInputs.isRight()) {
+ log.error("Failed to copy component instance with properties and inputs as part of deep copy");
+ return Either.right(componentsUtils.getResponseFormat(ActionStatus.USER_DEFINED,
+ "Failed to copy the component instance with properties and inputs as part of deep copy"));
+ }
+ Either<String, ResponseFormat> copyComponentInstanceWithAttributes = copyComponentInstanceWithAttributes(sourceComponent, destComponent,
+ sourceComponentInstanceId, destComponentInstance, userId);
+ if (copyComponentInstanceWithAttributes.isRight()) {
+ log.error("Failed to copy component instance with attributes as part of deep copy");
+ return Either.right(componentsUtils
+ .getResponseFormat(ActionStatus.USER_DEFINED, "Failed to copy the component instance with attributes as part of deep copy"));
+ }
+ return Either.left(COPY_COMPONENT_INSTANCE_OK);
+ }
+
+ private Either<String, ResponseFormat> copyComponentInstanceWithPropertiesAndInputs(Component sourceComponent, Component destComponent,
+ String sourceComponentInstanceId,
+ ComponentInstance destComponentInstance) {
+ log.debug("start to copy ComponentInstance with properties and inputs");
+ List<ComponentInstanceProperty> sourcePropList = null;
+ if (sourceComponent.getComponentInstancesProperties() != null
+ && sourceComponent.getComponentInstancesProperties().get(sourceComponentInstanceId) != null) {
+ sourcePropList = sourceComponent.getComponentInstancesProperties().get(sourceComponentInstanceId);
+ log.debug("sourcePropList");
+ }
+ List<ComponentInstanceProperty> destPropList = null;
+ String destComponentInstanceId = destComponentInstance.getUniqueId();
+ log.debug("destComponentInstanceId: {}", destComponentInstance.getUniqueId());
+ if (destComponent.getComponentInstancesProperties() != null
+ && destComponent.getComponentInstancesProperties().get(destComponentInstanceId) != null) {
+ destPropList = destComponent.getComponentInstancesProperties().get(destComponentInstanceId);
+ log.debug("destPropList {}");
+ }
+ List<ComponentInstancePropInput> componentInstancePropInputList = new ArrayList<>();
+ if (null != destPropList && null != sourcePropList) {
+ log.debug("start to set property and attribute");
+ for (ComponentInstanceProperty destProp : destPropList) {
+ String destPropertyName = destProp.getName();
+ for (ComponentInstanceProperty sourceProp : sourcePropList) {
+ if (!destPropertyName.equals(sourceProp.getName())) {
+ continue;
+ }
+ log.debug("now set property");
+ final List<GetInputValueDataDefinition> getInputValues = sourceProp.getGetInputValues();
+ if (getInputValues == null && !StringUtils.isEmpty(sourceProp.getValue()) && (destProp.getValue() == null || !destProp.getValue()
+ .equals(sourceProp.getValue()))) {
+ log.debug("Now starting to copy the property {} in value {}", destPropertyName, sourceProp.getValue());
+ destProp.setValue(sourceProp.getValue());
+ Either<String, ResponseFormat> updatePropertyValueEither = updateComponentInstanceProperty(destComponent.getUniqueId(),
+ destComponentInstanceId, destProp);
+ if (updatePropertyValueEither.isRight()) {
+ log.error("Failed to copy the property {}", destPropertyName);
+ return Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT_PARAM,
+ "Failed to paste component instance to the canvas, property copy"));
+ }
+ break;
+ }
+ log.debug("Now start to update inputs");
+ if (getInputValues != null) {
+ if (getInputValues.isEmpty()) {
+ log.debug("property is return from input, set by man");
+ break;
+ }
+ log.debug("Now starting to copy the {} property", destPropertyName);
+ Either<String, ResponseFormat> getSourceInputDefaultValue = getInputListDefaultValue(sourceComponent,
+ getInputValues.get(0).getInputId());
+ if (getSourceInputDefaultValue.isRight()) {
+ return Either.right(getSourceInputDefaultValue.right().value());
+ }
+ componentInstancePropInputList.add(new ComponentInstancePropInput(destProp));
+ }
+ }
+ }
+ }
+ return Either.left(COPY_COMPONENT_INSTANCE_OK);
+ }
+
+ private Either<String, ResponseFormat> copyComponentInstanceWithAttributes(Component sourceComponent, Component destComponent,
+ String sourceComponentInstanceId,
+ ComponentInstance destComponentInstance, String userId) {
+ String destComponentInstanceId = destComponentInstance.getUniqueId();
+ log.info("start to copy component instance with attributes");
+ List<ComponentInstanceAttribute> sourceAttributeList = null;
+ if (sourceComponent.getComponentInstancesAttributes() != null
+ && sourceComponent.getComponentInstancesAttributes().get(sourceComponentInstanceId) != null) {
+ sourceAttributeList = sourceComponent.getComponentInstancesAttributes().get(sourceComponentInstanceId);
+ log.info("sourceAttributes {}");
+ }
+ List<ComponentInstanceAttribute> destAttributeList = null;
+ if (destComponent.getComponentInstancesAttributes() != null
+ && destComponent.getComponentInstancesAttributes().get(destComponentInstanceId) != null) {
+ destAttributeList = destComponent.getComponentInstancesAttributes().get(destComponentInstanceId);
+ log.info("destAttributeList {}");
+ }
+ if (null != sourceAttributeList && null != destAttributeList) {
+ log.info("set attribute");
+ for (ComponentInstanceAttribute sourceAttribute : sourceAttributeList) {
+ String sourceAttributeName = sourceAttribute.getName();
+ for (ComponentInstanceAttribute destAttribute : destAttributeList) {
+ if (sourceAttributeName.equals(destAttribute.getName())) {
+ log.debug("Start to copy the attribute exists {}", sourceAttributeName);
+ sourceAttribute.setUniqueId(
+ UniqueIdBuilder.buildResourceInstanceUniuqeId("attribute", destComponentInstanceId.split("\\.")[1], sourceAttributeName));
+ Either<ComponentInstanceAttribute, ResponseFormat> updateAttributeValueEither = createOrUpdateAttributeValueForCopyPaste(
+ ComponentTypeEnum.SERVICE, destComponent.getUniqueId(), destComponentInstanceId, sourceAttribute, userId);
+ if (updateAttributeValueEither.isRight()) {
+ log.error("Failed to copy the attribute");
+ return Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT_PARAM,
+ "Failed to paste component instance to the canvas, attribute copy"));
+ }
+ break;
+ }
+ }
+ }
+ }
+ return Either.left(COPY_COMPONENT_INSTANCE_OK);
+ }
+
+ private Either<ComponentInstanceAttribute, ResponseFormat> createOrUpdateAttributeValueForCopyPaste(ComponentTypeEnum componentTypeEnum,
+ String componentId, String resourceInstanceId,
+ ComponentInstanceAttribute attribute,
+ String userId) {
+ Either<ComponentInstanceAttribute, ResponseFormat> resultOp = null;
+ validateUserExists(userId);
+ if (componentTypeEnum == null) {
+ BeEcompErrorManager.getInstance()
+ .logInvalidInputError("createOrUpdateAttributeValueForCopyPaste", INVALID_COMPONENT_TYPE, ErrorSeverity.INFO);
+ resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.NOT_ALLOWED));
+ return resultOp;
+ }
+ Either<Component, StorageOperationStatus> getResourceResult = toscaOperationFacade.getToscaElement(componentId, JsonParseFlagEnum.ParseAll);
+ if (getResourceResult.isRight()) {
+ log.info("Failed to retrieve component id {}", componentId);
+ resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
+ return resultOp;
+ }
+ Component containerComponent = getResourceResult.left().value();
+ Either<ComponentInstance, StorageOperationStatus> resourceInstanceStatus = getResourceInstanceById(containerComponent, resourceInstanceId);
+ if (resourceInstanceStatus.isRight()) {
+ resultOp = Either
+ .right(componentsUtils.getResponseFormat(ActionStatus.RESOURCE_INSTANCE_NOT_FOUND_ON_SERVICE, resourceInstanceId, componentId));
+ return resultOp;
+ }
+ ComponentInstance foundResourceInstance = resourceInstanceStatus.left().value();
+ String propertyType = attribute.getType();
+ ToscaPropertyType type = ToscaPropertyType.isValidType(propertyType);
+ log.info("The type of attribute id{},is {} ", attribute.getUniqueId(), propertyType);
+ if (type == ToscaPropertyType.LIST || type == ToscaPropertyType.MAP) {
+ SchemaDefinition def = attribute.getSchema();
+ if (def == null) {
+ log.info("Schema doesn't exists for attribute of type {}", type);
+ return Either
+ .right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(StorageOperationStatus.INVALID_VALUE)));
+ }
+ PropertyDataDefinition propDef = def.getProperty();
+ if (propDef == null) {
+ log.info("Attribute in Schema Definition inside attribute of type {} doesn't exist", type);
+ return Either
+ .right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(StorageOperationStatus.INVALID_VALUE)));
+ }
+ }
+ List<ComponentInstanceAttribute> instanceAttributes = containerComponent.getComponentInstancesAttributes().get(resourceInstanceId);
+ Optional<ComponentInstanceAttribute> instanceAttribute = instanceAttributes.stream()
+ .filter(p -> p.getUniqueId().equals(attribute.getUniqueId())).findAny();
+ StorageOperationStatus status;
+ if (instanceAttribute.isPresent()) {
+ log.info("updateComponentInstanceAttribute");
+ status = toscaOperationFacade.updateComponentInstanceAttribute(containerComponent, foundResourceInstance.getUniqueId(), attribute);
+ } else {
+ log.info("addComponentInstanceAttribute");
+ status = toscaOperationFacade.addComponentInstanceAttribute(containerComponent, foundResourceInstance.getUniqueId(), attribute);
+ }
+ if (status != StorageOperationStatus.OK) {
+ ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(status);
+ resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
+ return resultOp;
+ }
+ List<String> path = new ArrayList<>();
+ path.add(foundResourceInstance.getUniqueId());
+ attribute.setPath(path);
+ foundResourceInstance.setCustomizationUUID(UUID.randomUUID().toString());
+ Either<Component, StorageOperationStatus> updateContainerRes = toscaOperationFacade
+ .updateComponentInstanceMetadataOfTopologyTemplate(containerComponent);
+ if (updateContainerRes.isRight()) {
+ ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(updateContainerRes.right().value());
+ resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
+ return resultOp;
+ }
+ resultOp = Either.left(attribute);
+ return resultOp;
+ }
+
+ private Either<String, ResponseFormat> updateComponentInstanceProperty(String containerComponentId, String componentInstanceId,
+ ComponentInstanceProperty property) {
+ Either<String, ResponseFormat> resultOp;
+ Either<Component, StorageOperationStatus> getComponent = toscaOperationFacade.getToscaElement(containerComponentId);
+ if (getComponent.isRight()) {
+ log.error("Failed to get the component information");
+ return Either.right(componentsUtils
+ .getResponseFormatForResourceInstanceProperty(ActionStatus.INVALID_CONTENT_PARAM, "Failed to get the component information"));
+ }
+ Component containerComponent = getComponent.left().value();
+ StorageOperationStatus status = toscaOperationFacade.updateComponentInstanceProperty(containerComponent, componentInstanceId, property);
+ if (status != StorageOperationStatus.OK) {
+ ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(status);
+ resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
+ return resultOp;
+ }
+ Either<Component, StorageOperationStatus> updateContainerRes = toscaOperationFacade
+ .updateComponentInstanceMetadataOfTopologyTemplate(containerComponent);
+ if (updateContainerRes.isRight()) {
+ ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(updateContainerRes.right().value());
+ resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
+ return resultOp;
+ }
+ return Either.left("Update OK");
+ }
+
+ private Either<String, ResponseFormat> getInputListDefaultValue(Component component, String inputId) {
+ List<InputDefinition> inputList = component.getInputs();
+ for (InputDefinition input : inputList) {
+ if (input.getUniqueId().equals(inputId)) {
+ if (input.getDefaultValue() == null) {
+ log.debug("The input's default value is null");
+ return Either.left(null);
+ }
+ return Either.left(input.getDefaultValue());
+ }
+ }
+ log.error("The input's default value with id {} is not found", inputId);
+ return Either.right(componentsUtils.getResponseFormat(ActionStatus.USER_DEFINED, "Failed to paste component instance to the canvas"));
+ }
+
+ /**
+ * Method to delete selected nodes and edges on composition page
+ *
+ * @param containerComponentType
+ * @param componentId
+ * @param componentInstanceIdList
+ * @param userId
+ * @return
+ */
+ public Map<String, List<String>> batchDeleteComponentInstance(String containerComponentType, String componentId,
+ List<String> componentInstanceIdList, String userId) {
+ List<String> deleteErrorIds = new ArrayList<>();
+ Map<String, List<String>> deleteErrorMap = new HashMap<>();
+ validateUserExists(userId);
+ org.openecomp.sdc.be.model.Component containerComponent = validateComponentExists(componentId,
+ ComponentTypeEnum.findByParamName(containerComponentType), null);
+ boolean failed = false;
+ try {
+ lockComponent(containerComponent, "batchDeleteComponentInstance");
+ for (String eachInstanceId : componentInstanceIdList) {
+ Either<ComponentInstance, ResponseFormat> actionResponse = batchDeleteComponentInstance(containerComponent, containerComponentType,
+ eachInstanceId);
+ log.debug("batchDeleteResourceInstances actionResponse is {}", actionResponse);
+ if (actionResponse.isRight()) {
+ log.error("Failed to delete ComponentInstance [{}]", eachInstanceId);
+ deleteErrorIds.add(eachInstanceId);
+ }
+ }
+ //sending the ids of the error nodes that were not deleted to UI
+ deleteErrorMap.put("deleteFailedIds", deleteErrorIds);
+ return deleteErrorMap;
+ } catch (ComponentException e) {
+ failed = true;
+ throw e;
+ } finally {
+ unlockComponent(failed, containerComponent);
+ }
+ }
+
+ private Either<ComponentInstance, ResponseFormat> batchDeleteComponentInstance(Component containerComponent, String containerComponentType,
+ String componentInstanceId) {
+ ComponentInstance resultOp;
+ final ComponentTypeEnum containerComponentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
+ try {
+ resultOp = deleteComponentInstance(containerComponent, componentInstanceId, containerComponentTypeEnum);
+ log.info("Successfully deleted instance with id {}", componentInstanceId);
+ return Either.left(resultOp);
+ } catch (ComponentException e) {
+ log.error("Failed to deleteComponentInstance with instanceId[{}]", componentInstanceId);
+ return Either.right(new ResponseFormat());
+ }
+ }
+
+ public void validateUser(final String userId) {
+ final User user = userValidations.validateUserExists(userId);
+ userValidations.validateUserRole(user, Arrays.asList(Role.DESIGNER, Role.ADMIN));
+ }