2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.be.model.jsontitan.operations;
24 import fj.data.Either;
25 import java.util.Map.Entry;
26 import java.util.stream.Collectors;
27 import org.apache.commons.collections.CollectionUtils;
28 import org.apache.commons.lang3.StringUtils;
29 import org.apache.commons.lang3.tuple.ImmutablePair;
30 import org.apache.commons.lang3.tuple.Pair;
31 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
32 import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
33 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
34 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
35 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
36 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
37 import org.openecomp.sdc.be.datatypes.components.ComponentMetadataDataDefinition;
38 import org.openecomp.sdc.be.datatypes.components.ResourceMetadataDataDefinition;
39 import org.openecomp.sdc.be.datatypes.elements.*;
40 import org.openecomp.sdc.be.datatypes.enums.*;
41 import org.openecomp.sdc.be.model.*;
42 import org.openecomp.sdc.be.model.jsontitan.datamodel.TopologyTemplate;
43 import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElement;
44 import org.openecomp.sdc.be.model.jsontitan.utils.ModelConverter;
45 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
46 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
47 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
48 import org.openecomp.sdc.be.resources.data.ComponentMetadataData;
49 import org.openecomp.sdc.be.utils.CommonBeUtils;
50 import org.openecomp.sdc.common.jsongraph.util.CommonUtility;
51 import org.openecomp.sdc.common.jsongraph.util.CommonUtility.LogLevelEnum;
52 import org.openecomp.sdc.common.util.ValidationUtils;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55 import org.springframework.beans.factory.annotation.Autowired;
58 import java.util.Map.Entry;
59 import java.util.stream.Collectors;
61 @org.springframework.stereotype.Component("tosca-operation-facade")
62 public class ToscaOperationFacade {
64 private NodeTypeOperation nodeTypeOperation;
66 private TopologyTemplateOperation topologyTemplateOperation;
68 private NodeTemplateOperation nodeTemplateOperation;
70 private GroupsOperation groupsOperation;
72 private TitanDao titanDao;
74 private static Logger log = LoggerFactory.getLogger(ToscaOperationFacade.class.getName());
76 public <T extends Component> Either<T, StorageOperationStatus> getToscaElement(String componentId) {
78 return getToscaElement(componentId, JsonParseFlagEnum.ParseAll);
82 public <T extends Component> Either<T, StorageOperationStatus> getToscaFullElement(String componentId) {
83 ComponentParametersView filters = new ComponentParametersView();
84 filters.setIgnoreCapabiltyProperties(false);
86 return getToscaElement(componentId, filters);
89 public <T extends Component> Either<T, StorageOperationStatus> getToscaElement(String componentId, ComponentParametersView filters) {
91 Either<GraphVertex, TitanOperationStatus> getVertexEither = titanDao.getVertexById(componentId, filters.detectParseFlag());
92 if (getVertexEither.isRight()) {
93 log.debug("Couldn't fetch component with and unique id {}, error: {}", componentId, getVertexEither.right().value());
94 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value()));
97 return getToscaElementByOperation(getVertexEither.left().value(), filters);
100 public <T extends Component> Either<T, StorageOperationStatus> getToscaElement(String componentId, JsonParseFlagEnum parseFlag) {
102 Either<GraphVertex, TitanOperationStatus> getVertexEither = titanDao.getVertexById(componentId, parseFlag);
103 if (getVertexEither.isRight()) {
104 log.debug("Couldn't fetch component with and unique id {}, error: {}", componentId, getVertexEither.right().value());
105 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value()));
108 return getToscaElementByOperation(getVertexEither.left().value());
111 public <T extends Component> Either<T, StorageOperationStatus> getToscaElement(GraphVertex componentVertex) {
112 return getToscaElementByOperation(componentVertex);
115 public Either<Boolean, StorageOperationStatus> validateComponentExists(String componentId) {
117 Either<GraphVertex, TitanOperationStatus> getVertexEither = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
118 if (getVertexEither.isRight()) {
119 TitanOperationStatus status = getVertexEither.right().value();
120 if (status == TitanOperationStatus.NOT_FOUND) {
121 return Either.left(false);
123 log.debug("Couldn't fetch component with and unique id {}, error: {}", componentId, getVertexEither.right().value());
124 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value()));
127 return Either.left(true);
130 public <T extends Component> Either<T, StorageOperationStatus> findLastCertifiedToscaElementByUUID(T component) {
131 Map<GraphPropertyEnum, Object> props = new HashMap<>();
132 props.put(GraphPropertyEnum.UUID, component.getUUID());
133 props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
134 props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
136 Either<List<GraphVertex>, TitanOperationStatus> getVertexEither = titanDao.getByCriteria(ModelConverter.getVertexType(component), props);
137 if (getVertexEither.isRight()) {
138 log.debug("Couldn't fetch component with and unique id {}, error: {}", component.getUniqueId(), getVertexEither.right().value());
139 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value()));
142 return getToscaElementByOperation(getVertexEither.left().value().get(0));
145 private <T extends Component> Either<T, StorageOperationStatus> getToscaElementByOperation(GraphVertex componentV) {
146 return getToscaElementByOperation(componentV, new ComponentParametersView());
149 private <T extends Component> Either<T, StorageOperationStatus> getToscaElementByOperation(GraphVertex componentV, ComponentParametersView filters) {
150 VertexTypeEnum label = componentV.getLabel();
152 ToscaElementOperation toscaOperation = getToscaElementOperation(componentV);
153 Either<ToscaElement, StorageOperationStatus> toscaElement;
154 String componentId = componentV.getUniqueId();
155 if (toscaOperation != null) {
156 log.debug("Need to fetch tosca element for id {}", componentId);
157 toscaElement = toscaOperation.getToscaElement(componentV, filters);
159 log.debug("not supported tosca type {} for id {}", label, componentId);
160 toscaElement = Either.right(StorageOperationStatus.BAD_REQUEST);
162 if (toscaElement.isRight()) {
163 return Either.right(toscaElement.right().value());
165 return Either.left(ModelConverter.convertFromToscaElement(toscaElement.left().value()));
168 private ToscaElementOperation getToscaElementOperation(GraphVertex componentV) {
169 VertexTypeEnum label = componentV.getLabel();
172 return nodeTypeOperation;
173 case TOPOLOGY_TEMPLATE:
174 return topologyTemplateOperation;
185 public <T extends Component> Either<T, StorageOperationStatus> createToscaComponent(T resource) {
186 ToscaElement toscaElement = ModelConverter.convertToToscaElement(resource);
188 ToscaElementOperation toscaElementOperation = getToscaElementOperation(resource);
189 Either<ToscaElement, StorageOperationStatus> createToscaElement = toscaElementOperation.createToscaElement(toscaElement);
190 if (createToscaElement.isLeft()) {
191 log.debug("Component created successfully!!!");
192 T dataModel = ModelConverter.convertFromToscaElement(createToscaElement.left().value());
193 return Either.left(dataModel);
195 return Either.right(createToscaElement.right().value());
200 * @param componentToDelete
203 public StorageOperationStatus markComponentToDelete(Component componentToDelete) {
205 if ((componentToDelete.getIsDeleted() != null) && componentToDelete.getIsDeleted() && !componentToDelete.isHighestVersion()) {
206 // component already marked for delete
207 return StorageOperationStatus.OK;
210 Either<GraphVertex, TitanOperationStatus> getResponse = titanDao.getVertexById(componentToDelete.getUniqueId(), JsonParseFlagEnum.ParseAll);
211 if (getResponse.isRight()) {
212 log.debug("Couldn't fetch component with and unique id {}, error: {}", componentToDelete.getUniqueId(), getResponse.right().value());
213 return DaoStatusConverter.convertTitanStatusToStorageStatus(getResponse.right().value());
216 GraphVertex componentV = getResponse.left().value();
218 // same operation for node type and topology template operations
219 Either<GraphVertex, StorageOperationStatus> result = nodeTypeOperation.markComponentToDelete(componentV);
220 if (result.isRight()) {
221 return result.right().value();
223 return StorageOperationStatus.OK;
232 public <T extends Component> Either<T, StorageOperationStatus> deleteToscaComponent(String componentId) {
234 Either<GraphVertex, TitanOperationStatus> getVertexEither = titanDao.getVertexById(componentId, JsonParseFlagEnum.ParseAll);
235 if (getVertexEither.isRight()) {
236 log.debug("Couldn't fetch component vertex with and unique id {}, error: {}", componentId, getVertexEither.right().value());
237 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value()));
240 Either<ToscaElement, StorageOperationStatus> deleteElement = deleteToscaElement(getVertexEither.left().value());
241 if (deleteElement.isRight()) {
242 log.debug("Failed to delete component with and unique id {}, error: {}", componentId, deleteElement.right().value());
243 return Either.right(deleteElement.right().value());
245 T dataModel = ModelConverter.convertFromToscaElement(deleteElement.left().value());
247 return Either.left(dataModel);
250 private Either<ToscaElement, StorageOperationStatus> deleteToscaElement(GraphVertex componentV) {
251 VertexTypeEnum label = componentV.getLabel();
252 Either<ToscaElement, StorageOperationStatus> toscaElement;
253 Object componentId = componentV.getUniqueId();
256 log.debug("Need to fetch node type for id {}", componentId);
257 toscaElement = nodeTypeOperation.deleteToscaElement(componentV);
259 case TOPOLOGY_TEMPLATE:
260 log.debug("Need to fetch topology template for id {}", componentId);
261 toscaElement = topologyTemplateOperation.deleteToscaElement(componentV);
264 log.debug("not supported tosca type {} for id {}", label, componentId);
265 toscaElement = Either.right(StorageOperationStatus.BAD_REQUEST);
271 private ToscaElementOperation getToscaElementOperation(Component component) {
272 return ModelConverter.isAtomicComponent(component) ? nodeTypeOperation : topologyTemplateOperation;
275 public <T extends Component> Either<T, StorageOperationStatus> getLatestByToscaResourceName(String toscaResourceName) {
276 return getLatestByName(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName);
280 public <T extends Component> Either<T, StorageOperationStatus> getLatestByName(String resourceName) {
281 return getLatestByName(GraphPropertyEnum.NAME, resourceName);
285 public Either<Integer, StorageOperationStatus> validateCsarUuidUniqueness(String csarUUID) {
286 Either<List<ToscaElement>, StorageOperationStatus> byCsar = null;
288 Map<GraphPropertyEnum, Object> properties = new HashMap<GraphPropertyEnum, Object>();
289 properties.put(GraphPropertyEnum.CSAR_UUID, csarUUID);
291 Either<List<GraphVertex>, TitanOperationStatus> resources = titanDao.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata);
293 if (resources.isRight()) {
294 if (resources.right().value() == TitanOperationStatus.NOT_FOUND) {
295 return Either.left(new Integer(0));
297 log.debug("failed to get resources from graph with property name: {}", csarUUID);
298 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(resources.right().value()));
302 List<GraphVertex> resourceList = (resources.isLeft() ? resources.left().value() : null);
304 return Either.left(new Integer(resourceList.size()));
308 public <T extends Component> Either<Set<T>, StorageOperationStatus> getFollowed(String userId, Set<LifecycleStateEnum> lifecycleStates, Set<LifecycleStateEnum> lastStateStates, ComponentTypeEnum componentType) {
309 Either<List<ToscaElement>, StorageOperationStatus> followedResources;
310 if (componentType == ComponentTypeEnum.RESOURCE) {
311 followedResources = nodeTypeOperation.getFollowedComponent(userId, lifecycleStates, lastStateStates, componentType);
313 followedResources = topologyTemplateOperation.getFollowedComponent(userId, lifecycleStates, lastStateStates, componentType);
316 Set<T> components = new HashSet<>();
317 if (followedResources.isRight() && followedResources.right().value() != StorageOperationStatus.NOT_FOUND) {
318 return Either.right(followedResources.right().value());
320 if (followedResources.isLeft()) {
321 List<ToscaElement> toscaElements = followedResources.left().value();
322 toscaElements.forEach(te -> {
323 T component = ModelConverter.convertFromToscaElement(te);
324 components.add(component);
327 return Either.left(components);
330 public Either<Resource, StorageOperationStatus> getLatestCertifiedNodeTypeByToscaResourceName(String toscaResourceName) {
332 return getLatestCertifiedByToscaResourceName(toscaResourceName, VertexTypeEnum.NODE_TYPE, JsonParseFlagEnum.ParseMetadata);
335 public Either<Resource, StorageOperationStatus> getLatestCertifiedByToscaResourceName(String toscaResourceName, VertexTypeEnum vertexType, JsonParseFlagEnum parseFlag) {
337 Either<Resource, StorageOperationStatus> result = null;
338 Map<GraphPropertyEnum, Object> props = new HashMap<GraphPropertyEnum, Object>();
339 props.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName);
340 props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
341 props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
342 Either<List<GraphVertex>, TitanOperationStatus> getLatestRes = titanDao.getByCriteria(vertexType, props, parseFlag);
344 if (getLatestRes.isRight()) {
345 TitanOperationStatus status = getLatestRes.right().value();
346 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch {} with name {}. status={} ", vertexType, toscaResourceName, status);
347 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
349 if (result == null) {
350 List<GraphVertex> resources = getLatestRes.left().value();
351 double version = 0.0;
352 GraphVertex highestResource = null;
353 for (GraphVertex resource : resources) {
354 double resourceVersion = Double.parseDouble((String) resource.getJsonMetadataField(JsonPresentationFields.VERSION));
355 if (resourceVersion > version) {
356 version = resourceVersion;
357 highestResource = resource;
360 result = getToscaElement(highestResource.getUniqueId());
365 public Either<Boolean, StorageOperationStatus> validateToscaResourceNameExists(String templateName) {
366 Either<Boolean, StorageOperationStatus> validateUniquenessRes = validateToscaResourceNameUniqueness(templateName);
367 if (validateUniquenessRes.isLeft()) {
368 return Either.left(!validateUniquenessRes.left().value());
370 return validateUniquenessRes;
373 public Either<RequirementCapabilityRelDef, StorageOperationStatus> dissociateResourceInstances(String componentId, RequirementCapabilityRelDef requirementDef) {
374 return nodeTemplateOperation.dissociateResourceInstances(componentId, requirementDef);
378 public StorageOperationStatus associateResourceInstances(String componentId, List<RequirementCapabilityRelDef> relations) {
379 Either<List<RequirementCapabilityRelDef>, StorageOperationStatus> status = nodeTemplateOperation.associateResourceInstances(componentId, relations);
380 if (status.isRight()) {
381 return status.right().value();
383 return StorageOperationStatus.OK;
386 protected Either<Boolean, StorageOperationStatus> validateToscaResourceNameUniqueness(String name) {
388 Map<GraphPropertyEnum, Object> properties = new HashMap<GraphPropertyEnum, Object>();
389 properties.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, name);
391 Either<List<GraphVertex>, TitanOperationStatus> resources = titanDao.getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata);
393 if (resources.isRight() && resources.right().value() != TitanOperationStatus.NOT_FOUND) {
394 log.debug("failed to get resources from graph with property name: {}", name);
395 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(resources.right().value()));
397 List<GraphVertex> resourceList = (resources.isLeft() ? resources.left().value() : null);
398 if (resourceList != null && resourceList.size() > 0) {
399 if (log.isDebugEnabled()) {
400 StringBuilder builder = new StringBuilder();
401 for (GraphVertex resourceData : resourceList) {
402 builder.append(resourceData.getUniqueId() + "|");
404 log.debug("resources with property name:{} exists in graph. found {}", name, builder.toString());
406 return Either.left(false);
408 log.debug("resources with property name:{} does not exists in graph", name);
409 return Either.left(true);
416 * @param newComponent
417 * @param oldComponent
420 public <T extends Component> Either<T, StorageOperationStatus> overrideComponent(T newComponent, T oldComponent) {
423 // newComponent.setInterfaces(oldComponent.getInterfaces);
424 newComponent.setArtifacts(oldComponent.getArtifacts());
425 newComponent.setDeploymentArtifacts(oldComponent.getDeploymentArtifacts());
426 newComponent.setGroups(oldComponent.getGroups());
427 newComponent.setInputs(null);
428 newComponent.setLastUpdateDate(null);
429 newComponent.setHighestVersion(true);
431 Either<GraphVertex, TitanOperationStatus> componentVEither = titanDao.getVertexById(oldComponent.getUniqueId(), JsonParseFlagEnum.NoParse);
432 if (componentVEither.isRight()) {
433 log.debug("Falied to fetch component {} error {}", oldComponent.getUniqueId(), componentVEither.right().value());
434 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(componentVEither.right().value()));
436 GraphVertex componentv = componentVEither.left().value();
437 Either<GraphVertex, TitanOperationStatus> parentVertexEither = titanDao.getParentVertex(componentv, EdgeLabelEnum.VERSION, JsonParseFlagEnum.NoParse);
438 if (parentVertexEither.isRight() && parentVertexEither.right().value() != TitanOperationStatus.NOT_FOUND) {
439 log.debug("Falied to fetch parent version for component {} error {}", oldComponent.getUniqueId(), parentVertexEither.right().value());
440 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(parentVertexEither.right().value()));
443 Either<ToscaElement, StorageOperationStatus> deleteToscaComponent = deleteToscaElement(componentv);
444 if (deleteToscaComponent.isRight()) {
445 log.debug("Falied to remove old component {} error {}", oldComponent.getUniqueId(), deleteToscaComponent.right().value());
446 return Either.right(deleteToscaComponent.right().value());
448 Either<T, StorageOperationStatus> createToscaComponent = createToscaComponent(newComponent);
449 if (createToscaComponent.isRight()) {
450 log.debug("Falied to create tosca element component {} error {}", newComponent.getUniqueId(), createToscaComponent.right().value());
451 return Either.right(createToscaComponent.right().value());
453 T newElement = createToscaComponent.left().value();
454 Either<GraphVertex, TitanOperationStatus> newVersionEither = titanDao.getVertexById(newElement.getUniqueId(), JsonParseFlagEnum.NoParse);
455 if (newVersionEither.isRight()) {
456 log.debug("Falied to fetch new tosca element component {} error {}", newComponent.getUniqueId(), newVersionEither.right().value());
457 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(newVersionEither.right().value()));
459 if (parentVertexEither.isLeft()) {
460 GraphVertex previousVersionV = parentVertexEither.left().value();
461 TitanOperationStatus createEdge = titanDao.createEdge(previousVersionV, newVersionEither.left().value(), EdgeLabelEnum.VERSION, null);
462 if (createEdge != TitanOperationStatus.OK) {
463 log.debug("Falied to associate to previous version {} new version {} error {}", previousVersionV.getUniqueId(), newVersionEither.right().value(), createEdge);
464 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(createEdge));
467 return Either.left(newElement);
472 * @param componentToUpdate
475 public <T extends Component> Either<T, StorageOperationStatus> updateToscaElement(T componentToUpdate) {
476 return updateToscaElement(componentToUpdate, new ComponentParametersView());
481 * @param componentToUpdate
483 * @param filterResult
486 public <T extends Component> Either<T, StorageOperationStatus> updateToscaElement(T componentToUpdate, ComponentParametersView filterResult) {
487 String componentId = componentToUpdate.getUniqueId();
488 Either<GraphVertex, TitanOperationStatus> getVertexEither = titanDao.getVertexById(componentId, JsonParseFlagEnum.ParseAll);
489 if (getVertexEither.isRight()) {
490 log.debug("Couldn't fetch component with and unique id {}, error: {}", componentId, getVertexEither.right().value());
491 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value()));
493 GraphVertex elementV = getVertexEither.left().value();
494 ToscaElementOperation toscaElementOperation = getToscaElementOperation(elementV);
496 ToscaElement toscaElementToUpdate = ModelConverter.convertToToscaElement(componentToUpdate);
497 Either<ToscaElement, StorageOperationStatus> updateToscaElement = toscaElementOperation.updateToscaElement(toscaElementToUpdate, elementV, filterResult);
498 if (updateToscaElement.isRight()) {
499 log.debug("Failed to update tosca element {} error {}", componentId, updateToscaElement.right().value());
500 return Either.right(updateToscaElement.right().value());
502 return Either.left(ModelConverter.convertFromToscaElement(updateToscaElement.left().value()));
505 private <T extends Component> Either<T, StorageOperationStatus> getLatestByName(GraphPropertyEnum property, String nodeName) {
506 Either<T, StorageOperationStatus> result;
508 Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
509 Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
511 propertiesToMatch.put(property, nodeName);
512 propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
514 propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
516 Either<List<GraphVertex>, TitanOperationStatus> highestResources = titanDao.getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseMetadata);
517 if (highestResources.isRight()) {
518 TitanOperationStatus status = highestResources.right().value();
519 log.debug("failed to find resource with name {}. status={} ", nodeName, status);
520 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
524 List<GraphVertex> resources = highestResources.left().value();
525 double version = 0.0;
526 GraphVertex highestResource = null;
527 for (GraphVertex vertex : resources) {
528 Object versionObj = vertex.getMetadataProperty(GraphPropertyEnum.VERSION);
529 double resourceVersion = Double.valueOf((String) versionObj);
530 if (resourceVersion > version) {
531 version = resourceVersion;
532 highestResource = vertex;
535 return getToscaElementByOperation(highestResource);
538 public <T extends Component> Either<List<T>, StorageOperationStatus> getBySystemName(ComponentTypeEnum componentType, String systemName) {
540 Either<List<T>, StorageOperationStatus> result = null;
541 Either<T, StorageOperationStatus> getComponentRes;
542 List<T> components = new ArrayList<>();
543 List<GraphVertex> componentVertices;
544 Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
545 Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
547 propertiesToMatch.put(GraphPropertyEnum.SYSTEM_NAME, systemName);
548 if (componentType != null)
549 propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
551 propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
553 Either<List<GraphVertex>, TitanOperationStatus> getComponentsRes = titanDao.getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll);
554 if (getComponentsRes.isRight()) {
555 TitanOperationStatus status = getComponentsRes.right().value();
556 log.debug("Failed to fetch the component with system name {}. Status is {} ", systemName, status);
557 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
559 if (result == null) {
560 componentVertices = getComponentsRes.left().value();
561 for (GraphVertex componentVertex : componentVertices) {
562 getComponentRes = getToscaElementByOperation(componentVertex);
563 if (getComponentRes.isRight()) {
564 log.debug("Failed to get the component {}. Status is {} ", componentVertex.getJsonMetadataField(JsonPresentationFields.NAME), getComponentRes.right().value());
565 result = Either.right(getComponentRes.right().value());
568 T componentBySystemName = getComponentRes.left().value();
569 log.debug("Found component, id: {}", componentBySystemName.getUniqueId());
570 components.add(componentBySystemName);
573 if (result == null) {
574 result = Either.left(components);
579 public <T extends Component> Either<T, StorageOperationStatus> getComponentByNameAndVersion(ComponentTypeEnum componentType, String name, String version) {
580 return getComponentByNameAndVersion(componentType, name, version, JsonParseFlagEnum.ParseAll);
583 public <T extends Component> Either<T, StorageOperationStatus> getComponentByNameAndVersion(ComponentTypeEnum componentType, String name, String version, JsonParseFlagEnum parseFlag) {
584 Either<T, StorageOperationStatus> result;
586 Map<GraphPropertyEnum, Object> hasProperties = new EnumMap<>(GraphPropertyEnum.class);
587 Map<GraphPropertyEnum, Object> hasNotProperties = new EnumMap<>(GraphPropertyEnum.class);
589 hasProperties.put(GraphPropertyEnum.NAME, name);
590 hasProperties.put(GraphPropertyEnum.VERSION, version);
591 hasNotProperties.put(GraphPropertyEnum.IS_DELETED, true);
592 if (componentType != null) {
593 hasProperties.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
595 Either<List<GraphVertex>, TitanOperationStatus> getResourceRes = titanDao.getByCriteria(null, hasProperties, hasNotProperties, parseFlag);
596 if (getResourceRes.isRight()) {
597 TitanOperationStatus status = getResourceRes.right().value();
598 log.debug("failed to find resource with name {}, version {}. Status is {} ", name, version, status);
599 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
602 return getToscaElementByOperation(getResourceRes.left().value().get(0));
605 public <T extends Component> Either<List<T>, StorageOperationStatus> getCatalogComponents(ComponentTypeEnum componentType, List<OriginTypeEnum> excludeTypes, boolean isHighestVersions) {
606 List<T> components = new ArrayList<>();
607 Either<List<ToscaElement>, StorageOperationStatus> catalogDataResult;
608 List<ToscaElement> toscaElements = new ArrayList<>();
609 List<ResourceTypeEnum> excludedResourceTypes =
610 Optional.ofNullable(excludeTypes).orElse(Collections.emptyList())
612 .filter(type -> !type.equals(OriginTypeEnum.SERVICE))
613 .map(type -> ResourceTypeEnum.getTypeByName(type.name())).collect(Collectors.toList());
615 switch (componentType) {
617 catalogDataResult = nodeTypeOperation.getElementCatalogData(ComponentTypeEnum.RESOURCE,excludedResourceTypes , isHighestVersions);
618 if (catalogDataResult.isRight()) {
619 return Either.right(catalogDataResult.right().value());
621 toscaElements = catalogDataResult.left().value();
624 if (excludeTypes!= null && excludeTypes.contains(OriginTypeEnum.SERVICE)) {
627 catalogDataResult = topologyTemplateOperation.getElementCatalogData(ComponentTypeEnum.SERVICE, null, isHighestVersions);
628 if (catalogDataResult.isRight()) {
629 return Either.right(catalogDataResult.right().value());
631 toscaElements = catalogDataResult.left().value();
634 log.debug("Not supported component type {}", componentType);
635 return Either.right(StorageOperationStatus.BAD_REQUEST);
637 toscaElements.forEach(te -> {
638 T component = ModelConverter.convertFromToscaElement(te);
639 components.add(component);
641 return Either.left(components);
644 public Either<List<String>, StorageOperationStatus> deleteMarkedElements(ComponentTypeEnum componentType) {
645 Either<List<GraphVertex>, StorageOperationStatus> allComponentsMarkedForDeletion;
646 List<String> deleted = new ArrayList<>();
647 switch (componentType) {
649 allComponentsMarkedForDeletion = nodeTypeOperation.getAllComponentsMarkedForDeletion(componentType);
653 allComponentsMarkedForDeletion = topologyTemplateOperation.getAllComponentsMarkedForDeletion(componentType);
656 log.debug("Not supported component type {}", componentType);
657 return Either.right(StorageOperationStatus.BAD_REQUEST);
659 if (allComponentsMarkedForDeletion.isRight()) {
660 return Either.right(allComponentsMarkedForDeletion.right().value());
662 List<GraphVertex> allMarked = allComponentsMarkedForDeletion.left().value();
664 Either<List<GraphVertex>, TitanOperationStatus> allNotDeletedElements = topologyTemplateOperation.getAllNotDeletedElements();
665 if (allNotDeletedElements.isRight()) {
666 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(allNotDeletedElements.right().value()));
668 List<GraphVertex> allNonMarked = allNotDeletedElements.left().value();
669 for (GraphVertex elementV : allMarked) {
670 if (topologyTemplateOperation.isInUse(elementV, allNonMarked) == false) {
671 Either<ToscaElement, StorageOperationStatus> deleteToscaElement = deleteToscaElement(elementV);
672 if (deleteToscaElement.isRight()) {
673 log.debug("Failed to delete marked element {} error {}", elementV.getUniqueId(), deleteToscaElement.right().value());
676 deleted.add(elementV.getUniqueId());
677 log.debug("Marked element {} in use. don't delete it", elementV.getUniqueId());
680 return Either.left(deleted);
683 public Either<List<String>, StorageOperationStatus> getAllComponentsMarkedForDeletion(ComponentTypeEnum componentType) {
684 Either<List<GraphVertex>, StorageOperationStatus> allComponentsMarkedForDeletion;
685 switch (componentType) {
687 allComponentsMarkedForDeletion = nodeTypeOperation.getAllComponentsMarkedForDeletion(componentType);
691 allComponentsMarkedForDeletion = topologyTemplateOperation.getAllComponentsMarkedForDeletion(componentType);
694 log.debug("Not supported component type {}", componentType);
695 return Either.right(StorageOperationStatus.BAD_REQUEST);
697 if (allComponentsMarkedForDeletion.isRight()) {
698 return Either.right(allComponentsMarkedForDeletion.right().value());
700 return Either.left(allComponentsMarkedForDeletion.left().value().stream().map(v -> v.getUniqueId()).collect(Collectors.toList()));
703 public Either<Boolean, StorageOperationStatus> isComponentInUse(String componentId) {
704 Either<Boolean, StorageOperationStatus> result;
705 Either<List<GraphVertex>, TitanOperationStatus> allNotDeletedElements = topologyTemplateOperation.getAllNotDeletedElements();
706 if (allNotDeletedElements.isRight()) {
707 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(allNotDeletedElements.right().value()));
709 result = Either.left(topologyTemplateOperation.isInUse(componentId, allNotDeletedElements.left().value()));
714 public Either<ImmutablePair<Component, String>, StorageOperationStatus> addComponentInstanceToTopologyTemplate(Component containerComponent, Component origComponent, ComponentInstance componentInstance, boolean allowDeleted, User user) {
716 Either<ImmutablePair<Component, String>, StorageOperationStatus> result = null;
717 Either<ToscaElement, StorageOperationStatus> updateContainerComponentRes = null;
718 componentInstance.setIcon(origComponent.getIcon());
719 Either<ImmutablePair<TopologyTemplate, String>, StorageOperationStatus> addResult = nodeTemplateOperation.addComponentInstanceToTopologyTemplate(ModelConverter.convertToToscaElement(containerComponent),
720 ModelConverter.convertToToscaElement(origComponent), getNextComponentInstanceCounter(containerComponent, origComponent.getName()), componentInstance, allowDeleted, user);
722 if (addResult.isRight()) {
723 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to add the component instance {} to container component {}. ", componentInstance.getName(), containerComponent.getName());
724 result = Either.right(addResult.right().value());
726 if (result == null) {
727 updateContainerComponentRes = topologyTemplateOperation.getToscaElement(containerComponent.getUniqueId());
728 if (updateContainerComponentRes.isRight()) {
729 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch updated topology template {} with updated component instance {}. ", containerComponent.getName(), componentInstance.getName());
730 result = Either.right(updateContainerComponentRes.right().value());
733 if (result == null) {
734 Component updatedComponent = ModelConverter.convertFromToscaElement(updateContainerComponentRes.left().value());
735 String createdInstanceId = addResult.left().value().getRight();
736 CommonUtility.addRecordToLog(log, LogLevelEnum.TRACE, "The component instance {} has been added to container component {}. ", createdInstanceId, updatedComponent.getName());
737 result = Either.left(new ImmutablePair<>(updatedComponent, createdInstanceId));
742 public StorageOperationStatus associateComponentInstancesToComponent(Component containerComponent, Map<ComponentInstance, Resource> resourcesInstancesMap, boolean allowDeleted) {
744 StorageOperationStatus result = null;
745 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Going to add component instances to component {}", containerComponent.getUniqueId());
747 Either<GraphVertex, TitanOperationStatus> metadataVertex = titanDao.getVertexById(containerComponent.getUniqueId(), JsonParseFlagEnum.ParseAll);
748 if (metadataVertex.isRight()) {
749 TitanOperationStatus status = metadataVertex.right().value();
750 if (status == TitanOperationStatus.NOT_FOUND) {
751 status = TitanOperationStatus.INVALID_ID;
753 result = DaoStatusConverter.convertTitanStatusToStorageStatus(status);
755 if (result == null) {
756 result = nodeTemplateOperation.associateComponentInstancesToComponent(containerComponent, resourcesInstancesMap, metadataVertex.left().value(), allowDeleted);
761 public Either<ImmutablePair<Component, String>, StorageOperationStatus> updateComponentInstanceMetadataOfTopologyTemplate(Component containerComponent, Component origComponent, ComponentInstance componentInstance) {
763 Either<ImmutablePair<Component, String>, StorageOperationStatus> result = null;
765 CommonUtility.addRecordToLog(log, LogLevelEnum.TRACE, "Going to update the metadata of the component instance {} belonging to container component {}. ", componentInstance.getName(), containerComponent.getName());
766 componentInstance.setIcon(origComponent.getIcon());
767 Either<ImmutablePair<TopologyTemplate, String>, StorageOperationStatus> updateResult = nodeTemplateOperation.updateComponentInstanceMetadataOfTopologyTemplate(ModelConverter.convertToToscaElement(containerComponent),
768 ModelConverter.convertToToscaElement(origComponent), componentInstance);
769 if (updateResult.isRight()) {
770 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to update the metadata of the component instance {} belonging to container component {}. ", componentInstance.getName(), containerComponent.getName());
771 result = Either.right(updateResult.right().value());
773 if (result == null) {
774 Component updatedComponent = ModelConverter.convertFromToscaElement(updateResult.left().value().getLeft());
775 String createdInstanceId = updateResult.left().value().getRight();
776 CommonUtility.addRecordToLog(log, LogLevelEnum.TRACE, "The metadata of the component instance {} has been updated to container component {}. ", createdInstanceId, updatedComponent.getName());
777 result = Either.left(new ImmutablePair<>(updatedComponent, createdInstanceId));
782 public Either<Component, StorageOperationStatus> updateComponentInstanceMetadataOfTopologyTemplate(Component containerComponent) {
784 Either<Component, StorageOperationStatus> result = null;
786 CommonUtility.addRecordToLog(log, LogLevelEnum.TRACE, "Going to update the metadata belonging to container component {}. ", containerComponent.getName());
788 Either<TopologyTemplate, StorageOperationStatus> updateResult = nodeTemplateOperation.updateComponentInstanceMetadataOfTopologyTemplate(ModelConverter.convertToToscaElement(containerComponent));
789 if (updateResult.isRight()) {
790 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to update the metadata belonging to container component {}. ", containerComponent.getName());
791 result = Either.right(updateResult.right().value());
793 if (result == null) {
794 Component updatedComponent = ModelConverter.convertFromToscaElement(updateResult.left().value());
795 CommonUtility.addRecordToLog(log, LogLevelEnum.TRACE, "The metadata has been updated to container component {}. ", updatedComponent.getName());
796 result = Either.left(updatedComponent);
801 public Either<ImmutablePair<Component, String>, StorageOperationStatus> deleteComponentInstanceFromTopologyTemplate(Component containerComponent, String resourceInstanceId) {
803 Either<ImmutablePair<Component, String>, StorageOperationStatus> result = null;
805 CommonUtility.addRecordToLog(log, LogLevelEnum.TRACE, "Going to delete the component instance {} belonging to container component {}. ", resourceInstanceId, containerComponent.getName());
807 Either<ImmutablePair<TopologyTemplate, String>, StorageOperationStatus> updateResult = nodeTemplateOperation.deleteComponentInstanceFromTopologyTemplate(ModelConverter.convertToToscaElement(containerComponent), resourceInstanceId);
808 if (updateResult.isRight()) {
809 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to delete the component instance {} belonging to container component {}. ", resourceInstanceId, containerComponent.getName());
810 result = Either.right(updateResult.right().value());
812 if (result == null) {
813 Component updatedComponent = ModelConverter.convertFromToscaElement(updateResult.left().value().getLeft());
814 String deletedInstanceId = updateResult.left().value().getRight();
815 CommonUtility.addRecordToLog(log, LogLevelEnum.TRACE, "The component instance {} has been deleted from container component {}. ", deletedInstanceId, updatedComponent.getName());
816 result = Either.left(new ImmutablePair<>(updatedComponent, deletedInstanceId));
821 private String getNextComponentInstanceCounter(Component containerComponent, String originResourceName) {
823 Integer nextCounter = 0;
825 if (CollectionUtils.isNotEmpty(containerComponent.getComponentInstances())) {
827 String normalizedName = ValidationUtils.normalizeComponentInstanceName(originResourceName);
828 Integer maxCounterFromNames = getMaxCounterFromNames(containerComponent, normalizedName);
829 Integer maxCounterFromIds = getMaxCounterFromIds(containerComponent, normalizedName);
831 if (maxCounterFromNames == null && maxCounterFromIds != null) {
832 nextCounter = maxCounterFromIds + 1;
833 } else if (maxCounterFromIds == null && maxCounterFromNames != null) {
834 nextCounter = maxCounterFromNames + 1;
835 } else if (maxCounterFromIds != null && maxCounterFromNames != null) {
836 nextCounter = maxCounterFromNames > maxCounterFromIds ? maxCounterFromNames + 1 : maxCounterFromIds + 1;
839 return nextCounter.toString();
842 private Integer getMaxCounterFromNames(Component containerComponent, String normalizedName) {
844 Integer maxCounter = 0;
845 List<String> countersStr = containerComponent.getComponentInstances().stream().filter(ci -> ci.getNormalizedName() != null && ci.getNormalizedName().startsWith(normalizedName)).map(ci -> ci.getNormalizedName().split(normalizedName)[1])
846 .collect(Collectors.toList());
848 if (CollectionUtils.isEmpty(countersStr)) {
851 Integer currCounter = null;
852 for (String counter : countersStr) {
853 if (StringUtils.isEmpty(counter)) {
857 currCounter = Integer.parseInt(counter);
858 } catch (Exception e) {
861 maxCounter = maxCounter < currCounter ? currCounter : maxCounter;
863 if (currCounter == null) {
869 private Integer getMaxCounterFromIds(Component containerComponent, String normalizedName) {
871 Integer maxCounter = 0;
872 List<String> countersStr = containerComponent.getComponentInstances().stream().filter(ci -> ci.getUniqueId() != null && ci.getUniqueId().contains(normalizedName)).map(ci -> ci.getUniqueId().split(normalizedName)[1])
873 .collect(Collectors.toList());
875 if (CollectionUtils.isEmpty(countersStr)) {
878 Integer currCounter = null;
879 for (String counter : countersStr) {
880 if (StringUtils.isEmpty(counter)) {
884 currCounter = Integer.parseInt(counter);
885 } catch (Exception e) {
888 maxCounter = maxCounter < currCounter ? currCounter : maxCounter;
890 if (currCounter == null) {
896 public Either<RequirementCapabilityRelDef, StorageOperationStatus> associateResourceInstances(String componentId, RequirementCapabilityRelDef requirementDef) {
897 return nodeTemplateOperation.associateResourceInstances(componentId, requirementDef);
901 public Either<List<InputDefinition>, StorageOperationStatus> createAndAssociateInputs(Map<String, InputDefinition> inputs, String componentId) {
903 Either<GraphVertex, TitanOperationStatus> getVertexEither = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
904 if (getVertexEither.isRight()) {
905 log.debug("Couldn't fetch component with and unique id {}, error: {}", componentId, getVertexEither.right().value());
906 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value()));
910 GraphVertex vertex = getVertexEither.left().value();
911 Map<String, PropertyDataDefinition> inputsMap = inputs.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> new PropertyDataDefinition(e.getValue())));
913 StorageOperationStatus status = topologyTemplateOperation.associateInputsToComponent(vertex, inputsMap, componentId);
915 if (StorageOperationStatus.OK == status) {
916 log.debug("Component created successfully!!!");
917 List<InputDefinition> inputsResList = null;
918 if (inputsMap != null && !inputsMap.isEmpty()) {
919 inputsResList = inputsMap.values().stream().map(i -> new InputDefinition(i)).collect(Collectors.toList());
921 return Either.left(inputsResList);
923 return Either.right(status);
927 public Either<List<InputDefinition>, StorageOperationStatus> addInputsToComponent(Map<String, InputDefinition> inputs, String componentId) {
929 Either<GraphVertex, TitanOperationStatus> getVertexEither = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
930 if (getVertexEither.isRight()) {
931 log.debug("Couldn't fetch component with and unique id {}, error: {}", componentId, getVertexEither.right().value());
932 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value()));
936 GraphVertex vertex = getVertexEither.left().value();
937 Map<String, PropertyDataDefinition> inputsMap = inputs.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> new PropertyDataDefinition(e.getValue())));
939 StorageOperationStatus status = topologyTemplateOperation.addToscaDataToToscaElement(vertex, EdgeLabelEnum.INPUTS, VertexTypeEnum.INPUTS, inputsMap, JsonPresentationFields.NAME);
941 if (StorageOperationStatus.OK == status) {
942 log.debug("Component created successfully!!!");
943 List<InputDefinition> inputsResList = null;
944 if (inputsMap != null && !inputsMap.isEmpty()) {
945 inputsResList = inputsMap.values().stream().map(i -> new InputDefinition(i)).collect(Collectors.toList());
947 return Either.left(inputsResList);
949 return Either.right(status);
953 public Either<Map<String, List<ComponentInstanceProperty>>, StorageOperationStatus> associateComponentInstancePropertiesToComponent(Map<String, List<ComponentInstanceProperty>> instProperties, String componentId) {
955 Either<GraphVertex, TitanOperationStatus> getVertexEither = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
956 if (getVertexEither.isRight()) {
957 log.debug("Couldn't fetch component with and unique id {}, error: {}", componentId, getVertexEither.right().value());
958 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value()));
962 GraphVertex vertex = getVertexEither.left().value();
963 Map<String, MapPropertiesDataDefinition> instPropsMap = new HashMap<>();
964 if (instProperties != null) {
966 MapPropertiesDataDefinition propertiesMap;
967 for (Entry<String, List<ComponentInstanceProperty>> entry : instProperties.entrySet()) {
968 propertiesMap = new MapPropertiesDataDefinition();
970 propertiesMap.setMapToscaDataDefinition(entry.getValue().stream().map(e -> new PropertyDataDefinition(e)).collect(Collectors.toMap(e -> e.getName(), e -> e)));
972 instPropsMap.put(entry.getKey(), propertiesMap);
976 StorageOperationStatus status = topologyTemplateOperation.associateInstPropertiesToComponent(vertex, instPropsMap);
978 if (StorageOperationStatus.OK == status) {
979 log.debug("Component created successfully!!!");
980 return Either.left(instProperties);
982 return Either.right(status);
985 public Either<Map<String, List<ComponentInstanceInput>>, StorageOperationStatus> associateComponentInstanceInputsToComponent(Map<String, List<ComponentInstanceInput>> instInputs, String componentId) {
987 Either<GraphVertex, TitanOperationStatus> getVertexEither = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
988 if (getVertexEither.isRight()) {
989 log.debug("Couldn't fetch component with and unique id {}, error: {}", componentId, getVertexEither.right().value());
990 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value()));
993 GraphVertex vertex = getVertexEither.left().value();
994 Map<String, MapPropertiesDataDefinition> instPropsMap = new HashMap<>();
995 if (instInputs != null) {
997 MapPropertiesDataDefinition propertiesMap;
998 for (Entry<String, List<ComponentInstanceInput>> entry : instInputs.entrySet()) {
999 propertiesMap = new MapPropertiesDataDefinition();
1001 propertiesMap.setMapToscaDataDefinition(entry.getValue().stream().map(e -> new PropertyDataDefinition(e)).collect(Collectors.toMap(e -> e.getName(), e -> e)));
1003 instPropsMap.put(entry.getKey(), propertiesMap);
1007 StorageOperationStatus status = topologyTemplateOperation.associateInstInputsToComponent(vertex, instPropsMap);
1009 if (StorageOperationStatus.OK == status) {
1010 log.debug("Component created successfully!!!");
1011 return Either.left(instInputs);
1013 return Either.right(status);
1016 public Either<Map<String, List<ComponentInstanceInput>>, StorageOperationStatus> addComponentInstanceInputsToComponent(Component containerComponent, Map<String, List<ComponentInstanceInput>> instProperties) {
1018 StorageOperationStatus status = StorageOperationStatus.OK;
1019 if (instProperties != null) {
1021 for (Entry<String, List<ComponentInstanceInput>> entry : instProperties.entrySet()) {
1022 List<ComponentInstanceInput> props = entry.getValue();
1023 String componentInstanseId = entry.getKey();
1024 if (props != null && !props.isEmpty()) {
1025 for (ComponentInstanceInput property : props) {
1026 List<ComponentInstanceInput> componentInstancesInputs = containerComponent.getComponentInstancesInputs().get(componentInstanseId);
1027 Optional<ComponentInstanceInput> instanceProperty = componentInstancesInputs.stream().filter(p -> p.getName().equals(property.getName())).findAny();
1028 if (instanceProperty.isPresent()) {
1029 status = updateComponentInstanceInput(containerComponent, componentInstanseId, property);
1031 status = addComponentInstanceInput(containerComponent, componentInstanseId, property);
1033 if (status != StorageOperationStatus.OK) {
1034 log.debug("Failed to update instance input {} for instance {} error {} ", property, componentInstanseId, status);
1035 return Either.right(status);
1037 log.trace("instance input {} for instance {} updated", property, componentInstanseId);
1043 return Either.left(instProperties);
1046 public StorageOperationStatus deleteComponentInstanceInputsToComponent(Map<String, List<ComponentInstanceInput>> instProperties, String componentId) {
1048 Either<GraphVertex, TitanOperationStatus> getVertexEither = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
1049 if (getVertexEither.isRight()) {
1050 log.debug("Couldn't fetch component with and unique id {}, error: {}", componentId, getVertexEither.right().value());
1051 return DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value());
1055 GraphVertex vertex = getVertexEither.left().value();
1056 Map<String, MapPropertiesDataDefinition> instPropsMap = new HashMap<>();
1057 if (instProperties != null) {
1059 MapPropertiesDataDefinition propertiesMap;
1060 for (Entry<String, List<ComponentInstanceInput>> entry : instProperties.entrySet()) {
1061 propertiesMap = new MapPropertiesDataDefinition();
1063 propertiesMap.setMapToscaDataDefinition(entry.getValue().stream().map(e -> new PropertyDataDefinition(e)).collect(Collectors.toMap(e -> e.getName(), e -> e)));
1065 instPropsMap.put(entry.getKey(), propertiesMap);
1069 return topologyTemplateOperation.deleteInstInputsToComponent(vertex, instPropsMap);
1073 public Either<Map<String, List<ComponentInstanceProperty>>, StorageOperationStatus> addComponentInstancePropertiesToComponent(Component containerComponent, Map<String, List<ComponentInstanceProperty>> instProperties, String componentId) {
1075 StorageOperationStatus status = StorageOperationStatus.OK;
1076 if (instProperties != null) {
1078 for (Entry<String, List<ComponentInstanceProperty>> entry : instProperties.entrySet()) {
1079 List<ComponentInstanceProperty> props = entry.getValue();
1080 String componentInstanseId = entry.getKey();
1081 List<ComponentInstanceProperty> instanceProperties = containerComponent.getComponentInstancesProperties().get(componentInstanseId);
1082 if (props != null && !props.isEmpty()) {
1083 for (ComponentInstanceProperty property : props) {
1084 Optional<ComponentInstanceProperty> instanceProperty = instanceProperties.stream().filter(p -> p.getUniqueId().equals(property.getUniqueId())).findAny();
1085 if (instanceProperty.isPresent()) {
1086 status = updateComponentInstanceProperty(containerComponent, componentInstanseId, property);
1088 status = addComponentInstanceProperty(containerComponent, componentInstanseId, property);
1096 return Either.left(instProperties);
1100 public StorageOperationStatus associateArtifactToInstances(Map<String, Map<String, ArtifactDefinition>> instArtifacts, String componentId, User user) {
1102 Either<GraphVertex, TitanOperationStatus> getVertexEither = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
1103 if (getVertexEither.isRight()) {
1104 log.debug("Couldn't fetch component with and unique id {}, error: {}", componentId, getVertexEither.right().value());
1105 return DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value());
1109 GraphVertex vertex = getVertexEither.left().value();
1110 Map<String, MapArtifactDataDefinition> instArtMap = new HashMap<>();
1111 if (instArtifacts != null) {
1113 MapArtifactDataDefinition artifactsMap;
1114 for (Entry<String, Map<String, ArtifactDefinition>> entry : instArtifacts.entrySet()) {
1115 Map<String, ArtifactDefinition> artList = entry.getValue();
1116 Map<String, ArtifactDataDefinition> artifacts = artList.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> new ArtifactDataDefinition(e.getValue())));
1117 artifactsMap = nodeTemplateOperation.prepareInstDeploymentArtifactPerInstance(artifacts, entry.getKey(), user, NodeTemplateOperation.HEAT_VF_ENV_NAME);
1119 instArtMap.put(entry.getKey(), artifactsMap);
1123 return topologyTemplateOperation.associateInstArtifactToComponent(vertex, instArtMap);
1127 public StorageOperationStatus associateInstAttributeToComponentToInstances(Map<String, List<PropertyDefinition>> instArttributes, String componentId) {
1129 Either<GraphVertex, TitanOperationStatus> getVertexEither = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
1130 if (getVertexEither.isRight()) {
1131 log.debug("Couldn't fetch component with and unique id {}, error: {}", componentId, getVertexEither.right().value());
1132 return DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value());
1136 GraphVertex vertex = getVertexEither.left().value();
1137 Map<String, MapPropertiesDataDefinition> instAttr = new HashMap<>();
1138 if (instArttributes != null) {
1140 MapPropertiesDataDefinition attributesMap;
1141 for (Entry<String, List<PropertyDefinition>> entry : instArttributes.entrySet()) {
1142 attributesMap = new MapPropertiesDataDefinition();
1143 attributesMap.setMapToscaDataDefinition(entry.getValue().stream().map(e -> new PropertyDataDefinition(e)).collect(Collectors.toMap(e -> e.getName(), e -> e)));
1144 instAttr.put(entry.getKey(), attributesMap);
1148 return topologyTemplateOperation.associateInstAttributeToComponent(vertex, instAttr);
1152 public StorageOperationStatus associateCalculatedCapReq(Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> instCapabilties, Map<ComponentInstance, Map<String, List<RequirementDefinition>>> instReg, 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();
1162 Map<String, MapListRequirementDataDefinition> calcRequirements = new HashMap<>();
1164 Map<String, MapListCapabiltyDataDefinition> calcCapabilty = new HashMap<>();
1165 Map<String, MapCapabiltyProperty> calculatedCapabilitiesProperties = new HashMap<>();
1167 if (instCapabilties != null) {
1168 for (Entry<ComponentInstance, Map<String, List<CapabilityDefinition>>> entry : instCapabilties.entrySet()) {
1170 Map<String, List<CapabilityDefinition>> caps = entry.getValue();
1171 Map<String, ListCapabilityDataDefinition> mapToscaDataDefinition = new HashMap<>();
1172 for (Entry<String, List<CapabilityDefinition>> instCapability : caps.entrySet()) {
1173 mapToscaDataDefinition.put(instCapability.getKey(), new ListCapabilityDataDefinition(instCapability.getValue().stream().map(iCap -> new CapabilityDataDefinition(iCap)).collect(Collectors.toList())));
1176 ComponentInstanceDataDefinition componentInstance = new ComponentInstanceDataDefinition(entry.getKey());
1177 MapListCapabiltyDataDefinition capMap = nodeTemplateOperation.prepareCalculatedCapabiltyForNodeType(mapToscaDataDefinition, componentInstance);
1179 MapCapabiltyProperty mapCapabiltyProperty = ModelConverter.convertToMapOfMapCapabiltyProperties(caps, componentInstance.getUniqueId(), true);
1181 calcCapabilty.put(entry.getKey().getUniqueId(), capMap);
1182 calculatedCapabilitiesProperties.put(entry.getKey().getUniqueId(), mapCapabiltyProperty);
1186 if (instReg != null) {
1187 for (Entry<ComponentInstance, Map<String, List<RequirementDefinition>>> entry : instReg.entrySet()) {
1189 Map<String, List<RequirementDefinition>> req = entry.getValue();
1190 Map<String, ListRequirementDataDefinition> mapToscaDataDefinition = new HashMap<>();
1191 for (Entry<String, List<RequirementDefinition>> instReq : req.entrySet()) {
1192 mapToscaDataDefinition.put(instReq.getKey(), new ListRequirementDataDefinition(instReq.getValue().stream().map(iCap -> new RequirementDataDefinition(iCap)).collect(Collectors.toList())));
1195 MapListRequirementDataDefinition capMap = nodeTemplateOperation.prepareCalculatedRequirementForNodeType(mapToscaDataDefinition, new ComponentInstanceDataDefinition(entry.getKey()));
1197 calcRequirements.put(entry.getKey().getUniqueId(), capMap);
1201 StorageOperationStatus status = topologyTemplateOperation.associateCalcCapReqToComponent(vertex, calcRequirements, calcCapabilty, calculatedCapabilitiesProperties);
1206 private Either<List<Component>, StorageOperationStatus> getLatestVersionNotAbstractToscaElementsMetadataOnly(boolean isAbstract, Boolean isHighest, ComponentTypeEnum componentTypeEnum, String internalComponentType, VertexTypeEnum vertexType) {
1208 Map<GraphPropertyEnum, Object> hasProps = new EnumMap<>(GraphPropertyEnum.class);
1209 Map<GraphPropertyEnum, Object> hasNotProps = new EnumMap<>(GraphPropertyEnum.class);
1211 fillPropsMap(hasProps, hasNotProps, internalComponentType, componentTypeEnum, isAbstract, vertexType);
1213 Either<List<GraphVertex>, TitanOperationStatus> getRes = titanDao.getByCriteria(vertexType, hasProps, hasNotProps, JsonParseFlagEnum.ParseMetadata);
1214 if (getRes.isRight()) {
1215 if (getRes.right().value().equals(TitanOperationStatus.NOT_FOUND)) {
1216 return Either.left(new ArrayList<>());
1218 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getRes.right().value()));
1221 List<Component> nonAbstractLatestComponents = new ArrayList<>();
1222 ComponentParametersView params = new ComponentParametersView(true);
1223 params.setIgnoreAllVersions(false);
1224 for (GraphVertex vertexComponent : getRes.left().value()) {
1225 Either<ToscaElement, StorageOperationStatus> componentRes = topologyTemplateOperation.getLightComponent(vertexComponent, componentTypeEnum, params);
1226 if (componentRes.isRight()) {
1227 log.debug("Failed to fetch ligth element for {} error {}", vertexComponent.getUniqueId(), componentRes.right().value());
1228 return Either.right(componentRes.right().value());
1230 Component component = ModelConverter.convertFromToscaElement(componentRes.left().value());
1232 nonAbstractLatestComponents.add(component);
1236 return Either.left(nonAbstractLatestComponents);
1240 public Either<ComponentMetadataData, StorageOperationStatus> getLatestComponentMetadataByUuid(String componentUuid, JsonParseFlagEnum parseFlag, Boolean isHighest) {
1242 Either<ComponentMetadataData, StorageOperationStatus> result;
1244 Map<GraphPropertyEnum, Object> hasProperties = new EnumMap<>(GraphPropertyEnum.class);
1246 hasProperties.put(GraphPropertyEnum.UUID, componentUuid);
1247 if (isHighest != null) {
1248 hasProperties.put(GraphPropertyEnum.IS_HIGHEST_VERSION, isHighest.booleanValue());
1251 Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
1252 propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
1254 Either<List<GraphVertex>, TitanOperationStatus> getRes = titanDao.getByCriteria(null, hasProperties, propertiesNotToMatch, parseFlag);
1255 if (getRes.isRight()) {
1256 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getRes.right().value()));
1258 List<ComponentMetadataData> latestVersionList = getRes.left().value().stream().map(ModelConverter::convertToComponentMetadata).collect(Collectors.toList());
1259 ComponentMetadataData latestVersion = latestVersionList.size() == 1 ? latestVersionList.get(0)
1260 : latestVersionList.stream().max((c1, c2) -> Double.compare(Double.parseDouble(c1.getMetadataDataDefinition().getVersion()), Double.parseDouble(c2.getMetadataDataDefinition().getVersion()))).get();
1261 result = Either.left(latestVersion);
1266 public Either<ComponentMetadataData, StorageOperationStatus> getComponentMetadata(String componentId) {
1268 Either<ComponentMetadataData, StorageOperationStatus> result;
1269 Either<GraphVertex, TitanOperationStatus> getRes = titanDao.getVertexById(componentId, JsonParseFlagEnum.ParseMetadata);
1270 if (getRes.isRight()) {
1271 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getRes.right().value()));
1273 ComponentMetadataData componentMetadata = ModelConverter.convertToComponentMetadata(getRes.left().value());
1274 result = Either.left(componentMetadata);
1279 private Map<String, ComponentMetadataData> findLatestVersion(List<ComponentMetadataData> resourceDataList) {
1280 Map<Pair<String, String>, ComponentMetadataData> latestVersionMap = new HashMap<Pair<String, String>, ComponentMetadataData>();
1281 for (ComponentMetadataData resourceData : resourceDataList) {
1282 ComponentMetadataData latestVersionData = resourceData;
1284 ComponentMetadataDataDefinition metadataDataDefinition = resourceData.getMetadataDataDefinition();
1285 Pair<String, String> pair = createKeyPair(latestVersionData);
1286 if (latestVersionMap.containsKey(pair)) {
1287 latestVersionData = latestVersionMap.get(pair);
1288 String currentVersion = latestVersionData.getMetadataDataDefinition().getVersion();
1289 String newVersion = metadataDataDefinition.getVersion();
1290 if (CommonBeUtils.compareAsdcComponentVersions(newVersion, currentVersion)) {
1291 latestVersionData = resourceData;
1294 if (log.isDebugEnabled())
1295 log.debug("last certified version of resource = {} version is {}", latestVersionData.getMetadataDataDefinition().getName(), latestVersionData.getMetadataDataDefinition().getVersion());
1297 latestVersionMap.put(pair, latestVersionData);
1300 Map<String, ComponentMetadataData> resVersionMap = new HashMap<String, ComponentMetadataData>();
1301 for (ComponentMetadataData resourceData : latestVersionMap.values()) {
1302 ComponentMetadataData latestVersionData = resourceData;
1303 ComponentMetadataDataDefinition metadataDataDefinition = resourceData.getMetadataDataDefinition();
1304 if (resVersionMap.containsKey(metadataDataDefinition.getUUID())) {
1305 latestVersionData = resVersionMap.get(metadataDataDefinition.getUUID());
1306 String currentVersion = latestVersionData.getMetadataDataDefinition().getVersion();
1307 String newVersion = metadataDataDefinition.getVersion();
1308 if (CommonBeUtils.compareAsdcComponentVersions(newVersion, currentVersion)) {
1309 latestVersionData = resourceData;
1312 if (log.isDebugEnabled())
1313 log.debug("last uuid version of resource = {} version is {}", latestVersionData.getMetadataDataDefinition().getName(), latestVersionData.getMetadataDataDefinition().getVersion());
1314 resVersionMap.put(latestVersionData.getMetadataDataDefinition().getUUID(), latestVersionData);
1317 return resVersionMap;
1320 private Pair<String, String> createKeyPair(ComponentMetadataData metadataData) {
1321 Pair<String, String> pair;
1322 NodeTypeEnum label = NodeTypeEnum.getByName(metadataData.getLabel());
1325 pair = new ImmutablePair<>(metadataData.getMetadataDataDefinition().getName(), ((ResourceMetadataDataDefinition) metadataData.getMetadataDataDefinition()).getResourceType().name());
1328 pair = new ImmutablePair<>(metadataData.getMetadataDataDefinition().getName(), metadataData.getLabel());
1335 public Either<List<Component>, StorageOperationStatus> getLatestVersionNotAbstractComponents(boolean isAbstract, Boolean isHighest, ComponentTypeEnum componentTypeEnum, String internalComponentType, List<String> componentUids) {
1337 Either<List<Component>, StorageOperationStatus> result = null;
1338 List<Component> components = new ArrayList<>();
1339 if (componentUids == null) {
1340 Either<List<String>, StorageOperationStatus> componentUidsRes = getComponentUids(isAbstract, isHighest, componentTypeEnum, internalComponentType, componentUids);
1341 if (componentUidsRes.isRight()) {
1342 result = Either.right(componentUidsRes.right().value());
1344 componentUids = componentUidsRes.left().value();
1347 if (!componentUids.isEmpty()) {
1348 for (String componentUid : componentUids) {
1349 ComponentParametersView componentParametersView = buildComponentViewForNotAbstract();
1350 if (internalComponentType != null && "vl".equalsIgnoreCase(internalComponentType)) {
1351 componentParametersView.setIgnoreCapabilities(false);
1352 componentParametersView.setIgnoreRequirements(false);
1354 Either<ToscaElement, StorageOperationStatus> getToscaElementRes = nodeTemplateOperation.getToscaElementOperation(componentTypeEnum).getLightComponent(componentUid, componentTypeEnum, componentParametersView);
1355 if (getToscaElementRes.isRight()) {
1356 if (log.isDebugEnabled())
1357 log.debug("Failed to fetch resource for error is {}", getToscaElementRes.right().value());
1358 result = Either.right(getToscaElementRes.right().value());
1361 Component component = ModelConverter.convertFromToscaElement(getToscaElementRes.left().value());
1362 component.setContactId(null);
1363 component.setCreationDate(null);
1364 component.setCreatorUserId(null);
1365 component.setCreatorFullName(null);
1366 component.setLastUpdateDate(null);
1367 component.setLastUpdaterUserId(null);
1368 component.setLastUpdaterFullName(null);
1369 component.setNormalizedName(null);
1370 components.add(component);
1373 if (result == null) {
1374 result = Either.left(components);
1379 private Either<List<String>, StorageOperationStatus> getComponentUids(boolean isAbstract, Boolean isHighest, ComponentTypeEnum componentTypeEnum, String internalComponentType, List<String> componentUids) {
1381 Either<List<String>, StorageOperationStatus> result = null;
1382 Either<List<Component>, StorageOperationStatus> getToscaElementsRes = getLatestVersionNotAbstractMetadataOnly(isAbstract, isHighest, componentTypeEnum, internalComponentType);
1383 if (getToscaElementsRes.isRight()) {
1384 result = Either.right(getToscaElementsRes.right().value());
1386 List<Component> collection = getToscaElementsRes.left().value();
1387 if (collection == null) {
1388 componentUids = new ArrayList<>();
1390 componentUids = collection.stream().map(p -> p.getUniqueId()).collect(Collectors.toList());
1393 if (result == null) {
1394 result = Either.left(componentUids);
1399 private ComponentParametersView buildComponentViewForNotAbstract() {
1400 ComponentParametersView componentParametersView = new ComponentParametersView();
1401 componentParametersView.disableAll();
1402 componentParametersView.setIgnoreCategories(false);
1403 componentParametersView.setIgnoreAllVersions(false);
1404 return componentParametersView;
1407 public Either<Boolean, StorageOperationStatus> validateComponentNameExists(String name, ResourceTypeEnum resourceType, ComponentTypeEnum componentType) {
1408 Either<Boolean, StorageOperationStatus> result = validateComponentNameUniqueness(name, resourceType, componentType);
1409 if (result.isLeft()) {
1410 result = Either.left(!result.left().value());
1415 public Either<Boolean, StorageOperationStatus> validateComponentNameUniqueness(String name, ResourceTypeEnum resourceType, ComponentTypeEnum componentType) {
1416 VertexTypeEnum vertexType = ModelConverter.isAtomicComponent(resourceType) ? VertexTypeEnum.NODE_TYPE : VertexTypeEnum.TOPOLOGY_TEMPLATE;
1417 String normalizedName = ValidationUtils.normaliseComponentName(name);
1418 Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
1419 properties.put(GraphPropertyEnum.NORMALIZED_NAME, normalizedName);
1420 properties.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
1422 Either<List<GraphVertex>, TitanOperationStatus> vertexEither = titanDao.getByCriteria(vertexType, properties, JsonParseFlagEnum.NoParse);
1423 if (vertexEither.isRight() && vertexEither.right().value() != TitanOperationStatus.NOT_FOUND) {
1424 log.debug("failed to get vertex from graph with property normalizedName: {}", normalizedName);
1425 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(vertexEither.right().value()));
1427 List<GraphVertex> vertexList = vertexEither.isLeft() ? vertexEither.left().value() : null;
1428 if (vertexList != null && !vertexList.isEmpty()) {
1429 return Either.left(false);
1431 return Either.left(true);
1436 private void fillNodeTypePropsMap(Map<GraphPropertyEnum, Object> hasProps, Map<GraphPropertyEnum, Object> hasNotProps, String internalComponentType) {
1437 switch (internalComponentType.toLowerCase()) {
1440 hasNotProps.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.VFCMT.name());
1444 hasNotProps.put(GraphPropertyEnum.RESOURCE_TYPE, Arrays.asList(ResourceTypeEnum.VFC.name(), ResourceTypeEnum.VFCMT.name()));
1447 hasProps.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.VL.name());
1455 private void fillTopologyTemplatePropsMap(Map<GraphPropertyEnum, Object> hasProps, Map<GraphPropertyEnum, Object> hasNotProps, ComponentTypeEnum componentTypeEnum, String internalComponentType) {
1456 switch (componentTypeEnum) {
1458 hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
1461 hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
1466 switch (internalComponentType.toLowerCase()) {
1469 hasProps.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.CVFC.name());
1472 hasNotProps.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.CVFC.name());
1479 private void fillPropsMap(Map<GraphPropertyEnum, Object> hasProps, Map<GraphPropertyEnum, Object> hasNotProps, String internalComponentType, ComponentTypeEnum componentTypeEnum, boolean isAbstract, VertexTypeEnum internalVertexType) {
1480 hasNotProps.put(GraphPropertyEnum.STATE, LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name());
1482 hasNotProps.put(GraphPropertyEnum.IS_DELETED, true);
1483 hasProps.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
1484 if (VertexTypeEnum.NODE_TYPE == internalVertexType) {
1485 hasProps.put(GraphPropertyEnum.IS_ABSTRACT, isAbstract);
1486 if (internalComponentType != null) {
1487 fillNodeTypePropsMap(hasProps, hasNotProps, internalComponentType);
1490 fillTopologyTemplatePropsMap(hasProps, hasNotProps, componentTypeEnum, internalComponentType);
1494 private List<VertexTypeEnum> getInternalVertexTypes(ComponentTypeEnum componentTypeEnum, String internalComponentType) {
1495 List<VertexTypeEnum> internalVertexTypes = new ArrayList<>();
1496 if (ComponentTypeEnum.RESOURCE == componentTypeEnum) {
1497 internalVertexTypes.add(VertexTypeEnum.NODE_TYPE);
1499 if (ComponentTypeEnum.SERVICE == componentTypeEnum || "service".equalsIgnoreCase(internalComponentType) || "vf".equalsIgnoreCase(internalComponentType)) {
1500 internalVertexTypes.add(VertexTypeEnum.TOPOLOGY_TEMPLATE);
1502 return internalVertexTypes;
1505 public Either<List<Component>, StorageOperationStatus> getLatestVersionNotAbstractMetadataOnly(boolean isAbstract, Boolean isHighest, ComponentTypeEnum componentTypeEnum, String internalComponentType) {
1506 List<VertexTypeEnum> internalVertexTypes = getInternalVertexTypes(componentTypeEnum, internalComponentType);
1507 List<Component> result = new ArrayList<>();
1508 for (VertexTypeEnum vertexType : internalVertexTypes) {
1509 Either<List<Component>, StorageOperationStatus> listByVertexType = getLatestVersionNotAbstractToscaElementsMetadataOnly(isAbstract, isHighest, componentTypeEnum, internalComponentType, vertexType);
1510 if (listByVertexType.isRight()) {
1511 return listByVertexType;
1513 result.addAll(listByVertexType.left().value());
1515 return Either.left(result);
1519 public Either<List<Component>, StorageOperationStatus> getLatestComponentListByUuid(String componentUuid) {
1520 Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
1521 propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
1522 Either<List<Component>, StorageOperationStatus> componentListByUuid = getComponentListByUuid(componentUuid, propertiesToMatch);
1523 return componentListByUuid;
1526 public Either<List<Component>, StorageOperationStatus> getComponentListByUuid(String componentUuid, Map<GraphPropertyEnum, Object> additionalPropertiesToMatch) {
1528 Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
1530 if (additionalPropertiesToMatch != null) {
1531 propertiesToMatch.putAll(additionalPropertiesToMatch);
1534 propertiesToMatch.put(GraphPropertyEnum.UUID, componentUuid);
1536 Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
1537 propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
1539 Either<List<GraphVertex>, TitanOperationStatus> vertexEither = titanDao.getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll);
1541 if (vertexEither.isRight()) {
1542 log.debug("Couldn't fetch metadata for component with type {} and uuid {}, error: {}", componentUuid, vertexEither.right().value());
1543 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(vertexEither.right().value()));
1545 List<GraphVertex> vertexList = vertexEither.isLeft() ? vertexEither.left().value() : null;
1547 if (vertexList == null || vertexList.isEmpty()) {
1548 log.debug("Component with uuid {} was not found", componentUuid);
1549 return Either.right(StorageOperationStatus.NOT_FOUND);
1552 ArrayList<Component> latestComponents = new ArrayList<>();
1553 for (GraphVertex vertex : vertexList) {
1554 Either<Component, StorageOperationStatus> toscaElementByOperation = getToscaElementByOperation(vertex);
1556 if(toscaElementByOperation.isRight()){
1557 log.debug("Could not fetch the following Component by UUID {}", vertex.getUniqueId());
1558 return Either.right(toscaElementByOperation.right().value());
1561 latestComponents.add(toscaElementByOperation.left().value());
1564 if(latestComponents.size() > 1) {
1565 for (Component component : latestComponents) {
1566 if(component.isHighestVersion()){
1567 LinkedList<Component> highestComponent = new LinkedList<>();
1568 highestComponent.add(component);
1569 return Either.left(highestComponent);
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;
1854 servicesAll = fetchServicesByCriteria.left().value();
1857 return Either.left(servicesAll);
1859 return fetchServicesByCriteria(servicesAll, propertiesToMatch, propertiesNotToMatch);
1863 // private Either<List<Service>, StorageOperationStatus> fetchServicesByCriteria(List<Service> servicesAll, Map<GraphPropertyEnum, Object> propertiesToMatch, Map<GraphPropertyEnum, Object> propertiesNotToMatch) {
1864 // Either<List<GraphVertex>, TitanOperationStatus> getRes = titanDao.getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll);
1865 // if (getRes.isRight()) {
1866 // if (getRes.right().value() != TitanOperationStatus.NOT_FOUND) {
1867 // CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch certified services by match properties {} not match properties {} . Status is {}. ", propertiesToMatch, propertiesNotToMatch, getRes.right().value());
1868 // return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getRes.right().value()));
1871 // for (GraphVertex vertex : getRes.left().value()) {
1872 // Either<Component, StorageOperationStatus> getServiceRes = getToscaElementByOperation(vertex);
1873 // if (getServiceRes.isRight()) {
1874 // CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch certified service {}. Status is {}. ", vertex.getJsonMetadataField(JsonPresentationFields.NAME), getServiceRes.right().value());
1875 // return Either.right(getServiceRes.right().value());
1877 // servicesAll.add((Service) getToscaElementByOperation(vertex).left().value());
1881 // return Either.left(servicesAll);
1884 private Either<List<Service>, StorageOperationStatus> fetchServicesByCriteria(List<Service> servicesAll, Map<GraphPropertyEnum, Object> propertiesToMatch, Map<GraphPropertyEnum, Object> propertiesNotToMatch) {
1885 Either<List<GraphVertex>, TitanOperationStatus> getRes = titanDao.getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll);
1886 if (getRes.isRight()) {
1887 if (getRes.right().value() != TitanOperationStatus.NOT_FOUND) {
1888 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch certified services by match properties {} not match properties {} . Status is {}. ", propertiesToMatch, propertiesNotToMatch, getRes.right().value());
1889 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getRes.right().value()));
1892 for (GraphVertex vertex : getRes.left().value()) {
1893 // Either<Component, StorageOperationStatus> getServiceRes = getToscaElementByOperation(vertex);
1894 Either<ToscaElement, StorageOperationStatus> getServiceRes = topologyTemplateOperation.getLightComponent(vertex, ComponentTypeEnum.SERVICE, new ComponentParametersView(true));
1896 if (getServiceRes.isRight()) {
1897 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch certified service {}. Status is {}. ", vertex.getJsonMetadataField(JsonPresentationFields.NAME), getServiceRes.right().value());
1898 return Either.right(getServiceRes.right().value());
1900 servicesAll.add(ModelConverter.convertFromToscaElement(getServiceRes.left().value()));
1904 return Either.left(servicesAll);
1907 public void rollback() {
1908 titanDao.rollback();
1911 public StorageOperationStatus addDeploymentArtifactsToInstance(String componentId, ComponentInstance componentInstance, Map<String, ArtifactDefinition> finalDeploymentArtifacts) {
1912 Map<String, ArtifactDataDefinition> instDeplArtifacts = finalDeploymentArtifacts.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> new ArtifactDataDefinition(e.getValue())));
1914 return nodeTemplateOperation.addDeploymentArtifactsToInstance(componentId, componentInstance.getUniqueId(), instDeplArtifacts);
1917 public StorageOperationStatus generateCustomizationUUIDOnInstance(String componentId, String instanceId) {
1918 return nodeTemplateOperation.generateCustomizationUUIDOnInstance(componentId, instanceId);
1921 public StorageOperationStatus generateCustomizationUUIDOnInstanceGroup(String componentId, String instanceId, List<String> groupInstances) {
1922 return nodeTemplateOperation.generateCustomizationUUIDOnInstanceGroup(componentId, instanceId, groupInstances);
1925 public Either<PropertyDefinition, StorageOperationStatus> addPropertyToResource(String propertyName, PropertyDefinition newPropertyDefinition, Resource resource) {
1927 Either<PropertyDefinition, StorageOperationStatus> result = null;
1928 Either<Component, StorageOperationStatus> getUpdatedComponentRes = null;
1929 newPropertyDefinition.setName(propertyName);
1930 newPropertyDefinition.setParentUniqueId(resource.getUniqueId());
1931 StorageOperationStatus status = getToscaElementOperation(resource).addToscaDataToToscaElement(resource.getUniqueId(), EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, newPropertyDefinition, JsonPresentationFields.NAME);
1932 if (status != StorageOperationStatus.OK) {
1933 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to add the property {} to the resource {}. Status is {}. ", propertyName, resource.getName(), status);
1934 result = Either.right(status);
1936 if (result == null) {
1937 ComponentParametersView filter = new ComponentParametersView(true);
1938 filter.setIgnoreProperties(false);
1939 getUpdatedComponentRes = getToscaElement(resource.getUniqueId(), filter);
1940 if (getUpdatedComponentRes.isRight()) {
1941 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to get updated resource {}. Status is {}. ", resource.getUniqueId(), getUpdatedComponentRes.right().value());
1942 result = Either.right(status);
1945 if (result == null) {
1946 PropertyDefinition newProperty = null;
1947 List<PropertyDefinition> properties = ((Resource) getUpdatedComponentRes.left().value()).getProperties();
1948 if (CollectionUtils.isNotEmpty(properties)) {
1949 Optional<PropertyDefinition> newPropertyOptional = properties.stream().filter(p -> p.getName().equals(propertyName)).findAny();
1950 if (newPropertyOptional.isPresent()) {
1951 newProperty = newPropertyOptional.get();
1954 if (newProperty == null) {
1955 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to find recently added property {} on the resource {}. Status is {}. ", propertyName, resource.getUniqueId(), StorageOperationStatus.NOT_FOUND);
1956 result = Either.right(StorageOperationStatus.NOT_FOUND);
1958 result = Either.left(newProperty);
1964 public StorageOperationStatus deletePropertyOfResource(Resource resource, String propertyName) {
1965 return getToscaElementOperation(resource).deleteToscaDataElement(resource.getUniqueId(), EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, propertyName, JsonPresentationFields.NAME);
1968 public StorageOperationStatus deleteAttributeOfResource(Component component, String attributeName) {
1969 return getToscaElementOperation(component).deleteToscaDataElement(component.getUniqueId(), EdgeLabelEnum.ATTRIBUTES, VertexTypeEnum.ATTRIBUTES, attributeName, JsonPresentationFields.NAME);
1972 public StorageOperationStatus deleteInputOfResource(Component resource, String inputName) {
1973 return getToscaElementOperation(resource).deleteToscaDataElement(resource.getUniqueId(), EdgeLabelEnum.INPUTS, VertexTypeEnum.INPUTS, inputName, JsonPresentationFields.NAME);
1976 public Either<PropertyDefinition, StorageOperationStatus> updatePropertyOfResource(Resource resource, PropertyDefinition newPropertyDefinition) {
1978 Either<Component, StorageOperationStatus> getUpdatedComponentRes = null;
1979 Either<PropertyDefinition, StorageOperationStatus> result = null;
1980 StorageOperationStatus status = getToscaElementOperation(resource).updateToscaDataOfToscaElement(resource.getUniqueId(), EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, newPropertyDefinition, JsonPresentationFields.NAME);
1981 if (status != StorageOperationStatus.OK) {
1982 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to add the property {} to the resource {}. Status is {}. ", newPropertyDefinition.getName(), resource.getName(), status);
1983 result = Either.right(status);
1985 if (result == null) {
1986 ComponentParametersView filter = new ComponentParametersView(true);
1987 filter.setIgnoreProperties(false);
1988 getUpdatedComponentRes = getToscaElement(resource.getUniqueId(), filter);
1989 if (getUpdatedComponentRes.isRight()) {
1990 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to get updated resource {}. Status is {}. ", resource.getUniqueId(), getUpdatedComponentRes.right().value());
1991 result = Either.right(status);
1994 if (result == null) {
1995 Optional<PropertyDefinition> newProperty = ((Resource) getUpdatedComponentRes.left().value()).getProperties().stream().filter(p -> p.getName().equals(newPropertyDefinition.getName())).findAny();
1996 if (newProperty.isPresent()) {
1997 result = Either.left(newProperty.get());
1999 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to find recently added property {} on the resource {}. Status is {}. ", newPropertyDefinition.getName(), resource.getUniqueId(), StorageOperationStatus.NOT_FOUND);
2000 result = Either.right(StorageOperationStatus.NOT_FOUND);
2006 public Either<PropertyDefinition, StorageOperationStatus> addAttributeOfResource(Component component, PropertyDefinition newAttributeDef) {
2008 Either<Component, StorageOperationStatus> getUpdatedComponentRes = null;
2009 Either<PropertyDefinition, StorageOperationStatus> result = null;
2010 if (newAttributeDef.getUniqueId() == null || newAttributeDef.getUniqueId().isEmpty()) {
2011 String attUniqueId = UniqueIdBuilder.buildAttributeUid(component.getUniqueId(), newAttributeDef.getName());
2012 newAttributeDef.setUniqueId(attUniqueId);
2015 StorageOperationStatus status = getToscaElementOperation(component).addToscaDataToToscaElement(component.getUniqueId(), EdgeLabelEnum.ATTRIBUTES, VertexTypeEnum.ATTRIBUTES, newAttributeDef, JsonPresentationFields.NAME);
2016 if (status != StorageOperationStatus.OK) {
2017 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to add the property {} to the resource {}. Status is {}. ", newAttributeDef.getName(), component.getName(), status);
2018 result = Either.right(status);
2020 if (result == null) {
2021 ComponentParametersView filter = new ComponentParametersView(true);
2022 filter.setIgnoreAttributesFrom(false);
2023 getUpdatedComponentRes = getToscaElement(component.getUniqueId(), filter);
2024 if (getUpdatedComponentRes.isRight()) {
2025 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to get updated resource {}. Status is {}. ", component.getUniqueId(), getUpdatedComponentRes.right().value());
2026 result = Either.right(status);
2029 if (result == null) {
2030 Optional<PropertyDefinition> newAttribute = ((Resource) getUpdatedComponentRes.left().value()).getAttributes().stream().filter(p -> p.getName().equals(newAttributeDef.getName())).findAny();
2031 if (newAttribute.isPresent()) {
2032 result = Either.left(newAttribute.get());
2034 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to find recently added property {} on the resource {}. Status is {}. ", newAttributeDef.getName(), component.getUniqueId(), StorageOperationStatus.NOT_FOUND);
2035 result = Either.right(StorageOperationStatus.NOT_FOUND);
2041 public Either<PropertyDefinition, StorageOperationStatus> updateAttributeOfResource(Component component, PropertyDefinition newAttributeDef) {
2043 Either<Component, StorageOperationStatus> getUpdatedComponentRes = null;
2044 Either<PropertyDefinition, StorageOperationStatus> result = null;
2045 StorageOperationStatus status = getToscaElementOperation(component).updateToscaDataOfToscaElement(component.getUniqueId(), EdgeLabelEnum.ATTRIBUTES, VertexTypeEnum.ATTRIBUTES, newAttributeDef, JsonPresentationFields.NAME);
2046 if (status != StorageOperationStatus.OK) {
2047 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to add the property {} to the resource {}. Status is {}. ", newAttributeDef.getName(), component.getName(), status);
2048 result = Either.right(status);
2050 if (result == null) {
2051 ComponentParametersView filter = new ComponentParametersView(true);
2052 filter.setIgnoreAttributesFrom(false);
2053 getUpdatedComponentRes = getToscaElement(component.getUniqueId(), filter);
2054 if (getUpdatedComponentRes.isRight()) {
2055 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to get updated resource {}. Status is {}. ", component.getUniqueId(), getUpdatedComponentRes.right().value());
2056 result = Either.right(status);
2059 if (result == null) {
2060 Optional<PropertyDefinition> newProperty = ((Resource) getUpdatedComponentRes.left().value()).getAttributes().stream().filter(p -> p.getName().equals(newAttributeDef.getName())).findAny();
2061 if (newProperty.isPresent()) {
2062 result = Either.left(newProperty.get());
2064 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to find recently added property {} on the resource {}. Status is {}. ", newAttributeDef.getName(), component.getUniqueId(), StorageOperationStatus.NOT_FOUND);
2065 result = Either.right(StorageOperationStatus.NOT_FOUND);
2071 public Either<InputDefinition, StorageOperationStatus> updateInputOfComponent(Component component, InputDefinition newInputDefinition) {
2073 Either<Component, StorageOperationStatus> getUpdatedComponentRes = null;
2074 Either<InputDefinition, StorageOperationStatus> result = null;
2075 StorageOperationStatus status = getToscaElementOperation(component).updateToscaDataOfToscaElement(component.getUniqueId(), EdgeLabelEnum.INPUTS, VertexTypeEnum.INPUTS, newInputDefinition, JsonPresentationFields.NAME);
2076 if (status != StorageOperationStatus.OK) {
2077 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to update the input {} to the component {}. Status is {}. ", newInputDefinition.getName(), component.getName(), status);
2078 result = Either.right(status);
2080 if (result == null) {
2081 ComponentParametersView filter = new ComponentParametersView(true);
2082 filter.setIgnoreInputs(false);
2083 getUpdatedComponentRes = getToscaElement(component.getUniqueId(), filter);
2084 if (getUpdatedComponentRes.isRight()) {
2085 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to get updated resource {}. Status is {}. ", component.getUniqueId(), getUpdatedComponentRes.right().value());
2086 result = Either.right(status);
2089 if (result == null) {
2090 Optional<InputDefinition> updatedInput = getUpdatedComponentRes.left().value().getInputs().stream().filter(p -> p.getName().equals(newInputDefinition.getName())).findAny();
2091 if (updatedInput.isPresent()) {
2092 result = Either.left(updatedInput.get());
2094 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to find recently updated inputs {} on the resource {}. Status is {}. ", newInputDefinition.getName(), component.getUniqueId(), StorageOperationStatus.NOT_FOUND);
2095 result = Either.right(StorageOperationStatus.NOT_FOUND);
2102 * method - ename the group instances after referenced container name renamed
2103 * flow - VF rename -(triggers)-> Group rename
2105 * @param containerComponent - container such as service
2106 * @param componentInstance - context component
2107 * @param componentInstanceId - id
2109 * @return - successfull/failed status
2111 public Either<StorageOperationStatus,StorageOperationStatus> cleanAndAddGroupInstancesToComponentInstance(Component containerComponent, ComponentInstance componentInstance, String componentInstanceId){
2112 String uniqueId = componentInstance.getUniqueId();
2113 StorageOperationStatus status = nodeTemplateOperation.deleteToscaDataDeepElementsBlockToToscaElement( containerComponent.getUniqueId(), EdgeLabelEnum.INST_GROUPS, VertexTypeEnum.INST_GROUPS, uniqueId );
2114 if (status != StorageOperationStatus.OK && status != StorageOperationStatus.NOT_FOUND) {
2115 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to delete group instances for container {}. error {] ", componentInstanceId, status);
2116 return Either.right(status);
2118 status = addGroupInstancesToComponentInstance( containerComponent , componentInstance, componentInstance.getGroupInstances() );
2119 if (status != StorageOperationStatus.OK && status != StorageOperationStatus.NOT_FOUND) {
2120 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to add group instances for container {}. error {] ", componentInstanceId, status);
2121 return Either.right(status);
2123 return Either.left(status);
2126 public StorageOperationStatus addGroupInstancesToComponentInstance(Component containerComponent, ComponentInstance componentInstance, List<GroupDefinition> groups, Map<String, List<ArtifactDefinition>> groupInstancesArtifacts) {
2127 return nodeTemplateOperation.addGroupInstancesToComponentInstance(containerComponent, componentInstance, groups, groupInstancesArtifacts);
2130 public Either<List<GroupDefinition>, StorageOperationStatus> updateGroupsOnComponent(Component component, ComponentTypeEnum componentType, List<GroupDataDefinition> updatedGroups) {
2131 return groupsOperation.updateGroups(component, componentType, updatedGroups);
2134 public Either<List<GroupInstance>, StorageOperationStatus> updateGroupInstancesOnComponent(Component component, ComponentTypeEnum componentType, String instanceId, List<GroupInstance> updatedGroupInstances) {
2135 return groupsOperation.updateGroupInstances(component, componentType, instanceId, updatedGroupInstances);
2138 public StorageOperationStatus addGroupInstancesToComponentInstance(Component containerComponent, ComponentInstance componentInstance, List<GroupInstance> groupInstances) {
2139 return nodeTemplateOperation.addGroupInstancesToComponentInstance(containerComponent, componentInstance, groupInstances);
2142 public StorageOperationStatus addDeploymentArtifactsToComponentInstance(Component containerComponent, ComponentInstance componentInstance, Map<String, ArtifactDefinition> deploymentArtifacts) {
2143 return nodeTemplateOperation.addDeploymentArtifactsToComponentInstance(containerComponent, componentInstance, deploymentArtifacts);
2146 public StorageOperationStatus updateComponentInstanceProperty(Component containerComponent, String componentInstanceId, ComponentInstanceProperty property) {
2147 return nodeTemplateOperation.updateComponentInstanceProperty(containerComponent, componentInstanceId, property);
2150 public StorageOperationStatus addComponentInstanceProperty(Component containerComponent, String componentInstanceId, ComponentInstanceProperty property) {
2151 return nodeTemplateOperation.addComponentInstanceProperty(containerComponent, componentInstanceId, property);
2154 public StorageOperationStatus updateComponentInstanceInput(Component containerComponent, String componentInstanceId, ComponentInstanceInput property) {
2155 return nodeTemplateOperation.updateComponentInstanceInput(containerComponent, componentInstanceId, property);
2158 public StorageOperationStatus addComponentInstanceInput(Component containerComponent, String componentInstanceId, ComponentInstanceInput property) {
2159 return nodeTemplateOperation.addComponentInstanceInput(containerComponent, componentInstanceId, property);
2162 public void setNodeTypeOperation(NodeTypeOperation nodeTypeOperation) {
2163 this.nodeTypeOperation = nodeTypeOperation;
2166 public void setTopologyTemplateOperation(TopologyTemplateOperation topologyTemplateOperation) {
2167 this.topologyTemplateOperation = topologyTemplateOperation;
2170 public StorageOperationStatus deleteComponentInstanceInputsFromTopologyTemplate(Component containerComponent, ComponentTypeEnum componentType, List<InputDefinition> inputsToDelete) {
2171 return topologyTemplateOperation.deleteToscaDataElements(containerComponent.getUniqueId(), EdgeLabelEnum.INPUTS, inputsToDelete.stream().map(i -> i.getName()).collect(Collectors.toList()));