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.jsonjanusgraph.operations;
23 import static java.util.Objects.requireNonNull;
24 import static org.apache.commons.collections.CollectionUtils.isEmpty;
25 import static org.apache.commons.collections.CollectionUtils.isNotEmpty;
26 import static org.janusgraph.core.attribute.Contain.NOT_IN;
27 import static org.janusgraph.core.attribute.Text.REGEX;
28 import static org.openecomp.sdc.be.dao.janusgraph.JanusGraphUtils.buildNotInPredicate;
29 import fj.data.Either;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.Collection;
33 import java.util.Collections;
34 import java.util.EnumMap;
35 import java.util.HashMap;
36 import java.util.HashSet;
37 import java.util.Iterator;
38 import java.util.LinkedList;
39 import java.util.List;
41 import java.util.Map.Entry;
42 import java.util.Optional;
44 import java.util.function.BiPredicate;
45 import java.util.stream.Collectors;
46 import org.apache.commons.collections.CollectionUtils;
47 import org.apache.commons.collections.MapUtils;
48 import org.apache.commons.lang3.StringUtils;
49 import org.apache.commons.lang3.tuple.ImmutablePair;
50 import org.apache.tinkerpop.gremlin.structure.Direction;
51 import org.apache.tinkerpop.gremlin.structure.Edge;
52 import org.janusgraph.graphdb.query.JanusGraphPredicate;
53 import org.openecomp.sdc.be.config.Configuration;
54 import org.openecomp.sdc.be.config.ConfigurationManager;
55 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
56 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
57 import org.openecomp.sdc.be.dao.jsongraph.HealingJanusGraphDao;
58 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
59 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
60 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
61 import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary;
62 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
63 import org.openecomp.sdc.be.datatypes.elements.CapabilityDataDefinition;
64 import org.openecomp.sdc.be.datatypes.elements.ComponentInstanceDataDefinition;
65 import org.openecomp.sdc.be.datatypes.elements.DataTypeDataDefinition;
66 import org.openecomp.sdc.be.datatypes.elements.GroupDataDefinition;
67 import org.openecomp.sdc.be.datatypes.elements.ListCapabilityDataDefinition;
68 import org.openecomp.sdc.be.datatypes.elements.ListRequirementDataDefinition;
69 import org.openecomp.sdc.be.datatypes.elements.MapArtifactDataDefinition;
70 import org.openecomp.sdc.be.datatypes.elements.MapAttributesDataDefinition;
71 import org.openecomp.sdc.be.datatypes.elements.MapCapabilityProperty;
72 import org.openecomp.sdc.be.datatypes.elements.MapInterfaceDataDefinition;
73 import org.openecomp.sdc.be.datatypes.elements.MapListCapabilityDataDefinition;
74 import org.openecomp.sdc.be.datatypes.elements.MapListRequirementDataDefinition;
75 import org.openecomp.sdc.be.datatypes.elements.MapPropertiesDataDefinition;
76 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
77 import org.openecomp.sdc.be.datatypes.elements.RequirementDataDefinition;
78 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
79 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
80 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
81 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
82 import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
83 import org.openecomp.sdc.be.datatypes.enums.PromoteVersionEnum;
84 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
85 import org.openecomp.sdc.be.model.ArtifactDefinition;
86 import org.openecomp.sdc.be.model.AttributeDefinition;
87 import org.openecomp.sdc.be.model.CapabilityDefinition;
88 import org.openecomp.sdc.be.model.CatalogUpdateTimestamp;
89 import org.openecomp.sdc.be.model.Component;
90 import org.openecomp.sdc.be.model.ComponentInstance;
91 import org.openecomp.sdc.be.model.ComponentInstanceAttribute;
92 import org.openecomp.sdc.be.model.ComponentInstanceInput;
93 import org.openecomp.sdc.be.model.ComponentInstanceInterface;
94 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
95 import org.openecomp.sdc.be.model.ComponentParametersView;
96 import org.openecomp.sdc.be.model.DataTypeDefinition;
97 import org.openecomp.sdc.be.model.DistributionStatusEnum;
98 import org.openecomp.sdc.be.model.GroupDefinition;
99 import org.openecomp.sdc.be.model.GroupInstance;
100 import org.openecomp.sdc.be.model.InputDefinition;
101 import org.openecomp.sdc.be.model.LifecycleStateEnum;
102 import org.openecomp.sdc.be.model.PolicyDefinition;
103 import org.openecomp.sdc.be.model.PropertyDefinition;
104 import org.openecomp.sdc.be.model.RelationshipInfo;
105 import org.openecomp.sdc.be.model.RequirementCapabilityRelDef;
106 import org.openecomp.sdc.be.model.RequirementDefinition;
107 import org.openecomp.sdc.be.model.Resource;
108 import org.openecomp.sdc.be.model.Service;
109 import org.openecomp.sdc.be.model.User;
110 import org.openecomp.sdc.be.model.catalog.CatalogComponent;
111 import org.openecomp.sdc.be.model.jsonjanusgraph.config.ContainerInstanceTypesData;
112 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.TopologyTemplate;
113 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElement;
114 import org.openecomp.sdc.be.model.jsonjanusgraph.utils.ModelConverter;
115 import org.openecomp.sdc.be.model.operations.StorageException;
116 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
117 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
118 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
119 import org.openecomp.sdc.be.model.utils.GroupUtils;
120 import org.openecomp.sdc.be.resources.data.ComponentMetadataData;
121 import org.openecomp.sdc.be.resources.data.GroupTypeData;
122 import org.openecomp.sdc.common.jsongraph.util.CommonUtility;
123 import org.openecomp.sdc.common.jsongraph.util.CommonUtility.LogLevelEnum;
124 import org.openecomp.sdc.common.log.wrappers.Logger;
125 import org.openecomp.sdc.common.util.ValidationUtils;
126 import org.springframework.beans.factory.annotation.Autowired;
127 import com.vdurmont.semver4j.Semver;
128 import com.vdurmont.semver4j.Semver.SemverType;
131 @org.springframework.stereotype.Component("tosca-operation-facade")
132 public class ToscaOperationFacade {
136 private static final String COULDNT_FETCH_A_COMPONENT_WITH_AND_UNIQUE_ID_ERROR = "Couldn't fetch a component with and UniqueId {}, error: {}";
137 private static final String FAILED_TO_FIND_RECENTLY_ADDED_PROPERTY_ON_THE_RESOURCE_STATUS_IS = "Failed to find recently added property {} on the resource {}. Status is {}. ";
138 private static final String FAILED_TO_GET_UPDATED_RESOURCE_STATUS_IS = "Failed to get updated resource {}. Status is {}. ";
139 private static final String FAILED_TO_ADD_THE_PROPERTY_TO_THE_RESOURCE_STATUS_IS = "Failed to add the property {} to the resource {}. Status is {}. ";
140 private static final String SERVICE = "service";
141 private static final String NOT_SUPPORTED_COMPONENT_TYPE = "Not supported component type {}";
142 private static final String COMPONENT_CREATED_SUCCESSFULLY = "Component created successfully!!!";
143 private static final String COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR = "Couldn't fetch component with and unique id {}, error: {}";
145 private NodeTypeOperation nodeTypeOperation;
147 private TopologyTemplateOperation topologyTemplateOperation;
149 private NodeTemplateOperation nodeTemplateOperation;
151 private GroupsOperation groupsOperation;
153 private HealingJanusGraphDao janusGraphDao;
155 private ContainerInstanceTypesData containerInstanceTypesData;
157 private static final Logger log = Logger.getLogger(ToscaOperationFacade.class.getName());
160 // region - ToscaElement - GetById
161 public static final String PROXY_SUFFIX = "_proxy";
163 public <T extends Component> Either<T, StorageOperationStatus> getToscaFullElement(String componentId) {
164 ComponentParametersView filters = new ComponentParametersView();
165 filters.setIgnoreCapabiltyProperties(false);
166 filters.setIgnoreServicePath(false);
167 return getToscaElement(componentId, filters);
170 public <T extends Component> Either<T, StorageOperationStatus> getToscaElement(String componentId) {
172 return getToscaElement(componentId, JsonParseFlagEnum.ParseAll);
176 public <T extends Component> Either<T, StorageOperationStatus> getToscaElement(String componentId, ComponentParametersView filters) {
178 Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao
179 .getVertexById(componentId, filters.detectParseFlag());
180 if (getVertexEither.isRight()) {
181 log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
182 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
185 return getToscaElementByOperation(getVertexEither.left().value(), filters);
188 public <T extends Component> Either<T, StorageOperationStatus> getToscaElement(String componentId, JsonParseFlagEnum parseFlag) {
190 Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao
191 .getVertexById(componentId, parseFlag);
192 if (getVertexEither.isRight()) {
193 log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
194 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
197 return getToscaElementByOperation(getVertexEither.left().value());
200 public <T extends Component> Either<T, StorageOperationStatus> getToscaElement(GraphVertex componentVertex) {
201 return getToscaElementByOperation(componentVertex);
204 public Either<Boolean, StorageOperationStatus> validateComponentExists(String componentId) {
206 Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao
207 .getVertexById(componentId, JsonParseFlagEnum.NoParse);
208 if (getVertexEither.isRight()) {
209 JanusGraphOperationStatus status = getVertexEither.right().value();
210 if (status == JanusGraphOperationStatus.NOT_FOUND) {
211 return Either.left(false);
213 log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
214 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
217 return Either.left(true);
220 public <T extends Component> Either<T, StorageOperationStatus> findLastCertifiedToscaElementByUUID(T component) {
221 Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
222 props.put(GraphPropertyEnum.UUID, component.getUUID());
223 props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
224 props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
226 Either<List<GraphVertex>, JanusGraphOperationStatus> getVertexEither = janusGraphDao
227 .getByCriteria(ModelConverter.getVertexType(component), props);
228 if (getVertexEither.isRight()) {
229 log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, component.getUniqueId(), getVertexEither.right().value());
230 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
233 return getToscaElementByOperation(getVertexEither.left().value().get(0));
237 // region - ToscaElement - GetByOperation
238 private <T extends Component> Either<T, StorageOperationStatus> getToscaElementByOperation(GraphVertex componentV) {
239 return getToscaElementByOperation(componentV, new ComponentParametersView());
242 private <T extends Component> Either<T, StorageOperationStatus> getToscaElementByOperation(GraphVertex componentV,
243 ComponentParametersView filters) {
244 if (componentV == null) {
245 log.debug("Unexpected null value for `componentV`");
246 return Either.right(StorageOperationStatus.GENERAL_ERROR);
248 VertexTypeEnum label = componentV.getLabel();
250 ToscaElementOperation toscaOperation = getToscaElementOperation(componentV);
251 if (toscaOperation != null) {
252 log.debug("getToscaElementByOperation: toscaOperation={}", toscaOperation.getClass());
255 Either<ToscaElement, StorageOperationStatus> toscaElement;
256 String componentId = componentV.getUniqueId();
257 if (toscaOperation != null) {
258 log.debug("Need to fetch tosca element for id {}", componentId);
259 toscaElement = toscaOperation.getToscaElement(componentV, filters);
261 log.debug("not supported tosca type {} for id {}", label, componentId);
262 toscaElement = Either.right(StorageOperationStatus.BAD_REQUEST);
264 return toscaElement.left().map(ModelConverter::convertFromToscaElement);
269 private ToscaElementOperation getToscaElementOperation(GraphVertex componentV) {
270 VertexTypeEnum label = componentV.getLabel();
273 return nodeTypeOperation;
274 case TOPOLOGY_TEMPLATE:
275 return topologyTemplateOperation;
281 public <T extends Component> Either<T, StorageOperationStatus> createToscaComponent(T resource) {
282 ToscaElement toscaElement = ModelConverter.convertToToscaElement(resource);
284 ToscaElementOperation toscaElementOperation = getToscaElementOperation(resource);
285 Either<ToscaElement, StorageOperationStatus> createToscaElement = toscaElementOperation.createToscaElement(toscaElement);
286 if (createToscaElement.isLeft()) {
287 log.debug(COMPONENT_CREATED_SUCCESSFULLY);
288 T dataModel = ModelConverter.convertFromToscaElement(createToscaElement.left().value());
289 return Either.left(dataModel);
291 return Either.right(createToscaElement.right().value());
294 // region - ToscaElement Delete
295 public StorageOperationStatus markComponentToDelete(Component componentToDelete) {
297 if (Boolean.TRUE.equals(componentToDelete.getIsDeleted())
298 && Boolean.FALSE.equals(componentToDelete.isHighestVersion())) {
299 // component already marked for delete
300 return StorageOperationStatus.OK;
303 Either<GraphVertex, JanusGraphOperationStatus> getResponse = janusGraphDao
304 .getVertexById(componentToDelete.getUniqueId(), JsonParseFlagEnum.ParseAll);
305 if (getResponse.isRight()) {
306 log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentToDelete.getUniqueId(),
307 getResponse.right().value());
308 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getResponse.right().value());
311 GraphVertex componentV = getResponse.left().value();
313 // same operation for node type and topology template operations
314 Either<GraphVertex, StorageOperationStatus> result = nodeTypeOperation.markComponentToDelete(componentV);
315 if (result.isRight()) {
316 return result.right().value();
318 return StorageOperationStatus.OK;
322 public <T extends Component> Either<T, StorageOperationStatus> deleteToscaComponent(String componentId) {
324 Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao
325 .getVertexById(componentId, JsonParseFlagEnum.ParseAll);
326 if (getVertexEither.isRight()) {
327 log.debug("Couldn't fetch component vertex with and unique id {}, error: {}", componentId, getVertexEither.right().value());
328 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
331 Either<ToscaElement, StorageOperationStatus> deleteElement = deleteToscaElement(getVertexEither.left().value());
332 if (deleteElement.isRight()) {
333 log.debug("Failed to delete component with and unique id {}, error: {}", componentId, deleteElement.right().value());
334 return Either.right(deleteElement.right().value());
336 T dataModel = ModelConverter.convertFromToscaElement(deleteElement.left().value());
338 return Either.left(dataModel);
341 private Either<ToscaElement, StorageOperationStatus> deleteToscaElement(GraphVertex componentV) {
342 VertexTypeEnum label = componentV.getLabel();
343 Either<ToscaElement, StorageOperationStatus> toscaElement;
344 Object componentId = componentV.getUniqueId();
347 log.debug("Need to fetch node type for id {}", componentId);
348 toscaElement = nodeTypeOperation.deleteToscaElement(componentV);
350 case TOPOLOGY_TEMPLATE:
351 log.debug("Need to fetch topology template for id {}", componentId);
352 toscaElement = topologyTemplateOperation.deleteToscaElement(componentV);
355 log.debug("not supported tosca type {} for id {}", label, componentId);
356 toscaElement = Either.right(StorageOperationStatus.BAD_REQUEST);
363 private ToscaElementOperation getToscaElementOperation(Component component) {
364 return ModelConverter.isAtomicComponent(component) ? nodeTypeOperation : topologyTemplateOperation;
367 public <T extends Component> Either<T, StorageOperationStatus> getLatestByToscaResourceName(String toscaResourceName) {
368 return getLatestByName(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName);
371 public <T extends Component> Either<T, StorageOperationStatus> getFullLatestComponentByToscaResourceName(String toscaResourceName) {
372 ComponentParametersView fetchAllFilter = new ComponentParametersView();
373 fetchAllFilter.setIgnoreServicePath(true);
374 fetchAllFilter.setIgnoreCapabiltyProperties(false);
375 return getLatestByName(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName, JsonParseFlagEnum.ParseAll, fetchAllFilter);
378 public <T extends Component> Either<T, StorageOperationStatus> getLatestByName(String resourceName) {
379 return getLatestByName(GraphPropertyEnum.NAME, resourceName);
383 public StorageOperationStatus validateCsarUuidUniqueness(String csarUUID) {
385 Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
386 properties.put(GraphPropertyEnum.CSAR_UUID, csarUUID);
388 Either<List<GraphVertex>, JanusGraphOperationStatus> resources = janusGraphDao
389 .getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata);
391 if (resources.isRight()) {
392 if (resources.right().value() == JanusGraphOperationStatus.NOT_FOUND) {
393 return StorageOperationStatus.OK;
395 log.debug("failed to get resources from graph with property name: {}", csarUUID);
396 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(resources.right().value());
399 return StorageOperationStatus.ENTITY_ALREADY_EXISTS;
403 public <T extends Component> Either<Set<T>, StorageOperationStatus> getFollowed(String userId, Set<LifecycleStateEnum> lifecycleStates, Set<LifecycleStateEnum> lastStateStates, ComponentTypeEnum componentType) {
404 Either<List<ToscaElement>, StorageOperationStatus> followedResources;
405 if (componentType == ComponentTypeEnum.RESOURCE) {
406 followedResources = nodeTypeOperation.getFollowedComponent(userId, lifecycleStates, lastStateStates, componentType);
408 followedResources = topologyTemplateOperation.getFollowedComponent(userId, lifecycleStates, lastStateStates, componentType);
411 Set<T> components = new HashSet<>();
412 if (followedResources.isRight() && followedResources.right().value() != StorageOperationStatus.NOT_FOUND) {
413 return Either.right(followedResources.right().value());
415 if (followedResources.isLeft()) {
416 List<ToscaElement> toscaElements = followedResources.left().value();
417 toscaElements.forEach(te -> {
418 T component = ModelConverter.convertFromToscaElement(te);
419 components.add(component);
422 return Either.left(components);
425 public Either<Resource, StorageOperationStatus> getLatestCertifiedNodeTypeByToscaResourceName(String toscaResourceName) {
427 return getLatestCertifiedByToscaResourceName(toscaResourceName, VertexTypeEnum.NODE_TYPE, JsonParseFlagEnum.ParseMetadata);
430 public Either<Resource, StorageOperationStatus> getByToscaResourceNameMatchingVendorRelease(final String toscaResourceName, final String vendorVersion) {
432 return getByToscaResourceNameMatchingVendorRelease(toscaResourceName, VertexTypeEnum.NODE_TYPE, JsonParseFlagEnum.ParseMetadata, vendorVersion);
435 public Either<Resource, StorageOperationStatus> getByToscaResourceNameMatchingVendorRelease(String toscaResourceName,
436 VertexTypeEnum vertexType, JsonParseFlagEnum parseFlag, String vendorRelease) {
438 Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
439 props.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName);
440 props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
442 Map<String, Entry<JanusGraphPredicate, Object>> predicateCriteria = getVendorVersionPredicate(vendorRelease);
444 Either<List<GraphVertex>, JanusGraphOperationStatus> getLatestRes = janusGraphDao
445 .getByCriteria(vertexType, props, null, predicateCriteria, parseFlag);
447 if(getLatestRes.isRight() || CollectionUtils.isEmpty(getLatestRes.left().value())) {
448 getLatestRes = janusGraphDao.getByCriteria(vertexType, props, parseFlag);
454 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch {} with name {}. status={} ",
455 vertexType, toscaResourceName, status);
456 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status);
461 double version = 0.0;
462 GraphVertex highestResource = null;
463 for (GraphVertex resource : resources) {
464 double resourceVersion = Double
465 .parseDouble((String) resource.getJsonMetadataField(JsonPresentationFields.VERSION));
466 if (resourceVersion > version && isValidForVendorRelease(resource, vendorRelease)) {
467 version = resourceVersion;
468 highestResource = resource;
471 if (highestResource != null) {
472 return getToscaFullElement(highestResource.getUniqueId());
474 log.debug("The vertex with the highest version could not be found for {}", toscaResourceName);
475 return Either.right(StorageOperationStatus.GENERAL_ERROR);
481 private Map<String, Entry<JanusGraphPredicate, Object>> getVendorVersionPredicate(final String vendorRelease) {
482 Map<String, Entry<JanusGraphPredicate, Object>> predicateCriteria = new HashMap<>();
483 if (vendorRelease != null && vendorRelease != "1.0") {
484 String[] vendorReleaseElements = vendorRelease.split("\\.");
485 if (vendorReleaseElements.length > 0) {
486 String regex = ".*\"vendorRelease\":\"";
487 for (int i = 0; i <vendorReleaseElements.length; i++) {
488 regex += vendorReleaseElements[i];
489 regex += i < vendorReleaseElements.length -1 ? "\\." : "\".*";
491 predicateCriteria.put("metadata", new HashMap.SimpleEntry<>(REGEX, regex));
494 return predicateCriteria;
497 private boolean isValidForVendorRelease(final GraphVertex resource, final String vendorRelease) {
498 if (!vendorRelease.equals("1.0")) {
500 Semver resourceSemVer = new Semver((String)resource.getJsonMetadataField(JsonPresentationFields.VENDOR_RELEASE), SemverType.NPM);
501 Semver packageSemVer = new Semver(vendorRelease, SemverType.NPM);
502 return !resourceSemVer.isGreaterThan(packageSemVer);
503 } catch (Exception exception) {
504 log.debug("Error in comparing vendor release", exception);
511 public Either<Resource, StorageOperationStatus> getLatestCertifiedByToscaResourceName(String toscaResourceName,
512 VertexTypeEnum vertexType, JsonParseFlagEnum parseFlag) {
514 Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
515 props.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName);
516 props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
517 props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
518 Either<List<GraphVertex>, JanusGraphOperationStatus> getLatestRes = janusGraphDao
519 .getByCriteria(vertexType, props, parseFlag);
524 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch {} with name {}. status={} ",
525 vertexType, toscaResourceName, status);
526 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status);
531 double version = 0.0;
532 GraphVertex highestResource = null;
533 for (GraphVertex resource : resources) {
534 double resourceVersion = Double
535 .parseDouble((String) resource.getJsonMetadataField(JsonPresentationFields.VERSION));
536 if (resourceVersion > version) {
537 version = resourceVersion;
538 highestResource = resource;
541 if (highestResource != null) {
542 return getToscaFullElement(highestResource.getUniqueId());
544 log.debug("The vertex with the highest version could not be found for {}", toscaResourceName);
545 return Either.right(StorageOperationStatus.GENERAL_ERROR);
551 public Either<Resource, StorageOperationStatus> getLatestResourceByToscaResourceName(String toscaResourceName) {
552 if (toscaResourceName != null && toscaResourceName.contains("org.openecomp.resource.vf"))
553 return getLatestResourceByToscaResourceName(toscaResourceName, VertexTypeEnum.TOPOLOGY_TEMPLATE, JsonParseFlagEnum.ParseMetadata);
555 return getLatestResourceByToscaResourceName(toscaResourceName, VertexTypeEnum.NODE_TYPE, JsonParseFlagEnum.ParseMetadata);
558 public Either<Resource, StorageOperationStatus> getLatestResourceByToscaResourceName(String toscaResourceName, VertexTypeEnum vertexType, JsonParseFlagEnum parseFlag) {
560 Either<Resource, StorageOperationStatus> result = null;
561 Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
562 props.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName);
563 props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
564 if (!toscaResourceName.contains("org.openecomp.resource.vf")) {
565 props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
568 Either<List<GraphVertex>, JanusGraphOperationStatus> getLatestRes = janusGraphDao.getByCriteria(vertexType, props, parseFlag);
570 if (getLatestRes.isRight()) {
571 JanusGraphOperationStatus status = getLatestRes.right().value();
572 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch {} with name {}. status={} ", vertexType, toscaResourceName, status);
573 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
575 if (result == null) {
576 List<GraphVertex> resources = getLatestRes.left().value();
577 double version = 0.0;
578 GraphVertex highestResource = null;
579 for (GraphVertex resource : resources) {
580 double resourceVersion = Double.parseDouble((String) resource.getJsonMetadataField(JsonPresentationFields.VERSION));
581 if (resourceVersion > version) {
582 version = resourceVersion;
583 highestResource = resource;
587 if (highestResource != null) {
588 result = getToscaFullElement(highestResource.getUniqueId());
590 log.debug("The vertex with the highest version could not be found for {}", toscaResourceName);
591 result = Either.right(StorageOperationStatus.GENERAL_ERROR);
597 public Either<Boolean, StorageOperationStatus> validateToscaResourceNameExists(String templateName) {
598 Either<Boolean, StorageOperationStatus> validateUniquenessRes = validateToscaResourceNameUniqueness(templateName);
599 if (validateUniquenessRes.isLeft()) {
600 return Either.left(!validateUniquenessRes.left().value());
602 return validateUniquenessRes;
605 public Either<RequirementCapabilityRelDef, StorageOperationStatus> dissociateResourceInstances(String componentId, RequirementCapabilityRelDef requirementDef) {
606 return nodeTemplateOperation.dissociateResourceInstances(componentId, requirementDef);
610 * Allows to get fulfilled requirement by relation and received predicate
612 public Either<RequirementDataDefinition, StorageOperationStatus> getFulfilledRequirementByRelation(String componentId, String instanceId, RequirementCapabilityRelDef relation, BiPredicate<RelationshipInfo, RequirementDataDefinition> predicate) {
613 return nodeTemplateOperation.getFulfilledRequirementByRelation(componentId, instanceId, relation, predicate);
617 * Allows to get fulfilled capability by relation and received predicate
619 public Either<CapabilityDataDefinition, StorageOperationStatus> getFulfilledCapabilityByRelation(String componentId, String instanceId, RequirementCapabilityRelDef relation, BiPredicate<RelationshipInfo, CapabilityDataDefinition> predicate) {
620 return nodeTemplateOperation.getFulfilledCapabilityByRelation(componentId, instanceId, relation, predicate);
623 public Either<List<RequirementCapabilityRelDef>, StorageOperationStatus> associateResourceInstances(Component component, String componentId, List<RequirementCapabilityRelDef> relations) {
624 Either<List<RequirementCapabilityRelDef>, StorageOperationStatus> reqAndCapListEither = nodeTemplateOperation.associateResourceInstances(component, componentId, relations);
625 if (component != null) {
626 updateInstancesCapAndReqOnComponentFromDB(component);
628 return reqAndCapListEither;
632 protected Either<Boolean, StorageOperationStatus> validateToscaResourceNameUniqueness(String name) {
634 Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
635 properties.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, name);
637 Either<List<GraphVertex>, JanusGraphOperationStatus> resources = janusGraphDao
638 .getByCriteria(null, properties, JsonParseFlagEnum.ParseMetadata);
640 if (resources.isRight() && resources.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
641 log.debug("failed to get resources from graph with property name: {}", name);
642 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(resources.right().value()));
644 List<GraphVertex> resourceList = (resources.isLeft() ? resources.left().value() : null);
645 if (isNotEmpty(resourceList)) {
646 if (log.isDebugEnabled()) {
647 StringBuilder builder = new StringBuilder();
648 for (GraphVertex resourceData : resourceList) {
649 builder.append(resourceData.getUniqueId() + "|");
651 log.debug("resources with property name:{} exists in graph. found {}", name, builder);
653 return Either.left(false);
655 log.debug("resources with property name:{} does not exists in graph", name);
656 return Either.left(true);
661 // region - Component Update
663 public Either<Resource, StorageOperationStatus> overrideComponent(Resource newComponent, Resource oldComponent) {
665 copyArtifactsToNewComponent(newComponent, oldComponent);
667 Either<GraphVertex, JanusGraphOperationStatus> componentVEither = janusGraphDao
668 .getVertexById(oldComponent.getUniqueId(), JsonParseFlagEnum.NoParse);
669 if (componentVEither.isRight()) {
670 log.debug("Failed to fetch component {} error {}", oldComponent.getUniqueId(), componentVEither.right().value());
671 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(componentVEither.right().value()));
673 GraphVertex componentv = componentVEither.left().value();
674 Either<GraphVertex, JanusGraphOperationStatus> parentVertexEither = janusGraphDao.getParentVertex(componentv, EdgeLabelEnum.VERSION, JsonParseFlagEnum.NoParse);
675 if (parentVertexEither.isRight() && parentVertexEither.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
676 log.debug("Failed to fetch parent version for component {} error {}", oldComponent.getUniqueId(), parentVertexEither.right().value());
677 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(parentVertexEither.right().value()));
680 Either<ToscaElement, StorageOperationStatus> deleteToscaComponent = deleteToscaElement(componentv);
681 if (deleteToscaComponent.isRight()) {
682 log.debug("Failed to remove old component {} error {}", oldComponent.getUniqueId(), deleteToscaComponent.right().value());
683 return Either.right(deleteToscaComponent.right().value());
685 Either<Resource, StorageOperationStatus> createToscaComponent = createToscaComponent(newComponent);
686 if (createToscaComponent.isRight()) {
687 log.debug("Failed to create tosca element component {} error {}", newComponent.getUniqueId(), createToscaComponent.right().value());
688 return Either.right(createToscaComponent.right().value());
690 Resource newElement = createToscaComponent.left().value();
691 Either<GraphVertex, JanusGraphOperationStatus> newVersionEither = janusGraphDao
692 .getVertexById(newElement.getUniqueId(), JsonParseFlagEnum.NoParse);
693 if (newVersionEither.isRight()) {
694 log.debug("Failed to fetch new tosca element component {} error {}", newComponent.getUniqueId(), newVersionEither.right().value());
695 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(newVersionEither.right().value()));
697 if (parentVertexEither.isLeft()) {
698 GraphVertex previousVersionV = parentVertexEither.left().value();
699 JanusGraphOperationStatus createEdge = janusGraphDao.createEdge(previousVersionV, newVersionEither.left().value(), EdgeLabelEnum.VERSION, null);
700 if (createEdge != JanusGraphOperationStatus.OK) {
701 log.debug("Failed to associate to previous version {} new version {} error {}", previousVersionV.getUniqueId(), newVersionEither.right().value(), createEdge);
702 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(createEdge));
705 return Either.left(newElement);
708 void copyArtifactsToNewComponent(Resource newComponent, Resource oldComponent) {
709 // TODO - check if required
710 Map<String, ArtifactDefinition> toscaArtifacts = oldComponent.getToscaArtifacts();
711 if (toscaArtifacts != null && !toscaArtifacts.isEmpty()) {
712 toscaArtifacts.values().stream().forEach(a -> a.setDuplicated(Boolean.TRUE));
714 newComponent.setToscaArtifacts(toscaArtifacts);
716 Map<String, ArtifactDefinition> artifacts = oldComponent.getArtifacts();
717 if (artifacts != null && !artifacts.isEmpty()) {
718 artifacts.values().stream().forEach(a -> a.setDuplicated(Boolean.TRUE));
720 newComponent.setArtifacts(artifacts);
722 Map<String, ArtifactDefinition> depArtifacts = oldComponent.getDeploymentArtifacts();
723 if (depArtifacts != null && !depArtifacts.isEmpty()) {
724 depArtifacts.values().stream().forEach(a -> a.setDuplicated(Boolean.TRUE));
726 newComponent.setDeploymentArtifacts(depArtifacts);
729 newComponent.setLastUpdateDate(null);
730 newComponent.setHighestVersion(true);
733 public <T extends Component> Either<T, StorageOperationStatus> updateToscaElement(T componentToUpdate) {
734 return updateToscaElement(componentToUpdate, new ComponentParametersView());
737 public <T extends Component> Either<T, StorageOperationStatus> updateToscaElement(T componentToUpdate, ComponentParametersView filterResult) {
738 String componentId = componentToUpdate.getUniqueId();
739 Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao
740 .getVertexById(componentId, JsonParseFlagEnum.ParseAll);
741 if (getVertexEither.isRight()) {
742 log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
743 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
745 GraphVertex elementV = getVertexEither.left().value();
746 ToscaElementOperation toscaElementOperation = getToscaElementOperation(elementV);
748 ToscaElement toscaElementToUpdate = ModelConverter.convertToToscaElement(componentToUpdate);
749 Either<ToscaElement, StorageOperationStatus> updateToscaElement = null;
750 if (toscaElementOperation != null) {
751 updateToscaElement = toscaElementOperation.updateToscaElement(toscaElementToUpdate, elementV, filterResult);
753 log.debug("Null value returned by `getToscaElementOperation` with value {}", elementV);
754 updateToscaElement = Either.right(StorageOperationStatus.GENERAL_ERROR);
757 return updateToscaElement.bimap(
758 ModelConverter::convertFromToscaElement,
760 log.debug("Failed to update tosca element {} error {}", componentId, status);
765 private <T extends Component> Either<T, StorageOperationStatus> getLatestByName(GraphPropertyEnum property, String nodeName, JsonParseFlagEnum parseFlag) {
766 return getLatestByName(property, nodeName, parseFlag, new ComponentParametersView());
769 private <T extends Component> Either<T, StorageOperationStatus> getLatestByName(GraphPropertyEnum property, String nodeName, JsonParseFlagEnum parseFlag, ComponentParametersView filter) {
770 Either<T, StorageOperationStatus> result;
772 Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
773 Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
775 propertiesToMatch.put(property, nodeName);
776 propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
778 propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
780 Either<List<GraphVertex>, JanusGraphOperationStatus> highestResources = janusGraphDao
781 .getByCriteria(null, propertiesToMatch, propertiesNotToMatch, parseFlag);
782 if (highestResources.isRight()) {
783 JanusGraphOperationStatus status = highestResources.right().value();
784 log.debug("failed to find resource with name {}. status={} ", nodeName, status);
785 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
789 List<GraphVertex> resources = highestResources.left().value();
790 double version = 0.0;
791 GraphVertex highestResource = null;
792 for (GraphVertex vertex : resources) {
793 Object versionObj = vertex.getMetadataProperty(GraphPropertyEnum.VERSION);
794 double resourceVersion = Double.parseDouble((String) versionObj);
795 if (resourceVersion > version) {
796 version = resourceVersion;
797 highestResource = vertex;
800 return getToscaElementByOperation(highestResource, filter);
804 // region - Component Get By ..
805 private <T extends Component> Either<T, StorageOperationStatus> getLatestByName(GraphPropertyEnum property, String nodeName) {
806 return getLatestByName(property, nodeName, JsonParseFlagEnum.ParseMetadata);
809 public <T extends Component> Either<List<T>, StorageOperationStatus> getBySystemName(ComponentTypeEnum componentType, String systemName) {
811 Either<List<T>, StorageOperationStatus> result = null;
812 Either<T, StorageOperationStatus> getComponentRes;
813 List<T> components = new ArrayList<>();
814 List<GraphVertex> componentVertices;
815 Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
816 Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
818 propertiesToMatch.put(GraphPropertyEnum.SYSTEM_NAME, systemName);
819 if (componentType != null)
820 propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
822 propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
824 Either<List<GraphVertex>, JanusGraphOperationStatus> getComponentsRes = janusGraphDao
825 .getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll);
826 if (getComponentsRes.isRight()) {
827 JanusGraphOperationStatus status = getComponentsRes.right().value();
828 log.debug("Failed to fetch the component with system name {}. Status is {} ", systemName, status);
829 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
831 if (result == null) {
832 componentVertices = getComponentsRes.left().value();
833 for (GraphVertex componentVertex : componentVertices) {
834 getComponentRes = getToscaElementByOperation(componentVertex);
835 if (getComponentRes.isRight()) {
836 log.debug("Failed to get the component {}. Status is {} ", componentVertex.getJsonMetadataField(JsonPresentationFields.NAME), getComponentRes.right().value());
837 result = Either.right(getComponentRes.right().value());
840 T componentBySystemName = getComponentRes.left().value();
841 log.debug("Found component, id: {}", componentBySystemName.getUniqueId());
842 components.add(componentBySystemName);
845 if (result == null) {
846 result = Either.left(components);
851 public <T extends Component> Either<T, StorageOperationStatus> getComponentByNameAndVersion(ComponentTypeEnum componentType, String name, String version) {
852 return getComponentByNameAndVersion(componentType, name, version, JsonParseFlagEnum.ParseAll);
855 public <T extends Component> Either<T, StorageOperationStatus> getComponentByNameAndVersion(ComponentTypeEnum componentType, String name, String version, JsonParseFlagEnum parseFlag) {
856 Either<T, StorageOperationStatus> result;
858 Map<GraphPropertyEnum, Object> hasProperties = new EnumMap<>(GraphPropertyEnum.class);
859 Map<GraphPropertyEnum, Object> hasNotProperties = new EnumMap<>(GraphPropertyEnum.class);
861 hasProperties.put(GraphPropertyEnum.NAME, name);
862 hasProperties.put(GraphPropertyEnum.VERSION, version);
863 hasNotProperties.put(GraphPropertyEnum.IS_DELETED, true);
864 if (componentType != null) {
865 hasProperties.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
867 Either<List<GraphVertex>, JanusGraphOperationStatus> getResourceRes = janusGraphDao
868 .getByCriteria(null, hasProperties, hasNotProperties, parseFlag);
869 if (getResourceRes.isRight()) {
870 JanusGraphOperationStatus status = getResourceRes.right().value();
871 log.debug("failed to find resource with name {}, version {}. Status is {} ", name, version, status);
872 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
875 return getToscaElementByOperation(getResourceRes.left().value().get(0));
878 public <T extends Component> Either<T, StorageOperationStatus> getComponentByNameAndVendorRelease(
879 final ComponentTypeEnum componentType, final String name, final String vendorRelease,
880 final JsonParseFlagEnum parseFlag) {
882 Map<GraphPropertyEnum, Object> hasProperties = new EnumMap<>(GraphPropertyEnum.class);
883 Map<GraphPropertyEnum, Object> hasNotProperties = new EnumMap<>(GraphPropertyEnum.class);
885 hasProperties.put(GraphPropertyEnum.NAME, name);
886 hasNotProperties.put(GraphPropertyEnum.IS_DELETED, true);
887 if (componentType != null) {
888 hasProperties.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
890 Map<String, Entry<JanusGraphPredicate, Object>> predicateCriteria = getVendorVersionPredicate(vendorRelease);
892 Either<List<GraphVertex>, JanusGraphOperationStatus> getResourceRes = janusGraphDao
893 .getByCriteria(null, hasProperties, hasNotProperties, predicateCriteria, parseFlag);
894 if (getResourceRes.isRight()) {
895 JanusGraphOperationStatus status = getResourceRes.right().value();
896 log.debug("failed to find resource with name {}, version {}. Status is {} ", name, predicateCriteria, status);
897 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
899 return getToscaElementByOperation(getResourceRes.left().value().get(0));
902 public Either<List<CatalogComponent>, StorageOperationStatus> getCatalogOrArchiveComponents(boolean isCatalog, List<OriginTypeEnum> excludeTypes) {
903 List<ResourceTypeEnum> excludedResourceTypes = Optional.ofNullable(excludeTypes).orElse(Collections.emptyList()).stream().filter(type -> !type.equals(OriginTypeEnum.SERVICE)).map(type -> ResourceTypeEnum.getTypeByName(type.name()))
904 .collect(Collectors.toList());
905 return topologyTemplateOperation.getElementCatalogData(isCatalog, excludedResourceTypes);
909 public <T extends Component> Either<List<T>, StorageOperationStatus> getCatalogComponents(ComponentTypeEnum componentType, List<OriginTypeEnum> excludeTypes, boolean isHighestVersions) {
910 List<T> components = new ArrayList<>();
911 Either<List<ToscaElement>, StorageOperationStatus> catalogDataResult;
912 List<ToscaElement> toscaElements = new ArrayList<>();
913 List<ResourceTypeEnum> excludedResourceTypes = Optional.ofNullable(excludeTypes).orElse(Collections.emptyList()).stream().filter(type -> !type.equals(OriginTypeEnum.SERVICE)).map(type -> ResourceTypeEnum.getTypeByName(type.name()))
914 .collect(Collectors.toList());
916 switch (componentType) {
918 catalogDataResult = nodeTypeOperation.getElementCatalogData(ComponentTypeEnum.RESOURCE, excludedResourceTypes, isHighestVersions);
919 if (catalogDataResult.isRight()) {
920 return Either.right(catalogDataResult.right().value());
922 toscaElements = catalogDataResult.left().value();
925 if (excludeTypes != null && excludeTypes.contains(OriginTypeEnum.SERVICE)) {
928 catalogDataResult = topologyTemplateOperation.getElementCatalogData(ComponentTypeEnum.SERVICE, null, isHighestVersions);
929 if (catalogDataResult.isRight()) {
930 return Either.right(catalogDataResult.right().value());
932 toscaElements = catalogDataResult.left().value();
935 log.debug(NOT_SUPPORTED_COMPONENT_TYPE, componentType);
936 return Either.right(StorageOperationStatus.BAD_REQUEST);
938 toscaElements.forEach(te -> {
939 T component = ModelConverter.convertFromToscaElement(te);
940 components.add(component);
942 return Either.left(components);
945 public Either<List<String>, StorageOperationStatus> deleteMarkedElements(ComponentTypeEnum componentType) {
946 Either<List<GraphVertex>, StorageOperationStatus> allComponentsMarkedForDeletion;
947 switch (componentType) {
949 allComponentsMarkedForDeletion = nodeTypeOperation.getAllComponentsMarkedForDeletion(componentType);
953 allComponentsMarkedForDeletion = topologyTemplateOperation.getAllComponentsMarkedForDeletion(componentType);
956 log.debug(NOT_SUPPORTED_COMPONENT_TYPE, componentType);
957 return Either.right(StorageOperationStatus.BAD_REQUEST);
959 if (allComponentsMarkedForDeletion.isRight()) {
960 return Either.right(allComponentsMarkedForDeletion.right().value());
962 List<GraphVertex> allMarked = allComponentsMarkedForDeletion.left().value();
963 return Either.left(checkIfInUseAndDelete(allMarked));
966 private List<String> checkIfInUseAndDelete(List<GraphVertex> allMarked) {
967 final List<EdgeLabelEnum> forbiddenEdgeLabelEnums = Arrays.asList(EdgeLabelEnum.INSTANCE_OF, EdgeLabelEnum.PROXY_OF, EdgeLabelEnum.ALLOTTED_OF);
968 List<String> deleted = new ArrayList<>();
970 for (GraphVertex elementV : allMarked) {
971 boolean isAllowedToDelete = true;
973 for (EdgeLabelEnum edgeLabelEnum : forbiddenEdgeLabelEnums) {
974 Either<Edge, JanusGraphOperationStatus> belongingEdgeByCriteria = janusGraphDao
975 .getBelongingEdgeByCriteria(elementV, edgeLabelEnum, null);
976 if (belongingEdgeByCriteria.isLeft()) {
977 log.debug("Marked element {} in use. don't delete it", elementV.getUniqueId());
978 isAllowedToDelete = false;
983 if (isAllowedToDelete) {
984 Either<ToscaElement, StorageOperationStatus> deleteToscaElement = deleteToscaElement(elementV);
985 if (deleteToscaElement.isRight()) {
986 log.debug("Failed to delete marked element UniqueID {}, Name {}, error {}", elementV.getUniqueId(), elementV.getMetadataProperties().get(GraphPropertyEnum.NAME), deleteToscaElement.right().value());
989 deleted.add(elementV.getUniqueId());
995 public Either<List<String>, StorageOperationStatus> getAllComponentsMarkedForDeletion(ComponentTypeEnum componentType) {
996 Either<List<GraphVertex>, StorageOperationStatus> allComponentsMarkedForDeletion;
997 switch (componentType) {
999 allComponentsMarkedForDeletion = nodeTypeOperation.getAllComponentsMarkedForDeletion(componentType);
1003 allComponentsMarkedForDeletion = topologyTemplateOperation.getAllComponentsMarkedForDeletion(componentType);
1006 log.debug(NOT_SUPPORTED_COMPONENT_TYPE, componentType);
1007 return Either.right(StorageOperationStatus.BAD_REQUEST);
1009 if (allComponentsMarkedForDeletion.isRight()) {
1010 return Either.right(allComponentsMarkedForDeletion.right().value());
1012 return Either.left(allComponentsMarkedForDeletion.left().value().stream().map(GraphVertex::getUniqueId).collect(Collectors.toList()));
1015 // region - Component Update
1016 public Either<ImmutablePair<Component, String>, StorageOperationStatus> addComponentInstanceToTopologyTemplate(Component containerComponent, Component origComponent, ComponentInstance componentInstance, boolean allowDeleted, User user) {
1018 Either<ImmutablePair<Component, String>, StorageOperationStatus> result = null;
1019 Either<ToscaElement, StorageOperationStatus> updateContainerComponentRes = null;
1020 if (StringUtils.isEmpty(componentInstance.getIcon())) {
1021 componentInstance.setIcon(origComponent.getIcon());
1023 String nameToFindForCounter;
1024 switch (componentInstance.getOriginType()) {
1026 nameToFindForCounter = ValidationUtils.normaliseComponentName(componentInstance.getSourceModelName()) + PROXY_SUFFIX;
1028 case ServiceSubstitution:
1029 nameToFindForCounter = ValidationUtils.normaliseComponentName(componentInstance.getSourceModelName());
1032 nameToFindForCounter = origComponent.getName();
1034 String nextComponentInstanceCounter = getNextComponentInstanceCounter(containerComponent, nameToFindForCounter);
1035 Either<ImmutablePair<TopologyTemplate, String>, StorageOperationStatus> addResult = nodeTemplateOperation.addComponentInstanceToTopologyTemplate(ModelConverter.convertToToscaElement(containerComponent),
1036 ModelConverter.convertToToscaElement(origComponent), nextComponentInstanceCounter, componentInstance, allowDeleted, user);
1038 if (addResult.isRight()) {
1039 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to add the component instance {} to container component {}. ", componentInstance.getName(), containerComponent.getName());
1040 result = Either.right(addResult.right().value());
1042 if (result == null) {
1043 updateContainerComponentRes = topologyTemplateOperation.getToscaElement(containerComponent.getUniqueId());
1044 if (updateContainerComponentRes.isRight()) {
1045 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch updated topology template {} with updated component instance {}. ", containerComponent.getName(), componentInstance.getName());
1046 result = Either.right(updateContainerComponentRes.right().value());
1049 if (result == null) {
1050 Component updatedComponent = ModelConverter.convertFromToscaElement(updateContainerComponentRes.left().value());
1051 String createdInstanceId = addResult.left().value().getRight();
1052 CommonUtility.addRecordToLog(log, LogLevelEnum.TRACE, "The component instance {} has been added to container component {}. ", createdInstanceId, updatedComponent.getName());
1053 result = Either.left(new ImmutablePair<>(updatedComponent, createdInstanceId));
1058 public void associateComponentInstancesToComponent(Component containerComponent, Map<ComponentInstance, Resource> resourcesInstancesMap, boolean allowDeleted, boolean isUpdateCsar) {
1059 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Going to add component instances to component {}", containerComponent.getUniqueId());
1061 Either<GraphVertex, JanusGraphOperationStatus> metadataVertex = janusGraphDao
1062 .getVertexById(containerComponent.getUniqueId(), JsonParseFlagEnum.ParseAll);
1063 if (metadataVertex.isRight()) {
1064 JanusGraphOperationStatus status = metadataVertex.right().value();
1065 if (status == JanusGraphOperationStatus.NOT_FOUND) {
1066 status = JanusGraphOperationStatus.INVALID_ID;
1068 throw new StorageException(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
1071 Map<String, ComponentInstanceDataDefinition> compnentInstancesMap = nodeTemplateOperation.associateComponentInstancesToComponent(containerComponent, resourcesInstancesMap, metadataVertex.left().value(), allowDeleted, isUpdateCsar);
1073 containerComponent.setComponentInstances(ModelConverter.getComponentInstancesFromMapObject(compnentInstancesMap, containerComponent));
1077 public Either<ImmutablePair<Component, String>, StorageOperationStatus> updateComponentInstanceMetadataOfTopologyTemplate(Component containerComponent, Component origComponent, ComponentInstance componentInstance) {
1079 Either<ImmutablePair<Component, String>, StorageOperationStatus> result = null;
1081 CommonUtility.addRecordToLog(log, LogLevelEnum.TRACE, "Going to update the metadata of the component instance {} belonging to container component {}. ", componentInstance.getName(), containerComponent.getName());
1082 componentInstance.setIcon(origComponent.getIcon());
1083 Either<ImmutablePair<TopologyTemplate, String>, StorageOperationStatus> updateResult = nodeTemplateOperation.updateComponentInstanceMetadataOfTopologyTemplate(ModelConverter.convertToToscaElement(containerComponent),
1084 ModelConverter.convertToToscaElement(origComponent), componentInstance);
1085 if (updateResult.isRight()) {
1086 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to update the metadata of the component instance {} belonging to container component {}. ", componentInstance.getName(), containerComponent.getName());
1087 result = Either.right(updateResult.right().value());
1089 if (result == null) {
1090 Component updatedComponent = ModelConverter.convertFromToscaElement(updateResult.left().value().getLeft());
1091 String createdInstanceId = updateResult.left().value().getRight();
1092 CommonUtility.addRecordToLog(log, LogLevelEnum.TRACE, "The metadata of the component instance {} has been updated to container component {}. ", createdInstanceId, updatedComponent.getName());
1093 result = Either.left(new ImmutablePair<>(updatedComponent, createdInstanceId));
1098 public Either<Component, StorageOperationStatus> updateComponentInstanceMetadataOfTopologyTemplate(Component containerComponent) {
1099 return updateComponentInstanceMetadataOfTopologyTemplate(containerComponent, new ComponentParametersView());
1102 public Either<Component, StorageOperationStatus> updateComponentInstanceMetadataOfTopologyTemplate(Component containerComponent, ComponentParametersView filter) {
1104 Either<Component, StorageOperationStatus> result = null;
1106 CommonUtility.addRecordToLog(log, LogLevelEnum.TRACE, "Going to update the metadata belonging to container component {}. ", containerComponent.getName());
1108 Either<TopologyTemplate, StorageOperationStatus> updateResult = nodeTemplateOperation.updateComponentInstanceMetadataOfTopologyTemplate(ModelConverter.convertToToscaElement(containerComponent), filter);
1109 if (updateResult.isRight()) {
1110 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to update the metadata belonging to container component {}. ", containerComponent.getName());
1111 result = Either.right(updateResult.right().value());
1113 if (result == null) {
1114 Component updatedComponent = ModelConverter.convertFromToscaElement(updateResult.left().value());
1115 CommonUtility.addRecordToLog(log, LogLevelEnum.TRACE, "The metadata has been updated to container component {}. ", updatedComponent.getName());
1116 result = Either.left(updatedComponent);
1122 public Either<ImmutablePair<Component, String>, StorageOperationStatus> deleteComponentInstanceFromTopologyTemplate(Component containerComponent, String resourceInstanceId) {
1124 Either<ImmutablePair<Component, String>, StorageOperationStatus> result = null;
1126 CommonUtility.addRecordToLog(log, LogLevelEnum.TRACE, "Going to delete the component instance {} belonging to container component {}. ", resourceInstanceId, containerComponent.getName());
1128 Either<ImmutablePair<TopologyTemplate, String>, StorageOperationStatus> updateResult = nodeTemplateOperation.deleteComponentInstanceFromTopologyTemplate(ModelConverter.convertToToscaElement(containerComponent), resourceInstanceId);
1129 if (updateResult.isRight()) {
1130 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to delete the component instance {} belonging to container component {}. ", resourceInstanceId, containerComponent.getName());
1131 result = Either.right(updateResult.right().value());
1133 if (result == null) {
1134 Component updatedComponent = ModelConverter.convertFromToscaElement(updateResult.left().value().getLeft());
1135 String deletedInstanceId = updateResult.left().value().getRight();
1136 CommonUtility.addRecordToLog(log, LogLevelEnum.TRACE, "The component instance {} has been deleted from container component {}. ", deletedInstanceId, updatedComponent.getName());
1137 result = Either.left(new ImmutablePair<>(updatedComponent, deletedInstanceId));
1142 private String getNextComponentInstanceCounter(Component containerComponent, String originResourceName) {
1143 Integer nextCounter = 0;
1144 if (CollectionUtils.isNotEmpty(containerComponent.getComponentInstances())) {
1145 String normalizedName = ValidationUtils.normalizeComponentInstanceName(originResourceName);
1146 Integer maxCounter = getMaxCounterFromNamesAndIds(containerComponent, normalizedName);
1147 if (maxCounter != null) {
1148 nextCounter = maxCounter + 1;
1151 return nextCounter.toString();
1155 * @return max counter of component instance Id's, null if not found
1157 private Integer getMaxCounterFromNamesAndIds(Component containerComponent, String normalizedName) {
1158 List<String> countersInNames = containerComponent.getComponentInstances().stream()
1159 .filter(ci -> ci.getNormalizedName() != null && ci.getNormalizedName().startsWith(normalizedName))
1160 .map(ci -> ci.getNormalizedName().split(normalizedName)[1])
1161 .collect(Collectors.toList());
1162 List<String> countersInIds = containerComponent.getComponentInstances().stream()
1163 .filter(ci -> ci.getUniqueId() != null && ci.getUniqueId().contains(normalizedName))
1164 .map(ci -> ci.getUniqueId().split(normalizedName)[1])
1165 .collect(Collectors.toList());
1166 List<String> namesAndIdsList = new ArrayList<>(countersInNames);
1167 namesAndIdsList.addAll(countersInIds);
1168 return getMaxInteger(namesAndIdsList);
1171 private Integer getMaxInteger(List<String> counters) {
1172 Integer maxCounter = 0;
1173 Integer currCounter = null;
1174 for (String counter : counters) {
1176 currCounter = Integer.parseInt(counter);
1177 if (maxCounter < currCounter) {
1178 maxCounter = currCounter;
1180 } catch (NumberFormatException e) {
1184 return currCounter == null ? null : maxCounter;
1187 public Either<RequirementCapabilityRelDef, StorageOperationStatus> associateResourceInstances(Component component, String componentId, RequirementCapabilityRelDef requirementDef) {
1188 return nodeTemplateOperation.associateResourceInstances(component, componentId, requirementDef);
1192 public Either<List<InputDefinition>, StorageOperationStatus> createAndAssociateInputs(Map<String, InputDefinition> inputs, String componentId) {
1194 Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao
1195 .getVertexById(componentId, JsonParseFlagEnum.NoParse);
1196 if (getVertexEither.isRight()) {
1197 log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
1198 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
1202 GraphVertex vertex = getVertexEither.left().value();
1203 Map<String, PropertyDataDefinition> inputsMap = inputs.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> new PropertyDataDefinition(e.getValue())));
1205 StorageOperationStatus status = topologyTemplateOperation.associateInputsToComponent(vertex, inputsMap, componentId);
1207 if (StorageOperationStatus.OK == status) {
1208 log.debug(COMPONENT_CREATED_SUCCESSFULLY);
1209 List<InputDefinition> inputsResList = null;
1210 if (inputsMap != null && !inputsMap.isEmpty()) {
1211 inputsResList = inputsMap.values().stream()
1212 .map(InputDefinition::new)
1213 .collect(Collectors.toList());
1215 return Either.left(inputsResList);
1217 return Either.right(status);
1221 public Either<List<InputDefinition>, StorageOperationStatus> addInputsToComponent(Map<String, InputDefinition> inputs, String componentId) {
1223 Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao
1224 .getVertexById(componentId, JsonParseFlagEnum.NoParse);
1225 if (getVertexEither.isRight()) {
1226 log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
1227 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
1231 GraphVertex vertex = getVertexEither.left().value();
1232 Map<String, PropertyDefinition> inputsMap = inputs.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> new PropertyDefinition(e.getValue())));
1234 StorageOperationStatus status = topologyTemplateOperation.addToscaDataToToscaElement(vertex, EdgeLabelEnum.INPUTS, VertexTypeEnum.INPUTS, inputsMap, JsonPresentationFields.NAME);
1236 if (StorageOperationStatus.OK == status) {
1237 log.debug(COMPONENT_CREATED_SUCCESSFULLY);
1238 List<InputDefinition> inputsResList = null;
1239 if (inputsMap != null && !inputsMap.isEmpty()) {
1240 inputsResList = inputsMap.values().stream().map(InputDefinition::new).collect(Collectors.toList());
1242 return Either.left(inputsResList);
1244 return Either.right(status);
1249 * Add data types into a Component.
1251 * @param dataTypes datatypes to be added. the key should be each name of data type.
1252 * @param componentId unique ID of Component.
1253 * @return list of data types.
1255 public Either<List<DataTypeDefinition>, StorageOperationStatus> addDataTypesToComponent(Map<String, DataTypeDefinition> dataTypes, String componentId) {
1257 log.trace("#addDataTypesToComponent - enter, componentId={}", componentId);
1259 /* get component vertex */
1260 Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao
1261 .getVertexById(componentId, JsonParseFlagEnum.NoParse);
1262 if (getVertexEither.isRight()) {
1263 /* not found / error */
1264 log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
1265 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
1267 GraphVertex vertex = getVertexEither.left().value();
1268 log.trace("#addDataTypesToComponent - get vertex ok");
1270 // convert DataTypeDefinition to DataTypeDataDefinition
1271 Map<String, DataTypeDataDefinition> dataTypeDataMap = dataTypes.entrySet().stream()
1272 .collect(Collectors.toMap(Map.Entry::getKey, e -> convertDataTypeToDataTypeData(e.getValue())));
1274 // add datatype(s) to the Component.
1275 // if child vertex does not exist, it will be created.
1276 StorageOperationStatus status = topologyTemplateOperation.addToscaDataToToscaElement(vertex,
1277 EdgeLabelEnum.DATA_TYPES, VertexTypeEnum.DATA_TYPES, dataTypeDataMap, JsonPresentationFields.NAME);
1279 if (StorageOperationStatus.OK == status) {
1280 log.debug(COMPONENT_CREATED_SUCCESSFULLY);
1281 List<DataTypeDefinition> inputsResList = null;
1282 if (!dataTypes.isEmpty()) {
1283 inputsResList = new ArrayList<>(dataTypes.values());
1285 return Either.left(inputsResList);
1288 log.trace("#addDataTypesToComponent - leave");
1289 return Either.right(status);
1292 private DataTypeDataDefinition convertDataTypeToDataTypeData(DataTypeDefinition dataType) {
1293 DataTypeDataDefinition dataTypeData = new DataTypeDataDefinition(dataType);
1294 if (CollectionUtils.isNotEmpty(dataType.getProperties())) {
1295 List<PropertyDataDefinition> propertyDataList = dataType.getProperties().stream()
1296 .map(PropertyDataDefinition::new).collect(Collectors.toList());
1297 dataTypeData.setPropertiesData(propertyDataList);
1300 // if "derivedFrom" data_type exists, copy the name to "derivedFromName"
1301 if (dataType.getDerivedFrom() != null && StringUtils.isNotEmpty(dataType.getDerivedFrom().getName())) {
1302 // if names are different, log it
1303 if (!StringUtils.equals(dataTypeData.getDerivedFromName(), dataType.getDerivedFrom().getName())) {
1304 log.debug("#convertDataTypeToDataTypeData - derivedFromName(={}) overwritten by derivedFrom.name(={})",
1305 dataType.getDerivedFromName(), dataType.getDerivedFrom().getName());
1307 dataTypeData.setDerivedFromName(dataType.getDerivedFrom().getName());
1310 // supply "name" field to toscaPresentationValue in each datatype object for DAO operations
1311 dataTypeData.setToscaPresentationValue(JsonPresentationFields.NAME, dataType.getName());
1312 return dataTypeData;
1316 public Either<List<InputDefinition>, StorageOperationStatus> getComponentInputs(String componentId) {
1318 Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao
1319 .getVertexById(componentId, JsonParseFlagEnum.NoParse);
1320 if (getVertexEither.isRight()) {
1321 log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
1323 .right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
1327 Either<ToscaElement, StorageOperationStatus> toscaElement =
1328 topologyTemplateOperation.getToscaElement(componentId);
1329 if(toscaElement.isRight()) {
1330 return Either.right(toscaElement.right().value());
1333 TopologyTemplate topologyTemplate = (TopologyTemplate) toscaElement.left().value();
1335 Map<String, PropertyDataDefinition> inputsMap = topologyTemplate.getInputs();
1337 List<InputDefinition> inputs = new ArrayList<>();
1338 if(MapUtils.isNotEmpty(inputsMap)) {
1340 inputsMap.values().stream().map(p -> new InputDefinition(p)).collect(Collectors.toList());
1343 return Either.left(inputs);
1346 public Either<List<InputDefinition>, StorageOperationStatus> updateInputsToComponent(List<InputDefinition> inputs, String componentId) {
1348 Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao
1349 .getVertexById(componentId, JsonParseFlagEnum.NoParse);
1350 if (getVertexEither.isRight()) {
1351 log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
1352 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
1356 GraphVertex vertex = getVertexEither.left().value();
1357 List<PropertyDataDefinition> inputsAsDataDef = inputs.stream().map(PropertyDataDefinition::new).collect(Collectors.toList());
1359 StorageOperationStatus status = topologyTemplateOperation.updateToscaDataOfToscaElement(vertex, EdgeLabelEnum.INPUTS, VertexTypeEnum.INPUTS, inputsAsDataDef, JsonPresentationFields.NAME);
1361 if (StorageOperationStatus.OK == status) {
1362 log.debug(COMPONENT_CREATED_SUCCESSFULLY);
1363 List<InputDefinition> inputsResList = null;
1364 if (inputsAsDataDef != null && !inputsAsDataDef.isEmpty()) {
1365 inputsResList = inputsAsDataDef.stream().map(InputDefinition::new).collect(Collectors.toList());
1367 return Either.left(inputsResList);
1369 return Either.right(status);
1373 // region - ComponentInstance
1374 public Either<Map<String, List<ComponentInstanceProperty>>, StorageOperationStatus> associateComponentInstancePropertiesToComponent(Map<String, List<ComponentInstanceProperty>> instProperties, String componentId) {
1376 Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao
1377 .getVertexById(componentId, JsonParseFlagEnum.NoParse);
1378 if (getVertexEither.isRight()) {
1379 log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
1380 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
1384 GraphVertex vertex = getVertexEither.left().value();
1385 Map<String, MapPropertiesDataDefinition> instPropsMap = new HashMap<>();
1386 if (instProperties != null) {
1388 MapPropertiesDataDefinition propertiesMap;
1389 for (Entry<String, List<ComponentInstanceProperty>> entry : instProperties.entrySet()) {
1390 propertiesMap = new MapPropertiesDataDefinition();
1392 propertiesMap.setMapToscaDataDefinition(entry.getValue().stream().map(PropertyDataDefinition::new).collect(Collectors.toMap(PropertyDataDefinition::getName, e -> e)));
1394 instPropsMap.put(entry.getKey(), propertiesMap);
1398 StorageOperationStatus status = topologyTemplateOperation.associateInstPropertiesToComponent(vertex, instPropsMap);
1400 if (StorageOperationStatus.OK == status) {
1401 log.debug(COMPONENT_CREATED_SUCCESSFULLY);
1402 return Either.left(instProperties);
1404 return Either.right(status);
1409 * saves the instInputs as the updated instance inputs of the component container in DB
1411 public Either<Map<String, List<ComponentInstanceInput>>, StorageOperationStatus> updateComponentInstanceInputsToComponent(Map<String, List<ComponentInstanceInput>> instInputs, String componentId) {
1412 if (instInputs == null || instInputs.isEmpty()) {
1413 return Either.left(instInputs);
1415 StorageOperationStatus status;
1416 for (Entry<String, List<ComponentInstanceInput>> inputsPerIntance : instInputs.entrySet()) {
1417 List<ComponentInstanceInput> toscaDataListPerInst = inputsPerIntance.getValue();
1418 List<String> pathKeysPerInst = new ArrayList<>();
1419 pathKeysPerInst.add(inputsPerIntance.getKey());
1420 status = topologyTemplateOperation.updateToscaDataDeepElementsOfToscaElement(componentId, EdgeLabelEnum.INST_INPUTS, VertexTypeEnum.INST_INPUTS, toscaDataListPerInst, pathKeysPerInst, JsonPresentationFields.NAME);
1421 if (status != StorageOperationStatus.OK) {
1422 log.debug("Failed to update component instance inputs for instance {} in component {} edge type {} error {}", inputsPerIntance.getKey(), componentId, EdgeLabelEnum.INST_INPUTS, status);
1423 return Either.right(status);
1427 return Either.left(instInputs);
1431 * saves the instProps as the updated instance properties of the component container in DB
1433 public Either<Map<String, List<ComponentInstanceProperty>>, StorageOperationStatus> updateComponentInstancePropsToComponent(Map<String, List<ComponentInstanceProperty>> instProps, String componentId) {
1434 if (instProps == null || instProps.isEmpty()) {
1435 return Either.left(instProps);
1437 StorageOperationStatus status;
1438 for (Entry<String, List<ComponentInstanceProperty>> propsPerIntance : instProps.entrySet()) {
1439 List<ComponentInstanceProperty> toscaDataListPerInst = propsPerIntance.getValue();
1440 List<String> pathKeysPerInst = new ArrayList<>();
1441 pathKeysPerInst.add(propsPerIntance.getKey());
1442 status = topologyTemplateOperation.updateToscaDataDeepElementsOfToscaElement(componentId, EdgeLabelEnum.INST_PROPERTIES, VertexTypeEnum.INST_PROPERTIES, toscaDataListPerInst, pathKeysPerInst, JsonPresentationFields.NAME);
1443 if (status != StorageOperationStatus.OK) {
1444 log.debug("Failed to update component instance inputs for instance {} in component {} edge type {} error {}", propsPerIntance.getKey(), componentId, EdgeLabelEnum.INST_PROPERTIES, status);
1445 return Either.right(status);
1449 return Either.left(instProps);
1452 public Either<Map<String, List<ComponentInstanceInput>>, StorageOperationStatus> associateComponentInstanceInputsToComponent(Map<String, List<ComponentInstanceInput>> instInputs, String componentId) {
1454 Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao
1455 .getVertexById(componentId, JsonParseFlagEnum.NoParse);
1456 if (getVertexEither.isRight()) {
1457 log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
1458 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
1461 GraphVertex vertex = getVertexEither.left().value();
1462 Map<String, MapPropertiesDataDefinition> instPropsMap = new HashMap<>();
1463 if (instInputs != null) {
1465 MapPropertiesDataDefinition propertiesMap;
1466 for (Entry<String, List<ComponentInstanceInput>> entry : instInputs.entrySet()) {
1467 propertiesMap = new MapPropertiesDataDefinition();
1469 propertiesMap.setMapToscaDataDefinition(entry.getValue().stream().map(PropertyDataDefinition::new).collect(Collectors.toMap(PropertyDataDefinition::getName, e -> e)));
1471 instPropsMap.put(entry.getKey(), propertiesMap);
1475 StorageOperationStatus status = topologyTemplateOperation.associateInstInputsToComponent(vertex, instPropsMap);
1477 if (StorageOperationStatus.OK == status) {
1478 log.debug(COMPONENT_CREATED_SUCCESSFULLY);
1479 return Either.left(instInputs);
1481 return Either.right(status);
1485 public Either<Map<String, List<ComponentInstanceInput>>, StorageOperationStatus> addComponentInstanceInputsToComponent(Component containerComponent, Map<String, List<ComponentInstanceInput>> instProperties) {
1486 requireNonNull(instProperties);
1487 StorageOperationStatus status;
1488 for (Entry<String, List<ComponentInstanceInput>> entry : instProperties.entrySet()) {
1489 List<ComponentInstanceInput> props = entry.getValue();
1490 String componentInstanceId = entry.getKey();
1491 if (!isEmpty(props)) {
1492 for (ComponentInstanceInput property : props) {
1493 List<ComponentInstanceInput> componentInstancesInputs = containerComponent.getComponentInstancesInputs().get(componentInstanceId);
1494 Optional<ComponentInstanceInput> instanceProperty = componentInstancesInputs.stream()
1495 .filter(p -> p.getName().equals(property.getName()))
1497 if (instanceProperty.isPresent()) {
1498 status = updateComponentInstanceInput(containerComponent, componentInstanceId, property);
1500 status = addComponentInstanceInput(containerComponent, componentInstanceId, property);
1502 if (status != StorageOperationStatus.OK) {
1503 log.debug("Failed to update instance input {} for instance {} error {} ", property, componentInstanceId, status);
1504 return Either.right(status);
1506 log.trace("instance input {} for instance {} updated", property, componentInstanceId);
1511 return Either.left(instProperties);
1514 public Either<Map<String, List<ComponentInstanceProperty>>, StorageOperationStatus> addComponentInstancePropertiesToComponent(Component containerComponent, Map<String, List<ComponentInstanceProperty>> instProperties) {
1515 requireNonNull(instProperties);
1516 for (Entry<String, List<ComponentInstanceProperty>> entry : instProperties.entrySet()) {
1517 List<ComponentInstanceProperty> props = entry.getValue();
1518 String componentInstanceId = entry.getKey();
1519 List<ComponentInstanceProperty> originalComponentInstProps =
1520 containerComponent.getComponentInstancesProperties().get(componentInstanceId);
1521 Map<String, List<CapabilityDefinition>> containerComponentCapabilities = containerComponent.getCapabilities();
1523 if(isEmpty(props)) {
1526 for (ComponentInstanceProperty property : props) {
1527 StorageOperationStatus status = null;
1528 String propertyParentUniqueId = property.getParentUniqueId();
1529 Optional<CapabilityDefinition>
1530 capPropDefinition = getPropertyCapability(propertyParentUniqueId, containerComponent);
1531 if(capPropDefinition.isPresent() && MapUtils.isNotEmpty(containerComponentCapabilities)) {
1532 status = populateAndUpdateInstanceCapProperty(containerComponent, componentInstanceId,
1533 containerComponentCapabilities, property, capPropDefinition.get());
1535 if(status == null) {
1536 status = updateOrAddComponentInstanceProperty(containerComponent, componentInstanceId,
1537 originalComponentInstProps, property);
1539 if(status != StorageOperationStatus.OK) {
1540 return Either.right(status);
1544 return Either.left(instProperties);
1547 private StorageOperationStatus populateAndUpdateInstanceCapProperty(Component containerComponent, String componentInstanceId,
1548 Map<String, List<CapabilityDefinition>> containerComponentCapabilities,
1549 ComponentInstanceProperty property,
1550 CapabilityDefinition capabilityDefinition) {
1551 List<CapabilityDefinition> capabilityDefinitions = containerComponentCapabilities.get(capabilityDefinition.getType());
1552 if(CollectionUtils.isEmpty(capabilityDefinitions)) {
1555 Optional<CapabilityDefinition> capDefToGetProp = capabilityDefinitions.stream()
1556 .filter(cap -> cap.getUniqueId().equals(capabilityDefinition.getUniqueId()) && cap.getPath().size() == 1).findAny();
1557 if(capDefToGetProp.isPresent()) {
1558 return updateInstanceCapabilityProperty(containerComponent, componentInstanceId, property, capDefToGetProp.get());
1563 private static Optional<CapabilityDefinition> getPropertyCapability(String propertyParentUniqueId,
1564 Component containerComponent) {
1566 Map<String, List<CapabilityDefinition>> componentCapabilities = containerComponent.getCapabilities();
1567 if(MapUtils.isEmpty(componentCapabilities)){
1568 return Optional.empty();
1570 List<CapabilityDefinition> capabilityDefinitionList = componentCapabilities.values()
1571 .stream().flatMap(Collection::stream).collect(Collectors.toList());
1572 if(CollectionUtils.isEmpty(capabilityDefinitionList)){
1573 return Optional.empty();
1575 return capabilityDefinitionList.stream()
1576 .filter(capabilityDefinition -> capabilityDefinition.getUniqueId().equals(propertyParentUniqueId))
1580 private StorageOperationStatus updateOrAddComponentInstanceProperty(Component containerComponent,
1581 String componentInstanceId, List<ComponentInstanceProperty> originalComponentInstProps,
1582 ComponentInstanceProperty property)
1584 StorageOperationStatus status;
1585 // check if the property already exists or not
1586 Optional<ComponentInstanceProperty> instanceProperty = originalComponentInstProps.stream()
1587 .filter(p -> p.getUniqueId().equals(property.getUniqueId())).findAny();
1588 if (instanceProperty.isPresent()) {
1589 status = updateComponentInstanceProperty(containerComponent, componentInstanceId, property);
1591 status = addComponentInstanceProperty(containerComponent, componentInstanceId, property);
1593 if (status != StorageOperationStatus.OK) {
1594 log.debug("Failed to update instance property {} for instance {} error {} ",
1595 property, componentInstanceId, status);
1600 public StorageOperationStatus updateInstanceCapabilityProperty(Component containerComponent, String componentInstanceId,
1601 ComponentInstanceProperty property,
1602 CapabilityDefinition capabilityDefinition) {
1603 Optional<ComponentInstance> fetchedCIOptional = containerComponent.getComponentInstanceById(componentInstanceId);
1604 if(!fetchedCIOptional.isPresent()) {
1605 return StorageOperationStatus.GENERAL_ERROR;
1607 Either<Component, StorageOperationStatus> getComponentRes =
1608 getToscaFullElement(fetchedCIOptional.get().getComponentUid());
1609 if(getComponentRes.isRight()) {
1610 return StorageOperationStatus.GENERAL_ERROR;
1612 Optional<Component> componentOptional = isNodeServiceProxy(getComponentRes.left().value());
1614 if(!componentOptional.isPresent()) {
1615 propOwner = componentInstanceId;
1617 propOwner = fetchedCIOptional.get().getSourceModelUid();
1619 StorageOperationStatus status;
1620 StringBuilder sb = new StringBuilder(componentInstanceId);
1621 sb.append(ModelConverter.CAP_PROP_DELIM).append(propOwner).append(ModelConverter.CAP_PROP_DELIM)
1622 .append(capabilityDefinition.getType()).append(ModelConverter.CAP_PROP_DELIM).append(capabilityDefinition.getName());
1623 String capKey = sb.toString();
1624 status = updateComponentInstanceCapabiltyProperty(containerComponent, componentInstanceId, capKey, property);
1625 if (status != StorageOperationStatus.OK) {
1626 log.debug("Failed to update instance capability property {} for instance {} error {} ", property,
1627 componentInstanceId, status);
1630 return StorageOperationStatus.OK;
1633 private Optional<Component> isNodeServiceProxy(Component component) {
1634 if (component.getComponentType().equals(ComponentTypeEnum.SERVICE)) {
1635 return Optional.empty();
1637 Resource resource = (Resource) component;
1638 ResourceTypeEnum resType = resource.getResourceType();
1639 if(resType.equals(ResourceTypeEnum.ServiceProxy)) {
1640 return Optional.of(component);
1642 return Optional.empty();
1645 public StorageOperationStatus associateCapabilitiesToService(Map<String,ListCapabilityDataDefinition> capabilities, String componentId) {
1647 Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
1648 if (getVertexEither.isRight()) {
1649 log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
1650 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value());
1654 GraphVertex vertex = getVertexEither.left().value();
1655 if(MapUtils.isNotEmpty(capabilities)) {
1656 Either<GraphVertex, StorageOperationStatus> associateElementToData = topologyTemplateOperation.
1657 associateElementToData(vertex, VertexTypeEnum.CAPABILITIES,
1658 EdgeLabelEnum.CAPABILITIES, capabilities);
1659 if (associateElementToData.isRight()) {
1660 return associateElementToData.right().value();
1665 return StorageOperationStatus.OK;
1669 public StorageOperationStatus associateRequirementsToService(Map<String, ListRequirementDataDefinition> requirements, String componentId) {
1671 Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
1672 if (getVertexEither.isRight()) {
1673 log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
1674 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value());
1678 GraphVertex vertex = getVertexEither.left().value();
1679 if(MapUtils.isNotEmpty(requirements)) {
1680 Either<GraphVertex, StorageOperationStatus> associateElementToData = topologyTemplateOperation.
1681 associateElementToData(vertex, VertexTypeEnum.REQUIREMENTS,
1682 EdgeLabelEnum.REQUIREMENTS, requirements);
1683 if (associateElementToData.isRight()) {
1684 return associateElementToData.right().value();
1688 return StorageOperationStatus.OK;
1692 public StorageOperationStatus associateDeploymentArtifactsToInstances(Map<String, Map<String, ArtifactDefinition>> instDeploymentArtifacts, Component component, User user) {
1694 Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao.getVertexById(component.getUniqueId(), JsonParseFlagEnum.NoParse);
1695 if (getVertexEither.isRight()) {
1696 log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, component.getUniqueId(), getVertexEither.right().value());
1697 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value());
1701 GraphVertex vertex = getVertexEither.left().value();
1702 Map<String, MapArtifactDataDefinition> instArtMap = new HashMap<>();
1703 if (instDeploymentArtifacts != null) {
1705 MapArtifactDataDefinition artifactsMap;
1706 for (Entry<String, Map<String, ArtifactDefinition>> entry : instDeploymentArtifacts.entrySet()) {
1707 Map<String, ArtifactDefinition> artList = entry.getValue();
1708 Map<String, ArtifactDataDefinition> artifacts = artList.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> new ArtifactDataDefinition(e.getValue())));
1709 artifactsMap = nodeTemplateOperation.prepareInstDeploymentArtifactPerInstance(artifacts, entry.getKey(), user, NodeTemplateOperation.HEAT_VF_ENV_NAME);
1711 instArtMap.put(entry.getKey(), artifactsMap);
1714 ModelConverter.setComponentInstancesDeploymentArtifactsToComponent(instArtMap, component);
1715 return topologyTemplateOperation.associateInstDeploymentArtifactsToComponent(vertex, instArtMap);
1718 public StorageOperationStatus associateArtifactsToInstances(Map<String, Map<String, ArtifactDefinition>> instArtifacts, Component component) {
1720 Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao.getVertexById(component.getUniqueId(), JsonParseFlagEnum.NoParse);
1721 if (getVertexEither.isRight()) {
1722 log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, component.getUniqueId(), getVertexEither.right().value());
1723 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value());
1727 GraphVertex vertex = getVertexEither.left().value();
1728 Map<String, MapArtifactDataDefinition> instArtMap = new HashMap<>();
1729 if (instArtifacts != null) {
1731 MapArtifactDataDefinition artifactsMap;
1732 for (Entry<String, Map<String, ArtifactDefinition>> entry : instArtifacts.entrySet()) {
1733 Map<String, ArtifactDefinition> artList = entry.getValue();
1734 Map<String, ArtifactDataDefinition> artifacts = artList.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> new ArtifactDataDefinition(e.getValue())));
1735 artifactsMap = new MapArtifactDataDefinition(artifacts);
1737 instArtMap.put(entry.getKey(), artifactsMap);
1740 ModelConverter.setComponentInstancesInformationalArtifactsToComponent(instArtMap, component);
1741 return topologyTemplateOperation.associateInstArtifactsToComponent(vertex, instArtMap);
1745 public StorageOperationStatus associateInstAttributeToComponentToInstances(Map<String, List<AttributeDefinition>> instArttributes, Component component) {
1747 Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao.getVertexById(component.getUniqueId(), JsonParseFlagEnum.NoParse);
1748 if (getVertexEither.isRight()) {
1749 log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, component.getUniqueId(), getVertexEither.right().value());
1750 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value());
1754 GraphVertex vertex = getVertexEither.left().value();
1755 Map<String, MapAttributesDataDefinition> instAttr = new HashMap<>();
1756 if (instArttributes != null) {
1758 MapAttributesDataDefinition attributesMap;
1759 for (Entry<String, List<AttributeDefinition>> entry : instArttributes.entrySet()) {
1760 final List<AttributeDefinition> value = entry.getValue();
1761 attributesMap = new MapAttributesDataDefinition();
1762 attributesMap.setMapToscaDataDefinition(value.stream().map(AttributeDefinition::new)
1763 .collect(Collectors.toMap(AttributeDefinition::getName, e -> e)));
1764 instAttr.put(entry.getKey(), attributesMap);
1767 setComponentInstanceAttributesOnComponent(component, instAttr);
1768 return topologyTemplateOperation.associateInstAttributeToComponent(vertex, instAttr);
1772 private void setComponentInstanceAttributesOnComponent(Component resource, Map<String, MapAttributesDataDefinition> instAttr) {
1773 Map<String, List<ComponentInstanceAttribute>> componentInstancesAttributes = resource.getComponentInstancesAttributes();
1774 if (componentInstancesAttributes == null)
1775 componentInstancesAttributes = new HashMap<>();
1776 componentInstancesAttributes.putAll(ModelConverter.getComponentInstancesAttributes(instAttr));
1777 resource.setComponentInstancesAttributes(componentInstancesAttributes);
1780 public StorageOperationStatus associateOrAddCalculatedCapReq(Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> instCapabilties, Map<ComponentInstance, Map<String, List<RequirementDefinition>>> instReg, Component component) {
1781 Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao.getVertexById(component.getUniqueId(), JsonParseFlagEnum.NoParse);
1782 if (getVertexEither.isRight()) {
1783 log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, component.getUniqueId(), getVertexEither.right().value());
1784 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value());
1788 GraphVertex vertex = getVertexEither.left().value();
1790 Map<String, MapListRequirementDataDefinition> calcRequirements = new HashMap<>();
1792 Map<String, MapListCapabilityDataDefinition> calcCapabilty = new HashMap<>();
1793 Map<String, MapCapabilityProperty> calculatedCapabilitiesProperties = new HashMap<>();
1794 if (instCapabilties != null) {
1795 for (Entry<ComponentInstance, Map<String, List<CapabilityDefinition>>> entry : instCapabilties.entrySet()) {
1797 Map<String, List<CapabilityDefinition>> caps = entry.getValue();
1798 Map<String, ListCapabilityDataDefinition> mapToscaDataDefinition = new HashMap<>();
1799 for (Entry<String, List<CapabilityDefinition>> instCapability : caps.entrySet()) {
1800 mapToscaDataDefinition.put(instCapability.getKey(), new ListCapabilityDataDefinition(instCapability.getValue().stream().map(CapabilityDataDefinition::new).collect(Collectors.toList())));
1803 ComponentInstanceDataDefinition componentInstance = new ComponentInstanceDataDefinition(entry.getKey());
1804 MapListCapabilityDataDefinition capMap = nodeTemplateOperation.prepareCalculatedCapabiltyForNodeType(mapToscaDataDefinition, componentInstance);
1806 MapCapabilityProperty mapCapabilityProperty = ModelConverter.convertToMapOfMapCapabiltyProperties(caps, componentInstance.getUniqueId(), true);
1808 calcCapabilty.put(entry.getKey().getUniqueId(), capMap);
1809 calculatedCapabilitiesProperties.put(entry.getKey().getUniqueId(), mapCapabilityProperty);
1813 if (instReg != null) {
1814 for (Entry<ComponentInstance, Map<String, List<RequirementDefinition>>> entry : instReg.entrySet()) {
1816 Map<String, List<RequirementDefinition>> req = entry.getValue();
1817 Map<String, ListRequirementDataDefinition> mapToscaDataDefinition = new HashMap<>();
1818 for (Entry<String, List<RequirementDefinition>> instReq : req.entrySet()) {
1819 mapToscaDataDefinition.put(instReq.getKey(), new ListRequirementDataDefinition(instReq.getValue().stream().map(RequirementDataDefinition::new).collect(Collectors.toList())));
1822 MapListRequirementDataDefinition reqMap = nodeTemplateOperation.prepareCalculatedRequirementForNodeType(mapToscaDataDefinition, new ComponentInstanceDataDefinition(entry.getKey()));
1824 String componentInstanceId = entry.getKey().getUniqueId();
1825 calcRequirements.put(componentInstanceId, reqMap);
1829 StorageOperationStatus storageOperationStatus = topologyTemplateOperation.associateOrAddCalcCapReqToComponent(vertex, calcRequirements, calcCapabilty, calculatedCapabilitiesProperties);
1830 updateInstancesCapAndReqOnComponentFromDB(component);
1831 return storageOperationStatus;
1834 private void updateInstancesCapAndReqOnComponentFromDB(Component component) {
1835 ComponentParametersView componentParametersView = new ComponentParametersView(true);
1836 componentParametersView.setIgnoreCapabilities(false);
1837 componentParametersView.setIgnoreRequirements(false);
1838 componentParametersView.setIgnoreCapabiltyProperties(false);
1839 componentParametersView.setIgnoreComponentInstances(false);
1840 Either<Component, StorageOperationStatus> componentEither = getToscaElement(component.getUniqueId(), componentParametersView);
1841 if (componentEither.isRight()) {
1842 throw new StorageException(StorageOperationStatus.NOT_FOUND);
1844 Component updatedComponent = componentEither.left().value();
1845 component.setCapabilities(updatedComponent.getCapabilities());
1846 component.setRequirements(updatedComponent.getRequirements());
1847 component.setComponentInstances(updatedComponent.getComponentInstances());
1850 private Either<List<Service>, StorageOperationStatus> getLatestVersionNonCheckoutServicesMetadataOnly(Map<GraphPropertyEnum, Object> hasProps, Map<GraphPropertyEnum, Object> hasNotProps) {
1851 List<Service> services = new ArrayList<>();
1852 List<LifecycleStateEnum> states = new ArrayList<>();
1854 hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
1855 hasProps.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
1858 states.add(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
1859 hasNotProps.put(GraphPropertyEnum.STATE, states);
1860 hasNotProps.put(GraphPropertyEnum.IS_DELETED, true);
1861 hasNotProps.put(GraphPropertyEnum.IS_ARCHIVED, true);
1862 return fetchServicesByCriteria(services, hasProps, hasNotProps);
1865 private Either<List<Component>, StorageOperationStatus> getLatestVersionNotAbstractToscaElementsMetadataOnly(boolean isAbstract, ComponentTypeEnum componentTypeEnum, String internalComponentType, VertexTypeEnum vertexType) {
1866 List<Service> services = null;
1867 Map<GraphPropertyEnum, Object> hasProps = new EnumMap<>(GraphPropertyEnum.class);
1868 Map<GraphPropertyEnum, Object> hasNotProps = new EnumMap<>(GraphPropertyEnum.class);
1869 fillPropsMap(hasProps, hasNotProps, internalComponentType, componentTypeEnum, isAbstract, vertexType);
1870 Either<List<GraphVertex>, JanusGraphOperationStatus> getRes = janusGraphDao
1871 .getByCriteria(vertexType, hasProps, hasNotProps, JsonParseFlagEnum.ParseMetadata);
1872 if (getRes.isRight()) {
1873 if (getRes.right().value().equals(JanusGraphOperationStatus.NOT_FOUND)) {
1874 return Either.left(new ArrayList<>());
1876 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getRes.right().value()));
1879 // region -> Fetch non checked-out services
1880 if (internalComponentType != null && internalComponentType.toLowerCase().trim().equals(SERVICE) && VertexTypeEnum.NODE_TYPE == vertexType) {
1881 Either<List<Service>, StorageOperationStatus> result = getLatestVersionNonCheckoutServicesMetadataOnly(new EnumMap<>(GraphPropertyEnum.class), new EnumMap<>(GraphPropertyEnum.class));
1882 if (result.isRight()) {
1883 log.debug("Failed to fetch services for");
1884 return Either.right(result.right().value());
1886 services = result.left().value();
1887 if (log.isTraceEnabled() && isEmpty(services))
1888 log.trace("No relevant services available");
1891 List<Component> nonAbstractLatestComponents = new ArrayList<>();
1892 ComponentParametersView params = new ComponentParametersView(true);
1893 params.setIgnoreAllVersions(false);
1894 for (GraphVertex vertexComponent : getRes.left().value()) {
1895 Either<ToscaElement, StorageOperationStatus> componentRes = topologyTemplateOperation.getLightComponent(vertexComponent, componentTypeEnum, params);
1896 if (componentRes.isRight()) {
1897 log.debug("Failed to fetch light element for {} error {}", vertexComponent.getUniqueId(), componentRes.right().value());
1898 return Either.right(componentRes.right().value());
1900 Component component = ModelConverter.convertFromToscaElement(componentRes.left().value());
1901 nonAbstractLatestComponents.add(component);
1904 if (CollectionUtils.isNotEmpty(services)) {
1905 nonAbstractLatestComponents.addAll(services);
1907 return Either.left(nonAbstractLatestComponents);
1910 public Either<ComponentMetadataData, StorageOperationStatus> getLatestComponentMetadataByUuid(String componentUuid, JsonParseFlagEnum parseFlag, Boolean isHighest) {
1912 Either<ComponentMetadataData, StorageOperationStatus> result;
1913 Map<GraphPropertyEnum, Object> hasProperties = new EnumMap<>(GraphPropertyEnum.class);
1914 hasProperties.put(GraphPropertyEnum.UUID, componentUuid);
1915 if (isHighest != null) {
1916 hasProperties.put(GraphPropertyEnum.IS_HIGHEST_VERSION, isHighest);
1918 Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
1919 propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
1920 propertiesNotToMatch.put(GraphPropertyEnum.IS_ARCHIVED, true); //US382674, US382683
1922 Either<List<GraphVertex>, JanusGraphOperationStatus> getRes = janusGraphDao
1923 .getByCriteria(null, hasProperties, propertiesNotToMatch, parseFlag);
1924 if (getRes.isRight()) {
1925 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getRes.right().value()));
1927 List<ComponentMetadataData> latestVersionList = getRes.left().value().stream().map(ModelConverter::convertToComponentMetadata).collect(Collectors.toList());
1928 ComponentMetadataData latestVersion = latestVersionList.size() == 1 ? latestVersionList.get(0)
1929 : latestVersionList.stream().max((c1, c2) -> Double.compare(Double.parseDouble(c1.getMetadataDataDefinition().getVersion()), Double.parseDouble(c2.getMetadataDataDefinition().getVersion()))).get();
1930 result = Either.left(latestVersion);
1935 public Either<ComponentMetadataData, StorageOperationStatus> getComponentMetadata(String componentId) {
1936 Either<ComponentMetadataData, StorageOperationStatus> result;
1937 Either<GraphVertex, JanusGraphOperationStatus> getRes = janusGraphDao
1938 .getVertexById(componentId, JsonParseFlagEnum.ParseMetadata);
1939 if (getRes.isRight()) {
1940 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getRes.right().value()));
1942 ComponentMetadataData componentMetadata = ModelConverter.convertToComponentMetadata(getRes.left().value());
1943 result = Either.left(componentMetadata);
1948 public Either<List<Component>, StorageOperationStatus> getLatestVersionNotAbstractComponents(boolean isAbstract, ComponentTypeEnum componentTypeEnum,
1949 String internalComponentType, List<String> componentUids) {
1951 List<Component> components = new ArrayList<>();
1952 if (componentUids == null) {
1953 Either<List<String>, StorageOperationStatus> componentUidsRes = getComponentUids(isAbstract, componentTypeEnum, internalComponentType);
1954 if (componentUidsRes.isRight()) {
1955 return Either.right(componentUidsRes.right().value());
1957 componentUids = componentUidsRes.left().value();
1959 if (!isEmpty(componentUids)) {
1960 for (String componentUid : componentUids) {
1961 ComponentParametersView componentParametersView = buildComponentViewForNotAbstract();
1962 if ("vl".equalsIgnoreCase(internalComponentType)) {
1963 componentParametersView.setIgnoreCapabilities(false);
1964 componentParametersView.setIgnoreRequirements(false);
1966 Either<ToscaElement, StorageOperationStatus> getToscaElementRes = nodeTemplateOperation.getToscaElementOperation(componentTypeEnum).getLightComponent(componentUid, componentTypeEnum, componentParametersView);
1967 if (getToscaElementRes.isRight()) {
1968 log.debug("Failed to fetch resource for error is {}", getToscaElementRes.right().value());
1969 return Either.right(getToscaElementRes.right().value());
1971 Component component = ModelConverter.convertFromToscaElement(getToscaElementRes.left().value());
1972 nullifySomeComponentProperties(component);
1973 components.add(component);
1976 return Either.left(components);
1979 public void nullifySomeComponentProperties(Component component) {
1980 component.setContactId(null);
1981 component.setCreationDate(null);
1982 component.setCreatorUserId(null);
1983 component.setCreatorFullName(null);
1984 component.setLastUpdateDate(null);
1985 component.setLastUpdaterUserId(null);
1986 component.setLastUpdaterFullName(null);
1987 component.setNormalizedName(null);
1990 private Either<List<String>, StorageOperationStatus> getComponentUids(boolean isAbstract, ComponentTypeEnum componentTypeEnum, String internalComponentType) {
1992 Either<List<Component>, StorageOperationStatus> getToscaElementsRes = getLatestVersionNotAbstractMetadataOnly(isAbstract, componentTypeEnum, internalComponentType);
1993 if (getToscaElementsRes.isRight()) {
1994 return Either.right(getToscaElementsRes.right().value());
1996 List<Component> collection = getToscaElementsRes.left().value();
1997 List<String> componentUids;
1998 if (collection == null) {
1999 componentUids = new ArrayList<>();
2001 componentUids = collection.stream()
2002 .map(Component::getUniqueId)
2003 .collect(Collectors.toList());
2005 return Either.left(componentUids);
2008 private ComponentParametersView buildComponentViewForNotAbstract() {
2009 ComponentParametersView componentParametersView = new ComponentParametersView();
2010 componentParametersView.disableAll();
2011 componentParametersView.setIgnoreCategories(false);
2012 componentParametersView.setIgnoreAllVersions(false);
2013 return componentParametersView;
2016 public Either<Boolean, StorageOperationStatus> validateComponentNameExists(String name, ResourceTypeEnum resourceType, ComponentTypeEnum componentType) {
2017 Either<Boolean, StorageOperationStatus> result = validateComponentNameUniqueness(name, resourceType, componentType);
2018 if (result.isLeft()) {
2019 result = Either.left(!result.left().value());
2024 public Either<Boolean, StorageOperationStatus> validateComponentNameUniqueness(String name, ResourceTypeEnum resourceType, ComponentTypeEnum componentType) {
2025 VertexTypeEnum vertexType = ModelConverter.isAtomicComponent(resourceType) ? VertexTypeEnum.NODE_TYPE : VertexTypeEnum.TOPOLOGY_TEMPLATE;
2026 String normalizedName = ValidationUtils.normaliseComponentName(name);
2027 Map<GraphPropertyEnum, Object> properties = new EnumMap<>(GraphPropertyEnum.class);
2028 properties.put(GraphPropertyEnum.NORMALIZED_NAME, normalizedName);
2029 properties.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
2031 Either<List<GraphVertex>, JanusGraphOperationStatus> vertexEither = janusGraphDao
2032 .getByCriteria(vertexType, properties, JsonParseFlagEnum.NoParse);
2033 if (vertexEither.isRight() && vertexEither.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
2034 log.debug("failed to get vertex from graph with property normalizedName: {}", normalizedName);
2035 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(vertexEither.right().value()));
2037 List<GraphVertex> vertexList = vertexEither.isLeft() ? vertexEither.left().value() : null;
2038 if (vertexList != null && !vertexList.isEmpty()) {
2039 return Either.left(false);
2041 return Either.left(true);
2045 private void fillNodeTypePropsMap(final Map<GraphPropertyEnum, Object> hasProps,
2046 final Map<GraphPropertyEnum, Object> hasNotProps,
2047 final String internalComponentType) {
2048 final Configuration configuration = ConfigurationManager.getConfigurationManager().getConfiguration();
2049 final List<String> allowedTypes;
2051 if (ComponentTypeEnum.SERVICE.getValue().equalsIgnoreCase(internalComponentType)) {
2052 allowedTypes = containerInstanceTypesData.getComponentAllowedList(ComponentTypeEnum.SERVICE, null);
2054 final ResourceTypeEnum resourceType = ResourceTypeEnum.getTypeIgnoreCase(internalComponentType);
2055 allowedTypes = containerInstanceTypesData.getComponentAllowedList(ComponentTypeEnum.RESOURCE, resourceType);
2057 final List<String> allResourceTypes = configuration.getResourceTypes();
2058 if (allowedTypes == null) {
2059 hasNotProps.put(GraphPropertyEnum.RESOURCE_TYPE, allResourceTypes);
2063 if (ResourceTypeEnum.VL.getValue().equalsIgnoreCase(internalComponentType)) {
2064 hasProps.put(GraphPropertyEnum.RESOURCE_TYPE, allowedTypes);
2066 final List<String> notAllowedTypes = allResourceTypes.stream().filter(s -> !allowedTypes.contains(s))
2067 .collect(Collectors.toList());
2068 hasNotProps.put(GraphPropertyEnum.RESOURCE_TYPE, notAllowedTypes);
2072 private void fillTopologyTemplatePropsMap(Map<GraphPropertyEnum, Object> hasProps, Map<GraphPropertyEnum, Object> hasNotProps, ComponentTypeEnum componentTypeEnum) {
2073 switch (componentTypeEnum) {
2075 hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
2078 hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
2083 hasNotProps.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.CVFC.name());
2086 private void fillPropsMap(Map<GraphPropertyEnum, Object> hasProps, Map<GraphPropertyEnum, Object> hasNotProps, String internalComponentType, ComponentTypeEnum componentTypeEnum, boolean isAbstract, VertexTypeEnum internalVertexType) {
2087 hasNotProps.put(GraphPropertyEnum.STATE, LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name());
2089 hasNotProps.put(GraphPropertyEnum.IS_DELETED, true);
2090 hasNotProps.put(GraphPropertyEnum.IS_ARCHIVED, true);
2091 hasProps.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
2092 if (VertexTypeEnum.NODE_TYPE == internalVertexType) {
2093 hasProps.put(GraphPropertyEnum.IS_ABSTRACT, isAbstract);
2094 if (internalComponentType != null) {
2095 fillNodeTypePropsMap(hasProps, hasNotProps, internalComponentType);
2098 fillTopologyTemplatePropsMap(hasProps, hasNotProps, componentTypeEnum);
2102 private List<VertexTypeEnum> getInternalVertexTypes(ComponentTypeEnum componentTypeEnum, String internalComponentType) {
2103 List<VertexTypeEnum> internalVertexTypes = new ArrayList<>();
2104 if (ComponentTypeEnum.RESOURCE == componentTypeEnum) {
2105 internalVertexTypes.add(VertexTypeEnum.NODE_TYPE);
2107 if (ComponentTypeEnum.SERVICE == componentTypeEnum || SERVICE.equalsIgnoreCase(internalComponentType)) {
2108 internalVertexTypes.add(VertexTypeEnum.TOPOLOGY_TEMPLATE);
2110 return internalVertexTypes;
2113 public Either<List<Component>, StorageOperationStatus> getLatestVersionNotAbstractMetadataOnly(boolean isAbstract, ComponentTypeEnum componentTypeEnum, String internalComponentType) {
2114 List<VertexTypeEnum> internalVertexTypes = getInternalVertexTypes(componentTypeEnum, internalComponentType);
2115 List<Component> result = new ArrayList<>();
2116 for (VertexTypeEnum vertexType : internalVertexTypes) {
2117 Either<List<Component>, StorageOperationStatus> listByVertexType = getLatestVersionNotAbstractToscaElementsMetadataOnly(isAbstract, componentTypeEnum, internalComponentType, vertexType);
2118 if (listByVertexType.isRight()) {
2119 return listByVertexType;
2121 result.addAll(listByVertexType.left().value());
2123 return Either.left(result);
2127 private Either<List<Component>, StorageOperationStatus> getLatestComponentListByUuid(String componentUuid, Map<GraphPropertyEnum, Object> additionalPropertiesToMatch) {
2128 Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
2129 if (additionalPropertiesToMatch != null) {
2130 propertiesToMatch.putAll(additionalPropertiesToMatch);
2132 propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
2133 return getComponentListByUuid(componentUuid, propertiesToMatch);
2136 public Either<Component, StorageOperationStatus> getComponentByUuidAndVersion(String componentUuid, String version) {
2137 Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
2139 propertiesToMatch.put(GraphPropertyEnum.UUID, componentUuid);
2140 propertiesToMatch.put(GraphPropertyEnum.VERSION, version);
2142 Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
2143 propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
2144 Either<List<GraphVertex>, JanusGraphOperationStatus> vertexEither = janusGraphDao
2145 .getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll);
2146 if (vertexEither.isRight()) {
2147 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(vertexEither.right().value()));
2150 List<GraphVertex> vertexList = vertexEither.isLeft() ? vertexEither.left().value() : null;
2151 if (vertexList == null || vertexList.isEmpty() || vertexList.size() > 1) {
2152 return Either.right(StorageOperationStatus.NOT_FOUND);
2155 return getToscaElementByOperation(vertexList.get(0));
2158 public Either<List<Component>, StorageOperationStatus> getComponentListByUuid(String componentUuid, Map<GraphPropertyEnum, Object> additionalPropertiesToMatch) {
2160 Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
2162 if (additionalPropertiesToMatch != null) {
2163 propertiesToMatch.putAll(additionalPropertiesToMatch);
2166 propertiesToMatch.put(GraphPropertyEnum.UUID, componentUuid);
2168 Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
2169 propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
2170 propertiesNotToMatch.put(GraphPropertyEnum.IS_ARCHIVED, true); //US382674, US382683
2172 Either<List<GraphVertex>, JanusGraphOperationStatus> vertexEither = janusGraphDao
2173 .getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll);
2175 if (vertexEither.isRight()) {
2176 log.debug("Couldn't fetch metadata for component with uuid {}, error: {}", componentUuid, vertexEither.right().value());
2177 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(vertexEither.right().value()));
2179 List<GraphVertex> vertexList = vertexEither.isLeft() ? vertexEither.left().value() : null;
2181 if (vertexList == null || vertexList.isEmpty()) {
2182 log.debug("Component with uuid {} was not found", componentUuid);
2183 return Either.right(StorageOperationStatus.NOT_FOUND);
2186 ArrayList<Component> latestComponents = new ArrayList<>();
2187 for (GraphVertex vertex : vertexList) {
2188 Either<Component, StorageOperationStatus> toscaElementByOperation = getToscaElementByOperation(vertex);
2190 if (toscaElementByOperation.isRight()) {
2191 log.debug("Could not fetch the following Component by UUID {}", vertex.getUniqueId());
2192 return Either.right(toscaElementByOperation.right().value());
2195 latestComponents.add(toscaElementByOperation.left().value());
2198 if (latestComponents.size() > 1) {
2199 for (Component component : latestComponents) {
2200 if (Boolean.TRUE.equals(component.isHighestVersion())) {
2201 LinkedList<Component> highestComponent = new LinkedList<>();
2202 highestComponent.add(component);
2203 return Either.left(highestComponent);
2208 return Either.left(latestComponents);
2211 public Either<Component, StorageOperationStatus> getLatestServiceByUuid(String serviceUuid) {
2212 Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
2213 propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
2214 return getLatestComponentByUuid(serviceUuid, propertiesToMatch);
2217 public Either<Component, StorageOperationStatus> getLatestComponentByUuid(String componentUuid) {
2218 return getLatestComponentByUuid(componentUuid, null);
2221 public Either<Component, StorageOperationStatus> getLatestComponentByUuid(String componentUuid, Map<GraphPropertyEnum, Object> propertiesToMatch) {
2223 Either<List<Component>, StorageOperationStatus> latestVersionListEither = getLatestComponentListByUuid(componentUuid, propertiesToMatch);
2225 if (latestVersionListEither.isRight()) {
2226 return Either.right(latestVersionListEither.right().value());
2229 List<Component> latestVersionList = latestVersionListEither.left().value();
2231 if (latestVersionList.isEmpty()) {
2232 return Either.right(StorageOperationStatus.NOT_FOUND);
2234 Component component = latestVersionList.size() == 1 ? latestVersionList.get(0) : latestVersionList.stream().max((c1, c2) -> Double.compare(Double.parseDouble(c1.getVersion()), Double.parseDouble(c2.getVersion()))).get();
2236 return Either.left(component);
2239 public Either<List<Resource>, StorageOperationStatus> getAllCertifiedResources(boolean isAbstract, Boolean isHighest) {
2241 List<Resource> resources = new ArrayList<>();
2242 Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
2243 Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
2245 propertiesToMatch.put(GraphPropertyEnum.IS_ABSTRACT, isAbstract);
2246 if (isHighest != null) {
2247 propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, isHighest);
2249 propertiesToMatch.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
2250 propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
2251 propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
2253 Either<List<GraphVertex>, JanusGraphOperationStatus> getResourcesRes = janusGraphDao
2254 .getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll);
2256 if (getResourcesRes.isRight()) {
2257 log.debug("Failed to fetch all certified resources. Status is {}", getResourcesRes.right().value());
2258 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getResourcesRes.right().value()));
2260 List<GraphVertex> resourceVerticies = getResourcesRes.left().value();
2261 for (GraphVertex resourceV : resourceVerticies) {
2262 Either<Resource, StorageOperationStatus> getResourceRes = getToscaElement(resourceV);
2263 if (getResourceRes.isRight()) {
2264 return Either.right(getResourceRes.right().value());
2266 resources.add(getResourceRes.left().value());
2268 return Either.left(resources);
2271 public <T extends Component> Either<T, StorageOperationStatus> getLatestByNameAndVersion(String name, String version, JsonParseFlagEnum parseFlag) {
2272 Either<T, StorageOperationStatus> result;
2274 Map<GraphPropertyEnum, Object> hasProperties = new EnumMap<>(GraphPropertyEnum.class);
2275 Map<GraphPropertyEnum, Object> hasNotProperties = new EnumMap<>(GraphPropertyEnum.class);
2277 hasProperties.put(GraphPropertyEnum.NAME, name);
2278 hasProperties.put(GraphPropertyEnum.VERSION, version);
2279 hasProperties.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
2281 hasNotProperties.put(GraphPropertyEnum.IS_DELETED, true);
2283 Either<List<GraphVertex>, JanusGraphOperationStatus> getResourceRes = janusGraphDao
2284 .getByCriteria(null, hasProperties, hasNotProperties, parseFlag);
2285 if (getResourceRes.isRight()) {
2286 JanusGraphOperationStatus status = getResourceRes.right().value();
2287 log.debug("failed to find resource with name {}, version {}. Status is {} ", name, version, status);
2288 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
2291 return getToscaElementByOperation(getResourceRes.left().value().get(0));
2294 public Either<Resource, StorageOperationStatus> getLatestComponentByCsarOrName(ComponentTypeEnum componentType, String csarUUID, String systemName) {
2295 return getLatestComponentByCsarOrName(componentType, csarUUID, systemName, JsonParseFlagEnum.ParseAll);
2298 public Either<Resource, StorageOperationStatus> getLatestComponentByCsarOrName(ComponentTypeEnum componentType, String csarUUID, String systemName, JsonParseFlagEnum parseFlag) {
2299 Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
2300 Map<GraphPropertyEnum, Object> propsHasNot = new EnumMap<>(GraphPropertyEnum.class);
2301 props.put(GraphPropertyEnum.CSAR_UUID, csarUUID);
2302 props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
2303 if (componentType != null) {
2304 props.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
2306 propsHasNot.put(GraphPropertyEnum.IS_DELETED, true);
2308 GraphVertex resourceMetadataData = null;
2309 List<GraphVertex> resourceMetadataDataList = null;
2310 Either<List<GraphVertex>, JanusGraphOperationStatus> byCsar = janusGraphDao
2311 .getByCriteria(null, props, propsHasNot, JsonParseFlagEnum.ParseMetadata);
2312 if (byCsar.isRight()) {
2313 if (JanusGraphOperationStatus.NOT_FOUND == byCsar.right().value()) {
2314 // Fix Defect DE256036
2315 if (StringUtils.isEmpty(systemName)) {
2316 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(
2317 JanusGraphOperationStatus.NOT_FOUND));
2321 props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
2322 props.put(GraphPropertyEnum.SYSTEM_NAME, systemName);
2323 Either<List<GraphVertex>, JanusGraphOperationStatus> bySystemname = janusGraphDao
2324 .getByCriteria(null, props, JsonParseFlagEnum.ParseMetadata);
2325 if (bySystemname.isRight()) {
2326 log.debug("getLatestResourceByCsarOrName - Failed to find by system name {} error {} ", systemName, bySystemname.right().value());
2327 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(bySystemname.right().value()));
2329 if (bySystemname.left().value().size() > 2) {
2330 log.debug("getLatestResourceByCsarOrName - getByCriteria(by system name) must return only 2 latest version, but was returned - {}", bySystemname.left().value().size());
2331 return Either.right(StorageOperationStatus.GENERAL_ERROR);
2333 resourceMetadataDataList = bySystemname.left().value();
2334 if (resourceMetadataDataList.size() == 1) {
2335 resourceMetadataData = resourceMetadataDataList.get(0);
2337 for (GraphVertex curResource : resourceMetadataDataList) {
2338 if (!((String) curResource.getJsonMetadataField(JsonPresentationFields.LIFECYCLE_STATE)).equals("CERTIFIED")) {
2339 resourceMetadataData = curResource;
2344 if (resourceMetadataData == null) {
2345 log.debug("getLatestResourceByCsarOrName - getByCriteria(by system name) returned 2 latest CERTIFIED versions");
2346 return Either.right(StorageOperationStatus.GENERAL_ERROR);
2348 if (resourceMetadataData.getJsonMetadataField(JsonPresentationFields.CSAR_UUID) != null && !((String) resourceMetadataData.getJsonMetadataField(JsonPresentationFields.CSAR_UUID)).equals(csarUUID)) {
2349 log.debug("getLatestResourceByCsarOrName - same system name {} but different csarUUID. exist {} and new {} ", systemName, resourceMetadataData.getJsonMetadataField(JsonPresentationFields.CSAR_UUID), csarUUID);
2350 // correct error will be returned from create flow. with all
2351 // correct audit records!!!!!
2352 return Either.right(StorageOperationStatus.NOT_FOUND);
2354 return getToscaElement(resourceMetadataData.getUniqueId());
2357 resourceMetadataDataList = byCsar.left().value();
2358 if (resourceMetadataDataList.size() > 2) {
2359 log.debug("getLatestResourceByCsarOrName - getByCriteria(by csar) must return only 2 latest version, but was returned - {}", byCsar.left().value().size());
2360 return Either.right(StorageOperationStatus.GENERAL_ERROR);
2362 if (resourceMetadataDataList.size() == 1) {
2363 resourceMetadataData = resourceMetadataDataList.get(0);
2365 for (GraphVertex curResource : resourceMetadataDataList) {
2366 if (!((String) curResource.getJsonMetadataField(JsonPresentationFields.LIFECYCLE_STATE)).equals("CERTIFIED")) {
2367 resourceMetadataData = curResource;
2372 if (resourceMetadataData == null) {
2373 log.debug("getLatestResourceByCsarOrName - getByCriteria(by csar) returned 2 latest CERTIFIED versions");
2374 return Either.right(StorageOperationStatus.GENERAL_ERROR);
2376 return getToscaElement((String) resourceMetadataData.getJsonMetadataField(JsonPresentationFields.UNIQUE_ID), parseFlag);
2381 public Either<Boolean, StorageOperationStatus> validateToscaResourceNameExtends(String templateNameCurrent, String templateNameExtends) {
2383 String currentTemplateNameChecked = templateNameExtends;
2385 while (currentTemplateNameChecked != null && !currentTemplateNameChecked.equalsIgnoreCase(templateNameCurrent)) {
2386 Either<Resource, StorageOperationStatus> latestByToscaResourceName = getLatestByToscaResourceName(currentTemplateNameChecked);
2388 if (latestByToscaResourceName.isRight()) {
2389 return latestByToscaResourceName.right().value() == StorageOperationStatus.NOT_FOUND ? Either.left(false) : Either.right(latestByToscaResourceName.right().value());
2392 Resource value = latestByToscaResourceName.left().value();
2394 if (value.getDerivedFrom() != null) {
2395 currentTemplateNameChecked = value.getDerivedFrom().get(0);
2397 currentTemplateNameChecked = null;
2401 return (currentTemplateNameChecked != null && currentTemplateNameChecked.equalsIgnoreCase(templateNameCurrent)) ? Either.left(true) : Either.left(false);
2404 public Either<List<Component>, StorageOperationStatus> fetchMetaDataByResourceType(String resourceType, ComponentParametersView filterBy) {
2405 Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
2406 props.put(GraphPropertyEnum.RESOURCE_TYPE, resourceType);
2407 props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
2408 Map<GraphPropertyEnum, Object> propsHasNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
2409 propsHasNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
2410 Either<List<GraphVertex>, JanusGraphOperationStatus> resourcesByTypeEither = janusGraphDao
2411 .getByCriteria(null, props, propsHasNotToMatch, JsonParseFlagEnum.ParseMetadata);
2413 if (resourcesByTypeEither.isRight()) {
2414 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(resourcesByTypeEither.right().value()));
2417 List<GraphVertex> vertexList = resourcesByTypeEither.left().value();
2418 List<Component> components = new ArrayList<>();
2420 for (GraphVertex vertex : vertexList) {
2421 components.add(getToscaElementByOperation(vertex, filterBy).left().value());
2424 return Either.left(components);
2427 public void commit() {
2428 janusGraphDao.commit();
2431 public Either<Service, StorageOperationStatus> updateDistributionStatus(Service service, User user, DistributionStatusEnum distributionStatus) {
2432 Either<GraphVertex, StorageOperationStatus> updateDistributionStatus = topologyTemplateOperation.updateDistributionStatus(service.getUniqueId(), user, distributionStatus);
2433 if (updateDistributionStatus.isRight()) {
2434 return Either.right(updateDistributionStatus.right().value());
2436 GraphVertex serviceV = updateDistributionStatus.left().value();
2437 service.setDistributionStatus(distributionStatus);
2438 service.setLastUpdateDate((Long) serviceV.getJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE));
2439 return Either.left(service);
2442 public Either<ComponentMetadataData, StorageOperationStatus> updateComponentLastUpdateDateOnGraph(Component component) {
2444 Either<ComponentMetadataData, StorageOperationStatus> result = null;
2445 GraphVertex serviceVertex;
2446 Either<GraphVertex, JanusGraphOperationStatus> updateRes = null;
2447 Either<GraphVertex, JanusGraphOperationStatus> getRes = janusGraphDao
2448 .getVertexById(component.getUniqueId(), JsonParseFlagEnum.ParseMetadata);
2449 if (getRes.isRight()) {
2450 JanusGraphOperationStatus status = getRes.right().value();
2451 log.error("Failed to fetch component {}. status is {}", component.getUniqueId(), status);
2452 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
2454 if (result == null) {
2455 serviceVertex = getRes.left().value();
2456 long lastUpdateDate = System.currentTimeMillis();
2457 serviceVertex.setJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE, lastUpdateDate);
2458 component.setLastUpdateDate(lastUpdateDate);
2459 updateRes = janusGraphDao.updateVertex(serviceVertex);
2460 if (updateRes.isRight()) {
2461 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(updateRes.right().value()));
2464 if (result == null) {
2465 result = Either.left(ModelConverter.convertToComponentMetadata(updateRes.left().value()));
2470 public HealingJanusGraphDao getJanusGraphDao() {
2471 return janusGraphDao;
2474 public Either<List<Service>, StorageOperationStatus> getCertifiedServicesWithDistStatus(Set<DistributionStatusEnum> distStatus) {
2475 Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
2476 propertiesToMatch.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
2478 return getServicesWithDistStatus(distStatus, propertiesToMatch);
2481 public Either<List<Service>, StorageOperationStatus> getServicesWithDistStatus(Set<DistributionStatusEnum> distStatus, Map<GraphPropertyEnum, Object> additionalPropertiesToMatch) {
2483 List<Service> servicesAll = new ArrayList<>();
2485 Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
2486 Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
2488 if (additionalPropertiesToMatch != null && !additionalPropertiesToMatch.isEmpty()) {
2489 propertiesToMatch.putAll(additionalPropertiesToMatch);
2492 propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
2494 propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
2496 if (distStatus != null && !distStatus.isEmpty()) {
2497 for (DistributionStatusEnum state : distStatus) {
2498 propertiesToMatch.put(GraphPropertyEnum.DISTRIBUTION_STATUS, state.name());
2499 Either<List<Service>, StorageOperationStatus> fetchServicesByCriteria = fetchServicesByCriteria(servicesAll, propertiesToMatch, propertiesNotToMatch);
2500 if (fetchServicesByCriteria.isRight()) {
2501 return fetchServicesByCriteria;
2503 servicesAll = fetchServicesByCriteria.left().value();
2506 return Either.left(servicesAll);
2508 return fetchServicesByCriteria(servicesAll, propertiesToMatch, propertiesNotToMatch);
2512 private Either<List<Service>, StorageOperationStatus> fetchServicesByCriteria(List<Service> servicesAll, Map<GraphPropertyEnum, Object> propertiesToMatch, Map<GraphPropertyEnum, Object> propertiesNotToMatch) {
2513 Either<List<GraphVertex>, JanusGraphOperationStatus> getRes = janusGraphDao
2514 .getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll);
2515 if (getRes.isRight()) {
2516 if (getRes.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
2517 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch certified services by match properties {} not match properties {} . Status is {}. ", propertiesToMatch, propertiesNotToMatch, getRes.right().value());
2518 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getRes.right().value()));
2521 for (GraphVertex vertex : getRes.left().value()) {
2522 Either<ToscaElement, StorageOperationStatus> getServiceRes = topologyTemplateOperation.getLightComponent(vertex, ComponentTypeEnum.SERVICE, new ComponentParametersView(true));
2524 if (getServiceRes.isRight()) {
2525 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to fetch certified service {}. Status is {}. ", vertex.getJsonMetadataField(JsonPresentationFields.NAME), getServiceRes.right().value());
2526 return Either.right(getServiceRes.right().value());
2528 servicesAll.add(ModelConverter.convertFromToscaElement(getServiceRes.left().value()));
2532 return Either.left(servicesAll);
2535 public void rollback() {
2536 janusGraphDao.rollback();
2539 public StorageOperationStatus addDeploymentArtifactsToInstance(String componentId, ComponentInstance componentInstance, Map<String, ArtifactDefinition> finalDeploymentArtifacts) {
2540 Map<String, ArtifactDataDefinition> instDeplArtifacts = finalDeploymentArtifacts.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> new ArtifactDataDefinition(e.getValue())));
2542 return nodeTemplateOperation.addDeploymentArtifactsToInstance(componentId, componentInstance.getUniqueId(), instDeplArtifacts);
2545 public StorageOperationStatus addInformationalArtifactsToInstance(String componentId, ComponentInstance componentInstance, Map<String, ArtifactDefinition> artifacts) {
2546 StorageOperationStatus status = StorageOperationStatus.OK;
2547 if (MapUtils.isNotEmpty(artifacts)) {
2548 Map<String, ArtifactDataDefinition> instDeplArtifacts = artifacts.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> new ArtifactDataDefinition(e.getValue())));
2549 status = nodeTemplateOperation.addInformationalArtifactsToInstance(componentId, componentInstance.getUniqueId(), instDeplArtifacts);
2554 public StorageOperationStatus generateCustomizationUUIDOnInstance(String componentId, String instanceId) {
2555 return nodeTemplateOperation.generateCustomizationUUIDOnInstance(componentId, instanceId);
2558 public StorageOperationStatus generateCustomizationUUIDOnInstanceGroup(String componentId, String instanceId, List<String> groupInstances) {
2559 return nodeTemplateOperation.generateCustomizationUUIDOnInstanceGroup(componentId, instanceId, groupInstances);
2562 public Either<PropertyDefinition, StorageOperationStatus> addPropertyToComponent(String propertyName,
2563 PropertyDefinition newPropertyDefinition,
2564 Component component) {
2565 newPropertyDefinition.setName(propertyName);
2567 StorageOperationStatus status = getToscaElementOperation(component)
2568 .addToscaDataToToscaElement(component.getUniqueId(), EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, newPropertyDefinition, JsonPresentationFields.NAME);
2569 if (status != StorageOperationStatus.OK) {
2570 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to add the property {} to the component {}. Status is {}. ", propertyName, component.getName(), status);
2571 return Either.right(status);
2574 ComponentParametersView filter = new ComponentParametersView(true);
2575 filter.setIgnoreProperties(false);
2576 filter.setIgnoreInputs(false);
2577 Either<Component, StorageOperationStatus> getUpdatedComponentRes = getToscaElement(component.getUniqueId(), filter);
2578 if (getUpdatedComponentRes.isRight()) {
2579 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to get updated component {}. Status is {}. ", component.getUniqueId(), getUpdatedComponentRes.right().value());
2580 return Either.right(status);
2583 PropertyDefinition newProperty = null;
2584 List<PropertyDefinition> properties =
2585 (getUpdatedComponentRes.left().value()).getProperties();
2586 if (CollectionUtils.isNotEmpty(properties)) {
2587 Optional<PropertyDefinition> propertyOptional = properties.stream().filter(
2588 propertyEntry -> propertyEntry.getName().equals(propertyName)).findAny();
2589 if (propertyOptional.isPresent()) {
2590 newProperty = propertyOptional.get();
2593 if (newProperty == null) {
2594 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to find recently added property {} on the component {}. Status is {}. ", propertyName, component.getUniqueId(), StorageOperationStatus.NOT_FOUND);
2595 return Either.right(StorageOperationStatus.NOT_FOUND);
2598 return Either.left(newProperty);
2601 public Either<InputDefinition, StorageOperationStatus> addInputToComponent(String inputName,
2602 InputDefinition newInputDefinition,
2603 Component component) {
2604 newInputDefinition.setName(inputName);
2606 StorageOperationStatus status = getToscaElementOperation(component)
2607 .addToscaDataToToscaElement(component.getUniqueId(), EdgeLabelEnum.INPUTS, VertexTypeEnum.INPUTS, newInputDefinition, JsonPresentationFields.NAME);
2608 if (status != StorageOperationStatus.OK) {
2609 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to add the input {} to the component {}. Status is {}. ", inputName, component.getName(), status);
2610 return Either.right(status);
2613 ComponentParametersView filter = new ComponentParametersView(true);
2614 filter.setIgnoreProperties(false);
2615 filter.setIgnoreInputs(false);
2616 Either<Component, StorageOperationStatus> getUpdatedComponentRes = getToscaElement(component.getUniqueId(), filter);
2617 if (getUpdatedComponentRes.isRight()) {
2618 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to get updated component {}. Status is {}. ", component.getUniqueId(), getUpdatedComponentRes.right().value());
2619 return Either.right(status);
2622 InputDefinition newInput = null;
2623 List<InputDefinition> inputs =
2624 (getUpdatedComponentRes.left().value()).getInputs();
2625 if (CollectionUtils.isNotEmpty(inputs)) {
2626 Optional<InputDefinition> inputOptional = inputs.stream().filter(
2627 inputEntry -> inputEntry.getName().equals(inputName)).findAny();
2628 if (inputOptional.isPresent()) {
2629 newInput = inputOptional.get();
2632 if (newInput == null) {
2633 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to find recently added input {} " +
2634 "on the component {}. Status is {}. ", inputs, component.getUniqueId(), StorageOperationStatus.NOT_FOUND);
2635 return Either.right(StorageOperationStatus.NOT_FOUND);
2638 return Either.left(newInput);
2641 public StorageOperationStatus deletePropertyOfComponent(Component component, String propertyName) {
2642 return getToscaElementOperation(component).deleteToscaDataElement(component.getUniqueId(), EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, propertyName, JsonPresentationFields.NAME);
2645 public StorageOperationStatus deleteAttributeOfResource(Component component, String attributeName) {
2646 return getToscaElementOperation(component).deleteToscaDataElement(component.getUniqueId(), EdgeLabelEnum.ATTRIBUTES, VertexTypeEnum.ATTRIBUTES, attributeName, JsonPresentationFields.NAME);
2649 public StorageOperationStatus deleteInputOfResource(Component resource, String inputName) {
2650 return getToscaElementOperation(resource).deleteToscaDataElement(resource.getUniqueId(), EdgeLabelEnum.INPUTS, VertexTypeEnum.INPUTS, inputName, JsonPresentationFields.NAME);
2654 * Deletes a data type from a component.
2655 * @param component the container which has the data type
2656 * @param dataTypeName the data type name to be deleted
2657 * @return Operation result.
2659 public StorageOperationStatus deleteDataTypeOfComponent(Component component, String dataTypeName) {
2660 return getToscaElementOperation(component).deleteToscaDataElement(component.getUniqueId(), EdgeLabelEnum.DATA_TYPES, VertexTypeEnum.DATA_TYPES, dataTypeName, JsonPresentationFields.NAME);
2663 public Either<PropertyDefinition, StorageOperationStatus> updatePropertyOfComponent(Component component,
2664 PropertyDefinition newPropertyDefinition) {
2666 Either<Component, StorageOperationStatus> getUpdatedComponentRes = null;
2667 Either<PropertyDefinition, StorageOperationStatus> result = null;
2668 StorageOperationStatus status = getToscaElementOperation(component).updateToscaDataOfToscaElement(component.getUniqueId(), EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, newPropertyDefinition, JsonPresentationFields.NAME);
2669 if (status != StorageOperationStatus.OK) {
2670 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_ADD_THE_PROPERTY_TO_THE_RESOURCE_STATUS_IS,
2671 newPropertyDefinition.getName(), component.getName(), status);
2672 result = Either.right(status);
2674 if (result == null) {
2675 ComponentParametersView filter = new ComponentParametersView(true);
2676 filter.setIgnoreProperties(false);
2677 getUpdatedComponentRes = getToscaElement(component.getUniqueId(), filter);
2678 if (getUpdatedComponentRes.isRight()) {
2679 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_GET_UPDATED_RESOURCE_STATUS_IS,
2680 component.getUniqueId(), getUpdatedComponentRes.right().value());
2681 result = Either.right(status);
2684 if (result == null) {
2685 Optional<PropertyDefinition> newProperty = (getUpdatedComponentRes.left().value())
2686 .getProperties().stream().filter(p -> p.getName().equals(newPropertyDefinition.getName())).findAny();
2687 if (newProperty.isPresent()) {
2688 result = Either.left(newProperty.get());
2690 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG,
2691 FAILED_TO_FIND_RECENTLY_ADDED_PROPERTY_ON_THE_RESOURCE_STATUS_IS, newPropertyDefinition.getName(),
2692 component.getUniqueId(), StorageOperationStatus.NOT_FOUND);
2693 result = Either.right(StorageOperationStatus.NOT_FOUND);
2699 public Either<AttributeDefinition, StorageOperationStatus> addAttributeOfResource(Component component, AttributeDefinition newAttributeDef) {
2701 Either<Component, StorageOperationStatus> getUpdatedComponentRes = null;
2702 Either<AttributeDefinition, StorageOperationStatus> result = null;
2703 if (newAttributeDef.getUniqueId() == null || newAttributeDef.getUniqueId().isEmpty()) {
2704 String attUniqueId = UniqueIdBuilder.buildAttributeUid(component.getUniqueId(), newAttributeDef.getName());
2705 newAttributeDef.setUniqueId(attUniqueId);
2706 newAttributeDef.setOwnerId(component.getUniqueId());
2709 StorageOperationStatus status = getToscaElementOperation(component).addToscaDataToToscaElement(component.getUniqueId(), EdgeLabelEnum.ATTRIBUTES, VertexTypeEnum.ATTRIBUTES, newAttributeDef, JsonPresentationFields.NAME);
2710 if (status != StorageOperationStatus.OK) {
2711 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_ADD_THE_PROPERTY_TO_THE_RESOURCE_STATUS_IS, newAttributeDef.getName(), component.getName(), status);
2712 result = Either.right(status);
2714 if (result == null) {
2715 ComponentParametersView filter = new ComponentParametersView(true);
2716 filter.setIgnoreAttributes(false);
2717 getUpdatedComponentRes = getToscaElement(component.getUniqueId(), filter);
2718 if (getUpdatedComponentRes.isRight()) {
2719 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_GET_UPDATED_RESOURCE_STATUS_IS, component.getUniqueId(), getUpdatedComponentRes.right().value());
2720 result = Either.right(status);
2723 if (result == null) {
2724 Optional<AttributeDefinition> newAttribute = ((Resource) getUpdatedComponentRes.left().value())
2725 .getAttributes().stream().filter(p -> p.getName().equals(newAttributeDef.getName())).findAny();
2726 if (newAttribute.isPresent()) {
2727 result = Either.left(newAttribute.get());
2729 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_FIND_RECENTLY_ADDED_PROPERTY_ON_THE_RESOURCE_STATUS_IS, newAttributeDef.getName(), component.getUniqueId(), StorageOperationStatus.NOT_FOUND);
2730 result = Either.right(StorageOperationStatus.NOT_FOUND);
2736 public Either<AttributeDefinition, StorageOperationStatus> updateAttributeOfResource(Component component, AttributeDefinition newAttributeDef) {
2738 Either<Component, StorageOperationStatus> getUpdatedComponentRes = null;
2739 Either<AttributeDefinition, StorageOperationStatus> result = null;
2740 StorageOperationStatus status = getToscaElementOperation(component).updateToscaDataOfToscaElement(component.getUniqueId(), EdgeLabelEnum.ATTRIBUTES, VertexTypeEnum.ATTRIBUTES, newAttributeDef, JsonPresentationFields.NAME);
2741 if (status != StorageOperationStatus.OK) {
2742 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_ADD_THE_PROPERTY_TO_THE_RESOURCE_STATUS_IS, newAttributeDef.getName(), component.getName(), status);
2743 result = Either.right(status);
2745 if (result == null) {
2746 ComponentParametersView filter = new ComponentParametersView(true);
2747 filter.setIgnoreAttributes(false);
2748 getUpdatedComponentRes = getToscaElement(component.getUniqueId(), filter);
2749 if (getUpdatedComponentRes.isRight()) {
2750 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_GET_UPDATED_RESOURCE_STATUS_IS, component.getUniqueId(), getUpdatedComponentRes.right().value());
2751 result = Either.right(status);
2754 if (result == null) {
2755 Optional<AttributeDefinition> newProperty = ((Resource) getUpdatedComponentRes.left().value())
2756 .getAttributes().stream().filter(p -> p.getName().equals(newAttributeDef.getName())).findAny();
2757 if (newProperty.isPresent()) {
2758 result = Either.left(newProperty.get());
2760 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_FIND_RECENTLY_ADDED_PROPERTY_ON_THE_RESOURCE_STATUS_IS, newAttributeDef.getName(), component.getUniqueId(), StorageOperationStatus.NOT_FOUND);
2761 result = Either.right(StorageOperationStatus.NOT_FOUND);
2767 public Either<InputDefinition, StorageOperationStatus> updateInputOfComponent(Component component, InputDefinition newInputDefinition) {
2769 Either<Component, StorageOperationStatus> getUpdatedComponentRes = null;
2770 Either<InputDefinition, StorageOperationStatus> result = null;
2771 StorageOperationStatus status = getToscaElementOperation(component).updateToscaDataOfToscaElement(component.getUniqueId(), EdgeLabelEnum.INPUTS, VertexTypeEnum.INPUTS, newInputDefinition, JsonPresentationFields.NAME);
2772 if (status != StorageOperationStatus.OK) {
2773 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to update the input {} to the component {}. Status is {}. ", newInputDefinition.getName(), component.getName(), status);
2774 result = Either.right(status);
2776 if (result == null) {
2777 ComponentParametersView filter = new ComponentParametersView(true);
2778 filter.setIgnoreInputs(false);
2779 getUpdatedComponentRes = getToscaElement(component.getUniqueId(), filter);
2780 if (getUpdatedComponentRes.isRight()) {
2781 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_GET_UPDATED_RESOURCE_STATUS_IS, component.getUniqueId(), getUpdatedComponentRes.right().value());
2782 result = Either.right(status);
2785 if (result == null) {
2786 Optional<InputDefinition> updatedInput = getUpdatedComponentRes.left().value().getInputs().stream().filter(p -> p.getName().equals(newInputDefinition.getName())).findAny();
2787 if (updatedInput.isPresent()) {
2788 result = Either.left(updatedInput.get());
2790 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to find recently updated inputs {} on the resource {}. Status is {}. ", newInputDefinition.getName(), component.getUniqueId(), StorageOperationStatus.NOT_FOUND);
2791 result = Either.right(StorageOperationStatus.NOT_FOUND);
2798 * method - ename the group instances after referenced container name renamed flow - VF rename -(triggers)-> Group rename
2800 * @param containerComponent - container such as service
2801 * @param componentInstance - context component
2802 * @param componentInstanceId - id
2803 * @return - successfull/failed status
2805 public Either<StorageOperationStatus, StorageOperationStatus> cleanAndAddGroupInstancesToComponentInstance(Component containerComponent, ComponentInstance componentInstance, String componentInstanceId) {
2806 String uniqueId = componentInstance.getUniqueId();
2807 StorageOperationStatus status = nodeTemplateOperation.deleteToscaDataDeepElementsBlockOfToscaElement(containerComponent.getUniqueId(), EdgeLabelEnum.INST_GROUPS, VertexTypeEnum.INST_GROUPS, uniqueId);
2808 if (status != StorageOperationStatus.OK && status != StorageOperationStatus.NOT_FOUND) {
2809 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to delete group instances for container {}. error {] ", componentInstanceId, status);
2810 return Either.right(status);
2812 if (componentInstance.getGroupInstances() != null) {
2813 status = addGroupInstancesToComponentInstance(containerComponent, componentInstance, componentInstance.getGroupInstances());
2814 if (status != StorageOperationStatus.OK && status != StorageOperationStatus.NOT_FOUND) {
2815 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to add group instances for container {}. error {] ", componentInstanceId, status);
2816 return Either.right(status);
2819 return Either.left(status);
2822 public StorageOperationStatus addGroupInstancesToComponentInstance(Component containerComponent, ComponentInstance componentInstance, List<GroupDefinition> groups, Map<String, List<ArtifactDefinition>> groupInstancesArtifacts) {
2823 return nodeTemplateOperation.addGroupInstancesToComponentInstance(containerComponent, componentInstance, groups, groupInstancesArtifacts);
2826 public Either<List<GroupDefinition>, StorageOperationStatus> updateGroupsOnComponent(Component component, List<GroupDataDefinition> updatedGroups) {
2827 return groupsOperation.updateGroups(component, updatedGroups, PromoteVersionEnum.MINOR);
2830 public Either<List<GroupInstance>, StorageOperationStatus> updateGroupInstancesOnComponent(Component component, String instanceId, List<GroupInstance> updatedGroupInstances) {
2831 return groupsOperation.updateGroupInstances(component, instanceId, updatedGroupInstances);
2834 public StorageOperationStatus addGroupInstancesToComponentInstance(Component containerComponent, ComponentInstance componentInstance, List<GroupInstance> groupInstances) {
2835 return nodeTemplateOperation.addGroupInstancesToComponentInstance(containerComponent, componentInstance, groupInstances);
2838 public StorageOperationStatus addDeploymentArtifactsToComponentInstance(Component containerComponent, ComponentInstance componentInstance, Map<String, ArtifactDefinition> deploymentArtifacts) {
2839 return nodeTemplateOperation.addDeploymentArtifactsToComponentInstance(containerComponent, componentInstance, deploymentArtifacts);
2842 public StorageOperationStatus updateComponentInstanceProperty(Component containerComponent, String componentInstanceId, ComponentInstanceProperty property) {
2843 return nodeTemplateOperation.updateComponentInstanceProperty(containerComponent, componentInstanceId, property);
2846 public StorageOperationStatus updateComponentInstanceProperties(Component containerComponent, String componentInstanceId, List<ComponentInstanceProperty> properties) {
2847 return nodeTemplateOperation.updateComponentInstanceProperties(containerComponent, componentInstanceId, properties);
2851 public StorageOperationStatus addComponentInstanceProperty(Component containerComponent, String componentInstanceId, ComponentInstanceProperty property) {
2852 return nodeTemplateOperation.addComponentInstanceProperty(containerComponent, componentInstanceId, property);
2855 public StorageOperationStatus updateComponentInstanceAttribute(Component containerComponent, String componentInstanceId, ComponentInstanceAttribute property){
2856 return nodeTemplateOperation.updateComponentInstanceAttribute(containerComponent, componentInstanceId, property);
2859 public StorageOperationStatus addComponentInstanceAttribute(Component containerComponent, String componentInstanceId, ComponentInstanceAttribute attribute){
2860 return nodeTemplateOperation.addComponentInstanceAttribute(containerComponent, componentInstanceId, attribute);
2863 public StorageOperationStatus updateComponentInstanceInput(Component containerComponent, String componentInstanceId, ComponentInstanceInput property) {
2864 return nodeTemplateOperation.updateComponentInstanceInput(containerComponent, componentInstanceId, property);
2867 public StorageOperationStatus updateComponentInstanceInputs(Component containerComponent, String componentInstanceId, List<ComponentInstanceInput> instanceInputs) {
2868 return nodeTemplateOperation.updateComponentInstanceInputs(containerComponent, componentInstanceId, instanceInputs);
2871 public StorageOperationStatus addComponentInstanceInput(Component containerComponent, String componentInstanceId, ComponentInstanceInput property) {
2872 return nodeTemplateOperation.addComponentInstanceInput(containerComponent, componentInstanceId, property);
2875 public void setNodeTypeOperation(NodeTypeOperation nodeTypeOperation) {
2876 this.nodeTypeOperation = nodeTypeOperation;
2879 public void setTopologyTemplateOperation(TopologyTemplateOperation topologyTemplateOperation) {
2880 this.topologyTemplateOperation = topologyTemplateOperation;
2883 public StorageOperationStatus deleteComponentInstanceInputsFromTopologyTemplate(Component containerComponent, List<InputDefinition> inputsToDelete) {
2884 return topologyTemplateOperation.deleteToscaDataElements(containerComponent.getUniqueId(), EdgeLabelEnum.INPUTS, inputsToDelete.stream().map(PropertyDataDefinition::getName).collect(Collectors.toList()));
2887 public StorageOperationStatus updateComponentInstanceCapabiltyProperty(Component containerComponent, String componentInstanceUniqueId, String capabilityPropertyKey, ComponentInstanceProperty property) {
2888 return nodeTemplateOperation.updateComponentInstanceCapabilityProperty(containerComponent, componentInstanceUniqueId, capabilityPropertyKey, property);
2891 public StorageOperationStatus updateComponentInstanceCapabilityProperties(Component containerComponent, String componentInstanceUniqueId) {
2892 return convertComponentInstanceProperties(containerComponent, componentInstanceUniqueId)
2893 .map(instanceCapProps -> topologyTemplateOperation.updateComponentInstanceCapabilityProperties(containerComponent, componentInstanceUniqueId, instanceCapProps))
2894 .orElse(StorageOperationStatus.NOT_FOUND);
2897 public StorageOperationStatus updateComponentInstanceRequirement(String containerComponentId, String componentInstanceUniqueId, RequirementDataDefinition requirementDataDefinition) {
2898 return nodeTemplateOperation.updateComponentInstanceRequirement(containerComponentId, componentInstanceUniqueId, requirementDataDefinition);
2901 public StorageOperationStatus updateComponentInstanceInterfaces(Component containerComponent, String componentInstanceUniqueId) {
2902 MapInterfaceDataDefinition mapInterfaceDataDefinition =
2903 convertComponentInstanceInterfaces(containerComponent, componentInstanceUniqueId);
2904 return topologyTemplateOperation
2905 .updateComponentInstanceInterfaces(containerComponent, componentInstanceUniqueId, mapInterfaceDataDefinition);
2908 public StorageOperationStatus updateComponentCalculatedCapabilitiesProperties(Component containerComponent) {
2909 Map<String, MapCapabilityProperty> mapCapabiltyPropertyMap =
2910 convertComponentCapabilitiesProperties(containerComponent);
2911 return nodeTemplateOperation.overrideComponentCapabilitiesProperties(containerComponent, mapCapabiltyPropertyMap);
2914 public StorageOperationStatus deleteAllCalculatedCapabilitiesRequirements(String topologyTemplateId) {
2915 StorageOperationStatus status = topologyTemplateOperation.removeToscaData(topologyTemplateId, EdgeLabelEnum.CALCULATED_CAPABILITIES, VertexTypeEnum.CALCULATED_CAPABILITIES);
2916 if (status == StorageOperationStatus.OK) {
2917 status = topologyTemplateOperation.removeToscaData(topologyTemplateId, EdgeLabelEnum.CALCULATED_REQUIREMENTS, VertexTypeEnum.CALCULATED_REQUIREMENTS);
2919 if (status == StorageOperationStatus.OK) {
2920 status = topologyTemplateOperation.removeToscaData(topologyTemplateId, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES, VertexTypeEnum.CALCULATED_CAP_PROPERTIES);
2925 public Either<Component, StorageOperationStatus> shouldUpgradeToLatestDerived(Resource clonedResource) {
2926 String componentId = clonedResource.getUniqueId();
2927 Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao
2928 .getVertexById(componentId, JsonParseFlagEnum.NoParse);
2929 if (getVertexEither.isRight()) {
2930 log.debug(COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
2931 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
2934 GraphVertex nodeTypeV = getVertexEither.left().value();
2936 ToscaElement toscaElementToUpdate = ModelConverter.convertToToscaElement(clonedResource);
2938 Either<ToscaElement, StorageOperationStatus> shouldUpdateDerivedVersion = nodeTypeOperation.shouldUpdateDerivedVersion(toscaElementToUpdate, nodeTypeV);
2939 if (shouldUpdateDerivedVersion.isRight() && StorageOperationStatus.OK != shouldUpdateDerivedVersion.right().value()) {
2940 log.debug("Failed to update derived version for node type {} derived {}, error: {}", componentId, clonedResource.getDerivedFrom().get(0), shouldUpdateDerivedVersion.right().value());
2941 return Either.right(shouldUpdateDerivedVersion.right().value());
2943 if (shouldUpdateDerivedVersion.isLeft()) {
2944 return Either.left(ModelConverter.convertFromToscaElement(shouldUpdateDerivedVersion.left().value()));
2946 return Either.left(clonedResource);
2950 * Returns list of ComponentInstanceProperty belonging to component instance capability specified by name, type and ownerId
2952 public Either<List<ComponentInstanceProperty>, StorageOperationStatus> getComponentInstanceCapabilityProperties(String componentId, String instanceId, String capabilityName, String capabilityType, String ownerId) {
2953 return topologyTemplateOperation.getComponentInstanceCapabilityProperties(componentId, instanceId, capabilityName, capabilityType, ownerId);
2956 private MapInterfaceDataDefinition convertComponentInstanceInterfaces(Component currComponent,
2957 String componentInstanceId) {
2958 MapInterfaceDataDefinition mapInterfaceDataDefinition = new MapInterfaceDataDefinition();
2959 List<ComponentInstanceInterface> componentInterface = currComponent.getComponentInstancesInterfaces().get(componentInstanceId);
2961 if(CollectionUtils.isNotEmpty(componentInterface)) {
2962 componentInterface.stream().forEach(interfaceDef -> mapInterfaceDataDefinition.put
2963 (interfaceDef.getUniqueId(), interfaceDef));
2966 return mapInterfaceDataDefinition;
2969 private Map<String, MapCapabilityProperty> convertComponentCapabilitiesProperties(Component currComponent) {
2970 Map<String, MapCapabilityProperty> map = ModelConverter.extractCapabilityPropertiesFromGroups(currComponent.getGroups(), true);
2971 map.putAll(ModelConverter.extractCapabilityProperteisFromInstances(currComponent.getComponentInstances(), true));
2975 private Optional<MapCapabilityProperty> convertComponentInstanceProperties(Component component, String instanceId) {
2976 return component.fetchInstanceById(instanceId)
2977 .map(ci -> ModelConverter.convertToMapOfMapCapabilityProperties(ci.getCapabilities(), instanceId, ci.getOriginType().isAtomicType()));
2980 public Either<PolicyDefinition, StorageOperationStatus> associatePolicyToComponent(String componentId, PolicyDefinition policyDefinition, int counter) {
2981 Either<PolicyDefinition, StorageOperationStatus> result = null;
2982 Either<GraphVertex, JanusGraphOperationStatus> getVertexEither;
2983 getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.ParseMetadata);
2984 if (getVertexEither.isRight()) {
2985 log.error(COULDNT_FETCH_A_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
2986 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
2988 if (getVertexEither.left().value().getLabel() != VertexTypeEnum.TOPOLOGY_TEMPLATE) {
2989 log.error("Policy association to component of Tosca type {} is not allowed. ", getVertexEither.left().value().getLabel());
2990 result = Either.right(StorageOperationStatus.BAD_REQUEST);
2993 if (result == null) {
2994 StorageOperationStatus status = topologyTemplateOperation.addPolicyToToscaElement(getVertexEither.left().value(), policyDefinition, counter);
2995 if (status != StorageOperationStatus.OK) {
2996 return Either.right(status);
2999 if (result == null) {
3000 result = Either.left(policyDefinition);
3005 public StorageOperationStatus associatePoliciesToComponent(String componentId, List<PolicyDefinition> policies) {
3006 log.debug("#associatePoliciesToComponent - associating policies for component {}.", componentId);
3007 return janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.ParseMetadata)
3008 .either(containerVertex -> topologyTemplateOperation.addPoliciesToToscaElement(containerVertex, policies),
3009 DaoStatusConverter::convertJanusGraphStatusToStorageStatus);
3012 public Either<PolicyDefinition, StorageOperationStatus> updatePolicyOfComponent(String componentId, PolicyDefinition policyDefinition, PromoteVersionEnum promoteVersionEnum) {
3013 Either<PolicyDefinition, StorageOperationStatus> result = null;
3014 Either<GraphVertex, JanusGraphOperationStatus> getVertexEither;
3015 getVertexEither = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
3016 if (getVertexEither.isRight()) {
3017 log.error(COULDNT_FETCH_A_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
3018 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value()));
3020 if (result == null) {
3021 policyDefinition.setVersion(GroupUtils.updateVersion(promoteVersionEnum, policyDefinition.getVersion()));
3022 StorageOperationStatus status = topologyTemplateOperation.updatePolicyOfToscaElement(getVertexEither.left().value(), policyDefinition);
3023 if (status != StorageOperationStatus.OK) {
3024 return Either.right(status);
3027 if (result == null) {
3028 result = Either.left(policyDefinition);
3033 public StorageOperationStatus updatePoliciesOfComponent(String componentId, List<PolicyDefinition> policyDefinition) {
3034 log.debug("#updatePoliciesOfComponent - updating policies for component {}", componentId);
3035 return janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse)
3037 .map(DaoStatusConverter::convertJanusGraphStatusToStorageStatus)
3038 .either(containerVertex -> topologyTemplateOperation.updatePoliciesOfToscaElement(containerVertex, policyDefinition),
3042 public StorageOperationStatus removePolicyFromComponent(String componentId, String policyId) {
3043 StorageOperationStatus status = null;
3044 Either<GraphVertex, JanusGraphOperationStatus> getVertexEither = janusGraphDao
3045 .getVertexById(componentId, JsonParseFlagEnum.NoParse);
3046 if (getVertexEither.isRight()) {
3047 log.error(COULDNT_FETCH_A_COMPONENT_WITH_AND_UNIQUE_ID_ERROR, componentId, getVertexEither.right().value());
3048 status = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getVertexEither.right().value());
3050 if (status == null) {
3051 status = topologyTemplateOperation.removePolicyFromToscaElement(getVertexEither.left().value(), policyId);
3056 public boolean canAddGroups(String componentId) {
3057 GraphVertex vertex = janusGraphDao.getVertexById(componentId)
3059 .on(this::onJanusGraphError);
3060 return topologyTemplateOperation.hasEdgeOfType(vertex, EdgeLabelEnum.GROUPS);
3063 GraphVertex onJanusGraphError(JanusGraphOperationStatus toe) {
3064 throw new StorageException(
3065 DaoStatusConverter.convertJanusGraphStatusToStorageStatus(toe));
3068 public CatalogUpdateTimestamp updateCatalogTimes() {
3069 long now = System.currentTimeMillis();
3071 GraphVertex catalogRoot = janusGraphDao.getVertexByLabel(VertexTypeEnum.CATALOG_ROOT)
3073 .on(this::onJanusGraphError);
3075 Long currentTime = (Long) catalogRoot.getMetadataProperty(GraphPropertyEnum.CURRENT_CATALOG_UPDATE_TIME);
3076 catalogRoot.addMetadataProperty(GraphPropertyEnum.PREV_CATALOG_UPDATE_TIME, currentTime);
3077 catalogRoot.addMetadataProperty(GraphPropertyEnum.CURRENT_CATALOG_UPDATE_TIME, now);
3079 janusGraphDao.updateVertex(catalogRoot).left().on(this::onJanusGraphError);
3081 return new CatalogUpdateTimestamp(currentTime, now);
3084 public CatalogUpdateTimestamp getCatalogTimes() {
3087 GraphVertex catalogRoot = janusGraphDao.getVertexByLabel(VertexTypeEnum.CATALOG_ROOT)
3089 .on(this::onJanusGraphError);
3091 Long currentTime = (Long) catalogRoot.getMetadataProperty(GraphPropertyEnum.CURRENT_CATALOG_UPDATE_TIME);
3092 Long prevTime = (Long) catalogRoot.getMetadataProperty(GraphPropertyEnum.PREV_CATALOG_UPDATE_TIME);
3094 return new CatalogUpdateTimestamp(prevTime == null ? 0 : prevTime.longValue(), currentTime == null ? 0 : currentTime.longValue());
3097 public void updateNamesOfCalculatedCapabilitiesRequirements(String componentId) {
3098 topologyTemplateOperation
3099 .updateNamesOfCalculatedCapabilitiesRequirements(componentId, getTopologyTemplate(componentId));
3102 public void revertNamesOfCalculatedCapabilitiesRequirements(String componentId) {
3103 topologyTemplateOperation
3104 .revertNamesOfCalculatedCapabilitiesRequirements(componentId, getTopologyTemplate(componentId));
3107 private TopologyTemplate getTopologyTemplate(String componentId) {
3108 return (TopologyTemplate) topologyTemplateOperation
3109 .getToscaElement(componentId, getFilterComponentWithCapProperties())
3111 .on(this::throwStorageException);
3114 private ComponentParametersView getFilterComponentWithCapProperties() {
3115 ComponentParametersView filter = new ComponentParametersView();
3116 filter.setIgnoreCapabiltyProperties(false);
3120 private ToscaElement throwStorageException(StorageOperationStatus status) {
3121 throw new StorageException(status);
3124 public Either<Boolean, StorageOperationStatus> isComponentInUse(String componentId) {
3125 final List<EdgeLabelEnum> forbiddenEdgeLabelEnums = Arrays.asList(EdgeLabelEnum.INSTANCE_OF, EdgeLabelEnum.PROXY_OF, EdgeLabelEnum.ALLOTTED_OF);
3126 Either<GraphVertex, JanusGraphOperationStatus> vertexById = janusGraphDao.getVertexById(componentId);
3127 if (vertexById.isLeft()) {
3128 for (EdgeLabelEnum edgeLabelEnum : forbiddenEdgeLabelEnums) {
3129 Iterator<Edge> edgeItr = vertexById.left().value().getVertex().edges(Direction.IN, edgeLabelEnum.name());
3130 if(edgeItr != null && edgeItr.hasNext()){
3131 return Either.left(true);
3135 return Either.left(false);
3138 public Either<List<Component>, StorageOperationStatus> getComponentListByInvariantUuid
3139 (String componentInvariantUuid, Map<GraphPropertyEnum, Object> additionalPropertiesToMatch) {
3141 Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
3142 if (MapUtils.isNotEmpty(additionalPropertiesToMatch)) {
3143 propertiesToMatch.putAll(additionalPropertiesToMatch);
3145 propertiesToMatch.put(GraphPropertyEnum.INVARIANT_UUID, componentInvariantUuid);
3147 Either<List<GraphVertex>, JanusGraphOperationStatus> vertexEither = janusGraphDao
3148 .getByCriteria(null, propertiesToMatch, JsonParseFlagEnum.ParseMetadata);
3150 if (vertexEither.isRight()) {
3151 log.debug("Couldn't fetch metadata for component with type {} and invariantUUId {}, error: {}", componentInvariantUuid, vertexEither.right().value());
3152 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(vertexEither.right().value()));
3154 List<GraphVertex> vertexList = vertexEither.isLeft() ? vertexEither.left().value() : null;
3156 if (vertexList == null || vertexList.isEmpty()) {
3157 log.debug("Component with invariantUUId {} was not found", componentInvariantUuid);
3158 return Either.right(StorageOperationStatus.NOT_FOUND);
3161 ArrayList<Component> components = new ArrayList<>();
3162 for (GraphVertex vertex : vertexList) {
3163 Either<Component, StorageOperationStatus> toscaElementByOperation = getToscaElementByOperation(vertex);
3164 if (toscaElementByOperation.isRight()) {
3165 log.debug("Could not fetch the following Component by Invariant UUID {}", vertex.getUniqueId());
3166 return Either.right(toscaElementByOperation.right().value());
3168 components.add(toscaElementByOperation.left().value());
3171 return Either.left(components);
3174 public Either<List<Component>, StorageOperationStatus> getParentComponents(String componentId) {
3175 List<Component> parentComponents = new ArrayList<>();
3176 final List<EdgeLabelEnum> relationEdgeLabelEnums = Arrays.asList(EdgeLabelEnum.INSTANCE_OF, EdgeLabelEnum.PROXY_OF);
3177 Either<GraphVertex, JanusGraphOperationStatus> vertexById = janusGraphDao.getVertexById(componentId);
3178 if (vertexById.isLeft()) {
3179 for (EdgeLabelEnum edgeLabelEnum : relationEdgeLabelEnums) {
3180 Either<GraphVertex, JanusGraphOperationStatus> parentVertexEither = janusGraphDao
3181 .getParentVertex(vertexById.left().value(), edgeLabelEnum, JsonParseFlagEnum.ParseJson);
3182 if(parentVertexEither.isLeft()){
3183 Either<Component, StorageOperationStatus> componentEither = getToscaElement(parentVertexEither.left().value().getUniqueId());
3184 if(componentEither.isLeft()){
3185 parentComponents.add(componentEither.left().value());
3190 return Either.left(parentComponents);
3192 public void updateCapReqPropertiesOwnerId(String componentId) {
3193 topologyTemplateOperation
3194 .updateCapReqPropertiesOwnerId(componentId, getTopologyTemplate(componentId));
3197 public <T extends Component> Either<T, StorageOperationStatus> getLatestByServiceName(String serviceName) {
3198 return getLatestByName(GraphPropertyEnum.NAME, serviceName);