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