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