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.Map.Entry;
30 import java.util.stream.Collectors;
32 import org.apache.commons.collections.MapUtils;
33 import org.apache.tinkerpop.gremlin.structure.Direction;
34 import org.apache.tinkerpop.gremlin.structure.Edge;
35 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
36 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
37 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
38 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
39 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
40 import org.openecomp.sdc.be.datatypes.elements.AdditionalInfoParameterDataDefinition;
41 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
42 import org.openecomp.sdc.be.datatypes.elements.ComponentInstanceDataDefinition;
43 import org.openecomp.sdc.be.datatypes.elements.CompositionDataDefinition;
44 import org.openecomp.sdc.be.datatypes.elements.GroupDataDefinition;
45 import org.openecomp.sdc.be.datatypes.elements.MapArtifactDataDefinition;
46 import org.openecomp.sdc.be.datatypes.elements.MapCapabiltyProperty;
47 import org.openecomp.sdc.be.datatypes.elements.MapGroupsDataDefinition;
48 import org.openecomp.sdc.be.datatypes.elements.MapListCapabiltyDataDefinition;
49 import org.openecomp.sdc.be.datatypes.elements.MapListRequirementDataDefinition;
50 import org.openecomp.sdc.be.datatypes.elements.MapPropertiesDataDefinition;
51 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
52 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
53 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
54 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
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.jsontitan.utils.ModelConverter;
65 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
66 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
67 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
68 import org.openecomp.sdc.common.jsongraph.util.CommonUtility;
69 import org.openecomp.sdc.common.jsongraph.util.CommonUtility.LogLevelEnum;
70 import org.slf4j.Logger;
71 import org.slf4j.LoggerFactory;
73 import com.google.gson.reflect.TypeToken;
75 import fj.data.Either;
77 @org.springframework.stereotype.Component("topology-template-operation")
78 public class TopologyTemplateOperation extends ToscaElementOperation {
79 private static Logger log = LoggerFactory.getLogger(TopologyTemplateOperation.class.getName());
81 public Either<TopologyTemplate, StorageOperationStatus> createTopologyTemplate(TopologyTemplate topologyTemplate) {
82 Either<TopologyTemplate, StorageOperationStatus> result = null;
84 topologyTemplate.generateUUID();
86 topologyTemplate = (TopologyTemplate) getResourceMetaDataFromResource(topologyTemplate);
87 String resourceUniqueId = topologyTemplate.getUniqueId();
88 if (resourceUniqueId == null) {
89 resourceUniqueId = UniqueIdBuilder.buildResourceUniqueId();
90 topologyTemplate.setUniqueId(resourceUniqueId);
93 GraphVertex topologyTemplateVertex = new GraphVertex();
94 topologyTemplateVertex = fillMetadata(topologyTemplateVertex, topologyTemplate, JsonParseFlagEnum.ParseAll);
96 Either<GraphVertex, TitanOperationStatus> createdVertex = titanDao.createVertex(topologyTemplateVertex);
97 if (createdVertex.isRight()) {
98 TitanOperationStatus status = createdVertex.right().value();
99 log.error("Error returned after creating topology template data node {}. status returned is ", topologyTemplateVertex, status);
100 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
104 topologyTemplateVertex = createdVertex.left().value();
106 StorageOperationStatus assosiateCommon = assosiateCommonForToscaElement(topologyTemplateVertex, topologyTemplate, null);
107 if (assosiateCommon != StorageOperationStatus.OK) {
108 result = Either.right(assosiateCommon);
112 StorageOperationStatus associateCategory = assosiateMetadataToCategory(topologyTemplateVertex, topologyTemplate);
113 if (associateCategory != StorageOperationStatus.OK) {
114 result = Either.right(associateCategory);
118 StorageOperationStatus associateInputs = associateInputsToComponent(topologyTemplateVertex, topologyTemplate);
119 if (associateInputs != StorageOperationStatus.OK) {
120 result = Either.right(associateInputs);
123 StorageOperationStatus associateGroups = associateGroupsToComponent(topologyTemplateVertex, topologyTemplate);
124 if (associateGroups != StorageOperationStatus.OK) {
125 result = Either.right(associateGroups);
128 StorageOperationStatus associateInstAttr = associateInstAttributesToComponent(topologyTemplateVertex, topologyTemplate);
129 if (associateInstAttr != StorageOperationStatus.OK) {
130 result = Either.right(associateInstAttr);
133 StorageOperationStatus associateInstProperties = associateInstPropertiesToComponent(topologyTemplateVertex, topologyTemplate);
134 if (associateInstProperties != StorageOperationStatus.OK) {
135 result = Either.right(associateInstProperties);
138 StorageOperationStatus associateInstInputs = associateInstInputsToComponent(topologyTemplateVertex, topologyTemplate);
139 if (associateInstProperties != StorageOperationStatus.OK) {
140 result = Either.right(associateInstInputs);
143 StorageOperationStatus associateInstGroups = associateInstGroupsToComponent(topologyTemplateVertex, topologyTemplate);
144 if (associateInstGroups != StorageOperationStatus.OK) {
145 result = Either.right(associateInstInputs);
149 StorageOperationStatus associateRequirements = associateRequirementsToResource(topologyTemplateVertex, topologyTemplate);
150 if (associateRequirements != StorageOperationStatus.OK) {
151 result = Either.right(associateRequirements);
155 StorageOperationStatus associateCapabilities = associateCapabilitiesToResource(topologyTemplateVertex, topologyTemplate);
156 if (associateCapabilities != StorageOperationStatus.OK) {
157 result = Either.right(associateCapabilities);
161 StorageOperationStatus associateArtifacts = associateTopologyTemplateArtifactsToComponent(topologyTemplateVertex, topologyTemplate);
162 if (associateArtifacts != StorageOperationStatus.OK) {
163 result = Either.right(associateArtifacts);
167 StorageOperationStatus addAdditionalInformation = addAdditionalInformationToResource(topologyTemplateVertex, topologyTemplate);
168 if (addAdditionalInformation != StorageOperationStatus.OK) {
169 result = Either.right(addAdditionalInformation);
172 StorageOperationStatus associateCapProperties = associateCapPropertiesToResource(topologyTemplateVertex, topologyTemplate);
173 if (associateCapProperties != StorageOperationStatus.OK) {
174 result = Either.right(associateCapProperties);
177 return Either.left(topologyTemplate);
181 private StorageOperationStatus associateCapPropertiesToResource(GraphVertex topologyTemplateVertex, TopologyTemplate topologyTemplate) {
182 Map<String, MapCapabiltyProperty> calculatedCapProperties = topologyTemplate.getCalculatedCapabilitiesProperties();
183 if (calculatedCapProperties != null && !calculatedCapProperties.isEmpty()) {
184 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(topologyTemplateVertex, VertexTypeEnum.CALCULATED_CAP_PROPERTIES, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES, calculatedCapProperties);
185 if (assosiateElementToData.isRight()) {
186 return assosiateElementToData.right().value();
189 return StorageOperationStatus.OK;
192 private StorageOperationStatus associateCapabilitiesToResource(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
193 Map<String, MapListCapabiltyDataDefinition> calculatedCapabilities = topologyTemplate.getCalculatedCapabilities();
194 if (calculatedCapabilities != null && !calculatedCapabilities.isEmpty()) {
195 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.CALCULATED_CAPABILITIES, EdgeLabelEnum.CALCULATED_CAPABILITIES, calculatedCapabilities);
196 if (assosiateElementToData.isRight()) {
197 return assosiateElementToData.right().value();
200 Map<String, MapListCapabiltyDataDefinition> fullfilledCapabilities = topologyTemplate.getFullfilledCapabilities();
201 if (fullfilledCapabilities != null && !fullfilledCapabilities.isEmpty()) {
202 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.FULLFILLED_CAPABILITIES, EdgeLabelEnum.FULLFILLED_CAPABILITIES, fullfilledCapabilities);
203 if (assosiateElementToData.isRight()) {
204 return assosiateElementToData.right().value();
207 return StorageOperationStatus.OK;
211 private StorageOperationStatus associateRequirementsToResource(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
212 Map<String, MapListRequirementDataDefinition> calculatedRequirements = topologyTemplate.getCalculatedRequirements();
213 if (calculatedRequirements != null && !calculatedRequirements.isEmpty()) {
214 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.CALCULATED_REQUIREMENTS, EdgeLabelEnum.CALCULATED_REQUIREMENTS, calculatedRequirements);
215 if (assosiateElementToData.isRight()) {
216 return assosiateElementToData.right().value();
219 Map<String, MapListRequirementDataDefinition> fullfilledRequirements = topologyTemplate.getFullfilledRequirements();
220 if (fullfilledRequirements != null && !fullfilledRequirements.isEmpty()) {
221 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.FULLFILLED_REQUIREMENTS, EdgeLabelEnum.FULLFILLED_REQUIREMENTS, fullfilledRequirements);
222 if (assosiateElementToData.isRight()) {
223 return assosiateElementToData.right().value();
226 return StorageOperationStatus.OK;
229 private StorageOperationStatus associateTopologyTemplateArtifactsToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
230 Map<String, ArtifactDataDefinition> addInformation = topologyTemplate.getServiceApiArtifacts();
232 if (addInformation != null && !addInformation.isEmpty()) {
233 addInformation.values().stream().filter(a -> a.getUniqueId() == null).forEach(a -> {
234 String uniqueId = UniqueIdBuilder.buildPropertyUniqueId(nodeTypeVertex.getUniqueId().toLowerCase(), a.getArtifactLabel().toLowerCase());
235 a.setUniqueId(uniqueId);
237 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.SERVICE_API_ARTIFACTS, EdgeLabelEnum.SERVICE_API_ARTIFACTS, addInformation);
238 if (assosiateElementToData.isRight()) {
239 return assosiateElementToData.right().value();
242 Map<String, MapArtifactDataDefinition> instArtifacts = topologyTemplate.getInstDeploymentArtifacts();
244 if (instArtifacts != null && !instArtifacts.isEmpty()) {
245 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.INST_DEPLOYMENT_ARTIFACTS, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS, instArtifacts);
246 if (assosiateElementToData.isRight()) {
247 return assosiateElementToData.right().value();
250 Map<String, MapArtifactDataDefinition> instInfoArtifacts = topologyTemplate.getInstanceArtifacts();
252 if (instInfoArtifacts != null && !instInfoArtifacts.isEmpty()) {
253 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.INSTANCE_ARTIFACTS, EdgeLabelEnum.INSTANCE_ARTIFACTS, instInfoArtifacts);
254 if (assosiateElementToData.isRight()) {
255 return assosiateElementToData.right().value();
258 return StorageOperationStatus.OK;
261 private StorageOperationStatus addAdditionalInformationToResource(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
263 Map<String, AdditionalInfoParameterDataDefinition> addInformation = topologyTemplate.getAdditionalInformation();
265 if (addInformation != null && !addInformation.isEmpty()) {
266 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.ADDITIONAL_INFORMATION, EdgeLabelEnum.ADDITIONAL_INFORMATION, addInformation);
267 if (assosiateElementToData.isRight()) {
268 return assosiateElementToData.right().value();
271 return StorageOperationStatus.OK;
274 public StorageOperationStatus associateInstPropertiesToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
275 Map<String, MapPropertiesDataDefinition> instProps = topologyTemplate.getInstProperties();
276 return associateInstPropertiesToComponent(nodeTypeVertex, instProps);
279 public StorageOperationStatus associateInstInputsToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
280 Map<String, MapPropertiesDataDefinition> instProps = topologyTemplate.getInstInputs();
281 return associateInstInputsToComponent(nodeTypeVertex, instProps);
284 public StorageOperationStatus associateInstGroupsToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
285 Map<String, MapGroupsDataDefinition> instGroups = topologyTemplate.getInstGroups();
286 return associateInstGroupsToComponent(nodeTypeVertex, instGroups);
290 public StorageOperationStatus associateInstPropertiesToComponent(GraphVertex nodeTypeVertex, Map<String, MapPropertiesDataDefinition> instProps) {
291 if (instProps != null && !instProps.isEmpty()) {
292 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.INST_PROPERTIES, EdgeLabelEnum.INST_PROPERTIES, instProps);
293 if (assosiateElementToData.isRight()) {
294 return assosiateElementToData.right().value();
297 return StorageOperationStatus.OK;
300 public StorageOperationStatus associateInstInputsToComponent(GraphVertex nodeTypeVertex, Map<String, MapPropertiesDataDefinition> instInputs) {
301 if (instInputs != null && !instInputs.isEmpty()) {
302 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.INST_INPUTS, EdgeLabelEnum.INST_INPUTS, instInputs);
303 if (assosiateElementToData.isRight()) {
304 return assosiateElementToData.right().value();
307 return StorageOperationStatus.OK;
310 public StorageOperationStatus associateInstGroupsToComponent(GraphVertex nodeTypeVertex, Map<String, MapGroupsDataDefinition> instGroups) {
311 if (instGroups != null && !instGroups.isEmpty()) {
312 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.INST_GROUPS, EdgeLabelEnum.INST_GROUPS, instGroups);
313 if (assosiateElementToData.isRight()) {
314 return assosiateElementToData.right().value();
317 return StorageOperationStatus.OK;
321 public StorageOperationStatus deleteInstInputsToComponent(GraphVertex nodeTypeVertex, Map<String, MapPropertiesDataDefinition> instInputs) {
323 if (instInputs != null && !instInputs.isEmpty()) {
324 instInputs.entrySet().forEach(i -> {
325 List<String> uniqueKeys = new ArrayList<String>(i.getValue().getMapToscaDataDefinition().keySet());
326 List<String> pathKeys = new ArrayList<String>();
327 pathKeys.add(i.getKey());
329 StorageOperationStatus status = deleteToscaDataDeepElements(nodeTypeVertex, EdgeLabelEnum.INST_INPUTS, VertexTypeEnum.INST_INPUTS, uniqueKeys, pathKeys, JsonPresentationFields.NAME);
330 if (status != StorageOperationStatus.OK) {
336 return StorageOperationStatus.OK;
339 public StorageOperationStatus addInstPropertiesToComponent(GraphVertex nodeTypeVertex, Map<String, MapPropertiesDataDefinition> instInputs) {
341 if (instInputs != null && !instInputs.isEmpty()) {
342 instInputs.entrySet().forEach(i -> {
343 StorageOperationStatus status = addToscaDataDeepElementsBlockToToscaElement(nodeTypeVertex, EdgeLabelEnum.INST_PROPERTIES, VertexTypeEnum.INST_PROPERTIES, i.getValue(), i.getKey());
344 if (status != StorageOperationStatus.OK) {
350 return StorageOperationStatus.OK;
353 public StorageOperationStatus associateInstDeploymentArtifactsToComponent(GraphVertex nodeTypeVertex, Map<String, MapArtifactDataDefinition> instArtifacts) {
354 return associateInstanceArtifactsToComponent(nodeTypeVertex, instArtifacts, VertexTypeEnum.INST_DEPLOYMENT_ARTIFACTS, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS);
357 public StorageOperationStatus associateInstArtifactsToComponent(GraphVertex nodeTypeVertex, Map<String, MapArtifactDataDefinition> instArtifacts) {
358 return associateInstanceArtifactsToComponent(nodeTypeVertex, instArtifacts, VertexTypeEnum.INSTANCE_ARTIFACTS, EdgeLabelEnum.INSTANCE_ARTIFACTS);
361 private StorageOperationStatus associateInstanceArtifactsToComponent(GraphVertex nodeTypeVertex, Map<String, MapArtifactDataDefinition> instProps, VertexTypeEnum vertexType, EdgeLabelEnum edgeLabel) {
362 if (instProps != null && !instProps.isEmpty()) {
363 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, vertexType, edgeLabel, instProps);
364 if (assosiateElementToData.isRight()) {
365 return assosiateElementToData.right().value();
368 return StorageOperationStatus.OK;
371 public StorageOperationStatus associateCalcCapReqToComponent(GraphVertex nodeTypeVertex, Map<String, MapListRequirementDataDefinition> calcRequirements, Map<String, MapListCapabiltyDataDefinition> calcCapabilty, Map<String, MapCapabiltyProperty> calculatedCapabilitiesProperties) {
372 if (calcRequirements != null && !calcRequirements.isEmpty()) {
373 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.CALCULATED_REQUIREMENTS, EdgeLabelEnum.CALCULATED_REQUIREMENTS, calcRequirements);
374 if (assosiateElementToData.isRight()) {
375 return assosiateElementToData.right().value();
377 Map<String, MapListRequirementDataDefinition> fullFilled = new HashMap<>();
378 assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.FULLFILLED_REQUIREMENTS, EdgeLabelEnum.FULLFILLED_REQUIREMENTS, fullFilled);
379 if (assosiateElementToData.isRight()) {
380 return assosiateElementToData.right().value();
383 if (calcCapabilty != null && !calcCapabilty.isEmpty()) {
384 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.CALCULATED_CAPABILITIES, EdgeLabelEnum.CALCULATED_CAPABILITIES, calcCapabilty);
385 if (assosiateElementToData.isRight()) {
386 return assosiateElementToData.right().value();
388 Map<String, MapListCapabiltyDataDefinition> fullFilled = new HashMap<>();
389 assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.FULLFILLED_CAPABILITIES, EdgeLabelEnum.FULLFILLED_CAPABILITIES, fullFilled);
390 if (assosiateElementToData.isRight()) {
391 return assosiateElementToData.right().value();
394 if ( calculatedCapabilitiesProperties != null && !calculatedCapabilitiesProperties.isEmpty() ){
395 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.CALCULATED_CAP_PROPERTIES, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES, calculatedCapabilitiesProperties);
396 if (assosiateElementToData.isRight()) {
397 return assosiateElementToData.right().value();
400 return StorageOperationStatus.OK;
403 private StorageOperationStatus associateInstAttributesToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
404 Map<String, MapPropertiesDataDefinition> instAttr = topologyTemplate.getInstAttributes();
405 return associateInstAttributeToComponent(nodeTypeVertex, instAttr);
408 public StorageOperationStatus associateInstAttributeToComponent(GraphVertex nodeTypeVertex, Map<String, MapPropertiesDataDefinition> instAttr) {
409 if (instAttr != null && !instAttr.isEmpty()) {
410 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.INST_ATTRIBUTES, EdgeLabelEnum.INST_ATTRIBUTES, instAttr);
411 if (assosiateElementToData.isRight()) {
412 return assosiateElementToData.right().value();
415 return StorageOperationStatus.OK;
418 public StorageOperationStatus associateGroupsToComponent(GraphVertex nodeTypeVertex, Map<String, GroupDataDefinition> groups) {
420 if (groups != null && !groups.isEmpty()) {
421 groups.values().stream().filter(p -> p.getUniqueId() == null).forEach(p -> {
422 String uid = UniqueIdBuilder.buildGroupingUid(nodeTypeVertex.getUniqueId(), p.getName());
425 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.GROUPS, EdgeLabelEnum.GROUPS, groups);
426 if (assosiateElementToData.isRight()) {
427 return assosiateElementToData.right().value();
430 return StorageOperationStatus.OK;
433 private StorageOperationStatus associateGroupsToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
434 return associateGroupsToComponent(nodeTypeVertex, topologyTemplate.getGroups());
437 public StorageOperationStatus associateInputsToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
438 Map<String, PropertyDataDefinition> inputs = topologyTemplate.getInputs();
439 return associateInputsToComponent(nodeTypeVertex, inputs, topologyTemplate.getUniqueId());
442 public StorageOperationStatus associateInputsToComponent(GraphVertex nodeTypeVertex, Map<String, PropertyDataDefinition> inputs, String id) {
443 if (inputs != null && !inputs.isEmpty()) {
444 inputs.values().stream().filter(e -> e.getUniqueId() == null).forEach(e -> e.setUniqueId(UniqueIdBuilder.buildPropertyUniqueId(id, e.getName())));
446 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.INPUTS, EdgeLabelEnum.INPUTS, inputs);
447 if (assosiateElementToData.isRight()) {
448 return assosiateElementToData.right().value();
451 return StorageOperationStatus.OK;
454 private GraphVertex fillMetadata(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate, JsonParseFlagEnum flag) {
455 nodeTypeVertex.setLabel(VertexTypeEnum.TOPOLOGY_TEMPLATE);
456 fillCommonMetadata(nodeTypeVertex, topologyTemplate);
457 if (flag == JsonParseFlagEnum.ParseAll || flag == JsonParseFlagEnum.ParseJson) {
458 nodeTypeVertex.setJson(topologyTemplate.getCompositions());
460 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.CSAR_UUID, topologyTemplate.getMetadataValue(JsonPresentationFields.CSAR_UUID));
461 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.DISTRIBUTION_STATUS, topologyTemplate.getMetadataValue(JsonPresentationFields.DISTRIBUTION_STATUS));
463 return nodeTypeVertex;
467 private StorageOperationStatus assosiateMetadataToCategory(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
468 if (topologyTemplate.getResourceType() == null) {
470 return associateServiceMetadataToCategory(nodeTypeVertex, topologyTemplate);
473 return assosiateResourceMetadataToCategory(nodeTypeVertex, topologyTemplate);
477 private StorageOperationStatus associateServiceMetadataToCategory(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
478 String categoryName = topologyTemplate.getCategories().get(0).getName();
479 Either<GraphVertex, StorageOperationStatus> category = categoryOperation.getCategory(categoryName, VertexTypeEnum.SERVICE_CATEGORY);
480 if (category.isRight()) {
481 log.trace("NO category {} for service {}", categoryName, topologyTemplate.getUniqueId());
482 return StorageOperationStatus.CATEGORY_NOT_FOUND;
484 GraphVertex categoryV = category.left().value();
485 TitanOperationStatus createEdge = titanDao.createEdge(nodeTypeVertex, categoryV, EdgeLabelEnum.CATEGORY, new HashMap<>());
486 if (createEdge != TitanOperationStatus.OK) {
487 log.trace("Failed to associate resource {} to category {} with id {}", topologyTemplate.getUniqueId(), categoryName, categoryV.getUniqueId());
488 return DaoStatusConverter.convertTitanStatusToStorageStatus(createEdge);
490 return StorageOperationStatus.OK;
494 public Either<ToscaElement, StorageOperationStatus> getToscaElement(String uniqueId, ComponentParametersView componentParametersView) {
495 JsonParseFlagEnum parseFlag = componentParametersView.detectParseFlag();
497 Either<GraphVertex, StorageOperationStatus> componentByLabelAndId = getComponentByLabelAndId(uniqueId, ToscaElementTypeEnum.TopologyTemplate, parseFlag);
498 if (componentByLabelAndId.isRight()) {
499 return Either.right(componentByLabelAndId.right().value());
501 GraphVertex componentV = componentByLabelAndId.left().value();
503 return getToscaElement(componentV, componentParametersView);
506 // -------------------------------------------------------------
508 public Either<ToscaElement, StorageOperationStatus> getToscaElement(GraphVertex componentV, ComponentParametersView componentParametersView) {
509 TopologyTemplate toscaElement;
511 toscaElement = convertToTopologyTemplate(componentV);
512 TitanOperationStatus status = null;
513 if (false == componentParametersView.isIgnoreUsers()) {
514 status = setCreatorFromGraph(componentV, toscaElement);
515 if (status != TitanOperationStatus.OK) {
516 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
519 status = setLastModifierFromGraph(componentV, toscaElement);
520 if (status != TitanOperationStatus.OK) {
521 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
524 if (false == componentParametersView.isIgnoreCategories()) {
525 status = setTopologyTempalteCategoriesFromGraph(componentV, toscaElement);
526 if (status != TitanOperationStatus.OK) {
527 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
531 if (false == componentParametersView.isIgnoreArtifacts()) {
532 TitanOperationStatus storageStatus = setAllArtifactsFromGraph(componentV, toscaElement);
533 if (storageStatus != TitanOperationStatus.OK) {
534 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(storageStatus));
537 if (false == componentParametersView.isIgnoreComponentInstancesProperties()) {
538 status = setComponentInstancesPropertiesFromGraph(componentV, toscaElement);
539 if (status != TitanOperationStatus.OK) {
540 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
543 if (false == componentParametersView.isIgnoreCapabilities()) {
544 status = setCapabilitiesFromGraph(componentV, toscaElement);
545 if (status != TitanOperationStatus.OK) {
546 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
549 if (false == componentParametersView.isIgnoreRequirements()) {
550 status = setRequirementsFromGraph(componentV, toscaElement);
551 if (status != TitanOperationStatus.OK) {
552 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
555 if (false == componentParametersView.isIgnoreAllVersions()) {
556 status = setAllVersions(componentV, toscaElement);
557 if (status != TitanOperationStatus.OK) {
558 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
561 if (false == componentParametersView.isIgnoreAdditionalInformation()) {
562 status = setAdditionalInformationFromGraph(componentV, toscaElement);
563 if (status != TitanOperationStatus.OK) {
564 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
568 if (false == componentParametersView.isIgnoreGroups()) {
569 status = setGroupsFromGraph(componentV, toscaElement);
570 if (status != TitanOperationStatus.OK) {
571 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
575 if (false == componentParametersView.isIgnoreComponentInstances()) {
576 status = setInstGroupsFromGraph(componentV, toscaElement);
577 if (status != TitanOperationStatus.OK) {
578 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
582 if (false == componentParametersView.isIgnoreInputs()) {
583 status = setInputsFromGraph(componentV, toscaElement);
584 if (status != TitanOperationStatus.OK) {
585 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
589 if (false == componentParametersView.isIgnoreProperties()) {
590 status = setPropertiesFromGraph(componentV, toscaElement);
591 if (status != TitanOperationStatus.OK) {
592 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
597 if (false == componentParametersView.isIgnoreComponentInstancesInputs()) {
598 status = setComponentInstancesInputsFromGraph(componentV, toscaElement);
599 if (status != TitanOperationStatus.OK) {
600 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
605 if (false == componentParametersView.isIgnoreCapabiltyProperties()) {
606 status = setComponentInstancesCapPropertiesFromGraph(componentV, toscaElement);
607 if (status != TitanOperationStatus.OK) {
608 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
612 return Either.left(toscaElement);
615 private TitanOperationStatus setComponentInstancesCapPropertiesFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
616 Either<Map<String, MapCapabiltyProperty>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES);
617 if (result.isLeft()) {
618 topologyTemplate.setCalculatedCapabilitiesProperties(result.left().value());
620 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
621 return result.right().value();
624 return TitanOperationStatus.OK;
627 private TitanOperationStatus setPropertiesFromGraph(GraphVertex componentV, TopologyTemplate toscaElement) {
628 Either<Map<String, PropertyDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.PROPERTIES);
629 if (result.isLeft()) {
630 toscaElement.setProperties(result.left().value());
632 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
633 return result.right().value();
636 return TitanOperationStatus.OK;
639 private TitanOperationStatus setInstGroupsFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
640 Either<Map<String, MapGroupsDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.INST_GROUPS);
641 if (result.isLeft()) {
642 topologyTemplate.setInstGroups(result.left().value());
644 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
645 return result.right().value();
648 return TitanOperationStatus.OK;
651 private TitanOperationStatus setComponentInstancesPropertiesFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
652 Either<Map<String, MapPropertiesDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.INST_PROPERTIES);
653 if (result.isLeft()) {
654 topologyTemplate.setInstProperties(result.left().value());
656 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
657 return result.right().value();
660 return TitanOperationStatus.OK;
663 private TitanOperationStatus setComponentInstancesInputsFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
664 Either<Map<String, MapPropertiesDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.INST_INPUTS);
665 if (result.isLeft()) {
666 topologyTemplate.setInstInputs(result.left().value());
668 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
669 return result.right().value();
672 return TitanOperationStatus.OK;
676 protected <T extends ToscaElement> TitanOperationStatus setRequirementsFromGraph(GraphVertex componentV, T toscaElement) {
677 Either<Map<String, MapListRequirementDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.CALCULATED_REQUIREMENTS);
678 if (result.isLeft()) {
679 ((TopologyTemplate) toscaElement).setCalculatedRequirements(result.left().value());
681 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
682 return result.right().value();
685 result = getDataFromGraph(componentV, EdgeLabelEnum.FULLFILLED_REQUIREMENTS);
686 if (result.isLeft()) {
687 ((TopologyTemplate) toscaElement).setFullfilledRequirements(result.left().value());
689 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
690 return result.right().value();
693 return TitanOperationStatus.OK;
697 protected <T extends ToscaElement> TitanOperationStatus setCapabilitiesFromGraph(GraphVertex componentV, T toscaElement) {
698 Either<Map<String, MapListCapabiltyDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.CALCULATED_CAPABILITIES);
699 if (result.isLeft()) {
700 ((TopologyTemplate) toscaElement).setCalculatedCapabilities(result.left().value());
702 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
703 return result.right().value();
706 result = getDataFromGraph(componentV, EdgeLabelEnum.FULLFILLED_CAPABILITIES);
707 if (result.isLeft()) {
708 ((TopologyTemplate) toscaElement).setFullfilledCapabilities(result.left().value());
710 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
711 return result.right().value();
714 return TitanOperationStatus.OK;
717 private TitanOperationStatus setAllArtifactsFromGraph(GraphVertex componentV, TopologyTemplate toscaElement) {
718 TitanOperationStatus storageStatus = setArtifactsFromGraph(componentV, toscaElement);
719 if (storageStatus != TitanOperationStatus.OK) {
720 return storageStatus;
722 Either<Map<String, ArtifactDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.SERVICE_API_ARTIFACTS);
723 if (result.isLeft()) {
724 toscaElement.setServiceApiArtifacts(result.left().value());
726 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
727 return result.right().value();
730 Either<Map<String, MapArtifactDataDefinition>, TitanOperationStatus> resultInstArt = getDataFromGraph(componentV, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS);
731 if (resultInstArt.isLeft()) {
732 toscaElement.setInstDeploymentArtifacts(resultInstArt.left().value());
734 if (resultInstArt.right().value() != TitanOperationStatus.NOT_FOUND) {
735 return resultInstArt.right().value();
738 Either<Map<String, MapArtifactDataDefinition>, TitanOperationStatus> instanceArt = getDataFromGraph(componentV, EdgeLabelEnum.INSTANCE_ARTIFACTS);
739 if (instanceArt.isLeft()) {
740 toscaElement.setInstanceArtifacts(instanceArt.left().value());
742 if (instanceArt.right().value() != TitanOperationStatus.NOT_FOUND) {
743 return instanceArt.right().value();
746 return TitanOperationStatus.OK;
749 private TitanOperationStatus setInputsFromGraph(GraphVertex componentV, TopologyTemplate toscaElement) {
750 Either<Map<String, PropertyDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.INPUTS);
751 if (result.isLeft()) {
752 toscaElement.setInputs(result.left().value());
754 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
755 return result.right().value();
758 return TitanOperationStatus.OK;
761 private TitanOperationStatus setGroupsFromGraph(GraphVertex componentV, TopologyTemplate toscaElement) {
762 Either<Map<String, GroupDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.GROUPS);
763 if (result.isLeft()) {
764 toscaElement.setGroups(result.left().value());
766 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
767 return result.right().value();
770 return TitanOperationStatus.OK;
773 private TitanOperationStatus setTopologyTempalteCategoriesFromGraph(GraphVertex componentV, ToscaElement toscaElement) {
774 List<CategoryDefinition> categories = new ArrayList<>();
776 switch (componentV.getType()) {
778 return setResourceCategoryFromGraph(componentV, toscaElement);
780 return setServiceCategoryFromGraph(componentV, toscaElement, categories);
783 log.debug("Not supported component type {} ", componentV.getType());
786 return TitanOperationStatus.OK;
789 private TitanOperationStatus setServiceCategoryFromGraph(GraphVertex componentV, ToscaElement toscaElement, List<CategoryDefinition> categories) {
790 Either<GraphVertex, TitanOperationStatus> childVertex = titanDao.getChildVertex(componentV, EdgeLabelEnum.CATEGORY, JsonParseFlagEnum.NoParse);
791 if (childVertex.isRight()) {
792 log.debug("failed to fetch {} for tosca element with id {}, error {}", EdgeLabelEnum.CATEGORY, componentV.getUniqueId(), childVertex.right().value());
793 return childVertex.right().value();
795 GraphVertex categoryV = childVertex.left().value();
796 Map<GraphPropertyEnum, Object> metadataProperties = categoryV.getMetadataProperties();
797 CategoryDefinition category = new CategoryDefinition();
798 category.setUniqueId(categoryV.getUniqueId());
799 category.setNormalizedName((String) metadataProperties.get(GraphPropertyEnum.NORMALIZED_NAME));
800 category.setName((String) metadataProperties.get(GraphPropertyEnum.NAME));
802 Type listTypeCat = new TypeToken<List<String>>() {}.getType();
803 List<String> iconsfromJsonCat = getGson().fromJson((String) metadataProperties.get(GraphPropertyEnum.ICONS.getProperty()), listTypeCat);
804 category.setIcons(iconsfromJsonCat);
805 categories.add(category);
806 toscaElement.setCategories(categories);
808 return TitanOperationStatus.OK;
811 private TopologyTemplate convertToTopologyTemplate(GraphVertex componentV) {
813 TopologyTemplate topologyTemplate = super.convertToComponent(componentV);
815 Map<String, CompositionDataDefinition> json = (Map<String, CompositionDataDefinition>) componentV.getJson();
816 topologyTemplate.setCompositions(json);
818 return topologyTemplate;
822 public Either<ToscaElement, StorageOperationStatus> deleteToscaElement(GraphVertex toscaElementVertex) {
823 Either<ToscaElement, StorageOperationStatus> nodeType = getToscaElement(toscaElementVertex, new ComponentParametersView());
824 if (nodeType.isRight()) {
825 log.debug("Failed to fetch tosca element {} error {}", toscaElementVertex.getUniqueId(), nodeType.right().value());
828 TitanOperationStatus status = disassociateAndDeleteCommonElements(toscaElementVertex);
829 if (status != TitanOperationStatus.OK) {
830 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
832 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_ATTRIBUTES);
833 if (status != TitanOperationStatus.OK) {
834 log.debug("Failed to disassociate instances attributes for {} error {}", toscaElementVertex.getUniqueId(), status);
835 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
837 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_PROPERTIES);
838 if (status != TitanOperationStatus.OK) {
839 log.debug("Failed to disassociate instances properties for {} error {}", toscaElementVertex.getUniqueId(), status);
840 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
843 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_INPUTS);
844 if (status != TitanOperationStatus.OK) {
845 log.debug("Failed to disassociate instances inputs for {} error {}", toscaElementVertex.getUniqueId(), status);
846 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
849 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.GROUPS);
850 if (status != TitanOperationStatus.OK) {
851 log.debug("Failed to disassociate groups for {} error {}", toscaElementVertex.getUniqueId(), status);
852 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
854 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_GROUPS);
855 if (status != TitanOperationStatus.OK) {
856 log.debug("Failed to disassociate instance groups for {} error {}", toscaElementVertex.getUniqueId(), status);
857 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
859 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INPUTS);
860 if (status != TitanOperationStatus.OK) {
861 log.debug("Failed to disassociate inputs for {} error {}", toscaElementVertex.getUniqueId(), status);
862 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
864 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_INPUTS);
865 if (status != TitanOperationStatus.OK) {
866 log.debug("Failed to disassociate instance inputs for {} error {}", toscaElementVertex.getUniqueId(), status);
867 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
869 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.CALCULATED_CAPABILITIES);
870 if (status != TitanOperationStatus.OK) {
871 log.debug("Failed to disassociate calculated capabiliites for {} error {}", toscaElementVertex.getUniqueId(), status);
872 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
874 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.FULLFILLED_CAPABILITIES);
875 if (status != TitanOperationStatus.OK) {
876 log.debug("Failed to disassociate fullfilled capabilities for {} error {}", toscaElementVertex.getUniqueId(), status);
877 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
879 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES);
880 if (status != TitanOperationStatus.OK) {
881 log.debug("Failed to disassociate calculated capabiliites properties for {} error {}", toscaElementVertex.getUniqueId(), status);
882 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
884 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.CALCULATED_REQUIREMENTS);
885 if (status != TitanOperationStatus.OK) {
886 log.debug("Failed to disassociate calculated requirements for {} error {}", toscaElementVertex.getUniqueId(), status);
887 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
889 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.FULLFILLED_REQUIREMENTS);
890 if (status != TitanOperationStatus.OK) {
891 log.debug("Failed to disassociate full filled requirements for {} error {}", toscaElementVertex.getUniqueId(), status);
892 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
894 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS);
895 if (status != TitanOperationStatus.OK) {
896 log.debug("Failed to disassociate instance artifacts for {} error {}", toscaElementVertex.getUniqueId(), status);
897 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
899 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.SERVICE_API_ARTIFACTS);
900 if (status != TitanOperationStatus.OK) {
901 log.debug("Failed to disassociate service api artifacts for {} error {}", toscaElementVertex.getUniqueId(), status);
902 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
904 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INSTANCE_ARTIFACTS);
905 toscaElementVertex.getVertex().remove();
906 log.trace("Tosca element vertex for {} was removed", toscaElementVertex.getUniqueId());
911 @SuppressWarnings("unchecked")
913 public Either<TopologyTemplate, StorageOperationStatus> createToscaElement(ToscaElement toscaElement) {
914 return createTopologyTemplate((TopologyTemplate) toscaElement);
918 protected <T extends ToscaElement> TitanOperationStatus setCategoriesFromGraph(GraphVertex vertexComponent, T toscaElement) {
919 return setTopologyTempalteCategoriesFromGraph(vertexComponent, toscaElement);
923 protected <T extends ToscaElement> StorageOperationStatus validateCategories(T toscaElementToUpdate, GraphVertex elementV) {
924 // Product isn't supported now!!
925 // TODO add for Product
926 if (toscaElementToUpdate.getComponentType() == ComponentTypeEnum.SERVICE) {
927 return validateServiceCategory(toscaElementToUpdate, elementV);
930 return validateResourceCategory(toscaElementToUpdate, elementV);
935 protected <T extends ToscaElement> StorageOperationStatus updateDerived(T toscaElementToUpdate, GraphVertex updateElementV) {
936 // not relevant now for topology template
937 return StorageOperationStatus.OK;
941 public <T extends ToscaElement> void fillToscaElementVertexData(GraphVertex elementV, T toscaElementToUpdate, JsonParseFlagEnum flag) {
942 fillMetadata(elementV, (TopologyTemplate) toscaElementToUpdate, flag);
945 private <T extends ToscaElement> StorageOperationStatus validateServiceCategory(T toscaElementToUpdate, GraphVertex elementV) {
946 StorageOperationStatus status = StorageOperationStatus.OK;
947 List<CategoryDefinition> newCategoryList = toscaElementToUpdate.getCategories();
948 CategoryDefinition newCategory = newCategoryList.get(0);
950 Either<GraphVertex, TitanOperationStatus> childVertex = titanDao.getChildVertex(elementV, EdgeLabelEnum.CATEGORY, JsonParseFlagEnum.NoParse);
951 if (childVertex.isRight()) {
952 log.debug("failed to fetch {} for tosca element with id {}, error {}", EdgeLabelEnum.CATEGORY, elementV.getUniqueId(), childVertex.right().value());
953 return DaoStatusConverter.convertTitanStatusToStorageStatus(childVertex.right().value());
956 GraphVertex categoryV = childVertex.left().value();
957 Map<GraphPropertyEnum, Object> metadataProperties = categoryV.getMetadataProperties();
958 String categoryNameCurrent = (String) metadataProperties.get(GraphPropertyEnum.NAME);
960 String newCategoryName = newCategory.getName();
961 if (newCategoryName != null && false == newCategoryName.equals(categoryNameCurrent)) {
962 // the category was changed
963 Either<GraphVertex, StorageOperationStatus> getCategoryVertex = categoryOperation.getCategory(newCategoryName, VertexTypeEnum.SERVICE_CATEGORY);
965 if (getCategoryVertex.isRight()) {
966 return getCategoryVertex.right().value();
968 GraphVertex newCategoryV = getCategoryVertex.left().value();
969 status = moveCategoryEdge(elementV, newCategoryV);
970 log.debug("Going to update the category of the resource from {} to {}. status is {}", categoryNameCurrent, newCategory, status);
975 public Either<List<GraphVertex>, TitanOperationStatus> getAllNotDeletedElements() {
976 Map<GraphPropertyEnum, Object> propsHasNot = new HashMap<>();
977 propsHasNot.put(GraphPropertyEnum.IS_DELETED, true);
979 Either<List<GraphVertex>, TitanOperationStatus> byCriteria = titanDao.getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, null, propsHasNot, JsonParseFlagEnum.ParseJson);
980 if (byCriteria.isRight()) {
981 log.debug("Failed to fetch all non marked topology templates , propsHasNot {}, error {}", propsHasNot, byCriteria.right().value());
982 return Either.right(byCriteria.right().value());
984 return Either.left(byCriteria.left().value());
987 public boolean isInUse(GraphVertex elementV, List<GraphVertex> allNonDeleted) {
988 for (GraphVertex containerV : allNonDeleted) {
989 Map<String, CompositionDataDefinition> composition = (Map<String, CompositionDataDefinition>) containerV.getJson();
990 if (composition != null) {
991 CompositionDataDefinition instances = composition.get(JsonConstantKeysEnum.COMPOSITION.getValue());
992 if (instances != null && instances.getComponentInstances() != null && !instances.getComponentInstances().isEmpty()) {
993 for (ComponentInstanceDataDefinition ci : instances.getComponentInstances().values()) {
994 if (ci.getComponentUid().equals(elementV.getUniqueId())) {
995 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());
1007 public boolean isInUse(String componentId, List<GraphVertex> allNonDeleted) {
1008 for (GraphVertex containerV : allNonDeleted) {
1009 Map<String, CompositionDataDefinition> composition = (Map<String, CompositionDataDefinition>) containerV.getJson();
1010 if (composition != null) {
1011 CompositionDataDefinition instances = composition.get(JsonConstantKeysEnum.COMPOSITION.getValue());
1012 if (instances != null && instances.getComponentInstances() != null && !instances.getComponentInstances().isEmpty()) {
1013 for (ComponentInstanceDataDefinition ci : instances.getComponentInstances().values()) {
1014 if (ci.getComponentUid().equals(componentId)) {
1026 public Either<GraphVertex, StorageOperationStatus> updateDistributionStatus(String uniqueId, User user, DistributionStatusEnum distributionStatus) {
1028 Either<GraphVertex, StorageOperationStatus> result = null;
1029 String userId = user.getUserId();
1030 Either<GraphVertex, TitanOperationStatus> getRes = findUserVertex(userId);
1031 GraphVertex userVertex = null;
1032 GraphVertex serviceVertex = null;
1033 if (getRes.isRight()) {
1034 TitanOperationStatus status = getRes.right().value();
1035 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Cannot find user {} in the graph. status is {}", userId, status);
1036 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1038 if (result == null) {
1039 userVertex = getRes.left().value();
1040 getRes = titanDao.getVertexById(uniqueId, JsonParseFlagEnum.ParseMetadata);
1041 if (getRes.isRight()) {
1042 TitanOperationStatus status = getRes.right().value();
1043 log.error("Cannot find service {} in the graph. status is {}", uniqueId, status);
1044 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1047 if (result == null) {
1048 serviceVertex = getRes.left().value();
1049 Iterator<Edge> edgeIterator = serviceVertex.getVertex().edges(Direction.IN, EdgeLabelEnum.LAST_DISTRIBUTION_STATE_MODIFIER.name());
1050 if (edgeIterator.hasNext()) {
1051 log.debug("Remove existing edge from user to component {}. Edge type is {}", userId, uniqueId, EdgeLabelEnum.LAST_DISTRIBUTION_STATE_MODIFIER);
1052 edgeIterator.next().remove();
1055 if (result == null) {
1056 TitanOperationStatus status = titanDao.createEdge(userVertex, serviceVertex, EdgeLabelEnum.LAST_DISTRIBUTION_STATE_MODIFIER, null);
1057 if (status != TitanOperationStatus.OK) {
1058 log.error("Failed to associate user {} to component {}. Edge type is {}", userId, uniqueId, EdgeLabelEnum.LAST_DISTRIBUTION_STATE_MODIFIER);
1059 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1062 if (result == null) {
1063 serviceVertex.addMetadataProperty(GraphPropertyEnum.DISTRIBUTION_STATUS, distributionStatus.name());
1064 long lastUpdateDate = System.currentTimeMillis();
1065 serviceVertex.setJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE, lastUpdateDate);
1066 Either<GraphVertex, TitanOperationStatus> updateRes = titanDao.updateVertex(serviceVertex);
1067 if (updateRes.isRight()) {
1068 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(updateRes.right().value()));
1071 if (result == null) {
1072 result = Either.left(serviceVertex);
1077 * Returns list of ComponentInstanceProperty belonging to component instance capability specified by name and type
1078 * @param componentId
1080 * @param capabilityName
1081 * @param capabilityType
1084 public Either<List<ComponentInstanceProperty>, StorageOperationStatus> getComponentInstanceCapabilityProperties(String componentId, String instanceId, String capabilityName, String capabilityType) {
1086 Either<List<ComponentInstanceProperty>, StorageOperationStatus> result = null;
1087 Map<String, MapCapabiltyProperty> mapPropertiesDataDefinition = null;
1088 Either<GraphVertex, StorageOperationStatus> componentByLabelAndId = getComponentByLabelAndId(componentId, ToscaElementTypeEnum.TopologyTemplate, JsonParseFlagEnum.NoParse);
1089 if (componentByLabelAndId.isRight()) {
1090 result = Either.right(componentByLabelAndId.right().value());
1092 if(componentByLabelAndId.isLeft()){
1093 Either<Map<String, MapCapabiltyProperty>, TitanOperationStatus> getDataRes = getDataFromGraph(componentByLabelAndId.left().value(), EdgeLabelEnum.CALCULATED_CAP_PROPERTIES);
1094 if (getDataRes.isRight()) {
1095 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getDataRes.right().value()));
1097 mapPropertiesDataDefinition = getDataRes.left().value();
1100 if(isNotEmptyMapOfProperties(instanceId, mapPropertiesDataDefinition)){
1101 result = Either.left(findComponentInstanceCapabilityProperties(instanceId, capabilityName, capabilityType, mapPropertiesDataDefinition.get(instanceId).getMapToscaDataDefinition()));
1106 private boolean isNotEmptyMapOfProperties(String instanceId, Map<String, MapCapabiltyProperty> mapPropertiesDataDefinition) {
1107 return MapUtils.isNotEmpty(mapPropertiesDataDefinition) &&
1108 mapPropertiesDataDefinition.get(instanceId) != null &&
1109 MapUtils.isNotEmpty(mapPropertiesDataDefinition.get(instanceId).getMapToscaDataDefinition());
1112 private List<ComponentInstanceProperty> findComponentInstanceCapabilityProperties(String instanceId, String capabilityName, String capabilityType, Map<String, MapPropertiesDataDefinition> propertiesMap) {
1113 List<ComponentInstanceProperty> capPropsList = null;
1114 for(Entry<String, MapPropertiesDataDefinition> capProp : propertiesMap.entrySet()){
1115 if (isBelongingPropertyMap(instanceId, capabilityName, capabilityType, capProp)) {
1116 Map<String, PropertyDataDefinition> capMap = capProp.getValue().getMapToscaDataDefinition();
1117 if (capMap != null && !capMap.isEmpty()) {
1118 capPropsList = capMap.values().stream().map(o -> new ComponentInstanceProperty(o)).collect(Collectors.toList());
1123 if(capPropsList == null){
1124 capPropsList = new ArrayList<>();
1126 return capPropsList;
1129 private boolean isBelongingPropertyMap(String instanceId, String capabilityName, String capabilityType, Entry<String, MapPropertiesDataDefinition> capProp) {
1130 if (capProp != null) {
1131 String[] path = capProp.getKey().split(ModelConverter.CAP_PROP_DELIM );
1132 if (path.length < 4) {
1133 log.debug("wrong key format for capabilty, key {}", capProp);
1136 return path[path.length - 2].equals(capabilityType) && path[path.length - 1].equals(capabilityName) && path[0].equals(instanceId);