2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.be.model.jsontitan.operations;
23 import java.lang.reflect.Type;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.Iterator;
27 import java.util.List;
29 import java.util.Optional;
30 import java.util.Map.Entry;
32 import org.apache.tinkerpop.gremlin.structure.Direction;
33 import org.apache.tinkerpop.gremlin.structure.Edge;
34 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
35 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
36 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
37 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
38 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
39 import org.openecomp.sdc.be.datatypes.elements.AdditionalInfoParameterDataDefinition;
40 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
41 import org.openecomp.sdc.be.datatypes.elements.ComponentInstanceDataDefinition;
42 import org.openecomp.sdc.be.datatypes.elements.CompositionDataDefinition;
43 import org.openecomp.sdc.be.datatypes.elements.GroupDataDefinition;
44 import org.openecomp.sdc.be.datatypes.elements.MapArtifactDataDefinition;
45 import org.openecomp.sdc.be.datatypes.elements.MapCapabiltyProperty;
46 import org.openecomp.sdc.be.datatypes.elements.MapGroupsDataDefinition;
47 import org.openecomp.sdc.be.datatypes.elements.MapListCapabiltyDataDefinition;
48 import org.openecomp.sdc.be.datatypes.elements.MapListRequirementDataDefinition;
49 import org.openecomp.sdc.be.datatypes.elements.MapPropertiesDataDefinition;
50 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
51 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
52 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
53 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
54 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
55 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
56 import org.openecomp.sdc.be.model.ComponentParametersView;
57 import org.openecomp.sdc.be.model.DistributionStatusEnum;
58 import org.openecomp.sdc.be.model.User;
59 import org.openecomp.sdc.be.model.category.CategoryDefinition;
60 import org.openecomp.sdc.be.model.jsontitan.datamodel.TopologyTemplate;
61 import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElement;
62 import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElementTypeEnum;
63 import org.openecomp.sdc.be.model.jsontitan.enums.JsonConstantKeysEnum;
64 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
65 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
66 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
67 import org.openecomp.sdc.common.jsongraph.util.CommonUtility;
68 import org.openecomp.sdc.common.jsongraph.util.CommonUtility.LogLevelEnum;
69 import org.slf4j.Logger;
70 import org.slf4j.LoggerFactory;
72 import com.google.gson.reflect.TypeToken;
74 import fj.data.Either;
76 @org.springframework.stereotype.Component("topology-template-operation")
77 public class TopologyTemplateOperation extends ToscaElementOperation {
78 private static Logger log = LoggerFactory.getLogger(TopologyTemplateOperation.class.getName());
80 public Either<TopologyTemplate, StorageOperationStatus> createTopologyTemplate(TopologyTemplate topologyTemplate) {
81 Either<TopologyTemplate, StorageOperationStatus> result = null;
83 topologyTemplate.generateUUID();
85 topologyTemplate = (TopologyTemplate) getResourceMetaDataFromResource(topologyTemplate);
86 String resourceUniqueId = topologyTemplate.getUniqueId();
87 if (resourceUniqueId == null) {
88 resourceUniqueId = UniqueIdBuilder.buildResourceUniqueId();
89 topologyTemplate.setUniqueId(resourceUniqueId);
92 GraphVertex topologyTemplateVertex = new GraphVertex();
93 topologyTemplateVertex = fillMetadata(topologyTemplateVertex, topologyTemplate, JsonParseFlagEnum.ParseAll);
95 Either<GraphVertex, TitanOperationStatus> createdVertex = titanDao.createVertex(topologyTemplateVertex);
96 if (createdVertex.isRight()) {
97 TitanOperationStatus status = createdVertex.right().value();
98 log.error("Error returned after creating topology template data node {}. status returned is ", topologyTemplateVertex, status);
99 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
103 topologyTemplateVertex = createdVertex.left().value();
105 StorageOperationStatus assosiateCommon = assosiateCommonForToscaElement(topologyTemplateVertex, topologyTemplate, null);
106 if (assosiateCommon != StorageOperationStatus.OK) {
107 result = Either.right(assosiateCommon);
111 StorageOperationStatus associateCategory = assosiateMetadataToCategory(topologyTemplateVertex, topologyTemplate);
112 if (associateCategory != StorageOperationStatus.OK) {
113 result = Either.right(associateCategory);
117 StorageOperationStatus associateInputs = associateInputsToComponent(topologyTemplateVertex, topologyTemplate);
118 if (associateInputs != StorageOperationStatus.OK) {
119 result = Either.right(associateInputs);
122 StorageOperationStatus associateGroups = associateGroupsToComponent(topologyTemplateVertex, topologyTemplate);
123 if (associateGroups != StorageOperationStatus.OK) {
124 result = Either.right(associateGroups);
127 StorageOperationStatus associateInstAttr = associateInstAttributesToComponent(topologyTemplateVertex, topologyTemplate);
128 if (associateInstAttr != StorageOperationStatus.OK) {
129 result = Either.right(associateInstAttr);
132 StorageOperationStatus associateInstProperties = associateInstPropertiesToComponent(topologyTemplateVertex, topologyTemplate);
133 if (associateInstProperties != StorageOperationStatus.OK) {
134 result = Either.right(associateInstProperties);
137 StorageOperationStatus associateInstInputs = associateInstInputsToComponent(topologyTemplateVertex, topologyTemplate);
138 if (associateInstProperties != StorageOperationStatus.OK) {
139 result = Either.right(associateInstInputs);
142 StorageOperationStatus associateInstGroups = associateInstGroupsToComponent(topologyTemplateVertex, topologyTemplate);
143 if (associateInstGroups != StorageOperationStatus.OK) {
144 result = Either.right(associateInstInputs);
148 StorageOperationStatus associateRequirements = associateRequirementsToResource(topologyTemplateVertex, topologyTemplate);
149 if (associateRequirements != StorageOperationStatus.OK) {
150 result = Either.right(associateRequirements);
154 StorageOperationStatus associateCapabilities = associateCapabilitiesToResource(topologyTemplateVertex, topologyTemplate);
155 if (associateCapabilities != StorageOperationStatus.OK) {
156 result = Either.right(associateCapabilities);
160 StorageOperationStatus associateArtifacts = associateTopologyTemplateArtifactsToComponent(topologyTemplateVertex, topologyTemplate);
161 if (associateArtifacts != StorageOperationStatus.OK) {
162 result = Either.right(associateArtifacts);
166 StorageOperationStatus addAdditionalInformation = addAdditionalInformationToResource(topologyTemplateVertex, topologyTemplate);
167 if (addAdditionalInformation != StorageOperationStatus.OK) {
168 result = Either.right(addAdditionalInformation);
171 StorageOperationStatus associateCapProperties = associateCapPropertiesToResource(topologyTemplateVertex, topologyTemplate);
172 if (associateCapProperties != StorageOperationStatus.OK) {
173 result = Either.right(associateCapProperties);
176 return Either.left(topologyTemplate);
180 private StorageOperationStatus associateCapPropertiesToResource(GraphVertex topologyTemplateVertex, TopologyTemplate topologyTemplate) {
181 Map<String, MapCapabiltyProperty> calculatedCapProperties = topologyTemplate.getCalculatedCapabilitiesProperties();
182 if (calculatedCapProperties != null && !calculatedCapProperties.isEmpty()) {
183 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(topologyTemplateVertex, VertexTypeEnum.CALCULATED_CAP_PROPERTIES, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES, calculatedCapProperties);
184 if (assosiateElementToData.isRight()) {
185 return assosiateElementToData.right().value();
188 return StorageOperationStatus.OK;
191 private StorageOperationStatus associateCapabilitiesToResource(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
192 Map<String, MapListCapabiltyDataDefinition> calculatedCapabilities = topologyTemplate.getCalculatedCapabilities();
193 if (calculatedCapabilities != null && !calculatedCapabilities.isEmpty()) {
194 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.CALCULATED_CAPABILITIES, EdgeLabelEnum.CALCULATED_CAPABILITIES, calculatedCapabilities);
195 if (assosiateElementToData.isRight()) {
196 return assosiateElementToData.right().value();
199 Map<String, MapListCapabiltyDataDefinition> fullfilledCapabilities = topologyTemplate.getFullfilledCapabilities();
200 if (fullfilledCapabilities != null && !fullfilledCapabilities.isEmpty()) {
201 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.FULLFILLED_CAPABILITIES, EdgeLabelEnum.FULLFILLED_CAPABILITIES, fullfilledCapabilities);
202 if (assosiateElementToData.isRight()) {
203 return assosiateElementToData.right().value();
206 return StorageOperationStatus.OK;
210 private StorageOperationStatus associateRequirementsToResource(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
211 Map<String, MapListRequirementDataDefinition> calculatedRequirements = topologyTemplate.getCalculatedRequirements();
212 if (calculatedRequirements != null && !calculatedRequirements.isEmpty()) {
213 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.CALCULATED_REQUIREMENTS, EdgeLabelEnum.CALCULATED_REQUIREMENTS, calculatedRequirements);
214 if (assosiateElementToData.isRight()) {
215 return assosiateElementToData.right().value();
218 Map<String, MapListRequirementDataDefinition> fullfilledRequirements = topologyTemplate.getFullfilledRequirements();
219 if (fullfilledRequirements != null && !fullfilledRequirements.isEmpty()) {
220 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.FULLFILLED_REQUIREMENTS, EdgeLabelEnum.FULLFILLED_REQUIREMENTS, fullfilledRequirements);
221 if (assosiateElementToData.isRight()) {
222 return assosiateElementToData.right().value();
225 return StorageOperationStatus.OK;
228 private StorageOperationStatus associateTopologyTemplateArtifactsToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
229 Map<String, ArtifactDataDefinition> addInformation = topologyTemplate.getServiceApiArtifacts();
231 if (addInformation != null && !addInformation.isEmpty()) {
232 addInformation.values().stream().filter(a -> a.getUniqueId() == null).forEach(a -> {
233 String uniqueId = UniqueIdBuilder.buildPropertyUniqueId(nodeTypeVertex.getUniqueId().toLowerCase(), a.getArtifactLabel().toLowerCase());
234 a.setUniqueId(uniqueId);
236 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.SERVICE_API_ARTIFACTS, EdgeLabelEnum.SERVICE_API_ARTIFACTS, addInformation);
237 if (assosiateElementToData.isRight()) {
238 return assosiateElementToData.right().value();
241 Map<String, MapArtifactDataDefinition> instArtifacts = topologyTemplate.getInstDeploymentArtifacts();
243 if (instArtifacts != null && !instArtifacts.isEmpty()) {
244 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.INST_DEPLOYMENT_ARTIFACTS, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS, instArtifacts);
245 if (assosiateElementToData.isRight()) {
246 return assosiateElementToData.right().value();
249 Map<String, MapArtifactDataDefinition> instInfoArtifacts = topologyTemplate.getInstanceArtifacts();
251 if (instInfoArtifacts != null && !instInfoArtifacts.isEmpty()) {
252 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.INSTANCE_ARTIFACTS, EdgeLabelEnum.INSTANCE_ARTIFACTS, instInfoArtifacts);
253 if (assosiateElementToData.isRight()) {
254 return assosiateElementToData.right().value();
257 return StorageOperationStatus.OK;
260 private StorageOperationStatus addAdditionalInformationToResource(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
262 Map<String, AdditionalInfoParameterDataDefinition> addInformation = topologyTemplate.getAdditionalInformation();
264 if (addInformation != null && !addInformation.isEmpty()) {
265 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.ADDITIONAL_INFORMATION, EdgeLabelEnum.ADDITIONAL_INFORMATION, addInformation);
266 if (assosiateElementToData.isRight()) {
267 return assosiateElementToData.right().value();
270 return StorageOperationStatus.OK;
273 public StorageOperationStatus associateInstPropertiesToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
274 Map<String, MapPropertiesDataDefinition> instProps = topologyTemplate.getInstProperties();
275 return associateInstPropertiesToComponent(nodeTypeVertex, instProps);
278 public StorageOperationStatus associateInstInputsToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
279 Map<String, MapPropertiesDataDefinition> instProps = topologyTemplate.getInstInputs();
280 return associateInstInputsToComponent(nodeTypeVertex, instProps);
283 public StorageOperationStatus associateInstGroupsToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
284 Map<String, MapGroupsDataDefinition> instGroups = topologyTemplate.getInstGroups();
285 return associateInstGroupsToComponent(nodeTypeVertex, instGroups);
289 public StorageOperationStatus associateInstPropertiesToComponent(GraphVertex nodeTypeVertex, Map<String, MapPropertiesDataDefinition> instProps) {
290 if (instProps != null && !instProps.isEmpty()) {
291 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.INST_PROPERTIES, EdgeLabelEnum.INST_PROPERTIES, instProps);
292 if (assosiateElementToData.isRight()) {
293 return assosiateElementToData.right().value();
296 return StorageOperationStatus.OK;
299 public StorageOperationStatus associateInstInputsToComponent(GraphVertex nodeTypeVertex, Map<String, MapPropertiesDataDefinition> instInputs) {
300 if (instInputs != null && !instInputs.isEmpty()) {
301 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.INST_INPUTS, EdgeLabelEnum.INST_INPUTS, instInputs);
302 if (assosiateElementToData.isRight()) {
303 return assosiateElementToData.right().value();
306 return StorageOperationStatus.OK;
309 public StorageOperationStatus associateInstGroupsToComponent(GraphVertex nodeTypeVertex, Map<String, MapGroupsDataDefinition> instGroups) {
310 if (instGroups != null && !instGroups.isEmpty()) {
311 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.INST_GROUPS, EdgeLabelEnum.INST_GROUPS, instGroups);
312 if (assosiateElementToData.isRight()) {
313 return assosiateElementToData.right().value();
316 return StorageOperationStatus.OK;
320 public StorageOperationStatus deleteInstInputsToComponent(GraphVertex nodeTypeVertex, Map<String, MapPropertiesDataDefinition> instInputs) {
322 if (instInputs != null && !instInputs.isEmpty()) {
323 instInputs.entrySet().forEach(i -> {
324 List<String> uniqueKeys = new ArrayList<String>(i.getValue().getMapToscaDataDefinition().keySet());
325 List<String> pathKeys = new ArrayList<String>();
326 pathKeys.add(i.getKey());
328 StorageOperationStatus status = deleteToscaDataDeepElements(nodeTypeVertex, EdgeLabelEnum.INST_INPUTS, VertexTypeEnum.INST_INPUTS, uniqueKeys, pathKeys, JsonPresentationFields.NAME);
329 if (status != StorageOperationStatus.OK) {
335 return StorageOperationStatus.OK;
338 public StorageOperationStatus addInstPropertiesToComponent(GraphVertex nodeTypeVertex, Map<String, MapPropertiesDataDefinition> instInputs) {
340 if (instInputs != null && !instInputs.isEmpty()) {
341 instInputs.entrySet().forEach(i -> {
342 StorageOperationStatus status = addToscaDataDeepElementsBlockToToscaElement(nodeTypeVertex, EdgeLabelEnum.INST_PROPERTIES, VertexTypeEnum.INST_PROPERTIES, i.getValue(), i.getKey());
343 if (status != StorageOperationStatus.OK) {
349 return StorageOperationStatus.OK;
352 public StorageOperationStatus associateInstDeploymentArtifactsToComponent(GraphVertex nodeTypeVertex, Map<String, MapArtifactDataDefinition> instArtifacts) {
353 return associateInstanceArtifactsToComponent(nodeTypeVertex, instArtifacts, VertexTypeEnum.INST_DEPLOYMENT_ARTIFACTS, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS);
356 public StorageOperationStatus associateInstArtifactsToComponent(GraphVertex nodeTypeVertex, Map<String, MapArtifactDataDefinition> instArtifacts) {
357 return associateInstanceArtifactsToComponent(nodeTypeVertex, instArtifacts, VertexTypeEnum.INSTANCE_ARTIFACTS, EdgeLabelEnum.INSTANCE_ARTIFACTS);
360 private StorageOperationStatus associateInstanceArtifactsToComponent(GraphVertex nodeTypeVertex, Map<String, MapArtifactDataDefinition> instProps, VertexTypeEnum vertexType, EdgeLabelEnum edgeLabel) {
361 if (instProps != null && !instProps.isEmpty()) {
362 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, vertexType, edgeLabel, instProps);
363 if (assosiateElementToData.isRight()) {
364 return assosiateElementToData.right().value();
367 return StorageOperationStatus.OK;
370 public StorageOperationStatus associateCalcCapReqToComponent(GraphVertex nodeTypeVertex, Map<String, MapListRequirementDataDefinition> calcRequirements, Map<String, MapListCapabiltyDataDefinition> calcCapabilty, Map<String, MapCapabiltyProperty> calculatedCapabilitiesProperties) {
371 if (calcRequirements != null && !calcRequirements.isEmpty()) {
372 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.CALCULATED_REQUIREMENTS, EdgeLabelEnum.CALCULATED_REQUIREMENTS, calcRequirements);
373 if (assosiateElementToData.isRight()) {
374 return assosiateElementToData.right().value();
376 Map<String, MapListRequirementDataDefinition> fullFilled = new HashMap<>();
377 assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.FULLFILLED_REQUIREMENTS, EdgeLabelEnum.FULLFILLED_REQUIREMENTS, fullFilled);
378 if (assosiateElementToData.isRight()) {
379 return assosiateElementToData.right().value();
382 if (calcCapabilty != null && !calcCapabilty.isEmpty()) {
383 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.CALCULATED_CAPABILITIES, EdgeLabelEnum.CALCULATED_CAPABILITIES, calcCapabilty);
384 if (assosiateElementToData.isRight()) {
385 return assosiateElementToData.right().value();
387 Map<String, MapListCapabiltyDataDefinition> fullFilled = new HashMap<>();
388 assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.FULLFILLED_CAPABILITIES, EdgeLabelEnum.FULLFILLED_CAPABILITIES, fullFilled);
389 if (assosiateElementToData.isRight()) {
390 return assosiateElementToData.right().value();
393 if ( calculatedCapabilitiesProperties != null && !calculatedCapabilitiesProperties.isEmpty() ){
394 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.CALCULATED_CAP_PROPERTIES, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES, calculatedCapabilitiesProperties);
395 if (assosiateElementToData.isRight()) {
396 return assosiateElementToData.right().value();
399 return StorageOperationStatus.OK;
402 private StorageOperationStatus associateInstAttributesToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
403 Map<String, MapPropertiesDataDefinition> instAttr = topologyTemplate.getInstAttributes();
404 return associateInstAttributeToComponent(nodeTypeVertex, instAttr);
407 public StorageOperationStatus associateInstAttributeToComponent(GraphVertex nodeTypeVertex, Map<String, MapPropertiesDataDefinition> instAttr) {
408 if (instAttr != null && !instAttr.isEmpty()) {
409 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.INST_ATTRIBUTES, EdgeLabelEnum.INST_ATTRIBUTES, instAttr);
410 if (assosiateElementToData.isRight()) {
411 return assosiateElementToData.right().value();
414 return StorageOperationStatus.OK;
417 public StorageOperationStatus associateGroupsToComponent(GraphVertex nodeTypeVertex, Map<String, GroupDataDefinition> groups) {
419 if (groups != null && !groups.isEmpty()) {
420 groups.values().stream().filter(p -> p.getUniqueId() == null).forEach(p -> {
421 String uid = UniqueIdBuilder.buildGroupingUid(nodeTypeVertex.getUniqueId(), p.getName());
424 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.GROUPS, EdgeLabelEnum.GROUPS, groups);
425 if (assosiateElementToData.isRight()) {
426 return assosiateElementToData.right().value();
429 return StorageOperationStatus.OK;
432 private StorageOperationStatus associateGroupsToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
433 return associateGroupsToComponent(nodeTypeVertex, topologyTemplate.getGroups());
436 public StorageOperationStatus associateInputsToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
437 Map<String, PropertyDataDefinition> inputs = topologyTemplate.getInputs();
438 return associateInputsToComponent(nodeTypeVertex, inputs, topologyTemplate.getUniqueId());
441 public StorageOperationStatus associateInputsToComponent(GraphVertex nodeTypeVertex, Map<String, PropertyDataDefinition> inputs, String id) {
442 if (inputs != null && !inputs.isEmpty()) {
443 inputs.values().stream().filter(e -> e.getUniqueId() == null).forEach(e -> e.setUniqueId(UniqueIdBuilder.buildPropertyUniqueId(id, e.getName())));
445 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.INPUTS, EdgeLabelEnum.INPUTS, inputs);
446 if (assosiateElementToData.isRight()) {
447 return assosiateElementToData.right().value();
450 return StorageOperationStatus.OK;
453 private GraphVertex fillMetadata(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate, JsonParseFlagEnum flag) {
454 nodeTypeVertex.setLabel(VertexTypeEnum.TOPOLOGY_TEMPLATE);
455 fillCommonMetadata(nodeTypeVertex, topologyTemplate);
456 if (flag == JsonParseFlagEnum.ParseAll || flag == JsonParseFlagEnum.ParseJson) {
457 nodeTypeVertex.setJson(topologyTemplate.getCompositions());
459 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.CSAR_UUID, topologyTemplate.getMetadataValue(JsonPresentationFields.CSAR_UUID));
460 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.DISTRIBUTION_STATUS, topologyTemplate.getMetadataValue(JsonPresentationFields.DISTRIBUTION_STATUS));
462 return nodeTypeVertex;
466 private StorageOperationStatus assosiateMetadataToCategory(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
467 if (topologyTemplate.getResourceType() == null) {
469 return associateServiceMetadataToCategory(nodeTypeVertex, topologyTemplate);
472 return assosiateResourceMetadataToCategory(nodeTypeVertex, topologyTemplate);
476 private StorageOperationStatus associateServiceMetadataToCategory(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
477 String categoryName = topologyTemplate.getCategories().get(0).getName();
478 Either<GraphVertex, StorageOperationStatus> category = categoryOperation.getCategory(categoryName, VertexTypeEnum.SERVICE_CATEGORY);
479 if (category.isRight()) {
480 log.trace("NO category {} for service {}", categoryName, topologyTemplate.getUniqueId());
481 return StorageOperationStatus.CATEGORY_NOT_FOUND;
483 GraphVertex categoryV = category.left().value();
484 TitanOperationStatus createEdge = titanDao.createEdge(nodeTypeVertex, categoryV, EdgeLabelEnum.CATEGORY, new HashMap<>());
485 if (createEdge != TitanOperationStatus.OK) {
486 log.trace("Failed to associate resource {} to category {} with id {}", topologyTemplate.getUniqueId(), categoryName, categoryV.getUniqueId());
487 return DaoStatusConverter.convertTitanStatusToStorageStatus(createEdge);
489 return StorageOperationStatus.OK;
493 public Either<ToscaElement, StorageOperationStatus> getToscaElement(String uniqueId, ComponentParametersView componentParametersView) {
494 JsonParseFlagEnum parseFlag = componentParametersView.detectParseFlag();
496 Either<GraphVertex, StorageOperationStatus> componentByLabelAndId = getComponentByLabelAndId(uniqueId, ToscaElementTypeEnum.TopologyTemplate, parseFlag);
497 if (componentByLabelAndId.isRight()) {
498 return Either.right(componentByLabelAndId.right().value());
500 GraphVertex componentV = componentByLabelAndId.left().value();
502 return getToscaElement(componentV, componentParametersView);
505 // -------------------------------------------------------------
507 public Either<ToscaElement, StorageOperationStatus> getToscaElement(GraphVertex componentV, ComponentParametersView componentParametersView) {
508 TopologyTemplate toscaElement;
510 toscaElement = convertToTopologyTemplate(componentV);
511 TitanOperationStatus status = null;
512 if (false == componentParametersView.isIgnoreUsers()) {
513 status = setCreatorFromGraph(componentV, toscaElement);
514 if (status != TitanOperationStatus.OK) {
515 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
518 status = setLastModifierFromGraph(componentV, toscaElement);
519 if (status != TitanOperationStatus.OK) {
520 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
523 if (false == componentParametersView.isIgnoreCategories()) {
524 status = setTopologyTempalteCategoriesFromGraph(componentV, toscaElement);
525 if (status != TitanOperationStatus.OK) {
526 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
530 if (false == componentParametersView.isIgnoreArtifacts()) {
531 TitanOperationStatus storageStatus = setAllArtifactsFromGraph(componentV, toscaElement);
532 if (storageStatus != TitanOperationStatus.OK) {
533 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(storageStatus));
536 if (false == componentParametersView.isIgnoreComponentInstancesProperties()) {
537 status = setComponentInstancesPropertiesFromGraph(componentV, toscaElement);
538 if (status != TitanOperationStatus.OK) {
539 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
542 if (false == componentParametersView.isIgnoreCapabilities()) {
543 status = setCapabilitiesFromGraph(componentV, toscaElement);
544 if (status != TitanOperationStatus.OK) {
545 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
548 if (false == componentParametersView.isIgnoreRequirements()) {
549 status = setRequirementsFromGraph(componentV, toscaElement);
550 if (status != TitanOperationStatus.OK) {
551 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
554 if (false == componentParametersView.isIgnoreAllVersions()) {
555 status = setAllVersions(componentV, toscaElement);
556 if (status != TitanOperationStatus.OK) {
557 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
560 if (false == componentParametersView.isIgnoreAdditionalInformation()) {
561 status = setAdditionalInformationFromGraph(componentV, toscaElement);
562 if (status != TitanOperationStatus.OK) {
563 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
567 if (false == componentParametersView.isIgnoreGroups()) {
568 status = setGroupsFromGraph(componentV, toscaElement);
569 if (status != TitanOperationStatus.OK) {
570 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
574 if (false == componentParametersView.isIgnoreComponentInstances()) {
575 status = setInstGroupsFromGraph(componentV, toscaElement);
576 if (status != TitanOperationStatus.OK) {
577 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
581 if (false == componentParametersView.isIgnoreInputs()) {
582 status = setInputsFromGraph(componentV, toscaElement);
583 if (status != TitanOperationStatus.OK) {
584 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
588 if (false == componentParametersView.isIgnoreProperties()) {
589 status = setPropertiesFromGraph(componentV, toscaElement);
590 if (status != TitanOperationStatus.OK) {
591 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
596 if (false == componentParametersView.isIgnoreComponentInstancesInputs()) {
597 status = setComponentInstancesInputsFromGraph(componentV, toscaElement);
598 if (status != TitanOperationStatus.OK) {
599 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
604 if (false == componentParametersView.isIgnoreCapabiltyProperties()) {
605 status = setComponentInstancesCapPropertiesFromGraph(componentV, toscaElement);
606 if (status != TitanOperationStatus.OK) {
607 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
611 return Either.left(toscaElement);
614 private TitanOperationStatus setComponentInstancesCapPropertiesFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
615 Either<Map<String, MapCapabiltyProperty>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES);
616 if (result.isLeft()) {
617 topologyTemplate.setCalculatedCapabilitiesProperties(result.left().value());
619 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
620 return result.right().value();
623 return TitanOperationStatus.OK;
626 private TitanOperationStatus setPropertiesFromGraph(GraphVertex componentV, TopologyTemplate toscaElement) {
627 Either<Map<String, PropertyDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.PROPERTIES);
628 if (result.isLeft()) {
629 toscaElement.setProperties(result.left().value());
631 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
632 return result.right().value();
635 return TitanOperationStatus.OK;
638 private TitanOperationStatus setInstGroupsFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
639 Either<Map<String, MapGroupsDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.INST_GROUPS);
640 if (result.isLeft()) {
641 topologyTemplate.setInstGroups(result.left().value());
643 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
644 return result.right().value();
647 return TitanOperationStatus.OK;
650 private TitanOperationStatus setComponentInstancesPropertiesFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
651 Either<Map<String, MapPropertiesDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.INST_PROPERTIES);
652 if (result.isLeft()) {
653 topologyTemplate.setInstProperties(result.left().value());
655 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
656 return result.right().value();
659 return TitanOperationStatus.OK;
662 private TitanOperationStatus setComponentInstancesInputsFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
663 Either<Map<String, MapPropertiesDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.INST_INPUTS);
664 if (result.isLeft()) {
665 topologyTemplate.setInstInputs(result.left().value());
667 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
668 return result.right().value();
671 return TitanOperationStatus.OK;
675 protected <T extends ToscaElement> TitanOperationStatus setRequirementsFromGraph(GraphVertex componentV, T toscaElement) {
676 Either<Map<String, MapListRequirementDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.CALCULATED_REQUIREMENTS);
677 if (result.isLeft()) {
678 ((TopologyTemplate) toscaElement).setCalculatedRequirements(result.left().value());
680 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
681 return result.right().value();
684 result = getDataFromGraph(componentV, EdgeLabelEnum.FULLFILLED_REQUIREMENTS);
685 if (result.isLeft()) {
686 ((TopologyTemplate) toscaElement).setFullfilledRequirements(result.left().value());
688 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
689 return result.right().value();
692 return TitanOperationStatus.OK;
696 protected <T extends ToscaElement> TitanOperationStatus setCapabilitiesFromGraph(GraphVertex componentV, T toscaElement) {
697 Either<Map<String, MapListCapabiltyDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.CALCULATED_CAPABILITIES);
698 if (result.isLeft()) {
699 ((TopologyTemplate) toscaElement).setCalculatedCapabilities(result.left().value());
701 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
702 return result.right().value();
705 result = getDataFromGraph(componentV, EdgeLabelEnum.FULLFILLED_CAPABILITIES);
706 if (result.isLeft()) {
707 ((TopologyTemplate) toscaElement).setFullfilledCapabilities(result.left().value());
709 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
710 return result.right().value();
713 return TitanOperationStatus.OK;
716 private TitanOperationStatus setAllArtifactsFromGraph(GraphVertex componentV, TopologyTemplate toscaElement) {
717 TitanOperationStatus storageStatus = setArtifactsFromGraph(componentV, toscaElement);
718 if (storageStatus != TitanOperationStatus.OK) {
719 return storageStatus;
721 Either<Map<String, ArtifactDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.SERVICE_API_ARTIFACTS);
722 if (result.isLeft()) {
723 toscaElement.setServiceApiArtifacts(result.left().value());
725 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
726 return result.right().value();
729 Either<Map<String, MapArtifactDataDefinition>, TitanOperationStatus> resultInstArt = getDataFromGraph(componentV, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS);
730 if (resultInstArt.isLeft()) {
731 toscaElement.setInstDeploymentArtifacts(resultInstArt.left().value());
733 if (resultInstArt.right().value() != TitanOperationStatus.NOT_FOUND) {
734 return resultInstArt.right().value();
737 Either<Map<String, MapArtifactDataDefinition>, TitanOperationStatus> instanceArt = getDataFromGraph(componentV, EdgeLabelEnum.INSTANCE_ARTIFACTS);
738 if (instanceArt.isLeft()) {
739 toscaElement.setInstanceArtifacts(instanceArt.left().value());
741 if (instanceArt.right().value() != TitanOperationStatus.NOT_FOUND) {
742 return instanceArt.right().value();
745 return TitanOperationStatus.OK;
748 private TitanOperationStatus setInputsFromGraph(GraphVertex componentV, TopologyTemplate toscaElement) {
749 Either<Map<String, PropertyDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.INPUTS);
750 if (result.isLeft()) {
751 toscaElement.setInputs(result.left().value());
753 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
754 return result.right().value();
757 return TitanOperationStatus.OK;
760 private TitanOperationStatus setGroupsFromGraph(GraphVertex componentV, TopologyTemplate toscaElement) {
761 Either<Map<String, GroupDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.GROUPS);
762 if (result.isLeft()) {
763 toscaElement.setGroups(result.left().value());
765 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
766 return result.right().value();
769 return TitanOperationStatus.OK;
772 private TitanOperationStatus setTopologyTempalteCategoriesFromGraph(GraphVertex componentV, ToscaElement toscaElement) {
773 List<CategoryDefinition> categories = new ArrayList<>();
775 switch (componentV.getType()) {
777 return setResourceCategoryFromGraph(componentV, toscaElement);
779 return setServiceCategoryFromGraph(componentV, toscaElement, categories);
782 log.debug("Not supported component type {} ", componentV.getType());
785 return TitanOperationStatus.OK;
788 private TitanOperationStatus setServiceCategoryFromGraph(GraphVertex componentV, ToscaElement toscaElement, List<CategoryDefinition> categories) {
789 Either<GraphVertex, TitanOperationStatus> childVertex = titanDao.getChildVertex(componentV, EdgeLabelEnum.CATEGORY, JsonParseFlagEnum.NoParse);
790 if (childVertex.isRight()) {
791 log.debug("failed to fetch {} for tosca element with id {}, error {}", EdgeLabelEnum.CATEGORY, componentV.getUniqueId(), childVertex.right().value());
792 return childVertex.right().value();
794 GraphVertex categoryV = childVertex.left().value();
795 Map<GraphPropertyEnum, Object> metadataProperties = categoryV.getMetadataProperties();
796 CategoryDefinition category = new CategoryDefinition();
797 category.setUniqueId(categoryV.getUniqueId());
798 category.setNormalizedName((String) metadataProperties.get(GraphPropertyEnum.NORMALIZED_NAME));
799 category.setName((String) metadataProperties.get(GraphPropertyEnum.NAME));
801 Type listTypeCat = new TypeToken<List<String>>() {}.getType();
802 List<String> iconsfromJsonCat = getGson().fromJson((String) metadataProperties.get(GraphPropertyEnum.ICONS.getProperty()), listTypeCat);
803 category.setIcons(iconsfromJsonCat);
804 categories.add(category);
805 toscaElement.setCategories(categories);
807 return TitanOperationStatus.OK;
810 private TopologyTemplate convertToTopologyTemplate(GraphVertex componentV) {
812 TopologyTemplate topologyTemplate = super.convertToComponent(componentV);
814 Map<String, CompositionDataDefinition> json = (Map<String, CompositionDataDefinition>) componentV.getJson();
815 topologyTemplate.setCompositions(json);
817 return topologyTemplate;
821 public Either<ToscaElement, StorageOperationStatus> deleteToscaElement(GraphVertex toscaElementVertex) {
822 Either<ToscaElement, StorageOperationStatus> nodeType = getToscaElement(toscaElementVertex, new ComponentParametersView());
823 if (nodeType.isRight()) {
824 log.debug("Failed to fetch tosca element {} error {}", toscaElementVertex.getUniqueId(), nodeType.right().value());
827 TitanOperationStatus status = disassociateAndDeleteCommonElements(toscaElementVertex);
828 if (status != TitanOperationStatus.OK) {
829 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
831 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_ATTRIBUTES);
832 if (status != TitanOperationStatus.OK) {
833 log.debug("Failed to disassociate instances attributes for {} error {}", toscaElementVertex.getUniqueId(), status);
834 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
836 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_PROPERTIES);
837 if (status != TitanOperationStatus.OK) {
838 log.debug("Failed to disassociate instances properties for {} error {}", toscaElementVertex.getUniqueId(), status);
839 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
842 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_INPUTS);
843 if (status != TitanOperationStatus.OK) {
844 log.debug("Failed to disassociate instances inputs for {} error {}", toscaElementVertex.getUniqueId(), status);
845 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
848 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.GROUPS);
849 if (status != TitanOperationStatus.OK) {
850 log.debug("Failed to disassociate groups for {} error {}", toscaElementVertex.getUniqueId(), status);
851 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
853 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_GROUPS);
854 if (status != TitanOperationStatus.OK) {
855 log.debug("Failed to disassociate instance groups for {} error {}", toscaElementVertex.getUniqueId(), status);
856 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
858 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INPUTS);
859 if (status != TitanOperationStatus.OK) {
860 log.debug("Failed to disassociate inputs for {} error {}", toscaElementVertex.getUniqueId(), status);
861 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
863 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_INPUTS);
864 if (status != TitanOperationStatus.OK) {
865 log.debug("Failed to disassociate instance inputs for {} error {}", toscaElementVertex.getUniqueId(), status);
866 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
868 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.CALCULATED_CAPABILITIES);
869 if (status != TitanOperationStatus.OK) {
870 log.debug("Failed to disassociate calculated capabiliites for {} error {}", toscaElementVertex.getUniqueId(), status);
871 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
873 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.FULLFILLED_CAPABILITIES);
874 if (status != TitanOperationStatus.OK) {
875 log.debug("Failed to disassociate fullfilled capabilities for {} error {}", toscaElementVertex.getUniqueId(), status);
876 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
878 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES);
879 if (status != TitanOperationStatus.OK) {
880 log.debug("Failed to disassociate calculated capabiliites properties for {} error {}", toscaElementVertex.getUniqueId(), status);
881 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
883 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.CALCULATED_REQUIREMENTS);
884 if (status != TitanOperationStatus.OK) {
885 log.debug("Failed to disassociate calculated requirements for {} error {}", toscaElementVertex.getUniqueId(), status);
886 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
888 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.FULLFILLED_REQUIREMENTS);
889 if (status != TitanOperationStatus.OK) {
890 log.debug("Failed to disassociate full filled requirements for {} error {}", toscaElementVertex.getUniqueId(), status);
891 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
893 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS);
894 if (status != TitanOperationStatus.OK) {
895 log.debug("Failed to disassociate instance artifacts for {} error {}", toscaElementVertex.getUniqueId(), status);
896 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
898 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.SERVICE_API_ARTIFACTS);
899 if (status != TitanOperationStatus.OK) {
900 log.debug("Failed to disassociate service api artifacts for {} error {}", toscaElementVertex.getUniqueId(), status);
901 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
903 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INSTANCE_ARTIFACTS);
904 toscaElementVertex.getVertex().remove();
905 log.trace("Tosca element vertex for {} was removed", toscaElementVertex.getUniqueId());
910 @SuppressWarnings("unchecked")
912 public Either<TopologyTemplate, StorageOperationStatus> createToscaElement(ToscaElement toscaElement) {
913 return createTopologyTemplate((TopologyTemplate) toscaElement);
917 protected <T extends ToscaElement> TitanOperationStatus setCategoriesFromGraph(GraphVertex vertexComponent, T toscaElement) {
918 return setTopologyTempalteCategoriesFromGraph(vertexComponent, toscaElement);
922 protected <T extends ToscaElement> StorageOperationStatus validateCategories(T toscaElementToUpdate, GraphVertex elementV) {
923 // Product isn't supported now!!
924 // TODO add for Product
925 if (toscaElementToUpdate.getComponentType() == ComponentTypeEnum.SERVICE) {
926 return validateServiceCategory(toscaElementToUpdate, elementV);
929 return validateResourceCategory(toscaElementToUpdate, elementV);
934 protected <T extends ToscaElement> StorageOperationStatus updateDerived(T toscaElementToUpdate, GraphVertex updateElementV) {
935 // not relevant now for topology template
936 return StorageOperationStatus.OK;
940 public <T extends ToscaElement> void fillToscaElementVertexData(GraphVertex elementV, T toscaElementToUpdate, JsonParseFlagEnum flag) {
941 fillMetadata(elementV, (TopologyTemplate) toscaElementToUpdate, flag);
944 private <T extends ToscaElement> StorageOperationStatus validateServiceCategory(T toscaElementToUpdate, GraphVertex elementV) {
945 StorageOperationStatus status = StorageOperationStatus.OK;
946 List<CategoryDefinition> newCategoryList = toscaElementToUpdate.getCategories();
947 CategoryDefinition newCategory = newCategoryList.get(0);
949 Either<GraphVertex, TitanOperationStatus> childVertex = titanDao.getChildVertex(elementV, EdgeLabelEnum.CATEGORY, JsonParseFlagEnum.NoParse);
950 if (childVertex.isRight()) {
951 log.debug("failed to fetch {} for tosca element with id {}, error {}", EdgeLabelEnum.CATEGORY, elementV.getUniqueId(), childVertex.right().value());
952 return DaoStatusConverter.convertTitanStatusToStorageStatus(childVertex.right().value());
955 GraphVertex categoryV = childVertex.left().value();
956 Map<GraphPropertyEnum, Object> metadataProperties = categoryV.getMetadataProperties();
957 String categoryNameCurrent = (String) metadataProperties.get(GraphPropertyEnum.NAME);
959 String newCategoryName = newCategory.getName();
960 if (newCategoryName != null && false == newCategoryName.equals(categoryNameCurrent)) {
961 // the category was changed
962 Either<GraphVertex, StorageOperationStatus> getCategoryVertex = categoryOperation.getCategory(newCategoryName, VertexTypeEnum.SERVICE_CATEGORY);
964 if (getCategoryVertex.isRight()) {
965 return getCategoryVertex.right().value();
967 GraphVertex newCategoryV = getCategoryVertex.left().value();
968 status = moveCategoryEdge(elementV, newCategoryV);
969 log.debug("Going to update the category of the resource from {} to {}. status is {}", categoryNameCurrent, newCategory, status);
974 public Either<List<GraphVertex>, TitanOperationStatus> getAllNotDeletedElements() {
975 Map<GraphPropertyEnum, Object> propsHasNot = new HashMap<>();
976 propsHasNot.put(GraphPropertyEnum.IS_DELETED, true);
978 Either<List<GraphVertex>, TitanOperationStatus> byCriteria = titanDao.getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, null, propsHasNot, JsonParseFlagEnum.ParseJson);
979 if (byCriteria.isRight()) {
980 log.debug("Failed to fetch all non marked topology templates , propsHasNot {}, error {}", propsHasNot, byCriteria.right().value());
981 return Either.right(byCriteria.right().value());
983 return Either.left(byCriteria.left().value());
986 public boolean isInUse(GraphVertex elementV, List<GraphVertex> allNonDeleted) {
987 for (GraphVertex containerV : allNonDeleted) {
988 Map<String, CompositionDataDefinition> composition = (Map<String, CompositionDataDefinition>) containerV.getJson();
989 if (composition != null) {
990 CompositionDataDefinition instances = composition.get(JsonConstantKeysEnum.COMPOSITION.getValue());
991 if (instances != null && instances.getComponentInstances() != null && !instances.getComponentInstances().isEmpty()) {
992 for (ComponentInstanceDataDefinition ci : instances.getComponentInstances().values()) {
993 if (ci.getComponentUid().equals(elementV.getUniqueId())) {
994 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());
1006 public boolean isInUse(String componentId, List<GraphVertex> allNonDeleted) {
1007 for (GraphVertex containerV : allNonDeleted) {
1008 Map<String, CompositionDataDefinition> composition = (Map<String, CompositionDataDefinition>) containerV.getJson();
1009 if (composition != null) {
1010 CompositionDataDefinition instances = composition.get(JsonConstantKeysEnum.COMPOSITION.getValue());
1011 if (instances != null && instances.getComponentInstances() != null && !instances.getComponentInstances().isEmpty()) {
1012 for (ComponentInstanceDataDefinition ci : instances.getComponentInstances().values()) {
1013 if (ci.getComponentUid().equals(componentId)) {
1025 public Either<GraphVertex, StorageOperationStatus> updateDistributionStatus(String uniqueId, User user, DistributionStatusEnum distributionStatus) {
1027 Either<GraphVertex, StorageOperationStatus> result = null;
1028 String userId = user.getUserId();
1029 Either<GraphVertex, TitanOperationStatus> getRes = findUserVertex(userId);
1030 GraphVertex userVertex = null;
1031 GraphVertex serviceVertex = null;
1032 if (getRes.isRight()) {
1033 TitanOperationStatus status = getRes.right().value();
1034 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Cannot find user {} in the graph. status is {}", userId, status);
1035 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1037 if (result == null) {
1038 userVertex = getRes.left().value();
1039 getRes = titanDao.getVertexById(uniqueId, JsonParseFlagEnum.ParseMetadata);
1040 if (getRes.isRight()) {
1041 TitanOperationStatus status = getRes.right().value();
1042 log.error("Cannot find service {} in the graph. status is {}", uniqueId, status);
1043 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1046 if (result == null) {
1047 serviceVertex = getRes.left().value();
1048 Iterator<Edge> edgeIterator = serviceVertex.getVertex().edges(Direction.IN, EdgeLabelEnum.LAST_DISTRIBUTION_STATE_MODIFIER.name());
1049 if (edgeIterator.hasNext()) {
1050 log.debug("Remove existing edge from user to component {}. Edge type is {}", userId, uniqueId, EdgeLabelEnum.LAST_DISTRIBUTION_STATE_MODIFIER);
1051 edgeIterator.next().remove();
1054 if (result == null) {
1055 TitanOperationStatus status = titanDao.createEdge(userVertex, serviceVertex, EdgeLabelEnum.LAST_DISTRIBUTION_STATE_MODIFIER, null);
1056 if (status != TitanOperationStatus.OK) {
1057 log.error("Failed to associate user {} to component {}. Edge type is {}", userId, uniqueId, EdgeLabelEnum.LAST_DISTRIBUTION_STATE_MODIFIER);
1058 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1061 if (result == null) {
1062 serviceVertex.addMetadataProperty(GraphPropertyEnum.DISTRIBUTION_STATUS, distributionStatus.name());
1063 long lastUpdateDate = System.currentTimeMillis();
1064 serviceVertex.setJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE, lastUpdateDate);
1065 Either<GraphVertex, TitanOperationStatus> updateRes = titanDao.updateVertex(serviceVertex);
1066 if (updateRes.isRight()) {
1067 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(updateRes.right().value()));
1070 if (result == null) {
1071 result = Either.left(serviceVertex);