Provide tosca function to list of map values
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / impl / ComponentInstanceBusinessLogic.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.components.impl;
22
23 import static org.openecomp.sdc.be.components.attribute.GetOutputUtils.isGetOutputValueForOutput;
24 import static org.openecomp.sdc.be.components.property.GetInputUtils.isGetInputValueForInput;
25 import static org.openecomp.sdc.be.components.utils.PropertiesUtils.getPropertyCapabilityOfChildInstance;
26
27 import com.google.common.collect.Sets;
28 import fj.data.Either;
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.Collection;
32 import java.util.Collections;
33 import java.util.HashMap;
34 import java.util.Iterator;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.Map.Entry;
38 import java.util.Objects;
39 import java.util.Optional;
40 import java.util.Set;
41 import java.util.UUID;
42 import java.util.stream.Collectors;
43 import org.apache.commons.collections.CollectionUtils;
44 import org.apache.commons.collections.MapUtils;
45 import org.apache.commons.lang3.StringUtils;
46 import org.apache.commons.lang3.tuple.ImmutablePair;
47 import org.json.JSONArray;
48 import org.json.JSONObject;
49 import org.onap.sdc.tosca.datatypes.model.PropertyType;
50 import org.openecomp.sdc.be.components.impl.exceptions.BusinessLogicException;
51 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
52 import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException;
53 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
54 import org.openecomp.sdc.be.components.impl.exceptions.ToscaGetFunctionExceptionSupplier;
55 import org.openecomp.sdc.be.components.impl.instance.ComponentInstanceChangeOperationOrchestrator;
56 import org.openecomp.sdc.be.components.impl.utils.DirectivesUtil;
57 import org.openecomp.sdc.be.components.merge.instance.ComponentInstanceMergeDataBusinessLogic;
58 import org.openecomp.sdc.be.components.merge.instance.DataForMergeHolder;
59 import org.openecomp.sdc.be.components.utils.PropertiesUtils;
60 import org.openecomp.sdc.be.components.validation.ComponentValidations;
61 import org.openecomp.sdc.be.config.BeEcompErrorManager;
62 import org.openecomp.sdc.be.config.BeEcompErrorManager.ErrorSeverity;
63 import org.openecomp.sdc.be.config.ConfigurationManager;
64 import org.openecomp.sdc.be.dao.api.ActionStatus;
65 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
66 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
67 import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary;
68 import org.openecomp.sdc.be.datamodel.utils.PropertyValueConstraintValidationUtil;
69 import org.openecomp.sdc.be.datatypes.elements.CINodeFilterDataDefinition;
70 import org.openecomp.sdc.be.datatypes.elements.CapabilityDataDefinition;
71 import org.openecomp.sdc.be.datatypes.elements.ForwardingPathDataDefinition;
72 import org.openecomp.sdc.be.datatypes.elements.GetInputValueDataDefinition;
73 import org.openecomp.sdc.be.datatypes.elements.GetOutputValueDataDefinition;
74 import org.openecomp.sdc.be.datatypes.elements.GetPolicyValueDataDefinition;
75 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
76 import org.openecomp.sdc.be.datatypes.elements.RequirementDataDefinition;
77 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
78 import org.openecomp.sdc.be.datatypes.elements.ToscaGetFunctionDataDefinition;
79 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
80 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
81 import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
82 import org.openecomp.sdc.be.datatypes.enums.PropertySource;
83 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
84 import org.openecomp.sdc.be.datatypes.tosca.ToscaGetFunctionType;
85 import org.openecomp.sdc.be.exception.BusinessException;
86 import org.openecomp.sdc.be.impl.ComponentsUtils;
87 import org.openecomp.sdc.be.impl.ForwardingPathUtils;
88 import org.openecomp.sdc.be.impl.ServiceFilterUtils;
89 import org.openecomp.sdc.be.info.CreateAndAssotiateInfo;
90 import org.openecomp.sdc.be.model.ArtifactDefinition;
91 import org.openecomp.sdc.be.model.AttributeDefinition;
92 import org.openecomp.sdc.be.model.CapabilityDefinition;
93 import org.openecomp.sdc.be.model.Component;
94 import org.openecomp.sdc.be.model.ComponentInstance;
95 import org.openecomp.sdc.be.model.ComponentInstanceAttribute;
96 import org.openecomp.sdc.be.model.ComponentInstanceInput;
97 import org.openecomp.sdc.be.model.ComponentInstanceOutput;
98 import org.openecomp.sdc.be.model.ComponentInstancePropInput;
99 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
100 import org.openecomp.sdc.be.model.ComponentParametersView;
101 import org.openecomp.sdc.be.model.DataTypeDefinition;
102 import org.openecomp.sdc.be.model.GroupDefinition;
103 import org.openecomp.sdc.be.model.InputDefinition;
104 import org.openecomp.sdc.be.model.InterfaceDefinition;
105 import org.openecomp.sdc.be.model.LifecycleStateEnum;
106 import org.openecomp.sdc.be.model.OutputDefinition;
107 import org.openecomp.sdc.be.model.PolicyDefinition;
108 import org.openecomp.sdc.be.model.PropertyConstraint;
109 import org.openecomp.sdc.be.model.PropertyDefinition;
110 import org.openecomp.sdc.be.model.RelationshipInfo;
111 import org.openecomp.sdc.be.model.RequirementCapabilityRelDef;
112 import org.openecomp.sdc.be.model.RequirementDefinition;
113 import org.openecomp.sdc.be.model.Resource;
114 import org.openecomp.sdc.be.model.Service;
115 import org.openecomp.sdc.be.model.ToscaPropertyData;
116 import org.openecomp.sdc.be.model.User;
117 import org.openecomp.sdc.be.model.jsonjanusgraph.config.ContainerInstanceTypesData;
118 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ArtifactsOperations;
119 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ForwardingPathOperation;
120 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.InterfaceOperation;
121 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.NodeFilterOperation;
122 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.NodeTemplateOperation;
123 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
124 import org.openecomp.sdc.be.model.jsonjanusgraph.utils.ModelConverter;
125 import org.openecomp.sdc.be.model.operations.StorageException;
126 import org.openecomp.sdc.be.model.operations.api.IElementOperation;
127 import org.openecomp.sdc.be.model.operations.api.IGroupInstanceOperation;
128 import org.openecomp.sdc.be.model.operations.api.IGroupOperation;
129 import org.openecomp.sdc.be.model.operations.api.IGroupTypeOperation;
130 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
131 import org.openecomp.sdc.be.model.operations.impl.ComponentInstanceOperation;
132 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
133 import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation;
134 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
135 import org.openecomp.sdc.be.model.operations.utils.ComponentValidationUtils;
136 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
137 import org.openecomp.sdc.be.model.validation.ToscaFunctionValidator;
138 import org.openecomp.sdc.be.resources.data.ComponentInstanceData;
139 import org.openecomp.sdc.be.user.Role;
140 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
141 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
142 import org.openecomp.sdc.common.api.Constants;
143 import org.openecomp.sdc.common.datastructure.Wrapper;
144 import org.openecomp.sdc.common.jsongraph.util.CommonUtility;
145 import org.openecomp.sdc.common.jsongraph.util.CommonUtility.LogLevelEnum;
146 import org.openecomp.sdc.common.log.elements.ErrorLogOptionalData;
147 import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode;
148 import org.openecomp.sdc.common.log.wrappers.Logger;
149 import org.openecomp.sdc.common.util.ValidationUtils;
150 import org.openecomp.sdc.exception.ResponseFormat;
151 import org.springframework.beans.factory.annotation.Autowired;
152 import org.yaml.snakeyaml.Yaml;
153
154 @org.springframework.stereotype.Component
155 public class ComponentInstanceBusinessLogic extends BaseBusinessLogic {
156
157     private static final Logger log = Logger.getLogger(ComponentInstanceBusinessLogic.class);
158     private static final String VF_MODULE = "org.openecomp.groups.VfModule";
159     private static final String TRY_TO_CREATE_ENTRY_ON_GRAPH = "Try to create entry on graph";
160     private static final String CLOUD_SPECIFIC_FIXED_KEY_WORD = "cloudtech";
161     private static final String[][] CLOUD_SPECIFIC_KEY_WORDS = {{"k8s", "azure", "aws"}, /* cloud specific technology */
162         {"charts", "day0", "configtemplate"} /*cloud specific sub type*/};
163     private static final String FAILED_TO_CREATE_ENTRY_ON_GRAPH_FOR_COMPONENT_INSTANCE = "Failed to create entry on graph for component instance {}";
164     private static final String ENTITY_ON_GRAPH_IS_CREATED = "Entity on graph is created.";
165     private static final String INVALID_COMPONENT_TYPE = "invalid component type";
166     private static final String FAILED_TO_RETRIEVE_COMPONENT_COMPONENT_ID = "Failed to retrieve component, component id {}";
167     private static final String FAILED_TO_LOCK_SERVICE = "Failed to lock service {}";
168     private static final String CREATE_OR_UPDATE_PROPERTY_VALUE = "CreateOrUpdatePropertyValue";
169     private static final String FAILED_TO_COPY_COMP_INSTANCE_TO_CANVAS = "Failed to copy the component instance to the canvas";
170     private static final String COPY_COMPONENT_INSTANCE_OK = "Copy component instance OK";
171     private static final String CANNOT_ATTACH_RESOURCE_INSTANCES_TO_CONTAINER_RESOURCE_OF_TYPE =
172         "Cannot attach resource instances to container resource of type {}";
173     private static final String FAILED_TO_UPDATE_COMPONENT_INSTANCE_CAPABILITY = "Failed to update component instance capability on instance {} in "
174         + "container {}";
175     private static final String SERVICE_PROXY = "serviceProxy";
176     private static final String ASSOCIATE_RI_TO_RI = "associateRIToRI";
177     private static final String COMPONENT_ARCHIVED = "Component is archived. Component id: {}";
178     private static final String RESTRICTED_OPERATION_ON_SERVIVE = "Restricted operation for user: {} on service {}";
179     private static final String FAILED_TO_LOCK_COMPONENT = "Failed to lock component {}";
180     private static final String RESTRICTED_OPERATION_ON_COMPONENT = "Restricted operation for user: {} on component {}";
181     private static final String RESOURCE_INSTANCE = "resource instance";
182     private static final String SERVICE = "service";
183
184     private final ComponentInstanceOperation componentInstanceOperation;
185     private final ArtifactsBusinessLogic artifactBusinessLogic;
186     private final ComponentInstanceMergeDataBusinessLogic compInstMergeDataBL;
187     private final ComponentInstanceChangeOperationOrchestrator onChangeInstanceOperationOrchestrator;
188     private final ForwardingPathOperation forwardingPathOperation;
189     private final NodeFilterOperation nodeFilterOperation;
190     private final ToscaFunctionValidator toscaFunctionValidator;
191     private final PropertyBusinessLogic propertyBusinessLogic;
192     @Autowired
193     private CompositionBusinessLogic compositionBusinessLogic;
194     @Autowired
195     private ContainerInstanceTypesData containerInstanceTypesData;
196
197     @Autowired
198     public ComponentInstanceBusinessLogic(IElementOperation elementDao, IGroupOperation groupOperation,
199                                           IGroupInstanceOperation groupInstanceOperation, IGroupTypeOperation groupTypeOperation,
200                                           InterfaceOperation interfaceOperation, InterfaceLifecycleOperation interfaceLifecycleTypeOperation,
201                                           ComponentInstanceOperation componentInstanceOperation, ArtifactsBusinessLogic artifactBusinessLogic,
202                                           ComponentInstanceMergeDataBusinessLogic compInstMergeDataBL,
203                                           ComponentInstanceChangeOperationOrchestrator onChangeInstanceOperationOrchestrator,
204                                           ForwardingPathOperation forwardingPathOperation, NodeFilterOperation nodeFilterOperation,
205                                           ArtifactsOperations artifactToscaOperation, final ToscaFunctionValidator toscaFunctionValidator,
206                                           PropertyBusinessLogic propertyBusinessLogic) {
207         super(elementDao, groupOperation, groupInstanceOperation, groupTypeOperation, interfaceOperation, interfaceLifecycleTypeOperation,
208             artifactToscaOperation);
209         this.componentInstanceOperation = componentInstanceOperation;
210         this.artifactBusinessLogic = artifactBusinessLogic;
211         this.compInstMergeDataBL = compInstMergeDataBL;
212         this.onChangeInstanceOperationOrchestrator = onChangeInstanceOperationOrchestrator;
213         this.forwardingPathOperation = forwardingPathOperation;
214         this.nodeFilterOperation = nodeFilterOperation;
215         this.toscaFunctionValidator = toscaFunctionValidator;
216         this.propertyBusinessLogic = propertyBusinessLogic;
217     }
218
219     public ComponentInstance createComponentInstance(String containerComponentParam, String containerComponentId, String userId,
220                                                      ComponentInstance resourceInstance) {
221         return createComponentInstance(containerComponentParam, containerComponentId, userId, resourceInstance, true);
222     }
223
224     public List<ComponentInstanceProperty> getComponentInstancePropertiesByInputId(org.openecomp.sdc.be.model.Component component, String inputId) {
225         List<ComponentInstanceProperty> resList = new ArrayList<>();
226         Map<String, List<ComponentInstanceProperty>> ciPropertiesMap = component.getComponentInstancesProperties();
227         if (ciPropertiesMap != null && !ciPropertiesMap.isEmpty()) {
228             ciPropertiesMap.forEach((s, ciPropList) -> {
229                 String ciName = "";
230                 Optional<ComponentInstance> ciOp = component.getComponentInstances().stream().filter(ci -> ci.getUniqueId().equals(s)).findAny();
231                 if (ciOp.isPresent()) {
232                     ciName = ciOp.get().getName();
233                 }
234                 if (ciPropList != null && !ciPropList.isEmpty()) {
235                     for (ComponentInstanceProperty prop : ciPropList) {
236                         List<GetInputValueDataDefinition> inputsValues = prop.getGetInputValues();
237                         addCompInstanceProperty(s, ciName, prop, inputsValues, inputId, resList);
238                     }
239                 }
240             });
241         }
242         return resList;
243     }
244
245     public List<ComponentInstanceAttribute> getComponentInstanceAttributesByOutputId(final org.openecomp.sdc.be.model.Component component,
246                                                                                      final String outputId) {
247         final List<ComponentInstanceAttribute> resList = new ArrayList<>();
248         final Map<String, List<ComponentInstanceAttribute>> componentInstancesAttributes = component.getComponentInstancesAttributes();
249         if (org.apache.commons.collections4.MapUtils.isNotEmpty(componentInstancesAttributes)) {
250             componentInstancesAttributes.forEach((s, componentInstanceAttributeList) -> {
251                 String ciName = "";
252                 final Optional<ComponentInstance> ciOp = component.getComponentInstances().stream().filter(ci -> ci.getUniqueId().equals(s))
253                     .findAny();
254                 if (ciOp.isPresent()) {
255                     ciName = ciOp.get().getName();
256                 }
257                 if (componentInstanceAttributeList != null && !componentInstanceAttributeList.isEmpty()) {
258                     for (final ComponentInstanceAttribute compInstanceAttribute : componentInstanceAttributeList) {
259                         List<GetOutputValueDataDefinition> outputValues = compInstanceAttribute.getGetOutputValues();
260                         addCompInstanceAttribute(s, ciName, compInstanceAttribute, outputValues, outputId, resList);
261                     }
262                 }
263             });
264         }
265         return resList;
266     }
267
268     private void addCompInstanceProperty(String s, String ciName, ComponentInstanceProperty prop, List<GetInputValueDataDefinition> inputsValues,
269                                          String inputId, List<ComponentInstanceProperty> resList) {
270         if (inputsValues != null && !inputsValues.isEmpty()) {
271             for (GetInputValueDataDefinition inputData : inputsValues) {
272                 if (isGetInputValueForInput(inputData, inputId)) {
273                     prop.setComponentInstanceId(s);
274                     prop.setComponentInstanceName(ciName);
275                     resList.add(prop);
276                     break;
277                 }
278             }
279         }
280     }
281
282     private void addCompInstanceAttribute(final String s, final String ciName, final ComponentInstanceAttribute attribute,
283                                           final List<GetOutputValueDataDefinition> outputsValues, final String outputId,
284                                           final List<ComponentInstanceAttribute> resList) {
285         if (outputsValues != null && !outputsValues.isEmpty()) {
286             for (final GetOutputValueDataDefinition outputData : outputsValues) {
287                 if (isGetOutputValueForOutput(outputData, outputId)) {
288                     attribute.setComponentInstanceId(s);
289                     attribute.setComponentInstanceName(ciName);
290                     resList.add(attribute);
291                     break;
292                 }
293             }
294         }
295     }
296
297     public Optional<ComponentInstanceProperty> getComponentInstancePropertyByPolicyId(Component component, PolicyDefinition policy) {
298         Optional<ComponentInstanceProperty> propertyCandidate = getComponentInstancePropertyByPolicy(component, policy);
299         if (propertyCandidate.isPresent()) {
300             ComponentInstanceProperty componentInstanceProperty = propertyCandidate.get();
301             Optional<GetPolicyValueDataDefinition> getPolicyCandidate = getGetPolicyValueDataDefinition(policy, componentInstanceProperty);
302             getPolicyCandidate
303                 .ifPresent(getPolicyValue -> updateComponentInstancePropertyAfterUndeclaration(componentInstanceProperty, getPolicyValue, policy));
304             return Optional.of(componentInstanceProperty);
305         }
306         return Optional.empty();
307     }
308
309     private void updateComponentInstancePropertyAfterUndeclaration(ComponentInstanceProperty componentInstanceProperty,
310                                                                    GetPolicyValueDataDefinition getPolicyValue, PolicyDefinition policyDefinition) {
311         componentInstanceProperty.setValue(getPolicyValue.getOrigPropertyValue());
312         List<GetPolicyValueDataDefinition> getPolicyValues = componentInstanceProperty.getGetPolicyValues();
313         if (CollectionUtils.isNotEmpty(getPolicyValues)) {
314             getPolicyValues.remove(getPolicyValue);
315             componentInstanceProperty.setGetPolicyValues(getPolicyValues);
316             policyDefinition.setGetPolicyValues(getPolicyValues);
317         }
318     }
319
320     private Optional<GetPolicyValueDataDefinition> getGetPolicyValueDataDefinition(PolicyDefinition policy,
321                                                                                    ComponentInstanceProperty componentInstanceProperty) {
322         List<GetPolicyValueDataDefinition> getPolicyValues = policy.getGetPolicyValues();
323         return getPolicyValues.stream().filter(getPolicyValue -> getPolicyValue.getPropertyName().equals(componentInstanceProperty.getName()))
324             .findAny();
325     }
326
327     private Optional<ComponentInstanceProperty> getComponentInstancePropertyByPolicy(Component component, PolicyDefinition policy) {
328         Map<String, List<ComponentInstanceProperty>> componentInstancesProperties = component.getComponentInstancesProperties();
329         if (MapUtils.isEmpty(componentInstancesProperties)) {
330             return Optional.empty();
331         }
332         String instanceUniqueId = policy.getInstanceUniqueId();
333         List<ComponentInstanceProperty> componentInstanceProperties =
334             componentInstancesProperties.containsKey(instanceUniqueId) ? componentInstancesProperties.get(instanceUniqueId) : new ArrayList<>();
335         return componentInstanceProperties.stream().filter(property -> property.getName().equals(policy.getName())).findAny();
336     }
337
338     public List<ComponentInstanceInput> getComponentInstanceInputsByInputId(org.openecomp.sdc.be.model.Component component, String inputId) {
339         List<ComponentInstanceInput> resList = new ArrayList<>();
340         Map<String, List<ComponentInstanceInput>> ciInputsMap = component.getComponentInstancesInputs();
341         if (ciInputsMap != null && !ciInputsMap.isEmpty()) {
342             ciInputsMap.forEach((s, ciPropList) -> {
343                 String ciName = "";
344                 Optional<ComponentInstance> ciOp = component.getComponentInstances().stream().filter(ci -> ci.getUniqueId().equals(s)).findAny();
345                 if (ciOp.isPresent()) {
346                     ciName = ciOp.get().getName();
347                 }
348                 if (ciPropList != null && !ciPropList.isEmpty()) {
349                     for (ComponentInstanceInput prop : ciPropList) {
350                         List<GetInputValueDataDefinition> inputsValues = prop.getGetInputValues();
351                         addCompInstanceInput(s, ciName, prop, inputsValues, inputId, resList);
352                     }
353                 }
354             });
355         }
356         return resList;
357     }
358
359     public List<ComponentInstanceOutput> getComponentInstanceOutputsByOutputId(final org.openecomp.sdc.be.model.Component component,
360                                                                                final String outputId) {
361         final List<ComponentInstanceOutput> resList = new ArrayList<>();
362         final Map<String, List<ComponentInstanceOutput>> ciInputsMap = component.getComponentInstancesOutputs();
363         if (ciInputsMap != null && !ciInputsMap.isEmpty()) {
364             ciInputsMap.forEach((s, ciPropList) -> {
365                 String ciName = "";
366                 final Optional<ComponentInstance> ciOp = component.getComponentInstances().stream().filter(ci -> ci.getUniqueId().equals(s))
367                     .findAny();
368                 if (ciOp.isPresent()) {
369                     ciName = ciOp.get().getName();
370                 }
371                 if (ciPropList != null && !ciPropList.isEmpty()) {
372                     for (final ComponentInstanceOutput prop : ciPropList) {
373                         final List<GetOutputValueDataDefinition> outputValues = prop.getGetOutputValues();
374                         addCompInstanceOutput(s, ciName, prop, outputValues, outputId, resList);
375                     }
376                 }
377             });
378         }
379         return resList;
380     }
381
382     private void addCompInstanceInput(String s, String ciName, ComponentInstanceInput prop, List<GetInputValueDataDefinition> inputsValues,
383                                       String inputId, List<ComponentInstanceInput> resList) {
384         if (inputsValues != null && !inputsValues.isEmpty()) {
385             for (GetInputValueDataDefinition inputData : inputsValues) {
386                 if (isGetInputValueForInput(inputData, inputId)) {
387                     prop.setComponentInstanceId(s);
388                     prop.setComponentInstanceName(ciName);
389                     resList.add(prop);
390                     break;
391                 }
392             }
393         }
394     }
395
396     private void addCompInstanceOutput(final String s, final String ciName, final ComponentInstanceOutput prop,
397                                        final List<GetOutputValueDataDefinition> outputsValues, final String outputId,
398                                        final List<ComponentInstanceOutput> resList) {
399         if (outputsValues != null && !outputsValues.isEmpty()) {
400             for (final GetOutputValueDataDefinition outputData : outputsValues) {
401                 if (isGetOutputValueForOutput(outputData, outputId)) {
402                     prop.setComponentInstanceId(s);
403                     prop.setComponentInstanceName(ciName);
404                     resList.add(prop);
405                     break;
406                 }
407             }
408         }
409     }
410
411     public ComponentInstance createComponentInstance(final String containerComponentParam, final String containerComponentId, final String userId,
412                                                      final ComponentInstance resourceInstance, final boolean needLock) {
413         final User user = validateUserExists(userId);
414         validateUserNotEmpty(user, "Create component instance");
415         validateJsonBody(resourceInstance, ComponentInstance.class);
416         final ComponentTypeEnum containerComponentType = validateComponentType(containerComponentParam);
417         final org.openecomp.sdc.be.model.Component containerComponent = validateComponentExists(containerComponentId, containerComponentType, null);
418         if (ModelConverter.isAtomicComponent(containerComponent)) {
419             if (log.isDebugEnabled()) {
420                 log.debug(CANNOT_ATTACH_RESOURCE_INSTANCES_TO_CONTAINER_RESOURCE_OF_TYPE, containerComponent.assetType());
421             }
422             throw new ByActionStatusComponentException(ActionStatus.RESOURCE_CANNOT_CONTAIN_RESOURCE_INSTANCES, containerComponent.assetType());
423         }
424         validateCanWorkOnComponent(containerComponent, userId);
425         Component origComponent = null;
426         if (resourceInstance != null && containerComponentType != null) {
427             final OriginTypeEnum originType = resourceInstance.getOriginType();
428             validateInstanceName(resourceInstance);
429             if (originType == OriginTypeEnum.ServiceProxy) {
430                 origComponent = getOrigComponentForServiceProxy(containerComponent, resourceInstance);
431             } else if (originType == OriginTypeEnum.ServiceSubstitution) {
432                 origComponent = getOrigComponentForServiceSubstitution(resourceInstance);
433             } else {
434                 origComponent = getAndValidateOriginComponentOfComponentInstance(containerComponent, resourceInstance);
435                 validateOriginAndResourceInstanceTypes(containerComponent, origComponent, originType);
436             }
437             validateResourceInstanceState(containerComponent, origComponent);
438             overrideFields(origComponent, resourceInstance);
439             compositionBusinessLogic.validateAndSetDefaultCoordinates(resourceInstance);
440         }
441         return createComponent(needLock, containerComponent, origComponent, resourceInstance, user);
442     }
443
444     private Component getOrigComponentForServiceProxy(org.openecomp.sdc.be.model.Component containerComponent, ComponentInstance resourceInstance) {
445         Either<Component, StorageOperationStatus> serviceProxyOrigin = toscaOperationFacade.getLatestByName(SERVICE_PROXY, null);
446         if (isServiceProxyOrigin(serviceProxyOrigin)) {
447             throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(serviceProxyOrigin.right().value()));
448         }
449         Component origComponent = serviceProxyOrigin.left().value();
450         StorageOperationStatus fillProxyRes = fillInstanceData(resourceInstance, origComponent);
451         if (isFillProxyRes(fillProxyRes)) {
452             throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(fillProxyRes));
453         }
454         validateOriginAndResourceInstanceTypes(containerComponent, origComponent, OriginTypeEnum.ServiceProxy);
455         return origComponent;
456     }
457
458     private Component getOrigComponentForServiceSubstitution(ComponentInstance resourceInstance) {
459         final Either<Component, StorageOperationStatus> getServiceResult = toscaOperationFacade
460             .getToscaFullElement(resourceInstance.getComponentUid());
461         if (getServiceResult.isRight()) {
462             throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(getServiceResult.right().value()));
463         }
464         final Component service = getServiceResult.left().value();
465         final Either<Component, StorageOperationStatus> getServiceDerivedFromTypeResult = toscaOperationFacade
466             .getLatestByToscaResourceName(service.getDerivedFromGenericType(), service.getModel());
467         if (getServiceDerivedFromTypeResult.isRight()) {
468             throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(getServiceResult.right().value()));
469         }
470         Component origComponent = getServiceDerivedFromTypeResult.left().value();
471         final StorageOperationStatus fillProxyRes = fillInstanceData(resourceInstance, origComponent);
472         if (isFillProxyRes(fillProxyRes)) {
473             throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(fillProxyRes));
474         }
475         return origComponent;
476     }
477
478     private ComponentInstance createComponent(boolean needLock, Component containerComponent, Component origComponent,
479                                               ComponentInstance resourceInstance, User user) {
480         boolean failed = false;
481         try {
482             lockIfNeed(needLock, containerComponent);
483             log.debug(TRY_TO_CREATE_ENTRY_ON_GRAPH);
484             return createComponentInstanceOnGraph(containerComponent, origComponent, resourceInstance, user);
485         } catch (ComponentException e) {
486             failed = true;
487             throw e;
488         } finally {
489             if (needLock) {
490                 unlockComponent(failed, containerComponent);
491             }
492         }
493     }
494
495     /**
496      * Try using either to make a judgment
497      *
498      * @param containerComponentParam
499      * @param containerComponentId
500      * @param userId
501      * @param resourceInstance
502      * @return
503      */
504     public Either<ComponentInstance, ResponseFormat> createRealComponentInstance(String containerComponentParam, String containerComponentId,
505                                                                                  String userId, ComponentInstance resourceInstance) {
506         log.debug("enter createRealComponentInstance");
507         return createRealComponentInstance(containerComponentParam, containerComponentId, userId, resourceInstance, true);
508     }
509
510     /**
511      * Try using either to make a judgment
512      *
513      * @param needLock
514      * @param containerComponentParam
515      * @param containerComponentId
516      * @param userId
517      * @param resourceInstance
518      * @return
519      */
520     public Either<ComponentInstance, ResponseFormat> createRealComponentInstance(String containerComponentParam, String containerComponentId,
521                                                                                  String userId, ComponentInstance resourceInstance,
522                                                                                  boolean needLock) {
523         log.debug("enter createRealComponentInstance");
524         Component origComponent = null;
525         User user;
526         org.openecomp.sdc.be.model.Component containerComponent = null;
527         ComponentTypeEnum containerComponentType;
528         try {
529             user = validateUserExists(userId);
530             validateUserNotEmpty(user, "Create component instance");
531             validateJsonBody(resourceInstance, ComponentInstance.class);
532             containerComponentType = validateComponentType(containerComponentParam);
533             containerComponent = validateComponentExists(containerComponentId, containerComponentType, null);
534             log.debug("enter createRealComponentInstance,validate user json success");
535             if (ModelConverter.isAtomicComponent(containerComponent)) {
536                 log.debug(CANNOT_ATTACH_RESOURCE_INSTANCES_TO_CONTAINER_RESOURCE_OF_TYPE, containerComponent.assetType());
537                 throw new ByActionStatusComponentException(ActionStatus.RESOURCE_CANNOT_CONTAIN_RESOURCE_INSTANCES, containerComponent.assetType());
538             }
539             validateCanWorkOnComponent(containerComponent, userId);
540             log.debug("enter createRealComponentInstance,validateCanWorkOnComponent success");
541             if (resourceInstance != null && containerComponentType != null) {
542                 log.debug("enter createRealComponentInstance,start create ComponentInstance");
543                 OriginTypeEnum originType = resourceInstance.getOriginType();
544                 validateInstanceName(resourceInstance);
545                 if (originType == OriginTypeEnum.ServiceProxy) {
546                     log.debug("enter createRealComponentInstance,originType equals ServiceProxy");
547                     Either<Component, StorageOperationStatus> serviceProxyOrigin = toscaOperationFacade.getLatestByName(SERVICE_PROXY, null);
548                     if (isServiceProxyOrigin(serviceProxyOrigin)) {
549                         throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(serviceProxyOrigin.right().value()));
550                     }
551                     origComponent = serviceProxyOrigin.left().value();
552                     StorageOperationStatus fillProxyRes = fillInstanceData(resourceInstance, origComponent);
553                     if (isFillProxyRes(fillProxyRes)) {
554                         throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(fillProxyRes));
555                     }
556                 } else {
557                     log.debug("enter createRealComponentInstance,originType is not ServiceProxy");
558                     origComponent = getAndValidateOriginComponentOfComponentInstance(containerComponent, resourceInstance);
559                 }
560                 validateOriginAndResourceInstanceTypes(containerComponent, origComponent, originType);
561                 validateResourceInstanceState(containerComponent, origComponent);
562                 overrideFields(origComponent, resourceInstance);
563                 compositionBusinessLogic.validateAndSetDefaultCoordinates(resourceInstance);
564                 log.debug("enter createRealComponentInstance,final validate success");
565             }
566             return createRealComponent(needLock, containerComponent, origComponent, resourceInstance, user);
567         } catch (ComponentException e) {
568             log.debug("create Real Component Instance failed");
569             throw e;
570         }
571     }
572
573     private Either<ComponentInstance, ResponseFormat> createRealComponent(boolean needLock, Component containerComponent, Component origComponent,
574                                                                           ComponentInstance resourceInstance, User user) {
575         log.debug("enter createRealComponent");
576         boolean failed = false;
577         try {
578             lockIfNeed(needLock, containerComponent);
579             log.debug(TRY_TO_CREATE_ENTRY_ON_GRAPH);
580             return createRealComponentInstanceOnGraph(containerComponent, origComponent, resourceInstance, user);
581         } catch (ComponentException e) {
582             failed = true;
583             throw e;
584         } finally {
585             if (needLock) {
586                 unlockComponent(failed, containerComponent);
587             }
588         }
589     }
590
591     private Either<ComponentInstance, ResponseFormat> createRealComponentInstanceOnGraph(org.openecomp.sdc.be.model.Component containerComponent,
592                                                                                          Component originComponent,
593                                                                                          ComponentInstance componentInstance, User user) {
594         log.debug("enter createRealComponentInstanceOnGraph");
595         Either<ImmutablePair<Component, String>, StorageOperationStatus> result = toscaOperationFacade
596             .addComponentInstanceToTopologyTemplate(containerComponent, originComponent, componentInstance, false, user);
597         if (result.isRight()) {
598             log.debug("enter createRealComponentInstanceOnGraph,result is right");
599             ActionStatus status = componentsUtils.convertFromStorageResponse(result.right().value());
600             log.debug(FAILED_TO_CREATE_ENTRY_ON_GRAPH_FOR_COMPONENT_INSTANCE, componentInstance.getName());
601             return Either.right(componentsUtils.getResponseFormat(status));
602         }
603         log.debug(ENTITY_ON_GRAPH_IS_CREATED);
604         log.debug("enter createRealComponentInstanceOnGraph,Entity on graph is created.");
605         Component updatedComponent = result.left().value().getLeft();
606         Map<String, String> existingEnvVersions = new HashMap<>();
607         // TODO existingEnvVersions ??
608         addComponentInstanceArtifacts(updatedComponent, componentInstance, originComponent, user, existingEnvVersions);
609         Optional<ComponentInstance> updatedInstanceOptional = updatedComponent.getComponentInstances().stream()
610             .filter(ci -> ci.getUniqueId().equals(result.left().value().getRight())).findFirst();
611         if (!updatedInstanceOptional.isPresent()) {
612             log.debug("Failed to fetch new added component instance {} from component {}", componentInstance.getName(), containerComponent.getName());
613             throw new ByActionStatusComponentException(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER, componentInstance.getName());
614         }
615         log.debug("enter createRealComponentInstanceOnGraph,and final success");
616         return Either.left(updatedInstanceOptional.get());
617     }
618
619     private void overrideFields(Component origComponent, ComponentInstance resourceInstance) {
620         resourceInstance.setComponentVersion(origComponent.getVersion());
621         resourceInstance.setIcon(origComponent.getIcon());
622     }
623
624     private void validateInstanceName(ComponentInstance resourceInstance) {
625         String resourceInstanceName = resourceInstance.getName();
626         if (StringUtils.isEmpty(resourceInstanceName)) {
627             log.debug("ComponentInstance name is empty");
628             throw new ByActionStatusComponentException(ActionStatus.INVALID_COMPONENT_NAME, resourceInstance.getName());
629         }
630         if (!ValidationUtils.validateComponentNameLength(resourceInstanceName)) {
631             log.debug("ComponentInstance name exceeds max length {} ", ValidationUtils.COMPONENT_NAME_MAX_LENGTH);
632             throw new ByActionStatusComponentException(ActionStatus.INVALID_COMPONENT_NAME, resourceInstance.getName());
633         }
634         if (!ValidationUtils.validateComponentNamePattern(resourceInstanceName)) {
635             log.debug("ComponentInstance name {} has invalid format", resourceInstanceName);
636             throw new ByActionStatusComponentException(ActionStatus.INVALID_COMPONENT_NAME, resourceInstance.getName());
637         }
638     }
639
640     private void validateResourceInstanceState(Component containerComponent, Component origComponent) {
641         if (origComponent.getLifecycleState() == LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT) {
642             throw new ByActionStatusComponentException(ActionStatus.CONTAINER_CANNOT_CONTAIN_INSTANCE,
643                 containerComponent.getComponentType().getValue(), origComponent.getLifecycleState().toString());
644         }
645     }
646
647     private void validateOriginAndResourceInstanceTypes(final Component containerComponent, final Component origComponent,
648                                                         final OriginTypeEnum originType) {
649         final ResourceTypeEnum resourceType = getResourceTypeEnumFromOriginComponent(origComponent);
650         validateOriginType(originType, resourceType);
651         validateOriginComponentIsValidForContainer(containerComponent, resourceType);
652     }
653
654     private void validateOriginComponentIsValidForContainer(Component containerComponent, ResourceTypeEnum resourceType) {
655         switch (containerComponent.getComponentType()) {
656             case SERVICE:
657                 if (!containerInstanceTypesData.isAllowedForServiceComponent(resourceType, containerComponent.getModel())) {
658                     throw new ByActionStatusComponentException(ActionStatus.CONTAINER_CANNOT_CONTAIN_INSTANCE,
659                         containerComponent.getComponentType().toString(), resourceType.name());
660                 }
661                 break;
662             case RESOURCE:
663                 final ResourceTypeEnum componentResourceType = ((Resource) containerComponent).getResourceType();
664                 if (!containerInstanceTypesData.isAllowedForResourceComponent(componentResourceType, resourceType)) {
665                     throw new ByActionStatusComponentException(ActionStatus.CONTAINER_CANNOT_CONTAIN_INSTANCE,
666                         containerComponent.getComponentType().toString(), resourceType.name());
667                 }
668                 break;
669             default:
670                 throw new ByActionStatusComponentException(ActionStatus.INVALID_CONTENT);
671         }
672     }
673
674     private void validateOriginType(OriginTypeEnum originType, ResourceTypeEnum resourceType) {
675         ResourceTypeEnum convertedOriginType;
676         try {
677             convertedOriginType = ResourceTypeEnum.getTypeIgnoreCase(originType.name());
678         } catch (Exception e) {
679             throw new ByActionStatusComponentException(ActionStatus.INVALID_CONTENT);
680         }
681         if (resourceType != convertedOriginType) {
682             throw new ByActionStatusComponentException(ActionStatus.INVALID_CONTENT);
683         }
684     }
685
686     private ResourceTypeEnum getResourceTypeEnumFromOriginComponent(final Component origComponent) {
687         switch (origComponent.getComponentType()) {
688             case SERVICE:
689                 return ResourceTypeEnum.ServiceProxy;
690             case RESOURCE:
691                 return ((Resource) origComponent).getResourceType();
692             default:
693                 throw new ByActionStatusComponentException(ActionStatus.INVALID_CONTENT);
694         }
695     }
696
697     private void lockIfNeed(boolean needLock, Component containerComponent) {
698         if (needLock) {
699             lockComponent(containerComponent, "createComponentInstance");
700         }
701     }
702
703     private boolean isServiceProxyOrigin(Either<Component, StorageOperationStatus> serviceProxyOrigin) {
704         if (serviceProxyOrigin.isRight()) {
705             log.debug("Failed to fetch normative service proxy resource by tosca name, error {}", serviceProxyOrigin.right().value());
706             return true;
707         }
708         return false;
709     }
710
711     private StorageOperationStatus fillInstanceData(ComponentInstance resourceInstance, Component origComponent) {
712         final ComponentParametersView filter = new ComponentParametersView(true);
713         filter.setIgnoreCapabilities(false);
714         filter.setIgnoreCapabiltyProperties(false);
715         filter.setIgnoreComponentInstances(false);
716         filter.setIgnoreRequirements(false);
717         filter.setIgnoreInterfaces(false);
718         filter.setIgnoreProperties(false);
719         filter.setIgnoreAttributes(false);
720         filter.setIgnoreInputs(false);
721         Either<Component, StorageOperationStatus> serviceRes = toscaOperationFacade.getToscaElement(resourceInstance.getComponentUid(), filter);
722         if (serviceRes.isRight()) {
723             return serviceRes.right().value();
724         }
725         final Component service = serviceRes.left().value();
726         final Map<String, List<CapabilityDefinition>> capabilities = service.getCapabilities();
727         resourceInstance.setCapabilities(capabilities);
728         final Map<String, List<RequirementDefinition>> req = service.getRequirements();
729         resourceInstance.setRequirements(req);
730         final Map<String, InterfaceDefinition> serviceInterfaces = service.getInterfaces();
731         if (MapUtils.isNotEmpty(serviceInterfaces)) {
732             serviceInterfaces.forEach(resourceInstance::addInterface);
733         }
734         resourceInstance.setProperties(PropertiesUtils.getProperties(service));
735         resourceInstance.setAttributes(service.getAttributes());
736         final List<InputDefinition> serviceInputs = service.getInputs();
737         resourceInstance.setInputs(serviceInputs);
738         resourceInstance.setSourceModelInvariant(service.getInvariantUUID());
739         resourceInstance.setSourceModelName(service.getName());
740         resourceInstance.setSourceModelUuid(service.getUUID());
741         resourceInstance.setSourceModelUid(service.getUniqueId());
742         resourceInstance.setComponentUid(origComponent.getUniqueId());
743         resourceInstance.setComponentVersion(service.getVersion());
744         switch (resourceInstance.getOriginType()) {
745             case ServiceProxy:
746                 return fillProxyInstanceData(resourceInstance, origComponent, service);
747             case ServiceSubstitution:
748                 return fillServiceSubstitutableNodeTypeData(resourceInstance, service);
749             default:
750                 return StorageOperationStatus.OK;
751         }
752     }
753
754     private StorageOperationStatus fillProxyInstanceData(final ComponentInstance resourceInstance, final Component origComponent,
755                                                          final Component service) {
756         final String name = ValidationUtils.normalizeComponentInstanceName(service.getName()) + ToscaOperationFacade.PROXY_SUFFIX;
757         final String toscaResourceName = ((Resource) origComponent).getToscaResourceName();
758         final int lastIndexOf = toscaResourceName.lastIndexOf('.');
759         if (lastIndexOf != -1) {
760             final String proxyToscaName = toscaResourceName.substring(0, lastIndexOf + 1) + name;
761             resourceInstance.setToscaComponentName(proxyToscaName);
762         }
763         resourceInstance.setName(name);
764         resourceInstance.setIsProxy(true);
765         resourceInstance.setDescription("A Proxy for Service " + service.getName());
766         return StorageOperationStatus.OK;
767     }
768
769     private StorageOperationStatus fillServiceSubstitutableNodeTypeData(final ComponentInstance resourceInstance, final Component service) {
770         resourceInstance.setToscaComponentName("org.openecomp.service." + ValidationUtils.convertToSystemName(service.getName()));
771         resourceInstance.setName(ValidationUtils.normalizeComponentInstanceName(service.getName()));
772         resourceInstance.setIsProxy(false);
773         resourceInstance.setDescription("A substitutable node type for service " + service.getName());
774         return StorageOperationStatus.OK;
775     }
776
777     public Either<CreateAndAssotiateInfo, ResponseFormat> createAndAssociateRIToRI(String containerComponentParam, String containerComponentId,
778                                                                                    String userId, CreateAndAssotiateInfo createAndAssotiateInfo) {
779         Either<CreateAndAssotiateInfo, ResponseFormat> resultOp = null;
780         ComponentInstance resourceInstance = createAndAssotiateInfo.getNode();
781         RequirementCapabilityRelDef associationInfo = createAndAssotiateInfo.getAssociate();
782         User user = validateUserExists(userId);
783         final ComponentTypeEnum containerComponentType = validateComponentType(containerComponentParam);
784         org.openecomp.sdc.be.model.Component containerComponent = validateComponentExists(containerComponentId, containerComponentType, null);
785         if (ModelConverter.isAtomicComponent(containerComponent)) {
786             log.debug(CANNOT_ATTACH_RESOURCE_INSTANCES_TO_CONTAINER_RESOURCE_OF_TYPE, containerComponent.assetType());
787             return Either
788                 .right(componentsUtils.getResponseFormat(ActionStatus.RESOURCE_CANNOT_CONTAIN_RESOURCE_INSTANCES, containerComponent.assetType()));
789         }
790         validateCanWorkOnComponent(containerComponent, userId);
791         boolean failed = false;
792         try {
793             lockComponent(containerComponent, "createAndAssociateRIToRI");
794             log.debug(TRY_TO_CREATE_ENTRY_ON_GRAPH);
795             Component origComponent = getOriginComponentFromComponentInstance(resourceInstance);
796             log.debug(ENTITY_ON_GRAPH_IS_CREATED);
797             ComponentInstance resResourceInfo = createComponentInstanceOnGraph(containerComponent, origComponent, resourceInstance, user);
798             if (associationInfo.getFromNode() == null || associationInfo.getFromNode().isEmpty()) {
799                 associationInfo.setFromNode(resResourceInfo.getUniqueId());
800             } else {
801                 associationInfo.setToNode(resResourceInfo.getUniqueId());
802             }
803             Either<RequirementCapabilityRelDef, StorageOperationStatus> resultReqCapDef = toscaOperationFacade
804                 .associateResourceInstances(containerComponent, containerComponentId, associationInfo);
805             if (resultReqCapDef.isLeft()) {
806                 log.debug(ENTITY_ON_GRAPH_IS_CREATED);
807                 RequirementCapabilityRelDef resReqCapabilityRelDef = resultReqCapDef.left().value();
808                 CreateAndAssotiateInfo resInfo = new CreateAndAssotiateInfo(resResourceInfo, resReqCapabilityRelDef);
809                 resultOp = Either.left(resInfo);
810                 return resultOp;
811             } else {
812                 log.info("Failed to associate node {} with node {}", associationInfo.getFromNode(), associationInfo.getToNode());
813                 resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstance(
814                     componentsUtils.convertFromStorageResponseForResourceInstance(resultReqCapDef.right().value(), true), "", null));
815                 return resultOp;
816             }
817         } catch (ComponentException e) {
818             failed = true;
819             throw e;
820         } finally {
821             unlockComponent(failed, containerComponent);
822         }
823     }
824
825     private Component getOriginComponentFromComponentInstance(ComponentInstance componentInstance) {
826         return getOriginComponentFromComponentInstance(componentInstance.getName(), componentInstance.getComponentUid());
827     }
828
829     private Component getInstanceOriginNode(ComponentInstance componentInstance) {
830         return getOriginComponentFromComponentInstance(componentInstance.getName(), componentInstance.getActualComponentUid());
831     }
832
833     private Component getOriginComponentFromComponentInstance(String componentInstanceName, String origComponetId) {
834         Either<Component, StorageOperationStatus> eitherComponent = toscaOperationFacade.getToscaFullElement(origComponetId);
835         if (eitherComponent.isRight()) {
836             log.debug("Failed to get origin component with id {} for component instance {} ", origComponetId, componentInstanceName);
837             throw new ByActionStatusComponentException(
838                 componentsUtils.convertFromStorageResponse(eitherComponent.right().value(), ComponentTypeEnum.RESOURCE), "", null);
839         }
840         return eitherComponent.left().value();
841     }
842
843     private ComponentInstance createComponentInstanceOnGraph(org.openecomp.sdc.be.model.Component containerComponent, Component originComponent,
844                                                              ComponentInstance componentInstance, User user) {
845         Either<ImmutablePair<Component, String>, StorageOperationStatus> result = toscaOperationFacade
846             .addComponentInstanceToTopologyTemplate(containerComponent, originComponent, componentInstance, false, user);
847         if (result.isRight()) {
848             log.debug(FAILED_TO_CREATE_ENTRY_ON_GRAPH_FOR_COMPONENT_INSTANCE, componentInstance.getName());
849             throw new ByResponseFormatComponentException(componentsUtils
850                 .getResponseFormatForResourceInstance(componentsUtils.convertFromStorageResponseForResourceInstance(result.right().value(), true), "",
851                     null));
852         }
853         log.debug(ENTITY_ON_GRAPH_IS_CREATED);
854         Component updatedComponent = result.left().value().getLeft();
855         Map<String, String> existingEnvVersions = new HashMap<>();
856         // TODO existingEnvVersions ??
857         addComponentInstanceArtifacts(updatedComponent, componentInstance, originComponent, user, existingEnvVersions);
858         Optional<ComponentInstance> updatedInstanceOptional = updatedComponent.getComponentInstances().stream()
859             .filter(ci -> ci.getUniqueId().equals(result.left().value().getRight())).findFirst();
860         if (!updatedInstanceOptional.isPresent()) {
861             log.debug("Failed to fetch new added component instance {} from component {}", componentInstance.getName(), containerComponent.getName());
862             throw new ByActionStatusComponentException(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER, componentInstance.getName());
863         }
864         return updatedInstanceOptional.get();
865     }
866
867     public boolean isCloudSpecificArtifact(String artifact) {
868         if (artifact.contains(CLOUD_SPECIFIC_FIXED_KEY_WORD)) {
869             for (int i = 0; i < CLOUD_SPECIFIC_KEY_WORDS.length; i++) {
870                 if (Arrays.stream(CLOUD_SPECIFIC_KEY_WORDS[i]).noneMatch(artifact::contains)) {
871                     return false;
872                 }
873             }
874             return true;
875         } else {
876             return false;
877         }
878     }
879
880     /**
881      * addResourceInstanceArtifacts - add artifacts (HEAT_ENV) to resource instance The instance artifacts are generated from the resource's
882      * artifacts
883      *
884      * @param containerComponent
885      * @param componentInstance
886      * @param originComponent
887      * @param user
888      * @param existingEnvVersions
889      * @return
890      */
891     protected ActionStatus addComponentInstanceArtifacts(org.openecomp.sdc.be.model.Component containerComponent, ComponentInstance componentInstance,
892                                                          org.openecomp.sdc.be.model.Component originComponent, User user,
893                                                          Map<String, String> existingEnvVersions) {
894         log.debug("add artifacts to resource instance");
895         List<GroupDefinition> filteredGroups = new ArrayList<>();
896         ActionStatus status = setResourceArtifactsOnResourceInstance(componentInstance);
897         if (ActionStatus.OK != status) {
898             throw new ByResponseFormatComponentException(componentsUtils.getResponseFormatForResourceInstance(status, "", null));
899         }
900         StorageOperationStatus artStatus;
901         // generate heat_env if necessary
902         Map<String, ArtifactDefinition> componentDeploymentArtifacts = componentInstance.getDeploymentArtifacts();
903         if (MapUtils.isNotEmpty(componentDeploymentArtifacts)) {
904             Map<String, ArtifactDefinition> finalDeploymentArtifacts = new HashMap<>();
905             Map<String, List<ArtifactDefinition>> groupInstancesArtifacts = new HashMap<>();
906             Integer defaultHeatTimeout = ConfigurationManager.getConfigurationManager().getConfiguration().getHeatArtifactDeploymentTimeout()
907                 .getDefaultMinutes();
908             List<ArtifactDefinition> listOfCloudSpecificArts = new ArrayList<>();
909             for (ArtifactDefinition artifact : componentDeploymentArtifacts.values()) {
910                 String type = artifact.getArtifactType();
911                 if (!type.equalsIgnoreCase(ArtifactTypeEnum.HEAT_ENV.getType())) {
912                     finalDeploymentArtifacts.put(artifact.getArtifactLabel(), artifact);
913                 }
914                 if (type.equalsIgnoreCase(ArtifactTypeEnum.HEAT.getType()) || type.equalsIgnoreCase(ArtifactTypeEnum.HELM.getType()) || type
915                     .equalsIgnoreCase(ArtifactTypeEnum.HEAT_NET.getType()) || type.equalsIgnoreCase(ArtifactTypeEnum.HEAT_VOL.getType()) || type
916                     .equalsIgnoreCase(ArtifactTypeEnum.CLOUD_TECHNOLOGY_SPECIFIC_ARTIFACT.getType())) {
917                     artifact.setTimeout(defaultHeatTimeout);
918                 } else {
919                     continue;
920                 }
921                 if (artifact.checkEsIdExist()) {
922                     ArtifactDefinition artifactDefinition = artifactBusinessLogic
923                         .createHeatEnvPlaceHolder(new ArrayList<>(), artifact, ArtifactsBusinessLogic.HEAT_ENV_NAME, componentInstance.getUniqueId(),
924                             NodeTypeEnum.ResourceInstance, componentInstance.getName(), user, containerComponent, existingEnvVersions);
925                     // put env
926                     finalDeploymentArtifacts.put(artifactDefinition.getArtifactLabel(), artifactDefinition);
927                     if (CollectionUtils.isNotEmpty(originComponent.getGroups())) {
928                         filteredGroups = originComponent.getGroups().stream().filter(g -> g.getType().equals(VF_MODULE)).collect(Collectors.toList());
929                     }
930                     if (isCloudSpecificArtifact(artifactDefinition.getArtifactName())) {
931                         listOfCloudSpecificArts.add(artifact);
932                     }
933                     if (CollectionUtils.isNotEmpty(filteredGroups)) {
934                         filteredGroups.stream()
935                             .filter(g -> g.getArtifacts() != null &&
936                                 g.getArtifacts().stream().anyMatch(p -> p.equals(artifactDefinition.getGeneratedFromId()))).findFirst()
937                             .ifPresent(g -> fillInstanceArtifactMap(groupInstancesArtifacts, artifactDefinition, g));
938                     }
939                 }
940             }
941             groupInstancesArtifacts.forEach((k, v) -> v.addAll(listOfCloudSpecificArts));
942             filteredGroups.forEach(g -> listOfCloudSpecificArts.forEach(e -> {
943                 g.getArtifactsUuid().add(e.getArtifactUUID());
944                 g.getArtifacts().add(e.getUniqueId());
945             }));
946             artStatus = toscaOperationFacade
947                 .addDeploymentArtifactsToInstance(containerComponent.getUniqueId(), componentInstance, finalDeploymentArtifacts);
948             if (artStatus != StorageOperationStatus.OK) {
949                 log.debug("Failed to add instance deployment artifacts for instance {} in conatiner {} error {}", componentInstance.getUniqueId(),
950                     containerComponent.getUniqueId(), artStatus);
951                 throw new ByResponseFormatComponentException(
952                     componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponseForResourceInstance(artStatus, false)));
953             }
954             StorageOperationStatus result = toscaOperationFacade
955                 .addGroupInstancesToComponentInstance(containerComponent, componentInstance, filteredGroups, groupInstancesArtifacts);
956             if (result != StorageOperationStatus.OK) {
957                 log.debug("failed to update group instance for component instance {}", componentInstance.getUniqueId());
958                 throw new ByResponseFormatComponentException(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(result)));
959             }
960             componentInstance.setDeploymentArtifacts(finalDeploymentArtifacts);
961         }
962         artStatus = toscaOperationFacade
963             .addInformationalArtifactsToInstance(containerComponent.getUniqueId(), componentInstance, originComponent.getArtifacts());
964         if (artStatus != StorageOperationStatus.OK) {
965             log.debug("Failed to add informational artifacts to the instance {} belonging to the conatiner {}. Status is {}",
966                 componentInstance.getUniqueId(), containerComponent.getUniqueId(), artStatus);
967             throw new ByResponseFormatComponentException(
968                 componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponseForResourceInstance(artStatus, false)));
969         }
970         componentInstance.setArtifacts(originComponent.getArtifacts());
971         return ActionStatus.OK;
972     }
973
974     private void fillInstanceArtifactMap(Map<String, List<ArtifactDefinition>> groupInstancesArtifacts, ArtifactDefinition artifactDefinition,
975                                          GroupDefinition groupInstance) {
976         List<ArtifactDefinition> artifactsUid;
977         if (groupInstancesArtifacts.containsKey(groupInstance.getUniqueId())) {
978             artifactsUid = groupInstancesArtifacts.get(groupInstance.getUniqueId());
979         } else {
980             artifactsUid = new ArrayList<>();
981         }
982         artifactsUid.add(artifactDefinition);
983         groupInstancesArtifacts.put(groupInstance.getUniqueId(), artifactsUid);
984     }
985
986     private ActionStatus setResourceArtifactsOnResourceInstance(ComponentInstance resourceInstance) {
987         Either<Map<String, ArtifactDefinition>, StorageOperationStatus> getResourceDeploymentArtifacts = artifactBusinessLogic
988             .getArtifacts(resourceInstance.getComponentUid(), NodeTypeEnum.Resource, ArtifactGroupTypeEnum.DEPLOYMENT, null);
989         Map<String, ArtifactDefinition> deploymentArtifacts = new HashMap<>();
990         if (getResourceDeploymentArtifacts.isRight()) {
991             StorageOperationStatus status = getResourceDeploymentArtifacts.right().value();
992             if (status != StorageOperationStatus.NOT_FOUND) {
993                 log.debug("Failed to fetch resource: {} artifacts. status is {}", resourceInstance.getComponentUid(), status);
994                 return componentsUtils.convertFromStorageResponseForResourceInstance(status, true);
995             }
996         } else {
997             deploymentArtifacts = getResourceDeploymentArtifacts.left().value();
998         }
999         if (!deploymentArtifacts.isEmpty()) {
1000             Map<String, ArtifactDefinition> tempDeploymentArtifacts = new HashMap<>(deploymentArtifacts);
1001             for (Entry<String, ArtifactDefinition> artifact : deploymentArtifacts.entrySet()) {
1002                 if (!artifact.getValue().checkEsIdExist()) {
1003                     tempDeploymentArtifacts.remove(artifact.getKey());
1004                 }
1005             }
1006             resourceInstance.setDeploymentArtifacts(tempDeploymentArtifacts);
1007         }
1008         return ActionStatus.OK;
1009     }
1010
1011     public Either<ComponentInstance, ResponseFormat> updateComponentInstanceMetadata(String containerComponentParam, String containerComponentId,
1012                                                                                      String componentInstanceId, String userId,
1013                                                                                      ComponentInstance componentInstance) {
1014         return updateComponentInstanceMetadata(containerComponentParam, containerComponentId, componentInstanceId, userId, componentInstance, true);
1015     }
1016
1017     public Either<ComponentInstance, ResponseFormat> updateComponentInstanceMetadata(final String containerComponentParam,
1018                                                                                      final String containerComponentId,
1019                                                                                      final String componentInstanceId, final String userId,
1020                                                                                      ComponentInstance componentInstance, boolean needLock) {
1021         validateUserExists(userId);
1022         final ComponentTypeEnum containerComponentType = validateComponentType(containerComponentParam);
1023         final Component containerComponent = validateComponentExists(containerComponentId, containerComponentType, null);
1024         validateCanWorkOnComponent(containerComponent, userId);
1025         ComponentTypeEnum instanceType = getComponentType(containerComponentType);
1026         Either<Boolean, StorageOperationStatus> validateParentStatus = toscaOperationFacade
1027             .validateComponentExists(componentInstance.getComponentUid());
1028         if (validateParentStatus.isRight()) {
1029             log.debug("Failed to get component instance {} on service {}", componentInstanceId, containerComponentId);
1030             throw new ByActionStatusComponentException(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND, componentInstance.getName(),
1031                 instanceType.getValue().toLowerCase());
1032         }
1033         if (!Boolean.TRUE.equals(validateParentStatus.left().value())) {
1034             throw new ByActionStatusComponentException(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER, componentInstance.getName(),
1035                 instanceType.getValue().toLowerCase(), containerComponentType.getValue().toLowerCase(), containerComponentId);
1036         }
1037         if (needLock) {
1038             lockComponent(containerComponent, "updateComponentInstance");
1039         }
1040         Component origComponent;
1041         boolean failed = false;
1042         try {
1043             origComponent = getOriginComponentFromComponentInstance(componentInstance);
1044             componentInstance = updateComponentInstanceMetadata(containerComponent, containerComponentType, origComponent, componentInstanceId,
1045                 componentInstance);
1046         } catch (ComponentException e) {
1047             failed = true;
1048             throw e;
1049         } finally {
1050             if (needLock) {
1051                 unlockComponent(failed, containerComponent);
1052             }
1053         }
1054         return Either.left(componentInstance);
1055     }
1056
1057     // New Multiple Instance Update API
1058     public List<ComponentInstance> updateComponentInstance(String containerComponentParam, Component containerComponent, String containerComponentId,
1059                                                            String userId, List<ComponentInstance> componentInstanceList, boolean needLock) {
1060         boolean failed = false;
1061         try {
1062             validateUserExists(userId);
1063             final ComponentTypeEnum containerComponentType = validateComponentType(containerComponentParam);
1064             ComponentParametersView componentFilter = new ComponentParametersView();
1065             componentFilter.disableAll();
1066             componentFilter.setIgnoreUsers(false);
1067             componentFilter.setIgnoreComponentInstances(false);
1068             if (containerComponent == null) {
1069                 containerComponent = validateComponentExistsByFilter(containerComponentId, containerComponentType, componentFilter);
1070             }
1071             validateCanWorkOnComponent(containerComponent, userId);
1072             ComponentTypeEnum instanceType = getComponentType(containerComponentType);
1073             for (ComponentInstance componentInstance : componentInstanceList) {
1074                 boolean validateParent = validateParent(containerComponent, componentInstance.getUniqueId());
1075                 if (!validateParent) {
1076                     throw new ByActionStatusComponentException(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER, componentInstance.getName(),
1077                         instanceType.getValue().toLowerCase(), containerComponentType.getValue().toLowerCase(), containerComponentId);
1078                 }
1079             }
1080             if (needLock) {
1081                 lockComponent(containerComponent, "updateComponentInstance");
1082             }
1083             List<ComponentInstance> updatedList = new ArrayList<>();
1084             List<ComponentInstance> instancesFromContainerComponent = containerComponent.getComponentInstances();
1085             List<ComponentInstance> listForUpdate = new ArrayList<>();
1086             if (instancesFromContainerComponent == null || instancesFromContainerComponent.isEmpty()) {
1087                 containerComponent.setComponentInstances(componentInstanceList);
1088             } else {
1089                 Iterator<ComponentInstance> iterator = instancesFromContainerComponent.iterator();
1090                 while (iterator.hasNext()) {
1091                     ComponentInstance origInst = iterator.next();
1092                     Optional<ComponentInstance> op = componentInstanceList.stream().filter(ci -> ci.getUniqueId().equals(origInst.getUniqueId()))
1093                         .findAny();
1094                     if (op.isPresent()) {
1095                         ComponentInstance updatedCi = op.get();
1096                         updatedCi = buildComponentInstance(updatedCi, origInst);
1097                         Boolean isUniqueName = validateInstanceNameUniquenessUponUpdate(containerComponent, origInst, updatedCi.getName());
1098                         if (!Boolean.TRUE.equals(isUniqueName)) {
1099                             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG,
1100                                 "Failed to update the name of the component instance {} to {}. A component instance with the same name already exists. ",
1101                                 origInst.getName(), updatedCi.getName());
1102                             throw new ByResponseFormatComponentException(componentsUtils
1103                                 .getResponseFormat(ActionStatus.COMPONENT_NAME_ALREADY_EXIST, containerComponentType.getValue(), origInst.getName()));
1104                         }
1105                         listForUpdate.add(updatedCi);
1106                     } else {
1107                         listForUpdate.add(origInst);
1108                     }
1109                 }
1110                 containerComponent.setComponentInstances(listForUpdate);
1111                 Either<Component, StorageOperationStatus> updateStatus = toscaOperationFacade
1112                     .updateComponentInstanceMetadataOfTopologyTemplate(containerComponent, componentFilter);
1113                 if (updateStatus.isRight()) {
1114                     CommonUtility
1115                         .addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to update metadata belonging to container component {}. Status is {}. ",
1116                             containerComponent.getName(), updateStatus.right().value());
1117                     throw new ByResponseFormatComponentException(componentsUtils.getResponseFormatForResourceInstance(
1118                         componentsUtils.convertFromStorageResponseForResourceInstance(updateStatus.right().value(), true), "", null));
1119                 }
1120                 for (ComponentInstance updatedInstance : updateStatus.left().value().getComponentInstances()) {
1121                     Optional<ComponentInstance> op = componentInstanceList.stream().filter(ci -> ci.getName().equals(updatedInstance.getName()))
1122                         .findAny();
1123                     if (op.isPresent()) {
1124                         updatedList.add(updatedInstance);
1125                     }
1126                 }
1127             }
1128             return updatedList;
1129         } catch (ComponentException e) {
1130             failed = true;
1131             throw e;
1132         } finally {
1133             if (needLock) {
1134                 unlockComponent(failed, containerComponent);
1135             }
1136         }
1137     }
1138
1139     private boolean validateParent(org.openecomp.sdc.be.model.Component containerComponent, String nodeTemplateId) {
1140         return containerComponent.getComponentInstances().stream().anyMatch(p -> p.getUniqueId().equals(nodeTemplateId));
1141     }
1142
1143     private ComponentTypeEnum getComponentType(ComponentTypeEnum containerComponentType) {
1144         if (ComponentTypeEnum.PRODUCT == containerComponentType) {
1145             return ComponentTypeEnum.SERVICE_INSTANCE;
1146         } else {
1147             return ComponentTypeEnum.RESOURCE_INSTANCE;
1148         }
1149     }
1150
1151     private ComponentInstance updateComponentInstanceMetadata(Component containerComponent, ComponentTypeEnum containerComponentType,
1152                                                               org.openecomp.sdc.be.model.Component origComponent, String componentInstanceId,
1153                                                               ComponentInstance componentInstance) {
1154         Optional<ComponentInstance> componentInstanceOptional;
1155         Either<ImmutablePair<Component, String>, StorageOperationStatus> updateRes = null;
1156         ComponentInstance oldComponentInstance = null;
1157         boolean isNameChanged = false;
1158         componentInstanceOptional = containerComponent.getComponentInstances().stream()
1159             .filter(ci -> ci.getUniqueId().equals(componentInstance.getUniqueId())).findFirst();
1160         if (!componentInstanceOptional.isPresent()) {
1161             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to find the component instance {} in container component {}. ",
1162                 componentInstance.getName(), containerComponent.getName());
1163             throw new ByActionStatusComponentException(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER, componentInstance.getName());
1164         }
1165         String oldComponentName;
1166         oldComponentInstance = componentInstanceOptional.get();
1167         oldComponentName = oldComponentInstance.getName();
1168         String newInstanceName = componentInstance.getName();
1169         if (oldComponentName != null && !oldComponentInstance.getName().equals(newInstanceName)) {
1170             isNameChanged = true;
1171         }
1172         Boolean isUniqueName = validateInstanceNameUniquenessUponUpdate(containerComponent, oldComponentInstance, newInstanceName);
1173         if (!Boolean.TRUE.equals(isUniqueName)) {
1174             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG,
1175                 "Failed to update the name of the component instance {} to {}. A component instance with the same name already exists. ",
1176                 oldComponentInstance.getName(), newInstanceName);
1177             throw new ByActionStatusComponentException(ActionStatus.COMPONENT_NAME_ALREADY_EXIST, containerComponentType.getValue(),
1178                 componentInstance.getName());
1179         }
1180         if (!DirectivesUtil.isValid(componentInstance.getDirectives())) {
1181             final String directivesStr = componentInstance.getDirectives().stream().collect(Collectors.joining(" , ", " [ ", " ] "));
1182             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG,
1183                 "Failed to update the directives of the component instance {} to {}. Directives data {} is invalid. ", oldComponentInstance.getName(),
1184                 newInstanceName, directivesStr);
1185             throw new ByActionStatusComponentException(ActionStatus.DIRECTIVES_INVALID_VALUE, containerComponentType.getValue(),
1186                 componentInstance.getName());
1187         }
1188         updateRes = toscaOperationFacade.updateComponentInstanceMetadataOfTopologyTemplate(containerComponent, origComponent,
1189             updateComponentInstanceMetadata(oldComponentInstance, componentInstance));
1190         if (updateRes.isRight()) {
1191             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG,
1192                 "Failed to update metadata of component instance {} belonging to container component {}. Status is {}. ", componentInstance.getName(),
1193                 containerComponent.getName(), updateRes.right().value());
1194             throw new ByResponseFormatComponentException(componentsUtils
1195                 .getResponseFormatForResourceInstance(componentsUtils.convertFromStorageResponseForResourceInstance(updateRes.right().value(), true),
1196                     "", null));
1197         } else {
1198             // region - Update instance Groups
1199             if (isNameChanged) {
1200                 Either<StorageOperationStatus, StorageOperationStatus> result = toscaOperationFacade
1201                     .cleanAndAddGroupInstancesToComponentInstance(containerComponent, oldComponentInstance, componentInstanceId);
1202                 if (result.isRight()) {
1203                     CommonUtility
1204                         .addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to rename group instances for container {}. error {} ", componentInstanceId,
1205                             result.right().value());
1206                 }
1207                 if (containerComponent instanceof Service) {
1208                     Either<ComponentInstance, ResponseFormat> renameEither = renameServiceFilter((Service) containerComponent, newInstanceName,
1209                         oldComponentInstance.getName());
1210                     if (renameEither.isRight()) {
1211                         throw new ByResponseFormatComponentException(renameEither.right().value());
1212                     }
1213                     updateForwardingPathDefinition(containerComponent, componentInstance, oldComponentName);
1214                 }
1215             }
1216             // endregion
1217         }
1218         String newInstanceId = updateRes.left().value().getRight();
1219         Optional<ComponentInstance> updatedInstanceOptional = updateRes.left().value().getLeft().getComponentInstances().stream()
1220             .filter(ci -> ci.getUniqueId().equals(newInstanceId)).findFirst();
1221         if (!updatedInstanceOptional.isPresent()) {
1222             log.debug("Failed to update metadata of component instance {} of container component {}", componentInstance.getName(),
1223                 containerComponent.getName());
1224             throw new ByResponseFormatComponentException(
1225                 componentsUtils.getResponseFormat(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER, componentInstance.getName()));
1226         }
1227         return componentInstanceOptional.get();
1228     }
1229
1230     private void updateForwardingPathDefinition(Component containerComponent, ComponentInstance componentInstance, String oldComponentName) {
1231         Collection<ForwardingPathDataDefinition> forwardingPathDataDefinitions = getForwardingPathDataDefinitions(containerComponent.getUniqueId());
1232         Set<ForwardingPathDataDefinition> updated = new ForwardingPathUtils()
1233             .updateComponentInstanceName(forwardingPathDataDefinitions, oldComponentName, componentInstance.getName());
1234         updated.forEach(fp -> {
1235             Either<ForwardingPathDataDefinition, StorageOperationStatus> resultEither = forwardingPathOperation
1236                 .updateForwardingPath(containerComponent.getUniqueId(), fp);
1237             if (resultEither.isRight()) {
1238                 CommonUtility.addRecordToLog(log, LogLevelEnum.ERROR, "Failed to rename forwarding path for container {}. error {} ",
1239                     containerComponent.getName(), resultEither.right().value());
1240             }
1241         });
1242     }
1243
1244     public Either<ComponentInstance, ResponseFormat> renameServiceFilter(final Service containerComponent, final String newInstanceName,
1245                                                                          final String oldInstanceName) {
1246         Map<String, CINodeFilterDataDefinition> renamedNodesFilter = ServiceFilterUtils
1247             .getRenamedNodesFilter(containerComponent, oldInstanceName, newInstanceName);
1248         for (Entry<String, CINodeFilterDataDefinition> entry : renamedNodesFilter.entrySet()) {
1249             Either<CINodeFilterDataDefinition, StorageOperationStatus> renameEither = nodeFilterOperation
1250                 .updateNodeFilter(containerComponent.getUniqueId(), entry.getKey(), entry.getValue());
1251             if (renameEither.isRight()) {
1252                 return Either.right(componentsUtils.getResponseFormatForResourceInstance(
1253                     componentsUtils.convertFromStorageResponse(renameEither.right().value(), ComponentTypeEnum.SERVICE), containerComponent.getName(),
1254                     null));
1255             }
1256         }
1257         return Either.left(null);
1258     }
1259
1260     /**
1261      * @param oldPrefix-                  The normalized old vf name
1262      * @param newNormailzedPrefix-        The normalized new vf name
1263      * @param qualifiedGroupInstanceName- old Group Instance Name
1264      **/
1265
1266     // modify group names
1267     private String getNewGroupName(String oldPrefix, String newNormailzedPrefix, String qualifiedGroupInstanceName) {
1268         if (qualifiedGroupInstanceName == null) {
1269             log.info("CANNOT change group name ");
1270             return null;
1271         }
1272         if (qualifiedGroupInstanceName.startsWith(oldPrefix) || qualifiedGroupInstanceName
1273             .startsWith(ValidationUtils.normalizeComponentInstanceName(oldPrefix))) {
1274             return qualifiedGroupInstanceName.replaceFirst(oldPrefix, newNormailzedPrefix);
1275         }
1276         return qualifiedGroupInstanceName;
1277     }
1278
1279     private ComponentInstance updateComponentInstanceMetadata(ComponentInstance oldComponentInstance, ComponentInstance newComponentInstance) {
1280         oldComponentInstance.setName(newComponentInstance.getName());
1281         oldComponentInstance.setModificationTime(System.currentTimeMillis());
1282         oldComponentInstance.setCustomizationUUID(UUID.randomUUID().toString());
1283         oldComponentInstance.setDirectives(newComponentInstance.getDirectives());
1284         oldComponentInstance.setMaxOccurrences(newComponentInstance.getMaxOccurrences());
1285         oldComponentInstance.setMinOccurrences(newComponentInstance.getMinOccurrences());
1286         oldComponentInstance.setInstanceCount(newComponentInstance.getInstanceCount());
1287         if (oldComponentInstance.getGroupInstances() != null) {
1288             oldComponentInstance.getGroupInstances().forEach(group -> group.setName(getNewGroupName(oldComponentInstance.getNormalizedName(),
1289                 ValidationUtils.normalizeComponentInstanceName(newComponentInstance.getName()), group.getName())));
1290         }
1291         return oldComponentInstance;
1292     }
1293
1294     public ComponentInstance deleteComponentInstance(final String containerComponentParam, final String containerComponentId,
1295                                                      final String componentInstanceId, String userId) throws BusinessLogicException {
1296         validateUserExists(userId);
1297         final ComponentTypeEnum containerComponentType = validateComponentType(containerComponentParam);
1298         final Component containerComponent = validateComponentExists(containerComponentId, containerComponentType, null);
1299         validateCanWorkOnComponent(containerComponent, userId);
1300         boolean failed = false;
1301         final Optional<ComponentInstance> componentInstanceOptional = containerComponent.getComponentInstanceById(componentInstanceId);
1302         if (!componentInstanceOptional.isPresent()) {
1303             throw new BusinessLogicException(componentsUtils.getResponseFormat(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND));
1304         }
1305         ComponentInstance componentInstance = componentInstanceOptional.get();
1306         try {
1307             if (containerComponent instanceof Service || containerComponent instanceof Resource) {
1308                 final Either<String, StorageOperationStatus> deleteServiceFilterEither = nodeFilterOperation
1309                     .deleteNodeFilter(containerComponent, componentInstanceId);
1310                 if (deleteServiceFilterEither.isRight()) {
1311                     final ActionStatus status = componentsUtils
1312                         .convertFromStorageResponse(deleteServiceFilterEither.right().value(), containerComponentType);
1313                     janusGraphDao.rollback();
1314                     throw new ByResponseFormatComponentException(componentsUtils.getResponseFormat(status, componentInstanceId));
1315                 }
1316                 final Either<ComponentInstance, ResponseFormat> resultOp = deleteNodeFiltersFromComponentInstance(containerComponent,
1317                     componentInstance, containerComponentType, userId);
1318                 if (resultOp.isRight()) {
1319                     janusGraphDao.rollback();
1320                     throw new ByResponseFormatComponentException(resultOp.right().value());
1321                 }
1322             }
1323             lockComponent(containerComponent, "deleteComponentInstance");
1324             final ComponentInstance deletedCompInstance = deleteComponentInstance(containerComponent, componentInstanceId, containerComponentType);
1325             componentInstance = deleteForwardingPathsRelatedTobeDeletedComponentInstance(containerComponentId, containerComponentType,
1326                 deletedCompInstance);
1327             final ActionStatus onDeleteOperationsStatus = onChangeInstanceOperationOrchestrator
1328                 .doOnDeleteInstanceOperations(containerComponent, componentInstanceId);
1329             if (ActionStatus.OK != onDeleteOperationsStatus) {
1330                 throw new ByActionStatusComponentException(onDeleteOperationsStatus);
1331             }
1332         } catch (final ComponentException e) {
1333             failed = true;
1334             throw e;
1335         } finally {
1336             unlockComponent(failed, containerComponent);
1337         }
1338         return componentInstance;
1339     }
1340
1341     /**
1342      * Try to modify the delete and return two cases
1343      *
1344      * @param containerComponentParam
1345      * @param containerComponentId
1346      * @param componentInstanceId
1347      * @param userId
1348      * @return
1349      */
1350     public Either<ComponentInstance, ResponseFormat> deleteAbstractComponentInstance(String containerComponentParam, String containerComponentId,
1351                                                                                      String componentInstanceId, String userId) {
1352         log.debug("enter deleteAbstractComponentInstance");
1353         validateUserExists(userId);
1354         final ComponentTypeEnum containerComponentType = validateComponentType(containerComponentParam);
1355         org.openecomp.sdc.be.model.Component containerComponent = validateComponentExists(containerComponentId, containerComponentType, null);
1356         validateCanWorkOnComponent(containerComponent, userId);
1357         boolean failed = false;
1358         ComponentInstance deletedRelatedInst;
1359         try {
1360             if (containerComponent instanceof Service) {
1361                 final Optional<ComponentInstance> componentInstanceById = containerComponent.getComponentInstanceById(componentInstanceId);
1362                 if (componentInstanceById.isPresent()) {
1363                     ComponentInstance componentInstance = componentInstanceById.get();
1364                     Either<String, StorageOperationStatus> deleteServiceFilterEither = nodeFilterOperation
1365                         .deleteNodeFilter(containerComponent, componentInstanceId);
1366                     if (deleteServiceFilterEither.isRight()) {
1367                         log.debug("enter deleteAbstractComponentInstance:deleteServiceFilterEither is right, filed");
1368                         ActionStatus status = componentsUtils
1369                             .convertFromStorageResponse(deleteServiceFilterEither.right().value(), containerComponentType);
1370                         janusGraphDao.rollback();
1371                         return Either.right(componentsUtils.getResponseFormat(status, componentInstance.getName()));
1372                     }
1373                     Either<ComponentInstance, ResponseFormat> resultOp = deleteNodeFiltersFromComponentInstance(containerComponent, componentInstance,
1374                         ComponentTypeEnum.SERVICE, userId);
1375                     if (resultOp.isRight()) {
1376                         log.debug("enter deleteAbstractComponentInstance:resultOp is right, filed");
1377                         janusGraphDao.rollback();
1378                         return resultOp;
1379                     }
1380                 }
1381             }
1382             log.debug("enter deleteAbstractComponentInstance:");
1383             lockComponent(containerComponent, "deleteComponentInstance");
1384             ComponentInstance deletedCompInstance = deleteComponentInstance(containerComponent, componentInstanceId, containerComponentType);
1385             deletedRelatedInst = deleteForwardingPathsRelatedTobeDeletedComponentInstance(containerComponentId, containerComponentType,
1386                 deletedCompInstance);
1387             ActionStatus onDeleteOperationsStatus = onChangeInstanceOperationOrchestrator
1388                 .doOnDeleteInstanceOperations(containerComponent, componentInstanceId);
1389             log.debug("enter deleteAbstractComponentInstance,get onDeleteOperationsStatus:{}", onDeleteOperationsStatus);
1390             if (ActionStatus.OK != onDeleteOperationsStatus) {
1391                 throw new ByActionStatusComponentException(onDeleteOperationsStatus);
1392             }
1393         } catch (ComponentException e) {
1394             failed = true;
1395             throw e;
1396         } finally {
1397             unlockComponent(failed, containerComponent);
1398         }
1399         log.debug("enter deleteAbstractComponentInstance,deleted RelatedInst success");
1400         return Either.left(deletedRelatedInst);
1401     }
1402
1403     public Either<ComponentInstance, ResponseFormat> deleteNodeFiltersFromComponentInstance(final Component component,
1404                                                                                             final ComponentInstance componentInstance,
1405                                                                                             final ComponentTypeEnum containerComponentType,
1406                                                                                             final String userId) {
1407         final Set<String> componentFiltersIDsToBeDeleted = getComponentFiltersRelatedToComponentInstance(component.getUniqueId(), componentInstance);
1408         if (!componentFiltersIDsToBeDeleted.isEmpty()) {
1409             final Set<String> ids = component.getComponentInstances().stream().filter(ci -> componentFiltersIDsToBeDeleted.contains(ci.getName()))
1410                 .map(ComponentInstance::getUniqueId).collect(Collectors.toSet());
1411             final Either<Set<String>, StorageOperationStatus> deleteComponentNodeFiltersEither = nodeFilterOperation
1412                 .deleteNodeFilters(component, ids);
1413             if (deleteComponentNodeFiltersEither.isRight()) {
1414                 final ActionStatus status = componentsUtils
1415                     .convertFromStorageResponse(deleteComponentNodeFiltersEither.right().value(), containerComponentType);
1416                 return Either.right(componentsUtils.getResponseFormat(status, componentInstance.getName()));
1417             }
1418             for (final String id : ids) {
1419                 final Optional<ComponentInstance> componentInstanceById = component.getComponentInstanceById(id);
1420                 if (!componentInstanceById.isPresent()) {
1421                     return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
1422                 }
1423                 final ComponentInstance componentInstanceToBeUpdated = componentInstanceById.get();
1424                 componentInstanceToBeUpdated.setDirectives(Collections.emptyList());
1425                 final Either<ComponentInstance, ResponseFormat> componentInstanceResponseFormatEither = updateComponentInstanceMetadata(
1426                     containerComponentType.getValue(), component.getUniqueId(), componentInstanceToBeUpdated.getUniqueId(), userId,
1427                     componentInstanceToBeUpdated, false);
1428                 if (componentInstanceResponseFormatEither.isRight()) {
1429                     return componentInstanceResponseFormatEither;
1430                 }
1431             }
1432         }
1433         return Either.left(componentInstance);
1434     }
1435
1436     private Set<String> getComponentFiltersRelatedToComponentInstance(String containerComponentId, ComponentInstance componentInstance) {
1437         ComponentParametersView filter = new ComponentParametersView(true);
1438         filter.setIgnoreComponentInstances(false);
1439         Either<Component, StorageOperationStatus> componentFilterOrigin = toscaOperationFacade.getToscaElement(containerComponentId, filter);
1440         final Component component = componentFilterOrigin.left().value();
1441         return ComponentsUtils.getNodesFiltersToBeDeleted(component, componentInstance);
1442     }
1443
1444     ComponentInstance deleteForwardingPathsRelatedTobeDeletedComponentInstance(String containerComponentId, ComponentTypeEnum containerComponentType,
1445                                                                                ComponentInstance componentInstance) {
1446         if (containerComponentType == ComponentTypeEnum.SERVICE) {
1447             List<String> pathIDsToBeDeleted = getForwardingPathsRelatedToComponentInstance(containerComponentId, componentInstance.getName());
1448             if (!pathIDsToBeDeleted.isEmpty()) {
1449                 deleteForwardingPaths(containerComponentId, pathIDsToBeDeleted);
1450             }
1451         }
1452         return componentInstance;
1453     }
1454
1455     private void deleteForwardingPaths(String serviceId, List<String> pathIdsToDelete) {
1456         Either<Service, StorageOperationStatus> storageStatus = toscaOperationFacade.getToscaElement(serviceId);
1457         if (storageStatus.isRight()) {
1458             throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(storageStatus.right().value()));
1459         }
1460         Either<Set<String>, StorageOperationStatus> result = forwardingPathOperation
1461             .deleteForwardingPath(storageStatus.left().value(), Sets.newHashSet(pathIdsToDelete));
1462         if (result.isRight()) {
1463             throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(result.right().value()));
1464         }
1465     }
1466
1467     private List<String> getForwardingPathsRelatedToComponentInstance(String containerComponentId, String componentInstanceId) {
1468         Collection<ForwardingPathDataDefinition> allPaths = getForwardingPathDataDefinitions(containerComponentId);
1469         List<String> pathIDsToBeDeleted = new ArrayList<>();
1470         allPaths.stream().filter(path -> isPathRelatedToComponent(path, componentInstanceId))
1471             .forEach(path -> pathIDsToBeDeleted.add(path.getUniqueId()));
1472         return pathIDsToBeDeleted;
1473     }
1474
1475     private Collection<ForwardingPathDataDefinition> getForwardingPathDataDefinitions(String containerComponentId) {
1476         ComponentParametersView filter = new ComponentParametersView(true);
1477         filter.setIgnoreServicePath(false);
1478         Either<Service, StorageOperationStatus> forwardingPathOrigin = toscaOperationFacade.getToscaElement(containerComponentId, filter);
1479         return forwardingPathOrigin.left().value().getForwardingPaths().values();
1480     }
1481
1482     private boolean isPathRelatedToComponent(ForwardingPathDataDefinition pathDataDefinition, String componentInstanceId) {
1483         return pathDataDefinition.getPathElements().getListToscaDataDefinition().stream().anyMatch(
1484             elementDataDefinition -> elementDataDefinition.getFromNode().equalsIgnoreCase(componentInstanceId) || elementDataDefinition.getToNode()
1485                 .equalsIgnoreCase(componentInstanceId));
1486     }
1487
1488     private ComponentInstance deleteComponentInstance(Component containerComponent, String componentInstanceId,
1489                                                       ComponentTypeEnum containerComponentType) {
1490         Either<ImmutablePair<Component, String>, StorageOperationStatus> deleteRes = toscaOperationFacade
1491             .deleteComponentInstanceFromTopologyTemplate(containerComponent, componentInstanceId);
1492         if (deleteRes.isRight()) {
1493             log.debug("Failed to delete entry on graph for resourceInstance {}", componentInstanceId);
1494             ActionStatus status = componentsUtils.convertFromStorageResponse(deleteRes.right().value(), containerComponentType);
1495             throw new ByActionStatusComponentException(status, componentInstanceId);
1496         }
1497         log.debug("The component instance {} has been removed from container component {}. ", componentInstanceId, containerComponent);
1498         ComponentInstance deletedInstance = findAndRemoveComponentInstanceFromContainerComponent(componentInstanceId, containerComponent);
1499         if (CollectionUtils.isNotEmpty(containerComponent.getInputs())) {
1500             List<InputDefinition> inputsToDelete = containerComponent.getInputs().stream()
1501                 .filter(i -> i.getInstanceUniqueId() != null && i.getInstanceUniqueId().equals(componentInstanceId)).collect(Collectors.toList());
1502             if (CollectionUtils.isNotEmpty(inputsToDelete)) {
1503                 StorageOperationStatus deleteInputsRes = toscaOperationFacade
1504                     .deleteComponentInstanceInputsFromTopologyTemplate(containerComponent, inputsToDelete);
1505                 if (deleteInputsRes != StorageOperationStatus.OK) {
1506                     log.debug("Failed to delete inputs of the component instance {} from container component. ", componentInstanceId);
1507                     throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(deleteInputsRes, containerComponentType),
1508                         componentInstanceId);
1509                 }
1510             }
1511         }
1512         if (CollectionUtils.isNotEmpty(containerComponent.getOutputs())) {
1513             final List<OutputDefinition> outputsToDelete = containerComponent.getOutputs().stream()
1514                 .filter(i -> i.getInstanceUniqueId() != null && i.getInstanceUniqueId().equals(componentInstanceId)).collect(Collectors.toList());
1515             if (CollectionUtils.isNotEmpty(outputsToDelete)) {
1516                 final StorageOperationStatus deleteOutputsRes = toscaOperationFacade
1517                     .deleteComponentInstanceOutputsFromTopologyTemplate(containerComponent, outputsToDelete);
1518                 if (deleteOutputsRes != StorageOperationStatus.OK) {
1519                     log.debug("Failed to delete outputs of the component instance {} from container component. ", componentInstanceId);
1520                     throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(deleteOutputsRes, containerComponentType),
1521                         componentInstanceId);
1522                 }
1523             }
1524         }
1525         return deletedInstance;
1526     }
1527
1528     private ComponentInstance findAndRemoveComponentInstanceFromContainerComponent(String componentInstanceId, Component containerComponent) {
1529         ComponentInstance foundInstance = null;
1530         for (ComponentInstance instance : containerComponent.getComponentInstances()) {
1531             if (instance.getUniqueId().equals(componentInstanceId)) {
1532                 foundInstance = instance;
1533                 containerComponent.getComponentInstances().remove(instance);
1534                 break;
1535             }
1536         }
1537         findAndRemoveComponentInstanceRelations(componentInstanceId, containerComponent);
1538         return foundInstance;
1539     }
1540
1541     private void findAndRemoveComponentInstanceRelations(String componentInstanceId, Component containerComponent) {
1542         if (CollectionUtils.isNotEmpty(containerComponent.getComponentInstancesRelations())) {
1543             containerComponent.setComponentInstancesRelations(
1544                 containerComponent.getComponentInstancesRelations().stream().filter(r -> isNotBelongingRelation(componentInstanceId, r))
1545                     .collect(Collectors.toList()));
1546         }
1547     }
1548
1549     private boolean isNotBelongingRelation(String componentInstanceId, RequirementCapabilityRelDef relation) {
1550         return !relation.getToNode().equals(componentInstanceId) && !relation.getFromNode().equals(componentInstanceId);
1551     }
1552
1553     public RequirementCapabilityRelDef associateRIToRI(String componentId, String userId, RequirementCapabilityRelDef requirementDef,
1554                                                        ComponentTypeEnum componentTypeEnum) {
1555         return associateRIToRI(componentId, userId, requirementDef, componentTypeEnum, true);
1556     }
1557
1558     public RequirementCapabilityRelDef associateRIToRI(String componentId, String userId, RequirementCapabilityRelDef requirementDef,
1559                                                        ComponentTypeEnum componentTypeEnum, boolean needLock) {
1560         validateUserExists(userId);
1561         RequirementCapabilityRelDef requirementCapabilityRelDef;
1562         org.openecomp.sdc.be.model.Component containerComponent = validateComponentExists(componentId, componentTypeEnum, null);
1563         validateCanWorkOnComponent(containerComponent, userId);
1564         boolean failed = false;
1565         try {
1566             if (needLock) {
1567                 lockComponent(containerComponent, ASSOCIATE_RI_TO_RI);
1568             }
1569             requirementCapabilityRelDef = associateRIToRIOnGraph(containerComponent, requirementDef);
1570         } catch (ComponentException e) {
1571             failed = true;
1572             throw e;
1573         } finally {
1574             if (needLock) {
1575                 unlockComponent(failed, containerComponent);
1576             }
1577         }
1578         return requirementCapabilityRelDef;
1579     }
1580
1581     public RequirementCapabilityRelDef associateRIToRIOnGraph(Component containerComponent, RequirementCapabilityRelDef requirementDef) {
1582         log.debug(TRY_TO_CREATE_ENTRY_ON_GRAPH);
1583         Either<RequirementCapabilityRelDef, StorageOperationStatus> result = toscaOperationFacade
1584             .associateResourceInstances(null, containerComponent.getUniqueId(), requirementDef);
1585         if (result.isLeft()) {
1586             log.debug(ENTITY_ON_GRAPH_IS_CREATED);
1587             return result.left().value();
1588         } else {
1589             log.debug("Failed to associate node: {} with node {}", requirementDef.getFromNode(), requirementDef.getToNode());
1590             String fromNameOrId = "";
1591             String toNameOrId = "";
1592             Either<ComponentInstance, StorageOperationStatus> fromResult = getResourceInstanceById(containerComponent, requirementDef.getFromNode());
1593             Either<ComponentInstance, StorageOperationStatus> toResult = getResourceInstanceById(containerComponent, requirementDef.getToNode());
1594             toNameOrId = requirementDef.getFromNode();
1595             fromNameOrId = requirementDef.getFromNode();
1596             if (fromResult.isLeft()) {
1597                 fromNameOrId = fromResult.left().value().getName();
1598             }
1599             if (toResult.isLeft()) {
1600                 toNameOrId = toResult.left().value().getName();
1601             }
1602             throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponseForResourceInstance(result.right().value(), true),
1603                 fromNameOrId, toNameOrId, requirementDef.getRelationships().get(0).getRelation().getRequirement());
1604         }
1605     }
1606
1607     /**
1608      * @param componentId
1609      * @param userId
1610      * @param requirementDefList
1611      * @param componentTypeEnum
1612      * @return
1613      */
1614     public List<RequirementCapabilityRelDef> batchDissociateRIFromRI(String componentId, String userId,
1615                                                                      List<RequirementCapabilityRelDef> requirementDefList,
1616                                                                      ComponentTypeEnum componentTypeEnum) {
1617         validateUserExists(userId);
1618         org.openecomp.sdc.be.model.Component containerComponent = validateComponentExists(componentId, componentTypeEnum, null);
1619         validateCanWorkOnComponent(containerComponent, userId);
1620         boolean failed = false;
1621         List<RequirementCapabilityRelDef> delOkResult = new ArrayList<>();
1622         try {
1623             lockComponent(containerComponent, ASSOCIATE_RI_TO_RI);
1624             for (RequirementCapabilityRelDef requirementDef : requirementDefList) {
1625                 RequirementCapabilityRelDef requirementCapabilityRelDef = dissociateRIFromRI(componentId, userId, requirementDef,
1626                     containerComponent.getComponentType());
1627                 delOkResult.add(requirementCapabilityRelDef);
1628             }
1629         } catch (ComponentException e) {
1630             failed = true;
1631             throw e;
1632         } finally {
1633             unlockComponent(failed, containerComponent);
1634         }
1635         return delOkResult;
1636     }
1637
1638     public RequirementCapabilityRelDef dissociateRIFromRI(String componentId, String userId, RequirementCapabilityRelDef requirementDef,
1639                                                           ComponentTypeEnum componentTypeEnum) {
1640         validateUserExists(userId);
1641         org.openecomp.sdc.be.model.Component containerComponent = validateComponentExists(componentId, componentTypeEnum, null);
1642         validateCanWorkOnComponent(containerComponent, userId);
1643         boolean failed = false;
1644         try {
1645             lockComponent(containerComponent, ASSOCIATE_RI_TO_RI);
1646             log.debug(TRY_TO_CREATE_ENTRY_ON_GRAPH);
1647             Either<RequirementCapabilityRelDef, StorageOperationStatus> result = toscaOperationFacade
1648                 .dissociateResourceInstances(componentId, requirementDef);
1649             if (result.isLeft()) {
1650                 log.debug(ENTITY_ON_GRAPH_IS_CREATED);
1651                 return result.left().value();
1652             } else {
1653                 log.debug("Failed to dissocaite node  {} from node {}", requirementDef.getFromNode(), requirementDef.getToNode());
1654                 String fromNameOrId = "";
1655                 String toNameOrId = "";
1656                 Either<ComponentInstance, StorageOperationStatus> fromResult = getResourceInstanceById(containerComponent,
1657                     requirementDef.getFromNode());
1658                 Either<ComponentInstance, StorageOperationStatus> toResult = getResourceInstanceById(containerComponent, requirementDef.getToNode());
1659                 toNameOrId = requirementDef.getFromNode();
1660                 fromNameOrId = requirementDef.getFromNode();
1661                 if (fromResult.isLeft()) {
1662                     fromNameOrId = fromResult.left().value().getName();
1663                 }
1664                 if (toResult.isLeft()) {
1665                     toNameOrId = toResult.left().value().getName();
1666                 }
1667                 throw new ByActionStatusComponentException(
1668                     componentsUtils.convertFromStorageResponseForResourceInstance(result.right().value(), true), fromNameOrId, toNameOrId,
1669                     requirementDef.getRelationships().get(0).getRelation().getRequirement());
1670             }
1671         } catch (ComponentException e) {
1672             failed = true;
1673             throw e;
1674         } finally {
1675             unlockComponent(failed, containerComponent);
1676         }
1677     }
1678
1679     /**
1680      * Allows to get relation contained in specified component according to received Id
1681      *
1682      * @param componentId
1683      * @param relationId
1684      * @param userId
1685      * @param componentTypeEnum
1686      * @return
1687      */
1688     public Either<RequirementCapabilityRelDef, ResponseFormat> getRelationById(String componentId, String relationId, String userId,
1689                                                                                ComponentTypeEnum componentTypeEnum) {
1690         Either<RequirementCapabilityRelDef, ResponseFormat> resultOp = null;
1691         try {
1692             org.openecomp.sdc.be.model.Component containerComponent = null;
1693             RequirementCapabilityRelDef foundRelation = null;
1694             validateUserExists(userId);
1695             containerComponent = validateComponentExists(componentId, componentTypeEnum, null);
1696             List<RequirementCapabilityRelDef> requirementCapabilityRelations = containerComponent.getComponentInstancesRelations();
1697             foundRelation = findRelation(relationId, requirementCapabilityRelations);
1698             if (foundRelation == null) {
1699                 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.RELATION_NOT_FOUND, relationId, componentId);
1700                 log.debug("Relation with id {} was not found on the component", relationId, componentId);
1701                 resultOp = Either.right(responseFormat);
1702             }
1703             if (resultOp == null) {
1704                 resultOp = setRelatedCapability(foundRelation, containerComponent);
1705             }
1706             if (resultOp.isLeft()) {
1707                 resultOp = setRelatedRequirement(foundRelation, containerComponent);
1708             }
1709         } catch (Exception e) {
1710             log.error("The exception {} occured upon get relation {} of the component {} ", e, relationId, componentId);
1711             resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
1712         }
1713         return resultOp;
1714     }
1715
1716     private RequirementCapabilityRelDef findRelation(String relationId, List<RequirementCapabilityRelDef> requirementCapabilityRelations) {
1717         for (RequirementCapabilityRelDef relationship : requirementCapabilityRelations) {
1718             if (relationship.getRelationships().stream().anyMatch(r -> r.getRelation().getId().equals(relationId))) {
1719                 return relationship;
1720             }
1721         }
1722         return null;
1723     }
1724
1725     private Either<RequirementCapabilityRelDef, ResponseFormat> setRelatedRequirement(RequirementCapabilityRelDef foundRelation,
1726                                                                                       Component containerComponent) {
1727         Either<RequirementCapabilityRelDef, ResponseFormat> result = null;
1728         RelationshipInfo relationshipInfo = foundRelation.resolveSingleRelationship().getRelation();
1729         String instanceId = foundRelation.getFromNode();
1730         Optional<RequirementDefinition> foundRequirement;
1731         Optional<ComponentInstance> instance = containerComponent.getComponentInstances().stream().filter(i -> i.getUniqueId().equals(instanceId))
1732             .findFirst();
1733         if (!instance.isPresent()) {
1734             ResponseFormat responseFormat = componentsUtils
1735                 .getResponseFormat(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER, instanceId, "instance",
1736                     containerComponent.getComponentType().getValue(), containerComponent.getName());
1737             log.debug("Component instance with id {} was not found on the component", instanceId, containerComponent.getUniqueId());
1738             result = Either.right(responseFormat);
1739         }
1740         if (result == null && instance.isPresent()) {
1741             for (List<RequirementDefinition> requirements : instance.get().getRequirements().values()) {
1742                 foundRequirement = requirements.stream()
1743                     .filter(r -> isBelongingCalcRequirement(relationshipInfo, r, containerComponent.getLifecycleState())).findFirst();
1744                 if (foundRequirement.isPresent()) {
1745                     foundRelation.resolveSingleRelationship().setRequirement(foundRequirement.get());
1746                     result = Either.left(foundRelation);
1747                 }
1748             }
1749         }
1750         if (result == null) {
1751             Either<RequirementDataDefinition, StorageOperationStatus> getfulfilledRequirementRes = toscaOperationFacade
1752                 .getFulfilledRequirementByRelation(containerComponent.getUniqueId(), instanceId, foundRelation, this::isBelongingFullRequirement);
1753             if (getfulfilledRequirementRes.isRight()) {
1754                 ResponseFormat responseFormat = componentsUtils
1755                     .getResponseFormat(ActionStatus.REQUIREMENT_OF_INSTANCE_NOT_FOUND_ON_CONTAINER, relationshipInfo.getRequirement(), instanceId,
1756                         containerComponent.getUniqueId());
1757                 log.debug("Requirement {} of instance {} was not found on the container {}. ", relationshipInfo.getCapability(), instanceId,
1758                     containerComponent.getUniqueId());
1759                 result = Either.right(responseFormat);
1760             } else {
1761                 foundRelation.resolveSingleRelationship().setRequirement(getfulfilledRequirementRes.left().value());
1762             }
1763         }
1764         if (result == null) {
1765             result = Either.left(foundRelation);
1766         }
1767         return result;
1768     }
1769
1770     private boolean isBelongingFullRequirement(RelationshipInfo relationshipInfo, RequirementDataDefinition req) {
1771         return req.getName().equals(relationshipInfo.getRequirement()) && req.getUniqueId().equals(relationshipInfo.getRequirementUid()) && req
1772             .getOwnerId().equals(relationshipInfo.getRequirementOwnerId());
1773     }
1774
1775     private boolean isBelongingCalcRequirement(RelationshipInfo relationshipInfo, RequirementDataDefinition req, LifecycleStateEnum state) {
1776         return nameMatches(relationshipInfo.getRequirement(), req.getName(), req.getPreviousName(), state) && req.getUniqueId()
1777             .equals(relationshipInfo.getRequirementUid()) && req.getOwnerId().equals(relationshipInfo.getRequirementOwnerId());
1778     }
1779
1780     private Either<RequirementCapabilityRelDef, ResponseFormat> setRelatedCapability(RequirementCapabilityRelDef foundRelation,
1781                                                                                      Component containerComponent) {
1782         Either<RequirementCapabilityRelDef, ResponseFormat> result = null;
1783         RelationshipInfo relationshipInfo = foundRelation.resolveSingleRelationship().getRelation();
1784         String instanceId = foundRelation.getToNode();
1785         Optional<CapabilityDefinition> foundCapability;
1786         Optional<ComponentInstance> instance = containerComponent.getComponentInstances().stream().filter(i -> i.getUniqueId().equals(instanceId))
1787             .findFirst();
1788         if (!instance.isPresent()) {
1789             ResponseFormat responseFormat = componentsUtils
1790                 .getResponseFormat(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER, instanceId, "instance",
1791                     containerComponent.getComponentType().getValue(), containerComponent.getName());
1792             log.debug("Component instance with id {} was not found on the component", instanceId, containerComponent.getUniqueId());
1793             result = Either.right(responseFormat);
1794         }
1795         if (result == null && instance.isPresent()) {
1796             for (List<CapabilityDefinition> capabilities : instance.get().getCapabilities().values()) {
1797                 foundCapability = capabilities.stream()
1798                     .filter(c -> isBelongingCalcCapability(relationshipInfo, c, containerComponent.getLifecycleState())).findFirst();
1799                 if (foundCapability.isPresent()) {
1800                     foundRelation.resolveSingleRelationship().setCapability(foundCapability.get());
1801                     result = Either.left(foundRelation);
1802                 }
1803             }
1804         }
1805         if (result == null) {
1806             Either<CapabilityDataDefinition, StorageOperationStatus> getfulfilledRequirementRes = toscaOperationFacade
1807                 .getFulfilledCapabilityByRelation(containerComponent.getUniqueId(), instanceId, foundRelation, this::isBelongingFullCapability);
1808             if (getfulfilledRequirementRes.isRight()) {
1809                 ResponseFormat responseFormat = componentsUtils
1810                     .getResponseFormat(ActionStatus.CAPABILITY_OF_INSTANCE_NOT_FOUND_ON_CONTAINER, relationshipInfo.getCapability(), instanceId,
1811                         containerComponent.getUniqueId());
1812                 log.debug("Capability {} of instance {} was not found on the container {}. ", relationshipInfo.getCapability(), instanceId,
1813                     containerComponent.getUniqueId());
1814                 result = Either.right(responseFormat);
1815             } else {
1816                 foundRelation.resolveSingleRelationship().setCapability(getfulfilledRequirementRes.left().value());
1817             }
1818         }
1819         if (result == null) {
1820             result = Either.left(foundRelation);
1821         }
1822         return result;
1823     }
1824
1825     private boolean isBelongingFullCapability(RelationshipInfo relationshipInfo, CapabilityDataDefinition cap) {
1826         return cap.getName().equals(relationshipInfo.getCapability()) && cap.getUniqueId().equals(relationshipInfo.getCapabilityUid()) && cap
1827             .getOwnerId().equals(relationshipInfo.getCapabilityOwnerId());
1828     }
1829
1830     private boolean isBelongingCalcCapability(RelationshipInfo relationshipInfo, CapabilityDataDefinition cap, LifecycleStateEnum state) {
1831         return nameMatches(relationshipInfo.getCapability(), cap.getName(), cap.getPreviousName(), state) && cap.getUniqueId()
1832             .equals(relationshipInfo.getCapabilityUid()) && cap.getOwnerId().equals(relationshipInfo.getCapabilityOwnerId());
1833     }
1834
1835     private boolean nameMatches(String nameFromRelationship, String currName, String previousName, LifecycleStateEnum state) {
1836         return state == LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT ? currName.equals(nameFromRelationship)
1837             : previousName != null && previousName.equals(nameFromRelationship);
1838     }
1839
1840     private Either<ComponentInstanceProperty, ResponseFormat> updateAttributeValue(ComponentInstanceProperty attribute, String resourceInstanceId) {
1841         Either<ComponentInstanceProperty, StorageOperationStatus> eitherAttribute = componentInstanceOperation
1842             .updateAttributeValueInResourceInstance(attribute, resourceInstanceId, true);
1843         Either<ComponentInstanceProperty, ResponseFormat> result;
1844         if (eitherAttribute.isLeft()) {
1845             log.debug("Attribute value {} was updated on graph.", attribute.getValueUniqueUid());
1846             ComponentInstanceProperty instanceAttribute = eitherAttribute.left().value();
1847             result = Either.left(instanceAttribute);
1848         } else {
1849             log.debug("Failed to update attribute value {} in resource instance {}", attribute, resourceInstanceId);
1850             ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(eitherAttribute.right().value());
1851             result = Either.right(componentsUtils.getResponseFormat(actionStatus, ""));
1852         }
1853         return result;
1854     }
1855
1856     private Either<ComponentInstanceProperty, ResponseFormat> createAttributeValue(ComponentInstanceProperty attribute, String resourceInstanceId) {
1857         Either<ComponentInstanceProperty, ResponseFormat> result;
1858         Wrapper<Integer> indexCounterWrapper = new Wrapper<>();
1859         Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
1860         validateIncrementCounter(resourceInstanceId, GraphPropertiesDictionary.ATTRIBUTE_COUNTER, indexCounterWrapper, errorWrapper);
1861         if (!errorWrapper.isEmpty()) {
1862             result = Either.right(errorWrapper.getInnerElement());
1863         } else {
1864             Either<ComponentInstanceProperty, StorageOperationStatus> eitherAttribute = componentInstanceOperation
1865                 .addAttributeValueToResourceInstance(attribute, resourceInstanceId, indexCounterWrapper.getInnerElement(), true);
1866             if (eitherAttribute.isLeft()) {
1867                 log.debug("Attribute value was added to resource instance {}", resourceInstanceId);
1868                 ComponentInstanceProperty instanceAttribute = eitherAttribute.left().value();
1869                 result = Either.left(instanceAttribute);
1870             } else {
1871                 log.debug("Failed to add attribute value {}  to resource instance {}", attribute, resourceInstanceId);
1872                 ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(eitherAttribute.right().value());
1873                 result = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
1874             }
1875         }
1876         return result;
1877     }
1878
1879     /**
1880      * Create Or Updates Attribute Instance
1881      *
1882      * @param componentTypeEnum
1883      * @param componentId
1884      * @param resourceInstanceId
1885      * @param attribute
1886      * @param userId
1887      * @return
1888      */
1889     public Either<ComponentInstanceProperty, ResponseFormat> createOrUpdateAttributeValue(ComponentTypeEnum componentTypeEnum, String componentId,
1890                                                                                           String resourceInstanceId,
1891                                                                                           ComponentInstanceProperty attribute, String userId) {
1892         Either<ComponentInstanceProperty, ResponseFormat> result = null;
1893         Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
1894         validateUserExists(userId);
1895         if (errorWrapper.isEmpty()) {
1896             validateComponentTypeEnum(componentTypeEnum, "CreateOrUpdateAttributeValue", errorWrapper);
1897         }
1898         if (errorWrapper.isEmpty()) {
1899             validateCanWorkOnComponent(componentId, componentTypeEnum, userId, errorWrapper);
1900         }
1901         if (errorWrapper.isEmpty()) {
1902             validateComponentLock(componentId, componentTypeEnum, errorWrapper);
1903         }
1904         try {
1905             if (errorWrapper.isEmpty()) {
1906                 final boolean isCreate = Objects.isNull(attribute.getValueUniqueUid());
1907                 if (isCreate) {
1908                     result = createAttributeValue(attribute, resourceInstanceId);
1909                 } else {
1910                     result = updateAttributeValue(attribute, resourceInstanceId);
1911                 }
1912             } else {
1913                 result = Either.right(errorWrapper.getInnerElement());
1914             }
1915             return result;
1916         } finally {
1917             if (result == null || result.isRight()) {
1918                 janusGraphDao.rollback();
1919             } else {
1920                 janusGraphDao.commit();
1921             }
1922             // unlock resource
1923             graphLockOperation.unlockComponent(componentId, componentTypeEnum.getNodeType());
1924         }
1925     }
1926
1927     public Either<List<ComponentInstanceProperty>, ResponseFormat> createOrUpdatePropertiesValues(ComponentTypeEnum componentTypeEnum,
1928                                                                                                   String componentId, String resourceInstanceId,
1929                                                                                                   List<ComponentInstanceProperty> properties,
1930                                                                                                   String userId) {
1931         Either<List<ComponentInstanceProperty>, ResponseFormat> resultOp = null;
1932         /*-------------------------------Validations---------------------------------*/
1933         validateUserExists(userId);
1934
1935         if (componentTypeEnum == null) {
1936             BeEcompErrorManager.getInstance().logInvalidInputError("CreateOrUpdatePropertiesValues", INVALID_COMPONENT_TYPE, ErrorSeverity.INFO);
1937             resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.NOT_ALLOWED));
1938             return resultOp;
1939         }
1940         Either<Component, StorageOperationStatus> getResourceResult = toscaOperationFacade.getToscaElement(componentId, JsonParseFlagEnum.ParseAll);
1941
1942         if (getResourceResult.isRight()) {
1943             log.debug(FAILED_TO_RETRIEVE_COMPONENT_COMPONENT_ID, componentId);
1944             ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(getResourceResult.right().value(), componentTypeEnum);
1945             return Either.right(componentsUtils.getResponseFormat(actionStatus, componentId));
1946         }
1947         Component containerComponent = getResourceResult.left().value();
1948
1949         if (!ComponentValidationUtils.canWorkOnComponent(containerComponent, userId)) {
1950             if (Boolean.TRUE.equals(containerComponent.isArchived())) {
1951                 log.info(COMPONENT_ARCHIVED, componentId);
1952                 return Either.right(componentsUtils.getResponseFormat(ActionStatus.COMPONENT_IS_ARCHIVED, containerComponent.getName()));
1953             }
1954             log.info(RESTRICTED_OPERATION_ON_SERVIVE, userId, componentId);
1955             return Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
1956         }
1957
1958         Either<ComponentInstance, StorageOperationStatus> resourceInstanceStatus = getResourceInstanceById(containerComponent, resourceInstanceId);
1959         if (resourceInstanceStatus.isRight()) {
1960             return Either.right(componentsUtils.getResponseFormat(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER,
1961                 resourceInstanceId, RESOURCE_INSTANCE, SERVICE, componentId));
1962         }
1963         ComponentInstance foundResourceInstance = resourceInstanceStatus.left().value();
1964
1965         // Validate instance property against it's constrains
1966         Either<Boolean, ResponseFormat> constraintValidatorResponse = validatePropertyValueConstraint(properties, componentId);
1967         if (constraintValidatorResponse.isRight()) {
1968             log.error("Failed validation value and constraint of property: {}", constraintValidatorResponse.right().value());
1969             return Either.right(constraintValidatorResponse.right().value());
1970         }
1971         // lock resource
1972         StorageOperationStatus lockStatus = graphLockOperation.lockComponent(componentId, componentTypeEnum.getNodeType());
1973         if (lockStatus != StorageOperationStatus.OK) {
1974             log.debug(FAILED_TO_LOCK_SERVICE, componentId);
1975             return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(lockStatus)));
1976         }
1977         List<ComponentInstanceProperty> updatedProperties = new ArrayList<>();
1978         try {
1979             for (ComponentInstanceProperty property : properties) {
1980                 validateMandatoryFields(property);
1981                 validatePropertyExistsOnComponent(property, containerComponent, foundResourceInstance);
1982                 validatePropertyConstraintsNotChanged(properties, foundResourceInstance);
1983                 String propertyParentUniqueId = property.getParentUniqueId();
1984                 if (property.isToscaFunction()) {
1985                     toscaFunctionValidator.validate(property, containerComponent);
1986                     property.setValue(property.getToscaFunction().getValue());
1987                 }
1988                 if (CollectionUtils.isNotEmpty(property.getSubPropertyToscaFunctions())) {
1989                     if (StringUtils.isNumeric(property.getSubPropertyToscaFunctions().iterator().next().getSubPropertyPath().get(0))) {
1990                         final JSONArray jsonArray = property.getValue() == null ? new JSONArray() : new JSONArray(property.getValue());
1991                         property.getSubPropertyToscaFunctions().stream().forEach(subToscaFunction -> {
1992                             addE(jsonArray, subToscaFunction.getSubPropertyPath(), subToscaFunction.getToscaFunction().getValue());
1993                         });
1994                         property.setValue(jsonArray.toString());
1995                     } else {
1996                         final JSONObject jObject = property.getValue() == null ? new JSONObject() : new JSONObject(property.getValue());
1997                         property.getSubPropertyToscaFunctions().stream().forEach(subToscaFunction -> {
1998                             addE(jObject, subToscaFunction.getSubPropertyPath(), subToscaFunction.getToscaFunction().getValue());
1999                         });
2000                         property.setValue(jObject.toString());
2001                     }
2002                 }
2003                 Either<String, ResponseFormat> updatedPropertyValue = updatePropertyObjectValue(property, containerComponent.getModel());
2004                 if (updatedPropertyValue.isRight()) {
2005                     log.error("Failed to update property object value of property: {}",
2006                         property);
2007                     throw new ByResponseFormatComponentException(updatedPropertyValue.right().value());
2008                 }
2009                 Optional<CapabilityDefinition>
2010                     capPropDefinition = getPropertyCapabilityOfChildInstance(propertyParentUniqueId, foundResourceInstance.getCapabilities());
2011                 if (capPropDefinition.isPresent()) {
2012                     updatedPropertyValue
2013                         .bimap(updatedValue -> updateCapabilityPropFromUpdateInstProp(property, updatedValue,
2014                             containerComponent, foundResourceInstance, capPropDefinition.get().getType(),
2015                             capPropDefinition.get().getName()), Either::right);
2016                 } else {
2017                     updatedPropertyValue.bimap(
2018                         updatedValue -> updatePropertyOnContainerComponent(property, updatedValue, containerComponent, foundResourceInstance),
2019                         Either::right
2020                     );
2021                     updatedProperties.add(property);
2022                 }
2023             }
2024
2025             Either<Component, StorageOperationStatus> updateContainerRes = toscaOperationFacade
2026                 .updateComponentInstanceMetadataOfTopologyTemplate(containerComponent);
2027             if (updateContainerRes.isRight()) {
2028                 ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(updateContainerRes.right().value());
2029                 resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
2030                 return resultOp;
2031             }
2032             resultOp = Either.left(updatedProperties);
2033             return resultOp;
2034
2035         } catch (final ComponentException e) {
2036             return Either.right(e.getResponseFormat());
2037         } finally {
2038             if (resultOp == null || resultOp.isRight()) {
2039                 janusGraphDao.rollback();
2040             } else {
2041                 janusGraphDao.commit();
2042             }
2043             // unlock resource
2044             graphLockOperation.unlockComponent(componentId, componentTypeEnum.getNodeType());
2045         }
2046     }
2047
2048     private void addE(JSONArray jsonArray, List<String> path, String value) {
2049         Object objectForPath = jsonArray.opt(Integer.parseInt(path.get(0)));
2050         if (objectForPath == null) {
2051             if (path.size() > 1) {
2052                 if (StringUtils.isNumeric(path.get(1))) {
2053                     objectForPath = new JSONArray();
2054                 } else {
2055                     objectForPath = new JSONObject();
2056                 }
2057                 jsonArray.put(Integer.parseInt(path.get(0)), objectForPath);
2058             }
2059         }
2060
2061         if (path.size() == 1) {
2062             Object valueAsObject = new Yaml().loadAs(value, Object.class);
2063             jsonArray.put(Integer.parseInt(path.get(0)), valueAsObject);
2064         } else {
2065             if (objectForPath instanceof JSONObject) {
2066                 addE((JSONObject) objectForPath, path.subList(1, path.size()), value);
2067             } else {
2068                 addE((JSONArray) objectForPath, path.subList(1, path.size()), value);
2069             }
2070         }
2071     }
2072
2073     private void addE(JSONObject jsonObject, List<String> path, String value) {
2074
2075         Object objectForPath = null;
2076         if (jsonObject.has(path.get(0))) {
2077             objectForPath = jsonObject.get(path.get(0));
2078         } else {
2079             if (StringUtils.isNumeric(path.get(0))) {
2080                 objectForPath = new JSONArray();
2081             } else {
2082                 objectForPath = new JSONObject();
2083             }
2084             jsonObject.put(path.get(0), objectForPath);
2085         }
2086
2087         if (path.size() == 1) {
2088             Object valueAsObject = new Yaml().loadAs(value, Object.class);
2089             jsonObject.put(path.get(0), valueAsObject);
2090         } else {
2091             if (objectForPath instanceof JSONObject) {
2092                 addE((JSONObject) objectForPath, path.subList(1, path.size()), value);
2093             } else {
2094                 addE((JSONArray) objectForPath, path.subList(1, path.size()), value);
2095             }
2096         }
2097     }
2098
2099     private void setJsonObjectForSubProperty(final JSONObject jObject, final List<String> path, String value) {
2100         if (path.size() == 1) {
2101             Object valueAsObject = new Yaml().loadAs(value, Object.class);
2102             jObject.put(path.get(0), valueAsObject);
2103         } else {
2104             if (!jObject.has(path.get(0))) {
2105                 jObject.put(path.get(0), new JSONObject());
2106             }
2107             final JSONObject jsonObject = jObject.getJSONObject(path.get(0));
2108             setJsonObjectForSubProperty(jsonObject, path.subList(1, path.size()), value);
2109         }
2110     }
2111
2112     public Either<List<ComponentInstanceAttribute>, ResponseFormat> createOrUpdateAttributeValues(final ComponentTypeEnum componentTypeEnum,
2113                                                                                                   final String componentId,
2114                                                                                                   final String resourceInstanceId,
2115                                                                                                   final List<ComponentInstanceAttribute> attributes,
2116                                                                                                   final String userId) {
2117         Either<List<ComponentInstanceAttribute>, ResponseFormat> resultOp = null;
2118         /*-------------------------------Validations---------------------------------*/
2119         validateUserExists(userId);
2120
2121         if (componentTypeEnum == null) {
2122             BeEcompErrorManager.getInstance().logInvalidInputError("CreateOrUpdatePropertiesValues", INVALID_COMPONENT_TYPE, ErrorSeverity.INFO);
2123             resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.NOT_ALLOWED));
2124             return resultOp;
2125         }
2126         final Either<Component, StorageOperationStatus> getResourceResult = toscaOperationFacade
2127             .getToscaElement(componentId, JsonParseFlagEnum.ParseAll);
2128
2129         if (getResourceResult.isRight()) {
2130             log.debug(FAILED_TO_RETRIEVE_COMPONENT_COMPONENT_ID, componentId);
2131             final ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(getResourceResult.right().value(), componentTypeEnum);
2132             return Either.right(componentsUtils.getResponseFormat(actionStatus, componentId));
2133         }
2134         final Component containerComponent = getResourceResult.left().value();
2135
2136         if (!ComponentValidationUtils.canWorkOnComponent(containerComponent, userId)) {
2137             if (Boolean.TRUE.equals(containerComponent.isArchived())) {
2138                 log.info(COMPONENT_ARCHIVED, componentId);
2139                 return Either.right(componentsUtils.getResponseFormat(ActionStatus.COMPONENT_IS_ARCHIVED, containerComponent.getName()));
2140             }
2141             log.info(RESTRICTED_OPERATION_ON_SERVIVE, userId, componentId);
2142             return Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
2143         }
2144
2145         final Either<ComponentInstance, StorageOperationStatus> resourceInstanceStatus = getResourceInstanceById(containerComponent,
2146             resourceInstanceId);
2147         if (resourceInstanceStatus.isRight()) {
2148             return Either.right(componentsUtils.getResponseFormat(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER,
2149                 resourceInstanceId, RESOURCE_INSTANCE, SERVICE, componentId));
2150         }
2151         final ComponentInstance foundResourceInstance = resourceInstanceStatus.left().value();
2152
2153         // Validate instance attributes against it's constraints
2154         List<PropertyDefinition> attributesToValidate = new ArrayList<>();
2155         attributes.forEach((componentInstanceAttribute) -> {
2156             PropertyDefinition propertyDefinition = new PropertyDefinition();
2157             propertyDefinition.setValue(componentInstanceAttribute.getValue());
2158             propertyDefinition.setType(componentInstanceAttribute.getType());
2159             propertyDefinition.setName(componentInstanceAttribute.getName());
2160             propertyDefinition.setUniqueId(componentInstanceAttribute.getUniqueId());
2161             attributesToValidate.add(propertyDefinition);
2162         });
2163         Either<Boolean, ResponseFormat> constraintValidatorResponse = validatePropertyValueConstraint(attributesToValidate, componentId);
2164         if (constraintValidatorResponse.isRight()) {
2165             log.error("Failed validation value and constraint of attribute: {}", constraintValidatorResponse.right().value());
2166             return Either.right(constraintValidatorResponse.right().value());
2167         }
2168
2169         // lock resource
2170         final StorageOperationStatus lockStatus = graphLockOperation.lockComponent(componentId, componentTypeEnum.getNodeType());
2171         if (lockStatus != StorageOperationStatus.OK) {
2172             log.debug(FAILED_TO_LOCK_SERVICE, componentId);
2173             return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(lockStatus)));
2174         }
2175         final List<ComponentInstanceAttribute> updatedProperties = new ArrayList<>();
2176         try {
2177             for (final ComponentInstanceAttribute attribute : attributes) {
2178                 final ComponentInstanceAttribute componentInstanceProperty = validateAttributeExistsOnComponent(attribute, containerComponent,
2179                     foundResourceInstance);
2180                 final Either<String, ResponseFormat> updatedPropertyValue = updateAttributeObjectValue(attribute);
2181                 if (updatedPropertyValue.isRight()) {
2182                     log.error("Failed to update attribute object value of attribute: {}", attribute);
2183                     throw new ByResponseFormatComponentException(updatedPropertyValue.right().value());
2184                 }
2185                 updatedPropertyValue.bimap(
2186                     updatedValue -> {
2187                         componentInstanceProperty.setValue(updatedValue);
2188                         return updateAttributeOnContainerComponent(attribute, updatedValue,
2189                             containerComponent, foundResourceInstance);
2190                     }, Either::right);
2191                 updatedProperties.add(componentInstanceProperty);
2192             }
2193
2194             final Either<Component, StorageOperationStatus> updateContainerRes = toscaOperationFacade
2195                 .updateComponentInstanceMetadataOfTopologyTemplate(containerComponent);
2196             if (updateContainerRes.isRight()) {
2197                 final ActionStatus actionStatus = componentsUtils
2198                     .convertFromStorageResponseForResourceInstanceProperty(updateContainerRes.right().value());
2199                 resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
2200                 return resultOp;
2201             }
2202             resultOp = Either.left(updatedProperties);
2203             return resultOp;
2204
2205         } catch (final ComponentException e) {
2206             return Either.right(e.getResponseFormat());
2207         } finally {
2208             if (resultOp == null || resultOp.isRight()) {
2209                 janusGraphDao.rollback();
2210             } else {
2211                 janusGraphDao.commit();
2212             }
2213             // unlock resource
2214             graphLockOperation.unlockComponent(componentId, componentTypeEnum.getNodeType());
2215         }
2216     }
2217
2218     private void validateMandatoryFields(PropertyDataDefinition property) {
2219         if (StringUtils.isEmpty(property.getName())) {
2220             throw new ByActionStatusComponentException(ActionStatus.MISSING_PROPERTY_NAME);
2221         }
2222     }
2223
2224     private void validatePropertyExistsOnComponent(ComponentInstanceProperty property, Component containerComponent,
2225                                                    ComponentInstance foundResourceInstance) {
2226         List<ComponentInstanceProperty> instanceProperties = containerComponent.getComponentInstancesProperties()
2227             .get(foundResourceInstance.getUniqueId());
2228         final boolean hasProperty = instanceProperties.stream().anyMatch(p -> p.getName().equals(property.getName()));
2229         if (!hasProperty) {
2230             throw new ByActionStatusComponentException(ActionStatus.PROPERTY_NOT_FOUND, property.getName());
2231         }
2232     }
2233
2234     private ComponentInstanceAttribute validateAttributeExistsOnComponent(final ComponentInstanceAttribute attribute,
2235                                                                           final Component containerComponent,
2236                                                                           final ComponentInstance foundResourceInstance) {
2237         final List<ComponentInstanceAttribute> instanceProperties =
2238             containerComponent.getComponentInstancesAttributes().get(foundResourceInstance.getUniqueId());
2239         final Optional<ComponentInstanceAttribute> instanceAttribute =
2240             instanceProperties.stream().filter(p -> p.getName().equals(attribute.getName())).findAny();
2241         if (!instanceAttribute.isPresent()) {
2242             throw new ByActionStatusComponentException(ActionStatus.PROPERTY_NOT_FOUND, attribute.getName());
2243         }
2244         return instanceAttribute.get();
2245     }
2246
2247     private ResponseFormat updateCapabilityPropertyOnContainerComponent(ComponentInstanceProperty property,
2248                                                                         String newValue, Component containerComponent,
2249                                                                         ComponentInstance foundResourceInstance,
2250                                                                         String capabilityType, String capabilityName) {
2251         String componentInstanceUniqueId = foundResourceInstance.getUniqueId();
2252         ResponseFormat actionStatus = updateCapPropOnContainerComponent(property, newValue, containerComponent,
2253             foundResourceInstance, capabilityType, capabilityName, componentInstanceUniqueId);
2254         if (actionStatus != null) {
2255             return actionStatus;
2256         }
2257
2258         return componentsUtils.getResponseFormat(ActionStatus.OK);
2259     }
2260
2261     private ResponseFormat updateCapabilityPropFromUpdateInstProp(ComponentInstanceProperty property,
2262                                                                   String newValue, Component containerComponent,
2263                                                                   ComponentInstance foundResourceInstance,
2264                                                                   String capabilityType, String capabilityName) {
2265         String componentInstanceUniqueId = foundResourceInstance.getUniqueId();
2266         Either<Component, StorageOperationStatus> getComponentRes =
2267             toscaOperationFacade.getToscaFullElement(foundResourceInstance.getComponentUid());
2268         if (getComponentRes.isRight()) {
2269             return componentsUtils.getResponseFormat(getComponentRes.right().value());
2270         }
2271
2272         ResponseFormat actionStatus = updateCapPropOnContainerComponent(property, newValue, containerComponent,
2273             foundResourceInstance, capabilityType, capabilityName, componentInstanceUniqueId);
2274         if (actionStatus != null) {
2275             return actionStatus;
2276         }
2277
2278         return componentsUtils.getResponseFormat(ActionStatus.OK);
2279     }
2280
2281     private ResponseFormat updateCapPropOnContainerComponent(ComponentInstanceProperty property, String newValue,
2282                                                              Component containerComponent,
2283                                                              ComponentInstance foundResourceInstance,
2284                                                              String capabilityType, String capabilityName,
2285                                                              String componentInstanceUniqueId) {
2286         Map<String, List<CapabilityDefinition>> capabilities =
2287             Optional.ofNullable(foundResourceInstance.getCapabilities()).orElse(Collections.emptyMap());
2288         List<CapabilityDefinition> capPerType =
2289             Optional.ofNullable(capabilities.get(capabilityType)).orElse(Collections.emptyList());
2290         Optional<CapabilityDefinition> cap =
2291             capPerType.stream().filter(c -> c.getName().equals(capabilityName)).findAny();
2292         if (cap.isPresent()) {
2293             List<ComponentInstanceProperty> capProperties = cap.get().getProperties();
2294             if (capProperties != null) {
2295                 Optional<ComponentInstanceProperty> instanceProperty =
2296                     capProperties.stream().filter(p -> p.getUniqueId().equals(property.getUniqueId())).findAny();
2297                 StorageOperationStatus status;
2298                 if (instanceProperty.isPresent()) {
2299                     String capKey = ModelConverter
2300                         .buildCapabilityPropertyKey(foundResourceInstance.getOriginType().isAtomicType(), capabilityType, capabilityName,
2301                             componentInstanceUniqueId, cap.get());
2302                     instanceProperty.get().setValue(newValue);
2303                     List<String> path = new ArrayList<>();
2304                     path.add(componentInstanceUniqueId);
2305                     path.add(capKey);
2306                     instanceProperty.get().setPath(path);
2307                     status = toscaOperationFacade.updateComponentInstanceCapabiltyProperty(containerComponent,
2308                         componentInstanceUniqueId, capKey, instanceProperty.get());
2309                     if (status != StorageOperationStatus.OK) {
2310                         ActionStatus actionStatus =
2311                             componentsUtils.convertFromStorageResponseForResourceInstanceProperty(status);
2312                         return componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, "");
2313
2314                     }
2315                     foundResourceInstance.setCustomizationUUID(UUID.randomUUID().toString());
2316                 }
2317             }
2318         }
2319         return null;
2320     }
2321
2322     private ResponseFormat updatePropertyOnContainerComponent(ComponentInstanceProperty instanceProperty, String newValue,
2323                                                               Component containerComponent, ComponentInstance foundResourceInstance) {
2324         StorageOperationStatus status;
2325         instanceProperty.setValue(newValue);
2326         status = toscaOperationFacade.updateComponentInstanceProperty(containerComponent, foundResourceInstance.getUniqueId(), instanceProperty);
2327         if (status != StorageOperationStatus.OK) {
2328             ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(status);
2329             return componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, "");
2330         }
2331         foundResourceInstance.setCustomizationUUID(UUID.randomUUID().toString());
2332         return componentsUtils.getResponseFormat(ActionStatus.OK);
2333     }
2334
2335     private ResponseFormat updateAttributeOnContainerComponent(final ComponentInstanceAttribute instanceAttribute,
2336                                                                final String newValue,
2337                                                                final Component containerComponent,
2338                                                                final ComponentInstance foundResourceInstance) {
2339
2340         instanceAttribute.setValue(newValue);
2341         final StorageOperationStatus status =
2342             toscaOperationFacade.updateComponentInstanceAttribute(containerComponent, foundResourceInstance.getUniqueId(), instanceAttribute);
2343         if (status != StorageOperationStatus.OK) {
2344             final ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(status);
2345             return componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, "");
2346         }
2347         foundResourceInstance.setCustomizationUUID(UUID.randomUUID().toString());
2348         return componentsUtils.getResponseFormat(ActionStatus.OK);
2349     }
2350
2351     private <T extends PropertyDefinition> Either<String, ResponseFormat> validatePropertyObjectValue(T property, String newValue, boolean isInput) {
2352         final Map<String, DataTypeDefinition> allDataTypes = componentsUtils.getAllDataTypes(applicationDataTypeCache, property.getModel());
2353         String propertyType = property.getType();
2354         String innerType = getInnerType(property);
2355
2356         // Specific Update Logic
2357         Either<Object, Boolean> isValid = propertyOperation
2358             .validateAndUpdatePropertyValue(property.getType(), newValue, true, innerType, allDataTypes);
2359         if (isValid.isRight()) {
2360             if (!Boolean.TRUE.equals(isValid.right().value())) {
2361                 log.error("Invalid value {} of property {} ", newValue, property.getName());
2362                 return Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT));
2363             }
2364         } else {
2365             Object object = isValid.left().value();
2366             if (object != null) {
2367                 newValue = object.toString();
2368             }
2369         }
2370         if (validateAndUpdateRules(property, isInput, allDataTypes, innerType, propertyType)) {
2371             return Either.right(componentsUtils.getResponseFormat(componentsUtils
2372                 .convertFromStorageResponse(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(JanusGraphOperationStatus.ILLEGAL_ARGUMENT))));
2373         }
2374         return Either.left(newValue);
2375     }
2376
2377     private <T extends PropertyDefinition> boolean validateAndUpdateRules(T property, boolean isInput, Map<String, DataTypeDefinition> allDataTypes,
2378                                                                           String innerType, String propertyType) {
2379         if (!isInput) {
2380             ImmutablePair<String, Boolean> pair = propertyOperation
2381                 .validateAndUpdateRules(propertyType, ((ComponentInstanceProperty) property).getRules(), innerType, allDataTypes, true);
2382             if (pair.getRight() != null && !pair.getRight()) {
2383                 BeEcompErrorManager.getInstance().logBeInvalidValueError("Add property value", pair.getLeft(), property.getName(), propertyType);
2384                 return true;
2385             }
2386         }
2387         return false;
2388     }
2389
2390     private <T extends PropertyDefinition> Either<String, ResponseFormat> updatePropertyObjectValue(T property, final String model) {
2391         final Map<String, DataTypeDefinition> allDataTypes = componentsUtils.getAllDataTypes(applicationDataTypeCache, model);
2392         String innerType = null;
2393         String propertyType = property.getType();
2394         ToscaPropertyType type = ToscaPropertyType.isValidType(propertyType);
2395         log.debug("The type of the property {} is {}", property.getUniqueId(), propertyType);
2396
2397         if (type == ToscaPropertyType.LIST || type == ToscaPropertyType.MAP) {
2398             SchemaDefinition schema = property.getSchema();
2399             if (schema == null) {
2400                 log.debug("Schema doesn't exists for property of type {}", type);
2401                 return Either
2402                     .right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(StorageOperationStatus.INVALID_VALUE)));
2403             }
2404             PropertyDataDefinition propDef = schema.getProperty();
2405             if (propDef == null) {
2406                 log.debug("Property in Schema Definition inside property of type {} doesn't exist", type);
2407                 return Either
2408                     .right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(StorageOperationStatus.INVALID_VALUE)));
2409             }
2410             innerType = propDef.getType();
2411         }
2412
2413         // Specific Update Logic
2414         String newValue = property.getValue();
2415
2416         if (property.hasToscaFunction() || CollectionUtils.isNotEmpty(property.getSubPropertyToscaFunctions())) {
2417             return Either.left(newValue);
2418         }
2419
2420         Either<Object, Boolean> isValid = propertyOperation
2421             .validateAndUpdatePropertyValue(propertyType, property.getValue(), true, innerType, allDataTypes);
2422         if (isValid.isRight()) {
2423             if (!Boolean.TRUE.equals(isValid.right().value())) {
2424                 log.debug("validate and update property value has failed with value: {}", property.getValue());
2425                 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(
2426                     DaoStatusConverter.convertJanusGraphStatusToStorageStatus(JanusGraphOperationStatus.ILLEGAL_ARGUMENT)));
2427             }
2428         } else {
2429             Object object = isValid.left().value();
2430             if (object != null) {
2431                 newValue = object.toString();
2432             }
2433         }
2434         ImmutablePair<String, Boolean> pair = propertyOperation
2435             .validateAndUpdateRules(propertyType, ((ComponentInstanceProperty) property).getRules(), innerType, allDataTypes, true);
2436         if (pair.getRight() != null && Boolean.FALSE.equals(pair.getRight())) {
2437             BeEcompErrorManager.getInstance().logBeInvalidValueError("Add property value", pair.getLeft(), property.getName(), propertyType);
2438             return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(
2439                 DaoStatusConverter.convertJanusGraphStatusToStorageStatus(JanusGraphOperationStatus.ILLEGAL_ARGUMENT))));
2440         }
2441         return Either.left(newValue);
2442     }
2443
2444     private <T extends AttributeDefinition> Either<String, ResponseFormat> updateAttributeObjectValue(final T attribute) {
2445         String innerType = null;
2446         final String attributeType = attribute.getType();
2447         final ToscaPropertyType type = ToscaPropertyType.isValidType(attributeType);
2448         log.debug("The type of the attribute {} is {}", attribute.getUniqueId(), attributeType);
2449
2450         if (type == ToscaPropertyType.LIST || type == ToscaPropertyType.MAP) {
2451             final SchemaDefinition def = attribute.getSchema();
2452             if (def == null) {
2453                 log.debug("Schema doesn't exists for attribute of type {}", type);
2454                 return Either
2455                     .right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(StorageOperationStatus.INVALID_VALUE)));
2456             }
2457             PropertyDataDefinition propDef = def.getProperty();
2458             if (propDef == null) {
2459                 log.debug("Property in Schema Definition inside attribute of type {} doesn't exist", type);
2460                 return Either
2461                     .right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(StorageOperationStatus.INVALID_VALUE)));
2462             }
2463             innerType = propDef.getType();
2464         }
2465
2466         // Specific Update Logic
2467         String newValue = attribute.getValue();
2468
2469         final var isValid = attributeOperation.validateAndUpdateAttributeValue(attribute, innerType,
2470             componentsUtils.getAllDataTypes(applicationDataTypeCache, attribute.getModel()));
2471         if (isValid.isRight()) {
2472             final Boolean res = isValid.right().value();
2473             if (!Boolean.TRUE.equals(res)) {
2474                 log.debug("validate and update attribute value has failed with value: {}", newValue);
2475                 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(
2476                     DaoStatusConverter.convertJanusGraphStatusToStorageStatus(JanusGraphOperationStatus.ILLEGAL_ARGUMENT)));
2477             }
2478         } else {
2479             final Object object = isValid.left().value();
2480             if (object != null) {
2481                 newValue = object.toString();
2482             }
2483         }
2484         return Either.left(newValue);
2485     }
2486
2487     private <T extends PropertyDefinition> void validateToscaGetFunction(T property, Component parentComponent) {
2488         final ToscaGetFunctionDataDefinition toscaGetFunction = (ToscaGetFunctionDataDefinition) property.getToscaFunction();
2489         validateGetToscaFunctionAttributes(toscaGetFunction);
2490         validateGetPropertySource(toscaGetFunction.getFunctionType(), toscaGetFunction.getPropertySource());
2491         if (toscaGetFunction.getFunctionType() == ToscaGetFunctionType.GET_INPUT) {
2492             validateGetFunction(property, parentComponent.getInputs(), parentComponent.getModel());
2493             return;
2494         }
2495         if (toscaGetFunction.getFunctionType() == ToscaGetFunctionType.GET_PROPERTY) {
2496             if (toscaGetFunction.getPropertySource() == PropertySource.SELF) {
2497                 validateGetFunction(property, parentComponent.getProperties(), parentComponent.getModel());
2498             } else if (toscaGetFunction.getPropertySource() == PropertySource.INSTANCE) {
2499                 final ComponentInstance componentInstance =
2500                     parentComponent.getComponentInstanceById(toscaGetFunction.getSourceUniqueId())
2501                         .orElseThrow(ToscaGetFunctionExceptionSupplier.instanceNotFound(toscaGetFunction.getSourceName()));
2502                 validateGetFunction(property, componentInstance.getProperties(), parentComponent.getModel());
2503             }
2504
2505             return;
2506         }
2507         if (toscaGetFunction.getFunctionType() == ToscaGetFunctionType.GET_ATTRIBUTE) {
2508             if (toscaGetFunction.getPropertySource() == PropertySource.SELF) {
2509                 validateGetFunction(property, parentComponent.getAttributes(), parentComponent.getModel());
2510             } else if (toscaGetFunction.getPropertySource() == PropertySource.INSTANCE) {
2511                 final ComponentInstance componentInstance =
2512                     parentComponent.getComponentInstanceById(toscaGetFunction.getSourceUniqueId())
2513                         .orElseThrow(ToscaGetFunctionExceptionSupplier.instanceNotFound(toscaGetFunction.getSourceName()));
2514                 validateGetFunction(property, componentInstance.getAttributes(), parentComponent.getModel());
2515             }
2516
2517             return;
2518         }
2519
2520         throw ToscaGetFunctionExceptionSupplier.functionNotSupported(toscaGetFunction.getFunctionType()).get();
2521     }
2522
2523     private <T extends PropertyDefinition> void validateGetFunction(final T property,
2524                                                                     final List<? extends ToscaPropertyData> parentProperties,
2525                                                                     final String model) {
2526         final ToscaGetFunctionDataDefinition toscaGetFunction = (ToscaGetFunctionDataDefinition) property.getToscaFunction();
2527         if (CollectionUtils.isEmpty(parentProperties)) {
2528             throw ToscaGetFunctionExceptionSupplier
2529                 .propertyNotFoundOnTarget(toscaGetFunction.getPropertyName(), toscaGetFunction.getPropertySource(),
2530                     toscaGetFunction.getFunctionType()
2531                 ).get();
2532         }
2533         final String getFunctionPropertyUniqueId = toscaGetFunction.getPropertyUniqueId();
2534         ToscaPropertyData referredProperty = parentProperties.stream()
2535             .filter(property1 -> getFunctionPropertyUniqueId.equals(property1.getUniqueId()))
2536             .findFirst()
2537             .orElseThrow(ToscaGetFunctionExceptionSupplier
2538                 .propertyNotFoundOnTarget(toscaGetFunction.getPropertyName(), toscaGetFunction.getPropertySource()
2539                     , toscaGetFunction.getFunctionType())
2540             );
2541         if (toscaGetFunction.isSubProperty()) {
2542             referredProperty = findSubProperty(referredProperty, toscaGetFunction, model);
2543         }
2544
2545         if (!property.getType().equals(referredProperty.getType())) {
2546             throw ToscaGetFunctionExceptionSupplier
2547                 .propertyTypeDiverge(toscaGetFunction.getType(), referredProperty.getType(), property.getType()).get();
2548         }
2549         if (PropertyType.typeHasSchema(referredProperty.getType()) && !referredProperty.getSchemaType().equals(property.getSchemaType())) {
2550             throw ToscaGetFunctionExceptionSupplier
2551                 .propertySchemaDiverge(toscaGetFunction.getType(), referredProperty.getSchemaType(), property.getSchemaType()).get();
2552         }
2553     }
2554
2555     private ToscaPropertyData findSubProperty(final ToscaPropertyData referredProperty,
2556                                               final ToscaGetFunctionDataDefinition toscaGetFunction,
2557                                               final String model) {
2558         final Map<String, DataTypeDefinition> dataTypeMap = loadDataTypes(model);
2559         final List<String> propertyPathFromSource = toscaGetFunction.getPropertyPathFromSource();
2560         DataTypeDefinition dataType = dataTypeMap.get(referredProperty.getType());
2561         if (dataType == null) {
2562             throw ToscaGetFunctionExceptionSupplier
2563                 .propertyDataTypeNotFound(propertyPathFromSource.get(0), referredProperty.getType(), toscaGetFunction.getFunctionType()).get();
2564         }
2565         ToscaPropertyData foundProperty = referredProperty;
2566         for (int i = 1; i < propertyPathFromSource.size(); i++) {
2567             final String currentPropertyName = propertyPathFromSource.get(i);
2568             foundProperty = dataType.getProperties().stream()
2569                 .filter(propertyDefinition -> currentPropertyName.equals(propertyDefinition.getName())).findFirst()
2570                 .orElseThrow(
2571                     ToscaGetFunctionExceptionSupplier
2572                         .propertyNotFoundOnTarget(propertyPathFromSource.subList(0, i), toscaGetFunction.getPropertySource(),
2573                             toscaGetFunction.getFunctionType())
2574                 );
2575             dataType = dataTypeMap.get(foundProperty.getType());
2576             if (dataType == null) {
2577                 throw ToscaGetFunctionExceptionSupplier
2578                     .propertyDataTypeNotFound(propertyPathFromSource.subList(0, i), foundProperty.getType(),
2579                         toscaGetFunction.getFunctionType()).get();
2580             }
2581         }
2582         return foundProperty;
2583     }
2584
2585     private Map<String, DataTypeDefinition> loadDataTypes(String model) {
2586         final Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> dataTypeEither =
2587             applicationDataTypeCache.getAll(model);
2588         if (dataTypeEither.isRight()) {
2589             throw ToscaGetFunctionExceptionSupplier.couldNotLoadDataTypes(model).get();
2590         }
2591         return dataTypeEither.left().value();
2592     }
2593
2594     private void validateGetPropertySource(final ToscaGetFunctionType functionType, final PropertySource propertySource) {
2595         if (functionType == ToscaGetFunctionType.GET_INPUT && propertySource != PropertySource.SELF) {
2596             throw ToscaGetFunctionExceptionSupplier
2597                 .targetSourceNotSupported(functionType, propertySource).get();
2598         }
2599         if (functionType == ToscaGetFunctionType.GET_PROPERTY && !List.of(PropertySource.SELF, PropertySource.INSTANCE).contains(propertySource)) {
2600             throw ToscaGetFunctionExceptionSupplier
2601                 .targetSourceNotSupported(functionType, propertySource).get();
2602         }
2603     }
2604
2605     private void validateGetToscaFunctionAttributes(final ToscaGetFunctionDataDefinition toscaGetFunction) {
2606         if (toscaGetFunction.getFunctionType() == null) {
2607             throw ToscaGetFunctionExceptionSupplier.targetFunctionTypeNotFound().get();
2608         }
2609         if (toscaGetFunction.getPropertySource() == null) {
2610             throw ToscaGetFunctionExceptionSupplier.targetPropertySourceNotFound(toscaGetFunction.getFunctionType()).get();
2611         }
2612         if (CollectionUtils.isEmpty(toscaGetFunction.getPropertyPathFromSource())) {
2613             throw ToscaGetFunctionExceptionSupplier
2614                 .targetSourcePathNotFound(toscaGetFunction.getFunctionType()).get();
2615         }
2616         if (StringUtils.isEmpty(toscaGetFunction.getSourceName()) || StringUtils.isBlank(toscaGetFunction.getSourceName())) {
2617             throw ToscaGetFunctionExceptionSupplier.sourceNameNotFound(toscaGetFunction.getPropertySource()).get();
2618         }
2619         if (StringUtils.isEmpty(toscaGetFunction.getSourceUniqueId()) || StringUtils.isBlank(toscaGetFunction.getSourceUniqueId())) {
2620             throw ToscaGetFunctionExceptionSupplier.sourceIdNotFound(toscaGetFunction.getPropertySource()).get();
2621         }
2622         if (StringUtils.isEmpty(toscaGetFunction.getPropertyName()) || StringUtils.isBlank(toscaGetFunction.getPropertyName())) {
2623             throw ToscaGetFunctionExceptionSupplier.propertyNameNotFound(toscaGetFunction.getPropertySource()).get();
2624         }
2625         if (StringUtils.isEmpty(toscaGetFunction.getPropertyUniqueId()) || StringUtils.isBlank(toscaGetFunction.getPropertyUniqueId())) {
2626             throw ToscaGetFunctionExceptionSupplier.propertyIdNotFound(toscaGetFunction.getPropertySource()).get();
2627         }
2628     }
2629
2630     private ResponseFormat updateInputOnContainerComponent(ComponentInstanceInput input, String newValue, Component containerComponent,
2631                                                            ComponentInstance foundResourceInstance) {
2632         StorageOperationStatus status;
2633         input.setValue(newValue);
2634         status = toscaOperationFacade.updateComponentInstanceInput(containerComponent, foundResourceInstance.getUniqueId(), input);
2635         if (status != StorageOperationStatus.OK) {
2636             ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(status);
2637             return componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, "");
2638         }
2639         foundResourceInstance.setCustomizationUUID(UUID.randomUUID().toString());
2640         return componentsUtils.getResponseFormat(ActionStatus.OK);
2641     }
2642
2643     public Either<List<ComponentInstanceInput>, ResponseFormat> createOrUpdateInstanceInputValues(ComponentTypeEnum componentTypeEnum,
2644                                                                                                   String componentId, String resourceInstanceId,
2645                                                                                                   List<ComponentInstanceInput> inputs,
2646                                                                                                   String userId) {
2647
2648         Either<List<ComponentInstanceInput>, ResponseFormat> resultOp = null;
2649
2650         validateUserExists(userId);
2651
2652         if (componentTypeEnum == null) {
2653             BeEcompErrorManager.getInstance().logInvalidInputError(CREATE_OR_UPDATE_PROPERTY_VALUE, INVALID_COMPONENT_TYPE, ErrorSeverity.INFO);
2654             resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.NOT_ALLOWED));
2655             return resultOp;
2656         }
2657         Either<Component, StorageOperationStatus> getResourceResult = toscaOperationFacade.getToscaElement(componentId, JsonParseFlagEnum.ParseAll);
2658
2659         if (getResourceResult.isRight()) {
2660             log.debug(FAILED_TO_RETRIEVE_COMPONENT_COMPONENT_ID, componentId);
2661             ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(getResourceResult.right().value(), componentTypeEnum);
2662             return Either.right(componentsUtils.getResponseFormat(actionStatus, componentId));
2663         }
2664         Component containerComponent = getResourceResult.left().value();
2665
2666         if (!ComponentValidationUtils.canWorkOnComponent(containerComponent, userId)) {
2667             if (Boolean.TRUE.equals(containerComponent.isArchived())) {
2668                 log.info(COMPONENT_ARCHIVED, componentId);
2669                 return Either.right(componentsUtils.getResponseFormat(ActionStatus.COMPONENT_IS_ARCHIVED, containerComponent.getName()));
2670             }
2671             log.info(RESTRICTED_OPERATION_ON_SERVIVE, userId, componentId);
2672             resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
2673             return resultOp;
2674         }
2675         Either<ComponentInstance, StorageOperationStatus> resourceInstanceStatus = getResourceInstanceById(containerComponent, resourceInstanceId);
2676         if (resourceInstanceStatus.isRight()) {
2677             return Either.right(componentsUtils.getResponseFormat(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER,
2678                 resourceInstanceId, RESOURCE_INSTANCE, SERVICE, componentId));
2679         }
2680
2681         ComponentInstance foundResourceInstance = resourceInstanceStatus.left().value();
2682
2683         // lock resource
2684         StorageOperationStatus lockStatus = graphLockOperation.lockComponent(componentId, componentTypeEnum.getNodeType());
2685         if (lockStatus != StorageOperationStatus.OK) {
2686             log.debug(FAILED_TO_LOCK_SERVICE, componentId);
2687             return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(lockStatus)));
2688         }
2689         List<ComponentInstanceInput> updatedInputs = new ArrayList<>();
2690         try {
2691             for (ComponentInstanceInput input : inputs) {
2692                 validateMandatoryFields(input);
2693                 ComponentInstanceInput componentInstanceInput = validateInputExistsOnComponent(input, containerComponent, foundResourceInstance);
2694                 Either<String, ResponseFormat> validatedInputValue = validatePropertyObjectValue(componentInstanceInput, input.getValue(), true);
2695                 if (validatedInputValue.isRight()) {
2696                     throw new ByActionStatusComponentException(ActionStatus.INVALID_CONTENT, input.getName());
2697                 }
2698                 updateInputOnContainerComponent(componentInstanceInput, validatedInputValue.left().value(), containerComponent,
2699                     foundResourceInstance);
2700                 updatedInputs.add(componentInstanceInput);
2701             }
2702             Either<Component, StorageOperationStatus> updateContainerRes = toscaOperationFacade
2703                 .updateComponentInstanceMetadataOfTopologyTemplate(containerComponent);
2704             if (updateContainerRes.isRight()) {
2705                 ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(updateContainerRes.right().value());
2706                 resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
2707                 return resultOp;
2708             }
2709             resultOp = Either.left(updatedInputs);
2710             return resultOp;
2711
2712         } finally {
2713             if (resultOp == null || resultOp.isRight()) {
2714                 janusGraphDao.rollback();
2715             } else {
2716                 janusGraphDao.commit();
2717             }
2718             // unlock resource
2719             graphLockOperation.unlockComponent(componentId, componentTypeEnum.getNodeType());
2720         }
2721
2722     }
2723
2724     private ComponentInstanceInput validateInputExistsOnComponent(ComponentInstanceInput input, Component containerComponent,
2725                                                                   ComponentInstance foundResourceInstance) {
2726         List<ComponentInstanceInput> instanceProperties = containerComponent.getComponentInstancesInputs().get(foundResourceInstance.getUniqueId());
2727         Optional<ComponentInstanceInput> instanceInput = instanceProperties.stream().filter(p -> p.getName().equals(input.getName())).findAny();
2728         if (!instanceInput.isPresent()) {
2729             throw new ByActionStatusComponentException(ActionStatus.PROPERTY_NOT_FOUND, input.getName());
2730         }
2731         return instanceInput.get();
2732     }
2733
2734     public Either<ComponentInstanceProperty, ResponseFormat> createOrUpdateGroupInstancePropertyValue(ComponentTypeEnum componentTypeEnum,
2735                                                                                                       String componentId, String resourceInstanceId,
2736                                                                                                       String groupInstanceId,
2737                                                                                                       ComponentInstanceProperty property,
2738                                                                                                       String userId) {
2739
2740         Either<ComponentInstanceProperty, ResponseFormat> resultOp = null;
2741
2742         validateUserExists(userId);
2743
2744         if (componentTypeEnum == null) {
2745             BeEcompErrorManager.getInstance().logInvalidInputError(CREATE_OR_UPDATE_PROPERTY_VALUE, INVALID_COMPONENT_TYPE, ErrorSeverity.INFO);
2746             resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.NOT_ALLOWED));
2747             return resultOp;
2748         }
2749
2750         if (!ComponentValidationUtils.canWorkOnComponent(componentId, toscaOperationFacade, userId)) {
2751             log.info("Restricted operation for user: {} on service: {}", userId, componentId);
2752             resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
2753             return resultOp;
2754         }
2755         // lock resource
2756         StorageOperationStatus lockStatus = graphLockOperation.lockComponent(componentId, componentTypeEnum.getNodeType());
2757         if (lockStatus != StorageOperationStatus.OK) {
2758             log.debug(FAILED_TO_LOCK_SERVICE, componentId);
2759             resultOp = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(lockStatus)));
2760             return resultOp;
2761         }
2762         try {
2763             String propertyValueUid = property.getValueUniqueUid();
2764
2765             if (propertyValueUid == null) {
2766
2767                 Either<Integer, StorageOperationStatus> counterRes = groupInstanceOperation
2768                     .increaseAndGetGroupInstancePropertyCounter(groupInstanceId);
2769
2770                 if (counterRes.isRight()) {
2771                     log.debug("increaseAndGetResourcePropertyCounter failed resource instance: {} property: {}", resourceInstanceId, property);
2772                     StorageOperationStatus status = counterRes.right().value();
2773                     ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(status);
2774                     resultOp = Either.right(componentsUtils.getResponseFormat(actionStatus));
2775                 }
2776                 Integer index = counterRes.left().value();
2777                 Either<ComponentInstanceProperty, StorageOperationStatus> result = groupInstanceOperation
2778                     .addPropertyValueToGroupInstance(property, resourceInstanceId, index, true);
2779
2780                 if (result.isLeft()) {
2781                     log.trace("Property value was added to resource instance {}", resourceInstanceId);
2782                     ComponentInstanceProperty instanceProperty = result.left().value();
2783
2784                     resultOp = Either.left(instanceProperty);
2785
2786                 } else {
2787                     log.debug("Failed to add property value: {} to resource instance {}", property, resourceInstanceId);
2788
2789                     ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(result.right().value());
2790
2791                     resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
2792                 }
2793
2794             } else {
2795                 Either<ComponentInstanceProperty, StorageOperationStatus> result = groupInstanceOperation
2796                     .updatePropertyValueInGroupInstance(property, resourceInstanceId, true);
2797
2798                 if (result.isLeft()) {
2799                     log.debug("Property value {} was updated on graph.", property.getValueUniqueUid());
2800                     ComponentInstanceProperty instanceProperty = result.left().value();
2801
2802                     resultOp = Either.left(instanceProperty);
2803
2804                 } else {
2805                     log.debug("Failed to update property value: {}, in resource instance {}", property, resourceInstanceId);
2806
2807                     ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(result.right().value());
2808
2809                     resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
2810                 }
2811             }
2812             if (resultOp.isLeft()) {
2813                 StorageOperationStatus updateCustomizationUUID = componentInstanceOperation.updateCustomizationUUID(resourceInstanceId);
2814                 if (updateCustomizationUUID != StorageOperationStatus.OK) {
2815                     ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(updateCustomizationUUID);
2816
2817                     resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
2818
2819                 }
2820             }
2821             return resultOp;
2822
2823         } finally {
2824             if (resultOp == null || resultOp.isRight()) {
2825                 janusGraphDao.rollback();
2826             } else {
2827                 janusGraphDao.commit();
2828             }
2829             // unlock resource
2830             graphLockOperation.unlockComponent(componentId, componentTypeEnum.getNodeType());
2831         }
2832
2833     }
2834
2835     public Either<ComponentInstanceProperty, ResponseFormat> deletePropertyValue(ComponentTypeEnum componentTypeEnum, String serviceId,
2836                                                                                  String resourceInstanceId, String propertyValueId, String userId) {
2837
2838         validateUserExists(userId);
2839
2840         Either<ComponentInstanceProperty, ResponseFormat> resultOp = null;
2841
2842         if (componentTypeEnum == null) {
2843             BeEcompErrorManager.getInstance().logInvalidInputError(CREATE_OR_UPDATE_PROPERTY_VALUE, INVALID_COMPONENT_TYPE, ErrorSeverity.INFO);
2844             resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.NOT_ALLOWED));
2845             return resultOp;
2846         }
2847
2848         if (!ComponentValidationUtils.canWorkOnComponent(serviceId, toscaOperationFacade, userId)) {
2849             log.info("Restricted operation for user {} on service {}", userId, serviceId);
2850             resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
2851             return resultOp;
2852         }
2853         // lock resource
2854         StorageOperationStatus lockStatus = graphLockOperation.lockComponent(serviceId, componentTypeEnum.getNodeType());
2855         if (lockStatus != StorageOperationStatus.OK) {
2856             log.debug(FAILED_TO_LOCK_SERVICE, serviceId);
2857             resultOp = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(lockStatus)));
2858             return resultOp;
2859         }
2860         try {
2861             Either<ComponentInstanceProperty, StorageOperationStatus> result = propertyOperation
2862                 .removePropertyValueFromResourceInstance(propertyValueId, resourceInstanceId, true);
2863
2864             if (result.isLeft()) {
2865                 log.debug("Property value {} was removed from graph.", propertyValueId);
2866                 ComponentInstanceProperty instanceProperty = result.left().value();
2867
2868                 resultOp = Either.left(instanceProperty);
2869                 return resultOp;
2870
2871             } else {
2872                 log.debug("Failed to remove property value {} in resource instance {}", propertyValueId, resourceInstanceId);
2873
2874                 ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(result.right().value());
2875
2876                 resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
2877
2878                 return resultOp;
2879             }
2880
2881         } finally {
2882             if (resultOp == null || resultOp.isRight()) {
2883                 janusGraphDao.rollback();
2884             } else {
2885                 janusGraphDao.commit();
2886             }
2887             // unlock resource
2888             graphLockOperation.unlockComponent(serviceId, componentTypeEnum.getNodeType());
2889         }
2890
2891     }
2892
2893     private Component getAndValidateOriginComponentOfComponentInstance(Component containerComponent, ComponentInstance componentInstance) {
2894
2895         ComponentTypeEnum componentType = getComponentTypeByParentComponentType(containerComponent.getComponentType());
2896         Component component;
2897         Either<Component, StorageOperationStatus> getComponentRes = toscaOperationFacade.getToscaFullElement(componentInstance.getComponentUid());
2898         if (getComponentRes.isRight()) {
2899             log.debug("Failed to get the component with id {} for component instance {} creation. ", componentInstance.getComponentUid(),
2900                 componentInstance.getName());
2901             ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(getComponentRes.right().value(), componentType);
2902             throw new ByActionStatusComponentException(actionStatus, Constants.EMPTY_STRING);
2903         }
2904         component = getComponentRes.left().value();
2905         LifecycleStateEnum resourceCurrState = component.getLifecycleState();
2906         if (resourceCurrState == LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT) {
2907             ActionStatus actionStatus = ActionStatus.CONTAINER_CANNOT_CONTAIN_COMPONENT_IN_STATE;
2908             throw new ByActionStatusComponentException(actionStatus, containerComponent.getComponentType().toString(), resourceCurrState.toString());
2909         }
2910         if (Boolean.TRUE.equals(component.isArchived())) {
2911             ActionStatus actionStatus = ActionStatus.COMPONENT_IS_ARCHIVED;
2912             throw new ByActionStatusComponentException(actionStatus, component.getName());
2913         }
2914         final Map<String, InterfaceDefinition> componentInterfaces = component.getInterfaces();
2915         if (MapUtils.isNotEmpty(componentInterfaces)) {
2916             componentInterfaces.forEach(componentInstance::addInterface);
2917         }
2918         return component;
2919     }
2920
2921     public Either<Set<String>, ResponseFormat> forwardingPathOnVersionChange(String containerComponentParam,
2922                                                                              String containerComponentId,
2923                                                                              String componentInstanceId,
2924                                                                              ComponentInstance newComponentInstance) {
2925         Either<Set<String>, ResponseFormat> resultOp;
2926         final ComponentTypeEnum containerComponentType = validateComponentType(containerComponentParam);
2927         ComponentParametersView componentParametersView = getComponentParametersViewForForwardingPath();
2928
2929         //Fetch Component
2930         Component containerComponent = validateComponentExists(containerComponentId, containerComponentType, componentParametersView);
2931
2932         //Fetch current component instance
2933         Either<ComponentInstance, StorageOperationStatus> eitherResourceInstance =
2934             getResourceInstanceById(containerComponent, componentInstanceId);
2935         if (eitherResourceInstance.isRight()) {
2936             resultOp = Either.right(componentsUtils.getResponseFormat(
2937                 ActionStatus.RESOURCE_INSTANCE_NOT_FOUND_ON_SERVICE, componentInstanceId, containerComponentId));
2938             return resultOp;
2939         }
2940         ComponentInstance currentResourceInstance = eitherResourceInstance.left().value();
2941
2942         //Check whether new componentInstance exists
2943         String resourceId = newComponentInstance.getComponentUid();
2944         Either<Boolean, StorageOperationStatus> componentExistsRes = toscaOperationFacade.validateComponentExists(resourceId);
2945         if (componentExistsRes.isRight()) {
2946             log.debug("Failed to find resource {}", resourceId);
2947             resultOp = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse
2948                 (componentExistsRes.right().value()), resourceId));
2949             return resultOp;
2950         } else if (!Boolean.TRUE.equals(componentExistsRes.left().value())) {
2951             log.debug("The resource {} not found ", resourceId);
2952             resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.RESOURCE_NOT_FOUND, resourceId));
2953             return resultOp;
2954         }
2955
2956         //Fetch component using new component instance uid
2957         Component updatedContainerComponent = getOriginComponentFromComponentInstance(newComponentInstance);
2958         Set<String> toDeleteForwardingPaths = getForwardingPaths(containerComponent,
2959             currentResourceInstance, updatedContainerComponent);
2960         resultOp = Either.left(toDeleteForwardingPaths);
2961
2962         return resultOp;
2963     }
2964
2965     private Set<String> getForwardingPaths(Component containerComponent, ComponentInstance currentResourceInstance,
2966                                            Component updatedContainerComponent) {
2967         DataForMergeHolder dataForMergeHolder = new DataForMergeHolder();
2968         dataForMergeHolder.setOrigComponentInstId(currentResourceInstance.getName());
2969
2970         Service service = (Service) containerComponent;
2971         ForwardingPathUtils forwardingPathUtils = new ForwardingPathUtils();
2972
2973         return forwardingPathUtils.
2974             getForwardingPathsToBeDeletedOnVersionChange(service, dataForMergeHolder, updatedContainerComponent);
2975     }
2976
2977     private ComponentParametersView getComponentParametersViewForForwardingPath() {
2978         ComponentParametersView componentParametersView = new ComponentParametersView();
2979         componentParametersView.setIgnoreCapabiltyProperties(false);
2980         componentParametersView.setIgnoreServicePath(false);
2981         return componentParametersView;
2982     }
2983
2984     public ComponentInstance changeComponentInstanceVersion(String containerComponentParam, String containerComponentId, String componentInstanceId,
2985                                                             String userId, ComponentInstance newComponentInstance) {
2986
2987         User user = validateUserExists(userId);
2988         final ComponentTypeEnum containerComponentType = validateComponentType(containerComponentParam);
2989         ComponentParametersView componentParametersView = new ComponentParametersView();
2990         componentParametersView.setIgnoreCapabiltyProperties(false);
2991
2992         org.openecomp.sdc.be.model.Component containerComponent = validateComponentExists(containerComponentId, containerComponentType,
2993             componentParametersView);
2994
2995         validateCanWorkOnComponent(containerComponent, userId);
2996
2997         Either<ComponentInstance, StorageOperationStatus> resourceInstanceStatus = getResourceInstanceById(containerComponent, componentInstanceId);
2998         if (resourceInstanceStatus.isRight()) {
2999             throw new ByActionStatusComponentException(ActionStatus.RESOURCE_INSTANCE_NOT_FOUND_ON_SERVICE, componentInstanceId,
3000                 containerComponentId);
3001         }
3002
3003         ComponentInstance currentResourceInstance = resourceInstanceStatus.left().value();
3004
3005         return changeInstanceVersion(containerComponent, currentResourceInstance, newComponentInstance, user, containerComponentType);
3006     }
3007
3008     public ComponentInstance changeInstanceVersion(org.openecomp.sdc.be.model.Component containerComponent,
3009                                                    ComponentInstance currentResourceInstance,
3010                                                    ComponentInstance newComponentInstance,
3011                                                    User user,
3012                                                    final ComponentTypeEnum containerComponentType) {
3013         boolean failed = false;
3014         Either<ComponentInstance, StorageOperationStatus> resourceInstanceStatus;
3015
3016         try {
3017             lockComponent(containerComponent, "changeComponentInstanceVersion");
3018             String containerComponentId = containerComponent.getUniqueId();
3019             String componentInstanceId = currentResourceInstance.getUniqueId();
3020             if (currentResourceInstance.getComponentUid().equals(newComponentInstance.getComponentUid())) {
3021                 return currentResourceInstance;
3022             }
3023             String resourceId = newComponentInstance.getComponentUid();
3024
3025             Either<Boolean, StorageOperationStatus> componentExistsRes = toscaOperationFacade
3026                 .validateComponentExists(resourceId);
3027             if (componentExistsRes.isRight()) {
3028                 StorageOperationStatus errorStatus = componentExistsRes.right().value();
3029
3030                 log.debug("Failed to validate existing of the component {}. Status is {} ", resourceId, errorStatus);
3031                 throw new ByActionStatusComponentException(
3032                     componentsUtils.convertFromStorageResponse(errorStatus), resourceId);
3033             } else if (!Boolean.TRUE.equals(componentExistsRes.left().value())) {
3034                 log.debug("The resource {} not found ", resourceId);
3035                 throw new ByActionStatusComponentException(ActionStatus.RESOURCE_NOT_FOUND, resourceId);
3036             }
3037
3038             Component eitherOriginComponent = getInstanceOriginNode(currentResourceInstance);
3039             DataForMergeHolder dataHolder = compInstMergeDataBL
3040                 .saveAllDataBeforeDeleting(containerComponent, currentResourceInstance, eitherOriginComponent);
3041             ComponentInstance resResourceInfo = deleteComponentInstance(containerComponent, componentInstanceId,
3042                 containerComponentType);
3043
3044             if (resResourceInfo == null) {
3045                 log.error("Calling `deleteComponentInstance` for the resource {} returned a null", resourceId);
3046                 throw new ByActionStatusComponentException(ActionStatus.RESOURCE_NOT_FOUND, resourceId);
3047             } else {
3048                 Component origComponent = null;
3049                 OriginTypeEnum originType = currentResourceInstance.getOriginType();
3050                 newComponentInstance.setOriginType(originType);
3051                 if (originType == OriginTypeEnum.ServiceProxy) {
3052                     Either<Component, StorageOperationStatus> serviceProxyOrigin = toscaOperationFacade
3053                         .getLatestByName(SERVICE_PROXY, null);
3054                     if (isServiceProxyOrigin(serviceProxyOrigin)) {
3055                         throw new ByActionStatusComponentException(
3056                             componentsUtils.convertFromStorageResponse(serviceProxyOrigin.right().value()));
3057                     }
3058                     origComponent = serviceProxyOrigin.left().value();
3059
3060                     StorageOperationStatus fillProxyRes = fillInstanceData(newComponentInstance, origComponent);
3061
3062                     if (isFillProxyRes(fillProxyRes)) {
3063                         throw new ByActionStatusComponentException(
3064                             componentsUtils.convertFromStorageResponse(fillProxyRes));
3065                     }
3066                 } else if (originType == OriginTypeEnum.ServiceSubstitution) {
3067                     final Either<Component, StorageOperationStatus> getServiceResult = toscaOperationFacade
3068                         .getToscaFullElement(newComponentInstance.getComponentUid());
3069                     if (getServiceResult.isRight()) {
3070                         throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(getServiceResult.right().value()));
3071                     }
3072                     final Component service = getServiceResult.left().value();
3073
3074                     final Either<Component, StorageOperationStatus> getServiceDerivedFromTypeResult = toscaOperationFacade
3075                         .getLatestByToscaResourceName(service.getDerivedFromGenericType(), service.getModel());
3076                     if (getServiceDerivedFromTypeResult.isRight()) {
3077                         throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(getServiceResult.right().value()));
3078                     }
3079
3080                     origComponent = getServiceDerivedFromTypeResult.left().value();
3081
3082                     final StorageOperationStatus fillProxyRes = fillInstanceData(newComponentInstance, origComponent);
3083                     if (isFillProxyRes(fillProxyRes)) {
3084                         throw new ByActionStatusComponentException(
3085                             componentsUtils.convertFromStorageResponse(fillProxyRes));
3086                     }
3087                 } else {
3088                     origComponent = getOriginComponentFromComponentInstance(newComponentInstance);
3089                     newComponentInstance.setName(resResourceInfo.getName());
3090                     final Map<String, InterfaceDefinition> componentInterfaces = origComponent.getInterfaces();
3091                     if (MapUtils.isNotEmpty(componentInterfaces)) {
3092                         componentInterfaces.forEach(newComponentInstance::addInterface);
3093                     }
3094                 }
3095
3096                 newComponentInstance.setInvariantName(resResourceInfo.getInvariantName());
3097                 newComponentInstance.setPosX(resResourceInfo.getPosX());
3098                 newComponentInstance.setPosY(resResourceInfo.getPosY());
3099                 newComponentInstance.setDescription(resResourceInfo.getDescription());
3100                 newComponentInstance.setInstanceCount(resResourceInfo.getInstanceCount());
3101                 newComponentInstance.setMaxOccurrences(resResourceInfo.getMaxOccurrences());
3102                 newComponentInstance.setMinOccurrences(resResourceInfo.getMinOccurrences());
3103                 newComponentInstance.setDirectives(resResourceInfo.getDirectives());
3104                 checkForExternalReqAndCapabilities(origComponent, resResourceInfo);
3105
3106                 ComponentInstance updatedComponentInstance =
3107                     createComponentInstanceOnGraph(containerComponent, origComponent, newComponentInstance, user);
3108                 dataHolder.setCurrInstanceNode(origComponent);
3109                 compInstMergeDataBL
3110                     .mergeComponentUserOrigData(user, dataHolder, containerComponent, containerComponentId, newComponentInstance.getUniqueId());
3111
3112                 ActionStatus postChangeVersionResult = onChangeInstanceOperationOrchestrator
3113                     .doPostChangeVersionOperations(containerComponent, currentResourceInstance, newComponentInstance);
3114                 if (postChangeVersionResult != ActionStatus.OK) {
3115                     throw new ByActionStatusComponentException(postChangeVersionResult);
3116                 }
3117
3118                 ComponentParametersView filter = new ComponentParametersView(true);
3119                 filter.setIgnoreComponentInstances(false);
3120                 Either<Component, StorageOperationStatus> updatedComponentRes = toscaOperationFacade.getToscaElement(containerComponentId, filter);
3121                 if (updatedComponentRes.isRight()) {
3122                     StorageOperationStatus storageOperationStatus = updatedComponentRes.right().value();
3123                     ActionStatus actionStatus = componentsUtils
3124                         .convertFromStorageResponse(storageOperationStatus, containerComponent.getComponentType());
3125                     log.debug("Component with id {} was not found", containerComponentId);
3126                     throw new ByActionStatusComponentException(actionStatus, Constants.EMPTY_STRING);
3127                 }
3128
3129                 maintainNodeFilters(currentResourceInstance, newComponentInstance, containerComponentId);
3130
3131                 resourceInstanceStatus = getResourceInstanceById(updatedComponentRes.left().value(),
3132                     updatedComponentInstance.getUniqueId());
3133                 if (resourceInstanceStatus.isRight()) {
3134                     throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse
3135                         (resourceInstanceStatus.right().value()), updatedComponentInstance.getUniqueId());
3136                 }
3137                 return resourceInstanceStatus.left().value();
3138             }
3139         } catch (ComponentException e) {
3140             failed = true;
3141             throw e;
3142         } finally {
3143             unlockComponent(failed, containerComponent);
3144         }
3145     }
3146
3147     private void maintainNodeFilters(
3148         ComponentInstance currentResourceInstance,
3149         ComponentInstance newComponentInstance,
3150         String containerComponentId) {
3151         CINodeFilterDataDefinition filterToMaintain = currentResourceInstance.getNodeFilter();
3152         if (null != filterToMaintain) {
3153             nodeFilterOperation.addNodeFilterData(
3154                 containerComponentId.toLowerCase(),
3155                 newComponentInstance.getUniqueId(),
3156                 filterToMaintain);
3157         }
3158     }
3159
3160     private void checkForExternalReqAndCapabilities(Component component, ComponentInstance resResourceInfo) {
3161         if (MapUtils.isNotEmpty(component.getRequirements())) {
3162             component.getRequirements().entrySet().forEach(requirementsMap -> {
3163                 if (MapUtils.isNotEmpty(resResourceInfo.getRequirements()) &&
3164                     resResourceInfo.getRequirements().containsKey(requirementsMap.getKey())) {
3165                     List<RequirementDefinition> resourceReqList = resResourceInfo.getRequirements().get(requirementsMap.getKey());
3166                     for (RequirementDefinition requirements : requirementsMap.getValue()) {
3167                         String requirementName = requirements.getName();
3168                         resourceReqList.forEach(requirementDefinition -> {
3169                             if (requirementName.equals(requirementDefinition.getName()) && requirementDefinition.isExternal()) {
3170                                 requirements.setExternal(requirementDefinition.isExternal());
3171                             }
3172                         });
3173                     }
3174                 }
3175             });
3176         }
3177         if (MapUtils.isNotEmpty(component.getCapabilities())) {
3178             component.getCapabilities().entrySet().forEach(capabilityMap -> {
3179                 if (MapUtils.isNotEmpty(resResourceInfo.getCapabilities()) && resResourceInfo.getCapabilities().containsKey(capabilityMap.getKey())) {
3180                     List<CapabilityDefinition> resourceCapList = resResourceInfo.getCapabilities().get(capabilityMap.getKey());
3181                     capabilityMap.getValue().forEach(capabilities -> {
3182                         String capabilityName = capabilities.getName();
3183                         for (CapabilityDefinition capDef : resourceCapList) {
3184                             if (capabilityName.equals(capDef.getName()) && capDef.isExternal()) {
3185                                 capabilities.setExternal(capDef.isExternal());
3186                             }
3187                         }
3188                     });
3189                 }
3190             });
3191         }
3192     }
3193
3194     private boolean isFillProxyRes(StorageOperationStatus fillProxyRes) {
3195         if (fillProxyRes != StorageOperationStatus.OK) {
3196             log.debug("Failed to fill service proxy resource data with data from service, error {}", fillProxyRes);
3197             return true;
3198         }
3199         return false;
3200     }
3201
3202     // US831698
3203     public List<ComponentInstanceProperty> getComponentInstancePropertiesById(String containerComponentTypeParam, String containerComponentId,
3204                                                                               String componentInstanceUniqueId, String userId) {
3205         Component containerComponent = null;
3206
3207         boolean failed = false;
3208         try {
3209             validateUserExists(userId);
3210             validateComponentType(containerComponentTypeParam);
3211
3212             Either<Component, StorageOperationStatus> validateContainerComponentExists = toscaOperationFacade.getToscaElement(containerComponentId);
3213             if (validateContainerComponentExists.isRight()) {
3214                 throw new ByActionStatusComponentException(
3215                     componentsUtils.convertFromStorageResponse(validateContainerComponentExists.right().value()));
3216             }
3217             containerComponent = validateContainerComponentExists.left().value();
3218
3219             Either<ComponentInstance, StorageOperationStatus> resourceInstanceStatus = getResourceInstanceById(containerComponent,
3220                 componentInstanceUniqueId);
3221             if (resourceInstanceStatus.isRight()) {
3222                 throw new ByActionStatusComponentException(ActionStatus.RESOURCE_INSTANCE_NOT_FOUND_ON_SERVICE, componentInstanceUniqueId,
3223                     containerComponentId);
3224             }
3225
3226             List<ComponentInstanceProperty> instanceProperties = new ArrayList<>();
3227             if (MapUtils.isNotEmpty(containerComponent.getComponentInstancesProperties())) {
3228                 instanceProperties = containerComponent.getComponentInstancesProperties()
3229                     .get(componentInstanceUniqueId);
3230             }
3231             return instanceProperties;
3232         } catch (ComponentException e) {
3233             failed = true;
3234             throw e;
3235         } finally {
3236             unlockComponent(failed, containerComponent);
3237         }
3238     }
3239
3240     public List<ComponentInstanceAttribute> getComponentInstanceAttributesById(final String containerComponentTypeParam,
3241                                                                                final String containerComponentId,
3242                                                                                final String componentInstanceUniqueId,
3243                                                                                final String userId) {
3244         Component containerComponent = null;
3245
3246         boolean failed = false;
3247         try {
3248             validateUserExists(userId);
3249             validateComponentType(containerComponentTypeParam);
3250
3251             final Either<Component, StorageOperationStatus> validateContainerComponentExists =
3252                 toscaOperationFacade.getToscaElement(containerComponentId);
3253             if (validateContainerComponentExists.isRight()) {
3254                 throw new ByActionStatusComponentException(
3255                     componentsUtils.convertFromStorageResponse(validateContainerComponentExists.right().value()));
3256             }
3257             containerComponent = validateContainerComponentExists.left().value();
3258
3259             if (getResourceInstanceById(containerComponent, componentInstanceUniqueId).isRight()) {
3260                 throw new ByActionStatusComponentException(
3261                     ActionStatus.RESOURCE_INSTANCE_NOT_FOUND_ON_SERVICE, componentInstanceUniqueId, containerComponentId);
3262             }
3263
3264             final Map<String, List<ComponentInstanceAttribute>> componentInstancesAttributes = containerComponent.getComponentInstancesAttributes();
3265             return componentInstancesAttributes == null ? new ArrayList<>()
3266                 : componentInstancesAttributes.getOrDefault(componentInstanceUniqueId, new ArrayList<>());
3267         } catch (final ComponentException e) {
3268             failed = true;
3269             throw e;
3270         } finally {
3271             unlockComponent(failed, containerComponent);
3272         }
3273     }
3274
3275     protected void validateIncrementCounter(String resourceInstanceId, GraphPropertiesDictionary counterType,
3276                                             Wrapper<Integer> instaceCounterWrapper,
3277                                             Wrapper<ResponseFormat> errorWrapper) {
3278         Either<Integer, StorageOperationStatus> counterRes = componentInstanceOperation
3279             .increaseAndGetResourceInstanceSpecificCounter(resourceInstanceId, counterType, true);
3280
3281         if (counterRes.isRight()) {
3282             log.debug("increase And Get {} failed resource instance {}", counterType, resourceInstanceId);
3283             StorageOperationStatus status = counterRes.right().value();
3284             ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(status);
3285             errorWrapper.setInnerElement(componentsUtils.getResponseFormat(actionStatus));
3286         } else {
3287             instaceCounterWrapper.setInnerElement(counterRes.left().value());
3288         }
3289
3290     }
3291
3292     /**
3293      * updates componentInstance modificationTime
3294      *
3295      * @param componentInstance
3296      * @param componentInstanceType
3297      * @param modificationTime
3298      * @param inTransaction
3299      * @return
3300      */
3301     public Either<ComponentInstanceData, ResponseFormat> updateComponentInstanceModificationTimeAndCustomizationUuid(
3302         ComponentInstance componentInstance, NodeTypeEnum componentInstanceType, Long modificationTime, boolean inTransaction) {
3303         Either<ComponentInstanceData, ResponseFormat> result;
3304         Either<ComponentInstanceData, StorageOperationStatus> updateComponentInstanceRes = componentInstanceOperation
3305             .updateComponentInstanceModificationTimeAndCustomizationUuidOnGraph(componentInstance, componentInstanceType, modificationTime,
3306                 inTransaction);
3307         if (updateComponentInstanceRes.isRight()) {
3308             log.debug("Failed to update component instance {} with new last update date and mofifier. Status is {}. ", componentInstance.getName(),
3309                 updateComponentInstanceRes.right().value());
3310             result = Either
3311                 .right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(updateComponentInstanceRes.right().value())));
3312         } else {
3313             result = Either.left(updateComponentInstanceRes.left().value());
3314         }
3315         return result;
3316     }
3317
3318     public Either<ComponentInstance, ResponseFormat> deleteServiceProxy() {
3319         // TODO Add implementation
3320         return Either.left(new ComponentInstance());
3321     }
3322
3323     public Either<ComponentInstance, ResponseFormat> createServiceProxy() {
3324         // TODO Add implementation
3325         return Either.left(new ComponentInstance());
3326     }
3327
3328     public Either<ComponentInstance, ResponseFormat> changeServiceProxyVersion() {
3329         // TODO Add implementation
3330         return Either.left(new ComponentInstance());
3331     }
3332
3333     private Boolean validateInstanceNameUniquenessUponUpdate(Component containerComponent, ComponentInstance oldComponentInstance,
3334                                                              String newInstanceName) {
3335         return ComponentValidations.validateNameIsUniqueInComponent(oldComponentInstance.getName(), newInstanceName, containerComponent);
3336     }
3337
3338     private Either<ComponentInstance, StorageOperationStatus> getResourceInstanceById(final Component containerComponent, final String instanceId) {
3339         final List<ComponentInstance> instances = containerComponent.getComponentInstances();
3340         if (CollectionUtils.isEmpty(instances)) {
3341             return Either.right(StorageOperationStatus.NOT_FOUND);
3342         }
3343
3344         final Optional<ComponentInstance> foundInstance = instances.stream().filter(i -> i.getUniqueId().equals(instanceId)).findFirst();
3345         if (foundInstance.isEmpty()) {
3346             return Either.right(StorageOperationStatus.NOT_FOUND);
3347         }
3348
3349         return Either.left(foundInstance.get());
3350     }
3351
3352     private ComponentInstance buildComponentInstance(ComponentInstance resourceInstanceForUpdate, ComponentInstance origInstanceForUpdate) {
3353         Long creationDate = origInstanceForUpdate.getCreationTime();
3354         Long modificationTime = System.currentTimeMillis();
3355         resourceInstanceForUpdate.setCreationTime(creationDate);
3356         resourceInstanceForUpdate.setModificationTime(modificationTime);
3357         resourceInstanceForUpdate.setCustomizationUUID(origInstanceForUpdate.getCustomizationUUID());
3358         if (StringUtils.isEmpty(resourceInstanceForUpdate.getName()) && StringUtils.isNotEmpty(origInstanceForUpdate.getName())) {
3359             resourceInstanceForUpdate.setName(origInstanceForUpdate.getName());
3360         }
3361         resourceInstanceForUpdate.setNormalizedName(ValidationUtils.normalizeComponentInstanceName(resourceInstanceForUpdate.getName()));
3362         if (StringUtils.isEmpty(resourceInstanceForUpdate.getIcon())) {
3363             resourceInstanceForUpdate.setIcon(origInstanceForUpdate.getIcon());
3364         }
3365         if (StringUtils.isEmpty(resourceInstanceForUpdate.getComponentVersion())) {
3366             resourceInstanceForUpdate.setComponentVersion(origInstanceForUpdate.getComponentVersion());
3367         }
3368         if (StringUtils.isEmpty(resourceInstanceForUpdate.getComponentName())) {
3369             resourceInstanceForUpdate.setComponentName(origInstanceForUpdate.getComponentName());
3370         }
3371         if (StringUtils.isEmpty(resourceInstanceForUpdate.getToscaComponentName())) {
3372             resourceInstanceForUpdate.setToscaComponentName(origInstanceForUpdate.getToscaComponentName());
3373         }
3374         if (resourceInstanceForUpdate.getOriginType() == null) {
3375             resourceInstanceForUpdate.setOriginType(origInstanceForUpdate.getOriginType());
3376         }
3377         if (resourceInstanceForUpdate.getOriginType() == OriginTypeEnum.ServiceProxy) {
3378             resourceInstanceForUpdate.setIsProxy(true);
3379         }
3380         if (resourceInstanceForUpdate.getSourceModelInvariant() == null) {
3381             resourceInstanceForUpdate.setSourceModelInvariant(origInstanceForUpdate.getSourceModelInvariant());
3382         }
3383         if (resourceInstanceForUpdate.getSourceModelName() == null) {
3384             resourceInstanceForUpdate.setSourceModelName(origInstanceForUpdate.getSourceModelName());
3385         }
3386         if (resourceInstanceForUpdate.getSourceModelUuid() == null) {
3387             resourceInstanceForUpdate.setSourceModelUuid(origInstanceForUpdate.getSourceModelUuid());
3388         }
3389         if (resourceInstanceForUpdate.getSourceModelUid() == null) {
3390             resourceInstanceForUpdate.setSourceModelUid(origInstanceForUpdate.getSourceModelUid());
3391         }
3392         if (resourceInstanceForUpdate.getCreatedFrom() == null) {
3393             resourceInstanceForUpdate.setCreatedFrom(origInstanceForUpdate.getCreatedFrom());
3394         }
3395         return resourceInstanceForUpdate;
3396     }
3397
3398     /**
3399      * Returns list of ComponentInstanceProperty belonging to component instance capability specified by name, type and ownerId
3400      *
3401      * @param containerComponentType
3402      * @param containerComponentId
3403      * @param componentInstanceUniqueId
3404      * @param capabilityType
3405      * @param capabilityName
3406      * @param userId
3407      * @param ownerId
3408      * @return
3409      */
3410     public List<ComponentInstanceProperty> getComponentInstanceCapabilityPropertiesById(String containerComponentType, String containerComponentId,
3411                                                                                         String componentInstanceUniqueId, String capabilityType,
3412                                                                                         String capabilityName, String ownerId, String userId) {
3413         Component containerComponent = null;
3414         List<ComponentInstanceProperty> resultOp = null;
3415         try {
3416             validateUserExists(userId);
3417             validateComponentType(containerComponentType);
3418             containerComponent = toscaOperationFacade.getToscaFullElement(containerComponentId).left().on(this::componentException);
3419             ComponentInstance resourceInstanceStatus = getResourceInstanceById(containerComponent, componentInstanceUniqueId).left()
3420                 .on(this::componentInstanceException);
3421             resultOp = findCapabilityOfInstance(containerComponentId, componentInstanceUniqueId, capabilityType, capabilityName, ownerId,
3422                 resourceInstanceStatus.getCapabilities());
3423         } catch (StorageException | ComponentException e) {
3424             unlockRollbackWithException(containerComponent, e);
3425         } catch (Exception e) {
3426             unlockRollbackWithException(containerComponent, new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR));
3427         }
3428         unlockWithCommit(containerComponent);
3429         return resultOp;
3430     }
3431
3432     private List<ComponentInstanceProperty> findCapabilityOfInstance(String componentId, String instanceId, String capabilityType,
3433                                                                      String capabilityName, String ownerId,
3434                                                                      Map<String, List<CapabilityDefinition>> instanceCapabilities) {
3435         CapabilityDefinition foundCapability;
3436         if (MapUtils.isNotEmpty(instanceCapabilities)) {
3437             List<CapabilityDefinition> capabilitiesPerType = instanceCapabilities.get(capabilityType);
3438             if (capabilitiesPerType != null) {
3439                 Optional<CapabilityDefinition> capabilityOpt = capabilitiesPerType.stream()
3440                     .filter(c -> c.getName().equals(capabilityName) && c.getOwnerId().equals(ownerId)).findFirst();
3441                 if (capabilityOpt.isPresent()) {
3442                     foundCapability = capabilityOpt.get();
3443                     return foundCapability.getProperties() == null ? new ArrayList<>() : foundCapability.getProperties();
3444                 }
3445             }
3446         }
3447         return fetchComponentInstanceCapabilityProperties(componentId, instanceId, capabilityType, capabilityName, ownerId);
3448     }
3449
3450     private List<ComponentInstanceProperty> fetchComponentInstanceCapabilityProperties(String componentId, String instanceId, String capabilityType,
3451                                                                                        String capabilityName, String ownerId) {
3452         try {
3453             return toscaOperationFacade.getComponentInstanceCapabilityProperties(componentId, instanceId, capabilityName, capabilityType, ownerId)
3454                 .left().on(this::componentInstancePropertyListException);
3455         } catch (Exception e) {
3456             log.debug("The exception {} occurred upon the component {} instance {} capability {} properties retrieving. ", componentId, instanceId,
3457                 capabilityName, e);
3458             throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR);
3459         }
3460     }
3461
3462     public Either<RequirementDefinition, ResponseFormat> updateInstanceRequirement(ComponentTypeEnum componentTypeEnum, String containerComponentId,
3463                                                                                    String componentInstanceUniqueId,
3464                                                                                    RequirementDefinition requirementDef, String userId) {
3465         Either<RequirementDefinition, ResponseFormat> resultOp = null;
3466         validateUserExists(userId);
3467         if (componentTypeEnum == null) {
3468             BeEcompErrorManager.getInstance().logInvalidInputError("updateInstanceRequirement", INVALID_COMPONENT_TYPE, ErrorSeverity.INFO);
3469             return Either.right(componentsUtils.getResponseFormat(ActionStatus.NOT_ALLOWED));
3470         }
3471         Either<Component, StorageOperationStatus> getResourceResult = toscaOperationFacade.getToscaFullElement(containerComponentId);
3472         if (getResourceResult.isRight()) {
3473             log.debug(FAILED_TO_RETRIEVE_COMPONENT_COMPONENT_ID, containerComponentId);
3474             return Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
3475         }
3476         Component containerComponent = getResourceResult.left().value();
3477         if (!ComponentValidationUtils.canWorkOnComponent(containerComponent, userId)) {
3478             log.info(RESTRICTED_OPERATION_ON_COMPONENT, userId, containerComponentId);
3479             return Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
3480         }
3481         Either<ComponentInstance, StorageOperationStatus> resourceInstanceStatus = getResourceInstanceById(containerComponent,
3482             componentInstanceUniqueId);
3483         if (resourceInstanceStatus.isRight()) {
3484             return Either.right(componentsUtils
3485                 .getResponseFormat(ActionStatus.RESOURCE_INSTANCE_NOT_FOUND_ON_SERVICE, componentInstanceUniqueId, containerComponentId));
3486         }
3487         // lock resource
3488         StorageOperationStatus lockStatus = graphLockOperation.lockComponent(containerComponentId, componentTypeEnum.getNodeType());
3489         if (lockStatus != StorageOperationStatus.OK) {
3490             log.debug(FAILED_TO_LOCK_COMPONENT, containerComponentId);
3491             return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(lockStatus)));
3492         }
3493         try {
3494             StorageOperationStatus updateRequirementStatus = toscaOperationFacade
3495                 .updateComponentInstanceRequirement(containerComponentId, componentInstanceUniqueId, requirementDef);
3496             if (updateRequirementStatus != StorageOperationStatus.OK) {
3497                 log.debug("Failed to update component instance requirement on instance {} in container {}", componentInstanceUniqueId,
3498                     containerComponentId);
3499                 return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(updateRequirementStatus)));
3500             }
3501             Either<Component, StorageOperationStatus> updateContainerRes = toscaOperationFacade
3502                 .updateComponentInstanceMetadataOfTopologyTemplate(containerComponent);
3503             if (updateContainerRes.isRight()) {
3504                 ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(updateContainerRes.right().value());
3505                 resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
3506                 return resultOp;
3507             }
3508             resultOp = Either.left(requirementDef);
3509             return resultOp;
3510         } finally {
3511             if (resultOp == null || resultOp.isRight()) {
3512                 janusGraphDao.rollback();
3513             } else {
3514                 janusGraphDao.commit();
3515             }
3516             // unlock resource
3517             graphLockOperation.unlockComponent(containerComponentId, componentTypeEnum.getNodeType());
3518         }
3519     }
3520
3521     public Either<CapabilityDefinition, ResponseFormat> updateInstanceCapability(final ComponentTypeEnum containerComponentType,
3522                                                                                  final String containerComponentId,
3523                                                                                  final String componentInstanceUniqueId,
3524                                                                                  final CapabilityDefinition capabilityDefinition,
3525                                                                                  final String userId) {
3526         if (containerComponentType == null) {
3527             BeEcompErrorManager.getInstance().logInvalidInputError("updateInstanceCapability", INVALID_COMPONENT_TYPE, ErrorSeverity.INFO);
3528             return Either.right(componentsUtils.getResponseFormat(ActionStatus.NOT_ALLOWED));
3529         }
3530         validateUserExists(userId);
3531         final Either<Component, StorageOperationStatus> getResourceResult = toscaOperationFacade.getToscaFullElement(containerComponentId);
3532         if (getResourceResult.isRight()) {
3533             log.debug(FAILED_TO_RETRIEVE_COMPONENT_COMPONENT_ID, containerComponentId);
3534             return Either.right(componentsUtils.getResponseFormat(ActionStatus.COMPONENT_NOT_FOUND, containerComponentId));
3535         }
3536         final Component containerComponent = getResourceResult.left().value();
3537         if (!ComponentValidationUtils.canWorkOnComponent(containerComponent, userId)) {
3538             log.info(RESTRICTED_OPERATION_ON_COMPONENT, userId, containerComponentId);
3539             return Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
3540         }
3541         final Either<ComponentInstance, StorageOperationStatus> resourceInstanceStatus =
3542             getResourceInstanceById(containerComponent, componentInstanceUniqueId);
3543         if (resourceInstanceStatus.isRight()) {
3544             return Either.right(componentsUtils
3545                 .getResponseFormat(ActionStatus.RESOURCE_INSTANCE_NOT_FOUND_ON_SERVICE, componentInstanceUniqueId, containerComponentId));
3546         }
3547         // lock resource
3548         final StorageOperationStatus lockStatus = graphLockOperation.lockComponent(containerComponentId, containerComponentType.getNodeType());
3549         if (lockStatus != StorageOperationStatus.OK) {
3550             log.debug(FAILED_TO_LOCK_COMPONENT, containerComponentId);
3551             return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(lockStatus)));
3552         }
3553         var success = false;
3554         try {
3555             final CapabilityDataDefinition updatedCapabilityDefinition = toscaOperationFacade
3556                 .updateComponentInstanceCapability(containerComponentId, componentInstanceUniqueId, capabilityDefinition);
3557             final Either<Component, StorageOperationStatus> updateContainerEither = toscaOperationFacade
3558                 .updateComponentInstanceMetadataOfTopologyTemplate(containerComponent);
3559             if (updateContainerEither.isRight()) {
3560                 var actionStatus = componentsUtils.convertFromStorageResponse(updateContainerEither.right().value(), containerComponentType);
3561                 return Either.right(componentsUtils.getResponseFormat(actionStatus));
3562             }
3563             success = true;
3564             return Either.left(new CapabilityDefinition(updatedCapabilityDefinition));
3565         } catch (final BusinessException e) {
3566             log.error(EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR, NodeTemplateOperation.class.getName(), (ErrorLogOptionalData) null,
3567                 FAILED_TO_UPDATE_COMPONENT_INSTANCE_CAPABILITY, componentInstanceUniqueId, containerComponentId);
3568             throw e;
3569         } catch (final Exception e) {
3570             log.error(EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR, NodeTemplateOperation.class.getName(), (ErrorLogOptionalData) null,
3571                 FAILED_TO_UPDATE_COMPONENT_INSTANCE_CAPABILITY, componentInstanceUniqueId, containerComponentId);
3572             throw new ByResponseFormatComponentException(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
3573         } finally {
3574             if (success) {
3575                 janusGraphDao.commit();
3576             } else {
3577                 janusGraphDao.rollback();
3578             }
3579             // unlock resource
3580             graphLockOperation.unlockComponent(containerComponentId, containerComponentType.getNodeType());
3581         }
3582     }
3583
3584     public Either<List<ComponentInstanceProperty>, ResponseFormat> updateInstanceCapabilityProperties(ComponentTypeEnum componentTypeEnum,
3585                                                                                                       String containerComponentId,
3586                                                                                                       String componentInstanceUniqueId,
3587                                                                                                       String capabilityType, String capabilityName,
3588                                                                                                       List<ComponentInstanceProperty> properties,
3589                                                                                                       String userId) {
3590         Either<List<ComponentInstanceProperty>, ResponseFormat> resultOp = null;
3591         validateUserExists(userId);
3592         if (componentTypeEnum == null) {
3593             BeEcompErrorManager.getInstance().logInvalidInputError("updateInstanceCapabilityProperty", INVALID_COMPONENT_TYPE, ErrorSeverity.INFO);
3594             return Either.right(componentsUtils.getResponseFormat(ActionStatus.NOT_ALLOWED));
3595         }
3596         Either<Component, StorageOperationStatus> getResourceResult = toscaOperationFacade.getToscaFullElement(containerComponentId);
3597         if (getResourceResult.isRight()) {
3598             log.debug(FAILED_TO_RETRIEVE_COMPONENT_COMPONENT_ID, containerComponentId);
3599             return Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
3600         }
3601         Component containerComponent = getResourceResult.left().value();
3602         if (!ComponentValidationUtils.canWorkOnComponent(containerComponent, userId)) {
3603             log.info(RESTRICTED_OPERATION_ON_COMPONENT, userId, containerComponentId);
3604             return Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
3605         }
3606         Either<ComponentInstance, StorageOperationStatus> resourceInstanceStatus = getResourceInstanceById(containerComponent,
3607             componentInstanceUniqueId);
3608         if (resourceInstanceStatus.isRight()) {
3609             return Either.right(componentsUtils
3610                 .getResponseFormat(ActionStatus.RESOURCE_INSTANCE_NOT_FOUND_ON_SERVICE, componentInstanceUniqueId, containerComponentId));
3611         }
3612         ComponentInstance foundResourceInstance = resourceInstanceStatus.left().value();
3613         // lock resource
3614         StorageOperationStatus lockStatus = graphLockOperation.lockComponent(containerComponentId, componentTypeEnum.getNodeType());
3615         if (lockStatus != StorageOperationStatus.OK) {
3616             log.debug(FAILED_TO_LOCK_COMPONENT, containerComponentId);
3617             return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(lockStatus)));
3618         }
3619         try {
3620             for (ComponentInstanceProperty property : properties) {
3621                 Either<String, ResponseFormat> newPropertyValueEither = validatePropertyObjectValue(property, property.getValue(), false);
3622                 newPropertyValueEither.bimap(
3623                     updatedValue -> updateCapabilityPropertyOnContainerComponent(property, updatedValue, containerComponent, foundResourceInstance,
3624                         capabilityType, capabilityName), Either::right);
3625             }
3626             Either<Component, StorageOperationStatus> updateContainerRes = toscaOperationFacade
3627                 .updateComponentInstanceMetadataOfTopologyTemplate(containerComponent);
3628             if (updateContainerRes.isRight()) {
3629                 ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(updateContainerRes.right().value());
3630                 resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
3631                 return resultOp;
3632             }
3633             resultOp = Either.left(properties);
3634             return resultOp;
3635         } finally {
3636             if (resultOp == null || resultOp.isRight()) {
3637                 janusGraphDao.rollback();
3638             } else {
3639                 janusGraphDao.commit();
3640             }
3641             // unlock resource
3642             graphLockOperation.unlockComponent(containerComponentId, componentTypeEnum.getNodeType());
3643         }
3644     }
3645
3646     public Either<Map<String, ComponentInstance>, ResponseFormat> copyComponentInstance(ComponentInstance inputComponentInstance,
3647                                                                                         String containerComponentId, String componentInstanceId,
3648                                                                                         String userId) {
3649         Map<String, ComponentInstance> resultMap = new HashMap<>();
3650         Either<Component, StorageOperationStatus> getOrigComponent = toscaOperationFacade.getToscaElement(containerComponentId);
3651         if (getOrigComponent.isRight()) {
3652             log.error("Failed to get the original component information");
3653             return Either.right(componentsUtils.getResponseFormat(ActionStatus.USER_DEFINED, FAILED_TO_COPY_COMP_INSTANCE_TO_CANVAS));
3654         }
3655         Component origComponent = getOrigComponent.left().value();
3656         try {
3657             lockComponent(origComponent, "copyComponentInstance");
3658         } catch (ComponentException e) {
3659             log.error("destComponentInstance's data is {}", origComponent.toString());
3660             return Either.right(componentsUtils
3661                 .getResponseFormat(ActionStatus.USER_DEFINED, "Failed to lock component destComponentInstance's data is {}",
3662                     origComponent.toString()));
3663         }
3664         boolean failed = false;
3665         ComponentInstance actionResponse = null;
3666         try {
3667             actionResponse = createComponentInstance("services", containerComponentId, userId, inputComponentInstance, false);
3668         } catch (ComponentException e) {
3669             failed = true;
3670             // on failure of the create instance unlock the resource and rollback the transaction.
3671             return Either.right(componentsUtils.getResponseFormat(ActionStatus.USER_DEFINED, FAILED_TO_COPY_COMP_INSTANCE_TO_CANVAS));
3672         } finally {
3673             // on failure of the create instance unlock the resource and rollback the transaction.
3674             if (null == actionResponse) {
3675                 log.error(FAILED_TO_COPY_COMP_INSTANCE_TO_CANVAS);
3676                 unlockComponent(failed, origComponent);
3677             }
3678         }
3679         Either<String, ResponseFormat> resultOp = null;
3680         try {
3681             ComponentInstance destComponentInstance = actionResponse;
3682             log.debug("destComponentInstance's data is {}", destComponentInstance);
3683             resultOp = deepCopyComponentInstance(origComponent, containerComponentId, componentInstanceId, destComponentInstance, userId);
3684             resultMap.put("componentInstance", destComponentInstance);
3685         } finally {
3686             // unlock resource
3687             if (resultOp == null || resultOp.isRight()) {
3688                 unlockComponent(true, origComponent);
3689                 janusGraphDao.rollback();
3690                 log.error("Failed to deep copy component instance");
3691             } else {
3692                 unlockComponent(false, origComponent);
3693                 janusGraphDao.commit();
3694                 log.debug("Success trasaction commit");
3695             }
3696         }
3697         if (resultOp == null || resultOp.isRight()) {
3698             return Either
3699                 .right(componentsUtils.getResponseFormat(ActionStatus.USER_DEFINED, "Failed to deep copy the component instance to the canvas"));
3700         } else {
3701             return Either.left(resultMap);
3702         }
3703     }
3704
3705     private Either<String, ResponseFormat> deepCopyComponentInstance(Component sourceComponent, String containerComponentId,
3706                                                                      String sourceComponentInstanceId, ComponentInstance destComponentInstance,
3707                                                                      String userId) {
3708         Either<Component, StorageOperationStatus> getDestComponent = toscaOperationFacade.getToscaElement(containerComponentId);
3709         if (getDestComponent.isRight()) {
3710             log.error("Failed to get the dest component information");
3711             return Either.right(componentsUtils.getResponseFormat(ActionStatus.USER_DEFINED, FAILED_TO_COPY_COMP_INSTANCE_TO_CANVAS));
3712         }
3713         Component destComponent = getDestComponent.left().value();
3714         Either<String, ResponseFormat> copyComponentInstanceWithPropertiesAndInputs = copyComponentInstanceWithPropertiesAndInputs(sourceComponent,
3715             destComponent, sourceComponentInstanceId, destComponentInstance);
3716         if (copyComponentInstanceWithPropertiesAndInputs.isRight()) {
3717             log.error("Failed to copy component instance with properties and inputs as part of deep copy");
3718             return Either.right(componentsUtils.getResponseFormat(ActionStatus.USER_DEFINED,
3719                 "Failed to copy the component instance with properties and inputs as part of deep copy"));
3720         }
3721         Either<String, ResponseFormat> copyComponentInstanceWithAttributes = copyComponentInstanceWithAttributes(sourceComponent, destComponent,
3722             sourceComponentInstanceId, destComponentInstance, userId);
3723         if (copyComponentInstanceWithAttributes.isRight()) {
3724             log.error("Failed to copy component instance with attributes as part of deep copy");
3725             return Either.right(componentsUtils
3726                 .getResponseFormat(ActionStatus.USER_DEFINED, "Failed to copy the component instance with attributes as part of deep copy"));
3727         }
3728         return Either.left(COPY_COMPONENT_INSTANCE_OK);
3729     }
3730
3731     private Either<String, ResponseFormat> copyComponentInstanceWithPropertiesAndInputs(Component sourceComponent, Component destComponent,
3732                                                                                         String sourceComponentInstanceId,
3733                                                                                         ComponentInstance destComponentInstance) {
3734         log.debug("start to copy ComponentInstance with properties and inputs");
3735         List<ComponentInstanceProperty> sourcePropList = null;
3736         if (sourceComponent.getComponentInstancesProperties() != null
3737             && sourceComponent.getComponentInstancesProperties().get(sourceComponentInstanceId) != null) {
3738             sourcePropList = sourceComponent.getComponentInstancesProperties().get(sourceComponentInstanceId);
3739             log.debug("sourcePropList");
3740         }
3741         List<ComponentInstanceProperty> destPropList = null;
3742         String destComponentInstanceId = destComponentInstance.getUniqueId();
3743         log.debug("destComponentInstanceId: {}", destComponentInstance.getUniqueId());
3744         if (destComponent.getComponentInstancesProperties() != null
3745             && destComponent.getComponentInstancesProperties().get(destComponentInstanceId) != null) {
3746             destPropList = destComponent.getComponentInstancesProperties().get(destComponentInstanceId);
3747             log.debug("destPropList {}");
3748         }
3749         List<ComponentInstancePropInput> componentInstancePropInputList = new ArrayList<>();
3750         if (null != destPropList && null != sourcePropList) {
3751             log.debug("start to set property and attribute");
3752             for (ComponentInstanceProperty destProp : destPropList) {
3753                 String destPropertyName = destProp.getName();
3754                 for (ComponentInstanceProperty sourceProp : sourcePropList) {
3755                     if (!destPropertyName.equals(sourceProp.getName())) {
3756                         continue;
3757                     }
3758                     log.debug("now set property");
3759                     final List<GetInputValueDataDefinition> getInputValues = sourceProp.getGetInputValues();
3760                     if (getInputValues == null && !StringUtils.isEmpty(sourceProp.getValue()) && (destProp.getValue() == null || !destProp.getValue()
3761                         .equals(sourceProp.getValue()))) {
3762                         log.debug("Now starting to copy the property {} in value {}", destPropertyName, sourceProp.getValue());
3763                         destProp.setValue(sourceProp.getValue());
3764                         Either<String, ResponseFormat> updatePropertyValueEither = updateComponentInstanceProperty(destComponent.getUniqueId(),
3765                             destComponentInstanceId, destProp);
3766                         if (updatePropertyValueEither.isRight()) {
3767                             log.error("Failed to copy the property {}", destPropertyName);
3768                             return Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT_PARAM,
3769                                 "Failed to paste component instance to the canvas, property copy"));
3770                         }
3771                         break;
3772                     }
3773                     log.debug("Now start to update inputs");
3774                     if (getInputValues != null) {
3775                         if (getInputValues.isEmpty()) {
3776                             log.debug("property is return from input, set by man");
3777                             break;
3778                         }
3779                         log.debug("Now starting to copy the {} property", destPropertyName);
3780                         Either<String, ResponseFormat> getSourceInputDefaultValue = getInputListDefaultValue(sourceComponent,
3781                             getInputValues.get(0).getInputId());
3782                         if (getSourceInputDefaultValue.isRight()) {
3783                             return Either.right(getSourceInputDefaultValue.right().value());
3784                         }
3785                         componentInstancePropInputList.add(new ComponentInstancePropInput(destProp));
3786                     }
3787                 }
3788             }
3789         }
3790         return Either.left(COPY_COMPONENT_INSTANCE_OK);
3791     }
3792
3793     private Either<String, ResponseFormat> copyComponentInstanceWithAttributes(Component sourceComponent, Component destComponent,
3794                                                                                String sourceComponentInstanceId,
3795                                                                                ComponentInstance destComponentInstance, String userId) {
3796         String destComponentInstanceId = destComponentInstance.getUniqueId();
3797         log.info("start to copy component instance with attributes");
3798         List<ComponentInstanceAttribute> sourceAttributeList = null;
3799         if (sourceComponent.getComponentInstancesAttributes() != null
3800             && sourceComponent.getComponentInstancesAttributes().get(sourceComponentInstanceId) != null) {
3801             sourceAttributeList = sourceComponent.getComponentInstancesAttributes().get(sourceComponentInstanceId);
3802             log.info("sourceAttributes {}");
3803         }
3804         List<ComponentInstanceAttribute> destAttributeList = null;
3805         if (destComponent.getComponentInstancesAttributes() != null
3806             && destComponent.getComponentInstancesAttributes().get(destComponentInstanceId) != null) {
3807             destAttributeList = destComponent.getComponentInstancesAttributes().get(destComponentInstanceId);
3808             log.info("destAttributeList {}");
3809         }
3810         if (null != sourceAttributeList && null != destAttributeList) {
3811             log.info("set attribute");
3812             for (ComponentInstanceAttribute sourceAttribute : sourceAttributeList) {
3813                 String sourceAttributeName = sourceAttribute.getName();
3814                 for (ComponentInstanceAttribute destAttribute : destAttributeList) {
3815                     if (sourceAttributeName.equals(destAttribute.getName())) {
3816                         log.debug("Start to copy the attribute exists {}", sourceAttributeName);
3817                         sourceAttribute.setUniqueId(
3818                             UniqueIdBuilder.buildResourceInstanceUniqueId("attribute", destComponentInstanceId.split("\\.")[1], sourceAttributeName));
3819                         Either<ComponentInstanceAttribute, ResponseFormat> updateAttributeValueEither = createOrUpdateAttributeValueForCopyPaste(
3820                             ComponentTypeEnum.SERVICE, destComponent.getUniqueId(), destComponentInstanceId, sourceAttribute, userId);
3821                         if (updateAttributeValueEither.isRight()) {
3822                             log.error("Failed to copy the attribute");
3823                             return Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT_PARAM,
3824                                 "Failed to paste component instance to the canvas, attribute copy"));
3825                         }
3826                         break;
3827                     }
3828                 }
3829             }
3830         }
3831         return Either.left(COPY_COMPONENT_INSTANCE_OK);
3832     }
3833
3834     private Either<ComponentInstanceAttribute, ResponseFormat> createOrUpdateAttributeValueForCopyPaste(ComponentTypeEnum componentTypeEnum,
3835                                                                                                         String componentId, String resourceInstanceId,
3836                                                                                                         ComponentInstanceAttribute attribute,
3837                                                                                                         String userId) {
3838         Either<ComponentInstanceAttribute, ResponseFormat> resultOp = null;
3839         validateUserExists(userId);
3840         if (componentTypeEnum == null) {
3841             BeEcompErrorManager.getInstance()
3842                 .logInvalidInputError("createOrUpdateAttributeValueForCopyPaste", INVALID_COMPONENT_TYPE, ErrorSeverity.INFO);
3843             resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.NOT_ALLOWED));
3844             return resultOp;
3845         }
3846         Either<Component, StorageOperationStatus> getResourceResult = toscaOperationFacade.getToscaElement(componentId, JsonParseFlagEnum.ParseAll);
3847         if (getResourceResult.isRight()) {
3848             log.info("Failed to retrieve component id {}", componentId);
3849             resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
3850             return resultOp;
3851         }
3852         Component containerComponent = getResourceResult.left().value();
3853         Either<ComponentInstance, StorageOperationStatus> resourceInstanceStatus = getResourceInstanceById(containerComponent, resourceInstanceId);
3854         if (resourceInstanceStatus.isRight()) {
3855             resultOp = Either
3856                 .right(componentsUtils.getResponseFormat(ActionStatus.RESOURCE_INSTANCE_NOT_FOUND_ON_SERVICE, resourceInstanceId, componentId));
3857             return resultOp;
3858         }
3859         ComponentInstance foundResourceInstance = resourceInstanceStatus.left().value();
3860         String propertyType = attribute.getType();
3861         ToscaPropertyType type = ToscaPropertyType.isValidType(propertyType);
3862         log.info("The type of attribute id{},is {} ", attribute.getUniqueId(), propertyType);
3863         if (type == ToscaPropertyType.LIST || type == ToscaPropertyType.MAP) {
3864             SchemaDefinition def = attribute.getSchema();
3865             if (def == null) {
3866                 log.info("Schema doesn't exists for attribute of type {}", type);
3867                 return Either
3868                     .right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(StorageOperationStatus.INVALID_VALUE)));
3869             }
3870             PropertyDataDefinition propDef = def.getProperty();
3871             if (propDef == null) {
3872                 log.info("Attribute in Schema Definition inside attribute of type {} doesn't exist", type);
3873                 return Either
3874                     .right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(StorageOperationStatus.INVALID_VALUE)));
3875             }
3876         }
3877         List<ComponentInstanceAttribute> instanceAttributes = containerComponent.getComponentInstancesAttributes().get(resourceInstanceId);
3878         Optional<ComponentInstanceAttribute> instanceAttribute = instanceAttributes.stream()
3879             .filter(p -> p.getUniqueId().equals(attribute.getUniqueId())).findAny();
3880         StorageOperationStatus status;
3881         if (instanceAttribute.isPresent()) {
3882             log.info("updateComponentInstanceAttribute");
3883             status = toscaOperationFacade.updateComponentInstanceAttribute(containerComponent, foundResourceInstance.getUniqueId(), attribute);
3884         } else {
3885             log.info("addComponentInstanceAttribute");
3886             status = toscaOperationFacade.addComponentInstanceAttribute(containerComponent, foundResourceInstance.getUniqueId(), attribute);
3887         }
3888         if (status != StorageOperationStatus.OK) {
3889             ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(status);
3890             resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
3891             return resultOp;
3892         }
3893         List<String> path = new ArrayList<>();
3894         path.add(foundResourceInstance.getUniqueId());
3895         attribute.setPath(path);
3896         foundResourceInstance.setCustomizationUUID(UUID.randomUUID().toString());
3897         Either<Component, StorageOperationStatus> updateContainerRes = toscaOperationFacade
3898             .updateComponentInstanceMetadataOfTopologyTemplate(containerComponent);
3899         if (updateContainerRes.isRight()) {
3900             ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(updateContainerRes.right().value());
3901             resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
3902             return resultOp;
3903         }
3904         resultOp = Either.left(attribute);
3905         return resultOp;
3906     }
3907
3908     private Either<String, ResponseFormat> updateComponentInstanceProperty(String containerComponentId, String componentInstanceId,
3909                                                                            ComponentInstanceProperty property) {
3910         Either<String, ResponseFormat> resultOp;
3911         Either<Component, StorageOperationStatus> getComponent = toscaOperationFacade.getToscaElement(containerComponentId);
3912         if (getComponent.isRight()) {
3913             log.error("Failed to get the component information");
3914             return Either.right(componentsUtils
3915                 .getResponseFormatForResourceInstanceProperty(ActionStatus.INVALID_CONTENT_PARAM, "Failed to get the component information"));
3916         }
3917         Component containerComponent = getComponent.left().value();
3918         StorageOperationStatus status = toscaOperationFacade.updateComponentInstanceProperty(containerComponent, componentInstanceId, property);
3919         if (status != StorageOperationStatus.OK) {
3920             ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(status);
3921             resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
3922             return resultOp;
3923         }
3924         Either<Component, StorageOperationStatus> updateContainerRes = toscaOperationFacade
3925             .updateComponentInstanceMetadataOfTopologyTemplate(containerComponent);
3926         if (updateContainerRes.isRight()) {
3927             ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(updateContainerRes.right().value());
3928             resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
3929             return resultOp;
3930         }
3931         return Either.left("Update OK");
3932     }
3933
3934     private Either<String, ResponseFormat> getInputListDefaultValue(Component component, String inputId) {
3935         List<InputDefinition> inputList = component.getInputs();
3936         for (InputDefinition input : inputList) {
3937             if (input.getUniqueId().equals(inputId)) {
3938                 if (input.getDefaultValue() == null) {
3939                     log.debug("The input's default value is null");
3940                     return Either.left(null);
3941                 }
3942                 return Either.left(input.getDefaultValue());
3943             }
3944         }
3945         log.error("The input's default value with id {} is not found", inputId);
3946         return Either.right(componentsUtils.getResponseFormat(ActionStatus.USER_DEFINED, "Failed to paste component instance to the canvas"));
3947     }
3948
3949     /**
3950      * Method to delete selected nodes and edges on composition page
3951      *
3952      * @param containerComponentType
3953      * @param componentId
3954      * @param componentInstanceIdList
3955      * @param userId
3956      * @return
3957      */
3958     public Map<String, List<String>> batchDeleteComponentInstance(String containerComponentType, String componentId,
3959                                                                   List<String> componentInstanceIdList, String userId) {
3960         List<String> deleteErrorIds = new ArrayList<>();
3961         Map<String, List<String>> deleteErrorMap = new HashMap<>();
3962         validateUserExists(userId);
3963         org.openecomp.sdc.be.model.Component containerComponent = validateComponentExists(componentId,
3964             ComponentTypeEnum.findByParamName(containerComponentType), null);
3965         boolean failed = false;
3966         try {
3967             lockComponent(containerComponent, "batchDeleteComponentInstance");
3968             for (String eachInstanceId : componentInstanceIdList) {
3969                 Either<ComponentInstance, ResponseFormat> actionResponse = batchDeleteComponentInstance(containerComponent, containerComponentType,
3970                     eachInstanceId);
3971                 log.debug("batchDeleteResourceInstances actionResponse is {}", actionResponse);
3972                 if (actionResponse.isRight()) {
3973                     log.error("Failed to delete ComponentInstance [{}]", eachInstanceId);
3974                     deleteErrorIds.add(eachInstanceId);
3975                 }
3976             }
3977             //sending the ids of the error nodes that were not deleted to UI
3978             deleteErrorMap.put("deleteFailedIds", deleteErrorIds);
3979             return deleteErrorMap;
3980         } catch (ComponentException e) {
3981             failed = true;
3982             throw e;
3983         } finally {
3984             unlockComponent(failed, containerComponent);
3985         }
3986     }
3987
3988     private Either<ComponentInstance, ResponseFormat> batchDeleteComponentInstance(Component containerComponent, String containerComponentType,
3989                                                                                    String componentInstanceId) {
3990         ComponentInstance resultOp;
3991         final ComponentTypeEnum containerComponentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
3992         try {
3993             resultOp = deleteComponentInstance(containerComponent, componentInstanceId, containerComponentTypeEnum);
3994             log.info("Successfully deleted instance with id {}", componentInstanceId);
3995             return Either.left(resultOp);
3996         } catch (ComponentException e) {
3997             log.error("Failed to deleteComponentInstance with instanceId[{}]", componentInstanceId);
3998             return Either.right(new ResponseFormat());
3999         }
4000     }
4001
4002     private void validatePropertyConstraintsNotChanged(List<ComponentInstanceProperty> newProperties, ComponentInstance originalResourceInstance) {
4003         for (ComponentInstanceProperty newProperty : newProperties) {
4004             Optional<PropertyDefinition> originalProperty = originalResourceInstance.getProperties().stream()
4005                 .filter(prop -> prop.getUniqueId().equals(newProperty.getUniqueId())).findAny();
4006             if (originalProperty.isPresent()) {
4007                 List<PropertyConstraint> originalConstraints = originalProperty.get().getConstraints();
4008                 List<PropertyConstraint> newConstraints = newProperty.getConstraints();
4009                 if (!Objects.equals(originalConstraints, newConstraints)) {
4010                     throw new ByActionStatusComponentException(ActionStatus.CANNOT_CHANGE_CONSTRAINTS);
4011                 }
4012             } else {
4013                 throw new ByActionStatusComponentException(ActionStatus.PROPERTY_NOT_FOUND, newProperty.getUniqueId());
4014             }
4015         }
4016     }
4017
4018     private Either<Boolean, ResponseFormat> validatePropertyValueConstraint(List<? extends PropertyDefinition> properties, final String componentId) {
4019         try {
4020             String propertyModel = propertyBusinessLogic.getComponentModelByComponentId(componentId);
4021             PropertyValueConstraintValidationUtil propertyValueConstraintValidationUtil = new PropertyValueConstraintValidationUtil();
4022             return propertyValueConstraintValidationUtil.validatePropertyConstraints(properties, applicationDataTypeCache, propertyModel);
4023         } catch (BusinessLogicException e) {
4024             return Either.right(e.getResponseFormat());
4025         }
4026     }
4027
4028     public void validateUser(final String userId) {
4029         final User user = userValidations.validateUserExists(userId);
4030         userValidations.validateUserRole(user, Arrays.asList(Role.DESIGNER, Role.ADMIN));
4031     }
4032
4033     public void setCompositionBusinessLogic(CompositionBusinessLogic compositionBusinessLogic) {
4034         this.compositionBusinessLogic = compositionBusinessLogic;
4035     }
4036
4037     public void setContainerInstanceTypesData(ContainerInstanceTypesData containerInstanceTypesData) {
4038         this.containerInstanceTypesData = containerInstanceTypesData;
4039     }
4040
4041 }