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=========================================================
20 package org.openecomp.sdc.be.model.jsonjanusgraph.operations;
22 import static org.openecomp.sdc.be.utils.TypeUtils.setField;
24 import com.google.gson.Gson;
25 import com.google.gson.reflect.TypeToken;
26 import fj.data.Either;
27 import java.lang.reflect.Type;
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.EnumMap;
31 import java.util.HashMap;
32 import java.util.HashSet;
33 import java.util.Iterator;
34 import java.util.List;
36 import java.util.Map.Entry;
37 import java.util.Optional;
39 import java.util.stream.Collectors;
40 import org.apache.commons.collections4.MapUtils;
41 import org.apache.commons.lang3.StringUtils;
42 import org.apache.tinkerpop.gremlin.structure.Direction;
43 import org.apache.tinkerpop.gremlin.structure.Edge;
44 import org.apache.tinkerpop.gremlin.structure.Vertex;
45 import org.apache.tinkerpop.gremlin.structure.VertexProperty;
46 import org.apache.tinkerpop.gremlin.structure.util.empty.EmptyVertexProperty;
47 import org.janusgraph.core.JanusGraphVertex;
48 import org.openecomp.sdc.be.config.ConfigurationManager;
49 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
50 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
51 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
52 import org.openecomp.sdc.be.dao.jsongraph.types.EdgePropertyEnum;
53 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
54 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
55 import org.openecomp.sdc.be.dao.jsongraph.utils.JsonParserUtils;
56 import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary;
57 import org.openecomp.sdc.be.datatypes.category.MetadataKeyDataDefinition;
58 import org.openecomp.sdc.be.datatypes.elements.AdditionalInfoParameterDataDefinition;
59 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
60 import org.openecomp.sdc.be.datatypes.elements.DataTypeDataDefinition;
61 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
62 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
63 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
64 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
65 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
66 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
67 import org.openecomp.sdc.be.model.ComponentParametersView;
68 import org.openecomp.sdc.be.model.DataTypeDefinition;
69 import org.openecomp.sdc.be.model.LifecycleStateEnum;
70 import org.openecomp.sdc.be.model.catalog.CatalogComponent;
71 import org.openecomp.sdc.be.model.category.CategoryDefinition;
72 import org.openecomp.sdc.be.model.category.SubCategoryDefinition;
73 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.NodeType;
74 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.TopologyTemplate;
75 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElement;
76 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElementTypeEnum;
77 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.exception.ModelOperationExceptionSupplier;
78 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
79 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
80 import org.openecomp.sdc.be.model.operations.impl.ModelOperation;
81 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
82 import org.openecomp.sdc.be.utils.TypeUtils;
83 import org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum;
84 import org.openecomp.sdc.common.jsongraph.util.CommonUtility;
85 import org.openecomp.sdc.common.jsongraph.util.CommonUtility.LogLevelEnum;
86 import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode;
87 import org.openecomp.sdc.common.log.wrappers.Logger;
88 import org.openecomp.sdc.common.util.ValidationUtils;
89 import org.springframework.beans.factory.annotation.Autowired;
90 import org.springframework.util.StopWatch;
92 public abstract class ToscaElementOperation extends BaseOperation {
94 private static final String FAILED_TO_FETCH_FOR_TOSCA_ELEMENT_WITH_ID_ERROR = "failed to fetch {} for tosca element with id {}, error {}";
95 private static final String CANNOT_FIND_USER_IN_THE_GRAPH_STATUS_IS = "Cannot find user {} in the graph. status is {}";
96 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 {}. ";
97 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 {}. ";
98 private static final Gson gson = new Gson();
99 private static Logger log = Logger.getLogger(ToscaElementOperation.class.getName());
101 protected CategoryOperation categoryOperation;
103 protected ModelOperation modelOperation;
105 public static DataTypeDefinition createDataType(final String dataTypeName) {
106 final DataTypeDefinition dataType = new DataTypeDefinition();
107 dataType.setName(dataTypeName);
111 public static DataTypeDefinition createDataTypeDefinitionWithName(final Entry<String, Object> attributeNameValue) {
112 final Map<String, Object> attributeMap = (Map<String, Object>) attributeNameValue.getValue();
113 final DataTypeDefinition dataType = createDataType(attributeNameValue.getKey());
114 setField(attributeMap, TypeUtils.ToscaTagNamesEnum.DESCRIPTION, dataType::setDescription);
115 setField(attributeMap, TypeUtils.ToscaTagNamesEnum.DERIVED_FROM_NAME, dataType::setDerivedFromName);
116 // TODO - find the way to set the properties
118 // CommonImportManager.setProperties(attributeMap, dataType::setProperties);
119 final Object derivedFrom = attributeMap.get(JsonPresentationFields.DERIVED_FROM.getPresentation());
120 if (derivedFrom instanceof Map) {
121 final Map<String, Object> derivedFromMap = (Map<String, Object>) derivedFrom;
122 final DataTypeDefinition parentDataTypeDataDefinition = new DataTypeDefinition();
123 parentDataTypeDataDefinition.setName((String) derivedFromMap.get(JsonPresentationFields.NAME.getPresentation()));
124 parentDataTypeDataDefinition.setUniqueId((String) derivedFromMap.get(JsonPresentationFields.UNIQUE_ID.getPresentation()));
125 parentDataTypeDataDefinition.setCreationTime((Long) derivedFromMap.get(JsonPresentationFields.CREATION_TIME.getPresentation()));
126 parentDataTypeDataDefinition.setModificationTime((Long) derivedFromMap.get(JsonPresentationFields.MODIFICATION_TIME.getPresentation()));
127 dataType.setDerivedFrom(parentDataTypeDataDefinition);
132 protected Gson getGson() {
136 protected Either<GraphVertex, StorageOperationStatus> getComponentByLabelAndId(String uniqueId, ToscaElementTypeEnum nodeType,
137 JsonParseFlagEnum parseFlag) {
138 Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
139 propertiesToMatch.put(GraphPropertyEnum.UNIQUE_ID, uniqueId);
140 VertexTypeEnum vertexType = ToscaElementTypeEnum.getVertexTypeByToscaType(nodeType);
141 Either<List<GraphVertex>, JanusGraphOperationStatus> getResponse = janusGraphDao.getByCriteria(vertexType, propertiesToMatch, parseFlag);
142 if (getResponse.isRight()) {
143 log.debug("Couldn't fetch component with type {} and unique id {}, error: {}", vertexType, uniqueId, getResponse.right().value());
144 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getResponse.right().value()));
146 List<GraphVertex> componentList = getResponse.left().value();
147 if (componentList.isEmpty()) {
148 log.debug("Component with type {} and unique id {} was not found", vertexType, uniqueId);
149 return Either.right(StorageOperationStatus.NOT_FOUND);
151 GraphVertex vertexG = componentList.get(0);
152 return Either.left(vertexG);
155 protected GraphVertex getHighestVersionFrom(GraphVertex v) {
156 Either<GraphVertex, JanusGraphOperationStatus> childVertexE = janusGraphDao
157 .getChildVertex(v, EdgeLabelEnum.VERSION, JsonParseFlagEnum.NoParse);
158 GraphVertex highestVersionVertex = v;
159 while (childVertexE.isLeft()) {
160 highestVersionVertex = childVertexE.left().value();
161 childVertexE = janusGraphDao.getChildVertex(highestVersionVertex, EdgeLabelEnum.VERSION, JsonParseFlagEnum.NoParse);
163 return highestVersionVertex;
166 public Either<ToscaElement, StorageOperationStatus> getToscaElement(String uniqueId) {
167 return getToscaElement(uniqueId, new ComponentParametersView());
170 public Either<GraphVertex, StorageOperationStatus> markComponentToDelete(GraphVertex componentToDelete) {
171 Boolean isDeleted = (Boolean) componentToDelete.getMetadataProperty(GraphPropertyEnum.IS_DELETED);
172 if (isDeleted != null && isDeleted && !(Boolean) componentToDelete.getMetadataProperty(GraphPropertyEnum.IS_HIGHEST_VERSION)) {
173 // component already marked for delete
175 componentToDelete.addMetadataProperty(GraphPropertyEnum.IS_DELETED, Boolean.TRUE);
176 componentToDelete.setJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE, System.currentTimeMillis());
177 Either<GraphVertex, JanusGraphOperationStatus> updateNode = janusGraphDao.updateVertex(componentToDelete);
178 StorageOperationStatus updateComponent;
179 if (updateNode.isRight()) {
180 log.debug("Failed to update component {}. status is {}", componentToDelete.getUniqueId(), updateNode.right().value());
181 updateComponent = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(updateNode.right().value());
182 return Either.right(updateComponent);
185 return Either.left(componentToDelete);
189 * Performs a shadow clone of previousToscaElement
191 * @param previousToscaElement
192 * @param nextToscaElement
196 public Either<GraphVertex, StorageOperationStatus> cloneToscaElement(GraphVertex previousToscaElement, GraphVertex nextToscaElement,
198 Either<GraphVertex, StorageOperationStatus> result = null;
199 GraphVertex createdToscaElementVertex = null;
200 JanusGraphOperationStatus status;
201 Either<GraphVertex, JanusGraphOperationStatus> createNextVersionRes = janusGraphDao.createVertex(nextToscaElement);
202 if (createNextVersionRes.isRight()) {
203 status = createNextVersionRes.right().value();
204 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to create tosca element vertex {} with version {} on graph. Status is {}. ",
205 previousToscaElement.getMetadataProperty(GraphPropertyEnum.NORMALIZED_NAME),
206 previousToscaElement.getMetadataProperty(GraphPropertyEnum.VERSION), status);
207 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
209 if (result == null) {
210 createdToscaElementVertex = createNextVersionRes.left().value();
211 final Map<EdgePropertyEnum, Object> properties = new EnumMap<>(EdgePropertyEnum.class);
212 properties.put(EdgePropertyEnum.STATE, createdToscaElementVertex.getMetadataProperty(GraphPropertyEnum.STATE));
213 status = janusGraphDao.createEdge(user.getVertex(), createdToscaElementVertex.getVertex(), EdgeLabelEnum.STATE, properties);
214 if (status != JanusGraphOperationStatus.OK) {
215 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG,
216 FAILED_TO_CREATE_EDGE_WITH_LABEL_FROM_USER_VERTEX_TO_TOSCA_ELEMENT_VERTEX_ON_GRAPH_STATUS_IS, EdgeLabelEnum.STATE,
217 user.getUniqueId(), previousToscaElement.getMetadataProperty(GraphPropertyEnum.NORMALIZED_NAME), status);
218 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
221 if (result == null) {
223 VertexTypeEnum.TOPOLOGY_TEMPLATE.equals(previousToscaElement.getLabel())
224 ? createModelEdge(previousToscaElement, nextToscaElement, user, createdToscaElementVertex, EdgeLabelEnum.MODEL)
225 : createModelEdge(previousToscaElement, nextToscaElement, user, createdToscaElementVertex, EdgeLabelEnum.MODEL_ELEMENT);
227 if (result == null) {
228 status = janusGraphDao.createEdge(user.getVertex(), createdToscaElementVertex.getVertex(), EdgeLabelEnum.LAST_MODIFIER, new HashMap<>());
229 if (status != JanusGraphOperationStatus.OK) {
230 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG,
231 FAILED_TO_CREATE_EDGE_WITH_LABEL_FROM_USER_VERTEX_TO_TOSCA_ELEMENT_VERTEX_ON_GRAPH_STATUS_IS, EdgeLabelEnum.LAST_MODIFIER,
232 user.getUniqueId(), nextToscaElement.getMetadataProperty(GraphPropertyEnum.NORMALIZED_NAME), status);
233 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
236 if (result == null) {
237 Either<GraphVertex, JanusGraphOperationStatus> creatorVertexRes = janusGraphDao
238 .getParentVertex(previousToscaElement, EdgeLabelEnum.CREATOR, JsonParseFlagEnum.NoParse);
239 if (creatorVertexRes.isRight()) {
240 status = creatorVertexRes.right().value();
241 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_GET_CREATOR_VERTEX_OF_TOSCA_ELEMENT_VERTEX_ON_GRAPH_STATUS_IS,
242 EdgeLabelEnum.CREATOR, nextToscaElement.getMetadataProperty(GraphPropertyEnum.NORMALIZED_NAME), status);
243 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
245 status = janusGraphDao
246 .createEdge(creatorVertexRes.left().value().getVertex(), createdToscaElementVertex.getVertex(), EdgeLabelEnum.CREATOR,
248 if (status != JanusGraphOperationStatus.OK) {
249 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG,
250 FAILED_TO_CREATE_EDGE_WITH_LABEL_FROM_USER_VERTEX_TO_TOSCA_ELEMENT_VERTEX_ON_GRAPH_STATUS_IS, EdgeLabelEnum.CREATOR,
251 user.getUniqueId(), nextToscaElement.getMetadataProperty(GraphPropertyEnum.NORMALIZED_NAME), status);
252 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
255 if (result == null) {
256 Iterator<Edge> edgesToCopyIter = previousToscaElement.getVertex().edges(Direction.OUT);
257 while (edgesToCopyIter.hasNext()) {
258 Edge currEdge = edgesToCopyIter.next();
259 Vertex currVertex = currEdge.inVertex();
260 status = janusGraphDao
261 .createEdge(createdToscaElementVertex.getVertex(), currVertex, EdgeLabelEnum.getEdgeLabelEnum(currEdge.label()), currEdge);
262 if (status != JanusGraphOperationStatus.OK) {
263 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG,
264 "Failed to create edge with label {} from tosca element vertex {} to vertex with label {} on graph. Status is {}. ",
265 currEdge.label(), createdToscaElementVertex.getUniqueId(), currVertex.property(GraphPropertyEnum.LABEL.getProperty()),
267 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
272 if (result == null) {
273 result = Either.left(createdToscaElementVertex);
276 .addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to clone tosca element {} with the name {}. ", previousToscaElement.getUniqueId(),
277 previousToscaElement.getMetadataProperty(GraphPropertyEnum.NORMALIZED_NAME));
283 * Creates the MODEL in case it exits on the previous version
285 * @param previousToscaElement previous element version
286 * @param nextToscaElement latest element version
288 * @param createdToscaElementVertex created tosca element
289 * @param edgeLabelEnum
292 private Either<GraphVertex, StorageOperationStatus> createModelEdge(final GraphVertex previousToscaElement,
293 final GraphVertex nextToscaElement, GraphVertex user,
294 final GraphVertex createdToscaElementVertex,
295 final EdgeLabelEnum edgeLabelEnum) {
296 Either<GraphVertex, StorageOperationStatus> result = null;
297 final Either<GraphVertex, JanusGraphOperationStatus> modelElementVertexResponse = janusGraphDao
298 .getParentVertex(previousToscaElement, edgeLabelEnum, JsonParseFlagEnum.NoParse);
299 if (modelElementVertexResponse.isLeft()) {
300 final JanusGraphOperationStatus status = janusGraphDao
301 .createEdge(modelElementVertexResponse.left().value().getVertex(), createdToscaElementVertex.getVertex(), edgeLabelEnum,
303 if (JanusGraphOperationStatus.OK != status) {
304 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG,
305 FAILED_TO_CREATE_EDGE_WITH_LABEL_FROM_USER_VERTEX_TO_TOSCA_ELEMENT_VERTEX_ON_GRAPH_STATUS_IS, edgeLabelEnum,
306 user.getUniqueId(), nextToscaElement.getMetadataProperty(GraphPropertyEnum.NORMALIZED_NAME), status);
307 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
313 protected JanusGraphOperationStatus setLastModifierFromGraph(GraphVertex componentV, ToscaElement toscaElement) {
314 Either<GraphVertex, JanusGraphOperationStatus> parentVertex = janusGraphDao
315 .getParentVertex(componentV, EdgeLabelEnum.LAST_MODIFIER, JsonParseFlagEnum.NoParse);
316 if (parentVertex.isRight()) {
317 log.debug("Failed to fetch last modifier for tosca element with id {} error {}", componentV.getUniqueId(), parentVertex.right().value());
318 return parentVertex.right().value();
320 GraphVertex userV = parentVertex.left().value();
321 String userId = (String) userV.getMetadataProperty(GraphPropertyEnum.USERID);
322 toscaElement.setLastUpdaterUserId(userId);
323 toscaElement.setLastUpdaterFullName(buildFullName(userV));
324 return JanusGraphOperationStatus.OK;
327 public String buildFullName(GraphVertex userV) {
328 String fullName = (String) userV.getMetadataProperty(GraphPropertyEnum.FIRST_NAME);
329 if (fullName == null) {
332 fullName = fullName + " ";
334 String lastName = (String) userV.getMetadataProperty(GraphPropertyEnum.LAST_NAME);
335 if (lastName != null) {
336 fullName += lastName;
341 protected JanusGraphOperationStatus setCreatorFromGraph(GraphVertex componentV, ToscaElement toscaElement) {
342 Either<GraphVertex, JanusGraphOperationStatus> parentVertex = janusGraphDao
343 .getParentVertex(componentV, EdgeLabelEnum.CREATOR, JsonParseFlagEnum.NoParse);
344 if (parentVertex.isRight()) {
345 log.debug("Failed to fetch creator for tosca element with id {} error {}", componentV.getUniqueId(), parentVertex.right().value());
346 return parentVertex.right().value();
348 GraphVertex userV = parentVertex.left().value();
349 String creatorUserId = (String) userV.getMetadataProperty(GraphPropertyEnum.USERID);
350 toscaElement.setCreatorUserId(creatorUserId);
351 toscaElement.setCreatorFullName(buildFullName(userV));
352 return JanusGraphOperationStatus.OK;
355 protected <T extends ToscaElement> T getResourceMetaDataFromResource(T toscaElement) {
356 if (toscaElement.getNormalizedName() == null || toscaElement.getNormalizedName().isEmpty()) {
357 toscaElement.setNormalizedName(ValidationUtils.normaliseComponentName(toscaElement.getName()));
359 if (toscaElement.getSystemName() == null || toscaElement.getSystemName().isEmpty()) {
360 toscaElement.setSystemName(ValidationUtils.convertToSystemName(toscaElement.getName()));
362 LifecycleStateEnum lifecycleStateEnum = toscaElement.getLifecycleState();
363 if (lifecycleStateEnum == null) {
364 toscaElement.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
366 long currentDate = System.currentTimeMillis();
367 if (toscaElement.getCreationDate() == null) {
368 toscaElement.setCreationDate(currentDate);
370 toscaElement.setLastUpdateDate(currentDate);
374 protected void fillCommonMetadata(GraphVertex nodeTypeVertex, ToscaElement toscaElement) {
375 if (toscaElement.isHighestVersion() == null) {
376 toscaElement.setHighestVersion(true);
378 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.IS_DELETED, toscaElement.getMetadataValue(JsonPresentationFields.IS_DELETED));
379 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.IS_HIGHEST_VERSION,
380 toscaElement.getMetadataValueOrDefault(JsonPresentationFields.HIGHEST_VERSION, Boolean.TRUE));
381 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.STATE, toscaElement.getMetadataValue(JsonPresentationFields.LIFECYCLE_STATE));
382 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.RESOURCE_TYPE, toscaElement.getMetadataValue(JsonPresentationFields.RESOURCE_TYPE));
383 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.VERSION, toscaElement.getMetadataValue(JsonPresentationFields.VERSION));
384 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.NORMALIZED_NAME, toscaElement.getMetadataValue(JsonPresentationFields.NORMALIZED_NAME));
385 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.UNIQUE_ID, toscaElement.getMetadataValue(JsonPresentationFields.UNIQUE_ID));
387 .addMetadataProperty(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaElement.getMetadataValue(JsonPresentationFields.TOSCA_RESOURCE_NAME));
388 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.UUID, toscaElement.getMetadataValue(JsonPresentationFields.UUID));
389 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.IS_ABSTRACT, toscaElement.getMetadataValue(JsonPresentationFields.IS_ABSTRACT));
390 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.INVARIANT_UUID, toscaElement.getMetadataValue(JsonPresentationFields.INVARIANT_UUID));
391 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.NAME, toscaElement.getMetadataValue(JsonPresentationFields.NAME));
392 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.SYSTEM_NAME, toscaElement.getMetadataValue(JsonPresentationFields.SYSTEM_NAME));
393 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.IS_ARCHIVED, toscaElement.getMetadataValue(JsonPresentationFields.IS_ARCHIVED));
394 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.ARCHIVE_TIME, toscaElement.getMetadataValue(JsonPresentationFields.ARCHIVE_TIME));
395 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.IS_VSP_ARCHIVED, toscaElement.getMetadataValue(JsonPresentationFields.IS_VSP_ARCHIVED));
396 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.MODEL, toscaElement.getMetadataValue(JsonPresentationFields.MODEL));
397 toscaElement.getMetadata().entrySet().stream().filter(e -> e.getValue() != null)
398 .forEach(e -> nodeTypeVertex.setJsonMetadataField(e.getKey(), e.getValue()));
399 nodeTypeVertex.setUniqueId(toscaElement.getUniqueId());
400 nodeTypeVertex.setType(toscaElement.getComponentType());
401 final String toscaVersion = toscaElement.getToscaVersion();
402 if (toscaVersion != null) {
403 nodeTypeVertex.setJsonMetadataField(JsonPresentationFields.TOSCA_DEFINITIONS_VERSION, toscaVersion);
405 final Map<String, DataTypeDataDefinition> dataTypes = toscaElement.getDataTypes();
406 if (MapUtils.isNotEmpty(dataTypes)) {
407 nodeTypeVertex.setJsonMetadataField(JsonPresentationFields.DATA_TYPES, dataTypes);
411 protected StorageOperationStatus assosiateToUsers(GraphVertex nodeTypeVertex, ToscaElement toscaElement) {
413 String userId = toscaElement.getCreatorUserId();
414 Either<GraphVertex, JanusGraphOperationStatus> findUser = findUserVertex(userId);
415 if (findUser.isRight()) {
416 JanusGraphOperationStatus status = findUser.right().value();
417 log.error(CANNOT_FIND_USER_IN_THE_GRAPH_STATUS_IS, userId, status);
418 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status);
420 GraphVertex creatorVertex = findUser.left().value();
421 GraphVertex updaterVertex = creatorVertex;
422 String updaterId = toscaElement.getLastUpdaterUserId();
423 if (updaterId != null && !updaterId.equals(userId)) {
424 findUser = findUserVertex(updaterId);
425 if (findUser.isRight()) {
426 JanusGraphOperationStatus status = findUser.right().value();
427 log.error(CANNOT_FIND_USER_IN_THE_GRAPH_STATUS_IS, userId, status);
428 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status);
430 updaterVertex = findUser.left().value();
433 Map<EdgePropertyEnum, Object> props = new EnumMap<>(EdgePropertyEnum.class);
434 props.put(EdgePropertyEnum.STATE, (String) toscaElement.getMetadataValue(JsonPresentationFields.LIFECYCLE_STATE));
435 JanusGraphOperationStatus result = janusGraphDao.createEdge(updaterVertex, nodeTypeVertex, EdgeLabelEnum.STATE, props);
436 log.debug("After associating user {} to resource {}. Edge type is {}", updaterVertex, nodeTypeVertex.getUniqueId(), EdgeLabelEnum.STATE);
437 if (JanusGraphOperationStatus.OK != result) {
438 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(result);
440 result = janusGraphDao.createEdge(updaterVertex, nodeTypeVertex, EdgeLabelEnum.LAST_MODIFIER, null);
441 log.debug("After associating user {} to resource {}. Edge type is {}", updaterVertex, nodeTypeVertex.getUniqueId(),
442 EdgeLabelEnum.LAST_MODIFIER);
443 if (!result.equals(JanusGraphOperationStatus.OK)) {
444 log.error("Failed to associate user {} to resource {}. Edge type is {}", updaterVertex, nodeTypeVertex.getUniqueId(),
445 EdgeLabelEnum.LAST_MODIFIER);
446 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(result);
448 toscaElement.setLastUpdaterUserId(toscaElement.getCreatorUserId());
449 toscaElement.setLastUpdaterFullName(toscaElement.getCreatorFullName());
450 result = janusGraphDao.createEdge(creatorVertex, nodeTypeVertex, EdgeLabelEnum.CREATOR, null);
451 log.debug("After associating user {} to resource {}. Edge type is {} ", creatorVertex, nodeTypeVertex.getUniqueId(), EdgeLabelEnum.CREATOR);
452 if (!result.equals(JanusGraphOperationStatus.OK)) {
453 log.error("Failed to associate user {} to resource {}. Edge type is {} ", creatorVertex, nodeTypeVertex.getUniqueId(),
454 EdgeLabelEnum.CREATOR);
455 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(result);
457 return StorageOperationStatus.OK;
460 protected StorageOperationStatus assosiateResourceMetadataToCategory(GraphVertex nodeTypeVertex, ToscaElement nodeType) {
461 String subcategoryName = nodeType.getCategories().get(0).getSubcategories().get(0).getName();
462 String categoryName = nodeType.getCategories().get(0).getName();
463 Either<GraphVertex, StorageOperationStatus> getCategoryVertex = getResourceCategoryVertex(nodeType.getUniqueId(), subcategoryName,
465 if (getCategoryVertex.isRight()) {
466 return getCategoryVertex.right().value();
468 GraphVertex subCategoryV = getCategoryVertex.left().value();
469 JanusGraphOperationStatus createEdge = janusGraphDao.createEdge(nodeTypeVertex, subCategoryV, EdgeLabelEnum.CATEGORY, new HashMap<>());
470 if (createEdge != JanusGraphOperationStatus.OK) {
471 log.trace("Failed to associate resource {} to category {} with id {}", nodeType.getUniqueId(), subcategoryName,
472 subCategoryV.getUniqueId());
473 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(createEdge);
475 return StorageOperationStatus.OK;
478 protected StorageOperationStatus associateComponentToModel(final GraphVertex nodeTypeVertex, final ToscaElement nodeType,
479 final EdgeLabelEnum edgeLabelEnum) {
480 Object metadataValue = nodeType.getMetadataValue(JsonPresentationFields.MODEL);
481 if (metadataValue == null || StringUtils.isEmpty((String) metadataValue)) {
482 return StorageOperationStatus.OK;
484 final String model = ((String) metadataValue);
485 final JanusGraphOperationStatus createEdge = janusGraphDao.createEdge(getModelVertex(model), nodeTypeVertex, edgeLabelEnum, new HashMap<>());
486 if (createEdge != JanusGraphOperationStatus.OK) {
487 log.trace("Failed to associate resource {} to model {}", nodeType.getUniqueId(), model);
488 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(createEdge);
490 return StorageOperationStatus.OK;
493 private GraphVertex getModelVertex(final String modelName) {
494 log.debug("getModelVertex: fetching model {}", modelName);
495 final Optional<GraphVertex> modelVertexByNameOptional = modelOperation.findModelVertexByName(modelName);
496 if (modelVertexByNameOptional.isEmpty()) {
497 throw ModelOperationExceptionSupplier.invalidModel(modelName).get();
499 return modelVertexByNameOptional.get();
502 protected Either<GraphVertex, StorageOperationStatus> getResourceCategoryVertex(String elementId, String subcategoryName, String categoryName) {
503 Either<GraphVertex, StorageOperationStatus> category = categoryOperation.getCategory(categoryName, VertexTypeEnum.RESOURCE_CATEGORY);
504 if (category.isRight()) {
505 log.trace("Failed to fetch category {} for resource {} error {}", categoryName, elementId, category.right().value());
506 return Either.right(category.right().value());
508 GraphVertex categoryV = category.left().value();
509 if (subcategoryName != null) {
510 Either<GraphVertex, StorageOperationStatus> subCategory = categoryOperation.getSubCategoryForCategory(categoryV, subcategoryName);
511 if (subCategory.isRight()) {
512 log.trace("Failed to fetch subcategory {} of category for resource {} error {}", subcategoryName, categoryName, elementId,
513 subCategory.right().value());
514 return Either.right(subCategory.right().value());
516 GraphVertex subCategoryV = subCategory.left().value();
517 return Either.left(subCategoryV);
519 return Either.left(categoryV);
522 private StorageOperationStatus associateArtifactsToResource(GraphVertex nodeTypeVertex, ToscaElement toscaElement) {
523 Map<String, ArtifactDataDefinition> artifacts = toscaElement.getArtifacts();
524 Either<GraphVertex, StorageOperationStatus> status;
525 if (artifacts != null) {
526 artifacts.values().stream().filter(a -> a.getUniqueId() == null).forEach(a -> {
527 String uniqueId = UniqueIdBuilder
528 .buildPropertyUniqueId(nodeTypeVertex.getUniqueId().toLowerCase(), a.getArtifactLabel().toLowerCase());
529 a.setUniqueId(uniqueId);
531 status = associateElementToData(nodeTypeVertex, VertexTypeEnum.ARTIFACTS, EdgeLabelEnum.ARTIFACTS, artifacts);
532 if (status.isRight()) {
533 return status.right().value();
536 Map<String, ArtifactDataDefinition> toscaArtifacts = toscaElement.getToscaArtifacts();
537 if (toscaArtifacts != null) {
538 toscaArtifacts.values().stream().filter(a -> a.getUniqueId() == null).forEach(a -> {
539 String uniqueId = UniqueIdBuilder
540 .buildPropertyUniqueId(nodeTypeVertex.getUniqueId().toLowerCase(), a.getArtifactLabel().toLowerCase());
541 a.setUniqueId(uniqueId);
543 status = associateElementToData(nodeTypeVertex, VertexTypeEnum.TOSCA_ARTIFACTS, EdgeLabelEnum.TOSCA_ARTIFACTS, toscaArtifacts);
544 if (status.isRight()) {
545 return status.right().value();
548 Map<String, ArtifactDataDefinition> deploymentArtifacts = toscaElement.getDeploymentArtifacts();
549 if (deploymentArtifacts != null) {
550 deploymentArtifacts.values().stream().filter(a -> a.getUniqueId() == null).forEach(a -> {
551 String uniqueId = UniqueIdBuilder
552 .buildPropertyUniqueId(nodeTypeVertex.getUniqueId().toLowerCase(), a.getArtifactLabel().toLowerCase());
553 a.setUniqueId(uniqueId);
555 status = associateElementToData(nodeTypeVertex, VertexTypeEnum.DEPLOYMENT_ARTIFACTS, EdgeLabelEnum.DEPLOYMENT_ARTIFACTS,
556 deploymentArtifacts);
557 if (status.isRight()) {
558 return status.right().value();
561 return StorageOperationStatus.OK;
564 protected JanusGraphOperationStatus disassociateAndDeleteCommonElements(GraphVertex toscaElementVertex) {
565 JanusGraphOperationStatus status = janusGraphDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.ARTIFACTS);
566 if (status != JanusGraphOperationStatus.OK) {
567 log.debug("Failed to disaccociate artifact for {} error {}", toscaElementVertex.getUniqueId(), status);
570 status = janusGraphDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.TOSCA_ARTIFACTS);
571 if (status != JanusGraphOperationStatus.OK) {
572 log.debug("Failed to disaccociate tosca artifact for {} error {}", toscaElementVertex.getUniqueId(), status);
575 status = janusGraphDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.DEPLOYMENT_ARTIFACTS);
576 if (status != JanusGraphOperationStatus.OK) {
577 log.debug("Failed to deployment artifact for {} error {}", toscaElementVertex.getUniqueId(), status);
580 status = janusGraphDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.PROPERTIES);
581 if (status != JanusGraphOperationStatus.OK) {
582 log.debug("Failed to disaccociate properties for {} error {}", toscaElementVertex.getUniqueId(), status);
585 status = janusGraphDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.ATTRIBUTES);
586 if (status != JanusGraphOperationStatus.OK) {
587 log.debug("Failed to disaccociate attributes for {} error {}", toscaElementVertex.getUniqueId(), status);
590 status = janusGraphDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.ADDITIONAL_INFORMATION);
591 if (status != JanusGraphOperationStatus.OK) {
592 log.debug("Failed to disaccociate additional information for {} error {}", toscaElementVertex.getUniqueId(), status);
595 status = janusGraphDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.CAPABILITIES);
596 if (status != JanusGraphOperationStatus.OK) {
597 log.debug("Failed to disaccociate capabilities for {} error {}", toscaElementVertex.getUniqueId(), status);
600 status = janusGraphDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.REQUIREMENTS);
601 if (status != JanusGraphOperationStatus.OK) {
602 log.debug("Failed to disaccociate requirements for {} error {}", toscaElementVertex.getUniqueId(), status);
605 status = janusGraphDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.FORWARDING_PATH);
606 if (status != JanusGraphOperationStatus.OK) {
607 log.debug("Failed to disaccociate requirements for {} error {}", toscaElementVertex.getUniqueId(), status);
610 return JanusGraphOperationStatus.OK;
613 protected StorageOperationStatus assosiateCommonForToscaElement(GraphVertex nodeTypeVertex, ToscaElement toscaElement) {
614 return assosiateCommonForToscaElement(nodeTypeVertex, toscaElement, null);
617 protected StorageOperationStatus assosiateCommonForToscaElement(GraphVertex nodeTypeVertex, ToscaElement toscaElement,
618 List<GraphVertex> derivedResources) {
619 StorageOperationStatus associateUsers = assosiateToUsers(nodeTypeVertex, toscaElement);
620 if (associateUsers != StorageOperationStatus.OK) {
621 return associateUsers;
623 StorageOperationStatus associateArtifacts = associateArtifactsToResource(nodeTypeVertex, toscaElement);
624 if (associateArtifacts != StorageOperationStatus.OK) {
625 return associateArtifacts;
627 StorageOperationStatus associateProperties = associatePropertiesToResource(nodeTypeVertex, toscaElement, derivedResources);
628 if (associateProperties != StorageOperationStatus.OK) {
629 return associateProperties;
631 StorageOperationStatus associateAdditionaInfo = associateAdditionalInfoToResource(nodeTypeVertex, toscaElement);
632 if (associateAdditionaInfo != StorageOperationStatus.OK) {
633 return associateAdditionaInfo;
635 if (needConnectToCatalog(toscaElement)) {
636 StorageOperationStatus associateToCatalog = associateToCatalogRoot(nodeTypeVertex);
637 if (associateToCatalog != StorageOperationStatus.OK) {
638 return associateToCatalog;
641 return StorageOperationStatus.OK;
644 private boolean needConnectToCatalog(ToscaElement toscaElement) {
645 Boolean isAbstract = (Boolean) toscaElement.getMetadataValue(JsonPresentationFields.IS_ABSTRACT);
646 if (isAbstract != null && isAbstract) {
649 return toscaElement.isHighestVersion();
652 private StorageOperationStatus associateToCatalogRoot(GraphVertex nodeTypeVertex) {
653 Either<GraphVertex, JanusGraphOperationStatus> catalog = janusGraphDao.getVertexByLabel(VertexTypeEnum.CATALOG_ROOT);
654 if (catalog.isRight()) {
655 log.debug("Failed to fetch catalog vertex. error {}", catalog.right().value());
656 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(catalog.right().value());
658 JanusGraphOperationStatus createEdge = janusGraphDao.createEdge(catalog.left().value(), nodeTypeVertex, EdgeLabelEnum.CATALOG_ELEMENT, null);
659 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(createEdge);
662 protected StorageOperationStatus associatePropertiesToResource(GraphVertex nodeTypeVertex, ToscaElement nodeType,
663 List<GraphVertex> derivedResources) {
664 // Note : currently only one derived supported!!!!
665 Either<Map<String, PropertyDataDefinition>, StorageOperationStatus> dataFromDerived = getDataFromDerived(derivedResources,
666 EdgeLabelEnum.PROPERTIES);
667 if (dataFromDerived.isRight()) {
668 return dataFromDerived.right().value();
670 Map<String, PropertyDataDefinition> propertiesAll = dataFromDerived.left().value();
671 Map<String, PropertyDataDefinition> properties = nodeType.getProperties();
672 if (properties != null) {
673 properties.values().stream().filter(p -> p.getUniqueId() == null).forEach(p -> {
674 String uid = UniqueIdBuilder.buildPropertyUniqueId(nodeTypeVertex.getUniqueId(), p.getName());
677 Either<Map<String, PropertyDataDefinition>, String> eitherMerged = ToscaDataDefinition.mergeDataMaps(propertiesAll, properties);
678 if (eitherMerged.isRight()) {
679 // TODO re-factor error handling - moving BL to operation resulted in loss of info about the invalid property
680 log.debug("property {} cannot be overriden", eitherMerged.right().value());
681 return StorageOperationStatus.INVALID_PROPERTY;
684 if (!propertiesAll.isEmpty()) {
685 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = associateElementToData(nodeTypeVertex, VertexTypeEnum.PROPERTIES,
686 EdgeLabelEnum.PROPERTIES, propertiesAll);
687 if (assosiateElementToData.isRight()) {
688 return assosiateElementToData.right().value();
691 return StorageOperationStatus.OK;
694 private StorageOperationStatus associateAdditionalInfoToResource(GraphVertex nodeTypeVertex, ToscaElement nodeType) {
695 Map<String, AdditionalInfoParameterDataDefinition> additionalInformation = nodeType.getAdditionalInformation();
696 if (additionalInformation != null) {
697 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = associateElementToData(nodeTypeVertex,
698 VertexTypeEnum.ADDITIONAL_INFORMATION, EdgeLabelEnum.ADDITIONAL_INFORMATION, additionalInformation);
699 if (assosiateElementToData.isRight()) {
700 return assosiateElementToData.right().value();
703 return StorageOperationStatus.OK;
706 protected <T extends ToscaDataDefinition> Either<Map<String, T>, StorageOperationStatus> getDataFromDerived(List<GraphVertex> derivedResources,
707 EdgeLabelEnum edge) {
708 Map<String, T> propertiesAll = new HashMap<>();
709 if (derivedResources != null && !derivedResources.isEmpty()) {
710 for (GraphVertex derived : derivedResources) {
711 Either<List<GraphVertex>, JanusGraphOperationStatus> derivedProperties = janusGraphDao
712 .getChildrenVertices(derived, edge, JsonParseFlagEnum.ParseJson);
713 if (derivedProperties.isRight()) {
714 if (derivedProperties.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
715 log.debug("Failed to get properties for derived from {} error {}", derived.getUniqueId(), derivedProperties.right().value());
716 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(derivedProperties.right().value()));
721 List<GraphVertex> propList = derivedProperties.left().value();
722 for (GraphVertex propV : propList) {
723 Map<String, T> propertiesFromDerived = (Map<String, T>) propV.getJson();
724 if (propertiesFromDerived != null) {
725 propertiesFromDerived.entrySet().forEach(x -> x.getValue().setOwnerIdIfEmpty(derived.getUniqueId()));
726 propertiesAll.putAll(propertiesFromDerived);
731 return Either.left(propertiesAll);
734 protected JanusGraphOperationStatus setArtifactsFromGraph(GraphVertex componentV, ToscaElement toscaElement) {
735 Either<Map<String, ArtifactDataDefinition>, JanusGraphOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.ARTIFACTS);
736 if (result.isLeft()) {
737 toscaElement.setArtifacts(result.left().value());
739 if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
740 return result.right().value();
743 result = getDataFromGraph(componentV, EdgeLabelEnum.DEPLOYMENT_ARTIFACTS);
744 if (result.isLeft()) {
745 toscaElement.setDeploymentArtifacts(result.left().value());
747 if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
748 return result.right().value();
751 result = getDataFromGraph(componentV, EdgeLabelEnum.TOSCA_ARTIFACTS);
752 if (result.isLeft()) {
753 toscaElement.setToscaArtifacts(result.left().value());
755 if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
756 return result.right().value();
759 return JanusGraphOperationStatus.OK;
762 protected JanusGraphOperationStatus setAllVersions(GraphVertex componentV, ToscaElement toscaElement) {
763 Map<String, String> allVersion = new HashMap<>();
764 allVersion.put((String) componentV.getMetadataProperty(GraphPropertyEnum.VERSION), componentV.getUniqueId());
765 ArrayList<GraphVertex> allChildrenAndParants = new ArrayList<>();
766 Either<GraphVertex, JanusGraphOperationStatus> childResourceRes = janusGraphDao
767 .getChildVertex(componentV, EdgeLabelEnum.VERSION, JsonParseFlagEnum.NoParse);
768 while (childResourceRes.isLeft()) {
769 GraphVertex child = childResourceRes.left().value();
770 allChildrenAndParants.add(child);
771 childResourceRes = janusGraphDao.getChildVertex(child, EdgeLabelEnum.VERSION, JsonParseFlagEnum.NoParse);
773 JanusGraphOperationStatus operationStatus = childResourceRes.right().value();
774 if (operationStatus != JanusGraphOperationStatus.NOT_FOUND) {
775 return operationStatus;
777 Either<GraphVertex, JanusGraphOperationStatus> parentResourceRes = janusGraphDao
778 .getParentVertex(componentV, EdgeLabelEnum.VERSION, JsonParseFlagEnum.NoParse);
779 while (parentResourceRes.isLeft()) {
780 GraphVertex parent = parentResourceRes.left().value();
781 allChildrenAndParants.add(parent);
782 parentResourceRes = janusGraphDao.getParentVertex(parent, EdgeLabelEnum.VERSION, JsonParseFlagEnum.NoParse);
784 operationStatus = parentResourceRes.right().value();
785 if (operationStatus != JanusGraphOperationStatus.NOT_FOUND) {
786 return operationStatus;
788 allChildrenAndParants.stream().filter(vertex -> {
789 Boolean isDeleted = (Boolean) vertex.getMetadataProperty(GraphPropertyEnum.IS_DELETED);
790 return (isDeleted == null || !isDeleted);
791 }).forEach(vertex -> allVersion.put((String) vertex.getMetadataProperty(GraphPropertyEnum.VERSION), vertex.getUniqueId()));
792 toscaElement.setAllVersions(allVersion);
793 return JanusGraphOperationStatus.OK;
798 protected <T extends ToscaElement> Either<List<T>, StorageOperationStatus> getFollowedComponent(String userId,
799 Set<LifecycleStateEnum> lifecycleStates,
800 Set<LifecycleStateEnum> lastStateStates,
801 ComponentTypeEnum neededType) {
802 Map<GraphPropertyEnum, Object> props = null;
803 if (userId != null) {
804 props = new EnumMap<>(GraphPropertyEnum.class);
805 // for Designer retrieve specific user
806 props.put(GraphPropertyEnum.USERID, userId);
808 // in case of user id == null -> get all users by label
810 // for Tester and Admin retrieve all users
811 Either<List<GraphVertex>, JanusGraphOperationStatus> usersByCriteria = janusGraphDao
812 .getByCriteria(VertexTypeEnum.USER, props, JsonParseFlagEnum.NoParse);
813 if (usersByCriteria.isRight()) {
814 log.debug("Failed to fetch users by criteria {} error {}", props, usersByCriteria.right().value());
815 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(usersByCriteria.right().value()));
817 GraphVertex userV = usersByCriteria.left().value().get(0);
818 List<T> components = new ArrayList<>();
819 List<T> componentsPerUser;
820 final Set<String> ids = new HashSet<>();
821 Either<List<GraphVertex>, JanusGraphOperationStatus> childrenVertecies = janusGraphDao
822 .getChildrenVertices(userV, EdgeLabelEnum.STATE, JsonParseFlagEnum.NoParse);
823 if (childrenVertecies.isRight() && childrenVertecies.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
824 log.debug("Failed to fetch children vertices for user {} by edge {} error {}", userV.getMetadataProperty(GraphPropertyEnum.USERID),
825 EdgeLabelEnum.STATE, childrenVertecies.right().value());
826 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(childrenVertecies.right().value()));
828 // get all resource with current state
829 if (childrenVertecies.isLeft()) {
830 componentsPerUser = fetchComponents(userId, lifecycleStates, childrenVertecies.left().value(), neededType, EdgeLabelEnum.STATE);
831 if (componentsPerUser != null) {
832 for (T comp : componentsPerUser) {
833 ids.add(comp.getUniqueId());
834 components.add(comp);
837 if (lastStateStates != null && !lastStateStates.isEmpty()) {
838 // get all resource with last state
839 childrenVertecies = janusGraphDao.getChildrenVertices(userV, EdgeLabelEnum.LAST_STATE, JsonParseFlagEnum.NoParse);
840 if (childrenVertecies.isRight() && childrenVertecies.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
841 log.debug("Failed to fetch children vertices for user {} by edge {} error {}",
842 userV.getMetadataProperty(GraphPropertyEnum.USERID), EdgeLabelEnum.LAST_STATE, childrenVertecies.right().value());
843 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(childrenVertecies.right().value()));
845 if (childrenVertecies.isLeft()) {
847 componentsPerUser = fetchComponents(userId, lastStateStates, childrenVertecies.left().value(), neededType,
848 EdgeLabelEnum.LAST_STATE);
849 if (componentsPerUser != null) {
850 for (T comp : componentsPerUser) {
852 if (ids.contains(comp.getUniqueId())) {
856 components.add(comp);
863 return Either.left(components);
866 private <T extends ToscaElement> List<T> fetchComponents(String userId, Set<LifecycleStateEnum> lifecycleStates, List<GraphVertex> vertices,
867 ComponentTypeEnum neededType, EdgeLabelEnum edgelabel) {
868 List<T> components = new ArrayList<>();
869 for (GraphVertex node : vertices) {
870 Iterator<Edge> edges = node.getVertex().edges(Direction.IN, edgelabel.name());
871 while (edges.hasNext()) {
872 Edge edge = edges.next();
873 String stateStr = (String) janusGraphDao.getProperty(edge, EdgePropertyEnum.STATE);
874 LifecycleStateEnum nodeState = LifecycleStateEnum.findState(stateStr);
875 if (nodeState == null) {
876 log.debug("no supported STATE {} for element {}", stateStr, node.getUniqueId());
879 //get user from edge and compare to user from followed request
880 JanusGraphVertex userVertex = (JanusGraphVertex) edge.outVertex();
881 String userIdFromEdge = (String) janusGraphDao.getProperty(userVertex, GraphPropertyEnum.USERID.getProperty());
882 if (lifecycleStates != null && lifecycleStates.contains(nodeState) && (userIdFromEdge.equals(userId))) {
883 Boolean isDeleted = (Boolean) node.getMetadataProperty(GraphPropertyEnum.IS_DELETED);
884 Boolean isArchived = (Boolean) node.getMetadataProperty(GraphPropertyEnum.IS_ARCHIVED);
885 if (isDeleted != null && isDeleted || isArchived != null && isArchived) {
886 log.trace("Deleted/Archived element {}, discard", node.getUniqueId());
889 Boolean isHighest = (Boolean) node.getMetadataProperty(GraphPropertyEnum.IS_HIGHEST_VERSION);
891 ComponentTypeEnum componentType = node.getType();
892 // get only latest versions
893 if (componentType == null) {
894 log.debug("No supported type {} for vertex {}", componentType, node.getUniqueId());
897 if (neededType == componentType) {
898 switch (componentType) {
901 handleNode(components, node, componentType);
904 Boolean isAbtract = (Boolean) node.getMetadataProperty(GraphPropertyEnum.IS_ABSTRACT);
905 if (isAbtract == null || !isAbtract) {
906 handleNode(components, node, componentType);
910 log.debug("not supported node type {}", componentType);
924 protected <T extends ToscaElement> void handleNode(List<T> components, GraphVertex vertexComponent, ComponentTypeEnum nodeType) {
925 Either<T, StorageOperationStatus> component = getLightComponent(vertexComponent, nodeType, new ComponentParametersView(true));
926 if (component.isRight()) {
927 log.debug("Failed to get component for id = {} error : {} skip resource", vertexComponent.getUniqueId(), component.right().value());
929 components.add(component.left().value());
933 protected <T extends ToscaElement> Either<T, StorageOperationStatus> getLightComponent(String componentUid, ComponentTypeEnum nodeType,
934 ComponentParametersView parametersFilter) {
935 Either<GraphVertex, JanusGraphOperationStatus> getVertexRes = janusGraphDao.getVertexById(componentUid);
936 if (getVertexRes.isRight()) {
937 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexRes.right().value()));
939 return getLightComponent(getVertexRes.left().value(), nodeType, parametersFilter);
942 protected <T extends ToscaElement> Either<T, StorageOperationStatus> getLightComponent(GraphVertex vertexComponent, ComponentTypeEnum nodeType,
943 ComponentParametersView parametersFilter) {
944 log.trace("Starting to build light component of type {}, id {}", nodeType, vertexComponent.getUniqueId());
945 janusGraphDao.parseVertexProperties(vertexComponent, JsonParseFlagEnum.ParseMetadata);
946 T toscaElement = convertToComponent(vertexComponent);
947 JanusGraphOperationStatus status = setCreatorFromGraph(vertexComponent, toscaElement);
948 if (status != JanusGraphOperationStatus.OK) {
949 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
951 status = setLastModifierFromGraph(vertexComponent, toscaElement);
952 if (status != JanusGraphOperationStatus.OK) {
953 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
955 status = setCategoriesFromGraph(vertexComponent, toscaElement);
956 if (status != JanusGraphOperationStatus.OK) {
957 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
959 if (!parametersFilter.isIgnoreAllVersions()) {
960 status = setAllVersions(vertexComponent, toscaElement);
961 if (status != JanusGraphOperationStatus.OK) {
962 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
965 if (!parametersFilter.isIgnoreCapabilities()) {
966 status = setCapabilitiesFromGraph(vertexComponent, toscaElement);
967 if (status != JanusGraphOperationStatus.OK) {
968 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
971 if (!parametersFilter.isIgnoreRequirements()) {
972 status = setRequirementsFromGraph(vertexComponent, toscaElement);
973 if (status != JanusGraphOperationStatus.OK) {
974 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
977 log.debug("Ended to build light component of type {}, id {}", nodeType, vertexComponent.getUniqueId());
978 return Either.left(toscaElement);
981 @SuppressWarnings("unchecked")
982 protected <T extends ToscaElement> T convertToComponent(GraphVertex componentV) {
983 ToscaElement toscaElement = null;
984 VertexTypeEnum label = componentV.getLabel();
987 toscaElement = new NodeType();
989 case TOPOLOGY_TEMPLATE:
990 toscaElement = new TopologyTemplate();
993 log.debug("Not supported tosca type {}", label);
996 if (toscaElement != null) {
997 final Map<String, Object> jsonMetada = componentV.getMetadataJson();
998 if (MapUtils.isNotEmpty(jsonMetada)) {
999 toscaElement.setMetadata(jsonMetada);
1000 final Object toscaVersion = jsonMetada.get(ToscaTagNamesEnum.TOSCA_VERSION.getElementName());
1001 if (toscaVersion != null) {
1002 toscaElement.setToscaVersion((String) toscaVersion);
1004 final Object dataTypes = jsonMetada.get(ToscaTagNamesEnum.DATA_TYPES.getElementName());
1005 if (dataTypes != null) {
1006 final Map<String, DataTypeDataDefinition> dataTypeDefinitionMap = new HashMap<>();
1007 final Map<String, Object> toscaAttributes = (Map<String, Object>) dataTypes;
1008 for (final Entry<String, Object> attributeNameValue : toscaAttributes.entrySet()) {
1009 final Object value = attributeNameValue.getValue();
1010 final String key = attributeNameValue.getKey();
1011 if (value instanceof Map) {
1012 final DataTypeDefinition dataTypeDefinition = createDataTypeDefinitionWithName(attributeNameValue);
1013 dataTypeDefinitionMap.put(dataTypeDefinition.getName(), dataTypeDefinition);
1015 dataTypeDefinitionMap.put(key, createDataType(String.valueOf(value)));
1018 toscaElement.setDataTypes(dataTypeDefinitionMap);
1022 return (T) toscaElement;
1025 protected JanusGraphOperationStatus setResourceCategoryFromGraphV(Vertex vertex, CatalogComponent catalogComponent) {
1026 List<CategoryDefinition> categories = new ArrayList<>();
1027 SubCategoryDefinition subcategory;
1028 Either<Vertex, JanusGraphOperationStatus> childVertex = janusGraphDao.getChildVertex(vertex, EdgeLabelEnum.CATEGORY);
1029 if (childVertex.isRight()) {
1030 log.debug(FAILED_TO_FETCH_FOR_TOSCA_ELEMENT_WITH_ID_ERROR, EdgeLabelEnum.CATEGORY, catalogComponent.getUniqueId(),
1031 childVertex.right().value());
1032 return childVertex.right().value();
1034 Vertex subCategoryV = childVertex.left().value();
1035 String subCategoryNormalizedName = (String) subCategoryV.property(GraphPropertyEnum.NORMALIZED_NAME.getProperty()).value();
1036 catalogComponent.setSubCategoryNormalizedName(subCategoryNormalizedName);
1037 subcategory = new SubCategoryDefinition();
1038 subcategory.setUniqueId((String) subCategoryV.property(GraphPropertyEnum.UNIQUE_ID.getProperty()).value());
1039 subcategory.setNormalizedName(subCategoryNormalizedName);
1040 subcategory.setName((String) subCategoryV.property(GraphPropertyEnum.NAME.getProperty()).value());
1041 Type listTypeSubcat = new TypeToken<List<MetadataKeyDataDefinition>>() {
1043 List<MetadataKeyDataDefinition> metadataKeys = subCategoryV.property(GraphPropertyEnum.METADATA_KEYS.getProperty()).isPresent() ? getGson()
1044 .fromJson((String) subCategoryV.property(GraphPropertyEnum.METADATA_KEYS.getProperty()).value(), listTypeSubcat)
1045 : Collections.emptyList();
1046 subcategory.setMetadataKeys(metadataKeys);
1047 Either<Vertex, JanusGraphOperationStatus> parentVertex = janusGraphDao.getParentVertex(subCategoryV, EdgeLabelEnum.SUB_CATEGORY);
1048 Vertex categoryV = parentVertex.left().value();
1049 String categoryNormalizedName = (String) categoryV.property(GraphPropertyEnum.NORMALIZED_NAME.getProperty()).value();
1050 catalogComponent.setCategoryNormalizedName(categoryNormalizedName);
1051 CategoryDefinition category = new CategoryDefinition();
1052 category.setUniqueId((String) categoryV.property(GraphPropertyEnum.UNIQUE_ID.getProperty()).value());
1053 category.setNormalizedName(categoryNormalizedName);
1054 category.setName((String) categoryV.property(GraphPropertyEnum.NAME.getProperty()).value());
1055 category.addSubCategory(subcategory);
1056 categories.add(category);
1057 catalogComponent.setCategories(categories);
1058 return JanusGraphOperationStatus.OK;
1061 protected JanusGraphOperationStatus setServiceCategoryFromGraphV(Vertex vertex, CatalogComponent catalogComponent) {
1062 List<CategoryDefinition> categories = new ArrayList<>();
1063 Either<Vertex, JanusGraphOperationStatus> childVertex = janusGraphDao.getChildVertex(vertex, EdgeLabelEnum.CATEGORY);
1064 if (childVertex.isRight()) {
1065 log.debug(FAILED_TO_FETCH_FOR_TOSCA_ELEMENT_WITH_ID_ERROR, EdgeLabelEnum.CATEGORY, catalogComponent.getUniqueId(),
1066 childVertex.right().value());
1067 return childVertex.right().value();
1069 Vertex categoryV = childVertex.left().value();
1070 String categoryNormalizedName = (String) categoryV.property(GraphPropertyEnum.NORMALIZED_NAME.getProperty()).value();
1071 catalogComponent.setCategoryNormalizedName(categoryNormalizedName);
1072 CategoryDefinition category = new CategoryDefinition();
1073 category.setUniqueId((String) categoryV.property(GraphPropertyEnum.UNIQUE_ID.getProperty()).value());
1074 category.setNormalizedName(categoryNormalizedName);
1075 category.setModels(categoryV.property(GraphPropertyEnum.MODEL.getProperty()).isPresent() ? getGson()
1076 .fromJson((String) categoryV.property(GraphPropertyEnum.MODEL.getProperty()).value(), new TypeToken<List<String>>() {
1077 }.getType()) : Collections.emptyList());
1078 category.setName((String) categoryV.property(GraphPropertyEnum.NAME.getProperty()).value());
1079 category.setUseServiceSubstitutionForNestedServices(
1080 (Boolean) categoryV.property(GraphPropertyEnum.USE_SUBSTITUTION_FOR_NESTED_SERVICES.getProperty()).orElse(false));
1081 Type listTypeCat = new TypeToken<List<MetadataKeyDataDefinition>>() {
1083 List<MetadataKeyDataDefinition> metadataKeys = categoryV.property(GraphPropertyEnum.METADATA_KEYS.getProperty()).isPresent() ? getGson()
1084 .fromJson((String) categoryV.property(GraphPropertyEnum.METADATA_KEYS.getProperty()).value(), listTypeCat) : Collections.emptyList();
1085 category.setMetadataKeys(metadataKeys);
1086 VertexProperty<Object> property = categoryV.property(GraphPropertyEnum.NOT_APPLICABLE_METADATA_KEYS.getProperty());
1087 category.setNotApplicableMetadataKeys(
1088 property.isPresent() ? getGson().fromJson((String) property.value(), new TypeToken<List<String>>() {
1089 }.getType()) : Collections.emptyList());
1090 categories.add(category);
1091 catalogComponent.setCategories(categories);
1092 return JanusGraphOperationStatus.OK;
1095 protected JanusGraphOperationStatus setResourceCategoryFromGraph(GraphVertex componentV, ToscaElement toscaElement) {
1096 List<CategoryDefinition> categories = new ArrayList<>();
1097 SubCategoryDefinition subcategory;
1098 Either<GraphVertex, JanusGraphOperationStatus> childVertex = janusGraphDao
1099 .getChildVertex(componentV, EdgeLabelEnum.CATEGORY, JsonParseFlagEnum.NoParse);
1100 if (childVertex.isRight()) {
1101 log.debug(FAILED_TO_FETCH_FOR_TOSCA_ELEMENT_WITH_ID_ERROR, EdgeLabelEnum.CATEGORY, componentV.getUniqueId(), childVertex.right().value());
1102 return childVertex.right().value();
1104 GraphVertex subCategoryV = childVertex.left().value();
1105 Map<GraphPropertyEnum, Object> metadataProperties = subCategoryV.getMetadataProperties();
1106 subcategory = new SubCategoryDefinition();
1107 subcategory.setUniqueId(subCategoryV.getUniqueId());
1108 subcategory.setNormalizedName((String) metadataProperties.get(GraphPropertyEnum.NORMALIZED_NAME));
1109 subcategory.setName((String) metadataProperties.get(GraphPropertyEnum.NAME));
1110 Type listTypeSubcat = new TypeToken<List<String>>() {
1112 List<String> iconsfromJsonSubcat = getGson().fromJson((String) metadataProperties.get(GraphPropertyEnum.ICONS), listTypeSubcat);
1113 subcategory.setIcons(iconsfromJsonSubcat);
1114 final Type metadataKeysTypeCat = new TypeToken<List<MetadataKeyDataDefinition>>() {
1116 final List<MetadataKeyDataDefinition> metadataKeysfromJsonCat = getGson()
1117 .fromJson((String) metadataProperties.get(GraphPropertyEnum.METADATA_KEYS), metadataKeysTypeCat);
1118 subcategory.setMetadataKeys(metadataKeysfromJsonCat);
1119 Either<GraphVertex, JanusGraphOperationStatus> parentVertex = janusGraphDao
1120 .getParentVertex(subCategoryV, EdgeLabelEnum.SUB_CATEGORY, JsonParseFlagEnum.NoParse);
1121 if (parentVertex.isRight()) {
1122 log.debug("failed to fetch {} for category with id {}, error {}", EdgeLabelEnum.SUB_CATEGORY, subCategoryV.getUniqueId(),
1123 parentVertex.right().value());
1124 return childVertex.right().value();
1126 GraphVertex categoryV = parentVertex.left().value();
1127 metadataProperties = categoryV.getMetadataProperties();
1128 CategoryDefinition category = new CategoryDefinition();
1129 category.setUniqueId(categoryV.getUniqueId());
1130 category.setNormalizedName((String) metadataProperties.get(GraphPropertyEnum.NORMALIZED_NAME));
1131 category.setName((String) metadataProperties.get(GraphPropertyEnum.NAME));
1132 Type listTypeCat = new TypeToken<List<String>>() {
1134 List<String> iconsfromJsonCat = getGson().fromJson((String) metadataProperties.get(GraphPropertyEnum.ICONS), listTypeCat);
1135 category.setIcons(iconsfromJsonCat);
1136 category.addSubCategory(subcategory);
1137 categories.add(category);
1138 toscaElement.setCategories(categories);
1139 return JanusGraphOperationStatus.OK;
1142 public <T extends ToscaElement> Either<T, StorageOperationStatus> updateToscaElement(T toscaElementToUpdate, GraphVertex elementV,
1143 ComponentParametersView filterResult) {
1144 Either<T, StorageOperationStatus> result = null;
1145 log.debug("In updateToscaElement. received component uid = {}", (toscaElementToUpdate == null ? null : toscaElementToUpdate.getUniqueId()));
1146 if (toscaElementToUpdate == null) {
1147 log.error("Service object is null");
1148 result = Either.right(StorageOperationStatus.BAD_REQUEST);
1151 String modifierUserId = toscaElementToUpdate.getLastUpdaterUserId();
1152 if (modifierUserId == null || modifierUserId.isEmpty()) {
1153 log.error("UserId is missing in the request.");
1154 result = Either.right(StorageOperationStatus.BAD_REQUEST);
1157 Either<GraphVertex, JanusGraphOperationStatus> findUser = findUserVertex(modifierUserId);
1158 if (findUser.isRight()) {
1159 JanusGraphOperationStatus status = findUser.right().value();
1160 log.error(CANNOT_FIND_USER_IN_THE_GRAPH_STATUS_IS, modifierUserId, status);
1163 GraphVertex modifierV = findUser.left().value();
1164 String toscaElementId = toscaElementToUpdate.getUniqueId();
1165 Either<GraphVertex, JanusGraphOperationStatus> parentVertex = janusGraphDao
1166 .getParentVertex(elementV, EdgeLabelEnum.LAST_MODIFIER, JsonParseFlagEnum.NoParse);
1167 if (parentVertex.isRight()) {
1168 log.debug("Failed to fetch last modifier for tosca element with id {} error {}", toscaElementId, parentVertex.right().value());
1169 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(parentVertex.right().value()));
1171 GraphVertex userV = parentVertex.left().value();
1172 String currentModifier = (String) userV.getMetadataProperty(GraphPropertyEnum.USERID);
1173 String prevSystemName = (String) elementV.getMetadataProperty(GraphPropertyEnum.SYSTEM_NAME);
1174 if (currentModifier.equals(modifierUserId)) {
1175 log.debug("Graph LAST MODIFIER edge should not be changed since the modifier is the same as the last modifier.");
1177 log.debug("Going to update the last modifier user of the resource from {} to {}", currentModifier, modifierUserId);
1178 StorageOperationStatus status = moveLastModifierEdge(elementV, modifierV);
1179 log.debug("Finish to update the last modifier user of the resource from {} to {}. status is {}", currentModifier, modifierUserId, status);
1180 if (status != StorageOperationStatus.OK) {
1181 result = Either.right(status);
1185 final long currentTimeMillis = System.currentTimeMillis();
1186 log.debug("Going to update the last Update Date of the resource from {} to {}",
1187 elementV.getJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE), currentTimeMillis);
1188 elementV.setJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE, currentTimeMillis);
1189 StorageOperationStatus checkCategories = validateCategories(toscaElementToUpdate, elementV);
1190 if (checkCategories != StorageOperationStatus.OK) {
1191 result = Either.right(checkCategories);
1194 // update all data on vertex
1195 fillToscaElementVertexData(elementV, toscaElementToUpdate, JsonParseFlagEnum.ParseMetadata);
1196 Either<GraphVertex, JanusGraphOperationStatus> updateElement = janusGraphDao.updateVertex(elementV);
1197 if (updateElement.isRight()) {
1198 log.error("Failed to update resource {}. status is {}", toscaElementId, updateElement.right().value());
1199 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(updateElement.right().value()));
1202 GraphVertex updateElementV = updateElement.left().value();
1203 // DE230195 in case resource name changed update TOSCA artifacts
1205 // file names accordingly
1206 String newSystemName = (String) updateElementV.getMetadataProperty(GraphPropertyEnum.SYSTEM_NAME);
1207 if (newSystemName != null && !newSystemName.equals(prevSystemName)) {
1208 Either<Map<String, ArtifactDataDefinition>, JanusGraphOperationStatus> resultToscaArt = getDataFromGraph(updateElementV,
1209 EdgeLabelEnum.TOSCA_ARTIFACTS);
1210 if (resultToscaArt.isRight()) {
1211 log.debug("Failed to get tosca artifact from graph for tosca element {} error {}", toscaElementId, resultToscaArt.right().value());
1212 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(resultToscaArt.right().value()));
1214 Map<String, ArtifactDataDefinition> toscaArtifacts = resultToscaArt.left().value();
1215 if (toscaArtifacts != null) {
1216 for (Entry<String, ArtifactDataDefinition> artifact : toscaArtifacts.entrySet()) {
1217 generateNewToscaFileName(toscaElementToUpdate.getComponentType().getValue().toLowerCase(), newSystemName, artifact.getValue());
1219 // TODO call to new Artifact operation in order to update list of artifacts
1222 if (toscaElementToUpdate.getComponentType() == ComponentTypeEnum.RESOURCE) {
1223 StorageOperationStatus resultDerived = updateDerived(toscaElementToUpdate, updateElementV);
1224 if (resultDerived != StorageOperationStatus.OK) {
1225 log.debug("Failed to update from derived data for element {} error {}", toscaElementId, resultDerived);
1226 return Either.right(resultDerived);
1229 Either<T, StorageOperationStatus> updatedResource = getToscaElement(updateElementV, filterResult);
1230 if (updatedResource.isRight()) {
1231 log.error("Failed to fetch tosca element {} after update , error {}", toscaElementId, updatedResource.right().value());
1232 result = Either.right(StorageOperationStatus.BAD_REQUEST);
1235 T updatedResourceValue = updatedResource.left().value();
1236 result = Either.left(updatedResourceValue);
1240 protected StorageOperationStatus moveLastModifierEdge(GraphVertex elementV, GraphVertex modifierV) {
1241 return DaoStatusConverter
1242 .convertJanusGraphStatusToStorageStatus(janusGraphDao.moveEdge(elementV, modifierV, EdgeLabelEnum.LAST_MODIFIER, Direction.IN));
1245 protected StorageOperationStatus moveCategoryEdge(GraphVertex elementV, GraphVertex categoryV) {
1246 return DaoStatusConverter
1247 .convertJanusGraphStatusToStorageStatus(janusGraphDao.moveEdge(elementV, categoryV, EdgeLabelEnum.CATEGORY, Direction.OUT));
1250 private void generateNewToscaFileName(String componentType, String componentName, ArtifactDataDefinition artifactInfo) {
1251 Optional<Entry<String, Object>> oConfig = ConfigurationManager.getConfigurationManager().getConfiguration().getToscaArtifacts()
1252 .entrySet().stream().filter(p -> p.getKey().equalsIgnoreCase(artifactInfo.getArtifactLabel())).findAny();
1253 if (oConfig.isPresent()) {
1254 artifactInfo.setArtifactName(componentType + "-" + componentName + ((Map<String, Object>) oConfig.get().getValue()).get("artifactName"));
1256 artifactInfo.setArtifactName(componentType + "-" + componentName);
1260 protected <T extends ToscaElement> StorageOperationStatus validateResourceCategory(T toscaElementToUpdate, GraphVertex elementV) {
1261 StorageOperationStatus status = StorageOperationStatus.OK;
1262 List<CategoryDefinition> newCategoryList = toscaElementToUpdate.getCategories();
1263 CategoryDefinition newCategory = newCategoryList.get(0);
1264 Either<GraphVertex, JanusGraphOperationStatus> childVertex = janusGraphDao
1265 .getChildVertex(elementV, EdgeLabelEnum.CATEGORY, JsonParseFlagEnum.NoParse);
1266 if (childVertex.isRight()) {
1267 log.debug(FAILED_TO_FETCH_FOR_TOSCA_ELEMENT_WITH_ID_ERROR, EdgeLabelEnum.CATEGORY, elementV.getUniqueId(), childVertex.right().value());
1268 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(childVertex.right().value());
1270 GraphVertex subCategoryV = childVertex.left().value();
1271 Map<GraphPropertyEnum, Object> metadataProperties = subCategoryV.getMetadataProperties();
1272 String subCategoryNameCurrent = (String) metadataProperties.get(GraphPropertyEnum.NAME);
1273 Either<GraphVertex, JanusGraphOperationStatus> parentVertex = janusGraphDao
1274 .getParentVertex(subCategoryV, EdgeLabelEnum.SUB_CATEGORY, JsonParseFlagEnum.NoParse);
1275 if (parentVertex.isRight()) {
1276 log.debug("failed to fetch {} for category with id {}, error {}", EdgeLabelEnum.SUB_CATEGORY, subCategoryV.getUniqueId(),
1277 parentVertex.right().value());
1278 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(childVertex.right().value());
1280 GraphVertex categoryV = parentVertex.left().value();
1281 metadataProperties = categoryV.getMetadataProperties();
1282 String categoryNameCurrent = (String) metadataProperties.get(GraphPropertyEnum.NAME);
1283 boolean categoryWasChanged = false;
1284 String newCategoryName = newCategory.getName();
1285 SubCategoryDefinition newSubcategory = newCategory.getSubcategories().get(0);
1286 String newSubCategoryName = newSubcategory.getName();
1287 if (newCategoryName != null && !newCategoryName.equals(categoryNameCurrent)) {
1288 // the category was changed
1289 categoryWasChanged = true;
1291 // the sub-category was changed
1292 if (newSubCategoryName != null && !newSubCategoryName.equals(subCategoryNameCurrent)) {
1293 log.debug("Going to update the category of the resource from {} to {}", categoryNameCurrent, newCategory);
1294 categoryWasChanged = true;
1297 if (categoryWasChanged) {
1298 Either<GraphVertex, StorageOperationStatus> getCategoryVertex = getResourceCategoryVertex(elementV.getUniqueId(), newSubCategoryName,
1300 if (getCategoryVertex.isRight()) {
1301 return getCategoryVertex.right().value();
1303 GraphVertex newCategoryV = getCategoryVertex.left().value();
1304 status = moveCategoryEdge(elementV, newCategoryV);
1305 log.debug("Going to update the category of the resource from {} to {}. status is {}", categoryNameCurrent, newCategory, status);
1310 public <T extends ToscaElement> Either<List<T>, StorageOperationStatus> getElementCatalogData(ComponentTypeEnum componentType,
1311 List<ResourceTypeEnum> excludeTypes,
1312 boolean isHighestVersions) {
1313 Either<List<GraphVertex>, JanusGraphOperationStatus> listOfComponents;
1314 if (isHighestVersions) {
1315 listOfComponents = getListOfHighestComponents(componentType, excludeTypes, JsonParseFlagEnum.NoParse);
1317 listOfComponents = getListOfHighestAndAllCertifiedComponents(componentType, excludeTypes);
1319 if (listOfComponents.isRight() && listOfComponents.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
1320 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(listOfComponents.right().value()));
1322 List<T> result = new ArrayList<>();
1323 if (listOfComponents.isLeft()) {
1324 List<GraphVertex> highestAndAllCertified = listOfComponents.left().value();
1325 if (highestAndAllCertified != null && !highestAndAllCertified.isEmpty()) {
1326 for (GraphVertex vertexComponent : highestAndAllCertified) {
1327 Either<T, StorageOperationStatus> component = getLightComponent(vertexComponent, componentType,
1328 new ComponentParametersView(true));
1329 if (component.isRight()) {
1330 log.debug("Failed to fetch light element for {} error {}", vertexComponent.getUniqueId(), component.right().value());
1331 return Either.right(component.right().value());
1333 result.add(component.left().value());
1338 return Either.left(result);
1341 public Either<List<CatalogComponent>, StorageOperationStatus> getElementCatalogData(boolean isCatalog, List<ResourceTypeEnum> excludeTypes) {
1342 StopWatch stopWatch = new StopWatch();
1344 Map<String, CatalogComponent> existInCatalog = new HashMap<>();
1345 Either<Iterator<Vertex>, JanusGraphOperationStatus> verticesEither = janusGraphDao.getCatalogOrArchiveVertices(isCatalog);
1346 if (verticesEither.isRight()) {
1347 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(verticesEither.right().value()));
1349 Iterator<Vertex> vertices = verticesEither.left().value();
1350 while (vertices.hasNext()) {
1351 Vertex vertex = vertices.next();
1352 VertexProperty<?> vertexProperty = vertex.property(GraphPropertiesDictionary.METADATA.getProperty());
1353 if(!(vertexProperty instanceof EmptyVertexProperty)) {
1354 handleCatalogComponent(existInCatalog, vertex, excludeTypes);
1360 String timeToFetchElements = stopWatch.prettyPrint();
1361 log.info("time to fetch all catalog elements: {}", timeToFetchElements);
1362 return Either.left(existInCatalog.values().stream().collect(Collectors.toList()));
1365 private void logVertex(Vertex vertex) {
1366 StringBuilder sb = new StringBuilder();
1367 vertex.keys().forEach(key -> {
1368 Object value = vertex.property(key);
1369 sb.append("[").append(key).append(": ").append(value).append("] ");
1371 log.warn(EcompLoggerErrorCode.DATA_ERROR, "catalog-model", "Vertex has no metadata property: ", sb.toString());
1374 private void handleCatalogComponent(Map<String, CatalogComponent> existInCatalog, Vertex vertex, List<ResourceTypeEnum> excludeTypes) {
1375 VertexProperty<Object> property = vertex.property(GraphPropertiesDictionary.METADATA.getProperty());
1376 String json = (String) property.value();
1377 Map<String, Object> metadatObj = JsonParserUtils.toMap(json);
1378 String uniqueId = (String) metadatObj.get(JsonPresentationFields.UNIQUE_ID.getPresentation());
1379 Boolean isDeleted = (Boolean) metadatObj.get(JsonPresentationFields.IS_DELETED.getPresentation());
1380 if (isNotExcluded(excludeTypes, metadatObj) && (existInCatalog.get(uniqueId) == null && (isDeleted == null || !isDeleted.booleanValue()))) {
1381 CatalogComponent catalogComponent = createCatalogComponent(vertex, metadatObj, uniqueId);
1382 existInCatalog.put(uniqueId, catalogComponent);
1386 private CatalogComponent createCatalogComponent(Vertex vertex, Map<String, Object> metadatObj, String uniqueId) {
1387 CatalogComponent catalogComponent = new CatalogComponent();
1388 catalogComponent.setUniqueId(uniqueId);
1389 catalogComponent.setModel((String) metadatObj.get(JsonPresentationFields.MODEL.getPresentation()));
1391 .setComponentType(ComponentTypeEnum.valueOf((String) metadatObj.get(JsonPresentationFields.COMPONENT_TYPE.getPresentation())));
1392 catalogComponent.setVersion((String) metadatObj.get(JsonPresentationFields.VERSION.getPresentation()));
1393 catalogComponent.setName((String) metadatObj.get(JsonPresentationFields.NAME.getPresentation()));
1394 catalogComponent.setIcon((String) metadatObj.get(JsonPresentationFields.ICON.getPresentation()));
1395 catalogComponent.setLifecycleState((String) metadatObj.get(JsonPresentationFields.LIFECYCLE_STATE.getPresentation()));
1396 Object lastUpdateDate = metadatObj.get(JsonPresentationFields.LAST_UPDATE_DATE.getPresentation());
1397 catalogComponent.setLastUpdateDate((lastUpdateDate != null ? (Long) lastUpdateDate : 0L));
1398 catalogComponent.setDistributionStatus((String) metadatObj.get(JsonPresentationFields.DISTRIBUTION_STATUS.getPresentation()));
1399 catalogComponent.setDescription((String) metadatObj.get(JsonPresentationFields.DESCRIPTION.getPresentation()));
1400 catalogComponent.setTenant((String) metadatObj.get(JsonPresentationFields.TENANT.getPresentation()));
1401 catalogComponent.setSystemName((String) metadatObj.get(JsonPresentationFields.SYSTEM_NAME.getPresentation()));
1402 catalogComponent.setUuid((String) metadatObj.get(JsonPresentationFields.UUID.getPresentation()));
1403 catalogComponent.setInvariantUUID((String) metadatObj.get(JsonPresentationFields.INVARIANT_UUID.getPresentation()));
1404 catalogComponent.setIsHighestVersion((Boolean) metadatObj.get(JsonPresentationFields.HIGHEST_VERSION.getPresentation()));
1405 Iterator<Edge> edges = vertex.edges(Direction.IN, EdgeLabelEnum.STATE.name());
1406 if (edges.hasNext()) {
1408 .setLastUpdaterUserId((String) edges.next().outVertex().property(GraphPropertiesDictionary.USERID.getProperty()).value());
1410 Object resourceType = metadatObj.get(JsonPresentationFields.RESOURCE_TYPE.getPresentation());
1411 if (resourceType != null) {
1412 catalogComponent.setResourceType((String) resourceType);
1414 if (catalogComponent.getComponentType() == ComponentTypeEnum.SERVICE) {
1415 setServiceCategoryFromGraphV(vertex, catalogComponent);
1417 setResourceCategoryFromGraphV(vertex, catalogComponent);
1419 List<String> tags = (List<String>) metadatObj.get(JsonPresentationFields.TAGS.getPresentation());
1421 catalogComponent.setTags(tags);
1423 return catalogComponent;
1426 private boolean isNotExcluded(List<ResourceTypeEnum> excludeTypes, Map<String, Object> metadatObj) {
1427 boolean isAddToCatalog = true;
1428 Object resourceTypeStr = metadatObj.get(JsonPresentationFields.RESOURCE_TYPE.getPresentation());
1429 if (resourceTypeStr != null && excludeTypes != null) {
1430 ResourceTypeEnum resourceType = ResourceTypeEnum.getType((String) resourceTypeStr);
1431 return !excludeTypes.stream()
1432 .filter(type -> type == resourceType)
1436 return isAddToCatalog;
1439 private Either<List<GraphVertex>, JanusGraphOperationStatus> getListOfHighestComponents(ComponentTypeEnum componentType,
1440 List<ResourceTypeEnum> excludeTypes,
1441 JsonParseFlagEnum parseFlag) {
1442 Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
1443 Map<GraphPropertyEnum, Object> propertiesHasNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
1444 propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
1445 propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
1446 if (componentType == ComponentTypeEnum.RESOURCE) {
1447 propertiesToMatch.put(GraphPropertyEnum.IS_ABSTRACT, false);
1448 propertiesHasNotToMatch.put(GraphPropertyEnum.RESOURCE_TYPE, excludeTypes);
1450 propertiesHasNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
1451 propertiesHasNotToMatch.put(GraphPropertyEnum.IS_ARCHIVED, true); //US382674, US382683
1452 return janusGraphDao.getByCriteria(null, propertiesToMatch, propertiesHasNotToMatch, parseFlag);
1455 // highest + (certified && !highest)
1456 private Either<List<GraphVertex>, JanusGraphOperationStatus> getListOfHighestAndAllCertifiedComponents(ComponentTypeEnum componentType,
1457 List<ResourceTypeEnum> excludeTypes) {
1458 long startFetchAllStates = System.currentTimeMillis();
1459 Either<List<GraphVertex>, JanusGraphOperationStatus> highestNodes = getListOfHighestComponents(componentType, excludeTypes,
1460 JsonParseFlagEnum.ParseMetadata);
1461 Map<GraphPropertyEnum, Object> propertiesToMatchCertified = new EnumMap<>(GraphPropertyEnum.class);
1462 Map<GraphPropertyEnum, Object> propertiesHasNotToMatchCertified = new EnumMap<>(GraphPropertyEnum.class);
1463 propertiesToMatchCertified.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
1464 propertiesToMatchCertified.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
1465 if (componentType == ComponentTypeEnum.RESOURCE) {
1466 propertiesToMatchCertified.put(GraphPropertyEnum.IS_ABSTRACT, false);
1467 propertiesHasNotToMatchCertified.put(GraphPropertyEnum.RESOURCE_TYPE, excludeTypes);
1469 propertiesHasNotToMatchCertified.put(GraphPropertyEnum.IS_DELETED, true);
1470 propertiesHasNotToMatchCertified.put(GraphPropertyEnum.IS_ARCHIVED, true); //US382674, US382683
1471 propertiesHasNotToMatchCertified.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
1472 Either<List<GraphVertex>, JanusGraphOperationStatus> certifiedNotHighestNodes = janusGraphDao
1473 .getByCriteria(null, propertiesToMatchCertified, propertiesHasNotToMatchCertified, JsonParseFlagEnum.ParseMetadata);
1474 if (certifiedNotHighestNodes.isRight() && certifiedNotHighestNodes.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
1475 return Either.right(certifiedNotHighestNodes.right().value());
1477 long endFetchAllStates = System.currentTimeMillis();
1478 List<GraphVertex> allNodes = new ArrayList<>();
1479 if (certifiedNotHighestNodes.isLeft()) {
1480 allNodes.addAll(certifiedNotHighestNodes.left().value());
1482 if (highestNodes.isLeft()) {
1483 allNodes.addAll(highestNodes.left().value());
1485 log.debug("Fetch catalog {}s all states from graph took {} ms", componentType, endFetchAllStates - startFetchAllStates);
1486 return Either.left(allNodes);
1489 protected Either<List<GraphVertex>, StorageOperationStatus> getAllComponentsMarkedForDeletion(ComponentTypeEnum componentType) {
1490 // get all components marked for delete
1491 Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
1492 props.put(GraphPropertyEnum.IS_DELETED, true);
1493 props.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
1494 Either<List<GraphVertex>, JanusGraphOperationStatus> componentsToDelete = janusGraphDao.getByCriteria(null, props, JsonParseFlagEnum.NoParse);
1495 if (componentsToDelete.isRight()) {
1496 JanusGraphOperationStatus error = componentsToDelete.right().value();
1497 if (error.equals(JanusGraphOperationStatus.NOT_FOUND)) {
1498 log.trace("no components to delete");
1499 return Either.left(new ArrayList<>());
1501 log.info("failed to find components to delete. error : {}", error.name());
1502 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(error));
1505 return Either.left(componentsToDelete.left().value());
1508 protected JanusGraphOperationStatus setAdditionalInformationFromGraph(GraphVertex componentV, ToscaElement toscaElement) {
1509 Either<Map<String, AdditionalInfoParameterDataDefinition>, JanusGraphOperationStatus> result = getDataFromGraph(componentV,
1510 EdgeLabelEnum.ADDITIONAL_INFORMATION);
1511 if (result.isLeft()) {
1512 toscaElement.setAdditionalInformation(result.left().value());
1514 if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
1515 return result.right().value();
1518 return JanusGraphOperationStatus.OK;
1521 // --------------------------------------------
1522 public abstract <T extends ToscaElement> Either<T, StorageOperationStatus> getToscaElement(String uniqueId,
1523 ComponentParametersView componentParametersView);
1525 public abstract <T extends ToscaElement> Either<T, StorageOperationStatus> getToscaElement(GraphVertex toscaElementVertex,
1526 ComponentParametersView componentParametersView);
1528 public abstract <T extends ToscaElement> Either<T, StorageOperationStatus> deleteToscaElement(GraphVertex toscaElementVertex);
1530 public abstract <T extends ToscaElement> Either<T, StorageOperationStatus> createToscaElement(ToscaElement toscaElement);
1532 protected abstract <T extends ToscaElement> JanusGraphOperationStatus setCategoriesFromGraph(GraphVertex vertexComponent, T toscaElement);
1534 protected abstract <T extends ToscaElement> JanusGraphOperationStatus setCapabilitiesFromGraph(GraphVertex componentV, T toscaElement);
1536 protected abstract <T extends ToscaElement> JanusGraphOperationStatus setRequirementsFromGraph(GraphVertex componentV, T toscaElement);
1538 protected abstract <T extends ToscaElement> StorageOperationStatus validateCategories(T toscaElementToUpdate, GraphVertex elementV);
1540 protected abstract <T extends ToscaElement> StorageOperationStatus updateDerived(T toscaElementToUpdate, GraphVertex updateElementV);
1542 public abstract <T extends ToscaElement> void fillToscaElementVertexData(GraphVertex elementV, T toscaElementToUpdate, JsonParseFlagEnum flag);