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 com.google.gson.reflect.TypeToken;
24 import fj.data.Either;
25 import org.apache.commons.collections.MapUtils;
26 import org.apache.tinkerpop.gremlin.structure.Direction;
27 import org.apache.tinkerpop.gremlin.structure.Edge;
28 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
29 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
30 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
31 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
32 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
33 import org.openecomp.sdc.be.datatypes.elements.*;
34 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
35 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
36 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
37 import org.openecomp.sdc.be.model.Component;
38 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
39 import org.openecomp.sdc.be.model.ComponentParametersView;
40 import org.openecomp.sdc.be.model.DistributionStatusEnum;
41 import org.openecomp.sdc.be.model.PolicyDefinition;
42 import org.openecomp.sdc.be.model.User;
43 import org.openecomp.sdc.be.model.category.CategoryDefinition;
44 import org.openecomp.sdc.be.model.jsontitan.datamodel.TopologyTemplate;
45 import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElement;
46 import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElementTypeEnum;
47 import org.openecomp.sdc.be.model.jsontitan.enums.JsonConstantKeysEnum;
48 import org.openecomp.sdc.be.model.jsontitan.utils.ModelConverter;
49 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
50 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
51 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
52 import org.openecomp.sdc.common.api.Constants;
53 import org.openecomp.sdc.common.jsongraph.util.CommonUtility;
54 import org.openecomp.sdc.common.jsongraph.util.CommonUtility.LogLevelEnum;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
58 import java.lang.reflect.Type;
59 import java.util.ArrayList;
60 import java.util.HashMap;
61 import java.util.Iterator;
62 import java.util.List;
64 import java.util.Map.Entry;
65 import java.util.stream.Collectors;
67 @org.springframework.stereotype.Component("topology-template-operation")
68 public class TopologyTemplateOperation extends ToscaElementOperation {
70 private static Logger log = LoggerFactory.getLogger(TopologyTemplateOperation.class.getName());
73 public Either<ToscaElement, StorageOperationStatus> getToscaElement(String uniqueId) {
74 return super.getToscaElement(uniqueId);
77 public Either<TopologyTemplate, StorageOperationStatus> createTopologyTemplate(TopologyTemplate topologyTemplate) {
78 Either<TopologyTemplate, StorageOperationStatus> result = null;
80 topologyTemplate.generateUUID();
82 topologyTemplate = (TopologyTemplate) getResourceMetaDataFromResource(topologyTemplate);
83 String resourceUniqueId = topologyTemplate.getUniqueId();
84 if (resourceUniqueId == null) {
85 resourceUniqueId = UniqueIdBuilder.buildResourceUniqueId();
86 topologyTemplate.setUniqueId(resourceUniqueId);
89 GraphVertex topologyTemplateVertex = new GraphVertex();
90 topologyTemplateVertex = fillMetadata(topologyTemplateVertex, topologyTemplate, JsonParseFlagEnum.ParseAll);
92 Either<GraphVertex, TitanOperationStatus> createdVertex = titanDao.createVertex(topologyTemplateVertex);
93 if (createdVertex.isRight()) {
94 TitanOperationStatus status = createdVertex.right().value();
95 log.error("Error returned after creating topology template data node {}. status returned is ", topologyTemplateVertex, status);
96 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
100 topologyTemplateVertex = createdVertex.left().value();
102 StorageOperationStatus assosiateCommon = assosiateCommonForToscaElement(topologyTemplateVertex, topologyTemplate, null);
103 if (assosiateCommon != StorageOperationStatus.OK) {
104 result = Either.right(assosiateCommon);
108 StorageOperationStatus associateCategory = assosiateMetadataToCategory(topologyTemplateVertex, topologyTemplate);
109 if (associateCategory != StorageOperationStatus.OK) {
110 result = Either.right(associateCategory);
114 StorageOperationStatus associateInputs = associateInputsToComponent(topologyTemplateVertex, topologyTemplate);
115 if (associateInputs != StorageOperationStatus.OK) {
116 result = Either.right(associateInputs);
119 StorageOperationStatus associateGroups = associateGroupsToComponent(topologyTemplateVertex, topologyTemplate);
120 if (associateGroups != StorageOperationStatus.OK) {
121 result = Either.right(associateGroups);
124 StorageOperationStatus associatePolicies = associatePoliciesToComponent(topologyTemplateVertex, topologyTemplate);
125 if (associatePolicies != StorageOperationStatus.OK) {
126 result = Either.right(associatePolicies);
129 StorageOperationStatus associateInstAttr = associateInstAttributesToComponent(topologyTemplateVertex, topologyTemplate);
130 if (associateInstAttr != StorageOperationStatus.OK) {
131 result = Either.right(associateInstAttr);
134 StorageOperationStatus associateInstProperties = associateInstPropertiesToComponent(topologyTemplateVertex, topologyTemplate);
135 if (associateInstProperties != StorageOperationStatus.OK) {
136 result = Either.right(associateInstProperties);
139 StorageOperationStatus associateInstInputs = associateInstInputsToComponent(topologyTemplateVertex, topologyTemplate);
140 if (associateInstProperties != StorageOperationStatus.OK) {
141 result = Either.right(associateInstInputs);
144 StorageOperationStatus associateInstGroups = associateInstGroupsToComponent(topologyTemplateVertex, topologyTemplate);
145 if (associateInstGroups != StorageOperationStatus.OK) {
146 result = Either.right(associateInstInputs);
150 StorageOperationStatus associateRequirements = associateRequirementsToResource(topologyTemplateVertex, topologyTemplate);
151 if (associateRequirements != StorageOperationStatus.OK) {
152 result = Either.right(associateRequirements);
156 StorageOperationStatus associateCapabilities = associateCapabilitiesToResource(topologyTemplateVertex, topologyTemplate);
157 if (associateCapabilities != StorageOperationStatus.OK) {
158 result = Either.right(associateCapabilities);
162 StorageOperationStatus associateArtifacts = associateTopologyTemplateArtifactsToComponent(topologyTemplateVertex, topologyTemplate);
163 if (associateArtifacts != StorageOperationStatus.OK) {
164 result = Either.right(associateArtifacts);
168 StorageOperationStatus addAdditionalInformation = addAdditionalInformationToResource(topologyTemplateVertex, topologyTemplate);
169 if (addAdditionalInformation != StorageOperationStatus.OK) {
170 result = Either.right(addAdditionalInformation);
173 StorageOperationStatus associateCapProperties = associateCapPropertiesToResource(topologyTemplateVertex, topologyTemplate);
174 if (associateCapProperties != StorageOperationStatus.OK) {
175 result = Either.right(associateCapProperties);
178 StorageOperationStatus associatePathProperties = associateForwardingPathToResource(topologyTemplateVertex, topologyTemplate);
179 if (associateCapProperties != StorageOperationStatus.OK) {
180 result = Either.right(associatePathProperties);
185 return Either.left(topologyTemplate);
189 private StorageOperationStatus associatePoliciesToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
190 return associatePoliciesToComponent(nodeTypeVertex, topologyTemplate.getPolicies());
193 private StorageOperationStatus associatePoliciesToComponent(GraphVertex nodeTypeVertex, Map<String, PolicyDataDefinition> policies) {
194 if (policies != null && !policies.isEmpty()) {
195 policies.values().stream().filter(p -> p.getUniqueId() == null).forEach(p -> {
196 String uid = UniqueIdBuilder.buildGroupingUid(nodeTypeVertex.getUniqueId(), p.getName());
199 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.POLICIES, EdgeLabelEnum.POLICIES, policies);
200 if (assosiateElementToData.isRight()) {
201 return assosiateElementToData.right().value();
204 return StorageOperationStatus.OK;
207 private StorageOperationStatus associateForwardingPathToResource(GraphVertex topologyTemplateVertex, TopologyTemplate topologyTemplate) {
208 Map<String, ForwardingPathDataDefinition> forwardingPaths = topologyTemplate.getForwardingPaths();
209 return associateForwardingPathToComponent(topologyTemplateVertex,forwardingPaths);
212 private StorageOperationStatus associateCapPropertiesToResource(GraphVertex topologyTemplateVertex, TopologyTemplate topologyTemplate) {
213 Map<String, MapCapabiltyProperty> calculatedCapProperties = topologyTemplate.getCalculatedCapabilitiesProperties();
214 if (calculatedCapProperties != null && !calculatedCapProperties.isEmpty()) {
215 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(topologyTemplateVertex, VertexTypeEnum.CALCULATED_CAP_PROPERTIES, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES, calculatedCapProperties);
216 if (assosiateElementToData.isRight()) {
217 return assosiateElementToData.right().value();
220 return StorageOperationStatus.OK;
223 private StorageOperationStatus associateCapabilitiesToResource(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
224 Map<String, MapListCapabiltyDataDefinition> calculatedCapabilities = topologyTemplate.getCalculatedCapabilities();
225 if (calculatedCapabilities != null && !calculatedCapabilities.isEmpty()) {
226 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.CALCULATED_CAPABILITIES, EdgeLabelEnum.CALCULATED_CAPABILITIES, calculatedCapabilities);
227 if (assosiateElementToData.isRight()) {
228 return assosiateElementToData.right().value();
231 Map<String, MapListCapabiltyDataDefinition> fullfilledCapabilities = topologyTemplate.getFullfilledCapabilities();
232 if (fullfilledCapabilities != null && !fullfilledCapabilities.isEmpty()) {
233 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.FULLFILLED_CAPABILITIES, EdgeLabelEnum.FULLFILLED_CAPABILITIES, fullfilledCapabilities);
234 if (assosiateElementToData.isRight()) {
235 return assosiateElementToData.right().value();
238 return StorageOperationStatus.OK;
242 private StorageOperationStatus associateRequirementsToResource(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
243 Map<String, MapListRequirementDataDefinition> calculatedRequirements = topologyTemplate.getCalculatedRequirements();
244 if (calculatedRequirements != null && !calculatedRequirements.isEmpty()) {
245 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.CALCULATED_REQUIREMENTS, EdgeLabelEnum.CALCULATED_REQUIREMENTS, calculatedRequirements);
246 if (assosiateElementToData.isRight()) {
247 return assosiateElementToData.right().value();
250 Map<String, MapListRequirementDataDefinition> fullfilledRequirements = topologyTemplate.getFullfilledRequirements();
251 if (fullfilledRequirements != null && !fullfilledRequirements.isEmpty()) {
252 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.FULLFILLED_REQUIREMENTS, EdgeLabelEnum.FULLFILLED_REQUIREMENTS, fullfilledRequirements);
253 if (assosiateElementToData.isRight()) {
254 return assosiateElementToData.right().value();
257 return StorageOperationStatus.OK;
260 private StorageOperationStatus associateTopologyTemplateArtifactsToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
261 Map<String, ArtifactDataDefinition> addInformation = topologyTemplate.getServiceApiArtifacts();
263 if (addInformation != null && !addInformation.isEmpty()) {
264 addInformation.values().stream().filter(a -> a.getUniqueId() == null).forEach(a -> {
265 String uniqueId = UniqueIdBuilder.buildPropertyUniqueId(nodeTypeVertex.getUniqueId().toLowerCase(), a.getArtifactLabel().toLowerCase());
266 a.setUniqueId(uniqueId);
268 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.SERVICE_API_ARTIFACTS, EdgeLabelEnum.SERVICE_API_ARTIFACTS, addInformation);
269 if (assosiateElementToData.isRight()) {
270 return assosiateElementToData.right().value();
273 Map<String, MapArtifactDataDefinition> instArtifacts = topologyTemplate.getInstDeploymentArtifacts();
275 if (instArtifacts != null && !instArtifacts.isEmpty()) {
276 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.INST_DEPLOYMENT_ARTIFACTS, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS, instArtifacts);
277 if (assosiateElementToData.isRight()) {
278 return assosiateElementToData.right().value();
281 Map<String, MapArtifactDataDefinition> instInfoArtifacts = topologyTemplate.getInstanceArtifacts();
283 if (instInfoArtifacts != null && !instInfoArtifacts.isEmpty()) {
284 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.INSTANCE_ARTIFACTS, EdgeLabelEnum.INSTANCE_ARTIFACTS, instInfoArtifacts);
285 if (assosiateElementToData.isRight()) {
286 return assosiateElementToData.right().value();
289 return StorageOperationStatus.OK;
292 private StorageOperationStatus addAdditionalInformationToResource(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
294 Map<String, AdditionalInfoParameterDataDefinition> addInformation = topologyTemplate.getAdditionalInformation();
296 if (addInformation != null && !addInformation.isEmpty()) {
297 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.ADDITIONAL_INFORMATION, EdgeLabelEnum.ADDITIONAL_INFORMATION, addInformation);
298 if (assosiateElementToData.isRight()) {
299 return assosiateElementToData.right().value();
302 return StorageOperationStatus.OK;
305 public StorageOperationStatus associateInstPropertiesToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
306 Map<String, MapPropertiesDataDefinition> instProps = topologyTemplate.getInstProperties();
307 return associateInstPropertiesToComponent(nodeTypeVertex, instProps);
310 public StorageOperationStatus associateInstInputsToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
311 Map<String, MapPropertiesDataDefinition> instProps = topologyTemplate.getInstInputs();
312 return associateInstInputsToComponent(nodeTypeVertex, instProps);
315 public StorageOperationStatus associateInstGroupsToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
316 Map<String, MapGroupsDataDefinition> instGroups = topologyTemplate.getInstGroups();
317 return associateInstGroupsToComponent(nodeTypeVertex, instGroups);
321 public StorageOperationStatus associateInstPropertiesToComponent(GraphVertex nodeTypeVertex, Map<String, MapPropertiesDataDefinition> instProps) {
322 if (instProps != null && !instProps.isEmpty()) {
323 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.INST_PROPERTIES, EdgeLabelEnum.INST_PROPERTIES, instProps);
324 if (assosiateElementToData.isRight()) {
325 return assosiateElementToData.right().value();
328 return StorageOperationStatus.OK;
331 public StorageOperationStatus associateInstInputsToComponent(GraphVertex nodeTypeVertex, Map<String, MapPropertiesDataDefinition> instInputs) {
332 if (instInputs != null && !instInputs.isEmpty()) {
333 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.INST_INPUTS, EdgeLabelEnum.INST_INPUTS, instInputs);
334 if (assosiateElementToData.isRight()) {
335 return assosiateElementToData.right().value();
338 return StorageOperationStatus.OK;
341 public StorageOperationStatus associateInstGroupsToComponent(GraphVertex nodeTypeVertex, Map<String, MapGroupsDataDefinition> instGroups) {
342 if (instGroups != null && !instGroups.isEmpty()) {
343 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.INST_GROUPS, EdgeLabelEnum.INST_GROUPS, instGroups);
344 if (assosiateElementToData.isRight()) {
345 return assosiateElementToData.right().value();
348 return StorageOperationStatus.OK;
352 public StorageOperationStatus deleteInstInputsToComponent(GraphVertex nodeTypeVertex, Map<String, MapPropertiesDataDefinition> instInputs) {
354 if (instInputs != null && !instInputs.isEmpty()) {
355 instInputs.entrySet().forEach(i -> {
356 List<String> uniqueKeys = new ArrayList<String>(i.getValue().getMapToscaDataDefinition().keySet());
357 List<String> pathKeys = new ArrayList<String>();
358 pathKeys.add(i.getKey());
360 StorageOperationStatus status = deleteToscaDataDeepElements(nodeTypeVertex, EdgeLabelEnum.INST_INPUTS, VertexTypeEnum.INST_INPUTS, uniqueKeys, pathKeys, JsonPresentationFields.NAME);
361 if (status != StorageOperationStatus.OK) {
367 return StorageOperationStatus.OK;
370 public StorageOperationStatus addInstPropertiesToComponent(GraphVertex nodeTypeVertex, Map<String, MapPropertiesDataDefinition> instInputs) {
372 if (instInputs != null && !instInputs.isEmpty()) {
373 instInputs.entrySet().forEach(i -> {
374 StorageOperationStatus status = addToscaDataDeepElementsBlockToToscaElement(nodeTypeVertex, EdgeLabelEnum.INST_PROPERTIES, VertexTypeEnum.INST_PROPERTIES, i.getValue(), i.getKey());
375 if (status != StorageOperationStatus.OK) {
381 return StorageOperationStatus.OK;
384 public StorageOperationStatus associateInstDeploymentArtifactsToComponent(GraphVertex nodeTypeVertex, Map<String, MapArtifactDataDefinition> instArtifacts) {
385 return associateInstanceArtifactsToComponent(nodeTypeVertex, instArtifacts, VertexTypeEnum.INST_DEPLOYMENT_ARTIFACTS, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS);
388 public StorageOperationStatus associateInstArtifactsToComponent(GraphVertex nodeTypeVertex, Map<String, MapArtifactDataDefinition> instArtifacts) {
389 return associateInstanceArtifactsToComponent(nodeTypeVertex, instArtifacts, VertexTypeEnum.INSTANCE_ARTIFACTS, EdgeLabelEnum.INSTANCE_ARTIFACTS);
392 private StorageOperationStatus associateInstanceArtifactsToComponent(GraphVertex nodeTypeVertex, Map<String, MapArtifactDataDefinition> instProps, VertexTypeEnum vertexType, EdgeLabelEnum edgeLabel) {
393 if (instProps != null && !instProps.isEmpty()) {
394 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, vertexType, edgeLabel, instProps);
395 if (assosiateElementToData.isRight()) {
396 return assosiateElementToData.right().value();
399 return StorageOperationStatus.OK;
402 public StorageOperationStatus associateCalcCapReqToComponent(GraphVertex nodeTypeVertex, Map<String, MapListRequirementDataDefinition> calcRequirements, Map<String, MapListCapabiltyDataDefinition> calcCapabilty, Map<String, MapCapabiltyProperty> calculatedCapabilitiesProperties) {
403 if (calcRequirements != null && !calcRequirements.isEmpty()) {
404 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.CALCULATED_REQUIREMENTS, EdgeLabelEnum.CALCULATED_REQUIREMENTS, calcRequirements);
405 if (assosiateElementToData.isRight()) {
406 return assosiateElementToData.right().value();
408 Map<String, MapListRequirementDataDefinition> fullFilled = new HashMap<>();
409 assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.FULLFILLED_REQUIREMENTS, EdgeLabelEnum.FULLFILLED_REQUIREMENTS, fullFilled);
410 if (assosiateElementToData.isRight()) {
411 return assosiateElementToData.right().value();
414 if (calcCapabilty != null && !calcCapabilty.isEmpty()) {
415 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.CALCULATED_CAPABILITIES, EdgeLabelEnum.CALCULATED_CAPABILITIES, calcCapabilty);
416 if (assosiateElementToData.isRight()) {
417 return assosiateElementToData.right().value();
419 Map<String, MapListCapabiltyDataDefinition> fullFilled = new HashMap<>();
420 assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.FULLFILLED_CAPABILITIES, EdgeLabelEnum.FULLFILLED_CAPABILITIES, fullFilled);
421 if (assosiateElementToData.isRight()) {
422 return assosiateElementToData.right().value();
425 if ( calculatedCapabilitiesProperties != null && !calculatedCapabilitiesProperties.isEmpty() ){
426 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.CALCULATED_CAP_PROPERTIES, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES, calculatedCapabilitiesProperties);
427 if (assosiateElementToData.isRight()) {
428 return assosiateElementToData.right().value();
431 return StorageOperationStatus.OK;
434 private StorageOperationStatus associateInstAttributesToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
435 Map<String, MapPropertiesDataDefinition> instAttr = topologyTemplate.getInstAttributes();
436 return associateInstAttributeToComponent(nodeTypeVertex, instAttr);
439 public StorageOperationStatus associateForwardingPathToComponent(GraphVertex nodeTypeVertex, Map<String, ForwardingPathDataDefinition> forwardingPathMap) {
440 if (forwardingPathMap != null && !forwardingPathMap.isEmpty()) {
441 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.FORWARDING_PATH, EdgeLabelEnum.FORWARDING_PATH, forwardingPathMap);
442 if (assosiateElementToData.isRight()) {
443 return assosiateElementToData.right().value();
446 return StorageOperationStatus.OK;
449 public StorageOperationStatus associateInstAttributeToComponent(GraphVertex nodeTypeVertex, Map<String, MapPropertiesDataDefinition> instAttr) {
450 if (instAttr != null && !instAttr.isEmpty()) {
451 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.INST_ATTRIBUTES, EdgeLabelEnum.INST_ATTRIBUTES, instAttr);
452 if (assosiateElementToData.isRight()) {
453 return assosiateElementToData.right().value();
456 return StorageOperationStatus.OK;
459 public StorageOperationStatus associateGroupsToComponent(GraphVertex nodeTypeVertex, Map<String, GroupDataDefinition> groups) {
461 if (groups != null && !groups.isEmpty()) {
462 groups.values().stream().filter(p -> p.getUniqueId() == null).forEach(p -> {
463 String uid = UniqueIdBuilder.buildGroupingUid(nodeTypeVertex.getUniqueId(), p.getName());
466 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.GROUPS, EdgeLabelEnum.GROUPS, groups);
467 if (assosiateElementToData.isRight()) {
468 return assosiateElementToData.right().value();
471 return StorageOperationStatus.OK;
474 private StorageOperationStatus associateGroupsToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
475 return associateGroupsToComponent(nodeTypeVertex, topologyTemplate.getGroups());
478 public StorageOperationStatus associateInputsToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
479 Map<String, PropertyDataDefinition> inputs = topologyTemplate.getInputs();
480 return associateInputsToComponent(nodeTypeVertex, inputs, topologyTemplate.getUniqueId());
483 public StorageOperationStatus associateInputsToComponent(GraphVertex nodeTypeVertex, Map<String, PropertyDataDefinition> inputs, String id) {
484 if (inputs != null && !inputs.isEmpty()) {
485 inputs.values().stream().filter(e -> e.getUniqueId() == null).forEach(e -> e.setUniqueId(UniqueIdBuilder.buildPropertyUniqueId(id, e.getName())));
487 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.INPUTS, EdgeLabelEnum.INPUTS, inputs);
488 if (assosiateElementToData.isRight()) {
489 return assosiateElementToData.right().value();
492 return StorageOperationStatus.OK;
495 private GraphVertex fillMetadata(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate, JsonParseFlagEnum flag) {
496 nodeTypeVertex.setLabel(VertexTypeEnum.TOPOLOGY_TEMPLATE);
497 fillCommonMetadata(nodeTypeVertex, topologyTemplate);
498 if (flag == JsonParseFlagEnum.ParseAll || flag == JsonParseFlagEnum.ParseJson) {
499 nodeTypeVertex.setJson(topologyTemplate.getCompositions());
501 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.CSAR_UUID, topologyTemplate.getMetadataValue(JsonPresentationFields.CSAR_UUID));
502 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.DISTRIBUTION_STATUS, topologyTemplate.getMetadataValue(JsonPresentationFields.DISTRIBUTION_STATUS));
504 return nodeTypeVertex;
508 private StorageOperationStatus assosiateMetadataToCategory(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
509 if (topologyTemplate.getResourceType() == null) {
511 return associateServiceMetadataToCategory(nodeTypeVertex, topologyTemplate);
514 return assosiateResourceMetadataToCategory(nodeTypeVertex, topologyTemplate);
518 private StorageOperationStatus associateServiceMetadataToCategory(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
519 String categoryName = topologyTemplate.getCategories().get(0).getName();
520 Either<GraphVertex, StorageOperationStatus> category = categoryOperation.getCategory(categoryName, VertexTypeEnum.SERVICE_CATEGORY);
521 if (category.isRight()) {
522 log.trace("NO category {} for service {}", categoryName, topologyTemplate.getUniqueId());
523 return StorageOperationStatus.CATEGORY_NOT_FOUND;
525 GraphVertex categoryV = category.left().value();
526 TitanOperationStatus createEdge = titanDao.createEdge(nodeTypeVertex, categoryV, EdgeLabelEnum.CATEGORY, new HashMap<>());
527 if (createEdge != TitanOperationStatus.OK) {
528 log.trace("Failed to associate resource {} to category {} with id {}", topologyTemplate.getUniqueId(), categoryName, categoryV.getUniqueId());
529 return DaoStatusConverter.convertTitanStatusToStorageStatus(createEdge);
531 return StorageOperationStatus.OK;
535 public Either<ToscaElement, StorageOperationStatus> getToscaElement(String uniqueId, ComponentParametersView componentParametersView) {
536 JsonParseFlagEnum parseFlag = componentParametersView.detectParseFlag();
538 Either<GraphVertex, StorageOperationStatus> componentByLabelAndId = getComponentByLabelAndId(uniqueId, ToscaElementTypeEnum.TopologyTemplate, parseFlag);
539 if (componentByLabelAndId.isRight()) {
540 return Either.right(componentByLabelAndId.right().value());
542 GraphVertex componentV = componentByLabelAndId.left().value();
544 return getToscaElement(componentV, componentParametersView);
547 // -------------------------------------------------------------
549 public Either<ToscaElement, StorageOperationStatus> getToscaElement(GraphVertex componentV, ComponentParametersView componentParametersView) {
550 TopologyTemplate toscaElement;
552 toscaElement = convertToTopologyTemplate(componentV);
553 TitanOperationStatus status;
554 if (!componentParametersView.isIgnoreUsers()) {
555 status = setCreatorFromGraph(componentV, toscaElement);
556 if (status != TitanOperationStatus.OK) {
557 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
560 status = setLastModifierFromGraph(componentV, toscaElement);
561 if (status != TitanOperationStatus.OK) {
562 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
565 if (!componentParametersView.isIgnoreCategories()) {
566 status = setTopologyTempalteCategoriesFromGraph(componentV, toscaElement);
567 if (status != TitanOperationStatus.OK) {
568 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
572 if (!componentParametersView.isIgnoreArtifacts()) {
573 TitanOperationStatus storageStatus = setAllArtifactsFromGraph(componentV, toscaElement);
574 if (storageStatus != TitanOperationStatus.OK) {
575 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(storageStatus));
578 if (!componentParametersView.isIgnoreComponentInstancesProperties()) {
579 status = setComponentInstancesPropertiesFromGraph(componentV, toscaElement);
580 if (status != TitanOperationStatus.OK) {
581 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
584 if (!componentParametersView.isIgnoreCapabilities()) {
585 status = setCapabilitiesFromGraph(componentV, toscaElement);
586 if (status != TitanOperationStatus.OK) {
587 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
590 if (!componentParametersView.isIgnoreRequirements()) {
591 status = setRequirementsFromGraph(componentV, toscaElement);
592 if (status != TitanOperationStatus.OK) {
593 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
596 if (!componentParametersView.isIgnoreAllVersions()) {
597 status = setAllVersions(componentV, toscaElement);
598 if (status != TitanOperationStatus.OK) {
599 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
602 if (!componentParametersView.isIgnoreAdditionalInformation()) {
603 status = setAdditionalInformationFromGraph(componentV, toscaElement);
604 if (status != TitanOperationStatus.OK) {
605 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
609 if (!componentParametersView.isIgnoreGroups()) {
610 status = setGroupsFromGraph(componentV, toscaElement);
611 if (status != TitanOperationStatus.OK) {
612 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
616 if (!componentParametersView.isIgnorePolicies()) {
617 status = setPoliciesFromGraph(componentV, toscaElement);
618 if (status != TitanOperationStatus.OK) {
619 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
623 if (!componentParametersView.isIgnoreComponentInstances()) {
624 status = setInstGroupsFromGraph(componentV, toscaElement);
625 if (status != TitanOperationStatus.OK) {
626 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
630 if (!componentParametersView.isIgnoreInputs()) {
631 status = setInputsFromGraph(componentV, toscaElement);
632 if (status != TitanOperationStatus.OK) {
633 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
637 if (!componentParametersView.isIgnoreProperties()) {
638 status = setPropertiesFromGraph(componentV, toscaElement);
639 if (status != TitanOperationStatus.OK) {
640 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
645 if (!componentParametersView.isIgnoreComponentInstancesInputs()) {
646 status = setComponentInstancesInputsFromGraph(componentV, toscaElement);
647 if (status != TitanOperationStatus.OK) {
648 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
653 if (!componentParametersView.isIgnoreCapabiltyProperties()) {
654 status = setComponentInstancesCapPropertiesFromGraph(componentV, toscaElement);
655 if (status != TitanOperationStatus.OK) {
656 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
661 if (!componentParametersView.isIgnoreForwardingPath()) {
662 status = setForwardingGraphPropertiesFromGraph(componentV, toscaElement);
663 if (status != TitanOperationStatus.OK) {
664 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
668 return Either.left(toscaElement);
671 private TitanOperationStatus setPoliciesFromGraph(GraphVertex componentV, TopologyTemplate toscaElement) {
672 Either<Map<String, PolicyDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.POLICIES);
673 if (result.isLeft()) {
674 toscaElement.setPolicies(result.left().value());
676 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
677 return result.right().value();
680 return TitanOperationStatus.OK;
683 private TitanOperationStatus setForwardingGraphPropertiesFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
684 Either<Map<String, ForwardingPathDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.FORWARDING_PATH);
685 if (result.isLeft()) {
686 topologyTemplate.setForwardingPaths(result.left().value());
688 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
689 return result.right().value();
692 return TitanOperationStatus.OK;
696 private TitanOperationStatus setComponentInstancesCapPropertiesFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
697 Either<Map<String, MapCapabiltyProperty>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES);
698 if (result.isLeft()) {
699 topologyTemplate.setCalculatedCapabilitiesProperties(result.left().value());
701 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
702 return result.right().value();
705 return TitanOperationStatus.OK;
708 private TitanOperationStatus setPropertiesFromGraph(GraphVertex componentV, TopologyTemplate toscaElement) {
709 Either<Map<String, PropertyDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.PROPERTIES);
710 if (result.isLeft()) {
711 toscaElement.setProperties(result.left().value());
713 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
714 return result.right().value();
717 return TitanOperationStatus.OK;
720 private TitanOperationStatus setInstGroupsFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
721 Either<Map<String, MapGroupsDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.INST_GROUPS);
722 if (result.isLeft()) {
723 topologyTemplate.setInstGroups(result.left().value());
725 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
726 return result.right().value();
729 return TitanOperationStatus.OK;
732 private TitanOperationStatus setComponentInstancesPropertiesFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
733 Either<Map<String, MapPropertiesDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.INST_PROPERTIES);
734 if (result.isLeft()) {
735 topologyTemplate.setInstProperties(result.left().value());
737 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
738 return result.right().value();
741 return TitanOperationStatus.OK;
744 private TitanOperationStatus setComponentInstancesInputsFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
745 Either<Map<String, MapPropertiesDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.INST_INPUTS);
746 if (result.isLeft()) {
747 topologyTemplate.setInstInputs(result.left().value());
749 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
750 return result.right().value();
753 return TitanOperationStatus.OK;
757 protected <T extends ToscaElement> TitanOperationStatus setRequirementsFromGraph(GraphVertex componentV, T toscaElement) {
758 Either<Map<String, MapListRequirementDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.CALCULATED_REQUIREMENTS);
759 if (result.isLeft()) {
760 ((TopologyTemplate) toscaElement).setCalculatedRequirements(result.left().value());
762 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
763 return result.right().value();
766 result = getDataFromGraph(componentV, EdgeLabelEnum.FULLFILLED_REQUIREMENTS);
767 if (result.isLeft()) {
768 ((TopologyTemplate) toscaElement).setFullfilledRequirements(result.left().value());
770 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
771 return result.right().value();
774 return TitanOperationStatus.OK;
778 protected <T extends ToscaElement> TitanOperationStatus setCapabilitiesFromGraph(GraphVertex componentV, T toscaElement) {
779 Either<Map<String, MapListCapabiltyDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.CALCULATED_CAPABILITIES);
780 if (result.isLeft()) {
781 ((TopologyTemplate) toscaElement).setCalculatedCapabilities(result.left().value());
783 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
784 return result.right().value();
787 result = getDataFromGraph(componentV, EdgeLabelEnum.FULLFILLED_CAPABILITIES);
788 if (result.isLeft()) {
789 ((TopologyTemplate) toscaElement).setFullfilledCapabilities(result.left().value());
791 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
792 return result.right().value();
795 return TitanOperationStatus.OK;
798 private TitanOperationStatus setAllArtifactsFromGraph(GraphVertex componentV, TopologyTemplate toscaElement) {
799 TitanOperationStatus storageStatus = setArtifactsFromGraph(componentV, toscaElement);
800 if (storageStatus != TitanOperationStatus.OK) {
801 return storageStatus;
803 Either<Map<String, ArtifactDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.SERVICE_API_ARTIFACTS);
804 if (result.isLeft()) {
805 toscaElement.setServiceApiArtifacts(result.left().value());
807 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
808 return result.right().value();
811 Either<Map<String, MapArtifactDataDefinition>, TitanOperationStatus> resultInstArt = getDataFromGraph(componentV, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS);
812 if (resultInstArt.isLeft()) {
813 toscaElement.setInstDeploymentArtifacts(resultInstArt.left().value());
815 if (resultInstArt.right().value() != TitanOperationStatus.NOT_FOUND) {
816 return resultInstArt.right().value();
819 Either<Map<String, MapArtifactDataDefinition>, TitanOperationStatus> instanceArt = getDataFromGraph(componentV, EdgeLabelEnum.INSTANCE_ARTIFACTS);
820 if (instanceArt.isLeft()) {
821 toscaElement.setInstanceArtifacts(instanceArt.left().value());
823 if (instanceArt.right().value() != TitanOperationStatus.NOT_FOUND) {
824 return instanceArt.right().value();
827 return TitanOperationStatus.OK;
830 private TitanOperationStatus setInputsFromGraph(GraphVertex componentV, TopologyTemplate toscaElement) {
831 Either<Map<String, PropertyDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.INPUTS);
832 if (result.isLeft()) {
833 toscaElement.setInputs(result.left().value());
835 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
836 return result.right().value();
839 return TitanOperationStatus.OK;
842 private TitanOperationStatus setGroupsFromGraph(GraphVertex componentV, TopologyTemplate toscaElement) {
843 Either<Map<String, GroupDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.GROUPS);
844 if (result.isLeft()) {
845 toscaElement.setGroups(result.left().value());
847 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
848 return result.right().value();
851 return TitanOperationStatus.OK;
854 private TitanOperationStatus setTopologyTempalteCategoriesFromGraph(GraphVertex componentV, ToscaElement toscaElement) {
855 List<CategoryDefinition> categories = new ArrayList<>();
857 switch (componentV.getType()) {
859 return setResourceCategoryFromGraph(componentV, toscaElement);
861 return setServiceCategoryFromGraph(componentV, toscaElement, categories);
864 log.debug("Not supported component type {} ", componentV.getType());
867 return TitanOperationStatus.OK;
870 private TitanOperationStatus setServiceCategoryFromGraph(GraphVertex componentV, ToscaElement toscaElement, List<CategoryDefinition> categories) {
871 Either<GraphVertex, TitanOperationStatus> childVertex = titanDao.getChildVertex(componentV, EdgeLabelEnum.CATEGORY, JsonParseFlagEnum.NoParse);
872 if (childVertex.isRight()) {
873 log.debug("failed to fetch {} for tosca element with id {}, error {}", EdgeLabelEnum.CATEGORY, componentV.getUniqueId(), childVertex.right().value());
874 return childVertex.right().value();
876 GraphVertex categoryV = childVertex.left().value();
877 Map<GraphPropertyEnum, Object> metadataProperties = categoryV.getMetadataProperties();
878 CategoryDefinition category = new CategoryDefinition();
879 category.setUniqueId(categoryV.getUniqueId());
880 category.setNormalizedName((String) metadataProperties.get(GraphPropertyEnum.NORMALIZED_NAME));
881 category.setName((String) metadataProperties.get(GraphPropertyEnum.NAME));
883 Type listTypeCat = new TypeToken<List<String>>() {}.getType();
884 List<String> iconsfromJsonCat = getGson().fromJson((String) metadataProperties.get(GraphPropertyEnum.ICONS.getProperty()), listTypeCat);
885 category.setIcons(iconsfromJsonCat);
886 categories.add(category);
887 toscaElement.setCategories(categories);
889 return TitanOperationStatus.OK;
892 @SuppressWarnings("unchecked")
893 private TopologyTemplate convertToTopologyTemplate(GraphVertex componentV) {
895 TopologyTemplate topologyTemplate = super.convertToComponent(componentV);
897 Map<String, CompositionDataDefinition> json = (Map<String, CompositionDataDefinition>) componentV.getJson();
898 topologyTemplate.setCompositions(json);
900 return topologyTemplate;
904 public Either<ToscaElement, StorageOperationStatus> deleteToscaElement(GraphVertex toscaElementVertex) {
905 Either<ToscaElement, StorageOperationStatus> nodeType = getToscaElement(toscaElementVertex, new ComponentParametersView());
906 if (nodeType.isRight()) {
907 log.debug("Failed to fetch tosca element {} error {}", toscaElementVertex.getUniqueId(), nodeType.right().value());
910 TitanOperationStatus status = disassociateAndDeleteCommonElements(toscaElementVertex);
911 if (status != TitanOperationStatus.OK) {
912 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
914 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_ATTRIBUTES);
915 if (status != TitanOperationStatus.OK) {
916 log.debug("Failed to disassociate instances attributes for {} error {}", toscaElementVertex.getUniqueId(), status);
917 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
919 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_PROPERTIES);
920 if (status != TitanOperationStatus.OK) {
921 log.debug("Failed to disassociate instances properties for {} error {}", toscaElementVertex.getUniqueId(), status);
922 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
925 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_INPUTS);
926 if (status != TitanOperationStatus.OK) {
927 log.debug("Failed to disassociate instances inputs for {} error {}", toscaElementVertex.getUniqueId(), status);
928 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
931 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.GROUPS);
932 if (status != TitanOperationStatus.OK) {
933 log.debug("Failed to disassociate groups for {} error {}", toscaElementVertex.getUniqueId(), status);
934 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
936 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_GROUPS);
937 if (status != TitanOperationStatus.OK) {
938 log.debug("Failed to disassociate instance groups for {} error {}", toscaElementVertex.getUniqueId(), status);
939 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
941 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INPUTS);
942 if (status != TitanOperationStatus.OK) {
943 log.debug("Failed to disassociate inputs for {} error {}", toscaElementVertex.getUniqueId(), status);
944 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
946 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_INPUTS);
947 if (status != TitanOperationStatus.OK) {
948 log.debug("Failed to disassociate instance inputs for {} error {}", toscaElementVertex.getUniqueId(), status);
949 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
951 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.CALCULATED_CAPABILITIES);
952 if (status != TitanOperationStatus.OK) {
953 log.debug("Failed to disassociate calculated capabiliites for {} error {}", toscaElementVertex.getUniqueId(), status);
954 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
956 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.FULLFILLED_CAPABILITIES);
957 if (status != TitanOperationStatus.OK) {
958 log.debug("Failed to disassociate fullfilled capabilities for {} error {}", toscaElementVertex.getUniqueId(), status);
959 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
961 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES);
962 if (status != TitanOperationStatus.OK) {
963 log.debug("Failed to disassociate calculated capabiliites properties for {} error {}", toscaElementVertex.getUniqueId(), status);
964 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
966 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.CALCULATED_REQUIREMENTS);
967 if (status != TitanOperationStatus.OK) {
968 log.debug("Failed to disassociate calculated requirements for {} error {}", toscaElementVertex.getUniqueId(), status);
969 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
971 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.FULLFILLED_REQUIREMENTS);
972 if (status != TitanOperationStatus.OK) {
973 log.debug("Failed to disassociate full filled requirements for {} error {}", toscaElementVertex.getUniqueId(), status);
974 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
976 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS);
977 if (status != TitanOperationStatus.OK) {
978 log.debug("Failed to disassociate instance artifacts for {} error {}", toscaElementVertex.getUniqueId(), status);
979 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
981 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.SERVICE_API_ARTIFACTS);
982 if (status != TitanOperationStatus.OK) {
983 log.debug("Failed to disassociate service api artifacts for {} error {}", toscaElementVertex.getUniqueId(), status);
984 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
986 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.FORWARDING_PATH);
987 if (status != TitanOperationStatus.OK) {
988 log.debug("Failed to disassociate service api artifacts for {} error {}", toscaElementVertex.getUniqueId(), status);
989 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
992 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INSTANCE_ARTIFACTS);
993 toscaElementVertex.getVertex().remove();
994 log.trace("Tosca element vertex for {} was removed", toscaElementVertex.getUniqueId());
999 @SuppressWarnings("unchecked")
1001 public Either<TopologyTemplate, StorageOperationStatus> createToscaElement(ToscaElement toscaElement) {
1002 return createTopologyTemplate((TopologyTemplate) toscaElement);
1006 protected <T extends ToscaElement> TitanOperationStatus setCategoriesFromGraph(GraphVertex vertexComponent, T toscaElement) {
1007 return setTopologyTempalteCategoriesFromGraph(vertexComponent, toscaElement);
1011 protected <T extends ToscaElement> StorageOperationStatus validateCategories(T toscaElementToUpdate, GraphVertex elementV) {
1012 // Product isn't supported now!!
1013 // TODO add for Product
1014 if (toscaElementToUpdate.getComponentType() == ComponentTypeEnum.SERVICE) {
1015 return validateServiceCategory(toscaElementToUpdate, elementV);
1018 return validateResourceCategory(toscaElementToUpdate, elementV);
1023 protected <T extends ToscaElement> StorageOperationStatus updateDerived(T toscaElementToUpdate, GraphVertex updateElementV) {
1024 // not relevant now for topology template
1025 return StorageOperationStatus.OK;
1029 public <T extends ToscaElement> void fillToscaElementVertexData(GraphVertex elementV, T toscaElementToUpdate, JsonParseFlagEnum flag) {
1030 fillMetadata(elementV, (TopologyTemplate) toscaElementToUpdate, flag);
1033 private <T extends ToscaElement> StorageOperationStatus validateServiceCategory(T toscaElementToUpdate, GraphVertex elementV) {
1034 StorageOperationStatus status = StorageOperationStatus.OK;
1035 List<CategoryDefinition> newCategoryList = toscaElementToUpdate.getCategories();
1036 CategoryDefinition newCategory = newCategoryList.get(0);
1038 Either<GraphVertex, TitanOperationStatus> childVertex = titanDao.getChildVertex(elementV, EdgeLabelEnum.CATEGORY, JsonParseFlagEnum.NoParse);
1039 if (childVertex.isRight()) {
1040 log.debug("failed to fetch {} for tosca element with id {}, error {}", EdgeLabelEnum.CATEGORY, elementV.getUniqueId(), childVertex.right().value());
1041 return DaoStatusConverter.convertTitanStatusToStorageStatus(childVertex.right().value());
1044 GraphVertex categoryV = childVertex.left().value();
1045 Map<GraphPropertyEnum, Object> metadataProperties = categoryV.getMetadataProperties();
1046 String categoryNameCurrent = (String) metadataProperties.get(GraphPropertyEnum.NAME);
1048 String newCategoryName = newCategory.getName();
1049 if (newCategoryName != null && false == newCategoryName.equals(categoryNameCurrent)) {
1050 // the category was changed
1051 Either<GraphVertex, StorageOperationStatus> getCategoryVertex = categoryOperation.getCategory(newCategoryName, VertexTypeEnum.SERVICE_CATEGORY);
1053 if (getCategoryVertex.isRight()) {
1054 return getCategoryVertex.right().value();
1056 GraphVertex newCategoryV = getCategoryVertex.left().value();
1057 status = moveCategoryEdge(elementV, newCategoryV);
1058 log.debug("Going to update the category of the resource from {} to {}. status is {}", categoryNameCurrent, newCategory, status);
1063 public Either<List<GraphVertex>, TitanOperationStatus> getAllNotDeletedElements() {
1064 Map<GraphPropertyEnum, Object> propsHasNot = new HashMap<>();
1065 propsHasNot.put(GraphPropertyEnum.IS_DELETED, true);
1067 Either<List<GraphVertex>, TitanOperationStatus> byCriteria = titanDao.getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, null, propsHasNot, JsonParseFlagEnum.ParseJson);
1068 if (byCriteria.isRight()) {
1069 log.debug("Failed to fetch all non marked topology templates , propsHasNot {}, error {}", propsHasNot, byCriteria.right().value());
1070 return Either.right(byCriteria.right().value());
1072 return Either.left(byCriteria.left().value());
1075 @SuppressWarnings("unchecked")
1076 public boolean isInUse(GraphVertex elementV, List<GraphVertex> allNonDeleted) {
1077 for (GraphVertex containerV : allNonDeleted) {
1078 Map<String, CompositionDataDefinition> composition = (Map<String, CompositionDataDefinition>) containerV.getJson();
1079 if (composition != null) {
1080 CompositionDataDefinition instances = composition.get(JsonConstantKeysEnum.COMPOSITION.getValue());
1081 if (instances != null && instances.getComponentInstances() != null && !instances.getComponentInstances().isEmpty()) {
1082 for (ComponentInstanceDataDefinition ci : instances.getComponentInstances().values()) {
1083 if (ci.getComponentUid().equals(elementV.getUniqueId())) {
1084 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());
1096 @SuppressWarnings("unchecked")
1097 public boolean isInUse(String componentId, List<GraphVertex> allNonDeleted) {
1098 for (GraphVertex containerV : allNonDeleted) {
1099 Map<String, CompositionDataDefinition> composition = (Map<String, CompositionDataDefinition>) containerV.getJson();
1100 if (composition != null) {
1101 CompositionDataDefinition instances = composition.get(JsonConstantKeysEnum.COMPOSITION.getValue());
1102 if (instances != null && instances.getComponentInstances() != null && !instances.getComponentInstances().isEmpty()) {
1103 for (ComponentInstanceDataDefinition ci : instances.getComponentInstances().values()) {
1104 if (ci.getComponentUid().equals(componentId)) {
1116 public Either<GraphVertex, StorageOperationStatus> updateDistributionStatus(String uniqueId, User user, DistributionStatusEnum distributionStatus) {
1118 Either<GraphVertex, StorageOperationStatus> result = null;
1119 String userId = user.getUserId();
1120 Either<GraphVertex, TitanOperationStatus> getRes = findUserVertex(userId);
1121 GraphVertex userVertex = null;
1122 GraphVertex serviceVertex = null;
1123 if (getRes.isRight()) {
1124 TitanOperationStatus status = getRes.right().value();
1125 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Cannot find user {} in the graph. status is {}", userId, status);
1126 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1128 if (result == null) {
1129 userVertex = getRes.left().value();
1130 getRes = titanDao.getVertexById(uniqueId, JsonParseFlagEnum.ParseMetadata);
1131 if (getRes.isRight()) {
1132 TitanOperationStatus status = getRes.right().value();
1133 log.error("Cannot find service {} in the graph. status is {}", uniqueId, status);
1134 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1137 if (result == null) {
1138 serviceVertex = getRes.left().value();
1139 Iterator<Edge> edgeIterator = serviceVertex.getVertex().edges(Direction.IN, EdgeLabelEnum.LAST_DISTRIBUTION_STATE_MODIFIER.name());
1140 if (edgeIterator.hasNext()) {
1141 log.debug("Remove existing edge from user to component {}. Edge type is {}", userId, uniqueId, EdgeLabelEnum.LAST_DISTRIBUTION_STATE_MODIFIER);
1142 edgeIterator.next().remove();
1145 if (result == null) {
1146 TitanOperationStatus status = titanDao.createEdge(userVertex, serviceVertex, EdgeLabelEnum.LAST_DISTRIBUTION_STATE_MODIFIER, null);
1147 if (status != TitanOperationStatus.OK) {
1148 log.error("Failed to associate user {} to component {}. Edge type is {}", userId, uniqueId, EdgeLabelEnum.LAST_DISTRIBUTION_STATE_MODIFIER);
1149 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1152 if (result == null) {
1153 serviceVertex.addMetadataProperty(GraphPropertyEnum.DISTRIBUTION_STATUS, distributionStatus.name());
1154 long lastUpdateDate = System.currentTimeMillis();
1155 serviceVertex.setJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE, lastUpdateDate);
1156 Either<GraphVertex, TitanOperationStatus> updateRes = titanDao.updateVertex(serviceVertex);
1157 if (updateRes.isRight()) {
1158 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(updateRes.right().value()));
1161 if (result == null) {
1162 result = Either.left(serviceVertex);
1167 * Returns list of ComponentInstanceProperty belonging to component instance capability specified by name, type and ownerId
1168 * @param componentId
1170 * @param capabilityName
1171 * @param capabilityType
1175 public Either<List<ComponentInstanceProperty>, StorageOperationStatus> getComponentInstanceCapabilityProperties(String componentId, String instanceId, String capabilityName, String capabilityType, String ownerId) {
1177 Either<List<ComponentInstanceProperty>, StorageOperationStatus> result = null;
1178 Map<String, MapCapabiltyProperty> mapPropertiesDataDefinition = null;
1179 Either<GraphVertex, StorageOperationStatus> componentByLabelAndId = getComponentByLabelAndId(componentId, ToscaElementTypeEnum.TopologyTemplate, JsonParseFlagEnum.NoParse);
1180 if (componentByLabelAndId.isRight()) {
1181 result = Either.right(componentByLabelAndId.right().value());
1183 if(componentByLabelAndId.isLeft()){
1184 Either<Map<String, MapCapabiltyProperty>, TitanOperationStatus> getDataRes = getDataFromGraph(componentByLabelAndId.left().value(), EdgeLabelEnum.CALCULATED_CAP_PROPERTIES);
1185 if (getDataRes.isRight()) {
1186 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getDataRes.right().value()));
1188 mapPropertiesDataDefinition = getDataRes.left().value();
1191 if(isNotEmptyMapOfProperties(instanceId, mapPropertiesDataDefinition)){
1192 result = Either.left(findComponentInstanceCapabilityProperties(instanceId, capabilityName, capabilityType, ownerId, mapPropertiesDataDefinition.get(instanceId).getMapToscaDataDefinition()));
1197 public StorageOperationStatus updateComponentInstanceCapabilityProperties(Component containerComponent, String componentInstanceId, MapCapabiltyProperty instanceProperties) {
1198 return updateToscaDataDeepElementsBlockToToscaElement(containerComponent.getUniqueId(), EdgeLabelEnum.CALCULATED_CAP_PROPERTIES, instanceProperties, componentInstanceId);
1202 private boolean isNotEmptyMapOfProperties(String instanceId, Map<String, MapCapabiltyProperty> mapPropertiesDataDefinition) {
1203 return MapUtils.isNotEmpty(mapPropertiesDataDefinition) &&
1204 mapPropertiesDataDefinition.get(instanceId) != null &&
1205 MapUtils.isNotEmpty(mapPropertiesDataDefinition.get(instanceId).getMapToscaDataDefinition());
1208 private List<ComponentInstanceProperty> findComponentInstanceCapabilityProperties(String instanceId, String capabilityName, String capabilityType, String ownerId, Map<String, MapPropertiesDataDefinition> propertiesMap) {
1209 List<ComponentInstanceProperty> capPropsList = null;
1210 for(Entry<String, MapPropertiesDataDefinition> capProp : propertiesMap.entrySet()){
1211 if (isBelongingPropertyMap(instanceId, capabilityName, capabilityType, ownerId, capProp)) {
1212 Map<String, PropertyDataDefinition> capMap = capProp.getValue().getMapToscaDataDefinition();
1213 if (capMap != null && !capMap.isEmpty()) {
1214 capPropsList = capMap.values().stream().map(o -> new ComponentInstanceProperty(o)).collect(Collectors.toList());
1219 if(capPropsList == null){
1220 capPropsList = new ArrayList<>();
1222 return capPropsList;
1225 private boolean isBelongingPropertyMap(String instanceId, String capabilityName, String capabilityType, String ownerId, Entry<String, MapPropertiesDataDefinition> capProp) {
1226 if (capProp != null) {
1227 String[] path = capProp.getKey().split(ModelConverter.CAP_PROP_DELIM );
1228 if (path.length < 4) {
1229 log.debug("wrong key format for capabilty, key {}", capProp);
1232 return path[path.length - 2].equals(capabilityType) && path[path.length - 1].equals(capabilityName) && path[1].equals(ownerId) && path[0].equals(instanceId);
1237 public StorageOperationStatus addPolicyToToscaElement(GraphVertex componentV, PolicyDefinition policyDefinition, int counter) {
1238 fillPolicyDefinition(componentV, policyDefinition, counter);
1239 return addToscaDataToToscaElement(componentV, EdgeLabelEnum.POLICIES, VertexTypeEnum.POLICIES, policyDefinition, JsonPresentationFields.UNIQUE_ID);
1242 public StorageOperationStatus updatePolicyOfToscaElement(GraphVertex componentV, PolicyDefinition policyDefinition) {
1243 return updateToscaDataOfToscaElement(componentV, EdgeLabelEnum.POLICIES, VertexTypeEnum.POLICIES, policyDefinition, JsonPresentationFields.UNIQUE_ID);
1246 public StorageOperationStatus updatePoliciesOfToscaElement(GraphVertex componentV, List<PolicyDefinition> policiesDefinitions) {
1247 return updateToscaDataOfToscaElement(componentV, EdgeLabelEnum.POLICIES, VertexTypeEnum.POLICIES, policiesDefinitions, JsonPresentationFields.UNIQUE_ID);
1250 public StorageOperationStatus removePolicyFromToscaElement(GraphVertex componentV, String policyId) {
1251 return deleteToscaDataElement(componentV, EdgeLabelEnum.POLICIES, VertexTypeEnum.POLICIES, policyId, JsonPresentationFields.UNIQUE_ID);
1254 private void fillPolicyDefinition(GraphVertex componentV, PolicyDefinition policyDefinition, int counter) {
1255 String policyName = buildSubComponentName((String) componentV.getJsonMetadataField(JsonPresentationFields.NAME), policyDefinition.getPolicyTypeName(), counter);
1256 policyDefinition.setName(policyName);
1257 policyDefinition.setInvariantName(policyName);
1258 policyDefinition.setComponentName((String) componentV.getJsonMetadataField(JsonPresentationFields.NAME));
1259 policyDefinition.setUniqueId(UniqueIdBuilder.buildPolicyUniqueId(componentV.getUniqueId(), policyName));
1260 policyDefinition.setInvariantUUID(UniqueIdBuilder.buildInvariantUUID());
1261 policyDefinition.setPolicyUUID(UniqueIdBuilder.generateUUID());
1264 public static String buildSubComponentName(String componentName, String subComponentTypeName, int counter) {
1265 String typeSuffix = subComponentTypeName.substring(subComponentTypeName.lastIndexOf('.') + 1, subComponentTypeName.length());
1266 return componentName + Constants.GROUP_POLICY_NAME_DELIMETER + typeSuffix + Constants.GROUP_POLICY_NAME_DELIMETER + counter;