Catalog alignment
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / operations / impl / ComponentInstanceOperation.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.model.operations.impl;
22
23 import org.janusgraph.core.JanusGraph;
24 import org.janusgraph.core.JanusGraphVertex;
25 import fj.data.Either;
26 import java.util.HashMap;
27 import java.util.Map;
28 import java.util.UUID;
29 import java.util.function.Supplier;
30 import org.apache.commons.lang3.tuple.ImmutablePair;
31 import org.apache.tinkerpop.gremlin.structure.Vertex;
32 import org.apache.tinkerpop.gremlin.structure.VertexProperty;
33 import org.openecomp.sdc.be.config.BeEcompErrorManager;
34 import org.openecomp.sdc.be.config.BeEcompErrorManager.ErrorSeverity;
35 import org.openecomp.sdc.be.dao.graph.datatype.GraphEdge;
36 import org.openecomp.sdc.be.dao.graph.datatype.GraphNode;
37 import org.openecomp.sdc.be.dao.graph.datatype.GraphRelation;
38 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
39 import org.openecomp.sdc.be.dao.neo4j.GraphEdgeLabels;
40 import org.openecomp.sdc.be.dao.neo4j.GraphEdgePropertiesDictionary;
41 import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary;
42 import org.openecomp.sdc.be.dao.janusgraph.HealingJanusGraphGenericDao;
43 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
44 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
45 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
46 import org.openecomp.sdc.be.model.ComponentInstance;
47 import org.openecomp.sdc.be.model.ComponentInstanceInput;
48 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
49 import org.openecomp.sdc.be.model.DataTypeDefinition;
50 import org.openecomp.sdc.be.model.IComponentInstanceConnectedElement;
51 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
52 import org.openecomp.sdc.be.model.operations.api.IInputsOperation;
53 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
54 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
55 import org.openecomp.sdc.be.resources.data.AttributeData;
56 import org.openecomp.sdc.be.resources.data.AttributeValueData;
57 import org.openecomp.sdc.be.resources.data.ComponentInstanceData;
58 import org.openecomp.sdc.be.resources.data.InputValueData;
59 import org.openecomp.sdc.be.resources.data.InputsData;
60 import org.openecomp.sdc.common.datastructure.Wrapper;
61 import org.openecomp.sdc.common.log.wrappers.Logger;
62 import org.springframework.beans.factory.annotation.Autowired;
63
64 @org.springframework.stereotype.Component("component-instance-operation")
65 public class ComponentInstanceOperation extends AbstractOperation {
66
67     public ComponentInstanceOperation() {
68         super();
69     }
70
71     private static final Logger log = Logger.getLogger(ComponentInstanceOperation.class.getName());
72
73     @Autowired
74     PropertyOperation propertyOperation;
75
76     @Autowired
77     private IInputsOperation inputOperation;
78
79     @Autowired
80     private ApplicationDataTypeCache dataTypeCache;
81
82     /**
83      * FOR TEST ONLY
84      *
85      * @param janusGraphGenericDao
86      */
87     public void setJanusGraphGenericDao(HealingJanusGraphGenericDao janusGraphGenericDao) {
88         this.janusGraphGenericDao = janusGraphGenericDao;
89     }
90
91     public Either<Integer, StorageOperationStatus> increaseAndGetResourceInstanceSpecificCounter(String resourceInstanceId, GraphPropertiesDictionary counterType, boolean inTransaction) {
92
93         Either<Integer, StorageOperationStatus> result = null;
94         try {
95
96             Either<JanusGraph, JanusGraphOperationStatus> graphResult = janusGraphGenericDao.getGraph();
97             if (graphResult.isRight()) {
98                 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(graphResult.right().value()));
99                 return result;
100             }
101             Either<JanusGraphVertex, JanusGraphOperationStatus> vertexService = janusGraphGenericDao
102                 .getVertexByProperty(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.ResourceInstance), resourceInstanceId);
103             if (vertexService.isRight()) {
104                 log.debug("failed to fetch vertex of resource instance for id = {}", resourceInstanceId);
105                 JanusGraphOperationStatus status = vertexService.right().value();
106                 if (status == JanusGraphOperationStatus.NOT_FOUND) {
107                     status = JanusGraphOperationStatus.INVALID_ID;
108                 }
109                 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(vertexService.right().value()));
110                 return result;
111             }
112             Vertex vertex = vertexService.left().value();
113
114             VertexProperty<Object> vertexProperty = vertex.property(counterType.getProperty());
115             Integer counter = 0;
116             if (vertexProperty.isPresent()) {
117                 if (vertexProperty.value() != null) {
118                     counter = (Integer) vertexProperty.value();
119                 }
120             }
121
122             counter++;
123             vertex.property(counterType.getProperty(), counter);
124
125             result = Either.left(counter);
126             return result;
127
128         } finally {
129             if (!inTransaction) {
130                 if (result == null || result.isRight()) {
131                     log.error("increaseAndGetResourceInstanceSpecificCounter operation : Going to execute rollback on graph.");
132                     janusGraphGenericDao.rollback();
133                 } else {
134                     log.debug("increaseAndGetResourceInstanceSpecificCounter operation : Going to execute commit on graph.");
135                     janusGraphGenericDao.commit();
136                 }
137             }
138         }
139
140     }
141
142     private void connectAttValueDataToComponentInstanceData(Wrapper<JanusGraphOperationStatus> errorWrapper, ComponentInstanceData compIns, AttributeValueData attValueData) {
143
144         Either<GraphRelation, JanusGraphOperationStatus> createRelResult = janusGraphGenericDao
145             .createRelation(compIns, attValueData, GraphEdgeLabels.ATTRIBUTE_VALUE, null);
146
147         if (createRelResult.isRight()) {
148             JanusGraphOperationStatus operationStatus = createRelResult.right().value();
149             errorWrapper.setInnerElement(operationStatus);
150             BeEcompErrorManager.getInstance().logInternalFlowError("connectAttValueDataToComponentInstanceData",
151                     "Failed to associate resource instance " + compIns.getUniqueId() + " attribute value " + attValueData.getUniqueId() + " in graph. status is " + operationStatus, ErrorSeverity.ERROR);
152         }
153     }
154
155     private void connectAttValueDataToAttData(Wrapper<JanusGraphOperationStatus> errorWrapper, AttributeData attData, AttributeValueData attValueData) {
156
157         Either<GraphRelation, JanusGraphOperationStatus> createRelResult = janusGraphGenericDao
158             .createRelation(attValueData, attData, GraphEdgeLabels.ATTRIBUTE_IMPL, null);
159
160         if (createRelResult.isRight()) {
161             JanusGraphOperationStatus operationStatus = createRelResult.right().value();
162             BeEcompErrorManager.getInstance().logInternalFlowError("connectAttValueDataToAttData",
163                     "Failed to associate attribute value " + attValueData.getUniqueId() + " to attribute " + attData.getUniqueId() + " in graph. status is " + operationStatus, ErrorSeverity.ERROR);
164
165             errorWrapper.setInnerElement(operationStatus);
166         }
167     }
168
169     private void createAttributeValueDataNode(ComponentInstanceProperty attributeInstanceProperty, Integer index, Wrapper<JanusGraphOperationStatus> errorWrapper, ComponentInstanceData resourceInstanceData,
170                                               Wrapper<AttributeValueData> attValueDataWrapper) {
171         String valueUniqueUid = attributeInstanceProperty.getValueUniqueUid();
172         if (valueUniqueUid == null) {
173
174             String attValueDatauniqueId = UniqueIdBuilder.buildResourceInstanceAttributeValueUid(resourceInstanceData.getUniqueId(), index);
175             AttributeValueData attributeValueData = buildAttributeValueDataFromComponentInstanceAttribute(attributeInstanceProperty, attValueDatauniqueId);
176
177             log.debug("Before adding attribute value to graph {}", attributeValueData);
178             Either<AttributeValueData, JanusGraphOperationStatus> createNodeResult = janusGraphGenericDao
179                 .createNode(attributeValueData, AttributeValueData.class);
180             log.debug("After adding attribute value to graph {}", attributeValueData);
181
182             if (createNodeResult.isRight()) {
183                 JanusGraphOperationStatus operationStatus = createNodeResult.right().value();
184                 errorWrapper.setInnerElement(operationStatus);
185             } else {
186                 attValueDataWrapper.setInnerElement(createNodeResult.left().value());
187             }
188
189         } else {
190             BeEcompErrorManager.getInstance().logInternalFlowError("CreateAttributeValueDataNode", "attribute value already exists.", ErrorSeverity.ERROR);
191             errorWrapper.setInnerElement(JanusGraphOperationStatus.ALREADY_EXIST);
192         }
193     }
194
195     private AttributeValueData buildAttributeValueDataFromComponentInstanceAttribute(ComponentInstanceProperty resourceInstanceAttribute, String uniqueId) {
196         AttributeValueData attributeValueData = new AttributeValueData();
197         attributeValueData.setUniqueId(uniqueId);
198         attributeValueData.setHidden(resourceInstanceAttribute.isHidden());
199         attributeValueData.setValue(resourceInstanceAttribute.getValue());
200         attributeValueData.setType(resourceInstanceAttribute.getType());
201         long currentTimeMillis = System.currentTimeMillis();
202         attributeValueData.setCreationTime(currentTimeMillis);
203         attributeValueData.setModificationTime(currentTimeMillis);
204         return attributeValueData;
205     }
206
207     private static final class UpdateDataContainer<SomeData, SomeValueData> {
208         final Wrapper<SomeValueData> valueDataWrapper;
209         final Wrapper<SomeData> dataWrapper;
210         final GraphEdgeLabels graphEdge;
211         final Supplier<Class<SomeData>> someDataClassGen;
212         final Supplier<Class<SomeValueData>> someValueDataClassGen;
213         final NodeTypeEnum nodeType;
214         final NodeTypeEnum nodeTypeValue;
215
216         private UpdateDataContainer(GraphEdgeLabels graphEdge, Supplier<Class<SomeData>> someDataClassGen, Supplier<Class<SomeValueData>> someValueDataClassGen, NodeTypeEnum nodeType, NodeTypeEnum nodeTypeValue) {
217             super();
218             this.valueDataWrapper = new Wrapper<>();
219             this.dataWrapper = new Wrapper<>();
220             this.graphEdge = graphEdge;
221             this.someDataClassGen = someDataClassGen;
222             this.someValueDataClassGen = someValueDataClassGen;
223             this.nodeType = nodeType;
224             this.nodeTypeValue = nodeTypeValue;
225         }
226
227         public Wrapper<SomeValueData> getValueDataWrapper() {
228             return valueDataWrapper;
229         }
230
231         public Wrapper<SomeData> getDataWrapper() {
232             return dataWrapper;
233         }
234
235         public GraphEdgeLabels getGraphEdge() {
236             return graphEdge;
237         }
238
239         public Supplier<Class<SomeData>> getSomeDataClassGen() {
240             return someDataClassGen;
241         }
242
243         public Supplier<Class<SomeValueData>> getSomeValueDataClassGen() {
244             return someValueDataClassGen;
245         }
246
247         public NodeTypeEnum getNodeType() {
248             return nodeType;
249         }
250
251         public NodeTypeEnum getNodeTypeValue() {
252             return nodeTypeValue;
253         }
254     }
255
256     /**
257      * update value of attribute on resource instance
258      *
259      * @param resourceInstanceAttribute
260      * @param resourceInstanceId
261      * @return
262      */
263     private Either<AttributeValueData, JanusGraphOperationStatus> updateAttributeOfResourceInstance(ComponentInstanceProperty resourceInstanceAttribute, String resourceInstanceId) {
264
265         Either<AttributeValueData, JanusGraphOperationStatus> result = null;
266         Wrapper<JanusGraphOperationStatus> errorWrapper = new Wrapper<>();
267         UpdateDataContainer<AttributeData, AttributeValueData> updateDataContainer = new UpdateDataContainer<>(GraphEdgeLabels.ATTRIBUTE_IMPL, (() -> AttributeData.class), (() -> AttributeValueData.class), NodeTypeEnum.Attribute,
268                 NodeTypeEnum.AttributeValue);
269         preUpdateElementOfResourceInstanceValidations(updateDataContainer, resourceInstanceAttribute, resourceInstanceId, errorWrapper);
270         if (errorWrapper.isEmpty()) {
271             AttributeValueData attributeValueData = updateDataContainer.getValueDataWrapper().getInnerElement();
272             attributeValueData.setHidden(resourceInstanceAttribute.isHidden());
273             attributeValueData.setValue(resourceInstanceAttribute.getValue());
274             Either<AttributeValueData, JanusGraphOperationStatus> updateRes = janusGraphGenericDao
275                 .updateNode(attributeValueData, AttributeValueData.class);
276             if (updateRes.isRight()) {
277                 JanusGraphOperationStatus status = updateRes.right().value();
278                 errorWrapper.setInnerElement(status);
279             } else {
280                 result = Either.left(updateRes.left().value());
281             }
282         }
283         if (!errorWrapper.isEmpty()) {
284             result = Either.right(errorWrapper.getInnerElement());
285         }
286         return result;
287
288     }
289
290     private Either<AttributeValueData, JanusGraphOperationStatus> addAttributeToResourceInstance(ComponentInstanceProperty attributeInstanceProperty, String resourceInstanceId, Integer index) {
291         Wrapper<JanusGraphOperationStatus> errorWrapper = new Wrapper<>();
292         Wrapper<ComponentInstanceData> compInsWrapper = new Wrapper<>();
293         Wrapper<AttributeData> attDataWrapper = new Wrapper<>();
294         Wrapper<AttributeValueData> attValueDataWrapper = new Wrapper<>();
295
296         // Verify RI Exist
297         validateRIExist(resourceInstanceId, compInsWrapper, errorWrapper);
298
299         if (errorWrapper.isEmpty()) {
300             // Verify Attribute Exist
301             validateElementExistInGraph(attributeInstanceProperty.getUniqueId(), NodeTypeEnum.Attribute, () -> AttributeData.class, attDataWrapper, errorWrapper);
302         }
303         if (errorWrapper.isEmpty()) {
304             // Create AttributeValueData that is connected to RI
305             createAttributeValueDataNode(attributeInstanceProperty, index, errorWrapper, compInsWrapper.getInnerElement(), attValueDataWrapper);
306         }
307         if (errorWrapper.isEmpty()) {
308             // Connect AttributeValueData (Att on RI) to AttData (Att on
309             // Resource)
310             connectAttValueDataToAttData(errorWrapper, attDataWrapper.getInnerElement(), attValueDataWrapper.getInnerElement());
311         }
312         if (errorWrapper.isEmpty()) {
313             // Connect AttributeValueData to RI
314             connectAttValueDataToComponentInstanceData(errorWrapper, compInsWrapper.getInnerElement(), attValueDataWrapper.getInnerElement());
315         }
316
317         if (errorWrapper.isEmpty()) {
318             return Either.left(attValueDataWrapper.getInnerElement());
319         } else {
320             return Either.right(errorWrapper.getInnerElement());
321         }
322
323     }
324
325     private <SomeData extends GraphNode, SomeValueData extends GraphNode> void preUpdateElementOfResourceInstanceValidations(UpdateDataContainer<SomeData, SomeValueData> updateDataContainer, IComponentInstanceConnectedElement resourceInstanceProerty,
326             String resourceInstanceId, Wrapper<JanusGraphOperationStatus> errorWrapper) {
327
328         if (errorWrapper.isEmpty()) {
329             // Verify VFC instance Exist
330             validateRIExist(resourceInstanceId, errorWrapper);
331         }
332
333         if (errorWrapper.isEmpty()) {
334             // Example: Verify Property connected to VFC exist
335             validateElementConnectedToComponentExist(updateDataContainer, resourceInstanceProerty, errorWrapper);
336         }
337
338         if (errorWrapper.isEmpty()) {
339             // Example: Verify PropertyValue connected to VFC Instance exist
340             validateElementConnectedToComponentInstanceExist(updateDataContainer, resourceInstanceProerty, errorWrapper);
341         }
342
343         if (errorWrapper.isEmpty()) {
344             // Example: Verify PropertyValue connected Property
345             validateElementConnectedToInstance(updateDataContainer, resourceInstanceProerty, errorWrapper);
346         }
347     }
348
349     private <SomeData extends GraphNode, SomeValueData extends GraphNode> void validateElementConnectedToInstance(UpdateDataContainer<SomeData, SomeValueData> updateDataContainer, IComponentInstanceConnectedElement resourceInstanceProerty,
350             Wrapper<JanusGraphOperationStatus> errorWrapper) {
351         Either<ImmutablePair<SomeData, GraphEdge>, JanusGraphOperationStatus> child = janusGraphGenericDao
352             .getChild(UniqueIdBuilder.getKeyByNodeType(updateDataContainer.getNodeTypeValue()), resourceInstanceProerty.getValueUniqueUid(),
353                 updateDataContainer.getGraphEdge(), updateDataContainer.getNodeType(), updateDataContainer.getSomeDataClassGen().get());
354
355         if (child.isRight()) {
356             JanusGraphOperationStatus status = child.right().value();
357             if (status == JanusGraphOperationStatus.NOT_FOUND) {
358                 status = JanusGraphOperationStatus.INVALID_ID;
359             }
360             errorWrapper.setInnerElement(status);
361
362         } else {
363             updateDataContainer.getDataWrapper().setInnerElement(child.left().value().left);
364         }
365     }
366
367     private <SomeValueData extends GraphNode, SomeData extends GraphNode> void validateElementConnectedToComponentInstanceExist(UpdateDataContainer<SomeData, SomeValueData> updateDataContainer,
368             IComponentInstanceConnectedElement resourceInstanceProerty, Wrapper<JanusGraphOperationStatus> errorWrapper) {
369         String valueUniqueUid = resourceInstanceProerty.getValueUniqueUid();
370         if (valueUniqueUid == null) {
371             errorWrapper.setInnerElement(JanusGraphOperationStatus.INVALID_ID);
372         } else {
373             Either<SomeValueData, JanusGraphOperationStatus> findPropertyValueRes = janusGraphGenericDao
374                 .getNode(UniqueIdBuilder.getKeyByNodeType(updateDataContainer.getNodeTypeValue()), valueUniqueUid, updateDataContainer.getSomeValueDataClassGen().get());
375             if (findPropertyValueRes.isRight()) {
376                 JanusGraphOperationStatus status = findPropertyValueRes.right().value();
377                 if (status == JanusGraphOperationStatus.NOT_FOUND) {
378                     status = JanusGraphOperationStatus.INVALID_ID;
379                 }
380                 errorWrapper.setInnerElement(status);
381             } else {
382                 updateDataContainer.getValueDataWrapper().setInnerElement(findPropertyValueRes.left().value());
383             }
384         }
385     }
386
387     private <SomeData extends GraphNode, SomeValueData extends GraphNode> void validateElementConnectedToComponentExist(UpdateDataContainer<SomeData, SomeValueData> updateDataContainer,
388             IComponentInstanceConnectedElement resourceInstanceElementConnected, Wrapper<JanusGraphOperationStatus> errorWrapper) {
389         String uniqueId = resourceInstanceElementConnected.getUniqueId();
390         Either<SomeData, JanusGraphOperationStatus> findPropertyDefRes = janusGraphGenericDao
391             .getNode(UniqueIdBuilder.getKeyByNodeType(updateDataContainer.getNodeType()), uniqueId, updateDataContainer.getSomeDataClassGen().get());
392
393         if (findPropertyDefRes.isRight()) {
394             JanusGraphOperationStatus status = findPropertyDefRes.right().value();
395             errorWrapper.setInnerElement(status);
396         }
397     }
398
399     private void validateRIExist(String resourceInstanceId, Wrapper<JanusGraphOperationStatus> errorWrapper) {
400         validateRIExist(resourceInstanceId, null, errorWrapper);
401     }
402
403     private void validateRIExist(String resourceInstanceId, Wrapper<ComponentInstanceData> compInsDataWrapper, Wrapper<JanusGraphOperationStatus> errorWrapper) {
404         validateElementExistInGraph(resourceInstanceId, NodeTypeEnum.ResourceInstance, () -> ComponentInstanceData.class, compInsDataWrapper, errorWrapper);
405     }
406
407     public <ElementData extends GraphNode> void validateElementExistInGraph(String elementUniqueId, NodeTypeEnum elementNodeType, Supplier<Class<ElementData>> elementClassGen, Wrapper<ElementData> elementDataWrapper,
408             Wrapper<JanusGraphOperationStatus> errorWrapper) {
409         Either<ElementData, JanusGraphOperationStatus> findResInstanceRes = janusGraphGenericDao
410             .getNode(UniqueIdBuilder.getKeyByNodeType(elementNodeType), elementUniqueId, elementClassGen.get());
411         if (findResInstanceRes.isRight()) {
412             JanusGraphOperationStatus status = findResInstanceRes.right().value();
413             if (status == JanusGraphOperationStatus.NOT_FOUND) {
414                 status = JanusGraphOperationStatus.INVALID_ID;
415             }
416             errorWrapper.setInnerElement(status);
417         } else {
418             if (elementDataWrapper != null) {
419                 elementDataWrapper.setInnerElement(findResInstanceRes.left().value());
420             }
421         }
422     }
423
424     /**
425      * add property to resource instance
426      *
427      * @param resourceInstanceId
428      * @param index
429      * @return
430      */
431     private Either<InputValueData, JanusGraphOperationStatus> addInputToResourceInstance(ComponentInstanceInput resourceInstanceInput, String resourceInstanceId, Integer index) {
432
433         Either<ComponentInstanceData, JanusGraphOperationStatus> findResInstanceRes = janusGraphGenericDao
434             .getNode(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.ResourceInstance), resourceInstanceId, ComponentInstanceData.class);
435
436         if (findResInstanceRes.isRight()) {
437             JanusGraphOperationStatus status = findResInstanceRes.right().value();
438             if (status == JanusGraphOperationStatus.NOT_FOUND) {
439                 status = JanusGraphOperationStatus.INVALID_ID;
440             }
441             return Either.right(status);
442         }
443
444         String propertyId = resourceInstanceInput.getUniqueId();
445         Either<InputsData, JanusGraphOperationStatus> findPropertyDefRes = janusGraphGenericDao
446             .getNode(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.Input), propertyId, InputsData.class);
447
448         if (findPropertyDefRes.isRight()) {
449             JanusGraphOperationStatus status = findPropertyDefRes.right().value();
450             if (status == JanusGraphOperationStatus.NOT_FOUND) {
451                 status = JanusGraphOperationStatus.INVALID_ID;
452             }
453             return Either.right(status);
454         }
455
456         String valueUniqueUid = resourceInstanceInput.getValueUniqueUid();
457         if (valueUniqueUid == null) {
458
459             InputsData propertyData = findPropertyDefRes.left().value();
460
461             ComponentInstanceData resourceInstanceData = findResInstanceRes.left().value();
462
463             ImmutablePair<JanusGraphOperationStatus, String> isInputValueExists = inputOperation.findInputValue(resourceInstanceId, propertyId);
464             if (isInputValueExists.getLeft() == JanusGraphOperationStatus.ALREADY_EXIST) {
465                 log.debug("The property {} already added to the resource instance {}", propertyId, resourceInstanceId);
466                 resourceInstanceInput.setValueUniqueUid(isInputValueExists.getRight());
467             }
468
469             if (isInputValueExists.getLeft() != JanusGraphOperationStatus.NOT_FOUND) {
470                 log.debug("After finding input value of {} on componenet instance {}", propertyId, resourceInstanceId);
471                 return Either.right(isInputValueExists.getLeft());
472             }
473
474             String innerType = null;
475
476             PropertyDataDefinition propDataDef = propertyData.getPropertyDataDefinition();
477             String propertyType = propDataDef.getType();
478             String value = resourceInstanceInput.getValue();
479             ToscaPropertyType type = ToscaPropertyType.isValidType(propertyType);
480
481             if (type == ToscaPropertyType.LIST || type == ToscaPropertyType.MAP) {
482                 SchemaDefinition def = propDataDef.getSchema();
483                 if (def == null) {
484                     log.debug("Schema doesn't exists for property of type {}", type);
485                     return Either.right(JanusGraphOperationStatus.ILLEGAL_ARGUMENT);
486                 }
487                 PropertyDataDefinition propDef = def.getProperty();
488                 if (propDef == null) {
489                     log.debug("Property in Schema Definition inside property of type {} doesn't exist", type);
490                     return Either.right(JanusGraphOperationStatus.ILLEGAL_ARGUMENT);
491                 }
492                 innerType = propDef.getType();
493             }
494
495             log.debug("Before validateAndUpdatePropertyValue");
496             Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> allDataTypes = dataTypeCache.getAll();
497             if (allDataTypes.isRight()) {
498                 JanusGraphOperationStatus status = allDataTypes.right().value();
499                 BeEcompErrorManager.getInstance().logInternalFlowError("UpdatePropertyValueOnComponentInstance", "Failed to update property value on instance. Status is " + status, ErrorSeverity.ERROR);
500                 return Either.right(status);
501             }
502
503             String uniqueId = UniqueIdBuilder.buildResourceInstanceInputValueUid(resourceInstanceData.getUniqueId(), index);
504             InputValueData propertyValueData = new InputValueData();
505             propertyValueData.setUniqueId(uniqueId);
506             propertyValueData.setValue(value);
507
508             log.debug("Before validateAndUpdateRules");
509             ImmutablePair<String, Boolean> pair = propertyOperation.validateAndUpdateRules(propertyType, resourceInstanceInput.getRules(), innerType, allDataTypes.left().value(), true);
510             log.debug("After validateAndUpdateRules. pair = {} ", pair);
511             if (pair.getRight() != null && !pair.getRight()) {
512                 BeEcompErrorManager.getInstance().logBeInvalidValueError("Add property value", pair.getLeft(), resourceInstanceInput.getName(), propertyType);
513                 return Either.right(JanusGraphOperationStatus.ILLEGAL_ARGUMENT);
514             }
515             log.debug("Before adding property value to graph {}", propertyValueData);
516             Either<InputValueData, JanusGraphOperationStatus> createNodeResult = janusGraphGenericDao
517                 .createNode(propertyValueData, InputValueData.class);
518             log.debug("After adding property value to graph {}", propertyValueData);
519
520             if (createNodeResult.isRight()) {
521                 JanusGraphOperationStatus operationStatus = createNodeResult.right().value();
522                 return Either.right(operationStatus);
523             }
524
525             Either<GraphRelation, JanusGraphOperationStatus> createRelResult = janusGraphGenericDao
526                 .createRelation(propertyValueData, propertyData, GraphEdgeLabels.INPUT_IMPL, null);
527
528             if (createRelResult.isRight()) {
529                 JanusGraphOperationStatus operationStatus = createRelResult.right().value();
530                 log.error("Failed to associate property value {} to property {} in graph. status is {}", uniqueId, propertyId, operationStatus);
531                 return Either.right(operationStatus);
532             }
533
534             Map<String, Object> properties1 = new HashMap<>();
535
536             properties1.put(GraphEdgePropertiesDictionary.NAME.getProperty(), resourceInstanceData.getComponentInstDataDefinition().getName());
537             properties1.put(GraphEdgePropertiesDictionary.OWNER_ID.getProperty(), resourceInstanceData.getComponentInstDataDefinition().getUniqueId());
538
539             createRelResult = janusGraphGenericDao
540                 .createRelation(resourceInstanceData, propertyValueData, GraphEdgeLabels.INPUT_VALUE, properties1);
541
542             if (createRelResult.isRight()) {
543                 JanusGraphOperationStatus operationStatus = createNodeResult.right().value();
544                 log.error("Failed to associate resource instance {} property value {} in graph. status is {}", resourceInstanceId, uniqueId, operationStatus);
545                 return Either.right(operationStatus);
546
547             }
548
549             return Either.left(createNodeResult.left().value());
550         } else {
551             log.error("property value already exists.");
552             return Either.right(JanusGraphOperationStatus.ALREADY_EXIST);
553         }
554
555     }
556
557     public Either<ComponentInstanceProperty, StorageOperationStatus> addAttributeValueToResourceInstance(ComponentInstanceProperty resourceInstanceAttribute, String resourceInstanceId, Integer index, boolean inTransaction) {
558         Either<ComponentInstanceProperty, StorageOperationStatus> result = null;
559
560         try {
561
562             Either<AttributeValueData, JanusGraphOperationStatus> eitherStatus = this.addAttributeToResourceInstance(resourceInstanceAttribute, resourceInstanceId, index);
563
564             if (eitherStatus.isRight()) {
565                 log.error("Failed to add attribute value {} to resource instance {} in Graph. status is {}", resourceInstanceAttribute, resourceInstanceId, eitherStatus.right().value().name());
566                 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(eitherStatus.right().value()));
567                 return result;
568             } else {
569                 AttributeValueData attributeValueData = eitherStatus.left().value();
570
571                 ComponentInstanceProperty attributeValueResult = buildResourceInstanceAttribute(attributeValueData, resourceInstanceAttribute);
572                 log.debug("The returned ResourceInstanceAttribute is {}", attributeValueResult);
573
574                 result = Either.left(attributeValueResult);
575                 return result;
576             }
577         }
578
579         finally {
580             handleTransactionCommitRollback(inTransaction, result);
581         }
582     }
583
584     private ComponentInstanceProperty buildResourceInstanceAttribute(AttributeValueData attributeValueData, ComponentInstanceProperty resourceInstanceAttribute) {
585         Boolean hidden = attributeValueData.isHidden();
586         String uid = attributeValueData.getUniqueId();
587         return new ComponentInstanceProperty(hidden, resourceInstanceAttribute, uid);
588     }
589
590     public Either<ComponentInstanceProperty, StorageOperationStatus> updateAttributeValueInResourceInstance(ComponentInstanceProperty resourceInstanceAttribute, String resourceInstanceId, boolean inTransaction) {
591
592         Either<ComponentInstanceProperty, StorageOperationStatus> result = null;
593
594         try {
595             Either<AttributeValueData, JanusGraphOperationStatus> eitherAttributeValue = updateAttributeOfResourceInstance(resourceInstanceAttribute, resourceInstanceId);
596
597             if (eitherAttributeValue.isRight()) {
598                 log.error("Failed to add attribute value {} to resource instance {} in Graph. status is {}", resourceInstanceAttribute, resourceInstanceId, eitherAttributeValue.right().value().name());
599                 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(eitherAttributeValue.right().value()));
600                 return result;
601             } else {
602                 AttributeValueData attributeValueData = eitherAttributeValue.left().value();
603
604                 ComponentInstanceProperty attributeValueResult = buildResourceInstanceAttribute(attributeValueData, resourceInstanceAttribute);
605                 log.debug("The returned ResourceInstanceAttribute is {}", attributeValueResult);
606
607                 result = Either.left(attributeValueResult);
608                 return result;
609             }
610         }
611
612         finally {
613             handleTransactionCommitRollback(inTransaction, result);
614         }
615
616     }
617
618     public Either<ComponentInstanceInput, StorageOperationStatus> addInputValueToResourceInstance(ComponentInstanceInput resourceInstanceInput, String resourceInstanceId, Integer index, boolean inTransaction) {
619
620         /// #RULES SUPPORT
621         /// Ignore rules received from client till support
622         resourceInstanceInput.setRules(null);
623         ///
624         ///
625
626         Either<ComponentInstanceInput, StorageOperationStatus> result = null;
627
628         try {
629
630             Either<InputValueData, JanusGraphOperationStatus> eitherStatus = addInputToResourceInstance(resourceInstanceInput, resourceInstanceId, index);
631
632             if (eitherStatus.isRight()) {
633                 log.error("Failed to add input value {} to resource instance {} in Graph. status is {}", resourceInstanceInput, resourceInstanceId, eitherStatus.right().value().name());
634                 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(eitherStatus.right().value()));
635                 return result;
636             } else {
637                 InputValueData propertyValueData = eitherStatus.left().value();
638
639                 ComponentInstanceInput propertyValueResult = inputOperation.buildResourceInstanceInput(propertyValueData, resourceInstanceInput);
640                 log.debug("The returned ResourceInstanceProperty is {}", propertyValueResult);
641
642                 Either<String, JanusGraphOperationStatus> findDefaultValue = propertyOperation.findDefaultValueFromSecondPosition(resourceInstanceInput.getPath(), resourceInstanceInput.getUniqueId(), resourceInstanceInput.getDefaultValue());
643                 if (findDefaultValue.isRight()) {
644                     result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(findDefaultValue.right().value()));
645                     return result;
646                 }
647                 String defaultValue = findDefaultValue.left().value();
648                 propertyValueResult.setDefaultValue(defaultValue);
649                 log.debug("The returned default value in ResourceInstanceProperty is {}", defaultValue);
650
651                 result = Either.left(propertyValueResult);
652                 return result;
653             }
654         }
655
656         finally {
657             if (!inTransaction) {
658                 if (result == null || result.isRight()) {
659                     log.error("Going to execute rollback on graph.");
660                     janusGraphGenericDao.rollback();
661                 } else {
662                     log.debug("Going to execute commit on graph.");
663                     janusGraphGenericDao.commit();
664                 }
665             }
666         }
667
668     }
669
670     public Either<ComponentInstanceInput, StorageOperationStatus> updateInputValueInResourceInstance(ComponentInstanceInput input, String resourceInstanceId, boolean b) {
671         return null;
672     }
673
674     public StorageOperationStatus updateCustomizationUUID(String componentInstanceId) {
675         Either<JanusGraphVertex, JanusGraphOperationStatus> vertexByProperty = janusGraphGenericDao.getVertexByProperty(GraphPropertiesDictionary.UNIQUE_ID.getProperty(), componentInstanceId);
676         if (vertexByProperty.isRight()) {
677             log.debug("Failed to fetch component instance by id {} error {}", componentInstanceId, vertexByProperty.right().value());
678             return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(vertexByProperty.right().value());
679         }
680         UUID uuid = UUID.randomUUID();
681         JanusGraphVertex ciVertex = vertexByProperty.left().value();
682         ciVertex.property(GraphPropertiesDictionary.CUSTOMIZATION_UUID.getProperty(), uuid.toString());
683
684         return StorageOperationStatus.OK;
685     }
686
687     public Either<ComponentInstanceData, StorageOperationStatus> updateComponentInstanceModificationTimeAndCustomizationUuidOnGraph(ComponentInstance componentInstance, NodeTypeEnum componentInstanceType, Long modificationTime, boolean inTransaction) {
688
689         log.debug("Going to update modification time of component instance {}. ", componentInstance.getName());
690         Either<ComponentInstanceData, StorageOperationStatus> result = null;
691         try{
692             ComponentInstanceData componentData = new ComponentInstanceData(componentInstance, componentInstance.getGroupInstances().size());
693             componentData.getComponentInstDataDefinition().setModificationTime(modificationTime);
694             componentData.getComponentInstDataDefinition().setCustomizationUUID(UUID.randomUUID().toString());
695             Either<ComponentInstanceData, JanusGraphOperationStatus> updateNode = janusGraphGenericDao
696                 .updateNode(componentData, ComponentInstanceData.class);
697             if (updateNode.isRight()) {
698                 log.error("Failed to update resource {}. status is {}", componentInstance.getUniqueId(), updateNode.right().value());
699                 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(updateNode.right().value()));
700             }else{
701                 result = Either.left(updateNode.left().value());
702             }
703         }catch(Exception e){
704             log.error("Exception occured during  update modification date of compomemt instance{}. The message is {}. ", componentInstance.getName(), e.getMessage(), e);
705             result = Either.right(StorageOperationStatus.GENERAL_ERROR);
706         }finally {
707             if(!inTransaction){
708                 if (result == null || result.isRight()) {
709                     log.error("Going to execute rollback on graph.");
710                     janusGraphGenericDao.rollback();
711                 } else {
712                     log.debug("Going to execute commit on graph.");
713                     janusGraphGenericDao.commit();
714                 }
715             }
716         }
717         return result;
718     }
719 }