2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.model.operations.impl;
23 import java.time.Duration;
24 import java.time.Instant;
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;
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;
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;
123 import fj.data.Either;
125 public abstract class ComponentOperation {
126 private static Logger log = LoggerFactory.getLogger(ComponentOperation.class.getName());
129 protected TitanGenericDao titanGenericDao;
132 protected IArtifactOperation artifactOperation;
135 protected IElementOperation elementOperation;
138 protected ICapabilityOperation capabilityOperation;
141 protected IRequirementOperation requirementOperation;
144 protected ComponentInstanceOperation componentInstanceOperation;
147 private PropertyOperation propertyOperation;
150 protected InputsOperation inputOperation;
153 protected IAdditionalInformationOperation additionalInformationOperation;
156 protected GroupOperation groupOperation;
159 protected InputsOperation inputsOperation;
162 protected ApplicationDataTypeCache applicationDataTypeCache;
165 private ComponentCache componentCache;
168 private ToscaDefinitionPathCalculator toscaDefinitionPathCalculator;
170 private static Pattern uuidNewVersion = Pattern.compile("^\\d{1,}.1");
172 protected Gson prettyJson = new GsonBuilder().setPrettyPrinting().create();
174 protected Either<List<TagData>, StorageOperationStatus> createNewTagsList(List<String> tags) {
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);
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();
186 for (String tagName : tags) {
187 TagData tag = new TagData(tagName);
188 if ((existingTags == null) || (!existingTags.contains(tag))) {
189 tagsToCreate.add(tag);
192 return Either.left(tagsToCreate);
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);
210 log.debug("After creating tag {}", tagData);
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());
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());
230 getComponentResult = Either.left(component.left().value());
233 return getComponentResult;
236 public Either<ComponentMetadataData, StorageOperationStatus> getLatestComponentMetadataByUuid(NodeTypeEnum nodeType, String uuid, boolean inTransaction) {
238 Either<ComponentMetadataData, StorageOperationStatus> getComponentResult = null;
239 List<ComponentMetadataData> latestVersionList = null;
240 ComponentMetadataData latestVersion = null;
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);
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()));
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);
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);
264 } catch (Exception e){
265 log.debug("Failed to get latest component metadata with type {} by uuid {}. ", nodeType.getName(), uuid, e);
267 if (!inTransaction) {
268 titanGenericDao.commit();
271 return getComponentResult;
274 public <T extends GraphNode> Either<T, StorageOperationStatus> getComponentByLabelAndId(String uniqueId, NodeTypeEnum nodeType, Class<T> clazz) {
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()));
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);
289 T serviceData = serviceDataList.get(0);
290 return Either.left(serviceData);
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();
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);
318 return TitanOperationStatus.OK;
321 protected TitanOperationStatus setComponentLastModifierFromGraph(Component component, String uniqueId, NodeTypeEnum nodeType) {
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();
328 ImmutablePair<UserData, GraphEdge> value = parentNode.left().value();
329 if (log.isDebugEnabled())
330 log.debug("Found parent node {}", value);
331 UserData userData = value.getKey();
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);
341 return TitanOperationStatus.OK;
349 protected String buildFullName(UserData userData) {
351 String fullName = userData.getFirstName();
352 if (fullName == null) {
355 fullName = fullName + " ";
357 String lastName = userData.getLastName();
358 if (lastName != null) {
359 fullName += lastName;
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);
370 protected Either<TitanVertex, TitanOperationStatus> findUserVertex(String userId) {
371 String key = UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.User);
372 return titanGenericDao.getVertexByProperty(key, userId);
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);
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;
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);
393 protected TitanOperationStatus associateMetadataToComponent(ComponentMetadataData componentData, UserData userData, UserData updater, CategoryData categoryData, List<ResourceMetadataData> derivedResources) {
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();
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();
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();
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();
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();
437 return TitanOperationStatus.OK;
440 protected StorageOperationStatus associateArtifactsToComponent(NodeTypeEnum nodeType, ComponentMetadataData componentData, Map<String, ArtifactDefinition> artifacts) {
442 if (artifacts != null) {
443 for (Entry<String, ArtifactDefinition> entry : artifacts.entrySet()) {
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();
453 return StorageOperationStatus.OK;
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>();
461 String normalizedName = ValidationUtils.normaliseComponentName(name);
462 hasProps.put(GraphPropertiesDictionary.NORMALIZED_NAME.getProperty(), normalizedName);
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()));
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() + "|");
476 log.debug("resources with property name:{} exists in graph. found {}",name, builder.toString());
478 return Either.left(false);
480 log.debug("resources with property name:{} does not exists in graph", name);
481 return Either.left(true);
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);
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()));
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() + "|");
503 log.debug("Service with property name:{} exists in graph. found {}" , name, builder.toString());
506 return Either.left(false);
508 log.debug("Service with property name:{} does not exists in graph", name);
509 return Either.left(true);
513 protected Either<Boolean, StorageOperationStatus> validateToscaResourceNameUniqueness(String name, TitanGenericDao titanGenericDao) {
514 Map<String, Object> properties = new HashMap<>();
516 properties.put(GraphPropertiesDictionary.TOSCA_RESOURCE_NAME.getProperty(), name);
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()));
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() + "|");
530 log.debug("resources with property name:{} exists in graph. found {}" , name, builder.toString());
532 return Either.left(false);
534 log.debug("resources with property name:{} does not exists in graph", name);
535 return Either.left(true);
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);
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()));
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() + "|");
557 log.debug("Component with property name:{} exists in graph. found {}" , name, builder.toString());
560 return Either.left(false);
562 log.debug("Component with property name:{} does not exists in graph", name);
563 return Either.left(true);
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();
573 // component.setArtifacts(artifacts.left().value());
574 createSpecificArtifactList(component, artifacts.left().value());
579 protected Component createSpecificArtifactList(Component component, Map<String, ArtifactDefinition> artifacts) {
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<>();
586 Set<Entry<String, ArtifactDefinition>> specificet = new HashSet<>();
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;
595 switch (artifactGroupType) {
597 deploymentArtifacts.put(artifact.getArtifactLabel(), artifact);
598 specificet.add(entry);
601 serviceApiArtifacts.put(artifact.getArtifactLabel(), artifact);
602 specificet.add(entry);
605 toscaArtifacts.put(artifact.getArtifactLabel(), artifact);
606 specificet.add(entry);
613 artifacts.entrySet().removeAll(specificet);
615 component.setSpecificComponetTypeArtifacts(serviceApiArtifacts);
616 component.setDeploymentArtifacts(deploymentArtifacts);
617 component.setToscaArtifacts(toscaArtifacts);
618 component.setArtifacts(artifacts);
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);
632 if (isHighest && isMatchingResourceType) {
633 Either<T, StorageOperationStatus> result = getLightComponent(componentData.getUniqueId(), true);
634 if (result.isRight()) {
635 return Either.right(result.right().value());
637 components.add(result.left().value());
641 return Either.left(components);
644 private boolean isMatchingByResourceType(NodeTypeEnum componentType, ResourceTypeEnum resourceType,
645 ComponentMetadataDataDefinition componentData) {
648 if (componentType == NodeTypeEnum.Resource) {
649 if (resourceType == null) {
652 isMatching = resourceType == ((ResourceMetadataDataDefinition)componentData).getResourceType();
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) {
663 Either<TitanGraph, TitanOperationStatus> graph = titanGenericDao.getGraph();
664 if (graph.isRight()) {
665 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(graph.right().value()));
668 return collectComponents(graph.left().value(), neededType, categoryUid, categoryType, clazz, resourceType);
671 if (false == inTransaction) {
672 titanGenericDao.commit();
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<>();
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);
688 Either<TitanGraph, TitanOperationStatus> graph = titanGenericDao.getGraph();
689 if (graph.isRight()) {
690 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(graph.right().value()));
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()) {
698 components.addAll(result.left().value());
701 return Either.left(components);
703 if (false == inTransaction) {
704 titanGenericDao.commit();
709 <T> Either<List<T>, StorageOperationStatus> getFilteredComponents(Map<FilterKeyEnum, String> filters, boolean inTransaction, NodeTypeEnum neededType) {
713 protected Either<List<Component>, StorageOperationStatus> getFollowedComponent(String userId, Set<LifecycleStateEnum> lifecycleStates, Set<LifecycleStateEnum> lastStateStates, boolean inTransaction, TitanGenericDao titanGenericDao,
714 NodeTypeEnum neededType) {
716 Either<List<Component>, StorageOperationStatus> result = null;
719 Either<TitanGraph, TitanOperationStatus> graph = titanGenericDao.getGraph();
720 if (graph.isRight()) {
721 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(graph.right().value()));
724 Iterable<TitanVertex> users;
726 if (userId == null) {
727 // get all users by label
728 // for Tester and Admin retrieve all 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();
736 // for Designer retrieve specific user
737 String key = UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.User);
738 users = graph.left().value().query().has(key, userId).vertices();
740 Iterator<TitanVertex> userIterator = users.iterator();
742 List<Component> components = new ArrayList<>();
743 while (userIterator.hasNext()) {
744 Vertex vertexUser = userIterator.next();
746 // get all resource with current state
747 Iterator<Edge> iterator = vertexUser.edges(Direction.OUT, GraphEdgeLabels.STATE.getProperty());
749 List<Component> componentsPerUser = fetchComponents(lifecycleStates, iterator, neededType, inTransaction);
751 HashSet<String> ids = new HashSet<String>();
753 if (componentsPerUser != null) {
754 for (Component comp : componentsPerUser) {
755 ids.add(comp.getUniqueId());
756 components.add(comp);
760 if (lastStateStates != null && !lastStateStates.isEmpty()) {
761 // get all resource with last state
762 iterator = vertexUser.edges(Direction.OUT, GraphEdgeLabels.LAST_STATE.getProperty());
764 componentsPerUser = fetchComponents(lastStateStates, iterator, neededType, inTransaction);
765 if (componentsPerUser != null) {
766 for (Component comp : componentsPerUser) {
769 if (ids.contains(comp.getUniqueId())) {
772 if (isFirst == true) {
773 components.add(comp);
782 result = Either.left(components);
786 if (false == inTransaction) {
787 if (result == null || result.isRight()) {
788 titanGenericDao.rollback();
790 titanGenericDao.commit();
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();
802 String stateStr = edge.value(GraphEdgePropertiesDictionary.STATE.getProperty());
803 LifecycleStateEnum state = LifecycleStateEnum.findState(stateStr);
805 log.debug("not supported STATE for element {}" , stateStr);
808 if (lifecycleStates != null && lifecycleStates.contains(state)) {
809 Vertex vertexComponent = edge.inVertex();
811 Boolean isHighest = vertexComponent.value(GraphPropertiesDictionary.IS_HIGHEST_VERSION.getProperty());
814 String nodeTypeStr = vertexComponent.value(GraphPropertiesDictionary.LABEL.getProperty());
815 // get only latest versions
816 NodeTypeEnum nodeType = NodeTypeEnum.getByName(nodeTypeStr);
818 if (nodeType == null) {
819 log.debug("missing node label for vertex {}", vertexComponent);
823 if (neededType.equals(nodeType)) {
826 handleNode(components, vertexComponent, nodeType, inTransaction);
829 Boolean isAbtract = vertexComponent.value(GraphPropertiesDictionary.IS_ABSTRACT.getProperty());
830 if (false == isAbtract) {
831 handleNode(components, vertexComponent, nodeType, inTransaction);
835 handleNode(components, vertexComponent, nodeType, inTransaction);
838 log.debug("not supported node type {}", nodeType);
848 protected <T> void handleNode(List<T> components, Vertex vertexComponent, NodeTypeEnum nodeType, boolean inTransaction) {
851 id = vertexComponent.value(UniqueIdBuilder.getKeyByNodeType(nodeType));
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());
857 components.add(component.left().value());
861 Map<String, Object> properties = this.titanGenericDao.getProperties(vertexComponent);
862 log.debug("missing resource unique id for node with properties {}", properties);
869 * @param inTransaction
870 * @param titanGenericDao
874 public <T> Either<T, StorageOperationStatus> updateComponent(Component component, boolean inTransaction, TitanGenericDao titanGenericDao, Class<T> clazz, NodeTypeEnum type) {
876 ComponentParametersView componentParametersView = new ComponentParametersView();
877 return updateComponentFilterResult(component, inTransaction, titanGenericDao, clazz, type, componentParametersView);
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()))
887 artifactInfo.setArtifactName(componentType + "-" + componentName + getConfig.get("artifactName"));
888 return artifactOperation.updateToscaArtifactNameOnGraph(artifactInfo, artifactInfo.getUniqueId(), type, componentId);
891 protected StorageOperationStatus moveCategoryEdge(Component component, ComponentMetadataData componentData, CategoryDefinition newCategory, NodeTypeEnum type) {
893 StorageOperationStatus result = StorageOperationStatus.OK;
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());
906 log.debug("After removing edge from graph {}", deleteOutgoingRelation);
908 NodeTypeEnum categoryType;
909 if (NodeTypeEnum.Service.name().equalsIgnoreCase(type.name())) {
910 categoryType = NodeTypeEnum.ServiceCategory;
912 categoryType = NodeTypeEnum.ResourceCategory;
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);
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());
933 private StorageOperationStatus moveLastModifierEdge(Component component, ComponentMetadataData componentData, UserData modifierUserData, NodeTypeEnum type) {
935 StorageOperationStatus result = StorageOperationStatus.OK;
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());
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());
958 protected abstract ComponentMetadataData getMetaDataFromComponent(Component component);
960 public abstract <T> Either<T, StorageOperationStatus> getComponent(String id, boolean inTransaction);
962 public abstract <T> Either<T, StorageOperationStatus> getComponent(String id, ComponentParametersView componentParametersView, boolean inTrasnaction);
964 protected abstract <T> Either<T, StorageOperationStatus> getComponentByNameAndVersion(String name, String version, Map<String, Object> additionalParams, boolean inTransaction);
966 public abstract <T> Either<T, StorageOperationStatus> getLightComponent(String id, boolean inTransaction);
968 public abstract <T> Either<List<T>, StorageOperationStatus> getFilteredComponents(Map<FilterKeyEnum, String> filters, boolean inTransaction);
970 abstract Component convertComponentMetadataDataToComponent(ComponentMetadataData componentMetadataData);
972 abstract TitanOperationStatus setComponentCategoriesFromGraph(Component component);
974 protected abstract Either<Component, StorageOperationStatus> getMetadataComponent(String id, boolean inTransaction);
976 protected abstract <T> Either<T, StorageOperationStatus> updateComponent(T component, boolean inTransaction);
978 protected abstract <T> Either<T, StorageOperationStatus> updateComponentFilterResult(T component, boolean inTransaction, ComponentParametersView filterParametersView);
980 public abstract Either<Component, StorageOperationStatus> deleteComponent(String id, boolean inTransaction);
982 public <T> Either<T, StorageOperationStatus> cloneComponent(T other, String version, boolean inTransaction) {
983 return cloneComponent(other, version, null, inTransaction);
986 public abstract <T> Either<T, StorageOperationStatus> cloneComponent(T other, String version, LifecycleStateEnum targetLifecycle, boolean inTransaction);
988 public abstract Component getDefaultComponent();
990 public abstract boolean isComponentExist(String componentId);
992 public abstract Either<Boolean, StorageOperationStatus> validateComponentNameExists(String componentName);
994 public abstract Either<Boolean, StorageOperationStatus> isComponentInUse(String componentId);
996 protected Either<Boolean, StorageOperationStatus> isComponentInUse(String componentId, NodeTypeEnum nodeType) {
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()));
1004 if (relationByCriteria.isLeft()) {
1005 // component is in use
1006 return Either.left(true);
1008 return Either.left(false);
1013 public abstract Either<List<String>, StorageOperationStatus> getAllComponentsMarkedForDeletion();
1015 protected Either<List<String>, StorageOperationStatus> getAllComponentsMarkedForDeletion(NodeTypeEnum nodeType) {
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);
1022 Either<List<ComponentMetadataData>, TitanOperationStatus> componentsToDelete = titanGenericDao.getByCriteria(nodeType, props, ComponentMetadataData.class);
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);
1030 log.info("failed to find components to delete. error : {}", error.name());
1031 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(error));
1035 for (ComponentMetadataData resourceData : componentsToDelete.left().value()) {
1036 componentIdsToDelete.add(resourceData.getMetadataDataDefinition().getUniqueId());
1038 return Either.left(componentIdsToDelete);
1041 protected <T extends GraphNode> Either<List<T>, TitanOperationStatus> __getLastVersion(NodeTypeEnum type, Map<String, Object> props, Class<T> clazz) {
1044 Either<TitanGraph, TitanOperationStatus> graph = titanGenericDao.getGraph();
1045 if (graph.isRight()) {
1046 return Either.right(graph.right().value());
1049 TitanGraph tGraph = graph.left().value();
1050 TitanGraphQuery<? extends TitanGraphQuery> query = tGraph.query();
1051 query = query.has(GraphPropertiesDictionary.LABEL.getProperty(), type.getName());
1053 if (props != null && !props.isEmpty()) {
1054 for (Map.Entry<String, Object> entry : props.entrySet()) {
1055 query = query.hasNot(entry.getKey(), entry.getValue());
1058 query.has(GraphPropertiesDictionary.IS_HIGHEST_VERSION.getProperty(), true);
1060 Iterable<TitanVertex> vertices = query.vertices();
1062 if (vertices == null) {
1063 return Either.right(TitanOperationStatus.NOT_FOUND);
1066 Iterator<TitanVertex> iterator = vertices.iterator();
1067 List<T> result = new ArrayList<T>();
1069 while (iterator.hasNext()) {
1070 Vertex vertex = iterator.next();
1072 Map<String, Object> newProp = titanGenericDao.getProperties(vertex);
1073 T element = GraphElementFactory.createElement(type.getName(), GraphElementTypeEnum.Node, newProp, clazz);
1074 result.add(element);
1076 if (result.size() == 0) {
1077 return Either.right(TitanOperationStatus.NOT_FOUND);
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));
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);
1091 protected <T extends GraphNode> Either<List<T>, TitanOperationStatus> getLastVersion(NodeTypeEnum type, Map<String, Object> hasProps, Map<String, Object> hasNotProps, Class<T> clazz) {
1093 Map<String, Object> props = new HashMap<>();
1095 if (hasProps != null) {
1096 props.putAll(hasProps);
1098 props.put(GraphPropertiesDictionary.IS_HIGHEST_VERSION.getProperty(), true);
1100 Either<List<T>, TitanOperationStatus> byCriteria = titanGenericDao.getByCriteria(type, props, hasNotProps, clazz);
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());
1114 if (type == NodeTypeEnum.Resource) {
1115 propertiesToMatch.put(GraphPropertiesDictionary.IS_ABSTRACT.getProperty(), false);
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());
1122 return Either.left(result);
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");
1133 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(componentsNodes.right().value()));
1136 List<S> componentDataList = componentsNodes.left().value();
1137 for (S componentData : componentDataList) {
1138 // Either<T, StorageOperationStatus> component =
1139 // getComponent((String) componentData.getUniqueId(),
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());
1146 result.add(component.left().value());
1150 return Either.left(result);
1153 protected StorageOperationStatus removeArtifactsFromComponent(Component component, NodeTypeEnum componentType) {
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();
1161 if (artifactsRes.isLeft() && artifactsRes.left().value() != null) {
1162 Map<String, ArtifactDefinition> artifacts = artifactsRes.left().value();
1163 for (Entry<String, ArtifactDefinition> entry : artifacts.entrySet()) {
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();
1172 return StorageOperationStatus.OK;
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);
1179 List<Component> ripRes = rip.left().value();
1180 if (ripRes != null && !ripRes.isEmpty()) {
1181 resList.addAll(ripRes);
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);
1187 List<Component> rfcRes = rfc.left().value();
1188 if (rfcRes != null && !rfcRes.isEmpty()) {
1189 resList.addAll(rfcRes);
1192 return Either.right(rfc.right().value());
1196 return Either.right(rip.right().value());
1198 return Either.left(resList);
1203 * generate UUID only for case that version is "XX.01" - (start new version)
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());
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);
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>();
1224 if (version.startsWith("0")) {
1225 props.put(GraphPropertiesDictionary.UUID.getProperty(), uuid);
1227 props.put(GraphPropertiesDictionary.SYSTEM_NAME.getProperty(), systemName);
1229 hasNotProps.put(GraphPropertiesDictionary.IS_DELETED.getProperty(), true);
1230 Either<List<T>, TitanOperationStatus> result = titanGenericDao.getByCriteria(type, props, hasNotProps, clazz);
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());
1241 List<ResourceMetadataData> components = (List<ResourceMetadataData>) result.left().value();
1242 for (ResourceMetadataData data : components) {
1243 versionMap.put(data.getMetadataDataDefinition().getVersion(), (String) data.getUniqueId());
1247 List<ServiceMetadataData> componentsS = (List<ServiceMetadataData>) result.left().value();
1248 for (ServiceMetadataData data : componentsS) {
1249 versionMap.put(data.getMetadataDataDefinition().getVersion(), (String) data.getUniqueId());
1253 List<ProductMetadataData> componentsP = (List<ProductMetadataData>) result.left().value();
1254 for (ProductMetadataData data : componentsP) {
1255 versionMap.put(data.getMetadataDataDefinition().getVersion(), (String) data.getUniqueId());
1263 return Either.left(versionMap);
1266 protected StorageOperationStatus deleteAdditionalInformation(NodeTypeEnum nodeType, String componentId) {
1268 Either<AdditionalInformationDefinition, StorageOperationStatus> deleteRes = additionalInformationOperation.deleteAllAdditionalInformationParameters(nodeType, componentId, true);
1270 if (deleteRes.isRight()) {
1271 StorageOperationStatus status = deleteRes.right().value();
1275 return StorageOperationStatus.OK;
1279 protected StorageOperationStatus addAdditionalInformation(NodeTypeEnum nodeType, String componentId, AdditionalInformationDefinition informationDefinition) {
1281 Either<AdditionalInformationDefinition, TitanOperationStatus> status = additionalInformationOperation.addAdditionalInformationNode(nodeType, componentId, informationDefinition);
1283 if (status.isRight()) {
1284 TitanOperationStatus titanStatus = status.right().value();
1285 return DaoStatusConverter.convertTitanStatusToStorageStatus(titanStatus);
1288 log.trace("After adding additional information to component {}. Result is {}" , componentId ,status.left().value());
1290 return StorageOperationStatus.OK;
1294 protected StorageOperationStatus addAdditionalInformation(NodeTypeEnum nodeType, String componentId, AdditionalInformationDefinition informationDefinition, TitanVertex metadataVertex) {
1296 TitanOperationStatus status = additionalInformationOperation.addAdditionalInformationNode(nodeType, componentId, informationDefinition, metadataVertex);
1297 log.trace("After adding additional information to component {}. Result is {}", componentId, status);
1299 if (!status.equals(TitanOperationStatus.OK)) {
1300 return DaoStatusConverter.convertTitanStatusToStorageStatus(status);
1303 return StorageOperationStatus.OK;
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());
1316 artifacts.addAll(artifactsResponse.left().value().values());
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());
1328 return Either.left(artifacts);
1331 protected void addComponentInternalFields(ComponentMetadataData componentMetadataData) {
1332 org.openecomp.sdc.be.datatypes.components.ComponentMetadataDataDefinition metadataDataDefinition = componentMetadataData.getMetadataDataDefinition();
1333 Long creationDate = metadataDataDefinition.getCreationDate();
1335 long currentDate = System.currentTimeMillis();
1336 if (creationDate == null) {
1337 metadataDataDefinition.setCreationDate(currentDate);
1339 metadataDataDefinition.setLastUpdateDate(currentDate);
1341 String lifecycleStateEnum = metadataDataDefinition.getState();
1342 if (lifecycleStateEnum == null) {
1343 metadataDataDefinition.setState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name());
1345 String componentUniqueId = UniqueIdBuilder.buildComponentUniqueId();
1346 metadataDataDefinition.setUniqueId(componentUniqueId);
1347 metadataDataDefinition.setHighestVersion(true);
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);
1355 if (tagsResult == null) {
1356 log.debug("tagsResult is null");
1357 return StorageOperationStatus.GENERAL_ERROR;
1359 if (tagsResult.isRight()) {
1360 return tagsResult.right().value();
1362 List<TagData> tagsToCreate = tagsResult.left().value();
1363 return createTagNodesOnGraph(tagsToCreate);
1365 log.trace("All tags created succesfully for component {}", component.getUniqueId());
1366 return StorageOperationStatus.OK;
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);
1389 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1391 groupingDataToAssociate.add(findGroupingEither.left().value());
1399 return Either.left(groupingDataToAssociate);
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();
1411 log.trace("All groupings associated succesfully to component {}", componentMetadataData);
1412 return TitanOperationStatus.OK;
1415 public abstract Either<Integer, StorageOperationStatus> increaseAndGetComponentInstanceCounter(String componentId, boolean inTransaction);
1417 protected Either<Integer, StorageOperationStatus> increaseAndGetComponentInstanceCounter(String componentId, NodeTypeEnum nodeType, boolean inTransaction) {
1418 Either<Integer, StorageOperationStatus> result = null;
1421 Either<TitanGraph, TitanOperationStatus> graphResult = titanGenericDao.getGraph();
1422 if (graphResult.isRight()) {
1423 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(graphResult.right().value()));
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()));
1432 Vertex vertex = vertexService.left().value();
1433 Integer instanceCounter = vertex.value(GraphPropertiesDictionary.INSTANCE_COUNTER.getProperty());
1435 vertex.property(GraphPropertiesDictionary.INSTANCE_COUNTER.getProperty(), instanceCounter);
1436 result = Either.left(instanceCounter);
1440 if (false == inTransaction) {
1441 if (result == null || result.isRight()) {
1442 log.debug("increaseAndGetComponentInstanceCounter operation : Going to execute rollback on graph.");
1443 titanGenericDao.rollback();
1445 log.debug("increaseAndGetComponentInstanceCounter operation : Going to execute commit on graph.");
1446 titanGenericDao.commit();
1453 protected Either<Integer, StorageOperationStatus> setComponentInstanceCounter(String componentId, NodeTypeEnum nodeType, int counter, boolean inTransaction) {
1454 Either<Integer, StorageOperationStatus> result = null;
1457 Either<TitanGraph, TitanOperationStatus> graphResult = titanGenericDao.getGraph();
1458 if (graphResult.isRight()) {
1459 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(graphResult.right().value()));
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()));
1468 Vertex vertex = vertexService.left().value();
1469 vertex.property(GraphPropertiesDictionary.INSTANCE_COUNTER.getProperty(), counter);
1470 result = Either.left(counter);
1474 if (false == inTransaction) {
1475 if (result == null || result.isRight()) {
1476 log.debug("deleteService operation : Going to execute rollback on graph.");
1477 titanGenericDao.rollback();
1479 log.debug("deleteService operation : Going to execute commit on graph.");
1480 titanGenericDao.commit();
1487 protected TitanOperationStatus setComponentInstancesFromGraph(String uniqueId, Component component, NodeTypeEnum containerNodeType, NodeTypeEnum compInstNodeType) {
1489 Either<ImmutablePair<List<ComponentInstance>, List<RequirementCapabilityRelDef>>, TitanOperationStatus> resourceInstancesOfService = componentInstanceOperation.getComponentInstancesOfComponent(uniqueId, containerNodeType, compInstNodeType);
1491 if (resourceInstancesOfService.isRight()) {
1492 TitanOperationStatus status = resourceInstancesOfService.right().value();
1493 if (status == TitanOperationStatus.NOT_FOUND) {
1494 status = TitanOperationStatus.OK;
1496 log.error("Failed to fetch resource instances and their relations. status is {}", status);
1501 ImmutablePair<List<ComponentInstance>, List<RequirementCapabilityRelDef>> immutablePair = resourceInstancesOfService.left().value();
1502 List<ComponentInstance> instances = immutablePair.getKey();
1503 List<RequirementCapabilityRelDef> relations = immutablePair.getValue();
1505 component.setComponentInstances(instances);
1506 component.setComponentInstancesRelations(relations);
1508 return TitanOperationStatus.OK;
1512 * set all properties of all of its resources
1517 protected TitanOperationStatus ___setComponentInstancesPropertiesFromGraph(String uniqueId, Component component) {
1519 List<ComponentInstance> resourceInstances = component.getComponentInstances();
1521 Map<String, List<ComponentInstanceProperty>> resourceInstancesProperties = new HashMap<>();
1523 Map<String, List<PropertyDefinition>> alreadyProcessedResources = new HashMap<>();
1525 if (resourceInstances != null) {
1526 for (ComponentInstance resourceInstance : resourceInstances) {
1528 log.debug("Going to update properties of resource instance {}", resourceInstance.getUniqueId());
1529 String resourceUid = resourceInstance.getComponentUid();
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) {
1538 alreadyProcessedResources.put(resourceUid, properties);
1540 log.debug("After getting properties of resource {}. Number of properties is {}", resourceUid, (properties == null ? 0 : properties.size()));
1541 if (false == properties.isEmpty()) {
1543 String resourceInstanceUid = resourceInstance.getUniqueId();
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) {
1554 Map<String, ComponentInstanceProperty> propertyIdToValue = new HashMap<>();
1555 populateMapperWithPropertyValues(propertyValuesRes, propertyIdToValue);
1557 List<ComponentInstanceProperty> resourceInstancePropertyList = new ArrayList<>();
1558 for (PropertyDefinition propertyDefinition : properties) {
1560 String defaultValue = propertyDefinition.getDefaultValue();
1561 String value = defaultValue;
1562 String valueUid = null;
1564 String propertyId = propertyDefinition.getUniqueId();
1565 ComponentInstanceProperty valuedProperty = propertyIdToValue.get(propertyId);
1566 if (valuedProperty != null) {
1567 String newValue = valuedProperty.getValue();
1568 // if (newValue != null) {
1572 valueUid = valuedProperty.getValueUniqueUid();
1573 log.trace("Found value {} under resource instance which override the default value {}" , value, defaultValue);
1575 ComponentInstanceProperty resourceInstanceProperty = new ComponentInstanceProperty(propertyDefinition, value, valueUid);
1577 // TODO: currently ignore constraints since they are not
1578 // inuse and cause to error in convertion to object.
1579 resourceInstanceProperty.setConstraints(null);
1581 resourceInstancePropertyList.add(resourceInstanceProperty);
1585 resourceInstancesProperties.put(resourceInstanceUid, resourceInstancePropertyList);
1590 component.setComponentInstancesProperties(resourceInstancesProperties);
1593 return TitanOperationStatus.OK;
1596 private void populateMapperWithPropertyValues(Either<List<ComponentInstanceProperty>, TitanOperationStatus> propertyValuesRes, Map<String, ComponentInstanceProperty> propertyIdToValue) {
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);
1608 public abstract Either<List<ArtifactDefinition>, StorageOperationStatus> getAdditionalArtifacts(String resourceId, boolean recursively, boolean inTransaction);
1610 protected abstract StorageOperationStatus validateCategories(Component currentComponent, Component component, ComponentMetadataData componentData, NodeTypeEnum type);
1612 protected abstract <T extends Component> StorageOperationStatus updateDerived(Component component, Component currentComponent, ComponentMetadataData updatedResourceData, Class<T> clazz);
1614 public abstract Either<Component, StorageOperationStatus> markComponentToDelete(Component componentToDelete, boolean inTransaction);
1616 protected Either<Component, StorageOperationStatus> internalMarkComponentToDelete(Component componentToDelete, boolean inTransaction) {
1617 Either<Component, StorageOperationStatus> result = null;
1619 if ((componentToDelete.getIsDeleted() != null) && componentToDelete.getIsDeleted() && !componentToDelete.isHighestVersion()) {
1620 // component already marked for delete
1621 result = Either.left(componentToDelete);
1625 ComponentMetadataData componentMetaData = getMetaDataFromComponent(componentToDelete);
1627 componentMetaData.getMetadataDataDefinition().setIsDeleted(true);
1628 componentMetaData.getMetadataDataDefinition().setHighestVersion(false);
1629 componentMetaData.getMetadataDataDefinition().setLastUpdateDate(System.currentTimeMillis());
1631 Either<ComponentMetadataData, TitanOperationStatus> updateNode = titanGenericDao.updateNode(componentMetaData, ComponentMetadataData.class);
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);
1641 result = Either.left(componentToDelete);
1645 if (false == inTransaction) {
1646 if (result == null || result.isRight()) {
1647 log.error("updateResource operation : Going to execute rollback on graph.");
1648 titanGenericDao.rollback();
1650 log.debug("updateResource operation : Going to execute commit on graph.");
1651 titanGenericDao.commit();
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);
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());
1672 return convertationResult;
1675 private Either<CapabilityDefinition, TitanOperationStatus> convertCapDataToCapDef(ComponentInstance componentInstance, CapabilityData data, GraphEdge edge) {
1676 Either<CapabilityDefinition, TitanOperationStatus> eitherDef = capabilityOperation.getCapabilityByCapabilityData(data);
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));
1690 String requiredOccurrences = (String) properties.get(GraphEdgePropertiesDictionary.REQUIRED_OCCURRENCES.getProperty());
1691 if (requiredOccurrences != null) {
1692 capDef.setMinOccurrences(requiredOccurrences);
1694 String leftOccurrences = (String) properties.get(GraphEdgePropertiesDictionary.LEFT_OCCURRENCES.getProperty());
1695 if (leftOccurrences != null) {
1696 capDef.setMaxOccurrences(leftOccurrences);
1700 eitherDef = Either.left(capDef);
1705 private Either<RequirementDefinition, TitanOperationStatus> convertReqDataToReqDef(ComponentInstance componentInstance, RequirementData data, GraphEdge edge) {
1706 Either<RequirementDefinition, TitanOperationStatus> eitherDef = requirementOperation.getRequirement(data.getUniqueId());
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);
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);
1724 eitherDef = Either.left(requirementDef);
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));
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());
1740 // Convert From Either To Definition And Collect
1741 List<Def> reqDefList = filteredReqDefList.stream().map(e -> e.left().value()).collect(Collectors.toList());
1743 reqDefList.forEach(e -> dataAdder.addData(e, componentInstance));
1744 eitherResult = Either.left(reqDefList);
1747 return eitherResult;
1750 interface ConvertDataToDef<Def, Data> {
1751 Either<Def, TitanOperationStatus> convert(ComponentInstance compInstance, Data d, GraphEdge edge);
1754 interface AddOwnerData<Def> {
1755 void addData(Def def, ComponentInstance compInstance);
1758 private void addOwnerDataCap(CapabilityDefinition capDef, ComponentInstance componentInstance) {
1759 capDef.setOwnerId(componentInstance.getUniqueId());
1760 capDef.setOwnerName(componentInstance.getName());
1763 private void addOwnerDataReq(RequirementDefinition reqDef, ComponentInstance componentInstance) {
1764 reqDef.setOwnerId(componentInstance.getUniqueId());
1765 reqDef.setOwnerName(componentInstance.getName());
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);
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,
1777 if (eitherDataCollected.isRight()) {
1778 eitherResult = Either.right(eitherDataCollected.right().value());
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());
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);
1796 if (inTransaction == false) {
1797 titanGenericDao.commit();
1801 return eitherResult;
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);
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,
1813 if (eitherDataCollected.isRight()) {
1814 eitherResult = Either.right(eitherDataCollected.right().value());
1816 // Converts CapData to CapDef removes stop if encountered
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());
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);
1833 if (inTransaction == false) {
1834 titanGenericDao.commit();
1838 return eitherResult;
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;
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());
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());
1856 eitherResult = Either.left(ownerCapDataList.stream().filter(p -> p.right.isLeft()).collect(Collectors.toList()));
1859 return eitherResult;
1862 interface DataDefConvertor<Def, Data> {
1863 Either<List<Def>, TitanOperationStatus> convertDataToDefComponentInstance(ComponentInstance componentInstance, List<ImmutablePair<Data, GraphEdge>> data);
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
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();
1879 defList.add(convertCapDataListToCapDefList.left().value());
1883 Either<List<List<Def>>, TitanOperationStatus> eitherResult = (error != null) ? Either.right(error) : Either.left(defList);
1884 return eitherResult;
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;
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;
1903 if (log.isDebugEnabled())
1904 log.debug("last certified version of resource = {} version is {}", latestVersionData.getMetadataDataDefinition().getName(), latestVersionData.getMetadataDataDefinition().getVersion());
1906 latestVersionMap.put(pair, latestVersionData);
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;
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);
1926 return resVersionMap;
1929 private Pair<String, String> createKeyPair(ComponentMetadataData metadataData) {
1930 Pair<String, String> pair = null;
1931 NodeTypeEnum label = NodeTypeEnum.getByName(metadataData.getLabel());
1934 pair = new ImmutablePair<String, String>(metadataData.getMetadataDataDefinition().getName(), ((ResourceMetadataDataDefinition) metadataData.getMetadataDataDefinition()).getResourceType().name());
1937 pair = new ImmutablePair<String, String>(metadataData.getMetadataDataDefinition().getName(), metadataData.getLabel());
1944 public Either<Collection<ComponentMetadataData>, StorageOperationStatus> getLatestVersionNotAbstractComponentsMetadataOnly(boolean isAbstract, Boolean isHighest, ComponentTypeEnum componentTypeEnum, String internalComponentType) {
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(),
1953 properties.add(new ImmutableTriple<>(QueryType.HAS, GraphPropertiesDictionary.IS_ABSTRACT.getProperty(), isAbstract));
1955 if (internalComponentType != null) {
1956 switch (internalComponentType.toLowerCase()) {
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());
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());
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());
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(),
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
1996 if (resourceNodes.right().value().equals(TitanOperationStatus.NOT_FOUND)) {
1997 return Either.left(new ArrayList<>());
1999 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(resourceNodes.right().value()));
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();
2008 return Either.left(resCollection);
2011 titanGenericDao.commit();
2016 public Either<List<Component>, StorageOperationStatus> getLatestVersionNotAbstractComponents(boolean isAbstract, Boolean isHighest, ComponentTypeEnum componentTypeEnum, String internalComponentType, List<String> componentUids) {
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());
2025 Collection<ComponentMetadataData> collection = resourceNodes.left().value();
2027 if (collection == null) {
2028 componentUids = new ArrayList<>();
2030 componentUids = collection.stream().map(p -> p.getMetadataDataDefinition().getUniqueId()).collect(Collectors.toList());
2031 // collection.forEach(p -> {
2032 // if (NodeTypeEnum.Resource.getName().equals(p.getLabel()))
2034 // componentUidsMap.put(p.getMetadataDataDefinition().getUniqueId(),
2035 // ((ResourceMetadataDataDefinition)
2036 // p.getMetadataDataDefinition()).getResourceType());
2043 if (false == componentUids.isEmpty()) {
2045 Manager manager = new Manager();
2046 int numberOfWorkers = 5;
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);
2058 manager.addJob(new Job() {
2060 public Either<Component, StorageOperationStatus> doWork() {
2061 Either<Component, StorageOperationStatus> component = getComponent(componentUid, componentParametersView, false);
2066 LinkedBlockingQueue<Either<Component, StorageOperationStatus>> res = manager.start();
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);
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());
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());
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);
2098 return Either.left(result);
2101 titanGenericDao.commit();
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;
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);
2126 titanStatus = eitherCapabilities.right().value();
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);
2141 status = eitherRequirements.right().value();
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);
2157 <T> Either<T, StorageOperationStatus> getLightComponent(String id, NodeTypeEnum nodeType, boolean inTransaction) {
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()));
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);
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);
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);
2191 Iterator<Edge> iterModifier = vertex.edges(Direction.IN, GraphEdgeLabels.LAST_MODIFIER.name());
2193 if (iterModifier.hasNext() == false) {
2194 log.debug("no modifier was defined for component {}", id);
2195 return Either.right(StorageOperationStatus.GENERAL_ERROR);
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);
2206 TitanOperationStatus status = setComponentCategoriesFromGraph((Component) component);
2207 if (status != TitanOperationStatus.OK) {
2208 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
2212 log.debug("Component with id {} not found", id);
2213 return Either.right(StorageOperationStatus.NOT_FOUND);
2217 log.debug("Component with id {} not found", id);
2218 return Either.right(StorageOperationStatus.NOT_FOUND);
2220 log.debug("Ended to build light component of type {}, id {}", nodeType, id);
2221 return Either.left(component);
2223 if (false == inTransaction) {
2224 titanGenericDao.commit();
2229 Either<Component, StorageOperationStatus> getMetadataComponent(String id, NodeTypeEnum nodeType, boolean inTransaction) {
2230 Component component = null;
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()));
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);
2248 log.debug("Component with id {} not found", id);
2249 return Either.right(StorageOperationStatus.NOT_FOUND);
2253 log.debug("Component with id {} not found", id);
2254 return Either.right(StorageOperationStatus.NOT_FOUND);
2256 log.debug("Ended to build metadata component of type {}, id {}", nodeType, id);
2257 return Either.left(component);
2259 if (false == inTransaction) {
2260 titanGenericDao.commit();
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()));
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()));
2278 Vertex vertex = vertexService.left().value();
2279 Integer instanceCounter = vertex.value(GraphPropertiesDictionary.INSTANCE_COUNTER.getProperty());
2280 return Either.left(instanceCounter);
2283 protected TitanOperationStatus setComponentInstancesPropertiesFromGraph(Component component) {
2285 List<ComponentInstance> resourceInstances = component.getComponentInstances();
2287 Map<String, List<ComponentInstanceProperty>> resourceInstancesProperties = new HashMap<>();
2289 Map<String, List<PropertyDefinition>> alreadyProcessedResources = new HashMap<>();
2291 Map<String, List<ComponentInstanceProperty>> alreadyProcessedInstances = new HashMap<>();
2293 Map<String, ImmutablePair<ComponentInstance, Integer>> processedInstances = new HashMap<>();
2295 if (resourceInstances != null) {
2297 for (ComponentInstance resourceInstance : resourceInstances) {
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);
2304 if (componentInstanceProperties.isRight()) {
2305 TitanOperationStatus status = componentInstanceProperties.right().value();
2306 if (status != TitanOperationStatus.OK) {
2311 List<ComponentInstanceProperty> listOfProps = componentInstanceProperties.left().value();
2312 String resourceInstanceUid = resourceInstance.getUniqueId();
2313 resourceInstancesProperties.put(resourceInstanceUid, listOfProps);
2315 // alreadyProcessedInstances.put(resourceInstance.getUniqueId(),
2316 // resourceInstance);
2318 processedInstances.put(resourceInstance.getUniqueId(), new ImmutablePair<ComponentInstance, Integer>(resourceInstance, path.size()));
2319 path.remove(path.size() - 1);
2325 Either<Map<String, Map<String, ComponentInstanceProperty>>, TitanOperationStatus> findAllPropertiesValuesOnInstances = componentInstanceOperation.findAllPropertyValueOnInstances(processedInstances);
2327 if (findAllPropertiesValuesOnInstances.isRight()) {
2328 TitanOperationStatus status = findAllPropertiesValuesOnInstances.right().value();
2329 if (status != TitanOperationStatus.OK) {
2333 // 2. merge data from rules on properties (resourceInstancesProperties)
2334 propertyOperation.updatePropertiesByPropertyValues(resourceInstancesProperties, findAllPropertiesValuesOnInstances.left().value());
2336 component.setComponentInstancesProperties(resourceInstancesProperties);
2338 return TitanOperationStatus.OK;
2341 protected TitanOperationStatus setComponentInstancesInputsFromGraph(String uniqueId, Component component) {
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();
2355 resourceInstancesInputs.put(resourceInstance.getUniqueId(), eitherRIAttributes.left().value());
2358 if (!resourceInstancesInputs.isEmpty())
2359 component.setComponentInstancesInputs(resourceInstancesInputs);
2365 public Either<String, StorageOperationStatus> getInvariantUUID(NodeTypeEnum nodeType, String componentId, boolean inTransaction) {
2366 Either<String, StorageOperationStatus> res = null;
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;
2374 res = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
2376 Vertex v = vertexByProperty.left().value();
2377 String invariantUUID = v.value(GraphPropertiesDictionary.INVARIANT_UUID.getProperty());
2379 if (invariantUUID == null || invariantUUID.isEmpty()) {
2381 log.info("The component {} has empty invariant UUID.", componentId);
2382 res = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(TitanOperationStatus.INVALID_ELEMENT));
2385 res = Either.left(invariantUUID);
2388 if (false == inTransaction) {
2389 titanGenericDao.commit();
2395 protected TitanOperationStatus setGroupsFromGraph(String uniqueId, Component component, NodeTypeEnum nodeTypeEnum) {
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;
2406 component.setGroups(res.left().value());
2408 return TitanOperationStatus.OK;
2412 protected TitanOperationStatus setComponentInputsFromGraph(String uniqueId, Component component, boolean inTransaction) {
2414 List<InputDefinition> inputs = new ArrayList<>();
2415 TitanOperationStatus status = inputsOperation.findAllResourceInputs(uniqueId, inputs);
2416 if (status == TitanOperationStatus.OK) {
2417 component.setInputs(inputs);
2424 protected StorageOperationStatus deleteGroups(NodeTypeEnum nodeType, String componentId) {
2426 Either<List<GroupDefinition>, StorageOperationStatus> deleteRes = groupOperation.deleteAllGroups(componentId, nodeType, true);
2428 if (deleteRes.isRight()) {
2429 StorageOperationStatus status = deleteRes.right().value();
2433 return StorageOperationStatus.OK;
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;
2442 protected TitanOperationStatus associateInputsToComponent(NodeTypeEnum nodeType, ComponentMetadataData resourceData, List<InputDefinition> properties) {
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);
2451 Map<String, InputDefinition> convertedProperties = new HashMap<>();
2453 if (properties != null) {
2454 for (InputDefinition propertyDefinition : properties) {
2455 convertedProperties.put(propertyDefinition.getName(), propertyDefinition);
2458 Either<List<InputDefinition>, TitanOperationStatus> operationStatus = inputsOperation.addInputsToGraph(resourceData.getMetadataDataDefinition().getUniqueId(), nodeType, convertedProperties, allDataTypes.left().value());
2459 if (operationStatus.isLeft())
2460 return TitanOperationStatus.OK;
2462 return operationStatus.right().value();
2465 return TitanOperationStatus.OK;
2469 protected TitanOperationStatus associateInputsToComponent(TitanVertex metadataVertex, String componentId, List<InputDefinition> properties) {
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);
2478 Map<String, InputDefinition> convertedProperties = new HashMap<>();
2480 if (properties != null) {
2481 for (InputDefinition propertyDefinition : properties) {
2482 convertedProperties.put(propertyDefinition.getName(), propertyDefinition);
2485 return inputsOperation.addInputsToGraph(metadataVertex, componentId, convertedProperties, allDataTypes.left().value());
2488 return TitanOperationStatus.OK;
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;
2501 public Either<List<Component>, ActionStatus> getComponentsFromCacheForCatalog(Set<String> components, ComponentTypeEnum componentType) {
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();
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());
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);
2521 return Either.right(componentsForCatalog.right().value());
2524 public <T extends ComponentMetadataData> Either<List<T>, TitanOperationStatus> getListOfHighestComponents(NodeTypeEnum nodeTypeEnum, Class<T> clazz) {
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());
2533 long endFetchAllStates = System.currentTimeMillis();
2535 if (allHighestStates.isRight()) {
2536 return Either.left(new ArrayList<>());
2538 List<T> services = allHighestStates.left().value();
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);
2546 notCertifiedHighest.add(reData);
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);
2553 HashMap<String, String> serviceNames = new HashMap<>();
2554 for (T data : notCertifiedHighest) {
2555 String serviceName = data.getMetadataDataDefinition().getName();
2556 serviceNames.put(serviceName, serviceName);
2559 for (T data : certifiedHighest) {
2560 String serviceName = data.getMetadataDataDefinition().getName();
2561 if (!serviceNames.containsKey(serviceName)) {
2562 notCertifiedHighest.add(data);
2566 return Either.left(notCertifiedHighest);
2569 protected <T extends Component> Either<T, ActionStatus> getComponentFromCacheIfUpToDate(String uniqueId, ComponentMetadataData componentMetadataData, ComponentParametersView componentParametersView, Class<T> clazz,
2570 ComponentTypeEnum componentTypeEnum) {
2572 long start = System.currentTimeMillis();
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();
2580 // Must calculate allVersions
2581 if (false == componentParametersView.isIgnoreAllVersions()) {
2582 Class<? extends ComponentMetadataData> clazz1 = null;
2583 switch (componentTypeEnum) {
2585 clazz1 = ResourceMetadataData.class;
2588 clazz1 = ServiceMetadataData.class;
2591 clazz1 = ProductMetadataData.class;
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);
2602 Map<String, String> allVersions = versionList.left().value();
2603 cachedComponent.setAllVersions(allVersions);
2605 return Either.right(ActionStatus.GENERAL_ERROR);
2608 if (componentParametersView != null) {
2609 cachedComponent = componentParametersView.filter(cachedComponent, componentTypeEnum);
2611 return Either.left(clazz.cast(cachedComponent));
2614 return Either.right(cacheComponentRes.right().value());
2617 log.trace("Fetch component {} with uid {} from cache took {} ms", componentTypeEnum.name().toLowerCase(), uniqueId, System.currentTimeMillis() - start);
2621 public Either<ImmutablePair<List<Component>, Set<String>>, ActionStatus> getComponentsFromCacheForCatalog(Map<String, Long> components, ComponentTypeEnum componentType) {
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();
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());
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);
2636 ImmutablePair<List<Component>, Set<String>> result = new ImmutablePair<List<Component>, Set<String>>(foundComponents, leftComponents);
2637 return Either.left(result);
2640 return Either.right(componentsForCatalog.right().value());
2646 * @param inTransaction
2647 * @param titanGenericDao
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;
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);
2663 ComponentMetadataData componentData = getMetaDataFromComponent(component);
2665 log.debug("After converting component to componentData. ComponentData = {}", componentData);
2667 if (componentData.getUniqueId() == null) {
2668 log.error("Resource id is missing in the request.");
2669 return Either.right(StorageOperationStatus.BAD_REQUEST);
2672 Either<Integer, StorageOperationStatus> counterStatus = this.getComponentInstanceCoutner(component.getUniqueId(), component.getComponentType().getNodeType());
2674 if (counterStatus.isRight()) {
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);
2682 componentData.setComponentInstanceCounter(counterStatus.left().value());
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);
2690 Either<UserData, TitanOperationStatus> findUser = findUser(modifierUserId);
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);
2700 UserData modifierUserData = findUser.left().value();
2701 String resourceId = component.getUniqueId();
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());
2716 Component currentComponent = (Component) currentComponentResult.left().value();
2717 String currentModifier = currentComponent.getLastUpdaterUserId();
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.");
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);
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);
2734 StorageOperationStatus checkCategories = validateCategories(currentComponent, component, componentData, type);
2735 if (checkCategories != StorageOperationStatus.OK) {
2736 result = Either.right(checkCategories);
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());
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));
2759 log.debug("After creating tag {}", tagData);
2764 Either<ComponentMetadataData, TitanOperationStatus> updateNode = titanGenericDao.updateNode(componentData, ComponentMetadataData.class);
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()));
2772 ComponentMetadataData updatedResourceData = updateNode.left().value();
2773 log.debug("ComponentData After update is {}", updatedResourceData);
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());
2791 //TODO call to new Artifact operation in order to update list of artifacts
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);
2801 if (component.getComponentType().equals(ComponentTypeEnum.RESOURCE)) {
2802 updateDerived(component, currentComponent, componentData, component.getClass());
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);
2812 T updatedResourceValue = updatedResource.left().value();
2813 result = Either.left(updatedResourceValue);
2818 if (false == inTransaction) {
2819 if (result == null || result.isRight()) {
2820 log.error("updateComponent operation : Going to execute rollback on graph.");
2821 titanGenericDao.rollback();
2823 log.debug("updateComponent operation : Going to execute commit on graph.");
2824 titanGenericDao.commit();
2830 private boolean updateServiceNameInVLIsNetworkRolePropertyValues (Component component, String prevSystemName, String newSystemName) {
2831 // find VLIs in service
2833 if(null == component.getComponentInstances() || component.getComponentInstances().isEmpty()){
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()))
2847 res = res && updateNetworkRolePropertyValue(prevSystemName, newSystemName, vlInstance, networkRoleProperty);
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());
2866 updateCustomizationUUID = StorageOperationStatus.EXEUCTION_FAILED;
2867 log.debug("Failed to update property value: {} in resource instance {}", updatedValue, vlInstance.getUniqueId());
2869 return result.isLeft() && StorageOperationStatus.OK == updateCustomizationUUID;
2874 public Either<ComponentMetadataData, StorageOperationStatus> updateComponentLastUpdateDateAndLastModifierOnGraph( Component component, User modifier, NodeTypeEnum componentType, boolean inTransaction) {
2876 log.debug("Going to update last update date and last modifier info of component {}. ", component.getName());
2877 Either<ComponentMetadataData, StorageOperationStatus> result = null;
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.");
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);
2894 Either<ComponentMetadataData, TitanOperationStatus> updateNode = 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()));
2905 result = Either.left(updateNode.left().value());
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());
2911 if (result == null || result.isRight()) {
2912 log.error("Going to execute rollback on graph.");
2913 titanGenericDao.rollback();
2915 log.debug("Going to execute commit on graph.");
2916 titanGenericDao.commit();
2923 * updates component lastUpdateDate on graph node
2925 * @param componentType
2926 * @param lastUpdateDate
2927 * @param inTransaction
2930 public Either<ComponentMetadataData, StorageOperationStatus> updateComponentLastUpdateDateOnGraph( Component component, NodeTypeEnum componentType, Long lastUpdateDate, boolean inTransaction) {
2932 log.debug("Going to update last update date of component {}. ", component.getName());
2933 Either<ComponentMetadataData, StorageOperationStatus> result = null;
2935 ComponentMetadataData componentData = getMetaDataFromComponent(component);
2936 Either<ComponentMetadataData, TitanOperationStatus> updateNode = 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()));
2947 result = Either.left(updateNode.left().value());
2949 }catch(Exception e){
2950 log.error("Exception occured during update last update date of component {}. The message is {}. ", component.getName(), e.getMessage());
2953 if (result == null || result.isRight()) {
2954 log.error("Going to execute rollback on graph.");
2955 titanGenericDao.rollback();
2957 log.debug("Going to execute commit on graph.");
2958 titanGenericDao.commit();