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