1 package org.openecomp.sdc.be.model.jsontitan.operations;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.EnumMap;
6 import java.util.HashMap;
9 import java.util.Map.Entry;
10 import java.util.Optional;
12 import java.util.stream.Collectors;
14 import org.apache.commons.collections.CollectionUtils;
15 import org.apache.commons.lang3.StringUtils;
16 import org.apache.commons.lang3.tuple.ImmutablePair;
17 import org.apache.commons.lang3.tuple.Pair;
18 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
19 import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
20 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
21 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
22 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
23 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
24 import org.openecomp.sdc.be.datatypes.components.ComponentMetadataDataDefinition;
25 import org.openecomp.sdc.be.datatypes.components.ResourceMetadataDataDefinition;
26 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
27 import org.openecomp.sdc.be.datatypes.elements.AttributeDataDefinition;
28 import org.openecomp.sdc.be.datatypes.elements.CapabilityDataDefinition;
29 import org.openecomp.sdc.be.datatypes.elements.ComponentInstanceDataDefinition;
30 import org.openecomp.sdc.be.datatypes.elements.GroupDataDefinition;
31 import org.openecomp.sdc.be.datatypes.elements.ListCapabilityDataDefinition;
32 import org.openecomp.sdc.be.datatypes.elements.ListRequirementDataDefinition;
33 import org.openecomp.sdc.be.datatypes.elements.MapArtifactDataDefinition;
34 import org.openecomp.sdc.be.datatypes.elements.MapAttributesDataDefinition;
35 import org.openecomp.sdc.be.datatypes.elements.MapCapabiltyProperty;
36 import org.openecomp.sdc.be.datatypes.elements.MapListCapabiltyDataDefinition;
37 import org.openecomp.sdc.be.datatypes.elements.MapListRequirementDataDefinition;
38 import org.openecomp.sdc.be.datatypes.elements.MapPropertiesDataDefinition;
39 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
40 import org.openecomp.sdc.be.datatypes.elements.RequirementDataDefinition;
41 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
42 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
43 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
44 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
45 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
46 import org.openecomp.sdc.be.model.ArtifactDefinition;
47 import org.openecomp.sdc.be.model.AttributeDefinition;
48 import org.openecomp.sdc.be.model.CapabilityDefinition;
49 import org.openecomp.sdc.be.model.Component;
50 import org.openecomp.sdc.be.model.ComponentInstance;
51 import org.openecomp.sdc.be.model.ComponentInstanceInput;
52 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
53 import org.openecomp.sdc.be.model.ComponentParametersView;
54 import org.openecomp.sdc.be.model.DistributionStatusEnum;
55 import org.openecomp.sdc.be.model.GroupDefinition;
56 import org.openecomp.sdc.be.model.GroupInstance;
57 import org.openecomp.sdc.be.model.InputDefinition;
58 import org.openecomp.sdc.be.model.LifecycleStateEnum;
59 import org.openecomp.sdc.be.model.PropertyDefinition;
60 import org.openecomp.sdc.be.model.RequirementCapabilityRelDef;
61 import org.openecomp.sdc.be.model.RequirementDefinition;
62 import org.openecomp.sdc.be.model.Resource;
63 import org.openecomp.sdc.be.model.Service;
64 import org.openecomp.sdc.be.model.User;
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.utils.ModelConverter;
69 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
70 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
71 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
72 import org.openecomp.sdc.be.resources.data.ComponentMetadataData;
73 import org.openecomp.sdc.be.utils.CommonBeUtils;
74 import org.openecomp.sdc.common.jsongraph.util.CommonUtility;
75 import org.openecomp.sdc.common.jsongraph.util.CommonUtility.LogLevelEnum;
76 import org.openecomp.sdc.common.util.ValidationUtils;
77 import org.slf4j.Logger;
78 import org.slf4j.LoggerFactory;
79 import org.springframework.beans.factory.annotation.Autowired;
81 import fj.data.Either;
83 @org.springframework.stereotype.Component("tosca-operation-facade")
84 public class ToscaOperationFacade {
86 private NodeTypeOperation nodeTypeOperation;
88 private TopologyTemplateOperation topologyTemplateOperation;
90 private NodeTemplateOperation nodeTemplateOperation;
92 private GroupsOperation groupsOperation;
94 private TitanDao titanDao;
96 private static Logger log = LoggerFactory.getLogger(ToscaOperationFacade.class.getName());
98 public <T extends Component> Either<T, StorageOperationStatus> getToscaElement(String componentId) {
100 return getToscaElement(componentId, JsonParseFlagEnum.ParseAll);
103 public <T extends Component> Either<T, StorageOperationStatus> getToscaFullElement(String componentId) {
104 ComponentParametersView filters = new ComponentParametersView();
105 filters.setIgnoreCapabiltyProperties(false);
107 return getToscaElement(componentId, filters);
109 public <T extends Component> Either<T, StorageOperationStatus> getToscaElement(String componentId, ComponentParametersView filters) {
111 Either<GraphVertex, TitanOperationStatus> getVertexEither = titanDao.getVertexById(componentId, filters.detectParseFlag());
112 if (getVertexEither.isRight()) {
113 log.debug("Couldn't fetch component with and unique id {}, error: {}", componentId, getVertexEither.right().value());
114 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value()));
117 return getToscaElementByOperation(getVertexEither.left().value(), filters);
120 public <T extends Component> Either<T, StorageOperationStatus> getToscaElement(String componentId, JsonParseFlagEnum parseFlag) {
122 Either<GraphVertex, TitanOperationStatus> getVertexEither = titanDao.getVertexById(componentId, parseFlag);
123 if (getVertexEither.isRight()) {
124 log.debug("Couldn't fetch component with and unique id {}, error: {}", componentId, getVertexEither.right().value());
125 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value()));
128 return getToscaElementByOperation(getVertexEither.left().value());
131 public <T extends Component> Either<T, StorageOperationStatus> getToscaElement(GraphVertex componentVertex) {
132 return getToscaElementByOperation(componentVertex);
135 public Either<Boolean, StorageOperationStatus> validateComponentExists(String componentId) {
137 Either<GraphVertex, TitanOperationStatus> getVertexEither = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
138 if (getVertexEither.isRight()) {
139 TitanOperationStatus status = getVertexEither.right().value();
140 if (status == TitanOperationStatus.NOT_FOUND) {
141 return Either.left(false);
143 log.debug("Couldn't fetch component with and unique id {}, error: {}", componentId, getVertexEither.right().value());
144 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value()));
147 return Either.left(true);
150 public <T extends Component> Either<T, StorageOperationStatus> findLastCertifiedToscaElementByUUID(T component) {
151 Map<GraphPropertyEnum, Object> props = new HashMap<>();
152 props.put(GraphPropertyEnum.UUID, component.getUUID());
153 props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
154 props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
156 Either<List<GraphVertex>, TitanOperationStatus> getVertexEither = titanDao.getByCriteria(ModelConverter.getVertexType(component), props);
157 if (getVertexEither.isRight()) {
158 log.debug("Couldn't fetch component with and unique id {}, error: {}", component.getUniqueId(), getVertexEither.right().value());
159 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value()));
162 return getToscaElementByOperation(getVertexEither.left().value().get(0));
165 private <T extends Component> Either<T, StorageOperationStatus> getToscaElementByOperation(GraphVertex componentV) {
166 return getToscaElementByOperation(componentV, new ComponentParametersView());
169 private <T extends Component> Either<T, StorageOperationStatus> getToscaElementByOperation(GraphVertex componentV, ComponentParametersView filters) {
170 VertexTypeEnum label = componentV.getLabel();
172 ToscaElementOperation toscaOperation = getToscaElementOperation(componentV);
173 Either<ToscaElement, StorageOperationStatus> toscaElement;
174 String componentId = componentV.getUniqueId();
175 if (toscaOperation != null) {
176 log.debug("Need to fetch tosca element for id {}", componentId);
177 toscaElement = toscaOperation.getToscaElement(componentV, filters);
179 log.debug("not supported tosca type {} for id {}", label, componentId);
180 toscaElement = Either.right(StorageOperationStatus.BAD_REQUEST);
182 if (toscaElement.isRight()) {
183 return Either.right(toscaElement.right().value());
185 return Either.left(ModelConverter.convertFromToscaElement(toscaElement.left().value()));
188 private ToscaElementOperation getToscaElementOperation(GraphVertex componentV) {
189 VertexTypeEnum label = componentV.getLabel();
192 return nodeTypeOperation;
193 case TOPOLOGY_TEMPLATE:
194 return topologyTemplateOperation;
205 public <T extends Component> Either<T, StorageOperationStatus> createToscaComponent(T resource) {
206 ToscaElement toscaElement = ModelConverter.convertToToscaElement(resource);
208 ToscaElementOperation toscaElementOperation = getToscaElementOperation(toscaElement);
209 Either<ToscaElement, StorageOperationStatus> createToscaElement = toscaElementOperation.createToscaElement(toscaElement);
210 if (createToscaElement.isLeft()) {
211 log.debug("Component created successfully!!!");
212 T dataModel = ModelConverter.convertFromToscaElement(createToscaElement.left().value());
213 return Either.left(dataModel);
215 return Either.right(createToscaElement.right().value());
220 * @param componentToDelete
223 public StorageOperationStatus markComponentToDelete(Component componentToDelete) {
225 if ((componentToDelete.getIsDeleted() != null) && componentToDelete.getIsDeleted() && !componentToDelete.isHighestVersion()) {
226 // component already marked for delete
227 return StorageOperationStatus.OK;
230 Either<GraphVertex, TitanOperationStatus> getResponse = titanDao.getVertexById(componentToDelete.getUniqueId(), JsonParseFlagEnum.ParseAll);
231 if (getResponse.isRight()) {
232 log.debug("Couldn't fetch component with and unique id {}, error: {}", componentToDelete.getUniqueId(), getResponse.right().value());
233 return DaoStatusConverter.convertTitanStatusToStorageStatus(getResponse.right().value());
236 GraphVertex componentV = getResponse.left().value();
238 // same operation for node type and topology template operations
239 Either<GraphVertex, StorageOperationStatus> result = nodeTypeOperation.markComponentToDelete(componentV);
240 if (result.isRight()) {
241 return result.right().value();
243 return StorageOperationStatus.OK;
252 public <T extends Component> Either<T, StorageOperationStatus> deleteToscaComponent(String componentId) {
254 Either<GraphVertex, TitanOperationStatus> getVertexEither = titanDao.getVertexById(componentId, JsonParseFlagEnum.ParseAll);
255 if (getVertexEither.isRight()) {
256 log.debug("Couldn't fetch component vertex with and unique id {}, error: {}", componentId, getVertexEither.right().value());
257 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value()));
260 Either<ToscaElement, StorageOperationStatus> deleteElement = deleteToscaElement(getVertexEither.left().value());
261 if (deleteElement.isRight()) {
262 log.debug("Failed to delete component with and unique id {}, error: {}", componentId, deleteElement.right().value());
263 return Either.right(deleteElement.right().value());
265 T dataModel = ModelConverter.convertFromToscaElement(deleteElement.left().value());
267 return Either.left(dataModel);
270 private Either<ToscaElement, StorageOperationStatus> deleteToscaElement(GraphVertex componentV) {
271 VertexTypeEnum label = componentV.getLabel();
272 Either<ToscaElement, StorageOperationStatus> toscaElement;
273 Object componentId = componentV.getUniqueId();
276 log.debug("Need to fetch node type for id {}", componentId);
277 toscaElement = nodeTypeOperation.deleteToscaElement(componentV);
279 case TOPOLOGY_TEMPLATE:
280 log.debug("Need to fetch topology template for id {}", componentId);
281 toscaElement = topologyTemplateOperation.deleteToscaElement(componentV);
284 log.debug("not supported tosca type {} for id {}", label, componentId);
285 toscaElement = Either.right(StorageOperationStatus.BAD_REQUEST);
291 private ToscaElementOperation getToscaElementOperation(ToscaElement toscaElement) {
293 ComponentTypeEnum componentType = toscaElement.getComponentType();
294 switch (componentType) {
296 ResourceTypeEnum resourceType = toscaElement.getResourceType();
297 if (resourceType == ResourceTypeEnum.VF) {
298 return topologyTemplateOperation;
300 return nodeTypeOperation;
304 return topologyTemplateOperation;
311 private ToscaElementOperation getToscaElementOperation(Component component) {
313 switch (component.getComponentType()) {
315 ResourceTypeEnum resourceType = ((Resource) component).getResourceType();
316 if (resourceType != null && resourceType == ResourceTypeEnum.VF) {
317 return topologyTemplateOperation;
319 return nodeTypeOperation;
323 return topologyTemplateOperation;
330 public <T extends Component> Either<T, StorageOperationStatus> getLatestByToscaResourceName(String toscaResourceName) {
331 return getLatestByName(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName);
335 public <T extends Component> Either<T, StorageOperationStatus> getLatestByName(String resourceName) {
336 return getLatestByName(GraphPropertyEnum.NAME, resourceName);
340 public Either<Integer, StorageOperationStatus> validateCsarUuidUniqueness(String csarUUID) {
341 Either<List<ToscaElement>, StorageOperationStatus> byCsar = null;
343 Map<GraphPropertyEnum, Object> properties = new HashMap<GraphPropertyEnum, Object>();
344 properties.put(GraphPropertyEnum.CSAR_UUID, csarUUID);
346 Either<List<GraphVertex>, TitanOperationStatus> resources = titanDao.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata);
348 if (resources.isRight()) {
349 if (resources.right().value() == TitanOperationStatus.NOT_FOUND) {
350 return Either.left(new Integer(0));
352 log.debug("failed to get resources from graph with property name: {}", csarUUID);
353 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(resources.right().value()));
357 List<GraphVertex> resourceList = (resources.isLeft() ? resources.left().value() : null);
359 return Either.left(new Integer(resourceList.size()));
363 public <T extends Component> Either<List<T>, StorageOperationStatus> getFollowed(String userId, Set<LifecycleStateEnum> lifecycleStates, Set<LifecycleStateEnum> lastStateStates, ComponentTypeEnum componentType) {
364 Either<List<ToscaElement>, StorageOperationStatus> followedResources;
365 if (componentType == ComponentTypeEnum.RESOURCE) {
366 followedResources = nodeTypeOperation.getFollowedComponent(userId, lifecycleStates, lastStateStates, componentType);
368 followedResources = topologyTemplateOperation.getFollowedComponent(userId, lifecycleStates, lastStateStates, componentType);
371 List<T> components = new ArrayList<>();
372 if (followedResources.isRight() && followedResources.right().value() != StorageOperationStatus.NOT_FOUND) {
373 return Either.right(followedResources.right().value());
375 if (followedResources.isLeft()) {
376 List<ToscaElement> toscaElements = followedResources.left().value();
377 toscaElements.forEach(te -> {
378 T component = ModelConverter.convertFromToscaElement(te);
379 components.add(component);
382 return Either.left(components);
385 public Either<Resource, StorageOperationStatus> getLatestCertifiedNodeTypeByToscaResourceName(String toscaResourceName) {
387 return getLatestCertifiedByToscaResourceName(toscaResourceName, VertexTypeEnum.NODE_TYPE, JsonParseFlagEnum.ParseMetadata);
390 public Either<Resource, StorageOperationStatus> getLatestCertifiedByToscaResourceName(String toscaResourceName, VertexTypeEnum vertexType, JsonParseFlagEnum parseFlag) {
392 Either<Resource, StorageOperationStatus> result = null;
393 Map<GraphPropertyEnum, Object> props = new HashMap<GraphPropertyEnum, Object>();
394 props.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName);
395 props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
396 props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
397 Either<List<GraphVertex>, TitanOperationStatus> getLatestRes = titanDao.getByCriteria(vertexType, props, parseFlag);
399 if (getLatestRes.isRight()) {
400 TitanOperationStatus status = getLatestRes.right().value();
401 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch {} with name {}. status={} ", vertexType, toscaResourceName, status);
402 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
404 if (result == null) {
405 List<GraphVertex> resources = getLatestRes.left().value();
406 double version = 0.0;
407 GraphVertex highestResource = null;
408 for (GraphVertex resource : resources) {
409 double resourceVersion = Double.parseDouble((String) resource.getJsonMetadataField(JsonPresentationFields.VERSION));
410 if (resourceVersion > version) {
411 version = resourceVersion;
412 highestResource = resource;
415 result = getToscaElement(highestResource.getUniqueId());
420 public Either<Boolean, StorageOperationStatus> validateToscaResourceNameExists(String templateName) {
421 Either<Boolean, StorageOperationStatus> validateUniquenessRes = validateToscaResourceNameUniqueness(templateName);
422 if (validateUniquenessRes.isLeft()) {
423 return Either.left(!validateUniquenessRes.left().value());
425 return validateUniquenessRes;
428 public Either<RequirementCapabilityRelDef, StorageOperationStatus> dissociateResourceInstances(String componentId, RequirementCapabilityRelDef requirementDef) {
429 return nodeTemplateOperation.dissociateResourceInstances(componentId, requirementDef);
433 public StorageOperationStatus associateResourceInstances(String componentId, List<RequirementCapabilityRelDef> relations) {
434 Either<List<RequirementCapabilityRelDef>, StorageOperationStatus> status = nodeTemplateOperation.associateResourceInstances(componentId, relations);
435 if (status.isRight()) {
436 return status.right().value();
438 return StorageOperationStatus.OK;
441 protected Either<Boolean, StorageOperationStatus> validateToscaResourceNameUniqueness(String name) {
443 Map<GraphPropertyEnum, Object> properties = new HashMap<GraphPropertyEnum, Object>();
444 properties.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, name);
446 Either<List<GraphVertex>, TitanOperationStatus> resources = titanDao.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata);
448 if (resources.isRight() && resources.right().value() != TitanOperationStatus.NOT_FOUND) {
449 log.debug("failed to get resources from graph with property name: {}", name);
450 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(resources.right().value()));
452 List<GraphVertex> resourceList = (resources.isLeft() ? resources.left().value() : null);
453 if (resourceList != null && resourceList.size() > 0) {
454 if (log.isDebugEnabled()) {
455 StringBuilder builder = new StringBuilder();
456 for (GraphVertex resourceData : resourceList) {
457 builder.append(resourceData.getUniqueId() + "|");
459 log.debug("resources with property name:{} exists in graph. found {}", name, builder.toString());
461 return Either.left(false);
463 log.debug("resources with property name:{} does not exists in graph", name);
464 return Either.left(true);
471 * @param newComponent
472 * @param oldComponent
475 public <T extends Component> Either<T, StorageOperationStatus> overrideComponent(T newComponent, T oldComponent) {
478 // newComponent.setInterfaces(oldComponent.getInterfaces);
479 newComponent.setArtifacts(oldComponent.getArtifacts());
480 newComponent.setDeploymentArtifacts(oldComponent.getDeploymentArtifacts());
481 newComponent.setGroups(oldComponent.getGroups());
482 newComponent.setInputs(null);
483 newComponent.setLastUpdateDate(null);
484 newComponent.setHighestVersion(true);
486 Either<GraphVertex, TitanOperationStatus> componentVEither = titanDao.getVertexById(oldComponent.getUniqueId(), JsonParseFlagEnum.NoParse);
487 if (componentVEither.isRight()) {
488 log.debug("Falied to fetch component {} error {}", oldComponent.getUniqueId(), componentVEither.right().value());
489 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(componentVEither.right().value()));
491 GraphVertex componentv = componentVEither.left().value();
492 Either<GraphVertex, TitanOperationStatus> parentVertexEither = titanDao.getParentVertex(componentv, EdgeLabelEnum.VERSION, JsonParseFlagEnum.NoParse);
493 if (parentVertexEither.isRight() && parentVertexEither.right().value() != TitanOperationStatus.NOT_FOUND) {
494 log.debug("Falied to fetch parent version for component {} error {}", oldComponent.getUniqueId(), parentVertexEither.right().value());
495 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(parentVertexEither.right().value()));
498 Either<ToscaElement, StorageOperationStatus> deleteToscaComponent = deleteToscaElement(componentv);
499 if (deleteToscaComponent.isRight()) {
500 log.debug("Falied to remove old component {} error {}", oldComponent.getUniqueId(), deleteToscaComponent.right().value());
501 return Either.right(deleteToscaComponent.right().value());
503 Either<T, StorageOperationStatus> createToscaComponent = createToscaComponent(newComponent);
504 if (createToscaComponent.isRight()) {
505 log.debug("Falied to create tosca element component {} error {}", newComponent.getUniqueId(), createToscaComponent.right().value());
506 return Either.right(createToscaComponent.right().value());
508 T newElement = createToscaComponent.left().value();
509 Either<GraphVertex, TitanOperationStatus> newVersionEither = titanDao.getVertexById(newElement.getUniqueId(), JsonParseFlagEnum.NoParse);
510 if (newVersionEither.isRight()) {
511 log.debug("Falied to fetch new tosca element component {} error {}", newComponent.getUniqueId(), newVersionEither.right().value());
512 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(newVersionEither.right().value()));
514 if (parentVertexEither.isLeft()) {
515 GraphVertex previousVersionV = parentVertexEither.left().value();
516 TitanOperationStatus createEdge = titanDao.createEdge(previousVersionV, newVersionEither.left().value(), EdgeLabelEnum.VERSION, null);
517 if (createEdge != TitanOperationStatus.OK) {
518 log.debug("Falied to associate to previous version {} new version {} error {}", previousVersionV.getUniqueId(), newVersionEither.right().value(), createEdge);
519 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(createEdge));
522 return Either.left(newElement);
527 * @param componentToUpdate
530 public <T extends Component> Either<T, StorageOperationStatus> updateToscaElement(T componentToUpdate) {
531 return updateToscaElement(componentToUpdate, new ComponentParametersView());
536 * @param componentToUpdate
538 * @param filterResult
541 public <T extends Component> Either<T, StorageOperationStatus> updateToscaElement(T componentToUpdate, ComponentParametersView filterResult) {
542 String componentId = componentToUpdate.getUniqueId();
543 Either<GraphVertex, TitanOperationStatus> getVertexEither = titanDao.getVertexById(componentId, JsonParseFlagEnum.ParseAll);
544 if (getVertexEither.isRight()) {
545 log.debug("Couldn't fetch component with and unique id {}, error: {}", componentId, getVertexEither.right().value());
546 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value()));
548 GraphVertex elementV = getVertexEither.left().value();
549 ToscaElementOperation toscaElementOperation = getToscaElementOperation(elementV);
551 ToscaElement toscaElementToUpdate = ModelConverter.convertToToscaElement(componentToUpdate);
552 Either<ToscaElement, StorageOperationStatus> updateToscaElement = toscaElementOperation.updateToscaElement(toscaElementToUpdate, elementV, filterResult);
553 if (updateToscaElement.isRight()) {
554 log.debug("Failed to update tosca element {} error {}", componentId, updateToscaElement.right().value());
555 return Either.right(updateToscaElement.right().value());
557 return Either.left(ModelConverter.convertFromToscaElement(updateToscaElement.left().value()));
560 private <T extends Component> Either<T, StorageOperationStatus> getLatestByName(GraphPropertyEnum property, String nodeName) {
561 Either<T, StorageOperationStatus> result;
563 Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
564 Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
566 propertiesToMatch.put(property, nodeName);
567 propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
569 propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
571 Either<List<GraphVertex>, TitanOperationStatus> highestResources = titanDao.getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseMetadata);
572 if (highestResources.isRight()) {
573 TitanOperationStatus status = highestResources.right().value();
574 log.debug("failed to find resource with name {}. status={} ", nodeName, status);
575 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
579 List<GraphVertex> resources = highestResources.left().value();
580 double version = 0.0;
581 GraphVertex highestResource = null;
582 for (GraphVertex vertex : resources) {
583 Object versionObj = vertex.getMetadataProperty(GraphPropertyEnum.VERSION);
584 double resourceVersion = Double.valueOf((String) versionObj);
585 if (resourceVersion > version) {
586 version = resourceVersion;
587 highestResource = vertex;
590 return getToscaElementByOperation(highestResource);
593 public <T extends Component> Either<List<T>, StorageOperationStatus> getBySystemName(ComponentTypeEnum componentType, String systemName) {
595 Either<List<T>, StorageOperationStatus> result = null;
596 Either<T, StorageOperationStatus> getComponentRes;
597 List<T> components = new ArrayList<>();
598 List<GraphVertex> componentVertices;
599 Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
600 Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
602 propertiesToMatch.put(GraphPropertyEnum.SYSTEM_NAME, systemName);
603 if (componentType != null)
604 propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
606 propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
608 Either<List<GraphVertex>, TitanOperationStatus> getComponentsRes = titanDao.getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll);
609 if (getComponentsRes.isRight()) {
610 TitanOperationStatus status = getComponentsRes.right().value();
611 log.debug("Failed to fetch the component with system name {}. Status is {} ", systemName, status);
612 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
614 if (result == null) {
615 componentVertices = getComponentsRes.left().value();
616 for (GraphVertex componentVertex : componentVertices) {
617 getComponentRes = getToscaElementByOperation(componentVertex);
618 if (getComponentRes.isRight()) {
619 log.debug("Failed to get the component {}. Status is {} ", componentVertex.getJsonMetadataField(JsonPresentationFields.NAME), getComponentRes.right().value());
620 result = Either.right(getComponentRes.right().value());
623 T componentBySystemName = getComponentRes.left().value();
624 log.debug("Found component, id: {}", componentBySystemName.getUniqueId());
625 components.add(componentBySystemName);
628 if (result == null) {
629 result = Either.left(components);
634 public <T extends Component> Either<T, StorageOperationStatus> getComponentByNameAndVersion(ComponentTypeEnum componentType, String name, String version) {
635 return getComponentByNameAndVersion(componentType, name, version, JsonParseFlagEnum.ParseAll);
638 public <T extends Component> Either<T, StorageOperationStatus> getComponentByNameAndVersion(ComponentTypeEnum componentType, String name, String version, JsonParseFlagEnum parseFlag) {
639 Either<T, StorageOperationStatus> result;
641 Map<GraphPropertyEnum, Object> hasProperties = new EnumMap<>(GraphPropertyEnum.class);
642 Map<GraphPropertyEnum, Object> hasNotProperties = new EnumMap<>(GraphPropertyEnum.class);
644 hasProperties.put(GraphPropertyEnum.NAME, name);
645 hasProperties.put(GraphPropertyEnum.VERSION, version);
646 hasNotProperties.put(GraphPropertyEnum.IS_DELETED, true);
647 if (componentType != null) {
648 hasProperties.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
650 Either<List<GraphVertex>, TitanOperationStatus> getResourceRes = titanDao.getByCriteria(null, hasProperties, hasNotProperties, parseFlag);
651 if (getResourceRes.isRight()) {
652 TitanOperationStatus status = getResourceRes.right().value();
653 log.debug("failed to find resource with name {}, version {}. Status is {} ", name, version, status);
654 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
657 return getToscaElementByOperation(getResourceRes.left().value().get(0));
660 public <T extends Component> Either<List<T>, StorageOperationStatus> getCatalogComponents(ComponentTypeEnum componentType) {
661 List<T> components = new ArrayList<>();
662 Either<List<ToscaElement>, StorageOperationStatus> catalogDataResult;
663 List<ToscaElement> toscaElements;
664 switch (componentType) {
666 catalogDataResult = nodeTypeOperation.getElementCatalogData(ComponentTypeEnum.RESOURCE ,ToscaElementTypeEnum.NodeType);
667 if (catalogDataResult.isRight()) {
668 return Either.right(catalogDataResult.right().value());
670 toscaElements = catalogDataResult.left().value();
671 Either<List<ToscaElement>, StorageOperationStatus> resourceCatalogData = topologyTemplateOperation.getElementCatalogData(ComponentTypeEnum.RESOURCE ,ToscaElementTypeEnum.TopologyTemplate);
672 if (resourceCatalogData.isRight()) {
673 return Either.right(resourceCatalogData.right().value());
675 toscaElements.addAll(resourceCatalogData.left().value());
678 catalogDataResult = topologyTemplateOperation.getElementCatalogData(ComponentTypeEnum.SERVICE , ToscaElementTypeEnum.TopologyTemplate);
679 if (catalogDataResult.isRight()) {
680 return Either.right(catalogDataResult.right().value());
682 toscaElements = catalogDataResult.left().value();
685 log.debug("Not supported component type {}", componentType);
686 return Either.right(StorageOperationStatus.BAD_REQUEST);
688 toscaElements.forEach(te -> {
689 T component = ModelConverter.convertFromToscaElement(te);
690 components.add(component);
692 return Either.left(components);
695 public Either<List<String>, StorageOperationStatus> deleteMarkedElements(ComponentTypeEnum componentType) {
696 Either<List<GraphVertex>, StorageOperationStatus> allComponentsMarkedForDeletion;
697 List<String> deleted = new ArrayList<>();
698 switch (componentType) {
700 allComponentsMarkedForDeletion = nodeTypeOperation.getAllComponentsMarkedForDeletion(componentType);
704 allComponentsMarkedForDeletion = topologyTemplateOperation.getAllComponentsMarkedForDeletion(componentType);
707 log.debug("Not supported component type {}", componentType);
708 return Either.right(StorageOperationStatus.BAD_REQUEST);
710 if (allComponentsMarkedForDeletion.isRight()) {
711 return Either.right(allComponentsMarkedForDeletion.right().value());
713 List<GraphVertex> allMarked = allComponentsMarkedForDeletion.left().value();
715 Either<List<GraphVertex>, TitanOperationStatus> allNotDeletedElements = topologyTemplateOperation.getAllNotDeletedElements();
716 if (allNotDeletedElements.isRight()) {
717 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(allNotDeletedElements.right().value()));
719 List<GraphVertex> allNonMarked = allNotDeletedElements.left().value();
720 for (GraphVertex elementV : allMarked) {
721 if (topologyTemplateOperation.isInUse(elementV, allNonMarked) == false) {
722 Either<ToscaElement, StorageOperationStatus> deleteToscaElement = deleteToscaElement(elementV);
723 if (deleteToscaElement.isRight()) {
724 log.debug("Failed to delete marked element {} error {}", elementV.getUniqueId(), deleteToscaElement.right().value());
727 deleted.add(elementV.getUniqueId());
728 log.debug("Marked element {} in use. don't delete it", elementV.getUniqueId());
731 return Either.left(deleted);
734 public Either<List<String>, StorageOperationStatus> getAllComponentsMarkedForDeletion(ComponentTypeEnum componentType) {
735 Either<List<GraphVertex>, StorageOperationStatus> allComponentsMarkedForDeletion;
736 switch (componentType) {
738 allComponentsMarkedForDeletion = nodeTypeOperation.getAllComponentsMarkedForDeletion(componentType);
742 allComponentsMarkedForDeletion = topologyTemplateOperation.getAllComponentsMarkedForDeletion(componentType);
745 log.debug("Not supported component type {}", componentType);
746 return Either.right(StorageOperationStatus.BAD_REQUEST);
748 if (allComponentsMarkedForDeletion.isRight()) {
749 return Either.right(allComponentsMarkedForDeletion.right().value());
751 return Either.left(allComponentsMarkedForDeletion.left().value().stream().map(v -> v.getUniqueId()).collect(Collectors.toList()));
754 public Either<Boolean, StorageOperationStatus> isComponentInUse(String componentId) {
755 Either<Boolean, StorageOperationStatus> result;
756 Either<List<GraphVertex>, TitanOperationStatus> allNotDeletedElements = topologyTemplateOperation.getAllNotDeletedElements();
757 if (allNotDeletedElements.isRight()) {
758 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(allNotDeletedElements.right().value()));
760 result = Either.left(topologyTemplateOperation.isInUse(componentId, allNotDeletedElements.left().value()));
765 public Either<ImmutablePair<Component, String>, StorageOperationStatus> addComponentInstanceToTopologyTemplate(Component containerComponent, Component origComponent, ComponentInstance componentInstance, boolean allowDeleted, User user) {
767 Either<ImmutablePair<Component, String>, StorageOperationStatus> result = null;
768 Either<ToscaElement, StorageOperationStatus> updateContainerComponentRes = null;
769 componentInstance.setIcon(origComponent.getIcon());
770 Either<ImmutablePair<TopologyTemplate, String>, StorageOperationStatus> addResult = nodeTemplateOperation.addComponentInstanceToTopologyTemplate(ModelConverter.convertToToscaElement(containerComponent),
771 ModelConverter.convertToToscaElement(origComponent), getNextComponentInstanceCounter(containerComponent, origComponent.getName()), componentInstance, allowDeleted, user);
773 if (addResult.isRight()) {
774 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to add the component instance {} to container component {}. ", componentInstance.getName(), containerComponent.getName());
775 result = Either.right(addResult.right().value());
777 if (result == null) {
778 updateContainerComponentRes = topologyTemplateOperation.getToscaElement(containerComponent.getUniqueId());
779 if (updateContainerComponentRes.isRight()) {
780 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch updated topology template {} with updated component instance {}. ", containerComponent.getName(), componentInstance.getName());
781 result = Either.right(updateContainerComponentRes.right().value());
784 if (result == null) {
785 Component updatedComponent = ModelConverter.convertFromToscaElement(updateContainerComponentRes.left().value());
786 String createdInstanceId = addResult.left().value().getRight();
787 CommonUtility.addRecordToLog(log, LogLevelEnum.TRACE, "The component instance {} has been added to container component {}. ", createdInstanceId, updatedComponent.getName());
788 result = Either.left(new ImmutablePair<>(updatedComponent, createdInstanceId));
793 public StorageOperationStatus associateComponentInstancesToComponent(Component containerComponent, Map<ComponentInstance, Resource> resourcesInstancesMap, boolean allowDeleted) {
795 StorageOperationStatus result = null;
796 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Going to add component instances to component {}", containerComponent.getUniqueId());
798 Either<GraphVertex, TitanOperationStatus> metadataVertex = titanDao.getVertexById(containerComponent.getUniqueId(), JsonParseFlagEnum.ParseAll);
799 if (metadataVertex.isRight()) {
800 TitanOperationStatus status = metadataVertex.right().value();
801 if (status == TitanOperationStatus.NOT_FOUND) {
802 status = TitanOperationStatus.INVALID_ID;
804 result = DaoStatusConverter.convertTitanStatusToStorageStatus(status);
806 if (result == null) {
807 result = nodeTemplateOperation.associateComponentInstancesToComponent(containerComponent, resourcesInstancesMap, metadataVertex.left().value(), allowDeleted);
812 public Either<ImmutablePair<Component, String>, StorageOperationStatus> updateComponentInstanceMetadataOfTopologyTemplate(Component containerComponent, Component origComponent, ComponentInstance componentInstance) {
814 Either<ImmutablePair<Component, String>, StorageOperationStatus> result = null;
816 CommonUtility.addRecordToLog(log, LogLevelEnum.TRACE, "Going to update the metadata of the component instance {} belonging to container component {}. ", componentInstance.getName(), containerComponent.getName());
817 componentInstance.setIcon(origComponent.getIcon());
818 Either<ImmutablePair<TopologyTemplate, String>, StorageOperationStatus> updateResult = nodeTemplateOperation.updateComponentInstanceMetadataOfTopologyTemplate(ModelConverter.convertToToscaElement(containerComponent),
819 ModelConverter.convertToToscaElement(origComponent), componentInstance);
820 if (updateResult.isRight()) {
821 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to update the metadata of the component instance {} belonging to container component {}. ", componentInstance.getName(), containerComponent.getName());
822 result = Either.right(updateResult.right().value());
824 if (result == null) {
825 Component updatedComponent = ModelConverter.convertFromToscaElement(updateResult.left().value().getLeft());
826 String createdInstanceId = updateResult.left().value().getRight();
827 CommonUtility.addRecordToLog(log, LogLevelEnum.TRACE, "The metadata of the component instance {} has been updated to container component {}. ", createdInstanceId, updatedComponent.getName());
828 result = Either.left(new ImmutablePair<>(updatedComponent, createdInstanceId));
833 public Either<Component, StorageOperationStatus> updateComponentInstanceMetadataOfTopologyTemplate(Component containerComponent) {
835 Either<Component, StorageOperationStatus> result = null;
837 CommonUtility.addRecordToLog(log, LogLevelEnum.TRACE, "Going to update the metadata belonging to container component {}. ", containerComponent.getName());
839 Either<TopologyTemplate, StorageOperationStatus> updateResult = nodeTemplateOperation.updateComponentInstanceMetadataOfTopologyTemplate(ModelConverter.convertToToscaElement(containerComponent));
840 if (updateResult.isRight()) {
841 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to update the metadata belonging to container component {}. ", containerComponent.getName());
842 result = Either.right(updateResult.right().value());
844 if (result == null) {
845 Component updatedComponent = ModelConverter.convertFromToscaElement(updateResult.left().value());
846 CommonUtility.addRecordToLog(log, LogLevelEnum.TRACE, "The metadata has been updated to container component {}. ", updatedComponent.getName());
847 result = Either.left(updatedComponent);
852 public Either<ImmutablePair<Component, String>, StorageOperationStatus> deleteComponentInstanceFromTopologyTemplate(Component containerComponent, String resourceInstanceId) {
854 Either<ImmutablePair<Component, String>, StorageOperationStatus> result = null;
856 CommonUtility.addRecordToLog(log, LogLevelEnum.TRACE, "Going to delete the component instance {} belonging to container component {}. ", resourceInstanceId, containerComponent.getName());
858 Either<ImmutablePair<TopologyTemplate, String>, StorageOperationStatus> updateResult = nodeTemplateOperation.deleteComponentInstanceFromTopologyTemplate(ModelConverter.convertToToscaElement(containerComponent), resourceInstanceId);
859 if (updateResult.isRight()) {
860 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to delete the component instance {} belonging to container component {}. ", resourceInstanceId, containerComponent.getName());
861 result = Either.right(updateResult.right().value());
863 if (result == null) {
864 Component updatedComponent = ModelConverter.convertFromToscaElement(updateResult.left().value().getLeft());
865 String deletedInstanceId = updateResult.left().value().getRight();
866 CommonUtility.addRecordToLog(log, LogLevelEnum.TRACE, "The component instance {} has been deleted from container component {}. ", deletedInstanceId, updatedComponent.getName());
867 result = Either.left(new ImmutablePair<>(updatedComponent, deletedInstanceId));
872 private String getNextComponentInstanceCounter(Component containerComponent, String originResourceName) {
874 Integer nextCounter = 0;
876 if (CollectionUtils.isNotEmpty(containerComponent.getComponentInstances())) {
878 String normalizedName = ValidationUtils.normalizeComponentInstanceName(originResourceName);
879 Integer maxCounterFromNames = getMaxCounterFromNames(containerComponent, normalizedName);
880 Integer maxCounterFromIds = getMaxCounterFromIds(containerComponent, normalizedName);
882 if (maxCounterFromNames == null && maxCounterFromIds != null) {
883 nextCounter = maxCounterFromIds + 1;
884 } else if (maxCounterFromIds == null && maxCounterFromNames != null) {
885 nextCounter = maxCounterFromNames + 1;
886 } else if (maxCounterFromIds != null && maxCounterFromNames != null) {
887 nextCounter = maxCounterFromNames > maxCounterFromIds ? maxCounterFromNames + 1 : maxCounterFromIds + 1;
890 return nextCounter.toString();
893 private Integer getMaxCounterFromNames(Component containerComponent, String normalizedName) {
895 Integer maxCounter = 0;
896 List<String> countersStr = containerComponent.getComponentInstances().stream().filter(ci -> ci.getNormalizedName() != null && ci.getNormalizedName().startsWith(normalizedName)).map(ci -> ci.getNormalizedName().split(normalizedName)[1])
897 .collect(Collectors.toList());
899 if (CollectionUtils.isEmpty(countersStr)) {
902 Integer currCounter = null;
903 for (String counter : countersStr) {
904 if (StringUtils.isEmpty(counter)) {
908 currCounter = Integer.parseInt(counter);
909 } catch (Exception e) {
912 maxCounter = maxCounter < currCounter ? currCounter : maxCounter;
914 if (currCounter == null) {
920 private Integer getMaxCounterFromIds(Component containerComponent, String normalizedName) {
922 Integer maxCounter = 0;
923 List<String> countersStr = containerComponent.getComponentInstances().stream().filter(ci -> ci.getUniqueId() != null && ci.getUniqueId().contains(normalizedName)).map(ci -> ci.getUniqueId().split(normalizedName)[1])
924 .collect(Collectors.toList());
926 if (CollectionUtils.isEmpty(countersStr)) {
929 Integer currCounter = null;
930 for (String counter : countersStr) {
931 if (StringUtils.isEmpty(counter)) {
935 currCounter = Integer.parseInt(counter);
936 } catch (Exception e) {
939 maxCounter = maxCounter < currCounter ? currCounter : maxCounter;
941 if (currCounter == null) {
947 public Either<RequirementCapabilityRelDef, StorageOperationStatus> associateResourceInstances(String componentId, RequirementCapabilityRelDef requirementDef) {
948 return nodeTemplateOperation.associateResourceInstances(componentId, requirementDef);
952 public Either<List<InputDefinition>, StorageOperationStatus> createAndAssociateInputs(Map<String, InputDefinition> inputs, String componentId) {
954 Either<GraphVertex, TitanOperationStatus> getVertexEither = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
955 if (getVertexEither.isRight()) {
956 log.debug("Couldn't fetch component with and unique id {}, error: {}", componentId, getVertexEither.right().value());
957 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value()));
961 GraphVertex vertex = getVertexEither.left().value();
962 Map<String, PropertyDataDefinition> inputsMap = inputs.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> new PropertyDataDefinition(e.getValue())));
964 StorageOperationStatus status = topologyTemplateOperation.associateInputsToComponent(vertex, inputsMap, componentId);
966 if (StorageOperationStatus.OK == status) {
967 log.debug("Component created successfully!!!");
968 List<InputDefinition> inputsResList = null;
969 if (inputsMap != null && !inputsMap.isEmpty()) {
970 inputsResList = inputsMap.values().stream().map(i -> new InputDefinition(i)).collect(Collectors.toList());
972 return Either.left(inputsResList);
974 return Either.right(status);
978 public Either<List<InputDefinition>, StorageOperationStatus> addInputsToComponent(Map<String, InputDefinition> inputs, String componentId) {
980 Either<GraphVertex, TitanOperationStatus> getVertexEither = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
981 if (getVertexEither.isRight()) {
982 log.debug("Couldn't fetch component with and unique id {}, error: {}", componentId, getVertexEither.right().value());
983 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value()));
987 GraphVertex vertex = getVertexEither.left().value();
988 Map<String, PropertyDataDefinition> inputsMap = inputs.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> new PropertyDataDefinition(e.getValue())));
990 StorageOperationStatus status = topologyTemplateOperation.addToscaDataToToscaElement(vertex, EdgeLabelEnum.INPUTS, VertexTypeEnum.INPUTS, inputsMap, JsonPresentationFields.NAME);
992 if (StorageOperationStatus.OK == status) {
993 log.debug("Component created successfully!!!");
994 List<InputDefinition> inputsResList = null;
995 if (inputsMap != null && !inputsMap.isEmpty()) {
996 inputsResList = inputsMap.values().stream().map(i -> new InputDefinition(i)).collect(Collectors.toList());
998 return Either.left(inputsResList);
1000 return Either.right(status);
1004 public Either<Map<String, List<ComponentInstanceProperty>>, StorageOperationStatus> associateComponentInstancePropertiesToComponent(Map<String, List<ComponentInstanceProperty>> instProperties, String componentId) {
1006 Either<GraphVertex, TitanOperationStatus> getVertexEither = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
1007 if (getVertexEither.isRight()) {
1008 log.debug("Couldn't fetch component with and unique id {}, error: {}", componentId, getVertexEither.right().value());
1009 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value()));
1013 GraphVertex vertex = getVertexEither.left().value();
1014 Map<String, MapPropertiesDataDefinition> instPropsMap = new HashMap<>();
1015 if (instProperties != null) {
1017 MapPropertiesDataDefinition propertiesMap;
1018 for (Entry<String, List<ComponentInstanceProperty>> entry : instProperties.entrySet()) {
1019 propertiesMap = new MapPropertiesDataDefinition();
1021 propertiesMap.setMapToscaDataDefinition(entry.getValue().stream().map(e -> new PropertyDataDefinition(e)).collect(Collectors.toMap(e -> e.getName(), e -> e)));
1023 instPropsMap.put(entry.getKey(), propertiesMap);
1027 StorageOperationStatus status = topologyTemplateOperation.associateInstPropertiesToComponent(vertex, instPropsMap);
1029 if (StorageOperationStatus.OK == status) {
1030 log.debug("Component created successfully!!!");
1031 return Either.left(instProperties);
1033 return Either.right(status);
1037 public Either<Map<String, List<ComponentInstanceInput>>, StorageOperationStatus> addComponentInstanceInputsToComponent(Map<String, List<ComponentInstanceInput>> instProperties, String componentId) {
1039 Either<GraphVertex, TitanOperationStatus> getVertexEither = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
1040 if (getVertexEither.isRight()) {
1041 log.debug("Couldn't fetch component with and unique id {}, error: {}", componentId, getVertexEither.right().value());
1042 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value()));
1046 GraphVertex vertex = getVertexEither.left().value();
1047 Map<String, MapPropertiesDataDefinition> instPropsMap = new HashMap<>();
1048 if (instProperties != null) {
1050 MapPropertiesDataDefinition propertiesMap;
1051 for (Entry<String, List<ComponentInstanceInput>> entry : instProperties.entrySet()) {
1052 propertiesMap = new MapPropertiesDataDefinition();
1054 propertiesMap.setMapToscaDataDefinition(entry.getValue().stream().map(e -> new PropertyDataDefinition(e)).collect(Collectors.toMap(e -> e.getName(), e -> e)));
1056 instPropsMap.put(entry.getKey(), propertiesMap);
1060 StorageOperationStatus status = topologyTemplateOperation.addInstInputsToComponent(vertex, instPropsMap);
1062 if (StorageOperationStatus.OK == status) {
1063 log.debug("Component created successfully!!!");
1064 return Either.left(instProperties);
1066 return Either.right(status);
1070 public StorageOperationStatus deleteComponentInstanceInputsToComponent(Map<String, List<ComponentInstanceInput>> instProperties, String componentId) {
1072 Either<GraphVertex, TitanOperationStatus> getVertexEither = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
1073 if (getVertexEither.isRight()) {
1074 log.debug("Couldn't fetch component with and unique id {}, error: {}", componentId, getVertexEither.right().value());
1075 return DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value());
1079 GraphVertex vertex = getVertexEither.left().value();
1080 Map<String, MapPropertiesDataDefinition> instPropsMap = new HashMap<>();
1081 if (instProperties != null) {
1083 MapPropertiesDataDefinition propertiesMap;
1084 for (Entry<String, List<ComponentInstanceInput>> entry : instProperties.entrySet()) {
1085 propertiesMap = new MapPropertiesDataDefinition();
1087 propertiesMap.setMapToscaDataDefinition(entry.getValue().stream().map(e -> new PropertyDataDefinition(e)).collect(Collectors.toMap(e -> e.getName(), e -> e)));
1089 instPropsMap.put(entry.getKey(), propertiesMap);
1093 return topologyTemplateOperation.deleteInstInputsToComponent(vertex, instPropsMap);
1097 public Either<Map<String, List<ComponentInstanceProperty>>, StorageOperationStatus> addComponentInstancePropertiesToComponent(Component containerComponent, Map<String, List<ComponentInstanceProperty>> instProperties, String componentId) {
1099 StorageOperationStatus status = StorageOperationStatus.OK;
1100 if (instProperties != null) {
1102 for (Entry<String, List<ComponentInstanceProperty>> entry : instProperties.entrySet()) {
1103 List<ComponentInstanceProperty> props = entry.getValue();
1104 String componentInstanseId = entry.getKey();
1105 List<ComponentInstanceProperty> instanceProperties = containerComponent.getComponentInstancesProperties().get(componentInstanseId);
1106 if (props != null && !props.isEmpty()) {
1107 for (ComponentInstanceProperty property : props) {
1108 Optional<ComponentInstanceProperty> instanceProperty = instanceProperties.stream().filter(p -> p.getUniqueId().equals(property.getUniqueId())).findAny();
1109 if (instanceProperty.isPresent()) {
1110 status = updateComponentInstanceProperty(containerComponent, componentInstanseId, property);
1112 status = addComponentInstanceProperty(containerComponent, componentInstanseId, property);
1120 return Either.left(instProperties);
1124 public StorageOperationStatus associateArtifactToInstances(Map<String, Map<String, ArtifactDefinition>> instArtifacts, String componentId, User user) {
1126 Either<GraphVertex, TitanOperationStatus> getVertexEither = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
1127 if (getVertexEither.isRight()) {
1128 log.debug("Couldn't fetch component with and unique id {}, error: {}", componentId, getVertexEither.right().value());
1129 return DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value());
1133 GraphVertex vertex = getVertexEither.left().value();
1134 Map<String, MapArtifactDataDefinition> instArtMap = new HashMap<>();
1135 if (instArtifacts != null) {
1137 MapArtifactDataDefinition artifactsMap;
1138 for (Entry<String, Map<String, ArtifactDefinition>> entry : instArtifacts.entrySet()) {
1139 Map<String, ArtifactDefinition> artList = entry.getValue();
1140 Map<String, ArtifactDataDefinition> artifacts = artList.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> new ArtifactDataDefinition(e.getValue())));
1141 artifactsMap = nodeTemplateOperation.prepareInstDeploymentArtifactPerInstance(artifacts, entry.getKey(), user, NodeTemplateOperation.HEAT_VF_ENV_NAME);
1143 instArtMap.put(entry.getKey(), artifactsMap);
1147 return topologyTemplateOperation.associateInstArtifactToComponent(vertex, instArtMap);
1151 public StorageOperationStatus associateInstAttributeToComponentToInstances(Map<String, List<AttributeDefinition>> instArttributes, String componentId) {
1153 Either<GraphVertex, TitanOperationStatus> getVertexEither = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
1154 if (getVertexEither.isRight()) {
1155 log.debug("Couldn't fetch component with and unique id {}, error: {}", componentId, getVertexEither.right().value());
1156 return DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value());
1160 GraphVertex vertex = getVertexEither.left().value();
1161 Map<String, MapAttributesDataDefinition> instAttr = new HashMap<>();
1162 if (instArttributes != null) {
1164 MapAttributesDataDefinition attributesMap;
1165 for (Entry<String, List<AttributeDefinition>> entry : instArttributes.entrySet()) {
1166 attributesMap = new MapAttributesDataDefinition();
1167 attributesMap.setMapToscaDataDefinition(entry.getValue().stream().map(e -> new AttributeDataDefinition(e)).collect(Collectors.toMap(e -> e.getName(), e -> e)));
1168 instAttr.put(entry.getKey(), attributesMap);
1172 return topologyTemplateOperation.associateInstAttributeToComponent(vertex, instAttr);
1176 public StorageOperationStatus associateCalculatedCapReq(Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> instCapabilties, Map<ComponentInstance, Map<String, List<RequirementDefinition>>> instReg, String componentId) {
1177 Either<GraphVertex, TitanOperationStatus> getVertexEither = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
1178 if (getVertexEither.isRight()) {
1179 log.debug("Couldn't fetch component with and unique id {}, error: {}", componentId, getVertexEither.right().value());
1180 return DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value());
1184 GraphVertex vertex = getVertexEither.left().value();
1186 Map<String, MapListRequirementDataDefinition> calcRequirements = new HashMap<>();
1188 Map<String, MapListCapabiltyDataDefinition> calcCapabilty = new HashMap<>();
1189 Map<String, MapCapabiltyProperty> calculatedCapabilitiesProperties = new HashMap<>();;
1190 if (instCapabilties != null) {
1191 for (Entry<ComponentInstance, Map<String, List<CapabilityDefinition>>> entry : instCapabilties.entrySet()) {
1193 Map<String, List<CapabilityDefinition>> caps = entry.getValue();
1194 Map<String, ListCapabilityDataDefinition> mapToscaDataDefinition = new HashMap<>();
1195 for (Entry<String, List<CapabilityDefinition>> instCapability : caps.entrySet()) {
1196 mapToscaDataDefinition.put(instCapability.getKey(), new ListCapabilityDataDefinition(instCapability.getValue().stream().map(iCap -> new CapabilityDataDefinition(iCap)).collect(Collectors.toList())));
1199 ComponentInstanceDataDefinition componentInstance = new ComponentInstanceDataDefinition(entry.getKey());
1200 MapListCapabiltyDataDefinition capMap = nodeTemplateOperation.prepareCalculatedCapabiltyForNodeType(mapToscaDataDefinition, componentInstance);
1202 MapCapabiltyProperty mapCapabiltyProperty = ModelConverter.convertToMapOfMapCapabiltyProperties(caps, componentInstance.getUniqueId(), true);
1204 calcCapabilty.put(entry.getKey().getUniqueId(), capMap);
1205 calculatedCapabilitiesProperties.put(entry.getKey().getUniqueId(), mapCapabiltyProperty);
1209 if (instReg != null) {
1210 for (Entry<ComponentInstance, Map<String, List<RequirementDefinition>>> entry : instReg.entrySet()) {
1212 Map<String, List<RequirementDefinition>> req = entry.getValue();
1213 Map<String, ListRequirementDataDefinition> mapToscaDataDefinition = new HashMap<>();
1214 for (Entry<String, List<RequirementDefinition>> instReq : req.entrySet()) {
1215 mapToscaDataDefinition.put(instReq.getKey(), new ListRequirementDataDefinition(instReq.getValue().stream().map(iCap -> new RequirementDataDefinition(iCap)).collect(Collectors.toList())));
1218 MapListRequirementDataDefinition capMap = nodeTemplateOperation.prepareCalculatedRequirementForNodeType(mapToscaDataDefinition, new ComponentInstanceDataDefinition(entry.getKey()));
1220 calcRequirements.put(entry.getKey().getUniqueId(), capMap);
1224 StorageOperationStatus status = topologyTemplateOperation.associateCalcCapReqToComponent(vertex, calcRequirements, calcCapabilty, calculatedCapabilitiesProperties);
1229 private Either<List<Component>, StorageOperationStatus> getLatestVersionNotAbstractToscaElementsMetadataOnly(boolean isAbstract, Boolean isHighest, ComponentTypeEnum componentTypeEnum, String internalComponentType,
1230 VertexTypeEnum vertexType) {
1232 Map<GraphPropertyEnum, Object> hasProps = new EnumMap<>(GraphPropertyEnum.class);
1233 Map<GraphPropertyEnum, Object> hasNotProps = new EnumMap<>(GraphPropertyEnum.class);
1235 fillPropsMap(hasProps, hasNotProps, internalComponentType, componentTypeEnum, isAbstract, vertexType);
1237 Either<List<GraphVertex>, TitanOperationStatus> getRes = titanDao.getByCriteria(vertexType, hasProps, hasNotProps, JsonParseFlagEnum.ParseMetadata);
1238 if (getRes.isRight()) {
1239 if (getRes.right().value().equals(TitanOperationStatus.NOT_FOUND)) {
1240 return Either.left(new ArrayList<>());
1242 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getRes.right().value()));
1245 List<Component> nonAbstractLatestComponents = new ArrayList<>();
1246 ComponentParametersView params = new ComponentParametersView(true);
1247 params.setIgnoreAllVersions(false);
1248 for (GraphVertex vertexComponent : getRes.left().value()) {
1249 Either<ToscaElement, StorageOperationStatus> componentRes = topologyTemplateOperation.getLightComponent(vertexComponent, componentTypeEnum, params);
1250 if (componentRes.isRight()) {
1251 log.debug("Failed to fetch ligth element for {} error {}", vertexComponent.getUniqueId(), componentRes.right().value());
1252 return Either.right(componentRes.right().value());
1254 Component component = ModelConverter.convertFromToscaElement(componentRes.left().value());
1256 nonAbstractLatestComponents.add(component);
1260 return Either.left(nonAbstractLatestComponents);
1264 public Either<ComponentMetadataData, StorageOperationStatus> getLatestComponentMetadataByUuid(String componentUuid, JsonParseFlagEnum parseFlag) {
1266 Either<ComponentMetadataData, StorageOperationStatus> result;
1268 Map<GraphPropertyEnum, Object> hasProperties = new EnumMap<>(GraphPropertyEnum.class);
1270 hasProperties.put(GraphPropertyEnum.UUID, componentUuid);
1271 hasProperties.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
1273 Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
1274 propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
1276 Either<List<GraphVertex>, TitanOperationStatus> getRes = titanDao.getByCriteria(null, hasProperties, propertiesNotToMatch, parseFlag);
1277 if (getRes.isRight()) {
1278 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getRes.right().value()));
1280 List<ComponentMetadataData> latestVersionList = getRes.left().value().stream().map(ModelConverter::convertToComponentMetadata).collect(Collectors.toList());
1281 ComponentMetadataData latestVersion = latestVersionList.size() == 1 ? latestVersionList.get(0)
1282 : latestVersionList.stream().max((c1, c2) -> Double.compare(Double.parseDouble(c1.getMetadataDataDefinition().getVersion()), Double.parseDouble(c2.getMetadataDataDefinition().getVersion()))).get();
1283 result = Either.left(latestVersion);
1288 public Either<ComponentMetadataData, StorageOperationStatus> getComponentMetadata(String componentId) {
1290 Either<ComponentMetadataData, StorageOperationStatus> result;
1291 Either<GraphVertex, TitanOperationStatus> getRes = titanDao.getVertexById(componentId, JsonParseFlagEnum.ParseMetadata);
1292 if (getRes.isRight()) {
1293 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getRes.right().value()));
1295 ComponentMetadataData componentMetadata = ModelConverter.convertToComponentMetadata(getRes.left().value());
1296 result = Either.left(componentMetadata);
1301 private Map<String, ComponentMetadataData> findLatestVersion(List<ComponentMetadataData> resourceDataList) {
1302 Map<Pair<String, String>, ComponentMetadataData> latestVersionMap = new HashMap<Pair<String, String>, ComponentMetadataData>();
1303 for (ComponentMetadataData resourceData : resourceDataList) {
1304 ComponentMetadataData latestVersionData = resourceData;
1306 ComponentMetadataDataDefinition metadataDataDefinition = resourceData.getMetadataDataDefinition();
1307 Pair<String, String> pair = createKeyPair(latestVersionData);
1308 if (latestVersionMap.containsKey(pair)) {
1309 latestVersionData = latestVersionMap.get(pair);
1310 String currentVersion = latestVersionData.getMetadataDataDefinition().getVersion();
1311 String newVersion = metadataDataDefinition.getVersion();
1312 if (CommonBeUtils.compareAsdcComponentVersions(newVersion, currentVersion)) {
1313 latestVersionData = resourceData;
1316 if (log.isDebugEnabled())
1317 log.debug("last certified version of resource = {} version is {}", latestVersionData.getMetadataDataDefinition().getName(), latestVersionData.getMetadataDataDefinition().getVersion());
1319 latestVersionMap.put(pair, latestVersionData);
1322 Map<String, ComponentMetadataData> resVersionMap = new HashMap<String, ComponentMetadataData>();
1323 for (ComponentMetadataData resourceData : latestVersionMap.values()) {
1324 ComponentMetadataData latestVersionData = resourceData;
1325 ComponentMetadataDataDefinition metadataDataDefinition = resourceData.getMetadataDataDefinition();
1326 if (resVersionMap.containsKey(metadataDataDefinition.getUUID())) {
1327 latestVersionData = resVersionMap.get(metadataDataDefinition.getUUID());
1328 String currentVersion = latestVersionData.getMetadataDataDefinition().getVersion();
1329 String newVersion = metadataDataDefinition.getVersion();
1330 if (CommonBeUtils.compareAsdcComponentVersions(newVersion, currentVersion)) {
1331 latestVersionData = resourceData;
1334 if (log.isDebugEnabled())
1335 log.debug("last uuid version of resource = {} version is {}", latestVersionData.getMetadataDataDefinition().getName(), latestVersionData.getMetadataDataDefinition().getVersion());
1336 resVersionMap.put(latestVersionData.getMetadataDataDefinition().getUUID(), latestVersionData);
1339 return resVersionMap;
1342 private Pair<String, String> createKeyPair(ComponentMetadataData metadataData) {
1343 Pair<String, String> pair;
1344 NodeTypeEnum label = NodeTypeEnum.getByName(metadataData.getLabel());
1347 pair = new ImmutablePair<>(metadataData.getMetadataDataDefinition().getName(), ((ResourceMetadataDataDefinition) metadataData.getMetadataDataDefinition()).getResourceType().name());
1350 pair = new ImmutablePair<>(metadataData.getMetadataDataDefinition().getName(), metadataData.getLabel());
1357 public Either<List<Component>, StorageOperationStatus> getLatestVersionNotAbstractComponents(boolean isAbstract, Boolean isHighest, ComponentTypeEnum componentTypeEnum, String internalComponentType, List<String> componentUids) {
1359 Either<List<Component>, StorageOperationStatus> result = null;
1360 List<Component> components = new ArrayList<>();
1361 if (componentUids == null) {
1362 Either<List<String>, StorageOperationStatus> componentUidsRes = getComponentUids(isAbstract, isHighest, componentTypeEnum, internalComponentType, componentUids);
1363 if (componentUidsRes.isRight()) {
1364 result = Either.right(componentUidsRes.right().value());
1366 componentUids = componentUidsRes.left().value();
1369 if (!componentUids.isEmpty()) {
1370 for (String componentUid : componentUids) {
1371 ComponentParametersView componentParametersView = buildComponentViewForNotAbstract();
1372 if (internalComponentType != null && "vl".equalsIgnoreCase(internalComponentType)) {
1373 componentParametersView.setIgnoreCapabilities(false);
1374 componentParametersView.setIgnoreRequirements(false);
1376 Either<ToscaElement, StorageOperationStatus> getToscaElementRes = nodeTemplateOperation.getToscaElementOperation(componentTypeEnum).getLightComponent(componentUid, componentTypeEnum, componentParametersView);
1377 if (getToscaElementRes.isRight()) {
1378 if (log.isDebugEnabled())
1379 log.debug("Failed to fetch resource for error is {}", getToscaElementRes.right().value());
1380 result = Either.right(getToscaElementRes.right().value());
1383 Component component = ModelConverter.convertFromToscaElement(getToscaElementRes.left().value());
1384 component.setContactId(null);
1385 component.setCreationDate(null);
1386 component.setCreatorUserId(null);
1387 component.setCreatorFullName(null);
1388 component.setLastUpdateDate(null);
1389 component.setLastUpdaterUserId(null);
1390 component.setLastUpdaterFullName(null);
1391 component.setNormalizedName(null);
1392 components.add(component);
1395 if (result == null) {
1396 result = Either.left(components);
1401 private Either<List<String>, StorageOperationStatus> getComponentUids(boolean isAbstract, Boolean isHighest, ComponentTypeEnum componentTypeEnum, String internalComponentType, List<String> componentUids) {
1403 Either<List<String>, StorageOperationStatus> result = null;
1404 Either<List<Component>, StorageOperationStatus> getToscaElementsRes = getLatestVersionNotAbstractMetadataOnly(isAbstract, isHighest, componentTypeEnum, internalComponentType);
1405 if (getToscaElementsRes.isRight()) {
1406 result = Either.right(getToscaElementsRes.right().value());
1408 List<Component> collection = getToscaElementsRes.left().value();
1409 if (collection == null) {
1410 componentUids = new ArrayList<>();
1412 componentUids = collection.stream().map(p -> p.getUniqueId()).collect(Collectors.toList());
1415 if (result == null) {
1416 result = Either.left(componentUids);
1421 private ComponentParametersView buildComponentViewForNotAbstract() {
1422 ComponentParametersView componentParametersView = new ComponentParametersView();
1423 componentParametersView.disableAll();
1424 componentParametersView.setIgnoreCategories(false);
1425 componentParametersView.setIgnoreAllVersions(false);
1426 return componentParametersView;
1429 public Either<Boolean, StorageOperationStatus> validateComponentNameExists(String name, ResourceTypeEnum resourceType, ComponentTypeEnum componentType) {
1430 Either<Boolean, StorageOperationStatus> result = validateComponentNameUniqueness(name, resourceType, componentType);
1431 if (result.isLeft()) {
1432 result = Either.left(!result.left().value());
1437 public Either<Boolean, StorageOperationStatus> validateComponentNameUniqueness(String name, ResourceTypeEnum resourceType, ComponentTypeEnum componentType) {
1438 VertexTypeEnum vertexType = getVertexTypeByComponentAndResourceTypeEnum(resourceType, componentType);
1439 String normalizedName = ValidationUtils.normaliseComponentName(name);
1440 Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
1441 properties.put(GraphPropertyEnum.NORMALIZED_NAME, normalizedName);
1442 properties.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
1444 Either<List<GraphVertex>, TitanOperationStatus> vertexEither = titanDao.getByCriteria(vertexType, properties, JsonParseFlagEnum.NoParse);
1445 if (vertexEither.isRight() && vertexEither.right().value() != TitanOperationStatus.NOT_FOUND) {
1446 log.debug("failed to get vertex from graph with property normalizedName: {}", normalizedName);
1447 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(vertexEither.right().value()));
1449 List<GraphVertex> vertexList = vertexEither.isLeft() ? vertexEither.left().value() : null;
1450 if (vertexList != null && !vertexList.isEmpty()) {
1451 return Either.left(false);
1453 return Either.left(true);
1457 // UI query parameter is either VFC/CP/VL(for yaml upload/update) or VF (for CSAR upload/user composed VF)
1458 // TODO implementation of topology template VFCs may require updating this method - depending on UI implementation
1459 private VertexTypeEnum getVertexTypeByComponentAndResourceTypeEnum(ResourceTypeEnum resourceType, ComponentTypeEnum componentType) {
1460 VertexTypeEnum vertexType = VertexTypeEnum.TOPOLOGY_TEMPLATE;
1461 if (ComponentTypeEnum.RESOURCE == componentType && ResourceTypeEnum.VF != resourceType) {
1462 vertexType = VertexTypeEnum.NODE_TYPE;
1467 private void fillNodeTypePropsMap(Map<GraphPropertyEnum, Object> hasProps, Map<GraphPropertyEnum, Object> hasNotProps, String internalComponentType) {
1468 switch (internalComponentType.toLowerCase()) {
1470 hasNotProps.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.VF.name());
1471 hasNotProps.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.VFCMT.name());
1474 hasNotProps.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.VFC.name());
1475 hasNotProps.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.VFCMT.name());
1478 hasProps.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.VL.name());
1485 private void fillTopologyTemplatePropsMap(Map<GraphPropertyEnum, Object> hasProps, ComponentTypeEnum componentTypeEnum) {
1486 switch (componentTypeEnum) {
1488 hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
1491 hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
1498 private void fillPropsMap(Map<GraphPropertyEnum, Object> hasProps, Map<GraphPropertyEnum, Object> hasNotProps, String internalComponentType, ComponentTypeEnum componentTypeEnum, boolean isAbstract, VertexTypeEnum internalVertexType) {
1499 hasNotProps.put(GraphPropertyEnum.STATE, LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name());
1501 hasNotProps.put(GraphPropertyEnum.IS_DELETED, true);
1502 if (VertexTypeEnum.NODE_TYPE == internalVertexType) {
1503 hasProps.put(GraphPropertyEnum.IS_ABSTRACT, isAbstract);
1504 if (internalComponentType != null) {
1505 fillNodeTypePropsMap(hasProps, hasNotProps, internalComponentType);
1508 fillTopologyTemplatePropsMap(hasProps, componentTypeEnum);
1512 private List<VertexTypeEnum> getInternalVertexTypes(ComponentTypeEnum componentTypeEnum, String internalComponentType) {
1513 List<VertexTypeEnum> internalVertexTypes = new ArrayList<>();
1514 if (ComponentTypeEnum.RESOURCE == componentTypeEnum) {
1515 internalVertexTypes.add(VertexTypeEnum.NODE_TYPE);
1517 if (ComponentTypeEnum.SERVICE == componentTypeEnum || "service".equalsIgnoreCase(internalComponentType)) {
1518 internalVertexTypes.add(VertexTypeEnum.TOPOLOGY_TEMPLATE);
1520 return internalVertexTypes;
1523 public Either<List<Component>, StorageOperationStatus> getLatestVersionNotAbstractMetadataOnly(boolean isAbstract, Boolean isHighest, ComponentTypeEnum componentTypeEnum, String internalComponentType) {
1524 List<VertexTypeEnum> internalVertexTypes = getInternalVertexTypes(componentTypeEnum, internalComponentType);
1525 List<Component> result = new ArrayList<>();
1526 for (VertexTypeEnum vertexType : internalVertexTypes) {
1527 Either<List<Component>, StorageOperationStatus> listByVertexType = getLatestVersionNotAbstractToscaElementsMetadataOnly(isAbstract, isHighest, componentTypeEnum, internalComponentType, vertexType);
1528 if (listByVertexType.isRight()) {
1529 return listByVertexType;
1531 result.addAll(listByVertexType.left().value());
1533 return Either.left(result);
1537 public Either<List<Component>, StorageOperationStatus> getLatestComponentListByUuid(String componentUuid) {
1538 Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
1539 propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
1540 return getComponentListByUuid(componentUuid, propertiesToMatch);
1543 public Either<List<Component>, StorageOperationStatus> getComponentListByUuid(String componentUuid, Map<GraphPropertyEnum, Object> additionalPropertiesToMatch) {
1545 Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
1547 if(additionalPropertiesToMatch != null){
1548 propertiesToMatch.putAll(additionalPropertiesToMatch);
1551 propertiesToMatch.put(GraphPropertyEnum.UUID, componentUuid);
1553 Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
1554 propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
1556 Either<List<GraphVertex>, TitanOperationStatus> vertexEither = titanDao.getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll);
1558 if (vertexEither.isRight()) {
1559 log.debug("Couldn't fetch metadata for component with type {} and uuid {}, error: {}", componentUuid, vertexEither.right().value());
1560 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(vertexEither.right().value()));
1562 List<GraphVertex> vertexList = vertexEither.isLeft() ? vertexEither.left().value() : null;
1564 if (vertexList == null || vertexList.isEmpty()) {
1565 log.debug("Component with uuid {} was not found", componentUuid);
1566 return Either.right(StorageOperationStatus.NOT_FOUND);
1569 List<Component> latestComponents = new ArrayList<Component>();
1570 for (GraphVertex vertex : vertexList) {
1571 latestComponents.add(getToscaElementByOperation(vertex).left().value());
1574 return Either.left(latestComponents);
1577 public Either<Component, StorageOperationStatus> getLatestComponentByUuid(String componentUuid) {
1579 Either<List<Component>, StorageOperationStatus> latestVersionListEither = getLatestComponentListByUuid(componentUuid);
1581 if (latestVersionListEither.isRight()) {
1582 return Either.right(latestVersionListEither.right().value());
1585 List<Component> latestVersionList = latestVersionListEither.left().value();
1587 if (latestVersionList.isEmpty()) {
1588 return Either.right(StorageOperationStatus.NOT_FOUND);
1590 Component component = latestVersionList.size() == 1 ? latestVersionList.get(0) : latestVersionList.stream().max((c1, c2) -> Double.compare(Double.parseDouble(c1.getVersion()), Double.parseDouble(c2.getVersion()))).get();
1592 return Either.left(component);
1595 public Either<List<Resource>, StorageOperationStatus> getAllCertifiedResources(boolean isAbstract, Boolean isHighest) {
1597 List<Resource> resources = new ArrayList<>();
1598 Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
1599 Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
1601 propertiesToMatch.put(GraphPropertyEnum.IS_ABSTRACT, isAbstract);
1602 if (isHighest != null) {
1603 propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, isHighest.booleanValue());
1605 propertiesToMatch.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
1606 propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
1607 propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
1609 Either<List<GraphVertex>, TitanOperationStatus> getResourcesRes = titanDao.getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll);
1611 if (getResourcesRes.isRight()) {
1612 log.debug("Failed to fetch all certified resources. Status is {}", getResourcesRes.right().value());
1613 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getResourcesRes.right().value()));
1615 List<GraphVertex> resourceVerticies = getResourcesRes.left().value();
1616 for (GraphVertex resourceV : resourceVerticies) {
1617 Either<Resource, StorageOperationStatus> getResourceRes = getToscaElement(resourceV);
1618 if (getResourceRes.isRight()) {
1619 return Either.right(getResourceRes.right().value());
1621 resources.add(getResourceRes.left().value());
1623 return Either.left(resources);
1626 public <T extends Component> Either<T, StorageOperationStatus> getLatestByNameAndVersion(String name, String version, JsonParseFlagEnum parseFlag) {
1627 Either<T, StorageOperationStatus> result;
1629 Map<GraphPropertyEnum, Object> hasProperties = new EnumMap<>(GraphPropertyEnum.class);
1630 Map<GraphPropertyEnum, Object> hasNotProperties = new EnumMap<>(GraphPropertyEnum.class);
1632 hasProperties.put(GraphPropertyEnum.NAME, name);
1633 hasProperties.put(GraphPropertyEnum.VERSION, version);
1634 hasProperties.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
1636 hasNotProperties.put(GraphPropertyEnum.IS_DELETED, true);
1638 Either<List<GraphVertex>, TitanOperationStatus> getResourceRes = titanDao.getByCriteria(null, hasProperties, hasNotProperties, parseFlag);
1639 if (getResourceRes.isRight()) {
1640 TitanOperationStatus status = getResourceRes.right().value();
1641 log.debug("failed to find resource with name {}, version {}. Status is {} ", name, version, status);
1642 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1645 return getToscaElementByOperation(getResourceRes.left().value().get(0));
1648 public Either<Resource, StorageOperationStatus> getLatestComponentByCsarOrName(ComponentTypeEnum componentType, String csarUUID, String systemName) {
1649 return getLatestComponentByCsarOrName(componentType, csarUUID, systemName, false, JsonParseFlagEnum.ParseAll);
1652 public Either<Resource, StorageOperationStatus> getLatestComponentByCsarOrName(ComponentTypeEnum componentType, String csarUUID, String systemName, boolean allowDeleted, JsonParseFlagEnum parseFlag) {
1653 Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
1654 props.put(GraphPropertyEnum.CSAR_UUID, csarUUID);
1655 props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
1656 if (componentType != null) {
1657 props.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
1659 Map<GraphPropertyEnum, Object> propsHasNot = new EnumMap<>(GraphPropertyEnum.class);
1660 propsHasNot.put(GraphPropertyEnum.IS_DELETED, true);
1662 GraphVertex resourceMetadataData = null;
1663 List<GraphVertex> resourceMetadataDataList = null;
1664 Either<List<GraphVertex>, TitanOperationStatus> byCsar = titanDao.getByCriteria(null, props, propsHasNot, JsonParseFlagEnum.ParseMetadata);
1665 if (byCsar.isRight()) {
1666 if (TitanOperationStatus.NOT_FOUND == byCsar.right().value()) {
1667 // Fix Defect DE256036
1668 if (StringUtils.isEmpty(systemName)) {
1669 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(TitanOperationStatus.NOT_FOUND));
1673 props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
1674 props.put(GraphPropertyEnum.SYSTEM_NAME, systemName);
1675 Either<List<GraphVertex>, TitanOperationStatus> bySystemname = titanDao.getByCriteria(null, props, JsonParseFlagEnum.ParseMetadata);
1676 if (bySystemname.isRight()) {
1677 log.debug("getLatestResourceByCsarOrName - Failed to find by system name {} error {} ", systemName, bySystemname.right().value());
1678 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(bySystemname.right().value()));
1680 if (bySystemname.left().value().size() > 2) {
1681 log.debug("getLatestResourceByCsarOrName - getByCriteria(by system name) must return only 2 latest version, but was returned - {}", bySystemname.left().value().size());
1682 return Either.right(StorageOperationStatus.GENERAL_ERROR);
1684 resourceMetadataDataList = bySystemname.left().value();
1685 if (resourceMetadataDataList.size() == 1) {
1686 resourceMetadataData = resourceMetadataDataList.get(0);
1688 for (GraphVertex curResource : resourceMetadataDataList) {
1689 if (!((String) curResource.getJsonMetadataField(JsonPresentationFields.LIFECYCLE_STATE)).equals("CERTIFIED")) {
1690 resourceMetadataData = curResource;
1695 if (resourceMetadataData == null) {
1696 log.debug("getLatestResourceByCsarOrName - getByCriteria(by system name) returned 2 latest CERTIFIED versions");
1697 return Either.right(StorageOperationStatus.GENERAL_ERROR);
1699 if (resourceMetadataData.getJsonMetadataField(JsonPresentationFields.CSAR_UUID) != null && !((String) resourceMetadataData.getJsonMetadataField(JsonPresentationFields.CSAR_UUID)).equals(csarUUID)) {
1700 log.debug("getLatestResourceByCsarOrName - same system name {} but different csarUUID. exist {} and new {} ", systemName, resourceMetadataData.getJsonMetadataField(JsonPresentationFields.CSAR_UUID), csarUUID);
1701 // correct error will be returned from create flow. with all
1702 // correct audit records!!!!!
1703 return Either.right(StorageOperationStatus.NOT_FOUND);
1705 Either<Resource, StorageOperationStatus> resource = getToscaElement((String) resourceMetadataData.getUniqueId());
1709 resourceMetadataDataList = byCsar.left().value();
1710 if (resourceMetadataDataList.size() > 2) {
1711 log.debug("getLatestResourceByCsarOrName - getByCriteria(by csar) must return only 2 latest version, but was returned - {}", byCsar.left().value().size());
1712 return Either.right(StorageOperationStatus.GENERAL_ERROR);
1714 if (resourceMetadataDataList.size() == 1) {
1715 resourceMetadataData = resourceMetadataDataList.get(0);
1717 for (GraphVertex curResource : resourceMetadataDataList) {
1718 if (!((String) curResource.getJsonMetadataField(JsonPresentationFields.LIFECYCLE_STATE)).equals("CERTIFIED")) {
1719 resourceMetadataData = curResource;
1724 if (resourceMetadataData == null) {
1725 log.debug("getLatestResourceByCsarOrName - getByCriteria(by csar) returned 2 latest CERTIFIED versions");
1726 return Either.right(StorageOperationStatus.GENERAL_ERROR);
1728 Either<Resource, StorageOperationStatus> resource = getToscaElement((String) resourceMetadataData.getJsonMetadataField(JsonPresentationFields.UNIQUE_ID), parseFlag);
1734 public Either<Boolean, StorageOperationStatus> validateToscaResourceNameExtends(String templateNameCurrent, String templateNameExtends) {
1736 String currentTemplateNameChecked = templateNameExtends;
1738 while (currentTemplateNameChecked != null && !currentTemplateNameChecked.equalsIgnoreCase(templateNameCurrent)) {
1739 Either<Resource, StorageOperationStatus> latestByToscaResourceName = getLatestByToscaResourceName(currentTemplateNameChecked);
1741 if (latestByToscaResourceName.isRight()) {
1742 return latestByToscaResourceName.right().value() == StorageOperationStatus.NOT_FOUND ? Either.left(false) : Either.right(latestByToscaResourceName.right().value());
1745 Resource value = latestByToscaResourceName.left().value();
1747 if (value.getDerivedFrom() != null) {
1748 currentTemplateNameChecked = value.getDerivedFrom().get(0);
1750 currentTemplateNameChecked = null;
1754 return (currentTemplateNameChecked != null && currentTemplateNameChecked.equalsIgnoreCase(templateNameCurrent)) ? Either.left(true) : Either.left(false);
1757 public Either<List<Component>, StorageOperationStatus> fetchByResourceType(String resourceType) {
1759 Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
1760 props.put(GraphPropertyEnum.RESOURCE_TYPE, resourceType);
1761 props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
1762 Either<List<GraphVertex>, TitanOperationStatus> resourcesByTypeEither = titanDao.getByCriteria(null, props);
1764 if (resourcesByTypeEither.isRight()) {
1765 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(resourcesByTypeEither.right().value()));
1768 List<GraphVertex> vertexList = resourcesByTypeEither.left().value();
1769 List<Component> components = new ArrayList<>();
1771 for (GraphVertex vertex : vertexList) {
1772 components.add(getToscaElementByOperation(vertex).left().value());
1775 return Either.left(components);
1779 public void commit() {
1783 public Either<Service, StorageOperationStatus> updateDistributionStatus(Service service, User user, DistributionStatusEnum distributionStatus) {
1784 Either<GraphVertex, StorageOperationStatus> updateDistributionStatus = topologyTemplateOperation.updateDistributionStatus(service.getUniqueId(), user, distributionStatus);
1785 if ( updateDistributionStatus.isRight() ){
1786 return Either.right(updateDistributionStatus.right().value());
1788 GraphVertex serviceV = updateDistributionStatus.left().value();
1789 service.setDistributionStatus(distributionStatus);
1790 service.setLastUpdateDate((Long) serviceV.getJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE));
1791 return Either.left(service);
1794 public Either<ComponentMetadataData, StorageOperationStatus> updateComponentLastUpdateDateOnGraph(Component component, Long modificationTime) {
1796 Either<ComponentMetadataData, StorageOperationStatus> result = null;
1797 GraphVertex serviceVertex;
1798 Either<GraphVertex, TitanOperationStatus> updateRes = null;
1799 Either<GraphVertex, TitanOperationStatus> getRes = titanDao.getVertexById(component.getUniqueId(), JsonParseFlagEnum.ParseMetadata);
1800 if (getRes.isRight()) {
1801 TitanOperationStatus status = getRes.right().value();
1802 log.error("Failed to fetch component {}. status is {}", component.getUniqueId(), status);
1803 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
1805 if (result == null) {
1806 serviceVertex = getRes.left().value();
1807 long lastUpdateDate = System.currentTimeMillis();
1808 serviceVertex.setJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE, lastUpdateDate);
1809 component.setLastUpdateDate(lastUpdateDate);
1810 updateRes = titanDao.updateVertex(serviceVertex);
1811 if (updateRes.isRight()) {
1812 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(updateRes.right().value()));
1815 if (result == null) {
1816 result = Either.left(ModelConverter.convertToComponentMetadata(updateRes.left().value()));
1821 public TitanDao getTitanDao() {
1825 public Either<List<Service>, StorageOperationStatus> getCertifiedServicesWithDistStatus(Set<DistributionStatusEnum> distStatus) {
1826 Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
1827 propertiesToMatch.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
1829 return getServicesWithDistStatus(distStatus, propertiesToMatch);
1832 public Either<List<Service>, StorageOperationStatus> getServicesWithDistStatus(Set<DistributionStatusEnum> distStatus, Map<GraphPropertyEnum, Object> additionalPropertiesToMatch) {
1834 List<Service> servicesAll = new ArrayList<>();
1836 Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
1837 Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
1839 if(additionalPropertiesToMatch != null && !additionalPropertiesToMatch.isEmpty()) {
1840 propertiesToMatch.putAll(additionalPropertiesToMatch);
1843 propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
1845 propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
1847 if (distStatus != null && !distStatus.isEmpty()) {
1848 for (DistributionStatusEnum state : distStatus) {
1849 propertiesToMatch.put(GraphPropertyEnum.DISTRIBUTION_STATUS, state.name());
1850 Either<List<Service>, StorageOperationStatus> fetchServicesByCriteria = fetchServicesByCriteria(servicesAll, propertiesToMatch, propertiesNotToMatch);
1851 if ( fetchServicesByCriteria.isRight() ){
1852 return fetchServicesByCriteria;
1855 servicesAll = fetchServicesByCriteria.left().value();
1858 return Either.left(servicesAll);
1860 return fetchServicesByCriteria(servicesAll, propertiesToMatch, propertiesNotToMatch);
1864 private Either<List<Service>, StorageOperationStatus> fetchServicesByCriteria(List<Service> servicesAll, Map<GraphPropertyEnum, Object> propertiesToMatch, Map<GraphPropertyEnum, Object> propertiesNotToMatch) {
1865 Either<List<GraphVertex>, TitanOperationStatus> getRes = titanDao.getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll);
1866 if (getRes.isRight()) {
1867 if (getRes.right().value() != TitanOperationStatus.NOT_FOUND) {
1868 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch certified services by match properties {} not match properties {} . Status is {}. ", propertiesToMatch, propertiesNotToMatch, getRes.right().value());
1869 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getRes.right().value()));
1872 for (GraphVertex vertex : getRes.left().value()) {
1873 Either<Component, StorageOperationStatus> getServiceRes = getToscaElementByOperation(vertex);
1874 if (getServiceRes.isRight()) {
1875 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch certified service {}. Status is {}. ", vertex.getJsonMetadataField(JsonPresentationFields.NAME), getServiceRes.right().value());
1876 return Either.right(getServiceRes.right().value());
1878 servicesAll.add((Service) getToscaElementByOperation(vertex).left().value());
1882 return Either.left(servicesAll);
1885 public void rollback() {
1886 titanDao.rollback();
1889 public StorageOperationStatus addDeploymentArtifactsToInstance(String componentId, ComponentInstance componentInstance, Map<String, ArtifactDefinition> finalDeploymentArtifacts) {
1890 Map<String, ArtifactDataDefinition> instDeplArtifacts = finalDeploymentArtifacts.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> new ArtifactDataDefinition(e.getValue())));
1892 return nodeTemplateOperation.addDeploymentArtifactsToInstance(componentId, componentInstance.getUniqueId(), instDeplArtifacts);
1895 public StorageOperationStatus generateCustomizationUUIDOnInstance(String componentId, String instanceId) {
1896 return nodeTemplateOperation.generateCustomizationUUIDOnInstance(componentId, instanceId);
1899 public StorageOperationStatus generateCustomizationUUIDOnInstanceGroup(String componentId, String instanceId, List<String> groupInstances) {
1900 return nodeTemplateOperation.generateCustomizationUUIDOnInstanceGroup(componentId, instanceId, groupInstances);
1903 public Either<PropertyDefinition, StorageOperationStatus> addPropertyToResource(String propertyName, PropertyDefinition newPropertyDefinition, Resource resource) {
1905 Either<PropertyDefinition, StorageOperationStatus> result = null;
1906 Either<Component, StorageOperationStatus> getUpdatedComponentRes = null;
1907 newPropertyDefinition.setName(propertyName);
1908 newPropertyDefinition.setParentUniqueId(resource.getUniqueId());
1909 StorageOperationStatus status = getToscaElementOperation(resource).addToscaDataToToscaElement(resource.getUniqueId(), EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, newPropertyDefinition, JsonPresentationFields.NAME);
1910 if (status != StorageOperationStatus.OK) {
1911 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to add the property {} to the resource {}. Status is {}. ", propertyName, resource.getName(), status);
1912 result = Either.right(status);
1914 if (result == null) {
1915 ComponentParametersView filter = new ComponentParametersView(true);
1916 filter.setIgnoreProperties(false);
1917 getUpdatedComponentRes = getToscaElement(resource.getUniqueId(), filter);
1918 if (getUpdatedComponentRes.isRight()) {
1919 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to get updated resource {}. Status is {}. ", resource.getUniqueId(), getUpdatedComponentRes.right().value());
1920 result = Either.right(status);
1923 if (result == null) {
1924 PropertyDefinition newProperty = null;
1925 List<PropertyDefinition> properties = ((Resource) getUpdatedComponentRes.left().value()).getProperties();
1926 if (CollectionUtils.isNotEmpty(properties)) {
1927 Optional<PropertyDefinition> newPropertyOptional = properties.stream().filter(p -> p.getName().equals(propertyName)).findAny();
1928 if (newPropertyOptional.isPresent()) {
1929 newProperty = newPropertyOptional.get();
1932 if (newProperty == null) {
1933 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to find recently added property {} on the resource {}. Status is {}. ", propertyName, resource.getUniqueId(), StorageOperationStatus.NOT_FOUND);
1934 result = Either.right(StorageOperationStatus.NOT_FOUND);
1936 result = Either.left(newProperty);
1942 public StorageOperationStatus deletePropertyOfResource(Resource resource, String propertyName) {
1943 return getToscaElementOperation(resource).deleteToscaDataElement(resource.getUniqueId(), EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, propertyName, JsonPresentationFields.NAME);
1946 public StorageOperationStatus deleteAttributeOfResource(Component component, String attributeName) {
1947 return getToscaElementOperation(component).deleteToscaDataElement(component.getUniqueId(), EdgeLabelEnum.ATTRIBUTES, VertexTypeEnum.ATTRIBUTES, attributeName, JsonPresentationFields.NAME);
1950 public StorageOperationStatus deleteInputOfResource(Component resource, String inputName) {
1951 return getToscaElementOperation(resource).deleteToscaDataElement(resource.getUniqueId(), EdgeLabelEnum.INPUTS, VertexTypeEnum.INPUTS, inputName, JsonPresentationFields.NAME);
1954 public Either<PropertyDefinition, StorageOperationStatus> updatePropertyOfResource(Resource resource, PropertyDefinition newPropertyDefinition) {
1956 Either<Component, StorageOperationStatus> getUpdatedComponentRes = null;
1957 Either<PropertyDefinition, StorageOperationStatus> result = null;
1958 StorageOperationStatus status = getToscaElementOperation(resource).updateToscaDataOfToscaElement(resource.getUniqueId(), EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, newPropertyDefinition, JsonPresentationFields.NAME);
1959 if (status != StorageOperationStatus.OK) {
1960 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to add the property {} to the resource {}. Status is {}. ", newPropertyDefinition.getName(), resource.getName(), status);
1961 result = Either.right(status);
1963 if (result == null) {
1964 ComponentParametersView filter = new ComponentParametersView(true);
1965 filter.setIgnoreProperties(false);
1966 getUpdatedComponentRes = getToscaElement(resource.getUniqueId(), filter);
1967 if (getUpdatedComponentRes.isRight()) {
1968 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to get updated resource {}. Status is {}. ", resource.getUniqueId(), getUpdatedComponentRes.right().value());
1969 result = Either.right(status);
1972 if (result == null) {
1973 Optional<PropertyDefinition> newProperty = ((Resource) getUpdatedComponentRes.left().value()).getProperties().stream().filter(p -> p.getName().equals(newPropertyDefinition.getName())).findAny();
1974 if (newProperty.isPresent()) {
1975 result = Either.left(newProperty.get());
1977 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to find recently added property {} on the resource {}. Status is {}. ", newPropertyDefinition.getName(), resource.getUniqueId(), StorageOperationStatus.NOT_FOUND);
1978 result = Either.right(StorageOperationStatus.NOT_FOUND);
1984 public Either<AttributeDefinition, StorageOperationStatus> addAttributeOfResource(Component component, AttributeDefinition newAttributeDef) {
1986 Either<Component, StorageOperationStatus> getUpdatedComponentRes = null;
1987 Either<AttributeDefinition, StorageOperationStatus> result = null;
1988 if(newAttributeDef.getUniqueId() == null || newAttributeDef.getUniqueId().isEmpty()){
1989 String attUniqueId = UniqueIdBuilder.buildAttributeUid(component.getUniqueId(), newAttributeDef.getName());
1990 newAttributeDef.setUniqueId(attUniqueId);
1993 StorageOperationStatus status = getToscaElementOperation(component).addToscaDataToToscaElement(component.getUniqueId(), EdgeLabelEnum.ATTRIBUTES, VertexTypeEnum.ATTRIBUTES, newAttributeDef, JsonPresentationFields.NAME);
1994 if (status != StorageOperationStatus.OK) {
1995 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to add the property {} to the resource {}. Status is {}. ", newAttributeDef.getName(), component.getName(), status);
1996 result = Either.right(status);
1998 if (result == null) {
1999 ComponentParametersView filter = new ComponentParametersView(true);
2000 filter.setIgnoreAttributesFrom(false);
2001 getUpdatedComponentRes = getToscaElement(component.getUniqueId(), filter);
2002 if (getUpdatedComponentRes.isRight()) {
2003 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to get updated resource {}. Status is {}. ", component.getUniqueId(), getUpdatedComponentRes.right().value());
2004 result = Either.right(status);
2007 if (result == null) {
2008 Optional<AttributeDefinition> newAttribute = ((Resource) getUpdatedComponentRes.left().value()).getAttributes().stream().filter(p -> p.getName().equals(newAttributeDef.getName())).findAny();
2009 if (newAttribute.isPresent()) {
2010 result = Either.left(newAttribute.get());
2012 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to find recently added property {} on the resource {}. Status is {}. ", newAttributeDef.getName(), component.getUniqueId(), StorageOperationStatus.NOT_FOUND);
2013 result = Either.right(StorageOperationStatus.NOT_FOUND);
2019 public Either<AttributeDefinition, StorageOperationStatus> updateAttributeOfResource(Component component, AttributeDefinition newAttributeDef) {
2021 Either<Component, StorageOperationStatus> getUpdatedComponentRes = null;
2022 Either<AttributeDefinition, StorageOperationStatus> result = null;
2023 StorageOperationStatus status = getToscaElementOperation(component).updateToscaDataOfToscaElement(component.getUniqueId(), EdgeLabelEnum.ATTRIBUTES, VertexTypeEnum.ATTRIBUTES, newAttributeDef, JsonPresentationFields.NAME);
2024 if (status != StorageOperationStatus.OK) {
2025 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to add the property {} to the resource {}. Status is {}. ", newAttributeDef.getName(), component.getName(), status);
2026 result = Either.right(status);
2028 if (result == null) {
2029 ComponentParametersView filter = new ComponentParametersView(true);
2030 filter.setIgnoreAttributesFrom(false);
2031 getUpdatedComponentRes = getToscaElement(component.getUniqueId(), filter);
2032 if (getUpdatedComponentRes.isRight()) {
2033 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to get updated resource {}. Status is {}. ", component.getUniqueId(), getUpdatedComponentRes.right().value());
2034 result = Either.right(status);
2037 if (result == null) {
2038 Optional<AttributeDefinition> newProperty = ((Resource) getUpdatedComponentRes.left().value()).getAttributes().stream().filter(p -> p.getName().equals(newAttributeDef.getName())).findAny();
2039 if (newProperty.isPresent()) {
2040 result = Either.left(newProperty.get());
2042 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to find recently added property {} on the resource {}. Status is {}. ", newAttributeDef.getName(), component.getUniqueId(), StorageOperationStatus.NOT_FOUND);
2043 result = Either.right(StorageOperationStatus.NOT_FOUND);
2049 public Either<InputDefinition, StorageOperationStatus> updateInputOfComponent(Component component, InputDefinition newInputDefinition) {
2051 Either<Component, StorageOperationStatus> getUpdatedComponentRes = null;
2052 Either<InputDefinition, StorageOperationStatus> result = null;
2053 StorageOperationStatus status = getToscaElementOperation(component).updateToscaDataOfToscaElement(component.getUniqueId(), EdgeLabelEnum.INPUTS, VertexTypeEnum.INPUTS, newInputDefinition, JsonPresentationFields.NAME);
2054 if (status != StorageOperationStatus.OK) {
2055 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to update the input {} to the component {}. Status is {}. ", newInputDefinition.getName(), component.getName(), status);
2056 result = Either.right(status);
2058 if (result == null) {
2059 ComponentParametersView filter = new ComponentParametersView(true);
2060 filter.setIgnoreInputs(false);
2061 getUpdatedComponentRes = getToscaElement(component.getUniqueId(), filter);
2062 if (getUpdatedComponentRes.isRight()) {
2063 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to get updated resource {}. Status is {}. ", component.getUniqueId(), getUpdatedComponentRes.right().value());
2064 result = Either.right(status);
2067 if (result == null) {
2068 Optional<InputDefinition> updatedInput = getUpdatedComponentRes.left().value().getInputs().stream().filter(p -> p.getName().equals(newInputDefinition.getName())).findAny();
2069 if (updatedInput.isPresent()) {
2070 result = Either.left(updatedInput.get());
2072 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to find recently updated inputs {} on the resource {}. Status is {}. ", newInputDefinition.getName(), component.getUniqueId(), StorageOperationStatus.NOT_FOUND);
2073 result = Either.right(StorageOperationStatus.NOT_FOUND);
2079 public StorageOperationStatus addGroupInstancesToComponentInstance(Component containerComponent, ComponentInstance componentInstance, List<GroupDefinition> groups, Map<String, List<ArtifactDefinition>> groupInstancesArtifacts) {
2080 return nodeTemplateOperation.addGroupInstancesToComponentInstance(containerComponent, componentInstance, groups, groupInstancesArtifacts);
2083 public Either<List<GroupDefinition>, StorageOperationStatus> updateGroupsOnComponent(Component component, ComponentTypeEnum componentType, List<GroupDataDefinition> updatedGroups) {
2084 return groupsOperation.updateGroups(component, componentType, updatedGroups);
2087 public Either<List<GroupInstance>, StorageOperationStatus> updateGroupInstancesOnComponent(Component component, ComponentTypeEnum componentType, String instanceId, List<GroupInstance> updatedGroupInstances) {
2088 return groupsOperation.updateGroupInstances(component, componentType, instanceId, updatedGroupInstances);
2091 public StorageOperationStatus addGroupInstancesToComponentInstance(Component containerComponent, ComponentInstance componentInstance, List<GroupInstance> groupInstances) {
2092 return nodeTemplateOperation.addGroupInstancesToComponentInstance(containerComponent, componentInstance, groupInstances);
2095 public StorageOperationStatus addDeploymentArtifactsToComponentInstance(Component containerComponent, ComponentInstance componentInstance, Map<String, ArtifactDefinition> deploymentArtifacts) {
2096 return nodeTemplateOperation.addDeploymentArtifactsToComponentInstance(containerComponent, componentInstance, deploymentArtifacts);
2099 public StorageOperationStatus updateComponentInstanceProperty(Component containerComponent, String componentInstanceId, ComponentInstanceProperty property) {
2100 return nodeTemplateOperation.updateComponentInstanceProperty(containerComponent, componentInstanceId, property);
2103 public StorageOperationStatus addComponentInstanceProperty(Component containerComponent, String componentInstanceId, ComponentInstanceProperty property) {
2104 return nodeTemplateOperation.addComponentInstanceProperty(containerComponent, componentInstanceId, property);
2107 public StorageOperationStatus updateComponentInstanceInput(Component containerComponent, String componentInstanceId, ComponentInstanceInput property) {
2108 return nodeTemplateOperation.updateComponentInstanceInput(containerComponent, componentInstanceId, property);
2111 public StorageOperationStatus addComponentInstanceInput(Component containerComponent, String componentInstanceId, ComponentInstanceInput property) {
2112 return nodeTemplateOperation.addComponentInstanceInput(containerComponent, componentInstanceId, property);
2115 public void setNodeTypeOperation(NodeTypeOperation nodeTypeOperation) {
2116 this.nodeTypeOperation = nodeTypeOperation;
2119 public void setTopologyTemplateOperation(TopologyTemplateOperation topologyTemplateOperation) {
2120 this.topologyTemplateOperation = topologyTemplateOperation;