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.jsonjanusgraph.operations;
23 import static org.openecomp.sdc.be.utils.TypeUtils.setField;
25 import java.lang.reflect.Type;
27 import java.util.Map.Entry;
28 import java.util.stream.Collectors;
29 import org.apache.commons.collections4.CollectionUtils;
30 import org.apache.commons.collections4.MapUtils;
31 import org.apache.tinkerpop.gremlin.structure.Direction;
32 import org.apache.tinkerpop.gremlin.structure.Edge;
33 import org.apache.tinkerpop.gremlin.structure.Vertex;
34 import org.apache.tinkerpop.gremlin.structure.VertexProperty;
35 import org.janusgraph.core.JanusGraphVertex;
36 import org.openecomp.sdc.be.config.ConfigurationManager;
37 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
38 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
39 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
40 import org.openecomp.sdc.be.dao.jsongraph.types.EdgePropertyEnum;
41 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
42 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
43 import org.openecomp.sdc.be.dao.jsongraph.utils.JsonParserUtils;
44 import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary;
45 import org.openecomp.sdc.be.datatypes.elements.AdditionalInfoParameterDataDefinition;
46 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
47 import org.openecomp.sdc.be.datatypes.elements.AttributeDataDefinition;
48 import org.openecomp.sdc.be.datatypes.elements.DataTypeDataDefinition;
49 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
50 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
51 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
52 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
53 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
54 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
55 import org.openecomp.sdc.be.model.AttributeDefinition;
56 import org.openecomp.sdc.be.model.ComponentParametersView;
57 import org.openecomp.sdc.be.model.DataTypeDefinition;
58 import org.openecomp.sdc.be.model.LifecycleStateEnum;
59 import org.openecomp.sdc.be.model.catalog.CatalogComponent;
60 import org.openecomp.sdc.be.model.category.CategoryDefinition;
61 import org.openecomp.sdc.be.model.category.SubCategoryDefinition;
62 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.NodeType;
63 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.TopologyTemplate;
64 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElement;
65 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElementTypeEnum;
66 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
67 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
68 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
69 import org.openecomp.sdc.be.utils.TypeUtils;
70 import org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum;
71 import org.openecomp.sdc.common.jsongraph.util.CommonUtility;
72 import org.openecomp.sdc.common.jsongraph.util.CommonUtility.LogLevelEnum;
73 import org.openecomp.sdc.common.log.wrappers.Logger;
74 import org.openecomp.sdc.common.util.ValidationUtils;
75 import org.springframework.beans.factory.annotation.Autowired;
76 import org.springframework.util.StopWatch;
78 import com.google.gson.Gson;
79 import com.google.gson.reflect.TypeToken;
81 import fj.data.Either;
83 public abstract class ToscaElementOperation extends BaseOperation {
84 private static final String FAILED_TO_FETCH_FOR_TOSCA_ELEMENT_WITH_ID_ERROR = "failed to fetch {} for tosca element with id {}, error {}";
86 private static final String CANNOT_FIND_USER_IN_THE_GRAPH_STATUS_IS = "Cannot find user {} in the graph. status is {}";
88 private static final String FAILED_TO_CREATE_EDGE_WITH_LABEL_FROM_USER_VERTEX_TO_TOSCA_ELEMENT_VERTEX_ON_GRAPH_STATUS_IS = "Failed to create edge with label {} from user vertex {} to tosca element vertex {} on graph. Status is {}. ";
90 private static final String FAILED_TO_GET_CREATOR_VERTEX_OF_TOSCA_ELEMENT_VERTEX_ON_GRAPH_STATUS_IS = "Failed to get creator vertex with label {} of tosca element vertex {} on graph. Status is {}. ";
92 private static Logger log = Logger.getLogger(ToscaElementOperation.class.getName());
94 private static final Gson gson = new Gson();
97 protected CategoryOperation categoryOperation;
99 protected Gson getGson() {
103 protected Either<GraphVertex, StorageOperationStatus> getComponentByLabelAndId(String uniqueId, ToscaElementTypeEnum nodeType, JsonParseFlagEnum parseFlag) {
105 Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
106 propertiesToMatch.put(GraphPropertyEnum.UNIQUE_ID, uniqueId);
108 VertexTypeEnum vertexType = ToscaElementTypeEnum.getVertexTypeByToscaType(nodeType);
109 Either<List<GraphVertex>, JanusGraphOperationStatus> getResponse = janusGraphDao
110 .getByCriteria(vertexType, propertiesToMatch, parseFlag);
111 if (getResponse.isRight()) {
112 log.debug("Couldn't fetch component with type {} and unique id {}, error: {}", vertexType, uniqueId, getResponse.right().value());
113 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getResponse.right().value()));
116 List<GraphVertex> componentList = getResponse.left().value();
117 if (componentList.isEmpty()) {
118 log.debug("Component with type {} and unique id {} was not found", vertexType, uniqueId);
119 return Either.right(StorageOperationStatus.NOT_FOUND);
121 GraphVertex vertexG = componentList.get(0);
122 return Either.left(vertexG);
125 public Either<ToscaElement, StorageOperationStatus> getToscaElement(String uniqueId) {
126 return getToscaElement(uniqueId, new ComponentParametersView());
129 public Either<GraphVertex, StorageOperationStatus> markComponentToDelete(GraphVertex componentToDelete) {
130 Either<GraphVertex, StorageOperationStatus> result = null;
132 Boolean isDeleted = (Boolean) componentToDelete.getMetadataProperty(GraphPropertyEnum.IS_DELETED);
133 if (isDeleted != null && isDeleted && !(Boolean) componentToDelete.getMetadataProperty(GraphPropertyEnum.IS_HIGHEST_VERSION)) {
134 // component already marked for delete
135 result = Either.left(componentToDelete);
139 componentToDelete.addMetadataProperty(GraphPropertyEnum.IS_DELETED, Boolean.TRUE);
140 componentToDelete.setJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE, System.currentTimeMillis());
142 Either<GraphVertex, JanusGraphOperationStatus> updateNode = janusGraphDao.updateVertex(componentToDelete);
144 StorageOperationStatus updateComponent;
145 if (updateNode.isRight()) {
146 log.debug("Failed to update component {}. status is {}", componentToDelete.getUniqueId(), updateNode.right().value());
147 updateComponent = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(updateNode.right().value());
148 result = Either.right(updateComponent);
152 result = Either.left(componentToDelete);
158 * Performs a shadow clone of previousToscaElement
160 * @param previousToscaElement
161 * @param nextToscaElement
165 public Either<GraphVertex, StorageOperationStatus> cloneToscaElement(GraphVertex previousToscaElement, GraphVertex nextToscaElement, GraphVertex user) {
167 Either<GraphVertex, StorageOperationStatus> result = null;
168 GraphVertex createdToscaElementVertex = null;
169 JanusGraphOperationStatus status;
171 Either<GraphVertex, JanusGraphOperationStatus> createNextVersionRes = janusGraphDao.createVertex(nextToscaElement);
172 if (createNextVersionRes.isRight()) {
173 status = createNextVersionRes.right().value();
174 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to create tosca element vertex {} with version {} on graph. Status is {}. ", previousToscaElement.getMetadataProperty(GraphPropertyEnum.NORMALIZED_NAME),
175 previousToscaElement.getMetadataProperty(GraphPropertyEnum.VERSION), status);
176 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
178 if (result == null) {
179 createdToscaElementVertex = createNextVersionRes.left().value();
180 final Map<EdgePropertyEnum, Object> properties = new EnumMap<>(EdgePropertyEnum.class);
181 properties.put(EdgePropertyEnum.STATE, createdToscaElementVertex.getMetadataProperty(GraphPropertyEnum.STATE));
182 status = janusGraphDao
183 .createEdge(user.getVertex(), createdToscaElementVertex.getVertex(), EdgeLabelEnum.STATE, properties);
184 if (status != JanusGraphOperationStatus.OK) {
185 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_CREATE_EDGE_WITH_LABEL_FROM_USER_VERTEX_TO_TOSCA_ELEMENT_VERTEX_ON_GRAPH_STATUS_IS, EdgeLabelEnum.STATE, user.getUniqueId(),
186 previousToscaElement.getMetadataProperty(GraphPropertyEnum.NORMALIZED_NAME), status);
187 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
190 if (result == null) {
191 status = janusGraphDao
192 .createEdge(user.getVertex(), createdToscaElementVertex.getVertex(), EdgeLabelEnum.LAST_MODIFIER, new HashMap<>());
193 if (status != JanusGraphOperationStatus.OK) {
194 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_CREATE_EDGE_WITH_LABEL_FROM_USER_VERTEX_TO_TOSCA_ELEMENT_VERTEX_ON_GRAPH_STATUS_IS, EdgeLabelEnum.LAST_MODIFIER, user.getUniqueId(),
195 nextToscaElement.getMetadataProperty(GraphPropertyEnum.NORMALIZED_NAME), status);
196 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
199 if (result == null) {
200 Either<GraphVertex, JanusGraphOperationStatus> creatorVertexRes = janusGraphDao.getParentVertex(previousToscaElement,
201 EdgeLabelEnum.CREATOR, JsonParseFlagEnum.NoParse);
202 if (creatorVertexRes.isRight()) {
203 status = creatorVertexRes.right().value();
204 CommonUtility.addRecordToLog(log,
205 LogLevelEnum.DEBUG, FAILED_TO_GET_CREATOR_VERTEX_OF_TOSCA_ELEMENT_VERTEX_ON_GRAPH_STATUS_IS,
206 EdgeLabelEnum.CREATOR,
207 nextToscaElement.getMetadataProperty(GraphPropertyEnum.NORMALIZED_NAME), status);
208 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
210 status = janusGraphDao.createEdge(creatorVertexRes.left().value().getVertex(), createdToscaElementVertex.getVertex(),
211 EdgeLabelEnum.CREATOR, new HashMap<>());
212 if (status != JanusGraphOperationStatus.OK) {
213 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_CREATE_EDGE_WITH_LABEL_FROM_USER_VERTEX_TO_TOSCA_ELEMENT_VERTEX_ON_GRAPH_STATUS_IS, EdgeLabelEnum.CREATOR, user.getUniqueId(),
214 nextToscaElement.getMetadataProperty(GraphPropertyEnum.NORMALIZED_NAME), status);
215 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
218 if (result == null) {
219 Iterator<Edge> edgesToCopyIter = previousToscaElement.getVertex().edges(Direction.OUT);
220 while (edgesToCopyIter.hasNext()) {
221 Edge currEdge = edgesToCopyIter.next();
222 Vertex currVertex = currEdge.inVertex();
223 status = janusGraphDao
224 .createEdge(createdToscaElementVertex.getVertex(), currVertex, EdgeLabelEnum.getEdgeLabelEnum(currEdge.label()), currEdge);
225 if (status != JanusGraphOperationStatus.OK) {
226 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to create edge with label {} from tosca element vertex {} to vertex with label {} on graph. Status is {}. ", currEdge.label(), createdToscaElementVertex.getUniqueId(),
227 currVertex.property(GraphPropertyEnum.LABEL.getProperty()), status);
228 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
234 if (result == null) {
235 result = Either.left(createdToscaElementVertex);
237 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to clone tosca element {} with the name {}. ", previousToscaElement.getUniqueId(), previousToscaElement.getMetadataProperty(GraphPropertyEnum.NORMALIZED_NAME));
242 protected JanusGraphOperationStatus setLastModifierFromGraph(GraphVertex componentV, ToscaElement toscaElement) {
243 Either<GraphVertex, JanusGraphOperationStatus> parentVertex = janusGraphDao
244 .getParentVertex(componentV, EdgeLabelEnum.LAST_MODIFIER, JsonParseFlagEnum.NoParse);
245 if (parentVertex.isRight()) {
246 log.debug("Failed to fetch last modifier for tosca element with id {} error {}", componentV.getUniqueId(), parentVertex.right().value());
247 return parentVertex.right().value();
249 GraphVertex userV = parentVertex.left().value();
250 String userId = (String) userV.getMetadataProperty(GraphPropertyEnum.USERID);
251 toscaElement.setLastUpdaterUserId(userId);
252 toscaElement.setLastUpdaterFullName(buildFullName(userV));
253 return JanusGraphOperationStatus.OK;
256 public String buildFullName(GraphVertex userV) {
258 String fullName = (String) userV.getMetadataProperty(GraphPropertyEnum.FIRST_NAME);
259 if (fullName == null) {
262 fullName = fullName + " ";
264 String lastName = (String) userV.getMetadataProperty(GraphPropertyEnum.LAST_NAME);
265 if (lastName != null) {
266 fullName += lastName;
271 protected JanusGraphOperationStatus setCreatorFromGraph(GraphVertex componentV, ToscaElement toscaElement) {
272 Either<GraphVertex, JanusGraphOperationStatus> parentVertex = janusGraphDao
273 .getParentVertex(componentV, EdgeLabelEnum.CREATOR, JsonParseFlagEnum.NoParse);
274 if (parentVertex.isRight()) {
275 log.debug("Failed to fetch creator for tosca element with id {} error {}", componentV.getUniqueId(), parentVertex.right().value());
276 return parentVertex.right().value();
278 GraphVertex userV = parentVertex.left().value();
279 String creatorUserId = (String) userV.getMetadataProperty(GraphPropertyEnum.USERID);
280 toscaElement.setCreatorUserId(creatorUserId);
281 toscaElement.setCreatorFullName(buildFullName(userV));
283 return JanusGraphOperationStatus.OK;
286 protected <T extends ToscaElement> T getResourceMetaDataFromResource(T toscaElement) {
287 if (toscaElement.getNormalizedName() == null || toscaElement.getNormalizedName().isEmpty()) {
288 toscaElement.setNormalizedName(ValidationUtils.normaliseComponentName(toscaElement.getName()));
290 if (toscaElement.getSystemName() == null || toscaElement.getSystemName().isEmpty()) {
291 toscaElement.setSystemName(ValidationUtils.convertToSystemName(toscaElement.getName()));
294 LifecycleStateEnum lifecycleStateEnum = toscaElement.getLifecycleState();
295 if (lifecycleStateEnum == null) {
296 toscaElement.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
298 long currentDate = System.currentTimeMillis();
299 if (toscaElement.getCreationDate() == null) {
300 toscaElement.setCreationDate(currentDate);
302 toscaElement.setLastUpdateDate(currentDate);
307 protected void fillCommonMetadata(GraphVertex nodeTypeVertex, ToscaElement toscaElement) {
308 if (toscaElement.isHighestVersion() == null) {
309 toscaElement.setHighestVersion(true);
311 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.IS_DELETED, toscaElement.getMetadataValue(JsonPresentationFields.IS_DELETED));
312 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.IS_HIGHEST_VERSION, toscaElement.getMetadataValueOrDefault(JsonPresentationFields.HIGHEST_VERSION, Boolean.TRUE));
313 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.STATE, toscaElement.getMetadataValue(JsonPresentationFields.LIFECYCLE_STATE));
314 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.RESOURCE_TYPE, toscaElement.getMetadataValue(JsonPresentationFields.RESOURCE_TYPE));
315 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.VERSION, toscaElement.getMetadataValue(JsonPresentationFields.VERSION));
316 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.NORMALIZED_NAME, toscaElement.getMetadataValue(JsonPresentationFields.NORMALIZED_NAME));
317 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.UNIQUE_ID, toscaElement.getMetadataValue(JsonPresentationFields.UNIQUE_ID));
318 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaElement.getMetadataValue(JsonPresentationFields.TOSCA_RESOURCE_NAME));
319 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.UUID, toscaElement.getMetadataValue(JsonPresentationFields.UUID));
320 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.IS_ABSTRACT, toscaElement.getMetadataValue(JsonPresentationFields.IS_ABSTRACT));
321 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.INVARIANT_UUID, toscaElement.getMetadataValue(JsonPresentationFields.INVARIANT_UUID));
322 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.NAME, toscaElement.getMetadataValue(JsonPresentationFields.NAME));
323 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.SYSTEM_NAME, toscaElement.getMetadataValue(JsonPresentationFields.SYSTEM_NAME));
324 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.IS_ARCHIVED, toscaElement.getMetadataValue(JsonPresentationFields.IS_ARCHIVED));
325 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.ARCHIVE_TIME, toscaElement.getMetadataValue(JsonPresentationFields.ARCHIVE_TIME));
326 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.IS_VSP_ARCHIVED, toscaElement.getMetadataValue(JsonPresentationFields.IS_VSP_ARCHIVED));
327 toscaElement.getMetadata().entrySet().stream().filter(e -> e.getValue() != null).forEach(e -> nodeTypeVertex.setJsonMetadataField(JsonPresentationFields.getByPresentation(e.getKey()), e.getValue()));
329 nodeTypeVertex.setUniqueId(toscaElement.getUniqueId());
330 nodeTypeVertex.setType(toscaElement.getComponentType());
331 final String toscaVersion = toscaElement.getToscaVersion();
332 if (toscaVersion != null) {
333 nodeTypeVertex.setJsonMetadataField(JsonPresentationFields.TOSCA_DEFINITIONS_VERSION, toscaVersion);
335 final Map<String, DataTypeDataDefinition> dataTypes = toscaElement.getDataTypes();
336 if (MapUtils.isNotEmpty(dataTypes)) {
337 nodeTypeVertex.setJsonMetadataField(JsonPresentationFields.DATA_TYPES, dataTypes);
341 protected StorageOperationStatus assosiateToUsers(GraphVertex nodeTypeVertex, ToscaElement toscaElement) {
343 String userId = toscaElement.getCreatorUserId();
345 Either<GraphVertex, JanusGraphOperationStatus> findUser = findUserVertex(userId);
347 if (findUser.isRight()) {
348 JanusGraphOperationStatus status = findUser.right().value();
349 log.error(CANNOT_FIND_USER_IN_THE_GRAPH_STATUS_IS, userId, status);
350 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status);
353 GraphVertex creatorVertex = findUser.left().value();
354 GraphVertex updaterVertex = creatorVertex;
355 String updaterId = toscaElement.getLastUpdaterUserId();
356 if (updaterId != null && !updaterId.equals(userId)) {
357 findUser = findUserVertex(updaterId);
358 if (findUser.isRight()) {
359 JanusGraphOperationStatus status = findUser.right().value();
360 log.error(CANNOT_FIND_USER_IN_THE_GRAPH_STATUS_IS, userId, status);
361 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status);
363 updaterVertex = findUser.left().value();
366 Map<EdgePropertyEnum, Object> props = new EnumMap<>(EdgePropertyEnum.class);
367 props.put(EdgePropertyEnum.STATE, (String) toscaElement.getMetadataValue(JsonPresentationFields.LIFECYCLE_STATE));
369 JanusGraphOperationStatus
370 result = janusGraphDao
371 .createEdge(updaterVertex, nodeTypeVertex, EdgeLabelEnum.STATE, props);
372 log.debug("After associating user {} to resource {}. Edge type is {}", updaterVertex, nodeTypeVertex.getUniqueId(), EdgeLabelEnum.STATE);
373 if (JanusGraphOperationStatus.OK != result) {
374 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(result);
376 result = janusGraphDao
377 .createEdge(updaterVertex, nodeTypeVertex, EdgeLabelEnum.LAST_MODIFIER, null);
378 log.debug("After associating user {} to resource {}. Edge type is {}", updaterVertex, nodeTypeVertex.getUniqueId(), EdgeLabelEnum.LAST_MODIFIER);
379 if (!result.equals(JanusGraphOperationStatus.OK)) {
380 log.error("Failed to associate user {} to resource {}. Edge type is {}", updaterVertex, nodeTypeVertex.getUniqueId(), EdgeLabelEnum.LAST_MODIFIER);
381 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(result);
384 toscaElement.setLastUpdaterUserId(toscaElement.getCreatorUserId());
385 toscaElement.setLastUpdaterFullName(toscaElement.getCreatorFullName());
387 result = janusGraphDao.createEdge(creatorVertex, nodeTypeVertex, EdgeLabelEnum.CREATOR, null);
388 log.debug("After associating user {} to resource {}. Edge type is {} ", creatorVertex, nodeTypeVertex.getUniqueId(), EdgeLabelEnum.CREATOR);
389 if (!result.equals(JanusGraphOperationStatus.OK)) {
390 log.error("Failed to associate user {} to resource {}. Edge type is {} ", creatorVertex, nodeTypeVertex.getUniqueId(), EdgeLabelEnum.CREATOR);
391 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(result);
393 return StorageOperationStatus.OK;
396 protected StorageOperationStatus assosiateResourceMetadataToCategory(GraphVertex nodeTypeVertex, ToscaElement nodeType) {
397 String subcategoryName = nodeType.getCategories().get(0).getSubcategories().get(0).getName();
398 String categoryName = nodeType.getCategories().get(0).getName();
399 Either<GraphVertex, StorageOperationStatus> getCategoryVertex = getResourceCategoryVertex(nodeType.getUniqueId(), subcategoryName, categoryName);
401 if (getCategoryVertex.isRight()) {
402 return getCategoryVertex.right().value();
405 GraphVertex subCategoryV = getCategoryVertex.left().value();
407 JanusGraphOperationStatus
408 createEdge = janusGraphDao
409 .createEdge(nodeTypeVertex, subCategoryV, EdgeLabelEnum.CATEGORY, new HashMap<>());
410 if (createEdge != JanusGraphOperationStatus.OK) {
411 log.trace("Failed to associate resource {} to category {} with id {}", nodeType.getUniqueId(), subcategoryName, subCategoryV.getUniqueId());
412 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(createEdge);
414 return StorageOperationStatus.OK;
417 protected Either<GraphVertex, StorageOperationStatus> getResourceCategoryVertex(String elementId, String subcategoryName, String categoryName) {
418 Either<GraphVertex, StorageOperationStatus> category = categoryOperation.getCategory(categoryName, VertexTypeEnum.RESOURCE_CATEGORY);
419 if (category.isRight()) {
420 log.trace("Failed to fetch category {} for resource {} error {}", categoryName, elementId, category.right().value());
421 return Either.right(category.right().value());
423 GraphVertex categoryV = category.left().value();
425 if (subcategoryName != null) {
426 Either<GraphVertex, StorageOperationStatus> subCategory = categoryOperation.getSubCategoryForCategory(categoryV, subcategoryName);
427 if (subCategory.isRight()) {
428 log.trace("Failed to fetch subcategory {} of category for resource {} error {}", subcategoryName, categoryName, elementId, subCategory.right().value());
429 return Either.right(subCategory.right().value());
432 GraphVertex subCategoryV = subCategory.left().value();
433 return Either.left(subCategoryV);
435 return Either.left(categoryV);
438 private StorageOperationStatus associateArtifactsToResource(GraphVertex nodeTypeVertex, ToscaElement toscaElement) {
439 Map<String, ArtifactDataDefinition> artifacts = toscaElement.getArtifacts();
440 Either<GraphVertex, StorageOperationStatus> status;
441 if (artifacts != null) {
442 artifacts.values().stream().filter(a -> a.getUniqueId() == null).forEach(a -> {
443 String uniqueId = UniqueIdBuilder.buildPropertyUniqueId(nodeTypeVertex.getUniqueId().toLowerCase(), a.getArtifactLabel().toLowerCase());
444 a.setUniqueId(uniqueId);
446 status = associateElementToData(nodeTypeVertex, VertexTypeEnum.ARTIFACTS, EdgeLabelEnum.ARTIFACTS, artifacts);
447 if (status.isRight()) {
448 return status.right().value();
451 Map<String, ArtifactDataDefinition> toscaArtifacts = toscaElement.getToscaArtifacts();
452 if (toscaArtifacts != null) {
453 toscaArtifacts.values().stream().filter(a -> a.getUniqueId() == null).forEach(a -> {
454 String uniqueId = UniqueIdBuilder.buildPropertyUniqueId(nodeTypeVertex.getUniqueId().toLowerCase(), a.getArtifactLabel().toLowerCase());
455 a.setUniqueId(uniqueId);
457 status = associateElementToData(nodeTypeVertex, VertexTypeEnum.TOSCA_ARTIFACTS, EdgeLabelEnum.TOSCA_ARTIFACTS, toscaArtifacts);
458 if (status.isRight()) {
459 return status.right().value();
462 Map<String, ArtifactDataDefinition> deploymentArtifacts = toscaElement.getDeploymentArtifacts();
463 if (deploymentArtifacts != null) {
464 deploymentArtifacts.values().stream().filter(a -> a.getUniqueId() == null).forEach(a -> {
465 String uniqueId = UniqueIdBuilder.buildPropertyUniqueId(nodeTypeVertex.getUniqueId().toLowerCase(), a.getArtifactLabel().toLowerCase());
466 a.setUniqueId(uniqueId);
468 status = associateElementToData(nodeTypeVertex, VertexTypeEnum.DEPLOYMENT_ARTIFACTS, EdgeLabelEnum.DEPLOYMENT_ARTIFACTS, deploymentArtifacts);
469 if (status.isRight()) {
470 return status.right().value();
473 return StorageOperationStatus.OK;
476 protected JanusGraphOperationStatus disassociateAndDeleteCommonElements(GraphVertex toscaElementVertex) {
477 JanusGraphOperationStatus
478 status = janusGraphDao
479 .disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.ARTIFACTS);
480 if (status != JanusGraphOperationStatus.OK) {
481 log.debug("Failed to disaccociate artifact for {} error {}", toscaElementVertex.getUniqueId(), status);
484 status = janusGraphDao
485 .disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.TOSCA_ARTIFACTS);
486 if (status != JanusGraphOperationStatus.OK) {
487 log.debug("Failed to disaccociate tosca artifact for {} error {}", toscaElementVertex.getUniqueId(), status);
490 status = janusGraphDao
491 .disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.DEPLOYMENT_ARTIFACTS);
492 if (status != JanusGraphOperationStatus.OK) {
493 log.debug("Failed to deployment artifact for {} error {}", toscaElementVertex.getUniqueId(), status);
496 status = janusGraphDao
497 .disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.PROPERTIES);
498 if (status != JanusGraphOperationStatus.OK) {
499 log.debug("Failed to disaccociate properties for {} error {}", toscaElementVertex.getUniqueId(), status);
502 status = janusGraphDao
503 .disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.ATTRIBUTES);
504 if (status != JanusGraphOperationStatus.OK) {
505 log.debug("Failed to disaccociate attributes for {} error {}", toscaElementVertex.getUniqueId(), status);
508 status = janusGraphDao
509 .disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.ADDITIONAL_INFORMATION);
510 if (status != JanusGraphOperationStatus.OK) {
511 log.debug("Failed to disaccociate additional information for {} error {}", toscaElementVertex.getUniqueId(), status);
514 status = janusGraphDao
515 .disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.CAPABILITIES);
516 if (status != JanusGraphOperationStatus.OK) {
517 log.debug("Failed to disaccociate capabilities for {} error {}", toscaElementVertex.getUniqueId(), status);
520 status = janusGraphDao
521 .disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.REQUIREMENTS);
522 if (status != JanusGraphOperationStatus.OK) {
523 log.debug("Failed to disaccociate requirements for {} error {}", toscaElementVertex.getUniqueId(), status);
526 status = janusGraphDao
527 .disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.FORWARDING_PATH);
528 if (status != JanusGraphOperationStatus.OK) {
529 log.debug("Failed to disaccociate requirements for {} error {}", toscaElementVertex.getUniqueId(), status);
532 return JanusGraphOperationStatus.OK;
535 protected StorageOperationStatus assosiateCommonForToscaElement(GraphVertex nodeTypeVertex, ToscaElement toscaElement, List<GraphVertex> derivedResources) {
537 StorageOperationStatus associateUsers = assosiateToUsers(nodeTypeVertex, toscaElement);
538 if (associateUsers != StorageOperationStatus.OK) {
539 return associateUsers;
541 StorageOperationStatus associateArtifacts = associateArtifactsToResource(nodeTypeVertex, toscaElement);
542 if (associateArtifacts != StorageOperationStatus.OK) {
543 return associateArtifacts;
545 StorageOperationStatus associateProperties = associatePropertiesToResource(nodeTypeVertex, toscaElement, derivedResources);
546 if (associateProperties != StorageOperationStatus.OK) {
547 return associateProperties;
549 StorageOperationStatus associateAdditionaInfo = associateAdditionalInfoToResource(nodeTypeVertex, toscaElement);
550 if (associateAdditionaInfo != StorageOperationStatus.OK) {
551 return associateAdditionaInfo;
553 if (needConnectToCatalog(toscaElement)) {
554 StorageOperationStatus associateToCatalog = associateToCatalogRoot(nodeTypeVertex);
555 if (associateToCatalog != StorageOperationStatus.OK) {
556 return associateToCatalog;
559 return StorageOperationStatus.OK;
562 private boolean needConnectToCatalog(ToscaElement toscaElement) {
563 Boolean isAbstract = (Boolean) toscaElement.getMetadataValue(JsonPresentationFields.IS_ABSTRACT);
564 if (isAbstract != null && isAbstract) {
567 return toscaElement.isHighestVersion();
570 private StorageOperationStatus associateToCatalogRoot(GraphVertex nodeTypeVertex) {
571 Either<GraphVertex, JanusGraphOperationStatus> catalog = janusGraphDao.getVertexByLabel(VertexTypeEnum.CATALOG_ROOT);
572 if (catalog.isRight()) {
573 log.debug("Failed to fetch catalog vertex. error {}", catalog.right().value());
574 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(catalog.right().value());
576 JanusGraphOperationStatus
577 createEdge = janusGraphDao
578 .createEdge(catalog.left().value(), nodeTypeVertex, EdgeLabelEnum.CATALOG_ELEMENT, null);
580 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(createEdge);
583 protected StorageOperationStatus associatePropertiesToResource(GraphVertex nodeTypeVertex, ToscaElement nodeType, List<GraphVertex> derivedResources) {
584 // Note : currently only one derived supported!!!!
585 Either<Map<String, PropertyDataDefinition>, StorageOperationStatus> dataFromDerived = getDataFromDerived(derivedResources, EdgeLabelEnum.PROPERTIES);
586 if (dataFromDerived.isRight()) {
587 return dataFromDerived.right().value();
589 Map<String, PropertyDataDefinition> propertiesAll = dataFromDerived.left().value();
591 Map<String, PropertyDataDefinition> properties = nodeType.getProperties();
593 if (properties != null) {
594 properties.values().stream().filter(p -> p.getUniqueId() == null).forEach(p -> {
595 String uid = UniqueIdBuilder.buildPropertyUniqueId(nodeTypeVertex.getUniqueId(), p.getName());
599 Either<Map<String, PropertyDataDefinition>, String> eitherMerged = ToscaDataDefinition.mergeDataMaps(propertiesAll, properties);
600 if (eitherMerged.isRight()) {
601 // TODO re-factor error handling - moving BL to operation resulted in loss of info about the invalid property
602 log.debug("property {} cannot be overriden", eitherMerged.right().value());
603 return StorageOperationStatus.INVALID_PROPERTY;
606 if (!propertiesAll.isEmpty()) {
607 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = associateElementToData(nodeTypeVertex, VertexTypeEnum.PROPERTIES, EdgeLabelEnum.PROPERTIES, propertiesAll);
608 if (assosiateElementToData.isRight()) {
609 return assosiateElementToData.right().value();
612 return StorageOperationStatus.OK;
615 private StorageOperationStatus associateAdditionalInfoToResource(GraphVertex nodeTypeVertex, ToscaElement nodeType) {
616 Map<String, AdditionalInfoParameterDataDefinition> additionalInformation = nodeType.getAdditionalInformation();
617 if (additionalInformation != null) {
618 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = associateElementToData(nodeTypeVertex, VertexTypeEnum.ADDITIONAL_INFORMATION, EdgeLabelEnum.ADDITIONAL_INFORMATION, additionalInformation);
619 if (assosiateElementToData.isRight()) {
620 return assosiateElementToData.right().value();
623 return StorageOperationStatus.OK;
626 protected <T extends ToscaDataDefinition> Either<Map<String, T>, StorageOperationStatus> getDataFromDerived(List<GraphVertex> derivedResources, EdgeLabelEnum edge) {
627 Map<String, T> propertiesAll = new HashMap<>();
629 if (derivedResources != null && !derivedResources.isEmpty()) {
630 for (GraphVertex derived : derivedResources) {
631 Either<List<GraphVertex>, JanusGraphOperationStatus> derivedProperties = janusGraphDao.getChildrenVertices(derived, edge, JsonParseFlagEnum.ParseJson);
632 if (derivedProperties.isRight()) {
633 if (derivedProperties.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
634 log.debug("Failed to get properties for derived from {} error {}", derived.getUniqueId(), derivedProperties.right().value());
635 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(derivedProperties.right().value()));
640 List<GraphVertex> propList = derivedProperties.left().value();
641 for (GraphVertex propV : propList) {
642 Map<String, T> propertiesFromDerived = (Map<String, T>) propV.getJson();
643 if (propertiesFromDerived != null) {
644 propertiesFromDerived.entrySet().forEach(x -> x.getValue().setOwnerIdIfEmpty(derived.getUniqueId()));
645 propertiesAll.putAll(propertiesFromDerived);
650 return Either.left(propertiesAll);
653 protected JanusGraphOperationStatus setArtifactsFromGraph(GraphVertex componentV, ToscaElement toscaElement) {
654 Either<Map<String, ArtifactDataDefinition>, JanusGraphOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.ARTIFACTS);
655 if (result.isLeft()) {
656 toscaElement.setArtifacts(result.left().value());
658 if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
659 return result.right().value();
662 result = getDataFromGraph(componentV, EdgeLabelEnum.DEPLOYMENT_ARTIFACTS);
663 if (result.isLeft()) {
664 toscaElement.setDeploymentArtifacts(result.left().value());
666 if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
667 return result.right().value();
670 result = getDataFromGraph(componentV, EdgeLabelEnum.TOSCA_ARTIFACTS);
671 if (result.isLeft()) {
672 toscaElement.setToscaArtifacts(result.left().value());
674 if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
675 return result.right().value();
678 return JanusGraphOperationStatus.OK;
681 protected JanusGraphOperationStatus setAllVersions(GraphVertex componentV, ToscaElement toscaElement) {
682 Map<String, String> allVersion = new HashMap<>();
684 allVersion.put((String) componentV.getMetadataProperty(GraphPropertyEnum.VERSION), componentV.getUniqueId());
685 ArrayList<GraphVertex> allChildrenAndParants = new ArrayList<>();
686 Either<GraphVertex, JanusGraphOperationStatus> childResourceRes = janusGraphDao
687 .getChildVertex(componentV, EdgeLabelEnum.VERSION, JsonParseFlagEnum.NoParse);
688 while (childResourceRes.isLeft()) {
689 GraphVertex child = childResourceRes.left().value();
690 allChildrenAndParants.add(child);
691 childResourceRes = janusGraphDao
692 .getChildVertex(child, EdgeLabelEnum.VERSION, JsonParseFlagEnum.NoParse);
694 JanusGraphOperationStatus operationStatus = childResourceRes.right().value();
696 if (operationStatus != JanusGraphOperationStatus.NOT_FOUND) {
697 return operationStatus;
699 Either<GraphVertex, JanusGraphOperationStatus> parentResourceRes = janusGraphDao
700 .getParentVertex(componentV, EdgeLabelEnum.VERSION, JsonParseFlagEnum.NoParse);
701 while (parentResourceRes.isLeft()) {
702 GraphVertex parent = parentResourceRes.left().value();
703 allChildrenAndParants.add(parent);
704 parentResourceRes = janusGraphDao
705 .getParentVertex(parent, EdgeLabelEnum.VERSION, JsonParseFlagEnum.NoParse);
707 operationStatus = parentResourceRes.right().value();
708 if (operationStatus != JanusGraphOperationStatus.NOT_FOUND) {
709 return operationStatus;
711 allChildrenAndParants.stream().filter(vertex -> {
712 Boolean isDeleted = (Boolean) vertex.getMetadataProperty(GraphPropertyEnum.IS_DELETED);
713 return (isDeleted == null || !isDeleted);
714 }).forEach(vertex -> allVersion.put((String) vertex.getMetadataProperty(GraphPropertyEnum.VERSION), vertex.getUniqueId()));
716 toscaElement.setAllVersions(allVersion);
717 return JanusGraphOperationStatus.OK;
722 protected <T extends ToscaElement> Either<List<T>, StorageOperationStatus> getFollowedComponent(String userId, Set<LifecycleStateEnum> lifecycleStates, Set<LifecycleStateEnum> lastStateStates, ComponentTypeEnum neededType) {
724 Either<List<T>, StorageOperationStatus> result = null;
726 Map<GraphPropertyEnum, Object> props = null;
728 if (userId != null) {
729 props = new EnumMap<>(GraphPropertyEnum.class);
730 // for Designer retrieve specific user
731 props.put(GraphPropertyEnum.USERID, userId);
733 // in case of user id == null -> get all users by label
734 // for Tester and Admin retrieve all users
735 Either<List<GraphVertex>, JanusGraphOperationStatus> usersByCriteria = janusGraphDao
736 .getByCriteria(VertexTypeEnum.USER, props, JsonParseFlagEnum.NoParse);
737 if (usersByCriteria.isRight()) {
738 log.debug("Failed to fetch users by criteria {} error {}", props, usersByCriteria.right().value());
739 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(usersByCriteria.right().value()));
741 GraphVertex userV = usersByCriteria.left().value().get(0);
743 List<T> components = new ArrayList<>();
744 List<T> componentsPerUser;
746 final Set<String> ids = new HashSet<>();
747 Either<List<GraphVertex>, JanusGraphOperationStatus> childrenVertecies = janusGraphDao.getChildrenVertices(userV, EdgeLabelEnum.STATE, JsonParseFlagEnum.NoParse);
748 if (childrenVertecies.isRight() && childrenVertecies.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
749 log.debug("Failed to fetch children vertices for user {} by edge {} error {}", userV.getMetadataProperty(GraphPropertyEnum.USERID), EdgeLabelEnum.STATE, childrenVertecies.right().value());
750 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(childrenVertecies.right().value()));
753 // get all resource with current state
754 if (childrenVertecies.isLeft()) {
755 componentsPerUser = fetchComponents(userId, lifecycleStates, childrenVertecies.left().value(), neededType, EdgeLabelEnum.STATE);
757 if (componentsPerUser != null) {
758 for (T comp : componentsPerUser) {
759 ids.add(comp.getUniqueId());
760 components.add(comp);
763 if (lastStateStates != null && !lastStateStates.isEmpty()) {
764 // get all resource with last state
765 childrenVertecies = janusGraphDao.getChildrenVertices(userV, EdgeLabelEnum.LAST_STATE, JsonParseFlagEnum.NoParse);
766 if (childrenVertecies.isRight() && childrenVertecies.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
767 log.debug("Failed to fetch children vertices for user {} by edge {} error {}", userV.getMetadataProperty(GraphPropertyEnum.USERID), EdgeLabelEnum.LAST_STATE, childrenVertecies.right().value());
768 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(childrenVertecies.right().value()));
770 if (childrenVertecies.isLeft()) {
772 componentsPerUser = fetchComponents(userId, lastStateStates, childrenVertecies.left().value(), neededType, EdgeLabelEnum.LAST_STATE);
773 if (componentsPerUser != null) {
774 for (T comp : componentsPerUser) {
777 if (ids.contains(comp.getUniqueId())) {
781 components.add(comp);
791 result = Either.left(components);
796 private <T extends ToscaElement> List<T> fetchComponents(String userId, Set<LifecycleStateEnum> lifecycleStates, List<GraphVertex> vertices, ComponentTypeEnum neededType, EdgeLabelEnum edgelabel) {
797 List<T> components = new ArrayList<>();
798 for (GraphVertex node : vertices) {
800 Iterator<Edge> edges = node.getVertex().edges(Direction.IN, edgelabel.name());
801 while (edges.hasNext()) {
802 Edge edge = edges.next();
803 String stateStr = (String) janusGraphDao.getProperty(edge, EdgePropertyEnum.STATE);
805 LifecycleStateEnum nodeState = LifecycleStateEnum.findState(stateStr);
806 if (nodeState == null) {
807 log.debug("no supported STATE {} for element {}", stateStr, node.getUniqueId());
811 //get user from edge and compare to user from followed request
812 JanusGraphVertex userVertex = (JanusGraphVertex) edge.outVertex();
813 String userIdFromEdge = (String) janusGraphDao.getProperty(userVertex, GraphPropertyEnum.USERID.getProperty());
815 if (lifecycleStates != null && lifecycleStates.contains(nodeState) && (userIdFromEdge.equals(userId))) {
817 Boolean isDeleted = (Boolean) node.getMetadataProperty(GraphPropertyEnum.IS_DELETED);
818 Boolean isArchived = (Boolean) node.getMetadataProperty(GraphPropertyEnum.IS_ARCHIVED);
819 if (isDeleted != null && isDeleted || isArchived != null && isArchived) {
820 log.trace("Deleted/Archived element {}, discard", node.getUniqueId());
824 Boolean isHighest = (Boolean) node.getMetadataProperty(GraphPropertyEnum.IS_HIGHEST_VERSION);
827 ComponentTypeEnum componentType = node.getType();
828 // get only latest versions
830 if (componentType == null) {
831 log.debug("No supported type {} for vertex {}", componentType, node.getUniqueId());
834 if (neededType == componentType) {
835 switch (componentType) {
838 handleNode(components, node, componentType);
841 Boolean isAbtract = (Boolean) node.getMetadataProperty(GraphPropertyEnum.IS_ABSTRACT);
842 if (isAbtract == null || !isAbtract) {
843 handleNode(components, node, componentType);
847 log.debug("not supported node type {}", componentType);
858 protected <T extends ToscaElement> void handleNode(List<T> components, GraphVertex vertexComponent, ComponentTypeEnum nodeType) {
860 Either<T, StorageOperationStatus> component = getLightComponent(vertexComponent, nodeType, new ComponentParametersView(true));
861 if (component.isRight()) {
862 log.debug("Failed to get component for id = {} error : {} skip resource", vertexComponent.getUniqueId(), component.right().value());
864 components.add(component.left().value());
868 protected <T extends ToscaElement> Either<T, StorageOperationStatus> getLightComponent(String componentUid, ComponentTypeEnum nodeType, ComponentParametersView parametersFilter) {
869 Either<GraphVertex, JanusGraphOperationStatus> getVertexRes = janusGraphDao.getVertexById(componentUid);
870 if (getVertexRes.isRight()) {
871 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexRes.right().value()));
873 return getLightComponent(getVertexRes.left().value(), nodeType, parametersFilter);
876 protected <T extends ToscaElement> Either<T, StorageOperationStatus> getLightComponent(GraphVertex vertexComponent, ComponentTypeEnum nodeType, ComponentParametersView parametersFilter) {
878 log.trace("Starting to build light component of type {}, id {}", nodeType, vertexComponent.getUniqueId());
880 janusGraphDao.parseVertexProperties(vertexComponent, JsonParseFlagEnum.ParseMetadata);
882 T toscaElement = convertToComponent(vertexComponent);
884 JanusGraphOperationStatus status = setCreatorFromGraph(vertexComponent, toscaElement);
885 if (status != JanusGraphOperationStatus.OK) {
886 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
889 status = setLastModifierFromGraph(vertexComponent, toscaElement);
890 if (status != JanusGraphOperationStatus.OK) {
891 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
893 status = setCategoriesFromGraph(vertexComponent, toscaElement);
894 if (status != JanusGraphOperationStatus.OK) {
895 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
897 if (!parametersFilter.isIgnoreAllVersions()) {
898 status = setAllVersions(vertexComponent, toscaElement);
899 if (status != JanusGraphOperationStatus.OK) {
900 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
903 if (!parametersFilter.isIgnoreCapabilities()) {
904 status = setCapabilitiesFromGraph(vertexComponent, toscaElement);
905 if (status != JanusGraphOperationStatus.OK) {
906 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
909 if (!parametersFilter.isIgnoreRequirements()) {
910 status = setRequirementsFromGraph(vertexComponent, toscaElement);
911 if (status != JanusGraphOperationStatus.OK) {
912 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
915 log.debug("Ended to build light component of type {}, id {}", nodeType, vertexComponent.getUniqueId());
916 return Either.left(toscaElement);
919 @SuppressWarnings("unchecked")
920 protected <T extends ToscaElement> T convertToComponent(GraphVertex componentV) {
921 ToscaElement toscaElement = null;
922 VertexTypeEnum label = componentV.getLabel();
925 toscaElement = new NodeType();
926 ((NodeType) toscaElement).setAttributes(getAttributesFromComponentV(componentV));
928 case TOPOLOGY_TEMPLATE:
929 toscaElement = new TopologyTemplate();
932 log.debug("Not supported tosca type {}", label);
936 if (toscaElement != null) {
937 final Map<String, Object> jsonMetada = componentV.getMetadataJson();
938 if (MapUtils.isNotEmpty(jsonMetada)) {
939 toscaElement.setMetadata(jsonMetada);
940 final Object toscaVersion = jsonMetada.get(ToscaTagNamesEnum.TOSCA_VERSION.getElementName());
941 if (toscaVersion != null) {
942 toscaElement.setToscaVersion((String) toscaVersion);
944 final Object dataTypes = jsonMetada.get(ToscaTagNamesEnum.DATA_TYPES.getElementName());
945 if (dataTypes != null) {
946 final Map<String, DataTypeDataDefinition> dataTypeDefinitionMap = new HashMap<>();
948 final Map<String, Object> toscaAttributes = (Map<String, Object>) dataTypes;
950 for (final Entry<String, Object> attributeNameValue : toscaAttributes.entrySet()) {
951 final Object value = attributeNameValue.getValue();
952 final String key = attributeNameValue.getKey();
953 if (value instanceof Map) {
954 final DataTypeDefinition dataTypeDefinition =
955 createDataTypeDefinitionWithName(attributeNameValue);
956 dataTypeDefinitionMap.put(dataTypeDefinition.getName(), dataTypeDefinition);
958 dataTypeDefinitionMap.put(key, createDataType(String.valueOf(value)));
961 toscaElement.setDataTypes(dataTypeDefinitionMap);
965 return (T) toscaElement;
968 public static DataTypeDefinition createDataType(final String dataTypeName) {
969 final DataTypeDefinition dataType = new DataTypeDefinition();
970 dataType.setName(dataTypeName);
974 public static DataTypeDefinition createDataTypeDefinitionWithName(final Entry<String, Object> attributeNameValue) {
975 final Map<String, Object> attributeMap = (Map<String, Object>) attributeNameValue.getValue();
976 final DataTypeDefinition dataType = createDataType(attributeNameValue.getKey());
977 setField(attributeMap, TypeUtils.ToscaTagNamesEnum.DESCRIPTION, dataType::setDescription);
978 setField(attributeMap, TypeUtils.ToscaTagNamesEnum.DERIVED_FROM_NAME, dataType::setDerivedFromName);
979 // TODO - find the way to set the properties
980 // CommonImportManager.setProperties(attributeMap, dataType::setProperties);
981 final Object derivedFrom = attributeMap.get(JsonPresentationFields.DERIVED_FROM.getPresentation());
982 if (derivedFrom instanceof Map) {
983 final Map<String, Object> derivedFromMap = (Map<String, Object>) derivedFrom;
984 final DataTypeDefinition parentDataTypeDataDefinition = new DataTypeDefinition();
985 parentDataTypeDataDefinition
986 .setName((String) derivedFromMap.get(JsonPresentationFields.NAME.getPresentation()));
987 parentDataTypeDataDefinition
988 .setUniqueId((String) derivedFromMap.get(JsonPresentationFields.UNIQUE_ID.getPresentation()));
989 parentDataTypeDataDefinition
990 .setCreationTime((Long) derivedFromMap.get(JsonPresentationFields.CREATION_TIME.getPresentation()));
991 parentDataTypeDataDefinition.setModificationTime(
992 (Long) derivedFromMap.get(JsonPresentationFields.MODIFICATION_TIME.getPresentation()));
993 dataType.setDerivedFrom(parentDataTypeDataDefinition);
998 private Map<String, AttributeDataDefinition> getAttributesFromComponentV(final GraphVertex componentV) {
999 final Map<String, Object> jsonMetada = componentV.getMetadataJson();
1000 final Map<String, AttributeDataDefinition> attributeDataDefinitionMap = new HashMap<>();
1001 if (MapUtils.isNotEmpty(jsonMetada)) {
1002 final Object attributes = jsonMetada.get(ToscaTagNamesEnum.ATTRIBUTES.getElementName());
1003 if (attributes instanceof Map) {
1004 final Map<String, Object> map = (Map<String, Object>) attributes;
1005 attributeDataDefinitionMap.putAll(map.values().stream().map(attributeMap -> {
1006 final AttributeDefinition attributeDef = new AttributeDefinition();
1007 final String name = (String) ((Map<String, Object>) attributeMap).get("name");
1008 attributeDef.setName(name);
1009 final String type = (String) ((Map<String, Object>) attributeMap).get("type");
1010 attributeDef.setType(type);
1011 final String description = (String) ((Map<String, Object>) attributeMap).get("description");
1012 attributeDef.setDescription(description);
1013 return attributeDef;
1014 }).collect(Collectors.toMap(AttributeDefinition::getName, a -> a)));
1017 return attributeDataDefinitionMap;
1020 protected JanusGraphOperationStatus setResourceCategoryFromGraphV(Vertex vertex, CatalogComponent catalogComponent) {
1021 List<CategoryDefinition> categories = new ArrayList<>();
1022 SubCategoryDefinition subcategory;
1024 Either<Vertex, JanusGraphOperationStatus> childVertex = janusGraphDao
1025 .getChildVertex(vertex, EdgeLabelEnum.CATEGORY, JsonParseFlagEnum.NoParse);
1026 if (childVertex.isRight()) {
1027 log.debug(FAILED_TO_FETCH_FOR_TOSCA_ELEMENT_WITH_ID_ERROR, EdgeLabelEnum.CATEGORY, catalogComponent.getUniqueId(), childVertex.right().value());
1028 return childVertex.right().value();
1030 Vertex subCategoryV = childVertex.left().value();
1031 String subCategoryNormalizedName = (String) subCategoryV.property(GraphPropertyEnum.NORMALIZED_NAME.getProperty()).value();
1032 catalogComponent.setSubCategoryNormalizedName(subCategoryNormalizedName);
1033 subcategory = new SubCategoryDefinition();
1034 subcategory.setUniqueId((String) subCategoryV.property(GraphPropertyEnum.UNIQUE_ID.getProperty()).value());
1035 subcategory.setNormalizedName(subCategoryNormalizedName);
1036 subcategory.setName((String) subCategoryV.property(GraphPropertyEnum.NAME.getProperty()).value());
1037 Either<Vertex, JanusGraphOperationStatus> parentVertex = janusGraphDao.getParentVertex(subCategoryV, EdgeLabelEnum.SUB_CATEGORY, JsonParseFlagEnum.NoParse);
1038 Vertex categoryV = parentVertex.left().value();
1039 String categoryNormalizedName = (String) categoryV.property(GraphPropertyEnum.NORMALIZED_NAME.getProperty()).value();
1040 catalogComponent.setCategoryNormalizedName(categoryNormalizedName);
1041 CategoryDefinition category = new CategoryDefinition();
1042 category.setUniqueId((String) categoryV.property(GraphPropertyEnum.UNIQUE_ID.getProperty()).value());
1043 category.setNormalizedName(categoryNormalizedName);
1044 category.setName((String) categoryV.property(GraphPropertyEnum.NAME.getProperty()).value());
1046 category.addSubCategory(subcategory);
1047 categories.add(category);
1048 catalogComponent.setCategories(categories);
1049 return JanusGraphOperationStatus.OK;
1052 protected JanusGraphOperationStatus setServiceCategoryFromGraphV(Vertex vertex, CatalogComponent catalogComponent) {
1053 List<CategoryDefinition> categories = new ArrayList<>();
1054 Either<Vertex, JanusGraphOperationStatus> childVertex = janusGraphDao.getChildVertex(vertex, EdgeLabelEnum.CATEGORY, JsonParseFlagEnum.NoParse);
1055 if (childVertex.isRight()) {
1056 log.debug(FAILED_TO_FETCH_FOR_TOSCA_ELEMENT_WITH_ID_ERROR, EdgeLabelEnum.CATEGORY, catalogComponent.getUniqueId(), childVertex.right().value());
1057 return childVertex.right().value();
1059 Vertex categoryV = childVertex.left().value();
1060 String categoryNormalizedName = (String) categoryV.property(GraphPropertyEnum.NORMALIZED_NAME.getProperty()).value();
1061 catalogComponent.setCategoryNormalizedName(categoryNormalizedName);
1062 CategoryDefinition category = new CategoryDefinition();
1063 category.setUniqueId((String) categoryV.property(GraphPropertyEnum.UNIQUE_ID.getProperty()).value());
1064 category.setNormalizedName(categoryNormalizedName);
1065 category.setName((String) categoryV.property(GraphPropertyEnum.NAME.getProperty()).value());
1066 category.setUseServiceSubstitutionForNestedServices((Boolean) categoryV.property(GraphPropertyEnum.USE_SUBSTITUTION_FOR_NESTED_SERVICES.getProperty()).orElse(false));
1068 categories.add(category);
1069 catalogComponent.setCategories(categories);
1070 return JanusGraphOperationStatus.OK;
1073 protected JanusGraphOperationStatus setResourceCategoryFromGraph(GraphVertex componentV, ToscaElement toscaElement) {
1074 List<CategoryDefinition> categories = new ArrayList<>();
1075 SubCategoryDefinition subcategory;
1077 Either<GraphVertex, JanusGraphOperationStatus> childVertex = janusGraphDao
1078 .getChildVertex(componentV, EdgeLabelEnum.CATEGORY, JsonParseFlagEnum.NoParse);
1079 if (childVertex.isRight()) {
1080 log.debug(FAILED_TO_FETCH_FOR_TOSCA_ELEMENT_WITH_ID_ERROR, EdgeLabelEnum.CATEGORY, componentV.getUniqueId(), childVertex.right().value());
1081 return childVertex.right().value();
1083 GraphVertex subCategoryV = childVertex.left().value();
1084 Map<GraphPropertyEnum, Object> metadataProperties = subCategoryV.getMetadataProperties();
1085 subcategory = new SubCategoryDefinition();
1086 subcategory.setUniqueId(subCategoryV.getUniqueId());
1087 subcategory.setNormalizedName((String) metadataProperties.get(GraphPropertyEnum.NORMALIZED_NAME));
1088 subcategory.setName((String) metadataProperties.get(GraphPropertyEnum.NAME));
1090 Type listTypeSubcat = new TypeToken<List<String>>() {
1092 List<String> iconsfromJsonSubcat = getGson().fromJson((String) metadataProperties.get(GraphPropertyEnum.ICONS), listTypeSubcat);
1093 subcategory.setIcons(iconsfromJsonSubcat);
1095 Either<GraphVertex, JanusGraphOperationStatus> parentVertex = janusGraphDao
1096 .getParentVertex(subCategoryV, EdgeLabelEnum.SUB_CATEGORY, JsonParseFlagEnum.NoParse);
1097 if (parentVertex.isRight()) {
1098 log.debug("failed to fetch {} for category with id {}, error {}", EdgeLabelEnum.SUB_CATEGORY, subCategoryV.getUniqueId(), parentVertex.right().value());
1099 return childVertex.right().value();
1101 GraphVertex categoryV = parentVertex.left().value();
1102 metadataProperties = categoryV.getMetadataProperties();
1104 CategoryDefinition category = new CategoryDefinition();
1105 category.setUniqueId(categoryV.getUniqueId());
1106 category.setNormalizedName((String) metadataProperties.get(GraphPropertyEnum.NORMALIZED_NAME));
1107 category.setName((String) metadataProperties.get(GraphPropertyEnum.NAME));
1109 Type listTypeCat = new TypeToken<List<String>>() {
1111 List<String> iconsfromJsonCat = getGson().fromJson((String) metadataProperties.get(GraphPropertyEnum.ICONS), listTypeCat);
1112 category.setIcons(iconsfromJsonCat);
1114 category.addSubCategory(subcategory);
1115 categories.add(category);
1116 toscaElement.setCategories(categories);
1118 return JanusGraphOperationStatus.OK;
1121 public <T extends ToscaElement> Either<T, StorageOperationStatus> updateToscaElement(T toscaElementToUpdate, GraphVertex elementV, ComponentParametersView filterResult) {
1122 Either<T, StorageOperationStatus> result = null;
1124 log.debug("In updateToscaElement. received component uid = {}", (toscaElementToUpdate == null ? null : toscaElementToUpdate.getUniqueId()));
1125 if (toscaElementToUpdate == null) {
1126 log.error("Service object is null");
1127 result = Either.right(StorageOperationStatus.BAD_REQUEST);
1131 String modifierUserId = toscaElementToUpdate.getLastUpdaterUserId();
1132 if (modifierUserId == null || modifierUserId.isEmpty()) {
1133 log.error("UserId is missing in the request.");
1134 result = Either.right(StorageOperationStatus.BAD_REQUEST);
1137 Either<GraphVertex, JanusGraphOperationStatus> findUser = findUserVertex(modifierUserId);
1139 if (findUser.isRight()) {
1140 JanusGraphOperationStatus status = findUser.right().value();
1141 log.error(CANNOT_FIND_USER_IN_THE_GRAPH_STATUS_IS, modifierUserId, status);
1145 GraphVertex modifierV = findUser.left().value();
1146 String toscaElementId = toscaElementToUpdate.getUniqueId();
1148 Either<GraphVertex, JanusGraphOperationStatus> parentVertex = janusGraphDao
1149 .getParentVertex(elementV, EdgeLabelEnum.LAST_MODIFIER, JsonParseFlagEnum.NoParse);
1150 if (parentVertex.isRight()) {
1151 log.debug("Failed to fetch last modifier for tosca element with id {} error {}", toscaElementId, parentVertex.right().value());
1152 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(parentVertex.right().value()));
1154 GraphVertex userV = parentVertex.left().value();
1155 String currentModifier = (String) userV.getMetadataProperty(GraphPropertyEnum.USERID);
1157 String prevSystemName = (String) elementV.getMetadataProperty(GraphPropertyEnum.SYSTEM_NAME);
1159 if (currentModifier.equals(modifierUserId)) {
1160 log.debug("Graph LAST MODIFIER edge should not be changed since the modifier is the same as the last modifier.");
1162 log.debug("Going to update the last modifier user of the resource from {} to {}", currentModifier, modifierUserId);
1163 StorageOperationStatus status = moveLastModifierEdge(elementV, modifierV);
1164 log.debug("Finish to update the last modifier user of the resource from {} to {}. status is {}", currentModifier, modifierUserId, status);
1165 if (status != StorageOperationStatus.OK) {
1166 result = Either.right(status);
1171 final long currentTimeMillis = System.currentTimeMillis();
1172 log.debug("Going to update the last Update Date of the resource from {} to {}", elementV.getJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE), currentTimeMillis);
1173 elementV.setJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE, currentTimeMillis);
1175 StorageOperationStatus checkCategories = validateCategories(toscaElementToUpdate, elementV);
1176 if (checkCategories != StorageOperationStatus.OK) {
1177 result = Either.right(checkCategories);
1181 // update all data on vertex
1182 fillToscaElementVertexData(elementV, toscaElementToUpdate, JsonParseFlagEnum.ParseMetadata);
1184 Either<GraphVertex, JanusGraphOperationStatus> updateElement = janusGraphDao.updateVertex(elementV);
1186 if (updateElement.isRight()) {
1187 log.error("Failed to update resource {}. status is {}", toscaElementId, updateElement.right().value());
1188 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(updateElement.right().value()));
1191 GraphVertex updateElementV = updateElement.left().value();
1193 // DE230195 in case resource name changed update TOSCA artifacts
1194 // file names accordingly
1195 String newSystemName = (String) updateElementV.getMetadataProperty(GraphPropertyEnum.SYSTEM_NAME);
1196 if (newSystemName != null && !newSystemName.equals(prevSystemName)) {
1197 Either<Map<String, ArtifactDataDefinition>, JanusGraphOperationStatus> resultToscaArt = getDataFromGraph(updateElementV, EdgeLabelEnum.TOSCA_ARTIFACTS);
1198 if (resultToscaArt.isRight()) {
1199 log.debug("Failed to get tosca artifact from graph for tosca element {} error {}", toscaElementId, resultToscaArt.right().value());
1200 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(resultToscaArt.right().value()));
1203 Map<String, ArtifactDataDefinition> toscaArtifacts = resultToscaArt.left().value();
1204 if (toscaArtifacts != null) {
1205 for (Entry<String, ArtifactDataDefinition> artifact : toscaArtifacts.entrySet()) {
1206 generateNewToscaFileName(toscaElementToUpdate.getComponentType().getValue().toLowerCase(), newSystemName, artifact.getValue());
1208 // TODO call to new Artifact operation in order to update list of artifacts
1213 if (toscaElementToUpdate.getComponentType() == ComponentTypeEnum.RESOURCE) {
1214 StorageOperationStatus resultDerived = updateDerived(toscaElementToUpdate, updateElementV);
1215 if (resultDerived != StorageOperationStatus.OK) {
1216 log.debug("Failed to update from derived data for element {} error {}", toscaElementId, resultDerived);
1217 return Either.right(resultDerived);
1221 Either<T, StorageOperationStatus> updatedResource = getToscaElement(updateElementV, filterResult);
1222 if (updatedResource.isRight()) {
1223 log.error("Failed to fetch tosca element {} after update , error {}", toscaElementId, updatedResource.right().value());
1224 result = Either.right(StorageOperationStatus.BAD_REQUEST);
1228 T updatedResourceValue = updatedResource.left().value();
1229 result = Either.left(updatedResourceValue);
1234 protected StorageOperationStatus moveLastModifierEdge(GraphVertex elementV, GraphVertex modifierV) {
1235 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(
1236 janusGraphDao.moveEdge(elementV, modifierV, EdgeLabelEnum.LAST_MODIFIER, Direction.IN));
1239 protected StorageOperationStatus moveCategoryEdge(GraphVertex elementV, GraphVertex categoryV) {
1240 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(
1241 janusGraphDao.moveEdge(elementV, categoryV, EdgeLabelEnum.CATEGORY, Direction.OUT));
1244 private void generateNewToscaFileName(String componentType, String componentName, ArtifactDataDefinition artifactInfo) {
1245 Map<String, Object> getConfig = (Map<String, Object>) ConfigurationManager.getConfigurationManager().getConfiguration().getToscaArtifacts().entrySet().stream().filter(p -> p.getKey().equalsIgnoreCase(artifactInfo.getArtifactLabel()))
1246 .findAny().get().getValue();
1247 artifactInfo.setArtifactName(componentType + "-" + componentName + getConfig.get("artifactName"));
1250 protected <T extends ToscaElement> StorageOperationStatus validateResourceCategory(T toscaElementToUpdate, GraphVertex elementV) {
1251 StorageOperationStatus status = StorageOperationStatus.OK;
1252 List<CategoryDefinition> newCategoryList = toscaElementToUpdate.getCategories();
1253 CategoryDefinition newCategory = newCategoryList.get(0);
1255 Either<GraphVertex, JanusGraphOperationStatus> childVertex = janusGraphDao
1256 .getChildVertex(elementV, EdgeLabelEnum.CATEGORY, JsonParseFlagEnum.NoParse);
1257 if (childVertex.isRight()) {
1258 log.debug(FAILED_TO_FETCH_FOR_TOSCA_ELEMENT_WITH_ID_ERROR, EdgeLabelEnum.CATEGORY, elementV.getUniqueId(), childVertex.right().value());
1259 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(childVertex.right().value());
1261 GraphVertex subCategoryV = childVertex.left().value();
1262 Map<GraphPropertyEnum, Object> metadataProperties = subCategoryV.getMetadataProperties();
1263 String subCategoryNameCurrent = (String) metadataProperties.get(GraphPropertyEnum.NAME);
1265 Either<GraphVertex, JanusGraphOperationStatus> parentVertex = janusGraphDao
1266 .getParentVertex(subCategoryV, EdgeLabelEnum.SUB_CATEGORY, JsonParseFlagEnum.NoParse);
1267 if (parentVertex.isRight()) {
1268 log.debug("failed to fetch {} for category with id {}, error {}", EdgeLabelEnum.SUB_CATEGORY, subCategoryV.getUniqueId(), parentVertex.right().value());
1269 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(childVertex.right().value());
1271 GraphVertex categoryV = parentVertex.left().value();
1272 metadataProperties = categoryV.getMetadataProperties();
1273 String categoryNameCurrent = (String) metadataProperties.get(GraphPropertyEnum.NAME);
1275 boolean categoryWasChanged = false;
1277 String newCategoryName = newCategory.getName();
1278 SubCategoryDefinition newSubcategory = newCategory.getSubcategories().get(0);
1279 String newSubCategoryName = newSubcategory.getName();
1280 if (newCategoryName != null && !newCategoryName.equals(categoryNameCurrent)) {
1281 // the category was changed
1282 categoryWasChanged = true;
1284 // the sub-category was changed
1285 if (newSubCategoryName != null && !newSubCategoryName.equals(subCategoryNameCurrent)) {
1286 log.debug("Going to update the category of the resource from {} to {}", categoryNameCurrent, newCategory);
1287 categoryWasChanged = true;
1290 if (categoryWasChanged) {
1291 Either<GraphVertex, StorageOperationStatus> getCategoryVertex = getResourceCategoryVertex(elementV.getUniqueId(), newSubCategoryName, newCategoryName);
1293 if (getCategoryVertex.isRight()) {
1294 return getCategoryVertex.right().value();
1296 GraphVertex newCategoryV = getCategoryVertex.left().value();
1297 status = moveCategoryEdge(elementV, newCategoryV);
1298 log.debug("Going to update the category of the resource from {} to {}. status is {}", categoryNameCurrent, newCategory, status);
1303 public <T extends ToscaElement> Either<List<T>, StorageOperationStatus> getElementCatalogData(ComponentTypeEnum componentType, List<ResourceTypeEnum> excludeTypes, boolean isHighestVersions) {
1304 Either<List<GraphVertex>, JanusGraphOperationStatus> listOfComponents;
1305 if (isHighestVersions) {
1306 listOfComponents = getListOfHighestComponents(componentType, excludeTypes, JsonParseFlagEnum.NoParse);
1308 listOfComponents = getListOfHighestAndAllCertifiedComponents(componentType, excludeTypes);
1311 if (listOfComponents.isRight() && listOfComponents.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
1312 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(listOfComponents.right().value()));
1314 List<T> result = new ArrayList<>();
1315 if (listOfComponents.isLeft()) {
1316 List<GraphVertex> highestAndAllCertified = listOfComponents.left().value();
1317 if (highestAndAllCertified != null && !highestAndAllCertified.isEmpty()) {
1318 for (GraphVertex vertexComponent : highestAndAllCertified) {
1319 Either<T, StorageOperationStatus> component = getLightComponent(vertexComponent, componentType, new ComponentParametersView(true));
1320 if (component.isRight()) {
1321 log.debug("Failed to fetch light element for {} error {}", vertexComponent.getUniqueId(), component.right().value());
1322 return Either.right(component.right().value());
1324 result.add(component.left().value());
1329 return Either.left(result);
1332 public Either<List<CatalogComponent>, StorageOperationStatus> getElementCatalogData(boolean isCatalog, List<ResourceTypeEnum> excludeTypes) {
1333 StopWatch stopWatch = new StopWatch();
1336 Map<String, CatalogComponent> existInCatalog = new HashMap<>();
1337 Either<Iterator<Vertex>, JanusGraphOperationStatus> verticesEither = janusGraphDao.getCatalogOrArchiveVerticies(isCatalog);
1338 if (verticesEither.isRight()) {
1339 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(verticesEither.right().value()));
1341 Iterator<Vertex> vertices = verticesEither.left().value();
1342 while (vertices.hasNext()) {
1343 handleCatalogComponent(existInCatalog, vertices.next(), excludeTypes);
1346 String timeToFetchElements = stopWatch.prettyPrint();
1347 log.info("time to fetch all catalog elements: {}", timeToFetchElements);
1348 return Either.left(existInCatalog.values().stream().collect(Collectors.toList()));
1351 private void handleCatalogComponent(Map<String, CatalogComponent> existInCatalog, Vertex vertex, List<ResourceTypeEnum> excludeTypes) {
1352 VertexProperty<Object> property = vertex.property(GraphPropertiesDictionary.METADATA.getProperty());
1353 String json = (String) property.value();
1354 Map<String, Object> metadatObj = JsonParserUtils.toMap(json);
1356 String uniqueId = (String) metadatObj.get(JsonPresentationFields.UNIQUE_ID.getPresentation());
1357 Boolean isDeleted = (Boolean) metadatObj.get(JsonPresentationFields.IS_DELETED.getPresentation());
1360 if (isAddToCatalog(excludeTypes, metadatObj) && (existInCatalog.get(uniqueId) == null && (isDeleted == null || !isDeleted.booleanValue()))) {
1361 CatalogComponent catalogComponent = new CatalogComponent();
1362 catalogComponent.setUniqueId(uniqueId);
1364 catalogComponent.setComponentType(ComponentTypeEnum.valueOf((String) metadatObj.get(JsonPresentationFields.COMPONENT_TYPE.getPresentation())));
1365 catalogComponent.setVersion((String) metadatObj.get(JsonPresentationFields.VERSION.getPresentation()));
1366 catalogComponent.setName((String) metadatObj.get(JsonPresentationFields.NAME.getPresentation()));
1367 catalogComponent.setIcon((String) metadatObj.get(JsonPresentationFields.ICON.getPresentation()));
1368 catalogComponent.setLifecycleState((String) metadatObj.get(JsonPresentationFields.LIFECYCLE_STATE.getPresentation()));
1369 Object lastUpdateDate = metadatObj.get(JsonPresentationFields.LAST_UPDATE_DATE.getPresentation());
1370 catalogComponent.setLastUpdateDate( (lastUpdateDate != null ? (Long)lastUpdateDate : 0L));
1371 catalogComponent.setDistributionStatus((String) metadatObj.get(JsonPresentationFields.DISTRIBUTION_STATUS.getPresentation()));
1372 catalogComponent.setDescription((String) metadatObj.get(JsonPresentationFields.DESCRIPTION.getPresentation()));
1373 catalogComponent.setSystemName((String) metadatObj.get(JsonPresentationFields.SYSTEM_NAME.getPresentation()));
1374 catalogComponent.setUuid((String) metadatObj.get(JsonPresentationFields.UUID.getPresentation()));
1375 catalogComponent.setInvariantUUID((String) metadatObj.get(JsonPresentationFields.INVARIANT_UUID.getPresentation()));
1376 catalogComponent.setIsHighestVersion((Boolean) metadatObj.get(JsonPresentationFields.HIGHEST_VERSION.getPresentation()));
1377 Iterator<Edge> edges = vertex.edges(Direction.IN, EdgeLabelEnum.STATE.name());
1378 if(edges.hasNext()){
1379 catalogComponent.setLastUpdaterUserId((String) edges.next().outVertex().property(GraphPropertiesDictionary.USERID.getProperty()).value());
1381 Object resourceType = metadatObj.get(JsonPresentationFields.RESOURCE_TYPE.getPresentation());
1382 if (resourceType != null) {
1383 catalogComponent.setResourceType((String) resourceType);
1386 if (catalogComponent.getComponentType() == ComponentTypeEnum.SERVICE) {
1387 setServiceCategoryFromGraphV(vertex, catalogComponent);
1390 setResourceCategoryFromGraphV(vertex, catalogComponent);
1392 List<String> tags = (List<String>) metadatObj.get(JsonPresentationFields.TAGS.getPresentation());
1394 catalogComponent.setTags(tags);
1396 existInCatalog.put(uniqueId, catalogComponent);
1400 private boolean isAddToCatalog(List<ResourceTypeEnum> excludeTypes, Map<String, Object> metadatObj) {
1401 boolean isAddToCatalog = true;
1402 Object resourceTypeStr = metadatObj.get(JsonPresentationFields.RESOURCE_TYPE.getPresentation());
1403 if (resourceTypeStr != null) {
1404 ResourceTypeEnum resourceType = ResourceTypeEnum.getType((String) resourceTypeStr);
1405 if (!CollectionUtils.isEmpty(excludeTypes)) {
1406 Optional<ResourceTypeEnum> op = excludeTypes.stream().filter(rt -> rt == resourceType).findAny();
1407 if (op.isPresent()) {
1408 isAddToCatalog = false;
1412 return isAddToCatalog;
1415 public Either<List<GraphVertex>, JanusGraphOperationStatus> getListOfHighestComponents(ComponentTypeEnum
1416 componentType, List<ResourceTypeEnum> excludeTypes, JsonParseFlagEnum parseFlag) {
1417 Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
1418 Map<GraphPropertyEnum, Object> propertiesHasNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
1419 propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
1420 propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
1422 if (componentType == ComponentTypeEnum.RESOURCE) {
1423 propertiesToMatch.put(GraphPropertyEnum.IS_ABSTRACT, false);
1424 propertiesHasNotToMatch.put(GraphPropertyEnum.RESOURCE_TYPE, excludeTypes);
1426 propertiesHasNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
1427 propertiesHasNotToMatch.put(GraphPropertyEnum.IS_ARCHIVED, true); //US382674, US382683
1429 return janusGraphDao
1430 .getByCriteria(null, propertiesToMatch, propertiesHasNotToMatch, parseFlag);
1433 // highest + (certified && !highest)
1434 public Either<List<GraphVertex>, JanusGraphOperationStatus> getListOfHighestAndAllCertifiedComponents
1435 (ComponentTypeEnum componentType, List<ResourceTypeEnum> excludeTypes) {
1436 long startFetchAllStates = System.currentTimeMillis();
1437 Either<List<GraphVertex>, JanusGraphOperationStatus> highestNodes = getListOfHighestComponents(componentType, excludeTypes, JsonParseFlagEnum.ParseMetadata);
1439 Map<GraphPropertyEnum, Object> propertiesToMatchCertified = new EnumMap<>(GraphPropertyEnum.class);
1440 Map<GraphPropertyEnum, Object> propertiesHasNotToMatchCertified = new EnumMap<>(GraphPropertyEnum.class);
1441 propertiesToMatchCertified.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
1442 propertiesToMatchCertified.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
1443 if (componentType == ComponentTypeEnum.RESOURCE) {
1444 propertiesToMatchCertified.put(GraphPropertyEnum.IS_ABSTRACT, false);
1445 propertiesHasNotToMatchCertified.put(GraphPropertyEnum.RESOURCE_TYPE, excludeTypes);
1448 propertiesHasNotToMatchCertified.put(GraphPropertyEnum.IS_DELETED, true);
1449 propertiesHasNotToMatchCertified.put(GraphPropertyEnum.IS_ARCHIVED, true); //US382674, US382683
1450 propertiesHasNotToMatchCertified.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
1452 Either<List<GraphVertex>, JanusGraphOperationStatus> certifiedNotHighestNodes = janusGraphDao
1453 .getByCriteria(null, propertiesToMatchCertified, propertiesHasNotToMatchCertified, JsonParseFlagEnum.ParseMetadata);
1454 if (certifiedNotHighestNodes.isRight() && certifiedNotHighestNodes.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
1455 return Either.right(certifiedNotHighestNodes.right().value());
1458 long endFetchAllStates = System.currentTimeMillis();
1460 List<GraphVertex> allNodes = new ArrayList<>();
1462 if (certifiedNotHighestNodes.isLeft()) {
1463 allNodes.addAll(certifiedNotHighestNodes.left().value());
1465 if (highestNodes.isLeft()) {
1466 allNodes.addAll(highestNodes.left().value());
1469 log.debug("Fetch catalog {}s all states from graph took {} ms", componentType, endFetchAllStates - startFetchAllStates);
1470 return Either.left(allNodes);
1473 protected Either<List<GraphVertex>, StorageOperationStatus> getAllComponentsMarkedForDeletion(ComponentTypeEnum
1476 // get all components marked for delete
1477 Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
1478 props.put(GraphPropertyEnum.IS_DELETED, true);
1479 props.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
1481 Either<List<GraphVertex>, JanusGraphOperationStatus> componentsToDelete = janusGraphDao
1482 .getByCriteria(null, props, JsonParseFlagEnum.NoParse);
1484 if (componentsToDelete.isRight()) {
1485 JanusGraphOperationStatus error = componentsToDelete.right().value();
1486 if (error.equals(JanusGraphOperationStatus.NOT_FOUND)) {
1487 log.trace("no components to delete");
1488 return Either.left(new ArrayList<>());
1490 log.info("failed to find components to delete. error : {}", error.name());
1491 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(error));
1494 return Either.left(componentsToDelete.left().value());
1497 protected JanusGraphOperationStatus setAdditionalInformationFromGraph(GraphVertex componentV, ToscaElement
1499 Either<Map<String, AdditionalInfoParameterDataDefinition>, JanusGraphOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.ADDITIONAL_INFORMATION);
1500 if (result.isLeft()) {
1501 toscaElement.setAdditionalInformation(result.left().value());
1503 if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
1504 return result.right().value();
1507 return JanusGraphOperationStatus.OK;
1510 // --------------------------------------------
1511 public abstract <T extends
1512 ToscaElement> Either<T, StorageOperationStatus> getToscaElement(String uniqueId, ComponentParametersView componentParametersView);
1514 public abstract <T extends
1515 ToscaElement> Either<T, StorageOperationStatus> getToscaElement(GraphVertex toscaElementVertex, ComponentParametersView componentParametersView);
1517 public abstract <T extends
1518 ToscaElement> Either<T, StorageOperationStatus> deleteToscaElement(GraphVertex toscaElementVertex);
1520 public abstract <T extends
1521 ToscaElement> Either<T, StorageOperationStatus> createToscaElement(ToscaElement toscaElement);
1523 protected abstract <T extends ToscaElement> JanusGraphOperationStatus
1524 setCategoriesFromGraph(GraphVertex vertexComponent, T toscaElement);
1526 protected abstract <T extends ToscaElement> JanusGraphOperationStatus
1527 setCapabilitiesFromGraph(GraphVertex componentV, T toscaElement);
1529 protected abstract <T extends ToscaElement> JanusGraphOperationStatus
1530 setRequirementsFromGraph(GraphVertex componentV, T toscaElement);
1532 protected abstract <T extends ToscaElement> StorageOperationStatus
1533 validateCategories(T toscaElementToUpdate, GraphVertex elementV);
1535 protected abstract <T extends ToscaElement> StorageOperationStatus
1536 updateDerived(T toscaElementToUpdate, GraphVertex updateElementV);
1538 public abstract <T extends ToscaElement> void fillToscaElementVertexData(GraphVertex elementV, T
1539 toscaElementToUpdate, JsonParseFlagEnum flag);