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.CollectionUtils;
41 import org.apache.commons.collections4.MapUtils;
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.janusgraph.core.JanusGraphVertex;
47 import org.openecomp.sdc.be.config.ConfigurationManager;
48 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
49 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
50 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
51 import org.openecomp.sdc.be.dao.jsongraph.types.EdgePropertyEnum;
52 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
53 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
54 import org.openecomp.sdc.be.dao.jsongraph.utils.JsonParserUtils;
55 import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary;
56 import org.openecomp.sdc.be.datatypes.category.MetadataKeyDataDefinition;
57 import org.openecomp.sdc.be.datatypes.elements.AdditionalInfoParameterDataDefinition;
58 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
59 import org.openecomp.sdc.be.datatypes.elements.DataTypeDataDefinition;
60 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
61 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
62 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
63 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
64 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
65 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
66 import org.openecomp.sdc.be.model.ComponentParametersView;
67 import org.openecomp.sdc.be.model.DataTypeDefinition;
68 import org.openecomp.sdc.be.model.LifecycleStateEnum;
69 import org.openecomp.sdc.be.model.catalog.CatalogComponent;
70 import org.openecomp.sdc.be.model.category.CategoryDefinition;
71 import org.openecomp.sdc.be.model.category.SubCategoryDefinition;
72 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.NodeType;
73 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.TopologyTemplate;
74 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElement;
75 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElementTypeEnum;
76 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.exception.ModelOperationExceptionSupplier;
77 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
78 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
79 import org.openecomp.sdc.be.model.operations.impl.ModelOperation;
80 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
81 import org.openecomp.sdc.be.utils.TypeUtils;
82 import org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum;
83 import org.openecomp.sdc.common.jsongraph.util.CommonUtility;
84 import org.openecomp.sdc.common.jsongraph.util.CommonUtility.LogLevelEnum;
85 import org.openecomp.sdc.common.log.wrappers.Logger;
86 import org.openecomp.sdc.common.util.ValidationUtils;
87 import org.springframework.beans.factory.annotation.Autowired;
88 import org.springframework.util.StopWatch;
90 public abstract class ToscaElementOperation extends BaseOperation {
92 private static final String FAILED_TO_FETCH_FOR_TOSCA_ELEMENT_WITH_ID_ERROR = "failed to fetch {} for tosca element with id {}, error {}";
93 private static final String CANNOT_FIND_USER_IN_THE_GRAPH_STATUS_IS = "Cannot find user {} in the graph. status is {}";
94 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 {}. ";
95 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 {}. ";
96 private static final Gson gson = new Gson();
97 private static Logger log = Logger.getLogger(ToscaElementOperation.class.getName());
99 protected CategoryOperation categoryOperation;
101 protected ModelOperation modelOperation;
103 public static DataTypeDefinition createDataType(final String dataTypeName) {
104 final DataTypeDefinition dataType = new DataTypeDefinition();
105 dataType.setName(dataTypeName);
109 public static DataTypeDefinition createDataTypeDefinitionWithName(final Entry<String, Object> attributeNameValue) {
110 final Map<String, Object> attributeMap = (Map<String, Object>) attributeNameValue.getValue();
111 final DataTypeDefinition dataType = createDataType(attributeNameValue.getKey());
112 setField(attributeMap, TypeUtils.ToscaTagNamesEnum.DESCRIPTION, dataType::setDescription);
113 setField(attributeMap, TypeUtils.ToscaTagNamesEnum.DERIVED_FROM_NAME, dataType::setDerivedFromName);
114 // TODO - find the way to set the properties
116 // CommonImportManager.setProperties(attributeMap, dataType::setProperties);
117 final Object derivedFrom = attributeMap.get(JsonPresentationFields.DERIVED_FROM.getPresentation());
118 if (derivedFrom instanceof Map) {
119 final Map<String, Object> derivedFromMap = (Map<String, Object>) derivedFrom;
120 final DataTypeDefinition parentDataTypeDataDefinition = new DataTypeDefinition();
121 parentDataTypeDataDefinition.setName((String) derivedFromMap.get(JsonPresentationFields.NAME.getPresentation()));
122 parentDataTypeDataDefinition.setUniqueId((String) derivedFromMap.get(JsonPresentationFields.UNIQUE_ID.getPresentation()));
123 parentDataTypeDataDefinition.setCreationTime((Long) derivedFromMap.get(JsonPresentationFields.CREATION_TIME.getPresentation()));
124 parentDataTypeDataDefinition.setModificationTime((Long) derivedFromMap.get(JsonPresentationFields.MODIFICATION_TIME.getPresentation()));
125 dataType.setDerivedFrom(parentDataTypeDataDefinition);
130 protected Gson getGson() {
134 protected Either<GraphVertex, StorageOperationStatus> getComponentByLabelAndId(String uniqueId, ToscaElementTypeEnum nodeType,
135 JsonParseFlagEnum parseFlag) {
136 Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
137 propertiesToMatch.put(GraphPropertyEnum.UNIQUE_ID, uniqueId);
138 VertexTypeEnum vertexType = ToscaElementTypeEnum.getVertexTypeByToscaType(nodeType);
139 Either<List<GraphVertex>, JanusGraphOperationStatus> getResponse = janusGraphDao.getByCriteria(vertexType, propertiesToMatch, parseFlag);
140 if (getResponse.isRight()) {
141 log.debug("Couldn't fetch component with type {} and unique id {}, error: {}", vertexType, uniqueId, getResponse.right().value());
142 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getResponse.right().value()));
144 List<GraphVertex> componentList = getResponse.left().value();
145 if (componentList.isEmpty()) {
146 log.debug("Component with type {} and unique id {} was not found", vertexType, uniqueId);
147 return Either.right(StorageOperationStatus.NOT_FOUND);
149 GraphVertex vertexG = componentList.get(0);
150 return Either.left(vertexG);
153 protected GraphVertex getHighestVersionFrom(GraphVertex v) {
154 Either<GraphVertex, JanusGraphOperationStatus> childVertexE = janusGraphDao
155 .getChildVertex(v, EdgeLabelEnum.VERSION, JsonParseFlagEnum.NoParse);
156 GraphVertex highestVersionVertex = v;
157 while (childVertexE.isLeft()) {
158 highestVersionVertex = childVertexE.left().value();
159 childVertexE = janusGraphDao.getChildVertex(highestVersionVertex, EdgeLabelEnum.VERSION, JsonParseFlagEnum.NoParse);
161 return highestVersionVertex;
164 public Either<ToscaElement, StorageOperationStatus> getToscaElement(String uniqueId) {
165 return getToscaElement(uniqueId, new ComponentParametersView());
168 public Either<GraphVertex, StorageOperationStatus> markComponentToDelete(GraphVertex componentToDelete) {
169 Boolean isDeleted = (Boolean) componentToDelete.getMetadataProperty(GraphPropertyEnum.IS_DELETED);
170 if (isDeleted != null && isDeleted && !(Boolean) componentToDelete.getMetadataProperty(GraphPropertyEnum.IS_HIGHEST_VERSION)) {
171 // component already marked for delete
173 componentToDelete.addMetadataProperty(GraphPropertyEnum.IS_DELETED, Boolean.TRUE);
174 componentToDelete.setJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE, System.currentTimeMillis());
175 Either<GraphVertex, JanusGraphOperationStatus> updateNode = janusGraphDao.updateVertex(componentToDelete);
176 StorageOperationStatus updateComponent;
177 if (updateNode.isRight()) {
178 log.debug("Failed to update component {}. status is {}", componentToDelete.getUniqueId(), updateNode.right().value());
179 updateComponent = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(updateNode.right().value());
180 return Either.right(updateComponent);
183 return Either.left(componentToDelete);
187 * Performs a shadow clone of previousToscaElement
189 * @param previousToscaElement
190 * @param nextToscaElement
194 public Either<GraphVertex, StorageOperationStatus> cloneToscaElement(GraphVertex previousToscaElement, GraphVertex nextToscaElement,
196 Either<GraphVertex, StorageOperationStatus> result = null;
197 GraphVertex createdToscaElementVertex = null;
198 JanusGraphOperationStatus status;
199 Either<GraphVertex, JanusGraphOperationStatus> createNextVersionRes = janusGraphDao.createVertex(nextToscaElement);
200 if (createNextVersionRes.isRight()) {
201 status = createNextVersionRes.right().value();
202 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to create tosca element vertex {} with version {} on graph. Status is {}. ",
203 previousToscaElement.getMetadataProperty(GraphPropertyEnum.NORMALIZED_NAME),
204 previousToscaElement.getMetadataProperty(GraphPropertyEnum.VERSION), status);
205 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
207 if (result == null) {
208 createdToscaElementVertex = createNextVersionRes.left().value();
209 final Map<EdgePropertyEnum, Object> properties = new EnumMap<>(EdgePropertyEnum.class);
210 properties.put(EdgePropertyEnum.STATE, createdToscaElementVertex.getMetadataProperty(GraphPropertyEnum.STATE));
211 status = janusGraphDao.createEdge(user.getVertex(), createdToscaElementVertex.getVertex(), EdgeLabelEnum.STATE, properties);
212 if (status != JanusGraphOperationStatus.OK) {
213 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG,
214 FAILED_TO_CREATE_EDGE_WITH_LABEL_FROM_USER_VERTEX_TO_TOSCA_ELEMENT_VERTEX_ON_GRAPH_STATUS_IS, EdgeLabelEnum.STATE,
215 user.getUniqueId(), previousToscaElement.getMetadataProperty(GraphPropertyEnum.NORMALIZED_NAME), status);
216 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
219 if (result == null) {
221 VertexTypeEnum.TOPOLOGY_TEMPLATE.equals(previousToscaElement.getLabel())
222 ? createModelEdge(previousToscaElement, nextToscaElement, user, createdToscaElementVertex, EdgeLabelEnum.MODEL)
223 : createModelEdge(previousToscaElement, nextToscaElement, user, createdToscaElementVertex, EdgeLabelEnum.MODEL_ELEMENT);
225 if (result == null) {
226 status = janusGraphDao.createEdge(user.getVertex(), createdToscaElementVertex.getVertex(), EdgeLabelEnum.LAST_MODIFIER, new HashMap<>());
227 if (status != JanusGraphOperationStatus.OK) {
228 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG,
229 FAILED_TO_CREATE_EDGE_WITH_LABEL_FROM_USER_VERTEX_TO_TOSCA_ELEMENT_VERTEX_ON_GRAPH_STATUS_IS, EdgeLabelEnum.LAST_MODIFIER,
230 user.getUniqueId(), nextToscaElement.getMetadataProperty(GraphPropertyEnum.NORMALIZED_NAME), status);
231 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
234 if (result == null) {
235 Either<GraphVertex, JanusGraphOperationStatus> creatorVertexRes = janusGraphDao
236 .getParentVertex(previousToscaElement, EdgeLabelEnum.CREATOR, JsonParseFlagEnum.NoParse);
237 if (creatorVertexRes.isRight()) {
238 status = creatorVertexRes.right().value();
239 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_GET_CREATOR_VERTEX_OF_TOSCA_ELEMENT_VERTEX_ON_GRAPH_STATUS_IS,
240 EdgeLabelEnum.CREATOR, nextToscaElement.getMetadataProperty(GraphPropertyEnum.NORMALIZED_NAME), status);
241 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
243 status = janusGraphDao
244 .createEdge(creatorVertexRes.left().value().getVertex(), createdToscaElementVertex.getVertex(), EdgeLabelEnum.CREATOR,
246 if (status != JanusGraphOperationStatus.OK) {
247 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG,
248 FAILED_TO_CREATE_EDGE_WITH_LABEL_FROM_USER_VERTEX_TO_TOSCA_ELEMENT_VERTEX_ON_GRAPH_STATUS_IS, EdgeLabelEnum.CREATOR,
249 user.getUniqueId(), nextToscaElement.getMetadataProperty(GraphPropertyEnum.NORMALIZED_NAME), status);
250 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
253 if (result == null) {
254 Iterator<Edge> edgesToCopyIter = previousToscaElement.getVertex().edges(Direction.OUT);
255 while (edgesToCopyIter.hasNext()) {
256 Edge currEdge = edgesToCopyIter.next();
257 Vertex currVertex = currEdge.inVertex();
258 status = janusGraphDao
259 .createEdge(createdToscaElementVertex.getVertex(), currVertex, EdgeLabelEnum.getEdgeLabelEnum(currEdge.label()), currEdge);
260 if (status != JanusGraphOperationStatus.OK) {
261 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG,
262 "Failed to create edge with label {} from tosca element vertex {} to vertex with label {} on graph. Status is {}. ",
263 currEdge.label(), createdToscaElementVertex.getUniqueId(), currVertex.property(GraphPropertyEnum.LABEL.getProperty()),
265 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
270 if (result == null) {
271 result = Either.left(createdToscaElementVertex);
274 .addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to clone tosca element {} with the name {}. ", previousToscaElement.getUniqueId(),
275 previousToscaElement.getMetadataProperty(GraphPropertyEnum.NORMALIZED_NAME));
281 * Creates the MODEL in case it exits on the previous version
283 * @param previousToscaElement previous element version
284 * @param nextToscaElement latest element version
286 * @param createdToscaElementVertex created tosca element
287 * @param edgeLabelEnum
290 private Either<GraphVertex, StorageOperationStatus> createModelEdge(final GraphVertex previousToscaElement,
291 final GraphVertex nextToscaElement, GraphVertex user,
292 final GraphVertex createdToscaElementVertex,
293 final EdgeLabelEnum edgeLabelEnum) {
294 Either<GraphVertex, StorageOperationStatus> result = null;
295 final Either<GraphVertex, JanusGraphOperationStatus> modelElementVertexResponse = janusGraphDao
296 .getParentVertex(previousToscaElement, edgeLabelEnum, JsonParseFlagEnum.NoParse);
297 if (modelElementVertexResponse.isLeft()) {
298 final JanusGraphOperationStatus status = janusGraphDao
299 .createEdge(modelElementVertexResponse.left().value().getVertex(), createdToscaElementVertex.getVertex(), edgeLabelEnum,
301 if (JanusGraphOperationStatus.OK != status) {
302 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG,
303 FAILED_TO_CREATE_EDGE_WITH_LABEL_FROM_USER_VERTEX_TO_TOSCA_ELEMENT_VERTEX_ON_GRAPH_STATUS_IS, edgeLabelEnum,
304 user.getUniqueId(), nextToscaElement.getMetadataProperty(GraphPropertyEnum.NORMALIZED_NAME), status);
305 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
311 protected JanusGraphOperationStatus setLastModifierFromGraph(GraphVertex componentV, ToscaElement toscaElement) {
312 Either<GraphVertex, JanusGraphOperationStatus> parentVertex = janusGraphDao
313 .getParentVertex(componentV, EdgeLabelEnum.LAST_MODIFIER, JsonParseFlagEnum.NoParse);
314 if (parentVertex.isRight()) {
315 log.debug("Failed to fetch last modifier for tosca element with id {} error {}", componentV.getUniqueId(), parentVertex.right().value());
316 return parentVertex.right().value();
318 GraphVertex userV = parentVertex.left().value();
319 String userId = (String) userV.getMetadataProperty(GraphPropertyEnum.USERID);
320 toscaElement.setLastUpdaterUserId(userId);
321 toscaElement.setLastUpdaterFullName(buildFullName(userV));
322 return JanusGraphOperationStatus.OK;
325 public String buildFullName(GraphVertex userV) {
326 String fullName = (String) userV.getMetadataProperty(GraphPropertyEnum.FIRST_NAME);
327 if (fullName == null) {
330 fullName = fullName + " ";
332 String lastName = (String) userV.getMetadataProperty(GraphPropertyEnum.LAST_NAME);
333 if (lastName != null) {
334 fullName += lastName;
339 protected JanusGraphOperationStatus setCreatorFromGraph(GraphVertex componentV, ToscaElement toscaElement) {
340 Either<GraphVertex, JanusGraphOperationStatus> parentVertex = janusGraphDao
341 .getParentVertex(componentV, EdgeLabelEnum.CREATOR, JsonParseFlagEnum.NoParse);
342 if (parentVertex.isRight()) {
343 log.debug("Failed to fetch creator for tosca element with id {} error {}", componentV.getUniqueId(), parentVertex.right().value());
344 return parentVertex.right().value();
346 GraphVertex userV = parentVertex.left().value();
347 String creatorUserId = (String) userV.getMetadataProperty(GraphPropertyEnum.USERID);
348 toscaElement.setCreatorUserId(creatorUserId);
349 toscaElement.setCreatorFullName(buildFullName(userV));
350 return JanusGraphOperationStatus.OK;
353 protected <T extends ToscaElement> T getResourceMetaDataFromResource(T toscaElement) {
354 if (toscaElement.getNormalizedName() == null || toscaElement.getNormalizedName().isEmpty()) {
355 toscaElement.setNormalizedName(ValidationUtils.normaliseComponentName(toscaElement.getName()));
357 if (toscaElement.getSystemName() == null || toscaElement.getSystemName().isEmpty()) {
358 toscaElement.setSystemName(ValidationUtils.convertToSystemName(toscaElement.getName()));
360 LifecycleStateEnum lifecycleStateEnum = toscaElement.getLifecycleState();
361 if (lifecycleStateEnum == null) {
362 toscaElement.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
364 long currentDate = System.currentTimeMillis();
365 if (toscaElement.getCreationDate() == null) {
366 toscaElement.setCreationDate(currentDate);
368 toscaElement.setLastUpdateDate(currentDate);
372 protected void fillCommonMetadata(GraphVertex nodeTypeVertex, ToscaElement toscaElement) {
373 if (toscaElement.isHighestVersion() == null) {
374 toscaElement.setHighestVersion(true);
376 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.IS_DELETED, toscaElement.getMetadataValue(JsonPresentationFields.IS_DELETED));
377 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.IS_HIGHEST_VERSION,
378 toscaElement.getMetadataValueOrDefault(JsonPresentationFields.HIGHEST_VERSION, Boolean.TRUE));
379 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.STATE, toscaElement.getMetadataValue(JsonPresentationFields.LIFECYCLE_STATE));
380 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.RESOURCE_TYPE, toscaElement.getMetadataValue(JsonPresentationFields.RESOURCE_TYPE));
381 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.VERSION, toscaElement.getMetadataValue(JsonPresentationFields.VERSION));
382 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.NORMALIZED_NAME, toscaElement.getMetadataValue(JsonPresentationFields.NORMALIZED_NAME));
383 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.UNIQUE_ID, toscaElement.getMetadataValue(JsonPresentationFields.UNIQUE_ID));
385 .addMetadataProperty(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaElement.getMetadataValue(JsonPresentationFields.TOSCA_RESOURCE_NAME));
386 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.UUID, toscaElement.getMetadataValue(JsonPresentationFields.UUID));
387 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.IS_ABSTRACT, toscaElement.getMetadataValue(JsonPresentationFields.IS_ABSTRACT));
388 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.INVARIANT_UUID, toscaElement.getMetadataValue(JsonPresentationFields.INVARIANT_UUID));
389 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.NAME, toscaElement.getMetadataValue(JsonPresentationFields.NAME));
390 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.SYSTEM_NAME, toscaElement.getMetadataValue(JsonPresentationFields.SYSTEM_NAME));
391 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.IS_ARCHIVED, toscaElement.getMetadataValue(JsonPresentationFields.IS_ARCHIVED));
392 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.ARCHIVE_TIME, toscaElement.getMetadataValue(JsonPresentationFields.ARCHIVE_TIME));
393 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.IS_VSP_ARCHIVED, toscaElement.getMetadataValue(JsonPresentationFields.IS_VSP_ARCHIVED));
394 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.MODEL, toscaElement.getMetadataValue(JsonPresentationFields.MODEL));
395 toscaElement.getMetadata().entrySet().stream().filter(e -> e.getValue() != null)
396 .forEach(e -> nodeTypeVertex.setJsonMetadataField(e.getKey(), e.getValue()));
397 nodeTypeVertex.setUniqueId(toscaElement.getUniqueId());
398 nodeTypeVertex.setType(toscaElement.getComponentType());
399 final String toscaVersion = toscaElement.getToscaVersion();
400 if (toscaVersion != null) {
401 nodeTypeVertex.setJsonMetadataField(JsonPresentationFields.TOSCA_DEFINITIONS_VERSION, toscaVersion);
403 final Map<String, DataTypeDataDefinition> dataTypes = toscaElement.getDataTypes();
404 if (MapUtils.isNotEmpty(dataTypes)) {
405 nodeTypeVertex.setJsonMetadataField(JsonPresentationFields.DATA_TYPES, dataTypes);
409 protected StorageOperationStatus assosiateToUsers(GraphVertex nodeTypeVertex, ToscaElement toscaElement) {
411 String userId = toscaElement.getCreatorUserId();
412 Either<GraphVertex, JanusGraphOperationStatus> findUser = findUserVertex(userId);
413 if (findUser.isRight()) {
414 JanusGraphOperationStatus status = findUser.right().value();
415 log.error(CANNOT_FIND_USER_IN_THE_GRAPH_STATUS_IS, userId, status);
416 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status);
418 GraphVertex creatorVertex = findUser.left().value();
419 GraphVertex updaterVertex = creatorVertex;
420 String updaterId = toscaElement.getLastUpdaterUserId();
421 if (updaterId != null && !updaterId.equals(userId)) {
422 findUser = findUserVertex(updaterId);
423 if (findUser.isRight()) {
424 JanusGraphOperationStatus status = findUser.right().value();
425 log.error(CANNOT_FIND_USER_IN_THE_GRAPH_STATUS_IS, userId, status);
426 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status);
428 updaterVertex = findUser.left().value();
431 Map<EdgePropertyEnum, Object> props = new EnumMap<>(EdgePropertyEnum.class);
432 props.put(EdgePropertyEnum.STATE, (String) toscaElement.getMetadataValue(JsonPresentationFields.LIFECYCLE_STATE));
433 JanusGraphOperationStatus result = janusGraphDao.createEdge(updaterVertex, nodeTypeVertex, EdgeLabelEnum.STATE, props);
434 log.debug("After associating user {} to resource {}. Edge type is {}", updaterVertex, nodeTypeVertex.getUniqueId(), EdgeLabelEnum.STATE);
435 if (JanusGraphOperationStatus.OK != result) {
436 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(result);
438 result = janusGraphDao.createEdge(updaterVertex, nodeTypeVertex, EdgeLabelEnum.LAST_MODIFIER, null);
439 log.debug("After associating user {} to resource {}. Edge type is {}", updaterVertex, nodeTypeVertex.getUniqueId(),
440 EdgeLabelEnum.LAST_MODIFIER);
441 if (!result.equals(JanusGraphOperationStatus.OK)) {
442 log.error("Failed to associate user {} to resource {}. Edge type is {}", updaterVertex, nodeTypeVertex.getUniqueId(),
443 EdgeLabelEnum.LAST_MODIFIER);
444 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(result);
446 toscaElement.setLastUpdaterUserId(toscaElement.getCreatorUserId());
447 toscaElement.setLastUpdaterFullName(toscaElement.getCreatorFullName());
448 result = janusGraphDao.createEdge(creatorVertex, nodeTypeVertex, EdgeLabelEnum.CREATOR, null);
449 log.debug("After associating user {} to resource {}. Edge type is {} ", creatorVertex, nodeTypeVertex.getUniqueId(), EdgeLabelEnum.CREATOR);
450 if (!result.equals(JanusGraphOperationStatus.OK)) {
451 log.error("Failed to associate user {} to resource {}. Edge type is {} ", creatorVertex, nodeTypeVertex.getUniqueId(),
452 EdgeLabelEnum.CREATOR);
453 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(result);
455 return StorageOperationStatus.OK;
458 protected StorageOperationStatus assosiateResourceMetadataToCategory(GraphVertex nodeTypeVertex, ToscaElement nodeType) {
459 String subcategoryName = nodeType.getCategories().get(0).getSubcategories().get(0).getName();
460 String categoryName = nodeType.getCategories().get(0).getName();
461 Either<GraphVertex, StorageOperationStatus> getCategoryVertex = getResourceCategoryVertex(nodeType.getUniqueId(), subcategoryName,
463 if (getCategoryVertex.isRight()) {
464 return getCategoryVertex.right().value();
466 GraphVertex subCategoryV = getCategoryVertex.left().value();
467 JanusGraphOperationStatus createEdge = janusGraphDao.createEdge(nodeTypeVertex, subCategoryV, EdgeLabelEnum.CATEGORY, new HashMap<>());
468 if (createEdge != JanusGraphOperationStatus.OK) {
469 log.trace("Failed to associate resource {} to category {} with id {}", nodeType.getUniqueId(), subcategoryName,
470 subCategoryV.getUniqueId());
471 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(createEdge);
473 return StorageOperationStatus.OK;
476 protected StorageOperationStatus associateComponentToModel(final GraphVertex nodeTypeVertex, final ToscaElement nodeType,
477 final EdgeLabelEnum edgeLabelEnum) {
478 if (nodeType.getMetadataValue(JsonPresentationFields.MODEL) == null) {
479 return StorageOperationStatus.OK;
481 final String model = ((String) nodeType.getMetadataValue(JsonPresentationFields.MODEL));
482 final JanusGraphOperationStatus createEdge = janusGraphDao.createEdge(getModelVertex(model), nodeTypeVertex, edgeLabelEnum, new HashMap<>());
483 if (createEdge != JanusGraphOperationStatus.OK) {
484 log.trace("Failed to associate resource {} to model {}", nodeType.getUniqueId(), model);
485 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(createEdge);
487 return StorageOperationStatus.OK;
490 private GraphVertex getModelVertex(final String modelName) {
491 log.debug("getModelVertex: fetching model {}", modelName);
492 final Optional<GraphVertex> modelVertexByNameOptional = modelOperation.findModelVertexByName(modelName);
493 if (modelVertexByNameOptional.isEmpty()) {
494 throw ModelOperationExceptionSupplier.invalidModel(modelName).get();
496 return modelVertexByNameOptional.get();
499 protected Either<GraphVertex, StorageOperationStatus> getResourceCategoryVertex(String elementId, String subcategoryName, String categoryName) {
500 Either<GraphVertex, StorageOperationStatus> category = categoryOperation.getCategory(categoryName, VertexTypeEnum.RESOURCE_CATEGORY);
501 if (category.isRight()) {
502 log.trace("Failed to fetch category {} for resource {} error {}", categoryName, elementId, category.right().value());
503 return Either.right(category.right().value());
505 GraphVertex categoryV = category.left().value();
506 if (subcategoryName != null) {
507 Either<GraphVertex, StorageOperationStatus> subCategory = categoryOperation.getSubCategoryForCategory(categoryV, subcategoryName);
508 if (subCategory.isRight()) {
509 log.trace("Failed to fetch subcategory {} of category for resource {} error {}", subcategoryName, categoryName, elementId,
510 subCategory.right().value());
511 return Either.right(subCategory.right().value());
513 GraphVertex subCategoryV = subCategory.left().value();
514 return Either.left(subCategoryV);
516 return Either.left(categoryV);
519 private StorageOperationStatus associateArtifactsToResource(GraphVertex nodeTypeVertex, ToscaElement toscaElement) {
520 Map<String, ArtifactDataDefinition> artifacts = toscaElement.getArtifacts();
521 Either<GraphVertex, StorageOperationStatus> status;
522 if (artifacts != null) {
523 artifacts.values().stream().filter(a -> a.getUniqueId() == null).forEach(a -> {
524 String uniqueId = UniqueIdBuilder
525 .buildPropertyUniqueId(nodeTypeVertex.getUniqueId().toLowerCase(), a.getArtifactLabel().toLowerCase());
526 a.setUniqueId(uniqueId);
528 status = associateElementToData(nodeTypeVertex, VertexTypeEnum.ARTIFACTS, EdgeLabelEnum.ARTIFACTS, artifacts);
529 if (status.isRight()) {
530 return status.right().value();
533 Map<String, ArtifactDataDefinition> toscaArtifacts = toscaElement.getToscaArtifacts();
534 if (toscaArtifacts != null) {
535 toscaArtifacts.values().stream().filter(a -> a.getUniqueId() == null).forEach(a -> {
536 String uniqueId = UniqueIdBuilder
537 .buildPropertyUniqueId(nodeTypeVertex.getUniqueId().toLowerCase(), a.getArtifactLabel().toLowerCase());
538 a.setUniqueId(uniqueId);
540 status = associateElementToData(nodeTypeVertex, VertexTypeEnum.TOSCA_ARTIFACTS, EdgeLabelEnum.TOSCA_ARTIFACTS, toscaArtifacts);
541 if (status.isRight()) {
542 return status.right().value();
545 Map<String, ArtifactDataDefinition> deploymentArtifacts = toscaElement.getDeploymentArtifacts();
546 if (deploymentArtifacts != null) {
547 deploymentArtifacts.values().stream().filter(a -> a.getUniqueId() == null).forEach(a -> {
548 String uniqueId = UniqueIdBuilder
549 .buildPropertyUniqueId(nodeTypeVertex.getUniqueId().toLowerCase(), a.getArtifactLabel().toLowerCase());
550 a.setUniqueId(uniqueId);
552 status = associateElementToData(nodeTypeVertex, VertexTypeEnum.DEPLOYMENT_ARTIFACTS, EdgeLabelEnum.DEPLOYMENT_ARTIFACTS,
553 deploymentArtifacts);
554 if (status.isRight()) {
555 return status.right().value();
558 return StorageOperationStatus.OK;
561 protected JanusGraphOperationStatus disassociateAndDeleteCommonElements(GraphVertex toscaElementVertex) {
562 JanusGraphOperationStatus status = janusGraphDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.ARTIFACTS);
563 if (status != JanusGraphOperationStatus.OK) {
564 log.debug("Failed to disaccociate artifact for {} error {}", toscaElementVertex.getUniqueId(), status);
567 status = janusGraphDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.TOSCA_ARTIFACTS);
568 if (status != JanusGraphOperationStatus.OK) {
569 log.debug("Failed to disaccociate tosca artifact for {} error {}", toscaElementVertex.getUniqueId(), status);
572 status = janusGraphDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.DEPLOYMENT_ARTIFACTS);
573 if (status != JanusGraphOperationStatus.OK) {
574 log.debug("Failed to deployment artifact for {} error {}", toscaElementVertex.getUniqueId(), status);
577 status = janusGraphDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.PROPERTIES);
578 if (status != JanusGraphOperationStatus.OK) {
579 log.debug("Failed to disaccociate properties for {} error {}", toscaElementVertex.getUniqueId(), status);
582 status = janusGraphDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.ATTRIBUTES);
583 if (status != JanusGraphOperationStatus.OK) {
584 log.debug("Failed to disaccociate attributes for {} error {}", toscaElementVertex.getUniqueId(), status);
587 status = janusGraphDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.ADDITIONAL_INFORMATION);
588 if (status != JanusGraphOperationStatus.OK) {
589 log.debug("Failed to disaccociate additional information for {} error {}", toscaElementVertex.getUniqueId(), status);
592 status = janusGraphDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.CAPABILITIES);
593 if (status != JanusGraphOperationStatus.OK) {
594 log.debug("Failed to disaccociate capabilities for {} error {}", toscaElementVertex.getUniqueId(), status);
597 status = janusGraphDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.REQUIREMENTS);
598 if (status != JanusGraphOperationStatus.OK) {
599 log.debug("Failed to disaccociate requirements for {} error {}", toscaElementVertex.getUniqueId(), status);
602 status = janusGraphDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.FORWARDING_PATH);
603 if (status != JanusGraphOperationStatus.OK) {
604 log.debug("Failed to disaccociate requirements for {} error {}", toscaElementVertex.getUniqueId(), status);
607 return JanusGraphOperationStatus.OK;
610 protected StorageOperationStatus assosiateCommonForToscaElement(GraphVertex nodeTypeVertex, ToscaElement toscaElement) {
611 return assosiateCommonForToscaElement(nodeTypeVertex, toscaElement, null);
614 protected StorageOperationStatus assosiateCommonForToscaElement(GraphVertex nodeTypeVertex, ToscaElement toscaElement,
615 List<GraphVertex> derivedResources) {
616 StorageOperationStatus associateUsers = assosiateToUsers(nodeTypeVertex, toscaElement);
617 if (associateUsers != StorageOperationStatus.OK) {
618 return associateUsers;
620 StorageOperationStatus associateArtifacts = associateArtifactsToResource(nodeTypeVertex, toscaElement);
621 if (associateArtifacts != StorageOperationStatus.OK) {
622 return associateArtifacts;
624 StorageOperationStatus associateProperties = associatePropertiesToResource(nodeTypeVertex, toscaElement, derivedResources);
625 if (associateProperties != StorageOperationStatus.OK) {
626 return associateProperties;
628 StorageOperationStatus associateAdditionaInfo = associateAdditionalInfoToResource(nodeTypeVertex, toscaElement);
629 if (associateAdditionaInfo != StorageOperationStatus.OK) {
630 return associateAdditionaInfo;
632 if (needConnectToCatalog(toscaElement)) {
633 StorageOperationStatus associateToCatalog = associateToCatalogRoot(nodeTypeVertex);
634 if (associateToCatalog != StorageOperationStatus.OK) {
635 return associateToCatalog;
638 return StorageOperationStatus.OK;
641 private boolean needConnectToCatalog(ToscaElement toscaElement) {
642 Boolean isAbstract = (Boolean) toscaElement.getMetadataValue(JsonPresentationFields.IS_ABSTRACT);
643 if (isAbstract != null && isAbstract) {
646 return toscaElement.isHighestVersion();
649 private StorageOperationStatus associateToCatalogRoot(GraphVertex nodeTypeVertex) {
650 Either<GraphVertex, JanusGraphOperationStatus> catalog = janusGraphDao.getVertexByLabel(VertexTypeEnum.CATALOG_ROOT);
651 if (catalog.isRight()) {
652 log.debug("Failed to fetch catalog vertex. error {}", catalog.right().value());
653 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(catalog.right().value());
655 JanusGraphOperationStatus createEdge = janusGraphDao.createEdge(catalog.left().value(), nodeTypeVertex, EdgeLabelEnum.CATALOG_ELEMENT, null);
656 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(createEdge);
659 protected StorageOperationStatus associatePropertiesToResource(GraphVertex nodeTypeVertex, ToscaElement nodeType,
660 List<GraphVertex> derivedResources) {
661 // Note : currently only one derived supported!!!!
662 Either<Map<String, PropertyDataDefinition>, StorageOperationStatus> dataFromDerived = getDataFromDerived(derivedResources,
663 EdgeLabelEnum.PROPERTIES);
664 if (dataFromDerived.isRight()) {
665 return dataFromDerived.right().value();
667 Map<String, PropertyDataDefinition> propertiesAll = dataFromDerived.left().value();
668 Map<String, PropertyDataDefinition> properties = nodeType.getProperties();
669 if (properties != null) {
670 properties.values().stream().filter(p -> p.getUniqueId() == null).forEach(p -> {
671 String uid = UniqueIdBuilder.buildPropertyUniqueId(nodeTypeVertex.getUniqueId(), p.getName());
674 Either<Map<String, PropertyDataDefinition>, String> eitherMerged = ToscaDataDefinition.mergeDataMaps(propertiesAll, properties);
675 if (eitherMerged.isRight()) {
676 // TODO re-factor error handling - moving BL to operation resulted in loss of info about the invalid property
677 log.debug("property {} cannot be overriden", eitherMerged.right().value());
678 return StorageOperationStatus.INVALID_PROPERTY;
681 if (!propertiesAll.isEmpty()) {
682 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = associateElementToData(nodeTypeVertex, VertexTypeEnum.PROPERTIES,
683 EdgeLabelEnum.PROPERTIES, propertiesAll);
684 if (assosiateElementToData.isRight()) {
685 return assosiateElementToData.right().value();
688 return StorageOperationStatus.OK;
691 private StorageOperationStatus associateAdditionalInfoToResource(GraphVertex nodeTypeVertex, ToscaElement nodeType) {
692 Map<String, AdditionalInfoParameterDataDefinition> additionalInformation = nodeType.getAdditionalInformation();
693 if (additionalInformation != null) {
694 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = associateElementToData(nodeTypeVertex,
695 VertexTypeEnum.ADDITIONAL_INFORMATION, EdgeLabelEnum.ADDITIONAL_INFORMATION, additionalInformation);
696 if (assosiateElementToData.isRight()) {
697 return assosiateElementToData.right().value();
700 return StorageOperationStatus.OK;
703 protected <T extends ToscaDataDefinition> Either<Map<String, T>, StorageOperationStatus> getDataFromDerived(List<GraphVertex> derivedResources,
704 EdgeLabelEnum edge) {
705 Map<String, T> propertiesAll = new HashMap<>();
706 if (derivedResources != null && !derivedResources.isEmpty()) {
707 for (GraphVertex derived : derivedResources) {
708 Either<List<GraphVertex>, JanusGraphOperationStatus> derivedProperties = janusGraphDao
709 .getChildrenVertices(derived, edge, JsonParseFlagEnum.ParseJson);
710 if (derivedProperties.isRight()) {
711 if (derivedProperties.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
712 log.debug("Failed to get properties for derived from {} error {}", derived.getUniqueId(), derivedProperties.right().value());
713 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(derivedProperties.right().value()));
718 List<GraphVertex> propList = derivedProperties.left().value();
719 for (GraphVertex propV : propList) {
720 Map<String, T> propertiesFromDerived = (Map<String, T>) propV.getJson();
721 if (propertiesFromDerived != null) {
722 propertiesFromDerived.entrySet().forEach(x -> x.getValue().setOwnerIdIfEmpty(derived.getUniqueId()));
723 propertiesAll.putAll(propertiesFromDerived);
728 return Either.left(propertiesAll);
731 protected JanusGraphOperationStatus setArtifactsFromGraph(GraphVertex componentV, ToscaElement toscaElement) {
732 Either<Map<String, ArtifactDataDefinition>, JanusGraphOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.ARTIFACTS);
733 if (result.isLeft()) {
734 toscaElement.setArtifacts(result.left().value());
736 if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
737 return result.right().value();
740 result = getDataFromGraph(componentV, EdgeLabelEnum.DEPLOYMENT_ARTIFACTS);
741 if (result.isLeft()) {
742 toscaElement.setDeploymentArtifacts(result.left().value());
744 if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
745 return result.right().value();
748 result = getDataFromGraph(componentV, EdgeLabelEnum.TOSCA_ARTIFACTS);
749 if (result.isLeft()) {
750 toscaElement.setToscaArtifacts(result.left().value());
752 if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
753 return result.right().value();
756 return JanusGraphOperationStatus.OK;
759 protected JanusGraphOperationStatus setAllVersions(GraphVertex componentV, ToscaElement toscaElement) {
760 Map<String, String> allVersion = new HashMap<>();
761 allVersion.put((String) componentV.getMetadataProperty(GraphPropertyEnum.VERSION), componentV.getUniqueId());
762 ArrayList<GraphVertex> allChildrenAndParants = new ArrayList<>();
763 Either<GraphVertex, JanusGraphOperationStatus> childResourceRes = janusGraphDao
764 .getChildVertex(componentV, EdgeLabelEnum.VERSION, JsonParseFlagEnum.NoParse);
765 while (childResourceRes.isLeft()) {
766 GraphVertex child = childResourceRes.left().value();
767 allChildrenAndParants.add(child);
768 childResourceRes = janusGraphDao.getChildVertex(child, EdgeLabelEnum.VERSION, JsonParseFlagEnum.NoParse);
770 JanusGraphOperationStatus operationStatus = childResourceRes.right().value();
771 if (operationStatus != JanusGraphOperationStatus.NOT_FOUND) {
772 return operationStatus;
774 Either<GraphVertex, JanusGraphOperationStatus> parentResourceRes = janusGraphDao
775 .getParentVertex(componentV, EdgeLabelEnum.VERSION, JsonParseFlagEnum.NoParse);
776 while (parentResourceRes.isLeft()) {
777 GraphVertex parent = parentResourceRes.left().value();
778 allChildrenAndParants.add(parent);
779 parentResourceRes = janusGraphDao.getParentVertex(parent, EdgeLabelEnum.VERSION, JsonParseFlagEnum.NoParse);
781 operationStatus = parentResourceRes.right().value();
782 if (operationStatus != JanusGraphOperationStatus.NOT_FOUND) {
783 return operationStatus;
785 allChildrenAndParants.stream().filter(vertex -> {
786 Boolean isDeleted = (Boolean) vertex.getMetadataProperty(GraphPropertyEnum.IS_DELETED);
787 return (isDeleted == null || !isDeleted);
788 }).forEach(vertex -> allVersion.put((String) vertex.getMetadataProperty(GraphPropertyEnum.VERSION), vertex.getUniqueId()));
789 toscaElement.setAllVersions(allVersion);
790 return JanusGraphOperationStatus.OK;
795 protected <T extends ToscaElement> Either<List<T>, StorageOperationStatus> getFollowedComponent(String userId,
796 Set<LifecycleStateEnum> lifecycleStates,
797 Set<LifecycleStateEnum> lastStateStates,
798 ComponentTypeEnum neededType) {
799 Map<GraphPropertyEnum, Object> props = null;
800 if (userId != null) {
801 props = new EnumMap<>(GraphPropertyEnum.class);
802 // for Designer retrieve specific user
803 props.put(GraphPropertyEnum.USERID, userId);
805 // in case of user id == null -> get all users by label
807 // for Tester and Admin retrieve all users
808 Either<List<GraphVertex>, JanusGraphOperationStatus> usersByCriteria = janusGraphDao
809 .getByCriteria(VertexTypeEnum.USER, props, JsonParseFlagEnum.NoParse);
810 if (usersByCriteria.isRight()) {
811 log.debug("Failed to fetch users by criteria {} error {}", props, usersByCriteria.right().value());
812 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(usersByCriteria.right().value()));
814 GraphVertex userV = usersByCriteria.left().value().get(0);
815 List<T> components = new ArrayList<>();
816 List<T> componentsPerUser;
817 final Set<String> ids = new HashSet<>();
818 Either<List<GraphVertex>, JanusGraphOperationStatus> childrenVertecies = janusGraphDao
819 .getChildrenVertices(userV, EdgeLabelEnum.STATE, JsonParseFlagEnum.NoParse);
820 if (childrenVertecies.isRight() && childrenVertecies.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
821 log.debug("Failed to fetch children vertices for user {} by edge {} error {}", userV.getMetadataProperty(GraphPropertyEnum.USERID),
822 EdgeLabelEnum.STATE, childrenVertecies.right().value());
823 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(childrenVertecies.right().value()));
825 // get all resource with current state
826 if (childrenVertecies.isLeft()) {
827 componentsPerUser = fetchComponents(userId, lifecycleStates, childrenVertecies.left().value(), neededType, EdgeLabelEnum.STATE);
828 if (componentsPerUser != null) {
829 for (T comp : componentsPerUser) {
830 ids.add(comp.getUniqueId());
831 components.add(comp);
834 if (lastStateStates != null && !lastStateStates.isEmpty()) {
835 // get all resource with last state
836 childrenVertecies = janusGraphDao.getChildrenVertices(userV, EdgeLabelEnum.LAST_STATE, JsonParseFlagEnum.NoParse);
837 if (childrenVertecies.isRight() && childrenVertecies.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
838 log.debug("Failed to fetch children vertices for user {} by edge {} error {}",
839 userV.getMetadataProperty(GraphPropertyEnum.USERID), EdgeLabelEnum.LAST_STATE, childrenVertecies.right().value());
840 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(childrenVertecies.right().value()));
842 if (childrenVertecies.isLeft()) {
844 componentsPerUser = fetchComponents(userId, lastStateStates, childrenVertecies.left().value(), neededType,
845 EdgeLabelEnum.LAST_STATE);
846 if (componentsPerUser != null) {
847 for (T comp : componentsPerUser) {
849 if (ids.contains(comp.getUniqueId())) {
853 components.add(comp);
860 return Either.left(components);
863 private <T extends ToscaElement> List<T> fetchComponents(String userId, Set<LifecycleStateEnum> lifecycleStates, List<GraphVertex> vertices,
864 ComponentTypeEnum neededType, EdgeLabelEnum edgelabel) {
865 List<T> components = new ArrayList<>();
866 for (GraphVertex node : vertices) {
867 Iterator<Edge> edges = node.getVertex().edges(Direction.IN, edgelabel.name());
868 while (edges.hasNext()) {
869 Edge edge = edges.next();
870 String stateStr = (String) janusGraphDao.getProperty(edge, EdgePropertyEnum.STATE);
871 LifecycleStateEnum nodeState = LifecycleStateEnum.findState(stateStr);
872 if (nodeState == null) {
873 log.debug("no supported STATE {} for element {}", stateStr, node.getUniqueId());
876 //get user from edge and compare to user from followed request
877 JanusGraphVertex userVertex = (JanusGraphVertex) edge.outVertex();
878 String userIdFromEdge = (String) janusGraphDao.getProperty(userVertex, GraphPropertyEnum.USERID.getProperty());
879 if (lifecycleStates != null && lifecycleStates.contains(nodeState) && (userIdFromEdge.equals(userId))) {
880 Boolean isDeleted = (Boolean) node.getMetadataProperty(GraphPropertyEnum.IS_DELETED);
881 Boolean isArchived = (Boolean) node.getMetadataProperty(GraphPropertyEnum.IS_ARCHIVED);
882 if (isDeleted != null && isDeleted || isArchived != null && isArchived) {
883 log.trace("Deleted/Archived element {}, discard", node.getUniqueId());
886 Boolean isHighest = (Boolean) node.getMetadataProperty(GraphPropertyEnum.IS_HIGHEST_VERSION);
888 ComponentTypeEnum componentType = node.getType();
889 // get only latest versions
890 if (componentType == null) {
891 log.debug("No supported type {} for vertex {}", componentType, node.getUniqueId());
894 if (neededType == componentType) {
895 switch (componentType) {
898 handleNode(components, node, componentType);
901 Boolean isAbtract = (Boolean) node.getMetadataProperty(GraphPropertyEnum.IS_ABSTRACT);
902 if (isAbtract == null || !isAbtract) {
903 handleNode(components, node, componentType);
907 log.debug("not supported node type {}", componentType);
921 protected <T extends ToscaElement> void handleNode(List<T> components, GraphVertex vertexComponent, ComponentTypeEnum nodeType) {
922 Either<T, StorageOperationStatus> component = getLightComponent(vertexComponent, nodeType, new ComponentParametersView(true));
923 if (component.isRight()) {
924 log.debug("Failed to get component for id = {} error : {} skip resource", vertexComponent.getUniqueId(), component.right().value());
926 components.add(component.left().value());
930 protected <T extends ToscaElement> Either<T, StorageOperationStatus> getLightComponent(String componentUid, ComponentTypeEnum nodeType,
931 ComponentParametersView parametersFilter) {
932 Either<GraphVertex, JanusGraphOperationStatus> getVertexRes = janusGraphDao.getVertexById(componentUid);
933 if (getVertexRes.isRight()) {
934 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexRes.right().value()));
936 return getLightComponent(getVertexRes.left().value(), nodeType, parametersFilter);
939 protected <T extends ToscaElement> Either<T, StorageOperationStatus> getLightComponent(GraphVertex vertexComponent, ComponentTypeEnum nodeType,
940 ComponentParametersView parametersFilter) {
941 log.trace("Starting to build light component of type {}, id {}", nodeType, vertexComponent.getUniqueId());
942 janusGraphDao.parseVertexProperties(vertexComponent, JsonParseFlagEnum.ParseMetadata);
943 T toscaElement = convertToComponent(vertexComponent);
944 JanusGraphOperationStatus status = setCreatorFromGraph(vertexComponent, toscaElement);
945 if (status != JanusGraphOperationStatus.OK) {
946 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
948 status = setLastModifierFromGraph(vertexComponent, toscaElement);
949 if (status != JanusGraphOperationStatus.OK) {
950 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
952 status = setCategoriesFromGraph(vertexComponent, toscaElement);
953 if (status != JanusGraphOperationStatus.OK) {
954 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
956 if (!parametersFilter.isIgnoreAllVersions()) {
957 status = setAllVersions(vertexComponent, toscaElement);
958 if (status != JanusGraphOperationStatus.OK) {
959 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
962 if (!parametersFilter.isIgnoreCapabilities()) {
963 status = setCapabilitiesFromGraph(vertexComponent, toscaElement);
964 if (status != JanusGraphOperationStatus.OK) {
965 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
968 if (!parametersFilter.isIgnoreRequirements()) {
969 status = setRequirementsFromGraph(vertexComponent, toscaElement);
970 if (status != JanusGraphOperationStatus.OK) {
971 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
974 log.debug("Ended to build light component of type {}, id {}", nodeType, vertexComponent.getUniqueId());
975 return Either.left(toscaElement);
978 @SuppressWarnings("unchecked")
979 protected <T extends ToscaElement> T convertToComponent(GraphVertex componentV) {
980 ToscaElement toscaElement = null;
981 VertexTypeEnum label = componentV.getLabel();
984 toscaElement = new NodeType();
986 case TOPOLOGY_TEMPLATE:
987 toscaElement = new TopologyTemplate();
990 log.debug("Not supported tosca type {}", label);
993 if (toscaElement != null) {
994 final Map<String, Object> jsonMetada = componentV.getMetadataJson();
995 if (MapUtils.isNotEmpty(jsonMetada)) {
996 toscaElement.setMetadata(jsonMetada);
997 final Object toscaVersion = jsonMetada.get(ToscaTagNamesEnum.TOSCA_VERSION.getElementName());
998 if (toscaVersion != null) {
999 toscaElement.setToscaVersion((String) toscaVersion);
1001 final Object dataTypes = jsonMetada.get(ToscaTagNamesEnum.DATA_TYPES.getElementName());
1002 if (dataTypes != null) {
1003 final Map<String, DataTypeDataDefinition> dataTypeDefinitionMap = new HashMap<>();
1004 final Map<String, Object> toscaAttributes = (Map<String, Object>) dataTypes;
1005 for (final Entry<String, Object> attributeNameValue : toscaAttributes.entrySet()) {
1006 final Object value = attributeNameValue.getValue();
1007 final String key = attributeNameValue.getKey();
1008 if (value instanceof Map) {
1009 final DataTypeDefinition dataTypeDefinition = createDataTypeDefinitionWithName(attributeNameValue);
1010 dataTypeDefinitionMap.put(dataTypeDefinition.getName(), dataTypeDefinition);
1012 dataTypeDefinitionMap.put(key, createDataType(String.valueOf(value)));
1015 toscaElement.setDataTypes(dataTypeDefinitionMap);
1019 return (T) toscaElement;
1022 protected JanusGraphOperationStatus setResourceCategoryFromGraphV(Vertex vertex, CatalogComponent catalogComponent) {
1023 List<CategoryDefinition> categories = new ArrayList<>();
1024 SubCategoryDefinition subcategory;
1025 Either<Vertex, JanusGraphOperationStatus> childVertex = janusGraphDao.getChildVertex(vertex, EdgeLabelEnum.CATEGORY);
1026 if (childVertex.isRight()) {
1027 log.debug(FAILED_TO_FETCH_FOR_TOSCA_ELEMENT_WITH_ID_ERROR, EdgeLabelEnum.CATEGORY, catalogComponent.getUniqueId(),
1028 childVertex.right().value());
1029 return childVertex.right().value();
1031 Vertex subCategoryV = childVertex.left().value();
1032 String subCategoryNormalizedName = (String) subCategoryV.property(GraphPropertyEnum.NORMALIZED_NAME.getProperty()).value();
1033 catalogComponent.setSubCategoryNormalizedName(subCategoryNormalizedName);
1034 subcategory = new SubCategoryDefinition();
1035 subcategory.setUniqueId((String) subCategoryV.property(GraphPropertyEnum.UNIQUE_ID.getProperty()).value());
1036 subcategory.setNormalizedName(subCategoryNormalizedName);
1037 subcategory.setName((String) subCategoryV.property(GraphPropertyEnum.NAME.getProperty()).value());
1038 Type listTypeSubcat = new TypeToken<List<MetadataKeyDataDefinition>>() {
1040 List<MetadataKeyDataDefinition> metadataKeys = subCategoryV.property(GraphPropertyEnum.METADATA_KEYS.getProperty()).isPresent() ? getGson()
1041 .fromJson((String) subCategoryV.property(GraphPropertyEnum.METADATA_KEYS.getProperty()).value(), listTypeSubcat)
1042 : Collections.emptyList();
1043 subcategory.setMetadataKeys(metadataKeys);
1044 Either<Vertex, JanusGraphOperationStatus> parentVertex = janusGraphDao.getParentVertex(subCategoryV, EdgeLabelEnum.SUB_CATEGORY);
1045 Vertex categoryV = parentVertex.left().value();
1046 String categoryNormalizedName = (String) categoryV.property(GraphPropertyEnum.NORMALIZED_NAME.getProperty()).value();
1047 catalogComponent.setCategoryNormalizedName(categoryNormalizedName);
1048 CategoryDefinition category = new CategoryDefinition();
1049 category.setUniqueId((String) categoryV.property(GraphPropertyEnum.UNIQUE_ID.getProperty()).value());
1050 category.setNormalizedName(categoryNormalizedName);
1051 category.setName((String) categoryV.property(GraphPropertyEnum.NAME.getProperty()).value());
1052 category.addSubCategory(subcategory);
1053 categories.add(category);
1054 catalogComponent.setCategories(categories);
1055 return JanusGraphOperationStatus.OK;
1058 protected JanusGraphOperationStatus setServiceCategoryFromGraphV(Vertex vertex, CatalogComponent catalogComponent) {
1059 List<CategoryDefinition> categories = new ArrayList<>();
1060 Either<Vertex, JanusGraphOperationStatus> childVertex = janusGraphDao.getChildVertex(vertex, EdgeLabelEnum.CATEGORY);
1061 if (childVertex.isRight()) {
1062 log.debug(FAILED_TO_FETCH_FOR_TOSCA_ELEMENT_WITH_ID_ERROR, EdgeLabelEnum.CATEGORY, catalogComponent.getUniqueId(),
1063 childVertex.right().value());
1064 return childVertex.right().value();
1066 Vertex categoryV = childVertex.left().value();
1067 String categoryNormalizedName = (String) categoryV.property(GraphPropertyEnum.NORMALIZED_NAME.getProperty()).value();
1068 catalogComponent.setCategoryNormalizedName(categoryNormalizedName);
1069 CategoryDefinition category = new CategoryDefinition();
1070 category.setUniqueId((String) categoryV.property(GraphPropertyEnum.UNIQUE_ID.getProperty()).value());
1071 category.setNormalizedName(categoryNormalizedName);
1072 category.setModels(categoryV.property(GraphPropertyEnum.MODEL.getProperty()).isPresent() ? getGson()
1073 .fromJson((String) categoryV.property(GraphPropertyEnum.MODEL.getProperty()).value(), new TypeToken<List<String>>() {
1074 }.getType()) : Collections.emptyList());
1075 category.setName((String) categoryV.property(GraphPropertyEnum.NAME.getProperty()).value());
1076 category.setUseServiceSubstitutionForNestedServices(
1077 (Boolean) categoryV.property(GraphPropertyEnum.USE_SUBSTITUTION_FOR_NESTED_SERVICES.getProperty()).orElse(false));
1078 Type listTypeCat = new TypeToken<List<MetadataKeyDataDefinition>>() {
1080 List<MetadataKeyDataDefinition> metadataKeys = categoryV.property(GraphPropertyEnum.METADATA_KEYS.getProperty()).isPresent() ? getGson()
1081 .fromJson((String) categoryV.property(GraphPropertyEnum.METADATA_KEYS.getProperty()).value(), listTypeCat) : Collections.emptyList();
1082 category.setMetadataKeys(metadataKeys);
1083 VertexProperty<Object> property = categoryV.property(GraphPropertyEnum.NOT_APPLICABLE_METADATA_KEYS.getProperty());
1084 category.setNotApplicableMetadataKeys(
1085 property.isPresent() ? getGson().fromJson((String) property.value(), new TypeToken<List<String>>() {
1086 }.getType()) : Collections.emptyList());
1087 categories.add(category);
1088 catalogComponent.setCategories(categories);
1089 return JanusGraphOperationStatus.OK;
1092 protected JanusGraphOperationStatus setResourceCategoryFromGraph(GraphVertex componentV, ToscaElement toscaElement) {
1093 List<CategoryDefinition> categories = new ArrayList<>();
1094 SubCategoryDefinition subcategory;
1095 Either<GraphVertex, JanusGraphOperationStatus> childVertex = janusGraphDao
1096 .getChildVertex(componentV, EdgeLabelEnum.CATEGORY, JsonParseFlagEnum.NoParse);
1097 if (childVertex.isRight()) {
1098 log.debug(FAILED_TO_FETCH_FOR_TOSCA_ELEMENT_WITH_ID_ERROR, EdgeLabelEnum.CATEGORY, componentV.getUniqueId(), childVertex.right().value());
1099 return childVertex.right().value();
1101 GraphVertex subCategoryV = childVertex.left().value();
1102 Map<GraphPropertyEnum, Object> metadataProperties = subCategoryV.getMetadataProperties();
1103 subcategory = new SubCategoryDefinition();
1104 subcategory.setUniqueId(subCategoryV.getUniqueId());
1105 subcategory.setNormalizedName((String) metadataProperties.get(GraphPropertyEnum.NORMALIZED_NAME));
1106 subcategory.setName((String) metadataProperties.get(GraphPropertyEnum.NAME));
1107 Type listTypeSubcat = new TypeToken<List<String>>() {
1109 List<String> iconsfromJsonSubcat = getGson().fromJson((String) metadataProperties.get(GraphPropertyEnum.ICONS), listTypeSubcat);
1110 subcategory.setIcons(iconsfromJsonSubcat);
1111 final Type metadataKeysTypeCat = new TypeToken<List<MetadataKeyDataDefinition>>() {
1113 final List<MetadataKeyDataDefinition> metadataKeysfromJsonCat = getGson()
1114 .fromJson((String) metadataProperties.get(GraphPropertyEnum.METADATA_KEYS), metadataKeysTypeCat);
1115 subcategory.setMetadataKeys(metadataKeysfromJsonCat);
1116 Either<GraphVertex, JanusGraphOperationStatus> parentVertex = janusGraphDao
1117 .getParentVertex(subCategoryV, EdgeLabelEnum.SUB_CATEGORY, JsonParseFlagEnum.NoParse);
1118 if (parentVertex.isRight()) {
1119 log.debug("failed to fetch {} for category with id {}, error {}", EdgeLabelEnum.SUB_CATEGORY, subCategoryV.getUniqueId(),
1120 parentVertex.right().value());
1121 return childVertex.right().value();
1123 GraphVertex categoryV = parentVertex.left().value();
1124 metadataProperties = categoryV.getMetadataProperties();
1125 CategoryDefinition category = new CategoryDefinition();
1126 category.setUniqueId(categoryV.getUniqueId());
1127 category.setNormalizedName((String) metadataProperties.get(GraphPropertyEnum.NORMALIZED_NAME));
1128 category.setName((String) metadataProperties.get(GraphPropertyEnum.NAME));
1129 Type listTypeCat = new TypeToken<List<String>>() {
1131 List<String> iconsfromJsonCat = getGson().fromJson((String) metadataProperties.get(GraphPropertyEnum.ICONS), listTypeCat);
1132 category.setIcons(iconsfromJsonCat);
1133 category.addSubCategory(subcategory);
1134 categories.add(category);
1135 toscaElement.setCategories(categories);
1136 return JanusGraphOperationStatus.OK;
1139 public <T extends ToscaElement> Either<T, StorageOperationStatus> updateToscaElement(T toscaElementToUpdate, GraphVertex elementV,
1140 ComponentParametersView filterResult) {
1141 Either<T, StorageOperationStatus> result = null;
1142 log.debug("In updateToscaElement. received component uid = {}", (toscaElementToUpdate == null ? null : toscaElementToUpdate.getUniqueId()));
1143 if (toscaElementToUpdate == null) {
1144 log.error("Service object is null");
1145 result = Either.right(StorageOperationStatus.BAD_REQUEST);
1148 String modifierUserId = toscaElementToUpdate.getLastUpdaterUserId();
1149 if (modifierUserId == null || modifierUserId.isEmpty()) {
1150 log.error("UserId is missing in the request.");
1151 result = Either.right(StorageOperationStatus.BAD_REQUEST);
1154 Either<GraphVertex, JanusGraphOperationStatus> findUser = findUserVertex(modifierUserId);
1155 if (findUser.isRight()) {
1156 JanusGraphOperationStatus status = findUser.right().value();
1157 log.error(CANNOT_FIND_USER_IN_THE_GRAPH_STATUS_IS, modifierUserId, status);
1160 GraphVertex modifierV = findUser.left().value();
1161 String toscaElementId = toscaElementToUpdate.getUniqueId();
1162 Either<GraphVertex, JanusGraphOperationStatus> parentVertex = janusGraphDao
1163 .getParentVertex(elementV, EdgeLabelEnum.LAST_MODIFIER, JsonParseFlagEnum.NoParse);
1164 if (parentVertex.isRight()) {
1165 log.debug("Failed to fetch last modifier for tosca element with id {} error {}", toscaElementId, parentVertex.right().value());
1166 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(parentVertex.right().value()));
1168 GraphVertex userV = parentVertex.left().value();
1169 String currentModifier = (String) userV.getMetadataProperty(GraphPropertyEnum.USERID);
1170 String prevSystemName = (String) elementV.getMetadataProperty(GraphPropertyEnum.SYSTEM_NAME);
1171 if (currentModifier.equals(modifierUserId)) {
1172 log.debug("Graph LAST MODIFIER edge should not be changed since the modifier is the same as the last modifier.");
1174 log.debug("Going to update the last modifier user of the resource from {} to {}", currentModifier, modifierUserId);
1175 StorageOperationStatus status = moveLastModifierEdge(elementV, modifierV);
1176 log.debug("Finish to update the last modifier user of the resource from {} to {}. status is {}", currentModifier, modifierUserId, status);
1177 if (status != StorageOperationStatus.OK) {
1178 result = Either.right(status);
1182 final long currentTimeMillis = System.currentTimeMillis();
1183 log.debug("Going to update the last Update Date of the resource from {} to {}",
1184 elementV.getJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE), currentTimeMillis);
1185 elementV.setJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE, currentTimeMillis);
1186 StorageOperationStatus checkCategories = validateCategories(toscaElementToUpdate, elementV);
1187 if (checkCategories != StorageOperationStatus.OK) {
1188 result = Either.right(checkCategories);
1191 // update all data on vertex
1192 fillToscaElementVertexData(elementV, toscaElementToUpdate, JsonParseFlagEnum.ParseMetadata);
1193 Either<GraphVertex, JanusGraphOperationStatus> updateElement = janusGraphDao.updateVertex(elementV);
1194 if (updateElement.isRight()) {
1195 log.error("Failed to update resource {}. status is {}", toscaElementId, updateElement.right().value());
1196 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(updateElement.right().value()));
1199 GraphVertex updateElementV = updateElement.left().value();
1200 // DE230195 in case resource name changed update TOSCA artifacts
1202 // file names accordingly
1203 String newSystemName = (String) updateElementV.getMetadataProperty(GraphPropertyEnum.SYSTEM_NAME);
1204 if (newSystemName != null && !newSystemName.equals(prevSystemName)) {
1205 Either<Map<String, ArtifactDataDefinition>, JanusGraphOperationStatus> resultToscaArt = getDataFromGraph(updateElementV,
1206 EdgeLabelEnum.TOSCA_ARTIFACTS);
1207 if (resultToscaArt.isRight()) {
1208 log.debug("Failed to get tosca artifact from graph for tosca element {} error {}", toscaElementId, resultToscaArt.right().value());
1209 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(resultToscaArt.right().value()));
1211 Map<String, ArtifactDataDefinition> toscaArtifacts = resultToscaArt.left().value();
1212 if (toscaArtifacts != null) {
1213 for (Entry<String, ArtifactDataDefinition> artifact : toscaArtifacts.entrySet()) {
1214 generateNewToscaFileName(toscaElementToUpdate.getComponentType().getValue().toLowerCase(), newSystemName, artifact.getValue());
1216 // TODO call to new Artifact operation in order to update list of artifacts
1219 if (toscaElementToUpdate.getComponentType() == ComponentTypeEnum.RESOURCE) {
1220 StorageOperationStatus resultDerived = updateDerived(toscaElementToUpdate, updateElementV);
1221 if (resultDerived != StorageOperationStatus.OK) {
1222 log.debug("Failed to update from derived data for element {} error {}", toscaElementId, resultDerived);
1223 return Either.right(resultDerived);
1226 Either<T, StorageOperationStatus> updatedResource = getToscaElement(updateElementV, filterResult);
1227 if (updatedResource.isRight()) {
1228 log.error("Failed to fetch tosca element {} after update , error {}", toscaElementId, updatedResource.right().value());
1229 result = Either.right(StorageOperationStatus.BAD_REQUEST);
1232 T updatedResourceValue = updatedResource.left().value();
1233 result = Either.left(updatedResourceValue);
1237 protected StorageOperationStatus moveLastModifierEdge(GraphVertex elementV, GraphVertex modifierV) {
1238 return DaoStatusConverter
1239 .convertJanusGraphStatusToStorageStatus(janusGraphDao.moveEdge(elementV, modifierV, EdgeLabelEnum.LAST_MODIFIER, Direction.IN));
1242 protected StorageOperationStatus moveCategoryEdge(GraphVertex elementV, GraphVertex categoryV) {
1243 return DaoStatusConverter
1244 .convertJanusGraphStatusToStorageStatus(janusGraphDao.moveEdge(elementV, categoryV, EdgeLabelEnum.CATEGORY, Direction.OUT));
1247 private void generateNewToscaFileName(String componentType, String componentName, ArtifactDataDefinition artifactInfo) {
1248 Optional<Entry<String, Object>> oConfig = ConfigurationManager.getConfigurationManager().getConfiguration().getToscaArtifacts()
1249 .entrySet().stream().filter(p -> p.getKey().equalsIgnoreCase(artifactInfo.getArtifactLabel())).findAny();
1250 if (oConfig.isPresent()) {
1251 artifactInfo.setArtifactName(componentType + "-" + componentName + ((Map<String, Object>) oConfig.get().getValue()).get("artifactName"));
1253 artifactInfo.setArtifactName(componentType + "-" + componentName);
1257 protected <T extends ToscaElement> StorageOperationStatus validateResourceCategory(T toscaElementToUpdate, GraphVertex elementV) {
1258 StorageOperationStatus status = StorageOperationStatus.OK;
1259 List<CategoryDefinition> newCategoryList = toscaElementToUpdate.getCategories();
1260 CategoryDefinition newCategory = newCategoryList.get(0);
1261 Either<GraphVertex, JanusGraphOperationStatus> childVertex = janusGraphDao
1262 .getChildVertex(elementV, EdgeLabelEnum.CATEGORY, JsonParseFlagEnum.NoParse);
1263 if (childVertex.isRight()) {
1264 log.debug(FAILED_TO_FETCH_FOR_TOSCA_ELEMENT_WITH_ID_ERROR, EdgeLabelEnum.CATEGORY, elementV.getUniqueId(), childVertex.right().value());
1265 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(childVertex.right().value());
1267 GraphVertex subCategoryV = childVertex.left().value();
1268 Map<GraphPropertyEnum, Object> metadataProperties = subCategoryV.getMetadataProperties();
1269 String subCategoryNameCurrent = (String) metadataProperties.get(GraphPropertyEnum.NAME);
1270 Either<GraphVertex, JanusGraphOperationStatus> parentVertex = janusGraphDao
1271 .getParentVertex(subCategoryV, EdgeLabelEnum.SUB_CATEGORY, JsonParseFlagEnum.NoParse);
1272 if (parentVertex.isRight()) {
1273 log.debug("failed to fetch {} for category with id {}, error {}", EdgeLabelEnum.SUB_CATEGORY, subCategoryV.getUniqueId(),
1274 parentVertex.right().value());
1275 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(childVertex.right().value());
1277 GraphVertex categoryV = parentVertex.left().value();
1278 metadataProperties = categoryV.getMetadataProperties();
1279 String categoryNameCurrent = (String) metadataProperties.get(GraphPropertyEnum.NAME);
1280 boolean categoryWasChanged = false;
1281 String newCategoryName = newCategory.getName();
1282 SubCategoryDefinition newSubcategory = newCategory.getSubcategories().get(0);
1283 String newSubCategoryName = newSubcategory.getName();
1284 if (newCategoryName != null && !newCategoryName.equals(categoryNameCurrent)) {
1285 // the category was changed
1286 categoryWasChanged = true;
1288 // the sub-category was changed
1289 if (newSubCategoryName != null && !newSubCategoryName.equals(subCategoryNameCurrent)) {
1290 log.debug("Going to update the category of the resource from {} to {}", categoryNameCurrent, newCategory);
1291 categoryWasChanged = true;
1294 if (categoryWasChanged) {
1295 Either<GraphVertex, StorageOperationStatus> getCategoryVertex = getResourceCategoryVertex(elementV.getUniqueId(), newSubCategoryName,
1297 if (getCategoryVertex.isRight()) {
1298 return getCategoryVertex.right().value();
1300 GraphVertex newCategoryV = getCategoryVertex.left().value();
1301 status = moveCategoryEdge(elementV, newCategoryV);
1302 log.debug("Going to update the category of the resource from {} to {}. status is {}", categoryNameCurrent, newCategory, status);
1307 public <T extends ToscaElement> Either<List<T>, StorageOperationStatus> getElementCatalogData(ComponentTypeEnum componentType,
1308 List<ResourceTypeEnum> excludeTypes,
1309 boolean isHighestVersions) {
1310 Either<List<GraphVertex>, JanusGraphOperationStatus> listOfComponents;
1311 if (isHighestVersions) {
1312 listOfComponents = getListOfHighestComponents(componentType, excludeTypes, JsonParseFlagEnum.NoParse);
1314 listOfComponents = getListOfHighestAndAllCertifiedComponents(componentType, excludeTypes);
1316 if (listOfComponents.isRight() && listOfComponents.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
1317 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(listOfComponents.right().value()));
1319 List<T> result = new ArrayList<>();
1320 if (listOfComponents.isLeft()) {
1321 List<GraphVertex> highestAndAllCertified = listOfComponents.left().value();
1322 if (highestAndAllCertified != null && !highestAndAllCertified.isEmpty()) {
1323 for (GraphVertex vertexComponent : highestAndAllCertified) {
1324 Either<T, StorageOperationStatus> component = getLightComponent(vertexComponent, componentType,
1325 new ComponentParametersView(true));
1326 if (component.isRight()) {
1327 log.debug("Failed to fetch light element for {} error {}", vertexComponent.getUniqueId(), component.right().value());
1328 return Either.right(component.right().value());
1330 result.add(component.left().value());
1335 return Either.left(result);
1338 public Either<List<CatalogComponent>, StorageOperationStatus> getElementCatalogData(boolean isCatalog, List<ResourceTypeEnum> excludeTypes) {
1339 StopWatch stopWatch = new StopWatch();
1341 Map<String, CatalogComponent> existInCatalog = new HashMap<>();
1342 Either<Iterator<Vertex>, JanusGraphOperationStatus> verticesEither = janusGraphDao.getCatalogOrArchiveVertices(isCatalog);
1343 if (verticesEither.isRight()) {
1344 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(verticesEither.right().value()));
1346 Iterator<Vertex> vertices = verticesEither.left().value();
1347 while (vertices.hasNext()) {
1348 handleCatalogComponent(existInCatalog, vertices.next(), excludeTypes);
1351 String timeToFetchElements = stopWatch.prettyPrint();
1352 log.info("time to fetch all catalog elements: {}", timeToFetchElements);
1353 return Either.left(existInCatalog.values().stream().collect(Collectors.toList()));
1356 private void handleCatalogComponent(Map<String, CatalogComponent> existInCatalog, Vertex vertex, List<ResourceTypeEnum> excludeTypes) {
1357 VertexProperty<Object> property = vertex.property(GraphPropertiesDictionary.METADATA.getProperty());
1358 String json = (String) property.value();
1359 Map<String, Object> metadatObj = JsonParserUtils.toMap(json);
1360 String uniqueId = (String) metadatObj.get(JsonPresentationFields.UNIQUE_ID.getPresentation());
1361 Boolean isDeleted = (Boolean) metadatObj.get(JsonPresentationFields.IS_DELETED.getPresentation());
1362 if (isAddToCatalog(excludeTypes, metadatObj) && (existInCatalog.get(uniqueId) == null && (isDeleted == null || !isDeleted.booleanValue()))) {
1363 CatalogComponent catalogComponent = new CatalogComponent();
1364 catalogComponent.setUniqueId(uniqueId);
1365 catalogComponent.setModel((String) metadatObj.get(JsonPresentationFields.MODEL.getPresentation()));
1367 .setComponentType(ComponentTypeEnum.valueOf((String) metadatObj.get(JsonPresentationFields.COMPONENT_TYPE.getPresentation())));
1368 catalogComponent.setVersion((String) metadatObj.get(JsonPresentationFields.VERSION.getPresentation()));
1369 catalogComponent.setName((String) metadatObj.get(JsonPresentationFields.NAME.getPresentation()));
1370 catalogComponent.setIcon((String) metadatObj.get(JsonPresentationFields.ICON.getPresentation()));
1371 catalogComponent.setLifecycleState((String) metadatObj.get(JsonPresentationFields.LIFECYCLE_STATE.getPresentation()));
1372 Object lastUpdateDate = metadatObj.get(JsonPresentationFields.LAST_UPDATE_DATE.getPresentation());
1373 catalogComponent.setLastUpdateDate((lastUpdateDate != null ? (Long) lastUpdateDate : 0L));
1374 catalogComponent.setDistributionStatus((String) metadatObj.get(JsonPresentationFields.DISTRIBUTION_STATUS.getPresentation()));
1375 catalogComponent.setDescription((String) metadatObj.get(JsonPresentationFields.DESCRIPTION.getPresentation()));
1376 catalogComponent.setTenant((String) metadatObj.get(JsonPresentationFields.TENANT.getPresentation()));
1377 catalogComponent.setSystemName((String) metadatObj.get(JsonPresentationFields.SYSTEM_NAME.getPresentation()));
1378 catalogComponent.setUuid((String) metadatObj.get(JsonPresentationFields.UUID.getPresentation()));
1379 catalogComponent.setInvariantUUID((String) metadatObj.get(JsonPresentationFields.INVARIANT_UUID.getPresentation()));
1380 catalogComponent.setIsHighestVersion((Boolean) metadatObj.get(JsonPresentationFields.HIGHEST_VERSION.getPresentation()));
1381 Iterator<Edge> edges = vertex.edges(Direction.IN, EdgeLabelEnum.STATE.name());
1382 if (edges.hasNext()) {
1384 .setLastUpdaterUserId((String) edges.next().outVertex().property(GraphPropertiesDictionary.USERID.getProperty()).value());
1386 Object resourceType = metadatObj.get(JsonPresentationFields.RESOURCE_TYPE.getPresentation());
1387 if (resourceType != null) {
1388 catalogComponent.setResourceType((String) resourceType);
1390 if (catalogComponent.getComponentType() == ComponentTypeEnum.SERVICE) {
1391 setServiceCategoryFromGraphV(vertex, catalogComponent);
1393 setResourceCategoryFromGraphV(vertex, catalogComponent);
1395 List<String> tags = (List<String>) metadatObj.get(JsonPresentationFields.TAGS.getPresentation());
1397 catalogComponent.setTags(tags);
1399 existInCatalog.put(uniqueId, catalogComponent);
1403 private boolean isAddToCatalog(List<ResourceTypeEnum> excludeTypes, Map<String, Object> metadatObj) {
1404 boolean isAddToCatalog = true;
1405 Object resourceTypeStr = metadatObj.get(JsonPresentationFields.RESOURCE_TYPE.getPresentation());
1406 if (resourceTypeStr != null) {
1407 ResourceTypeEnum resourceType = ResourceTypeEnum.getType((String) resourceTypeStr);
1408 if (!CollectionUtils.isEmpty(excludeTypes)) {
1409 Optional<ResourceTypeEnum> op = excludeTypes.stream().filter(rt -> rt == resourceType).findAny();
1410 if (op.isPresent()) {
1411 isAddToCatalog = false;
1415 return isAddToCatalog;
1418 private Either<List<GraphVertex>, JanusGraphOperationStatus> getListOfHighestComponents(ComponentTypeEnum componentType,
1419 List<ResourceTypeEnum> excludeTypes,
1420 JsonParseFlagEnum parseFlag) {
1421 Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
1422 Map<GraphPropertyEnum, Object> propertiesHasNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
1423 propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
1424 propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
1425 if (componentType == ComponentTypeEnum.RESOURCE) {
1426 propertiesToMatch.put(GraphPropertyEnum.IS_ABSTRACT, false);
1427 propertiesHasNotToMatch.put(GraphPropertyEnum.RESOURCE_TYPE, excludeTypes);
1429 propertiesHasNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
1430 propertiesHasNotToMatch.put(GraphPropertyEnum.IS_ARCHIVED, true); //US382674, US382683
1431 return janusGraphDao.getByCriteria(null, propertiesToMatch, propertiesHasNotToMatch, parseFlag);
1434 // highest + (certified && !highest)
1435 private Either<List<GraphVertex>, JanusGraphOperationStatus> getListOfHighestAndAllCertifiedComponents(ComponentTypeEnum componentType,
1436 List<ResourceTypeEnum> excludeTypes) {
1437 long startFetchAllStates = System.currentTimeMillis();
1438 Either<List<GraphVertex>, JanusGraphOperationStatus> highestNodes = getListOfHighestComponents(componentType, excludeTypes,
1439 JsonParseFlagEnum.ParseMetadata);
1440 Map<GraphPropertyEnum, Object> propertiesToMatchCertified = new EnumMap<>(GraphPropertyEnum.class);
1441 Map<GraphPropertyEnum, Object> propertiesHasNotToMatchCertified = new EnumMap<>(GraphPropertyEnum.class);
1442 propertiesToMatchCertified.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
1443 propertiesToMatchCertified.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
1444 if (componentType == ComponentTypeEnum.RESOURCE) {
1445 propertiesToMatchCertified.put(GraphPropertyEnum.IS_ABSTRACT, false);
1446 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);
1451 Either<List<GraphVertex>, JanusGraphOperationStatus> certifiedNotHighestNodes = janusGraphDao
1452 .getByCriteria(null, propertiesToMatchCertified, propertiesHasNotToMatchCertified, JsonParseFlagEnum.ParseMetadata);
1453 if (certifiedNotHighestNodes.isRight() && certifiedNotHighestNodes.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
1454 return Either.right(certifiedNotHighestNodes.right().value());
1456 long endFetchAllStates = System.currentTimeMillis();
1457 List<GraphVertex> allNodes = new ArrayList<>();
1458 if (certifiedNotHighestNodes.isLeft()) {
1459 allNodes.addAll(certifiedNotHighestNodes.left().value());
1461 if (highestNodes.isLeft()) {
1462 allNodes.addAll(highestNodes.left().value());
1464 log.debug("Fetch catalog {}s all states from graph took {} ms", componentType, endFetchAllStates - startFetchAllStates);
1465 return Either.left(allNodes);
1468 protected Either<List<GraphVertex>, StorageOperationStatus> getAllComponentsMarkedForDeletion(ComponentTypeEnum componentType) {
1469 // get all components marked for delete
1470 Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
1471 props.put(GraphPropertyEnum.IS_DELETED, true);
1472 props.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
1473 Either<List<GraphVertex>, JanusGraphOperationStatus> componentsToDelete = janusGraphDao.getByCriteria(null, props, JsonParseFlagEnum.NoParse);
1474 if (componentsToDelete.isRight()) {
1475 JanusGraphOperationStatus error = componentsToDelete.right().value();
1476 if (error.equals(JanusGraphOperationStatus.NOT_FOUND)) {
1477 log.trace("no components to delete");
1478 return Either.left(new ArrayList<>());
1480 log.info("failed to find components to delete. error : {}", error.name());
1481 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(error));
1484 return Either.left(componentsToDelete.left().value());
1487 protected JanusGraphOperationStatus setAdditionalInformationFromGraph(GraphVertex componentV, ToscaElement toscaElement) {
1488 Either<Map<String, AdditionalInfoParameterDataDefinition>, JanusGraphOperationStatus> result = getDataFromGraph(componentV,
1489 EdgeLabelEnum.ADDITIONAL_INFORMATION);
1490 if (result.isLeft()) {
1491 toscaElement.setAdditionalInformation(result.left().value());
1493 if (result.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
1494 return result.right().value();
1497 return JanusGraphOperationStatus.OK;
1500 // --------------------------------------------
1501 public abstract <T extends ToscaElement> Either<T, StorageOperationStatus> getToscaElement(String uniqueId,
1502 ComponentParametersView componentParametersView);
1504 public abstract <T extends ToscaElement> Either<T, StorageOperationStatus> getToscaElement(GraphVertex toscaElementVertex,
1505 ComponentParametersView componentParametersView);
1507 public abstract <T extends ToscaElement> Either<T, StorageOperationStatus> deleteToscaElement(GraphVertex toscaElementVertex);
1509 public abstract <T extends ToscaElement> Either<T, StorageOperationStatus> createToscaElement(ToscaElement toscaElement);
1511 protected abstract <T extends ToscaElement> JanusGraphOperationStatus setCategoriesFromGraph(GraphVertex vertexComponent, T toscaElement);
1513 protected abstract <T extends ToscaElement> JanusGraphOperationStatus setCapabilitiesFromGraph(GraphVertex componentV, T toscaElement);
1515 protected abstract <T extends ToscaElement> JanusGraphOperationStatus setRequirementsFromGraph(GraphVertex componentV, T toscaElement);
1517 protected abstract <T extends ToscaElement> StorageOperationStatus validateCategories(T toscaElementToUpdate, GraphVertex elementV);
1519 protected abstract <T extends ToscaElement> StorageOperationStatus updateDerived(T toscaElementToUpdate, GraphVertex updateElementV);
1521 public abstract <T extends ToscaElement> void fillToscaElementVertexData(GraphVertex elementV, T toscaElementToUpdate, JsonParseFlagEnum flag);