Implement adding Interface to VFC
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / jsonjanusgraph / operations / ToscaOperationFacade.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 package org.openecomp.sdc.be.model.jsonjanusgraph.operations;
21
22 import static java.util.Objects.requireNonNull;
23 import static org.apache.commons.collections.CollectionUtils.isEmpty;
24 import static org.apache.commons.collections.CollectionUtils.isNotEmpty;
25 import static org.janusgraph.core.attribute.Text.REGEX;
26
27 import com.vdurmont.semver4j.Semver;
28 import com.vdurmont.semver4j.Semver.SemverType;
29 import fj.data.Either;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.Collection;
33 import java.util.Collections;
34 import java.util.EnumMap;
35 import java.util.HashMap;
36 import java.util.HashSet;
37 import java.util.Iterator;
38 import java.util.LinkedList;
39 import java.util.List;
40 import java.util.Map;
41 import java.util.Map.Entry;
42 import java.util.Objects;
43 import java.util.Optional;
44 import java.util.Set;
45 import java.util.function.BiPredicate;
46 import java.util.stream.Collectors;
47 import org.apache.commons.collections.CollectionUtils;
48 import org.apache.commons.collections.MapUtils;
49 import org.apache.commons.lang3.StringUtils;
50 import org.apache.commons.lang3.tuple.ImmutablePair;
51 import org.apache.tinkerpop.gremlin.structure.Direction;
52 import org.apache.tinkerpop.gremlin.structure.Edge;
53 import org.janusgraph.graphdb.query.JanusGraphPredicate;
54 import org.openecomp.sdc.be.config.Configuration;
55 import org.openecomp.sdc.be.config.ConfigurationManager;
56 import org.openecomp.sdc.be.dao.janusgraph.HealingJanusGraphDao;
57 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
58 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
59 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
60 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
61 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
62 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
63 import org.openecomp.sdc.be.datatypes.elements.AttributeDataDefinition;
64 import org.openecomp.sdc.be.datatypes.elements.CapabilityDataDefinition;
65 import org.openecomp.sdc.be.datatypes.elements.ComponentInstanceDataDefinition;
66 import org.openecomp.sdc.be.datatypes.elements.DataTypeDataDefinition;
67 import org.openecomp.sdc.be.datatypes.elements.GroupDataDefinition;
68 import org.openecomp.sdc.be.datatypes.elements.ListCapabilityDataDefinition;
69 import org.openecomp.sdc.be.datatypes.elements.ListRequirementDataDefinition;
70 import org.openecomp.sdc.be.datatypes.elements.MapArtifactDataDefinition;
71 import org.openecomp.sdc.be.datatypes.elements.MapAttributesDataDefinition;
72 import org.openecomp.sdc.be.datatypes.elements.MapCapabilityProperty;
73 import org.openecomp.sdc.be.datatypes.elements.MapInterfaceDataDefinition;
74 import org.openecomp.sdc.be.datatypes.elements.MapListCapabilityDataDefinition;
75 import org.openecomp.sdc.be.datatypes.elements.MapListRequirementDataDefinition;
76 import org.openecomp.sdc.be.datatypes.elements.MapPropertiesDataDefinition;
77 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
78 import org.openecomp.sdc.be.datatypes.elements.RequirementDataDefinition;
79 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
80 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
81 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
82 import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
83 import org.openecomp.sdc.be.datatypes.enums.PromoteVersionEnum;
84 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
85 import org.openecomp.sdc.be.model.ArtifactDefinition;
86 import org.openecomp.sdc.be.model.AttributeDefinition;
87 import org.openecomp.sdc.be.model.CapabilityDefinition;
88 import org.openecomp.sdc.be.model.CatalogUpdateTimestamp;
89 import org.openecomp.sdc.be.model.Component;
90 import org.openecomp.sdc.be.model.ComponentInstance;
91 import org.openecomp.sdc.be.model.ComponentInstanceAttribute;
92 import org.openecomp.sdc.be.model.ComponentInstanceInput;
93 import org.openecomp.sdc.be.model.ComponentInstanceInterface;
94 import org.openecomp.sdc.be.model.ComponentInstanceOutput;
95 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
96 import org.openecomp.sdc.be.model.ComponentParametersView;
97 import org.openecomp.sdc.be.model.DataTypeDefinition;
98 import org.openecomp.sdc.be.model.DistributionStatusEnum;
99 import org.openecomp.sdc.be.model.GroupDefinition;
100 import org.openecomp.sdc.be.model.GroupInstance;
101 import org.openecomp.sdc.be.model.InputDefinition;
102 import org.openecomp.sdc.be.model.InterfaceDefinition;
103 import org.openecomp.sdc.be.model.LifecycleStateEnum;
104 import org.openecomp.sdc.be.model.OutputDefinition;
105 import org.openecomp.sdc.be.model.PolicyDefinition;
106 import org.openecomp.sdc.be.model.PropertyDefinition;
107 import org.openecomp.sdc.be.model.RelationshipInfo;
108 import org.openecomp.sdc.be.model.RequirementCapabilityRelDef;
109 import org.openecomp.sdc.be.model.RequirementDefinition;
110 import org.openecomp.sdc.be.model.Resource;
111 import org.openecomp.sdc.be.model.Service;
112 import org.openecomp.sdc.be.model.User;
113 import org.openecomp.sdc.be.model.catalog.CatalogComponent;
114 import org.openecomp.sdc.be.model.jsonjanusgraph.config.ContainerInstanceTypesData;
115 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.TopologyTemplate;
116 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElement;
117 import org.openecomp.sdc.be.model.jsonjanusgraph.utils.ModelConverter;
118 import org.openecomp.sdc.be.model.operations.StorageException;
119 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
120 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
121 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
122 import org.openecomp.sdc.be.model.utils.GroupUtils;
123 import org.openecomp.sdc.be.resources.data.ComponentMetadataData;
124 import org.openecomp.sdc.common.jsongraph.util.CommonUtility;
125 import org.openecomp.sdc.common.jsongraph.util.CommonUtility.LogLevelEnum;
126 import org.openecomp.sdc.common.log.wrappers.Logger;
127 import org.openecomp.sdc.common.util.ValidationUtils;
128 import org.springframework.beans.factory.annotation.Autowired;
129
130 @org.springframework.stereotype.Component("tosca-operation-facade")
131 public class ToscaOperationFacade {
132
133     // region - ToscaElement - GetById
134     public static final String PROXY_SUFFIX = "_proxy";
135     // region - Fields
136     private static final String COULDNT_FETCH_A_COMPONENT_WITH_AND_UNIQUE_ID_ERROR = "Couldn't fetch a component with and UniqueId {}, error: {}";
137     private static final String FAILED_TO_FIND_RECENTLY_ADDED_PROPERTY_ON_THE_RESOURCE_STATUS_IS = "Failed to find recently added property {} on the resource {}. Status is {}. ";
138     private static final String FAILED_TO_GET_UPDATED_RESOURCE_STATUS_IS = "Failed to get updated resource {}. Status is {}. ";
139     private static final String FAILED_TO_ADD_THE_PROPERTY_TO_THE_RESOURCE_STATUS_IS = "Failed to add the property {} to the resource {}. Status is {}. ";
140     private static final String SERVICE = "service";
141     private static final String VF = "VF";
142     private static final String NOT_SUPPORTED_COMPONENT_TYPE = "Not supported component type {}";
143     private static final String COMPONENT_CREATED_SUCCESSFULLY = "Component created successfully!!!";
144     private static final String COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR = "Couldn't fetch component with and unique id {}, error: {}";
145     private static final Logger log = Logger.getLogger(ToscaOperationFacade.class.getName());
146     @Autowired
147     private NodeTypeOperation nodeTypeOperation;
148     @Autowired
149     private TopologyTemplateOperation topologyTemplateOperation;
150     @Autowired
151     private NodeTemplateOperation nodeTemplateOperation;
152     @Autowired
153     private GroupsOperation groupsOperation;
154     @Autowired
155     private HealingJanusGraphDao janusGraphDao;
156     // endregion
157     @Autowired
158     private ContainerInstanceTypesData containerInstanceTypesData;
159
160     private static Optional<CapabilityDefinition> getPropertyCapability(String propertyParentUniqueId, Component containerComponent) {
161         Map<String, List<CapabilityDefinition>> componentCapabilities = containerComponent.getCapabilities();
162         if (MapUtils.isEmpty(componentCapabilities)) {
163             return Optional.empty();
164         }
165         List<CapabilityDefinition> capabilityDefinitionList = componentCapabilities.values().stream().flatMap(Collection::stream)
166             .collect(Collectors.toList());
167         if (CollectionUtils.isEmpty(capabilityDefinitionList)) {
168             return Optional.empty();
169         }
170         return capabilityDefinitionList.stream().filter(capabilityDefinition -> capabilityDefinition.getUniqueId().equals(propertyParentUniqueId))
171             .findAny();
172     }
173
174     public <T extends Component> Either<T, StorageOperationStatus> getToscaFullElement(String componentId) {
175         ComponentParametersView filters = new ComponentParametersView();
176         filters.setIgnoreCapabiltyProperties(false);
177         filters.setIgnoreServicePath(false);
178         return getToscaElement(componentId, filters);
179     }
180
181     public <T extends Component> Either<T, StorageOperationStatus> getToscaElement(String componentId) {
182         return getToscaElement(componentId, JsonParseFlagEnum.ParseAll);
183     }
184
185     public <T extends Component> Either<T, StorageOperationStatus> getToscaElement(String componentId, ComponentParametersView filters) {
186         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao.getVertexById(componentId, filters.detectParseFlag());
187         if (getVertexEither.isRight()) {
188             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
189             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
190         }
191         return getToscaElementByOperation(getVertexEither.left().value(), filters);
192     }
193
194     public <T extends Component> Either<T, StorageOperationStatus> getToscaElement(String componentId, JsonParseFlagEnum parseFlag) {
195         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao.getVertexById(componentId, parseFlag);
196         if (getVertexEither.isRight()) {
197             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
198             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
199         }
200         return getToscaElementByOperation(getVertexEither.left().value());
201     }
202
203     public <T extends Component> Either<T, StorageOperationStatus> getToscaElement(GraphVertex componentVertex) {
204         return getToscaElementByOperation(componentVertex);
205     }
206
207     public Either<Boolean, StorageOperationStatus> validateComponentExists(String componentId) {
208         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
209         if (getVertexEither.isRight()) {
210             JanusGraphOperationStatus status = getVertexEither.right().value();
211             if (status == JanusGraphOperationStatus.NOT_FOUND) {
212                 return Either.left(false);
213             } else {
214                 log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
215                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
216             }
217         }
218         return Either.left(true);
219     }
220     // endregion
221
222     public <T extends Component> Either<T, StorageOperationStatus> findLastCertifiedToscaElementByUUID(T component) {
223         Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
224         props.put(GraphPropertyEnum.UUID, component.getUUID());
225         props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
226         props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
227         Either<List<GraphVertex>, JanusGraphOperationStatus> getVertexEither = janusGraphDao
228             .getByCriteria(ModelConverter.getVertexType(component), props);
229         if (getVertexEither.isRight()) {
230             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, component.getUniqueId(), getVertexEither.right().value());
231             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
232         }
233         return getToscaElementByOperation(getVertexEither.left().value().get(0));
234     }
235
236     // region - ToscaElement - GetByOperation
237     private <T extends Component> Either<T, StorageOperationStatus> getToscaElementByOperation(GraphVertex componentV) {
238         return getToscaElementByOperation(componentV, new ComponentParametersView());
239     }
240
241     private <T extends Component> Either<T, StorageOperationStatus> getToscaElementByOperation(GraphVertex componentV,
242                                                                                                ComponentParametersView filters) {
243         if (componentV == null) {
244             log.debug("Unexpected null value for `componentV`");
245             return Either.right(StorageOperationStatus.GENERAL_ERROR);
246         } else {
247             VertexTypeEnum label = componentV.getLabel();
248             ToscaElementOperation toscaOperation = getToscaElementOperation(componentV);
249             if (toscaOperation != null) {
250                 log.debug("getToscaElementByOperation: toscaOperation={}", toscaOperation.getClass());
251             }
252             Either<ToscaElement, StorageOperationStatus> toscaElement;
253             String componentId = componentV.getUniqueId();
254             if (toscaOperation != null) {
255                 log.debug("Need to fetch tosca element for id {}", componentId);
256                 toscaElement = toscaOperation.getToscaElement(componentV, filters);
257             } else {
258                 log.debug("not supported tosca type {} for id {}", label, componentId);
259                 toscaElement = Either.right(StorageOperationStatus.BAD_REQUEST);
260             }
261             return toscaElement.left().map(ModelConverter::convertFromToscaElement);
262         }
263     }
264
265     // endregion
266     private ToscaElementOperation getToscaElementOperation(GraphVertex componentV) {
267         VertexTypeEnum label = componentV.getLabel();
268         switch (label) {
269             case NODE_TYPE:
270                 return nodeTypeOperation;
271             case TOPOLOGY_TEMPLATE:
272                 return topologyTemplateOperation;
273             default:
274                 return null;
275         }
276     }
277
278     public <T extends Component> Either<T, StorageOperationStatus> createToscaComponent(T resource) {
279         ToscaElement toscaElement = ModelConverter.convertToToscaElement(resource);
280         ToscaElementOperation toscaElementOperation = getToscaElementOperation(resource);
281         Either<ToscaElement, StorageOperationStatus> createToscaElement = toscaElementOperation.createToscaElement(toscaElement);
282         if (createToscaElement.isLeft()) {
283             log.debug(COMPONENT_CREATED_SUCCESSFULLY);
284             T dataModel = ModelConverter.convertFromToscaElement(createToscaElement.left().value());
285             return Either.left(dataModel);
286         }
287         return Either.right(createToscaElement.right().value());
288     }
289
290     // region - ToscaElement Delete
291     public StorageOperationStatus markComponentToDelete(Component componentToDelete) {
292         if (Boolean.TRUE.equals(componentToDelete.getIsDeleted()) && Boolean.FALSE.equals(componentToDelete.isHighestVersion())) {
293             // component already marked for delete
294             return StorageOperationStatus.OK;
295         } else {
296             Either<GraphVertex, JanusGraphOperationStatus> getResponse = janusGraphDao
297                 .getVertexById(componentToDelete.getUniqueId(), JsonParseFlagEnum.ParseAll);
298             if (getResponse.isRight()) {
299                 log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentToDelete.getUniqueId(), getResponse.right().value());
300                 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getResponse.right().value());
301             }
302             GraphVertex componentV = getResponse.left().value();
303             // same operation for node type and topology template operations
304             Either<GraphVertex, StorageOperationStatus> result = nodeTypeOperation.markComponentToDelete(componentV);
305             if (result.isRight()) {
306                 return result.right().value();
307             }
308             return StorageOperationStatus.OK;
309         }
310     }
311
312     public <T extends Component> Either<T, StorageOperationStatus> deleteToscaComponent(String componentId) {
313         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.ParseAll);
314         if (getVertexEither.isRight()) {
315             log.debug("Couldn't fetch component vertex with and unique id {}, error: {}", componentId, getVertexEither.right().value());
316             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
317         }
318         Either<ToscaElement, StorageOperationStatus> deleteElement = deleteToscaElement(getVertexEither.left().value());
319         if (deleteElement.isRight()) {
320             log.debug("Failed to delete component with and unique id {}, error: {}", componentId, deleteElement.right().value());
321             return Either.right(deleteElement.right().value());
322         }
323         T dataModel = ModelConverter.convertFromToscaElement(deleteElement.left().value());
324         return Either.left(dataModel);
325     }
326
327     private Either<ToscaElement, StorageOperationStatus> deleteToscaElement(GraphVertex componentV) {
328         VertexTypeEnum label = componentV.getLabel();
329         Either<ToscaElement, StorageOperationStatus> toscaElement;
330         Object componentId = componentV.getUniqueId();
331         switch (label) {
332             case NODE_TYPE:
333                 log.debug("Need to fetch node type for id {}", componentId);
334                 toscaElement = nodeTypeOperation.deleteToscaElement(componentV);
335                 break;
336             case TOPOLOGY_TEMPLATE:
337                 log.debug("Need to fetch topology template for id {}", componentId);
338                 toscaElement = topologyTemplateOperation.deleteToscaElement(componentV);
339                 break;
340             default:
341                 log.debug("not supported tosca type {} for id {}", label, componentId);
342                 toscaElement = Either.right(StorageOperationStatus.BAD_REQUEST);
343                 break;
344         }
345         return toscaElement;
346     }
347
348     // endregion
349     private ToscaElementOperation getToscaElementOperation(Component component) {
350         return ModelConverter.isAtomicComponent(component) ? nodeTypeOperation : topologyTemplateOperation;
351     }
352
353     public <T extends Component> Either<T, StorageOperationStatus> getLatestByToscaResourceNameAndModel(final String toscaResourceName,
354                                                                                                         final String model) {
355         return getLatestByNameAndModel(toscaResourceName, JsonParseFlagEnum.ParseMetadata, new ComponentParametersView(), model);
356     }
357
358     private <T extends Component> Either<T, StorageOperationStatus> getLatestByNameAndModel(final String nodeName,
359                                                                                             final JsonParseFlagEnum parseFlag,
360                                                                                             final ComponentParametersView filter,
361                                                                                             final String model) {
362         Either<T, StorageOperationStatus> result;
363         final Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
364         final Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
365         propertiesToMatch.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, nodeName);
366         propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
367         propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
368         final Either<List<GraphVertex>, JanusGraphOperationStatus> highestResources = janusGraphDao
369             .getByCriteria(null, propertiesToMatch, propertiesNotToMatch, parseFlag, model);
370         if (highestResources.isRight()) {
371             final JanusGraphOperationStatus status = highestResources.right().value();
372             log.debug("failed to find resource with name {}. status={} ", nodeName, status);
373             result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
374             return result;
375         }
376         final List<GraphVertex> resources = highestResources.left().value();
377         double version = 0.0;
378         GraphVertex highestResource = null;
379         for (final GraphVertex vertex : resources) {
380             final Object versionObj = vertex.getMetadataProperty(GraphPropertyEnum.VERSION);
381             double resourceVersion = Double.parseDouble((String) versionObj);
382             if (resourceVersion > version) {
383                 version = resourceVersion;
384                 highestResource = vertex;
385             }
386         }
387         return getToscaElementByOperation(highestResource, filter);
388     }
389
390     public <T extends Component> Either<T, StorageOperationStatus> getLatestByToscaResourceName(String toscaResourceName, String modelName) {
391         return getLatestByName(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName, modelName);
392     }
393
394     public <T extends Component> Either<T, StorageOperationStatus> getFullLatestComponentByToscaResourceName(String toscaResourceName) {
395         ComponentParametersView fetchAllFilter = new ComponentParametersView();
396         fetchAllFilter.setIgnoreServicePath(true);
397         fetchAllFilter.setIgnoreCapabiltyProperties(false);
398         return getLatestByName(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName, JsonParseFlagEnum.ParseAll, fetchAllFilter, null);
399     }
400
401     public <T extends Component> Either<T, StorageOperationStatus> getLatestByName(String resourceName, String modelName) {
402         return getLatestByName(GraphPropertyEnum.NAME, resourceName, modelName);
403     }
404
405     public StorageOperationStatus validateCsarUuidUniqueness(String csarUUID) {
406         Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
407         properties.put(GraphPropertyEnum.CSAR_UUID, csarUUID);
408         Either<List<GraphVertex>, JanusGraphOperationStatus> resources = janusGraphDao
409             .getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata);
410         if (resources.isRight()) {
411             if (resources.right().value() == JanusGraphOperationStatus.NOT_FOUND) {
412                 return StorageOperationStatus.OK;
413             } else {
414                 log.debug("failed to get resources from graph with property name: {}", csarUUID);
415                 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(resources.right().value());
416             }
417         }
418         return StorageOperationStatus.ENTITY_ALREADY_EXISTS;
419     }
420
421     public <T extends Component> Either<Set<T>, StorageOperationStatus> getFollowed(String userId, Set<LifecycleStateEnum> lifecycleStates,
422                                                                                     Set<LifecycleStateEnum> lastStateStates,
423                                                                                     ComponentTypeEnum componentType) {
424         Either<List<ToscaElement>, StorageOperationStatus> followedResources;
425         if (componentType == ComponentTypeEnum.RESOURCE) {
426             followedResources = nodeTypeOperation.getFollowedComponent(userId, lifecycleStates, lastStateStates, componentType);
427         } else {
428             followedResources = topologyTemplateOperation.getFollowedComponent(userId, lifecycleStates, lastStateStates, componentType);
429         }
430         Set<T> components = new HashSet<>();
431         if (followedResources.isRight() && followedResources.right().value() != StorageOperationStatus.NOT_FOUND) {
432             return Either.right(followedResources.right().value());
433         }
434         if (followedResources.isLeft()) {
435             List<ToscaElement> toscaElements = followedResources.left().value();
436             toscaElements.forEach(te -> {
437                 T component = ModelConverter.convertFromToscaElement(te);
438                 components.add(component);
439             });
440         }
441         return Either.left(components);
442     }
443
444     public Either<Resource, StorageOperationStatus> getLatestCertifiedNodeTypeByToscaResourceName(String toscaResourceName) {
445         return getLatestCertifiedByToscaResourceName(toscaResourceName, VertexTypeEnum.NODE_TYPE, JsonParseFlagEnum.ParseMetadata);
446     }
447
448     public Either<Resource, StorageOperationStatus> getByToscaResourceNameMatchingVendorRelease(final String toscaResourceName,
449                                                                                                 final String vendorVersion) {
450         return getByToscaResourceNameMatchingVendorRelease(toscaResourceName, VertexTypeEnum.NODE_TYPE, JsonParseFlagEnum.ParseMetadata,
451             vendorVersion);
452     }
453
454     public Either<Resource, StorageOperationStatus> getByToscaResourceNameMatchingVendorRelease(String toscaResourceName, VertexTypeEnum vertexType,
455                                                                                                 JsonParseFlagEnum parseFlag, String vendorRelease) {
456         Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
457         props.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName);
458         props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
459         Map<String, Entry<JanusGraphPredicate, Object>> predicateCriteria = getVendorVersionPredicate(vendorRelease);
460         Either<List<GraphVertex>, JanusGraphOperationStatus> getLatestRes = janusGraphDao
461             .getByCriteria(vertexType, props, null, predicateCriteria, parseFlag, null);
462         if (getLatestRes.isRight() || CollectionUtils.isEmpty(getLatestRes.left().value())) {
463             getLatestRes = janusGraphDao.getByCriteria(vertexType, props, parseFlag);
464         }
465         return getLatestRes.right().map(status -> {
466             CommonUtility
467                 .addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch {} with name {}. status={} ", vertexType, toscaResourceName, status);
468             return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status);
469         }).left().bind(resources -> {
470             double version = 0.0;
471             GraphVertex highestResource = null;
472             for (GraphVertex resource : resources) {
473                 double resourceVersion = Double.parseDouble((String) resource.getJsonMetadataField(JsonPresentationFields.VERSION));
474                 if (resourceVersion > version && isValidForVendorRelease(resource, vendorRelease)) {
475                     version = resourceVersion;
476                     highestResource = resource;
477                 }
478             }
479             if (highestResource != null) {
480                 return getToscaFullElement(highestResource.getUniqueId());
481             } else {
482                 log.debug("The vertex with the highest version could not be found for {}", toscaResourceName);
483                 return Either.right(StorageOperationStatus.GENERAL_ERROR);
484             }
485         });
486     }
487
488     public <T extends Component> Either<T, StorageOperationStatus> getByToscaResourceNameAndVersion(final String toscaResourceName,
489                                                                                                     final String version, final String model) {
490         Either<T, StorageOperationStatus> result;
491
492         Map<GraphPropertyEnum, Object> hasProperties = new EnumMap<>(GraphPropertyEnum.class);
493         Map<GraphPropertyEnum, Object> hasNotProperties = new EnumMap<>(GraphPropertyEnum.class);
494
495         hasProperties.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName);
496         hasProperties.put(GraphPropertyEnum.VERSION, version);
497         hasNotProperties.put(GraphPropertyEnum.IS_DELETED, true);
498
499         Either<List<GraphVertex>, JanusGraphOperationStatus> getResourceRes = janusGraphDao
500             .getByCriteria(VertexTypeEnum.NODE_TYPE, hasProperties, hasNotProperties, JsonParseFlagEnum.ParseAll, model);
501         if (getResourceRes.isRight()) {
502             JanusGraphOperationStatus status = getResourceRes.right().value();
503             log.debug("failed to find resource with toscaResourceName {}, version {}. Status is {} ", toscaResourceName, version, status);
504             result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
505             return result;
506         }
507         return getToscaElementByOperation(getResourceRes.left().value().get(0));
508     }
509
510     private Map<String, Entry<JanusGraphPredicate, Object>> getVendorVersionPredicate(final String vendorRelease) {
511         Map<String, Entry<JanusGraphPredicate, Object>> predicateCriteria = new HashMap<>();
512         if (!"1.0".equals(vendorRelease)) {
513             String[] vendorReleaseElements = vendorRelease.split("\\.");
514             if (vendorReleaseElements.length > 0) {
515                 String regex = ".*\"vendorRelease\":\"";
516                 for (int i = 0; i < vendorReleaseElements.length; i++) {
517                     regex += vendorReleaseElements[i];
518                     regex += i < vendorReleaseElements.length - 1 ? "\\." : "\".*";
519                 }
520                 predicateCriteria.put("metadata", new HashMap.SimpleEntry<>(REGEX, regex));
521             }
522         }
523         return predicateCriteria;
524     }
525
526     public boolean isNodeAssociatedToModel(final String model, final Resource resource) {
527         final List<GraphVertex> modelElementVertices = getResourceModelElementVertices(resource);
528         if (model == null) {
529             return modelElementVertices.isEmpty();
530         }
531         return modelElementVertices.stream().anyMatch(graphVertex -> graphVertex.getMetadataProperty(GraphPropertyEnum.NAME).equals(model));
532     }
533
534     public List<GraphVertex> getResourceModelElementVertices(final Resource resource) {
535         final Either<GraphVertex, JanusGraphOperationStatus> vertex =
536             janusGraphDao.getVertexById(resource.getUniqueId(), JsonParseFlagEnum.NoParse);
537         if (vertex.isRight() || Objects.isNull(vertex.left().value())) {
538             return Collections.emptyList();
539         }
540         final Either<List<GraphVertex>, JanusGraphOperationStatus> nodeModelVertices =
541             janusGraphDao.getParentVertices(vertex.left().value(), EdgeLabelEnum.MODEL_ELEMENT, JsonParseFlagEnum.NoParse);
542         if (nodeModelVertices.isRight() || nodeModelVertices.left().value() == null) {
543             return Collections.emptyList();
544         }
545         return nodeModelVertices.left().value();
546     }
547
548     private boolean isValidForVendorRelease(final GraphVertex resource, final String vendorRelease) {
549         if (!vendorRelease.equals("1.0")) {
550             try {
551                 Semver resourceSemVer = new Semver((String) resource.getJsonMetadataField(JsonPresentationFields.VENDOR_RELEASE), SemverType.NPM);
552                 Semver packageSemVer = new Semver(vendorRelease, SemverType.NPM);
553                 return !resourceSemVer.isGreaterThan(packageSemVer);
554             } catch (Exception exception) {
555                 log.debug("Error in comparing vendor release", exception);
556                 return true;
557             }
558         }
559         return true;
560     }
561
562     public Either<Resource, StorageOperationStatus> getLatestCertifiedByToscaResourceName(String toscaResourceName, VertexTypeEnum vertexType,
563                                                                                           JsonParseFlagEnum parseFlag) {
564         Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
565         props.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName);
566         props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
567         props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
568         Either<List<GraphVertex>, JanusGraphOperationStatus> getLatestRes = janusGraphDao.getByCriteria(vertexType, props, parseFlag);
569         return getLatestRes.right().map(status -> {
570             CommonUtility
571                 .addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch {} with name {}. status={} ", vertexType, toscaResourceName, status);
572             return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status);
573         }).left().bind(resources -> {
574             double version = 0.0;
575             GraphVertex highestResource = null;
576             for (GraphVertex resource : resources) {
577                 double resourceVersion = Double.parseDouble((String) resource.getJsonMetadataField(JsonPresentationFields.VERSION));
578                 if (resourceVersion > version) {
579                     version = resourceVersion;
580                     highestResource = resource;
581                 }
582             }
583             if (highestResource != null) {
584                 return getToscaFullElement(highestResource.getUniqueId());
585             } else {
586                 log.debug("The vertex with the highest version could not be found for {}", toscaResourceName);
587                 return Either.right(StorageOperationStatus.GENERAL_ERROR);
588             }
589         });
590     }
591
592     public Either<Resource, StorageOperationStatus> getLatestResourceByToscaResourceName(String toscaResourceName) {
593         if (toscaResourceName != null && toscaResourceName.contains("org.openecomp.resource.vf")) {
594             return getLatestResourceByToscaResourceName(toscaResourceName, VertexTypeEnum.TOPOLOGY_TEMPLATE, JsonParseFlagEnum.ParseMetadata);
595         } else {
596             return getLatestResourceByToscaResourceName(toscaResourceName, VertexTypeEnum.NODE_TYPE, JsonParseFlagEnum.ParseMetadata);
597         }
598     }
599
600     public Either<Resource, StorageOperationStatus> getLatestResourceByToscaResourceName(String toscaResourceName, VertexTypeEnum vertexType,
601                                                                                          JsonParseFlagEnum parseFlag) {
602         Either<Resource, StorageOperationStatus> result = null;
603         Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
604         props.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName);
605         props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
606         if (!toscaResourceName.contains("org.openecomp.resource.vf")) {
607             props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
608         }
609         Either<List<GraphVertex>, JanusGraphOperationStatus> getLatestRes = janusGraphDao.getByCriteria(vertexType, props, parseFlag);
610         if (getLatestRes.isRight()) {
611             JanusGraphOperationStatus status = getLatestRes.right().value();
612             CommonUtility
613                 .addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch {} with name {}. status={} ", vertexType, toscaResourceName, status);
614             result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
615         }
616         if (result == null) {
617             List<GraphVertex> resources = getLatestRes.left().value();
618             double version = 0.0;
619             GraphVertex highestResource = null;
620             for (GraphVertex resource : resources) {
621                 double resourceVersion = Double.parseDouble((String) resource.getJsonMetadataField(JsonPresentationFields.VERSION));
622                 if (resourceVersion > version) {
623                     version = resourceVersion;
624                     highestResource = resource;
625                 }
626             }
627             if (highestResource != null) {
628                 result = getToscaFullElement(highestResource.getUniqueId());
629             } else {
630                 log.debug("The vertex with the highest version could not be found for {}", toscaResourceName);
631                 result = Either.right(StorageOperationStatus.GENERAL_ERROR);
632             }
633         }
634         return result;
635     }
636
637     public Either<Boolean, StorageOperationStatus> validateToscaResourceNameExists(String templateName) {
638         Either<Boolean, StorageOperationStatus> validateUniquenessRes = validateToscaResourceNameUniqueness(templateName);
639         if (validateUniquenessRes.isLeft()) {
640             return Either.left(!validateUniquenessRes.left().value());
641         }
642         return validateUniquenessRes;
643     }
644
645     public Either<RequirementCapabilityRelDef, StorageOperationStatus> dissociateResourceInstances(String componentId,
646                                                                                                    RequirementCapabilityRelDef requirementDef) {
647         return nodeTemplateOperation.dissociateResourceInstances(componentId, requirementDef);
648     }
649
650     /**
651      * Allows to get fulfilled requirement by relation and received predicate
652      */
653     public Either<RequirementDataDefinition, StorageOperationStatus> getFulfilledRequirementByRelation(String componentId, String instanceId,
654                                                                                                        RequirementCapabilityRelDef relation,
655                                                                                                        BiPredicate<RelationshipInfo, RequirementDataDefinition> predicate) {
656         return nodeTemplateOperation.getFulfilledRequirementByRelation(componentId, instanceId, relation, predicate);
657     }
658
659     /**
660      * Allows to get fulfilled capability by relation and received predicate
661      */
662     public Either<CapabilityDataDefinition, StorageOperationStatus> getFulfilledCapabilityByRelation(String componentId, String instanceId,
663                                                                                                      RequirementCapabilityRelDef relation,
664                                                                                                      BiPredicate<RelationshipInfo, CapabilityDataDefinition> predicate) {
665         return nodeTemplateOperation.getFulfilledCapabilityByRelation(componentId, instanceId, relation, predicate);
666     }
667
668     public Either<List<RequirementCapabilityRelDef>, StorageOperationStatus> associateResourceInstances(Component component, String componentId,
669                                                                                                         List<RequirementCapabilityRelDef> relations) {
670         Either<List<RequirementCapabilityRelDef>, StorageOperationStatus> reqAndCapListEither = nodeTemplateOperation
671             .associateResourceInstances(component, componentId, relations);
672         if (component != null) {
673             updateInstancesCapAndReqOnComponentFromDB(component);
674         }
675         return reqAndCapListEither;
676     }
677
678     protected Either<Boolean, StorageOperationStatus> validateToscaResourceNameUniqueness(String name) {
679         Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
680         properties.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, name);
681         Either<List<GraphVertex>, JanusGraphOperationStatus> resources = janusGraphDao
682             .getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata);
683         if (resources.isRight() && resources.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
684             log.debug("failed to get resources from graph with property name: {}", name);
685             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(resources.right().value()));
686         }
687         List<GraphVertex> resourceList = (resources.isLeft() ? resources.left().value() : null);
688         if (isNotEmpty(resourceList)) {
689             if (log.isDebugEnabled()) {
690                 StringBuilder builder = new StringBuilder();
691                 for (GraphVertex resourceData : resourceList) {
692                     builder.append(resourceData.getUniqueId() + "|");
693                 }
694                 log.debug("resources  with property name:{} exists in graph. found {}", name, builder);
695             }
696             return Either.left(false);
697         } else {
698             log.debug("resources  with property name:{} does not exists in graph", name);
699             return Either.left(true);
700         }
701     }
702
703     // region - Component Update
704     public Either<Resource, StorageOperationStatus> overrideComponent(Resource newComponent, Resource oldComponent) {
705         copyArtifactsToNewComponent(newComponent, oldComponent);
706         Either<GraphVertex, JanusGraphOperationStatus> componentVEither = janusGraphDao
707             .getVertexById(oldComponent.getUniqueId(), JsonParseFlagEnum.NoParse);
708         if (componentVEither.isRight()) {
709             log.debug("Failed to fetch component {} error {}", oldComponent.getUniqueId(), componentVEither.right().value());
710             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(componentVEither.right().value()));
711         }
712         GraphVertex componentv = componentVEither.left().value();
713         Either<GraphVertex, JanusGraphOperationStatus> parentVertexEither = janusGraphDao
714             .getParentVertex(componentv, EdgeLabelEnum.VERSION, JsonParseFlagEnum.NoParse);
715         if (parentVertexEither.isRight() && parentVertexEither.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
716             log.debug("Failed to fetch parent version for component {} error {}", oldComponent.getUniqueId(), parentVertexEither.right().value());
717             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(parentVertexEither.right().value()));
718         }
719         Either<ToscaElement, StorageOperationStatus> deleteToscaComponent = deleteToscaElement(componentv);
720         if (deleteToscaComponent.isRight()) {
721             log.debug("Failed to remove old component {} error {}", oldComponent.getUniqueId(), deleteToscaComponent.right().value());
722             return Either.right(deleteToscaComponent.right().value());
723         }
724         Either<Resource, StorageOperationStatus> createToscaComponent = createToscaComponent(newComponent);
725         if (createToscaComponent.isRight()) {
726             log.debug("Failed to create tosca element component {} error {}", newComponent.getUniqueId(), createToscaComponent.right().value());
727             return Either.right(createToscaComponent.right().value());
728         }
729         Resource newElement = createToscaComponent.left().value();
730         Either<GraphVertex, JanusGraphOperationStatus> newVersionEither = janusGraphDao
731             .getVertexById(newElement.getUniqueId(), JsonParseFlagEnum.NoParse);
732         if (newVersionEither.isRight()) {
733             log.debug("Failed to fetch new tosca element component {} error {}", newComponent.getUniqueId(), newVersionEither.right().value());
734             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(newVersionEither.right().value()));
735         }
736         if (parentVertexEither.isLeft()) {
737             GraphVertex previousVersionV = parentVertexEither.left().value();
738             JanusGraphOperationStatus createEdge = janusGraphDao
739                 .createEdge(previousVersionV, newVersionEither.left().value(), EdgeLabelEnum.VERSION, null);
740             if (createEdge != JanusGraphOperationStatus.OK) {
741                 log.debug("Failed to associate to previous version {} new version {} error {}", previousVersionV.getUniqueId(),
742                     newVersionEither.right().value(), createEdge);
743                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(createEdge));
744             }
745         }
746         return Either.left(newElement);
747     }
748
749     void copyArtifactsToNewComponent(Resource newComponent, Resource oldComponent) {
750         // TODO - check if required
751         Map<String, ArtifactDefinition> toscaArtifacts = oldComponent.getToscaArtifacts();
752         if (toscaArtifacts != null && !toscaArtifacts.isEmpty()) {
753             toscaArtifacts.values().stream().forEach(a -> a.setDuplicated(Boolean.TRUE));
754         }
755         newComponent.setToscaArtifacts(toscaArtifacts);
756         Map<String, ArtifactDefinition> artifacts = oldComponent.getArtifacts();
757         if (artifacts != null && !artifacts.isEmpty()) {
758             artifacts.values().stream().forEach(a -> a.setDuplicated(Boolean.TRUE));
759         }
760         newComponent.setArtifacts(artifacts);
761         Map<String, ArtifactDefinition> depArtifacts = oldComponent.getDeploymentArtifacts();
762         if (depArtifacts != null && !depArtifacts.isEmpty()) {
763             depArtifacts.values().stream().forEach(a -> a.setDuplicated(Boolean.TRUE));
764         }
765         newComponent.setDeploymentArtifacts(depArtifacts);
766         newComponent.setLastUpdateDate(null);
767         newComponent.setHighestVersion(true);
768     }
769
770     public <T extends Component> Either<T, StorageOperationStatus> updateToscaElement(T componentToUpdate) {
771         return updateToscaElement(componentToUpdate, new ComponentParametersView());
772     }
773
774     public <T extends Component> Either<T, StorageOperationStatus> updateToscaElement(T componentToUpdate, ComponentParametersView filterResult) {
775         String componentId = componentToUpdate.getUniqueId();
776         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.ParseAll);
777         if (getVertexEither.isRight()) {
778             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
779             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
780         }
781         GraphVertex elementV = getVertexEither.left().value();
782         ToscaElementOperation toscaElementOperation = getToscaElementOperation(elementV);
783         ToscaElement toscaElementToUpdate = ModelConverter.convertToToscaElement(componentToUpdate);
784         Either<ToscaElement, StorageOperationStatus> updateToscaElement = null;
785         if (toscaElementOperation != null) {
786             updateToscaElement = toscaElementOperation.updateToscaElement(toscaElementToUpdate, elementV, filterResult);
787         } else {
788             log.debug("Null value returned by `getToscaElementOperation` with value {}", elementV);
789             updateToscaElement = Either.right(StorageOperationStatus.GENERAL_ERROR);
790         }
791         return updateToscaElement.bimap(ModelConverter::convertFromToscaElement, status -> {
792             log.debug("Failed to update tosca element {} error {}", componentId, status);
793             return status;
794         });
795     }
796
797     private <T extends Component> Either<T, StorageOperationStatus> getLatestByName(GraphPropertyEnum property, String nodeName,
798                                                                                     JsonParseFlagEnum parseFlag, String modelName) {
799         return getLatestByName(property, nodeName, parseFlag, new ComponentParametersView(), modelName);
800     }
801
802     private <T extends Component> Either<T, StorageOperationStatus> getLatestByName(GraphPropertyEnum property, String nodeName,
803                                                                                     JsonParseFlagEnum parseFlag, ComponentParametersView filter,
804                                                                                     String model) {
805         Either<T, StorageOperationStatus> result;
806         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
807         Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
808         propertiesToMatch.put(property, nodeName);
809         propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
810         propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
811         Either<List<GraphVertex>, JanusGraphOperationStatus> highestResources = janusGraphDao
812             .getByCriteria(null, propertiesToMatch, propertiesNotToMatch, parseFlag, model);
813         if (highestResources.isRight()) {
814             JanusGraphOperationStatus status = highestResources.right().value();
815             log.debug("failed to find resource with name {}. status={} ", nodeName, status);
816             result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
817             return result;
818         }
819         List<GraphVertex> resources = highestResources.left().value();
820         double version = 0.0;
821         GraphVertex highestResource = null;
822         for (GraphVertex vertex : resources) {
823             Object versionObj = vertex.getMetadataProperty(GraphPropertyEnum.VERSION);
824             double resourceVersion = Double.parseDouble((String) versionObj);
825             if (resourceVersion > version) {
826                 version = resourceVersion;
827                 highestResource = vertex;
828             }
829         }
830         return getToscaElementByOperation(highestResource, filter);
831     }
832
833     // region - Component Get By ..
834     private <T extends Component> Either<T, StorageOperationStatus> getLatestByName(GraphPropertyEnum property, String nodeName, String modelName) {
835         return getLatestByName(property, nodeName, JsonParseFlagEnum.ParseMetadata, modelName);
836     }
837
838     public <T extends Component> Either<List<T>, StorageOperationStatus> getBySystemName(ComponentTypeEnum componentType, String systemName) {
839         Either<List<T>, StorageOperationStatus> result = null;
840         Either<T, StorageOperationStatus> getComponentRes;
841         List<T> components = new ArrayList<>();
842         List<GraphVertex> componentVertices;
843         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
844         Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
845         propertiesToMatch.put(GraphPropertyEnum.SYSTEM_NAME, systemName);
846         if (componentType != null) {
847             propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
848         }
849         propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
850         Either<List<GraphVertex>, JanusGraphOperationStatus> getComponentsRes = janusGraphDao
851             .getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll);
852         if (getComponentsRes.isRight()) {
853             JanusGraphOperationStatus status = getComponentsRes.right().value();
854             log.debug("Failed to fetch the component with system name {}. Status is {} ", systemName, status);
855             result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
856         }
857         if (result == null) {
858             componentVertices = getComponentsRes.left().value();
859             for (GraphVertex componentVertex : componentVertices) {
860                 getComponentRes = getToscaElementByOperation(componentVertex);
861                 if (getComponentRes.isRight()) {
862                     log.debug("Failed to get the component {}. Status is {} ", componentVertex.getJsonMetadataField(JsonPresentationFields.NAME),
863                         getComponentRes.right().value());
864                     result = Either.right(getComponentRes.right().value());
865                     break;
866                 }
867                 T componentBySystemName = getComponentRes.left().value();
868                 log.debug("Found component, id: {}", componentBySystemName.getUniqueId());
869                 components.add(componentBySystemName);
870             }
871         }
872         if (result == null) {
873             result = Either.left(components);
874         }
875         return result;
876     }
877
878     public <T extends Component> Either<T, StorageOperationStatus> getComponentByNameAndVersion(ComponentTypeEnum componentType, String name,
879                                                                                                 String version) {
880         return getComponentByNameAndVersion(componentType, name, version, JsonParseFlagEnum.ParseAll);
881     }
882
883     public <T extends Component> Either<T, StorageOperationStatus> getComponentByNameAndVersion(ComponentTypeEnum componentType, String name,
884                                                                                                 String version, JsonParseFlagEnum parseFlag) {
885         Either<T, StorageOperationStatus> result;
886         Map<GraphPropertyEnum, Object> hasProperties = new EnumMap<>(GraphPropertyEnum.class);
887         Map<GraphPropertyEnum, Object> hasNotProperties = new EnumMap<>(GraphPropertyEnum.class);
888         hasProperties.put(GraphPropertyEnum.NAME, name);
889         hasProperties.put(GraphPropertyEnum.VERSION, version);
890         hasNotProperties.put(GraphPropertyEnum.IS_DELETED, true);
891         if (componentType != null) {
892             hasProperties.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
893         }
894         Either<List<GraphVertex>, JanusGraphOperationStatus> getResourceRes = janusGraphDao
895             .getByCriteria(null, hasProperties, hasNotProperties, parseFlag);
896         if (getResourceRes.isRight()) {
897             JanusGraphOperationStatus status = getResourceRes.right().value();
898             log.debug("failed to find resource with name {}, version {}. Status is {} ", name, version, status);
899             result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
900             return result;
901         }
902         return getToscaElementByOperation(getResourceRes.left().value().get(0));
903     }
904
905     public <T extends Component> Either<T, StorageOperationStatus> getComponentByNameAndVendorRelease(final ComponentTypeEnum componentType,
906                                                                                                       final String name, final String vendorRelease,
907                                                                                                       final JsonParseFlagEnum parseFlag,
908                                                                                                       final String modelName) {
909         Map<GraphPropertyEnum, Object> hasProperties = new EnumMap<>(GraphPropertyEnum.class);
910         Map<GraphPropertyEnum, Object> hasNotProperties = new EnumMap<>(GraphPropertyEnum.class);
911         hasProperties.put(GraphPropertyEnum.NAME, name);
912         hasNotProperties.put(GraphPropertyEnum.IS_DELETED, true);
913         if (componentType != null) {
914             hasProperties.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
915         }
916         Map<String, Entry<JanusGraphPredicate, Object>> predicateCriteria = getVendorVersionPredicate(vendorRelease);
917         Either<List<GraphVertex>, JanusGraphOperationStatus> getResourceRes = janusGraphDao.getByCriteria(null, hasProperties, hasNotProperties,
918             predicateCriteria, parseFlag, modelName);
919         if (getResourceRes.isRight()) {
920             JanusGraphOperationStatus status = getResourceRes.right().value();
921             log.debug("failed to find resource with name {}, version {}. Status is {} ", name, predicateCriteria, status);
922             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
923         }
924         return getToscaElementByOperation(getResourceRes.left().value().get(0));
925     }
926
927     public Either<List<CatalogComponent>, StorageOperationStatus> getCatalogOrArchiveComponents(boolean isCatalog,
928                                                                                                 List<OriginTypeEnum> excludeTypes) {
929         List<ResourceTypeEnum> excludedResourceTypes = Optional.ofNullable(excludeTypes).orElse(Collections.emptyList()).stream()
930             .filter(type -> !type.equals(OriginTypeEnum.SERVICE)).map(type -> ResourceTypeEnum.getTypeByName(type.name()))
931             .collect(Collectors.toList());
932         return topologyTemplateOperation.getElementCatalogData(isCatalog, excludedResourceTypes);
933     }
934
935     // endregion
936     public <T extends Component> Either<List<T>, StorageOperationStatus> getCatalogComponents(ComponentTypeEnum componentType,
937                                                                                               List<OriginTypeEnum> excludeTypes,
938                                                                                               boolean isHighestVersions) {
939         List<T> components = new ArrayList<>();
940         Either<List<ToscaElement>, StorageOperationStatus> catalogDataResult;
941         List<ToscaElement> toscaElements = new ArrayList<>();
942         List<ResourceTypeEnum> excludedResourceTypes = Optional.ofNullable(excludeTypes).orElse(Collections.emptyList()).stream()
943             .filter(type -> !type.equals(OriginTypeEnum.SERVICE)).map(type -> ResourceTypeEnum.getTypeByName(type.name()))
944             .collect(Collectors.toList());
945         switch (componentType) {
946             case RESOURCE:
947                 catalogDataResult = nodeTypeOperation.getElementCatalogData(ComponentTypeEnum.RESOURCE, excludedResourceTypes, isHighestVersions);
948                 if (catalogDataResult.isRight()) {
949                     return Either.right(catalogDataResult.right().value());
950                 }
951                 toscaElements = catalogDataResult.left().value();
952                 break;
953             case SERVICE:
954                 if (excludeTypes != null && excludeTypes.contains(OriginTypeEnum.SERVICE)) {
955                     break;
956                 }
957                 catalogDataResult = topologyTemplateOperation.getElementCatalogData(ComponentTypeEnum.SERVICE, null, isHighestVersions);
958                 if (catalogDataResult.isRight()) {
959                     return Either.right(catalogDataResult.right().value());
960                 }
961                 toscaElements = catalogDataResult.left().value();
962                 break;
963             default:
964                 log.debug(NOT_SUPPORTED_COMPONENT_TYPE, componentType);
965                 return Either.right(StorageOperationStatus.BAD_REQUEST);
966         }
967         toscaElements.forEach(te -> {
968             T component = ModelConverter.convertFromToscaElement(te);
969             components.add(component);
970         });
971         return Either.left(components);
972     }
973
974     public Either<List<String>, StorageOperationStatus> deleteMarkedElements(ComponentTypeEnum componentType) {
975         Either<List<GraphVertex>, StorageOperationStatus> allComponentsMarkedForDeletion;
976         switch (componentType) {
977             case RESOURCE:
978                 allComponentsMarkedForDeletion = nodeTypeOperation.getAllComponentsMarkedForDeletion(componentType);
979                 break;
980             case SERVICE:
981             case PRODUCT:
982                 allComponentsMarkedForDeletion = topologyTemplateOperation.getAllComponentsMarkedForDeletion(componentType);
983                 break;
984             default:
985                 log.debug(NOT_SUPPORTED_COMPONENT_TYPE, componentType);
986                 return Either.right(StorageOperationStatus.BAD_REQUEST);
987         }
988         if (allComponentsMarkedForDeletion.isRight()) {
989             return Either.right(allComponentsMarkedForDeletion.right().value());
990         }
991         List<GraphVertex> allMarked = allComponentsMarkedForDeletion.left().value();
992         return Either.left(checkIfInUseAndDelete(allMarked));
993     }
994
995     private List<String> checkIfInUseAndDelete(List<GraphVertex> allMarked) {
996         final List<EdgeLabelEnum> forbiddenEdgeLabelEnums = Arrays
997             .asList(EdgeLabelEnum.INSTANCE_OF, EdgeLabelEnum.PROXY_OF, EdgeLabelEnum.ALLOTTED_OF);
998         List<String> deleted = new ArrayList<>();
999         for (GraphVertex elementV : allMarked) {
1000             boolean isAllowedToDelete = true;
1001             for (EdgeLabelEnum edgeLabelEnum : forbiddenEdgeLabelEnums) {
1002                 Either<Edge, JanusGraphOperationStatus> belongingEdgeByCriteria = janusGraphDao
1003                     .getBelongingEdgeByCriteria(elementV, edgeLabelEnum, null);
1004                 if (belongingEdgeByCriteria.isLeft()) {
1005                     log.debug("Marked element {} in use. don't delete it", elementV.getUniqueId());
1006                     isAllowedToDelete = false;
1007                     break;
1008                 }
1009             }
1010             if (isAllowedToDelete) {
1011                 Either<ToscaElement, StorageOperationStatus> deleteToscaElement = deleteToscaElement(elementV);
1012                 if (deleteToscaElement.isRight()) {
1013                     log.debug("Failed to delete marked element UniqueID {}, Name {}, error {}", elementV.getUniqueId(),
1014                         elementV.getMetadataProperties().get(GraphPropertyEnum.NAME), deleteToscaElement.right().value());
1015                     continue;
1016                 }
1017                 deleted.add(elementV.getUniqueId());
1018             }
1019         }
1020         return deleted;
1021     }
1022
1023     public Either<List<String>, StorageOperationStatus> getAllComponentsMarkedForDeletion(ComponentTypeEnum componentType) {
1024         Either<List<GraphVertex>, StorageOperationStatus> allComponentsMarkedForDeletion;
1025         switch (componentType) {
1026             case RESOURCE:
1027                 allComponentsMarkedForDeletion = nodeTypeOperation.getAllComponentsMarkedForDeletion(componentType);
1028                 break;
1029             case SERVICE:
1030             case PRODUCT:
1031                 allComponentsMarkedForDeletion = topologyTemplateOperation.getAllComponentsMarkedForDeletion(componentType);
1032                 break;
1033             default:
1034                 log.debug(NOT_SUPPORTED_COMPONENT_TYPE, componentType);
1035                 return Either.right(StorageOperationStatus.BAD_REQUEST);
1036         }
1037         if (allComponentsMarkedForDeletion.isRight()) {
1038             return Either.right(allComponentsMarkedForDeletion.right().value());
1039         }
1040         return Either.left(allComponentsMarkedForDeletion.left().value().stream().map(GraphVertex::getUniqueId).collect(Collectors.toList()));
1041     }
1042
1043     // region - Component Update
1044     public Either<ImmutablePair<Component, String>, StorageOperationStatus> addComponentInstanceToTopologyTemplate(Component containerComponent,
1045                                                                                                                    Component origComponent,
1046                                                                                                                    ComponentInstance componentInstance,
1047                                                                                                                    boolean allowDeleted, User user) {
1048         Either<ImmutablePair<Component, String>, StorageOperationStatus> result = null;
1049         Either<ToscaElement, StorageOperationStatus> updateContainerComponentRes = null;
1050         if (StringUtils.isEmpty(componentInstance.getIcon())) {
1051             componentInstance.setIcon(origComponent.getIcon());
1052         }
1053         String nameToFindForCounter;
1054         switch (componentInstance.getOriginType()) {
1055             case ServiceProxy:
1056                 nameToFindForCounter = ValidationUtils.normaliseComponentName(componentInstance.getSourceModelName()) + PROXY_SUFFIX;
1057                 break;
1058             case ServiceSubstitution:
1059                 nameToFindForCounter = ValidationUtils.normaliseComponentName(componentInstance.getSourceModelName());
1060                 break;
1061             default:
1062                 nameToFindForCounter = origComponent.getName();
1063         }
1064         String nextComponentInstanceCounter = getNextComponentInstanceCounter(containerComponent, nameToFindForCounter);
1065         Either<ImmutablePair<TopologyTemplate, String>, StorageOperationStatus> addResult = nodeTemplateOperation
1066             .addComponentInstanceToTopologyTemplate(ModelConverter.convertToToscaElement(containerComponent),
1067                 ModelConverter.convertToToscaElement(origComponent), nextComponentInstanceCounter, componentInstance, allowDeleted, user);
1068         if (addResult.isRight()) {
1069             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to add the component instance {} to container component {}. ",
1070                 componentInstance.getName(), containerComponent.getName());
1071             result = Either.right(addResult.right().value());
1072         }
1073         if (result == null) {
1074             updateContainerComponentRes = topologyTemplateOperation.getToscaElement(containerComponent.getUniqueId());
1075             if (updateContainerComponentRes.isRight()) {
1076                 CommonUtility
1077                     .addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch updated topology template {} with updated component instance {}. ",
1078                         containerComponent.getName(), componentInstance.getName());
1079                 result = Either.right(updateContainerComponentRes.right().value());
1080             }
1081         }
1082         if (result == null) {
1083             Component updatedComponent = ModelConverter.convertFromToscaElement(updateContainerComponentRes.left().value());
1084             String createdInstanceId = addResult.left().value().getRight();
1085             CommonUtility
1086                 .addRecordToLog(log, LogLevelEnum.TRACE, "The component instance {} has been added to container component {}. ", createdInstanceId,
1087                     updatedComponent.getName());
1088             result = Either.left(new ImmutablePair<>(updatedComponent, createdInstanceId));
1089         }
1090         return result;
1091     }
1092
1093     public void associateComponentInstancesToComponent(Component containerComponent, Map<ComponentInstance, Resource> resourcesInstancesMap,
1094                                                        boolean allowDeleted, boolean isUpdateCsar) {
1095         CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Going to add component instances to component {}", containerComponent.getUniqueId());
1096         Either<GraphVertex, JanusGraphOperationStatus> metadataVertex = janusGraphDao
1097             .getVertexById(containerComponent.getUniqueId(), JsonParseFlagEnum.ParseAll);
1098         if (metadataVertex.isRight()) {
1099             JanusGraphOperationStatus status = metadataVertex.right().value();
1100             if (status == JanusGraphOperationStatus.NOT_FOUND) {
1101                 status = JanusGraphOperationStatus.INVALID_ID;
1102             }
1103             throw new StorageException(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
1104         }
1105         Map<String, ComponentInstanceDataDefinition> compnentInstancesMap = nodeTemplateOperation
1106             .associateComponentInstancesToComponent(containerComponent, resourcesInstancesMap, metadataVertex.left().value(), allowDeleted,
1107                 isUpdateCsar);
1108         containerComponent.setComponentInstances(ModelConverter.getComponentInstancesFromMapObject(compnentInstancesMap, containerComponent));
1109     }
1110
1111     public Either<ImmutablePair<Component, String>, StorageOperationStatus> updateComponentInstanceMetadataOfTopologyTemplate(
1112         Component containerComponent, Component origComponent, ComponentInstance componentInstance) {
1113         Either<ImmutablePair<Component, String>, StorageOperationStatus> result = null;
1114         CommonUtility.addRecordToLog(log, LogLevelEnum.TRACE,
1115             "Going to update the metadata of the component instance {} belonging to container component {}. ", componentInstance.getName(),
1116             containerComponent.getName());
1117         componentInstance.setIcon(origComponent.getIcon());
1118         Either<ImmutablePair<TopologyTemplate, String>, StorageOperationStatus> updateResult = nodeTemplateOperation
1119             .updateComponentInstanceMetadataOfTopologyTemplate(ModelConverter.convertToToscaElement(containerComponent),
1120                 ModelConverter.convertToToscaElement(origComponent), componentInstance);
1121         if (updateResult.isRight()) {
1122             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG,
1123                 "Failed to update the metadata of the component instance {} belonging to container component {}. ", componentInstance.getName(),
1124                 containerComponent.getName());
1125             result = Either.right(updateResult.right().value());
1126         }
1127         if (result == null) {
1128             Component updatedComponent = ModelConverter.convertFromToscaElement(updateResult.left().value().getLeft());
1129             String createdInstanceId = updateResult.left().value().getRight();
1130             CommonUtility
1131                 .addRecordToLog(log, LogLevelEnum.TRACE, "The metadata of the component instance {} has been updated to container component {}. ",
1132                     createdInstanceId, updatedComponent.getName());
1133             result = Either.left(new ImmutablePair<>(updatedComponent, createdInstanceId));
1134         }
1135         return result;
1136     }
1137
1138     public Either<Component, StorageOperationStatus> updateComponentInstanceMetadataOfTopologyTemplate(Component containerComponent) {
1139         return updateComponentInstanceMetadataOfTopologyTemplate(containerComponent, new ComponentParametersView());
1140     }
1141
1142     public Either<Component, StorageOperationStatus> updateComponentInstanceMetadataOfTopologyTemplate(Component containerComponent,
1143                                                                                                        ComponentParametersView filter) {
1144         Either<Component, StorageOperationStatus> result = null;
1145         CommonUtility.addRecordToLog(log, LogLevelEnum.TRACE, "Going to update the metadata  belonging to container component {}. ",
1146             containerComponent.getName());
1147         Either<TopologyTemplate, StorageOperationStatus> updateResult = nodeTemplateOperation
1148             .updateComponentInstanceMetadataOfTopologyTemplate(ModelConverter.convertToToscaElement(containerComponent), filter);
1149         if (updateResult.isRight()) {
1150             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to update the metadata  belonging to container component {}. ",
1151                 containerComponent.getName());
1152             result = Either.right(updateResult.right().value());
1153         }
1154         if (result == null) {
1155             Component updatedComponent = ModelConverter.convertFromToscaElement(updateResult.left().value());
1156             CommonUtility
1157                 .addRecordToLog(log, LogLevelEnum.TRACE, "The metadata has been updated to container component {}. ", updatedComponent.getName());
1158             result = Either.left(updatedComponent);
1159         }
1160         return result;
1161     }
1162
1163     // endregion
1164     public Either<ImmutablePair<Component, String>, StorageOperationStatus> deleteComponentInstanceFromTopologyTemplate(Component containerComponent,
1165                                                                                                                         String resourceInstanceId) {
1166         Either<ImmutablePair<Component, String>, StorageOperationStatus> result = null;
1167         CommonUtility.addRecordToLog(log, LogLevelEnum.TRACE, "Going to delete the component instance {} belonging to container component {}. ",
1168             resourceInstanceId, containerComponent.getName());
1169         Either<ImmutablePair<TopologyTemplate, String>, StorageOperationStatus> updateResult = nodeTemplateOperation
1170             .deleteComponentInstanceFromTopologyTemplate(ModelConverter.convertToToscaElement(containerComponent), resourceInstanceId);
1171         if (updateResult.isRight()) {
1172             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to delete the component instance {} belonging to container component {}. ",
1173                 resourceInstanceId, containerComponent.getName());
1174             result = Either.right(updateResult.right().value());
1175         }
1176         if (result == null) {
1177             Component updatedComponent = ModelConverter.convertFromToscaElement(updateResult.left().value().getLeft());
1178             String deletedInstanceId = updateResult.left().value().getRight();
1179             CommonUtility.addRecordToLog(log, LogLevelEnum.TRACE, "The component instance {} has been deleted from container component {}. ",
1180                 deletedInstanceId, updatedComponent.getName());
1181             result = Either.left(new ImmutablePair<>(updatedComponent, deletedInstanceId));
1182         }
1183         return result;
1184     }
1185
1186     private String getNextComponentInstanceCounter(Component containerComponent, String originResourceName) {
1187         Integer nextCounter = 0;
1188         if (CollectionUtils.isNotEmpty(containerComponent.getComponentInstances())) {
1189             String normalizedName = ValidationUtils.normalizeComponentInstanceName(originResourceName);
1190             Integer maxCounter = getMaxCounterFromNamesAndIds(containerComponent, normalizedName);
1191             if (maxCounter != null) {
1192                 nextCounter = maxCounter + 1;
1193             }
1194         }
1195         return nextCounter.toString();
1196     }
1197
1198     /**
1199      * @return max counter of component instance Id's, null if not found
1200      */
1201     private Integer getMaxCounterFromNamesAndIds(Component containerComponent, String normalizedName) {
1202         List<String> countersInNames = containerComponent.getComponentInstances().stream()
1203             .filter(ci -> ci.getNormalizedName() != null && ci.getNormalizedName().startsWith(normalizedName))
1204             .map(ci -> ci.getNormalizedName().split(normalizedName)[1]).collect(Collectors.toList());
1205         List<String> countersInIds = containerComponent.getComponentInstances().stream()
1206             .filter(ci -> ci.getUniqueId() != null && ci.getUniqueId().contains(normalizedName)).map(ci -> ci.getUniqueId().split(normalizedName)[1])
1207             .collect(Collectors.toList());
1208         List<String> namesAndIdsList = new ArrayList<>(countersInNames);
1209         namesAndIdsList.addAll(countersInIds);
1210         return getMaxInteger(namesAndIdsList);
1211     }
1212
1213     private Integer getMaxInteger(List<String> counters) {
1214         Integer maxCounter = 0;
1215         Integer currCounter = null;
1216         for (String counter : counters) {
1217             try {
1218                 currCounter = Integer.parseInt(counter);
1219                 if (maxCounter < currCounter) {
1220                     maxCounter = currCounter;
1221                 }
1222             } catch (NumberFormatException e) {
1223                 continue;
1224             }
1225         }
1226         return currCounter == null ? null : maxCounter;
1227     }
1228
1229     public Either<RequirementCapabilityRelDef, StorageOperationStatus> associateResourceInstances(Component component, String componentId,
1230                                                                                                   RequirementCapabilityRelDef requirementDef) {
1231         return nodeTemplateOperation.associateResourceInstances(component, componentId, requirementDef);
1232     }
1233
1234     public Either<List<InputDefinition>, StorageOperationStatus> createAndAssociateInputs(Map<String, InputDefinition> inputs, String componentId) {
1235         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
1236         if (getVertexEither.isRight()) {
1237             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
1238             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
1239         }
1240         GraphVertex vertex = getVertexEither.left().value();
1241         Map<String, PropertyDataDefinition> inputsMap = inputs.entrySet().stream()
1242             .collect(Collectors.toMap(Map.Entry::getKey, e -> new PropertyDataDefinition(e.getValue())));
1243         StorageOperationStatus status = topologyTemplateOperation.associateInputsToComponent(vertex, inputsMap, componentId);
1244         if (StorageOperationStatus.OK == status) {
1245             log.debug(COMPONENT_CREATED_SUCCESSFULLY);
1246             List<InputDefinition> inputsResList = null;
1247             if (inputsMap != null && !inputsMap.isEmpty()) {
1248                 inputsResList = inputsMap.values().stream().map(InputDefinition::new).collect(Collectors.toList());
1249             }
1250             return Either.left(inputsResList);
1251         }
1252         return Either.right(status);
1253     }
1254
1255     public Either<List<InputDefinition>, StorageOperationStatus> addInputsToComponent(Map<String, InputDefinition> inputs, String componentId) {
1256         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
1257         if (getVertexEither.isRight()) {
1258             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
1259             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
1260         }
1261         GraphVertex vertex = getVertexEither.left().value();
1262         Map<String, PropertyDefinition> inputsMap = inputs.entrySet().stream()
1263             .collect(Collectors.toMap(Map.Entry::getKey, e -> new PropertyDefinition(e.getValue())));
1264         StorageOperationStatus status = topologyTemplateOperation
1265             .addToscaDataToToscaElement(vertex, EdgeLabelEnum.INPUTS, VertexTypeEnum.INPUTS, inputsMap, JsonPresentationFields.NAME);
1266         if (StorageOperationStatus.OK == status) {
1267             log.debug(COMPONENT_CREATED_SUCCESSFULLY);
1268             List<InputDefinition> inputsResList = null;
1269             if (inputsMap != null && !inputsMap.isEmpty()) {
1270                 inputsResList = inputsMap.values().stream().map(InputDefinition::new).collect(Collectors.toList());
1271             }
1272             return Either.left(inputsResList);
1273         }
1274         return Either.right(status);
1275     }
1276
1277     public Either<List<OutputDefinition>, StorageOperationStatus> addOutputsToComponent(Map<String, OutputDefinition> outputs, String componentId) {
1278         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
1279         if (getVertexEither.isRight()) {
1280             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
1281             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
1282         }
1283         GraphVertex vertex = getVertexEither.left().value();
1284         Map<String, AttributeDefinition> outputsMap = outputs.entrySet().stream()
1285             .collect(Collectors.toMap(Map.Entry::getKey, e -> new AttributeDefinition(e.getValue())));
1286         StorageOperationStatus status = topologyTemplateOperation
1287             .addToscaDataToToscaElement(vertex, EdgeLabelEnum.OUTPUTS, VertexTypeEnum.OUTPUTS, outputsMap, JsonPresentationFields.NAME);
1288         if (StorageOperationStatus.OK == status) {
1289             log.debug(COMPONENT_CREATED_SUCCESSFULLY);
1290             List<OutputDefinition> outputsResList = null;
1291             if (outputsMap != null && !outputsMap.isEmpty()) {
1292                 outputsResList = outputsMap.values().stream().map(OutputDefinition::new).collect(Collectors.toList());
1293             }
1294             return Either.left(outputsResList);
1295         }
1296         return Either.right(status);
1297     }
1298
1299     /**
1300      * Add data types into a Component.
1301      *
1302      * @param dataTypes   datatypes to be added. the key should be each name of data type.
1303      * @param componentId unique ID of Component.
1304      * @return list of data types.
1305      */
1306     public Either<List<DataTypeDefinition>, StorageOperationStatus> addDataTypesToComponent(Map<String, DataTypeDefinition> dataTypes,
1307                                                                                             String componentId) {
1308         log.trace("#addDataTypesToComponent - enter, componentId={}", componentId);
1309         /* get component vertex */
1310         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao
1311             .getVertexById(componentId, JsonParseFlagEnum.NoParse);
1312         if (getVertexEither.isRight()) {
1313             /* not found / error */
1314             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
1315             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
1316         }
1317         GraphVertex vertex = getVertexEither.left().value();
1318         log.trace("#addDataTypesToComponent - get vertex ok");
1319         // convert DataTypeDefinition to DataTypeDataDefinition
1320         Map<String, DataTypeDataDefinition> dataTypeDataMap = dataTypes.entrySet().stream()
1321             .collect(Collectors.toMap(Map.Entry::getKey, e -> convertDataTypeToDataTypeData(e.getValue())));
1322         // add datatype(s) to the Component.
1323
1324         // if child vertex does not exist, it will be created.
1325         StorageOperationStatus status = topologyTemplateOperation
1326             .addToscaDataToToscaElement(vertex, EdgeLabelEnum.DATA_TYPES, VertexTypeEnum.DATA_TYPES, dataTypeDataMap, JsonPresentationFields.NAME);
1327         if (StorageOperationStatus.OK == status) {
1328             log.debug(COMPONENT_CREATED_SUCCESSFULLY);
1329             List<DataTypeDefinition> inputsResList = null;
1330             if (!dataTypes.isEmpty()) {
1331                 inputsResList = new ArrayList<>(dataTypes.values());
1332             }
1333             return Either.left(inputsResList);
1334         }
1335         log.trace("#addDataTypesToComponent - leave");
1336         return Either.right(status);
1337     }
1338
1339     private DataTypeDataDefinition convertDataTypeToDataTypeData(DataTypeDefinition dataType) {
1340         DataTypeDataDefinition dataTypeData = new DataTypeDataDefinition(dataType);
1341         if (CollectionUtils.isNotEmpty(dataType.getProperties())) {
1342             List<PropertyDataDefinition> propertyDataList = dataType.getProperties().stream().map(PropertyDataDefinition::new)
1343                 .collect(Collectors.toList());
1344             dataTypeData.setPropertiesData(propertyDataList);
1345         }
1346         // if "derivedFrom" data_type exists, copy the name to "derivedFromName"
1347         if (dataType.getDerivedFrom() != null && StringUtils.isNotEmpty(dataType.getDerivedFrom().getName())) {
1348             // if names are different, log it
1349             if (!StringUtils.equals(dataTypeData.getDerivedFromName(), dataType.getDerivedFrom().getName())) {
1350                 log.debug("#convertDataTypeToDataTypeData - derivedFromName(={}) overwritten by derivedFrom.name(={})", dataType.getDerivedFromName(),
1351                     dataType.getDerivedFrom().getName());
1352             }
1353             dataTypeData.setDerivedFromName(dataType.getDerivedFrom().getName());
1354         }
1355         // supply "name" field to toscaPresentationValue in each datatype object for DAO operations
1356         dataTypeData.setToscaPresentationValue(JsonPresentationFields.NAME, dataType.getName());
1357         return dataTypeData;
1358     }
1359
1360     public Either<List<InputDefinition>, StorageOperationStatus> getComponentInputs(String componentId) {
1361         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
1362         if (getVertexEither.isRight()) {
1363             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
1364             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
1365         }
1366         Either<ToscaElement, StorageOperationStatus> toscaElement = topologyTemplateOperation.getToscaElement(componentId);
1367         if (toscaElement.isRight()) {
1368             return Either.right(toscaElement.right().value());
1369         }
1370         TopologyTemplate topologyTemplate = (TopologyTemplate) toscaElement.left().value();
1371         Map<String, PropertyDataDefinition> inputsMap = topologyTemplate.getInputs();
1372         List<InputDefinition> inputs = new ArrayList<>();
1373         if (MapUtils.isNotEmpty(inputsMap)) {
1374             inputs = inputsMap.values().stream().map(p -> new InputDefinition(p)).collect(Collectors.toList());
1375         }
1376         return Either.left(inputs);
1377     }
1378
1379     public Either<List<InputDefinition>, StorageOperationStatus> updateInputsToComponent(List<InputDefinition> inputs, String componentId) {
1380         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
1381         if (getVertexEither.isRight()) {
1382             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
1383             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
1384         }
1385         GraphVertex vertex = getVertexEither.left().value();
1386         List<PropertyDataDefinition> inputsAsDataDef = inputs.stream().map(PropertyDataDefinition::new).collect(Collectors.toList());
1387         StorageOperationStatus status = topologyTemplateOperation
1388             .updateToscaDataOfToscaElement(vertex, EdgeLabelEnum.INPUTS, VertexTypeEnum.INPUTS, inputsAsDataDef, JsonPresentationFields.NAME);
1389         if (StorageOperationStatus.OK == status) {
1390             log.debug(COMPONENT_CREATED_SUCCESSFULLY);
1391             List<InputDefinition> inputsResList = null;
1392             if (inputsAsDataDef != null && !inputsAsDataDef.isEmpty()) {
1393                 inputsResList = inputsAsDataDef.stream().map(InputDefinition::new).collect(Collectors.toList());
1394             }
1395             return Either.left(inputsResList);
1396         }
1397         return Either.right(status);
1398     }
1399
1400     // region - ComponentInstance
1401     public Either<Map<String, List<ComponentInstanceProperty>>, StorageOperationStatus> associateComponentInstancePropertiesToComponent(
1402         Map<String, List<ComponentInstanceProperty>> instProperties, String componentId) {
1403         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
1404         if (getVertexEither.isRight()) {
1405             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
1406             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
1407         }
1408         GraphVertex vertex = getVertexEither.left().value();
1409         Map<String, MapPropertiesDataDefinition> instPropsMap = new HashMap<>();
1410         if (instProperties != null) {
1411             MapPropertiesDataDefinition propertiesMap;
1412             for (Entry<String, List<ComponentInstanceProperty>> entry : instProperties.entrySet()) {
1413                 propertiesMap = new MapPropertiesDataDefinition();
1414                 propertiesMap.setMapToscaDataDefinition(
1415                     entry.getValue().stream().map(PropertyDataDefinition::new).collect(Collectors.toMap(PropertyDataDefinition::getName, e -> e)));
1416                 instPropsMap.put(entry.getKey(), propertiesMap);
1417             }
1418         }
1419         StorageOperationStatus status = topologyTemplateOperation.associateInstPropertiesToComponent(vertex, instPropsMap);
1420         if (StorageOperationStatus.OK == status) {
1421             log.debug(COMPONENT_CREATED_SUCCESSFULLY);
1422             return Either.left(instProperties);
1423         }
1424         return Either.right(status);
1425     }
1426
1427     /**
1428      * saves the instInputs as the updated instance inputs of the component container in DB
1429      */
1430     public Either<Map<String, List<ComponentInstanceInput>>, StorageOperationStatus> updateComponentInstanceInputsToComponent(
1431         Map<String, List<ComponentInstanceInput>> instInputs, String componentId) {
1432         if (instInputs == null || instInputs.isEmpty()) {
1433             return Either.left(instInputs);
1434         }
1435         StorageOperationStatus status;
1436         for (Entry<String, List<ComponentInstanceInput>> inputsPerIntance : instInputs.entrySet()) {
1437             List<ComponentInstanceInput> toscaDataListPerInst = inputsPerIntance.getValue();
1438             List<String> pathKeysPerInst = new ArrayList<>();
1439             pathKeysPerInst.add(inputsPerIntance.getKey());
1440             status = topologyTemplateOperation
1441                 .updateToscaDataDeepElementsOfToscaElement(componentId, EdgeLabelEnum.INST_INPUTS, VertexTypeEnum.INST_INPUTS, toscaDataListPerInst,
1442                     pathKeysPerInst, JsonPresentationFields.NAME);
1443             if (status != StorageOperationStatus.OK) {
1444                 log.debug("Failed to update component instance inputs for instance {} in component {} edge type {} error {}",
1445                     inputsPerIntance.getKey(), componentId, EdgeLabelEnum.INST_INPUTS, status);
1446                 return Either.right(status);
1447             }
1448         }
1449         return Either.left(instInputs);
1450     }
1451
1452     /**
1453      * saves the instProps as the updated instance properties of the component container in DB
1454      */
1455     public Either<Map<String, List<ComponentInstanceProperty>>, StorageOperationStatus> updateComponentInstancePropsToComponent(
1456         Map<String, List<ComponentInstanceProperty>> instProps, String componentId) {
1457         if (instProps == null || instProps.isEmpty()) {
1458             return Either.left(instProps);
1459         }
1460         StorageOperationStatus status;
1461         for (Entry<String, List<ComponentInstanceProperty>> propsPerIntance : instProps.entrySet()) {
1462             List<ComponentInstanceProperty> toscaDataListPerInst = propsPerIntance.getValue();
1463             List<String> pathKeysPerInst = new ArrayList<>();
1464             pathKeysPerInst.add(propsPerIntance.getKey());
1465             status = topologyTemplateOperation
1466                 .updateToscaDataDeepElementsOfToscaElement(componentId, EdgeLabelEnum.INST_PROPERTIES, VertexTypeEnum.INST_PROPERTIES,
1467                     toscaDataListPerInst, pathKeysPerInst, JsonPresentationFields.NAME);
1468             if (status != StorageOperationStatus.OK) {
1469                 log.debug("Failed to update component instance inputs for instance {} in component {} edge type {} error {}",
1470                     propsPerIntance.getKey(), componentId, EdgeLabelEnum.INST_PROPERTIES, status);
1471                 return Either.right(status);
1472             }
1473         }
1474         return Either.left(instProps);
1475     }
1476
1477     public Either<Map<String, List<ComponentInstanceInput>>, StorageOperationStatus> associateComponentInstanceInputsToComponent(
1478         Map<String, List<ComponentInstanceInput>> instInputs, String componentId) {
1479         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
1480         if (getVertexEither.isRight()) {
1481             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
1482             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
1483         }
1484         GraphVertex vertex = getVertexEither.left().value();
1485         Map<String, MapPropertiesDataDefinition> instPropsMap = new HashMap<>();
1486         if (instInputs != null) {
1487             MapPropertiesDataDefinition propertiesMap;
1488             for (Entry<String, List<ComponentInstanceInput>> entry : instInputs.entrySet()) {
1489                 propertiesMap = new MapPropertiesDataDefinition();
1490                 propertiesMap.setMapToscaDataDefinition(
1491                     entry.getValue().stream().map(PropertyDataDefinition::new).collect(Collectors.toMap(PropertyDataDefinition::getName, e -> e)));
1492                 instPropsMap.put(entry.getKey(), propertiesMap);
1493             }
1494         }
1495         StorageOperationStatus status = topologyTemplateOperation.associateInstInputsToComponent(vertex, instPropsMap);
1496         if (StorageOperationStatus.OK == status) {
1497             log.debug(COMPONENT_CREATED_SUCCESSFULLY);
1498             return Either.left(instInputs);
1499         }
1500         return Either.right(status);
1501     }
1502
1503     public Either<Map<String, List<ComponentInstanceInput>>, StorageOperationStatus> addComponentInstanceInputsToComponent(
1504         Component containerComponent, Map<String, List<ComponentInstanceInput>> instProperties) {
1505         requireNonNull(instProperties);
1506         StorageOperationStatus status;
1507         for (Entry<String, List<ComponentInstanceInput>> entry : instProperties.entrySet()) {
1508             List<ComponentInstanceInput> props = entry.getValue();
1509             String componentInstanceId = entry.getKey();
1510             if (!isEmpty(props)) {
1511                 for (ComponentInstanceInput property : props) {
1512                     List<ComponentInstanceInput> componentInstancesInputs = containerComponent.getComponentInstancesInputs().get(componentInstanceId);
1513                     Optional<ComponentInstanceInput> instanceProperty = componentInstancesInputs.stream()
1514                         .filter(p -> p.getName().equals(property.getName())).findAny();
1515                     if (instanceProperty.isPresent()) {
1516                         status = updateComponentInstanceInput(containerComponent, componentInstanceId, property);
1517                     } else {
1518                         status = addComponentInstanceInput(containerComponent, componentInstanceId, property);
1519                     }
1520                     if (status != StorageOperationStatus.OK) {
1521                         log.debug("Failed to update instance input {} for instance {} error {} ", property, componentInstanceId, status);
1522                         return Either.right(status);
1523                     } else {
1524                         log.trace("instance input {} for instance {} updated", property, componentInstanceId);
1525                     }
1526                 }
1527             }
1528         }
1529         return Either.left(instProperties);
1530     }
1531
1532     public Either<Map<String, List<ComponentInstanceOutput>>, StorageOperationStatus> addComponentInstanceOutputsToComponent(
1533         Component containerComponent, Map<String, List<ComponentInstanceOutput>> instProperties) {
1534         requireNonNull(instProperties);
1535         StorageOperationStatus status;
1536         for (final Entry<String, List<ComponentInstanceOutput>> entry : instProperties.entrySet()) {
1537             final List<ComponentInstanceOutput> props = entry.getValue();
1538             final String componentInstanceId = entry.getKey();
1539             if (!isEmpty(props)) {
1540                 for (final ComponentInstanceOutput property : props) {
1541                     final List<ComponentInstanceOutput> componentInstancesInputs = containerComponent.getComponentInstancesOutputs()
1542                         .get(componentInstanceId);
1543                     final Optional<ComponentInstanceOutput> instanceProperty = componentInstancesInputs.stream()
1544                         .filter(p -> p.getName().equals(property.getName())).findAny();
1545                     if (instanceProperty.isPresent()) {
1546                         status = updateComponentInstanceOutput(containerComponent, componentInstanceId, property);
1547                     } else {
1548                         status = addComponentInstanceOutput(containerComponent, componentInstanceId, property);
1549                     }
1550                     if (status != StorageOperationStatus.OK) {
1551                         log.debug("Failed to update instance input {} for instance {} error {} ", property, componentInstanceId, status);
1552                         return Either.right(status);
1553                     } else {
1554                         log.trace("instance input {} for instance {} updated", property, componentInstanceId);
1555                     }
1556                 }
1557             }
1558         }
1559         return Either.left(instProperties);
1560     }
1561
1562     public Either<Map<String, List<ComponentInstanceProperty>>, StorageOperationStatus> addComponentInstancePropertiesToComponent(
1563         Component containerComponent, Map<String, List<ComponentInstanceProperty>> instProperties) {
1564         requireNonNull(instProperties);
1565         for (Entry<String, List<ComponentInstanceProperty>> entry : instProperties.entrySet()) {
1566             List<ComponentInstanceProperty> props = entry.getValue();
1567             String componentInstanceId = entry.getKey();
1568             List<ComponentInstanceProperty> originalComponentInstProps = containerComponent.getComponentInstancesProperties()
1569                 .get(componentInstanceId);
1570             Map<String, List<CapabilityDefinition>> containerComponentCapabilities = containerComponent.getCapabilities();
1571             if (isEmpty(props)) {
1572                 continue;
1573             }
1574             for (ComponentInstanceProperty property : props) {
1575                 StorageOperationStatus status = null;
1576                 String propertyParentUniqueId = property.getParentUniqueId();
1577                 Optional<CapabilityDefinition> capPropDefinition = getPropertyCapability(propertyParentUniqueId, containerComponent);
1578                 if (capPropDefinition.isPresent() && MapUtils.isNotEmpty(containerComponentCapabilities)) {
1579                     status = populateAndUpdateInstanceCapProperty(containerComponent, componentInstanceId, containerComponentCapabilities, property,
1580                         capPropDefinition.get());
1581                 }
1582                 if (status == null) {
1583                     status = updateOrAddComponentInstanceProperty(containerComponent, componentInstanceId, originalComponentInstProps, property);
1584                 }
1585                 if (status != StorageOperationStatus.OK) {
1586                     return Either.right(status);
1587                 }
1588             }
1589         }
1590         return Either.left(instProperties);
1591     }
1592
1593     public Either<Map<String, List<ComponentInstanceAttribute>>, StorageOperationStatus> addComponentInstanceAttributesToComponent(
1594         final Component containerComponent, final Map<String, List<ComponentInstanceAttribute>> instProperties) {
1595         requireNonNull(instProperties);
1596         for (final Entry<String, List<ComponentInstanceAttribute>> entry : instProperties.entrySet()) {
1597             final List<ComponentInstanceAttribute> props = entry.getValue();
1598             if (isEmpty(props)) {
1599                 continue;
1600             }
1601             final String componentInstanceId = entry.getKey();
1602             final List<ComponentInstanceAttribute> originalComponentInstProps = containerComponent.getComponentInstancesAttributes()
1603                 .get(componentInstanceId);
1604             for (final ComponentInstanceAttribute property : props) {
1605                 final StorageOperationStatus status = updateOrAddComponentInstanceAttribute(containerComponent, componentInstanceId,
1606                     originalComponentInstProps, property);
1607                 if (status != StorageOperationStatus.OK) {
1608                     return Either.right(status);
1609                 }
1610             }
1611         }
1612         return Either.left(instProperties);
1613     }
1614
1615     private StorageOperationStatus populateAndUpdateInstanceCapProperty(Component containerComponent, String componentInstanceId,
1616                                                                         Map<String, List<CapabilityDefinition>> containerComponentCapabilities,
1617                                                                         ComponentInstanceProperty property,
1618                                                                         CapabilityDefinition capabilityDefinition) {
1619         List<CapabilityDefinition> capabilityDefinitions = containerComponentCapabilities.get(capabilityDefinition.getType());
1620         if (CollectionUtils.isEmpty(capabilityDefinitions)) {
1621             return null;
1622         }
1623         Optional<CapabilityDefinition> capDefToGetProp = capabilityDefinitions.stream()
1624             .filter(cap -> cap.getUniqueId().equals(capabilityDefinition.getUniqueId()) && cap.getPath().size() == 1).findAny();
1625         if (capDefToGetProp.isPresent()) {
1626             return updateInstanceCapabilityProperty(containerComponent, componentInstanceId, property, capDefToGetProp.get());
1627         }
1628         return null;
1629     }
1630
1631     private StorageOperationStatus updateOrAddComponentInstanceProperty(Component containerComponent, String componentInstanceId,
1632                                                                         List<ComponentInstanceProperty> originalComponentInstProps,
1633                                                                         ComponentInstanceProperty property) {
1634         StorageOperationStatus status;
1635         // check if the property already exists or not
1636         Optional<ComponentInstanceProperty> instanceProperty = originalComponentInstProps.stream()
1637             .filter(p -> p.getUniqueId().equals(property.getUniqueId())).findAny();
1638         if (instanceProperty.isPresent()) {
1639             status = updateComponentInstanceProperty(containerComponent, componentInstanceId, property);
1640         } else {
1641             status = addComponentInstanceProperty(containerComponent, componentInstanceId, property);
1642         }
1643         if (status != StorageOperationStatus.OK) {
1644             log.debug("Failed to update instance property {} for instance {} error {} ", property, componentInstanceId, status);
1645         }
1646         return status;
1647     }
1648
1649     private StorageOperationStatus updateOrAddComponentInstanceAttribute(Component containerComponent, String componentInstanceId,
1650                                                                          List<ComponentInstanceAttribute> originalComponentInstProps,
1651                                                                          ComponentInstanceAttribute property) {
1652         StorageOperationStatus status;
1653         // check if the property already exists or not
1654         Optional<ComponentInstanceAttribute> instanceProperty = originalComponentInstProps.stream()
1655             .filter(p -> p.getUniqueId().equals(property.getUniqueId())).findAny();
1656         if (instanceProperty.isPresent()) {
1657             status = updateComponentInstanceAttribute(containerComponent, componentInstanceId, property);
1658         } else {
1659             status = addComponentInstanceAttribute(containerComponent, componentInstanceId, property);
1660         }
1661         if (status != StorageOperationStatus.OK) {
1662             log.debug("Failed to update instance property {} for instance {} error {} ", property, componentInstanceId, status);
1663         }
1664         return status;
1665     }
1666
1667     public StorageOperationStatus updateInstanceCapabilityProperty(Component containerComponent, String componentInstanceId,
1668                                                                    ComponentInstanceProperty property, CapabilityDefinition capabilityDefinition) {
1669         Optional<ComponentInstance> fetchedCIOptional = containerComponent.getComponentInstanceById(componentInstanceId);
1670         if (!fetchedCIOptional.isPresent()) {
1671             return StorageOperationStatus.GENERAL_ERROR;
1672         }
1673         Either<Component, StorageOperationStatus> getComponentRes = getToscaFullElement(fetchedCIOptional.get().getComponentUid());
1674         if (getComponentRes.isRight()) {
1675             return StorageOperationStatus.GENERAL_ERROR;
1676         }
1677         Optional<Component> componentOptional = isNodeServiceProxy(getComponentRes.left().value());
1678         String propOwner;
1679         if (!componentOptional.isPresent()) {
1680             propOwner = componentInstanceId;
1681         } else {
1682             propOwner = fetchedCIOptional.get().getSourceModelUid();
1683         }
1684         StorageOperationStatus status;
1685         StringBuilder sb = new StringBuilder(componentInstanceId);
1686         sb.append(ModelConverter.CAP_PROP_DELIM).append(propOwner).append(ModelConverter.CAP_PROP_DELIM).append(capabilityDefinition.getType())
1687             .append(ModelConverter.CAP_PROP_DELIM).append(capabilityDefinition.getName());
1688         String capKey = sb.toString();
1689         status = updateComponentInstanceCapabiltyProperty(containerComponent, componentInstanceId, capKey, property);
1690         if (status != StorageOperationStatus.OK) {
1691             log.debug("Failed to update instance capability property {} for instance {} error {} ", property, componentInstanceId, status);
1692             return status;
1693         }
1694         return StorageOperationStatus.OK;
1695     }
1696
1697     private Optional<Component> isNodeServiceProxy(Component component) {
1698         if (component.getComponentType().equals(ComponentTypeEnum.SERVICE)) {
1699             return Optional.empty();
1700         }
1701         Resource resource = (Resource) component;
1702         ResourceTypeEnum resType = resource.getResourceType();
1703         if (resType.equals(ResourceTypeEnum.ServiceProxy)) {
1704             return Optional.of(component);
1705         }
1706         return Optional.empty();
1707     }
1708
1709     public StorageOperationStatus associateCapabilitiesToService(Map<String, ListCapabilityDataDefinition> capabilities, String componentId) {
1710         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
1711         if (getVertexEither.isRight()) {
1712             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
1713             return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value());
1714         }
1715         GraphVertex vertex = getVertexEither.left().value();
1716         if (MapUtils.isNotEmpty(capabilities)) {
1717             Either<GraphVertex, StorageOperationStatus> associateElementToData = topologyTemplateOperation
1718                 .associateElementToData(vertex, VertexTypeEnum.CAPABILITIES, EdgeLabelEnum.CAPABILITIES, capabilities);
1719             if (associateElementToData.isRight()) {
1720                 return associateElementToData.right().value();
1721             }
1722         }
1723         return StorageOperationStatus.OK;
1724     }
1725
1726     public StorageOperationStatus associateRequirementsToService(Map<String, ListRequirementDataDefinition> requirements, String componentId) {
1727         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
1728         if (getVertexEither.isRight()) {
1729             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
1730             return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value());
1731         }
1732         GraphVertex vertex = getVertexEither.left().value();
1733         if (MapUtils.isNotEmpty(requirements)) {
1734             Either<GraphVertex, StorageOperationStatus> associateElementToData = topologyTemplateOperation
1735                 .associateElementToData(vertex, VertexTypeEnum.REQUIREMENTS, EdgeLabelEnum.REQUIREMENTS, requirements);
1736             if (associateElementToData.isRight()) {
1737                 return associateElementToData.right().value();
1738             }
1739         }
1740         return StorageOperationStatus.OK;
1741     }
1742
1743     public StorageOperationStatus associateDeploymentArtifactsToInstances(Map<String, Map<String, ArtifactDefinition>> instDeploymentArtifacts,
1744                                                                           Component component, User user) {
1745         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao
1746             .getVertexById(component.getUniqueId(), JsonParseFlagEnum.NoParse);
1747         if (getVertexEither.isRight()) {
1748             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, component.getUniqueId(), getVertexEither.right().value());
1749             return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value());
1750         }
1751         GraphVertex vertex = getVertexEither.left().value();
1752         Map<String, MapArtifactDataDefinition> instArtMap = new HashMap<>();
1753         if (instDeploymentArtifacts != null) {
1754             MapArtifactDataDefinition artifactsMap;
1755             for (Entry<String, Map<String, ArtifactDefinition>> entry : instDeploymentArtifacts.entrySet()) {
1756                 Map<String, ArtifactDefinition> artList = entry.getValue();
1757                 Map<String, ArtifactDataDefinition> artifacts = artList.entrySet().stream()
1758                     .collect(Collectors.toMap(Map.Entry::getKey, e -> new ArtifactDataDefinition(e.getValue())));
1759                 artifactsMap = nodeTemplateOperation
1760                     .prepareInstDeploymentArtifactPerInstance(artifacts, entry.getKey(), user, NodeTemplateOperation.HEAT_VF_ENV_NAME);
1761                 instArtMap.put(entry.getKey(), artifactsMap);
1762             }
1763         }
1764         ModelConverter.setComponentInstancesDeploymentArtifactsToComponent(instArtMap, component);
1765         return topologyTemplateOperation.associateInstDeploymentArtifactsToComponent(vertex, instArtMap);
1766     }
1767
1768     public StorageOperationStatus associateArtifactsToInstances(Map<String, Map<String, ArtifactDefinition>> instArtifacts, Component component) {
1769         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao
1770             .getVertexById(component.getUniqueId(), JsonParseFlagEnum.NoParse);
1771         if (getVertexEither.isRight()) {
1772             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, component.getUniqueId(), getVertexEither.right().value());
1773             return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value());
1774         }
1775         GraphVertex vertex = getVertexEither.left().value();
1776         Map<String, MapArtifactDataDefinition> instArtMap = new HashMap<>();
1777         if (instArtifacts != null) {
1778             MapArtifactDataDefinition artifactsMap;
1779             for (Entry<String, Map<String, ArtifactDefinition>> entry : instArtifacts.entrySet()) {
1780                 Map<String, ArtifactDefinition> artList = entry.getValue();
1781                 Map<String, ArtifactDataDefinition> artifacts = artList.entrySet().stream()
1782                     .collect(Collectors.toMap(Map.Entry::getKey, e -> new ArtifactDataDefinition(e.getValue())));
1783                 artifactsMap = new MapArtifactDataDefinition(artifacts);
1784                 instArtMap.put(entry.getKey(), artifactsMap);
1785             }
1786         }
1787         ModelConverter.setComponentInstancesInformationalArtifactsToComponent(instArtMap, component);
1788         return topologyTemplateOperation.associateInstArtifactsToComponent(vertex, instArtMap);
1789     }
1790
1791     public StorageOperationStatus associateInstAttributeToComponentToInstances(Map<String, List<AttributeDefinition>> instArttributes,
1792                                                                                Component component) {
1793         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao
1794             .getVertexById(component.getUniqueId(), JsonParseFlagEnum.NoParse);
1795         if (getVertexEither.isRight()) {
1796             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, component.getUniqueId(), getVertexEither.right().value());
1797             return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value());
1798         }
1799         GraphVertex vertex = getVertexEither.left().value();
1800         Map<String, MapAttributesDataDefinition> instAttr = new HashMap<>();
1801         if (instArttributes != null) {
1802             MapAttributesDataDefinition attributesMap;
1803             for (Entry<String, List<AttributeDefinition>> entry : instArttributes.entrySet()) {
1804                 final List<AttributeDefinition> value = entry.getValue();
1805                 attributesMap = new MapAttributesDataDefinition();
1806                 attributesMap.setMapToscaDataDefinition(
1807                     value.stream().map(AttributeDefinition::new).collect(Collectors.toMap(AttributeDefinition::getName, e -> e)));
1808                 instAttr.put(entry.getKey(), attributesMap);
1809             }
1810         }
1811         setComponentInstanceAttributesOnComponent(component, instAttr);
1812         return topologyTemplateOperation.associateInstAttributeToComponent(vertex, instAttr);
1813     }
1814
1815     // endregion
1816     private void setComponentInstanceAttributesOnComponent(Component resource, Map<String, MapAttributesDataDefinition> instAttr) {
1817         Map<String, List<ComponentInstanceAttribute>> componentInstancesAttributes = resource.getComponentInstancesAttributes();
1818         if (componentInstancesAttributes == null) {
1819             componentInstancesAttributes = new HashMap<>();
1820         }
1821         componentInstancesAttributes.putAll(ModelConverter.getComponentInstancesAttributes(instAttr));
1822         resource.setComponentInstancesAttributes(componentInstancesAttributes);
1823     }
1824
1825     public StorageOperationStatus associateOrAddCalculatedCapReq(Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> instCapabilties,
1826                                                                  Map<ComponentInstance, Map<String, List<RequirementDefinition>>> instReg,
1827                                                                  Component component) {
1828         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao
1829             .getVertexById(component.getUniqueId(), JsonParseFlagEnum.NoParse);
1830         if (getVertexEither.isRight()) {
1831             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, component.getUniqueId(), getVertexEither.right().value());
1832             return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value());
1833         }
1834         GraphVertex vertex = getVertexEither.left().value();
1835         Map<String, MapListRequirementDataDefinition> calcRequirements = new HashMap<>();
1836         Map<String, MapListCapabilityDataDefinition> calcCapabilty = new HashMap<>();
1837         Map<String, MapCapabilityProperty> calculatedCapabilitiesProperties = new HashMap<>();
1838         if (instCapabilties != null) {
1839             for (Entry<ComponentInstance, Map<String, List<CapabilityDefinition>>> entry : instCapabilties.entrySet()) {
1840                 Map<String, List<CapabilityDefinition>> caps = entry.getValue();
1841                 Map<String, ListCapabilityDataDefinition> mapToscaDataDefinition = new HashMap<>();
1842                 for (Entry<String, List<CapabilityDefinition>> instCapability : caps.entrySet()) {
1843                     mapToscaDataDefinition.put(instCapability.getKey(), new ListCapabilityDataDefinition(
1844                         instCapability.getValue().stream().map(CapabilityDataDefinition::new).collect(Collectors.toList())));
1845                 }
1846                 ComponentInstanceDataDefinition componentInstance = new ComponentInstanceDataDefinition(entry.getKey());
1847                 MapListCapabilityDataDefinition capMap = nodeTemplateOperation
1848                     .prepareCalculatedCapabiltyForNodeType(mapToscaDataDefinition, componentInstance);
1849                 MapCapabilityProperty mapCapabilityProperty = ModelConverter
1850                     .convertToMapOfMapCapabiltyProperties(caps, componentInstance.getUniqueId(), true);
1851                 calcCapabilty.put(entry.getKey().getUniqueId(), capMap);
1852                 calculatedCapabilitiesProperties.put(entry.getKey().getUniqueId(), mapCapabilityProperty);
1853             }
1854         }
1855         if (instReg != null) {
1856             for (Entry<ComponentInstance, Map<String, List<RequirementDefinition>>> entry : instReg.entrySet()) {
1857                 Map<String, List<RequirementDefinition>> req = entry.getValue();
1858                 Map<String, ListRequirementDataDefinition> mapToscaDataDefinition = new HashMap<>();
1859                 for (Entry<String, List<RequirementDefinition>> instReq : req.entrySet()) {
1860                     mapToscaDataDefinition.put(instReq.getKey(), new ListRequirementDataDefinition(
1861                         instReq.getValue().stream().map(RequirementDataDefinition::new).collect(Collectors.toList())));
1862                 }
1863                 MapListRequirementDataDefinition reqMap = nodeTemplateOperation
1864                     .prepareCalculatedRequirementForNodeType(mapToscaDataDefinition, new ComponentInstanceDataDefinition(entry.getKey()));
1865                 String componentInstanceId = entry.getKey().getUniqueId();
1866                 calcRequirements.put(componentInstanceId, reqMap);
1867             }
1868         }
1869         StorageOperationStatus storageOperationStatus = topologyTemplateOperation
1870             .associateOrAddCalcCapReqToComponent(vertex, calcRequirements, calcCapabilty, calculatedCapabilitiesProperties);
1871         updateInstancesCapAndReqOnComponentFromDB(component);
1872         return storageOperationStatus;
1873     }
1874
1875     public StorageOperationStatus updateCalculatedCapabilitiesRequirements(
1876         final Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> instCapabilties,
1877         final Map<ComponentInstance, Map<String, List<RequirementDefinition>>> instReg,
1878         final Component component) {
1879         StorageOperationStatus storageOperationStatus = StorageOperationStatus.OK;
1880         if (instCapabilties != null) {
1881             for (Entry<ComponentInstance, Map<String, List<CapabilityDefinition>>> entry : instCapabilties.entrySet()) {
1882                 final Map<String, List<CapabilityDefinition>> cap = entry.getValue();
1883                 for (List<CapabilityDefinition> capabilityList : cap.values()) {
1884                     for (CapabilityDefinition capability : capabilityList) {
1885                         nodeTemplateOperation.updateComponentInstanceCapabilities(component.getUniqueId(), entry.getKey().getUniqueId(), capability);
1886                     }
1887                 }
1888             }
1889         }
1890         if (instReg != null) {
1891             for (Entry<ComponentInstance, Map<String, List<RequirementDefinition>>> entry : instReg.entrySet()) {
1892                 final Map<String, List<RequirementDefinition>> req = entry.getValue();
1893                 for (List<RequirementDefinition> requirementList : req.values()) {
1894                     for (RequirementDefinition requirement : requirementList) {
1895                         storageOperationStatus = nodeTemplateOperation.updateComponentInstanceRequirement(component.getUniqueId(),
1896                             entry.getKey().getUniqueId(), requirement);
1897                         if (storageOperationStatus != StorageOperationStatus.OK) {
1898                             return storageOperationStatus;
1899                         }
1900                     }
1901                 }
1902             }
1903         }
1904         return storageOperationStatus;
1905     }
1906
1907     private void updateInstancesCapAndReqOnComponentFromDB(Component component) {
1908         ComponentParametersView componentParametersView = new ComponentParametersView(true);
1909         componentParametersView.setIgnoreCapabilities(false);
1910         componentParametersView.setIgnoreRequirements(false);
1911         componentParametersView.setIgnoreCapabiltyProperties(false);
1912         componentParametersView.setIgnoreComponentInstances(false);
1913         Either<Component, StorageOperationStatus> componentEither = getToscaElement(component.getUniqueId(), componentParametersView);
1914         if (componentEither.isRight()) {
1915             throw new StorageException(StorageOperationStatus.NOT_FOUND);
1916         }
1917         Component updatedComponent = componentEither.left().value();
1918         component.setCapabilities(updatedComponent.getCapabilities());
1919         component.setRequirements(updatedComponent.getRequirements());
1920         component.setComponentInstances(updatedComponent.getComponentInstances());
1921     }
1922
1923     private Either<List<Service>, StorageOperationStatus> getLatestVersionNonCheckoutServicesMetadataOnly(Map<GraphPropertyEnum, Object> hasProps,
1924                                                                                                           Map<GraphPropertyEnum, Object> hasNotProps,
1925                                                                                                           String modelName) {
1926         List<Service> services = new ArrayList<>();
1927         List<LifecycleStateEnum> states = new ArrayList<>();
1928         // include props
1929         hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
1930         hasProps.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
1931         if (modelName != null) {
1932             hasProps.put(GraphPropertyEnum.MODEL, modelName);
1933         }
1934         // exclude props
1935         states.add(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
1936         hasNotProps.put(GraphPropertyEnum.STATE, states);
1937         hasNotProps.put(GraphPropertyEnum.IS_DELETED, true);
1938         hasNotProps.put(GraphPropertyEnum.IS_ARCHIVED, true);
1939         return fetchServicesByCriteria(services, hasProps, hasNotProps, modelName);
1940     }
1941
1942     private Either<List<Component>, StorageOperationStatus> getLatestVersionNotAbstractToscaElementsMetadataOnly(final boolean isAbstract,
1943                                                                                                                  final ComponentTypeEnum componentTypeEnum,
1944                                                                                                                  final String internalComponentType,
1945                                                                                                                  final VertexTypeEnum vertexType,
1946                                                                                                                  final String modelName,
1947                                                                                                                  final boolean includeNormativeExtensionModels) {
1948         List<Service> services = null;
1949         Map<GraphPropertyEnum, Object> hasProps = new EnumMap<>(GraphPropertyEnum.class);
1950         Map<GraphPropertyEnum, Object> hasNotProps = new EnumMap<>(GraphPropertyEnum.class);
1951         fillPropsMap(hasProps, hasNotProps, internalComponentType, componentTypeEnum, isAbstract, vertexType, modelName);
1952         Either<List<GraphVertex>, JanusGraphOperationStatus> getRes = janusGraphDao
1953             .getByCriteria(vertexType, hasProps, hasNotProps, JsonParseFlagEnum.ParseMetadata, modelName, includeNormativeExtensionModels);
1954         if (getRes.isRight() && !JanusGraphOperationStatus.NOT_FOUND.equals(getRes.right().value())) {
1955             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getRes.right().value()));
1956         }
1957         // region -> Fetch non checked-out services
1958         if (internalComponentType != null && internalComponentType.toLowerCase().trim().equals(SERVICE) && VertexTypeEnum.NODE_TYPE == vertexType) {
1959             Either<List<Service>, StorageOperationStatus> result = getLatestVersionNonCheckoutServicesMetadataOnly(
1960                 new EnumMap<>(GraphPropertyEnum.class), new EnumMap<>(GraphPropertyEnum.class), modelName);
1961             if (result.isRight()) {
1962                 log.debug("Failed to fetch services for");
1963                 return Either.right(result.right().value());
1964             }
1965             services = result.left().value();
1966             if (log.isTraceEnabled() && isEmpty(services)) {
1967                 log.trace("No relevant services available");
1968             }
1969         }
1970         // endregion
1971         List<Component> nonAbstractLatestComponents = new ArrayList<>();
1972         ComponentParametersView params = new ComponentParametersView(true);
1973         params.setIgnoreAllVersions(false);
1974         if (getRes.isLeft()) {
1975             for (GraphVertex vertexComponent : getRes.left().value()) {
1976                 Either<ToscaElement, StorageOperationStatus> componentRes = topologyTemplateOperation
1977                     .getLightComponent(vertexComponent, componentTypeEnum, params);
1978                 if (componentRes.isRight()) {
1979                     log.debug("Failed to fetch light element for {} error {}", vertexComponent.getUniqueId(), componentRes.right().value());
1980                     return Either.right(componentRes.right().value());
1981                 } else {
1982                     Component component = ModelConverter.convertFromToscaElement(componentRes.left().value());
1983                     nonAbstractLatestComponents.add(component);
1984                 }
1985             }
1986         }
1987         if (CollectionUtils.isNotEmpty(services)) {
1988             nonAbstractLatestComponents.addAll(services);
1989         }
1990         return Either.left(nonAbstractLatestComponents);
1991     }
1992
1993     public Either<ComponentMetadataData, StorageOperationStatus> getLatestComponentMetadataByUuid(String componentUuid, JsonParseFlagEnum parseFlag,
1994                                                                                                   Boolean isHighest) {
1995         Either<ComponentMetadataData, StorageOperationStatus> result;
1996         Map<GraphPropertyEnum, Object> hasProperties = new EnumMap<>(GraphPropertyEnum.class);
1997         hasProperties.put(GraphPropertyEnum.UUID, componentUuid);
1998         if (isHighest != null) {
1999             hasProperties.put(GraphPropertyEnum.IS_HIGHEST_VERSION, isHighest);
2000         }
2001         Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
2002         propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
2003         propertiesNotToMatch.put(GraphPropertyEnum.IS_ARCHIVED, true); //US382674, US382683
2004         Either<List<GraphVertex>, JanusGraphOperationStatus> getRes = janusGraphDao
2005             .getByCriteria(null, hasProperties, propertiesNotToMatch, parseFlag);
2006         if (getRes.isRight()) {
2007             result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getRes.right().value()));
2008         } else {
2009             List<ComponentMetadataData> latestVersionList = getRes.left().value().stream().map(ModelConverter::convertToComponentMetadata)
2010                 .collect(Collectors.toList());
2011             ComponentMetadataData latestVersion = latestVersionList.size() == 1 ? latestVersionList.get(0) : latestVersionList.stream().max(
2012                 (c1, c2) -> Double.compare(Double.parseDouble(c1.getMetadataDataDefinition().getVersion()),
2013                     Double.parseDouble(c2.getMetadataDataDefinition().getVersion()))).get();
2014             result = Either.left(latestVersion);
2015         }
2016         return result;
2017     }
2018
2019     public Either<ComponentMetadataData, StorageOperationStatus> getComponentMetadata(String componentId) {
2020         Either<ComponentMetadataData, StorageOperationStatus> result;
2021         Either<GraphVertex, JanusGraphOperationStatus> getRes = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.ParseMetadata);
2022         if (getRes.isRight()) {
2023             result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getRes.right().value()));
2024         } else {
2025             ComponentMetadataData componentMetadata = ModelConverter.convertToComponentMetadata(getRes.left().value());
2026             result = Either.left(componentMetadata);
2027         }
2028         return result;
2029     }
2030
2031     public Either<List<Component>, StorageOperationStatus> getLatestVersionNotAbstractComponents(boolean isAbstract,
2032                                                                                                  ComponentTypeEnum componentTypeEnum,
2033                                                                                                  String internalComponentType,
2034                                                                                                  List<String> componentUids) {
2035         List<Component> components = new ArrayList<>();
2036         if (componentUids == null) {
2037             Either<List<String>, StorageOperationStatus> componentUidsRes = getComponentUids(isAbstract, componentTypeEnum, internalComponentType);
2038             if (componentUidsRes.isRight()) {
2039                 return Either.right(componentUidsRes.right().value());
2040             }
2041             componentUids = componentUidsRes.left().value();
2042         }
2043         if (!isEmpty(componentUids)) {
2044             for (String componentUid : componentUids) {
2045                 ComponentParametersView componentParametersView = buildComponentViewForNotAbstract();
2046                 if ("vl".equalsIgnoreCase(internalComponentType)) {
2047                     componentParametersView.setIgnoreCapabilities(false);
2048                     componentParametersView.setIgnoreRequirements(false);
2049                 }
2050                 Either<ToscaElement, StorageOperationStatus> getToscaElementRes = nodeTemplateOperation.getToscaElementOperation(componentTypeEnum)
2051                     .getLightComponent(componentUid, componentTypeEnum, componentParametersView);
2052                 if (getToscaElementRes.isRight()) {
2053                     log.debug("Failed to fetch resource for error is {}", getToscaElementRes.right().value());
2054                     return Either.right(getToscaElementRes.right().value());
2055                 }
2056                 Component component = ModelConverter.convertFromToscaElement(getToscaElementRes.left().value());
2057                 nullifySomeComponentProperties(component);
2058                 components.add(component);
2059             }
2060         }
2061         return Either.left(components);
2062     }
2063
2064     public void nullifySomeComponentProperties(Component component) {
2065         component.setContactId(null);
2066         component.setCreationDate(null);
2067         component.setCreatorUserId(null);
2068         component.setCreatorFullName(null);
2069         component.setLastUpdateDate(null);
2070         component.setLastUpdaterUserId(null);
2071         component.setLastUpdaterFullName(null);
2072         component.setNormalizedName(null);
2073     }
2074
2075     private Either<List<String>, StorageOperationStatus> getComponentUids(boolean isAbstract, ComponentTypeEnum componentTypeEnum,
2076                                                                           String internalComponentType) {
2077         Either<List<Component>, StorageOperationStatus> getToscaElementsRes = getLatestVersionNotAbstractMetadataOnly(isAbstract, componentTypeEnum,
2078             internalComponentType, null, false);
2079         if (getToscaElementsRes.isRight()) {
2080             return Either.right(getToscaElementsRes.right().value());
2081         }
2082         List<Component> collection = getToscaElementsRes.left().value();
2083         List<String> componentUids;
2084         if (collection == null) {
2085             componentUids = new ArrayList<>();
2086         } else {
2087             componentUids = collection.stream().map(Component::getUniqueId).collect(Collectors.toList());
2088         }
2089         return Either.left(componentUids);
2090     }
2091
2092     private ComponentParametersView buildComponentViewForNotAbstract() {
2093         ComponentParametersView componentParametersView = new ComponentParametersView();
2094         componentParametersView.disableAll();
2095         componentParametersView.setIgnoreCategories(false);
2096         componentParametersView.setIgnoreAllVersions(false);
2097         return componentParametersView;
2098     }
2099
2100     public Either<Boolean, StorageOperationStatus> validateComponentNameExists(String name, ResourceTypeEnum resourceType,
2101                                                                                ComponentTypeEnum componentType) {
2102         Either<Boolean, StorageOperationStatus> result = validateComponentNameUniqueness(name, resourceType, componentType);
2103         if (result.isLeft()) {
2104             result = Either.left(!result.left().value());
2105         }
2106         return result;
2107     }
2108
2109     public Either<Boolean, StorageOperationStatus> validateComponentNameUniqueness(String name, ResourceTypeEnum resourceType,
2110                                                                                    ComponentTypeEnum componentType) {
2111         String normalizedName = ValidationUtils.normaliseComponentName(name);
2112         Either<List<GraphVertex>, JanusGraphOperationStatus> vertexEither = janusGraphDao
2113             .getByCriteria(getVertexTypeEnum(resourceType), propertiesToMatch(normalizedName, componentType), JsonParseFlagEnum.NoParse);
2114         if (vertexEither.isRight() && vertexEither.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
2115             log.debug("failed to get vertex from graph with property normalizedName: {}", normalizedName);
2116             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(vertexEither.right().value()));
2117         }
2118         return Either.left(CollectionUtils.isEmpty(vertexEither.isLeft() ? vertexEither.left().value() : null));
2119     }
2120
2121     public Either<Boolean, StorageOperationStatus> validateComponentNameAndModelExists(final String resourceName, final String model,
2122                                                                                        final ResourceTypeEnum resourceType,
2123                                                                                        final ComponentTypeEnum componentType) {
2124         Either<Boolean, StorageOperationStatus> result = validateComponentNameAndModelUniqueness(resourceName, model, resourceType, componentType);
2125         if (result.isLeft()) {
2126             result = Either.left(!result.left().value());
2127         }
2128         return result;
2129     }
2130
2131     private Either<Boolean, StorageOperationStatus> validateComponentNameAndModelUniqueness(final String resourceName, final String modelName,
2132                                                                                             final ResourceTypeEnum resourceType,
2133                                                                                             final ComponentTypeEnum componentType) {
2134         final String normalizedName = ValidationUtils.normaliseComponentName(resourceName);
2135         final Either<List<GraphVertex>, JanusGraphOperationStatus> vertexEither = janusGraphDao
2136             .getByCriteria(getVertexTypeEnum(resourceType), propertiesToMatch(normalizedName, componentType), null, null, JsonParseFlagEnum.NoParse,
2137                 modelName);
2138         if (vertexEither.isRight() && vertexEither.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
2139             log.debug("failed to get vertex from graph with property normalizedName: {} and model: {}", normalizedName, modelName);
2140             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(vertexEither.right().value()));
2141         }
2142         return Either.left(CollectionUtils.isEmpty(vertexEither.isLeft() ? vertexEither.left().value().stream()
2143             .collect(Collectors.toList()) : null));
2144     }
2145
2146     private VertexTypeEnum getVertexTypeEnum(final ResourceTypeEnum resourceType) {
2147         return ModelConverter.isAtomicComponent(resourceType) ? VertexTypeEnum.NODE_TYPE : VertexTypeEnum.TOPOLOGY_TEMPLATE;
2148     }
2149
2150     private Map<GraphPropertyEnum, Object> propertiesToMatch(final String normalizedName, final ComponentTypeEnum componentType) {
2151         final Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
2152         properties.put(GraphPropertyEnum.NORMALIZED_NAME, normalizedName);
2153         properties.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
2154         return properties;
2155     }
2156
2157     private void fillNodeTypePropsMap(final Map<GraphPropertyEnum, Object> hasProps, final Map<GraphPropertyEnum, Object> hasNotProps,
2158                                       final String internalComponentType, String modelName) {
2159         final Configuration configuration = ConfigurationManager.getConfigurationManager().getConfiguration();
2160         final List<String> allowedTypes;
2161         if (ComponentTypeEnum.SERVICE.getValue().equalsIgnoreCase(internalComponentType)) {
2162             allowedTypes = containerInstanceTypesData.getServiceAllowedList(modelName);
2163         } else {
2164             final ResourceTypeEnum resourceType = ResourceTypeEnum.getTypeIgnoreCase(internalComponentType);
2165             allowedTypes = containerInstanceTypesData.getComponentAllowedList(ComponentTypeEnum.RESOURCE, resourceType);
2166         }
2167         final List<String> allResourceTypes = configuration.getResourceTypes();
2168         if (allowedTypes == null) {
2169             hasNotProps.put(GraphPropertyEnum.RESOURCE_TYPE, allResourceTypes);
2170             return;
2171         }
2172         if (ResourceTypeEnum.VL.getValue().equalsIgnoreCase(internalComponentType)) {
2173             hasProps.put(GraphPropertyEnum.RESOURCE_TYPE, allowedTypes);
2174         } else {
2175             final List<String> notAllowedTypes = allResourceTypes.stream().filter(s -> !allowedTypes.contains(s)).collect(Collectors.toList());
2176             hasNotProps.put(GraphPropertyEnum.RESOURCE_TYPE, notAllowedTypes);
2177         }
2178     }
2179
2180     private void fillTopologyTemplatePropsMap(Map<GraphPropertyEnum, Object> hasProps, Map<GraphPropertyEnum, Object> hasNotProps,
2181                                               ComponentTypeEnum componentTypeEnum) {
2182         switch (componentTypeEnum) {
2183             case RESOURCE:
2184                 hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
2185                 break;
2186             case SERVICE:
2187                 hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
2188                 break;
2189             default:
2190                 break;
2191         }
2192         hasNotProps.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.CVFC.name());
2193     }
2194
2195     private void fillPropsMap(Map<GraphPropertyEnum, Object> hasProps, Map<GraphPropertyEnum, Object> hasNotProps, String internalComponentType,
2196                               ComponentTypeEnum componentTypeEnum, boolean isAbstract, VertexTypeEnum internalVertexType, String modelName) {
2197         hasNotProps.put(GraphPropertyEnum.STATE, LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name());
2198         hasNotProps.put(GraphPropertyEnum.IS_DELETED, true);
2199         hasNotProps.put(GraphPropertyEnum.IS_ARCHIVED, true);
2200         hasProps.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
2201
2202         if (VertexTypeEnum.NODE_TYPE == internalVertexType) {
2203             hasProps.put(GraphPropertyEnum.IS_ABSTRACT, isAbstract);
2204             if (internalComponentType != null) {
2205                 fillNodeTypePropsMap(hasProps, hasNotProps, internalComponentType, modelName);
2206             }
2207         } else {
2208             fillTopologyTemplatePropsMap(hasProps, hasNotProps, componentTypeEnum);
2209         }
2210     }
2211
2212     private List<VertexTypeEnum> getInternalVertexTypes(ComponentTypeEnum componentTypeEnum, String internalComponentType) {
2213         List<VertexTypeEnum> internalVertexTypes = new ArrayList<>();
2214         if (ComponentTypeEnum.RESOURCE == componentTypeEnum) {
2215             internalVertexTypes.add(VertexTypeEnum.NODE_TYPE);
2216         }
2217         if (ComponentTypeEnum.SERVICE == componentTypeEnum || SERVICE.equalsIgnoreCase(internalComponentType) || VF.equalsIgnoreCase(
2218             internalComponentType)) {
2219             internalVertexTypes.add(VertexTypeEnum.TOPOLOGY_TEMPLATE);
2220         }
2221         return internalVertexTypes;
2222     }
2223
2224     public Either<List<Component>, StorageOperationStatus> getLatestVersionNotAbstractMetadataOnly(boolean isAbstract,
2225                                                                                                    final ComponentTypeEnum componentTypeEnum,
2226                                                                                                    final String internalComponentType,
2227                                                                                                    final String modelName,
2228                                                                                                    final boolean includeNormativeExtensionModels) {
2229         List<VertexTypeEnum> internalVertexTypes = getInternalVertexTypes(componentTypeEnum, internalComponentType);
2230         List<Component> result = new ArrayList<>();
2231         for (VertexTypeEnum vertexType : internalVertexTypes) {
2232             Either<List<Component>, StorageOperationStatus> listByVertexType = getLatestVersionNotAbstractToscaElementsMetadataOnly(isAbstract,
2233                 componentTypeEnum, internalComponentType, vertexType, modelName, includeNormativeExtensionModels);
2234             if (listByVertexType.isRight()) {
2235                 return listByVertexType;
2236             }
2237             result.addAll(listByVertexType.left().value());
2238         }
2239         return Either.left(result);
2240     }
2241
2242     private Either<List<Component>, StorageOperationStatus> getLatestComponentListByUuid(String componentUuid,
2243                                                                                          Map<GraphPropertyEnum, Object> additionalPropertiesToMatch) {
2244         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
2245         if (additionalPropertiesToMatch != null) {
2246             propertiesToMatch.putAll(additionalPropertiesToMatch);
2247         }
2248         propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
2249         return getComponentListByUuid(componentUuid, propertiesToMatch);
2250     }
2251
2252     public Either<Component, StorageOperationStatus> getComponentByUuidAndVersion(String componentUuid, String version) {
2253         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
2254         propertiesToMatch.put(GraphPropertyEnum.UUID, componentUuid);
2255         propertiesToMatch.put(GraphPropertyEnum.VERSION, version);
2256         Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
2257         propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
2258         Either<List<GraphVertex>, JanusGraphOperationStatus> vertexEither = janusGraphDao
2259             .getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll);
2260         if (vertexEither.isRight()) {
2261             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(vertexEither.right().value()));
2262         }
2263         List<GraphVertex> vertexList = vertexEither.isLeft() ? vertexEither.left().value() : null;
2264         if (vertexList == null || vertexList.isEmpty() || vertexList.size() > 1) {
2265             return Either.right(StorageOperationStatus.NOT_FOUND);
2266         }
2267         return getToscaElementByOperation(vertexList.get(0));
2268     }
2269
2270     public Either<List<Component>, StorageOperationStatus> getComponentListByUuid(String componentUuid,
2271                                                                                   Map<GraphPropertyEnum, Object> additionalPropertiesToMatch) {
2272         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
2273         if (additionalPropertiesToMatch != null) {
2274             propertiesToMatch.putAll(additionalPropertiesToMatch);
2275         }
2276         propertiesToMatch.put(GraphPropertyEnum.UUID, componentUuid);
2277         Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
2278         propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
2279         propertiesNotToMatch.put(GraphPropertyEnum.IS_ARCHIVED, true); //US382674, US382683
2280         Either<List<GraphVertex>, JanusGraphOperationStatus> vertexEither = janusGraphDao
2281             .getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll);
2282         if (vertexEither.isRight()) {
2283             log.debug("Couldn't fetch metadata for component with uuid {}, error: {}", componentUuid, vertexEither.right().value());
2284             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(vertexEither.right().value()));
2285         }
2286         List<GraphVertex> vertexList = vertexEither.isLeft() ? vertexEither.left().value() : null;
2287         if (vertexList == null || vertexList.isEmpty()) {
2288             log.debug("Component with uuid {} was not found", componentUuid);
2289             return Either.right(StorageOperationStatus.NOT_FOUND);
2290         }
2291         ArrayList<Component> latestComponents = new ArrayList<>();
2292         for (GraphVertex vertex : vertexList) {
2293             Either<Component, StorageOperationStatus> toscaElementByOperation = getToscaElementByOperation(vertex);
2294             if (toscaElementByOperation.isRight()) {
2295                 log.debug("Could not fetch the following Component by UUID {}", vertex.getUniqueId());
2296                 return Either.right(toscaElementByOperation.right().value());
2297             }
2298             latestComponents.add(toscaElementByOperation.left().value());
2299         }
2300         if (latestComponents.size() > 1) {
2301             for (Component component : latestComponents) {
2302                 if (Boolean.TRUE.equals(component.isHighestVersion())) {
2303                     LinkedList<Component> highestComponent = new LinkedList<>();
2304                     highestComponent.add(component);
2305                     return Either.left(highestComponent);
2306                 }
2307             }
2308         }
2309         return Either.left(latestComponents);
2310     }
2311
2312     public Either<Component, StorageOperationStatus> getLatestServiceByUuid(String serviceUuid) {
2313         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
2314         propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
2315         return getLatestComponentByUuid(serviceUuid, propertiesToMatch);
2316     }
2317
2318     public Either<Component, StorageOperationStatus> getLatestComponentByUuid(String componentUuid) {
2319         return getLatestComponentByUuid(componentUuid, null);
2320     }
2321
2322     public Either<Component, StorageOperationStatus> getLatestComponentByUuid(String componentUuid,
2323                                                                               Map<GraphPropertyEnum, Object> propertiesToMatch) {
2324         Either<List<Component>, StorageOperationStatus> latestVersionListEither = getLatestComponentListByUuid(componentUuid, propertiesToMatch);
2325         if (latestVersionListEither.isRight()) {
2326             return Either.right(latestVersionListEither.right().value());
2327         }
2328         List<Component> latestVersionList = latestVersionListEither.left().value();
2329         if (latestVersionList.isEmpty()) {
2330             return Either.right(StorageOperationStatus.NOT_FOUND);
2331         }
2332         Component component = latestVersionList.size() == 1 ? latestVersionList.get(0)
2333             : latestVersionList.stream().max((c1, c2) -> Double.compare(Double.parseDouble(c1.getVersion()), Double.parseDouble(c2.getVersion())))
2334                 .get();
2335         return Either.left(component);
2336     }
2337
2338     public Either<List<Resource>, StorageOperationStatus> getAllCertifiedResources(boolean isAbstract, Boolean isHighest) {
2339         List<Resource> resources = new ArrayList<>();
2340         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
2341         Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
2342         propertiesToMatch.put(GraphPropertyEnum.IS_ABSTRACT, isAbstract);
2343         if (isHighest != null) {
2344             propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, isHighest);
2345         }
2346         propertiesToMatch.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
2347         propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
2348         propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
2349         Either<List<GraphVertex>, JanusGraphOperationStatus> getResourcesRes = janusGraphDao
2350             .getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll);
2351         if (getResourcesRes.isRight()) {
2352             log.debug("Failed to fetch all certified resources. Status is {}", getResourcesRes.right().value());
2353             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getResourcesRes.right().value()));
2354         }
2355         List<GraphVertex> resourceVerticies = getResourcesRes.left().value();
2356         for (GraphVertex resourceV : resourceVerticies) {
2357             Either<Resource, StorageOperationStatus> getResourceRes = getToscaElement(resourceV);
2358             if (getResourceRes.isRight()) {
2359                 return Either.right(getResourceRes.right().value());
2360             }
2361             resources.add(getResourceRes.left().value());
2362         }
2363         return Either.left(resources);
2364     }
2365
2366     public <T extends Component> Either<T, StorageOperationStatus> getLatestByNameAndVersion(String name, String version,
2367                                                                                              JsonParseFlagEnum parseFlag, String model) {
2368         Either<T, StorageOperationStatus> result;
2369         Map<GraphPropertyEnum, Object> hasProperties = new EnumMap<>(GraphPropertyEnum.class);
2370         Map<GraphPropertyEnum, Object> hasNotProperties = new EnumMap<>(GraphPropertyEnum.class);
2371         hasProperties.put(GraphPropertyEnum.NAME, name);
2372         hasProperties.put(GraphPropertyEnum.VERSION, version);
2373         hasProperties.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
2374         hasNotProperties.put(GraphPropertyEnum.IS_DELETED, true);
2375         Either<List<GraphVertex>, JanusGraphOperationStatus> getResourceRes = janusGraphDao
2376             .getByCriteria(null, hasProperties, hasNotProperties, parseFlag, model);
2377         if (getResourceRes.isRight()) {
2378             JanusGraphOperationStatus status = getResourceRes.right().value();
2379             log.debug("failed to find resource with name {}, version {}. Status is {} ", name, version, status);
2380             result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
2381             return result;
2382         }
2383         return getToscaElementByOperation(getResourceRes.left().value().get(0));
2384     }
2385
2386     public Either<Resource, StorageOperationStatus> getLatestComponentByCsarOrName(ComponentTypeEnum componentType, String csarUUID,
2387                                                                                    String systemName) {
2388         return getLatestComponentByCsarOrName(componentType, csarUUID, systemName, JsonParseFlagEnum.ParseAll);
2389     }
2390
2391     public Either<Resource, StorageOperationStatus> getLatestComponentByCsarOrName(ComponentTypeEnum componentType, String csarUUID,
2392                                                                                    String systemName, JsonParseFlagEnum parseFlag) {
2393         Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
2394         Map<GraphPropertyEnum, Object> propsHasNot = new EnumMap<>(GraphPropertyEnum.class);
2395         props.put(GraphPropertyEnum.CSAR_UUID, csarUUID);
2396         props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
2397         if (componentType != null) {
2398             props.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
2399         }
2400         propsHasNot.put(GraphPropertyEnum.IS_DELETED, true);
2401         GraphVertex resourceMetadataData = null;
2402         List<GraphVertex> resourceMetadataDataList = null;
2403         Either<List<GraphVertex>, JanusGraphOperationStatus> byCsar = janusGraphDao
2404             .getByCriteria(null, props, propsHasNot, JsonParseFlagEnum.ParseMetadata);
2405         if (byCsar.isRight()) {
2406             if (JanusGraphOperationStatus.NOT_FOUND == byCsar.right().value()) {
2407                 // Fix Defect DE256036
2408                 if (StringUtils.isEmpty(systemName)) {
2409                     return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(JanusGraphOperationStatus.NOT_FOUND));
2410                 }
2411                 props.clear();
2412                 props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
2413                 props.put(GraphPropertyEnum.SYSTEM_NAME, systemName);
2414                 Either<List<GraphVertex>, JanusGraphOperationStatus> bySystemname = janusGraphDao
2415                     .getByCriteria(null, props, JsonParseFlagEnum.ParseMetadata);
2416                 if (bySystemname.isRight()) {
2417                     log.debug("getLatestResourceByCsarOrName - Failed to find by system name {}  error {} ", systemName,
2418                         bySystemname.right().value());
2419                     return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(bySystemname.right().value()));
2420                 }
2421                 if (bySystemname.left().value().size() > 2) {
2422                     log.debug(
2423                         "getLatestResourceByCsarOrName - getByCriteria(by system name) must return only 2 latest version, but was returned - {}",
2424                         bySystemname.left().value().size());
2425                     return Either.right(StorageOperationStatus.GENERAL_ERROR);
2426                 }
2427                 resourceMetadataDataList = bySystemname.left().value();
2428                 if (resourceMetadataDataList.size() == 1) {
2429                     resourceMetadataData = resourceMetadataDataList.get(0);
2430                 } else {
2431                     for (GraphVertex curResource : resourceMetadataDataList) {
2432                         if (!((String) curResource.getJsonMetadataField(JsonPresentationFields.LIFECYCLE_STATE)).equals("CERTIFIED")) {
2433                             resourceMetadataData = curResource;
2434                             break;
2435                         }
2436                     }
2437                 }
2438                 if (resourceMetadataData == null) {
2439                     log.debug("getLatestResourceByCsarOrName - getByCriteria(by system name) returned 2 latest CERTIFIED versions");
2440                     return Either.right(StorageOperationStatus.GENERAL_ERROR);
2441                 }
2442                 final Object csarUuid = resourceMetadataData.getJsonMetadataField(JsonPresentationFields.CSAR_UUID);
2443                 if (csarUuid != null && !csarUuid.equals(csarUUID)) {
2444                     log.debug("getLatestResourceByCsarOrName - same system name {} but different csarUUID. exist {} and new {} ", systemName,
2445                         csarUuid, csarUUID);
2446                     // correct error will be returned from create flow. with all
2447
2448                     // correct audit records!!!!!
2449                     return Either.right(StorageOperationStatus.NOT_FOUND);
2450                 }
2451                 return getToscaElement(resourceMetadataData.getUniqueId());
2452             }
2453         } else {
2454             resourceMetadataDataList = byCsar.left().value();
2455             if (resourceMetadataDataList.size() > 2) {
2456                 log.debug("getLatestResourceByCsarOrName - getByCriteria(by csar) must return only 2 latest version, but was returned - {}",
2457                     byCsar.left().value().size());
2458                 return Either.right(StorageOperationStatus.GENERAL_ERROR);
2459             }
2460             if (resourceMetadataDataList.size() == 1) {
2461                 resourceMetadataData = resourceMetadataDataList.get(0);
2462             } else {
2463                 for (GraphVertex curResource : resourceMetadataDataList) {
2464                     if (!((String) curResource.getJsonMetadataField(JsonPresentationFields.LIFECYCLE_STATE)).equals("CERTIFIED")) {
2465                         resourceMetadataData = curResource;
2466                         break;
2467                     }
2468                 }
2469             }
2470             if (resourceMetadataData == null) {
2471                 log.debug("getLatestResourceByCsarOrName - getByCriteria(by csar) returned 2 latest CERTIFIED versions");
2472                 return Either.right(StorageOperationStatus.GENERAL_ERROR);
2473             }
2474             return getToscaElement((String) resourceMetadataData.getJsonMetadataField(JsonPresentationFields.UNIQUE_ID), parseFlag);
2475         }
2476         return null;
2477     }
2478
2479     public Either<Boolean, StorageOperationStatus> validateToscaResourceNameExtends(String templateNameCurrent, String templateNameExtends,
2480                                                                                     String model) {
2481         String currentTemplateNameChecked = templateNameExtends;
2482         while (currentTemplateNameChecked != null && !currentTemplateNameChecked.equalsIgnoreCase(templateNameCurrent)) {
2483             Either<Resource, StorageOperationStatus> latestByToscaResourceName = getLatestByToscaResourceName(currentTemplateNameChecked, model);
2484             if (latestByToscaResourceName.isRight()) {
2485                 return latestByToscaResourceName.right().value() == StorageOperationStatus.NOT_FOUND ? Either.left(false)
2486                     : Either.right(latestByToscaResourceName.right().value());
2487             }
2488             Resource value = latestByToscaResourceName.left().value();
2489             if (value.getDerivedFrom() != null) {
2490                 currentTemplateNameChecked = value.getDerivedFrom().get(0);
2491             } else {
2492                 currentTemplateNameChecked = null;
2493             }
2494         }
2495         return (currentTemplateNameChecked != null && currentTemplateNameChecked.equalsIgnoreCase(templateNameCurrent)) ? Either.left(true)
2496             : Either.left(false);
2497     }
2498
2499     public Either<List<Component>, StorageOperationStatus> fetchMetaDataByResourceType(String resourceType, ComponentParametersView filterBy) {
2500         Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
2501         props.put(GraphPropertyEnum.RESOURCE_TYPE, resourceType);
2502         props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
2503         Map<GraphPropertyEnum, Object> propsHasNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
2504         propsHasNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
2505         Either<List<GraphVertex>, JanusGraphOperationStatus> resourcesByTypeEither = janusGraphDao
2506             .getByCriteria(null, props, propsHasNotToMatch, JsonParseFlagEnum.ParseMetadata);
2507         if (resourcesByTypeEither.isRight()) {
2508             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(resourcesByTypeEither.right().value()));
2509         }
2510         List<GraphVertex> vertexList = resourcesByTypeEither.left().value();
2511         List<Component> components = new ArrayList<>();
2512         for (GraphVertex vertex : vertexList) {
2513             components.add(getToscaElementByOperation(vertex, filterBy).left().value());
2514         }
2515         return Either.left(components);
2516     }
2517
2518     public void commit() {
2519         janusGraphDao.commit();
2520     }
2521
2522     public Either<Service, StorageOperationStatus> updateDistributionStatus(Service service, User user, DistributionStatusEnum distributionStatus) {
2523         Either<GraphVertex, StorageOperationStatus> updateDistributionStatus = topologyTemplateOperation
2524             .updateDistributionStatus(service.getUniqueId(), user, distributionStatus);
2525         if (updateDistributionStatus.isRight()) {
2526             return Either.right(updateDistributionStatus.right().value());
2527         }
2528         GraphVertex serviceV = updateDistributionStatus.left().value();
2529         service.setDistributionStatus(distributionStatus);
2530         service.setLastUpdateDate((Long) serviceV.getJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE));
2531         return Either.left(service);
2532     }
2533
2534     public Either<ComponentMetadataData, StorageOperationStatus> updateComponentLastUpdateDateOnGraph(Component component) {
2535         Either<ComponentMetadataData, StorageOperationStatus> result = null;
2536         GraphVertex serviceVertex;
2537         Either<GraphVertex, JanusGraphOperationStatus> updateRes = null;
2538         Either<GraphVertex, JanusGraphOperationStatus> getRes = janusGraphDao.getVertexById(component.getUniqueId(), JsonParseFlagEnum.ParseMetadata);
2539         if (getRes.isRight()) {
2540             JanusGraphOperationStatus status = getRes.right().value();
2541             log.error("Failed to fetch component {}. status is {}", component.getUniqueId(), status);
2542             result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
2543         }
2544         if (result == null) {
2545             serviceVertex = getRes.left().value();
2546             long lastUpdateDate = System.currentTimeMillis();
2547             serviceVertex.setJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE, lastUpdateDate);
2548             component.setLastUpdateDate(lastUpdateDate);
2549             updateRes = janusGraphDao.updateVertex(serviceVertex);
2550             if (updateRes.isRight()) {
2551                 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(updateRes.right().value()));
2552             }
2553         }
2554         if (result == null) {
2555             result = Either.left(ModelConverter.convertToComponentMetadata(updateRes.left().value()));
2556         }
2557         return result;
2558     }
2559
2560     public HealingJanusGraphDao getJanusGraphDao() {
2561         return janusGraphDao;
2562     }
2563
2564     public Either<List<Service>, StorageOperationStatus> getCertifiedServicesWithDistStatus(Set<DistributionStatusEnum> distStatus) {
2565         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
2566         propertiesToMatch.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
2567         return getServicesWithDistStatus(distStatus, propertiesToMatch);
2568     }
2569
2570     public Either<List<Service>, StorageOperationStatus> getServicesWithDistStatus(Set<DistributionStatusEnum> distStatus,
2571                                                                                    Map<GraphPropertyEnum, Object> additionalPropertiesToMatch) {
2572         List<Service> servicesAll = new ArrayList<>();
2573         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
2574         Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
2575         if (additionalPropertiesToMatch != null && !additionalPropertiesToMatch.isEmpty()) {
2576             propertiesToMatch.putAll(additionalPropertiesToMatch);
2577         }
2578         propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
2579         propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
2580         if (distStatus != null && !distStatus.isEmpty()) {
2581             for (DistributionStatusEnum state : distStatus) {
2582                 propertiesToMatch.put(GraphPropertyEnum.DISTRIBUTION_STATUS, state.name());
2583                 Either<List<Service>, StorageOperationStatus> fetchServicesByCriteria = fetchServicesByCriteria(servicesAll, propertiesToMatch,
2584                     propertiesNotToMatch, null);
2585                 if (fetchServicesByCriteria.isRight()) {
2586                     return fetchServicesByCriteria;
2587                 } else {
2588                     servicesAll = fetchServicesByCriteria.left().value();
2589                 }
2590             }
2591             return Either.left(servicesAll);
2592         } else {
2593             return fetchServicesByCriteria(servicesAll, propertiesToMatch, propertiesNotToMatch, null);
2594         }
2595     }
2596
2597     private Either<List<Service>, StorageOperationStatus> fetchServicesByCriteria(List<Service> servicesAll,
2598                                                                                   Map<GraphPropertyEnum, Object> propertiesToMatch,
2599                                                                                   Map<GraphPropertyEnum, Object> propertiesNotToMatch,
2600                                                                                   String modelName) {
2601         Either<List<GraphVertex>, JanusGraphOperationStatus> getRes = janusGraphDao
2602             .getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll, modelName);
2603         if (getRes.isRight()) {
2604             if (getRes.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
2605                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG,
2606                     "Failed to fetch certified services by match properties {} not match properties {} . Status is {}. ", propertiesToMatch,
2607                     propertiesNotToMatch, getRes.right().value());
2608                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getRes.right().value()));
2609             }
2610         } else {
2611             for (final GraphVertex vertex : getRes.left().value()) {
2612                 Either<ToscaElement, StorageOperationStatus> getServiceRes = topologyTemplateOperation
2613                     .getLightComponent(vertex, ComponentTypeEnum.SERVICE, new ComponentParametersView(true));
2614                 if (getServiceRes.isRight()) {
2615                     CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch certified service {}. Status is {}. ",
2616                         vertex.getJsonMetadataField(JsonPresentationFields.NAME), getServiceRes.right().value());
2617                     return Either.right(getServiceRes.right().value());
2618                 } else {
2619                     servicesAll.add(ModelConverter.convertFromToscaElement(getServiceRes.left().value()));
2620                 }
2621             }
2622         }
2623         return Either.left(servicesAll);
2624     }
2625
2626     public void rollback() {
2627         janusGraphDao.rollback();
2628     }
2629
2630     public StorageOperationStatus addDeploymentArtifactsToInstance(String componentId, ComponentInstance componentInstance,
2631                                                                    Map<String, ArtifactDefinition> finalDeploymentArtifacts) {
2632         Map<String, ArtifactDataDefinition> instDeplArtifacts = finalDeploymentArtifacts.entrySet().stream()
2633             .collect(Collectors.toMap(Map.Entry::getKey, e -> new ArtifactDataDefinition(e.getValue())));
2634         return nodeTemplateOperation.addDeploymentArtifactsToInstance(componentId, componentInstance.getUniqueId(), instDeplArtifacts);
2635     }
2636
2637     public StorageOperationStatus addInformationalArtifactsToInstance(String componentId, ComponentInstance componentInstance,
2638                                                                       Map<String, ArtifactDefinition> artifacts) {
2639         StorageOperationStatus status = StorageOperationStatus.OK;
2640         if (MapUtils.isNotEmpty(artifacts)) {
2641             Map<String, ArtifactDataDefinition> instDeplArtifacts = artifacts.entrySet().stream()
2642                 .collect(Collectors.toMap(Map.Entry::getKey, e -> new ArtifactDataDefinition(e.getValue())));
2643             status = nodeTemplateOperation.addInformationalArtifactsToInstance(componentId, componentInstance.getUniqueId(), instDeplArtifacts);
2644         }
2645         return status;
2646     }
2647
2648     public StorageOperationStatus generateCustomizationUUIDOnInstance(String componentId, String instanceId) {
2649         return nodeTemplateOperation.generateCustomizationUUIDOnInstance(componentId, instanceId);
2650     }
2651
2652     public StorageOperationStatus generateCustomizationUUIDOnInstanceGroup(String componentId, String instanceId, List<String> groupInstances) {
2653         return nodeTemplateOperation.generateCustomizationUUIDOnInstanceGroup(componentId, instanceId, groupInstances);
2654     }
2655
2656     public Either<PropertyDefinition, StorageOperationStatus> addPropertyToComponent(String propertyName, PropertyDefinition newPropertyDefinition,
2657                                                                                      Component component) {
2658         newPropertyDefinition.setName(propertyName);
2659         StorageOperationStatus status = getToscaElementOperation(component)
2660             .addToscaDataToToscaElement(component.getUniqueId(), EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, newPropertyDefinition,
2661                 JsonPresentationFields.NAME);
2662         if (status != StorageOperationStatus.OK) {
2663             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to add the property {} to the component {}. Status is {}. ", propertyName,
2664                 component.getName(), status);
2665             return Either.right(status);
2666         }
2667         ComponentParametersView filter = new ComponentParametersView(true);
2668         filter.setIgnoreProperties(false);
2669         filter.setIgnoreInputs(false);
2670         Either<Component, StorageOperationStatus> getUpdatedComponentRes = getToscaElement(component.getUniqueId(), filter);
2671         if (getUpdatedComponentRes.isRight()) {
2672             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to get updated component {}. Status is {}. ", component.getUniqueId(),
2673                 getUpdatedComponentRes.right().value());
2674             return Either.right(status);
2675         }
2676         PropertyDefinition newProperty = null;
2677         List<PropertyDefinition> properties = (getUpdatedComponentRes.left().value()).getProperties();
2678         if (CollectionUtils.isNotEmpty(properties)) {
2679             Optional<PropertyDefinition> propertyOptional = properties.stream().filter(propertyEntry -> propertyEntry.getName().equals(propertyName))
2680                 .findAny();
2681             if (propertyOptional.isPresent()) {
2682                 newProperty = propertyOptional.get();
2683             }
2684         }
2685         if (newProperty == null) {
2686             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to find recently added property {} on the component {}. Status is {}. ",
2687                 propertyName, component.getUniqueId(), StorageOperationStatus.NOT_FOUND);
2688             return Either.right(StorageOperationStatus.NOT_FOUND);
2689         }
2690         return Either.left(newProperty);
2691     }
2692
2693     public Either<InputDefinition, StorageOperationStatus> addInputToComponent(String inputName, InputDefinition newInputDefinition,
2694                                                                                Component component) {
2695         newInputDefinition.setName(inputName);
2696         StorageOperationStatus status = getToscaElementOperation(component)
2697             .addToscaDataToToscaElement(component.getUniqueId(), EdgeLabelEnum.INPUTS, VertexTypeEnum.INPUTS, newInputDefinition,
2698                 JsonPresentationFields.NAME);
2699         if (status != StorageOperationStatus.OK) {
2700             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to add the input {} to the component {}. Status is {}. ", inputName,
2701                 component.getName(), status);
2702             return Either.right(status);
2703         }
2704         ComponentParametersView filter = new ComponentParametersView(true);
2705         filter.setIgnoreProperties(false);
2706         filter.setIgnoreInputs(false);
2707         Either<Component, StorageOperationStatus> getUpdatedComponentRes = getToscaElement(component.getUniqueId(), filter);
2708         if (getUpdatedComponentRes.isRight()) {
2709             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to get updated component {}. Status is {}. ", component.getUniqueId(),
2710                 getUpdatedComponentRes.right().value());
2711             return Either.right(status);
2712         }
2713         InputDefinition newInput = null;
2714         List<InputDefinition> inputs = (getUpdatedComponentRes.left().value()).getInputs();
2715         if (CollectionUtils.isNotEmpty(inputs)) {
2716             Optional<InputDefinition> inputOptional = inputs.stream().filter(inputEntry -> inputEntry.getName().equals(inputName)).findAny();
2717             if (inputOptional.isPresent()) {
2718                 newInput = inputOptional.get();
2719             }
2720         }
2721         if (newInput == null) {
2722             CommonUtility
2723                 .addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to find recently added input {} " + "on the component {}. Status is {}. ", inputs,
2724                     component.getUniqueId(), StorageOperationStatus.NOT_FOUND);
2725             return Either.right(StorageOperationStatus.NOT_FOUND);
2726         }
2727         return Either.left(newInput);
2728     }
2729
2730     public StorageOperationStatus deletePropertyOfComponent(Component component, String propertyName) {
2731         return getToscaElementOperation(component)
2732             .deleteToscaDataElement(component.getUniqueId(), EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, propertyName,
2733                 JsonPresentationFields.NAME);
2734     }
2735
2736     public StorageOperationStatus deleteAttributeOfResource(Component component, String attributeName) {
2737         return getToscaElementOperation(component)
2738             .deleteToscaDataElement(component.getUniqueId(), EdgeLabelEnum.ATTRIBUTES, VertexTypeEnum.ATTRIBUTES, attributeName,
2739                 JsonPresentationFields.NAME);
2740     }
2741
2742     public StorageOperationStatus deleteInputOfResource(Component resource, String inputName) {
2743         return getToscaElementOperation(resource)
2744             .deleteToscaDataElement(resource.getUniqueId(), EdgeLabelEnum.INPUTS, VertexTypeEnum.INPUTS, inputName, JsonPresentationFields.NAME);
2745     }
2746
2747     public StorageOperationStatus deleteOutputOfResource(final Component resource, final String outputName) {
2748         return getToscaElementOperation(resource)
2749             .deleteToscaDataElement(resource.getUniqueId(), EdgeLabelEnum.OUTPUTS, VertexTypeEnum.OUTPUTS, outputName, JsonPresentationFields.NAME);
2750     }
2751
2752     /**
2753      * Deletes a data type from a component.
2754      *
2755      * @param component    the container which has the data type
2756      * @param dataTypeName the data type name to be deleted
2757      * @return Operation result.
2758      */
2759     public StorageOperationStatus deleteDataTypeOfComponent(Component component, String dataTypeName) {
2760         return getToscaElementOperation(component)
2761             .deleteToscaDataElement(component.getUniqueId(), EdgeLabelEnum.DATA_TYPES, VertexTypeEnum.DATA_TYPES, dataTypeName,
2762                 JsonPresentationFields.NAME);
2763     }
2764
2765     public Either<PropertyDefinition, StorageOperationStatus> updatePropertyOfComponent(Component component,
2766                                                                                         PropertyDefinition newPropertyDefinition) {
2767         Either<Component, StorageOperationStatus> getUpdatedComponentRes = null;
2768         Either<PropertyDefinition, StorageOperationStatus> result = null;
2769         StorageOperationStatus status = getToscaElementOperation(component)
2770             .updateToscaDataOfToscaElement(component.getUniqueId(), EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, newPropertyDefinition,
2771                 JsonPresentationFields.NAME);
2772         if (status != StorageOperationStatus.OK) {
2773             CommonUtility
2774                 .addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_ADD_THE_PROPERTY_TO_THE_RESOURCE_STATUS_IS, newPropertyDefinition.getName(),
2775                     component.getName(), status);
2776             result = Either.right(status);
2777         }
2778         if (result == null) {
2779             ComponentParametersView filter = new ComponentParametersView(true);
2780             filter.setIgnoreProperties(false);
2781             getUpdatedComponentRes = getToscaElement(component.getUniqueId(), filter);
2782             if (getUpdatedComponentRes.isRight()) {
2783                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_GET_UPDATED_RESOURCE_STATUS_IS, component.getUniqueId(),
2784                     getUpdatedComponentRes.right().value());
2785                 result = Either.right(status);
2786             }
2787         }
2788         if (result == null) {
2789             Optional<PropertyDefinition> newProperty = (getUpdatedComponentRes.left().value()).getProperties().stream()
2790                 .filter(p -> p.getName().equals(newPropertyDefinition.getName())).findAny();
2791             if (newProperty.isPresent()) {
2792                 result = Either.left(newProperty.get());
2793             } else {
2794                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_FIND_RECENTLY_ADDED_PROPERTY_ON_THE_RESOURCE_STATUS_IS,
2795                     newPropertyDefinition.getName(), component.getUniqueId(), StorageOperationStatus.NOT_FOUND);
2796                 result = Either.right(StorageOperationStatus.NOT_FOUND);
2797             }
2798         }
2799         return result;
2800     }
2801
2802     public Either<AttributeDefinition, StorageOperationStatus> updateAttributeOfComponent(Component component,
2803                                                                                           AttributeDefinition newPropertyDefinition) {
2804         Either<Component, StorageOperationStatus> getUpdatedComponentRes = null;
2805         Either<AttributeDefinition, StorageOperationStatus> result = null;
2806         StorageOperationStatus status = getToscaElementOperation(component)
2807             .updateToscaDataOfToscaElement(component.getUniqueId(), EdgeLabelEnum.ATTRIBUTES, VertexTypeEnum.ATTRIBUTES, newPropertyDefinition,
2808                 JsonPresentationFields.NAME);
2809         if (status != StorageOperationStatus.OK) {
2810             CommonUtility
2811                 .addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_ADD_THE_PROPERTY_TO_THE_RESOURCE_STATUS_IS, newPropertyDefinition.getName(),
2812                     component.getName(), status);
2813             result = Either.right(status);
2814         }
2815         if (result == null) {
2816             ComponentParametersView filter = new ComponentParametersView(true);
2817             filter.setIgnoreProperties(false);
2818             getUpdatedComponentRes = getToscaElement(component.getUniqueId(), filter);
2819             if (getUpdatedComponentRes.isRight()) {
2820                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_GET_UPDATED_RESOURCE_STATUS_IS, component.getUniqueId(),
2821                     getUpdatedComponentRes.right().value());
2822                 result = Either.right(status);
2823             }
2824         }
2825         if (result == null) {
2826             Optional<AttributeDefinition> newProperty = (getUpdatedComponentRes.left().value()).getAttributes().stream()
2827                 .filter(p -> p.getName().equals(newPropertyDefinition.getName())).findAny();
2828             if (newProperty.isPresent()) {
2829                 result = Either.left(newProperty.get());
2830             } else {
2831                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_FIND_RECENTLY_ADDED_PROPERTY_ON_THE_RESOURCE_STATUS_IS,
2832                     newPropertyDefinition.getName(), component.getUniqueId(), StorageOperationStatus.NOT_FOUND);
2833                 result = Either.right(StorageOperationStatus.NOT_FOUND);
2834             }
2835         }
2836         return result;
2837     }
2838
2839     public Either<AttributeDefinition, StorageOperationStatus> addAttributeOfResource(Component component, AttributeDefinition newAttributeDef) {
2840         Either<Component, StorageOperationStatus> getUpdatedComponentRes = null;
2841         Either<AttributeDefinition, StorageOperationStatus> result = null;
2842         if (newAttributeDef.getUniqueId() == null || newAttributeDef.getUniqueId().isEmpty()) {
2843             String attUniqueId = UniqueIdBuilder.buildAttributeUid(component.getUniqueId(), newAttributeDef.getName());
2844             newAttributeDef.setUniqueId(attUniqueId);
2845             newAttributeDef.setOwnerId(component.getUniqueId());
2846         }
2847         StorageOperationStatus status = getToscaElementOperation(component)
2848             .addToscaDataToToscaElement(component.getUniqueId(), EdgeLabelEnum.ATTRIBUTES, VertexTypeEnum.ATTRIBUTES, newAttributeDef,
2849                 JsonPresentationFields.NAME);
2850         if (status != StorageOperationStatus.OK) {
2851             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_ADD_THE_PROPERTY_TO_THE_RESOURCE_STATUS_IS, newAttributeDef.getName(),
2852                 component.getName(), status);
2853             result = Either.right(status);
2854         }
2855         if (result == null) {
2856             ComponentParametersView filter = new ComponentParametersView(true);
2857             filter.setIgnoreAttributes(false);
2858             getUpdatedComponentRes = getToscaElement(component.getUniqueId(), filter);
2859             if (getUpdatedComponentRes.isRight()) {
2860                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_GET_UPDATED_RESOURCE_STATUS_IS, component.getUniqueId(),
2861                     getUpdatedComponentRes.right().value());
2862                 result = Either.right(status);
2863             }
2864         }
2865         if (result == null) {
2866             Optional<AttributeDefinition> newAttribute = ((Resource) getUpdatedComponentRes.left().value()).getAttributes().stream()
2867                 .filter(p -> p.getName().equals(newAttributeDef.getName())).findAny();
2868             if (newAttribute.isPresent()) {
2869                 result = Either.left(newAttribute.get());
2870             } else {
2871                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_FIND_RECENTLY_ADDED_PROPERTY_ON_THE_RESOURCE_STATUS_IS,
2872                     newAttributeDef.getName(), component.getUniqueId(), StorageOperationStatus.NOT_FOUND);
2873                 result = Either.right(StorageOperationStatus.NOT_FOUND);
2874             }
2875         }
2876         return result;
2877     }
2878
2879     public Either<AttributeDefinition, StorageOperationStatus> updateAttributeOfResource(Component component, AttributeDefinition newAttributeDef) {
2880         Either<Component, StorageOperationStatus> getUpdatedComponentRes = null;
2881         Either<AttributeDefinition, StorageOperationStatus> result = null;
2882         StorageOperationStatus status = getToscaElementOperation(component)
2883             .updateToscaDataOfToscaElement(component.getUniqueId(), EdgeLabelEnum.ATTRIBUTES, VertexTypeEnum.ATTRIBUTES, newAttributeDef,
2884                 JsonPresentationFields.NAME);
2885         if (status != StorageOperationStatus.OK) {
2886             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_ADD_THE_PROPERTY_TO_THE_RESOURCE_STATUS_IS, newAttributeDef.getName(),
2887                 component.getName(), status);
2888             result = Either.right(status);
2889         }
2890         if (result == null) {
2891             ComponentParametersView filter = new ComponentParametersView(true);
2892             filter.setIgnoreAttributes(false);
2893             getUpdatedComponentRes = getToscaElement(component.getUniqueId(), filter);
2894             if (getUpdatedComponentRes.isRight()) {
2895                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_GET_UPDATED_RESOURCE_STATUS_IS, component.getUniqueId(),
2896                     getUpdatedComponentRes.right().value());
2897                 result = Either.right(status);
2898             }
2899         }
2900         if (result == null) {
2901             Optional<AttributeDefinition> newProperty = ((Resource) getUpdatedComponentRes.left().value()).getAttributes().stream()
2902                 .filter(p -> p.getName().equals(newAttributeDef.getName())).findAny();
2903             if (newProperty.isPresent()) {
2904                 result = Either.left(newProperty.get());
2905             } else {
2906                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_FIND_RECENTLY_ADDED_PROPERTY_ON_THE_RESOURCE_STATUS_IS,
2907                     newAttributeDef.getName(), component.getUniqueId(), StorageOperationStatus.NOT_FOUND);
2908                 result = Either.right(StorageOperationStatus.NOT_FOUND);
2909             }
2910         }
2911         return result;
2912     }
2913
2914     public Either<InputDefinition, StorageOperationStatus> updateInputOfComponent(Component component, InputDefinition newInputDefinition) {
2915         Either<Component, StorageOperationStatus> getUpdatedComponentRes = null;
2916         Either<InputDefinition, StorageOperationStatus> result = null;
2917         StorageOperationStatus status = getToscaElementOperation(component)
2918             .updateToscaDataOfToscaElement(component.getUniqueId(), EdgeLabelEnum.INPUTS, VertexTypeEnum.INPUTS, newInputDefinition,
2919                 JsonPresentationFields.NAME);
2920         if (status != StorageOperationStatus.OK) {
2921             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to update the input {} to the component {}. Status is {}. ",
2922                 newInputDefinition.getName(), component.getName(), status);
2923             result = Either.right(status);
2924         }
2925         if (result == null) {
2926             ComponentParametersView filter = new ComponentParametersView(true);
2927             filter.setIgnoreInputs(false);
2928             getUpdatedComponentRes = getToscaElement(component.getUniqueId(), filter);
2929             if (getUpdatedComponentRes.isRight()) {
2930                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_GET_UPDATED_RESOURCE_STATUS_IS, component.getUniqueId(),
2931                     getUpdatedComponentRes.right().value());
2932                 result = Either.right(status);
2933             }
2934         }
2935         if (result == null) {
2936             Optional<InputDefinition> updatedInput = getUpdatedComponentRes.left().value().getInputs().stream()
2937                 .filter(p -> p.getName().equals(newInputDefinition.getName())).findAny();
2938             if (updatedInput.isPresent()) {
2939                 result = Either.left(updatedInput.get());
2940             } else {
2941                 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to find recently updated inputs {} on the resource {}. Status is {}. ",
2942                     newInputDefinition.getName(), component.getUniqueId(), StorageOperationStatus.NOT_FOUND);
2943                 result = Either.right(StorageOperationStatus.NOT_FOUND);
2944             }
2945         }
2946         return result;
2947     }
2948
2949     /**
2950      * method - ename the group instances after referenced container name renamed flow - VF rename -(triggers)-> Group rename
2951      *
2952      * @param containerComponent  - container such as service
2953      * @param componentInstance   - context component
2954      * @param componentInstanceId - id
2955      * @return - successfull/failed status
2956      **/
2957     public Either<StorageOperationStatus, StorageOperationStatus> cleanAndAddGroupInstancesToComponentInstance(Component containerComponent,
2958                                                                                                                ComponentInstance componentInstance,
2959                                                                                                                String componentInstanceId) {
2960         String uniqueId = componentInstance.getUniqueId();
2961         StorageOperationStatus status = nodeTemplateOperation
2962             .deleteToscaDataDeepElementsBlockOfToscaElement(containerComponent.getUniqueId(), EdgeLabelEnum.INST_GROUPS, VertexTypeEnum.INST_GROUPS,
2963                 uniqueId);
2964         if (status != StorageOperationStatus.OK && status != StorageOperationStatus.NOT_FOUND) {
2965             CommonUtility
2966                 .addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to delete group instances for container {}. error {] ", componentInstanceId, status);
2967             return Either.right(status);
2968         }
2969         if (componentInstance.getGroupInstances() != null) {
2970             status = addGroupInstancesToComponentInstance(containerComponent, componentInstance, componentInstance.getGroupInstances());
2971             if (status != StorageOperationStatus.OK && status != StorageOperationStatus.NOT_FOUND) {
2972                 CommonUtility
2973                     .addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to add group instances for container {}. error {] ", componentInstanceId,
2974                         status);
2975                 return Either.right(status);
2976             }
2977         }
2978         return Either.left(status);
2979     }
2980
2981     public StorageOperationStatus addGroupInstancesToComponentInstance(Component containerComponent, ComponentInstance componentInstance,
2982                                                                        List<GroupDefinition> groups,
2983                                                                        Map<String, List<ArtifactDefinition>> groupInstancesArtifacts) {
2984         return nodeTemplateOperation.addGroupInstancesToComponentInstance(containerComponent, componentInstance, groups, groupInstancesArtifacts);
2985     }
2986
2987     public Either<List<GroupDefinition>, StorageOperationStatus> updateGroupsOnComponent(Component component,
2988                                                                                          List<GroupDataDefinition> updatedGroups) {
2989         return groupsOperation.updateGroups(component, updatedGroups, PromoteVersionEnum.MINOR);
2990     }
2991
2992     public Either<List<GroupInstance>, StorageOperationStatus> updateGroupInstancesOnComponent(Component component, String instanceId,
2993                                                                                                List<GroupInstance> updatedGroupInstances) {
2994         return groupsOperation.updateGroupInstances(component, instanceId, updatedGroupInstances);
2995     }
2996
2997     public StorageOperationStatus addGroupInstancesToComponentInstance(Component containerComponent, ComponentInstance componentInstance,
2998                                                                        List<GroupInstance> groupInstances) {
2999         return nodeTemplateOperation.addGroupInstancesToComponentInstance(containerComponent, componentInstance, groupInstances);
3000     }
3001
3002     public StorageOperationStatus addDeploymentArtifactsToComponentInstance(Component containerComponent, ComponentInstance componentInstance,
3003                                                                             Map<String, ArtifactDefinition> deploymentArtifacts) {
3004         return nodeTemplateOperation.addDeploymentArtifactsToComponentInstance(containerComponent, componentInstance, deploymentArtifacts);
3005     }
3006
3007     public StorageOperationStatus updateComponentInstanceProperty(Component containerComponent, String componentInstanceId,
3008                                                                   ComponentInstanceProperty property) {
3009         return nodeTemplateOperation.updateComponentInstanceProperty(containerComponent, componentInstanceId, property);
3010     }
3011
3012     public StorageOperationStatus updateComponentInstanceProperties(Component containerComponent, String componentInstanceId,
3013                                                                     List<ComponentInstanceProperty> properties) {
3014         return nodeTemplateOperation.updateComponentInstanceProperties(containerComponent, componentInstanceId, properties);
3015     }
3016
3017     public StorageOperationStatus updateComponentInstanceAttributes(final Component containerComponent, final String componentInstanceId,
3018                                                                     final List<ComponentInstanceAttribute> attributes) {
3019         return nodeTemplateOperation.updateComponentInstanceAttributes(containerComponent, componentInstanceId, attributes);
3020     }
3021
3022     public StorageOperationStatus addComponentInstanceProperty(Component containerComponent, String componentInstanceId,
3023                                                                ComponentInstanceProperty property) {
3024         return nodeTemplateOperation.addComponentInstanceProperty(containerComponent, componentInstanceId, property);
3025     }
3026
3027     public StorageOperationStatus updateComponentInstanceAttribute(final Component containerComponent, final String componentInstanceId,
3028                                                                    final ComponentInstanceAttribute attribute) {
3029         return nodeTemplateOperation.updateComponentInstanceAttribute(containerComponent, componentInstanceId, attribute);
3030     }
3031
3032     public StorageOperationStatus addComponentInstanceAttribute(Component containerComponent, String componentInstanceId,
3033                                                                 ComponentInstanceAttribute attribute) {
3034         return nodeTemplateOperation.addComponentInstanceAttribute(containerComponent, componentInstanceId, attribute);
3035     }
3036
3037     public StorageOperationStatus updateComponentInstanceInput(Component containerComponent, String componentInstanceId,
3038                                                                ComponentInstanceInput property) {
3039         return nodeTemplateOperation.updateComponentInstanceInput(containerComponent, componentInstanceId, property);
3040     }
3041
3042     public StorageOperationStatus updateComponentInstanceOutput(Component containerComponent, String componentInstanceId,
3043                                                                 ComponentInstanceOutput property) {
3044         return nodeTemplateOperation.updateComponentInstanceOutput(containerComponent, componentInstanceId, property);
3045     }
3046
3047     public StorageOperationStatus updateComponentInstanceInputs(Component containerComponent, String componentInstanceId,
3048                                                                 List<ComponentInstanceInput> instanceInputs) {
3049         return nodeTemplateOperation.updateComponentInstanceInputs(containerComponent, componentInstanceId, instanceInputs);
3050     }
3051
3052     public StorageOperationStatus updateComponentInstanceOutputs(Component containerComponent, String componentInstanceId,
3053                                                                  List<ComponentInstanceOutput> instanceInputs) {
3054         return nodeTemplateOperation.updateComponentInstanceOutputs(containerComponent, componentInstanceId, instanceInputs);
3055     }
3056
3057     public StorageOperationStatus addComponentInstanceInput(Component containerComponent, String componentInstanceId,
3058                                                             ComponentInstanceInput property) {
3059         return nodeTemplateOperation.addComponentInstanceInput(containerComponent, componentInstanceId, property);
3060     }
3061
3062     public StorageOperationStatus addComponentInstanceOutput(Component containerComponent, String componentInstanceId,
3063                                                              ComponentInstanceOutput property) {
3064         return nodeTemplateOperation.addComponentInstanceOutput(containerComponent, componentInstanceId, property);
3065     }
3066
3067     public void setNodeTypeOperation(NodeTypeOperation nodeTypeOperation) {
3068         this.nodeTypeOperation = nodeTypeOperation;
3069     }
3070
3071     public void setTopologyTemplateOperation(TopologyTemplateOperation topologyTemplateOperation) {
3072         this.topologyTemplateOperation = topologyTemplateOperation;
3073     }
3074
3075     public StorageOperationStatus deleteComponentInstanceInputsFromTopologyTemplate(Component containerComponent,
3076                                                                                     List<InputDefinition> inputsToDelete) {
3077         return topologyTemplateOperation.deleteToscaDataElements(containerComponent.getUniqueId(), EdgeLabelEnum.INPUTS,
3078             inputsToDelete.stream().map(PropertyDataDefinition::getName).collect(Collectors.toList()));
3079     }
3080
3081     public StorageOperationStatus deleteComponentInstanceOutputsFromTopologyTemplate(final Component containerComponent,
3082                                                                                      final List<OutputDefinition> outputsToDelete) {
3083         return topologyTemplateOperation.deleteToscaDataElements(containerComponent.getUniqueId(), EdgeLabelEnum.OUTPUTS,
3084             outputsToDelete.stream().map(AttributeDataDefinition::getName).collect(Collectors.toList()));
3085     }
3086
3087     public StorageOperationStatus updateComponentInstanceCapabiltyProperty(Component containerComponent, String componentInstanceUniqueId,
3088                                                                            String capabilityPropertyKey, ComponentInstanceProperty property) {
3089         return nodeTemplateOperation
3090             .updateComponentInstanceCapabilityProperty(containerComponent, componentInstanceUniqueId, capabilityPropertyKey, property);
3091     }
3092
3093     public StorageOperationStatus updateComponentInstanceCapabilityProperties(Component containerComponent, String componentInstanceUniqueId) {
3094         return convertComponentInstanceProperties(containerComponent, componentInstanceUniqueId).map(instanceCapProps -> topologyTemplateOperation
3095                 .updateComponentInstanceCapabilityProperties(containerComponent, componentInstanceUniqueId, instanceCapProps))
3096             .orElse(StorageOperationStatus.NOT_FOUND);
3097     }
3098
3099     public StorageOperationStatus updateComponentInstanceRequirement(String containerComponentId, String componentInstanceUniqueId,
3100                                                                      RequirementDataDefinition requirementDataDefinition) {
3101         return nodeTemplateOperation.updateComponentInstanceRequirement(containerComponentId, componentInstanceUniqueId, requirementDataDefinition);
3102     }
3103
3104     public CapabilityDataDefinition updateComponentInstanceCapability(final String containerComponentId, final String componentInstanceUniqueId,
3105                                                                       final CapabilityDataDefinition capabilityDataDefinition) {
3106
3107         return nodeTemplateOperation.updateComponentInstanceCapabilities(containerComponentId, componentInstanceUniqueId, capabilityDataDefinition);
3108     }
3109
3110     public StorageOperationStatus updateComponentInstanceInterfaces(Component containerComponent, String componentInstanceUniqueId) {
3111         MapInterfaceDataDefinition mapInterfaceDataDefinition = convertComponentInstanceInterfaces(containerComponent, componentInstanceUniqueId);
3112         return topologyTemplateOperation.updateComponentInstanceInterfaces(containerComponent, componentInstanceUniqueId, mapInterfaceDataDefinition);
3113     }
3114
3115     public StorageOperationStatus updateComponentInterfaces(final Component component, final String componentInterfaceUpdatedKey) {
3116         MapInterfaceDataDefinition mapInterfaceDataDefinition = convertComponentInterfaces(component.getInterfaces());
3117         return topologyTemplateOperation.updateComponentInterfaces(component.getUniqueId(), mapInterfaceDataDefinition, componentInterfaceUpdatedKey);
3118     }
3119
3120     public Either<InterfaceDefinition, StorageOperationStatus> addInterfaceToComponent(final String interfaceName,
3121                                                                                        final InterfaceDefinition interfaceDefinition,
3122                                                                                        final Component component) {
3123
3124         final boolean match = component.getInterfaces().keySet().stream().anyMatch(s -> s.equals(interfaceName));
3125         StorageOperationStatus status = StorageOperationStatus.OK;
3126         final ToscaElementOperation toscaElementOperation = getToscaElementOperation(component);
3127         if (match) {
3128             status = toscaElementOperation.updateToscaDataOfToscaElement(component.getUniqueId(), EdgeLabelEnum.INTERFACE_ARTIFACTS,
3129                 VertexTypeEnum.INTERFACE_ARTIFACTS, interfaceDefinition, JsonPresentationFields.TYPE);
3130         } else {
3131             status = toscaElementOperation.addToscaDataToToscaElement(component.getUniqueId(), EdgeLabelEnum.INTERFACE_ARTIFACTS,
3132                 VertexTypeEnum.INTERFACE_ARTIFACTS, interfaceDefinition, JsonPresentationFields.TYPE);
3133         }
3134
3135         if (status != StorageOperationStatus.OK) {
3136             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to add the interface {} to the component {}. Status is {}. ",
3137                 interfaceName, component.getName(), status);
3138             return Either.right(status);
3139         }
3140         final ComponentParametersView filter = new ComponentParametersView(true);
3141         filter.setIgnoreInterfaces(false);
3142         filter.setIgnoreInterfaceInstances(false);
3143         final Either<Component, StorageOperationStatus> getUpdatedComponentRes = getToscaElement(component.getUniqueId(), filter);
3144         if (getUpdatedComponentRes.isRight()) {
3145             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to get updated component {}. Status is {}. ",
3146                 component.getUniqueId(), getUpdatedComponentRes.right().value());
3147             return Either.right(getUpdatedComponentRes.right().value());
3148         }
3149         InterfaceDefinition newInterfaceDefinition = null;
3150         final Map<String, InterfaceDefinition> interfaces = (getUpdatedComponentRes.left().value()).getInterfaces();
3151         if (MapUtils.isNotEmpty(interfaces)) {
3152             final Optional<String> interfaceNameOptional = interfaces.keySet().stream().filter(key -> key.equals(interfaceName)).findAny();
3153             if (interfaceNameOptional.isPresent()) {
3154                 newInterfaceDefinition = interfaces.get(interfaceNameOptional.get());
3155             }
3156         }
3157         if (newInterfaceDefinition == null) {
3158             CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to find recently added interface {} on the component {}. Status is {}. ",
3159                 interfaceName, component.getUniqueId(), StorageOperationStatus.NOT_FOUND);
3160             return Either.right(StorageOperationStatus.NOT_FOUND);
3161         }
3162         return Either.left(newInterfaceDefinition);
3163     }
3164
3165     public StorageOperationStatus updateComponentCalculatedCapabilitiesProperties(Component containerComponent) {
3166         Map<String, MapCapabilityProperty> mapCapabiltyPropertyMap = convertComponentCapabilitiesProperties(containerComponent);
3167         return nodeTemplateOperation.overrideComponentCapabilitiesProperties(containerComponent, mapCapabiltyPropertyMap);
3168     }
3169
3170     public StorageOperationStatus deleteAllCalculatedCapabilitiesRequirements(String topologyTemplateId) {
3171         StorageOperationStatus status = topologyTemplateOperation
3172             .removeToscaData(topologyTemplateId, EdgeLabelEnum.CALCULATED_CAPABILITIES, VertexTypeEnum.CALCULATED_CAPABILITIES);
3173         if (status == StorageOperationStatus.OK) {
3174             status = topologyTemplateOperation
3175                 .removeToscaData(topologyTemplateId, EdgeLabelEnum.CALCULATED_REQUIREMENTS, VertexTypeEnum.CALCULATED_REQUIREMENTS);
3176         }
3177         if (status == StorageOperationStatus.OK) {
3178             status = topologyTemplateOperation
3179                 .removeToscaData(topologyTemplateId, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES, VertexTypeEnum.CALCULATED_CAP_PROPERTIES);
3180         }
3181         return status;
3182     }
3183
3184     public Either<Component, StorageOperationStatus> shouldUpgradeToLatestDerived(Resource clonedResource) {
3185         String componentId = clonedResource.getUniqueId();
3186         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
3187         if (getVertexEither.isRight()) {
3188             log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
3189             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
3190         }
3191         GraphVertex nodeTypeV = getVertexEither.left().value();
3192         ToscaElement toscaElementToUpdate = ModelConverter.convertToToscaElement(clonedResource);
3193         Either<ToscaElement, StorageOperationStatus> shouldUpdateDerivedVersion = nodeTypeOperation
3194             .shouldUpdateDerivedVersion(toscaElementToUpdate, nodeTypeV);
3195         if (shouldUpdateDerivedVersion.isRight() && StorageOperationStatus.OK != shouldUpdateDerivedVersion.right().value()) {
3196             log.debug("Failed to update derived version for node type {} derived {}, error: {}", componentId, clonedResource.getDerivedFrom().get(0),
3197                 shouldUpdateDerivedVersion.right().value());
3198             return Either.right(shouldUpdateDerivedVersion.right().value());
3199         }
3200         if (shouldUpdateDerivedVersion.isLeft()) {
3201             return Either.left(ModelConverter.convertFromToscaElement(shouldUpdateDerivedVersion.left().value()));
3202         }
3203         return Either.left(clonedResource);
3204     }
3205
3206     /**
3207      * Returns list of ComponentInstanceProperty belonging to component instance capability specified by name, type and ownerId
3208      */
3209     public Either<List<ComponentInstanceProperty>, StorageOperationStatus> getComponentInstanceCapabilityProperties(String componentId,
3210                                                                                                                     String instanceId,
3211                                                                                                                     String capabilityName,
3212                                                                                                                     String capabilityType,
3213                                                                                                                     String ownerId) {
3214         return topologyTemplateOperation.getComponentInstanceCapabilityProperties(componentId, instanceId, capabilityName, capabilityType, ownerId);
3215     }
3216
3217     private MapInterfaceDataDefinition convertComponentInstanceInterfaces(Component currComponent, String componentInstanceId) {
3218         MapInterfaceDataDefinition mapInterfaceDataDefinition = new MapInterfaceDataDefinition();
3219         List<ComponentInstanceInterface> componentInterface = currComponent.getComponentInstancesInterfaces().get(componentInstanceId);
3220         if (CollectionUtils.isNotEmpty(componentInterface)) {
3221             componentInterface.stream().forEach(interfaceDef -> mapInterfaceDataDefinition.put(interfaceDef.getUniqueId(), interfaceDef));
3222         }
3223         return mapInterfaceDataDefinition;
3224     }
3225
3226     private MapInterfaceDataDefinition convertComponentInterfaces(final Map<String, InterfaceDefinition> interfaces) {
3227         final MapInterfaceDataDefinition mapInterfaceDataDefinition = new MapInterfaceDataDefinition();
3228         if (MapUtils.isNotEmpty(interfaces)) {
3229             interfaces.values().stream().forEach(interfaceDef -> mapInterfaceDataDefinition.put(interfaceDef.getType(), interfaceDef));
3230         }
3231         return mapInterfaceDataDefinition;
3232     }
3233
3234     private Map<String, MapCapabilityProperty> convertComponentCapabilitiesProperties(Component currComponent) {
3235         Map<String, MapCapabilityProperty> map = ModelConverter.extractCapabilityPropertiesFromGroups(currComponent.getGroups(), true);
3236         map.putAll(ModelConverter.extractCapabilityProperteisFromInstances(currComponent.getComponentInstances(), true));
3237         return map;
3238     }
3239
3240     private Optional<MapCapabilityProperty> convertComponentInstanceProperties(Component component, String instanceId) {
3241         return component.fetchInstanceById(instanceId)
3242             .map(ci -> ModelConverter.convertToMapOfMapCapabilityProperties(ci.getCapabilities(), instanceId, ci.getOriginType().isAtomicType()));
3243     }
3244
3245     public Either<PolicyDefinition, StorageOperationStatus> associatePolicyToComponent(String componentId, PolicyDefinition policyDefinition,
3246                                                                                        int counter) {
3247         Either<PolicyDefinition, StorageOperationStatus> result = null;
3248         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither;
3249         getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.ParseMetadata);
3250         if (getVertexEither.isRight()) {
3251             log.error(COULDNT_FETCH_A_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
3252             result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
3253         } else {
3254             if (getVertexEither.left().value().getLabel() != VertexTypeEnum.TOPOLOGY_TEMPLATE) {
3255                 log.error("Policy association to component of Tosca type {} is not allowed. ", getVertexEither.left().value().getLabel());
3256                 result = Either.right(StorageOperationStatus.BAD_REQUEST);
3257             }
3258         }
3259         if (result == null) {
3260             StorageOperationStatus status = topologyTemplateOperation
3261                 .addPolicyToToscaElement(getVertexEither.left().value(), policyDefinition, counter);
3262             if (status != StorageOperationStatus.OK) {
3263                 return Either.right(status);
3264             }
3265         }
3266         if (result == null) {
3267             result = Either.left(policyDefinition);
3268         }
3269         return result;
3270     }
3271
3272     public StorageOperationStatus associatePoliciesToComponent(String componentId, List<PolicyDefinition> policies) {
3273         log.debug("#associatePoliciesToComponent - associating policies for component {}.", componentId);
3274         return janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.ParseMetadata)
3275             .either(containerVertex -> topologyTemplateOperation.addPoliciesToToscaElement(containerVertex, policies),
3276                 DaoStatusConverter::convertJanusGraphStatusToStorageStatus);
3277     }
3278
3279     public Either<PolicyDefinition, StorageOperationStatus> updatePolicyOfComponent(String componentId, PolicyDefinition policyDefinition,
3280                                                                                     PromoteVersionEnum promoteVersionEnum) {
3281         Either<PolicyDefinition, StorageOperationStatus> result = null;
3282         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither;
3283         getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
3284         if (getVertexEither.isRight()) {
3285             log.error(COULDNT_FETCH_A_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
3286             result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
3287         }
3288         if (result == null) {
3289             policyDefinition.setVersion(GroupUtils.updateVersion(promoteVersionEnum, policyDefinition.getVersion()));
3290             StorageOperationStatus status = topologyTemplateOperation.updatePolicyOfToscaElement(getVertexEither.left().value(), policyDefinition);
3291             if (status != StorageOperationStatus.OK) {
3292                 return Either.right(status);
3293             }
3294         }
3295         if (result == null) {
3296             result = Either.left(policyDefinition);
3297         }
3298         return result;
3299     }
3300
3301     public StorageOperationStatus updatePoliciesOfComponent(String componentId, List<PolicyDefinition> policyDefinition) {
3302         log.debug("#updatePoliciesOfComponent - updating policies for component {}", componentId);
3303         return janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse).right()
3304             .map(DaoStatusConverter::convertJanusGraphStatusToStorageStatus)
3305             .either(containerVertex -> topologyTemplateOperation.updatePoliciesOfToscaElement(containerVertex, policyDefinition), err -> err);
3306     }
3307
3308     public StorageOperationStatus removePolicyFromComponent(String componentId, String policyId) {
3309         StorageOperationStatus status = null;
3310         Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
3311         if (getVertexEither.isRight()) {
3312             log.error(COULDNT_FETCH_A_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
3313             status = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value());
3314         }
3315         if (status == null) {
3316             status = topologyTemplateOperation.removePolicyFromToscaElement(getVertexEither.left().value(), policyId);
3317         }
3318         return status;
3319     }
3320
3321     public boolean canAddGroups(String componentId) {
3322         GraphVertex vertex = janusGraphDao.getVertexById(componentId).left().on(this::onJanusGraphError);
3323         return topologyTemplateOperation.hasEdgeOfType(vertex, EdgeLabelEnum.GROUPS);
3324     }
3325
3326     GraphVertex onJanusGraphError(JanusGraphOperationStatus toe) {
3327         throw new StorageException(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(toe));
3328     }
3329
3330     public CatalogUpdateTimestamp updateCatalogTimes() {
3331         long now = System.currentTimeMillis();
3332         GraphVertex catalogRoot = janusGraphDao.getVertexByLabel(VertexTypeEnum.CATALOG_ROOT).left().on(this::onJanusGraphError);
3333         Long currentTime = (Long) catalogRoot.getMetadataProperty(GraphPropertyEnum.CURRENT_CATALOG_UPDATE_TIME);
3334         catalogRoot.addMetadataProperty(GraphPropertyEnum.PREV_CATALOG_UPDATE_TIME, currentTime);
3335         catalogRoot.addMetadataProperty(GraphPropertyEnum.CURRENT_CATALOG_UPDATE_TIME, now);
3336         janusGraphDao.updateVertex(catalogRoot).left().on(this::onJanusGraphError);
3337         return new CatalogUpdateTimestamp(currentTime, now);
3338     }
3339
3340     public CatalogUpdateTimestamp getCatalogTimes() {
3341         GraphVertex catalogRoot = janusGraphDao.getVertexByLabel(VertexTypeEnum.CATALOG_ROOT).left().on(this::onJanusGraphError);
3342         Long currentTime = (Long) catalogRoot.getMetadataProperty(GraphPropertyEnum.CURRENT_CATALOG_UPDATE_TIME);
3343         Long prevTime = (Long) catalogRoot.getMetadataProperty(GraphPropertyEnum.PREV_CATALOG_UPDATE_TIME);
3344         return new CatalogUpdateTimestamp(prevTime == null ? 0 : prevTime.longValue(), currentTime == null ? 0 : currentTime.longValue());
3345     }
3346
3347     public void updateNamesOfCalculatedCapabilitiesRequirements(String componentId) {
3348         topologyTemplateOperation.updateNamesOfCalculatedCapabilitiesRequirements(componentId, getTopologyTemplate(componentId));
3349     }
3350
3351     public void revertNamesOfCalculatedCapabilitiesRequirements(String componentId) {
3352         topologyTemplateOperation.revertNamesOfCalculatedCapabilitiesRequirements(componentId, getTopologyTemplate(componentId));
3353     }
3354
3355     private TopologyTemplate getTopologyTemplate(String componentId) {
3356         return (TopologyTemplate) topologyTemplateOperation.getToscaElement(componentId, getFilterComponentWithCapProperties()).left()
3357             .on(this::throwStorageException);
3358     }
3359
3360     private ComponentParametersView getFilterComponentWithCapProperties() {
3361         ComponentParametersView filter = new ComponentParametersView();
3362         filter.setIgnoreCapabiltyProperties(false);
3363         return filter;
3364     }
3365
3366     private ToscaElement throwStorageException(StorageOperationStatus status) {
3367         throw new StorageException(status);
3368     }
3369
3370     public Either<Boolean, StorageOperationStatus> isComponentInUse(String componentId) {
3371         final List<EdgeLabelEnum> forbiddenEdgeLabelEnums = Arrays
3372             .asList(EdgeLabelEnum.INSTANCE_OF, EdgeLabelEnum.PROXY_OF, EdgeLabelEnum.ALLOTTED_OF);
3373         Either<GraphVertex, JanusGraphOperationStatus> vertexById = janusGraphDao.getVertexById(componentId);
3374         if (vertexById.isLeft()) {
3375             for (EdgeLabelEnum edgeLabelEnum : forbiddenEdgeLabelEnums) {
3376                 Iterator<Edge> edgeItr = vertexById.left().value().getVertex().edges(Direction.IN, edgeLabelEnum.name());
3377                 if (edgeItr != null && edgeItr.hasNext()) {
3378                     return Either.left(true);
3379                 }
3380             }
3381         }
3382         return Either.left(false);
3383     }
3384
3385     public Either<List<Component>, StorageOperationStatus> getComponentListByInvariantUuid(String componentInvariantUuid,
3386                                                                                            Map<GraphPropertyEnum, Object> additionalPropertiesToMatch) {
3387         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
3388         if (MapUtils.isNotEmpty(additionalPropertiesToMatch)) {
3389             propertiesToMatch.putAll(additionalPropertiesToMatch);
3390         }
3391         propertiesToMatch.put(GraphPropertyEnum.INVARIANT_UUID, componentInvariantUuid);
3392         Either<List<GraphVertex>, JanusGraphOperationStatus> vertexEither = janusGraphDao
3393             .getByCriteria(null, propertiesToMatch, JsonParseFlagEnum.ParseMetadata);
3394         if (vertexEither.isRight()) {
3395             log.debug("Couldn't fetch metadata for component with type {} and invariantUUId {}, error: {}", componentInvariantUuid,
3396                 vertexEither.right().value());
3397             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(vertexEither.right().value()));
3398         }
3399         List<GraphVertex> vertexList = vertexEither.isLeft() ? vertexEither.left().value() : null;
3400         if (vertexList == null || vertexList.isEmpty()) {
3401             log.debug("Component with invariantUUId {} was not found", componentInvariantUuid);
3402             return Either.right(StorageOperationStatus.NOT_FOUND);
3403         }
3404         ArrayList<Component> components = new ArrayList<>();
3405         for (GraphVertex vertex : vertexList) {
3406             Either<Component, StorageOperationStatus> toscaElementByOperation = getToscaElementByOperation(vertex);
3407             if (toscaElementByOperation.isRight()) {
3408                 log.debug("Could not fetch the following Component by Invariant UUID {}", vertex.getUniqueId());
3409                 return Either.right(toscaElementByOperation.right().value());
3410             }
3411             components.add(toscaElementByOperation.left().value());
3412         }
3413         return Either.left(components);
3414     }
3415
3416     public Either<List<Component>, StorageOperationStatus> getParentComponents(String componentId) {
3417         List<Component> parentComponents = new ArrayList<>();
3418         final List<EdgeLabelEnum> relationEdgeLabelEnums = Arrays.asList(EdgeLabelEnum.INSTANCE_OF, EdgeLabelEnum.PROXY_OF);
3419         Either<GraphVertex, JanusGraphOperationStatus> vertexById = janusGraphDao.getVertexById(componentId);
3420         if (vertexById.isLeft()) {
3421             for (EdgeLabelEnum edgeLabelEnum : relationEdgeLabelEnums) {
3422                 Either<GraphVertex, JanusGraphOperationStatus> parentVertexEither = janusGraphDao
3423                     .getParentVertex(vertexById.left().value(), edgeLabelEnum, JsonParseFlagEnum.ParseJson);
3424                 if (parentVertexEither.isLeft()) {
3425                     Either<Component, StorageOperationStatus> componentEither = getToscaElement(parentVertexEither.left().value().getUniqueId());
3426                     if (componentEither.isLeft()) {
3427                         parentComponents.add(componentEither.left().value());
3428                     }
3429                 }
3430             }
3431         }
3432         return Either.left(parentComponents);
3433     }
3434
3435     public void updateCapReqPropertiesOwnerId(String componentId) {
3436         topologyTemplateOperation.updateCapReqPropertiesOwnerId(componentId, getTopologyTemplate(componentId));
3437     }
3438
3439     public <T extends Component> Either<T, StorageOperationStatus> getLatestByServiceName(String serviceName) {
3440         return getLatestByName(GraphPropertyEnum.NAME, serviceName, null);
3441     }
3442 }