miss a pair of {} in log
[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 com.google.common.collect.Sets;
24 import fj.data.Either;
25 import org.apache.commons.collections.CollectionUtils;
26 import org.apache.commons.collections.MapUtils;
27 import org.apache.commons.lang.StringUtils;
28 import org.apache.commons.lang3.tuple.ImmutablePair;
29 import org.openecomp.sdc.be.components.impl.instance.ComponentInstanceChangeOperationOrchestrator;
30 import org.openecomp.sdc.be.components.merge.instance.ComponentInstanceMergeDataBusinessLogic;
31 import org.openecomp.sdc.be.components.merge.instance.DataForMergeHolder;
32 import org.openecomp.sdc.be.components.validation.ComponentValidations;
33 import org.openecomp.sdc.be.config.BeEcompErrorManager;
34 import org.openecomp.sdc.be.config.BeEcompErrorManager.ErrorSeverity;
35 import org.openecomp.sdc.be.dao.api.ActionStatus;
36 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
37 import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary;
38 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
39 import org.openecomp.sdc.be.datatypes.elements.ForwardingPathDataDefinition;
40 import org.openecomp.sdc.be.datatypes.elements.GetInputValueDataDefinition;
41 import org.openecomp.sdc.be.datatypes.elements.GroupDataDefinition;
42 import org.openecomp.sdc.be.datatypes.elements.RequirementDataDefinition;
43 import org.openecomp.sdc.be.datatypes.elements.CapabilityDataDefinition;
44 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
45 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
46 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
47 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
48 import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
49 import org.openecomp.sdc.be.impl.ForwardingPathUtils;
50 import org.openecomp.sdc.be.info.CreateAndAssotiateInfo;
51 import org.openecomp.sdc.be.model.Component;
52 import org.openecomp.sdc.be.model.ComponentParametersView;
53 import org.openecomp.sdc.be.model.CapabilityDefinition;
54 import org.openecomp.sdc.be.model.RequirementDefinition;
55 import org.openecomp.sdc.be.model.Resource;
56 import org.openecomp.sdc.be.model.Service;
57 import org.openecomp.sdc.be.model.User;
58 import org.openecomp.sdc.be.model.ComponentInstancePropInput;
59 import org.openecomp.sdc.be.model.LifecycleStateEnum;
60 import org.openecomp.sdc.be.model.ArtifactDefinition;
61 import org.openecomp.sdc.be.model.DataTypeDefinition;
62 import org.openecomp.sdc.be.model.PropertyDefinition;
63 import org.openecomp.sdc.be.model.GroupDefinition;
64 import org.openecomp.sdc.be.model.InputDefinition;
65 import org.openecomp.sdc.be.model.RelationshipInfo;
66 import org.openecomp.sdc.be.model.RequirementCapabilityRelDef;
67 import org.openecomp.sdc.be.model.ComponentInstance;
68 import org.openecomp.sdc.be.model.ComponentInstanceInput;
69 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
70 import org.openecomp.sdc.be.model.PropertyDefinition.PropertyNames;
71 import org.openecomp.sdc.be.model.jsontitan.operations.ForwardingPathOperation;
72 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
73 import org.openecomp.sdc.be.model.jsontitan.utils.ModelConverter;
74 import org.openecomp.sdc.be.model.operations.api.IComponentInstanceOperation;
75 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
76 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
77 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
78 import org.openecomp.sdc.be.model.operations.utils.ComponentValidationUtils;
79 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
80 import org.openecomp.sdc.be.resources.data.ComponentInstanceData;
81 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
82 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
83 import org.openecomp.sdc.common.api.Constants;
84 import org.openecomp.sdc.common.datastructure.Wrapper;
85 import org.openecomp.sdc.common.jsongraph.util.CommonUtility;
86 import org.openecomp.sdc.common.jsongraph.util.CommonUtility.LogLevelEnum;
87 import org.openecomp.sdc.common.log.wrappers.Logger;
88 import org.openecomp.sdc.common.util.ValidationUtils;
89 import org.openecomp.sdc.exception.ResponseFormat;
90 import org.springframework.beans.factory.annotation.Autowired;
91
92 import java.util.ArrayList;
93 import java.util.Collection;
94 import java.util.Collections;
95 import java.util.HashMap;
96 import java.util.Iterator;
97 import java.util.List;
98 import java.util.Map;
99 import java.util.Map.Entry;
100 import java.util.Objects;
101 import java.util.Optional;
102 import java.util.Set;
103 import java.util.UUID;
104 import java.util.function.BiConsumer;
105 import java.util.stream.Collectors;
106
107 import static org.openecomp.sdc.be.components.property.GetInputUtils.isGetInputValueForInput;
108
109 @org.springframework.stereotype.Component
110 public class ComponentInstanceBusinessLogic extends BaseBusinessLogic {
111
112     private static final Logger log = Logger.getLogger(ComponentInstanceBusinessLogic.class.getName());
113     private static final String VF_MODULE = "org.openecomp.groups.VfModule";
114     public static final String TRY_TO_CREATE_ENTRY_ON_GRAPH = "Try to create entry on graph";
115     public static final String FAILED_TO_CREATE_ENTRY_ON_GRAPH_FOR_COMPONENT_INSTANCE = "Failed to create entry on graph for component instance {}";
116     public static final String ENTITY_ON_GRAPH_IS_CREATED = "Entity on graph is created.";
117     public static final String INVALID_COMPONENT_TYPE = "invalid component type";
118     public static final String FAILED_TO_RETRIEVE_COMPONENT_COMPONENT_ID = "Failed to retrieve component, component id {}";
119     public static final String FAILED_TO_LOCK_SERVICE = "Failed to lock service {}";
120     public static final String CREATE_OR_UPDATE_PROPERTY_VALUE = "CreateOrUpdatePropertyValue";
121
122     @Autowired
123     private IComponentInstanceOperation componentInstanceOperation;
124
125     @Autowired
126     private ArtifactsBusinessLogic artifactBusinessLogic;
127
128     @Autowired
129     private ComponentInstanceMergeDataBusinessLogic compInstMergeDataBL;
130
131     @Autowired
132     private ComponentInstanceChangeOperationOrchestrator onChangeInstanceOperationOrchestrator;
133
134     @Autowired
135     private ForwardingPathOperation forwardingPathOperation;
136
137     public ComponentInstanceBusinessLogic() {
138     }
139
140     public Either<ComponentInstance, ResponseFormat> createComponentInstance(
141             String containerComponentParam, String containerComponentId, String userId, ComponentInstance resourceInstance) {
142         return createComponentInstance(containerComponentParam, containerComponentId, userId, resourceInstance, false, true);
143     }
144
145     public List<ComponentInstanceProperty> getComponentInstancePropertiesByInputId(org.openecomp.sdc.be.model.Component component, String inputId){
146         List<ComponentInstanceProperty> resList = new ArrayList<>();
147         Map<String, List<ComponentInstanceProperty>> ciPropertiesMap = component.getComponentInstancesProperties();
148         if(ciPropertiesMap != null && !ciPropertiesMap.isEmpty()){
149             ciPropertiesMap.forEach(new BiConsumer<String, List<ComponentInstanceProperty>>() {
150                 @Override
151                 public void accept(String s, List<ComponentInstanceProperty> ciPropList) {
152                     String ciName = "";
153                     Optional<ComponentInstance> ciOp = component.getComponentInstances().stream().filter(ci ->ci.getUniqueId().equals(s)).findAny();
154                     if(ciOp.isPresent())
155                         ciName = ciOp.get().getName();
156                     if (ciPropList != null && !ciPropList.isEmpty()) {
157                         for(ComponentInstanceProperty prop: ciPropList){
158                             List<GetInputValueDataDefinition> inputsValues = prop.getGetInputValues();
159                             if(inputsValues != null && !inputsValues.isEmpty()){
160                                 for(GetInputValueDataDefinition inputData: inputsValues){
161                                     if(isGetInputValueForInput(inputData, inputId)){
162                                         prop.setComponentInstanceId(s);
163                                         prop.setComponentInstanceName(ciName);
164                                         resList.add(prop);
165                                         break;
166                                     }
167                                 }
168                             }
169
170                         }
171                     }
172                 }
173             });
174         }
175         return resList;
176     }
177
178     public List<ComponentInstanceInput> getComponentInstanceInputsByInputId(org.openecomp.sdc.be.model.Component component, String inputId){
179         List<ComponentInstanceInput> resList = new ArrayList<>();
180         Map<String, List<ComponentInstanceInput>> ciInputsMap = component.getComponentInstancesInputs();
181         if(ciInputsMap != null && !ciInputsMap.isEmpty()){
182             ciInputsMap.forEach(new BiConsumer<String, List<ComponentInstanceInput>>() {
183                 @Override
184                 public void accept(String s, List<ComponentInstanceInput> ciPropList) {
185                     String ciName = "";
186                     Optional<ComponentInstance> ciOp = component.getComponentInstances().stream().filter(ci ->ci.getUniqueId().equals(s)).findAny();
187                     if(ciOp.isPresent())
188                         ciName = ciOp.get().getName();
189                     if (ciPropList != null && !ciPropList.isEmpty()) {
190                         for(ComponentInstanceInput prop: ciPropList){
191                             List<GetInputValueDataDefinition> inputsValues = prop.getGetInputValues();
192                             if(inputsValues != null && !inputsValues.isEmpty()){
193                                 for(GetInputValueDataDefinition inputData: inputsValues){
194                                     if(isGetInputValueForInput(inputData, inputId)){
195                                         prop.setComponentInstanceId(s);
196                                         prop.setComponentInstanceName(ciName);
197                                         resList.add(prop);
198                                         break;
199                                     }
200                                 }
201                             }
202
203                         }
204                     }
205                 }
206             });
207         }
208         return resList;
209     }
210
211     public Either<ComponentInstance, ResponseFormat> createComponentInstance(
212             String containerComponentParam, String containerComponentId, String userId, ComponentInstance resourceInstance, boolean inTransaction, boolean needLock) {
213
214         Component origComponent = null;
215         Either<ComponentInstance, ResponseFormat> resultOp = null;
216         User user = null;
217         org.openecomp.sdc.be.model.Component containerComponent = null;
218         ComponentTypeEnum containerComponentType;
219
220         try {
221             user = validateUserExists(userId, "create Component Instance", inTransaction);
222
223             Either<Boolean, ResponseFormat> validateValidJson = validateJsonBody(resourceInstance, ComponentInstance.class);
224             if (validateValidJson.isRight()) {
225                 return Either.right(validateValidJson.right().value());
226             }
227
228             Either<ComponentTypeEnum, ResponseFormat> validateComponentType = validateComponentType(containerComponentParam);
229             if (validateComponentType.isRight()) {
230                 return Either.right(validateComponentType.right().value());
231             } else {
232                 containerComponentType = validateComponentType.left().value();
233             }
234
235             Either<org.openecomp.sdc.be.model.Component, ResponseFormat> validateComponentExists = validateComponentExists(containerComponentId, containerComponentType, null);
236             if (validateComponentExists.isRight()) {
237                 return Either.right(validateComponentExists.right().value());
238             } else {
239                 containerComponent = validateComponentExists.left().value();
240             }
241
242             if (ModelConverter.isAtomicComponent(containerComponent)) {
243                 log.debug("Cannot attach resource instances to container resource of type {}", containerComponent.assetType());
244                 return Either.right(componentsUtils.getResponseFormat(ActionStatus.RESOURCE_CANNOT_CONTAIN_RESOURCE_INSTANCES, containerComponent.assetType()));
245             }
246
247             Either<Boolean, ResponseFormat> validateCanWorkOnComponent = validateCanWorkOnComponent(containerComponent, userId);
248             if (validateCanWorkOnComponent.isRight()) {
249                 return Either.right(validateCanWorkOnComponent.right().value());
250             }
251
252             if (resourceInstance != null && containerComponentType != null) {
253                 OriginTypeEnum originType = resourceInstance.getOriginType();
254                 if (originType == OriginTypeEnum.ServiceProxy) {
255                     Either<Component, StorageOperationStatus> serviceProxyOrigin = toscaOperationFacade.getLatestByName("serviceProxy");
256                     if (serviceProxyOrigin.isRight()) {
257                         log.debug("Failed to fetch normative service proxy resource by tosca name, error {}", serviceProxyOrigin.right().value());
258                         return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(serviceProxyOrigin.right().value())));
259                     }
260                     origComponent = serviceProxyOrigin.left().value();
261
262                     StorageOperationStatus fillProxyRes = fillProxyInstanceData(resourceInstance, origComponent);
263                     if (fillProxyRes != StorageOperationStatus.OK) {
264                         log.debug("Failed to fill service proxy resource data with data from service, error {}", fillProxyRes);
265                         return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(fillProxyRes)));
266
267                     }
268
269                 } else {
270                     Either<Component, ResponseFormat> getOriginComponentRes = getAndValidateOriginComponentOfComponentInstance(containerComponentType, resourceInstance);
271
272                     if (getOriginComponentRes.isRight()) {
273                         return Either.right(getOriginComponentRes.right().value());
274                     } else {
275                         origComponent = getOriginComponentRes.left().value();
276                     }
277                 }
278             }
279             if (needLock) {
280                 Either<Boolean, ResponseFormat> lockComponent = lockComponent(containerComponent, "createComponentInstance");
281                 if (lockComponent.isRight()) {
282                     return Either.right(lockComponent.right().value());
283                 }
284             }
285             log.debug("Try to create entry on graph");
286             resultOp = createComponentInstanceOnGraph(containerComponent, origComponent, resourceInstance, user);
287             return resultOp;
288
289         } finally {
290             if (needLock)
291                 unlockComponent(resultOp, containerComponent);
292         }
293     }
294
295     private StorageOperationStatus fillProxyInstanceData(ComponentInstance resourceInstance, Component proxyTemplate) {
296         resourceInstance.setIsProxy(true);
297         ComponentParametersView filter = new ComponentParametersView(true);
298         filter.setIgnoreCapabilities(false);
299         filter.setIgnoreCapabiltyProperties(false);
300         filter.setIgnoreComponentInstances(false);
301         filter.setIgnoreRequirements(false);
302         Either<Component, StorageOperationStatus> serviceRes = toscaOperationFacade.getToscaElement(resourceInstance.getComponentUid(), filter);
303         if (serviceRes.isRight()) {
304             return serviceRes.right().value();
305         }
306         Component service = serviceRes.left().value();
307         Map<String, List<CapabilityDefinition>> capabilities = service.getCapabilities();
308         resourceInstance.setCapabilities(capabilities);
309         Map<String, List<RequirementDefinition>> req = service.getRequirements();
310         resourceInstance.setRequirements(req);
311
312         String name = service.getNormalizedName() + ToscaOperationFacade.PROXY_SUFFIX;
313         String toscaResourceName = ((Resource) proxyTemplate).getToscaResourceName();
314         int lastIndexOf = toscaResourceName.lastIndexOf('.');
315         if (lastIndexOf != -1) {
316             String proxyToscaName = toscaResourceName.substring(0, lastIndexOf + 1) + name;
317             resourceInstance.setToscaComponentName(proxyToscaName);
318         }
319         resourceInstance.setName(name);
320         resourceInstance.setIsProxy(true);
321         resourceInstance.setSourceModelInvariant(service.getInvariantUUID());
322         resourceInstance.setSourceModelName(service.getName());
323         resourceInstance.setSourceModelUuid(service.getUUID());
324         resourceInstance.setSourceModelUid(service.getUniqueId());
325         resourceInstance.setComponentUid(proxyTemplate.getUniqueId());
326         resourceInstance.setDescription("A Proxy for Service " + service.getName());
327         resourceInstance.setComponentVersion(service.getVersion());
328
329         return StorageOperationStatus.OK;
330     }
331
332     public Either<CreateAndAssotiateInfo, ResponseFormat> createAndAssociateRIToRI(String containerComponentParam, String containerComponentId, String userId, CreateAndAssotiateInfo createAndAssotiateInfo) {
333
334         Either<CreateAndAssotiateInfo, ResponseFormat> resultOp = null;
335         ComponentInstance resourceInstance = createAndAssotiateInfo.getNode();
336         RequirementCapabilityRelDef associationInfo = createAndAssotiateInfo.getAssociate();
337
338         User user = validateUserExists(userId, "create And Associate RI To RI", false);
339
340         Either<ComponentTypeEnum, ResponseFormat> validateComponentType = validateComponentType(containerComponentParam);
341         if (validateComponentType.isRight()) {
342             return Either.right(validateComponentType.right().value());
343         }
344
345         final ComponentTypeEnum containerComponentType = validateComponentType.left().value();
346
347         Either<org.openecomp.sdc.be.model.Component, ResponseFormat> validateComponentExists = validateComponentExists(containerComponentId, containerComponentType, null);
348         if (validateComponentExists.isRight()) {
349             return Either.right(validateComponentExists.right().value());
350         }
351         org.openecomp.sdc.be.model.Component containerComponent = validateComponentExists.left().value();
352
353         if (ModelConverter.isAtomicComponent(containerComponent)) {
354             log.debug("Cannot attach resource instances to container resource of type {}", containerComponent.assetType());
355             return Either.right(componentsUtils.getResponseFormat(ActionStatus.RESOURCE_CANNOT_CONTAIN_RESOURCE_INSTANCES, containerComponent.assetType()));
356         }
357
358         Either<Boolean, ResponseFormat> validateCanWorkOnComponent = validateCanWorkOnComponent(containerComponent, userId);
359         if (validateCanWorkOnComponent.isRight()) {
360             return Either.right(validateCanWorkOnComponent.right().value());
361         }
362
363         Either<Boolean, ResponseFormat> lockComponent = lockComponent(containerComponent, "createAndAssociateRIToRI");
364         if (lockComponent.isRight()) {
365             return Either.right(lockComponent.right().value());
366         }
367
368         try {
369             log.debug("Try to create entry on graph");
370             Either<Component, ResponseFormat> eitherResourceName = getOriginComponentFromComponentInstance(resourceInstance);
371
372             if (eitherResourceName.isRight()) {
373                 resultOp = Either.right(eitherResourceName.right().value());
374                 return resultOp;
375             }
376             Component origComponent = eitherResourceName.left().value();
377
378             Either<ComponentInstance, ResponseFormat> result = createComponentInstanceOnGraph(containerComponent, origComponent, resourceInstance, user);
379             if (result.isRight()) {
380                 log.debug("Failed to create resource instance {}", containerComponentId);
381                 resultOp = Either.right(result.right().value());
382                 return resultOp;
383
384             }
385
386             log.debug("Entity on graph is created.");
387             ComponentInstance resResourceInfo = result.left().value();
388             if (associationInfo.getFromNode() == null || associationInfo.getFromNode().isEmpty()) {
389                 associationInfo.setFromNode(resResourceInfo.getUniqueId());
390             } else {
391                 associationInfo.setToNode(resResourceInfo.getUniqueId());
392             }
393
394             RequirementCapabilityRelDef requirementCapabilityRelDef = associationInfo;
395             Either<RequirementCapabilityRelDef, StorageOperationStatus> resultReqCapDef = toscaOperationFacade.associateResourceInstances(containerComponentId, requirementCapabilityRelDef);
396             if (resultReqCapDef.isLeft()) {
397                 log.debug("Enty on graph is created.");
398                 RequirementCapabilityRelDef resReqCapabilityRelDef = resultReqCapDef.left().value();
399                 CreateAndAssotiateInfo resInfo = new CreateAndAssotiateInfo(resResourceInfo, resReqCapabilityRelDef);
400                 resultOp = Either.left(resInfo);
401                 return resultOp;
402
403             } else {
404                 log.info("Failed to associate node {} with node {}", associationInfo.getFromNode(), associationInfo.getToNode());
405                 resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstance(componentsUtils.convertFromStorageResponseForResourceInstance(resultReqCapDef.right().value(), true), "", null));
406                 return resultOp;
407             }
408
409         } finally {
410             unlockComponent(resultOp, containerComponent);
411         }
412     }
413
414     private Either<Component, ResponseFormat> getOriginComponentFromComponentInstance(ComponentInstance componentInstance) {
415         return getOriginComponentFromComponentInstance(componentInstance.getName(), componentInstance.getComponentUid());
416     }
417
418     private Either<Component, ResponseFormat> getInstanceOriginNode(ComponentInstance componentInstance) {
419         return getOriginComponentFromComponentInstance(componentInstance.getName(), componentInstance.getActualComponentUid());
420     }
421
422     private Either<Component, ResponseFormat> getOriginComponentFromComponentInstance(String componentInstanceName, String origComponetId) {
423         Either<Component, ResponseFormat> eitherResponse;
424         Either<Component, StorageOperationStatus> eitherComponent = toscaOperationFacade.getToscaFullElement(origComponetId);
425         if (eitherComponent.isRight()) {
426             log.debug("Failed to get origin component with id {} for component instance {} ", origComponetId, componentInstanceName);
427             eitherResponse = Either.right(componentsUtils.getResponseFormatForResourceInstance(componentsUtils.convertFromStorageResponse(eitherComponent.right().value(), ComponentTypeEnum.RESOURCE), "", null));
428         } else {
429             eitherResponse = Either.left(eitherComponent.left().value());
430         }
431         return eitherResponse;
432     }
433
434     private Either<ComponentInstance, ResponseFormat> createComponentInstanceOnGraph(org.openecomp.sdc.be.model.Component containerComponent, Component originComponent, ComponentInstance componentInstance, User user) {
435         Either<ComponentInstance, ResponseFormat> resultOp;
436
437         Either<ImmutablePair<Component, String>, StorageOperationStatus> result = toscaOperationFacade.addComponentInstanceToTopologyTemplate(containerComponent, originComponent, componentInstance, false, user);
438
439         if (result.isRight()) {
440             log.debug("Failed to create entry on graph for component instance {}", componentInstance.getName());
441             resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstance(componentsUtils.convertFromStorageResponseForResourceInstance(result.right().value(), true), "", null));
442             return resultOp;
443         }
444
445         log.debug("Entity on graph is created.");
446         Component updatedComponent = result.left().value().getLeft();
447         Map<String, String> existingEnvVersions = new HashMap<>();
448         // TODO existingEnvVersions ??
449         Either<ActionStatus, ResponseFormat> addComponentInstanceArtifacts = addComponentInstanceArtifacts(updatedComponent, componentInstance, originComponent, user, existingEnvVersions);
450         if (addComponentInstanceArtifacts.isRight()) {
451             log.debug("Failed to create component instance {}", componentInstance.getName());
452             resultOp = Either.right(addComponentInstanceArtifacts.right().value());
453             return resultOp;
454         }
455
456         Optional<ComponentInstance> updatedInstanceOptional = updatedComponent.getComponentInstances().stream().filter(ci -> ci.getUniqueId().equals(result.left().value().getRight())).findFirst();
457         if (!updatedInstanceOptional.isPresent()) {
458             log.debug("Failed to fetch new added component instance {} from component {}", componentInstance.getName(), containerComponent.getName());
459             resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER, componentInstance.getName()));
460             return resultOp;
461         }
462         resultOp = Either.left(updatedInstanceOptional.get());
463         return resultOp;
464     }
465
466     /**
467      * addResourceInstanceArtifacts - add artifacts (HEAT_ENV) to resource instance The instance artifacts are generated from the resource's artifacts
468      * @param containerComponent
469      * @param componentInstance
470      * @param originComponent
471      * @param user
472      * @param existingEnvVersions
473      * @return
474      */
475     protected Either<ActionStatus, ResponseFormat> addComponentInstanceArtifacts(org.openecomp.sdc.be.model.Component containerComponent, ComponentInstance componentInstance, org.openecomp.sdc.be.model.Component originComponent, User user,    Map<String, String> existingEnvVersions) {
476
477         log.debug("add artifacts to resource instance");
478         List<GroupDefinition> filteredGroups = null;
479         ActionStatus status = setResourceArtifactsOnResourceInstance(componentInstance);
480         if (!ActionStatus.OK.equals(status)) {
481             ResponseFormat resultOp = componentsUtils.getResponseFormatForResourceInstance(status, "", null);
482             return Either.right(resultOp);
483         }
484         StorageOperationStatus artStatus;
485         // generate heat_env if necessary
486         Map<String, ArtifactDefinition> componentDeploymentArtifacts = componentInstance.getDeploymentArtifacts();
487         if (MapUtils.isNotEmpty(componentDeploymentArtifacts)) {
488
489             Map<String, ArtifactDefinition> finalDeploymentArtifacts = new HashMap<String, ArtifactDefinition>();
490             Map<String, List<ArtifactDefinition>> groupInstancesArtifacts = new HashMap<>();
491
492             for (ArtifactDefinition artifact : componentDeploymentArtifacts.values()) {
493                 String type = artifact.getArtifactType();
494                 if (!type.equalsIgnoreCase(ArtifactTypeEnum.HEAT_ENV.getType())) {
495                     finalDeploymentArtifacts.put(artifact.getArtifactLabel(), artifact);
496                 }
497                 if (!(type.equalsIgnoreCase(ArtifactTypeEnum.HEAT.getType()) || type.equalsIgnoreCase(ArtifactTypeEnum.HEAT_NET.getType()) || type.equalsIgnoreCase(ArtifactTypeEnum.HEAT_VOL.getType()))) {
498                     continue;
499                 }
500                 if (artifact.checkEsIdExist()) {
501                     Either<ArtifactDefinition, ResponseFormat> createHeatEnvPlaceHolder = artifactBusinessLogic.createHeatEnvPlaceHolder(artifact, ArtifactsBusinessLogic.HEAT_ENV_NAME, componentInstance.getUniqueId(), NodeTypeEnum.ResourceInstance,
502                             componentInstance.getName(), user, containerComponent, existingEnvVersions);
503                     if (createHeatEnvPlaceHolder.isRight()) {
504                         return Either.right(createHeatEnvPlaceHolder.right().value());
505                     }
506                     ArtifactDefinition artifactDefinition = createHeatEnvPlaceHolder.left().value();
507                     // put env
508                     finalDeploymentArtifacts.put(artifactDefinition.getArtifactLabel(), artifactDefinition);
509
510                     if (CollectionUtils.isNotEmpty(originComponent.getGroups())) {
511                         filteredGroups = originComponent.getGroups().stream().filter(g -> g.getType().equals(VF_MODULE)).collect(Collectors.toList());
512                     }
513                     if (CollectionUtils.isNotEmpty(filteredGroups)) {
514                         for (GroupDefinition groupInstance : filteredGroups) {
515                             Optional<String> op = groupInstance.getArtifacts().stream().filter(p -> p.equals(artifactDefinition.getGeneratedFromId())).findAny();
516                             if (op.isPresent()) {
517                                 List<ArtifactDefinition> artifactsUid;
518                                 if (groupInstancesArtifacts.containsKey(groupInstance.getUniqueId())) {
519                                     artifactsUid = groupInstancesArtifacts.get(groupInstance.getUniqueId());
520                                 } else {
521                                     artifactsUid = new ArrayList<>();
522                                 }
523                                 artifactsUid.add(artifactDefinition);
524                                 groupInstancesArtifacts.put(groupInstance.getUniqueId(), artifactsUid);
525                                 break;
526                             }
527                         }
528                     }
529                 }
530             }
531             artStatus = toscaOperationFacade.addDeploymentArtifactsToInstance(containerComponent.getUniqueId(), componentInstance, finalDeploymentArtifacts);
532             if (artStatus != StorageOperationStatus.OK) {
533                 log.debug("Failed to add instance deployment artifacts for instance {} in conatiner {} error {}", componentInstance.getUniqueId(), containerComponent.getUniqueId(), artStatus);
534                 return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponseForResourceInstance(artStatus, false)));
535
536             }
537             StorageOperationStatus result = toscaOperationFacade.addGroupInstancesToComponentInstance(containerComponent, componentInstance, filteredGroups, groupInstancesArtifacts);
538             if (result != StorageOperationStatus.OK) {
539                 log.debug("failed to update group instance for component instance {}", componentInstance.getUniqueId());
540                 return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(result)));
541             }
542             componentInstance.setDeploymentArtifacts(finalDeploymentArtifacts);
543         }
544
545         artStatus = toscaOperationFacade.addInformationalArtifactsToInstance(containerComponent.getUniqueId(), componentInstance, originComponent.getArtifacts());
546         if (artStatus != StorageOperationStatus.OK) {
547             log.debug("Failed to add informational artifacts to the instance {} belonging to the conatiner {}. Status is {}", componentInstance.getUniqueId(), containerComponent.getUniqueId(), artStatus);
548             return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponseForResourceInstance(artStatus, false)));
549
550         }
551         componentInstance.setArtifacts(originComponent.getArtifacts());
552         return Either.left(ActionStatus.OK);
553     }
554
555     private ActionStatus setResourceArtifactsOnResourceInstance(ComponentInstance resourceInstance) {
556         Either<Map<String, ArtifactDefinition>, StorageOperationStatus> getResourceDeploymentArtifacts = artifactBusinessLogic.getArtifacts(resourceInstance.getComponentUid(), NodeTypeEnum.Resource, ArtifactGroupTypeEnum.DEPLOYMENT, null);
557
558         Map<String, ArtifactDefinition> deploymentArtifacts = new HashMap<String, ArtifactDefinition>();
559         if (getResourceDeploymentArtifacts.isRight()) {
560             StorageOperationStatus status = getResourceDeploymentArtifacts.right().value();
561             if (!status.equals(StorageOperationStatus.NOT_FOUND)) {
562                 log.debug("Failed to fetch resource: {} artifacts. status is {}", resourceInstance.getComponentUid(), status);
563                 return componentsUtils.convertFromStorageResponseForResourceInstance(status, true);
564             }
565         } else {
566             deploymentArtifacts = getResourceDeploymentArtifacts.left().value();
567         }
568
569         if (!deploymentArtifacts.isEmpty()) {
570             Map<String, ArtifactDefinition> tempDeploymentArtifacts = new HashMap<String, ArtifactDefinition>(deploymentArtifacts);
571             for (Entry<String, ArtifactDefinition> artifact : deploymentArtifacts.entrySet()) {
572                 if (!artifact.getValue().checkEsIdExist()) {
573                     tempDeploymentArtifacts.remove(artifact.getKey());
574                 }
575             }
576
577             resourceInstance.setDeploymentArtifacts(tempDeploymentArtifacts);
578         }
579
580         return ActionStatus.OK;
581     }
582
583     public Either<ComponentInstance, ResponseFormat> updateComponentInstanceMetadata(String containerComponentParam, String containerComponentId, String componentInstanceId, String userId, ComponentInstance componentInstance) {
584         return updateComponentInstanceMetadata(containerComponentParam, containerComponentId, componentInstanceId, userId, componentInstance, false, true, true);
585     }
586
587     public Either<ComponentInstance, ResponseFormat> updateComponentInstanceMetadata(String containerComponentParam, String containerComponentId, String componentInstanceId, String userId, ComponentInstance componentInstance, boolean inTransaction,
588                                                                                      boolean needLock, boolean createNewTransaction) {
589
590         validateUserExists(userId, "update Component Instance", inTransaction);
591
592         Either<ComponentInstance, ResponseFormat> resultOp = null;
593
594         Either<ComponentTypeEnum, ResponseFormat> validateComponentType = validateComponentType(containerComponentParam);
595         if (validateComponentType.isRight()) {
596             return Either.right(validateComponentType.right().value());
597         }
598
599         final ComponentTypeEnum containerComponentType = validateComponentType.left().value();
600
601         Either<org.openecomp.sdc.be.model.Component, ResponseFormat> validateComponentExists = validateComponentExists(containerComponentId, containerComponentType, null);
602         if (validateComponentExists.isRight()) {
603             return Either.right(validateComponentExists.right().value());
604         }
605         org.openecomp.sdc.be.model.Component containerComponent = validateComponentExists.left().value();
606
607         Either<Boolean, ResponseFormat> validateCanWorkOnComponent = validateCanWorkOnComponent(containerComponent, userId);
608         if (validateCanWorkOnComponent.isRight()) {
609             return Either.right(validateCanWorkOnComponent.right().value());
610         }
611         ComponentTypeEnum instanceType = getComponentType(containerComponentType);
612         Either<Boolean, StorageOperationStatus> validateParentStatus = toscaOperationFacade.validateComponentExists(componentInstance.getComponentUid());
613         if (validateParentStatus.isRight()) {
614             log.debug("Failed to get component instance {} on service {}", componentInstanceId, containerComponentId);
615             resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND, componentInstance.getName(), instanceType.getValue().toLowerCase()));
616             return resultOp;
617         }
618         if (!validateParentStatus.left().value()) {
619             resultOp = Either.right(
620                     componentsUtils.getResponseFormat(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER, componentInstance.getName(), instanceType.getValue().toLowerCase(), containerComponentType.getValue().toLowerCase(), containerComponentId));
621             return resultOp;
622         }
623
624         if (needLock) {
625             Either<Boolean, ResponseFormat> lockComponent = lockComponent(containerComponent, "updateComponentInstance");
626             if (lockComponent.isRight()) {
627                 return Either.right(lockComponent.right().value());
628             }
629         }
630         try {
631
632             Either<Component, ResponseFormat> eitherResourceName = getOriginComponentFromComponentInstance(componentInstance);
633
634             if (eitherResourceName.isRight()) {
635                 resultOp = Either.right(eitherResourceName.right().value());
636                 return resultOp;
637             }
638             Component origComponent = eitherResourceName.left().value();
639
640             resultOp = updateComponentInstanceMetadata(containerComponent, containerComponentType, origComponent, componentInstanceId, componentInstance);
641             return resultOp;
642
643         } finally {
644             if (needLock)
645                 unlockComponent(resultOp, containerComponent);
646         }
647     }
648
649     // New Multiple Instance Update API
650     public Either<List<ComponentInstance>, ResponseFormat> updateComponentInstance(String containerComponentParam, String containerComponentId, String userId, List<ComponentInstance> componentInstanceList, boolean needLock) {
651
652         Either<List<ComponentInstance>, ResponseFormat> resultOp = null;
653         org.openecomp.sdc.be.model.Component containerComponent = null;
654         try {
655             validateUserExists(userId, "update Component Instance", true);
656
657             Either<ComponentTypeEnum, ResponseFormat> validateComponentType = validateComponentType(containerComponentParam);
658             if (validateComponentType.isRight()) {
659                 return Either.right(validateComponentType.right().value());
660             }
661
662             final ComponentTypeEnum containerComponentType = validateComponentType.left().value();
663
664             ComponentParametersView componentFilter = new ComponentParametersView();
665             componentFilter.disableAll();
666             componentFilter.setIgnoreUsers(false);
667             componentFilter.setIgnoreComponentInstances(false);
668             Either<org.openecomp.sdc.be.model.Component, ResponseFormat> validateComponentExists = validateComponentExistsByFilter(containerComponentId, containerComponentType, componentFilter);
669             if (validateComponentExists.isRight()) {
670                 return Either.right(validateComponentExists.right().value());
671             }
672
673             containerComponent = validateComponentExists.left().value();
674
675             Either<Boolean, ResponseFormat> validateCanWorkOnComponent = validateCanWorkOnComponent(containerComponent, userId);
676             if (validateCanWorkOnComponent.isRight()) {
677                 return Either.right(validateCanWorkOnComponent.right().value());
678             }
679
680             ComponentTypeEnum instanceType = getComponentType(containerComponentType);
681
682             for (ComponentInstance componentInstance : componentInstanceList) {
683                 boolean validateParent = validateParent(containerComponent, componentInstance.getUniqueId());
684                 if (!validateParent) {
685                     resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER, componentInstance.getName(), instanceType.getValue().toLowerCase(), containerComponentType.getValue().toLowerCase(),
686                             containerComponentId));
687                     return resultOp;
688                 }
689             }
690
691             if (needLock) {
692
693                 Either<Boolean, ResponseFormat> lockComponent = lockComponent(containerComponent, "updateComponentInstance");
694                 if (lockComponent.isRight()) {
695                     return Either.right(lockComponent.right().value());
696                 }
697             }
698
699             List<ComponentInstance> updatedList = new ArrayList<>();
700             List<ComponentInstance> instancesFromContainerComponent = containerComponent.getComponentInstances();
701             List<ComponentInstance> listForUpdate = new ArrayList<>();
702             if (instancesFromContainerComponent == null || instancesFromContainerComponent.isEmpty())
703                 containerComponent.setComponentInstances(componentInstanceList);
704             else {
705                 Iterator<ComponentInstance> iterator = instancesFromContainerComponent.iterator();
706                 while (iterator.hasNext()) {
707                     ComponentInstance origInst = iterator.next();
708                     Optional<ComponentInstance> op = componentInstanceList.stream().filter(ci -> ci.getUniqueId().equals(origInst.getUniqueId())).findAny();
709                     if (op.isPresent()) {
710                         ComponentInstance updatedCi = op.get();
711                         updatedCi = buildComponentInstance(updatedCi, origInst);
712
713                         Boolean isUniqueName = validateInstanceNameUniquenessUponUpdate(containerComponent, origInst, updatedCi.getName());
714                         if (!isUniqueName) {
715                             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());
716                             resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.COMPONENT_NAME_ALREADY_EXIST, containerComponentType.getValue(), origInst.getName()));
717                             return resultOp;
718                         }
719
720                         listForUpdate.add(updatedCi);
721                     } else
722                         listForUpdate.add(origInst);
723                 }
724                 containerComponent.setComponentInstances(listForUpdate);
725
726                 if (resultOp == null) {
727                     Either<Component, StorageOperationStatus> updateStatus = toscaOperationFacade.updateComponentInstanceMetadataOfTopologyTemplate(containerComponent, componentFilter);
728                     if (updateStatus.isRight()) {
729                         CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to update metadata belonging to container component {}. Status is {}. ", containerComponent.getName(), updateStatus.right().value());
730                         resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstance(componentsUtils.convertFromStorageResponseForResourceInstance(updateStatus.right().value(), true), "", null));
731                         return resultOp;
732                     }
733                     for (ComponentInstance updatedInstance : updateStatus.left().value().getComponentInstances()) {
734                         Optional<ComponentInstance> op = componentInstanceList.stream().filter(ci -> ci.getName().equals(updatedInstance.getName())).findAny();
735                         if (op.isPresent()) {
736                             updatedList.add(updatedInstance);
737                         }
738                     }
739                 }
740             }
741
742             resultOp = Either.left(updatedList);
743             return resultOp;
744
745         } finally {
746             if (needLock) {
747                 unlockComponent(resultOp, containerComponent);
748             }
749         }
750     }
751
752     private boolean validateParent(org.openecomp.sdc.be.model.Component containerComponent, String nodeTemplateId) {
753         return containerComponent.getComponentInstances().stream().anyMatch(p -> p.getUniqueId().equals(nodeTemplateId));
754     }
755
756     private ComponentTypeEnum getComponentType(ComponentTypeEnum containerComponentType) {
757         if (ComponentTypeEnum.PRODUCT.equals(containerComponentType)) {
758             return ComponentTypeEnum.SERVICE_INSTANCE;
759         } else {
760             return ComponentTypeEnum.RESOURCE_INSTANCE;
761         }
762     }
763
764     private Either<ComponentInstance, ResponseFormat> updateComponentInstanceMetadata(Component containerComponent, ComponentTypeEnum containerComponentType, org.openecomp.sdc.be.model.Component origComponent, String componentInstanceId,
765                                                                                       ComponentInstance componentInstance) {
766
767         Either<ComponentInstance, ResponseFormat> resultOp = null;
768         Optional<ComponentInstance> componentInstanceOptional = null;
769         Either<ImmutablePair<Component, String>, StorageOperationStatus> updateRes = null;
770         ComponentInstance oldComponentInstance = null;
771         boolean isNameChanged = false;
772
773         if (resultOp == null) {
774             componentInstanceOptional = containerComponent.getComponentInstances().stream().filter(ci -> ci.getUniqueId().equals(componentInstance.getUniqueId())).findFirst();
775             if (!componentInstanceOptional.isPresent()) {
776                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to find the component instance {} in container component {}. ", componentInstance.getName(), containerComponent.getName());
777                 resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER, componentInstance.getName()));
778             }
779         }
780         if (resultOp == null) {
781             oldComponentInstance = componentInstanceOptional.get();
782             String newInstanceName = componentInstance.getName();
783             if (oldComponentInstance != null && oldComponentInstance.getName() != null && !oldComponentInstance.getName().equals(newInstanceName))
784                 isNameChanged = true;
785             Boolean isUniqueName = validateInstanceNameUniquenessUponUpdate(containerComponent, oldComponentInstance, newInstanceName);
786             if (!isUniqueName) {
787                 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);
788                 resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.COMPONENT_NAME_ALREADY_EXIST, containerComponentType.getValue(), componentInstance.getName()));
789             }
790         }
791         if (resultOp == null) {
792             updateRes = toscaOperationFacade.updateComponentInstanceMetadataOfTopologyTemplate(containerComponent, origComponent, updateComponentInstanceMetadata(oldComponentInstance, componentInstance));
793             if (updateRes.isRight()) {
794                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to update metadata of component instance {} belonging to container component {}. Status is {}. ", componentInstance.getName(), containerComponent.getName(),
795                         updateRes.right().value());
796                 resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstance(componentsUtils.convertFromStorageResponseForResourceInstance(updateRes.right().value(), true), "", null));
797             } else {
798                 // region - Update instance Groups
799                 if (isNameChanged) {
800                     Either result = toscaOperationFacade.cleanAndAddGroupInstancesToComponentInstance(containerComponent, oldComponentInstance, componentInstanceId);
801                     if (result.isRight())
802                         CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to rename group instances for container {}. error {} ", componentInstanceId, result.right().value());
803                 }
804                 // endregion
805             }
806         }
807         if (resultOp == null) {
808             String newInstanceId = updateRes.left().value().getRight();
809             Optional<ComponentInstance> updatedInstanceOptional = updateRes.left().value().getLeft().getComponentInstances().stream().filter(ci -> ci.getUniqueId().equals(newInstanceId)).findFirst();
810
811             if (!updatedInstanceOptional.isPresent()) {
812                 log.debug("Failed to update metadata of component instance {} of container component {}", componentInstance.getName(), containerComponent.getName());
813                 resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER, componentInstance.getName()));
814             } else {
815                 resultOp = Either.left(updatedInstanceOptional.get());
816             }
817
818         }
819         if (resultOp == null) {
820             resultOp = Either.left(componentInstanceOptional.get());
821         }
822         return resultOp;
823     }
824
825     /**
826      * @param oldPrefix-
827      *            The normalized old vf name
828      * @param newNormailzedPrefix-
829      *            The normalized new vf name
830      * @param qualifiedGroupInstanceName-
831      *            old Group Instance Name
832      **/
833     // modify group names
834     private String getNewGroupName(String oldPrefix, String newNormailzedPrefix, String qualifiedGroupInstanceName) {
835         if (qualifiedGroupInstanceName == null) {
836             log.info("CANNOT change group name ");
837             return null;
838         }
839         if (qualifiedGroupInstanceName.startsWith(oldPrefix) || qualifiedGroupInstanceName.startsWith(ValidationUtils.normalizeComponentInstanceName(oldPrefix)))
840             return qualifiedGroupInstanceName.replaceFirst(oldPrefix, newNormailzedPrefix);
841         return qualifiedGroupInstanceName;
842     }
843
844     private ComponentInstance updateComponentInstanceMetadata(ComponentInstance oldComponentInstance, ComponentInstance newComponentInstance) {
845         oldComponentInstance.setName(newComponentInstance.getName());
846         oldComponentInstance.setModificationTime(System.currentTimeMillis());
847         oldComponentInstance.setCustomizationUUID(UUID.randomUUID().toString());
848         if (oldComponentInstance.getGroupInstances() != null)
849             oldComponentInstance.getGroupInstances().forEach(group -> group.setName(getNewGroupName(oldComponentInstance.getNormalizedName(), ValidationUtils.normalizeComponentInstanceName(newComponentInstance.getName()), group.getName())));
850         return oldComponentInstance;
851     }
852
853     public Either<ComponentInstance, ResponseFormat> deleteComponentInstance(String containerComponentParam, String containerComponentId, String componentInstanceId, String userId) {
854
855         validateUserExists(userId, "delete Component Instance", false);
856
857         Either<ComponentTypeEnum, ResponseFormat> validateComponentType = validateComponentType(containerComponentParam);
858         if (validateComponentType.isRight()) {
859             return Either.right(validateComponentType.right().value());
860         }
861
862         final ComponentTypeEnum containerComponentType = validateComponentType.left().value();
863         Either<org.openecomp.sdc.be.model.Component, ResponseFormat> validateComponentExists = validateComponentExists(containerComponentId, containerComponentType, null);
864         if (validateComponentExists.isRight()) {
865             return Either.right(validateComponentExists.right().value());
866         }
867         org.openecomp.sdc.be.model.Component containerComponent = validateComponentExists.left().value();
868         Either<Boolean, ResponseFormat> validateCanWorkOnComponent = validateCanWorkOnComponent(containerComponent, userId);
869         if (validateCanWorkOnComponent.isRight()) {
870             return Either.right(validateCanWorkOnComponent.right().value());
871         }
872
873         Either<Boolean, ResponseFormat> lockComponent = lockComponent(containerComponent, "deleteComponentInstance");
874         if (lockComponent.isRight()) {
875             return Either.right(lockComponent.right().value());
876         }
877
878         Either<ComponentInstance, ResponseFormat> resultOp = null;
879         try {
880             resultOp = deleteComponentInstance(containerComponent, componentInstanceId, containerComponentType);
881             if (resultOp.isRight()){
882                 return resultOp;
883             }
884             Either<ComponentInstance, ResponseFormat> deleteEither = deleteForwardingPathsRelatedTobeDeletedComponentInstance(containerComponentId,
885                     containerComponentType, resultOp);
886             if (deleteEither.isRight()){
887                 return deleteEither;
888             }
889             return deleteEither;
890
891         } finally {
892             unlockComponent(resultOp, containerComponent);
893         }
894     }
895
896     public Either<ComponentInstance, ResponseFormat> deleteForwardingPathsRelatedTobeDeletedComponentInstance(String containerComponentId, ComponentTypeEnum containerComponentType,
897                                                                                                               Either<ComponentInstance, ResponseFormat> resultOp) {
898         if(containerComponentType.equals(ComponentTypeEnum.SERVICE) && resultOp.isLeft() ){
899             final ComponentInstance componentInstance = resultOp.left().value();
900             List<String> pathIDsToBeDeleted = getForwardingPathsRelatedToComponentInstance(containerComponentId, componentInstance.getName());
901             if (!pathIDsToBeDeleted.isEmpty()) {
902                 Either<Set<String>, ResponseFormat> deleteForwardingPathsEither = deleteForwardingPaths(containerComponentId,
903                         pathIDsToBeDeleted);
904                 if(deleteForwardingPathsEither.isRight()) {
905                     resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
906                 }
907
908             }
909         }
910         return resultOp;
911     }
912
913
914     private Either<Set<String>, ResponseFormat> deleteForwardingPaths(String serviceId,  List<String> pathIdsToDelete){
915
916         Either<Service, StorageOperationStatus> storageStatus = toscaOperationFacade.getToscaElement(serviceId);
917         if(storageStatus.isRight()) {
918             return Either.right(componentsUtils.getResponseFormat(storageStatus.right().value()));
919         }
920         Either<Set<String>, StorageOperationStatus> result = forwardingPathOperation.deleteForwardingPath(storageStatus.left().value(),
921                 Sets.newHashSet(pathIdsToDelete));
922
923         if(result.isRight()) {
924             return Either.right(componentsUtils.getResponseFormat(result.right().value()));
925         }
926         return  Either.left(result.left().value());
927     }
928
929     private List<String> getForwardingPathsRelatedToComponentInstance(String containerComponentId, String componentInstanceId){
930         ComponentParametersView filter = new ComponentParametersView(true);
931         filter.setIgnoreForwardingPath(false);
932         Either<Service, StorageOperationStatus> forwardingPathOrigin = toscaOperationFacade
933                 .getToscaElement(containerComponentId, filter);
934         Collection<ForwardingPathDataDefinition> allPaths = forwardingPathOrigin.left().value().getForwardingPaths().values();
935         List<String> pathIDsToBeDeleted = new ArrayList<>();
936
937         allPaths.stream().filter(path -> isPathRelatedToComponent(path,componentInstanceId ))
938                 .forEach(path -> pathIDsToBeDeleted.add(path.getUniqueId()));
939
940         return pathIDsToBeDeleted;
941     }
942
943     private boolean isPathRelatedToComponent(ForwardingPathDataDefinition pathDataDefinition,
944                                              String componentInstanceId){
945         return pathDataDefinition.getPathElements().getListToscaDataDefinition()
946                 .stream().anyMatch(elementDataDefinition -> elementDataDefinition.getFromNode().equalsIgnoreCase(componentInstanceId) ||
947                         elementDataDefinition.getToNode()
948                                 .equalsIgnoreCase(componentInstanceId));
949     }
950
951
952     private Either<ComponentInstance, ResponseFormat> deleteComponentInstance(Component containerComponent, String componentInstanceId, ComponentTypeEnum containerComponentType) {
953
954         Either<ComponentInstance, ResponseFormat> resultOp = null;
955         ComponentInstance deletedInstance = null;
956         Either<ImmutablePair<Component, String>, StorageOperationStatus> deleteRes = toscaOperationFacade.deleteComponentInstanceFromTopologyTemplate(containerComponent, componentInstanceId);
957
958         if (deleteRes.isRight()) {
959             log.debug("Failed to delete entry on graph for resourceInstance {}", componentInstanceId);
960             ActionStatus status = componentsUtils.convertFromStorageResponse(deleteRes.right().value(), containerComponentType);
961             resultOp = Either.right(componentsUtils.getResponseFormat(status, componentInstanceId));
962         }
963         if (resultOp == null) {
964             log.debug("The component instance {} has been removed from container component {}. ", componentInstanceId, containerComponent);
965             deletedInstance = findAndRemoveComponentInstanceFromContainerComponent(componentInstanceId, containerComponent);
966             resultOp = Either.left(deletedInstance);
967         }
968         if (resultOp.isLeft() && CollectionUtils.isNotEmpty(containerComponent.getGroups())) {
969             List<GroupDataDefinition> groupsToUpdate = new ArrayList<>();
970             for (GroupDataDefinition currGroup : containerComponent.getGroups()) {
971                 Map<String, String> members = currGroup.getMembers();
972                 if (members != null && members.containsKey(deletedInstance.getName())) {
973                     members.remove(deletedInstance.getName());
974                     groupsToUpdate.add(currGroup);
975                 }
976             }
977             Either<List<GroupDefinition>, StorageOperationStatus> updateGroupsRes = toscaOperationFacade.updateGroupsOnComponent(containerComponent, groupsToUpdate);
978             if (updateGroupsRes.isRight()) {
979                 log.debug("Failed to delete component instance {} from group members. ", componentInstanceId);
980                 ActionStatus status = componentsUtils.convertFromStorageResponse(updateGroupsRes.right().value(), containerComponentType);
981                 resultOp = Either.right(componentsUtils.getResponseFormat(status, componentInstanceId));
982             }
983         }
984         if (resultOp.isLeft() && CollectionUtils.isNotEmpty(containerComponent.getInputs())) {
985             List<InputDefinition> inputsToDelete = containerComponent.getInputs().stream().filter(i -> i.getInstanceUniqueId() != null && i.getInstanceUniqueId().equals(componentInstanceId)).collect(Collectors.toList());
986             if (CollectionUtils.isNotEmpty(inputsToDelete)) {
987                 StorageOperationStatus deleteInputsRes = toscaOperationFacade.deleteComponentInstanceInputsFromTopologyTemplate(containerComponent, inputsToDelete);
988                 if (deleteInputsRes != StorageOperationStatus.OK) {
989                     log.debug("Failed to delete inputs of the component instance {} from container component. ", componentInstanceId);
990                     resultOp = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(deleteInputsRes, containerComponentType), componentInstanceId));
991                 }
992             }
993         }
994         return resultOp;
995     }
996
997     private ComponentInstance findAndRemoveComponentInstanceFromContainerComponent(String componentInstanceId, Component containerComponent) {
998         ComponentInstance foundInstance = null;
999         for (ComponentInstance instance : containerComponent.getComponentInstances()) {
1000             if (instance.getUniqueId().equals(componentInstanceId)) {
1001                 foundInstance = instance;
1002                 containerComponent.getComponentInstances().remove(instance);
1003                 break;
1004             }
1005         }
1006         findAndRemoveComponentInstanceRelations(componentInstanceId, containerComponent);
1007         return foundInstance;
1008     }
1009
1010     private void findAndRemoveComponentInstanceRelations(String componentInstanceId, Component containerComponent) {
1011         if(CollectionUtils.isNotEmpty(containerComponent.getComponentInstancesRelations())){
1012             containerComponent.setComponentInstancesRelations(containerComponent.getComponentInstancesRelations().stream().filter(r -> isNotBelongingRelation(componentInstanceId, r)).collect(Collectors.toList()));
1013         }
1014     }
1015
1016     private boolean isNotBelongingRelation(String componentInstanceId, RequirementCapabilityRelDef relation) {
1017         return !relation.getToNode().equals(componentInstanceId) && !relation.getFromNode().equals(componentInstanceId);
1018     }
1019
1020     public Either<RequirementCapabilityRelDef, ResponseFormat> associateRIToRI(String componentId, String userId, RequirementCapabilityRelDef requirementDef, ComponentTypeEnum componentTypeEnum) {
1021         return associateRIToRI(componentId, userId, requirementDef, componentTypeEnum, false, true, true);
1022     }
1023
1024     public Either<RequirementCapabilityRelDef, ResponseFormat> associateRIToRI(String componentId, String userId, RequirementCapabilityRelDef requirementDef, ComponentTypeEnum componentTypeEnum, boolean inTransaction, boolean needLock,
1025                                                                                boolean createNewTransaction) {
1026
1027         validateUserExists(userId, "associate Ri To RI", inTransaction);
1028
1029         Either<RequirementCapabilityRelDef, ResponseFormat> resultOp = null;
1030
1031         Either<org.openecomp.sdc.be.model.Component, ResponseFormat> validateComponentExists = validateComponentExists(componentId, componentTypeEnum, null);
1032         if (validateComponentExists.isRight()) {
1033             return Either.right(validateComponentExists.right().value());
1034         }
1035         org.openecomp.sdc.be.model.Component containerComponent = validateComponentExists.left().value();
1036
1037         Either<Boolean, ResponseFormat> validateCanWorkOnComponent = validateCanWorkOnComponent(containerComponent, userId);
1038         if (validateCanWorkOnComponent.isRight()) {
1039             return Either.right(validateCanWorkOnComponent.right().value());
1040         }
1041         if (needLock) {
1042             Either<Boolean, ResponseFormat> lockComponent = lockComponent(containerComponent, "associateRIToRI");
1043
1044             if (lockComponent.isRight()) {
1045                 return Either.right(lockComponent.right().value());
1046             }
1047         }
1048
1049         try {
1050
1051             resultOp = associateRIToRIOnGraph(validateComponentExists.left().value(), requirementDef, componentTypeEnum, inTransaction);
1052
1053             return resultOp;
1054
1055         } finally {
1056             if (needLock)
1057                 unlockComponent(resultOp, containerComponent);
1058         }
1059     }
1060
1061     public Either<RequirementCapabilityRelDef, ResponseFormat> associateRIToRIOnGraph(Component containerComponent, RequirementCapabilityRelDef requirementDef, ComponentTypeEnum componentTypeEnum, boolean inTransaction) {
1062
1063         log.debug("Try to create entry on graph");
1064         Either<RequirementCapabilityRelDef, ResponseFormat> resultOp = null;
1065
1066         Either<RequirementCapabilityRelDef, StorageOperationStatus> result = toscaOperationFacade.associateResourceInstances(containerComponent.getUniqueId(), requirementDef);
1067
1068         if (result.isLeft()) {
1069             log.debug("Enty on graph is created.");
1070             RequirementCapabilityRelDef requirementCapabilityRelDef = result.left().value();
1071             resultOp = Either.left(requirementCapabilityRelDef);
1072             return resultOp;
1073
1074         } else {
1075             log.debug("Failed to associate node: {} with node {}", requirementDef.getFromNode(), requirementDef.getToNode());
1076             String fromNameOrId = "";
1077             String toNameOrId = "";
1078             Either<ComponentInstance, StorageOperationStatus> fromResult = getResourceInstanceById(containerComponent, requirementDef.getFromNode());
1079             Either<ComponentInstance, StorageOperationStatus> toResult = getResourceInstanceById(containerComponent, requirementDef.getToNode());
1080
1081             toNameOrId = requirementDef.getFromNode();
1082             fromNameOrId = requirementDef.getFromNode();
1083             if (fromResult.isLeft()) {
1084                 fromNameOrId = fromResult.left().value().getName();
1085             }
1086             if (toResult.isLeft()) {
1087                 toNameOrId = toResult.left().value().getName();
1088             }
1089
1090             resultOp = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponseForResourceInstance(result.right().value(), true), fromNameOrId, toNameOrId, requirementDef.getRelationships().get(0).getRelation().getRequirement()));
1091
1092             return resultOp;
1093         }
1094
1095     }
1096
1097     public Either<RequirementCapabilityRelDef, ResponseFormat> dissociateRIFromRI(String componentId, String userId, RequirementCapabilityRelDef requirementDef, ComponentTypeEnum componentTypeEnum) {
1098         validateUserExists(userId, "dissociate RI From RI", false);
1099
1100         Either<RequirementCapabilityRelDef, ResponseFormat> resultOp = null;
1101         Either<org.openecomp.sdc.be.model.Component, ResponseFormat> validateComponentExists = validateComponentExists(componentId, componentTypeEnum, null);
1102         if (validateComponentExists.isRight()) {
1103             return Either.right(validateComponentExists.right().value());
1104         }
1105         org.openecomp.sdc.be.model.Component containerComponent = validateComponentExists.left().value();
1106
1107         Either<Boolean, ResponseFormat> validateCanWorkOnComponent = validateCanWorkOnComponent(containerComponent, userId);
1108         if (validateCanWorkOnComponent.isRight()) {
1109             return Either.right(validateCanWorkOnComponent.right().value());
1110         }
1111         Either<Boolean, ResponseFormat> lockComponent = lockComponent(containerComponent, "associateRIToRI");
1112
1113         if (lockComponent.isRight()) {
1114             return Either.right(lockComponent.right().value());
1115         }
1116         try {
1117             log.debug("Try to create entry on graph");
1118             Either<RequirementCapabilityRelDef, StorageOperationStatus> result = toscaOperationFacade.dissociateResourceInstances(componentId, requirementDef);
1119             if (result.isLeft()) {
1120                 log.debug("Enty on graph is created.");
1121                 RequirementCapabilityRelDef requirementCapabilityRelDef = result.left().value();
1122                 resultOp = Either.left(requirementCapabilityRelDef);
1123                 return resultOp;
1124
1125             } else {
1126
1127                 log.debug("Failed to dissocaite node  {} from node {}", requirementDef.getFromNode(), requirementDef.getToNode());
1128                 String fromNameOrId = "";
1129                 String toNameOrId = "";
1130                 Either<ComponentInstance, StorageOperationStatus> fromResult = getResourceInstanceById(containerComponent, requirementDef.getFromNode());
1131                 Either<ComponentInstance, StorageOperationStatus> toResult = getResourceInstanceById(containerComponent, requirementDef.getToNode());
1132
1133                 toNameOrId = requirementDef.getFromNode();
1134                 fromNameOrId = requirementDef.getFromNode();
1135                 if (fromResult.isLeft()) {
1136                     fromNameOrId = fromResult.left().value().getName();
1137                 }
1138                 if (toResult.isLeft()) {
1139                     toNameOrId = toResult.left().value().getName();
1140                 }
1141
1142                 resultOp = Either
1143                         .right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponseForResourceInstance(result.right().value(), true), fromNameOrId, toNameOrId, requirementDef.getRelationships().get(0).getRelation().getRequirement()));
1144                 return resultOp;
1145             }
1146         } finally {
1147             unlockComponent(resultOp, containerComponent);
1148         }
1149     }
1150     /**
1151      * Allows to get relation contained in specified component according to received Id
1152      * @param componentId
1153      * @param relationId
1154      * @param userId
1155      * @param componentTypeEnum
1156      * @return
1157      */
1158     public Either<RequirementCapabilityRelDef, ResponseFormat> getRelationById(String componentId, String relationId, String userId, ComponentTypeEnum componentTypeEnum) {
1159
1160         Either<RequirementCapabilityRelDef, ResponseFormat> resultOp = null;
1161         try {
1162             org.openecomp.sdc.be.model.Component containerComponent = null;
1163             Either<org.openecomp.sdc.be.model.Component, ResponseFormat> validateComponentExists = null;
1164             RequirementCapabilityRelDef foundRelation = null;
1165
1166             validateUserExists(userId, "get relation by Id", false);
1167
1168             if(resultOp == null){
1169                 validateComponentExists = validateComponentExists(componentId, componentTypeEnum, null);
1170                 if (validateComponentExists.isRight()) {
1171                     resultOp = Either.right(validateComponentExists.right().value());
1172                 }
1173             }
1174             if(resultOp == null){
1175                 containerComponent = validateComponentExists.left().value();
1176                 List<RequirementCapabilityRelDef> requirementCapabilityRelations = containerComponent.getComponentInstancesRelations();
1177                 foundRelation = findRelation(relationId, requirementCapabilityRelations);
1178                 if(foundRelation == null){
1179                     ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.RELATION_NOT_FOUND, relationId, componentId);
1180                     log.debug("Relation with id {} was not found on the component", relationId, componentId);
1181                     resultOp = Either.right(responseFormat);
1182                 }
1183             }
1184             if(resultOp == null){
1185                 resultOp = setRelatedCapability(foundRelation, containerComponent);
1186             }
1187             if(resultOp.isLeft()){
1188                 resultOp = setRelatedRequirement(foundRelation, containerComponent);
1189             }
1190         } catch (Exception e) {
1191             log.error("The exception {} occured upon get relation {} of the component {} ", e, relationId, componentId);
1192             resultOp =  Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
1193         }
1194         return resultOp;
1195     }
1196
1197     private RequirementCapabilityRelDef findRelation(String relationId, List<RequirementCapabilityRelDef> requirementCapabilityRelations) {
1198         for(RequirementCapabilityRelDef relationship : requirementCapabilityRelations){
1199             if(relationship.getRelationships().stream().filter(r -> r.getRelation().getId().equals(relationId)).findFirst().isPresent()){
1200                 return relationship;
1201             }
1202         }
1203         return null;
1204     }
1205
1206     private Either<RequirementCapabilityRelDef, ResponseFormat> setRelatedRequirement(RequirementCapabilityRelDef foundRelation, Component containerComponent) {
1207         Either<RequirementCapabilityRelDef, ResponseFormat> result = null;
1208         RelationshipInfo relationshipInfo = foundRelation.resolveSingleRelationship().getRelation();
1209         String instanceId = foundRelation.getFromNode();
1210         Optional<RequirementDefinition> foundRequirement;
1211         Optional<ComponentInstance> instance = containerComponent.getComponentInstances().stream().filter(i -> i.getUniqueId().equals(instanceId)).findFirst();
1212         if(!instance.isPresent()){
1213             ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER, instanceId, "instance", containerComponent.getComponentType().getValue(), containerComponent.getName());
1214             log.debug("Component instance with id {} was not found on the component", instanceId, containerComponent.getUniqueId());
1215             result = Either.right(responseFormat);
1216         }
1217         if(result == null){
1218             for(List<RequirementDefinition> requirements : instance.get().getRequirements().values()){
1219                 foundRequirement = requirements.stream().filter(r -> isBelongingRequirement(relationshipInfo, r)).findFirst();
1220                 if(foundRequirement.isPresent()){
1221                     foundRelation.resolveSingleRelationship().setRequirement(foundRequirement.get());
1222                     result = Either.left(foundRelation);
1223                 }
1224             }
1225         }
1226         if(result == null){
1227             Either<RequirementDataDefinition, StorageOperationStatus> getfulfilledRequirementRes = toscaOperationFacade.getFulfilledRequirementByRelation(containerComponent.getUniqueId(), instanceId, foundRelation, (rel, req)->isBelongingRequirement(rel, req));
1228             if(getfulfilledRequirementRes.isRight()){
1229                 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.REQUIREMENT_OF_INSTANCE_NOT_FOUND_ON_CONTAINER, relationshipInfo.getRequirement(), instanceId, containerComponent.getUniqueId());
1230                 log.debug("Requirement {} of instance {} was not found on the container {}. ", relationshipInfo.getCapability(), instanceId, containerComponent.getUniqueId());
1231                 result = Either.right(responseFormat);
1232             } else {
1233                 foundRelation.resolveSingleRelationship().setRequirement(getfulfilledRequirementRes.left().value());
1234             }
1235         }
1236         if(result == null){
1237             result = Either.left(foundRelation);
1238         }
1239         return result;
1240     }
1241
1242     private boolean isBelongingRequirement(RelationshipInfo relationshipInfo, RequirementDataDefinition req) {
1243         return  req.getName().equals(relationshipInfo.getRequirement()) &&
1244                 req.getUniqueId().equals(relationshipInfo.getRequirementUid()) &&
1245                 req.getOwnerId().equals(relationshipInfo.getRequirementOwnerId());
1246     }
1247
1248     private Either<RequirementCapabilityRelDef, ResponseFormat> setRelatedCapability(RequirementCapabilityRelDef foundRelation, Component containerComponent) {
1249         Either<RequirementCapabilityRelDef, ResponseFormat> result = null;
1250         RelationshipInfo relationshipInfo = foundRelation.resolveSingleRelationship().getRelation();
1251         String instanceId = foundRelation.getToNode();
1252         Optional<CapabilityDefinition> foundCapability;
1253         Optional<ComponentInstance> instance = containerComponent.getComponentInstances().stream().filter(i -> i.getUniqueId().equals(instanceId)).findFirst();
1254         if(!instance.isPresent()){
1255             ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER, instanceId, "instance", containerComponent.getComponentType().getValue(), containerComponent.getName());
1256             log.debug("Component instance with id {} was not found on the component", instanceId, containerComponent.getUniqueId());
1257             result = Either.right(responseFormat);
1258         }
1259         if(result == null){
1260             for(List<CapabilityDefinition> capabilities : instance.get().getCapabilities().values()){
1261                 foundCapability = capabilities.stream().filter(c -> isBelongingCapability(relationshipInfo, c)).findFirst();
1262                 if(foundCapability.isPresent()){
1263                     foundRelation.resolveSingleRelationship().setCapability(foundCapability.get());
1264                     result = Either.left(foundRelation);
1265                 }
1266             }
1267         }
1268         if(result == null){
1269             Either<CapabilityDataDefinition, StorageOperationStatus> getfulfilledRequirementRes =
1270                     toscaOperationFacade.getFulfilledCapabilityByRelation(containerComponent.getUniqueId(), instanceId, foundRelation, (rel, cap)->isBelongingCapability(rel, cap));
1271             if(getfulfilledRequirementRes.isRight()){
1272                 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.CAPABILITY_OF_INSTANCE_NOT_FOUND_ON_CONTAINER, relationshipInfo.getCapability(), instanceId, containerComponent.getUniqueId());
1273                 log.debug("Capability {} of instance {} was not found on the container {}. ", relationshipInfo.getCapability(), instanceId, containerComponent.getUniqueId());
1274                 result = Either.right(responseFormat);
1275             } else {
1276                 foundRelation.resolveSingleRelationship().setCapability(getfulfilledRequirementRes.left().value());
1277             }
1278         }
1279         if(result == null){
1280             result = Either.left(foundRelation);
1281         }
1282         return result;
1283     }
1284
1285     private boolean isBelongingCapability(RelationshipInfo relationshipInfo, CapabilityDataDefinition cap) {
1286         return     cap.getName().equals(relationshipInfo.getCapability()) &&
1287                 cap.getUniqueId().equals(relationshipInfo.getCapabilityUid()) &&
1288                 cap.getOwnerId().equals(relationshipInfo.getCapabilityOwnerId());
1289     }
1290
1291     private Either<ComponentInstanceProperty, ResponseFormat> updateAttributeValue(ComponentInstanceProperty attribute, String resourceInstanceId) {
1292         Either<ComponentInstanceProperty, StorageOperationStatus> eitherAttribute = componentInstanceOperation.updateAttributeValueInResourceInstance(attribute, resourceInstanceId, true);
1293         Either<ComponentInstanceProperty, ResponseFormat> result;
1294         if (eitherAttribute.isLeft()) {
1295             log.debug("Attribute value {} was updated on graph.", attribute.getValueUniqueUid());
1296             ComponentInstanceProperty instanceAttribute = eitherAttribute.left().value();
1297
1298             result = Either.left(instanceAttribute);
1299
1300         } else {
1301             log.debug("Failed to update attribute value {} in resource instance {}", attribute, resourceInstanceId);
1302
1303             ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(eitherAttribute.right().value());
1304
1305             result = Either.right(componentsUtils.getResponseFormat(actionStatus, ""));
1306
1307         }
1308         return result;
1309     }
1310
1311     private Either<ComponentInstanceProperty, ResponseFormat> createAttributeValue(ComponentInstanceProperty attribute, String resourceInstanceId) {
1312
1313         Either<ComponentInstanceProperty, ResponseFormat> result;
1314
1315         Wrapper<Integer> indexCounterWrapper = new Wrapper<>();
1316         Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
1317         validateIncrementCounter(resourceInstanceId, GraphPropertiesDictionary.ATTRIBUTE_COUNTER, indexCounterWrapper, errorWrapper);
1318
1319         if (!errorWrapper.isEmpty()) {
1320             result = Either.right(errorWrapper.getInnerElement());
1321         } else {
1322             Either<ComponentInstanceProperty, StorageOperationStatus> eitherAttribute = componentInstanceOperation.addAttributeValueToResourceInstance(attribute, resourceInstanceId, indexCounterWrapper.getInnerElement(), true);
1323             if (eitherAttribute.isLeft()) {
1324                 log.debug("Attribute value was added to resource instance {}", resourceInstanceId);
1325                 ComponentInstanceProperty instanceAttribute = eitherAttribute.left().value();
1326                 result = Either.left(instanceAttribute);
1327
1328             } else {
1329                 log.debug("Failed to add attribute value {}  to resource instance {}", attribute, resourceInstanceId);
1330
1331                 ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(eitherAttribute.right().value());
1332                 result = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
1333
1334             }
1335         }
1336         return result;
1337     }
1338
1339     /**
1340      * Create Or Updates Attribute Instance
1341      *
1342      * @param componentTypeEnum
1343      * @param componentId
1344      * @param resourceInstanceId
1345      * @param attribute
1346      * @param userId
1347      * @return
1348      */
1349     public Either<ComponentInstanceProperty, ResponseFormat> createOrUpdateAttributeValue(ComponentTypeEnum componentTypeEnum, String componentId, String resourceInstanceId, ComponentInstanceProperty attribute, String userId) {
1350         Either<ComponentInstanceProperty, ResponseFormat> result = null;
1351         Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
1352
1353         validateUserExist(userId, "create Or Update Attribute Value");
1354         if (errorWrapper.isEmpty()) {
1355             validateComponentTypeEnum(componentTypeEnum, "CreateOrUpdateAttributeValue", errorWrapper);
1356         }
1357         if (errorWrapper.isEmpty()) {
1358             validateCanWorkOnComponent(componentId, componentTypeEnum, userId, errorWrapper);
1359         }
1360         if (errorWrapper.isEmpty()) {
1361             validateComponentLock(componentId, componentTypeEnum, errorWrapper);
1362         }
1363
1364         try {
1365             if (errorWrapper.isEmpty()) {
1366                 final boolean isCreate = Objects.isNull(attribute.getValueUniqueUid());
1367                 if (isCreate) {
1368                     result = createAttributeValue(attribute, resourceInstanceId);
1369                 } else {
1370                     result = updateAttributeValue(attribute, resourceInstanceId);
1371                 }
1372             } else {
1373                 result = Either.right(errorWrapper.getInnerElement());
1374             }
1375             return result;
1376         }
1377
1378         finally {
1379             if (result == null || result.isRight()) {
1380                 titanDao.rollback();
1381             } else {
1382                 titanDao.commit();
1383             }
1384             // unlock resource
1385             graphLockOperation.unlockComponent(componentId, componentTypeEnum.getNodeType());
1386         }
1387     }
1388
1389     private boolean isNetworkRoleServiceProperty(ComponentInstanceProperty property, ComponentTypeEnum componentTypeEnum) {
1390         return StringUtils.isNotEmpty(property.getValue())
1391                 && PropertyNames.NETWORK_ROLE.getPropertyName().equalsIgnoreCase(property.getName())
1392                 && ComponentTypeEnum.SERVICE == componentTypeEnum;
1393     }
1394
1395     // US833308 VLI in service - specific network_role property value logic
1396     private StorageOperationStatus concatServiceNameToVLINetworkRolePropertiesValues(ToscaOperationFacade toscaOperationFacade, ComponentTypeEnum componentTypeEnum, String componentId, String resourceInstanceId, List<ComponentInstanceProperty> properties) {
1397         for (ComponentInstanceProperty property: properties) {
1398             if (isNetworkRoleServiceProperty(property, componentTypeEnum)) {
1399                 ComponentParametersView componentParametersView = new ComponentParametersView();
1400                 componentParametersView.disableAll();
1401                 componentParametersView.setIgnoreComponentInstances(false);
1402                 Either<Component, StorageOperationStatus> getServiceResult = toscaOperationFacade.getToscaElement(componentId, componentParametersView);
1403                 if (getServiceResult.isRight()) {
1404                     return getServiceResult.right().value();
1405                 }
1406                 Component service = getServiceResult.left().value();
1407                 Optional<ComponentInstance> getInstance = service.getComponentInstances().stream().filter(p -> p.getUniqueId().equals(resourceInstanceId)).findAny();
1408                 if (!getInstance.isPresent()) {
1409                     return StorageOperationStatus.NOT_FOUND;
1410                 }
1411                 String prefix = service.getSystemName() + ".";
1412                 String value = property.getValue();
1413                 if (OriginTypeEnum.VL == getInstance.get().getOriginType() && (!value.startsWith(prefix) || value.equalsIgnoreCase(prefix))) {
1414                     property.setValue(prefix + value);
1415                 }
1416             }
1417         }
1418         return StorageOperationStatus.OK;
1419     }
1420
1421     public Either<List<ComponentInstanceProperty>, ResponseFormat> createOrUpdatePropertiesValues(ComponentTypeEnum componentTypeEnum, String componentId, String resourceInstanceId, List<ComponentInstanceProperty> properties, String userId) {
1422
1423         Either<List<ComponentInstanceProperty>, ResponseFormat> resultOp = null;
1424
1425         /*-------------------------------Validations---------------------------------*/
1426
1427         validateUserExists(userId, "create Or Update Properties Values", false);
1428
1429         if (componentTypeEnum == null) {
1430             BeEcompErrorManager.getInstance().logInvalidInputError("CreateOrUpdatePropertiesValues", "invalid component type", ErrorSeverity.INFO);
1431             resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.NOT_ALLOWED));
1432             return resultOp;
1433         }
1434         Either<Component, StorageOperationStatus> getResourceResult = toscaOperationFacade.getToscaElement(componentId, JsonParseFlagEnum.ParseAll);
1435
1436         if (getResourceResult.isRight()) {
1437             log.debug("Failed to retrieve component, component id {}", componentId);
1438             resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
1439             return resultOp;
1440         }
1441         Component containerComponent = getResourceResult.left().value();
1442
1443         if (!ComponentValidationUtils.canWorkOnComponent(containerComponent, userId)) {
1444             log.info("Restricted operation for user: {} on service {}", userId, componentId);
1445             resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
1446             return resultOp;
1447         }
1448         Either<ComponentInstance, StorageOperationStatus> resourceInstanceStatus = getResourceInstanceById(containerComponent, resourceInstanceId);
1449         if (resourceInstanceStatus.isRight()) {
1450             resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.RESOURCE_INSTANCE_NOT_FOUND_ON_SERVICE, resourceInstanceId, componentId));
1451             return resultOp;
1452         }
1453         ComponentInstance foundResourceInstance = resourceInstanceStatus.left().value();
1454         // specific property value logic US833308
1455         StorageOperationStatus fetchByIdsStatus = concatServiceNameToVLINetworkRolePropertiesValues(toscaOperationFacade, componentTypeEnum, componentId, resourceInstanceId, properties);
1456         if (StorageOperationStatus.OK != fetchByIdsStatus) {
1457             resultOp = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(fetchByIdsStatus)));
1458             return resultOp;
1459         }
1460         // lock resource
1461         StorageOperationStatus lockStatus = graphLockOperation.lockComponent(componentId, componentTypeEnum.getNodeType());
1462         if (lockStatus != StorageOperationStatus.OK) {
1463             log.debug("Failed to lock service {}", componentId);
1464             resultOp = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(lockStatus)));
1465             return resultOp;
1466         }
1467
1468         try {
1469             for (ComponentInstanceProperty property: properties) {
1470                 Either<String, ResponseFormat> updatedPropertyValue = updatePropertyObjectValue(property, false);
1471                 updatedPropertyValue.bimap(updatedValue -> updatePropertyOnContainerComponent(property,updatedValue, containerComponent, foundResourceInstance),
1472                         responseFormat -> Either.right(responseFormat));
1473             }
1474
1475             Either<Component, StorageOperationStatus> updateContainerRes = toscaOperationFacade.updateComponentInstanceMetadataOfTopologyTemplate(containerComponent);
1476             if (updateContainerRes.isRight()) {
1477                 ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(updateContainerRes.right().value());
1478                 resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
1479                 return resultOp;
1480             }
1481             resultOp = Either.left(properties);
1482             return resultOp;
1483
1484         } finally {
1485             if (resultOp == null || resultOp.isRight()) {
1486                 titanDao.rollback();
1487             } else {
1488                 titanDao.commit();
1489             }
1490             // unlock resource
1491             graphLockOperation.unlockComponent(componentId, componentTypeEnum.getNodeType());
1492         }
1493     }
1494
1495     private ResponseFormat updateCapabilityPropertyOnContainerComponent(ComponentInstanceProperty property, String newValue, Component containerComponent, ComponentInstance foundResourceInstance,
1496                                                                         String capabilityType, String capabilityName) {
1497         String componentInstanceUniqueId = foundResourceInstance.getUniqueId();
1498         StringBuffer sb = new StringBuffer(componentInstanceUniqueId);
1499         sb.append(ModelConverter.CAP_PROP_DELIM).append(property.getOwnerId()).append(ModelConverter.CAP_PROP_DELIM).append(capabilityType).append(ModelConverter.CAP_PROP_DELIM).append(capabilityName);
1500         String capKey = sb.toString();
1501
1502         Map<String, List<CapabilityDefinition>> capabilities = Optional.ofNullable(foundResourceInstance.getCapabilities())
1503                 .orElse(Collections.emptyMap());
1504         List<CapabilityDefinition> capPerType = Optional.ofNullable(capabilities.get(capabilityType)).orElse(Collections.EMPTY_LIST);
1505         Optional<CapabilityDefinition> cap = capPerType.stream().filter(c -> c.getName().equals(capabilityName)).findAny();
1506         if (cap.isPresent()) {
1507             List<ComponentInstanceProperty> capProperties = cap.get().getProperties();
1508             if (capProperties != null) {
1509                 Optional<ComponentInstanceProperty> instanceProperty = capProperties.stream().filter(p -> p.getUniqueId().equals(property.getUniqueId())).findAny();
1510                 StorageOperationStatus status;
1511                 if (instanceProperty.isPresent()) {
1512                     instanceProperty.get().setValue(newValue);
1513                     List<String> path = new ArrayList<>();
1514                     path.add(componentInstanceUniqueId);
1515                     path.add(capKey);
1516                     instanceProperty.get().setPath(path);
1517                     status = toscaOperationFacade.updateComponentInstanceCapabiltyProperty(containerComponent, componentInstanceUniqueId, capKey, instanceProperty.get());
1518                     if (status != StorageOperationStatus.OK) {
1519                         ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(status);
1520                         return componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, "");
1521
1522                     }
1523                     foundResourceInstance.setCustomizationUUID(UUID.randomUUID().toString());
1524                 }
1525             }
1526         }
1527
1528
1529         return componentsUtils.getResponseFormat(ActionStatus.OK);
1530     }
1531
1532     private ResponseFormat updatePropertyOnContainerComponent(ComponentInstanceProperty property, String newValue, Component containerComponent, ComponentInstance foundResourceInstance) {
1533         List<ComponentInstanceProperty> instanceProperties = containerComponent.getComponentInstancesProperties().get(foundResourceInstance.getUniqueId());
1534         Optional<ComponentInstanceProperty> instanceProperty = instanceProperties.stream().filter(p -> p.getUniqueId().equals(property.getUniqueId())).findAny();
1535         StorageOperationStatus status;
1536         instanceProperty.get().setValue(newValue);
1537         if (instanceProperty.isPresent()) {
1538             status = toscaOperationFacade.updateComponentInstanceProperty(containerComponent, foundResourceInstance.getUniqueId(), property);
1539         } else {
1540             status = toscaOperationFacade.addComponentInstanceProperty(containerComponent, foundResourceInstance.getUniqueId(), property);
1541         }
1542         if (status != StorageOperationStatus.OK) {
1543             ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(status);
1544             return componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, "");
1545         }
1546         List<String> path = new ArrayList<>();
1547         path.add(foundResourceInstance.getUniqueId());
1548         property.setPath(path);
1549
1550         foundResourceInstance.setCustomizationUUID(UUID.randomUUID().toString());
1551         return componentsUtils.getResponseFormat(ActionStatus.OK);
1552     }
1553
1554     private <T extends PropertyDefinition> Either<String,ResponseFormat> updatePropertyObjectValue(T property, boolean isInput) {
1555         Either<Map<String, DataTypeDefinition>, TitanOperationStatus> allDataTypesEither = dataTypeCache.getAll();
1556         if (allDataTypesEither.isRight()) {
1557             TitanOperationStatus status = allDataTypesEither.right().value();
1558             BeEcompErrorManager.getInstance().logInternalFlowError("UpdatePropertyValueOnComponentInstance", "Failed to update property value on instance. Status is " + status, ErrorSeverity.ERROR);
1559             return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(DaoStatusConverter.convertTitanStatusToStorageStatus(status))));
1560         }
1561         Map<String, DataTypeDefinition> allDataTypes = allDataTypesEither.left().value();
1562         String innerType = null;
1563         String propertyType = property.getType();
1564         ToscaPropertyType type = ToscaPropertyType.isValidType(propertyType);
1565         log.debug("The type of the property {} is {}", property.getUniqueId(), propertyType);
1566
1567         if (type == ToscaPropertyType.LIST || type == ToscaPropertyType.MAP) {
1568             SchemaDefinition def = property.getSchema();
1569             if (def == null) {
1570                 log.debug("Schema doesn't exists for property of type {}", type);
1571                 return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(StorageOperationStatus.INVALID_VALUE)));
1572             }
1573             PropertyDataDefinition propDef = def.getProperty();
1574             if (propDef == null) {
1575                 log.debug("Property in Schema Definition inside property of type {} doesn't exist", type);
1576                 return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(StorageOperationStatus.INVALID_VALUE)));
1577             }
1578             innerType = propDef.getType();
1579         }
1580         // Specific Update Logic
1581         Either<Object, Boolean> isValid = propertyOperation.validateAndUpdatePropertyValue(propertyType, property.getValue(), true, innerType, allDataTypes);
1582         String newValue = property.getValue();
1583         if (isValid.isRight()) {
1584             Boolean res = isValid.right().value();
1585             if (res == false) {
1586                 return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(DaoStatusConverter.convertTitanStatusToStorageStatus(TitanOperationStatus.ILLEGAL_ARGUMENT))));
1587             }
1588         } else {
1589             Object object = isValid.left().value();
1590             if (object != null) {
1591                 newValue = object.toString();
1592             }
1593         }
1594         if (!isInput) {
1595             ImmutablePair<String, Boolean> pair = propertyOperation.validateAndUpdateRules(propertyType, ((ComponentInstanceProperty) property).getRules(), innerType, allDataTypes, true);
1596             if (pair.getRight() != null && pair.getRight() == false) {
1597                 BeEcompErrorManager.getInstance().logBeInvalidValueError("Add property value", pair.getLeft(), property.getName(), propertyType);
1598                 return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(DaoStatusConverter.convertTitanStatusToStorageStatus(TitanOperationStatus.ILLEGAL_ARGUMENT))));
1599             }
1600         }
1601         return Either.left(newValue);
1602     }
1603
1604     private ResponseFormat updateInputOnContainerComponent(ComponentInstanceInput input, String newValue, Component containerComponent, ComponentInstance foundResourceInstance) {
1605         List<ComponentInstanceInput> instanceProperties = containerComponent.getComponentInstancesInputs().get(foundResourceInstance.getUniqueId());
1606         Optional<ComponentInstanceInput> instanceProperty = instanceProperties.stream().filter(p -> p.getUniqueId().equals(input.getUniqueId())).findAny();
1607         StorageOperationStatus status;
1608         if (instanceProperty.isPresent()) {
1609             instanceProperty.get().setValue(input.getValue());
1610             status = toscaOperationFacade.updateComponentInstanceInput(containerComponent, foundResourceInstance.getUniqueId(), input);
1611         } else {
1612             status = toscaOperationFacade.addComponentInstanceInput(containerComponent, foundResourceInstance.getUniqueId(), input);
1613         }
1614         if (status != StorageOperationStatus.OK) {
1615             ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(status);
1616             return componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, "");
1617         }
1618         foundResourceInstance.setCustomizationUUID(UUID.randomUUID().toString());
1619         return componentsUtils.getResponseFormat(ActionStatus.OK);
1620     }
1621
1622     public Either<List<ComponentInstanceInput>, ResponseFormat> createOrUpdateInstanceInputValues(ComponentTypeEnum componentTypeEnum, String componentId, String resourceInstanceId, List<ComponentInstanceInput> inputs, String userId) {
1623
1624         Either<List<ComponentInstanceInput>, ResponseFormat> resultOp = null;
1625
1626         validateUserExists(userId, "create Or Update Property Value", false);
1627
1628         if (componentTypeEnum == null) {
1629             BeEcompErrorManager.getInstance().logInvalidInputError("CreateOrUpdatePropertyValue", "invalid component type", ErrorSeverity.INFO);
1630             resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.NOT_ALLOWED));
1631             return resultOp;
1632         }
1633         Either<Component, StorageOperationStatus> getResourceResult = toscaOperationFacade.getToscaElement(componentId, JsonParseFlagEnum.ParseAll);
1634
1635         if (getResourceResult.isRight()) {
1636             log.debug("Failed to retrieve component, component id {}", componentId);
1637             resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
1638             return resultOp;
1639         }
1640         Component containerComponent = getResourceResult.left().value();
1641
1642         if (!ComponentValidationUtils.canWorkOnComponent(containerComponent, userId)) {
1643             log.info("Restricted operation for user: {} on service {}", userId, componentId);
1644             resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
1645             return resultOp;
1646         }
1647         Either<ComponentInstance, StorageOperationStatus> resourceInstanceStatus = getResourceInstanceById(containerComponent, resourceInstanceId);
1648         if (resourceInstanceStatus.isRight()) {
1649             resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.RESOURCE_INSTANCE_NOT_FOUND_ON_SERVICE, resourceInstanceId, componentId));
1650             return resultOp;
1651         }
1652
1653         ComponentInstance foundResourceInstance = resourceInstanceStatus.left().value();
1654
1655         // lock resource
1656         StorageOperationStatus lockStatus = graphLockOperation.lockComponent(componentId, componentTypeEnum.getNodeType());
1657         if (lockStatus != StorageOperationStatus.OK) {
1658             log.debug("Failed to lock service {}", componentId);
1659             resultOp = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(lockStatus)));
1660             return resultOp;
1661         }
1662         try {
1663             for (ComponentInstanceInput input: inputs) {
1664                 Either<String, ResponseFormat> updatedInputValue = updatePropertyObjectValue(input, true);
1665                 updatedInputValue.bimap(updatedValue -> updateInputOnContainerComponent(input,updatedValue, containerComponent, foundResourceInstance),
1666                         responseFormat -> Either.right(responseFormat));
1667
1668             }
1669             Either<Component, StorageOperationStatus> updateContainerRes = toscaOperationFacade.updateComponentInstanceMetadataOfTopologyTemplate(containerComponent);
1670
1671             if (updateContainerRes.isRight()) {
1672                 ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(updateContainerRes.right().value());
1673                 resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
1674                 return resultOp;
1675             }
1676             resultOp = Either.left(inputs);
1677             return resultOp;
1678
1679         } finally {
1680             if (resultOp == null || resultOp.isRight()) {
1681                 titanDao.rollback();
1682             } else {
1683                 titanDao.commit();
1684             }
1685             // unlock resource
1686             graphLockOperation.unlockComponent(componentId, componentTypeEnum.getNodeType());
1687         }
1688
1689     }
1690
1691     public Either<ComponentInstanceProperty, ResponseFormat> createOrUpdateGroupInstancePropertyValue(ComponentTypeEnum componentTypeEnum, String componentId, String resourceInstanceId, String groupInstanceId, ComponentInstanceProperty property,
1692                                                                                                       String userId) {
1693
1694         Either<ComponentInstanceProperty, ResponseFormat> resultOp = null;
1695
1696         validateUserExists(userId, "create Or Update Property Value", false);
1697
1698         if (componentTypeEnum == null) {
1699             BeEcompErrorManager.getInstance().logInvalidInputError("CreateOrUpdatePropertyValue", "invalid component type", ErrorSeverity.INFO);
1700             resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.NOT_ALLOWED));
1701             return resultOp;
1702         }
1703
1704         if (!ComponentValidationUtils.canWorkOnComponent(componentId, toscaOperationFacade, userId)) {
1705             log.info("Restricted operation for user: {} on service: {}", userId, componentId);
1706             resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
1707             return resultOp;
1708         }
1709         // lock resource
1710         StorageOperationStatus lockStatus = graphLockOperation.lockComponent(componentId, componentTypeEnum.getNodeType());
1711         if (lockStatus != StorageOperationStatus.OK) {
1712             log.debug("Failed to lock service {}", componentId);
1713             resultOp = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(lockStatus)));
1714             return resultOp;
1715         }
1716         try {
1717             String propertyValueUid = property.getValueUniqueUid();
1718
1719             if (propertyValueUid == null) {
1720
1721                 Either<Integer, StorageOperationStatus> counterRes = groupInstanceOperation.increaseAndGetGroupInstancePropertyCounter(groupInstanceId);
1722
1723                 if (counterRes.isRight()) {
1724                     log.debug("increaseAndGetResourcePropertyCounter failed resource instance: {} property: {}", resourceInstanceId, property);
1725                     StorageOperationStatus status = counterRes.right().value();
1726                     ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(status);
1727                     resultOp = Either.right(componentsUtils.getResponseFormat(actionStatus));
1728                 }
1729                 Integer index = counterRes.left().value();
1730                 Either<ComponentInstanceProperty, StorageOperationStatus> result = groupInstanceOperation.addPropertyValueToGroupInstance(property, resourceInstanceId, index, true);
1731
1732                 if (result.isLeft()) {
1733                     log.trace("Property value was added to resource instance {}", resourceInstanceId);
1734                     ComponentInstanceProperty instanceProperty = result.left().value();
1735
1736                     resultOp = Either.left(instanceProperty);
1737
1738                 } else {
1739                     log.debug("Failed to add property value: {} to resource instance {}", property, resourceInstanceId);
1740
1741                     ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(result.right().value());
1742
1743                     resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
1744                 }
1745
1746             } else {
1747                 Either<ComponentInstanceProperty, StorageOperationStatus> result = groupInstanceOperation.updatePropertyValueInGroupInstance(property, resourceInstanceId, true);
1748
1749                 if (result.isLeft()) {
1750                     log.debug("Property value {} was updated on graph.", property.getValueUniqueUid());
1751                     ComponentInstanceProperty instanceProperty = result.left().value();
1752
1753                     resultOp = Either.left(instanceProperty);
1754
1755                 } else {
1756                     log.debug("Failed to update property value: {}, in resource instance {}", property, resourceInstanceId);
1757
1758                     ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(result.right().value());
1759
1760                     resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
1761                 }
1762             }
1763             if (resultOp.isLeft()) {
1764                 StorageOperationStatus updateCustomizationUUID = componentInstanceOperation.updateCustomizationUUID(resourceInstanceId);
1765                 if (updateCustomizationUUID != StorageOperationStatus.OK) {
1766                     ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(updateCustomizationUUID);
1767
1768                     resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
1769
1770                 }
1771             }
1772             return resultOp;
1773
1774         } finally {
1775             if (resultOp == null || resultOp.isRight()) {
1776                 titanDao.rollback();
1777             } else {
1778                 titanDao.commit();
1779             }
1780             // unlock resource
1781             graphLockOperation.unlockComponent(componentId, componentTypeEnum.getNodeType());
1782         }
1783
1784     }
1785
1786     public Either<ComponentInstanceInput, ResponseFormat> createOrUpdateInputValue(ComponentTypeEnum componentTypeEnum, String componentId, String resourceInstanceId, ComponentInstanceInput inputProperty, String userId) {
1787
1788         Either<ComponentInstanceInput, ResponseFormat> resultOp = null;
1789
1790         validateUserExists(userId, "create Or Update Input Value", false);
1791
1792         if (componentTypeEnum == null) {
1793             BeEcompErrorManager.getInstance().logInvalidInputError("createOrUpdateInputValue", "invalid component type", ErrorSeverity.INFO);
1794             resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.NOT_ALLOWED));
1795             return resultOp;
1796         }
1797
1798         if (!ComponentValidationUtils.canWorkOnComponent(componentId, toscaOperationFacade, userId)) {
1799             log.info("Restricted operation for user: {} on service: {}", userId, componentId);
1800             resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
1801             return resultOp;
1802         }
1803         // lock resource
1804         StorageOperationStatus lockStatus = graphLockOperation.lockComponent(componentId, componentTypeEnum.getNodeType());
1805         if (lockStatus != StorageOperationStatus.OK) {
1806             log.debug("Failed to lock service {}", componentId);
1807             resultOp = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(lockStatus)));
1808             return resultOp;
1809         }
1810         try {
1811             String propertyValueUid = inputProperty.getValueUniqueUid();
1812             if (propertyValueUid == null) {
1813
1814                 Either<Integer, StorageOperationStatus> counterRes = componentInstanceOperation.increaseAndGetResourceInstanceSpecificCounter(resourceInstanceId, GraphPropertiesDictionary.INPUT_COUNTER, true);
1815
1816                 if (counterRes.isRight()) {
1817                     log.debug("increaseAndGetResourceInputCounter failed resource instance {} inputProperty {}", resourceInstanceId, inputProperty);
1818                     StorageOperationStatus status = counterRes.right().value();
1819                     ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(status);
1820                     resultOp = Either.right(componentsUtils.getResponseFormat(actionStatus));
1821                 }
1822                 Integer index = counterRes.left().value();
1823                 Either<ComponentInstanceInput, StorageOperationStatus> result = componentInstanceOperation.addInputValueToResourceInstance(inputProperty, resourceInstanceId, index, true);
1824
1825                 if (result.isLeft()) {
1826                     log.debug("Property value was added to resource instance {}", resourceInstanceId);
1827                     ComponentInstanceInput instanceProperty = result.left().value();
1828
1829                     resultOp = Either.left(instanceProperty);
1830                     return resultOp;
1831
1832                 } else {
1833                     log.debug("Failed to add input value {} to resource instance {}", inputProperty, resourceInstanceId);
1834
1835                     ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(result.right().value());
1836
1837                     resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
1838
1839                     return resultOp;
1840                 }
1841
1842             } else {
1843                 Either<ComponentInstanceInput, StorageOperationStatus> result = componentInstanceOperation.updateInputValueInResourceInstance(inputProperty, resourceInstanceId, true);
1844
1845                 if (result.isLeft()) {
1846                     log.debug("Input value {} was updated on graph.", inputProperty.getValueUniqueUid());
1847                     ComponentInstanceInput instanceProperty = result.left().value();
1848
1849                     resultOp = Either.left(instanceProperty);
1850                     return resultOp;
1851
1852                 } else {
1853                     log.debug("Failed to update property value {} in resource instance {}", inputProperty, resourceInstanceId);
1854
1855                     ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(result.right().value());
1856
1857                     resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
1858
1859                     return resultOp;
1860                 }
1861             }
1862
1863         } finally {
1864             if (resultOp == null || resultOp.isRight()) {
1865                 titanDao.rollback();
1866             } else {
1867                 titanDao.commit();
1868             }
1869             // unlock resource
1870             graphLockOperation.unlockComponent(componentId, componentTypeEnum.getNodeType());
1871         }
1872
1873     }
1874
1875     public Either<ComponentInstanceProperty, ResponseFormat> deletePropertyValue(ComponentTypeEnum componentTypeEnum, String serviceId, String resourceInstanceId, String propertyValueId, String userId) {
1876
1877         validateUserExists(userId, "delete Property Value", false);
1878
1879         Either<ComponentInstanceProperty, ResponseFormat> resultOp = null;
1880
1881         if (componentTypeEnum == null) {
1882             BeEcompErrorManager.getInstance().logInvalidInputError("CreateOrUpdatePropertyValue", "invalid component type", ErrorSeverity.INFO);
1883             resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.NOT_ALLOWED));
1884             return resultOp;
1885         }
1886
1887         if (!ComponentValidationUtils.canWorkOnComponent(serviceId, toscaOperationFacade, userId)) {
1888             log.info("Restricted operation for user {} on service {}", userId, serviceId);
1889             resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
1890             return resultOp;
1891         }
1892         // lock resource
1893         StorageOperationStatus lockStatus = graphLockOperation.lockComponent(serviceId, componentTypeEnum.getNodeType());
1894         if (lockStatus != StorageOperationStatus.OK) {
1895             log.debug("Failed to lock service {}", serviceId);
1896             resultOp = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(lockStatus)));
1897             return resultOp;
1898         }
1899         try {
1900             Either<ComponentInstanceProperty, StorageOperationStatus> result = propertyOperation.removePropertyValueFromResourceInstance(propertyValueId, resourceInstanceId, true);
1901
1902             if (result.isLeft()) {
1903                 log.debug("Property value {} was removed from graph.", propertyValueId);
1904                 ComponentInstanceProperty instanceProperty = result.left().value();
1905
1906                 resultOp = Either.left(instanceProperty);
1907                 return resultOp;
1908
1909             } else {
1910                 log.debug("Failed to remove property value {} in resource instance {}", propertyValueId, resourceInstanceId);
1911
1912                 ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(result.right().value());
1913
1914                 resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
1915
1916                 return resultOp;
1917             }
1918
1919         } finally {
1920             if (resultOp == null || resultOp.isRight()) {
1921                 titanDao.rollback();
1922             } else {
1923                 titanDao.commit();
1924             }
1925             // unlock resource
1926             graphLockOperation.unlockComponent(serviceId, componentTypeEnum.getNodeType());
1927         }
1928
1929     }
1930
1931     private Either<Component, ResponseFormat> getAndValidateOriginComponentOfComponentInstance(ComponentTypeEnum containerComponentType, ComponentInstance componentInstance) {
1932
1933         Either<Component, ResponseFormat> eitherResponse = null;
1934         ComponentTypeEnum componentType = getComponentTypeByParentComponentType(containerComponentType);
1935         Component component;
1936         ResponseFormat errorResponse;
1937         Either<Component, StorageOperationStatus> getComponentRes = toscaOperationFacade.getToscaFullElement(componentInstance.getComponentUid());
1938         if (getComponentRes.isRight()) {
1939             log.debug("Failed to get the component with id {} for component instance {} creation. ", componentInstance.getComponentUid(), componentInstance.getName());
1940             ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(getComponentRes.right().value(), componentType);
1941             errorResponse = componentsUtils.getResponseFormat(actionStatus, Constants.EMPTY_STRING);
1942             eitherResponse = Either.right(errorResponse);
1943         }
1944         if (eitherResponse == null) {
1945             component = getComponentRes.left().value();
1946             LifecycleStateEnum resourceCurrState = component.getLifecycleState();
1947             if (resourceCurrState == LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT) {
1948                 ActionStatus actionStatus = ActionStatus.ILLEGAL_COMPONENT_STATE;
1949                 errorResponse = componentsUtils.getResponseFormat(actionStatus, component.getComponentType().toString(), component.getName(), resourceCurrState.toString());
1950                 eitherResponse = Either.right(errorResponse);
1951             }
1952         }
1953         if (eitherResponse == null) {
1954             eitherResponse = Either.left(getComponentRes.left().value());
1955         }
1956         return eitherResponse;
1957     }
1958
1959     public Either<Set<String>, ResponseFormat> forwardingPathOnVersionChange(String containerComponentParam,
1960                                                                              String containerComponentId,
1961                                                                              String componentInstanceId,
1962                                                                              ComponentInstance newComponentInstance) {
1963         Either<Set<String>, ResponseFormat> resultOp;
1964         Either<ComponentTypeEnum, ResponseFormat> validateComponentType = validateComponentType(containerComponentParam);
1965         if (validateComponentType.isRight()) {
1966             return Either.right(validateComponentType.right().value());
1967         }
1968         final ComponentTypeEnum containerComponentType = validateComponentType.left().value();
1969         ComponentParametersView componentParametersView = getComponentParametersViewForForwardingPath();
1970
1971         //Fetch Component
1972         Either<org.openecomp.sdc.be.model.Component, ResponseFormat> validateComponentExists =
1973                 validateComponentExists(containerComponentId, containerComponentType, componentParametersView);
1974         if (validateComponentExists.isRight()) {
1975             return Either.right(validateComponentExists.right().value());
1976         }
1977         Component containerComponent = validateComponentExists.left().value();
1978
1979         //Fetch current component instance
1980         Either<ComponentInstance, StorageOperationStatus> eitherResourceInstance =
1981                 getResourceInstanceById(containerComponent, componentInstanceId);
1982         if (eitherResourceInstance.isRight()) {
1983             resultOp = Either.right(componentsUtils.getResponseFormat(
1984                     ActionStatus.RESOURCE_INSTANCE_NOT_FOUND_ON_SERVICE, componentInstanceId, containerComponentId));
1985             return resultOp;
1986         }
1987         ComponentInstance currentResourceInstance = eitherResourceInstance.left().value();
1988
1989         //Check whether new componentInstance exists
1990         String resourceId = newComponentInstance.getComponentUid();
1991         Either<Boolean, StorageOperationStatus> componentExistsRes = toscaOperationFacade.validateComponentExists(resourceId);
1992         if (componentExistsRes.isRight()) {
1993             log.debug("Failed to find resource {} ", resourceId);
1994             resultOp = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse
1995                     (componentExistsRes.right().value()), resourceId));
1996             return resultOp;
1997         } else if (!componentExistsRes.left().value()) {
1998             log.debug("The resource {} not found ", resourceId);
1999             resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.RESOURCE_NOT_FOUND, resourceId));
2000             return resultOp;
2001         }
2002
2003         //Fetch component using new component instance uid
2004         Either<Component, ResponseFormat> eitherResourceName = getOriginComponentFromComponentInstance(newComponentInstance);
2005         if (eitherResourceName.isRight()) {
2006             resultOp = Either.right(eitherResourceName.right().value());
2007             return resultOp;
2008         }
2009         Component updatedContainerComponent=eitherResourceName.left().value();
2010         Set<String> toDeleteForwardingPaths = getForwardingPaths(containerComponent,
2011                 currentResourceInstance, updatedContainerComponent);
2012         resultOp=Either.left(toDeleteForwardingPaths);
2013
2014         return resultOp;
2015     }
2016
2017     private Set<String> getForwardingPaths(Component containerComponent, ComponentInstance currentResourceInstance,
2018                                            Component updatedContainerComponent) {
2019         DataForMergeHolder dataForMergeHolder=new DataForMergeHolder();
2020         dataForMergeHolder.setOrigComponentInstId(currentResourceInstance.getUniqueId());
2021
2022         Service service = (Service) containerComponent;
2023         ForwardingPathUtils forwardingPathUtils = new ForwardingPathUtils();
2024
2025         return forwardingPathUtils.
2026                 getForwardingPathsToBeDeletedOnVersionChange(service,dataForMergeHolder,updatedContainerComponent);
2027     }
2028
2029     private ComponentParametersView getComponentParametersViewForForwardingPath() {
2030         ComponentParametersView componentParametersView = new ComponentParametersView();
2031         componentParametersView.setIgnoreCapabiltyProperties(false);
2032         componentParametersView.setIgnoreForwardingPath(false);
2033         return componentParametersView;
2034     }
2035
2036     public Either<ComponentInstance, ResponseFormat> changeComponentInstanceVersion(String containerComponentParam, String containerComponentId, String componentInstanceId, String userId, ComponentInstance newComponentInstance) {
2037
2038         User user = validateUserExists(userId, "change Component Instance Version", false);
2039
2040         Either<ComponentInstance, ResponseFormat> resultOp = null;
2041
2042         Either<ComponentTypeEnum, ResponseFormat> validateComponentType = validateComponentType(containerComponentParam);
2043         if (validateComponentType.isRight()) {
2044             return Either.right(validateComponentType.right().value());
2045         }
2046
2047         final ComponentTypeEnum containerComponentType = validateComponentType.left().value();
2048         ComponentParametersView componentParametersView = new ComponentParametersView();
2049         componentParametersView.setIgnoreCapabiltyProperties(false);
2050         Either<org.openecomp.sdc.be.model.Component, ResponseFormat> validateComponentExists = validateComponentExists(containerComponentId, containerComponentType, componentParametersView);
2051         if (validateComponentExists.isRight()) {
2052             return Either.right(validateComponentExists.right().value());
2053         }
2054         org.openecomp.sdc.be.model.Component containerComponent = validateComponentExists.left().value();
2055
2056         Either<Boolean, ResponseFormat> validateCanWorkOnComponent = validateCanWorkOnComponent(containerComponent, userId);
2057         if (validateCanWorkOnComponent.isRight()) {
2058             return Either.right(validateCanWorkOnComponent.right().value());
2059         }
2060
2061         Either<ComponentInstance, StorageOperationStatus> resourceInstanceStatus = getResourceInstanceById(containerComponent, componentInstanceId);
2062         if (resourceInstanceStatus.isRight()) {
2063             resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.RESOURCE_INSTANCE_NOT_FOUND_ON_SERVICE, componentInstanceId, containerComponentId));
2064             return resultOp;
2065         }
2066
2067         ComponentInstance currentResourceInstance = resourceInstanceStatus.left().value();
2068
2069         return changeInstanceVersion(containerComponent, currentResourceInstance, newComponentInstance, user, containerComponentType );
2070     }
2071
2072     public Either<ComponentInstance, ResponseFormat> changeInstanceVersion(org.openecomp.sdc.be.model.Component containerComponent, ComponentInstance currentResourceInstance,
2073                                                                            ComponentInstance newComponentInstance, User user, final ComponentTypeEnum containerComponentType    ) {
2074         Either<ComponentInstance, ResponseFormat> resultOp = null;
2075         Either<ComponentInstance, StorageOperationStatus> resourceInstanceStatus;
2076
2077         Either<Boolean, ResponseFormat> lockComponent = lockComponent(containerComponent, "changeComponentInstanceVersion");
2078         String containerComponentId = containerComponent.getUniqueId();
2079         String componentInstanceId = currentResourceInstance.getUniqueId();
2080         if (lockComponent.isRight()) {
2081             return Either.right(lockComponent.right().value());
2082         }
2083
2084         try {
2085
2086
2087             if (currentResourceInstance.getComponentUid().equals(newComponentInstance.getComponentUid())) {
2088                 resultOp = Either.left(currentResourceInstance);
2089                 return resultOp;
2090
2091             }
2092             String resourceId = newComponentInstance.getComponentUid();
2093
2094
2095
2096             Either<Boolean, StorageOperationStatus> componentExistsRes = toscaOperationFacade.validateComponentExists(resourceId);
2097             if (componentExistsRes.isRight()) {
2098                 log.debug("Failed to validate existing of the component {}. Status is {} ", resourceId);
2099                 resultOp = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(componentExistsRes.right().value()), resourceId));
2100                 return resultOp;
2101             } else if (!componentExistsRes.left().value()) {
2102                 log.debug("The resource {} not found ", resourceId);
2103                 resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.RESOURCE_NOT_FOUND, resourceId));
2104                 return resultOp;
2105             }
2106
2107             Either<Component, ResponseFormat> eitherOriginComponent = getInstanceOriginNode(currentResourceInstance);
2108
2109             if (eitherOriginComponent.isRight()) {
2110                 resultOp = Either.right(eitherOriginComponent.right().value());
2111                 return resultOp;
2112             }
2113             DataForMergeHolder dataHolder = compInstMergeDataBL.saveAllDataBeforeDeleting(containerComponent, currentResourceInstance, eitherOriginComponent.left().value());
2114             resultOp = deleteComponentInstance(containerComponent, componentInstanceId, containerComponentType);
2115             if (resultOp.isRight()) {
2116                 log.debug("failed to delete resource instance {}", resourceId);
2117                 return resultOp;
2118             }
2119             ComponentInstance resResourceInfo = resultOp.left().value();
2120             Component origComponent = null;
2121             OriginTypeEnum originType = currentResourceInstance.getOriginType();
2122             if (originType == OriginTypeEnum.ServiceProxy) {
2123                 Either<Component, StorageOperationStatus> serviceProxyOrigin = toscaOperationFacade.getLatestByName("serviceProxy");
2124                 if (serviceProxyOrigin.isRight()) {
2125                     log.debug("Failed to fetch normative service proxy resource by tosca name, error {}", serviceProxyOrigin.right().value());
2126                     return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(serviceProxyOrigin.right().value())));
2127                 }
2128                 origComponent = serviceProxyOrigin.left().value();
2129
2130                 StorageOperationStatus fillProxyRes = fillProxyInstanceData(newComponentInstance, origComponent);
2131
2132                 if (fillProxyRes != StorageOperationStatus.OK) {
2133                     log.debug("Failed to fill service proxy resource data with data from service, error {}", fillProxyRes);
2134                     return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(fillProxyRes)));
2135
2136                 }
2137                 newComponentInstance.setOriginType(originType);
2138             }else{
2139
2140
2141                 Either<Component, ResponseFormat> eitherResourceName = getOriginComponentFromComponentInstance(newComponentInstance);
2142
2143                 if (eitherResourceName.isRight()) {
2144                     resultOp = Either.right(eitherResourceName.right().value());
2145                     return resultOp;
2146                 }
2147
2148                 origComponent = eitherResourceName.left().value();
2149
2150                 newComponentInstance.setName(resResourceInfo.getName());
2151             }
2152
2153             newComponentInstance.setInvariantName(resResourceInfo.getInvariantName());
2154             newComponentInstance.setPosX(resResourceInfo.getPosX());
2155             newComponentInstance.setPosY(resResourceInfo.getPosY());
2156             newComponentInstance.setDescription(resResourceInfo.getDescription());
2157
2158             resultOp = createComponentInstanceOnGraph(containerComponent, origComponent, newComponentInstance, user);
2159
2160             if (resultOp.isRight()) {
2161                 log.debug("failed to create resource instance {}", resourceId);
2162                 return resultOp;
2163             }
2164
2165             ComponentInstance updatedComponentInstance = resultOp.left().value();
2166             if (resultOp.isRight()) {
2167                 log.debug("failed to create resource instance {}", resourceId);
2168                 return resultOp;
2169             }
2170
2171             Either<Component, ResponseFormat> mergeStatusEither = compInstMergeDataBL.mergeComponentUserOrigData(user, dataHolder, containerComponent, containerComponentId, newComponentInstance.getUniqueId());
2172             if (mergeStatusEither.isRight()) {
2173                 return Either.right(mergeStatusEither.right().value());
2174             }
2175
2176             ActionStatus postChangeVersionResult = onChangeInstanceOperationOrchestrator.doPostChangeVersionOperations(containerComponent, currentResourceInstance, newComponentInstance);
2177             if (postChangeVersionResult != ActionStatus.OK) {
2178                 return Either.right(componentsUtils.getResponseFormat(postChangeVersionResult));
2179             }
2180
2181             ComponentParametersView filter = new ComponentParametersView(true);
2182             filter.setIgnoreComponentInstances(false);
2183             Either<Component, StorageOperationStatus> updatedComponentRes = toscaOperationFacade.getToscaElement(containerComponentId, filter);
2184             if (updatedComponentRes.isRight()) {
2185                 StorageOperationStatus storageOperationStatus = updatedComponentRes.right().value();
2186                 ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(storageOperationStatus, containerComponent.getComponentType());
2187                 ResponseFormat responseFormat = componentsUtils.getResponseFormat(actionStatus, Constants.EMPTY_STRING);
2188                 log.debug("Component with id {} was not found", containerComponentId);
2189                 return Either.right(responseFormat);
2190             }
2191             resourceInstanceStatus = getResourceInstanceById(updatedComponentRes.left().value(), updatedComponentInstance.getUniqueId());
2192             if (resourceInstanceStatus.isRight()) {
2193                 resultOp = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(resourceInstanceStatus.right().value()), updatedComponentInstance.getUniqueId()));
2194                 return resultOp;
2195             }
2196             resultOp = Either.left(resourceInstanceStatus.left().value());
2197             return resultOp;
2198
2199         } finally {
2200             unlockComponent(resultOp, containerComponent);
2201         }
2202     }
2203
2204     // US831698
2205     public Either<List<ComponentInstanceProperty>, ResponseFormat> getComponentInstancePropertiesById(String containerComponentTypeParam, String containerComponentId, String componentInstanceUniqueId, String userId) {
2206         final String ECOMP_ERROR_CONTEXT = "Get Component Instance Properties By Id";
2207         Component containerComponent = null;
2208
2209         Either<List<ComponentInstanceProperty>, ResponseFormat> resultOp = null;
2210         try {
2211             validateUserExists(userId, ECOMP_ERROR_CONTEXT, false);
2212
2213             Either<ComponentTypeEnum, ResponseFormat> validateComponentType = validateComponentType(containerComponentTypeParam);
2214             if (validateComponentType.isRight()) {
2215                 resultOp = Either.right(validateComponentType.right().value());
2216                 return resultOp;
2217             }
2218
2219             Either<Component, StorageOperationStatus> validateContainerComponentExists = toscaOperationFacade.getToscaElement(containerComponentId);
2220             if (validateContainerComponentExists.isRight()) {
2221                 resultOp = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(validateContainerComponentExists.right().value())));
2222                 return resultOp;
2223             }
2224             containerComponent = validateContainerComponentExists.left().value();
2225
2226             Either<ComponentInstance, StorageOperationStatus> resourceInstanceStatus = getResourceInstanceById(containerComponent, componentInstanceUniqueId);
2227             if (resourceInstanceStatus.isRight()) {
2228                 resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.RESOURCE_INSTANCE_NOT_FOUND_ON_SERVICE, componentInstanceUniqueId, containerComponentId));
2229                 return resultOp;
2230             }
2231
2232             List<ComponentInstanceProperty> instanceProperties = containerComponent.getComponentInstancesProperties().get(componentInstanceUniqueId);
2233             if (CollectionUtils.isEmpty(instanceProperties)) {
2234                 instanceProperties = new ArrayList<>();
2235             }
2236             resultOp = Either.left(instanceProperties);
2237             return resultOp;
2238         } finally {
2239             unlockComponent(resultOp, containerComponent);
2240         }
2241     }
2242
2243     protected void validateIncrementCounter(String resourceInstanceId, GraphPropertiesDictionary counterType, Wrapper<Integer> instaceCounterWrapper, Wrapper<ResponseFormat> errorWrapper) {
2244         Either<Integer, StorageOperationStatus> counterRes = componentInstanceOperation.increaseAndGetResourceInstanceSpecificCounter(resourceInstanceId, counterType, true);
2245
2246         if (counterRes.isRight()) {
2247             log.debug("increase And Get {} failed resource instance {}", counterType, resourceInstanceId);
2248             StorageOperationStatus status = counterRes.right().value();
2249             ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(status);
2250             errorWrapper.setInnerElement(componentsUtils.getResponseFormat(actionStatus));
2251         } else {
2252             instaceCounterWrapper.setInnerElement(counterRes.left().value());
2253         }
2254
2255     }
2256
2257     /**
2258      * updates componentInstance modificationTime
2259      *
2260      * @param componentInstance
2261      * @param componentInstanceType
2262      * @param modificationTime
2263      * @param inTransaction
2264      * @return
2265      */
2266     public Either<ComponentInstanceData, ResponseFormat> updateComponentInstanceModificationTimeAndCustomizationUuid(ComponentInstance componentInstance, NodeTypeEnum componentInstanceType, Long modificationTime, boolean inTransaction) {
2267         Either<ComponentInstanceData, ResponseFormat> result;
2268         Either<ComponentInstanceData, StorageOperationStatus> updateComponentInstanceRes = componentInstanceOperation.updateComponentInstanceModificationTimeAndCustomizationUuidOnGraph(componentInstance, componentInstanceType, modificationTime,
2269                 inTransaction);
2270         if (updateComponentInstanceRes.isRight()) {
2271             log.debug("Failed to update component instance {} with new last update date and mofifier. Status is {}. ", componentInstance.getName(), updateComponentInstanceRes.right().value());
2272             result = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(updateComponentInstanceRes.right().value())));
2273         } else {
2274             result = Either.left(updateComponentInstanceRes.left().value());
2275         }
2276         return result;
2277     }
2278
2279     public Either<ComponentInstance, ResponseFormat> deleteServiceProxy() {
2280         // TODO Add implementation
2281         Either<ComponentInstance, ResponseFormat> result = Either.left(new ComponentInstance());
2282         return result;
2283     }
2284
2285     public Either<ComponentInstance, ResponseFormat> createServiceProxy() {
2286         // TODO Add implementation
2287         Either<ComponentInstance, ResponseFormat> result = Either.left(new ComponentInstance());
2288         return result;
2289     }
2290
2291     public Either<ComponentInstance, ResponseFormat> changeServiceProxyVersion() {
2292         // TODO Add implementation
2293         Either<ComponentInstance, ResponseFormat> result = Either.left(new ComponentInstance());
2294         return result;
2295     }
2296
2297     private Boolean validateInstanceNameUniquenessUponUpdate(Component containerComponent, ComponentInstance oldComponentInstance, String newInstanceName) {
2298         return ComponentValidations.validateNameIsUniqueInComponent(oldComponentInstance.getName(), newInstanceName, containerComponent);
2299     }
2300
2301     private Either<ComponentInstance, StorageOperationStatus> getResourceInstanceById(Component containerComponent, String instanceId) {
2302
2303         Either<ComponentInstance, StorageOperationStatus> result = null;
2304         List<ComponentInstance> instances = containerComponent.getComponentInstances();
2305         Optional<ComponentInstance> foundInstance = null;
2306         if (CollectionUtils.isEmpty(instances)) {
2307             result = Either.right(StorageOperationStatus.NOT_FOUND);
2308         }
2309         if (result == null) {
2310             foundInstance = instances.stream().filter(i -> i.getUniqueId().equals(instanceId)).findFirst();
2311             if (!foundInstance.isPresent()) {
2312                 result = Either.right(StorageOperationStatus.NOT_FOUND);
2313             }
2314         }
2315         if (result == null) {
2316             result = Either.left(foundInstance.get());
2317         }
2318         return result;
2319     }
2320
2321     private ComponentInstance buildComponentInstance(ComponentInstance resourceInstanceForUpdate, ComponentInstance origInstanceForUpdate) {
2322
2323         Long creationDate = origInstanceForUpdate.getCreationTime();
2324
2325         Long modificationTime = System.currentTimeMillis();
2326         resourceInstanceForUpdate.setCreationTime(creationDate);
2327         resourceInstanceForUpdate.setModificationTime(modificationTime);
2328
2329         resourceInstanceForUpdate.setCustomizationUUID(origInstanceForUpdate.getCustomizationUUID());
2330
2331         if (StringUtils.isEmpty(resourceInstanceForUpdate.getName()) && StringUtils.isNotEmpty(origInstanceForUpdate.getName())) {
2332             resourceInstanceForUpdate.setName(origInstanceForUpdate.getName());
2333         }
2334
2335         resourceInstanceForUpdate.setNormalizedName(ValidationUtils.normalizeComponentInstanceName(resourceInstanceForUpdate.getName()));
2336
2337         if (StringUtils.isEmpty(resourceInstanceForUpdate.getIcon()))
2338             resourceInstanceForUpdate.setIcon(origInstanceForUpdate.getIcon());
2339
2340         if (StringUtils.isEmpty(resourceInstanceForUpdate.getComponentVersion()))
2341             resourceInstanceForUpdate.setComponentVersion(origInstanceForUpdate.getComponentVersion());
2342
2343         if (StringUtils.isEmpty(resourceInstanceForUpdate.getComponentName()))
2344             resourceInstanceForUpdate.setComponentName(origInstanceForUpdate.getComponentName());
2345
2346         if (StringUtils.isEmpty(resourceInstanceForUpdate.getToscaComponentName()))
2347             resourceInstanceForUpdate.setToscaComponentName(origInstanceForUpdate.getToscaComponentName());
2348
2349         if (resourceInstanceForUpdate.getOriginType() == null) {
2350             resourceInstanceForUpdate.setOriginType(origInstanceForUpdate.getOriginType());
2351         }
2352         if(resourceInstanceForUpdate.getOriginType()  == OriginTypeEnum.ServiceProxy)
2353             resourceInstanceForUpdate.setIsProxy(true);
2354         if (resourceInstanceForUpdate.getSourceModelInvariant() == null) {
2355             resourceInstanceForUpdate.setSourceModelInvariant(origInstanceForUpdate.getSourceModelInvariant());
2356         }
2357         if (resourceInstanceForUpdate.getSourceModelName() == null) {
2358             resourceInstanceForUpdate.setSourceModelName(origInstanceForUpdate.getSourceModelName());
2359         }
2360         if (resourceInstanceForUpdate.getSourceModelUuid() == null) {
2361             resourceInstanceForUpdate.setSourceModelUuid(origInstanceForUpdate.getSourceModelUuid());
2362         }
2363         if (resourceInstanceForUpdate.getSourceModelUid() == null) {
2364             resourceInstanceForUpdate.setSourceModelUid(origInstanceForUpdate.getSourceModelUid());
2365         }
2366         return resourceInstanceForUpdate;
2367     }
2368     /**
2369      * Returns list of ComponentInstanceProperty belonging to component instance capability specified by name, type and ownerId
2370      * @param containerComponentType
2371      * @param containerComponentId
2372      * @param componentInstanceUniqueId
2373      * @param capabilityType
2374      * @param capabilityName
2375      * @param userId
2376      * @param ownerId
2377      * @return
2378      */
2379     public Either<List<ComponentInstanceProperty>, ResponseFormat> getComponentInstanceCapabilityPropertiesById(String containerComponentType, String containerComponentId, String componentInstanceUniqueId, String capabilityType, String capabilityName, String ownerId, String userId) {
2380
2381         Component containerComponent = null;
2382
2383         Either<List<ComponentInstanceProperty>, ResponseFormat> resultOp = null;
2384         try {
2385             validateUserExists(userId, "Get Component Instance Properties By Id", false);
2386             if(resultOp == null){
2387                 Either<ComponentTypeEnum, ResponseFormat> validateComponentType = validateComponentType(containerComponentType);
2388                 if (validateComponentType.isRight()) {
2389                     resultOp = Either.right(validateComponentType.right().value());
2390                 }
2391             }
2392             if(resultOp == null){
2393                 Either<Component, StorageOperationStatus> validateContainerComponentExists = toscaOperationFacade.getToscaFullElement(containerComponentId);
2394                 if (validateContainerComponentExists.isRight()) {
2395                     resultOp = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(validateContainerComponentExists.right().value())));
2396                 } else {
2397                     containerComponent = validateContainerComponentExists.left().value();
2398                 }
2399             }
2400             if(resultOp == null){
2401                 Either<ComponentInstance, StorageOperationStatus> resourceInstanceStatus = getResourceInstanceById(containerComponent, componentInstanceUniqueId);
2402                 if (resourceInstanceStatus.isRight()) {
2403                     resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.RESOURCE_INSTANCE_NOT_FOUND_ON_SERVICE, componentInstanceUniqueId, containerComponentId));
2404                 } else {
2405                     resultOp = findCapabilityOfInstance(containerComponentId, componentInstanceUniqueId, capabilityType, capabilityName, ownerId, resourceInstanceStatus.left().value().getCapabilities());
2406                 }
2407             }
2408             return resultOp;
2409         } finally {
2410             unlockComponent(resultOp, containerComponent);
2411         }
2412     }
2413
2414     private Either<List<ComponentInstanceProperty>, ResponseFormat> findCapabilityOfInstance( String componentId, String instanceId, String capabilityType, String capabilityName, String ownerId, Map<String, List<CapabilityDefinition>> instanceCapabilities) {
2415         Either<List<ComponentInstanceProperty>, ResponseFormat> result = null;
2416         CapabilityDefinition foundCapability;
2417         if (MapUtils.isNotEmpty(instanceCapabilities)) {
2418             List<CapabilityDefinition> capabilitiesPerType = instanceCapabilities.get(capabilityType);
2419             if (capabilitiesPerType != null) {
2420                 Optional<CapabilityDefinition> capabilityOpt = capabilitiesPerType.stream().filter(c -> c.getName().equals(capabilityName) && c.getOwnerId().equals(ownerId)).findFirst();
2421                 if (capabilityOpt.isPresent()) {
2422                     foundCapability = capabilityOpt.get();
2423                     result = Either.left(foundCapability.getProperties() == null ? new ArrayList<>() : foundCapability.getProperties());
2424                 }
2425             }
2426         }
2427         if (result == null) {
2428             result = fetchComponentInstanceCapabilityProperties(componentId, instanceId, capabilityType, capabilityName, ownerId);
2429         }
2430         return result;
2431     }
2432
2433     private Either<List<ComponentInstanceProperty>, ResponseFormat> fetchComponentInstanceCapabilityProperties(String componentId, String instanceId, String capabilityType, String capabilityName, String ownerId) {
2434         Either<List<ComponentInstanceProperty>, ResponseFormat> resultOp = null;
2435         try {
2436             Either<List<ComponentInstanceProperty>, StorageOperationStatus> getComponentInstanceCapabilityProperties = toscaOperationFacade.getComponentInstanceCapabilityProperties(componentId, instanceId, capabilityName, capabilityType, ownerId);
2437             if(getComponentInstanceCapabilityProperties.isRight()){
2438                 resultOp = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(getComponentInstanceCapabilityProperties.right().value()), capabilityType, instanceId, componentId));
2439             } else {
2440                 resultOp =  Either.left(getComponentInstanceCapabilityProperties.left().value());
2441             }
2442         } catch(Exception e){
2443             log.error("The exception {} occurred upon the component {} instance {} capability {} properties retrieving. ", componentId, instanceId, capabilityName, e);
2444             resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
2445         }
2446         return resultOp;
2447     }
2448
2449     private ResponseFormat updateCapabilityPropertyOnContainerComponent(ComponentInstanceProperty property, String newValue, Component containerComponent, ComponentInstance foundResourceInstance,
2450                                                                         String capabilityType, String capabilityName, String ownerId) {
2451         String componentInstanceUniqueId = foundResourceInstance.getUniqueId();
2452         StringBuilder sb = new StringBuilder(componentInstanceUniqueId);
2453         sb.append(ModelConverter.CAP_PROP_DELIM).append(property.getOwnerId()).append(ModelConverter.CAP_PROP_DELIM).append(capabilityType).append(ModelConverter.CAP_PROP_DELIM).append(capabilityName);
2454         String capKey = sb.toString();
2455
2456         Map<String, List<CapabilityDefinition>> capabilities = Optional.ofNullable(foundResourceInstance.getCapabilities())
2457                 .orElse(Collections.emptyMap());
2458         List<CapabilityDefinition> capPerType = Optional.ofNullable(capabilities.get(capabilityType)).orElse(Collections.emptyList());
2459         Optional<CapabilityDefinition> cap = capPerType.stream().filter(c -> c.getName().equals(capabilityName) && c.getOwnerId().equals(ownerId)).findAny();
2460         if (cap.isPresent()) {
2461             List<ComponentInstanceProperty> capProperties = cap.get().getProperties();
2462             if (capProperties != null) {
2463                 Optional<ComponentInstanceProperty> instanceProperty = capProperties.stream().filter(p -> p.getUniqueId().equals(property.getUniqueId())).findAny();
2464                 StorageOperationStatus status;
2465                 if (instanceProperty.isPresent()) {
2466                     instanceProperty.get().setValue(newValue);
2467                     List<String> path = new ArrayList<>();
2468                     path.add(componentInstanceUniqueId);
2469                     path.add(capKey);
2470                     instanceProperty.get().setPath(path);
2471                     status = toscaOperationFacade.updateComponentInstanceCapabiltyProperty(containerComponent, componentInstanceUniqueId, capKey, instanceProperty.get());
2472                     if (status != StorageOperationStatus.OK) {
2473                         ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(status);
2474                         return componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, "");
2475
2476                     }
2477                     foundResourceInstance.setCustomizationUUID(UUID.randomUUID().toString());
2478                 }
2479             }
2480         }
2481         return componentsUtils.getResponseFormat(ActionStatus.OK);
2482     }
2483
2484     public Either<List<ComponentInstanceProperty>, ResponseFormat> updateInstanceCapabilityProperties(ComponentTypeEnum componentTypeEnum, String containerComponentId, String componentInstanceUniqueId, String capabilityType, String capabilityName, String ownerId,
2485                                                                                                       List<ComponentInstanceProperty> properties, String userId) {
2486         Either<List<ComponentInstanceProperty>, ResponseFormat> resultOp = null;
2487
2488         validateUserExists(userId, "update instance capability property", false);
2489
2490         if (componentTypeEnum == null) {
2491             BeEcompErrorManager.getInstance().logInvalidInputError("updateInstanceCapabilityProperty", "invalid component type", ErrorSeverity.INFO);
2492             return Either.right(componentsUtils.getResponseFormat(ActionStatus.NOT_ALLOWED));
2493         }
2494         Either<Component, StorageOperationStatus> getResourceResult = toscaOperationFacade.getToscaFullElement(containerComponentId);
2495
2496         if (getResourceResult.isRight()) {
2497             log.debug("Failed to retrieve component, component id {}", containerComponentId);
2498             return Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
2499         }
2500         Component containerComponent = getResourceResult.left().value();
2501
2502         if (!ComponentValidationUtils.canWorkOnComponent(containerComponent, userId)) {
2503             log.info("Restricted operation for user: {sourcePropList} on component {}", userId, containerComponentId);
2504             return Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
2505         }
2506         Either<ComponentInstance, StorageOperationStatus> resourceInstanceStatus = getResourceInstanceById(containerComponent, componentInstanceUniqueId);
2507         if (resourceInstanceStatus.isRight()) {
2508             return Either.right(componentsUtils.getResponseFormat(ActionStatus.RESOURCE_INSTANCE_NOT_FOUND_ON_SERVICE, componentInstanceUniqueId, containerComponentId));
2509         }
2510         ComponentInstance foundResourceInstance = resourceInstanceStatus.left().value();
2511         // lock resource
2512         StorageOperationStatus lockStatus = graphLockOperation.lockComponent(containerComponentId, componentTypeEnum.getNodeType());
2513         if (lockStatus != StorageOperationStatus.OK) {
2514             log.debug("Failed to lock component {}", containerComponentId);
2515             return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(lockStatus)));
2516         }
2517
2518         Either<Map<String, DataTypeDefinition>, TitanOperationStatus> allDataTypes = dataTypeCache.getAll();
2519         if (allDataTypes.isRight()) {
2520             TitanOperationStatus status = allDataTypes.right().value();
2521             BeEcompErrorManager.getInstance().logInternalFlowError("UpdatePropertyValueOnComponentInstance", "Failed to update property value on instance. Status is " + status, ErrorSeverity.ERROR);
2522             return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(DaoStatusConverter.convertTitanStatusToStorageStatus(status))));
2523         }
2524
2525         try {
2526             for (ComponentInstanceProperty property : properties) {
2527                 Either<String, ResponseFormat> newPropertyValueEither = updatePropertyObjectValue(property, false);
2528                 newPropertyValueEither.bimap(updatedValue ->
2529                                 updateCapabilityPropertyOnContainerComponent(property,updatedValue, containerComponent, foundResourceInstance, capabilityType, capabilityName, ownerId),
2530                         responseFormat -> Either.right(responseFormat));
2531             }
2532             Either<Component, StorageOperationStatus> updateContainerRes = toscaOperationFacade.updateComponentInstanceMetadataOfTopologyTemplate(containerComponent);
2533
2534             if (updateContainerRes.isRight()) {
2535                 ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(updateContainerRes.right().value());
2536                 resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
2537                 return resultOp;
2538             }
2539             resultOp = Either.left(properties);
2540             return resultOp;
2541
2542         } finally {
2543             if (resultOp == null || resultOp.isRight()) {
2544                 titanDao.rollback();
2545             } else {
2546                 titanDao.commit();
2547             }
2548             // unlock resource
2549             graphLockOperation.unlockComponent(containerComponentId, componentTypeEnum.getNodeType());
2550         }
2551     }
2552
2553     public Either<List<ComponentInstanceProperty>, ResponseFormat> updateInstanceCapabilityProperties(ComponentTypeEnum componentTypeEnum, String containerComponentId, String componentInstanceUniqueId, String capabilityType, String capabilityName,
2554                                                                                                       List<ComponentInstanceProperty> properties, String userId) {
2555         Either<List<ComponentInstanceProperty>, ResponseFormat> resultOp = null;
2556
2557         validateUserExists(userId, "update instance capability property", false);
2558
2559         if (componentTypeEnum == null) {
2560             BeEcompErrorManager.getInstance().logInvalidInputError("updateInstanceCapabilityProperty", "invalid component type", ErrorSeverity.INFO);
2561             return Either.right(componentsUtils.getResponseFormat(ActionStatus.NOT_ALLOWED));
2562         }
2563         Either<Component, StorageOperationStatus> getResourceResult = toscaOperationFacade.getToscaFullElement(containerComponentId);
2564
2565         if (getResourceResult.isRight()) {
2566             log.debug("Failed to retrieve component, component id {}", containerComponentId);
2567             return Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
2568         }
2569         Component containerComponent = getResourceResult.left().value();
2570
2571         if (!ComponentValidationUtils.canWorkOnComponent(containerComponent, userId)) {
2572             log.info("Restricted operation for user: {} on component {}", userId, containerComponentId);
2573             return Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
2574         }
2575         Either<ComponentInstance, StorageOperationStatus> resourceInstanceStatus = getResourceInstanceById(containerComponent, componentInstanceUniqueId);
2576         if (resourceInstanceStatus.isRight()) {
2577             return Either.right(componentsUtils.getResponseFormat(ActionStatus.RESOURCE_INSTANCE_NOT_FOUND_ON_SERVICE, componentInstanceUniqueId, containerComponentId));
2578         }
2579         ComponentInstance foundResourceInstance = resourceInstanceStatus.left().value();
2580         // lock resource
2581         StorageOperationStatus lockStatus = graphLockOperation.lockComponent(containerComponentId, componentTypeEnum.getNodeType());
2582         if (lockStatus != StorageOperationStatus.OK) {
2583             log.debug("Failed to lock component {}", containerComponentId);
2584             return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(lockStatus)));
2585         }
2586
2587         try {
2588             for (ComponentInstanceProperty property : properties) {
2589                 Either<String, ResponseFormat> newPropertyValueEither = updatePropertyObjectValue(property, false);
2590                 newPropertyValueEither.bimap(updatedValue ->
2591                                 updateCapabilityPropertyOnContainerComponent(property,updatedValue, containerComponent, foundResourceInstance, capabilityType, capabilityName),
2592                         responseFormat -> Either.right(responseFormat));
2593             }
2594             Either<Component, StorageOperationStatus> updateContainerRes = toscaOperationFacade.updateComponentInstanceMetadataOfTopologyTemplate(containerComponent);
2595
2596             if (updateContainerRes.isRight()) {
2597                 ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(updateContainerRes.right().value());
2598                 resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
2599                 return resultOp;
2600             }
2601             resultOp = Either.left(properties);
2602             return resultOp;
2603
2604         } finally {
2605             if (resultOp == null || resultOp.isRight()) {
2606                 titanDao.rollback();
2607             } else {
2608                 titanDao.commit();
2609             }
2610             // unlock resource
2611             graphLockOperation.unlockComponent(containerComponentId, componentTypeEnum.getNodeType());
2612         }
2613     }
2614
2615     public Either<Map<String, ComponentInstance>, ResponseFormat> copyComponentInstance(ComponentInstance inputComponentInstance,
2616                                                                                         String containerComponentId,
2617                                                                                         String componentInstanceId,
2618                                                                                         String userId) {
2619
2620         Map<String, ComponentInstance> resultMap = new HashMap<>();
2621         Either<Component, StorageOperationStatus> getOrigComponent = toscaOperationFacade.getToscaElement(containerComponentId);
2622         if (getOrigComponent.isRight()) {
2623             log.error("Failed to get the original component information");
2624             return Either.right(componentsUtils.getResponseFormat(
2625                     ActionStatus.USER_DEFINED, "Failed to copy the component instance to the canvas"));
2626         }
2627
2628         Component origComponent = getOrigComponent.left().value();
2629
2630         Either<Boolean, ResponseFormat> lockComponent = lockComponent(origComponent, "copyComponentInstance");
2631         if (lockComponent.isRight()) {
2632             log.error("destComponentInstance's data is {}", origComponent.toString());
2633             return Either.right(lockComponent.right().value());
2634         }
2635
2636
2637         Either<ComponentInstance, ResponseFormat> actionResponse = null;
2638         try {
2639             actionResponse = createComponentInstance(
2640                     "services", containerComponentId, userId, inputComponentInstance, true, false);
2641
2642
2643         }
2644         finally {
2645
2646             // on failure of the create instance unlock the resource and rollback the transaction.
2647             if (null== actionResponse || actionResponse.isRight()) {
2648                 titanDao.rollback();
2649                 log.error("Failed to copy the component instance to the canvas");
2650
2651                 unlockComponent(actionResponse, origComponent);
2652
2653                 return Either.right(componentsUtils.getResponseFormat(
2654                         ActionStatus.USER_DEFINED, "Failed to copy the component instance to the canvas"));
2655             }
2656
2657         }
2658
2659         Either<String, ResponseFormat> resultOp = null;
2660
2661         try {
2662             ComponentInstance destComponentInstance = actionResponse.left().value();
2663             log.debug("destComponentInstance's data is {}", destComponentInstance.toString());
2664
2665
2666             resultOp = deepCopyComponentInstance(
2667                     origComponent, containerComponentId, componentInstanceId, destComponentInstance, userId);
2668
2669             resultMap.put("componentInstance", destComponentInstance);
2670         }
2671         finally{
2672             // unlock resource
2673             unlockComponent(resultOp, origComponent);
2674
2675             if (resultOp == null || resultOp.isRight()) {
2676                 titanDao.rollback();
2677                 log.error("Failed to deep copy component instance");
2678                 return Either.right(componentsUtils.getResponseFormat(
2679                         ActionStatus.USER_DEFINED, "Failed to deep copy the component instance to the canvas"));
2680             } else {
2681                 titanDao.commit();
2682                 log.debug("Success trasaction commit");
2683             }
2684         }
2685
2686         return Either.left(resultMap);
2687     }
2688
2689     private Either<String, ResponseFormat> deepCopyComponentInstance(
2690             Component sourceComponent, String containerComponentId, String sourceComponentInstanceId,
2691             ComponentInstance destComponentInstance, String userId) {
2692
2693         Either<Component, StorageOperationStatus> getDestComponent = toscaOperationFacade.getToscaElement(containerComponentId);
2694         if (getDestComponent.isRight()) {
2695             log.error("Failed to get the dest component information");
2696             return Either.right(componentsUtils.getResponseFormat(
2697                     ActionStatus.USER_DEFINED, "Failed to copy the component instance to the canvas"));
2698         }
2699
2700         Component destComponent = getDestComponent.left().value();
2701
2702         Either<String, ResponseFormat> copyComponentInstanceWithPropertiesAndInputs = copyComponentInstanceWithPropertiesAndInputs(
2703                 sourceComponent, destComponent, sourceComponentInstanceId, destComponentInstance, userId);
2704         if (copyComponentInstanceWithPropertiesAndInputs.isRight()) {
2705             log.error("Failed to copy component instance with properties and inputs as part of deep copy");
2706             return Either.right(componentsUtils.getResponseFormat(
2707                     ActionStatus.USER_DEFINED, "Failed to copy the component instance with properties and inputs as part of deep copy"));
2708         }
2709
2710         Either<String, ResponseFormat> copyComponentInstanceWithAttributes = copyComponentInstanceWithAttributes(
2711                 sourceComponent, destComponent, sourceComponentInstanceId, destComponentInstance, userId);
2712         if (copyComponentInstanceWithAttributes.isRight()) {
2713             log.error("Failed to copy component instance with attributes as part of deep copy");
2714             return Either.right(componentsUtils.getResponseFormat(
2715                     ActionStatus.USER_DEFINED, "Failed to copy the component instance with attributes as part of deep copy"));
2716         }
2717         return Either.left("Copy component Instance OK");
2718     }
2719
2720     private Either<String, ResponseFormat> copyComponentInstanceWithPropertiesAndInputs(
2721             Component sourceComponent, Component destComponent, String sourceComponentInstanceId,
2722             ComponentInstance destComponentInstance, String userId) {
2723         log.debug("start to copy ComponentInstance with properties and inputs");
2724
2725         List<ComponentInstanceProperty> sourcePropList = null;
2726         if (sourceComponent.getComponentInstancesProperties() != null
2727                 && sourceComponent.getComponentInstancesProperties().get(sourceComponentInstanceId) != null) {
2728             sourcePropList = sourceComponent.getComponentInstancesProperties().get(sourceComponentInstanceId);
2729             log.debug("sourcePropList");
2730         }
2731
2732         List<ComponentInstanceProperty> destPropList = null;
2733         String destComponentInstanceId = destComponentInstance.getUniqueId();
2734         log.debug("destComponentInstanceId: {}", destComponentInstance.getUniqueId());
2735         if (destComponent.getComponentInstancesProperties() != null
2736                 && destComponent.getComponentInstancesProperties().get(destComponentInstanceId) != null) {
2737             destPropList = destComponent.getComponentInstancesProperties().get(destComponentInstanceId);
2738             log.debug("destPropList {}");
2739         }
2740
2741         List<ComponentInstancePropInput> componentInstancePropInputList = new ArrayList<>();
2742
2743         if (null != destPropList && null != sourcePropList) {
2744             log.debug("start to set property and attribute");
2745             for (ComponentInstanceProperty destProp : destPropList) {
2746                 String destPropertyName = destProp.getName();
2747                 for (ComponentInstanceProperty sourceProp : sourcePropList) {
2748                     if (!destPropertyName.equals(sourceProp.getName())) {
2749                         continue;
2750                     }
2751                     log.debug("now set property");
2752                     if (sourceProp.getGetInputValues() == null && !StringUtils.isEmpty(sourceProp.getValue())
2753                             && (destProp.getValue() == null || !destProp.getValue().equals(sourceProp.getValue()))) {
2754                         log.debug("Now starting to copy the property {} in value {}", destPropertyName, sourceProp.getValue());
2755
2756                         destProp.setValue(sourceProp.getValue());
2757                         Either<String, ResponseFormat> updatePropertyValueEither = updateComponentInstanceProperty(
2758                                 destComponent.getUniqueId(), destComponentInstanceId, destProp);
2759                         if (updatePropertyValueEither.isRight()) {
2760                             log.error("Failed to copy the property {}", destPropertyName);
2761                             return Either.right(componentsUtils.getResponseFormat(
2762                                     ActionStatus.INVALID_CONTENT_PARAM, "Failed to paste component instance to the canvas, property copy"));
2763                         }
2764                         break;
2765                     }
2766
2767                     log.debug("Now start to update inputs");
2768
2769                     if (sourceProp.getGetInputValues() != null) {
2770                         if (sourceProp.getGetInputValues().size() < 1) {
2771                             log.debug("property is return from input, set by man");
2772                             break;
2773                         }
2774                         log.debug("Now starting to copy the {} property", destPropertyName);
2775
2776                         Either<String, ResponseFormat> getSourceInputDefaultValue = getInputListDefaultValue(
2777                                 sourceComponent, sourceProp.getGetInputValues().get(0).getInputId());
2778                         if (getSourceInputDefaultValue.isRight()) {
2779                             return Either.right(getSourceInputDefaultValue.right().value());
2780                         }
2781                         componentInstancePropInputList.add(new ComponentInstancePropInput(destProp));
2782                     }
2783                 }
2784             }
2785         }
2786         return Either.left("Copy component Instance OK");
2787     }
2788
2789     private Either<String, ResponseFormat> copyComponentInstanceWithAttributes(Component sourceComponent,
2790                                                                                Component destComponent,
2791                                                                                String sourceComponentInstanceId,
2792                                                                                ComponentInstance destComponentInstance,
2793                                                                                String userId) {
2794         String destComponentInstanceId = destComponentInstance.getUniqueId();
2795
2796         log.info("start to copy component instance with attributes");
2797
2798         List<ComponentInstanceProperty> sourceAttributeList = null;
2799         if (sourceComponent.getComponentInstancesAttributes() != null
2800                 && sourceComponent.getComponentInstancesAttributes().get(sourceComponentInstanceId) != null) {
2801             sourceAttributeList = sourceComponent.getComponentInstancesAttributes().get(sourceComponentInstanceId);
2802             log.info("sourceAttributes {}");
2803         }
2804
2805         List<ComponentInstanceProperty> destAttributeList = null;
2806         if (destComponent.getComponentInstancesAttributes() != null
2807                 && destComponent.getComponentInstancesAttributes().get(destComponentInstanceId) != null) {
2808             destAttributeList = destComponent.getComponentInstancesAttributes().get(destComponentInstanceId);
2809             log.info("destAttributeList {}");
2810         }
2811         if (null != sourceAttributeList && null != destAttributeList) {
2812             log.info("set attribute");
2813
2814             for (ComponentInstanceProperty sourceAttribute : sourceAttributeList) {
2815                 String sourceAttributeName = sourceAttribute.getName();
2816                 for (ComponentInstanceProperty destAttribute : destAttributeList) {
2817                     if (sourceAttributeName.equals(destAttribute.getName())) {
2818                         if (sourceAttribute.getValue() != null && !sourceAttribute.getValue().isEmpty()) {
2819                             log.debug("Start to copy the attribute exists {}", sourceAttributeName);
2820
2821                             sourceAttribute.setUniqueId(
2822                                     UniqueIdBuilder.buildResourceInstanceUniuqeId(
2823                                     "attribute" , destComponentInstanceId.split("\\.")[1] , sourceAttributeName));
2824
2825                             Either<ComponentInstanceProperty, ResponseFormat> updateAttributeValueEither =
2826                                     createOrUpdateAttributeValueForCopyPaste(ComponentTypeEnum.SERVICE,
2827                                             destComponent.getUniqueId(), destComponentInstanceId, sourceAttribute,
2828                                             userId);
2829                             if (updateAttributeValueEither.isRight()) {
2830                                 log.error("Failed to copy the attribute");
2831                                 return Either.right(componentsUtils
2832                                         .getResponseFormat(ActionStatus.INVALID_CONTENT_PARAM,
2833                                                 "Failed to paste component instance to the canvas, attribute copy"));
2834                             }
2835                             break;
2836                         }
2837                     }
2838                 }
2839             }
2840         }
2841
2842         return Either.left("Copy component Instance OK");
2843     }
2844
2845     private Either<ComponentInstanceProperty, ResponseFormat> createOrUpdateAttributeValueForCopyPaste(ComponentTypeEnum componentTypeEnum,
2846                                                                                                        String componentId,
2847                                                                                                        String resourceInstanceId,
2848                                                                                                        ComponentInstanceProperty attribute,
2849                                                                                                        String userId) {
2850
2851         Either<ComponentInstanceProperty, ResponseFormat> resultOp = null;
2852
2853         validateUserExists(userId, "Create or Update attribute value", false);
2854
2855         if (componentTypeEnum == null) {
2856             BeEcompErrorManager.getInstance().logInvalidInputError(
2857                     "createOrUpdateAttributeValue", "invalid component type", ErrorSeverity.INFO);
2858             resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.NOT_ALLOWED));
2859             return resultOp;
2860         }
2861
2862         Either<Component, StorageOperationStatus> getResourceResult = toscaOperationFacade.getToscaElement(componentId, JsonParseFlagEnum.ParseAll);
2863
2864         if (getResourceResult.isRight()) {
2865             log.info("Failed to retrieve component id {}", componentId);
2866             resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
2867             return resultOp;
2868         }
2869
2870         Component containerComponent = getResourceResult.left().value();
2871
2872         Either<ComponentInstance, StorageOperationStatus> resourceInstanceStatus = getResourceInstanceById(containerComponent, resourceInstanceId);
2873
2874         if (resourceInstanceStatus.isRight()) {
2875             resultOp = Either.right(componentsUtils.getResponseFormat(
2876                     ActionStatus.RESOURCE_INSTANCE_NOT_FOUND_ON_SERVICE, resourceInstanceId, componentId));
2877             return resultOp;
2878         }
2879
2880         ComponentInstance foundResourceInstance = resourceInstanceStatus.left().value();
2881
2882
2883         String propertyType = attribute.getType();
2884         ToscaPropertyType type = ToscaPropertyType.isValidType(propertyType);
2885         log.info("The type of attribute id{},is {} ", attribute.getUniqueId(), propertyType);
2886
2887         if (type == ToscaPropertyType.LIST || type == ToscaPropertyType.MAP) {
2888             SchemaDefinition def = attribute.getSchema();
2889             if (def == null) {
2890                 log.info("Schema doesn't exists for attribute of type {}", type);
2891                 return Either.right(componentsUtils.getResponseFormat(
2892                         componentsUtils.convertFromStorageResponse(StorageOperationStatus.INVALID_VALUE)));
2893             }
2894             PropertyDataDefinition propDef = def.getProperty();
2895             if (propDef == null) {
2896                 log.info("Attribute in Schema Definition inside attribute of type {} doesn't exist", type);
2897                 return Either.right(componentsUtils.getResponseFormat(
2898                         componentsUtils.convertFromStorageResponse(StorageOperationStatus.INVALID_VALUE)));
2899             }
2900         }
2901
2902         List<ComponentInstanceProperty> instanceAttributes = containerComponent.
2903                 getComponentInstancesAttributes().get(resourceInstanceId);
2904         Optional<ComponentInstanceProperty> instanceAttribute =
2905                 instanceAttributes.stream().filter(p -> p.getUniqueId().equals(attribute.getUniqueId())).findAny();
2906         StorageOperationStatus status;
2907
2908         if (instanceAttribute.isPresent()) {
2909             log.info("updateComponentInstanceAttribute");
2910             status = toscaOperationFacade.updateComponentInstanceAttribute(containerComponent, foundResourceInstance.getUniqueId(), attribute);
2911         } else {
2912             log.info("addComponentInstanceAttribute");
2913             status = toscaOperationFacade.addComponentInstanceAttribute(containerComponent, foundResourceInstance.getUniqueId(), attribute);
2914         }
2915         if (status != StorageOperationStatus.OK) {
2916             ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(status);
2917             resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
2918             return resultOp;
2919         }
2920         List<String> path = new ArrayList<>();
2921         path.add(foundResourceInstance.getUniqueId());
2922         attribute.setPath(path);
2923
2924         foundResourceInstance.setCustomizationUUID(UUID.randomUUID().toString());
2925         Either<Component, StorageOperationStatus> updateContainerRes = toscaOperationFacade.
2926                 updateComponentInstanceMetadataOfTopologyTemplate(containerComponent);
2927
2928         if (updateContainerRes.isRight()) {
2929             ActionStatus actionStatus = componentsUtils.
2930                     convertFromStorageResponseForResourceInstanceProperty(updateContainerRes.right().value());
2931             resultOp = Either.right(componentsUtils.
2932                     getResponseFormatForResourceInstanceProperty(actionStatus, ""));
2933             return resultOp;
2934         }
2935         resultOp = Either.left(attribute);
2936         return resultOp;
2937
2938
2939
2940     }
2941
2942     private Either<String, ResponseFormat> updateComponentInstanceProperty(String containerComponentId,
2943                                                                            String componentInstanceId,
2944                                                                            ComponentInstanceProperty property) {
2945         Either<String, ResponseFormat> resultOp;
2946         Either<Component, StorageOperationStatus> getComponent = toscaOperationFacade.getToscaElement(containerComponentId);
2947
2948         if (getComponent.isRight()) {
2949             log.error("Failed to get the component information");
2950             return Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(
2951                     ActionStatus.INVALID_CONTENT_PARAM, "Failed to get the component information"));
2952         }
2953
2954         Component containerComponent = getComponent.left().value();
2955
2956         StorageOperationStatus status = toscaOperationFacade.updateComponentInstanceProperty(
2957                 containerComponent, componentInstanceId, property);
2958         if (status != StorageOperationStatus.OK) {
2959             ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(status);
2960             resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
2961             return resultOp;
2962         }
2963
2964         Either<Component, StorageOperationStatus> updateContainerRes = toscaOperationFacade.
2965                 updateComponentInstanceMetadataOfTopologyTemplate(containerComponent);
2966
2967         if (updateContainerRes.isRight()) {
2968             ActionStatus actionStatus = componentsUtils.
2969                     convertFromStorageResponseForResourceInstanceProperty(updateContainerRes.right().value());
2970             resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
2971             return resultOp;
2972         }
2973
2974         return Either.left("Update OK");
2975     }
2976
2977     private Either<String, ResponseFormat> getInputListDefaultValue(Component component, String inputId) {
2978         List<InputDefinition> inputList = component.getInputs();
2979         for (InputDefinition input : inputList) {
2980             if (input.getUniqueId().equals(inputId)) {
2981                 if (input.getDefaultValue() == null) {
2982                     log.debug("The input's default value is null");
2983                     return Either.left(null);
2984                 }
2985                 return Either.left(input.getDefaultValue());
2986             }
2987         }
2988         log.error("The input's default value with id {} is not found", inputId);
2989         return Either.right(componentsUtils.getResponseFormat(
2990                 ActionStatus.USER_DEFINED, "Failed to paste component instance to the canvas"));
2991     }
2992 }