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