+ } catch (final ComponentException e) {
+ return Either.right(e.getResponseFormat());
+ } finally {
+ if (resultOp == null || resultOp.isRight()) {
+ janusGraphDao.rollback();
+ } else {
+ janusGraphDao.commit();
+ }
+ // unlock resource
+ graphLockOperation.unlockComponent(componentId, componentTypeEnum.getNodeType());
+ }
+ }
+
+ private GetInputValueDataDefinition buildGetInputValue(final ComponentInstanceProperty property) {
+ final GetInputValueDataDefinition getInputValueDataDefinition = new GetInputValueDataDefinition();
+ getInputValueDataDefinition.setPropName(property.getName());
+ getInputValueDataDefinition.setInputName(((ToscaGetFunctionDataDefinition) property.getToscaFunction()).getPropertyName());
+ getInputValueDataDefinition.setInputId(((ToscaGetFunctionDataDefinition) property.getToscaFunction()).getPropertyUniqueId());
+ return getInputValueDataDefinition;
+ }
+
+ private void addE(JSONArray jsonArray, List<String> path, String value) {
+ Object objectForPath = jsonArray.opt(Integer.parseInt(path.get(0)));
+ if (objectForPath == null) {
+ if (path.size() > 1) {
+ if (StringUtils.isNumeric(path.get(1))) {
+ objectForPath = new JSONArray();
+ } else {
+ objectForPath = new JSONObject();
+ }
+ jsonArray.put(Integer.parseInt(path.get(0)), objectForPath);
+ }
+ }
+
+ if (path.size() == 1) {
+ Object valueAsObject = new Yaml().loadAs(value, Object.class);
+ jsonArray.put(Integer.parseInt(path.get(0)), valueAsObject);
+ } else {
+ if (objectForPath instanceof JSONObject) {
+ addE((JSONObject) objectForPath, path.subList(1, path.size()), value);
+ } else {
+ addE((JSONArray) objectForPath, path.subList(1, path.size()), value);
+ }
+ }
+ }
+
+ private void addE(JSONObject jsonObject, List<String> path, String value) {
+
+ Object objectForPath = null;
+ if (jsonObject.has(path.get(0))) {
+ objectForPath = jsonObject.get(path.get(0));
+ } else {
+ if (path.size() > 1 && StringUtils.isNumeric(path.get(1))) {
+ objectForPath = new JSONArray();
+ } else {
+ objectForPath = new JSONObject();
+ }
+ jsonObject.put(path.get(0), objectForPath);
+ }
+
+ if (path.size() == 1) {
+ Object valueAsObject = new Yaml().loadAs(value, Object.class);
+ jsonObject.put(path.get(0), valueAsObject);
+ } else {
+ if (objectForPath instanceof JSONObject) {
+ addE((JSONObject) objectForPath, path.subList(1, path.size()), value);
+ } else {
+ addE((JSONArray) objectForPath, path.subList(1, path.size()), value);
+ }
+ }
+ }
+
+ private void setJsonObjectForSubProperty(final JSONObject jObject, final List<String> path, String value) {
+ if (path.size() == 1) {
+ Object valueAsObject = new Yaml().loadAs(value, Object.class);
+ jObject.put(path.get(0), valueAsObject);
+ } else {
+ if (!jObject.has(path.get(0))) {
+ jObject.put(path.get(0), new JSONObject());
+ }
+ final JSONObject jsonObject = jObject.getJSONObject(path.get(0));
+ setJsonObjectForSubProperty(jsonObject, path.subList(1, path.size()), value);
+ }
+ }
+
+ public Either<List<ComponentInstanceAttribute>, ResponseFormat> createOrUpdateAttributeValues(final ComponentTypeEnum componentTypeEnum,
+ final String componentId,
+ final String resourceInstanceId,
+ final List<ComponentInstanceAttribute> attributes,
+ final String userId) {
+ Either<List<ComponentInstanceAttribute>, ResponseFormat> resultOp = null;
+ /*-------------------------------Validations---------------------------------*/
+ validateUserExists(userId);
+
+ if (componentTypeEnum == null) {
+ BeEcompErrorManager.getInstance().logInvalidInputError("CreateOrUpdatePropertiesValues", INVALID_COMPONENT_TYPE, ErrorSeverity.INFO);
+ resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.NOT_ALLOWED));
+ return resultOp;
+ }
+ final Either<Component, StorageOperationStatus> getResourceResult = toscaOperationFacade
+ .getToscaElement(componentId, JsonParseFlagEnum.ParseAll);
+
+ if (getResourceResult.isRight()) {
+ log.debug(FAILED_TO_RETRIEVE_COMPONENT_COMPONENT_ID, componentId);
+ final ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(getResourceResult.right().value(), componentTypeEnum);
+ return Either.right(componentsUtils.getResponseFormat(actionStatus, componentId));
+ }
+ final Component containerComponent = getResourceResult.left().value();
+
+ if (!ComponentValidationUtils.canWorkOnComponent(containerComponent, userId)) {
+ if (Boolean.TRUE.equals(containerComponent.isArchived())) {
+ log.info(COMPONENT_ARCHIVED, componentId);
+ return Either.right(componentsUtils.getResponseFormat(ActionStatus.COMPONENT_IS_ARCHIVED, containerComponent.getName()));
+ }
+ log.info(RESTRICTED_OPERATION_ON_SERVIVE, userId, componentId);
+ return Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
+ }
+
+ final Either<ComponentInstance, StorageOperationStatus> resourceInstanceStatus = getResourceInstanceById(containerComponent,
+ resourceInstanceId);
+ if (resourceInstanceStatus.isRight()) {
+ return Either.right(componentsUtils.getResponseFormat(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER,
+ resourceInstanceId, RESOURCE_INSTANCE, SERVICE, componentId));
+ }
+ final ComponentInstance foundResourceInstance = resourceInstanceStatus.left().value();
+
+ // Validate instance attributes against it's constraints
+ List<PropertyDefinition> attributesToValidate = new ArrayList<>();
+ attributes.forEach((componentInstanceAttribute) -> {
+ PropertyDefinition propertyDefinition = new PropertyDefinition();
+ propertyDefinition.setValue(componentInstanceAttribute.getValue());
+ propertyDefinition.setType(componentInstanceAttribute.getType());
+ propertyDefinition.setName(componentInstanceAttribute.getName());
+ propertyDefinition.setUniqueId(componentInstanceAttribute.getUniqueId());
+ attributesToValidate.add(propertyDefinition);
+ });
+ Either<Boolean, ResponseFormat> constraintValidatorResponse = validatePropertyValueConstraint(attributesToValidate, componentId);
+ if (constraintValidatorResponse.isRight()) {
+ log.error("Failed validation value and constraint of attribute: {}", constraintValidatorResponse.right().value());
+ return Either.right(constraintValidatorResponse.right().value());
+ }
+
+ // lock resource
+ final StorageOperationStatus lockStatus = graphLockOperation.lockComponent(componentId, componentTypeEnum.getNodeType());
+ if (lockStatus != StorageOperationStatus.OK) {
+ log.debug(FAILED_TO_LOCK_SERVICE, componentId);
+ return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(lockStatus)));
+ }
+ final List<ComponentInstanceAttribute> updatedProperties = new ArrayList<>();
+ try {
+ for (final ComponentInstanceAttribute attribute : attributes) {
+ final ComponentInstanceAttribute componentInstanceProperty = validateAttributeExistsOnComponent(attribute, containerComponent,
+ foundResourceInstance);
+ final Either<String, ResponseFormat> updatedPropertyValue = updateAttributeObjectValue(attribute);
+ if (updatedPropertyValue.isRight()) {
+ log.error("Failed to update attribute object value of attribute: {}", attribute);
+ throw new ByResponseFormatComponentException(updatedPropertyValue.right().value());
+ }
+ updatedPropertyValue.bimap(
+ updatedValue -> {
+ componentInstanceProperty.setValue(updatedValue);
+ return updateAttributeOnContainerComponent(attribute, updatedValue,
+ containerComponent, foundResourceInstance);
+ }, Either::right);
+ updatedProperties.add(componentInstanceProperty);
+ }
+
+ final Either<Component, StorageOperationStatus> updateContainerRes = toscaOperationFacade
+ .updateComponentInstanceMetadataOfTopologyTemplate(containerComponent);
+ if (updateContainerRes.isRight()) {
+ final ActionStatus actionStatus = componentsUtils
+ .convertFromStorageResponseForResourceInstanceProperty(updateContainerRes.right().value());
+ resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
+ return resultOp;
+ }
+ resultOp = Either.left(updatedProperties);
+ return resultOp;
+
+ } catch (final ComponentException e) {
+ return Either.right(e.getResponseFormat());