[SDC] rebase 1710 code
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / operations / impl / ComponentOperation.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.model.operations.impl;
22
23 import java.time.Duration;
24 import java.time.Instant;
25 import java.util.*;
26 import java.util.Map.Entry;
27 import java.util.concurrent.LinkedBlockingQueue;
28 import java.util.function.Function;
29 import java.util.regex.Pattern;
30 import java.util.stream.Collectors;
31 import java.util.stream.Stream;
32
33 import org.apache.commons.lang.StringUtils;
34 import org.apache.commons.lang3.tuple.ImmutablePair;
35 import org.apache.commons.lang3.tuple.ImmutableTriple;
36 import org.apache.commons.lang3.tuple.Pair;
37 import org.apache.tinkerpop.gremlin.structure.Direction;
38 import org.apache.tinkerpop.gremlin.structure.Edge;
39 import org.apache.tinkerpop.gremlin.structure.Vertex;
40 import org.openecomp.sdc.be.config.ConfigurationManager;
41 import org.openecomp.sdc.be.dao.api.ActionStatus;
42 import org.openecomp.sdc.be.dao.graph.GraphElementFactory;
43 import org.openecomp.sdc.be.dao.graph.datatype.GraphEdge;
44 import org.openecomp.sdc.be.dao.graph.datatype.GraphElementTypeEnum;
45 import org.openecomp.sdc.be.dao.graph.datatype.GraphNode;
46 import org.openecomp.sdc.be.dao.graph.datatype.GraphRelation;
47 import org.openecomp.sdc.be.dao.graph.datatype.RelationEndPoint;
48 import org.openecomp.sdc.be.dao.neo4j.GraphEdgeLabels;
49 import org.openecomp.sdc.be.dao.neo4j.GraphEdgePropertiesDictionary;
50 import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary;
51 import org.openecomp.sdc.be.dao.titan.QueryType;
52 import org.openecomp.sdc.be.dao.titan.TitanGenericDao;
53 import org.openecomp.sdc.be.dao.titan.TitanGraphClient;
54 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
55 import org.openecomp.sdc.be.datatypes.category.GroupingDataDefinition;
56 import org.openecomp.sdc.be.datatypes.components.ComponentMetadataDataDefinition;
57 import org.openecomp.sdc.be.datatypes.components.ResourceMetadataDataDefinition;
58 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
59 import org.openecomp.sdc.be.datatypes.enums.FilterKeyEnum;
60 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
61 import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
62 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
63 import org.openecomp.sdc.be.model.AdditionalInformationDefinition;
64 import org.openecomp.sdc.be.model.ArtifactDefinition;
65 import org.openecomp.sdc.be.model.CapabilityDefinition;
66 import org.openecomp.sdc.be.model.Component;
67 import org.openecomp.sdc.be.model.ComponentInstance;
68 import org.openecomp.sdc.be.model.ComponentInstanceInput;
69 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
70 import org.openecomp.sdc.be.model.ComponentParametersView;
71 import org.openecomp.sdc.be.model.DataTypeDefinition;
72 import org.openecomp.sdc.be.model.GroupDefinition;
73 import org.openecomp.sdc.be.model.InputDefinition;
74 import org.openecomp.sdc.be.model.LifecycleStateEnum;
75 import org.openecomp.sdc.be.model.PropertyDefinition;
76 import org.openecomp.sdc.be.model.PropertyDefinition.PropertyNames;
77 import org.openecomp.sdc.be.model.RequirementCapabilityRelDef;
78 import org.openecomp.sdc.be.model.RequirementDefinition;
79 import org.openecomp.sdc.be.model.User;
80 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
81 import org.openecomp.sdc.be.model.cache.ComponentCache;
82 import org.openecomp.sdc.be.model.category.CategoryDefinition;
83 import org.openecomp.sdc.be.model.category.GroupingDefinition;
84 import org.openecomp.sdc.be.model.category.SubCategoryDefinition;
85 import org.openecomp.sdc.be.model.operations.api.IAdditionalInformationOperation;
86 import org.openecomp.sdc.be.model.operations.api.IArtifactOperation;
87 import org.openecomp.sdc.be.model.operations.api.ICapabilityOperation;
88 import org.openecomp.sdc.be.model.operations.api.IElementOperation;
89 import org.openecomp.sdc.be.model.operations.api.IRequirementOperation;
90 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
91 import org.openecomp.sdc.be.model.operations.api.ToscaDefinitionPathCalculator;
92 import org.openecomp.sdc.be.resources.data.ArtifactData;
93 import org.openecomp.sdc.be.resources.data.CapabilityData;
94 import org.openecomp.sdc.be.resources.data.ComponentMetadataData;
95 import org.openecomp.sdc.be.resources.data.ProductMetadataData;
96 import org.openecomp.sdc.be.resources.data.RequirementData;
97 import org.openecomp.sdc.be.resources.data.ResourceMetadataData;
98 import org.openecomp.sdc.be.resources.data.ServiceMetadataData;
99 import org.openecomp.sdc.be.resources.data.TagData;
100 import org.openecomp.sdc.be.resources.data.UniqueIdData;
101 import org.openecomp.sdc.be.resources.data.UserData;
102 import org.openecomp.sdc.be.resources.data.category.CategoryData;
103 import org.openecomp.sdc.be.resources.data.category.GroupingData;
104 import org.openecomp.sdc.be.resources.data.category.SubCategoryData;
105 import org.openecomp.sdc.be.utils.CommonBeUtils;
106 import org.openecomp.sdc.be.workers.Job;
107 import org.openecomp.sdc.be.workers.Manager;
108 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
109 import org.openecomp.sdc.common.util.StreamUtils;
110 import org.openecomp.sdc.common.util.ValidationUtils;
111 import org.slf4j.Logger;
112 import org.slf4j.LoggerFactory;
113 import org.slf4j.MDC;
114 import org.springframework.beans.factory.annotation.Autowired;
115
116 import com.google.common.collect.ImmutableSet;
117 import com.google.gson.Gson;
118 import com.google.gson.GsonBuilder;
119 import com.thinkaurelius.titan.core.TitanGraph;
120 import com.thinkaurelius.titan.core.TitanGraphQuery;
121 import com.thinkaurelius.titan.core.TitanVertex;
122
123 import fj.data.Either;
124
125 public abstract class ComponentOperation {
126         private static Logger log = LoggerFactory.getLogger(ComponentOperation.class.getName());
127
128         @Autowired
129         protected TitanGenericDao titanGenericDao;
130
131         @Autowired
132         protected IArtifactOperation artifactOperation;
133
134         @Autowired
135         protected IElementOperation elementOperation;
136
137         @Autowired
138         protected ICapabilityOperation capabilityOperation;
139
140         @Autowired
141         protected IRequirementOperation requirementOperation;
142
143         @Autowired
144         protected ComponentInstanceOperation componentInstanceOperation;
145
146         @Autowired
147         private PropertyOperation propertyOperation;
148
149         @Autowired
150         protected InputsOperation inputOperation;
151
152         @Autowired
153         protected IAdditionalInformationOperation additionalInformationOperation;
154
155         @Autowired
156         protected GroupOperation groupOperation;
157
158         @Autowired
159         protected InputsOperation inputsOperation;
160
161         @Autowired
162         protected ApplicationDataTypeCache applicationDataTypeCache;
163
164         @Autowired
165         private ComponentCache componentCache;
166
167         @Autowired
168         private ToscaDefinitionPathCalculator toscaDefinitionPathCalculator;
169
170         private static Pattern uuidNewVersion = Pattern.compile("^\\d{1,}.1");
171
172         protected Gson prettyJson = new GsonBuilder().setPrettyPrinting().create();
173
174         protected Either<List<TagData>, StorageOperationStatus> createNewTagsList(List<String> tags) {
175
176                 List<TagData> existingTags = new ArrayList<TagData>();
177                 List<TagData> tagsToCreate = new ArrayList<TagData>();
178                 Either<List<TagData>, TitanOperationStatus> either = titanGenericDao.getAll(NodeTypeEnum.Tag, TagData.class);
179
180                 if ((either.isRight()) && (either.right().value() != TitanOperationStatus.NOT_FOUND)) {
181                         return Either.right(StorageOperationStatus.GENERAL_ERROR);
182                 } else if (either.isLeft()) {
183                         existingTags = either.left().value();
184                 }
185
186                 for (String tagName : tags) {
187                         TagData tag = new TagData(tagName);
188                         if ((existingTags == null) || (!existingTags.contains(tag))) {
189                                 tagsToCreate.add(tag);
190                         }
191                 }
192                 return Either.left(tagsToCreate);
193
194         }
195  
196         protected StorageOperationStatus createTagNodesOnGraph(List<TagData> tagsToCreate) {
197                 StorageOperationStatus result = StorageOperationStatus.OK;
198                 // In order to avoid duplicate tags
199                 tagsToCreate = ImmutableSet.copyOf(tagsToCreate).asList();
200                 if (tagsToCreate != null && false == tagsToCreate.isEmpty()) {
201                         for (TagData tagData : tagsToCreate) {
202                                 log.debug("Before creating tag {}" , tagData);
203                                 Either<TagData, TitanOperationStatus> createTagResult = titanGenericDao.createNode(tagData, TagData.class);
204                                 if (createTagResult.isRight()) {
205                                         TitanOperationStatus status = createTagResult.right().value();
206                                         log.error("Cannot create {} in the graph. status is {}", tagData, status);
207                                         result = DaoStatusConverter.convertTitanStatusToStorageStatus(status);
208
209                                 }
210                                 log.debug("After creating tag {}", tagData);
211                         }
212                 }
213                 return result;
214         }
215
216         public Either<Component, StorageOperationStatus> getLatestComponentByUuid(NodeTypeEnum nodeType, String uuid) {
217                 Either<Component, StorageOperationStatus> getComponentResult = null;
218                 Either<ComponentMetadataData, StorageOperationStatus> latestComponentMetadataRes = getLatestComponentMetadataByUuid(nodeType, uuid, false);
219                 if (latestComponentMetadataRes.isRight()) {
220                         getComponentResult = Either.right(latestComponentMetadataRes.right().value());
221                 }
222                 if (getComponentResult == null) {
223                         ComponentMetadataData latestVersion = latestComponentMetadataRes.left().value();
224                         String id = latestVersion.getMetadataDataDefinition().getUniqueId();
225                         Either<Component, StorageOperationStatus> component = getComponent(id, false);
226                         if (component.isRight()) {
227                                 log.debug("Couldn't fetch component with type {} and id {}, error: {}", nodeType, id, component.right().value());
228                                 getComponentResult = Either.right(component.right().value());
229                         } else {
230                                 getComponentResult = Either.left(component.left().value());
231                         }
232                 }
233                 return getComponentResult;
234         }
235
236         public Either<ComponentMetadataData, StorageOperationStatus> getLatestComponentMetadataByUuid(NodeTypeEnum nodeType, String uuid, boolean inTransaction) {
237
238                 Either<ComponentMetadataData, StorageOperationStatus> getComponentResult = null;
239                 List<ComponentMetadataData> latestVersionList = null;
240                 ComponentMetadataData latestVersion = null;
241
242                 Map<String, Object> propertiesToMatch = new HashMap<String, Object>();
243                 propertiesToMatch.put(GraphPropertiesDictionary.UUID.getProperty(), uuid);
244                 propertiesToMatch.put(GraphPropertiesDictionary.IS_HIGHEST_VERSION.getProperty(), true);
245                 try{
246                         Either<List<ComponentMetadataData>, TitanOperationStatus> getComponentEither = titanGenericDao.getByCriteria(nodeType, propertiesToMatch, ComponentMetadataData.class);
247                         if (getComponentEither.isRight()) {
248                                 log.debug("Couldn't fetch metadata for component with type {} and uuid {}, error: {}", nodeType, uuid, getComponentEither.right().value());
249                                 getComponentResult = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getComponentEither.right().value()));
250         
251                         }
252                         if (getComponentResult == null) {
253                                 latestVersionList = getComponentEither.left().value();
254                                 if (latestVersionList.isEmpty()) {
255                                         log.debug("Component with type {} and uuid {} was not found", nodeType, uuid);
256                                         getComponentResult = Either.right(StorageOperationStatus.NOT_FOUND);
257                                 }
258                         }
259                         if (getComponentResult == null) {
260                                 latestVersion = latestVersionList.size() == 1 ? latestVersionList.get(0)
261                                                 : latestVersionList.stream().max((c1, c2) -> Double.compare(Double.parseDouble(c1.getMetadataDataDefinition().getVersion()), Double.parseDouble(c2.getMetadataDataDefinition().getVersion()))).get();
262                                 getComponentResult = Either.left(latestVersion);
263                         }
264                 } catch (Exception e){
265                         log.debug("Failed to get latest component metadata with type {} by uuid {}. ", nodeType.getName(), uuid, e);
266                 }finally {
267                         if (!inTransaction) {
268                                 titanGenericDao.commit();
269                         }
270                 }
271                 return getComponentResult;
272         }
273
274         public <T extends GraphNode> Either<T, StorageOperationStatus> getComponentByLabelAndId(String uniqueId, NodeTypeEnum nodeType, Class<T> clazz) {
275
276                 Map<String, Object> propertiesToMatch = new HashMap<String, Object>();
277                 propertiesToMatch.put(UniqueIdBuilder.getKeyByNodeType(nodeType), uniqueId);
278                 Either<List<T>, TitanOperationStatus> getResponse = titanGenericDao.getByCriteria(nodeType, propertiesToMatch, clazz);
279                 if (getResponse.isRight()) {
280                         log.debug("Couldn't fetch component with type {} and unique id {}, error: {}", nodeType, uniqueId, getResponse.right().value());
281                         return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getResponse.right().value()));
282
283                 }
284                 List<T> serviceDataList = getResponse.left().value();
285                 if (serviceDataList.isEmpty()) {
286                         log.debug("Component with type {} and unique id {} was not found", nodeType, uniqueId);
287                         return Either.right(StorageOperationStatus.NOT_FOUND);
288                 }
289                 T serviceData = serviceDataList.get(0);
290                 return Either.left(serviceData);
291         }
292
293         /**
294          * 
295          * @param component
296          * @param uniqueId
297          * @param nodeType
298          * @return
299          */
300         protected TitanOperationStatus setComponentCreatorFromGraph(Component component, String uniqueId, NodeTypeEnum nodeType) {
301                 Either<ImmutablePair<UserData, GraphEdge>, TitanOperationStatus> parentNode = titanGenericDao.getParentNode(UniqueIdBuilder.getKeyByNodeType(nodeType), uniqueId, GraphEdgeLabels.CREATOR, NodeTypeEnum.User, UserData.class);
302                 if (parentNode.isRight()) {
303                         return parentNode.right().value();
304                 }
305
306                 ImmutablePair<UserData, GraphEdge> value = parentNode.left().value();
307                 if (log.isDebugEnabled())
308                         log.debug("Found parent node {}", value);
309                 UserData userData = value.getKey();
310                 if (log.isDebugEnabled())
311                         log.debug("Build resource : set creator userId to {}", userData.getUserId());
312                 String fullName = buildFullName(userData);
313                 if (log.isDebugEnabled())
314                         log.debug("Build resource : set last modifier full name to {} ", fullName);
315                 component.setCreatorUserId(userData.getUserId());
316                 component.setCreatorFullName(fullName);
317
318                 return TitanOperationStatus.OK;
319         }
320
321         protected TitanOperationStatus setComponentLastModifierFromGraph(Component component, String uniqueId, NodeTypeEnum nodeType) {
322
323                 Either<ImmutablePair<UserData, GraphEdge>, TitanOperationStatus> parentNode = titanGenericDao.getParentNode(UniqueIdBuilder.getKeyByNodeType(nodeType), uniqueId, GraphEdgeLabels.LAST_MODIFIER, NodeTypeEnum.User, UserData.class);
324                 if (parentNode.isRight()) {
325                         return parentNode.right().value();
326                 }
327
328                 ImmutablePair<UserData, GraphEdge> value = parentNode.left().value();
329                 if (log.isDebugEnabled())
330                         log.debug("Found parent node {}", value);
331                 UserData userData = value.getKey();
332
333                 if (log.isDebugEnabled())
334                         log.debug("Build resource : set last modifier userId to {}", userData.getUserId());
335                 String fullName = buildFullName(userData);
336                 if (log.isDebugEnabled())
337                         log.debug("Build resource : set last modifier full name to {}", fullName);
338                 component.setLastUpdaterUserId(userData.getUserId());
339                 component.setLastUpdaterFullName(fullName);
340
341                 return TitanOperationStatus.OK;
342         }
343
344         /**
345          * 
346          * @param userData
347          * @return
348          */
349         protected String buildFullName(UserData userData) {
350
351                 String fullName = userData.getFirstName();
352                 if (fullName == null) {
353                         fullName = "";
354                 } else {
355                         fullName = fullName + " ";
356                 }
357                 String lastName = userData.getLastName();
358                 if (lastName != null) {
359                         fullName += lastName;
360                 }
361                 return fullName;
362         }
363
364         protected Either<UserData, TitanOperationStatus> findUser(String userId) {
365                 String key = UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.User);
366                 Either<UserData, TitanOperationStatus> findUser = titanGenericDao.getNode(key, userId, UserData.class);
367                 return findUser;
368         }
369
370         protected Either<TitanVertex, TitanOperationStatus> findUserVertex(String userId) {
371                 String key = UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.User);
372                 return titanGenericDao.getVertexByProperty(key, userId);
373         }
374
375         protected Either<GroupingData, TitanOperationStatus> findGrouping(NodeTypeEnum nodeType, String groupingId) {
376                 String key = UniqueIdBuilder.getKeyByNodeType(nodeType);
377                 Either<GroupingData, TitanOperationStatus> findGrouping = titanGenericDao.getNode(key, groupingId, GroupingData.class);
378                 return findGrouping;
379         }
380
381         protected Either<SubCategoryData, TitanOperationStatus> findSubCategory(NodeTypeEnum nodeType, String subCategoryId) {
382                 String key = UniqueIdBuilder.getKeyByNodeType(nodeType);
383                 Either<SubCategoryData, TitanOperationStatus> findSubCategory = titanGenericDao.getNode(key, subCategoryId, SubCategoryData.class);
384                 return findSubCategory;
385         }
386
387         protected Either<CategoryData, TitanOperationStatus> findCategory(NodeTypeEnum nodeType, String categoryId) {
388                 String key = UniqueIdBuilder.getKeyByNodeType(nodeType);
389                 Either<CategoryData, TitanOperationStatus> findCategory = titanGenericDao.getNode(key, categoryId, CategoryData.class);
390                 return findCategory;
391         }
392
393         protected TitanOperationStatus associateMetadataToComponent(ComponentMetadataData componentData, UserData userData, UserData updater, CategoryData categoryData, List<ResourceMetadataData> derivedResources) {
394
395                 Map<String, Object> props = new HashMap<String, Object>();
396                 props.put(GraphPropertiesDictionary.STATE.getProperty(), componentData.getMetadataDataDefinition().getState());
397                 Either<GraphRelation, TitanOperationStatus> result = titanGenericDao.createRelation(updater, componentData, GraphEdgeLabels.STATE, props);
398                 log.debug("After associating user {} to component {}. Edge type is {}" , updater, componentData.getUniqueId(),  GraphEdgeLabels.STATE);
399                 if (result.isRight()) {
400                         return result.right().value();
401                 }
402
403                 result = titanGenericDao.createRelation(updater, componentData, GraphEdgeLabels.LAST_MODIFIER, null);
404                 log.debug("After associating user {} to component {}. Edge type is {}",  updater,  componentData.getUniqueId(), GraphEdgeLabels.LAST_MODIFIER);
405                 if (result.isRight()) {
406                         log.error("Failed to associate user {} to component {}. Edge type is {}", updater, componentData.getUniqueId(), GraphEdgeLabels.LAST_MODIFIER);
407                         return result.right().value();
408                 }
409
410                 result = titanGenericDao.createRelation(userData, componentData, GraphEdgeLabels.CREATOR, null);
411                 log.debug("After associating user {} to component {}. Edge type is {}" , userData, componentData.getUniqueId(), GraphEdgeLabels.CREATOR);
412                 if (result.isRight()) {
413                         log.error("Failed to associate user {} to component {}. Edge type is {}", userData, componentData.getUniqueId(), GraphEdgeLabels.CREATOR);
414                         return result.right().value();
415                 }
416
417                 if (derivedResources != null) {
418                         for (ResourceMetadataData derivedResource : derivedResources) {
419                                 log.debug("After associating component {} to parent component {}. Edge type is {}" ,componentData.getUniqueId(), derivedResource.getUniqueId(), GraphEdgeLabels.DERIVED_FROM);
420                                 result = titanGenericDao.createRelation(componentData, derivedResource, GraphEdgeLabels.DERIVED_FROM, null);
421                                 if (result.isRight()) {
422                                         log.error("Failed to associate user {} to component {}. Edge type is {}", userData, componentData.getUniqueId(), GraphEdgeLabels.CREATOR);
423                                         return result.right().value();
424                                 }
425                         }
426                 }
427
428                 if (categoryData != null) {
429                         result = titanGenericDao.createRelation(componentData, categoryData, GraphEdgeLabels.CATEGORY, null);
430                         log.debug("After associating component {} to category {}. Edge type is {}", componentData.getUniqueId(), categoryData, GraphEdgeLabels.CATEGORY);
431                         if (result.isRight()) {
432                                 log.error("Faield to associate component {} to category {}. Edge type is {}", componentData.getUniqueId(), categoryData, GraphEdgeLabels.CATEGORY);
433                                 return result.right().value();
434                         }
435                 }
436
437                 return TitanOperationStatus.OK;
438         }
439
440         protected StorageOperationStatus associateArtifactsToComponent(NodeTypeEnum nodeType, ComponentMetadataData componentData, Map<String, ArtifactDefinition> artifacts) {
441
442                 if (artifacts != null) {
443                         for (Entry<String, ArtifactDefinition> entry : artifacts.entrySet()) {
444
445                                 ArtifactDefinition artifactDefinition = entry.getValue();
446                                 Either<ArtifactDefinition, StorageOperationStatus> addArifactToResource = Either.left(artifactDefinition);
447                                 addArifactToResource = artifactOperation.addArifactToComponent(artifactDefinition, (String) componentData.getUniqueId(), nodeType, false, true);
448                                 if (addArifactToResource.isRight()) {
449                                         return addArifactToResource.right().value();
450                                 }
451                         }
452                 }
453                 return StorageOperationStatus.OK;
454
455         }
456
457         protected Either<Boolean, StorageOperationStatus> validateResourceNameUniqueness(String name, Map<String, Object> hasProps, Map<String, Object> hasNotProps, TitanGenericDao titanGenericDao) {
458                 if (hasProps == null) {
459                         hasProps = new HashMap<String, Object>();
460                 }
461                 String normalizedName = ValidationUtils.normaliseComponentName(name);
462                 hasProps.put(GraphPropertiesDictionary.NORMALIZED_NAME.getProperty(), normalizedName);
463
464                 Either<List<ResourceMetadataData>, TitanOperationStatus> resources = titanGenericDao.getByCriteria(NodeTypeEnum.Resource, hasProps, hasNotProps, ResourceMetadataData.class);
465                 if (resources.isRight() && resources.right().value() != TitanOperationStatus.NOT_FOUND) {
466                         log.debug("failed to get resources from graph with property name: {}", name);
467                         return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(resources.right().value()));
468                 }
469                 List<ResourceMetadataData> resourceList = (resources.isLeft() ? resources.left().value() : null);
470                 if (resourceList != null && resourceList.size() > 0) {
471                         if (log.isDebugEnabled()) {
472                                 StringBuilder builder = new StringBuilder();
473                                 for (ResourceMetadataData resourceData : resourceList) {
474                                         builder.append(resourceData.getUniqueId() + "|");
475                                 }
476                                 log.debug("resources  with property name:{} exists in graph. found {}",name, builder.toString());
477                         }
478                         return Either.left(false);
479                 } else {
480                         log.debug("resources  with property name:{} does not exists in graph", name);
481                         return Either.left(true);
482                 }
483
484         }
485         
486         protected Either<Boolean, StorageOperationStatus> validateServiceNameUniqueness(String name, TitanGenericDao titanGenericDao) {
487                 Map<String, Object> properties = new HashMap<>();
488                 String normalizedName = ValidationUtils.normaliseComponentName(name);
489                 properties.put(GraphPropertiesDictionary.NORMALIZED_NAME.getProperty(), normalizedName);
490
491                 Either<List<ServiceMetadataData>, TitanOperationStatus> services = titanGenericDao.getByCriteria(NodeTypeEnum.Service, properties, ServiceMetadataData.class);
492                 if (services.isRight() && services.right().value() != TitanOperationStatus.NOT_FOUND) {
493                         log.debug("failed to get services from graph with property name: {}" , name);
494                         return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(services.right().value()));
495                 }
496                 List<ServiceMetadataData> serviceList = (services.isLeft() ? services.left().value() : null);
497                 if (serviceList != null && serviceList.size() > 0) {
498                         if (log.isDebugEnabled()) {
499                                 StringBuilder builder = new StringBuilder();
500                                 for (ServiceMetadataData serviceData : serviceList) {
501                                         builder.append(serviceData.getUniqueId() + "|");
502                                 }
503                                 log.debug("Service with property name:{} exists in graph. found {}" , name, builder.toString());
504                         }
505
506                         return Either.left(false);
507                 } else {
508                         log.debug("Service  with property name:{} does not exists in graph", name);
509                         return Either.left(true);
510                 }
511         }
512         
513         protected Either<Boolean, StorageOperationStatus> validateToscaResourceNameUniqueness(String name, TitanGenericDao titanGenericDao) {
514                 Map<String, Object> properties = new HashMap<>();
515
516                 properties.put(GraphPropertiesDictionary.TOSCA_RESOURCE_NAME.getProperty(), name);
517
518                 Either<List<ResourceMetadataData>, TitanOperationStatus> resources = titanGenericDao.getByCriteria(NodeTypeEnum.Resource, properties, ResourceMetadataData.class);
519                 if (resources.isRight() && resources.right().value() != TitanOperationStatus.NOT_FOUND) {
520                         log.debug("failed to get resources from graph with property name: {}" , name);
521                         return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(resources.right().value()));
522                 }
523                 List<ResourceMetadataData> resourceList = (resources.isLeft() ? resources.left().value() : null);
524                 if (resourceList != null && resourceList.size() > 0) {
525                         if (log.isDebugEnabled()) {
526                                 StringBuilder builder = new StringBuilder();
527                                 for (ResourceMetadataData resourceData : resourceList) {
528                                         builder.append(resourceData.getUniqueId() + "|");
529                                 }
530                                 log.debug("resources  with property name:{} exists in graph. found {}" , name, builder.toString());
531                         }
532                         return Either.left(false);
533                 } else {
534                         log.debug("resources  with property name:{} does not exists in graph", name);
535                         return Either.left(true);
536                 }
537
538         }
539
540         protected Either<Boolean, StorageOperationStatus> validateComponentNameUniqueness(String name, TitanGenericDao titanGenericDao, NodeTypeEnum type) {
541                 Map<String, Object> properties = new HashMap<>();
542                 String normalizedName = ValidationUtils.normaliseComponentName(name);
543                 properties.put(GraphPropertiesDictionary.NORMALIZED_NAME.getProperty(), normalizedName);
544
545                 Either<List<ComponentMetadataData>, TitanOperationStatus> components = titanGenericDao.getByCriteria(type, properties, ComponentMetadataData.class);
546                 if (components.isRight() && components.right().value() != TitanOperationStatus.NOT_FOUND) {
547                         log.debug("failed to get components from graph with property name: {}" , name);
548                         return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(components.right().value()));
549                 }
550                 List<ComponentMetadataData> componentList = (components.isLeft() ? components.left().value() : null);
551                 if (componentList != null && componentList.size() > 0) {
552                         if (log.isDebugEnabled()) {
553                                 StringBuilder builder = new StringBuilder();
554                                 for (ComponentMetadataData componentData : componentList) {
555                                         builder.append(componentData.getUniqueId() + "|");
556                                 }
557                                 log.debug("Component with property name:{} exists in graph. found {}" , name, builder.toString());
558                         }
559
560                         return Either.left(false);
561                 } else {
562                         log.debug("Component with property name:{} does not exists in graph", name);
563                         return Either.left(true);
564                 }
565         }
566
567         protected StorageOperationStatus setArtifactFromGraph(String uniqueId, Component component, NodeTypeEnum type, IArtifactOperation artifactOperation) {
568                 StorageOperationStatus result = StorageOperationStatus.OK;
569                 Either<Map<String, ArtifactDefinition>, StorageOperationStatus> artifacts = artifactOperation.getArtifacts(uniqueId, type, true);
570                 if (artifacts.isRight()) {
571                         result = artifacts.right().value();
572                 } else {
573                         // component.setArtifacts(artifacts.left().value());
574                         createSpecificArtifactList(component, artifacts.left().value());
575                 }
576                 return result;
577         }
578
579         protected Component createSpecificArtifactList(Component component, Map<String, ArtifactDefinition> artifacts) {
580
581                 if (artifacts != null) {
582                         Map<String, ArtifactDefinition> deploymentArtifacts = new HashMap<>();
583                         Map<String, ArtifactDefinition> serviceApiArtifacts = new HashMap<>();
584                         Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
585
586                         Set<Entry<String, ArtifactDefinition>> specificet = new HashSet<>();
587
588                         for (Entry<String, ArtifactDefinition> entry : artifacts.entrySet()) {
589                                 ArtifactDefinition artifact = entry.getValue();
590                                 ArtifactGroupTypeEnum artifactGroupType = artifact.getArtifactGroupType();
591                                 if (artifactGroupType == null) {
592                                         artifactGroupType = ArtifactGroupTypeEnum.INFORMATIONAL;
593                                 }
594
595                                 switch (artifactGroupType) {
596                                 case DEPLOYMENT:
597                                         deploymentArtifacts.put(artifact.getArtifactLabel(), artifact);
598                                         specificet.add(entry);
599                                         break;
600                                 case SERVICE_API:
601                                         serviceApiArtifacts.put(artifact.getArtifactLabel(), artifact);
602                                         specificet.add(entry);
603                                         break;
604                                 case TOSCA:
605                                         toscaArtifacts.put(artifact.getArtifactLabel(), artifact);
606                                         specificet.add(entry);
607                                         break;
608                                 default:
609                                         break;
610                                 }
611
612                         }
613                         artifacts.entrySet().removeAll(specificet);
614
615                         component.setSpecificComponetTypeArtifacts(serviceApiArtifacts);
616                         component.setDeploymentArtifacts(deploymentArtifacts);
617                         component.setToscaArtifacts(toscaArtifacts);
618                         component.setArtifacts(artifacts);
619                 }
620                 return component;
621         }
622
623         private <T, S extends ComponentMetadataData> Either<List<T>, StorageOperationStatus> collectComponents(TitanGraph graph, NodeTypeEnum neededType, String categoryUid, NodeTypeEnum categoryType, Class<S> clazz, ResourceTypeEnum resourceType) {
624                 List<T> components = new ArrayList<>();
625                 Either<List<ImmutablePair<S, GraphEdge>>, TitanOperationStatus> parentNodes = titanGenericDao.getParentNodes(UniqueIdBuilder.getKeyByNodeType(categoryType), categoryUid, GraphEdgeLabels.CATEGORY, neededType, clazz);
626                 if (parentNodes.isLeft()) {
627                         for (ImmutablePair<S, GraphEdge> component : parentNodes.left().value()) {
628                                 ComponentMetadataDataDefinition componentData = component.getLeft().getMetadataDataDefinition();
629                                 Boolean isHighest = componentData.isHighestVersion();
630                                 boolean isMatchingResourceType = isMatchingByResourceType(neededType, resourceType, componentData);
631                                 
632                                 if (isHighest && isMatchingResourceType) {
633                                         Either<T, StorageOperationStatus> result = getLightComponent(componentData.getUniqueId(), true);
634                                         if (result.isRight()) {
635                                                 return Either.right(result.right().value());
636                                         }
637                                         components.add(result.left().value());
638                                 }
639                         }
640                 }
641                 return Either.left(components);
642         }
643
644         private boolean isMatchingByResourceType(NodeTypeEnum componentType, ResourceTypeEnum resourceType,
645                         ComponentMetadataDataDefinition componentData) {
646
647                 boolean isMatching;
648                 if (componentType == NodeTypeEnum.Resource) {
649                         if (resourceType == null) {
650                                 isMatching = true;
651                         } else {
652                                 isMatching = resourceType == ((ResourceMetadataDataDefinition)componentData).getResourceType();
653                         }
654                 } else {
655                         isMatching = true;
656                 }
657                 return isMatching;
658         }
659
660         protected <T, S extends ComponentMetadataData> Either<List<T>, StorageOperationStatus> fetchByCategoryOrSubCategoryUid(String categoryUid, NodeTypeEnum categoryType, String categoryLabel, NodeTypeEnum neededType, boolean inTransaction,
661                         Class<S> clazz, ResourceTypeEnum resourceType) {
662                 try {
663                         Either<TitanGraph, TitanOperationStatus> graph = titanGenericDao.getGraph();
664                         if (graph.isRight()) {
665                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(graph.right().value()));
666
667                         }
668                         return collectComponents(graph.left().value(), neededType, categoryUid, categoryType, clazz, resourceType);
669
670                 } finally {
671                         if (false == inTransaction) {
672                                 titanGenericDao.commit();
673                         }
674                 }
675         }
676
677         protected <T, S extends ComponentMetadataData> Either<List<T>, StorageOperationStatus> fetchByCategoryOrSubCategoryName(String categoryName, NodeTypeEnum categoryType, String categoryLabel, NodeTypeEnum neededType, boolean inTransaction,
678                         Class<S> clazz, ResourceTypeEnum resourceType) {
679                 List<T> components = new ArrayList<>();
680                 try {
681                         Class categoryClazz = categoryType == NodeTypeEnum.ServiceNewCategory ? CategoryData.class : SubCategoryData.class;
682                         Map<String, Object> props = new HashMap<String, Object>();
683                         props.put(GraphPropertiesDictionary.NORMALIZED_NAME.getProperty(), ValidationUtils.normalizeCategoryName4Uniqueness(categoryName));
684                         Either<List<GraphNode>, TitanOperationStatus> getCategory = titanGenericDao.getByCriteria(categoryType, props, categoryClazz);
685                         if (getCategory.isRight()) {
686                                 return Either.right(StorageOperationStatus.CATEGORY_NOT_FOUND);
687                         }
688                         Either<TitanGraph, TitanOperationStatus> graph = titanGenericDao.getGraph();
689                         if (graph.isRight()) {
690                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(graph.right().value()));
691
692                         }
693                         for (GraphNode category : getCategory.left().value()) {
694                                 Either<List<T>, StorageOperationStatus> result = collectComponents(graph.left().value(), neededType, (String) category.getUniqueId(), categoryType, clazz, resourceType);
695                                 if (result.isRight()) {
696                                         return result;
697                                 }
698                                 components.addAll(result.left().value());
699                         }
700
701                         return Either.left(components);
702                 } finally {
703                         if (false == inTransaction) {
704                                 titanGenericDao.commit();
705                         }
706                 }
707         }
708
709         <T> Either<List<T>, StorageOperationStatus> getFilteredComponents(Map<FilterKeyEnum, String> filters, boolean inTransaction, NodeTypeEnum neededType) {
710                 return null;
711         }
712
713         protected Either<List<Component>, StorageOperationStatus> getFollowedComponent(String userId, Set<LifecycleStateEnum> lifecycleStates, Set<LifecycleStateEnum> lastStateStates, boolean inTransaction, TitanGenericDao titanGenericDao,
714                         NodeTypeEnum neededType) {
715
716                 Either<List<Component>, StorageOperationStatus> result = null;
717
718                 try {
719                         Either<TitanGraph, TitanOperationStatus> graph = titanGenericDao.getGraph();
720                         if (graph.isRight()) {
721                                 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(graph.right().value()));
722                                 return result;
723                         }
724                         Iterable<TitanVertex> users;
725
726                         if (userId == null) {
727                                 // get all users by label
728                                 // for Tester and Admin retrieve all users
729
730                                 // users =
731                                 // graph.left().value().getVertices(GraphPropertiesDictionary.LABEL.getProperty(),
732                                 // NodeTypeEnum.User.getName());
733                                 users = graph.left().value().query().has(GraphPropertiesDictionary.LABEL.getProperty(), NodeTypeEnum.User.getName()).vertices();
734
735                         } else {
736                                 // for Designer retrieve specific user
737                                 String key = UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.User);
738                                 users = graph.left().value().query().has(key, userId).vertices();
739                         }
740                         Iterator<TitanVertex> userIterator = users.iterator();
741
742                         List<Component> components = new ArrayList<>();
743                         while (userIterator.hasNext()) {
744                                 Vertex vertexUser = userIterator.next();
745
746                                 // get all resource with current state
747                                 Iterator<Edge> iterator = vertexUser.edges(Direction.OUT, GraphEdgeLabels.STATE.getProperty());
748
749                                 List<Component> componentsPerUser = fetchComponents(lifecycleStates, iterator, neededType, inTransaction);
750
751                                 HashSet<String> ids = new HashSet<String>();
752
753                                 if (componentsPerUser != null) {
754                                         for (Component comp : componentsPerUser) {
755                                                 ids.add(comp.getUniqueId());
756                                                 components.add(comp);
757                                         }
758                                 }
759
760                                 if (lastStateStates != null && !lastStateStates.isEmpty()) {
761                                         // get all resource with last state
762                                         iterator = vertexUser.edges(Direction.OUT, GraphEdgeLabels.LAST_STATE.getProperty());
763                                         boolean isFirst;
764                                         componentsPerUser = fetchComponents(lastStateStates, iterator, neededType, inTransaction);
765                                         if (componentsPerUser != null) {
766                                                 for (Component comp : componentsPerUser) {
767                                                         isFirst = true;
768
769                                                         if (ids.contains(comp.getUniqueId())) {
770                                                                 isFirst = false;
771                                                         }
772                                                         if (isFirst == true) {
773                                                                 components.add(comp);
774                                                         }
775
776                                                 }
777                                         }
778                                 }
779
780                         } // whlile users
781
782                         result = Either.left(components);
783                         return result;
784
785                 } finally {
786                         if (false == inTransaction) {
787                                 if (result == null || result.isRight()) {
788                                         titanGenericDao.rollback();
789                                 } else {
790                                         titanGenericDao.commit();
791                                 }
792                         }
793                 }
794
795         }
796
797         private List<Component> fetchComponents(Set<LifecycleStateEnum> lifecycleStates, Iterator<Edge> iterator, NodeTypeEnum neededType, boolean inTransaction) {
798                 List<Component> components = new ArrayList<>();
799                 while (iterator.hasNext()) {
800                         Edge edge = iterator.next();
801
802                         String stateStr = edge.value(GraphEdgePropertiesDictionary.STATE.getProperty());
803                         LifecycleStateEnum state = LifecycleStateEnum.findState(stateStr);
804                         if (state == null) {
805                                 log.debug("not supported STATE for element  {}" , stateStr);
806                                 continue;
807                         }
808                         if (lifecycleStates != null && lifecycleStates.contains(state)) {
809                                 Vertex vertexComponent = edge.inVertex();
810
811                                 Boolean isHighest = vertexComponent.value(GraphPropertiesDictionary.IS_HIGHEST_VERSION.getProperty());
812                                 if (isHighest) {
813
814                                         String nodeTypeStr = vertexComponent.value(GraphPropertiesDictionary.LABEL.getProperty());
815                                         // get only latest versions
816                                         NodeTypeEnum nodeType = NodeTypeEnum.getByName(nodeTypeStr);
817
818                                         if (nodeType == null) {
819                                                 log.debug("missing node label for vertex {}", vertexComponent);
820                                                 continue;
821                                         }
822
823                                         if (neededType.equals(nodeType)) {
824                                                 switch (nodeType) {
825                                                 case Service:
826                                                         handleNode(components, vertexComponent, nodeType, inTransaction);
827                                                         break;
828                                                 case Resource:
829                                                         Boolean isAbtract = vertexComponent.value(GraphPropertiesDictionary.IS_ABSTRACT.getProperty());
830                                                         if (false == isAbtract) {
831                                                                 handleNode(components, vertexComponent, nodeType, inTransaction);
832                                                         } // if not abstract
833                                                         break;
834                                                 case Product:
835                                                         handleNode(components, vertexComponent, nodeType, inTransaction);
836                                                         break;
837                                                 default:
838                                                         log.debug("not supported node type {}", nodeType);
839                                                         break;
840                                                 }// case
841                                         } // needed type
842                                 }
843                         } // if
844                 } // while resources
845                 return components;
846         }
847
848         protected <T> void handleNode(List<T> components, Vertex vertexComponent, NodeTypeEnum nodeType, boolean inTransaction) {
849                 String id;
850
851                 id = vertexComponent.value(UniqueIdBuilder.getKeyByNodeType(nodeType));
852                 if (id != null) {
853                         Either<T, StorageOperationStatus> component = getLightComponent(id, inTransaction);
854                         if (component.isRight()) {
855                                 log.debug("Failed to get component for id =  {}  error : {} skip resource", id, component.right().value());
856                         } else {
857                                 components.add(component.left().value());
858                         }
859                 } else {
860
861                         Map<String, Object> properties = this.titanGenericDao.getProperties(vertexComponent);
862                         log.debug("missing resource unique id for node with properties {}", properties);
863                 }
864         }
865
866         /**
867          * 
868          * @param component
869          * @param inTransaction
870          * @param titanGenericDao
871          * @param clazz
872          * @return
873          */
874         public <T> Either<T, StorageOperationStatus> updateComponent(Component component, boolean inTransaction, TitanGenericDao titanGenericDao, Class<T> clazz, NodeTypeEnum type) {
875
876                 ComponentParametersView componentParametersView = new ComponentParametersView();
877                 return updateComponentFilterResult(component, inTransaction, titanGenericDao, clazz, type, componentParametersView);
878
879         }
880
881         private Either<ArtifactData, StorageOperationStatus> generateAndUpdateToscaFileName(String componentType, String componentName, String componentId, NodeTypeEnum type, ArtifactDefinition artifactInfo) {
882                 Map<String, Object> getConfig = (Map<String, Object>) ConfigurationManager.getConfigurationManager().getConfiguration().getToscaArtifacts().entrySet().stream()
883                                 .filter(p -> p.getKey().equalsIgnoreCase(artifactInfo.getArtifactLabel()))
884                                 .findAny()
885                                 .get()
886                                 .getValue();
887                 artifactInfo.setArtifactName(componentType + "-" + componentName + getConfig.get("artifactName"));
888                 return artifactOperation.updateToscaArtifactNameOnGraph(artifactInfo, artifactInfo.getUniqueId(), type, componentId);
889         }
890
891         protected StorageOperationStatus moveCategoryEdge(Component component, ComponentMetadataData componentData, CategoryDefinition newCategory, NodeTypeEnum type) {
892
893                 StorageOperationStatus result = StorageOperationStatus.OK;
894
895                 GraphRelation categoryRelation = new GraphRelation();
896                 categoryRelation.setType(GraphEdgeLabels.CATEGORY.getProperty());
897                 RelationEndPoint relationEndPoint = new RelationEndPoint(type, UniqueIdBuilder.getKeyByNodeType(type), component.getUniqueId());
898                 categoryRelation.setFrom(relationEndPoint);
899                 Either<GraphRelation, TitanOperationStatus> deleteOutgoingRelation = titanGenericDao.deleteOutgoingRelation(categoryRelation);
900                 if (deleteOutgoingRelation.isRight()) {
901                         log.error("Failed to delete category from component {}. Edge type is {}", componentData.getUniqueId(), GraphEdgeLabels.CATEGORY);
902                         result = DaoStatusConverter.convertTitanStatusToStorageStatus(deleteOutgoingRelation.right().value());
903                         return result;
904                 }
905
906                 log.debug("After removing edge from graph {}", deleteOutgoingRelation);
907
908                 NodeTypeEnum categoryType;
909                 if (NodeTypeEnum.Service.name().equalsIgnoreCase(type.name())) {
910                         categoryType = NodeTypeEnum.ServiceCategory;
911                 } else {
912                         categoryType = NodeTypeEnum.ResourceCategory;
913                 }
914                 Either<CategoryData, StorageOperationStatus> categoryResult = elementOperation.getNewCategoryData(newCategory.getName(), NodeTypeEnum.ServiceNewCategory, CategoryData.class);
915                 if (categoryResult.isRight()) {
916                         StorageOperationStatus status = categoryResult.right().value();
917                         log.error("Cannot find category {} in the graph. status is {}", newCategory.getName(), status);
918                         return status;
919                 }
920
921                 CategoryData categoryData = categoryResult.left().value();
922                 Either<GraphRelation, TitanOperationStatus> createRelation = titanGenericDao.createRelation(componentData, categoryData, GraphEdgeLabels.CATEGORY, null);
923                 log.debug("After associating category {} to component {}. Edge type is {}", categoryData, componentData.getUniqueId(), GraphEdgeLabels.CATEGORY);
924                 if (createRelation.isRight()) {
925                         log.error("Failed to associate category {} to component {}. Edge type is {}", categoryData, componentData.getUniqueId(), GraphEdgeLabels.CATEGORY);
926                         result = DaoStatusConverter.convertTitanStatusToStorageStatus(createRelation.right().value());
927                         return result;
928                 }
929
930                 return result;
931         }
932
933         private StorageOperationStatus moveLastModifierEdge(Component component, ComponentMetadataData componentData, UserData modifierUserData, NodeTypeEnum type) {
934
935                 StorageOperationStatus result = StorageOperationStatus.OK;
936
937                 GraphRelation lastModifierRelation = new GraphRelation();
938                 lastModifierRelation.setType(GraphEdgeLabels.LAST_MODIFIER.getProperty());
939                 RelationEndPoint relationEndPoint = new RelationEndPoint(type, UniqueIdBuilder.getKeyByNodeType(type), component.getUniqueId());
940                 lastModifierRelation.setTo(relationEndPoint);
941                 Either<GraphRelation, TitanOperationStatus> deleteIncomingRelation = titanGenericDao.deleteIncomingRelation(lastModifierRelation);
942                 if (deleteIncomingRelation.isRight()) {
943                         log.error("Failed to delete user from component {}. Edge type is {}", componentData.getUniqueId(), GraphEdgeLabels.LAST_MODIFIER);
944                         result = DaoStatusConverter.convertTitanStatusToStorageStatus(deleteIncomingRelation.right().value());
945                         return result;
946                 }
947
948                 Either<GraphRelation, TitanOperationStatus> createRelation = titanGenericDao.createRelation(modifierUserData, componentData, GraphEdgeLabels.LAST_MODIFIER, null);
949                 log.debug("After associating user {} to component {}. Edge type is {}", modifierUserData, componentData.getUniqueId(), GraphEdgeLabels.LAST_MODIFIER);
950                 if (createRelation.isRight()) {
951                         log.error("Failed to associate user {} to component {}. Edge type is {}", modifierUserData, componentData.getUniqueId(), GraphEdgeLabels.LAST_MODIFIER);
952                         result = DaoStatusConverter.convertTitanStatusToStorageStatus(createRelation.right().value());
953                         return result;
954                 }
955                 return result;
956         }
957
958         protected abstract ComponentMetadataData getMetaDataFromComponent(Component component);
959
960         public abstract <T> Either<T, StorageOperationStatus> getComponent(String id, boolean inTransaction);
961
962         public abstract <T> Either<T, StorageOperationStatus> getComponent(String id, ComponentParametersView componentParametersView, boolean inTrasnaction);
963
964         protected abstract <T> Either<T, StorageOperationStatus> getComponentByNameAndVersion(String name, String version, Map<String, Object> additionalParams, boolean inTransaction);
965
966         public abstract <T> Either<T, StorageOperationStatus> getLightComponent(String id, boolean inTransaction);
967
968         public abstract <T> Either<List<T>, StorageOperationStatus> getFilteredComponents(Map<FilterKeyEnum, String> filters, boolean inTransaction);
969
970         abstract Component convertComponentMetadataDataToComponent(ComponentMetadataData componentMetadataData);
971
972         abstract TitanOperationStatus setComponentCategoriesFromGraph(Component component);
973
974         protected abstract Either<Component, StorageOperationStatus> getMetadataComponent(String id, boolean inTransaction);
975
976         protected abstract <T> Either<T, StorageOperationStatus> updateComponent(T component, boolean inTransaction);
977
978         protected abstract <T> Either<T, StorageOperationStatus> updateComponentFilterResult(T component, boolean inTransaction, ComponentParametersView filterParametersView);
979
980         public abstract Either<Component, StorageOperationStatus> deleteComponent(String id, boolean inTransaction);
981
982         public <T> Either<T, StorageOperationStatus> cloneComponent(T other, String version, boolean inTransaction) {
983                 return cloneComponent(other, version, null, inTransaction);
984         }
985
986         public abstract <T> Either<T, StorageOperationStatus> cloneComponent(T other, String version, LifecycleStateEnum targetLifecycle, boolean inTransaction);
987
988         public abstract Component getDefaultComponent();
989
990         public abstract boolean isComponentExist(String componentId);
991
992         public abstract Either<Boolean, StorageOperationStatus> validateComponentNameExists(String componentName);
993
994         public abstract Either<Boolean, StorageOperationStatus> isComponentInUse(String componentId);
995
996         protected Either<Boolean, StorageOperationStatus> isComponentInUse(String componentId, NodeTypeEnum nodeType) {
997
998                 Either<GraphRelation, TitanOperationStatus> relationByCriteria = titanGenericDao.getIncomingRelationByCriteria(new UniqueIdData(nodeType, componentId), GraphEdgeLabels.INSTANCE_OF, null);
999                 if (relationByCriteria.isRight() && !relationByCriteria.right().value().equals(TitanOperationStatus.NOT_FOUND)) {
1000                         log.debug("failed to check relations for component node. id = {}, type = {}, error = {}", componentId, nodeType, relationByCriteria.right().value().name());
1001                         return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(relationByCriteria.right().value()));
1002                 }
1003
1004                 if (relationByCriteria.isLeft()) {
1005                         // component is in use
1006                         return Either.left(true);
1007                 } else {
1008                         return Either.left(false);
1009                 }
1010
1011         }
1012
1013         public abstract Either<List<String>, StorageOperationStatus> getAllComponentsMarkedForDeletion();
1014
1015         protected Either<List<String>, StorageOperationStatus> getAllComponentsMarkedForDeletion(NodeTypeEnum nodeType) {
1016
1017                 List<String> componentIdsToDelete = new ArrayList<String>();
1018                 // get all components marked for delete
1019                 Map<String, Object> props = new HashMap<String, Object>();
1020                 props.put(GraphPropertiesDictionary.IS_DELETED.getProperty(), true);
1021
1022                 Either<List<ComponentMetadataData>, TitanOperationStatus> componentsToDelete = titanGenericDao.getByCriteria(nodeType, props, ComponentMetadataData.class);
1023
1024                 if (componentsToDelete.isRight()) {
1025                         TitanOperationStatus error = componentsToDelete.right().value();
1026                         if (error.equals(TitanOperationStatus.NOT_FOUND)) {
1027                                 log.trace("no components to delete");
1028                                 return Either.left(componentIdsToDelete);
1029                         } else {
1030                                 log.info("failed to find components to delete. error : {}", error.name());
1031                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(error));
1032                         }
1033
1034                 }
1035                 for (ComponentMetadataData resourceData : componentsToDelete.left().value()) {
1036                         componentIdsToDelete.add(resourceData.getMetadataDataDefinition().getUniqueId());
1037                 }
1038                 return Either.left(componentIdsToDelete);
1039         }
1040
1041         protected <T extends GraphNode> Either<List<T>, TitanOperationStatus> __getLastVersion(NodeTypeEnum type, Map<String, Object> props, Class<T> clazz) {
1042                 try {
1043
1044                         Either<TitanGraph, TitanOperationStatus> graph = titanGenericDao.getGraph();
1045                         if (graph.isRight()) {
1046                                 return Either.right(graph.right().value());
1047                         }
1048
1049                         TitanGraph tGraph = graph.left().value();
1050                         TitanGraphQuery<? extends TitanGraphQuery> query = tGraph.query();
1051                         query = query.has(GraphPropertiesDictionary.LABEL.getProperty(), type.getName());
1052
1053                         if (props != null && !props.isEmpty()) {
1054                                 for (Map.Entry<String, Object> entry : props.entrySet()) {
1055                                         query = query.hasNot(entry.getKey(), entry.getValue());
1056                                 }
1057                         }
1058                         query.has(GraphPropertiesDictionary.IS_HIGHEST_VERSION.getProperty(), true);
1059
1060                         Iterable<TitanVertex> vertices = query.vertices();
1061
1062                         if (vertices == null) {
1063                                 return Either.right(TitanOperationStatus.NOT_FOUND);
1064                         }
1065
1066                         Iterator<TitanVertex> iterator = vertices.iterator();
1067                         List<T> result = new ArrayList<T>();
1068
1069                         while (iterator.hasNext()) {
1070                                 Vertex vertex = iterator.next();
1071
1072                                 Map<String, Object> newProp = titanGenericDao.getProperties(vertex);
1073                                 T element = GraphElementFactory.createElement(type.getName(), GraphElementTypeEnum.Node, newProp, clazz);
1074                                 result.add(element);
1075                         }
1076                         if (result.size() == 0) {
1077                                 return Either.right(TitanOperationStatus.NOT_FOUND);
1078                         }
1079                         log.debug("No nodes in graph for criteria : from type = {} and properties = {}", type, props);
1080                         return Either.left(result);
1081                 } catch (Exception e) {
1082                         log.debug("Failed get by criteria for type = {} and properties = {}", type, props, e);
1083                         return Either.right(TitanGraphClient.handleTitanException(e));
1084                 }
1085         }
1086
1087         protected <T extends GraphNode> Either<List<T>, TitanOperationStatus> getLastVersion(NodeTypeEnum type, Map<String, Object> hasNotProps, Class<T> clazz) {
1088                 return getLastVersion(type, null, hasNotProps, clazz);
1089         }
1090
1091         protected <T extends GraphNode> Either<List<T>, TitanOperationStatus> getLastVersion(NodeTypeEnum type, Map<String, Object> hasProps, Map<String, Object> hasNotProps, Class<T> clazz) {
1092
1093                 Map<String, Object> props = new HashMap<>();
1094
1095                 if (hasProps != null) {
1096                         props.putAll(hasProps);
1097                 }
1098                 props.put(GraphPropertiesDictionary.IS_HIGHEST_VERSION.getProperty(), true);
1099
1100                 Either<List<T>, TitanOperationStatus> byCriteria = titanGenericDao.getByCriteria(type, props, hasNotProps, clazz);
1101
1102                 return byCriteria;
1103
1104         }
1105
1106         public <T, S extends GraphNode> Either<Set<T>, StorageOperationStatus> getComponentCatalogData(NodeTypeEnum type, Map<String, Object> propertiesToMatch, Class<T> clazz1, Class<S> clazz2, boolean inTransaction) {
1107                 log.debug("Start getComponentCatalogData for type: {}", type.name());
1108                 Set<T> result = new HashSet<T>();
1109                 Either<List<S>, TitanOperationStatus> lastVersionNodes = getLastVersion(type, propertiesToMatch, clazz2);
1110                 Either<Set<T>, StorageOperationStatus> last = retrieveComponentsFromNodes(lastVersionNodes, inTransaction);
1111                 if (last.isLeft() && last.left().value() != null) {
1112                         result.addAll(last.left().value());
1113                 }
1114                 if (type == NodeTypeEnum.Resource) {
1115                         propertiesToMatch.put(GraphPropertiesDictionary.IS_ABSTRACT.getProperty(), false);
1116                 }
1117                 Either<List<S>, TitanOperationStatus> componentsNodes = titanGenericDao.getByCriteria(type, propertiesToMatch, clazz2);
1118                 Either<Set<T>, StorageOperationStatus> certified = retrieveComponentsFromNodes(componentsNodes, inTransaction);
1119                 if (certified.isLeft() && certified.left().value() != null) {
1120                         result.addAll(certified.left().value());
1121                 }
1122                 return Either.left(result);
1123
1124         }
1125
1126         protected <T, S extends GraphNode> Either<Set<T>, StorageOperationStatus> retrieveComponentsFromNodes(Either<List<S>, TitanOperationStatus> componentsNodes, boolean inTransaction) {
1127                 Set<T> result = new HashSet<T>();
1128                 if (componentsNodes.isRight()) {
1129                         // in case of NOT_FOUND from Titan client return to UI empty list
1130                         if (componentsNodes.right().value().equals(TitanOperationStatus.NOT_FOUND)) {
1131                                 log.debug("No components were found");
1132                         } else {
1133                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(componentsNodes.right().value()));
1134                         }
1135                 } else {
1136                         List<S> componentDataList = componentsNodes.left().value();
1137                         for (S componentData : componentDataList) {
1138                                 // Either<T, StorageOperationStatus> component =
1139                                 // getComponent((String) componentData.getUniqueId(),
1140                                 // inTransaction);
1141                                 Either<T, StorageOperationStatus> component = getLightComponent((String) componentData.getUniqueId(), inTransaction);
1142                                 if (component.isRight()) {
1143                                         log.debug("Failed to get component for id =  {}  error : {} skip resource", componentData.getUniqueId(), component.right().value());
1144                                         // return Either.right(service.right().value());
1145                                 } else {
1146                                         result.add(component.left().value());
1147                                 }
1148                         }
1149                 }
1150                 return Either.left(result);
1151         }
1152
1153         protected StorageOperationStatus removeArtifactsFromComponent(Component component, NodeTypeEnum componentType) {
1154
1155                 String componentId = component.getUniqueId();
1156                 // Map<String, ArtifactDefinition> artifacts = component.getArtifacts();
1157                 Either<Map<String, ArtifactDefinition>, StorageOperationStatus> artifactsRes = artifactOperation.getArtifacts(componentId, componentType, true);
1158                 if (artifactsRes.isRight() && !artifactsRes.right().value().equals(StorageOperationStatus.NOT_FOUND)) {
1159                         return artifactsRes.right().value();
1160                 }
1161                 if (artifactsRes.isLeft() && artifactsRes.left().value() != null) {
1162                         Map<String, ArtifactDefinition> artifacts = artifactsRes.left().value();
1163                         for (Entry<String, ArtifactDefinition> entry : artifacts.entrySet()) {
1164
1165                                 ArtifactDefinition artifactDefinition = entry.getValue();
1166                                 Either<ArtifactDefinition, StorageOperationStatus> removeArifactFromResource = artifactOperation.removeArifactFromResource(componentId, artifactDefinition.getUniqueId(), componentType, true, true);
1167                                 if (removeArifactFromResource.isRight()) {
1168                                         return removeArifactFromResource.right().value();
1169                                 }
1170                         }
1171                 }
1172                 return StorageOperationStatus.OK;
1173         }
1174
1175         public Either<List<Component>, StorageOperationStatus> getTesterFollowedComponent(String userId, Set<LifecycleStateEnum> lifecycleStates, boolean inTransaction, NodeTypeEnum neededType) {
1176                 List<Component> resList = new ArrayList<>();
1177                 Either<List<Component>, StorageOperationStatus> rip = getFollowedComponent(userId, lifecycleStates, null, inTransaction, titanGenericDao, neededType);
1178                 if (rip.isLeft()) {
1179                         List<Component> ripRes = rip.left().value();
1180                         if (ripRes != null && !ripRes.isEmpty()) {
1181                                 resList.addAll(ripRes);
1182                         }
1183                         Set<LifecycleStateEnum> rfcState = new HashSet<>();
1184                         rfcState.add(LifecycleStateEnum.READY_FOR_CERTIFICATION);
1185                         Either<List<Component>, StorageOperationStatus> rfc = getFollowedComponent(null, rfcState, null, inTransaction, titanGenericDao, neededType);
1186                         if (rfc.isLeft()) {
1187                                 List<Component> rfcRes = rfc.left().value();
1188                                 if (rfcRes != null && !rfcRes.isEmpty()) {
1189                                         resList.addAll(rfcRes);
1190                                 }
1191                         } else {
1192                                 return Either.right(rfc.right().value());
1193                         }
1194
1195                 } else {
1196                         return Either.right(rip.right().value());
1197                 }
1198                 return Either.left(resList);
1199
1200         }
1201
1202         /**
1203          * generate UUID only for case that version is "XX.01" - (start new version)
1204          * 
1205          * @param component
1206          */
1207         protected void generateUUID(Component component) {
1208                 String version = component.getVersion();
1209                 if (uuidNewVersion.matcher(version).matches()) {
1210                         UUID uuid = UUID.randomUUID();
1211                         component.getComponentMetadataDefinition().getMetadataDataDefinition().setUUID(uuid.toString());
1212                         MDC.put("serviceInstanceID", uuid.toString());
1213                 }
1214         }
1215
1216         protected <T extends GraphNode> Either<Map<String, String>, TitanOperationStatus> getVersionList(NodeTypeEnum type, String version, Component component, Class<T> clazz) {
1217                 return getVersionList(type, version, component.getUUID(), component.getSystemName(), clazz);
1218         }
1219
1220         protected <T extends GraphNode> Either<Map<String, String>, TitanOperationStatus> getVersionList(NodeTypeEnum type, String version, String uuid, String systemName, Class<T> clazz) {
1221                 Map<String, Object> props = new HashMap<String, Object>();
1222                 Map<String, Object> hasNotProps = new HashMap<String, Object>();
1223
1224                 if (version.startsWith("0")) {
1225                         props.put(GraphPropertiesDictionary.UUID.getProperty(), uuid);
1226                 } else {
1227                         props.put(GraphPropertiesDictionary.SYSTEM_NAME.getProperty(), systemName);
1228                 }
1229                 hasNotProps.put(GraphPropertiesDictionary.IS_DELETED.getProperty(), true);
1230                 Either<List<T>, TitanOperationStatus> result = titanGenericDao.getByCriteria(type, props, hasNotProps, clazz);
1231
1232                 Map<String, String> versionMap = new HashMap<String, String>();
1233                 if (result.isRight()) {
1234                         if (!result.right().value().equals(TitanOperationStatus.NOT_FOUND)) {
1235                                 return Either.right(result.right().value());
1236                         }
1237
1238                 } else {
1239                         switch (type) {
1240                         case Resource:
1241                                 List<ResourceMetadataData> components = (List<ResourceMetadataData>) result.left().value();
1242                                 for (ResourceMetadataData data : components) {
1243                                         versionMap.put(data.getMetadataDataDefinition().getVersion(), (String) data.getUniqueId());
1244                                 }
1245                                 break;
1246                         case Service:
1247                                 List<ServiceMetadataData> componentsS = (List<ServiceMetadataData>) result.left().value();
1248                                 for (ServiceMetadataData data : componentsS) {
1249                                         versionMap.put(data.getMetadataDataDefinition().getVersion(), (String) data.getUniqueId());
1250                                 }
1251                                 break;
1252                         case Product:
1253                                 List<ProductMetadataData> componentsP = (List<ProductMetadataData>) result.left().value();
1254                                 for (ProductMetadataData data : componentsP) {
1255                                         versionMap.put(data.getMetadataDataDefinition().getVersion(), (String) data.getUniqueId());
1256                                 }
1257                                 break;
1258                         default:
1259                                 break;
1260                         }
1261                 }
1262
1263                 return Either.left(versionMap);
1264         }
1265
1266         protected StorageOperationStatus deleteAdditionalInformation(NodeTypeEnum nodeType, String componentId) {
1267
1268                 Either<AdditionalInformationDefinition, StorageOperationStatus> deleteRes = additionalInformationOperation.deleteAllAdditionalInformationParameters(nodeType, componentId, true);
1269
1270                 if (deleteRes.isRight()) {
1271                         StorageOperationStatus status = deleteRes.right().value();
1272                         return status;
1273                 }
1274
1275                 return StorageOperationStatus.OK;
1276
1277         }
1278
1279         protected StorageOperationStatus addAdditionalInformation(NodeTypeEnum nodeType, String componentId, AdditionalInformationDefinition informationDefinition) {
1280
1281                 Either<AdditionalInformationDefinition, TitanOperationStatus> status = additionalInformationOperation.addAdditionalInformationNode(nodeType, componentId, informationDefinition);
1282
1283                 if (status.isRight()) {
1284                         TitanOperationStatus titanStatus = status.right().value();
1285                         return DaoStatusConverter.convertTitanStatusToStorageStatus(titanStatus);
1286                 }
1287
1288                 log.trace("After adding additional information to component {}. Result is {}" , componentId ,status.left().value());
1289
1290                 return StorageOperationStatus.OK;
1291
1292         }
1293
1294         protected StorageOperationStatus addAdditionalInformation(NodeTypeEnum nodeType, String componentId, AdditionalInformationDefinition informationDefinition, TitanVertex metadataVertex) {
1295
1296                 TitanOperationStatus status = additionalInformationOperation.addAdditionalInformationNode(nodeType, componentId, informationDefinition, metadataVertex);
1297                 log.trace("After adding additional information to component {}. Result is {}", componentId, status);
1298
1299                 if (!status.equals(TitanOperationStatus.OK)) {
1300                         return DaoStatusConverter.convertTitanStatusToStorageStatus(status);
1301                 }
1302
1303                 return StorageOperationStatus.OK;
1304
1305         }
1306
1307         public Either<List<ArtifactDefinition>, StorageOperationStatus> getComponentArtifactsForDelete(String parentId, NodeTypeEnum parentType, boolean inTransacton) {
1308                 List<ArtifactDefinition> artifacts = new ArrayList<ArtifactDefinition>();
1309                 Either<Map<String, ArtifactDefinition>, StorageOperationStatus> artifactsResponse = artifactOperation.getArtifacts(parentId, parentType, inTransacton);
1310                 if (artifactsResponse.isRight()) {
1311                         if (!artifactsResponse.right().value().equals(StorageOperationStatus.NOT_FOUND)) {
1312                                 log.debug("failed to retrieve artifacts for {} {}", parentType, parentId);
1313                                 return Either.right(artifactsResponse.right().value());
1314                         }
1315                 } else {
1316                         artifacts.addAll(artifactsResponse.left().value().values());
1317                 }
1318
1319                 if (NodeTypeEnum.Resource.equals(parentType)) {
1320                         Either<List<ArtifactDefinition>, StorageOperationStatus> interfacesArtifactsForResource = getAdditionalArtifacts(parentId, false, true);
1321                         if (artifactsResponse.isRight() && !interfacesArtifactsForResource.right().value().equals(StorageOperationStatus.NOT_FOUND)) {
1322                                 log.debug("failed to retrieve interface artifacts for {} {}", parentType, parentId);
1323                                 return Either.right(interfacesArtifactsForResource.right().value());
1324                         } else if (artifactsResponse.isLeft()) {
1325                                 artifacts.addAll(interfacesArtifactsForResource.left().value());
1326                         }
1327                 }
1328                 return Either.left(artifacts);
1329         }
1330
1331         protected void addComponentInternalFields(ComponentMetadataData componentMetadataData) {
1332                 org.openecomp.sdc.be.datatypes.components.ComponentMetadataDataDefinition metadataDataDefinition = componentMetadataData.getMetadataDataDefinition();
1333                 Long creationDate = metadataDataDefinition.getCreationDate();
1334
1335                 long currentDate = System.currentTimeMillis();
1336                 if (creationDate == null) {
1337                         metadataDataDefinition.setCreationDate(currentDate);
1338                 }
1339                 metadataDataDefinition.setLastUpdateDate(currentDate);
1340
1341                 String lifecycleStateEnum = metadataDataDefinition.getState();
1342                 if (lifecycleStateEnum == null) {
1343                         metadataDataDefinition.setState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name());
1344                 }
1345                 String componentUniqueId = UniqueIdBuilder.buildComponentUniqueId();
1346                 metadataDataDefinition.setUniqueId(componentUniqueId);
1347                 metadataDataDefinition.setHighestVersion(true);
1348         }
1349
1350         protected StorageOperationStatus createTagsForComponent(Component component) {
1351                 List<String> tags = component.getTags();
1352                 if (tags != null && false == tags.isEmpty()) {
1353                         Either<List<TagData>, StorageOperationStatus> tagsResult = createNewTagsList(tags);
1354
1355                         if (tagsResult == null) {
1356                                 log.debug("tagsResult is null");
1357                                 return StorageOperationStatus.GENERAL_ERROR;
1358                         }
1359                         if (tagsResult.isRight()) {
1360                                 return tagsResult.right().value();
1361                         }
1362                         List<TagData> tagsToCreate = tagsResult.left().value();
1363                         return createTagNodesOnGraph(tagsToCreate);
1364                 }
1365                 log.trace("All tags created succesfully for component {}", component.getUniqueId());
1366                 return StorageOperationStatus.OK;
1367         }
1368
1369         protected Either<List<GroupingData>, StorageOperationStatus> findGroupingsForComponent(NodeTypeEnum nodeTypeEnum, Component component) {
1370                 List<CategoryDefinition> categories = component.getCategories();
1371                 List<GroupingData> groupingDataToAssociate = new ArrayList<>();
1372                 if (categories != null) {
1373                         groupingDataToAssociate = new ArrayList<>();
1374                         for (CategoryDefinition categoryDefinition : categories) {
1375                                 List<SubCategoryDefinition> subcategories = categoryDefinition.getSubcategories();
1376                                 if (subcategories != null) {
1377                                         for (SubCategoryDefinition subCategoryDefinition : subcategories) {
1378                                                 List<GroupingDefinition> groupingDataDefinitions = subCategoryDefinition.getGroupings();
1379                                                 if (groupingDataDefinitions != null) {
1380                                                         for (GroupingDataDefinition grouping : groupingDataDefinitions) {
1381                                                                 String groupingId = grouping.getUniqueId();
1382                                                                 Either<GroupingData, TitanOperationStatus> findGroupingEither = findGrouping(nodeTypeEnum, groupingId);
1383                                                                 if (findGroupingEither.isRight()) {
1384                                                                         TitanOperationStatus status = findGroupingEither.right().value();
1385                                                                         log.error("Cannot find grouping {} in the graph. status is {}", groupingId, status);
1386                                                                         if (status == TitanOperationStatus.NOT_FOUND) {
1387                                                                                 return Either.right(StorageOperationStatus.CATEGORY_NOT_FOUND);
1388                                                                         }
1389                                                                         return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1390                                                                 } else {
1391                                                                         groupingDataToAssociate.add(findGroupingEither.left().value());
1392                                                                 }
1393                                                         }
1394                                                 }
1395                                         }
1396                                 }
1397                         }
1398                 }
1399                 return Either.left(groupingDataToAssociate);
1400         }
1401
1402         protected TitanOperationStatus associateGroupingsToComponent(ComponentMetadataData componentMetadataData, List<GroupingData> groupingDataToAssociate) {
1403                 for (GroupingData groupingData : groupingDataToAssociate) {
1404                         GraphEdgeLabels groupingLabel = GraphEdgeLabels.GROUPING;
1405                         Either<GraphRelation, TitanOperationStatus> result = titanGenericDao.createRelation(componentMetadataData, groupingData, groupingLabel, null);
1406                         log.debug("After associating grouping {} to component {}. Edge type is {}", groupingData, componentMetadataData, groupingLabel);
1407                         if (result.isRight()) {
1408                                 return result.right().value();
1409                         }
1410                 }
1411                 log.trace("All groupings associated succesfully to component {}", componentMetadataData);
1412                 return TitanOperationStatus.OK;
1413         }
1414
1415         public abstract Either<Integer, StorageOperationStatus> increaseAndGetComponentInstanceCounter(String componentId, boolean inTransaction);
1416
1417         protected Either<Integer, StorageOperationStatus> increaseAndGetComponentInstanceCounter(String componentId, NodeTypeEnum nodeType, boolean inTransaction) {
1418                 Either<Integer, StorageOperationStatus> result = null;
1419                 try {
1420
1421                         Either<TitanGraph, TitanOperationStatus> graphResult = titanGenericDao.getGraph();
1422                         if (graphResult.isRight()) {
1423                                 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(graphResult.right().value()));
1424                                 return result;
1425                         }
1426                         Either<TitanVertex, TitanOperationStatus> vertexService = titanGenericDao.getVertexByProperty(UniqueIdBuilder.getKeyByNodeType(nodeType), componentId);
1427                         if (vertexService.isRight()) {
1428                                 log.debug("failed to fetch vertex of component metadata, nodeType:{} , id: {}", nodeType, componentId);
1429                                 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(vertexService.right().value()));
1430                                 return result;
1431                         }
1432                         Vertex vertex = vertexService.left().value();
1433                         Integer instanceCounter = vertex.value(GraphPropertiesDictionary.INSTANCE_COUNTER.getProperty());
1434                         ++instanceCounter;
1435                         vertex.property(GraphPropertiesDictionary.INSTANCE_COUNTER.getProperty(), instanceCounter);
1436                         result = Either.left(instanceCounter);
1437                         return result;
1438
1439                 } finally {
1440                         if (false == inTransaction) {
1441                                 if (result == null || result.isRight()) {
1442                                         log.debug("increaseAndGetComponentInstanceCounter operation : Going to execute rollback on graph.");
1443                                         titanGenericDao.rollback();
1444                                 } else {
1445                                         log.debug("increaseAndGetComponentInstanceCounter operation : Going to execute commit on graph.");
1446                                         titanGenericDao.commit();
1447                                 }
1448                         }
1449                 }
1450
1451         }
1452
1453         protected Either<Integer, StorageOperationStatus> setComponentInstanceCounter(String componentId, NodeTypeEnum nodeType, int counter, boolean inTransaction) {
1454                 Either<Integer, StorageOperationStatus> result = null;
1455                 try {
1456
1457                         Either<TitanGraph, TitanOperationStatus> graphResult = titanGenericDao.getGraph();
1458                         if (graphResult.isRight()) {
1459                                 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(graphResult.right().value()));
1460                                 return result;
1461                         }
1462                         Either<TitanVertex, TitanOperationStatus> vertexService = titanGenericDao.getVertexByProperty(UniqueIdBuilder.getKeyByNodeType(nodeType), componentId);
1463                         if (vertexService.isRight()) {
1464                                 log.debug("failed to fetch vertex of component metadata ofor id = {}", componentId);
1465                                 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(vertexService.right().value()));
1466                                 return result;
1467                         }
1468                         Vertex vertex = vertexService.left().value();
1469                         vertex.property(GraphPropertiesDictionary.INSTANCE_COUNTER.getProperty(), counter);
1470                         result = Either.left(counter);
1471                         return result;
1472
1473                 } finally {
1474                         if (false == inTransaction) {
1475                                 if (result == null || result.isRight()) {
1476                                         log.debug("deleteService operation : Going to execute rollback on graph.");
1477                                         titanGenericDao.rollback();
1478                                 } else {
1479                                         log.debug("deleteService operation : Going to execute commit on graph.");
1480                                         titanGenericDao.commit();
1481                                 }
1482                         }
1483                 }
1484
1485         }
1486
1487         protected TitanOperationStatus setComponentInstancesFromGraph(String uniqueId, Component component, NodeTypeEnum containerNodeType, NodeTypeEnum compInstNodeType) {
1488
1489                 Either<ImmutablePair<List<ComponentInstance>, List<RequirementCapabilityRelDef>>, TitanOperationStatus> resourceInstancesOfService = componentInstanceOperation.getComponentInstancesOfComponent(uniqueId, containerNodeType, compInstNodeType);
1490
1491                 if (resourceInstancesOfService.isRight()) {
1492                         TitanOperationStatus status = resourceInstancesOfService.right().value();
1493                         if (status == TitanOperationStatus.NOT_FOUND) {
1494                                 status = TitanOperationStatus.OK;
1495                         } else {
1496                                 log.error("Failed to fetch resource instances and their relations. status is {}", status);
1497                         }
1498                         return status;
1499                 }
1500
1501                 ImmutablePair<List<ComponentInstance>, List<RequirementCapabilityRelDef>> immutablePair = resourceInstancesOfService.left().value();
1502                 List<ComponentInstance> instances = immutablePair.getKey();
1503                 List<RequirementCapabilityRelDef> relations = immutablePair.getValue();
1504
1505                 component.setComponentInstances(instances);
1506                 component.setComponentInstancesRelations(relations);
1507
1508                 return TitanOperationStatus.OK;
1509         }
1510
1511         /**
1512          * set all properties of all of its resources
1513          * 
1514          * @param uniqueId
1515          * @return
1516          */
1517         protected TitanOperationStatus ___setComponentInstancesPropertiesFromGraph(String uniqueId, Component component) {
1518
1519                 List<ComponentInstance> resourceInstances = component.getComponentInstances();
1520
1521                 Map<String, List<ComponentInstanceProperty>> resourceInstancesProperties = new HashMap<>();
1522
1523                 Map<String, List<PropertyDefinition>> alreadyProcessedResources = new HashMap<>();
1524
1525                 if (resourceInstances != null) {
1526                         for (ComponentInstance resourceInstance : resourceInstances) {
1527
1528                                 log.debug("Going to update properties of resource instance {}", resourceInstance.getUniqueId());
1529                                 String resourceUid = resourceInstance.getComponentUid();
1530
1531                                 List<PropertyDefinition> properties = alreadyProcessedResources.get(resourceUid);
1532                                 if (properties == null) {
1533                                         properties = new ArrayList<>();
1534                                         TitanOperationStatus findAllRes = propertyOperation.findAllResourcePropertiesRecursively(resourceUid, properties);
1535                                         if (findAllRes != TitanOperationStatus.OK) {
1536                                                 return findAllRes;
1537                                         }
1538                                         alreadyProcessedResources.put(resourceUid, properties);
1539                                 }
1540                                 log.debug("After getting properties of resource {}. Number of properties is {}", resourceUid, (properties == null ? 0 : properties.size()));
1541                                 if (false == properties.isEmpty()) {
1542
1543                                         String resourceInstanceUid = resourceInstance.getUniqueId();
1544
1545                                         Either<List<ComponentInstanceProperty>, TitanOperationStatus> propertyValuesRes = propertyOperation.getAllPropertiesOfResourceInstanceOnlyPropertyDefId(resourceInstanceUid);
1546                                         log.debug("After fetching property under resource instance {}", resourceInstanceUid);
1547                                         if (propertyValuesRes.isRight()) {
1548                                                 TitanOperationStatus status = propertyValuesRes.right().value();
1549                                                 if (status != TitanOperationStatus.NOT_FOUND) {
1550                                                         return status;
1551                                                 }
1552                                         }
1553
1554                                         Map<String, ComponentInstanceProperty> propertyIdToValue = new HashMap<>();
1555                                         populateMapperWithPropertyValues(propertyValuesRes, propertyIdToValue);
1556
1557                                         List<ComponentInstanceProperty> resourceInstancePropertyList = new ArrayList<>();
1558                                         for (PropertyDefinition propertyDefinition : properties) {
1559
1560                                                 String defaultValue = propertyDefinition.getDefaultValue();
1561                                                 String value = defaultValue;
1562                                                 String valueUid = null;
1563
1564                                                 String propertyId = propertyDefinition.getUniqueId();
1565                                                 ComponentInstanceProperty valuedProperty = propertyIdToValue.get(propertyId);
1566                                                 if (valuedProperty != null) {
1567                                                         String newValue = valuedProperty.getValue();
1568                                                         // if (newValue != null) {
1569                                                         value = newValue;
1570                                                         // }
1571
1572                                                         valueUid = valuedProperty.getValueUniqueUid();
1573                                                         log.trace("Found value {} under resource instance which override the default value {}" , value, defaultValue);
1574                                                 }
1575                                                 ComponentInstanceProperty resourceInstanceProperty = new ComponentInstanceProperty(propertyDefinition, value, valueUid);
1576
1577                                                 // TODO: currently ignore constraints since they are not
1578                                                 // inuse and cause to error in convertion to object.
1579                                                 resourceInstanceProperty.setConstraints(null);
1580
1581                                                 resourceInstancePropertyList.add(resourceInstanceProperty);
1582
1583                                         }
1584
1585                                         resourceInstancesProperties.put(resourceInstanceUid, resourceInstancePropertyList);
1586                                 }
1587
1588                         }
1589
1590                         component.setComponentInstancesProperties(resourceInstancesProperties);
1591                 }
1592
1593                 return TitanOperationStatus.OK;
1594         }
1595
1596         private void populateMapperWithPropertyValues(Either<List<ComponentInstanceProperty>, TitanOperationStatus> propertyValuesRes, Map<String, ComponentInstanceProperty> propertyIdToValue) {
1597
1598                 if (propertyValuesRes.isLeft()) {
1599                         List<ComponentInstanceProperty> resourceInstanceValues = propertyValuesRes.left().value();
1600                         if (resourceInstanceValues != null) {
1601                                 for (ComponentInstanceProperty resourceInstanceProperty : resourceInstanceValues) {
1602                                         propertyIdToValue.put(resourceInstanceProperty.getUniqueId(), resourceInstanceProperty);
1603                                 }
1604                         }
1605                 }
1606         }
1607
1608         public abstract Either<List<ArtifactDefinition>, StorageOperationStatus> getAdditionalArtifacts(String resourceId, boolean recursively, boolean inTransaction);
1609
1610         protected abstract StorageOperationStatus validateCategories(Component currentComponent, Component component, ComponentMetadataData componentData, NodeTypeEnum type);
1611
1612         protected abstract <T extends Component> StorageOperationStatus updateDerived(Component component, Component currentComponent, ComponentMetadataData updatedResourceData, Class<T> clazz);
1613
1614         public abstract Either<Component, StorageOperationStatus> markComponentToDelete(Component componentToDelete, boolean inTransaction);
1615
1616         protected Either<Component, StorageOperationStatus> internalMarkComponentToDelete(Component componentToDelete, boolean inTransaction) {
1617                 Either<Component, StorageOperationStatus> result = null;
1618
1619                 if ((componentToDelete.getIsDeleted() != null) && componentToDelete.getIsDeleted() && !componentToDelete.isHighestVersion()) {
1620                         // component already marked for delete
1621                         result = Either.left(componentToDelete);
1622                         return result;
1623                 } else {
1624
1625                         ComponentMetadataData componentMetaData = getMetaDataFromComponent(componentToDelete);
1626
1627                         componentMetaData.getMetadataDataDefinition().setIsDeleted(true);
1628                         componentMetaData.getMetadataDataDefinition().setHighestVersion(false);
1629                         componentMetaData.getMetadataDataDefinition().setLastUpdateDate(System.currentTimeMillis());
1630                         try {
1631                                 Either<ComponentMetadataData, TitanOperationStatus> updateNode = titanGenericDao.updateNode(componentMetaData, ComponentMetadataData.class);
1632
1633                                 StorageOperationStatus updateComponent;
1634                                 if (updateNode.isRight()) {
1635                                         log.debug("Failed to update component {}. status is {}", componentMetaData.getUniqueId(), updateNode.right().value());
1636                                         updateComponent = DaoStatusConverter.convertTitanStatusToStorageStatus(updateNode.right().value());
1637                                         result = Either.right(updateComponent);
1638                                         return result;
1639                                 }
1640
1641                                 result = Either.left(componentToDelete);
1642                                 return result;
1643                         } finally {
1644
1645                                 if (false == inTransaction) {
1646                                         if (result == null || result.isRight()) {
1647                                                 log.error("updateResource operation : Going to execute rollback on graph.");
1648                                                 titanGenericDao.rollback();
1649                                         } else {
1650                                                 log.debug("updateResource operation : Going to execute commit on graph.");
1651                                                 titanGenericDao.commit();
1652                                         }
1653                                 }
1654
1655                         }
1656                 }
1657         }
1658
1659         private Either<List<RequirementDefinition>, TitanOperationStatus> convertReqDataListToReqDefList(ComponentInstance componentInstance, List<ImmutablePair<RequirementData, GraphEdge>> requirementData) {
1660                 ConvertDataToDef<RequirementDefinition, RequirementData> convertor = (instance, data, edge) -> convertReqDataToReqDef(instance, data, edge);
1661                 AddOwnerData<RequirementDefinition> dataAdder = (reqDef, compInstance) -> addOwnerDataReq(reqDef, compInstance);
1662                 return convertDataToDefinition(componentInstance, requirementData, convertor, dataAdder);
1663         }
1664
1665         private Either<List<CapabilityDefinition>, TitanOperationStatus> convertCapDataListToCapDefList(ComponentInstance componentInstance, List<ImmutablePair<CapabilityData, GraphEdge>> capabilityData) {
1666                 ConvertDataToDef<CapabilityDefinition, CapabilityData> convertor = (instance, data, edge) -> convertCapDataToCapDef(instance, data, edge);
1667                 AddOwnerData<CapabilityDefinition> dataAdder = (capDef, compInstance) -> addOwnerDataCap(capDef, compInstance);
1668                 Either<List<CapabilityDefinition>, TitanOperationStatus> convertationResult = convertDataToDefinition(componentInstance, capabilityData, convertor, dataAdder);
1669                 if (convertationResult.isLeft()) {
1670                         convertationResult = componentInstanceOperation.updateCapDefPropertyValues(componentInstance, convertationResult.left().value());
1671                 }
1672                 return convertationResult;
1673         }
1674
1675         private Either<CapabilityDefinition, TitanOperationStatus> convertCapDataToCapDef(ComponentInstance componentInstance, CapabilityData data, GraphEdge edge) {
1676                 Either<CapabilityDefinition, TitanOperationStatus> eitherDef = capabilityOperation.getCapabilityByCapabilityData(data);
1677
1678                 if (eitherDef.isLeft()) {
1679                         CapabilityDefinition capDef = eitherDef.left().value();
1680                         Map<String, Object> properties = edge.getProperties();
1681                         if (properties != null) {
1682                                 String name = (String) properties.get(GraphEdgePropertiesDictionary.NAME.getProperty());
1683                                 String source = (String) properties.get(GraphEdgePropertiesDictionary.SOURCE.getProperty());
1684                                 List<String> sourcesList = new ArrayList<String>();
1685                                 capabilityOperation.getCapabilitySourcesList(source, sourcesList);
1686                                 capDef.setName(name);
1687                                 capDef.setCapabilitySources(sourcesList);
1688                                 capDef.setPath(toscaDefinitionPathCalculator.calculateToscaDefinitionPath(componentInstance, edge));
1689
1690                                 String requiredOccurrences = (String) properties.get(GraphEdgePropertiesDictionary.REQUIRED_OCCURRENCES.getProperty());
1691                                 if (requiredOccurrences != null) {
1692                                         capDef.setMinOccurrences(requiredOccurrences);
1693                                 }
1694                                 String leftOccurrences = (String) properties.get(GraphEdgePropertiesDictionary.LEFT_OCCURRENCES.getProperty());
1695                                 if (leftOccurrences != null) {
1696                                         capDef.setMaxOccurrences(leftOccurrences);
1697                                 }
1698
1699                         }
1700                         eitherDef = Either.left(capDef);
1701                 }
1702                 return eitherDef;
1703         }
1704
1705         private Either<RequirementDefinition, TitanOperationStatus> convertReqDataToReqDef(ComponentInstance componentInstance, RequirementData data, GraphEdge edge) {
1706                 Either<RequirementDefinition, TitanOperationStatus> eitherDef = requirementOperation.getRequirement(data.getUniqueId());
1707
1708                 if (eitherDef.isLeft()) {
1709                         RequirementDefinition requirementDef = eitherDef.left().value();
1710                         Map<String, Object> properties = edge.getProperties();
1711                         if (properties != null) {
1712                                 String name = (String) properties.get(GraphEdgePropertiesDictionary.NAME.getProperty());
1713                                 requirementDef.setName(name);
1714                                 String requiredOccurrences = (String) properties.get(GraphEdgePropertiesDictionary.REQUIRED_OCCURRENCES.getProperty());
1715                                 if (requiredOccurrences != null) {
1716                                         requirementDef.setMinOccurrences(requiredOccurrences);
1717                                 }
1718                                 requirementDef.setPath(toscaDefinitionPathCalculator.calculateToscaDefinitionPath(componentInstance, edge));
1719                                 String leftOccurrences = (String) properties.get(GraphEdgePropertiesDictionary.LEFT_OCCURRENCES.getProperty());
1720                                 if (leftOccurrences != null) {
1721                                         requirementDef.setMaxOccurrences(leftOccurrences);
1722                                 }
1723                         }
1724                         eitherDef = Either.left(requirementDef);
1725                 }
1726                 return eitherDef;
1727         }
1728
1729         private <Def, Data> Either<List<Def>, TitanOperationStatus> convertDataToDefinition(ComponentInstance componentInstance, List<ImmutablePair<Data, GraphEdge>> requirementData, ConvertDataToDef<Def, Data> convertor, AddOwnerData<Def> dataAdder) {
1730                 Either<List<Def>, TitanOperationStatus> eitherResult;
1731                 // Convert Data To Definition
1732                 Stream<Either<Def, TitanOperationStatus>> reqDefStream = requirementData.stream().map(e -> convertor.convert(componentInstance, e.left, e.right));
1733
1734                 // Collect But Stop After First Error
1735                 List<Either<Def, TitanOperationStatus>> filteredReqDefList = StreamUtils.takeWhilePlusOne(reqDefStream, p -> p.isLeft()).collect(Collectors.toList());
1736                 Optional<Either<Def, TitanOperationStatus>> optionalError = filteredReqDefList.stream().filter(p -> p.isRight()).findAny();
1737                 if (optionalError.isPresent()) {
1738                         eitherResult = Either.right(optionalError.get().right().value());
1739                 } else {
1740                         // Convert From Either To Definition And Collect
1741                         List<Def> reqDefList = filteredReqDefList.stream().map(e -> e.left().value()).collect(Collectors.toList());
1742                         // Add Owner Data
1743                         reqDefList.forEach(e -> dataAdder.addData(e, componentInstance));
1744                         eitherResult = Either.left(reqDefList);
1745                 }
1746
1747                 return eitherResult;
1748         }
1749
1750         interface ConvertDataToDef<Def, Data> {
1751                 Either<Def, TitanOperationStatus> convert(ComponentInstance compInstance, Data d, GraphEdge edge);
1752         }
1753
1754         interface AddOwnerData<Def> {
1755                 void addData(Def def, ComponentInstance compInstance);
1756         }
1757
1758         private void addOwnerDataCap(CapabilityDefinition capDef, ComponentInstance componentInstance) {
1759                 capDef.setOwnerId(componentInstance.getUniqueId());
1760                 capDef.setOwnerName(componentInstance.getName());
1761         }
1762
1763         private void addOwnerDataReq(RequirementDefinition reqDef, ComponentInstance componentInstance) {
1764                 reqDef.setOwnerId(componentInstance.getUniqueId());
1765                 reqDef.setOwnerName(componentInstance.getName());
1766         }
1767
1768         public Either<Map<String, List<RequirementDefinition>>, TitanOperationStatus> getRequirements(Component component, NodeTypeEnum nodeTypeEnum, boolean inTransaction) {
1769                 final HashMap<String, List<RequirementDefinition>> emptyMap = new HashMap<>();
1770                 Either<Map<String, List<RequirementDefinition>>, TitanOperationStatus> eitherResult = Either.left(emptyMap);
1771                 try {
1772                         List<ComponentInstance> componentInstances = component.getComponentInstances();
1773                         if (componentInstances != null) {
1774                                 Function<ComponentInstance, Either<List<ImmutablePair<RequirementData, GraphEdge>>, TitanOperationStatus>> dataCollector = e -> componentInstanceOperation.getRequirements(e, nodeTypeEnum);
1775                                 Either<List<ImmutablePair<ComponentInstance, Either<List<ImmutablePair<RequirementData, GraphEdge>>, TitanOperationStatus>>>, TitanOperationStatus> eitherDataCollected = collectDataFromComponentsInstances(componentInstances,
1776                                                 dataCollector);
1777                                 if (eitherDataCollected.isRight()) {
1778                                         eitherResult = Either.right(eitherDataCollected.right().value());
1779                                 } else {
1780                                         // Converts Data to Def stop if encountered conversion error
1781                                         DataDefConvertor<RequirementDefinition, RequirementData> someConvertor = (e1, e2) -> convertReqDataListToReqDefList(e1, e2);
1782                                         Either<List<List<RequirementDefinition>>, TitanOperationStatus> fullDefList = convertDataToDefComponentLevel(eitherDataCollected.left().value(), someConvertor);
1783                                         if (fullDefList.isRight()) {
1784                                                 eitherResult = Either.right(fullDefList.right().value());
1785                                         } else {
1786                                                 Stream<RequirementDefinition> defStream = fullDefList.left().value().stream().flatMap(e -> e.stream());
1787                                                 // Collect to Map and using grouping by
1788                                                 Map<String, List<RequirementDefinition>> capTypeCapListMap = defStream.collect(Collectors.groupingBy(e -> e.getCapability()));
1789                                                 eitherResult = Either.left(capTypeCapListMap);
1790                                         }
1791
1792                                 }
1793
1794                         }
1795                 } finally {
1796                         if (inTransaction == false) {
1797                                 titanGenericDao.commit();
1798                         }
1799                 }
1800
1801                 return eitherResult;
1802         }
1803
1804         public Either<Map<String, List<CapabilityDefinition>>, TitanOperationStatus> getCapabilities(Component component, NodeTypeEnum nodeTypeEnum, boolean inTransaction) {
1805                 final HashMap<String, List<CapabilityDefinition>> emptyMap = new HashMap<>();
1806                 Either<Map<String, List<CapabilityDefinition>>, TitanOperationStatus> eitherResult = Either.left(emptyMap);
1807                 try {
1808                         List<ComponentInstance> componentInstances = component.getComponentInstances();
1809                         if (componentInstances != null) {
1810                                 Function<ComponentInstance, Either<List<ImmutablePair<CapabilityData, GraphEdge>>, TitanOperationStatus>> dataCollector = e -> componentInstanceOperation.getCapabilities(e, nodeTypeEnum);
1811                                 Either<List<ImmutablePair<ComponentInstance, Either<List<ImmutablePair<CapabilityData, GraphEdge>>, TitanOperationStatus>>>, TitanOperationStatus> eitherDataCollected = collectDataFromComponentsInstances(componentInstances,
1812                                                 dataCollector);
1813                                 if (eitherDataCollected.isRight()) {
1814                                         eitherResult = Either.right(eitherDataCollected.right().value());
1815                                 } else {
1816                                         // Converts CapData to CapDef removes stop if encountered
1817                                         // conversion error
1818                                         DataDefConvertor<CapabilityDefinition, CapabilityData> someConvertor = (e1, e2) -> convertCapDataListToCapDefList(e1, e2);
1819                                         Either<List<List<CapabilityDefinition>>, TitanOperationStatus> fullDefList = convertDataToDefComponentLevel(eitherDataCollected.left().value(), someConvertor);
1820                                         if (fullDefList.isRight()) {
1821                                                 eitherResult = Either.right(fullDefList.right().value());
1822                                         } else {
1823                                                 Stream<CapabilityDefinition> defStream = fullDefList.left().value().stream().flatMap(e -> e.stream());
1824                                                 // Collect to Map grouping by Type
1825                                                 Map<String, List<CapabilityDefinition>> capTypeCapListMap = defStream.collect(Collectors.groupingBy(e -> e.getType()));
1826                                                 eitherResult = Either.left(capTypeCapListMap);
1827                                         }
1828
1829                                 }
1830
1831                         }
1832                 } finally {
1833                         if (inTransaction == false) {
1834                                 titanGenericDao.commit();
1835                         }
1836                 }
1837
1838                 return eitherResult;
1839         }
1840
1841         public <Data> Either<List<ImmutablePair<ComponentInstance, Either<List<ImmutablePair<Data, GraphEdge>>, TitanOperationStatus>>>, TitanOperationStatus> collectDataFromComponentsInstances(List<ComponentInstance> componentInstances,
1842                         Function<ComponentInstance, Either<List<ImmutablePair<Data, GraphEdge>>, TitanOperationStatus>> dataGetter) {
1843                 Either<List<ImmutablePair<ComponentInstance, Either<List<ImmutablePair<Data, GraphEdge>>, TitanOperationStatus>>>, TitanOperationStatus> eitherResult;
1844
1845                 // Get List of Each componentInstance and it's Capabilities Data
1846                 Stream<ImmutablePair<ComponentInstance, Either<List<ImmutablePair<Data, GraphEdge>>, TitanOperationStatus>>> ownerDataStream = componentInstances.stream().map(element -> new ImmutablePair<>(element, dataGetter.apply(element)));
1847                 // Collect but stop after first error
1848                 List<ImmutablePair<ComponentInstance, Either<List<ImmutablePair<Data, GraphEdge>>, TitanOperationStatus>>> ownerCapDataList = StreamUtils
1849                                 .takeWhilePlusOne(ownerDataStream, p -> p.right.isLeft() || p.right.isRight() && p.right.right().value() == TitanOperationStatus.NOT_FOUND).collect(Collectors.toList());
1850
1851                 Optional<ImmutablePair<ComponentInstance, Either<List<ImmutablePair<Data, GraphEdge>>, TitanOperationStatus>>> optionalError = ownerCapDataList.stream()
1852                                 .filter(p -> p.right.isRight() && p.right.right().value() != TitanOperationStatus.NOT_FOUND).findAny();
1853                 if (optionalError.isPresent()) {
1854                         eitherResult = Either.right(optionalError.get().right.right().value());
1855                 } else {
1856                         eitherResult = Either.left(ownerCapDataList.stream().filter(p -> p.right.isLeft()).collect(Collectors.toList()));
1857                 }
1858
1859                 return eitherResult;
1860         }
1861
1862         interface DataDefConvertor<Def, Data> {
1863                 Either<List<Def>, TitanOperationStatus> convertDataToDefComponentInstance(ComponentInstance componentInstance, List<ImmutablePair<Data, GraphEdge>> data);
1864         }
1865
1866         public <Def, Data> Either<List<List<Def>>, TitanOperationStatus> convertDataToDefComponentLevel(List<ImmutablePair<ComponentInstance, Either<List<ImmutablePair<Data, GraphEdge>>, TitanOperationStatus>>> ownerCapDataList,
1867                         DataDefConvertor<Def, Data> convertor) {
1868                 // Converts CapData to CapDef removes stop if encountered conversion
1869                 // error
1870                 TitanOperationStatus error = null;
1871                 List<List<Def>> defList = new ArrayList<>();
1872                 for (int i = 0; i < ownerCapDataList.size(); i++) {
1873                         ImmutablePair<ComponentInstance, Either<List<ImmutablePair<Data, GraphEdge>>, TitanOperationStatus>> immutablePair = ownerCapDataList.get(i);
1874                         Either<List<Def>, TitanOperationStatus> convertCapDataListToCapDefList = convertor.convertDataToDefComponentInstance(immutablePair.left, immutablePair.right.left().value());
1875                         if (convertCapDataListToCapDefList.isRight()) {
1876                                 error = convertCapDataListToCapDefList.right().value();
1877                                 break;
1878                         } else {
1879                                 defList.add(convertCapDataListToCapDefList.left().value());
1880                         }
1881
1882                 }
1883                 Either<List<List<Def>>, TitanOperationStatus> eitherResult = (error != null) ? Either.right(error) : Either.left(defList);
1884                 return eitherResult;
1885
1886         }
1887
1888         private Map<String, ComponentMetadataData> findLatestVersion(List<ComponentMetadataData> resourceDataList) {
1889                 Map<Pair<String, String>, ComponentMetadataData> latestVersionMap = new HashMap<Pair<String, String>, ComponentMetadataData>();
1890                 for (ComponentMetadataData resourceData : resourceDataList) {
1891                         ComponentMetadataData latestVersionData = resourceData;
1892
1893                         ComponentMetadataDataDefinition metadataDataDefinition = resourceData.getMetadataDataDefinition();
1894                         Pair<String, String> pair = createKeyPair(latestVersionData);
1895                         if (latestVersionMap.containsKey(pair)) {
1896                                 latestVersionData = latestVersionMap.get(pair);
1897                                 String currentVersion = latestVersionData.getMetadataDataDefinition().getVersion();
1898                                 String newVersion = metadataDataDefinition.getVersion();
1899                                 if (CommonBeUtils.compareAsdcComponentVersions(newVersion, currentVersion)) {
1900                                         latestVersionData = resourceData;
1901                                 }
1902                         }
1903                         if (log.isDebugEnabled())
1904                                 log.debug("last certified version of resource = {}  version is {}", latestVersionData.getMetadataDataDefinition().getName(), latestVersionData.getMetadataDataDefinition().getVersion());
1905
1906                         latestVersionMap.put(pair, latestVersionData);
1907                 }
1908
1909                 Map<String, ComponentMetadataData> resVersionMap = new HashMap<String, ComponentMetadataData>();
1910                 for (ComponentMetadataData resourceData : latestVersionMap.values()) {
1911                         ComponentMetadataData latestVersionData = resourceData;
1912                         ComponentMetadataDataDefinition metadataDataDefinition = resourceData.getMetadataDataDefinition();
1913                         if (resVersionMap.containsKey(metadataDataDefinition.getUUID())) {
1914                                 latestVersionData = resVersionMap.get(metadataDataDefinition.getUUID());
1915                                 String currentVersion = latestVersionData.getMetadataDataDefinition().getVersion();
1916                                 String newVersion = metadataDataDefinition.getVersion();
1917                                 if (CommonBeUtils.compareAsdcComponentVersions(newVersion, currentVersion)) {
1918                                         latestVersionData = resourceData;
1919                                 }
1920                         }
1921                         if (log.isDebugEnabled())
1922                                 log.debug("last uuid version of resource = {}  version is {}", latestVersionData.getMetadataDataDefinition().getName(), latestVersionData.getMetadataDataDefinition().getVersion());
1923                         resVersionMap.put(latestVersionData.getMetadataDataDefinition().getUUID(), latestVersionData);
1924                 }
1925
1926                 return resVersionMap;
1927         }
1928
1929         private Pair<String, String> createKeyPair(ComponentMetadataData metadataData) {
1930                 Pair<String, String> pair = null;
1931                 NodeTypeEnum label = NodeTypeEnum.getByName(metadataData.getLabel());
1932                 switch (label) {
1933                 case Resource:
1934                         pair = new ImmutablePair<String, String>(metadataData.getMetadataDataDefinition().getName(), ((ResourceMetadataDataDefinition) metadataData.getMetadataDataDefinition()).getResourceType().name());
1935                         break;
1936                 default:
1937                         pair = new ImmutablePair<String, String>(metadataData.getMetadataDataDefinition().getName(), metadataData.getLabel());
1938                         break;
1939                 }
1940
1941                 return pair;
1942         }
1943
1944         public Either<Collection<ComponentMetadataData>, StorageOperationStatus> getLatestVersionNotAbstractComponentsMetadataOnly(boolean isAbstract, Boolean isHighest, ComponentTypeEnum componentTypeEnum, String internalComponentType) {
1945                 try {
1946
1947                         // Map<String, Object> hasPpropertiesToMatch = new HashMap<>();
1948                         // Map<String, Object> hasNotPpropertiesToMatch = new HashMap<>();
1949                         List<ImmutableTriple<QueryType, String, Object>> properties = new ArrayList<>();
1950                         if (componentTypeEnum.equals(ComponentTypeEnum.RESOURCE)) {
1951                                 // hasPpropertiesToMatch.put(GraphPropertiesDictionary.IS_ABSTRACT.getProperty(),
1952                                 // isAbstract);
1953                                 properties.add(new ImmutableTriple<>(QueryType.HAS, GraphPropertiesDictionary.IS_ABSTRACT.getProperty(), isAbstract));
1954
1955                                 if (internalComponentType != null) {
1956                                         switch (internalComponentType.toLowerCase()) {
1957                                         case "vf":
1958                                                 properties.add(new ImmutableTriple<>(QueryType.HAS_NOT, GraphPropertiesDictionary.RESOURCE_TYPE.getProperty(), ResourceTypeEnum.VF.name()));
1959 //                                              properties.add(new ImmutableTriple<>(QueryType.HAS_NOT, GraphPropertiesDictionary.RESOURCE_TYPE.getProperty(), ResourceTypeEnum.VL.name()));
1960                                                 // hasNotPpropertiesToMatch.put(GraphPropertiesDictionary.RESOURCE_TYPE.getProperty(),
1961                                                 // ResourceTypeEnum.VF.name());
1962                                                 break;
1963                                         case "service":
1964                                                 properties.add(new ImmutableTriple<>(QueryType.HAS_NOT, GraphPropertiesDictionary.RESOURCE_TYPE.getProperty(), ResourceTypeEnum.VFC.name()));
1965                                                 properties.add(new ImmutableTriple<>(QueryType.HAS_NOT, GraphPropertiesDictionary.RESOURCE_TYPE.getProperty(), ResourceTypeEnum.VFCMT.name()));
1966                                                 properties.add(new ImmutableTriple<>(QueryType.HAS_NOT, GraphPropertiesDictionary.RESOURCE_TYPE.getProperty(), ResourceTypeEnum.CVFC.name()));
1967 //                                              properties.add(new ImmutableTriple<>(QueryType.HAS_NOT, GraphPropertiesDictionary.RESOURCE_TYPE.getProperty(), ResourceTypeEnum.VL.name()));
1968                                                 // hasNotPpropertiesToMatch.put(GraphPropertiesDictionary.RESOURCE_TYPE.getProperty(),
1969                                                 // ResourceTypeEnum.VFC.name());
1970                                                 break;
1971                                         case "vl":
1972                                                 properties.add(new ImmutableTriple<>(QueryType.HAS, GraphPropertiesDictionary.RESOURCE_TYPE.getProperty(), ResourceTypeEnum.VL.name()));
1973                                                 // hasPpropertiesToMatch.put(GraphPropertiesDictionary.RESOURCE_TYPE.getProperty(),
1974                                                 // ResourceTypeEnum.VL.name());
1975                                                 break;
1976                                         default:
1977                                                 break;
1978                                         }
1979                                 }
1980                         }
1981                         // hasNotPpropertiesToMatch.put(GraphPropertiesDictionary.STATE.getProperty(),
1982                         // LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name());
1983                         properties.add(new ImmutableTriple<>(QueryType.HAS_NOT, GraphPropertiesDictionary.STATE.getProperty(), LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name()));
1984                         // hasNotPpropertiesToMatch.put(GraphPropertiesDictionary.IS_DELETED.getProperty(),
1985                         // true);
1986                         properties.add(new ImmutableTriple<>(QueryType.HAS_NOT, GraphPropertiesDictionary.IS_DELETED.getProperty(), true));
1987                         // Either<List<ComponentMetadataData>, TitanOperationStatus>
1988                         // resourceNodes = titanGenericDao.getByCriteria(
1989                         // componentTypeEnum.getNodeType(), hasPpropertiesToMatch,
1990                         // hasNotPpropertiesToMatch,
1991                         // ComponentMetadataData.class);
1992                         Either<List<ComponentMetadataData>, TitanOperationStatus> resourceNodes = titanGenericDao.getByCriteria(componentTypeEnum.getNodeType(), ComponentMetadataData.class, properties);
1993                         if (resourceNodes.isRight()) {
1994                                 // in case of NOT_FOUND from Titan client return to UI empty
1995                                 // list
1996                                 if (resourceNodes.right().value().equals(TitanOperationStatus.NOT_FOUND)) {
1997                                         return Either.left(new ArrayList<>());
1998                                 } else {
1999                                         return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(resourceNodes.right().value()));
2000                                 }
2001                         } else {
2002                                 List<ComponentMetadataData> resourceDataList = resourceNodes.left().value();
2003                                 Collection<ComponentMetadataData> resCollection = resourceDataList;
2004                                 if (isHighest != null && isHighest) {
2005                                         Map<String, ComponentMetadataData> latestVersionListMap = findLatestVersion(resourceDataList);
2006                                         resCollection = latestVersionListMap.values();
2007                                 }
2008                                 return Either.left(resCollection);
2009                         }
2010                 } finally {
2011                         titanGenericDao.commit();
2012                 }
2013
2014         }
2015
2016         public Either<List<Component>, StorageOperationStatus> getLatestVersionNotAbstractComponents(boolean isAbstract, Boolean isHighest, ComponentTypeEnum componentTypeEnum, String internalComponentType, List<String> componentUids) {
2017                 try {
2018                         List<Component> result = new ArrayList<>();
2019                         Map<String, ResourceTypeEnum> componentUidsMap = new HashMap<>();
2020                         if (componentUids == null) {
2021                                 Either<Collection<ComponentMetadataData>, StorageOperationStatus> resourceNodes = getLatestVersionNotAbstractComponentsMetadataOnly(isAbstract, isHighest, componentTypeEnum, internalComponentType);
2022                                 if (resourceNodes.isRight()) {
2023                                         return Either.right(resourceNodes.right().value());
2024                                 }
2025                                 Collection<ComponentMetadataData> collection = resourceNodes.left().value();
2026
2027                                 if (collection == null) {
2028                                         componentUids = new ArrayList<>();
2029                                 } else {
2030                                         componentUids = collection.stream().map(p -> p.getMetadataDataDefinition().getUniqueId()).collect(Collectors.toList());
2031                                         // collection.forEach(p -> {
2032                                         // if (NodeTypeEnum.Resource.getName().equals(p.getLabel()))
2033                                         // {
2034                                         // componentUidsMap.put(p.getMetadataDataDefinition().getUniqueId(),
2035                                         // ((ResourceMetadataDataDefinition)
2036                                         // p.getMetadataDataDefinition()).getResourceType());
2037                                         // }
2038                                         // });
2039
2040                                 }
2041
2042                         }
2043                         if (false == componentUids.isEmpty()) {
2044
2045                                 Manager manager = new Manager();
2046                                 int numberOfWorkers = 5;
2047
2048                                 manager.init(numberOfWorkers);
2049                                 for (String componentUid : componentUids) {
2050                                         ComponentParametersView componentParametersView = buildComponentViewForNotAbstract();
2051                                         // ResourceTypeEnum type =
2052                                         // componentUidsMap.get(componentUid);
2053                                         // if (type != null && ResourceTypeEnum.VL.equals(type)) {
2054                                         if (internalComponentType != null && "vl".equalsIgnoreCase(internalComponentType)) {
2055                                                 componentParametersView.setIgnoreCapabilities(false);
2056                                                 componentParametersView.setIgnoreRequirements(false);
2057                                         }
2058                                         manager.addJob(new Job() {
2059                                                 @Override
2060                                                 public Either<Component, StorageOperationStatus> doWork() {
2061                                                         Either<Component, StorageOperationStatus> component = getComponent(componentUid, componentParametersView, false);
2062                                                         return component;
2063                                                 }
2064                                         });
2065                                 }
2066                                 LinkedBlockingQueue<Either<Component, StorageOperationStatus>> res = manager.start();
2067
2068                                 for (Either<Component, StorageOperationStatus> resource : res) {
2069                                         if (resource == null) {
2070                                                 if (log.isDebugEnabled())
2071                                                         log.debug("Failed to fetch resource returned null ");
2072                                                 return Either.right(StorageOperationStatus.GENERAL_ERROR);
2073                                         }
2074                                         if (resource.isRight()) {
2075                                                 if (log.isDebugEnabled())
2076                                                         log.debug("Failed to fetch resource for error is {}", resource.right().value());
2077                                                 return Either.right(resource.right().value());
2078                                         }
2079                                         Component component = resource.left().value();
2080                                         component.setContactId(null);
2081                                         component.setCreationDate(null);
2082                                         component.setCreatorUserId(null);
2083                                         component.setCreatorFullName(null);
2084                                         component.setLastUpdateDate(null);
2085                                         component.setLastUpdaterUserId(null);
2086                                         component.setLastUpdaterFullName(null);
2087                                         component.setNormalizedName(null);
2088                                         result.add(resource.left().value());
2089                                 }
2090
2091                                 if (componentUids.size() != result.size()) {
2092                                         if (log.isDebugEnabled())
2093                                                 log.debug("one of the workers failed to complete job ");
2094                                         return Either.right(StorageOperationStatus.GENERAL_ERROR);
2095                                 }
2096                         }
2097
2098                         return Either.left(result);
2099
2100                 } finally {
2101                         titanGenericDao.commit();
2102                 }
2103         }
2104
2105         private ComponentParametersView buildComponentViewForNotAbstract() {
2106                 ComponentParametersView componentParametersView = new ComponentParametersView();
2107                 componentParametersView.disableAll();
2108                 // componentParametersView.setIgnoreRequirements(false);
2109                 // componentParametersView.setIgnoreCapabilities(false);
2110                 componentParametersView.setIgnoreCategories(false);
2111                 componentParametersView.setIgnoreAllVersions(false);
2112                 componentParametersView.setIgnoreAllVersions(false);
2113                 return componentParametersView;
2114         }
2115
2116         protected TitanOperationStatus setCapabilitiesFromGraph(String uniqueId, Component component, NodeTypeEnum nodeType) {
2117                 TitanOperationStatus titanStatus;
2118                 Either<Map<String, List<CapabilityDefinition>>, TitanOperationStatus> eitherCapabilities = getCapabilities(component, nodeType, true);
2119                 if (eitherCapabilities.isLeft()) {
2120                         titanStatus = TitanOperationStatus.OK;
2121                         Map<String, List<CapabilityDefinition>> capabilities = eitherCapabilities.left().value();
2122                         if (capabilities != null && !capabilities.isEmpty()) {
2123                                 component.setCapabilities(capabilities);
2124                         }
2125                 } else {
2126                         titanStatus = eitherCapabilities.right().value();
2127                 }
2128                 return titanStatus;
2129         }
2130
2131         protected TitanOperationStatus setRequirementsFromGraph(String uniqueId, Component component, NodeTypeEnum nodeType) {
2132                 TitanOperationStatus status;
2133                 Either<Map<String, List<RequirementDefinition>>, TitanOperationStatus> eitherRequirements = getRequirements(component, nodeType, false);
2134                 if (eitherRequirements.isLeft()) {
2135                         status = TitanOperationStatus.OK;
2136                         Map<String, List<RequirementDefinition>> requirements = eitherRequirements.left().value();
2137                         if (requirements != null && !requirements.isEmpty()) {
2138                                 component.setRequirements(requirements);
2139                         }
2140                 } else {
2141                         status = eitherRequirements.right().value();
2142                 }
2143                 return status;
2144         }
2145
2146         protected boolean isComponentExist(String componentId, NodeTypeEnum nodeType) {
2147                 boolean result = true;
2148                 Either<TitanVertex, TitanOperationStatus> compVertex = titanGenericDao.getVertexByProperty(UniqueIdBuilder.getKeyByNodeType(nodeType), componentId);
2149                 if (compVertex.isRight()) {
2150                         log.debug("failed to fetch vertex of component data for id {}", componentId);
2151                         result = false;
2152
2153                 }
2154                 return result;
2155         }
2156
2157         <T> Either<T, StorageOperationStatus> getLightComponent(String id, NodeTypeEnum nodeType, boolean inTransaction) {
2158
2159                 T component = null;
2160                 try {
2161                         log.debug("Starting to build light component of type {}, id {}", nodeType, id);
2162                         Either<TitanGraph, TitanOperationStatus> graphResult = titanGenericDao.getGraph();
2163                         if (graphResult.isRight()) {
2164                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(graphResult.right().value()));
2165                         }
2166                         TitanGraph titanGraph = graphResult.left().value();
2167                         Iterable<TitanVertex> vertecies = titanGraph.query().has(UniqueIdBuilder.getKeyByNodeType(nodeType), id).vertices();
2168                         if (vertecies != null) {
2169                                 Iterator<TitanVertex> iterator = vertecies.iterator();
2170                                 if (iterator != null && iterator.hasNext()) {
2171                                         Vertex vertex = iterator.next();
2172                                         Map<String, Object> resourceProperties = titanGenericDao.getProperties(vertex);
2173                                         ComponentMetadataData componentMetadataData = GraphElementFactory.createElement(nodeType.getName(), GraphElementTypeEnum.Node, resourceProperties, ComponentMetadataData.class);
2174                                         component = (T) convertComponentMetadataDataToComponent(componentMetadataData);
2175
2176                                         // get creator
2177                                         Iterator<Edge> iterCreator = vertex.edges(Direction.IN, GraphEdgeLabels.CREATOR.name());
2178                                         if (iterCreator.hasNext() == false) {
2179                                                 log.debug("no creator was defined for component {}", id);
2180                                                 return Either.right(StorageOperationStatus.GENERAL_ERROR);
2181                                         }
2182                                         Vertex vertexCreator = iterCreator.next().outVertex();
2183                                         UserData creator = GraphElementFactory.createElement(NodeTypeEnum.User.getName(), GraphElementTypeEnum.Node, titanGenericDao.getProperties(vertexCreator), UserData.class);
2184                                         log.debug("Build component : set creator userId to {}", creator.getUserId());
2185                                         String fullName = buildFullName(creator);
2186                                         log.debug("Build component : set creator full name to {}", fullName);
2187                                         ((Component) component).setCreatorUserId(creator.getUserId());
2188                                         ((Component) component).setCreatorFullName(fullName);
2189
2190                                         // get modifier
2191                                         Iterator<Edge> iterModifier = vertex.edges(Direction.IN, GraphEdgeLabels.LAST_MODIFIER.name());
2192
2193                                         if (iterModifier.hasNext() == false) {
2194                                                 log.debug("no modifier was defined for component {}", id);
2195                                                 return Either.right(StorageOperationStatus.GENERAL_ERROR);
2196                                         }
2197                                         Vertex vertexModifier = iterModifier.next().outVertex();
2198                                         UserData modifier = GraphElementFactory.createElement(NodeTypeEnum.User.getName(), GraphElementTypeEnum.Node, titanGenericDao.getProperties(vertexModifier), UserData.class);
2199                                         log.debug("Build component : set last modifier userId to {}", creator.getUserId());
2200                                         fullName = buildFullName(modifier);
2201                                         log.debug("Build component : set last modifier full name to {}", fullName);
2202                                         ((Component) component).setLastUpdaterUserId(modifier.getUserId());
2203                                         ((Component) component).setLastUpdaterFullName(fullName);
2204
2205                                         // get category
2206                                         TitanOperationStatus status = setComponentCategoriesFromGraph((Component) component);
2207                                         if (status != TitanOperationStatus.OK) {
2208                                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
2209                                         }
2210                                 } else {
2211                                         // Nothing found
2212                                         log.debug("Component with id {} not found", id);
2213                                         return Either.right(StorageOperationStatus.NOT_FOUND);
2214                                 }
2215                         } else {
2216                                 // Nothing found
2217                                 log.debug("Component with id {} not found", id);
2218                                 return Either.right(StorageOperationStatus.NOT_FOUND);
2219                         }
2220                         log.debug("Ended to build light component of type {}, id {}", nodeType, id);
2221                         return Either.left(component);
2222                 } finally {
2223                         if (false == inTransaction) {
2224                                 titanGenericDao.commit();
2225                         }
2226                 }
2227         }
2228
2229         Either<Component, StorageOperationStatus> getMetadataComponent(String id, NodeTypeEnum nodeType, boolean inTransaction) {
2230                 Component component = null;
2231                 try {
2232                         log.debug("Starting to build metadata component of type {}, id {}", nodeType, id);
2233                         Either<TitanGraph, TitanOperationStatus> graphResult = titanGenericDao.getGraph();
2234                         if (graphResult.isRight()) {
2235                                 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(graphResult.right().value()));
2236                         }
2237                         TitanGraph titanGraph = graphResult.left().value();
2238                         Iterable<TitanVertex> vertecies = titanGraph.query().has(UniqueIdBuilder.getKeyByNodeType(nodeType), id).vertices();
2239                         if (vertecies != null) {
2240                                 Iterator<TitanVertex> iterator = vertecies.iterator();
2241                                 if (iterator != null && iterator.hasNext()) {
2242                                         Vertex vertex = iterator.next();
2243                                         Map<String, Object> resourceProperties = titanGenericDao.getProperties(vertex);
2244                                         ComponentMetadataData componentMetadataData = GraphElementFactory.createElement(nodeType.getName(), GraphElementTypeEnum.Node, resourceProperties, ComponentMetadataData.class);
2245                                         component = convertComponentMetadataDataToComponent(componentMetadataData);
2246                                 } else {
2247                                         // Nothing found
2248                                         log.debug("Component with id {} not found", id);
2249                                         return Either.right(StorageOperationStatus.NOT_FOUND);
2250                                 }
2251                         } else {
2252                                 // Nothing found
2253                                 log.debug("Component with id {} not found", id);
2254                                 return Either.right(StorageOperationStatus.NOT_FOUND);
2255                         }
2256                         log.debug("Ended to build metadata component of type {}, id {}", nodeType, id);
2257                         return Either.left(component);
2258                 } finally {
2259                         if (false == inTransaction) {
2260                                 titanGenericDao.commit();
2261                         }
2262                 }
2263         }
2264
2265         public Either<Integer, StorageOperationStatus> getComponentInstanceCoutner(String origServiceId, NodeTypeEnum nodeType) {
2266                 Either<Integer, StorageOperationStatus> result;
2267                 Either<TitanGraph, TitanOperationStatus> graphResult = titanGenericDao.getGraph();
2268                 if (graphResult.isRight()) {
2269                         result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(graphResult.right().value()));
2270                         return result;
2271                 }
2272                 Either<TitanVertex, TitanOperationStatus> vertexService = titanGenericDao.getVertexByProperty(UniqueIdBuilder.getKeyByNodeType(nodeType), origServiceId);
2273                 if (vertexService.isRight()) {
2274                         log.debug("failed to fetch vertex of component metadata, nodeType:{} , id: {}", nodeType, origServiceId);
2275                         result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(vertexService.right().value()));
2276                         return result;
2277                 }
2278                 Vertex vertex = vertexService.left().value();
2279                 Integer instanceCounter = vertex.value(GraphPropertiesDictionary.INSTANCE_COUNTER.getProperty());
2280                 return Either.left(instanceCounter);
2281         }
2282
2283         protected TitanOperationStatus setComponentInstancesPropertiesFromGraph(Component component) {
2284
2285                 List<ComponentInstance> resourceInstances = component.getComponentInstances();
2286
2287                 Map<String, List<ComponentInstanceProperty>> resourceInstancesProperties = new HashMap<>();
2288
2289                 Map<String, List<PropertyDefinition>> alreadyProcessedResources = new HashMap<>();
2290
2291                 Map<String, List<ComponentInstanceProperty>> alreadyProcessedInstances = new HashMap<>();
2292
2293                 Map<String, ImmutablePair<ComponentInstance, Integer>> processedInstances = new HashMap<>();
2294
2295                 if (resourceInstances != null) {
2296
2297                         for (ComponentInstance resourceInstance : resourceInstances) {
2298
2299                                 List<String> path = new ArrayList<>();
2300                                 path.add(resourceInstance.getUniqueId());
2301                                 Either<List<ComponentInstanceProperty>, TitanOperationStatus> componentInstanceProperties = componentInstanceOperation.getComponentInstanceProperties(resourceInstance, alreadyProcessedResources, alreadyProcessedInstances,
2302                                                 processedInstances, path);
2303
2304                                 if (componentInstanceProperties.isRight()) {
2305                                         TitanOperationStatus status = componentInstanceProperties.right().value();
2306                                         if (status != TitanOperationStatus.OK) {
2307                                                 return status;
2308                                         }
2309                                 }
2310
2311                                 List<ComponentInstanceProperty> listOfProps = componentInstanceProperties.left().value();
2312                                 String resourceInstanceUid = resourceInstance.getUniqueId();
2313                                 resourceInstancesProperties.put(resourceInstanceUid, listOfProps);
2314
2315                                 // alreadyProcessedInstances.put(resourceInstance.getUniqueId(),
2316                                 // resourceInstance);
2317
2318                                 processedInstances.put(resourceInstance.getUniqueId(), new ImmutablePair<ComponentInstance, Integer>(resourceInstance, path.size()));
2319                                 path.remove(path.size() - 1);
2320
2321                         }
2322
2323                 }
2324
2325                 Either<Map<String, Map<String, ComponentInstanceProperty>>, TitanOperationStatus> findAllPropertiesValuesOnInstances = componentInstanceOperation.findAllPropertyValueOnInstances(processedInstances);
2326                 // 1. check status
2327                 if (findAllPropertiesValuesOnInstances.isRight()) {
2328                         TitanOperationStatus status = findAllPropertiesValuesOnInstances.right().value();
2329                         if (status != TitanOperationStatus.OK) {
2330                                 return status;
2331                         }
2332                 }
2333                 // 2. merge data from rules on properties (resourceInstancesProperties)
2334                 propertyOperation.updatePropertiesByPropertyValues(resourceInstancesProperties, findAllPropertiesValuesOnInstances.left().value());
2335
2336                 component.setComponentInstancesProperties(resourceInstancesProperties);
2337
2338                 return TitanOperationStatus.OK;
2339         }
2340         
2341         protected TitanOperationStatus setComponentInstancesInputsFromGraph(String uniqueId, Component component) {
2342
2343                 Map<String, List<ComponentInstanceInput>> resourceInstancesInputs = new HashMap<>();
2344                 TitanOperationStatus status = TitanOperationStatus.OK;
2345                 List<ComponentInstance> componentInstances = component.getComponentInstances();
2346                 if (componentInstances != null) {
2347                         for (ComponentInstance resourceInstance : componentInstances) {
2348                                 Either<List<ComponentInstanceInput>, TitanOperationStatus> eitherRIAttributes = inputOperation.getAllInputsOfResourceInstance(resourceInstance);
2349                                 if (eitherRIAttributes.isRight()) {
2350                                         if (eitherRIAttributes.right().value() != TitanOperationStatus.NOT_FOUND) {
2351                                                 status = eitherRIAttributes.right().value();
2352                                                 break;
2353                                         }
2354                                 } else {
2355                                         resourceInstancesInputs.put(resourceInstance.getUniqueId(), eitherRIAttributes.left().value());
2356                                 }
2357                         }
2358                         if (!resourceInstancesInputs.isEmpty())
2359                                 component.setComponentInstancesInputs(resourceInstancesInputs);
2360                 }
2361
2362                 return status;
2363         }
2364
2365         public Either<String, StorageOperationStatus> getInvariantUUID(NodeTypeEnum nodeType, String componentId, boolean inTransaction) {
2366                 Either<String, StorageOperationStatus> res = null;
2367                 try {
2368                         Either<TitanVertex, TitanOperationStatus> vertexByProperty = titanGenericDao.getVertexByProperty(UniqueIdBuilder.getKeyByNodeType(nodeType), componentId);
2369                         if (vertexByProperty.isRight()) {
2370                                 TitanOperationStatus status = vertexByProperty.right().value();
2371                                 if (status == TitanOperationStatus.NOT_FOUND) {
2372                                         status = TitanOperationStatus.INVALID_ID;
2373                                 }
2374                                 res = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
2375                         } else {
2376                                 Vertex v = vertexByProperty.left().value();
2377                                 String invariantUUID = v.value(GraphPropertiesDictionary.INVARIANT_UUID.getProperty());
2378
2379                                 if (invariantUUID == null || invariantUUID.isEmpty()) {
2380
2381                                         log.info("The component {} has empty invariant UUID.", componentId);
2382                                         res = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(TitanOperationStatus.INVALID_ELEMENT));
2383
2384                                 }
2385                                 res = Either.left(invariantUUID);
2386                         }
2387                 } finally {
2388                         if (false == inTransaction) {
2389                                 titanGenericDao.commit();
2390                         }
2391                 }
2392                 return res;
2393         }
2394
2395         protected TitanOperationStatus setGroupsFromGraph(String uniqueId, Component component, NodeTypeEnum nodeTypeEnum) {
2396
2397                 Either<List<GroupDefinition>, TitanOperationStatus> res = groupOperation.getAllGroupsFromGraph(uniqueId, nodeTypeEnum);
2398                 if (res.isRight()) {
2399                         TitanOperationStatus status = res.right().value();
2400                         if (status == TitanOperationStatus.NOT_FOUND) {
2401                                 return TitanOperationStatus.OK;
2402                         } else {
2403                                 return status;
2404                         }
2405                 }
2406                 component.setGroups(res.left().value());
2407
2408                 return TitanOperationStatus.OK;
2409
2410         }
2411
2412         protected TitanOperationStatus setComponentInputsFromGraph(String uniqueId, Component component, boolean inTransaction) {
2413
2414                 List<InputDefinition> inputs = new ArrayList<>();
2415                 TitanOperationStatus status = inputsOperation.findAllResourceInputs(uniqueId, inputs);
2416                 if (status == TitanOperationStatus.OK) {
2417                         component.setInputs(inputs);
2418                 }
2419
2420                 return status;
2421
2422         }
2423
2424         protected StorageOperationStatus deleteGroups(NodeTypeEnum nodeType, String componentId) {
2425
2426                 Either<List<GroupDefinition>, StorageOperationStatus> deleteRes = groupOperation.deleteAllGroups(componentId, nodeType, true);
2427
2428                 if (deleteRes.isRight()) {
2429                         StorageOperationStatus status = deleteRes.right().value();
2430                         return status;
2431                 }
2432
2433                 return StorageOperationStatus.OK;
2434
2435         }
2436
2437         protected StorageOperationStatus removeInputsFromComponent(NodeTypeEnum typeEnum, Component component) {
2438                 Either<Map<String, InputDefinition>, StorageOperationStatus> deleteAllInputsAssociatedToNode = inputsOperation.deleteAllInputsAssociatedToNode(typeEnum, component.getUniqueId());
2439                 return deleteAllInputsAssociatedToNode.isRight() ? deleteAllInputsAssociatedToNode.right().value() : StorageOperationStatus.OK;
2440         }
2441
2442         protected TitanOperationStatus associateInputsToComponent(NodeTypeEnum nodeType, ComponentMetadataData resourceData, List<InputDefinition> properties) {
2443
2444                 Either<Map<String, DataTypeDefinition>, TitanOperationStatus> allDataTypes = applicationDataTypeCache.getAll();
2445                 if (allDataTypes.isRight()) {
2446                         TitanOperationStatus status = allDataTypes.right().value();
2447                         log.debug("Cannot find any data type. Status is {}.", status);
2448                         return status;
2449                 }
2450
2451                 Map<String, InputDefinition> convertedProperties = new HashMap<>();
2452
2453                 if (properties != null) {
2454                         for (InputDefinition propertyDefinition : properties) {
2455                                 convertedProperties.put(propertyDefinition.getName(), propertyDefinition);
2456                         }
2457
2458                         Either<List<InputDefinition>, TitanOperationStatus> operationStatus = inputsOperation.addInputsToGraph(resourceData.getMetadataDataDefinition().getUniqueId(), nodeType, convertedProperties, allDataTypes.left().value());
2459                         if (operationStatus.isLeft())
2460                                 return TitanOperationStatus.OK;
2461                         else
2462                                 return operationStatus.right().value();
2463                 }
2464
2465                 return TitanOperationStatus.OK;
2466
2467         }
2468
2469         protected TitanOperationStatus associateInputsToComponent(TitanVertex metadataVertex, String componentId, List<InputDefinition> properties) {
2470
2471                 Either<Map<String, DataTypeDefinition>, TitanOperationStatus> allDataTypes = applicationDataTypeCache.getAll();
2472                 if (allDataTypes.isRight()) {
2473                         TitanOperationStatus status = allDataTypes.right().value();
2474                         log.debug("Cannot find any data type. Status is {}.", status);
2475                         return status;
2476                 }
2477
2478                 Map<String, InputDefinition> convertedProperties = new HashMap<>();
2479
2480                 if (properties != null) {
2481                         for (InputDefinition propertyDefinition : properties) {
2482                                 convertedProperties.put(propertyDefinition.getName(), propertyDefinition);
2483                         }
2484
2485                         return inputsOperation.addInputsToGraph(metadataVertex, componentId, convertedProperties, allDataTypes.left().value());
2486                 }
2487
2488                 return TitanOperationStatus.OK;
2489
2490         }
2491
2492         public Either<List<ComponentInstance>, StorageOperationStatus> getAllComponentInstncesMetadata(String componentId, NodeTypeEnum nodeType) {
2493                 Instant start = Instant.now();
2494                 Either<List<ComponentInstance>, StorageOperationStatus> resourceInstancesOfService = componentInstanceOperation.getAllComponentInstancesMetadataOnly(componentId, nodeType);
2495                 Instant end = Instant.now();
2496                 log.debug("TOTAL TIME BL GET INSTANCES: {}", Duration.between(start, end));
2497                 return resourceInstancesOfService;
2498         }
2499
2500         @Deprecated
2501         public Either<List<Component>, ActionStatus> getComponentsFromCacheForCatalog(Set<String> components, ComponentTypeEnum componentType) {
2502
2503                 Either<ImmutableTriple<List<Component>, List<Component>, Set<String>>, ActionStatus> componentsForCatalog = componentCache.getComponentsForCatalog(components, componentType);
2504                 if (componentsForCatalog.isLeft()) {
2505                         ImmutableTriple<List<Component>, List<Component>, Set<String>> immutableTriple = componentsForCatalog.left().value();
2506                         List<Component> foundComponents = immutableTriple.getLeft();
2507
2508                         if (foundComponents != null) {
2509                                 // foundComponents.forEach(p -> result.add((Resource)p));
2510                                 log.debug("The number of {}s added to catalog from cache is {}", componentType.name().toLowerCase(), foundComponents.size());
2511
2512                         }
2513                         List<Component> foundDirtyComponents = immutableTriple.getMiddle();
2514                         Set<String> nonCachedComponents = immutableTriple.getRight();
2515                         int numberDirtyResources = foundDirtyComponents == null ? 0 : foundDirtyComponents.size();
2516                         int numberNonCached = nonCachedComponents == null ? 0 : nonCachedComponents.size();
2517                         log.debug("The number of left {}s for catalog is {}", componentType.name().toLowerCase(), numberDirtyResources + numberNonCached);
2518                         return Either.left(foundComponents);
2519                 }
2520
2521                 return Either.right(componentsForCatalog.right().value());
2522         }
2523
2524         public <T extends ComponentMetadataData> Either<List<T>, TitanOperationStatus> getListOfHighestComponents(NodeTypeEnum nodeTypeEnum, Class<T> clazz) {
2525
2526                 long startFetchAllStates = System.currentTimeMillis();
2527                 Map<String, Object> propertiesToMatchHigest = new HashMap<>();
2528                 propertiesToMatchHigest.put(GraphPropertiesDictionary.IS_HIGHEST_VERSION.getProperty(), true);
2529                 Either<List<T>, TitanOperationStatus> allHighestStates = titanGenericDao.getByCriteria(nodeTypeEnum, propertiesToMatchHigest, clazz);
2530                 if (allHighestStates.isRight() && allHighestStates.right().value() != TitanOperationStatus.NOT_FOUND) {
2531                         return Either.right(allHighestStates.right().value());
2532                 }
2533                 long endFetchAllStates = System.currentTimeMillis();
2534
2535                 if (allHighestStates.isRight()) {
2536                         return Either.left(new ArrayList<>());
2537                 }
2538                 List<T> services = allHighestStates.left().value();
2539
2540                 List<T> certifiedHighest = new ArrayList<>();
2541                 List<T> notCertifiedHighest = new ArrayList<>();
2542                 for (T reData : services) {
2543                         if (reData.getMetadataDataDefinition().getState().equals(LifecycleStateEnum.CERTIFIED.name())) {
2544                                 certifiedHighest.add(reData);
2545                         } else {
2546                                 notCertifiedHighest.add(reData);
2547                         }
2548                 }
2549
2550                 log.debug("Fetch catalog {}s all states: certified {}, noncertified {}", nodeTypeEnum.getName(), certifiedHighest.size(), notCertifiedHighest.size());
2551                 log.debug("Fetch catalog {}s all states from graph took {} ms", nodeTypeEnum.getName(), endFetchAllStates - startFetchAllStates);
2552
2553                 HashMap<String, String> serviceNames = new HashMap<>();
2554                 for (T data : notCertifiedHighest) {
2555                         String serviceName = data.getMetadataDataDefinition().getName();
2556                         serviceNames.put(serviceName, serviceName);
2557                 }
2558
2559                 for (T data : certifiedHighest) {
2560                         String serviceName = data.getMetadataDataDefinition().getName();
2561                         if (!serviceNames.containsKey(serviceName)) {
2562                                 notCertifiedHighest.add(data);
2563                         }
2564                 }
2565
2566                 return Either.left(notCertifiedHighest);
2567         }
2568
2569         protected <T extends Component> Either<T, ActionStatus> getComponentFromCacheIfUpToDate(String uniqueId, ComponentMetadataData componentMetadataData, ComponentParametersView componentParametersView, Class<T> clazz,
2570                         ComponentTypeEnum componentTypeEnum) {
2571
2572                 long start = System.currentTimeMillis();
2573                 try {
2574
2575                         long lastModificationTime = componentMetadataData.getMetadataDataDefinition().getLastUpdateDate();
2576                         Either<Component, ActionStatus> cacheComponentRes = this.componentCache.getComponent(uniqueId, lastModificationTime);
2577                         if (cacheComponentRes.isLeft()) {
2578                                 Component cachedComponent = cacheComponentRes.left().value();
2579
2580                                 // Must calculate allVersions
2581                                 if (false == componentParametersView.isIgnoreAllVersions()) {
2582                                         Class<? extends ComponentMetadataData> clazz1 = null;
2583                                         switch (componentTypeEnum) {
2584                                         case RESOURCE:
2585                                                 clazz1 = ResourceMetadataData.class;
2586                                                 break;
2587                                         case SERVICE:
2588                                                 clazz1 = ServiceMetadataData.class;
2589                                                 break;
2590                                         case PRODUCT:
2591                                                 clazz1 = ProductMetadataData.class;
2592                                                 break;
2593                                         default:
2594                                                 break;
2595                                         }
2596                                         if (clazz1 != null) {
2597                                                 Either<Map<String, String>, TitanOperationStatus> versionList = getVersionList(componentTypeEnum.getNodeType(), cachedComponent.getVersion(), cachedComponent.getUUID(), cachedComponent.getSystemName(), clazz1);
2598                                                 if (versionList.isRight()) {
2599                                                         return Either.right(ActionStatus.GENERAL_ERROR);
2600                                                 }
2601
2602                                                 Map<String, String> allVersions = versionList.left().value();
2603                                                 cachedComponent.setAllVersions(allVersions);
2604                                         } else {
2605                                                 return Either.right(ActionStatus.GENERAL_ERROR);
2606                                         }
2607                                 }
2608                                 if (componentParametersView != null) {
2609                                         cachedComponent = componentParametersView.filter(cachedComponent, componentTypeEnum);
2610                                 }
2611                                 return Either.left(clazz.cast(cachedComponent));
2612                         }
2613
2614                         return Either.right(cacheComponentRes.right().value());
2615
2616                 } finally {
2617                         log.trace("Fetch component {} with uid {} from cache took {} ms", componentTypeEnum.name().toLowerCase(), uniqueId, System.currentTimeMillis() - start);
2618                 }
2619         }
2620
2621         public Either<ImmutablePair<List<Component>, Set<String>>, ActionStatus> getComponentsFromCacheForCatalog(Map<String, Long> components, ComponentTypeEnum componentType) {
2622
2623                 Either<ImmutablePair<List<Component>, Set<String>>, ActionStatus> componentsForCatalog = componentCache.getComponentsForCatalog(components, componentType);
2624                 if (componentsForCatalog.isLeft()) {
2625                         ImmutablePair<List<Component>, Set<String>> immutablePair = componentsForCatalog.left().value();
2626                         List<Component> foundComponents = immutablePair.getLeft();
2627
2628                         if (foundComponents != null) {
2629                                 // foundComponents.forEach(p -> result.add((Resource)p));
2630                                 log.debug("The number of {}s added to catalog from cache is {}", componentType.name().toLowerCase(), foundComponents.size());
2631                         }
2632                         Set<String> leftComponents = immutablePair.getRight();
2633                         int numberNonCached = leftComponents == null ? 0 : leftComponents.size();
2634                         log.debug("The number of left {}s for catalog is {}", componentType.name().toLowerCase(), numberNonCached);
2635
2636                         ImmutablePair<List<Component>, Set<String>> result = new ImmutablePair<List<Component>, Set<String>>(foundComponents, leftComponents);
2637                         return Either.left(result);
2638                 }
2639
2640                 return Either.right(componentsForCatalog.right().value());
2641         }
2642
2643         /**
2644          * 
2645          * @param component
2646          * @param inTransaction
2647          * @param titanGenericDao
2648          * @param clazz
2649          * @return
2650          */
2651         public <T> Either<T, StorageOperationStatus> updateComponentFilterResult(Component component, boolean inTransaction, TitanGenericDao titanGenericDao, Class<T> clazz, NodeTypeEnum type, ComponentParametersView filterResult) {
2652                 Either<T, StorageOperationStatus> result = null;
2653
2654                 try {
2655
2656                         log.debug("In updateComponent. received component uid = {}", (component == null ? null : component.getUniqueId()));
2657                         if (component == null) {
2658                                 log.error("Service object is null");
2659                                 result = Either.right(StorageOperationStatus.BAD_REQUEST);
2660                                 return result;
2661                         }
2662
2663                         ComponentMetadataData componentData = getMetaDataFromComponent(component);
2664
2665                         log.debug("After converting component to componentData. ComponentData = {}", componentData);
2666
2667                         if (componentData.getUniqueId() == null) {
2668                                 log.error("Resource id is missing in the request.");
2669                                 return Either.right(StorageOperationStatus.BAD_REQUEST);
2670                         }
2671
2672                         Either<Integer, StorageOperationStatus> counterStatus = this.getComponentInstanceCoutner(component.getUniqueId(), component.getComponentType().getNodeType());
2673
2674                         if (counterStatus.isRight()) {
2675
2676                                 log.error("Cannot find componentInstanceCounter for component {} in the graph. status is {}", componentData.getUniqueId(), counterStatus);
2677                                 // result = sendError(status,
2678                                 // StorageOperationStatus.USER_NOT_FOUND);
2679                                 return result;
2680                         }
2681
2682                         componentData.setComponentInstanceCounter(counterStatus.left().value());
2683
2684                         String modifierUserId = component.getLastUpdaterUserId();
2685                         if (modifierUserId == null || modifierUserId.isEmpty()) {
2686                                 log.error("UserId is missing in the request.");
2687                                 result = Either.right(StorageOperationStatus.BAD_REQUEST);
2688                                 return result;
2689                         }
2690                         Either<UserData, TitanOperationStatus> findUser = findUser(modifierUserId);
2691
2692                         if (findUser.isRight()) {
2693                                 TitanOperationStatus status = findUser.right().value();
2694                                 log.error("Cannot find user {} in the graph. status is {}", modifierUserId, status);
2695                                 // result = sendError(status,
2696                                 // StorageOperationStatus.USER_NOT_FOUND);
2697                                 return result;
2698                         }
2699
2700                         UserData modifierUserData = findUser.left().value();
2701                         String resourceId = component.getUniqueId();
2702
2703                         ComponentParametersView componentParametersView = new ComponentParametersView();
2704                         componentParametersView.disableAll();
2705                         componentParametersView.setIgnoreUsers(false);
2706                         componentParametersView.setIgnoreCategories(false);
2707                         componentParametersView.setIgnoreDerivedFrom(false);
2708                         componentParametersView.setIgnoreArtifacts(false);
2709                         Either<T, StorageOperationStatus> currentComponentResult = this.getComponent(resourceId, componentParametersView, inTransaction);
2710                         if (currentComponentResult.isRight()) {
2711                                 log.error("Cannot find resource with id {} in the graph.", resourceId);
2712                                 result = Either.right(currentComponentResult.right().value());
2713                                 return result;
2714                         }
2715
2716                         Component currentComponent = (Component) currentComponentResult.left().value();
2717                         String currentModifier = currentComponent.getLastUpdaterUserId();
2718
2719                         if (currentModifier.equals(modifierUserData.getUniqueId())) {
2720                                 log.debug("Graph LAST MODIFIER edge should not be changed since the modifier is the same as the last modifier.");
2721                         } else {
2722                                 log.debug("Going to update the last modifier user of the resource from {} to {}", currentModifier, modifierUserId);
2723                                 StorageOperationStatus status = moveLastModifierEdge(component, componentData, modifierUserData, type);
2724                                 log.debug("Finish to update the last modifier user of the resource from {} to {}. status is {}", currentModifier, modifierUserId, status);
2725                                 if (status != StorageOperationStatus.OK) {
2726                                         result = Either.right(status);
2727                                         return result;
2728                                 }
2729                         }
2730                         final long currentTimeMillis = System.currentTimeMillis();
2731                         log.debug("Going to update the last Update Date of the resource from {} to {}", component.getLastUpdateDate(), currentTimeMillis);
2732                         component.setLastUpdateDate(currentTimeMillis);
2733
2734                         StorageOperationStatus checkCategories = validateCategories(currentComponent, component, componentData, type);
2735                         if (checkCategories != StorageOperationStatus.OK) {
2736                                 result = Either.right(checkCategories);
2737                                 return result;
2738                         }
2739
2740                         List<String> tags = component.getTags();
2741                         if (tags != null && false == tags.isEmpty()) {
2742                                 Either<List<TagData>, StorageOperationStatus> tagsResult = createNewTagsList(tags);
2743                                 if (tagsResult.isRight()) {
2744                                         result = Either.right(tagsResult.right().value());
2745                                         return result;
2746                                 }
2747                                 List<TagData> tagsToCreate = tagsResult.left().value();
2748                                 if (tagsToCreate != null && !tagsToCreate.isEmpty()) {
2749                                         tagsToCreate = ImmutableSet.copyOf(tagsToCreate).asList();
2750                                         for (TagData tagData : tagsToCreate) {
2751                                                 log.debug("Before creating tag {}", tagData);
2752                                                 Either<TagData, TitanOperationStatus> createTagResult = titanGenericDao.createNode(tagData, TagData.class);
2753                                                 if (createTagResult.isRight()) {
2754                                                         TitanOperationStatus status = createTagResult.right().value();
2755                                                         log.error("Cannot find tag {} in the graph. status is {}", tagData, status);
2756                                                         result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
2757                                                         return result;
2758                                                 }
2759                                                 log.debug("After creating tag {}", tagData);
2760                                         }
2761                                 }
2762                         }
2763
2764                         Either<ComponentMetadataData, TitanOperationStatus> updateNode = titanGenericDao.updateNode(componentData, ComponentMetadataData.class);
2765
2766                         if (updateNode.isRight()) {
2767                                 log.error("Failed to update resource {}. status is {}", component.getUniqueId(), updateNode.right().value());
2768                                 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(updateNode.right().value()));
2769                                 return result;
2770                         }
2771
2772                         ComponentMetadataData updatedResourceData = updateNode.left().value();
2773                         log.debug("ComponentData After update is {}", updatedResourceData);
2774
2775                         // DE230195 in case resource name changed update TOSCA artifacts
2776                         // file names accordingly
2777                         String newSystemName = updatedResourceData.getMetadataDataDefinition().getSystemName();
2778                         String prevSystemName = currentComponent.getSystemName();
2779                         if (newSystemName != null && !newSystemName.equals(prevSystemName)) {
2780                                 Map<String, ArtifactDefinition> toscaArtifacts = component.getToscaArtifacts();
2781                                 if (toscaArtifacts != null) {
2782                                         for (Entry<String, ArtifactDefinition> artifact : toscaArtifacts.entrySet()) {
2783                                                 Either<ArtifactData, StorageOperationStatus> updateName = generateAndUpdateToscaFileName(component.getComponentType().getValue().toLowerCase(), newSystemName, updatedResourceData.getMetadataDataDefinition().getUniqueId(),
2784                                                                 type, artifact.getValue());
2785                                                 if (updateName.isRight()) {
2786                                                         result = Either.right(updateName.right().value());
2787                                                         return result;
2788                                                 }
2789                                         }
2790                                 }
2791                                 //TODO call to new Artifact operation in order to update list of artifacts 
2792                                 
2793                      //US833308 VLI in service - specific network_role property value logic
2794                                 if (ComponentTypeEnum.SERVICE == component.getComponentType()) {
2795                                         //update method logs success/error and returns boolean (true if nothing fails)
2796                                         updateServiceNameInVLIsNetworkRolePropertyValues(component, prevSystemName, newSystemName);
2797                                 }
2798                         }
2799                         
2800
2801                         if (component.getComponentType().equals(ComponentTypeEnum.RESOURCE)) {
2802                                 updateDerived(component, currentComponent, componentData, component.getClass());
2803                         }
2804
2805                         Either<T, StorageOperationStatus> updatedResource = getComponent(component.getUniqueId(), filterResult, inTransaction);
2806                         if (updatedResource.isRight()) {
2807                                 log.error("Resource id is missing in the request. status is {}", updatedResource.right().value());
2808                                 result = Either.right(StorageOperationStatus.BAD_REQUEST);
2809                                 return result;
2810                         }
2811
2812                         T updatedResourceValue = updatedResource.left().value();
2813                         result = Either.left(updatedResourceValue);
2814
2815                         return result;
2816                 } finally {
2817
2818                         if (false == inTransaction) {
2819                                 if (result == null || result.isRight()) {
2820                                         log.error("updateComponent operation : Going to execute rollback on graph.");
2821                                         titanGenericDao.rollback();
2822                                 } else {
2823                                         log.debug("updateComponent operation : Going to execute commit on graph.");
2824                                         titanGenericDao.commit();
2825                                 }
2826                         }
2827                 }
2828         }
2829         
2830         private boolean updateServiceNameInVLIsNetworkRolePropertyValues (Component component, String prevSystemName, String newSystemName) {
2831                 // find VLIs in service
2832                 boolean res = true;
2833                 if(null == component.getComponentInstances() || component.getComponentInstances().isEmpty()){
2834                         return res;
2835                 }
2836                 
2837                 List <ComponentInstance> vlInstances = 
2838                                 component.getComponentInstances().stream()
2839                                 .filter(p -> OriginTypeEnum.VL == p.getOriginType())
2840                                 .collect(Collectors.toList());
2841                 if (!vlInstances.isEmpty()) {
2842                         for (ComponentInstance vlInstance : vlInstances){
2843                                 // find network_role property 
2844                                 Optional <ComponentInstanceProperty> networkRoleProperty = component.getComponentInstancesProperties().get(vlInstance.getUniqueId()).stream()
2845                                                 .filter(p -> PropertyNames.NETWORK_ROLE.getPropertyName().equalsIgnoreCase(p.getName()))
2846                                                 .findAny();     
2847                                 res = res && updateNetworkRolePropertyValue(prevSystemName, newSystemName, vlInstance, networkRoleProperty);            
2848                         }       
2849                 }
2850                 return res;     
2851         }
2852
2853         private boolean updateNetworkRolePropertyValue(String prevSystemName, String newSystemName, ComponentInstance vlInstance, Optional<ComponentInstanceProperty> networkRoleProperty) {
2854                 if (networkRoleProperty.isPresent() && !StringUtils.isEmpty(networkRoleProperty.get().getValue()) ) {
2855                         ComponentInstanceProperty property = networkRoleProperty.get();
2856                         String updatedValue = property.getValue().replaceFirst(prevSystemName, newSystemName);
2857                         property.setValue(updatedValue);
2858                         StorageOperationStatus updateCustomizationUUID;
2859                         //disregard property value rule 
2860                         property.setRules(null);
2861                         Either<ComponentInstanceProperty, StorageOperationStatus> result = componentInstanceOperation.updatePropertyValueInResourceInstance(property, vlInstance.getUniqueId(), true);
2862                         if (result.isLeft()) {
2863                                 log.debug("Property value {} was updated on graph.", property.getValueUniqueUid());
2864                                 updateCustomizationUUID = componentInstanceOperation.updateCustomizationUUID(vlInstance.getUniqueId());
2865                         } else {
2866                                 updateCustomizationUUID = StorageOperationStatus.EXEUCTION_FAILED;
2867                                 log.debug("Failed to update property value: {} in resource instance {}", updatedValue, vlInstance.getUniqueId());
2868                         }
2869                         return result.isLeft() && StorageOperationStatus.OK == updateCustomizationUUID;
2870                 }
2871                 return true;
2872         }
2873
2874         public Either<ComponentMetadataData, StorageOperationStatus> updateComponentLastUpdateDateAndLastModifierOnGraph( Component component, User modifier, NodeTypeEnum componentType, boolean inTransaction) {
2875                 
2876                 log.debug("Going to update last update date and last modifier info of component {}. ", component.getName());
2877                 Either<ComponentMetadataData, StorageOperationStatus> result = null;
2878                 try{
2879                         String modifierUserId = modifier.getUserId();
2880                         ComponentMetadataData componentData = getMetaDataFromComponent(component);
2881                         String currentUser = component.getLastUpdaterUserId();
2882                         UserData modifierUserData = new UserData();
2883                         modifierUserData.setUserId(modifierUserId);
2884                         if (currentUser.equals(modifierUserId)) {
2885                                 log.debug("Graph last modifier edge should not be changed since the modifier is the same as the last modifier.");
2886                         } else {
2887                                 log.debug("Going to update the last modifier user of the component from {} to {}", currentUser, modifierUserId);
2888                                 StorageOperationStatus status = moveLastModifierEdge(component, componentData, modifierUserData, componentType);
2889                                 log.debug("Finish to update the last modifier user of the resource from {} to {}. status is {}", currentUser, modifierUserId, status);
2890                                 if (status != StorageOperationStatus.OK) {
2891                                         result = Either.right(status);
2892                                 }
2893                         }
2894                         Either<ComponentMetadataData, TitanOperationStatus> updateNode = null;
2895                         if(result == null){
2896                                 log.debug("Going to update the component {} with new last update date. ", component.getName());
2897                                 componentData.getMetadataDataDefinition().setLastUpdateDate(System.currentTimeMillis());
2898                                 updateNode = titanGenericDao.updateNode(componentData, ComponentMetadataData.class);
2899                                 if (updateNode.isRight()) {
2900                                         log.error("Failed to update component {}. status is {}", component.getUniqueId(), updateNode.right().value());
2901                                         result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(updateNode.right().value()));
2902                                 }
2903                         }
2904                         if(result == null){
2905                                 result = Either.left(updateNode.left().value());
2906                         }
2907                 }catch(Exception e){
2908                         log.error("Exception occured during  update last update date and last modifier info of component {}. The message is {}. ", component.getName(), e.getMessage());
2909                 }finally {
2910                         if(!inTransaction){
2911                                 if (result == null || result.isRight()) {
2912                                         log.error("Going to execute rollback on graph.");
2913                                         titanGenericDao.rollback();
2914                                 } else {
2915                                         log.debug("Going to execute commit on graph.");
2916                                         titanGenericDao.commit();
2917                                 }
2918                         }
2919                 }
2920                 return result;
2921         }
2922         /**
2923          * updates component lastUpdateDate on graph node
2924          * @param component
2925          * @param componentType
2926          * @param lastUpdateDate
2927          * @param inTransaction
2928          * @return
2929          */
2930         public Either<ComponentMetadataData, StorageOperationStatus> updateComponentLastUpdateDateOnGraph( Component component, NodeTypeEnum componentType, Long lastUpdateDate, boolean inTransaction) {
2931                 
2932                 log.debug("Going to update last update date of component {}. ", component.getName());
2933                 Either<ComponentMetadataData, StorageOperationStatus> result = null;
2934                 try{
2935                         ComponentMetadataData componentData = getMetaDataFromComponent(component);
2936                         Either<ComponentMetadataData, TitanOperationStatus> updateNode = null;
2937                         if(result == null){
2938                                 log.debug("Going to update the component {} with new last update date. ", component.getName());
2939                                 componentData.getMetadataDataDefinition().setLastUpdateDate(lastUpdateDate);
2940                                 updateNode = titanGenericDao.updateNode(componentData, ComponentMetadataData.class);
2941                                 if (updateNode.isRight()) {
2942                                         log.error("Failed to update component {}. status is {}", component.getUniqueId(), updateNode.right().value());
2943                                         result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(updateNode.right().value()));
2944                                 }
2945                         }
2946                         if(result == null){
2947                                 result = Either.left(updateNode.left().value());
2948                         }
2949                 }catch(Exception e){
2950                         log.error("Exception occured during  update last update date of component {}. The message is {}. ", component.getName(), e.getMessage());
2951                 }finally {
2952                         if(!inTransaction){
2953                                 if (result == null || result.isRight()) {
2954                                         log.error("Going to execute rollback on graph.");
2955                                         titanGenericDao.rollback();
2956                                 } else {
2957                                         log.debug("Going to execute commit on graph.");
2958                                         titanGenericDao.commit();
2959                                 }
2960                         }
2961                 }
2962                 return result;
2963         }
2964 }