1 package org.openecomp.sdc.be.model.jsontitan.operations;
3 import java.lang.reflect.Type;
4 import java.util.ArrayList;
5 import java.util.HashMap;
6 import java.util.Iterator;
9 import java.util.Optional;
10 import java.util.Map.Entry;
12 import org.apache.tinkerpop.gremlin.structure.Direction;
13 import org.apache.tinkerpop.gremlin.structure.Edge;
14 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
15 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
16 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
17 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
18 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
19 import org.openecomp.sdc.be.datatypes.elements.AdditionalInfoParameterDataDefinition;
20 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
21 import org.openecomp.sdc.be.datatypes.elements.ComponentInstanceDataDefinition;
22 import org.openecomp.sdc.be.datatypes.elements.CompositionDataDefinition;
23 import org.openecomp.sdc.be.datatypes.elements.GroupDataDefinition;
24 import org.openecomp.sdc.be.datatypes.elements.MapArtifactDataDefinition;
25 import org.openecomp.sdc.be.datatypes.elements.MapCapabiltyProperty;
26 import org.openecomp.sdc.be.datatypes.elements.MapGroupsDataDefinition;
27 import org.openecomp.sdc.be.datatypes.elements.MapListCapabiltyDataDefinition;
28 import org.openecomp.sdc.be.datatypes.elements.MapListRequirementDataDefinition;
29 import org.openecomp.sdc.be.datatypes.elements.MapPropertiesDataDefinition;
30 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
31 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
32 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
33 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
34 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
35 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
36 import org.openecomp.sdc.be.model.ComponentParametersView;
37 import org.openecomp.sdc.be.model.DistributionStatusEnum;
38 import org.openecomp.sdc.be.model.User;
39 import org.openecomp.sdc.be.model.category.CategoryDefinition;
40 import org.openecomp.sdc.be.model.jsontitan.datamodel.TopologyTemplate;
41 import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElement;
42 import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElementTypeEnum;
43 import org.openecomp.sdc.be.model.jsontitan.enums.JsonConstantKeysEnum;
44 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
45 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
46 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
47 import org.openecomp.sdc.common.jsongraph.util.CommonUtility;
48 import org.openecomp.sdc.common.jsongraph.util.CommonUtility.LogLevelEnum;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
52 import com.google.gson.reflect.TypeToken;
54 import fj.data.Either;
56 @org.springframework.stereotype.Component("topology-template-operation")
57 public class TopologyTemplateOperation extends ToscaElementOperation {
58 private static Logger log = LoggerFactory.getLogger(TopologyTemplateOperation.class.getName());
60 public Either<TopologyTemplate, StorageOperationStatus> createTopologyTemplate(TopologyTemplate topologyTemplate) {
61 Either<TopologyTemplate, StorageOperationStatus> result = null;
63 topologyTemplate.generateUUID();
65 topologyTemplate = (TopologyTemplate) getResourceMetaDataFromResource(topologyTemplate);
66 String resourceUniqueId = topologyTemplate.getUniqueId();
67 if (resourceUniqueId == null) {
68 resourceUniqueId = UniqueIdBuilder.buildResourceUniqueId();
69 topologyTemplate.setUniqueId(resourceUniqueId);
72 GraphVertex topologyTemplateVertex = new GraphVertex();
73 topologyTemplateVertex = fillMetadata(topologyTemplateVertex, topologyTemplate, JsonParseFlagEnum.ParseAll);
75 Either<GraphVertex, TitanOperationStatus> createdVertex = titanDao.createVertex(topologyTemplateVertex);
76 if (createdVertex.isRight()) {
77 TitanOperationStatus status = createdVertex.right().value();
78 log.error("Error returned after creating topology template data node {}. status returned is ", topologyTemplateVertex, status);
79 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
83 topologyTemplateVertex = createdVertex.left().value();
85 StorageOperationStatus assosiateCommon = assosiateCommonForToscaElement(topologyTemplateVertex, topologyTemplate, null);
86 if (assosiateCommon != StorageOperationStatus.OK) {
87 result = Either.right(assosiateCommon);
91 StorageOperationStatus associateCategory = assosiateMetadataToCategory(topologyTemplateVertex, topologyTemplate);
92 if (associateCategory != StorageOperationStatus.OK) {
93 result = Either.right(associateCategory);
97 StorageOperationStatus associateInputs = associateInputsToComponent(topologyTemplateVertex, topologyTemplate);
98 if (associateInputs != StorageOperationStatus.OK) {
99 result = Either.right(associateInputs);
102 StorageOperationStatus associateGroups = associateGroupsToComponent(topologyTemplateVertex, topologyTemplate);
103 if (associateGroups != StorageOperationStatus.OK) {
104 result = Either.right(associateGroups);
107 StorageOperationStatus associateInstAttr = associateInstAttributesToComponent(topologyTemplateVertex, topologyTemplate);
108 if (associateInstAttr != StorageOperationStatus.OK) {
109 result = Either.right(associateInstAttr);
112 StorageOperationStatus associateInstProperties = associateInstPropertiesToComponent(topologyTemplateVertex, topologyTemplate);
113 if (associateInstProperties != StorageOperationStatus.OK) {
114 result = Either.right(associateInstProperties);
117 StorageOperationStatus associateInstInputs = associateInstInputsToComponent(topologyTemplateVertex, topologyTemplate);
118 if (associateInstProperties != StorageOperationStatus.OK) {
119 result = Either.right(associateInstInputs);
122 StorageOperationStatus associateInstGroups = associateInstGroupsToComponent(topologyTemplateVertex, topologyTemplate);
123 if (associateInstProperties != StorageOperationStatus.OK) {
124 result = Either.right(associateInstInputs);
128 StorageOperationStatus associateRequirements = associateRequirementsToResource(topologyTemplateVertex, topologyTemplate);
129 if (associateRequirements != StorageOperationStatus.OK) {
130 result = Either.right(associateRequirements);
134 StorageOperationStatus associateCapabilities = associateCapabilitiesToResource(topologyTemplateVertex, topologyTemplate);
135 if (associateCapabilities != StorageOperationStatus.OK) {
136 result = Either.right(associateCapabilities);
140 StorageOperationStatus associateArtifacts = associateTopologyTemplateArtifactsToComponent(topologyTemplateVertex, topologyTemplate);
141 if (associateArtifacts != StorageOperationStatus.OK) {
142 result = Either.right(associateArtifacts);
146 StorageOperationStatus addAdditionalInformation = addAdditionalInformationToResource(topologyTemplateVertex, topologyTemplate);
147 if (addAdditionalInformation != StorageOperationStatus.OK) {
148 result = Either.right(addAdditionalInformation);
151 StorageOperationStatus associateCapProperties = associateCapPropertiesToResource(topologyTemplateVertex, topologyTemplate);
152 if (associateCapProperties != StorageOperationStatus.OK) {
153 result = Either.right(associateCapProperties);
156 return Either.left(topologyTemplate);
160 private StorageOperationStatus associateCapPropertiesToResource(GraphVertex topologyTemplateVertex, TopologyTemplate topologyTemplate) {
161 Map<String, MapCapabiltyProperty> calculatedCapProperties = topologyTemplate.getCalculatedCapabilitiesProperties();
162 if (calculatedCapProperties != null && !calculatedCapProperties.isEmpty()) {
163 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(topologyTemplateVertex, VertexTypeEnum.CALCULATED_CAP_PROPERTIES, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES, calculatedCapProperties);
164 if (assosiateElementToData.isRight()) {
165 return assosiateElementToData.right().value();
168 return StorageOperationStatus.OK;
171 private StorageOperationStatus associateCapabilitiesToResource(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
172 Map<String, MapListCapabiltyDataDefinition> calculatedCapabilities = topologyTemplate.getCalculatedCapabilities();
173 if (calculatedCapabilities != null && !calculatedCapabilities.isEmpty()) {
174 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.CALCULATED_CAPABILITIES, EdgeLabelEnum.CALCULATED_CAPABILITIES, calculatedCapabilities);
175 if (assosiateElementToData.isRight()) {
176 return assosiateElementToData.right().value();
179 Map<String, MapListCapabiltyDataDefinition> fullfilledCapabilities = topologyTemplate.getFullfilledCapabilities();
180 if (fullfilledCapabilities != null && !fullfilledCapabilities.isEmpty()) {
181 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.FULLFILLED_CAPABILITIES, EdgeLabelEnum.FULLFILLED_CAPABILITIES, fullfilledCapabilities);
182 if (assosiateElementToData.isRight()) {
183 return assosiateElementToData.right().value();
186 return StorageOperationStatus.OK;
190 private StorageOperationStatus associateRequirementsToResource(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
191 Map<String, MapListRequirementDataDefinition> calculatedRequirements = topologyTemplate.getCalculatedRequirements();
192 if (calculatedRequirements != null && !calculatedRequirements.isEmpty()) {
193 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.CALCULATED_REQUIREMENTS, EdgeLabelEnum.CALCULATED_REQUIREMENTS, calculatedRequirements);
194 if (assosiateElementToData.isRight()) {
195 return assosiateElementToData.right().value();
198 Map<String, MapListRequirementDataDefinition> fullfilledRequirements = topologyTemplate.getFullfilledRequirements();
199 if (fullfilledRequirements != null && !fullfilledRequirements.isEmpty()) {
200 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.FULLFILLED_REQUIREMENTS, EdgeLabelEnum.FULLFILLED_REQUIREMENTS, fullfilledRequirements);
201 if (assosiateElementToData.isRight()) {
202 return assosiateElementToData.right().value();
205 return StorageOperationStatus.OK;
208 private StorageOperationStatus associateTopologyTemplateArtifactsToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
209 Map<String, ArtifactDataDefinition> addInformation = topologyTemplate.getServiceApiArtifacts();
211 if (addInformation != null && !addInformation.isEmpty()) {
212 addInformation.values().stream().filter(a -> a.getUniqueId() == null).forEach(a -> {
213 String uniqueId = UniqueIdBuilder.buildPropertyUniqueId(nodeTypeVertex.getUniqueId().toLowerCase(), a.getArtifactLabel().toLowerCase());
214 a.setUniqueId(uniqueId);
216 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.SERVICE_API_ARTIFACTS, EdgeLabelEnum.SERVICE_API_ARTIFACTS, addInformation);
217 if (assosiateElementToData.isRight()) {
218 return assosiateElementToData.right().value();
221 Map<String, MapArtifactDataDefinition> instArtifacts = topologyTemplate.getInstDeploymentArtifacts();
223 if (instArtifacts != null && !instArtifacts.isEmpty()) {
224 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.INST_DEPLOYMENT_ARTIFACTS, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS, instArtifacts);
225 if (assosiateElementToData.isRight()) {
226 return assosiateElementToData.right().value();
229 Map<String, MapArtifactDataDefinition> instInfoArtifacts = topologyTemplate.getInstanceArtifacts();
231 if (instInfoArtifacts != null && !instInfoArtifacts.isEmpty()) {
232 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.INSTANCE_ARTIFACTS, EdgeLabelEnum.INSTANCE_ARTIFACTS, instInfoArtifacts);
233 if (assosiateElementToData.isRight()) {
234 return assosiateElementToData.right().value();
237 return StorageOperationStatus.OK;
240 private StorageOperationStatus addAdditionalInformationToResource(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
242 Map<String, AdditionalInfoParameterDataDefinition> addInformation = topologyTemplate.getAdditionalInformation();
244 if (addInformation != null && !addInformation.isEmpty()) {
245 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.ADDITIONAL_INFORMATION, EdgeLabelEnum.ADDITIONAL_INFORMATION, addInformation);
246 if (assosiateElementToData.isRight()) {
247 return assosiateElementToData.right().value();
250 return StorageOperationStatus.OK;
253 public StorageOperationStatus associateInstPropertiesToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
254 Map<String, MapPropertiesDataDefinition> instProps = topologyTemplate.getInstProperties();
255 return associateInstPropertiesToComponent(nodeTypeVertex, instProps);
258 public StorageOperationStatus associateInstInputsToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
259 Map<String, MapPropertiesDataDefinition> instProps = topologyTemplate.getInstInputs();
260 return associateInstInputsToComponent(nodeTypeVertex, instProps);
263 public StorageOperationStatus associateInstGroupsToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
264 Map<String, MapGroupsDataDefinition> instGroups = topologyTemplate.getInstGroups();
265 return associateInstGroupsToComponent(nodeTypeVertex, instGroups);
269 public StorageOperationStatus associateInstPropertiesToComponent(GraphVertex nodeTypeVertex, Map<String, MapPropertiesDataDefinition> instProps) {
270 if (instProps != null && !instProps.isEmpty()) {
271 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.INST_PROPERTIES, EdgeLabelEnum.INST_PROPERTIES, instProps);
272 if (assosiateElementToData.isRight()) {
273 return assosiateElementToData.right().value();
276 return StorageOperationStatus.OK;
279 public StorageOperationStatus associateInstInputsToComponent(GraphVertex nodeTypeVertex, Map<String, MapPropertiesDataDefinition> instInputs) {
280 if (instInputs != null && !instInputs.isEmpty()) {
281 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.INST_INPUTS, EdgeLabelEnum.INST_INPUTS, instInputs);
282 if (assosiateElementToData.isRight()) {
283 return assosiateElementToData.right().value();
286 return StorageOperationStatus.OK;
289 public StorageOperationStatus associateInstGroupsToComponent(GraphVertex nodeTypeVertex, Map<String, MapGroupsDataDefinition> instGroups) {
290 if (instGroups != null && !instGroups.isEmpty()) {
291 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.INST_GROUPS, EdgeLabelEnum.INST_GROUPS, instGroups);
292 if (assosiateElementToData.isRight()) {
293 return assosiateElementToData.right().value();
296 return StorageOperationStatus.OK;
300 public StorageOperationStatus deleteInstInputsToComponent(GraphVertex nodeTypeVertex, Map<String, MapPropertiesDataDefinition> instInputs) {
302 if (instInputs != null && !instInputs.isEmpty()) {
303 instInputs.entrySet().forEach(i -> {
304 List<String> uniqueKeys = new ArrayList<String>(i.getValue().getMapToscaDataDefinition().keySet());
305 List<String> pathKeys = new ArrayList<String>();
306 pathKeys.add(i.getKey());
308 StorageOperationStatus status = deleteToscaDataDeepElements(nodeTypeVertex, EdgeLabelEnum.INST_INPUTS, VertexTypeEnum.INST_INPUTS, uniqueKeys, pathKeys, JsonPresentationFields.NAME);
309 if (status != StorageOperationStatus.OK) {
315 return StorageOperationStatus.OK;
318 public StorageOperationStatus addInstPropertiesToComponent(GraphVertex nodeTypeVertex, Map<String, MapPropertiesDataDefinition> instInputs) {
320 if (instInputs != null && !instInputs.isEmpty()) {
321 instInputs.entrySet().forEach(i -> {
322 StorageOperationStatus status = addToscaDataDeepElementsBlockToToscaElement(nodeTypeVertex, EdgeLabelEnum.INST_PROPERTIES, VertexTypeEnum.INST_PROPERTIES, i.getValue(), i.getKey());
323 if (status != StorageOperationStatus.OK) {
329 return StorageOperationStatus.OK;
332 public StorageOperationStatus associateInstArtifactToComponent(GraphVertex nodeTypeVertex, Map<String, MapArtifactDataDefinition> instProps) {
333 if (instProps != null && !instProps.isEmpty()) {
334 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.INST_DEPLOYMENT_ARTIFACTS, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS, instProps);
335 if (assosiateElementToData.isRight()) {
336 return assosiateElementToData.right().value();
339 return StorageOperationStatus.OK;
342 public StorageOperationStatus associateCalcCapReqToComponent(GraphVertex nodeTypeVertex, Map<String, MapListRequirementDataDefinition> calcRequirements, Map<String, MapListCapabiltyDataDefinition> calcCapabilty, Map<String, MapCapabiltyProperty> calculatedCapabilitiesProperties) {
343 if (calcRequirements != null && !calcRequirements.isEmpty()) {
344 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.CALCULATED_REQUIREMENTS, EdgeLabelEnum.CALCULATED_REQUIREMENTS, calcRequirements);
345 if (assosiateElementToData.isRight()) {
346 return assosiateElementToData.right().value();
348 Map<String, MapListRequirementDataDefinition> fullFilled = new HashMap<>();
349 assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.FULLFILLED_REQUIREMENTS, EdgeLabelEnum.FULLFILLED_REQUIREMENTS, fullFilled);
350 if (assosiateElementToData.isRight()) {
351 return assosiateElementToData.right().value();
354 if (calcCapabilty != null && !calcCapabilty.isEmpty()) {
355 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.CALCULATED_CAPABILITIES, EdgeLabelEnum.CALCULATED_CAPABILITIES, calcCapabilty);
356 if (assosiateElementToData.isRight()) {
357 return assosiateElementToData.right().value();
359 Map<String, MapListCapabiltyDataDefinition> fullFilled = new HashMap<>();
360 assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.FULLFILLED_CAPABILITIES, EdgeLabelEnum.FULLFILLED_CAPABILITIES, fullFilled);
361 if (assosiateElementToData.isRight()) {
362 return assosiateElementToData.right().value();
365 if ( calculatedCapabilitiesProperties != null && !calculatedCapabilitiesProperties.isEmpty() ){
366 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.CALCULATED_CAP_PROPERTIES, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES, calculatedCapabilitiesProperties);
367 if (assosiateElementToData.isRight()) {
368 return assosiateElementToData.right().value();
371 return StorageOperationStatus.OK;
374 private StorageOperationStatus associateInstAttributesToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
375 Map<String, MapPropertiesDataDefinition> instAttr = topologyTemplate.getInstAttributes();
376 return associateInstAttributeToComponent(nodeTypeVertex, instAttr);
379 public StorageOperationStatus associateInstAttributeToComponent(GraphVertex nodeTypeVertex, Map<String, MapPropertiesDataDefinition> instAttr) {
380 if (instAttr != null && !instAttr.isEmpty()) {
381 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.INST_ATTRIBUTES, EdgeLabelEnum.INST_ATTRIBUTES, instAttr);
382 if (assosiateElementToData.isRight()) {
383 return assosiateElementToData.right().value();
386 return StorageOperationStatus.OK;
389 public StorageOperationStatus associateGroupsToComponent(GraphVertex nodeTypeVertex, Map<String, GroupDataDefinition> groups) {
391 if (groups != null && !groups.isEmpty()) {
392 groups.values().stream().filter(p -> p.getUniqueId() == null).forEach(p -> {
393 String uid = UniqueIdBuilder.buildGroupingUid(nodeTypeVertex.getUniqueId(), p.getName());
396 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.GROUPS, EdgeLabelEnum.GROUPS, groups);
397 if (assosiateElementToData.isRight()) {
398 return assosiateElementToData.right().value();
401 return StorageOperationStatus.OK;
404 private StorageOperationStatus associateGroupsToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
405 return associateGroupsToComponent(nodeTypeVertex, topologyTemplate.getGroups());
408 public StorageOperationStatus associateInputsToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
409 Map<String, PropertyDataDefinition> inputs = topologyTemplate.getInputs();
410 return associateInputsToComponent(nodeTypeVertex, inputs, topologyTemplate.getUniqueId());
413 public StorageOperationStatus associateInputsToComponent(GraphVertex nodeTypeVertex, Map<String, PropertyDataDefinition> inputs, String id) {
414 if (inputs != null && !inputs.isEmpty()) {
415 inputs.values().stream().filter(e -> e.getUniqueId() == null).forEach(e -> e.setUniqueId(UniqueIdBuilder.buildPropertyUniqueId(id, e.getName())));
417 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.INPUTS, EdgeLabelEnum.INPUTS, inputs);
418 if (assosiateElementToData.isRight()) {
419 return assosiateElementToData.right().value();
422 return StorageOperationStatus.OK;
425 private GraphVertex fillMetadata(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate, JsonParseFlagEnum flag) {
426 nodeTypeVertex.setLabel(VertexTypeEnum.TOPOLOGY_TEMPLATE);
427 fillCommonMetadata(nodeTypeVertex, topologyTemplate);
428 if (flag == JsonParseFlagEnum.ParseAll || flag == JsonParseFlagEnum.ParseJson) {
429 nodeTypeVertex.setJson(topologyTemplate.getCompositions());
431 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.CSAR_UUID, topologyTemplate.getMetadataValue(JsonPresentationFields.CSAR_UUID));
432 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.DISTRIBUTION_STATUS, topologyTemplate.getMetadataValue(JsonPresentationFields.DISTRIBUTION_STATUS));
434 return nodeTypeVertex;
438 private StorageOperationStatus assosiateMetadataToCategory(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
439 if (topologyTemplate.getResourceType() == null) {
441 return associateServiceMetadataToCategory(nodeTypeVertex, topologyTemplate);
444 return assosiateResourceMetadataToCategory(nodeTypeVertex, topologyTemplate);
448 private StorageOperationStatus associateServiceMetadataToCategory(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
449 String categoryName = topologyTemplate.getCategories().get(0).getName();
450 Either<GraphVertex, StorageOperationStatus> category = categoryOperation.getCategory(categoryName, VertexTypeEnum.SERVICE_CATEGORY);
451 if (category.isRight()) {
452 log.trace("NO category {} for service {}", categoryName, topologyTemplate.getUniqueId());
453 return StorageOperationStatus.CATEGORY_NOT_FOUND;
455 GraphVertex categoryV = category.left().value();
456 TitanOperationStatus createEdge = titanDao.createEdge(nodeTypeVertex, categoryV, EdgeLabelEnum.CATEGORY, new HashMap<>());
457 if (createEdge != TitanOperationStatus.OK) {
458 log.trace("Failed to associate resource {} to category {} with id {}", topologyTemplate.getUniqueId(), categoryName, categoryV.getUniqueId());
459 return DaoStatusConverter.convertTitanStatusToStorageStatus(createEdge);
461 return StorageOperationStatus.OK;
465 public Either<ToscaElement, StorageOperationStatus> getToscaElement(String uniqueId, ComponentParametersView componentParametersView) {
466 JsonParseFlagEnum parseFlag = componentParametersView.detectParseFlag();
468 Either<GraphVertex, StorageOperationStatus> componentByLabelAndId = getComponentByLabelAndId(uniqueId, ToscaElementTypeEnum.TopologyTemplate, parseFlag);
469 if (componentByLabelAndId.isRight()) {
470 return Either.right(componentByLabelAndId.right().value());
472 GraphVertex componentV = componentByLabelAndId.left().value();
474 return getToscaElement(componentV, componentParametersView);
477 // -------------------------------------------------------------
479 public Either<ToscaElement, StorageOperationStatus> getToscaElement(GraphVertex componentV, ComponentParametersView componentParametersView) {
480 TopologyTemplate toscaElement;
482 toscaElement = convertToTopologyTemplate(componentV);
483 TitanOperationStatus status = null;
484 if (false == componentParametersView.isIgnoreUsers()) {
485 status = setCreatorFromGraph(componentV, toscaElement);
486 if (status != TitanOperationStatus.OK) {
487 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
490 status = setLastModifierFromGraph(componentV, toscaElement);
491 if (status != TitanOperationStatus.OK) {
492 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
495 if (false == componentParametersView.isIgnoreCategories()) {
496 status = setTopologyTempalteCategoriesFromGraph(componentV, toscaElement);
497 if (status != TitanOperationStatus.OK) {
498 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
502 if (false == componentParametersView.isIgnoreArtifacts()) {
503 TitanOperationStatus storageStatus = setAllArtifactsFromGraph(componentV, toscaElement);
504 if (storageStatus != TitanOperationStatus.OK) {
505 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(storageStatus));
508 if (false == componentParametersView.isIgnoreComponentInstancesProperties()) {
509 status = setComponentInstancesPropertiesFromGraph(componentV, toscaElement);
510 if (status != TitanOperationStatus.OK) {
511 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
514 if (false == componentParametersView.isIgnoreCapabilities()) {
515 status = setCapabilitiesFromGraph(componentV, toscaElement);
516 if (status != TitanOperationStatus.OK) {
517 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
520 if (false == componentParametersView.isIgnoreRequirements()) {
521 status = setRequirementsFromGraph(componentV, toscaElement);
522 if (status != TitanOperationStatus.OK) {
523 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
526 if (false == componentParametersView.isIgnoreAllVersions()) {
527 status = setAllVersions(componentV, toscaElement);
528 if (status != TitanOperationStatus.OK) {
529 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
532 if (false == componentParametersView.isIgnoreAdditionalInformation()) {
533 status = setAdditionalInformationFromGraph(componentV, toscaElement);
534 if (status != TitanOperationStatus.OK) {
535 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
539 if (false == componentParametersView.isIgnoreGroups()) {
540 status = setGroupsFromGraph(componentV, toscaElement);
541 if (status != TitanOperationStatus.OK) {
542 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
546 if (false == componentParametersView.isIgnoreComponentInstances()) {
547 status = setInstGroupsFromGraph(componentV, toscaElement);
548 if (status != TitanOperationStatus.OK) {
549 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
553 if (false == componentParametersView.isIgnoreInputs()) {
554 status = setInputsFromGraph(componentV, toscaElement);
555 if (status != TitanOperationStatus.OK) {
556 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
560 if (false == componentParametersView.isIgnoreProperties()) {
561 status = setPropertiesFromGraph(componentV, toscaElement);
562 if (status != TitanOperationStatus.OK) {
563 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
568 if (false == componentParametersView.isIgnoreComponentInstancesInputs()) {
569 status = setComponentInstancesInputsFromGraph(componentV, toscaElement);
570 if (status != TitanOperationStatus.OK) {
571 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
576 if (false == componentParametersView.isIgnoreCapabiltyProperties()) {
577 status = setComponentInstancesCapPropertiesFromGraph(componentV, toscaElement);
578 if (status != TitanOperationStatus.OK) {
579 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
583 return Either.left(toscaElement);
586 private TitanOperationStatus setComponentInstancesCapPropertiesFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
587 Either<Map<String, MapCapabiltyProperty>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES);
588 if (result.isLeft()) {
589 topologyTemplate.setCalculatedCapabilitiesProperties(result.left().value());
591 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
592 return result.right().value();
595 return TitanOperationStatus.OK;
598 private TitanOperationStatus setPropertiesFromGraph(GraphVertex componentV, TopologyTemplate toscaElement) {
599 Either<Map<String, PropertyDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.PROPERTIES);
600 if (result.isLeft()) {
601 toscaElement.setProperties(result.left().value());
603 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
604 return result.right().value();
607 return TitanOperationStatus.OK;
610 private TitanOperationStatus setInstGroupsFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
611 Either<Map<String, MapGroupsDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.INST_GROUPS);
612 if (result.isLeft()) {
613 topologyTemplate.setInstGroups(result.left().value());
615 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
616 return result.right().value();
619 return TitanOperationStatus.OK;
622 private TitanOperationStatus setComponentInstancesPropertiesFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
623 Either<Map<String, MapPropertiesDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.INST_PROPERTIES);
624 if (result.isLeft()) {
625 topologyTemplate.setInstProperties(result.left().value());
627 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
628 return result.right().value();
631 return TitanOperationStatus.OK;
634 private TitanOperationStatus setComponentInstancesInputsFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
635 Either<Map<String, MapPropertiesDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.INST_INPUTS);
636 if (result.isLeft()) {
637 topologyTemplate.setInstInputs(result.left().value());
639 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
640 return result.right().value();
643 return TitanOperationStatus.OK;
647 protected <T extends ToscaElement> TitanOperationStatus setRequirementsFromGraph(GraphVertex componentV, T toscaElement) {
648 Either<Map<String, MapListRequirementDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.CALCULATED_REQUIREMENTS);
649 if (result.isLeft()) {
650 ((TopologyTemplate) toscaElement).setCalculatedRequirements(result.left().value());
652 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
653 return result.right().value();
656 result = getDataFromGraph(componentV, EdgeLabelEnum.FULLFILLED_REQUIREMENTS);
657 if (result.isLeft()) {
658 ((TopologyTemplate) toscaElement).setFullfilledRequirements(result.left().value());
660 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
661 return result.right().value();
664 return TitanOperationStatus.OK;
668 protected <T extends ToscaElement> TitanOperationStatus setCapabilitiesFromGraph(GraphVertex componentV, T toscaElement) {
669 Either<Map<String, MapListCapabiltyDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.CALCULATED_CAPABILITIES);
670 if (result.isLeft()) {
671 ((TopologyTemplate) toscaElement).setCalculatedCapabilities(result.left().value());
673 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
674 return result.right().value();
677 result = getDataFromGraph(componentV, EdgeLabelEnum.FULLFILLED_CAPABILITIES);
678 if (result.isLeft()) {
679 ((TopologyTemplate) toscaElement).setFullfilledCapabilities(result.left().value());
681 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
682 return result.right().value();
685 return TitanOperationStatus.OK;
688 private TitanOperationStatus setAllArtifactsFromGraph(GraphVertex componentV, TopologyTemplate toscaElement) {
689 TitanOperationStatus storageStatus = setArtifactsFromGraph(componentV, toscaElement);
690 if (storageStatus != TitanOperationStatus.OK) {
691 return storageStatus;
693 Either<Map<String, ArtifactDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.SERVICE_API_ARTIFACTS);
694 if (result.isLeft()) {
695 toscaElement.setServiceApiArtifacts(result.left().value());
697 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
698 return result.right().value();
701 Either<Map<String, MapArtifactDataDefinition>, TitanOperationStatus> resultInstArt = getDataFromGraph(componentV, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS);
702 if (resultInstArt.isLeft()) {
703 toscaElement.setInstDeploymentArtifacts(resultInstArt.left().value());
705 if (resultInstArt.right().value() != TitanOperationStatus.NOT_FOUND) {
706 return resultInstArt.right().value();
709 Either<Map<String, MapArtifactDataDefinition>, TitanOperationStatus> instanceArt = getDataFromGraph(componentV, EdgeLabelEnum.INSTANCE_ARTIFACTS);
710 if (instanceArt.isLeft()) {
711 toscaElement.setInstanceArtifacts(instanceArt.left().value());
713 if (instanceArt.right().value() != TitanOperationStatus.NOT_FOUND) {
714 return instanceArt.right().value();
717 return TitanOperationStatus.OK;
720 private TitanOperationStatus setInputsFromGraph(GraphVertex componentV, TopologyTemplate toscaElement) {
721 Either<Map<String, PropertyDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.INPUTS);
722 if (result.isLeft()) {
723 toscaElement.setInputs(result.left().value());
725 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
726 return result.right().value();
729 return TitanOperationStatus.OK;
732 private TitanOperationStatus setGroupsFromGraph(GraphVertex componentV, TopologyTemplate toscaElement) {
733 Either<Map<String, GroupDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.GROUPS);
734 if (result.isLeft()) {
735 toscaElement.setGroups(result.left().value());
737 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
738 return result.right().value();
741 return TitanOperationStatus.OK;
744 private TitanOperationStatus setTopologyTempalteCategoriesFromGraph(GraphVertex componentV, ToscaElement toscaElement) {
745 List<CategoryDefinition> categories = new ArrayList<>();
747 switch (componentV.getType()) {
749 return setResourceCategoryFromGraph(componentV, toscaElement);
751 return setServiceCategoryFromGraph(componentV, toscaElement, categories);
754 log.debug("Not supported component type {} ", componentV.getType());
757 return TitanOperationStatus.OK;
760 private TitanOperationStatus setServiceCategoryFromGraph(GraphVertex componentV, ToscaElement toscaElement, List<CategoryDefinition> categories) {
761 Either<GraphVertex, TitanOperationStatus> childVertex = titanDao.getChildVertex(componentV, EdgeLabelEnum.CATEGORY, JsonParseFlagEnum.NoParse);
762 if (childVertex.isRight()) {
763 log.debug("failed to fetch {} for tosca element with id {}, error {}", EdgeLabelEnum.CATEGORY, componentV.getUniqueId(), childVertex.right().value());
764 return childVertex.right().value();
766 GraphVertex categoryV = childVertex.left().value();
767 Map<GraphPropertyEnum, Object> metadataProperties = categoryV.getMetadataProperties();
768 CategoryDefinition category = new CategoryDefinition();
769 category.setUniqueId((String) metadataProperties.get(GraphPropertyEnum.UNIQUE_ID));
770 category.setNormalizedName((String) metadataProperties.get(GraphPropertyEnum.NORMALIZED_NAME));
771 category.setName((String) metadataProperties.get(GraphPropertyEnum.NAME));
773 Type listTypeCat = new TypeToken<List<String>>() {
775 List<String> iconsfromJsonCat = getGson().fromJson((String) metadataProperties.get(GraphPropertyEnum.ICONS.getProperty()), listTypeCat);
776 category.setIcons(iconsfromJsonCat);
777 categories.add(category);
778 toscaElement.setCategories(categories);
780 return TitanOperationStatus.OK;
783 private TopologyTemplate convertToTopologyTemplate(GraphVertex componentV) {
785 TopologyTemplate topologyTemplate = super.convertToComponent(componentV);
787 Map<String, CompositionDataDefinition> json = (Map<String, CompositionDataDefinition>) componentV.getJson();
788 topologyTemplate.setCompositions(json);
790 return topologyTemplate;
794 public Either<ToscaElement, StorageOperationStatus> deleteToscaElement(GraphVertex toscaElementVertex) {
795 Either<ToscaElement, StorageOperationStatus> nodeType = getToscaElement(toscaElementVertex, new ComponentParametersView());
796 if (nodeType.isRight()) {
797 log.debug("Failed to fetch tosca element {} error {}", toscaElementVertex.getUniqueId(), nodeType.right().value());
800 TitanOperationStatus status = disassociateAndDeleteCommonElements(toscaElementVertex);
801 if (status != TitanOperationStatus.OK) {
802 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
804 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_ATTRIBUTES);
805 if (status != TitanOperationStatus.OK) {
806 log.debug("Failed to disassociate instances attributes for {} error {}", toscaElementVertex.getUniqueId(), status);
807 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
809 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_PROPERTIES);
810 if (status != TitanOperationStatus.OK) {
811 log.debug("Failed to disassociate instances properties for {} error {}", toscaElementVertex.getUniqueId(), status);
812 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
815 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_INPUTS);
816 if (status != TitanOperationStatus.OK) {
817 log.debug("Failed to disassociate instances inputs for {} error {}", toscaElementVertex.getUniqueId(), status);
818 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
821 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.GROUPS);
822 if (status != TitanOperationStatus.OK) {
823 log.debug("Failed to disassociate groups for {} error {}", toscaElementVertex.getUniqueId(), status);
824 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
826 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_GROUPS);
827 if (status != TitanOperationStatus.OK) {
828 log.debug("Failed to disassociate instance groups for {} error {}", toscaElementVertex.getUniqueId(), status);
829 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
831 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INPUTS);
832 if (status != TitanOperationStatus.OK) {
833 log.debug("Failed to disassociate inputs for {} error {}", toscaElementVertex.getUniqueId(), status);
834 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
836 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_INPUTS);
837 if (status != TitanOperationStatus.OK) {
838 log.debug("Failed to disassociate instance inputs for {} error {}", toscaElementVertex.getUniqueId(), status);
839 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
841 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.CALCULATED_CAPABILITIES);
842 if (status != TitanOperationStatus.OK) {
843 log.debug("Failed to disassociate calculated capabiliites for {} error {}", toscaElementVertex.getUniqueId(), status);
844 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
846 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.FULLFILLED_CAPABILITIES);
847 if (status != TitanOperationStatus.OK) {
848 log.debug("Failed to disassociate fullfilled capabilities for {} error {}", toscaElementVertex.getUniqueId(), status);
849 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
851 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES);
852 if (status != TitanOperationStatus.OK) {
853 log.debug("Failed to disassociate calculated capabiliites properties for {} error {}", toscaElementVertex.getUniqueId(), status);
854 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
856 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.CALCULATED_REQUIREMENTS);
857 if (status != TitanOperationStatus.OK) {
858 log.debug("Failed to disassociate calculated requirements for {} error {}", toscaElementVertex.getUniqueId(), status);
859 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
861 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.FULLFILLED_REQUIREMENTS);
862 if (status != TitanOperationStatus.OK) {
863 log.debug("Failed to disassociate full filled requirements for {} error {}", toscaElementVertex.getUniqueId(), status);
864 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
866 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS);
867 if (status != TitanOperationStatus.OK) {
868 log.debug("Failed to disassociate instance artifacts for {} error {}", toscaElementVertex.getUniqueId(), status);
869 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
871 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.SERVICE_API_ARTIFACTS);
872 if (status != TitanOperationStatus.OK) {
873 log.debug("Failed to disassociate service api artifacts for {} error {}", toscaElementVertex.getUniqueId(), status);
874 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
876 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INSTANCE_ARTIFACTS);
877 toscaElementVertex.getVertex().remove();
878 log.trace("Tosca element vertex for {} was removed", toscaElementVertex.getUniqueId());
883 @SuppressWarnings("unchecked")
885 public Either<TopologyTemplate, StorageOperationStatus> createToscaElement(ToscaElement toscaElement) {
886 return createTopologyTemplate((TopologyTemplate) toscaElement);
890 protected <T extends ToscaElement> TitanOperationStatus setCategoriesFromGraph(GraphVertex vertexComponent, T toscaElement) {
891 return setTopologyTempalteCategoriesFromGraph(vertexComponent, toscaElement);
895 protected <T extends ToscaElement> StorageOperationStatus validateCategories(T toscaElementToUpdate, GraphVertex elementV) {
896 // Product isn't supported now!!
897 // TODO add for Product
898 if (toscaElementToUpdate.getComponentType() == ComponentTypeEnum.SERVICE) {
899 return validateServiceCategory(toscaElementToUpdate, elementV);
902 return validateResourceCategory(toscaElementToUpdate, elementV);
907 protected <T extends ToscaElement> StorageOperationStatus updateDerived(T toscaElementToUpdate, GraphVertex updateElementV) {
908 // not relevant now for topology template
909 return StorageOperationStatus.OK;
913 public <T extends ToscaElement> void fillToscaElementVertexData(GraphVertex elementV, T toscaElementToUpdate, JsonParseFlagEnum flag) {
914 fillMetadata(elementV, (TopologyTemplate) toscaElementToUpdate, flag);
917 private <T extends ToscaElement> StorageOperationStatus validateServiceCategory(T toscaElementToUpdate, GraphVertex elementV) {
918 StorageOperationStatus status = StorageOperationStatus.OK;
919 List<CategoryDefinition> newCategoryList = toscaElementToUpdate.getCategories();
920 CategoryDefinition newCategory = newCategoryList.get(0);
922 Either<GraphVertex, TitanOperationStatus> childVertex = titanDao.getChildVertex(elementV, EdgeLabelEnum.CATEGORY, JsonParseFlagEnum.NoParse);
923 if (childVertex.isRight()) {
924 log.debug("failed to fetch {} for tosca element with id {}, error {}", EdgeLabelEnum.CATEGORY, elementV.getUniqueId(), childVertex.right().value());
925 return DaoStatusConverter.convertTitanStatusToStorageStatus(childVertex.right().value());
928 GraphVertex categoryV = childVertex.left().value();
929 Map<GraphPropertyEnum, Object> metadataProperties = categoryV.getMetadataProperties();
930 String categoryNameCurrent = (String) metadataProperties.get(GraphPropertyEnum.NAME);
932 String newCategoryName = newCategory.getName();
933 if (newCategoryName != null && false == newCategoryName.equals(categoryNameCurrent)) {
934 // the category was changed
935 Either<GraphVertex, StorageOperationStatus> getCategoryVertex = categoryOperation.getCategory(newCategoryName, VertexTypeEnum.SERVICE_CATEGORY);
937 if (getCategoryVertex.isRight()) {
938 return getCategoryVertex.right().value();
940 GraphVertex newCategoryV = getCategoryVertex.left().value();
941 status = moveCategoryEdge(elementV, newCategoryV);
942 log.debug("Going to update the category of the resource from {} to {}. status is {}", categoryNameCurrent, newCategory, status);
947 public Either<List<GraphVertex>, TitanOperationStatus> getAllNotDeletedElements() {
948 Map<GraphPropertyEnum, Object> propsHasNot = new HashMap<>();
949 propsHasNot.put(GraphPropertyEnum.IS_DELETED, true);
951 Either<List<GraphVertex>, TitanOperationStatus> byCriteria = titanDao.getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, null, propsHasNot, JsonParseFlagEnum.ParseJson);
952 if (byCriteria.isRight()) {
953 log.debug("Failed to fetch all non marked topology templates , propsHasNot {}, error {}", propsHasNot, byCriteria.right().value());
954 return Either.right(byCriteria.right().value());
956 return Either.left(byCriteria.left().value());
959 public boolean isInUse(GraphVertex elementV, List<GraphVertex> allNonDeleted) {
960 for (GraphVertex containerV : allNonDeleted) {
961 Map<String, CompositionDataDefinition> composition = (Map<String, CompositionDataDefinition>) containerV.getJson();
962 if (composition != null) {
963 CompositionDataDefinition instances = composition.get(JsonConstantKeysEnum.COMPOSITION.getValue());
964 if (instances != null && instances.getComponentInstances() != null && !instances.getComponentInstances().isEmpty()) {
965 for (ComponentInstanceDataDefinition ci : instances.getComponentInstances().values()) {
966 if (ci.getComponentUid().equals(elementV.getUniqueId())) {
967 log.debug("The resource {} failed to delete cause in use as component instance UniqueID = {} in {} with UniqueID {}", elementV.getUniqueId(), ci.getUniqueId(), containerV.getType(), containerV.getUniqueId());
979 public boolean isInUse(String componentId, List<GraphVertex> allNonDeleted) {
980 for (GraphVertex containerV : allNonDeleted) {
981 Map<String, CompositionDataDefinition> composition = (Map<String, CompositionDataDefinition>) containerV.getJson();
982 if (composition != null) {
983 CompositionDataDefinition instances = composition.get(JsonConstantKeysEnum.COMPOSITION.getValue());
984 if (instances != null && instances.getComponentInstances() != null && !instances.getComponentInstances().isEmpty()) {
985 for (ComponentInstanceDataDefinition ci : instances.getComponentInstances().values()) {
986 if (ci.getComponentUid().equals(componentId)) {
998 public Either<GraphVertex, StorageOperationStatus> updateDistributionStatus(String uniqueId, User user, DistributionStatusEnum distributionStatus) {
1000 Either<GraphVertex, StorageOperationStatus> result = null;
1001 String userId = user.getUserId();
1002 Either<GraphVertex, TitanOperationStatus> getRes = findUserVertex(userId);
1003 GraphVertex userVertex = null;
1004 GraphVertex serviceVertex = null;
1005 if (getRes.isRight()) {
1006 TitanOperationStatus status = getRes.right().value();
1007 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Cannot find user {} in the graph. status is {}", userId, status);
1008 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1010 if (result == null) {
1011 userVertex = getRes.left().value();
1012 getRes = titanDao.getVertexById(uniqueId, JsonParseFlagEnum.ParseMetadata);
1013 if (getRes.isRight()) {
1014 TitanOperationStatus status = getRes.right().value();
1015 log.error("Cannot find service {} in the graph. status is {}", uniqueId, status);
1016 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1019 if (result == null) {
1020 serviceVertex = getRes.left().value();
1021 Iterator<Edge> edgeIterator = serviceVertex.getVertex().edges(Direction.IN, EdgeLabelEnum.LAST_DISTRIBUTION_STATE_MODIFIER.name());
1022 if (edgeIterator.hasNext()) {
1023 log.debug("Remove existing edge from user to component {}. Edge type is {}", userId, uniqueId, EdgeLabelEnum.LAST_DISTRIBUTION_STATE_MODIFIER);
1024 edgeIterator.next().remove();
1027 if (result == null) {
1028 TitanOperationStatus status = titanDao.createEdge(userVertex, serviceVertex, EdgeLabelEnum.LAST_DISTRIBUTION_STATE_MODIFIER, null);
1029 if (status != TitanOperationStatus.OK) {
1030 log.error("Failed to associate user {} to component {}. Edge type is {}", userId, uniqueId, EdgeLabelEnum.LAST_DISTRIBUTION_STATE_MODIFIER);
1031 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1034 if (result == null) {
1035 serviceVertex.addMetadataProperty(GraphPropertyEnum.DISTRIBUTION_STATUS, distributionStatus.name());
1036 long lastUpdateDate = System.currentTimeMillis();
1037 serviceVertex.setJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE, lastUpdateDate);
1038 Either<GraphVertex, TitanOperationStatus> updateRes = titanDao.updateVertex(serviceVertex);
1039 if (updateRes.isRight()) {
1040 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(updateRes.right().value()));
1043 if (result == null) {
1044 result = Either.left(serviceVertex);