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.ForwardingPathDataDefinition;
45 import org.openecomp.sdc.be.datatypes.elements.GroupDataDefinition;
46 import org.openecomp.sdc.be.datatypes.elements.InterfaceDataDefinition;
47 import org.openecomp.sdc.be.datatypes.elements.MapArtifactDataDefinition;
48 import org.openecomp.sdc.be.datatypes.elements.MapCapabiltyProperty;
49 import org.openecomp.sdc.be.datatypes.elements.MapGroupsDataDefinition;
50 import org.openecomp.sdc.be.datatypes.elements.MapListCapabiltyDataDefinition;
51 import org.openecomp.sdc.be.datatypes.elements.MapListRequirementDataDefinition;
52 import org.openecomp.sdc.be.datatypes.elements.MapPropertiesDataDefinition;
53 import org.openecomp.sdc.be.datatypes.elements.PolicyDataDefinition;
54 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
55 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
56 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
57 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
58 import org.openecomp.sdc.be.model.Component;
59 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
60 import org.openecomp.sdc.be.model.ComponentParametersView;
61 import org.openecomp.sdc.be.model.DistributionStatusEnum;
62 import org.openecomp.sdc.be.model.PolicyDefinition;
63 import org.openecomp.sdc.be.model.User;
64 import org.openecomp.sdc.be.model.category.CategoryDefinition;
65 import org.openecomp.sdc.be.model.jsontitan.datamodel.TopologyTemplate;
66 import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElement;
67 import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElementTypeEnum;
68 import org.openecomp.sdc.be.model.jsontitan.enums.JsonConstantKeysEnum;
69 import org.openecomp.sdc.be.model.jsontitan.utils.ModelConverter;
70 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
71 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
72 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
73 import org.openecomp.sdc.common.api.Constants;
74 import org.openecomp.sdc.common.jsongraph.util.CommonUtility;
75 import org.openecomp.sdc.common.jsongraph.util.CommonUtility.LogLevelEnum;
76 import org.slf4j.Logger;
77 import org.slf4j.LoggerFactory;
79 import com.google.gson.reflect.TypeToken;
81 import fj.data.Either;
83 @org.springframework.stereotype.Component("topology-template-operation")
84 public class TopologyTemplateOperation extends ToscaElementOperation {
86 private static Logger log = LoggerFactory.getLogger(TopologyTemplateOperation.class.getName());
89 public Either<ToscaElement, StorageOperationStatus> getToscaElement(String uniqueId) {
90 return super.getToscaElement(uniqueId);
93 public Either<TopologyTemplate, StorageOperationStatus> createTopologyTemplate(TopologyTemplate topologyTemplate) {
94 Either<TopologyTemplate, StorageOperationStatus> result = null;
96 topologyTemplate.generateUUID();
98 topologyTemplate = (TopologyTemplate) getResourceMetaDataFromResource(topologyTemplate);
99 String resourceUniqueId = topologyTemplate.getUniqueId();
100 if (resourceUniqueId == null) {
101 resourceUniqueId = UniqueIdBuilder.buildResourceUniqueId();
102 topologyTemplate.setUniqueId(resourceUniqueId);
105 GraphVertex topologyTemplateVertex = new GraphVertex();
106 topologyTemplateVertex = fillMetadata(topologyTemplateVertex, topologyTemplate, JsonParseFlagEnum.ParseAll);
108 Either<GraphVertex, TitanOperationStatus> createdVertex = titanDao.createVertex(topologyTemplateVertex);
109 if (createdVertex.isRight()) {
110 TitanOperationStatus status = createdVertex.right().value();
111 log.error("Error returned after creating topology template data node {}. status returned is ", topologyTemplateVertex, status);
112 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
116 topologyTemplateVertex = createdVertex.left().value();
118 StorageOperationStatus assosiateCommon = assosiateCommonForToscaElement(topologyTemplateVertex, topologyTemplate, null);
119 if (assosiateCommon != StorageOperationStatus.OK) {
120 result = Either.right(assosiateCommon);
124 StorageOperationStatus associateCategory = assosiateMetadataToCategory(topologyTemplateVertex, topologyTemplate);
125 if (associateCategory != StorageOperationStatus.OK) {
126 result = Either.right(associateCategory);
130 StorageOperationStatus associateInputs = associateInputsToComponent(topologyTemplateVertex, topologyTemplate);
131 if (associateInputs != StorageOperationStatus.OK) {
132 result = Either.right(associateInputs);
135 StorageOperationStatus associateGroups = associateGroupsToComponent(topologyTemplateVertex, topologyTemplate);
136 if (associateGroups != StorageOperationStatus.OK) {
137 result = Either.right(associateGroups);
140 StorageOperationStatus associatePolicies = associatePoliciesToComponent(topologyTemplateVertex, topologyTemplate);
141 if (associatePolicies != StorageOperationStatus.OK) {
142 result = Either.right(associatePolicies);
145 StorageOperationStatus associateInstAttr = associateInstAttributesToComponent(topologyTemplateVertex, topologyTemplate);
146 if (associateInstAttr != StorageOperationStatus.OK) {
147 result = Either.right(associateInstAttr);
150 StorageOperationStatus associateInstProperties = associateInstPropertiesToComponent(topologyTemplateVertex, topologyTemplate);
151 if (associateInstProperties != StorageOperationStatus.OK) {
152 result = Either.right(associateInstProperties);
155 StorageOperationStatus associateInstInputs = associateInstInputsToComponent(topologyTemplateVertex, topologyTemplate);
156 if (associateInstProperties != StorageOperationStatus.OK) {
157 result = Either.right(associateInstInputs);
160 StorageOperationStatus associateInstGroups = associateInstGroupsToComponent(topologyTemplateVertex, topologyTemplate);
161 if (associateInstGroups != StorageOperationStatus.OK) {
162 result = Either.right(associateInstInputs);
166 StorageOperationStatus associateRequirements = associateRequirementsToResource(topologyTemplateVertex, topologyTemplate);
167 if (associateRequirements != StorageOperationStatus.OK) {
168 result = Either.right(associateRequirements);
172 StorageOperationStatus associateCapabilities = associateCapabilitiesToResource(topologyTemplateVertex, topologyTemplate);
173 if (associateCapabilities != StorageOperationStatus.OK) {
174 result = Either.right(associateCapabilities);
178 StorageOperationStatus associateArtifacts = associateTopologyTemplateArtifactsToComponent(topologyTemplateVertex, topologyTemplate);
179 if (associateArtifacts != StorageOperationStatus.OK) {
180 result = Either.right(associateArtifacts);
184 StorageOperationStatus addAdditionalInformation = addAdditionalInformationToResource(topologyTemplateVertex, topologyTemplate);
185 if (addAdditionalInformation != StorageOperationStatus.OK) {
186 result = Either.right(addAdditionalInformation);
189 StorageOperationStatus associateCapProperties = associateCapPropertiesToResource(topologyTemplateVertex, topologyTemplate);
190 if (associateCapProperties != StorageOperationStatus.OK) {
191 result = Either.right(associateCapProperties);
195 StorageOperationStatus associateInterfaces = associateInterfacesToResource(topologyTemplateVertex, topologyTemplate);
196 if (associateInterfaces != StorageOperationStatus.OK) {
197 result = Either.right(associateInterfaces);
201 StorageOperationStatus associatePathProperties = associateForwardingPathToResource(topologyTemplateVertex, topologyTemplate);
202 if (associateCapProperties != StorageOperationStatus.OK) {
203 result = Either.right(associatePathProperties);
208 return Either.left(topologyTemplate);
212 private StorageOperationStatus associatePoliciesToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
213 return associatePoliciesToComponent(nodeTypeVertex, topologyTemplate.getPolicies());
216 private StorageOperationStatus associatePoliciesToComponent(GraphVertex nodeTypeVertex, Map<String, PolicyDataDefinition> policies) {
217 if (policies != null && !policies.isEmpty()) {
218 policies.values().stream().filter(p -> p.getUniqueId() == null).forEach(p -> {
219 String uid = UniqueIdBuilder.buildGroupingUid(nodeTypeVertex.getUniqueId(), p.getName());
222 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.POLICIES, EdgeLabelEnum.POLICIES, policies);
223 if (assosiateElementToData.isRight()) {
224 return assosiateElementToData.right().value();
227 return StorageOperationStatus.OK;
230 private StorageOperationStatus associateForwardingPathToResource(GraphVertex topologyTemplateVertex, TopologyTemplate topologyTemplate) {
231 Map<String, ForwardingPathDataDefinition> forwardingPaths = topologyTemplate.getForwardingPaths();
232 return associateForwardingPathToComponent(topologyTemplateVertex,forwardingPaths);
235 private StorageOperationStatus associateCapPropertiesToResource(GraphVertex topologyTemplateVertex, TopologyTemplate topologyTemplate) {
236 Map<String, MapCapabiltyProperty> calculatedCapProperties = topologyTemplate.getCalculatedCapabilitiesProperties();
237 if (calculatedCapProperties != null && !calculatedCapProperties.isEmpty()) {
238 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(topologyTemplateVertex, VertexTypeEnum.CALCULATED_CAP_PROPERTIES, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES, calculatedCapProperties);
239 if (assosiateElementToData.isRight()) {
240 return assosiateElementToData.right().value();
243 return StorageOperationStatus.OK;
246 private StorageOperationStatus associateCapabilitiesToResource(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
247 Map<String, MapListCapabiltyDataDefinition> calculatedCapabilities = topologyTemplate.getCalculatedCapabilities();
248 if (calculatedCapabilities != null && !calculatedCapabilities.isEmpty()) {
249 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.CALCULATED_CAPABILITIES, EdgeLabelEnum.CALCULATED_CAPABILITIES, calculatedCapabilities);
250 if (assosiateElementToData.isRight()) {
251 return assosiateElementToData.right().value();
254 Map<String, MapListCapabiltyDataDefinition> fullfilledCapabilities = topologyTemplate.getFullfilledCapabilities();
255 if (fullfilledCapabilities != null && !fullfilledCapabilities.isEmpty()) {
256 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.FULLFILLED_CAPABILITIES, EdgeLabelEnum.FULLFILLED_CAPABILITIES, fullfilledCapabilities);
257 if (assosiateElementToData.isRight()) {
258 return assosiateElementToData.right().value();
261 return StorageOperationStatus.OK;
265 private StorageOperationStatus associateRequirementsToResource(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
266 Map<String, MapListRequirementDataDefinition> calculatedRequirements = topologyTemplate.getCalculatedRequirements();
267 if (calculatedRequirements != null && !calculatedRequirements.isEmpty()) {
268 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.CALCULATED_REQUIREMENTS, EdgeLabelEnum.CALCULATED_REQUIREMENTS, calculatedRequirements);
269 if (assosiateElementToData.isRight()) {
270 return assosiateElementToData.right().value();
273 Map<String, MapListRequirementDataDefinition> fullfilledRequirements = topologyTemplate.getFullfilledRequirements();
274 if (fullfilledRequirements != null && !fullfilledRequirements.isEmpty()) {
275 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.FULLFILLED_REQUIREMENTS, EdgeLabelEnum.FULLFILLED_REQUIREMENTS, fullfilledRequirements);
276 if (assosiateElementToData.isRight()) {
277 return assosiateElementToData.right().value();
280 return StorageOperationStatus.OK;
283 private StorageOperationStatus associateTopologyTemplateArtifactsToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
284 Map<String, ArtifactDataDefinition> addInformation = topologyTemplate.getServiceApiArtifacts();
286 if (addInformation != null && !addInformation.isEmpty()) {
287 addInformation.values().stream().filter(a -> a.getUniqueId() == null).forEach(a -> {
288 String uniqueId = UniqueIdBuilder.buildPropertyUniqueId(nodeTypeVertex.getUniqueId().toLowerCase(), a.getArtifactLabel().toLowerCase());
289 a.setUniqueId(uniqueId);
291 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.SERVICE_API_ARTIFACTS, EdgeLabelEnum.SERVICE_API_ARTIFACTS, addInformation);
292 if (assosiateElementToData.isRight()) {
293 return assosiateElementToData.right().value();
296 Map<String, MapArtifactDataDefinition> instArtifacts = topologyTemplate.getInstDeploymentArtifacts();
298 if (instArtifacts != null && !instArtifacts.isEmpty()) {
299 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.INST_DEPLOYMENT_ARTIFACTS, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS, instArtifacts);
300 if (assosiateElementToData.isRight()) {
301 return assosiateElementToData.right().value();
304 Map<String, MapArtifactDataDefinition> instInfoArtifacts = topologyTemplate.getInstanceArtifacts();
306 if (instInfoArtifacts != null && !instInfoArtifacts.isEmpty()) {
307 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.INSTANCE_ARTIFACTS, EdgeLabelEnum.INSTANCE_ARTIFACTS, instInfoArtifacts);
308 if (assosiateElementToData.isRight()) {
309 return assosiateElementToData.right().value();
312 return StorageOperationStatus.OK;
315 private StorageOperationStatus addAdditionalInformationToResource(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
317 Map<String, AdditionalInfoParameterDataDefinition> addInformation = topologyTemplate.getAdditionalInformation();
319 if (addInformation != null && !addInformation.isEmpty()) {
320 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.ADDITIONAL_INFORMATION, EdgeLabelEnum.ADDITIONAL_INFORMATION, addInformation);
321 if (assosiateElementToData.isRight()) {
322 return assosiateElementToData.right().value();
325 return StorageOperationStatus.OK;
328 public StorageOperationStatus associateInstPropertiesToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
329 Map<String, MapPropertiesDataDefinition> instProps = topologyTemplate.getInstProperties();
330 return associateInstPropertiesToComponent(nodeTypeVertex, instProps);
333 public StorageOperationStatus associateInstInputsToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
334 Map<String, MapPropertiesDataDefinition> instProps = topologyTemplate.getInstInputs();
335 return associateInstInputsToComponent(nodeTypeVertex, instProps);
338 public StorageOperationStatus associateInstGroupsToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
339 Map<String, MapGroupsDataDefinition> instGroups = topologyTemplate.getInstGroups();
340 return associateInstGroupsToComponent(nodeTypeVertex, instGroups);
344 public StorageOperationStatus associateInstPropertiesToComponent(GraphVertex nodeTypeVertex, Map<String, MapPropertiesDataDefinition> instProps) {
345 if (instProps != null && !instProps.isEmpty()) {
346 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.INST_PROPERTIES, EdgeLabelEnum.INST_PROPERTIES, instProps);
347 if (assosiateElementToData.isRight()) {
348 return assosiateElementToData.right().value();
351 return StorageOperationStatus.OK;
354 public StorageOperationStatus associateInstInputsToComponent(GraphVertex nodeTypeVertex, Map<String, MapPropertiesDataDefinition> instInputs) {
355 if (instInputs != null && !instInputs.isEmpty()) {
356 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.INST_INPUTS, EdgeLabelEnum.INST_INPUTS, instInputs);
357 if (assosiateElementToData.isRight()) {
358 return assosiateElementToData.right().value();
361 return StorageOperationStatus.OK;
364 public StorageOperationStatus associateInstGroupsToComponent(GraphVertex nodeTypeVertex, Map<String, MapGroupsDataDefinition> instGroups) {
365 if (instGroups != null && !instGroups.isEmpty()) {
366 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.INST_GROUPS, EdgeLabelEnum.INST_GROUPS, instGroups);
367 if (assosiateElementToData.isRight()) {
368 return assosiateElementToData.right().value();
371 return StorageOperationStatus.OK;
375 public StorageOperationStatus deleteInstInputsToComponent(GraphVertex nodeTypeVertex, Map<String, MapPropertiesDataDefinition> instInputs) {
377 if (instInputs != null && !instInputs.isEmpty()) {
378 instInputs.entrySet().forEach(i -> {
379 List<String> uniqueKeys = new ArrayList<String>(i.getValue().getMapToscaDataDefinition().keySet());
380 List<String> pathKeys = new ArrayList<String>();
381 pathKeys.add(i.getKey());
383 StorageOperationStatus status = deleteToscaDataDeepElements(nodeTypeVertex, EdgeLabelEnum.INST_INPUTS, VertexTypeEnum.INST_INPUTS, uniqueKeys, pathKeys, JsonPresentationFields.NAME);
384 if (status != StorageOperationStatus.OK) {
390 return StorageOperationStatus.OK;
393 public StorageOperationStatus addInstPropertiesToComponent(GraphVertex nodeTypeVertex, Map<String, MapPropertiesDataDefinition> instInputs) {
395 if (instInputs != null && !instInputs.isEmpty()) {
396 instInputs.entrySet().forEach(i -> {
397 StorageOperationStatus status = addToscaDataDeepElementsBlockToToscaElement(nodeTypeVertex, EdgeLabelEnum.INST_PROPERTIES, VertexTypeEnum.INST_PROPERTIES, i.getValue(), i.getKey());
398 if (status != StorageOperationStatus.OK) {
404 return StorageOperationStatus.OK;
407 public StorageOperationStatus associateInstDeploymentArtifactsToComponent(GraphVertex nodeTypeVertex, Map<String, MapArtifactDataDefinition> instArtifacts) {
408 return associateInstanceArtifactsToComponent(nodeTypeVertex, instArtifacts, VertexTypeEnum.INST_DEPLOYMENT_ARTIFACTS, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS);
411 public StorageOperationStatus associateInstArtifactsToComponent(GraphVertex nodeTypeVertex, Map<String, MapArtifactDataDefinition> instArtifacts) {
412 return associateInstanceArtifactsToComponent(nodeTypeVertex, instArtifacts, VertexTypeEnum.INSTANCE_ARTIFACTS, EdgeLabelEnum.INSTANCE_ARTIFACTS);
415 private StorageOperationStatus associateInstanceArtifactsToComponent(GraphVertex nodeTypeVertex, Map<String, MapArtifactDataDefinition> instProps, VertexTypeEnum vertexType, EdgeLabelEnum edgeLabel) {
416 if (instProps != null && !instProps.isEmpty()) {
417 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, vertexType, edgeLabel, instProps);
418 if (assosiateElementToData.isRight()) {
419 return assosiateElementToData.right().value();
422 return StorageOperationStatus.OK;
425 public StorageOperationStatus associateCalcCapReqToComponent(GraphVertex nodeTypeVertex, Map<String, MapListRequirementDataDefinition> calcRequirements, Map<String, MapListCapabiltyDataDefinition> calcCapabilty, Map<String, MapCapabiltyProperty> calculatedCapabilitiesProperties) {
426 if (calcRequirements != null && !calcRequirements.isEmpty()) {
427 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.CALCULATED_REQUIREMENTS, EdgeLabelEnum.CALCULATED_REQUIREMENTS, calcRequirements);
428 if (assosiateElementToData.isRight()) {
429 return assosiateElementToData.right().value();
431 Map<String, MapListRequirementDataDefinition> fullFilled = new HashMap<>();
432 assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.FULLFILLED_REQUIREMENTS, EdgeLabelEnum.FULLFILLED_REQUIREMENTS, fullFilled);
433 if (assosiateElementToData.isRight()) {
434 return assosiateElementToData.right().value();
437 if (calcCapabilty != null && !calcCapabilty.isEmpty()) {
438 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.CALCULATED_CAPABILITIES, EdgeLabelEnum.CALCULATED_CAPABILITIES, calcCapabilty);
439 if (assosiateElementToData.isRight()) {
440 return assosiateElementToData.right().value();
442 Map<String, MapListCapabiltyDataDefinition> fullFilled = new HashMap<>();
443 assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.FULLFILLED_CAPABILITIES, EdgeLabelEnum.FULLFILLED_CAPABILITIES, fullFilled);
444 if (assosiateElementToData.isRight()) {
445 return assosiateElementToData.right().value();
448 if ( calculatedCapabilitiesProperties != null && !calculatedCapabilitiesProperties.isEmpty() ){
449 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.CALCULATED_CAP_PROPERTIES, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES, calculatedCapabilitiesProperties);
450 if (assosiateElementToData.isRight()) {
451 return assosiateElementToData.right().value();
454 return StorageOperationStatus.OK;
457 private StorageOperationStatus associateInstAttributesToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
458 Map<String, MapPropertiesDataDefinition> instAttr = topologyTemplate.getInstAttributes();
459 return associateInstAttributeToComponent(nodeTypeVertex, instAttr);
462 public StorageOperationStatus associateForwardingPathToComponent(GraphVertex nodeTypeVertex, Map<String, ForwardingPathDataDefinition> forwardingPathMap) {
463 if (forwardingPathMap != null && !forwardingPathMap.isEmpty()) {
464 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.FORWARDING_PATH, EdgeLabelEnum.FORWARDING_PATH, forwardingPathMap);
465 if (assosiateElementToData.isRight()) {
466 return assosiateElementToData.right().value();
469 return StorageOperationStatus.OK;
472 public StorageOperationStatus associateInstAttributeToComponent(GraphVertex nodeTypeVertex, Map<String, MapPropertiesDataDefinition> instAttr) {
473 if (instAttr != null && !instAttr.isEmpty()) {
474 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.INST_ATTRIBUTES, EdgeLabelEnum.INST_ATTRIBUTES, instAttr);
475 if (assosiateElementToData.isRight()) {
476 return assosiateElementToData.right().value();
479 return StorageOperationStatus.OK;
482 public StorageOperationStatus associateGroupsToComponent(GraphVertex nodeTypeVertex, Map<String, GroupDataDefinition> groups) {
484 if (groups != null && !groups.isEmpty()) {
485 groups.values().stream().filter(p -> p.getUniqueId() == null).forEach(p -> {
486 String uid = UniqueIdBuilder.buildGroupingUid(nodeTypeVertex.getUniqueId(), p.getName());
489 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.GROUPS, EdgeLabelEnum.GROUPS, groups);
490 if (assosiateElementToData.isRight()) {
491 return assosiateElementToData.right().value();
494 return StorageOperationStatus.OK;
497 private StorageOperationStatus associateGroupsToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
498 return associateGroupsToComponent(nodeTypeVertex, topologyTemplate.getGroups());
501 public StorageOperationStatus associateInputsToComponent(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
502 Map<String, PropertyDataDefinition> inputs = topologyTemplate.getInputs();
503 return associateInputsToComponent(nodeTypeVertex, inputs, topologyTemplate.getUniqueId());
506 public StorageOperationStatus associateInputsToComponent(GraphVertex nodeTypeVertex, Map<String, PropertyDataDefinition> inputs, String id) {
507 if (inputs != null && !inputs.isEmpty()) {
508 inputs.values().stream().filter(e -> e.getUniqueId() == null).forEach(e -> e.setUniqueId(UniqueIdBuilder.buildPropertyUniqueId(id, e.getName())));
510 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.INPUTS, EdgeLabelEnum.INPUTS, inputs);
511 if (assosiateElementToData.isRight()) {
512 return assosiateElementToData.right().value();
515 return StorageOperationStatus.OK;
518 private GraphVertex fillMetadata(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate, JsonParseFlagEnum flag) {
519 nodeTypeVertex.setLabel(VertexTypeEnum.TOPOLOGY_TEMPLATE);
520 fillCommonMetadata(nodeTypeVertex, topologyTemplate);
521 if (flag == JsonParseFlagEnum.ParseAll || flag == JsonParseFlagEnum.ParseJson) {
522 nodeTypeVertex.setJson(topologyTemplate.getCompositions());
524 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.CSAR_UUID, topologyTemplate.getMetadataValue(JsonPresentationFields.CSAR_UUID));
525 nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.DISTRIBUTION_STATUS, topologyTemplate.getMetadataValue(JsonPresentationFields.DISTRIBUTION_STATUS));
527 return nodeTypeVertex;
531 private StorageOperationStatus assosiateMetadataToCategory(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
532 if (topologyTemplate.getResourceType() == null) {
534 return associateServiceMetadataToCategory(nodeTypeVertex, topologyTemplate);
537 return assosiateResourceMetadataToCategory(nodeTypeVertex, topologyTemplate);
541 private StorageOperationStatus associateServiceMetadataToCategory(GraphVertex nodeTypeVertex, TopologyTemplate topologyTemplate) {
542 String categoryName = topologyTemplate.getCategories().get(0).getName();
543 Either<GraphVertex, StorageOperationStatus> category = categoryOperation.getCategory(categoryName, VertexTypeEnum.SERVICE_CATEGORY);
544 if (category.isRight()) {
545 log.trace("NO category {} for service {}", categoryName, topologyTemplate.getUniqueId());
546 return StorageOperationStatus.CATEGORY_NOT_FOUND;
548 GraphVertex categoryV = category.left().value();
549 TitanOperationStatus createEdge = titanDao.createEdge(nodeTypeVertex, categoryV, EdgeLabelEnum.CATEGORY, new HashMap<>());
550 if (createEdge != TitanOperationStatus.OK) {
551 log.trace("Failed to associate resource {} to category {} with id {}", topologyTemplate.getUniqueId(), categoryName, categoryV.getUniqueId());
552 return DaoStatusConverter.convertTitanStatusToStorageStatus(createEdge);
554 return StorageOperationStatus.OK;
558 public Either<ToscaElement, StorageOperationStatus> getToscaElement(String uniqueId, ComponentParametersView componentParametersView) {
559 JsonParseFlagEnum parseFlag = componentParametersView.detectParseFlag();
561 Either<GraphVertex, StorageOperationStatus> componentByLabelAndId = getComponentByLabelAndId(uniqueId, ToscaElementTypeEnum.TopologyTemplate, parseFlag);
562 if (componentByLabelAndId.isRight()) {
563 return Either.right(componentByLabelAndId.right().value());
565 GraphVertex componentV = componentByLabelAndId.left().value();
567 return getToscaElement(componentV, componentParametersView);
570 // -------------------------------------------------------------
572 public Either<ToscaElement, StorageOperationStatus> getToscaElement(GraphVertex componentV, ComponentParametersView componentParametersView) {
573 TopologyTemplate toscaElement;
575 toscaElement = convertToTopologyTemplate(componentV);
576 TitanOperationStatus status;
577 if (!componentParametersView.isIgnoreUsers()) {
578 status = setCreatorFromGraph(componentV, toscaElement);
579 if (status != TitanOperationStatus.OK) {
580 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
583 status = setLastModifierFromGraph(componentV, toscaElement);
584 if (status != TitanOperationStatus.OK) {
585 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
588 if (!componentParametersView.isIgnoreCategories()) {
589 status = setTopologyTempalteCategoriesFromGraph(componentV, toscaElement);
590 if (status != TitanOperationStatus.OK) {
591 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
595 if (!componentParametersView.isIgnoreArtifacts()) {
596 TitanOperationStatus storageStatus = setAllArtifactsFromGraph(componentV, toscaElement);
597 if (storageStatus != TitanOperationStatus.OK) {
598 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(storageStatus));
601 if (!componentParametersView.isIgnoreComponentInstancesProperties()) {
602 status = setComponentInstancesPropertiesFromGraph(componentV, toscaElement);
603 if (status != TitanOperationStatus.OK) {
604 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
607 if (!componentParametersView.isIgnoreCapabilities()) {
608 status = setCapabilitiesFromGraph(componentV, toscaElement);
609 if (status != TitanOperationStatus.OK) {
610 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
613 if (!componentParametersView.isIgnoreRequirements()) {
614 status = setRequirementsFromGraph(componentV, toscaElement);
615 if (status != TitanOperationStatus.OK) {
616 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
619 if (!componentParametersView.isIgnoreAllVersions()) {
620 status = setAllVersions(componentV, toscaElement);
621 if (status != TitanOperationStatus.OK) {
622 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
625 if (!componentParametersView.isIgnoreAdditionalInformation()) {
626 status = setAdditionalInformationFromGraph(componentV, toscaElement);
627 if (status != TitanOperationStatus.OK) {
628 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
632 if (!componentParametersView.isIgnoreGroups()) {
633 status = setGroupsFromGraph(componentV, toscaElement);
634 if (status != TitanOperationStatus.OK) {
635 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
639 if (!componentParametersView.isIgnorePolicies()) {
640 status = setPoliciesFromGraph(componentV, toscaElement);
641 if (status != TitanOperationStatus.OK) {
642 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
646 if (!componentParametersView.isIgnoreComponentInstances()) {
647 status = setInstGroupsFromGraph(componentV, toscaElement);
648 if (status != TitanOperationStatus.OK) {
649 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
653 if (!componentParametersView.isIgnoreInputs()) {
654 status = setInputsFromGraph(componentV, toscaElement);
655 if (status != TitanOperationStatus.OK) {
656 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
660 if (!componentParametersView.isIgnoreProperties()) {
661 status = setPropertiesFromGraph(componentV, toscaElement);
662 if (status != TitanOperationStatus.OK) {
663 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
668 if (!componentParametersView.isIgnoreComponentInstancesInputs()) {
669 status = setComponentInstancesInputsFromGraph(componentV, toscaElement);
670 if (status != TitanOperationStatus.OK) {
671 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
676 if (!componentParametersView.isIgnoreCapabiltyProperties()) {
677 status = setComponentInstancesCapPropertiesFromGraph(componentV, toscaElement);
678 if (status != TitanOperationStatus.OK) {
679 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
684 if (!componentParametersView.isIgnoreForwardingPath()) {
685 status = setForwardingGraphPropertiesFromGraph(componentV, toscaElement);
686 if (status != TitanOperationStatus.OK) {
687 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
691 if (!componentParametersView.isIgnoreInterfaces()) {
692 TitanOperationStatus storageStatus = setInterfcesFromGraph(componentV, toscaElement);
693 if (storageStatus != TitanOperationStatus.OK) {
694 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(storageStatus));
698 return Either.left(toscaElement);
701 private TitanOperationStatus setInterfcesFromGraph(GraphVertex componentV,
702 TopologyTemplate topologyTemplate) {
703 Either<Map<String, InterfaceDataDefinition>, TitanOperationStatus> result = getDataFromGraph
705 EdgeLabelEnum.INTERFACE_ARTIFACTS);
706 if (result.isLeft()) {
707 topologyTemplate.setInterfaces(result.left().value());
709 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
710 return result.right().value();
713 return TitanOperationStatus.OK;
716 private StorageOperationStatus associateInterfacesToResource(GraphVertex topologyTemplateVertex,
717 TopologyTemplate topologyTemplate) {
718 Map<String, InterfaceDataDefinition> interfaces = topologyTemplate.getInterfaces();
719 return associateInterfacesToComponent(topologyTemplateVertex,interfaces);
722 public StorageOperationStatus associateInterfacesToComponent(GraphVertex nodeTypeVertex,
723 Map<String, InterfaceDataDefinition>
725 if (interfaceMap != null && !interfaceMap.isEmpty()) {
726 Either<GraphVertex, StorageOperationStatus> assosiateElementToData = assosiateElementToData
727 (nodeTypeVertex, VertexTypeEnum.INTERFACE_ARTIFACTS, EdgeLabelEnum.INTERFACE_ARTIFACTS, interfaceMap);
728 if (assosiateElementToData.isRight()) {
729 return assosiateElementToData.right().value();
732 return StorageOperationStatus.OK;
736 private TitanOperationStatus setPoliciesFromGraph(GraphVertex componentV, TopologyTemplate toscaElement) {
737 Either<Map<String, PolicyDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.POLICIES);
738 if (result.isLeft()) {
739 toscaElement.setPolicies(result.left().value());
741 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
742 return result.right().value();
745 return TitanOperationStatus.OK;
748 private TitanOperationStatus setForwardingGraphPropertiesFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
749 Either<Map<String, ForwardingPathDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.FORWARDING_PATH);
750 if (result.isLeft()) {
751 topologyTemplate.setForwardingPaths(result.left().value());
753 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
754 return result.right().value();
757 return TitanOperationStatus.OK;
761 private TitanOperationStatus setComponentInstancesCapPropertiesFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
762 Either<Map<String, MapCapabiltyProperty>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES);
763 if (result.isLeft()) {
764 topologyTemplate.setCalculatedCapabilitiesProperties(result.left().value());
766 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
767 return result.right().value();
770 return TitanOperationStatus.OK;
773 private TitanOperationStatus setPropertiesFromGraph(GraphVertex componentV, TopologyTemplate toscaElement) {
774 Either<Map<String, PropertyDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.PROPERTIES);
775 if (result.isLeft()) {
776 toscaElement.setProperties(result.left().value());
778 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
779 return result.right().value();
782 return TitanOperationStatus.OK;
785 private TitanOperationStatus setInstGroupsFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
786 Either<Map<String, MapGroupsDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.INST_GROUPS);
787 if (result.isLeft()) {
788 topologyTemplate.setInstGroups(result.left().value());
790 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
791 return result.right().value();
794 return TitanOperationStatus.OK;
797 private TitanOperationStatus setComponentInstancesPropertiesFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
798 Either<Map<String, MapPropertiesDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.INST_PROPERTIES);
799 if (result.isLeft()) {
800 topologyTemplate.setInstProperties(result.left().value());
802 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
803 return result.right().value();
806 return TitanOperationStatus.OK;
809 private TitanOperationStatus setComponentInstancesInputsFromGraph(GraphVertex componentV, TopologyTemplate topologyTemplate) {
810 Either<Map<String, MapPropertiesDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.INST_INPUTS);
811 if (result.isLeft()) {
812 topologyTemplate.setInstInputs(result.left().value());
814 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
815 return result.right().value();
818 return TitanOperationStatus.OK;
822 protected <T extends ToscaElement> TitanOperationStatus setRequirementsFromGraph(GraphVertex componentV, T toscaElement) {
823 Either<Map<String, MapListRequirementDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.CALCULATED_REQUIREMENTS);
824 if (result.isLeft()) {
825 ((TopologyTemplate) toscaElement).setCalculatedRequirements(result.left().value());
827 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
828 return result.right().value();
831 result = getDataFromGraph(componentV, EdgeLabelEnum.FULLFILLED_REQUIREMENTS);
832 if (result.isLeft()) {
833 ((TopologyTemplate) toscaElement).setFullfilledRequirements(result.left().value());
835 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
836 return result.right().value();
839 return TitanOperationStatus.OK;
843 protected <T extends ToscaElement> TitanOperationStatus setCapabilitiesFromGraph(GraphVertex componentV, T toscaElement) {
844 Either<Map<String, MapListCapabiltyDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.CALCULATED_CAPABILITIES);
845 if (result.isLeft()) {
846 ((TopologyTemplate) toscaElement).setCalculatedCapabilities(result.left().value());
848 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
849 return result.right().value();
852 result = getDataFromGraph(componentV, EdgeLabelEnum.FULLFILLED_CAPABILITIES);
853 if (result.isLeft()) {
854 ((TopologyTemplate) toscaElement).setFullfilledCapabilities(result.left().value());
856 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
857 return result.right().value();
860 return TitanOperationStatus.OK;
863 private TitanOperationStatus setAllArtifactsFromGraph(GraphVertex componentV, TopologyTemplate toscaElement) {
864 TitanOperationStatus storageStatus = setArtifactsFromGraph(componentV, toscaElement);
865 if (storageStatus != TitanOperationStatus.OK) {
866 return storageStatus;
868 Either<Map<String, ArtifactDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.SERVICE_API_ARTIFACTS);
869 if (result.isLeft()) {
870 toscaElement.setServiceApiArtifacts(result.left().value());
872 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
873 return result.right().value();
876 Either<Map<String, MapArtifactDataDefinition>, TitanOperationStatus> resultInstArt = getDataFromGraph(componentV, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS);
877 if (resultInstArt.isLeft()) {
878 toscaElement.setInstDeploymentArtifacts(resultInstArt.left().value());
880 if (resultInstArt.right().value() != TitanOperationStatus.NOT_FOUND) {
881 return resultInstArt.right().value();
884 Either<Map<String, MapArtifactDataDefinition>, TitanOperationStatus> instanceArt = getDataFromGraph(componentV, EdgeLabelEnum.INSTANCE_ARTIFACTS);
885 if (instanceArt.isLeft()) {
886 toscaElement.setInstanceArtifacts(instanceArt.left().value());
888 if (instanceArt.right().value() != TitanOperationStatus.NOT_FOUND) {
889 return instanceArt.right().value();
892 return TitanOperationStatus.OK;
895 private TitanOperationStatus setInputsFromGraph(GraphVertex componentV, TopologyTemplate toscaElement) {
896 Either<Map<String, PropertyDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.INPUTS);
897 if (result.isLeft()) {
898 toscaElement.setInputs(result.left().value());
900 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
901 return result.right().value();
904 return TitanOperationStatus.OK;
907 private TitanOperationStatus setGroupsFromGraph(GraphVertex componentV, TopologyTemplate toscaElement) {
908 Either<Map<String, GroupDataDefinition>, TitanOperationStatus> result = getDataFromGraph(componentV, EdgeLabelEnum.GROUPS);
909 if (result.isLeft()) {
910 toscaElement.setGroups(result.left().value());
912 if (result.right().value() != TitanOperationStatus.NOT_FOUND) {
913 return result.right().value();
916 return TitanOperationStatus.OK;
919 private TitanOperationStatus setTopologyTempalteCategoriesFromGraph(GraphVertex componentV, ToscaElement toscaElement) {
920 List<CategoryDefinition> categories = new ArrayList<>();
922 switch (componentV.getType()) {
924 return setResourceCategoryFromGraph(componentV, toscaElement);
926 return setServiceCategoryFromGraph(componentV, toscaElement, categories);
929 log.debug("Not supported component type {} ", componentV.getType());
932 return TitanOperationStatus.OK;
935 private TitanOperationStatus setServiceCategoryFromGraph(GraphVertex componentV, ToscaElement toscaElement, List<CategoryDefinition> categories) {
936 Either<GraphVertex, TitanOperationStatus> childVertex = titanDao.getChildVertex(componentV, EdgeLabelEnum.CATEGORY, JsonParseFlagEnum.NoParse);
937 if (childVertex.isRight()) {
938 log.debug("failed to fetch {} for tosca element with id {}, error {}", EdgeLabelEnum.CATEGORY, componentV.getUniqueId(), childVertex.right().value());
939 return childVertex.right().value();
941 GraphVertex categoryV = childVertex.left().value();
942 Map<GraphPropertyEnum, Object> metadataProperties = categoryV.getMetadataProperties();
943 CategoryDefinition category = new CategoryDefinition();
944 category.setUniqueId(categoryV.getUniqueId());
945 category.setNormalizedName((String) metadataProperties.get(GraphPropertyEnum.NORMALIZED_NAME));
946 category.setName((String) metadataProperties.get(GraphPropertyEnum.NAME));
948 Type listTypeCat = new TypeToken<List<String>>() {}.getType();
949 List<String> iconsfromJsonCat = getGson().fromJson((String) metadataProperties.get(GraphPropertyEnum.ICONS.getProperty()), listTypeCat);
950 category.setIcons(iconsfromJsonCat);
951 categories.add(category);
952 toscaElement.setCategories(categories);
954 return TitanOperationStatus.OK;
957 @SuppressWarnings("unchecked")
958 private TopologyTemplate convertToTopologyTemplate(GraphVertex componentV) {
960 TopologyTemplate topologyTemplate = super.convertToComponent(componentV);
962 Map<String, CompositionDataDefinition> json = (Map<String, CompositionDataDefinition>) componentV.getJson();
963 topologyTemplate.setCompositions(json);
965 return topologyTemplate;
969 public Either<ToscaElement, StorageOperationStatus> deleteToscaElement(GraphVertex toscaElementVertex) {
970 Either<ToscaElement, StorageOperationStatus> nodeType = getToscaElement(toscaElementVertex, new ComponentParametersView());
971 if (nodeType.isRight()) {
972 log.debug("Failed to fetch tosca element {} error {}", toscaElementVertex.getUniqueId(), nodeType.right().value());
975 TitanOperationStatus status = disassociateAndDeleteCommonElements(toscaElementVertex);
976 if (status != TitanOperationStatus.OK) {
977 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
979 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_ATTRIBUTES);
980 if (status != TitanOperationStatus.OK) {
981 log.debug("Failed to disassociate instances attributes for {} error {}", toscaElementVertex.getUniqueId(), status);
982 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
984 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_PROPERTIES);
985 if (status != TitanOperationStatus.OK) {
986 log.debug("Failed to disassociate instances properties for {} error {}", toscaElementVertex.getUniqueId(), status);
987 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
990 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_INPUTS);
991 if (status != TitanOperationStatus.OK) {
992 log.debug("Failed to disassociate instances inputs for {} error {}", toscaElementVertex.getUniqueId(), status);
993 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
996 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.GROUPS);
997 if (status != TitanOperationStatus.OK) {
998 log.debug("Failed to disassociate groups for {} error {}", toscaElementVertex.getUniqueId(), status);
999 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1001 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_GROUPS);
1002 if (status != TitanOperationStatus.OK) {
1003 log.debug("Failed to disassociate instance groups for {} error {}", toscaElementVertex.getUniqueId(), status);
1004 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1006 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INPUTS);
1007 if (status != TitanOperationStatus.OK) {
1008 log.debug("Failed to disassociate inputs for {} error {}", toscaElementVertex.getUniqueId(), status);
1009 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1011 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_INPUTS);
1012 if (status != TitanOperationStatus.OK) {
1013 log.debug("Failed to disassociate instance inputs for {} error {}", toscaElementVertex.getUniqueId(), status);
1014 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1016 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.CALCULATED_CAPABILITIES);
1017 if (status != TitanOperationStatus.OK) {
1018 log.debug("Failed to disassociate calculated capabiliites for {} error {}", toscaElementVertex.getUniqueId(), status);
1019 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1021 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.FULLFILLED_CAPABILITIES);
1022 if (status != TitanOperationStatus.OK) {
1023 log.debug("Failed to disassociate fullfilled capabilities for {} error {}", toscaElementVertex.getUniqueId(), status);
1024 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1026 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES);
1027 if (status != TitanOperationStatus.OK) {
1028 log.debug("Failed to disassociate calculated capabiliites properties for {} error {}", toscaElementVertex.getUniqueId(), status);
1029 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1031 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.CALCULATED_REQUIREMENTS);
1032 if (status != TitanOperationStatus.OK) {
1033 log.debug("Failed to disassociate calculated requirements for {} error {}", toscaElementVertex.getUniqueId(), status);
1034 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1036 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.FULLFILLED_REQUIREMENTS);
1037 if (status != TitanOperationStatus.OK) {
1038 log.debug("Failed to disassociate full filled requirements for {} error {}", toscaElementVertex.getUniqueId(), status);
1039 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1041 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS);
1042 if (status != TitanOperationStatus.OK) {
1043 log.debug("Failed to disassociate instance artifacts for {} error {}", toscaElementVertex.getUniqueId(), status);
1044 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1046 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.SERVICE_API_ARTIFACTS);
1047 if (status != TitanOperationStatus.OK) {
1048 log.debug("Failed to disassociate service api artifacts for {} error {}", toscaElementVertex.getUniqueId(), status);
1049 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1051 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.FORWARDING_PATH);
1052 if (status != TitanOperationStatus.OK) {
1053 log.debug("Failed to disassociate service api artifacts for {} error {}", toscaElementVertex.getUniqueId(), status);
1054 Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1057 status = titanDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.INSTANCE_ARTIFACTS);
1058 toscaElementVertex.getVertex().remove();
1059 log.trace("Tosca element vertex for {} was removed", toscaElementVertex.getUniqueId());
1064 @SuppressWarnings("unchecked")
1066 public Either<TopologyTemplate, StorageOperationStatus> createToscaElement(ToscaElement toscaElement) {
1067 return createTopologyTemplate((TopologyTemplate) toscaElement);
1071 protected <T extends ToscaElement> TitanOperationStatus setCategoriesFromGraph(GraphVertex vertexComponent, T toscaElement) {
1072 return setTopologyTempalteCategoriesFromGraph(vertexComponent, toscaElement);
1076 protected <T extends ToscaElement> StorageOperationStatus validateCategories(T toscaElementToUpdate, GraphVertex elementV) {
1077 // Product isn't supported now!!
1078 // TODO add for Product
1079 if (toscaElementToUpdate.getComponentType() == ComponentTypeEnum.SERVICE) {
1080 return validateServiceCategory(toscaElementToUpdate, elementV);
1083 return validateResourceCategory(toscaElementToUpdate, elementV);
1088 protected <T extends ToscaElement> StorageOperationStatus updateDerived(T toscaElementToUpdate, GraphVertex updateElementV) {
1089 // not relevant now for topology template
1090 return StorageOperationStatus.OK;
1094 public <T extends ToscaElement> void fillToscaElementVertexData(GraphVertex elementV, T toscaElementToUpdate, JsonParseFlagEnum flag) {
1095 fillMetadata(elementV, (TopologyTemplate) toscaElementToUpdate, flag);
1098 private <T extends ToscaElement> StorageOperationStatus validateServiceCategory(T toscaElementToUpdate, GraphVertex elementV) {
1099 StorageOperationStatus status = StorageOperationStatus.OK;
1100 List<CategoryDefinition> newCategoryList = toscaElementToUpdate.getCategories();
1101 CategoryDefinition newCategory = newCategoryList.get(0);
1103 Either<GraphVertex, TitanOperationStatus> childVertex = titanDao.getChildVertex(elementV, EdgeLabelEnum.CATEGORY, JsonParseFlagEnum.NoParse);
1104 if (childVertex.isRight()) {
1105 log.debug("failed to fetch {} for tosca element with id {}, error {}", EdgeLabelEnum.CATEGORY, elementV.getUniqueId(), childVertex.right().value());
1106 return DaoStatusConverter.convertTitanStatusToStorageStatus(childVertex.right().value());
1109 GraphVertex categoryV = childVertex.left().value();
1110 Map<GraphPropertyEnum, Object> metadataProperties = categoryV.getMetadataProperties();
1111 String categoryNameCurrent = (String) metadataProperties.get(GraphPropertyEnum.NAME);
1113 String newCategoryName = newCategory.getName();
1114 if (newCategoryName != null && false == newCategoryName.equals(categoryNameCurrent)) {
1115 // the category was changed
1116 Either<GraphVertex, StorageOperationStatus> getCategoryVertex = categoryOperation.getCategory(newCategoryName, VertexTypeEnum.SERVICE_CATEGORY);
1118 if (getCategoryVertex.isRight()) {
1119 return getCategoryVertex.right().value();
1121 GraphVertex newCategoryV = getCategoryVertex.left().value();
1122 status = moveCategoryEdge(elementV, newCategoryV);
1123 log.debug("Going to update the category of the resource from {} to {}. status is {}", categoryNameCurrent, newCategory, status);
1128 public Either<List<GraphVertex>, TitanOperationStatus> getAllNotDeletedElements() {
1129 Map<GraphPropertyEnum, Object> propsHasNot = new HashMap<>();
1130 propsHasNot.put(GraphPropertyEnum.IS_DELETED, true);
1132 Either<List<GraphVertex>, TitanOperationStatus> byCriteria = titanDao.getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, null, propsHasNot, JsonParseFlagEnum.ParseJson);
1133 if (byCriteria.isRight()) {
1134 log.debug("Failed to fetch all non marked topology templates , propsHasNot {}, error {}", propsHasNot, byCriteria.right().value());
1135 return Either.right(byCriteria.right().value());
1137 return Either.left(byCriteria.left().value());
1140 @SuppressWarnings("unchecked")
1141 public boolean isInUse(GraphVertex elementV, List<GraphVertex> allNonDeleted) {
1142 for (GraphVertex containerV : allNonDeleted) {
1143 Map<String, CompositionDataDefinition> composition = (Map<String, CompositionDataDefinition>) containerV.getJson();
1144 if (composition != null) {
1145 CompositionDataDefinition instances = composition.get(JsonConstantKeysEnum.COMPOSITION.getValue());
1146 if (instances != null && instances.getComponentInstances() != null && !instances.getComponentInstances().isEmpty()) {
1147 for (ComponentInstanceDataDefinition ci : instances.getComponentInstances().values()) {
1148 if (ci.getComponentUid().equals(elementV.getUniqueId())) {
1149 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());
1161 @SuppressWarnings("unchecked")
1162 public boolean isInUse(String componentId, List<GraphVertex> allNonDeleted) {
1163 for (GraphVertex containerV : allNonDeleted) {
1164 Map<String, CompositionDataDefinition> composition = (Map<String, CompositionDataDefinition>) containerV.getJson();
1165 if (composition != null) {
1166 CompositionDataDefinition instances = composition.get(JsonConstantKeysEnum.COMPOSITION.getValue());
1167 if (instances != null && instances.getComponentInstances() != null && !instances.getComponentInstances().isEmpty()) {
1168 for (ComponentInstanceDataDefinition ci : instances.getComponentInstances().values()) {
1169 if (ci.getComponentUid().equals(componentId)) {
1181 public Either<GraphVertex, StorageOperationStatus> updateDistributionStatus(String uniqueId, User user, DistributionStatusEnum distributionStatus) {
1183 Either<GraphVertex, StorageOperationStatus> result = null;
1184 String userId = user.getUserId();
1185 Either<GraphVertex, TitanOperationStatus> getRes = findUserVertex(userId);
1186 GraphVertex userVertex = null;
1187 GraphVertex serviceVertex = null;
1188 if (getRes.isRight()) {
1189 TitanOperationStatus status = getRes.right().value();
1190 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Cannot find user {} in the graph. status is {}", userId, status);
1191 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1193 if (result == null) {
1194 userVertex = getRes.left().value();
1195 getRes = titanDao.getVertexById(uniqueId, JsonParseFlagEnum.ParseMetadata);
1196 if (getRes.isRight()) {
1197 TitanOperationStatus status = getRes.right().value();
1198 log.error("Cannot find service {} in the graph. status is {}", uniqueId, status);
1199 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1202 if (result == null) {
1203 serviceVertex = getRes.left().value();
1204 Iterator<Edge> edgeIterator = serviceVertex.getVertex().edges(Direction.IN, EdgeLabelEnum.LAST_DISTRIBUTION_STATE_MODIFIER.name());
1205 if (edgeIterator.hasNext()) {
1206 log.debug("Remove existing edge from user to component {}. Edge type is {}", userId, uniqueId, EdgeLabelEnum.LAST_DISTRIBUTION_STATE_MODIFIER);
1207 edgeIterator.next().remove();
1210 if (result == null) {
1211 TitanOperationStatus status = titanDao.createEdge(userVertex, serviceVertex, EdgeLabelEnum.LAST_DISTRIBUTION_STATE_MODIFIER, null);
1212 if (status != TitanOperationStatus.OK) {
1213 log.error("Failed to associate user {} to component {}. Edge type is {}", userId, uniqueId, EdgeLabelEnum.LAST_DISTRIBUTION_STATE_MODIFIER);
1214 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1217 if (result == null) {
1218 serviceVertex.addMetadataProperty(GraphPropertyEnum.DISTRIBUTION_STATUS, distributionStatus.name());
1219 long lastUpdateDate = System.currentTimeMillis();
1220 serviceVertex.setJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE, lastUpdateDate);
1221 Either<GraphVertex, TitanOperationStatus> updateRes = titanDao.updateVertex(serviceVertex);
1222 if (updateRes.isRight()) {
1223 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(updateRes.right().value()));
1226 if (result == null) {
1227 result = Either.left(serviceVertex);
1232 * Returns list of ComponentInstanceProperty belonging to component instance capability specified by name, type and ownerId
1233 * @param componentId
1235 * @param capabilityName
1236 * @param capabilityType
1240 public Either<List<ComponentInstanceProperty>, StorageOperationStatus> getComponentInstanceCapabilityProperties(String componentId, String instanceId, String capabilityName, String capabilityType, String ownerId) {
1242 Either<List<ComponentInstanceProperty>, StorageOperationStatus> result = null;
1243 Map<String, MapCapabiltyProperty> mapPropertiesDataDefinition = null;
1244 Either<GraphVertex, StorageOperationStatus> componentByLabelAndId = getComponentByLabelAndId(componentId, ToscaElementTypeEnum.TopologyTemplate, JsonParseFlagEnum.NoParse);
1245 if (componentByLabelAndId.isRight()) {
1246 result = Either.right(componentByLabelAndId.right().value());
1248 if(componentByLabelAndId.isLeft()){
1249 Either<Map<String, MapCapabiltyProperty>, TitanOperationStatus> getDataRes = getDataFromGraph(componentByLabelAndId.left().value(), EdgeLabelEnum.CALCULATED_CAP_PROPERTIES);
1250 if (getDataRes.isRight()) {
1251 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getDataRes.right().value()));
1253 mapPropertiesDataDefinition = getDataRes.left().value();
1256 if(isNotEmptyMapOfProperties(instanceId, mapPropertiesDataDefinition)){
1257 result = Either.left(findComponentInstanceCapabilityProperties(instanceId, capabilityName, capabilityType, ownerId, mapPropertiesDataDefinition.get(instanceId).getMapToscaDataDefinition()));
1262 public StorageOperationStatus updateComponentInstanceCapabilityProperties(Component containerComponent, String componentInstanceId, MapCapabiltyProperty instanceProperties) {
1263 return updateToscaDataDeepElementsBlockToToscaElement(containerComponent.getUniqueId(), EdgeLabelEnum.CALCULATED_CAP_PROPERTIES, instanceProperties, componentInstanceId);
1267 private boolean isNotEmptyMapOfProperties(String instanceId, Map<String, MapCapabiltyProperty> mapPropertiesDataDefinition) {
1268 return MapUtils.isNotEmpty(mapPropertiesDataDefinition) &&
1269 mapPropertiesDataDefinition.get(instanceId) != null &&
1270 MapUtils.isNotEmpty(mapPropertiesDataDefinition.get(instanceId).getMapToscaDataDefinition());
1273 private List<ComponentInstanceProperty> findComponentInstanceCapabilityProperties(String instanceId, String capabilityName, String capabilityType, String ownerId, Map<String, MapPropertiesDataDefinition> propertiesMap) {
1274 List<ComponentInstanceProperty> capPropsList = null;
1275 for(Entry<String, MapPropertiesDataDefinition> capProp : propertiesMap.entrySet()){
1276 if (isBelongingPropertyMap(instanceId, capabilityName, capabilityType, ownerId, capProp)) {
1277 Map<String, PropertyDataDefinition> capMap = capProp.getValue().getMapToscaDataDefinition();
1278 if (capMap != null && !capMap.isEmpty()) {
1279 capPropsList = capMap.values().stream().map(o -> new ComponentInstanceProperty(o)).collect(Collectors.toList());
1284 if(capPropsList == null){
1285 capPropsList = new ArrayList<>();
1287 return capPropsList;
1290 private boolean isBelongingPropertyMap(String instanceId, String capabilityName, String capabilityType, String ownerId, Entry<String, MapPropertiesDataDefinition> capProp) {
1291 if (capProp != null) {
1292 String[] path = capProp.getKey().split(ModelConverter.CAP_PROP_DELIM );
1293 if (path.length < 4) {
1294 log.debug("wrong key format for capabilty, key {}", capProp);
1297 return path[path.length - 2].equals(capabilityType) && path[path.length - 1].equals(capabilityName) && path[1].equals(ownerId) && path[0].equals(instanceId);
1302 public StorageOperationStatus addPolicyToToscaElement(GraphVertex componentV, PolicyDefinition policyDefinition, int counter) {
1303 fillPolicyDefinition(componentV, policyDefinition, counter);
1304 return addToscaDataToToscaElement(componentV, EdgeLabelEnum.POLICIES, VertexTypeEnum.POLICIES, policyDefinition, JsonPresentationFields.UNIQUE_ID);
1307 public StorageOperationStatus updatePolicyOfToscaElement(GraphVertex componentV, PolicyDefinition policyDefinition) {
1308 return updateToscaDataOfToscaElement(componentV, EdgeLabelEnum.POLICIES, VertexTypeEnum.POLICIES, policyDefinition, JsonPresentationFields.UNIQUE_ID);
1311 public StorageOperationStatus updatePoliciesOfToscaElement(GraphVertex componentV, List<PolicyDefinition> policiesDefinitions) {
1312 return updateToscaDataOfToscaElement(componentV, EdgeLabelEnum.POLICIES, VertexTypeEnum.POLICIES, policiesDefinitions, JsonPresentationFields.UNIQUE_ID);
1315 public StorageOperationStatus removePolicyFromToscaElement(GraphVertex componentV, String policyId) {
1316 return deleteToscaDataElement(componentV, EdgeLabelEnum.POLICIES, VertexTypeEnum.POLICIES, policyId, JsonPresentationFields.UNIQUE_ID);
1319 private void fillPolicyDefinition(GraphVertex componentV, PolicyDefinition policyDefinition, int counter) {
1320 String policyName = buildSubComponentName((String) componentV.getJsonMetadataField(JsonPresentationFields.NAME), policyDefinition.getPolicyTypeName(), counter);
1321 policyDefinition.setName(policyName);
1322 policyDefinition.setInvariantName(policyName);
1323 policyDefinition.setComponentName((String) componentV.getJsonMetadataField(JsonPresentationFields.NAME));
1324 policyDefinition.setUniqueId(UniqueIdBuilder.buildPolicyUniqueId(componentV.getUniqueId(), policyName));
1325 policyDefinition.setInvariantUUID(UniqueIdBuilder.buildInvariantUUID());
1326 policyDefinition.setPolicyUUID(UniqueIdBuilder.generateUUID());
1329 public static String buildSubComponentName(String componentName, String subComponentTypeName, int counter) {
1330 String typeSuffix = subComponentTypeName.substring(subComponentTypeName.lastIndexOf('.') + 1, subComponentTypeName.length());
1331 return componentName + Constants.GROUP_POLICY_NAME_DELIMETER + typeSuffix + Constants.GROUP_POLICY_NAME_DELIMETER + counter;