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=========================================================
20 package org.openecomp.sdc.be.components.impl;
22 import static java.util.stream.Collectors.toList;
23 import static java.util.stream.Collectors.toMap;
24 import static java.util.stream.Collectors.toSet;
25 import static org.apache.commons.collections.CollectionUtils.isNotEmpty;
26 import static org.apache.commons.collections.MapUtils.isEmpty;
27 import static org.apache.commons.collections.MapUtils.isNotEmpty;
28 import static org.openecomp.sdc.be.components.impl.ImportUtils.findFirstToscaStringElement;
29 import static org.openecomp.sdc.be.components.impl.ImportUtils.getPropertyJsonStringValue;
30 import static org.openecomp.sdc.be.tosca.CsarUtils.VF_NODE_TYPE_ARTIFACTS_PATH_PATTERN;
31 import static org.openecomp.sdc.common.api.Constants.DEFAULT_GROUP_VF_MODULE;
33 import java.util.ArrayList;
34 import java.util.Collection;
35 import java.util.Collections;
36 import java.util.EnumMap;
37 import java.util.HashMap;
38 import java.util.HashSet;
39 import java.util.Iterator;
40 import java.util.List;
41 import java.util.ListIterator;
43 import java.util.Map.Entry;
44 import java.util.Objects;
45 import java.util.Optional;
47 import java.util.function.Function;
48 import java.util.regex.Pattern;
49 import java.util.stream.Collectors;
51 import org.apache.commons.codec.binary.Base64;
52 import org.apache.commons.collections.CollectionUtils;
53 import org.apache.commons.collections.MapUtils;
54 import org.apache.commons.collections4.ListUtils;
55 import org.apache.commons.lang3.StringUtils;
56 import org.apache.commons.lang3.tuple.ImmutablePair;
57 import org.openecomp.sdc.be.catalog.enums.ChangeTypeEnum;
58 import org.openecomp.sdc.be.components.csar.CsarArtifactsAndGroupsBusinessLogic;
59 import org.openecomp.sdc.be.components.csar.CsarBusinessLogic;
60 import org.openecomp.sdc.be.components.csar.CsarInfo;
61 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic.ArtifactOperationEnum;
62 import org.openecomp.sdc.be.components.impl.ImportUtils.ResultStatusEnum;
63 import org.openecomp.sdc.be.components.impl.artifact.ArtifactOperationInfo;
64 import org.openecomp.sdc.be.components.impl.exceptions.BusinessLogicException;
65 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
66 import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException;
67 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
68 import org.openecomp.sdc.be.components.impl.utils.CINodeFilterUtils;
69 import org.openecomp.sdc.be.components.lifecycle.LifecycleBusinessLogic;
70 import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction;
71 import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction.LifecycleChanceActionEnum;
72 import org.openecomp.sdc.be.components.merge.TopologyComparator;
73 import org.openecomp.sdc.be.components.merge.property.PropertyDataValueMergeBusinessLogic;
74 import org.openecomp.sdc.be.components.merge.resource.ResourceDataMergeBusinessLogic;
75 import org.openecomp.sdc.be.components.merge.utils.MergeInstanceUtils;
76 import org.openecomp.sdc.be.components.property.PropertyConstraintsUtils;
77 import org.openecomp.sdc.be.components.validation.component.ComponentContactIdValidator;
78 import org.openecomp.sdc.be.components.validation.component.ComponentDescriptionValidator;
79 import org.openecomp.sdc.be.components.validation.component.ComponentIconValidator;
80 import org.openecomp.sdc.be.components.validation.component.ComponentNameValidator;
81 import org.openecomp.sdc.be.components.validation.component.ComponentProjectCodeValidator;
82 import org.openecomp.sdc.be.components.validation.component.ComponentTagsValidator;
83 import org.openecomp.sdc.be.components.validation.component.ComponentValidator;
84 import org.openecomp.sdc.be.config.BeEcompErrorManager;
85 import org.openecomp.sdc.be.config.BeEcompErrorManager.ErrorSeverity;
86 import org.openecomp.sdc.be.config.ConfigurationManager;
87 import org.openecomp.sdc.be.dao.api.ActionStatus;
88 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphDao;
89 import org.openecomp.sdc.be.datamodel.api.HighestFilterEnum;
90 import org.openecomp.sdc.be.datamodel.utils.ArtifactUtils;
91 import org.openecomp.sdc.be.datamodel.utils.UiComponentDataConverter;
92 import org.openecomp.sdc.be.datatypes.components.ResourceMetadataDataDefinition;
93 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
94 import org.openecomp.sdc.be.datatypes.elements.CapabilityDataDefinition;
95 import org.openecomp.sdc.be.datatypes.elements.GetInputValueDataDefinition;
96 import org.openecomp.sdc.be.datatypes.elements.GroupDataDefinition;
97 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
98 import org.openecomp.sdc.be.datatypes.elements.RequirementDataDefinition;
99 import org.openecomp.sdc.be.datatypes.elements.ToscaArtifactDataDefinition;
100 import org.openecomp.sdc.be.datatypes.enums.ComponentFieldsEnum;
101 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
102 import org.openecomp.sdc.be.datatypes.enums.CreatedFrom;
103 import org.openecomp.sdc.be.datatypes.enums.ModelTypeEnum;
104 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
105 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
106 import org.openecomp.sdc.be.impl.ComponentsUtils;
107 import org.openecomp.sdc.be.info.NodeTypeInfoToUpdateArtifacts;
108 import org.openecomp.sdc.be.model.ArtifactDefinition;
109 import org.openecomp.sdc.be.model.AttributeDefinition;
110 import org.openecomp.sdc.be.model.CapabilityDefinition;
111 import org.openecomp.sdc.be.model.CapabilityRequirementRelationship;
112 import org.openecomp.sdc.be.model.CapabilityTypeDefinition;
113 import org.openecomp.sdc.be.model.Component;
114 import org.openecomp.sdc.be.model.ComponentInstance;
115 import org.openecomp.sdc.be.model.ComponentInstanceInput;
116 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
117 import org.openecomp.sdc.be.model.ComponentParametersView;
118 import org.openecomp.sdc.be.model.DataTypeDefinition;
119 import org.openecomp.sdc.be.model.GroupDefinition;
120 import org.openecomp.sdc.be.model.InputDefinition;
121 import org.openecomp.sdc.be.model.InterfaceDefinition;
122 import org.openecomp.sdc.be.model.LifeCycleTransitionEnum;
123 import org.openecomp.sdc.be.model.LifecycleStateEnum;
124 import org.openecomp.sdc.be.model.Model;
125 import org.openecomp.sdc.be.model.NodeTypeInfo;
126 import org.openecomp.sdc.be.model.Operation;
127 import org.openecomp.sdc.be.model.ParsedToscaYamlInfo;
128 import org.openecomp.sdc.be.model.PolicyDefinition;
129 import org.openecomp.sdc.be.model.PolicyTypeDefinition;
130 import org.openecomp.sdc.be.model.PropertyDefinition;
131 import org.openecomp.sdc.be.model.RelationshipImpl;
132 import org.openecomp.sdc.be.model.RelationshipInfo;
133 import org.openecomp.sdc.be.model.RequirementCapabilityRelDef;
134 import org.openecomp.sdc.be.model.RequirementDefinition;
135 import org.openecomp.sdc.be.model.Resource;
136 import org.openecomp.sdc.be.model.UploadArtifactInfo;
137 import org.openecomp.sdc.be.model.UploadCapInfo;
138 import org.openecomp.sdc.be.model.UploadComponentInstanceInfo;
139 import org.openecomp.sdc.be.model.UploadInfo;
140 import org.openecomp.sdc.be.model.UploadNodeFilterInfo;
141 import org.openecomp.sdc.be.model.UploadPropInfo;
142 import org.openecomp.sdc.be.model.UploadReqInfo;
143 import org.openecomp.sdc.be.model.UploadResourceInfo;
144 import org.openecomp.sdc.be.model.User;
145 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
146 import org.openecomp.sdc.be.model.category.CategoryDefinition;
147 import org.openecomp.sdc.be.model.category.SubCategoryDefinition;
148 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ArtifactsOperations;
149 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.InterfaceOperation;
150 import org.openecomp.sdc.be.model.jsonjanusgraph.utils.ModelConverter;
151 import org.openecomp.sdc.be.model.operations.StorageException;
152 import org.openecomp.sdc.be.model.operations.api.ICapabilityTypeOperation;
153 import org.openecomp.sdc.be.model.operations.api.IElementOperation;
154 import org.openecomp.sdc.be.model.operations.api.IGroupInstanceOperation;
155 import org.openecomp.sdc.be.model.operations.api.IGroupOperation;
156 import org.openecomp.sdc.be.model.operations.api.IGroupTypeOperation;
157 import org.openecomp.sdc.be.model.operations.api.IInterfaceLifecycleOperation;
158 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
159 import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation;
160 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
161 import org.openecomp.sdc.be.model.operations.utils.ComponentValidationUtils;
162 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
163 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
164 import org.openecomp.sdc.be.resources.data.auditing.model.ResourceVersionInfo;
165 import org.openecomp.sdc.be.tosca.CsarUtils;
166 import org.openecomp.sdc.be.tosca.CsarUtils.NonMetaArtifactInfo;
167 import org.openecomp.sdc.be.ui.model.UiComponentDataTransfer;
168 import org.openecomp.sdc.be.user.UserBusinessLogic;
169 import org.openecomp.sdc.be.utils.CommonBeUtils;
170 import org.openecomp.sdc.be.utils.TypeUtils;
171 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
172 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
173 import org.openecomp.sdc.common.api.Constants;
174 import org.openecomp.sdc.common.datastructure.Wrapper;
175 import org.openecomp.sdc.common.kpi.api.ASDCKpiApi;
176 import org.openecomp.sdc.common.log.elements.LoggerSupportability;
177 import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode;
178 import org.openecomp.sdc.common.log.enums.LoggerSupportabilityActions;
179 import org.openecomp.sdc.common.log.enums.StatusCode;
180 import org.openecomp.sdc.common.log.wrappers.Logger;
181 import org.openecomp.sdc.common.util.GeneralUtility;
182 import org.openecomp.sdc.common.util.ValidationUtils;
183 import org.openecomp.sdc.exception.ResponseFormat;
184 import org.springframework.beans.factory.annotation.Autowired;
185 import org.springframework.context.annotation.Lazy;
186 import org.yaml.snakeyaml.DumperOptions;
187 import org.yaml.snakeyaml.Yaml;
189 import com.google.common.annotations.VisibleForTesting;
191 import fj.data.Either;
193 @org.springframework.stereotype.Component("resourceBusinessLogic")
194 public class ResourceBusinessLogic extends ComponentBusinessLogic {
196 private static final String DELETE_RESOURCE = "Delete Resource";
197 private static final String IN_RESOURCE = " in resource {} ";
198 private static final String PLACE_HOLDER_RESOURCE_TYPES = "validForResourceTypes";
199 private static final String INITIAL_VERSION = "0.1";
200 private static final Logger log = Logger.getLogger(ResourceBusinessLogic.class);
201 private static final String CERTIFICATION_ON_IMPORT = "certification on import";
202 private static final String CREATE_RESOURCE = "Create Resource";
203 private static final String VALIDATE_DERIVED_BEFORE_UPDATE = "validate derived before update";
204 private static final String CATEGORY_IS_EMPTY = "Resource category is empty";
205 private static final String CREATE_RESOURCE_VALIDATE_CAPABILITY_TYPES = "Create Resource - validateCapabilityTypesCreate";
206 private static final String COMPONENT_INSTANCE_WITH_NAME = "component instance with name ";
207 private static final String COMPONENT_INSTANCE_WITH_NAME_IN_RESOURCE = "component instance with name {} in resource {} ";
208 private static final String VALID_CHARACTERS_ARTIFACT_NAME = "'A-Z', 'a-z', '0-9', '.', '_', '-', '@' and space";
209 private static final LoggerSupportability loggerSupportability = LoggerSupportability.getLogger(ResourceBusinessLogic.class.getName());
210 private final ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
211 private final ResourceImportManager resourceImportManager;
212 private final InputsBusinessLogic inputsBusinessLogic;
213 private final OutputsBusinessLogic outputsBusinessLogic;
214 private final CompositionBusinessLogic compositionBusinessLogic;
215 private final ResourceDataMergeBusinessLogic resourceDataMergeBusinessLogic;
216 private final CsarArtifactsAndGroupsBusinessLogic csarArtifactsAndGroupsBusinessLogic;
217 private final MergeInstanceUtils mergeInstanceUtils;
218 private final UiComponentDataConverter uiComponentDataConverter;
219 private final CsarBusinessLogic csarBusinessLogic;
220 private final PropertyBusinessLogic propertyBusinessLogic;
221 private final PolicyBusinessLogic policyBusinessLogic;
222 private final ModelBusinessLogic modelBusinessLogic;
223 private IInterfaceLifecycleOperation interfaceTypeOperation;
224 private LifecycleBusinessLogic lifecycleBusinessLogic;
225 private final DataTypeBusinessLogic dataTypeBusinessLogic;
226 private final PolicyTypeBusinessLogic policyTypeBusinessLogic;
229 private ICapabilityTypeOperation capabilityTypeOperation;
231 private TopologyComparator topologyComparator;
233 private ComponentValidator componentValidator;
235 private PropertyDataValueMergeBusinessLogic propertyDataValueMergeBusinessLogic;
237 private SoftwareInformationBusinessLogic softwareInformationBusinessLogic;
241 public ResourceBusinessLogic(final IElementOperation elementDao, final IGroupOperation groupOperation,
242 final IGroupInstanceOperation groupInstanceOperation, final IGroupTypeOperation groupTypeOperation,
243 final GroupBusinessLogic groupBusinessLogic, final InterfaceOperation interfaceOperation,
244 final InterfaceLifecycleOperation interfaceLifecycleTypeOperation,
245 final ArtifactsBusinessLogic artifactsBusinessLogic,
246 final ComponentInstanceBusinessLogic componentInstanceBusinessLogic,
247 final @Lazy ResourceImportManager resourceImportManager, final InputsBusinessLogic inputsBusinessLogic,
248 final OutputsBusinessLogic outputsBusinessLogic, final CompositionBusinessLogic compositionBusinessLogic,
249 final ResourceDataMergeBusinessLogic resourceDataMergeBusinessLogic,
250 final CsarArtifactsAndGroupsBusinessLogic csarArtifactsAndGroupsBusinessLogic,
251 final MergeInstanceUtils mergeInstanceUtils, final UiComponentDataConverter uiComponentDataConverter,
252 final CsarBusinessLogic csarBusinessLogic, final ArtifactsOperations artifactToscaOperation,
253 final PropertyBusinessLogic propertyBusinessLogic, final ComponentContactIdValidator componentContactIdValidator,
254 final ComponentNameValidator componentNameValidator, final ComponentTagsValidator componentTagsValidator,
255 final ComponentValidator componentValidator, final ComponentIconValidator componentIconValidator,
256 final ComponentProjectCodeValidator componentProjectCodeValidator,
257 final ComponentDescriptionValidator componentDescriptionValidator, final PolicyBusinessLogic policyBusinessLogic,
258 final ModelBusinessLogic modelBusinessLogic,
259 final DataTypeBusinessLogic dataTypeBusinessLogic, final PolicyTypeBusinessLogic policyTypeBusinessLogic) {
260 super(elementDao, groupOperation, groupInstanceOperation, groupTypeOperation, groupBusinessLogic, interfaceOperation,
261 interfaceLifecycleTypeOperation, artifactsBusinessLogic, artifactToscaOperation, componentContactIdValidator, componentNameValidator,
262 componentTagsValidator, componentValidator, componentIconValidator, componentProjectCodeValidator, componentDescriptionValidator);
263 this.componentInstanceBusinessLogic = componentInstanceBusinessLogic;
264 this.resourceImportManager = resourceImportManager;
265 this.inputsBusinessLogic = inputsBusinessLogic;
266 this.outputsBusinessLogic = outputsBusinessLogic;
267 this.compositionBusinessLogic = compositionBusinessLogic;
268 this.resourceDataMergeBusinessLogic = resourceDataMergeBusinessLogic;
269 this.csarArtifactsAndGroupsBusinessLogic = csarArtifactsAndGroupsBusinessLogic;
270 this.mergeInstanceUtils = mergeInstanceUtils;
271 this.uiComponentDataConverter = uiComponentDataConverter;
272 this.csarBusinessLogic = csarBusinessLogic;
273 this.propertyBusinessLogic = propertyBusinessLogic;
274 this.policyBusinessLogic = policyBusinessLogic;
275 this.modelBusinessLogic = modelBusinessLogic;
276 this.dataTypeBusinessLogic = dataTypeBusinessLogic;
277 this.policyTypeBusinessLogic = policyTypeBusinessLogic;
280 static <T> Either<T, RuntimeException> rollbackWithEither(final JanusGraphDao janusGraphDao, final ActionStatus actionStatus,
281 final String... params) {
282 if (janusGraphDao != null) {
283 janusGraphDao.rollback();
285 return Either.right(new ByActionStatusComponentException(actionStatus, params));
288 public LifecycleBusinessLogic getLifecycleBusinessLogic() {
289 return lifecycleBusinessLogic;
293 public void setLifecycleManager(LifecycleBusinessLogic lifecycleBusinessLogic) {
294 this.lifecycleBusinessLogic = lifecycleBusinessLogic;
298 protected void setComponentValidator(ComponentValidator componentValidator) {
299 this.componentValidator = componentValidator;
302 public IElementOperation getElementDao() {
306 public void setElementDao(IElementOperation elementDao) {
307 this.elementDao = elementDao;
310 public UserBusinessLogic getUserAdmin() {
311 return this.userAdmin;
316 public void setUserAdmin(UserBusinessLogic userAdmin) {
317 this.userAdmin = userAdmin;
320 public ComponentsUtils getComponentsUtils() {
321 return this.componentsUtils;
326 public void setComponentsUtils(ComponentsUtils componentsUtils) {
327 this.componentsUtils = componentsUtils;
330 public ArtifactsBusinessLogic getArtifactsManager() {
331 return artifactsBusinessLogic;
334 public void setArtifactsManager(ArtifactsBusinessLogic artifactsManager) {
335 this.artifactsBusinessLogic = artifactsManager;
338 public ApplicationDataTypeCache getApplicationDataTypeCache() {
339 return applicationDataTypeCache;
344 public void setApplicationDataTypeCache(ApplicationDataTypeCache applicationDataTypeCache) {
345 this.applicationDataTypeCache = applicationDataTypeCache;
349 public void setInterfaceTypeOperation(IInterfaceLifecycleOperation interfaceTypeOperation) {
350 this.interfaceTypeOperation = interfaceTypeOperation;
354 * the method returns a list of all the resources that are certified, the returned resources are only abstract or only none abstract according to
361 public List<Resource> getAllCertifiedResources(boolean getAbstract, HighestFilterEnum highestFilter, String userId) {
362 User user = validateUserExists(userId);
363 Boolean isHighest = null;
364 switch (highestFilter) {
370 case NON_HIGHEST_ONLY:
376 Either<List<Resource>, StorageOperationStatus> getResponse = toscaOperationFacade.getAllCertifiedResources(getAbstract, isHighest);
377 if (getResponse.isRight()) {
378 throw new StorageException(getResponse.right().value());
380 return getResponse.left().value();
383 public Either<Map<String, Boolean>, ResponseFormat> validateResourceNameExists(String resourceName, ResourceTypeEnum resourceTypeEnum,
385 validateUserExists(userId);
386 Either<Boolean, StorageOperationStatus> dataModelResponse = toscaOperationFacade
387 .validateComponentNameUniqueness(resourceName, resourceTypeEnum, ComponentTypeEnum.RESOURCE);
389 janusGraphDao.commit();
390 if (dataModelResponse.isLeft()) {
391 Map<String, Boolean> result = new HashMap<>();
392 result.put("isValid", dataModelResponse.left().value());
393 log.debug("validation was successfully performed.");
394 return Either.left(result);
396 ResponseFormat responseFormat = componentsUtils
397 .getResponseFormat(componentsUtils.convertFromStorageResponse(dataModelResponse.right().value()));
398 return Either.right(responseFormat);
401 public Resource createResource(Resource resource, AuditingActionEnum auditingAction, User user, Map<String, byte[]> csarUIPayload,
402 String payloadName) {
403 validateResourceBeforeCreate(resource, user, false);
404 String csarUUID = payloadName == null ? resource.getCsarUUID() : payloadName;
405 loggerSupportability.log(LoggerSupportabilityActions.CREATE_RESOURCE, resource.getComponentMetadataForSupportLog(), StatusCode.STARTED,
406 "Starting to create resource from CSAR by user {} ", user.getUserId());
407 if (StringUtils.isNotEmpty(csarUUID)) {
408 csarBusinessLogic.validateCsarBeforeCreate(resource, auditingAction, user, csarUUID);
409 log.debug("CsarUUID is {} - going to create resource from CSAR", csarUUID);
410 Resource createResourceFromCsar = createResourceFromCsar(resource, user, csarUIPayload, csarUUID);
411 return updateCatalog(createResourceFromCsar, ChangeTypeEnum.LIFECYCLE).left().map(Resource.class::cast).left().value();
413 final Resource createResourceByDao = createResourceByDao(resource, user, auditingAction, false, false);
414 return updateCatalog(createResourceByDao, ChangeTypeEnum.LIFECYCLE).left().map(Resource.class::cast).left().value();
417 public Resource validateAndUpdateResourceFromCsar(Resource resource, User user, Map<String, byte[]> csarUIPayload, String payloadName,
418 String resourceUniqueId) {
419 String csarUUID = payloadName;
420 String csarVersion = null;
421 Resource updatedResource = null;
422 if (payloadName == null) {
423 csarUUID = resource.getCsarUUID();
424 csarVersion = resource.getCsarVersion();
426 if (csarUUID != null && !csarUUID.isEmpty()) {
427 Resource oldResource = getResourceByUniqueId(resourceUniqueId);
428 validateCsarUuidMatching(oldResource, resource, csarUUID, resourceUniqueId, user);
429 validateCsarIsNotAlreadyUsed(oldResource, resource, csarUUID, user);
430 if (oldResource != null && ValidationUtils.hasBeenCertified(oldResource.getVersion())) {
431 overrideImmutableMetadata(oldResource, resource);
433 validateResourceBeforeCreate(resource, user, false);
434 String oldCsarVersion = oldResource != null ? oldResource.getCsarVersion() : null;
435 log.debug("CsarUUID is {} - going to update resource with UniqueId {} from CSAR", csarUUID, resourceUniqueId);
436 // (on boarding flow): If the update includes same csarUUID and
438 // same csarVersion as already in the VF - no need to import the
440 // csar (do only metadata changes if there are).
441 if (csarVersion != null && oldCsarVersion != null && oldCsarVersion.equals(csarVersion)) {
442 updatedResource = updateResourceMetadata(resourceUniqueId, resource, oldResource, user, false);
444 updatedResource = updateResourceFromCsar(oldResource, resource, user, AuditingActionEnum.UPDATE_RESOURCE_METADATA, false,
445 csarUIPayload, csarUUID);
448 log.debug("Failed to update resource {}, csarUUID or payload name is missing", resource.getSystemName());
449 ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.MISSING_CSAR_UUID, resource.getName());
450 componentsUtils.auditResource(errorResponse, user, resource, AuditingActionEnum.CREATE_RESOURCE);
451 throw new ByActionStatusComponentException(ActionStatus.MISSING_CSAR_UUID, resource.getName());
453 return updatedResource;
456 private void validateCsarIsNotAlreadyUsed(Resource oldResource, Resource resource, String csarUUID, User user) {
457 // (on boarding flow): If the update includes a csarUUID: verify this
459 // csarUUID is not in use by another VF, If it is - use same error as
463 // "Error: The VSP with UUID %1 was already imported for VF %2. Please
465 // select another or update the existing VF." %1 - csarUUID, %2 - VF
468 Either<Resource, StorageOperationStatus> resourceLinkedToCsarRes = toscaOperationFacade
469 .getLatestComponentByCsarOrName(ComponentTypeEnum.RESOURCE, csarUUID, resource.getSystemName());
470 if (resourceLinkedToCsarRes.isRight()) {
471 if (StorageOperationStatus.NOT_FOUND != resourceLinkedToCsarRes.right().value()) {
472 log.debug("Failed to find previous resource by CSAR {} and system name {}", csarUUID, resource.getSystemName());
473 throw new StorageException(resourceLinkedToCsarRes.right().value());
475 } else if (!resourceLinkedToCsarRes.left().value().getUniqueId().equals(oldResource.getUniqueId()) && !resourceLinkedToCsarRes.left().value()
476 .getName().equals(oldResource.getName())) {
477 ResponseFormat errorResponse = componentsUtils
478 .getResponseFormat(ActionStatus.VSP_ALREADY_EXISTS, csarUUID, resourceLinkedToCsarRes.left().value().getName());
479 componentsUtils.auditResource(errorResponse, user, resource, AuditingActionEnum.UPDATE_RESOURCE_METADATA);
480 throw new ByActionStatusComponentException(ActionStatus.VSP_ALREADY_EXISTS, csarUUID, resourceLinkedToCsarRes.left().value().getName());
484 private void validateCsarUuidMatching(Resource resource, Resource oldResource, String csarUUID, String resourceUniqueId, User user) {
485 // (on boarding flow): If the update includes csarUUID which is
487 // different from the csarUUID of the VF - fail with
489 // error: "Error: Resource %1 cannot be updated using since it is linked
491 // to a different VSP" %1 - VF name
492 String oldCsarUUID = oldResource.getCsarUUID();
493 if (oldCsarUUID != null && !oldCsarUUID.isEmpty() && !csarUUID.equals(oldCsarUUID)) {
494 log.debug("Failed to update resource with UniqueId {} using Csar {}, since the resource is linked to a different VSP {}",
495 resourceUniqueId, csarUUID, oldCsarUUID);
496 ResponseFormat errorResponse = componentsUtils
497 .getResponseFormat(ActionStatus.RESOURCE_LINKED_TO_DIFFERENT_VSP, resource.getName(), csarUUID, oldCsarUUID);
498 componentsUtils.auditResource(errorResponse, user, resource, AuditingActionEnum.UPDATE_RESOURCE_METADATA);
499 throw new ByActionStatusComponentException(ActionStatus.RESOURCE_LINKED_TO_DIFFERENT_VSP, resource.getName(), csarUUID, oldCsarUUID);
503 private Resource getResourceByUniqueId(String resourceUniqueId) {
504 Either<Resource, StorageOperationStatus> oldResourceRes = toscaOperationFacade.getToscaFullElement(resourceUniqueId);
505 if (oldResourceRes.isRight()) {
506 log.debug("Failed to find previous resource by UniqueId {}, status: {}", resourceUniqueId, oldResourceRes.right().value());
507 throw new StorageException(oldResourceRes.right().value());
509 return oldResourceRes.left().value();
512 private void overrideImmutableMetadata(Resource oldResource, Resource resource) {
513 resource.setName(oldResource.getName());
514 resource.setIcon(oldResource.getIcon());
515 resource.setTags(oldResource.getTags());
516 resource.setCategories(oldResource.getCategories());
517 resource.setDerivedFrom(oldResource.getDerivedFrom());
520 private Resource updateResourceFromCsar(Resource oldResource, Resource newResource, User user, AuditingActionEnum updateResource,
521 boolean inTransaction, Map<String, byte[]> csarUIPayload, String csarUUID) {
522 Resource updatedResource = null;
523 validateLifecycleState(oldResource, user);
524 String lockedResourceId = oldResource.getUniqueId();
525 List<ArtifactDefinition> createdArtifacts = new ArrayList<>();
526 CsarInfo csarInfo = csarBusinessLogic.getCsarInfo(newResource, oldResource, user, csarUIPayload, csarUUID);
527 lockComponent(lockedResourceId, oldResource, "update Resource From Csar");
528 Map<String, NodeTypeInfo> nodeTypesInfo = csarInfo.extractTypesInfo();
529 Either<Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>>, ResponseFormat> findNodeTypesArtifactsToHandleRes = findNodeTypesArtifactsToHandle(
530 nodeTypesInfo, csarInfo, oldResource);
531 if (findNodeTypesArtifactsToHandleRes.isRight()) {
532 log.debug("failed to find node types for update with artifacts during import csar {}. ", csarInfo.getCsarUUID());
533 throw new ByResponseFormatComponentException(findNodeTypesArtifactsToHandleRes.right().value());
535 Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle = findNodeTypesArtifactsToHandleRes.left()
538 updatedResource = updateResourceFromYaml(oldResource, newResource, updateResource, createdArtifacts, csarInfo.getMainTemplateName(),
539 csarInfo.getMainTemplateContent(), csarInfo, nodeTypesInfo, nodeTypesArtifactsToHandle, null, false);
540 } catch (ComponentException | StorageException e) {
541 rollback(inTransaction, newResource, createdArtifacts, null);
544 janusGraphDao.commit();
545 log.debug("unlock resource {}", lockedResourceId);
546 graphLockOperation.unlockComponent(lockedResourceId, NodeTypeEnum.Resource);
548 return updatedResource;
551 private void validateLifecycleState(Resource oldResource, User user) {
552 if (LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT == oldResource.getLifecycleState() && !oldResource.getLastUpdaterUserId()
553 .equals(user.getUserId())) {
554 log.debug("#validateLifecycleState - Current user is not last updater, last updater userId: {}, current user userId: {}",
555 oldResource.getLastUpdaterUserId(), user.getUserId());
556 throw new ByActionStatusComponentException(ActionStatus.RESTRICTED_OPERATION);
560 private Resource updateResourceFromYaml(Resource oldResource, Resource newResource, AuditingActionEnum actionEnum,
561 List<ArtifactDefinition> createdArtifacts, String yamlFileName, String yamlFileContent, CsarInfo csarInfo,
562 Map<String, NodeTypeInfo> nodeTypesInfo,
563 Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle,
564 String nodeName, boolean isNested) {
565 boolean inTransaction = true;
566 boolean shouldLock = false;
567 Resource preparedResource = null;
568 ParsedToscaYamlInfo uploadComponentInstanceInfoMap;
570 uploadComponentInstanceInfoMap = csarBusinessLogic
571 .getParsedToscaYamlInfo(yamlFileContent, yamlFileName, nodeTypesInfo, csarInfo, nodeName, oldResource);
572 Map<String, UploadComponentInstanceInfo> instances = uploadComponentInstanceInfoMap.getInstances();
573 if (MapUtils.isEmpty(instances) && newResource.getResourceType() != ResourceTypeEnum.PNF) {
574 throw new ByActionStatusComponentException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlFileName);
576 preparedResource = updateExistingResourceByImport(newResource, oldResource, csarInfo.getModifier(), inTransaction, shouldLock,
578 log.trace("YAML topology file found in CSAR, file name: {}, contents: {}", yamlFileName, yamlFileContent);
579 handleResourceGenericType(preparedResource, yamlFileContent, uploadComponentInstanceInfoMap,
580 uploadComponentInstanceInfoMap.getSubstitutionMappingNodeType());
581 handleNodeTypes(yamlFileName, preparedResource, yamlFileContent, shouldLock, nodeTypesArtifactsToHandle, createdArtifacts, nodeTypesInfo,
582 csarInfo, nodeName, newResource.getModel());
583 preparedResource = createInputsOnResource(preparedResource, uploadComponentInstanceInfoMap.getInputs());
584 Map<String, Resource> existingNodeTypesByResourceNames = new HashMap<>();
585 final Map<String, UploadComponentInstanceInfo> instancesToCreate = getInstancesToCreate(uploadComponentInstanceInfoMap,
586 newResource.getModel());
587 preparedResource = createResourceInstances(yamlFileName, preparedResource, oldResource, instancesToCreate, csarInfo.getCreatedNodes(),
588 existingNodeTypesByResourceNames);
589 preparedResource = createResourceInstancesRelations(csarInfo.getModifier(), yamlFileName, preparedResource, oldResource,
591 existingNodeTypesByResourceNames);
592 } catch (ComponentException e) {
593 ResponseFormat responseFormat =
594 e.getResponseFormat() == null ? componentsUtils.getResponseFormat(e.getActionStatus(), e.getParams()) : e.getResponseFormat();
595 log.debug("#updateResourceFromYaml - failed to update newResource from yaml {} .The error is {}", yamlFileName, responseFormat);
597 .auditResource(responseFormat, csarInfo.getModifier(), preparedResource == null ? oldResource : preparedResource, actionEnum);
599 } catch (StorageException e) {
600 ResponseFormat responseFormat = componentsUtils
601 .getResponseFormat(componentsUtils.convertFromStorageResponse(e.getStorageOperationStatus()));
602 log.debug("#updateResourceFromYaml - failed to update newResource from yaml {} .The error is {}", yamlFileName, responseFormat);
604 .auditResource(responseFormat, csarInfo.getModifier(), preparedResource == null ? oldResource : preparedResource, actionEnum);
607 Either<Map<String, GroupDefinition>, ResponseFormat> validateUpdateVfGroupNamesRes = groupBusinessLogic
608 .validateUpdateVfGroupNames(uploadComponentInstanceInfoMap.getGroups(), preparedResource.getSystemName());
609 if (validateUpdateVfGroupNamesRes.isRight()) {
610 throw new ByResponseFormatComponentException(validateUpdateVfGroupNamesRes.right().value());
612 // add groups to newResource
613 Map<String, GroupDefinition> groups;
614 if (!validateUpdateVfGroupNamesRes.left().value().isEmpty()) {
615 groups = validateUpdateVfGroupNamesRes.left().value();
617 groups = uploadComponentInstanceInfoMap.getGroups();
619 handleGroupsProperties(preparedResource, groups);
620 Either<Boolean, ActionStatus> isTopologyChanged = topologyComparator.isTopologyChanged(oldResource, preparedResource);
621 preparedResource = updateGroupsOnResource(preparedResource, groups);
622 NodeTypeInfoToUpdateArtifacts nodeTypeInfoToUpdateArtifacts = new NodeTypeInfoToUpdateArtifacts(nodeName, nodeTypesArtifactsToHandle);
623 Either<Resource, ResponseFormat> updateArtifactsEither = createOrUpdateArtifacts(ArtifactOperationEnum.UPDATE, createdArtifacts, yamlFileName,
624 csarInfo, preparedResource, nodeTypeInfoToUpdateArtifacts, inTransaction, shouldLock);
625 if (updateArtifactsEither.isRight()) {
626 log.debug("failed to update artifacts {}", updateArtifactsEither.right().value());
627 throw new ByResponseFormatComponentException(updateArtifactsEither.right().value());
629 preparedResource = getResourceWithGroups(updateArtifactsEither.left().value().getUniqueId());
630 updateGroupsName(oldResource, preparedResource, isTopologyChanged.left().value());
631 updateResourceInstancesNames(oldResource, csarInfo, preparedResource, isTopologyChanged.left().value());
632 final String preparedResourceId = preparedResource != null ? preparedResource.getUniqueId() : "";
633 preparedResource = getResourceWithGroups(preparedResourceId);
634 updateVolumeGroup(preparedResource);
635 ActionStatus mergingPropsAndInputsStatus = resourceDataMergeBusinessLogic.mergeResourceEntities(oldResource, preparedResource);
636 if (mergingPropsAndInputsStatus != ActionStatus.OK) {
637 ResponseFormat responseFormat = componentsUtils.getResponseFormatByResource(mergingPropsAndInputsStatus, preparedResource);
638 throw new ByResponseFormatComponentException(responseFormat);
640 compositionBusinessLogic.setPositionsForComponentInstances(preparedResource, csarInfo.getModifier().getUserId());
641 return preparedResource;
644 protected void updateVolumeGroup(Resource preparedResource) {
645 List<GroupDefinition> groups = preparedResource.safeGetGroups();
646 for (GroupDefinition group : groups) {
647 Map<String, ArtifactDefinition> createdNewArtifacts = preparedResource.getDeploymentArtifacts();
648 if (DEFAULT_GROUP_VF_MODULE.equals(group.getType())) {
649 List<PropertyDataDefinition> volumePropList = group.getProperties().stream().filter(p -> "volume_group".equals(p.getName()))
650 .collect(Collectors.toList());
651 if (!volumePropList.isEmpty()) {
652 PropertyDataDefinition volumeProp = volumePropList.get(0);
653 if (volumeProp != null) {
654 boolean isVolumeGroup = isVolumeGroup(group.getArtifacts(), new ArrayList<>(createdNewArtifacts.values()));
655 if (!volumePropList.get(0).getValue().equals(String.valueOf(isVolumeGroup))) {
656 volumeProp.setValue(String.valueOf(isVolumeGroup));
657 volumeProp.setDefaultValue(String.valueOf(isVolumeGroup));
665 private void updateGroupsName(Resource oldResource, Resource preparedResource, boolean isTopologyChanged) {
666 if (oldResource == null || preparedResource == null) {
667 log.debug("Failed to update groups name : oldResource or preparedResource is null");
668 } else if (CollectionUtils.isNotEmpty(oldResource.getGroups()) && CollectionUtils.isNotEmpty(preparedResource.getGroups())) {
669 Map<String, String> oldGroups = oldResource.getGroups().stream()
670 .collect(toMap(GroupDataDefinition::getInvariantName, GroupDataDefinition::getName));
671 List<GroupDefinition> updatedGroups = preparedResource.getGroups().stream()
672 .filter(group -> oldGroups.containsKey(group.getInvariantName()) && !group.getName().equals(oldGroups.get(group.getInvariantName())))
674 if (CollectionUtils.isNotEmpty(updatedGroups)) {
675 if (isTopologyChanged) {
676 updatedGroups.stream().filter(group -> !group.isVspOriginated())
677 .forEach(group -> group.setName(oldGroups.get(group.getInvariantName())));
679 updatedGroups.forEach(group -> group.setName(oldGroups.get(group.getInvariantName())));
681 groupBusinessLogic.updateGroups(preparedResource, updatedGroups, false);
686 private void updateResourceInstancesNames(Resource oldResource, CsarInfo csarInfo, Resource preparedResource, boolean isTopologyChanged) {
687 if (oldResource == null || preparedResource == null) {
688 log.debug("Failed to update resource instances names : oldResource or preparedResource is null");
690 if (CollectionUtils.isNotEmpty(oldResource.getComponentInstances())) {
691 Map<String, String> oldInstances = oldResource.getComponentInstances().stream()
692 .collect(toMap(ComponentInstance::getInvariantName, ComponentInstance::getName));
693 List<ComponentInstance> updatedInstances = preparedResource.getComponentInstances().stream()
694 .filter(i -> oldInstances.containsKey(i.getInvariantName()) && !i.getName().equals(oldInstances.get(i.getInvariantName())))
696 if (CollectionUtils.isNotEmpty(updatedInstances)) {
697 if (isTopologyChanged) {
698 updatedInstances.stream().filter(i -> !i.isCreatedFromCsar()).forEach(i -> i.setName(oldInstances.get(i.getInvariantName())));
700 updatedInstances.forEach(i -> i.setName(oldInstances.get(i.getInvariantName())));
704 componentInstanceBusinessLogic.updateComponentInstance(ComponentTypeEnum.RESOURCE_PARAM_NAME, null, preparedResource.getUniqueId(),
705 csarInfo.getModifier().getUserId(), preparedResource.getComponentInstances(), false);
709 private Either<Resource, ResponseFormat> createOrUpdateArtifacts(ArtifactOperationEnum operation, List<ArtifactDefinition> createdArtifacts,
710 String yamlFileName, CsarInfo csarInfo, Resource preparedResource,
711 NodeTypeInfoToUpdateArtifacts nodeTypeInfoToUpdateArtifacts,
712 boolean inTransaction, boolean shouldLock) {
713 String nodeName = nodeTypeInfoToUpdateArtifacts.getNodeName();
714 Resource resource = preparedResource;
715 Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle = nodeTypeInfoToUpdateArtifacts
716 .getNodeTypesArtifactsToHandle();
717 if (preparedResource.getResourceType() == ResourceTypeEnum.CVFC) {
718 if (nodeName != null && nodeTypesArtifactsToHandle.get(nodeName) != null && !nodeTypesArtifactsToHandle.get(nodeName).isEmpty()) {
719 Either<List<ArtifactDefinition>, ResponseFormat> handleNodeTypeArtifactsRes = handleNodeTypeArtifacts(preparedResource,
720 nodeTypesArtifactsToHandle.get(nodeName), createdArtifacts, csarInfo.getModifier(), inTransaction, true);
721 if (handleNodeTypeArtifactsRes.isRight()) {
722 return Either.right(handleNodeTypeArtifactsRes.right().value());
726 Either<Resource, ResponseFormat> createdCsarArtifactsEither = handleVfCsarArtifacts(preparedResource, csarInfo, createdArtifacts,
727 new ArtifactOperationInfo(false, false, operation), shouldLock, inTransaction);
728 log.trace("************* Finished to add artifacts from yaml {}", yamlFileName);
729 if (createdCsarArtifactsEither.isRight()) {
730 return createdCsarArtifactsEither;
732 resource = createdCsarArtifactsEither.left().value();
734 return Either.left(resource);
737 private Resource handleResourceGenericType(Resource resource) {
738 Resource genericResource = fetchAndSetDerivedFromGenericType(resource);
740 if (resource.shouldGenerateInputs()) {
741 generateAndAddInputsFromGenericTypeProperties(resource, genericResource);
743 return genericResource;
746 private Resource handleResourceGenericType(final Resource resource, final String topologyTemplateYaml,
747 final ParsedToscaYamlInfo parsedToscaYamlInfo, final String substitutionMappingNodeType) {
748 if (processSubstitutableAsNodeType(resource, parsedToscaYamlInfo)) {
749 final Map<String, Object> substitutableAsNodeType = getSubstitutableAsNodeTypeFromTemplate(
750 (Map<String, Object>) new Yaml().load(topologyTemplateYaml), substitutionMappingNodeType);
751 final Resource genericResource = fetchAndSetDerivedFromGenericType(resource,
752 (String) substitutableAsNodeType.get(TypeUtils.ToscaTagNamesEnum.DERIVED_FROM.getElementName()));
754 generatePropertiesFromGenericType(resource, genericResource);
755 generatePropertiesFromNodeType(resource, substitutableAsNodeType);
756 final String resourceId = resource.getUniqueId();
757 resource.getProperties().forEach(propertyDefinition -> propertyDefinition.setUniqueId(
758 UniqueIdBuilder.buildPropertyUniqueId(resourceId, propertyDefinition.getName())));
759 createResourcePropertiesOnGraph(resource);
760 return genericResource;
762 return handleResourceGenericType(resource);
765 private Either<Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>>, ResponseFormat> findNodeTypesArtifactsToHandle(
766 final Map<String, NodeTypeInfo> nodeTypesInfo, final CsarInfo csarInfo, final Resource oldResource) {
767 final Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle = new HashMap<>();
768 Either<Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>>, ResponseFormat> nodeTypesArtifactsToHandleRes = Either
769 .left(nodeTypesArtifactsToHandle);
771 final Map<String, List<ArtifactDefinition>> extractedVfcsArtifacts = CsarUtils.extractVfcsArtifactsFromCsar(csarInfo.getCsar());
772 final Map<String, ImmutablePair<String, String>> extractedVfcToscaNames = extractVfcToscaNames(nodeTypesInfo, oldResource.getName(),
774 log.debug("Going to fetch node types for resource with name {} during import csar with UUID {}. ", oldResource.getName(),
775 csarInfo.getCsarUUID());
776 extractedVfcToscaNames.forEach(
777 (namespace, vfcToscaNames) -> findAddNodeTypeArtifactsToHandle(csarInfo, nodeTypesArtifactsToHandle, oldResource,
778 extractedVfcsArtifacts, namespace, vfcToscaNames));
779 } catch (Exception e) {
780 final ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR);
781 nodeTypesArtifactsToHandleRes = Either.right(responseFormat);
782 log.debug("Exception occurred when findNodeTypesUpdatedArtifacts, error is:{}", e.getMessage(), e);
784 return nodeTypesArtifactsToHandleRes;
787 private void findAddNodeTypeArtifactsToHandle(CsarInfo csarInfo,
788 Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle,
789 Resource resource, Map<String, List<ArtifactDefinition>> extractedVfcsArtifacts, String namespace,
790 ImmutablePair<String, String> vfcToscaNames) {
791 EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>> curNodeTypeArtifactsToHandle = null;
792 log.debug("Going to fetch node type with tosca name {}. ", vfcToscaNames.getLeft());
793 Resource curNodeType = findVfcResource(csarInfo, resource, vfcToscaNames.getLeft(), vfcToscaNames.getRight(), null);
794 if (!isEmpty(extractedVfcsArtifacts)) {
795 List<ArtifactDefinition> currArtifacts = new ArrayList<>();
796 if (extractedVfcsArtifacts.containsKey(namespace)) {
797 handleAndAddExtractedVfcsArtifacts(currArtifacts, extractedVfcsArtifacts.get(namespace));
799 curNodeTypeArtifactsToHandle = findNodeTypeArtifactsToHandle(curNodeType, currArtifacts);
800 } else if (curNodeType != null) {
801 // delete all artifacts if have not received artifacts from
804 curNodeTypeArtifactsToHandle = new EnumMap<>(ArtifactOperationEnum.class);
805 List<ArtifactDefinition> artifactsToDelete = new ArrayList<>();
806 // delete all informational artifacts
807 artifactsToDelete.addAll(
808 curNodeType.getArtifacts().values().stream().filter(a -> a.getArtifactGroupType() == ArtifactGroupTypeEnum.INFORMATIONAL)
810 // delete all deployment artifacts
811 artifactsToDelete.addAll(curNodeType.getDeploymentArtifacts().values());
812 if (!artifactsToDelete.isEmpty()) {
813 curNodeTypeArtifactsToHandle.put(ArtifactOperationEnum.DELETE, artifactsToDelete);
816 if (isNotEmpty(curNodeTypeArtifactsToHandle)) {
817 nodeTypesArtifactsToHandle.put(namespace, curNodeTypeArtifactsToHandle);
821 private Resource findVfcResource(CsarInfo csarInfo, Resource resource, String currVfcToscaName, String previousVfcToscaName,
822 StorageOperationStatus status) {
823 if (status != null && status != StorageOperationStatus.NOT_FOUND) {
824 log.debug("Error occurred during fetching node type with tosca name {}, error: {}", currVfcToscaName, status);
825 ResponseFormat responseFormat = componentsUtils
826 .getResponseFormat(componentsUtils.convertFromStorageResponse(status), csarInfo.getCsarUUID());
827 componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), resource, AuditingActionEnum.CREATE_RESOURCE);
828 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(status), csarInfo.getCsarUUID());
829 } else if (StringUtils.isNotEmpty(currVfcToscaName)) {
830 return (Resource) toscaOperationFacade.getLatestByToscaResourceName(currVfcToscaName, resource.getModel()).left()
831 .on(st -> findVfcResource(csarInfo, resource, previousVfcToscaName, null, st));
836 private EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>> findNodeTypeArtifactsToHandle(Resource curNodeType,
837 List<ArtifactDefinition> extractedArtifacts) {
839 List<ArtifactDefinition> artifactsToUpload = new ArrayList<>(extractedArtifacts);
840 List<ArtifactDefinition> artifactsToUpdate = new ArrayList<>();
841 List<ArtifactDefinition> artifactsToDelete = new ArrayList<>();
842 processExistingNodeTypeArtifacts(extractedArtifacts, artifactsToUpload, artifactsToUpdate, artifactsToDelete,
843 collectExistingArtifacts(curNodeType));
844 return putFoundArtifacts(artifactsToUpload, artifactsToUpdate, artifactsToDelete);
845 } catch (Exception e) {
846 log.debug("Exception occurred when findNodeTypeArtifactsToHandle, error is:{}", e.getMessage(), e);
847 throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR);
851 private EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>> putFoundArtifacts(List<ArtifactDefinition> artifactsToUpload,
852 List<ArtifactDefinition> artifactsToUpdate,
853 List<ArtifactDefinition> artifactsToDelete) {
854 EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>> nodeTypeArtifactsToHandle = null;
855 if (!artifactsToUpload.isEmpty() || !artifactsToUpdate.isEmpty() || !artifactsToDelete.isEmpty()) {
856 nodeTypeArtifactsToHandle = new EnumMap<>(ArtifactOperationEnum.class);
857 if (!artifactsToUpload.isEmpty()) {
858 nodeTypeArtifactsToHandle.put(ArtifactOperationEnum.CREATE, artifactsToUpload);
860 if (!artifactsToUpdate.isEmpty()) {
861 nodeTypeArtifactsToHandle.put(ArtifactOperationEnum.UPDATE, artifactsToUpdate);
863 if (!artifactsToDelete.isEmpty()) {
864 nodeTypeArtifactsToHandle.put(ArtifactOperationEnum.DELETE, artifactsToDelete);
867 return nodeTypeArtifactsToHandle;
870 private void processExistingNodeTypeArtifacts(List<ArtifactDefinition> extractedArtifacts, List<ArtifactDefinition> artifactsToUpload,
871 List<ArtifactDefinition> artifactsToUpdate, List<ArtifactDefinition> artifactsToDelete,
872 Map<String, ArtifactDefinition> existingArtifacts) {
873 if (!existingArtifacts.isEmpty()) {
874 extractedArtifacts.forEach(a -> processNodeTypeArtifact(artifactsToUpload, artifactsToUpdate, existingArtifacts, a));
875 artifactsToDelete.addAll(existingArtifacts.values());
879 private void processNodeTypeArtifact(List<ArtifactDefinition> artifactsToUpload, List<ArtifactDefinition> artifactsToUpdate,
880 Map<String, ArtifactDefinition> existingArtifacts, ArtifactDefinition currNewArtifact) {
881 Optional<ArtifactDefinition> foundArtifact = existingArtifacts.values().stream()
882 .filter(a -> a.getArtifactName().equals(currNewArtifact.getArtifactName())).findFirst();
883 if (foundArtifact.isPresent()) {
884 if (foundArtifact.get().getArtifactType().equals(currNewArtifact.getArtifactType())) {
885 updateFoundArtifact(artifactsToUpdate, currNewArtifact, foundArtifact.get());
886 existingArtifacts.remove(foundArtifact.get().getArtifactLabel());
887 artifactsToUpload.remove(currNewArtifact);
889 log.debug("Can't upload two artifact with the same name {}.", currNewArtifact.getArtifactName());
890 throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_ALREADY_EXIST_IN_DIFFERENT_TYPE_IN_CSAR,
891 currNewArtifact.getArtifactName(), currNewArtifact.getArtifactType(), foundArtifact.get().getArtifactType());
896 private void updateFoundArtifact(List<ArtifactDefinition> artifactsToUpdate, ArtifactDefinition currNewArtifact,
897 ArtifactDefinition foundArtifact) {
898 if (!foundArtifact.getArtifactChecksum().equals(currNewArtifact.getArtifactChecksum())) {
899 foundArtifact.setPayload(currNewArtifact.getPayloadData());
900 foundArtifact.setPayloadData(Base64.encodeBase64String(currNewArtifact.getPayloadData()));
901 foundArtifact.setArtifactChecksum(GeneralUtility.calculateMD5Base64EncodedByByteArray(currNewArtifact.getPayloadData()));
902 artifactsToUpdate.add(foundArtifact);
906 private Map<String, ArtifactDefinition> collectExistingArtifacts(Resource curNodeType) {
907 Map<String, ArtifactDefinition> existingArtifacts = new HashMap<>();
908 if (curNodeType == null) {
909 return existingArtifacts;
911 if (MapUtils.isNotEmpty(curNodeType.getDeploymentArtifacts())) {
912 existingArtifacts.putAll(curNodeType.getDeploymentArtifacts());
914 if (MapUtils.isNotEmpty(curNodeType.getArtifacts())) {
915 existingArtifacts.putAll(
916 curNodeType.getArtifacts().entrySet().stream().filter(e -> e.getValue().getArtifactGroupType() == ArtifactGroupTypeEnum.INFORMATIONAL)
917 .collect(toMap(Map.Entry::getKey, Map.Entry::getValue)));
919 return existingArtifacts;
923 * Changes resource life cycle state to checked out
927 * @param inTransaction
930 private Either<Resource, ResponseFormat> checkoutResource(Resource resource, User user, boolean inTransaction) {
931 Either<Resource, ResponseFormat> checkoutResourceRes;
933 if (!resource.getComponentMetadataDefinition().getMetadataDataDefinition().getState()
934 .equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name())) {
935 log.debug("************* Going to change life cycle state of resource {} to not certified checked out. ", resource.getName());
936 Either<? extends Component, ResponseFormat> checkoutRes = lifecycleBusinessLogic
937 .changeComponentState(resource.getComponentType(), resource.getUniqueId(), user, LifeCycleTransitionEnum.CHECKOUT,
938 new LifecycleChangeInfoWithAction(CERTIFICATION_ON_IMPORT, LifecycleChanceActionEnum.CREATE_FROM_CSAR), inTransaction, true);
939 if (checkoutRes.isRight()) {
940 log.debug("Could not change state of component {} with uid {} to checked out. Status is {}. ",
941 resource.getComponentType().getNodeType(), resource.getUniqueId(), checkoutRes.right().value().getStatus());
942 checkoutResourceRes = Either.right(checkoutRes.right().value());
944 checkoutResourceRes = Either.left((Resource) checkoutRes.left().value());
947 checkoutResourceRes = Either.left(resource);
949 } catch (Exception e) {
950 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR);
951 checkoutResourceRes = Either.right(responseFormat);
952 log.debug("Exception occurred when checkoutResource {} , error is:{}", resource.getName(), e.getMessage(), e);
954 return checkoutResourceRes;
958 * Handles Artifacts of NodeType
960 * @param nodeTypeResource
961 * @param nodeTypeArtifactsToHandle
963 * @param inTransaction
966 public Either<List<ArtifactDefinition>, ResponseFormat> handleNodeTypeArtifacts(Resource nodeTypeResource,
967 Map<ArtifactOperationEnum, List<ArtifactDefinition>> nodeTypeArtifactsToHandle,
968 List<ArtifactDefinition> createdArtifacts, User user,
969 boolean inTransaction, boolean ignoreLifecycleState) {
970 List<ArtifactDefinition> handleNodeTypeArtifactsRequestRes;
971 Either<List<ArtifactDefinition>, ResponseFormat> handleNodeTypeArtifactsRes = null;
972 Either<Resource, ResponseFormat> changeStateResponse;
974 changeStateResponse = checkoutResource(nodeTypeResource, user, inTransaction);
975 if (changeStateResponse.isRight()) {
976 return Either.right(changeStateResponse.right().value());
978 nodeTypeResource = changeStateResponse.left().value();
979 List<ArtifactDefinition> handledNodeTypeArtifacts = new ArrayList<>();
980 log.debug("************* Going to handle artifacts of node type resource {}. ", nodeTypeResource.getName());
981 for (Entry<ArtifactOperationEnum, List<ArtifactDefinition>> curOperationEntry : nodeTypeArtifactsToHandle.entrySet()) {
982 ArtifactOperationEnum curOperation = curOperationEntry.getKey();
983 List<ArtifactDefinition> curArtifactsToHandle = curOperationEntry.getValue();
984 if (curArtifactsToHandle != null && !curArtifactsToHandle.isEmpty()) {
985 log.debug("************* Going to {} artifact to vfc {}", curOperation.name(), nodeTypeResource.getName());
986 handleNodeTypeArtifactsRequestRes = artifactsBusinessLogic
987 .handleArtifactsRequestForInnerVfcComponent(curArtifactsToHandle, nodeTypeResource, user, createdArtifacts,
988 new ArtifactOperationInfo(false, ignoreLifecycleState, curOperation), false, inTransaction);
989 if (ArtifactOperationEnum.isCreateOrLink(curOperation)) {
990 createdArtifacts.addAll(handleNodeTypeArtifactsRequestRes);
992 handledNodeTypeArtifacts.addAll(handleNodeTypeArtifactsRequestRes);
995 handleNodeTypeArtifactsRes = Either.left(handledNodeTypeArtifacts);
996 } catch (Exception e) {
997 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR);
998 handleNodeTypeArtifactsRes = Either.right(responseFormat);
999 log.debug("Exception occurred when handleVfcArtifacts, error is:{}", e.getMessage(), e);
1001 return handleNodeTypeArtifactsRes;
1004 private Map<String, ImmutablePair<String, String>> extractVfcToscaNames(final Map<String, NodeTypeInfo> nodeTypesInfo,
1005 final String vfResourceName, final CsarInfo csarInfo) {
1006 final Map<String, ImmutablePair<String, String>> vfcToscaNames = new HashMap<>();
1007 final Map<String, Object> nodes = extractAllNodes(nodeTypesInfo, csarInfo);
1008 if (!nodes.isEmpty()) {
1009 for (Entry<String, Object> nodeType : nodes.entrySet()) {
1010 final ImmutablePair<String, String> toscaResourceName = buildNestedToscaResourceName(ResourceTypeEnum.VFC.name(), vfResourceName,
1012 vfcToscaNames.put(nodeType.getKey(), toscaResourceName);
1015 for (final NodeTypeInfo cvfc : nodeTypesInfo.values()) {
1016 vfcToscaNames.put(cvfc.getType(), buildNestedToscaResourceName(ResourceTypeEnum.CVFC.name(), vfResourceName, cvfc.getType()));
1018 return vfcToscaNames;
1021 private Map<String, Object> extractAllNodes(Map<String, NodeTypeInfo> nodeTypesInfo, CsarInfo csarInfo) {
1022 Map<String, Object> nodes = new HashMap<>();
1023 for (NodeTypeInfo nodeTypeInfo : nodeTypesInfo.values()) {
1024 extractNodeTypes(nodes, nodeTypeInfo.getMappedToscaTemplate());
1026 extractNodeTypes(nodes, csarInfo.getMappedToscaMainTemplate());
1030 private void extractNodeTypes(Map<String, Object> nodes, Map<String, Object> mappedToscaTemplate) {
1031 Either<Map<String, Object>, ResultStatusEnum> eitherNodeTypes = ImportUtils
1032 .findFirstToscaMapElement(mappedToscaTemplate, TypeUtils.ToscaTagNamesEnum.NODE_TYPES);
1033 if (eitherNodeTypes.isLeft()) {
1034 nodes.putAll(eitherNodeTypes.left().value());
1038 public Resource createResourceFromCsar(Resource resource, User user, Map<String, byte[]> csarUIPayload, String csarUUID) {
1039 log.trace("************* created successfully from YAML, resource TOSCA ");
1040 loggerSupportability
1041 .log(LoggerSupportabilityActions.CREATE_RESOURCE_FROM_YAML, StatusCode.STARTED, "Starting to create Resource From Csar by user {}",
1043 CsarInfo csarInfo = csarBusinessLogic.getCsarInfo(resource, null, user, csarUIPayload, csarUUID);
1044 Map<String, NodeTypeInfo> nodeTypesInfo = csarInfo.extractTypesInfo();
1045 final String model = resource.getModel();
1046 if (StringUtils.isNotEmpty(model)) {
1047 final Map<String, Object> dataTypesToCreate = getDatatypesToCreate(model, csarInfo.getDataTypes());
1048 final Map<String, Object> policyTypesToCreate = getPolicytypesToCreate(model, csarInfo.getPolicyTypes());
1049 if (MapUtils.isNotEmpty(dataTypesToCreate) || MapUtils.isNotEmpty(policyTypesToCreate)) {
1050 createModel(resource, csarInfo.getVfResourceName());
1052 if (MapUtils.isNotEmpty(dataTypesToCreate)) {
1053 dataTypeBusinessLogic.createDataTypeFromYaml(new Yaml().dump(dataTypesToCreate), model, true);
1055 if (MapUtils.isNotEmpty(policyTypesToCreate)) {
1056 policyTypeBusinessLogic.createPolicyTypeFromYaml(new Yaml().dump(policyTypesToCreate), model, true);
1060 Either<Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>>, ResponseFormat> findNodeTypesArtifactsToHandleRes = findNodeTypesArtifactsToHandle(
1061 nodeTypesInfo, csarInfo, resource);
1062 if (findNodeTypesArtifactsToHandleRes.isRight()) {
1063 log.debug("failed to find node types for update with artifacts during import csar {}. ", csarInfo.getCsarUUID());
1064 loggerSupportability
1065 .log(LoggerSupportabilityActions.CREATE_RESOURCE_FROM_YAML, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
1066 "error: {}", findNodeTypesArtifactsToHandleRes.right().value());
1067 throw new ByResponseFormatComponentException(findNodeTypesArtifactsToHandleRes.right().value());
1069 Resource vfResource = createResourceFromYaml(resource, csarInfo.getMainTemplateContent(), csarInfo.getMainTemplateName(), nodeTypesInfo,
1070 csarInfo, findNodeTypesArtifactsToHandleRes.left().value(), true, false, null);
1071 log.trace("*************VF Resource created successfully from YAML, resource TOSCA name: {}", vfResource.getToscaResourceName());
1072 loggerSupportability
1073 .log(LoggerSupportabilityActions.CREATE_RESOURCE_FROM_YAML, StatusCode.COMPLETE, "Ended create Resource From Csar by user {}",
1078 private Resource validateResourceBeforeCreate(Resource resource, User user, boolean inTransaction) {
1079 log.trace("validating resource before create");
1080 user.copyData(validateUser(user, CREATE_RESOURCE, resource, AuditingActionEnum.CREATE_RESOURCE, false));
1081 // validate user role
1082 validateUserRole(user, resource, new ArrayList<>(), AuditingActionEnum.CREATE_RESOURCE, null);
1083 // VF / PNF "derivedFrom" should be null (or ignored)
1084 if (ModelConverter.isAtomicComponent(resource)) {
1085 validateDerivedFromNotEmpty(user, resource, AuditingActionEnum.CREATE_RESOURCE);
1087 return validateResourceBeforeCreate(resource, user, AuditingActionEnum.CREATE_RESOURCE, inTransaction, null);
1090 private Resource createResourceFromYaml(Resource resource, String topologyTemplateYaml, String yamlName, Map<String, NodeTypeInfo> nodeTypesInfo,
1092 Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToCreate,
1093 boolean shouldLock, boolean inTransaction, String nodeName) {
1094 List<ArtifactDefinition> createdArtifacts = new ArrayList<>();
1095 Resource createdResource;
1097 ParsedToscaYamlInfo parsedToscaYamlInfo = csarBusinessLogic
1098 .getParsedToscaYamlInfo(topologyTemplateYaml, yamlName, nodeTypesInfo, csarInfo, nodeName, resource);
1099 if (MapUtils.isEmpty(parsedToscaYamlInfo.getInstances()) && resource.getResourceType() != ResourceTypeEnum.PNF) {
1100 throw new ByActionStatusComponentException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName);
1102 log.debug("#createResourceFromYaml - Going to create resource {} and RIs ", resource.getName());
1103 loggerSupportability
1104 .log(LoggerSupportabilityActions.CREATE_RESOURCE_FROM_YAML, resource.getComponentMetadataForSupportLog(), StatusCode.STARTED, "");
1105 createdResource = createResourceAndRIsFromYaml(yamlName, resource, parsedToscaYamlInfo, AuditingActionEnum.IMPORT_RESOURCE, false,
1106 createdArtifacts, topologyTemplateYaml, nodeTypesInfo, csarInfo, nodeTypesArtifactsToCreate, shouldLock, inTransaction, nodeName);
1107 log.debug("#createResourceFromYaml - The resource {} has been created ", resource.getName());
1108 loggerSupportability
1109 .log(LoggerSupportabilityActions.CREATE_RESOURCE_FROM_YAML, resource.getComponentMetadataForSupportLog(), StatusCode.COMPLETE,
1110 "The resource has been created: {}", resource.getName());
1111 } catch (ComponentException e) {
1112 ResponseFormat responseFormat =
1113 e.getResponseFormat() == null ? componentsUtils.getResponseFormat(e.getActionStatus(), e.getParams()) : e.getResponseFormat();
1114 componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), resource, AuditingActionEnum.IMPORT_RESOURCE);
1116 } catch (StorageException e) {
1117 ResponseFormat responseFormat = componentsUtils
1118 .getResponseFormat(componentsUtils.convertFromStorageResponse(e.getStorageOperationStatus()));
1119 componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), resource, AuditingActionEnum.IMPORT_RESOURCE);
1122 return createdResource;
1125 public Map<String, Resource> createResourcesFromYamlNodeTypesList(String yamlName, Resource resource, Map<String, Object> mappedToscaTemplate,
1127 Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle,
1128 List<ArtifactDefinition> nodeTypesNewCreatedArtifacts,
1129 Map<String, NodeTypeInfo> nodeTypesInfo, CsarInfo csarInfo,
1130 final String substitutableAsNodeType) {
1131 Either<String, ResultStatusEnum> toscaVersion = findFirstToscaStringElement(mappedToscaTemplate, TypeUtils.ToscaTagNamesEnum.TOSCA_VERSION);
1132 if (toscaVersion.isRight()) {
1133 throw new ByActionStatusComponentException(ActionStatus.INVALID_TOSCA_TEMPLATE);
1135 Map<String, Object> mapToConvert = new HashMap<>();
1136 mapToConvert.put(TypeUtils.ToscaTagNamesEnum.TOSCA_VERSION.getElementName(), toscaVersion.left().value());
1137 final Map<String, Object> nodeTypes = getNodeTypesFromTemplate(mappedToscaTemplate, substitutableAsNodeType);
1138 createNodeTypes(yamlName, resource, needLock, nodeTypesArtifactsToHandle, nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo, mapToConvert,
1140 return csarInfo.getCreatedNodes();
1143 private Map<String, Object> getNodeTypesFromTemplate(final Map<String, Object> mappedToscaTemplate, final String substitutableAsNodeType) {
1144 final Map<String, Object> nodeTypes = getAllNodeTypesInTemplate(mappedToscaTemplate);
1145 if (StringUtils.isNotEmpty(substitutableAsNodeType)) {
1146 nodeTypes.remove(substitutableAsNodeType);
1151 @SuppressWarnings("unchecked")
1152 private Map<String, Object> getSubstitutableAsNodeTypeFromTemplate(final Map<String, Object> mappedToscaTemplate,
1153 final String substitutableAsNodeType) {
1154 return (Map<String, Object>) getAllNodeTypesInTemplate(mappedToscaTemplate).get(substitutableAsNodeType);
1157 private Map<String, Object> getAllNodeTypesInTemplate(final Map<String, Object> mappedToscaTemplate) {
1158 return ImportUtils.findFirstToscaMapElement(mappedToscaTemplate, TypeUtils.ToscaTagNamesEnum.NODE_TYPES).left().orValue(HashMap::new);
1161 private void createModel(final Resource resource, final String vfResourcename) {
1162 final String nameForGeneratedModel = resource.getModel() + "_" + vfResourcename + resource.getCsarVersion();
1163 Model model = new Model(nameForGeneratedModel, resource.getModel(), ModelTypeEnum.NORMATIVE_EXTENSION);
1164 modelBusinessLogic.createModel(model);
1165 resource.setModel(nameForGeneratedModel);
1168 private Map<String, Object> getDatatypesToCreate(final String model, final Map<String, Object> dataTypes) {
1169 final Map<String, Object> dataTypesToCreate = new HashMap<>();
1170 for (final String dataType : dataTypes.keySet()) {
1171 final Either<DataTypeDefinition, StorageOperationStatus> result =
1172 propertyOperation.getDataTypeByName(dataType, model);
1173 if (result.isRight() && result.right().value().equals(StorageOperationStatus.NOT_FOUND)) {
1174 dataTypesToCreate.put(dataType, dataTypes.get(dataType));
1177 return dataTypesToCreate;
1180 private Map<String, Object> getPolicytypesToCreate(final String model, final Map<String, Object> policyTypes) {
1181 final Map<String, Object> policyTypesToCreate = new HashMap<>();
1182 for (final String policyType : policyTypes.keySet()) {
1183 final Either<PolicyTypeDefinition, StorageOperationStatus> result =
1184 policyTypeOperation.getLatestPolicyTypeByType(policyType, model);
1185 if (result.isRight() && result.right().value().equals(StorageOperationStatus.NOT_FOUND)) {
1186 policyTypesToCreate.put(policyType, policyTypes.get(policyType));
1189 return policyTypesToCreate;
1192 private void createNodeTypes(String yamlName, Resource resource, boolean needLock,
1193 Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle,
1194 List<ArtifactDefinition> nodeTypesNewCreatedArtifacts, Map<String, NodeTypeInfo> nodeTypesInfo, CsarInfo csarInfo,
1195 Map<String, Object> mapToConvert, Map<String, Object> nodeTypes) {
1196 Iterator<Entry<String, Object>> nodesNameValueIter = nodeTypes.entrySet().iterator();
1197 Resource vfcCreated = null;
1198 while (nodesNameValueIter.hasNext()) {
1199 Entry<String, Object> nodeType = nodesNameValueIter.next();
1200 Map<ArtifactOperationEnum, List<ArtifactDefinition>> nodeTypeArtifactsToHandle =
1201 nodeTypesArtifactsToHandle == null || nodeTypesArtifactsToHandle.isEmpty() ? null : nodeTypesArtifactsToHandle.get(nodeType.getKey());
1202 if (nodeTypesInfo.containsKey(nodeType.getKey())) {
1203 log.trace("************* Going to handle nested vfc {}", nodeType.getKey());
1204 vfcCreated = handleNestedVfc(resource, nodeTypesArtifactsToHandle, nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo,
1206 log.trace("************* Finished to handle nested vfc {}", nodeType.getKey());
1207 } else if (csarInfo.getCreatedNodesToscaResourceNames() != null && !csarInfo.getCreatedNodesToscaResourceNames()
1208 .containsKey(nodeType.getKey())) {
1209 log.trace("************* Going to create node {}", nodeType.getKey());
1210 ImmutablePair<Resource, ActionStatus> resourceCreated = createNodeTypeResourceFromYaml(yamlName, nodeType, csarInfo.getModifier(),
1211 mapToConvert, resource, needLock, nodeTypeArtifactsToHandle, nodeTypesNewCreatedArtifacts, true, csarInfo, true);
1212 log.debug("************* Finished to create node {}", nodeType.getKey());
1213 vfcCreated = resourceCreated.getLeft();
1214 csarInfo.getCreatedNodesToscaResourceNames().put(nodeType.getKey(), vfcCreated.getToscaResourceName());
1216 if (vfcCreated != null) {
1217 csarInfo.getCreatedNodes().put(nodeType.getKey(), vfcCreated);
1219 mapToConvert.remove(TypeUtils.ToscaTagNamesEnum.NODE_TYPES.getElementName());
1223 private Resource handleNestedVfc(Resource resource, Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>> nodesArtifactsToHandle,
1224 List<ArtifactDefinition> createdArtifacts, Map<String, NodeTypeInfo> nodesInfo, CsarInfo csarInfo,
1226 String yamlName = nodesInfo.get(nodeName).getTemplateFileName();
1227 Map<String, Object> nestedVfcJsonMap = nodesInfo.get(nodeName).getMappedToscaTemplate();
1228 log.debug("************* Going to create node types from yaml {}", yamlName);
1229 createResourcesFromYamlNodeTypesList(yamlName, resource, nestedVfcJsonMap, false, nodesArtifactsToHandle, createdArtifacts,
1230 Collections.emptyMap(), csarInfo, resource.getModel());
1231 log.debug("************* Finished to create node types from yaml {}", yamlName);
1232 if (nestedVfcJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.TOPOLOGY_TEMPLATE.getElementName())) {
1233 log.debug("************* Going to handle complex VFC from yaml {}", yamlName);
1234 resource = handleComplexVfc(resource, nodesArtifactsToHandle, createdArtifacts, nodesInfo, csarInfo, nodeName, yamlName);
1239 private Resource handleComplexVfc(final Resource resource,
1240 final Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>> nodesArtifactsToHandle,
1241 final List<ArtifactDefinition> createdArtifacts, Map<String, NodeTypeInfo> nodesInfo, CsarInfo csarInfo,
1242 final String nodeName, final String yamlName) {
1243 Resource oldComplexVfc = null;
1244 Resource newComplexVfc = buildValidComplexVfc(resource, csarInfo, nodeName, nodesInfo);
1245 Either<Resource, StorageOperationStatus> oldComplexVfcRes = toscaOperationFacade
1246 .getFullLatestComponentByToscaResourceName(newComplexVfc.getToscaResourceName());
1247 if (oldComplexVfcRes.isRight() && oldComplexVfcRes.right().value() == StorageOperationStatus.NOT_FOUND) {
1248 oldComplexVfcRes = toscaOperationFacade.getFullLatestComponentByToscaResourceName(
1249 buildNestedToscaResourceName(ResourceTypeEnum.CVFC.name(), csarInfo.getVfResourceName(), nodeName).getRight());
1251 if (oldComplexVfcRes.isRight() && oldComplexVfcRes.right().value() != StorageOperationStatus.NOT_FOUND) {
1252 log.debug("Failed to fetch previous complex VFC by tosca resource name {}. Status is {}. ", newComplexVfc.getToscaResourceName(),
1253 oldComplexVfcRes.right().value());
1254 throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR);
1255 } else if (oldComplexVfcRes.isLeft()) {
1256 log.debug(VALIDATE_DERIVED_BEFORE_UPDATE);
1257 final Either<Boolean, ResponseFormat> eitherValidation = validateNestedDerivedFromDuringUpdate(oldComplexVfcRes.left().value(),
1258 newComplexVfc, ValidationUtils.hasBeenCertified(oldComplexVfcRes.left().value().getVersion()));
1259 if (eitherValidation.isLeft()) {
1260 oldComplexVfc = oldComplexVfcRes.left().value();
1263 newComplexVfc = handleComplexVfc(nodesArtifactsToHandle, createdArtifacts, nodesInfo, csarInfo, nodeName, yamlName, oldComplexVfc,
1265 csarInfo.getCreatedNodesToscaResourceNames().put(nodeName, newComplexVfc.getToscaResourceName());
1266 final LifecycleChangeInfoWithAction lifecycleChangeInfo = new LifecycleChangeInfoWithAction(CERTIFICATION_ON_IMPORT,
1267 LifecycleChanceActionEnum.CREATE_FROM_CSAR);
1268 log.debug("Going to certify cvfc {}. ", newComplexVfc.getName());
1269 final Resource result = propagateStateToCertified(csarInfo.getModifier(), newComplexVfc, lifecycleChangeInfo, true, false, true);
1270 csarInfo.getCreatedNodes().put(nodeName, result);
1271 csarInfo.removeNodeFromQueue();
1275 private Resource handleComplexVfc(Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>> nodesArtifactsToHandle,
1276 List<ArtifactDefinition> createdArtifacts, Map<String, NodeTypeInfo> nodesInfo, CsarInfo csarInfo,
1277 String nodeName, String yamlName, Resource oldComplexVfc, Resource newComplexVfc) {
1278 Resource handleComplexVfcRes;
1279 Map<String, Object> mappedToscaTemplate = nodesInfo.get(nodeName).getMappedToscaTemplate();
1280 String yamlContent = new String(csarInfo.getCsar().get(yamlName));
1281 Map<String, NodeTypeInfo> newNodeTypesInfo = nodesInfo.entrySet().stream().collect(toMap(Entry::getKey, e -> e.getValue().getUnmarkedCopy()));
1282 CsarInfo.markNestedVfc(mappedToscaTemplate, newNodeTypesInfo);
1283 if (oldComplexVfc == null) {
1284 handleComplexVfcRes = createResourceFromYaml(newComplexVfc, yamlContent, yamlName, newNodeTypesInfo, csarInfo, nodesArtifactsToHandle,
1285 false, true, nodeName);
1287 handleComplexVfcRes = updateResourceFromYaml(oldComplexVfc, newComplexVfc, AuditingActionEnum.UPDATE_RESOURCE_METADATA, createdArtifacts,
1288 yamlContent, yamlName, csarInfo, newNodeTypesInfo, nodesArtifactsToHandle, nodeName, true);
1290 return handleComplexVfcRes;
1293 private Resource buildValidComplexVfc(Resource resource, CsarInfo csarInfo, String nodeName, Map<String, NodeTypeInfo> nodesInfo) {
1294 Resource complexVfc = buildComplexVfcMetadata(resource, csarInfo, nodeName, nodesInfo);
1295 log.debug("************* Going to validate complex VFC from yaml {}", complexVfc.getName());
1296 csarInfo.addNodeToQueue(nodeName);
1297 return validateResourceBeforeCreate(complexVfc, csarInfo.getModifier(), AuditingActionEnum.IMPORT_RESOURCE, true, csarInfo);
1300 private String getNodeTypeActualName(final String nodeTypefullName, final String nodeTypeNamePrefix) {
1301 final String nameWithouNamespacePrefix = nodeTypefullName.substring(nodeTypeNamePrefix.length());
1302 final String[] findTypes = nameWithouNamespacePrefix.split("\\.");
1303 if (findTypes.length > 1) {
1304 final String resourceType = findTypes[0];
1305 return nameWithouNamespacePrefix.substring(resourceType.length());
1307 return nameWithouNamespacePrefix;
1310 private ImmutablePair<Resource, ActionStatus> createNodeTypeResourceFromYaml(final String yamlName, final Entry<String, Object> nodeNameValue,
1311 User user, final Map<String, Object> mapToConvert,
1312 final Resource resourceVf, final boolean needLock,
1313 final Map<ArtifactOperationEnum, List<ArtifactDefinition>> nodeTypeArtifactsToHandle,
1314 final List<ArtifactDefinition> nodeTypesNewCreatedArtifacts,
1315 final boolean forceCertificationAllowed, final CsarInfo csarInfo,
1316 final boolean isNested) {
1317 final UploadResourceInfo resourceMetaData = fillResourceMetadata(yamlName, resourceVf, nodeNameValue.getKey(), user);
1318 final String singleVfcYaml = buildNodeTypeYaml(nodeNameValue, mapToConvert, resourceMetaData.getResourceType(), csarInfo);
1319 user = validateUser(user, "CheckIn Resource", resourceVf, AuditingActionEnum.CHECKIN_RESOURCE, true);
1320 return createResourceFromNodeType(singleVfcYaml, resourceMetaData, user, true, needLock, nodeTypeArtifactsToHandle,
1321 nodeTypesNewCreatedArtifacts, forceCertificationAllowed, csarInfo, nodeNameValue.getKey(), isNested);
1324 private String buildNodeTypeYaml(final Entry<String, Object> nodeNameValue, final Map<String, Object> mapToConvert, final String nodeResourceType,
1325 final CsarInfo csarInfo) {
1326 // We need to create a Yaml from each node_types in order to create
1328 // resource from each node type using import normative flow.
1329 final DumperOptions options = new DumperOptions();
1330 options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
1331 final Yaml yaml = new Yaml(options);
1332 final Map<String, Object> node = new HashMap<>();
1333 node.put(buildNestedToscaResourceName(nodeResourceType, csarInfo.getVfResourceName(), nodeNameValue.getKey()).getLeft(),
1334 nodeNameValue.getValue());
1335 mapToConvert.put(TypeUtils.ToscaTagNamesEnum.NODE_TYPES.getElementName(), node);
1336 return yaml.dumpAsMap(mapToConvert);
1339 public Boolean validateResourceCreationFromNodeType(Resource resource, User creator) {
1340 validateDerivedFromNotEmpty(creator, resource, AuditingActionEnum.CREATE_RESOURCE);
1344 public ImmutablePair<Resource, ActionStatus> createResourceFromNodeType(String nodeTypeYaml, UploadResourceInfo resourceMetaData, User creator,
1345 boolean isInTransaction, boolean needLock,
1346 Map<ArtifactOperationEnum, List<ArtifactDefinition>> nodeTypeArtifactsToHandle,
1347 List<ArtifactDefinition> nodeTypesNewCreatedArtifacts,
1348 boolean forceCertificationAllowed, CsarInfo csarInfo, String nodeName,
1350 LifecycleChangeInfoWithAction lifecycleChangeInfo = new LifecycleChangeInfoWithAction(CERTIFICATION_ON_IMPORT,
1351 LifecycleChanceActionEnum.CREATE_FROM_CSAR);
1352 Function<Resource, Boolean> validator = resource -> validateResourceCreationFromNodeType(resource, creator);
1353 return resourceImportManager
1354 .importCertifiedResource(nodeTypeYaml, resourceMetaData, creator, validator, lifecycleChangeInfo, isInTransaction, true, needLock,
1355 nodeTypeArtifactsToHandle, nodeTypesNewCreatedArtifacts, forceCertificationAllowed, csarInfo, nodeName, isNested);
1359 * Validates if a given node type name has a valid prefix.
1361 * @param nodeName node name from definition file
1362 * @param definedResourceNamespaceList is a list of all node type name prefix allowed
1363 * @return a valid node type name prefix if it`s found
1365 public Optional<String> validateNodeTypeNamePrefix(final String nodeName, final List<String> definedResourceNamespaceList) {
1366 for (final String validNamespace : definedResourceNamespaceList) {
1367 if (nodeName.startsWith(validNamespace)) {
1368 return Optional.of(validNamespace);
1371 return Optional.empty();
1374 private List<String> getDefinedNodeTypeNamespaceList() {
1375 return ConfigurationManager.getConfigurationManager().getConfiguration().getDefinedResourceNamespace();
1378 private UploadResourceInfo fillResourceMetadata(final String yamlName, final Resource resourceVf, final String nodeName, final User user) {
1379 final UploadResourceInfo resourceMetaData = new UploadResourceInfo();
1380 final String nodeTypeNamePrefix = getNodeTypeNamePrefix(nodeName);
1381 log.debug("Node type Name prefix {}", nodeTypeNamePrefix);
1382 if (!nodeName.startsWith(nodeTypeNamePrefix)) {
1383 log.debug("invalid nodeName:{} does not start with {}.", nodeName, getDefinedNodeTypeNamespaceList());
1384 throw new ByActionStatusComponentException(ActionStatus.INVALID_NODE_TEMPLATE, yamlName, resourceMetaData.getName(), nodeName);
1386 final String actualName = this.getNodeTypeActualName(nodeName, nodeTypeNamePrefix);
1387 final String namePrefix = nodeName.replace(actualName, "");
1388 String resourceType = namePrefix.substring(nodeTypeNamePrefix.length());
1389 log.debug("initial namePrefix:{} resourceType {}. nodeName {} , actualName {} prefix {}", namePrefix, resourceType, nodeName, actualName,
1390 nodeTypeNamePrefix);
1391 // if we import from csar, the node_type name can be
1393 // org.openecomp.resource.abstract.node_name - in this case we always
1396 if (resourceType.equals(Constants.ABSTRACT)) {
1397 resourceType = ResourceTypeEnum.VFC.name().toLowerCase();
1399 if (!ResourceTypeEnum.containsIgnoreCase(resourceType)) {
1400 resourceType = ResourceTypeEnum.VFC.name().toLowerCase();
1403 if (!ResourceTypeEnum.containsName(resourceType.toUpperCase())) {
1404 log.debug("invalid resourceType:{} the type is not one of the valide types:{}.", resourceType.toUpperCase(), ResourceTypeEnum.values());
1405 throw new ByActionStatusComponentException(ActionStatus.INVALID_NODE_TEMPLATE, yamlName, resourceMetaData.getName(), nodeName);
1408 resourceMetaData.setName(resourceVf.getSystemName() + actualName);
1409 // Setting type from name
1410 final String type = resourceType.toUpperCase();
1411 resourceMetaData.setResourceType(type);
1412 resourceMetaData.setDescription(ImportUtils.Constants.INNER_VFC_DESCRIPTION);
1413 resourceMetaData.setIcon(ImportUtils.Constants.DEFAULT_ICON);
1414 resourceMetaData.setContactId(user.getUserId());
1415 resourceMetaData.setVendorName(resourceVf.getVendorName());
1416 resourceMetaData.setVendorRelease(resourceVf.getVendorRelease());
1417 resourceMetaData.setModel(resourceVf.getModel());
1419 final List<String> tags = new ArrayList<>();
1420 tags.add(resourceMetaData.getName());
1421 resourceMetaData.setTags(tags);
1423 final CategoryDefinition category = new CategoryDefinition();
1424 category.setName(ImportUtils.Constants.ABSTRACT_CATEGORY_NAME);
1425 final SubCategoryDefinition subCategory = new SubCategoryDefinition();
1426 subCategory.setName(ImportUtils.Constants.ABSTRACT_SUBCATEGORY);
1427 category.addSubCategory(subCategory);
1428 final List<CategoryDefinition> categories = new ArrayList<>();
1429 categories.add(category);
1430 resourceMetaData.setCategories(categories);
1431 return resourceMetaData;
1434 private Resource buildComplexVfcMetadata(final Resource resourceVf, final CsarInfo csarInfo, final String nodeName,
1435 final Map<String, NodeTypeInfo> nodesInfo) {
1436 final Resource cvfc = new Resource();
1437 final NodeTypeInfo nodeTypeInfo = nodesInfo.get(nodeName);
1438 cvfc.setName(buildCvfcName(csarInfo.getVfResourceName(), nodeName));
1439 cvfc.setNormalizedName(ValidationUtils.normaliseComponentName(cvfc.getName()));
1440 cvfc.setSystemName(ValidationUtils.convertToSystemName(cvfc.getName()));
1441 cvfc.setResourceType(ResourceTypeEnum.CVFC);
1442 cvfc.setAbstract(true);
1443 cvfc.setDerivedFrom(nodeTypeInfo.getDerivedFrom());
1444 cvfc.setDescription(ImportUtils.Constants.CVFC_DESCRIPTION);
1445 cvfc.setIcon(ImportUtils.Constants.DEFAULT_ICON);
1446 cvfc.setContactId(csarInfo.getModifier().getUserId());
1447 cvfc.setCreatorUserId(csarInfo.getModifier().getUserId());
1448 cvfc.setVendorName(resourceVf.getVendorName());
1449 cvfc.setVendorRelease(resourceVf.getVendorRelease());
1450 cvfc.setModel(resourceVf.getModel());
1451 cvfc.setResourceVendorModelNumber(resourceVf.getResourceVendorModelNumber());
1452 cvfc.setToscaResourceName(buildNestedToscaResourceName(ResourceTypeEnum.CVFC.name(), csarInfo.getVfResourceName(), nodeName).getLeft());
1453 cvfc.setInvariantUUID(UniqueIdBuilder.buildInvariantUUID());
1454 final List<String> tags = new ArrayList<>();
1455 tags.add(cvfc.getName());
1457 final CategoryDefinition category = new CategoryDefinition();
1458 category.setName(ImportUtils.Constants.ABSTRACT_CATEGORY_NAME);
1459 SubCategoryDefinition subCategory = new SubCategoryDefinition();
1460 subCategory.setName(ImportUtils.Constants.ABSTRACT_SUBCATEGORY);
1461 category.addSubCategory(subCategory);
1462 final List<CategoryDefinition> categories = new ArrayList<>();
1463 categories.add(category);
1464 cvfc.setCategories(categories);
1465 cvfc.setVersion(ImportUtils.Constants.FIRST_NON_CERTIFIED_VERSION);
1466 cvfc.setLifecycleState(ImportUtils.Constants.NORMATIVE_TYPE_LIFE_CYCLE_NOT_CERTIFIED_CHECKOUT);
1467 cvfc.setHighestVersion(ImportUtils.Constants.NORMATIVE_TYPE_HIGHEST_VERSION);
1471 private String buildCvfcName(final String resourceVfName, final String nodeName) {
1472 String nameWithouNamespacePrefix = nodeName.substring(Constants.USER_DEFINED_RESOURCE_NAMESPACE_PREFIX.length());
1473 String[] findTypes = nameWithouNamespacePrefix.split("\\.");
1474 String resourceType = findTypes[0];
1475 String resourceName = resourceVfName + "-" + nameWithouNamespacePrefix.substring(resourceType.length() + 1);
1476 return addCvfcSuffixToResourceName(resourceName);
1479 private Resource createResourceAndRIsFromYaml(final String yamlName, Resource resource, final ParsedToscaYamlInfo parsedToscaYamlInfo,
1480 final AuditingActionEnum actionEnum, final boolean isNormative,
1481 final List<ArtifactDefinition> createdArtifacts, final String topologyTemplateYaml,
1482 final Map<String, NodeTypeInfo> nodeTypesInfo, final CsarInfo csarInfo,
1483 final Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToCreate,
1484 final boolean shouldLock, final boolean inTransaction, final String nodeName) {
1485 final List<ArtifactDefinition> nodeTypesNewCreatedArtifacts = new ArrayList<>();
1487 final Either<Boolean, ResponseFormat> lockResult = lockComponentByName(resource.getSystemName(), resource, CREATE_RESOURCE);
1488 if (lockResult.isRight()) {
1489 rollback(inTransaction, resource, createdArtifacts, nodeTypesNewCreatedArtifacts);
1490 throw new ByResponseFormatComponentException(lockResult.right().value());
1492 log.debug("name is locked {} status = {}", resource.getSystemName(), lockResult);
1495 log.trace("************* createResourceFromYaml before full create resource {}", yamlName);
1496 loggerSupportability.log(LoggerSupportabilityActions.CREATE_INPUTS, resource.getComponentMetadataForSupportLog(), StatusCode.STARTED,
1497 "Starting to add inputs from yaml: {}", yamlName);
1498 if (processSubstitutableAsNodeType(resource, parsedToscaYamlInfo)) {
1499 final Map<String, Object> substitutableAsNodeType = getSubstitutableAsNodeTypeFromTemplate(
1500 (Map<String, Object>) new Yaml().load(topologyTemplateYaml), parsedToscaYamlInfo.getSubstitutionMappingNodeType());
1501 resource.setToscaResourceName(parsedToscaYamlInfo.getSubstitutionMappingNodeType());
1502 final Resource genericResource = fetchAndSetDerivedFromGenericType(resource,
1503 (String) substitutableAsNodeType.get(TypeUtils.ToscaTagNamesEnum.DERIVED_FROM.getElementName()));
1504 resource = createResourceTransaction(resource, csarInfo.getModifier(), isNormative);
1505 generatePropertiesFromGenericType(resource, genericResource);
1506 generatePropertiesFromNodeType(resource, substitutableAsNodeType);
1507 final String resourceId = resource.getUniqueId();
1508 resource.getProperties().forEach(propertyDefinition -> propertyDefinition.setUniqueId(
1509 UniqueIdBuilder.buildPropertyUniqueId(resourceId, propertyDefinition.getName())));
1511 createResourcePropertiesOnGraph(resource);
1512 final Map<String, UploadComponentInstanceInfo> instancesToCreate = getInstancesToCreate(parsedToscaYamlInfo, resource.getModel());
1514 log.trace("************* Going to create nodes, RI's and Relations from yaml {}", yamlName);
1515 loggerSupportability
1516 .log(LoggerSupportabilityActions.CREATE_RESOURCE_FROM_YAML, resource.getComponentMetadataForSupportLog(), StatusCode.STARTED,
1517 "Start create nodes, RI and Relations from yaml: {}", yamlName);
1518 resource = createRIAndRelationsFromYaml(yamlName, resource, instancesToCreate, topologyTemplateYaml,
1519 nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo, nodeTypesArtifactsToCreate, nodeName,
1520 parsedToscaYamlInfo.getSubstitutionMappingNodeType());
1522 final Resource genericResource = fetchAndSetDerivedFromGenericType(resource, null);
1523 resource = createResourceTransaction(resource, csarInfo.getModifier(), isNormative);
1524 log.trace("************* createResourceFromYaml after full create resource {}", yamlName);
1525 log.trace("************* Going to add inputs from yaml {}", yamlName);
1526 if (resource.shouldGenerateInputs()) {
1527 generateAndAddInputsFromGenericTypeProperties(resource, genericResource);
1529 final Map<String, InputDefinition> inputs = parsedToscaYamlInfo.getInputs();
1530 resource = createInputsOnResource(resource, inputs);
1532 log.trace("************* Finish to add inputs from yaml {}", yamlName);
1533 loggerSupportability.log(LoggerSupportabilityActions.CREATE_INPUTS, resource.getComponentMetadataForSupportLog(), StatusCode.COMPLETE,
1534 "Finish to add inputs from yaml: {}", yamlName);
1535 if (resource.getResourceType() == ResourceTypeEnum.PNF) {
1536 log.trace("************* Adding generic properties to PNF");
1537 resource = (Resource) propertyBusinessLogic.copyPropertyToComponent(resource, genericResource.getProperties());
1538 log.trace("************* Adding software information to PNF");
1539 softwareInformationBusinessLogic.setSoftwareInformation(resource, csarInfo);
1540 log.trace("************* Removing non-mano software information file from PNF");
1541 if (csarInfo.getSoftwareInformationPath().isPresent() && !softwareInformationBusinessLogic.removeSoftwareInformationFile(
1543 log.warn(EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR, ResourceBusinessLogic.class.getName(), "catalog-be",
1544 "Could not remove the software information file.");
1547 final Map<String, UploadComponentInstanceInfo> instancesToCreate = getInstancesToCreate(parsedToscaYamlInfo);
1549 log.trace("************* Going to create nodes, RI's and Relations from yaml {}", yamlName);
1550 loggerSupportability
1551 .log(LoggerSupportabilityActions.CREATE_RESOURCE_FROM_YAML, resource.getComponentMetadataForSupportLog(), StatusCode.STARTED,
1552 "Start create nodes, RI and Relations from yaml: {}", yamlName);
1553 resource = createRIAndRelationsFromYaml(yamlName, resource, instancesToCreate, topologyTemplateYaml,
1554 nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo, nodeTypesArtifactsToCreate, nodeName, null);
1557 log.trace("************* Finished to create nodes, RI and Relation from yaml {}", yamlName);
1558 loggerSupportability.log(LoggerSupportabilityActions.CREATE_RELATIONS, resource.getComponentMetadataForSupportLog(), StatusCode.COMPLETE,
1559 "Finished to create nodes, RI and Relation from yaml: {}", yamlName);
1560 // validate update vf module group names
1561 final Either<Map<String, GroupDefinition>, ResponseFormat> validateUpdateVfGroupNamesRes = groupBusinessLogic
1562 .validateUpdateVfGroupNames(parsedToscaYamlInfo.getGroups(), resource.getSystemName());
1563 if (validateUpdateVfGroupNamesRes.isRight()) {
1564 rollback(inTransaction, resource, createdArtifacts, nodeTypesNewCreatedArtifacts);
1565 throw new ByResponseFormatComponentException(validateUpdateVfGroupNamesRes.right().value());
1567 // add groups to resource
1568 final Map<String, GroupDefinition> groups;
1569 log.trace("************* Going to add groups from yaml {}", yamlName);
1570 loggerSupportability.log(LoggerSupportabilityActions.CREATE_GROUPS, resource.getComponentMetadataForSupportLog(), StatusCode.STARTED,
1571 "Start to add groups from yaml: {}", yamlName);
1572 if (!validateUpdateVfGroupNamesRes.left().value().isEmpty()) {
1573 groups = validateUpdateVfGroupNamesRes.left().value();
1575 groups = parsedToscaYamlInfo.getGroups();
1577 final Either<Resource, ResponseFormat> createGroupsOnResource = createGroupsOnResource(resource, groups);
1578 if (createGroupsOnResource.isRight()) {
1579 rollback(inTransaction, resource, createdArtifacts, nodeTypesNewCreatedArtifacts);
1580 loggerSupportability.log(LoggerSupportabilityActions.CREATE_GROUPS, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
1581 "ERROR while adding groups from yaml: {}", yamlName);
1582 throw new ByResponseFormatComponentException(createGroupsOnResource.right().value());
1584 resource = createGroupsOnResource.left().value();
1585 log.trace("************* Finished to add groups from yaml {}", yamlName);
1586 loggerSupportability.log(LoggerSupportabilityActions.CREATE_GROUPS, resource.getComponentMetadataForSupportLog(), StatusCode.COMPLETE,
1587 "Finished to add groups from yaml: {}", yamlName);
1588 log.trace("************* Going to add artifacts from yaml {}", yamlName);
1589 loggerSupportability.log(LoggerSupportabilityActions.CREATE_ARTIFACTS, resource.getComponentMetadataForSupportLog(), StatusCode.STARTED,
1590 "Started to add artifacts from yaml: {}", yamlName);
1591 log.trace("************* Starting to add policies from yaml {}", yamlName);
1592 Map<String, PolicyDefinition> policies = parsedToscaYamlInfo.getPolicies();
1593 if (MapUtils.isNotEmpty(policies)) {
1594 resource = createPoliciesOnResource(resource, policies);
1596 log.trace("************* Finished to add policies from yaml {}", yamlName);
1597 final NodeTypeInfoToUpdateArtifacts nodeTypeInfoToUpdateArtifacts = new NodeTypeInfoToUpdateArtifacts(nodeName,
1598 nodeTypesArtifactsToCreate);
1599 final Either<Resource, ResponseFormat> createArtifactsEither = createOrUpdateArtifacts(ArtifactOperationEnum.CREATE, createdArtifacts,
1600 yamlName, csarInfo, resource, nodeTypeInfoToUpdateArtifacts, inTransaction, shouldLock);
1601 if (createArtifactsEither.isRight()) {
1602 rollback(inTransaction, resource, createdArtifacts, nodeTypesNewCreatedArtifacts);
1603 loggerSupportability.log(LoggerSupportabilityActions.CREATE_ARTIFACTS, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
1604 "error happened {}", createArtifactsEither.right().value());
1605 throw new ByResponseFormatComponentException(createArtifactsEither.right().value());
1607 loggerSupportability.log(LoggerSupportabilityActions.CREATE_ARTIFACTS, resource.getComponentMetadataForSupportLog(), StatusCode.COMPLETE,
1608 "Finished to add artifacts from yaml: " + resource.getToscaResourceName());
1609 final ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.CREATED);
1610 componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), resource, actionEnum);
1611 ASDCKpiApi.countCreatedResourcesKPI();
1613 } catch (final BusinessLogicException e) {
1614 log.error(EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR, ResourceBusinessLogic.class.getName(),
1615 "An error has occurred during resource and resource instance creation", e);
1616 rollback(inTransaction, resource, createdArtifacts, nodeTypesNewCreatedArtifacts);
1617 throw new ByResponseFormatComponentException(e.getResponseFormat());
1618 } catch (final ComponentException e) {
1619 log.error(EcompLoggerErrorCode.SCHEMA_ERROR, ResourceBusinessLogic.class.getName(),
1620 "An error has occurred during resource and resource instance creation", e);
1621 rollback(inTransaction, resource, createdArtifacts, nodeTypesNewCreatedArtifacts);
1622 throw new ByResponseFormatComponentException(e.getResponseFormat());
1623 } catch (final Exception e) {
1624 log.error(EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR, ResourceBusinessLogic.class.getName(),
1625 "An error has occurred during resource and resource instance creation", e);
1626 rollback(inTransaction, resource, createdArtifacts, nodeTypesNewCreatedArtifacts);
1627 throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR);
1629 if (!inTransaction) {
1630 janusGraphDao.commit();
1633 graphLockOperation.unlockComponentByName(resource.getSystemName(), resource.getUniqueId(), NodeTypeEnum.Resource);
1638 private boolean processSubstitutableAsNodeType(final Resource resource, final ParsedToscaYamlInfo parsedToscaYamlInfo) {
1639 return !resource.getResourceType().isAtomicType() && StringUtils.isNotEmpty(resource.getModel())
1640 && parsedToscaYamlInfo.getSubstitutionMappingNodeType() != null;
1643 private Map<String, UploadComponentInstanceInfo> getInstancesToCreate(final ParsedToscaYamlInfo parsedToscaYamlInfo) {
1644 return getInstancesToCreate(parsedToscaYamlInfo, null);
1647 private Map<String, UploadComponentInstanceInfo> getInstancesToCreate(final ParsedToscaYamlInfo parsedToscaYamlInfo, final String model) {
1648 if (StringUtils.isEmpty(model) || StringUtils.isEmpty(parsedToscaYamlInfo.getSubstitutionMappingNodeType())) {
1649 return parsedToscaYamlInfo.getInstances();
1651 return parsedToscaYamlInfo.getInstances().entrySet().stream()
1652 .filter(entry -> !parsedToscaYamlInfo.getSubstitutionMappingNodeType().equals(entry.getValue().getType()))
1653 .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
1656 private void rollback(boolean inTransaction, Resource resource, List<ArtifactDefinition> createdArtifacts,
1657 List<ArtifactDefinition> nodeTypesNewCreatedArtifacts) {
1658 if (!inTransaction) {
1659 janusGraphDao.rollback();
1661 if (isNotEmpty(createdArtifacts) && isNotEmpty(nodeTypesNewCreatedArtifacts)) {
1662 createdArtifacts.addAll(nodeTypesNewCreatedArtifacts);
1663 log.debug("Found {} newly created artifacts to deleted, the component name: {}", createdArtifacts.size(), resource.getName());
1667 private Resource getResourceWithGroups(String resourceId) {
1668 ComponentParametersView filter = new ComponentParametersView();
1669 filter.setIgnoreGroups(false);
1670 Either<Resource, StorageOperationStatus> updatedResource = toscaOperationFacade.getToscaElement(resourceId, filter);
1671 if (updatedResource.isRight()) {
1672 rollbackWithException(componentsUtils.convertFromStorageResponse(updatedResource.right().value()), resourceId);
1674 return updatedResource.left().value();
1677 private Either<Resource, ResponseFormat> createGroupsOnResource(Resource resource, Map<String, GroupDefinition> groups) {
1678 if (groups != null && !groups.isEmpty()) {
1679 List<GroupDefinition> groupsAsList = updateGroupsMembersUsingResource(groups, resource);
1680 handleGroupsProperties(resource, groups);
1681 fillGroupsFinalFields(groupsAsList);
1682 Either<List<GroupDefinition>, ResponseFormat> createGroups = groupBusinessLogic.createGroups(resource, groupsAsList, true);
1683 if (createGroups.isRight()) {
1684 return Either.right(createGroups.right().value());
1687 return Either.left(resource);
1690 private void handleGroupsProperties(Resource resource, Map<String, GroupDefinition> groups) {
1691 List<InputDefinition> inputs = resource.getInputs();
1692 if (MapUtils.isNotEmpty(groups)) {
1693 groups.values().stream().filter(g -> isNotEmpty(g.getProperties())).flatMap(g -> g.getProperties().stream())
1694 .forEach(p -> handleGetInputs(p, inputs));
1698 private Resource createPoliciesOnResource(Resource resource, Map<String, PolicyDefinition> policies) {
1699 policyBusinessLogic.createPoliciesFromParsedCsar(resource, policies);
1703 private void handleGetInputs(PropertyDataDefinition property, List<InputDefinition> inputs) {
1704 if (isNotEmpty(property.getGetInputValues())) {
1705 if (inputs == null || inputs.isEmpty()) {
1706 log.debug("Failed to add property {} to group. Inputs list is empty ", property);
1707 rollbackWithException(ActionStatus.INPUTS_NOT_FOUND,
1708 property.getGetInputValues().stream().map(GetInputValueDataDefinition::getInputName).collect(toList()).toString());
1710 ListIterator<GetInputValueDataDefinition> getInputValuesIter = property.getGetInputValues().listIterator();
1711 while (getInputValuesIter.hasNext()) {
1712 GetInputValueDataDefinition getInput = getInputValuesIter.next();
1713 Either<InputDefinition, RuntimeException> inputEither = findInputByName(inputs, getInput);
1714 if (inputEither.isRight()) {
1715 throw inputEither.right().value();
1717 InputDefinition input = inputEither.left().value();
1718 getInput.setInputId(input.getUniqueId());
1719 if (getInput.getGetInputIndex() != null) {
1720 GetInputValueDataDefinition getInputIndex = getInput.getGetInputIndex();
1721 Either<InputDefinition, RuntimeException> newInputEither = findInputByName(inputs, getInputIndex);
1722 if (newInputEither.isRight()) {
1723 throw newInputEither.right().value();
1725 InputDefinition newInput = newInputEither.left().value();
1726 getInputIndex.setInputId(newInput.getUniqueId());
1728 getInputValuesIter.add(getInputIndex);
1735 <T> Either<T, RuntimeException> rollbackWithEither(final ActionStatus actionStatus, final String... params) {
1736 return rollbackWithEither(janusGraphDao, actionStatus, params);
1739 private Either<InputDefinition, RuntimeException> findInputByName(List<InputDefinition> inputs, GetInputValueDataDefinition getInput) {
1740 final String inputName = getInput != null ? getInput.getInputName() : "";
1741 if (inputs == null || inputs.isEmpty()) {
1742 log.debug("#findInputByName - Inputs list is empty");
1743 return rollbackWithEither(ActionStatus.INPUTS_NOT_FOUND, inputName);
1745 Optional<InputDefinition> inputOpt = inputs.stream().filter(p -> p.getName().equals(inputName)).findFirst();
1746 if (inputOpt.isEmpty()) {
1747 log.debug("#findInputByName - Failed to find the input {} ", inputName);
1748 return rollbackWithEither(ActionStatus.INPUTS_NOT_FOUND, inputName);
1750 return Either.left(inputOpt.get());
1755 private void fillGroupsFinalFields(List<GroupDefinition> groupsAsList) {
1756 groupsAsList.forEach(groupDefinition -> {
1757 groupDefinition.setInvariantName(groupDefinition.getName());
1758 groupDefinition.setCreatedFrom(CreatedFrom.CSAR);
1762 private Resource updateGroupsOnResource(Resource resource, Map<String, GroupDefinition> groups) {
1763 if (isEmpty(groups)) {
1766 return updateOrCreateGroups(resource, groups);
1769 private Resource updateOrCreateGroups(Resource resource, Map<String, GroupDefinition> groups) {
1770 List<GroupDefinition> groupsFromResource = resource.getGroups();
1771 List<GroupDefinition> groupsAsList = updateGroupsMembersUsingResource(groups, resource);
1772 List<GroupDefinition> groupsToUpdate = new ArrayList<>();
1773 List<GroupDefinition> groupsToDelete = new ArrayList<>();
1774 List<GroupDefinition> groupsToCreate = new ArrayList<>();
1775 if (isNotEmpty(groupsFromResource)) {
1776 addGroupsToCreateOrUpdate(groupsFromResource, groupsAsList, groupsToUpdate, groupsToCreate);
1777 addGroupsToDelete(groupsFromResource, groupsAsList, groupsToDelete);
1779 groupsToCreate.addAll(groupsAsList);
1781 if (isNotEmpty(groupsToCreate)) {
1782 fillGroupsFinalFields(groupsToCreate);
1783 if (isNotEmpty(groupsFromResource)) {
1784 groupBusinessLogic.addGroups(resource, groupsToCreate, true).left().on(this::throwComponentException);
1786 groupBusinessLogic.createGroups(resource, groupsToCreate, true).left().on(this::throwComponentException);
1789 if (isNotEmpty(groupsToDelete)) {
1790 groupBusinessLogic.deleteGroups(resource, groupsToDelete).left().on(this::throwComponentException);
1792 if (isNotEmpty(groupsToUpdate)) {
1793 groupBusinessLogic.updateGroups(resource, groupsToUpdate, true).left().on(this::throwComponentException);
1798 private void addGroupsToDelete(List<GroupDefinition> groupsFromResource, List<GroupDefinition> groupsAsList,
1799 List<GroupDefinition> groupsToDelete) {
1800 for (GroupDefinition group : groupsFromResource) {
1801 Optional<GroupDefinition> op = groupsAsList.stream().filter(p -> p.getInvariantName().equalsIgnoreCase(group.getInvariantName()))
1803 if (op.isEmpty() && (group.getArtifacts() == null || group.getArtifacts().isEmpty())) {
1804 groupsToDelete.add(group);
1809 private void addGroupsToCreateOrUpdate(List<GroupDefinition> groupsFromResource, List<GroupDefinition> groupsAsList,
1810 List<GroupDefinition> groupsToUpdate, List<GroupDefinition> groupsToCreate) {
1811 for (GroupDefinition group : groupsAsList) {
1812 Optional<GroupDefinition> op = groupsFromResource.stream().filter(p -> p.getInvariantName().equalsIgnoreCase(group.getInvariantName()))
1814 if (op.isPresent()) {
1815 GroupDefinition groupToUpdate = op.get();
1816 groupToUpdate.setMembers(group.getMembers());
1817 groupToUpdate.setCapabilities(group.getCapabilities());
1818 groupToUpdate.setProperties(group.getProperties());
1819 groupsToUpdate.add(groupToUpdate);
1821 groupsToCreate.add(group);
1826 private Resource createInputsOnResource(Resource resource, Map<String, InputDefinition> inputs) {
1827 List<InputDefinition> resourceProperties = resource.getInputs();
1828 if (MapUtils.isNotEmpty(inputs) || isNotEmpty(resourceProperties)) {
1829 Either<List<InputDefinition>, ResponseFormat> createInputs = inputsBusinessLogic.createInputsInGraph(inputs, resource);
1830 if (createInputs.isRight()) {
1831 loggerSupportability.log(LoggerSupportabilityActions.CREATE_INPUTS, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
1832 "failed to add inputs from yaml: {}", createInputs.right().value());
1833 throw new ByResponseFormatComponentException(createInputs.right().value());
1835 resource.setInputs(createInputs.left().value());
1840 private Resource generatePropertiesFromNodeType(final Resource resource, final Map<String, Object> nodeType) {
1841 final Either<Map<String, PropertyDefinition>, ResultStatusEnum> properties = ImportUtils.getProperties(nodeType);
1842 if (properties.isLeft()) {
1843 final List<PropertyDefinition> propertiesList = new ArrayList<>();
1844 final Map<String, PropertyDefinition> value = properties.left().value();
1845 if (value != null) {
1846 for (Entry<String, PropertyDefinition> entry : value.entrySet()) {
1847 final String name = entry.getKey();
1848 final PropertyDefinition propertyDefinition = entry.getValue();
1849 propertyDefinition.setName(name);
1850 propertiesList.add(propertyDefinition);
1851 resource.getProperties().removeIf(p -> p.getName().equals(name));
1854 resource.getProperties().addAll(propertiesList);
1859 private Resource createResourcePropertiesOnGraph(final Resource resource) {
1860 final List<PropertyDefinition> resourceProperties = resource.getProperties();
1861 for (PropertyDefinition propertyDefinition : resourceProperties) {
1862 final Either<PropertyDefinition, StorageOperationStatus> addPropertyEither = toscaOperationFacade
1863 .addPropertyToComponent(propertyDefinition.getName(), propertyDefinition, resource);
1865 if (addPropertyEither.isRight()) {
1866 final String error = String.format("failed to add properties from yaml: {}", addPropertyEither.right().value());
1867 loggerSupportability.log(LoggerSupportabilityActions.CREATE_PROPERTIES, resource.getComponentMetadataForSupportLog(),
1870 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(addPropertyEither.right().value()), error);
1876 private List<GroupDefinition> updateGroupsMembersUsingResource(Map<String, GroupDefinition> groups, Resource component) {
1877 List<GroupDefinition> result = new ArrayList<>();
1878 List<ComponentInstance> componentInstances = component.getComponentInstances();
1879 if (groups != null) {
1880 Either<Boolean, ResponseFormat> validateCyclicGroupsDependencies = validateCyclicGroupsDependencies(groups);
1881 if (validateCyclicGroupsDependencies.isRight()) {
1882 throw new ByResponseFormatComponentException(validateCyclicGroupsDependencies.right().value());
1884 for (Entry<String, GroupDefinition> entry : groups.entrySet()) {
1885 String groupName = entry.getKey();
1886 GroupDefinition groupDefinition = entry.getValue();
1887 GroupDefinition updatedGroupDefinition = new GroupDefinition(groupDefinition);
1888 updatedGroupDefinition.setMembers(null);
1889 Map<String, String> members = groupDefinition.getMembers();
1890 if (members != null) {
1891 updateGroupMembers(groups, updatedGroupDefinition, component, componentInstances, groupName, members);
1893 result.add(updatedGroupDefinition);
1899 private void updateGroupMembers(Map<String, GroupDefinition> groups, GroupDefinition updatedGroupDefinition, Resource component,
1900 List<ComponentInstance> componentInstances, String groupName, Map<String, String> members) {
1901 Set<String> compInstancesNames = members.keySet();
1902 if (CollectionUtils.isEmpty(componentInstances)) {
1903 String membersAstString = String.join(",", compInstancesNames);
1904 log.debug("The members: {}, in group: {}, cannot be found in component {}. There are no component instances.", membersAstString,
1905 groupName, component.getNormalizedName());
1906 throw new ByActionStatusComponentException(ActionStatus.GROUP_INVALID_COMPONENT_INSTANCE, membersAstString, groupName,
1907 component.getNormalizedName(), getComponentTypeForResponse(component));
1909 // Find all component instances with the member names
1910 Map<String, String> memberNames = componentInstances.stream().collect(toMap(ComponentInstance::getName, ComponentInstance::getUniqueId));
1911 memberNames.putAll(groups.keySet().stream().collect(toMap(g -> g, g -> "")));
1912 Map<String, String> relevantInstances = memberNames.entrySet().stream().filter(n -> compInstancesNames.contains(n.getKey()))
1913 .collect(toMap(Entry::getKey, Entry::getValue));
1914 if (relevantInstances.size() != compInstancesNames.size()) {
1915 List<String> foundMembers = new ArrayList<>(relevantInstances.keySet());
1916 foundMembers.forEach(compInstancesNames::remove);
1917 String membersAstString = String.join(",", compInstancesNames);
1918 log.debug("The members: {}, in group: {}, cannot be found in component: {}", membersAstString, groupName, component.getNormalizedName());
1919 throw new ByActionStatusComponentException(ActionStatus.GROUP_INVALID_COMPONENT_INSTANCE, membersAstString, groupName,
1920 component.getNormalizedName(), getComponentTypeForResponse(component));
1922 updatedGroupDefinition.setMembers(relevantInstances);
1926 * This Method validates that there is no cyclic group dependencies. meaning group A as member in group B which is member in group A
1931 private Either<Boolean, ResponseFormat> validateCyclicGroupsDependencies(Map<String, GroupDefinition> allGroups) {
1932 Either<Boolean, ResponseFormat> result = Either.left(true);
1934 Iterator<Entry<String, GroupDefinition>> allGroupsItr = allGroups.entrySet().iterator();
1935 while (allGroupsItr.hasNext() && result.isLeft()) {
1936 Entry<String, GroupDefinition> groupAEntry = allGroupsItr.next();
1937 // Fetches a group member A
1938 String groupAName = groupAEntry.getKey();
1939 // Finds all group members in group A
1940 Set<String> allGroupAMembersNames = new HashSet<>();
1941 fillAllGroupMemebersRecursivly(groupAEntry.getKey(), allGroups, allGroupAMembersNames);
1942 // If A is a group member of itself found cyclic dependency
1943 if (allGroupAMembersNames.contains(groupAName)) {
1944 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.GROUP_HAS_CYCLIC_DEPENDENCY, groupAName);
1945 result = Either.right(responseFormat);
1948 } catch (Exception e) {
1949 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR);
1950 result = Either.right(responseFormat);
1951 log.debug("Exception occurred when validateCyclicGroupsDependencies, error is:{}", e.getMessage(), e);
1957 * This Method fills recursively the set groupMembers with all the members of the given group which are also of type group.
1961 * @param allGroupMembers
1964 private void fillAllGroupMemebersRecursivly(String groupName, Map<String, GroupDefinition> allGroups, Set<String> allGroupMembers) {
1965 // Found Cyclic dependency
1966 if (isfillGroupMemebersRecursivlyStopCondition(groupName, allGroups, allGroupMembers)) {
1969 GroupDefinition groupDefinition = allGroups.get(groupName);
1970 // All Members Of Current Group Resource Instances & Other Groups
1971 Set<String> currGroupMembers = groupDefinition.getMembers().keySet();
1972 // Filtered Members Of Current Group containing only members which
1975 List<String> currGroupFilteredMembers = currGroupMembers.stream().
1976 // Keep Only Elements of type group and not Resource Instances
1977 filter(allGroups::containsKey).
1978 // Add Filtered Elements to main Set
1979 peek(allGroupMembers::add).
1982 // Recursively call the method for all the filtered group members
1983 for (String innerGroupName : currGroupFilteredMembers) {
1984 fillAllGroupMemebersRecursivly(innerGroupName, allGroups, allGroupMembers);
1988 private boolean isfillGroupMemebersRecursivlyStopCondition(String groupName, Map<String, GroupDefinition> allGroups,
1989 Set<String> allGroupMembers) {
1990 boolean stop = !allGroups.containsKey(groupName);
1991 // In Case Not Group Stop
1992 // In Case Group Has no members stop
1994 GroupDefinition groupDefinition = allGroups.get(groupName);
1995 stop = isEmpty(groupDefinition.getMembers());
1997 // In Case all group members already contained stop
1999 final Set<String> allMembers = allGroups.get(groupName).getMembers().keySet();
2000 Set<String> membersOfTypeGroup = allMembers.stream().
2001 // Filter In Only Group members
2002 filter(allGroups::containsKey).
2005 stop = allGroupMembers.containsAll(membersOfTypeGroup);
2010 private Resource createRIAndRelationsFromYaml(String yamlName, Resource resource,
2011 Map<String, UploadComponentInstanceInfo> uploadComponentInstanceInfoMap,
2012 String topologyTemplateYaml, List<ArtifactDefinition> nodeTypesNewCreatedArtifacts,
2013 Map<String, NodeTypeInfo> nodeTypesInfo, CsarInfo csarInfo,
2014 Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToCreate,
2015 String nodeName, final String substitutableAsNodeType) {
2016 log.debug("************* Going to create all nodes {}", yamlName);
2017 handleNodeTypes(yamlName, resource, topologyTemplateYaml, false, nodeTypesArtifactsToCreate, nodeTypesNewCreatedArtifacts, nodeTypesInfo,
2018 csarInfo, nodeName, substitutableAsNodeType);
2019 log.debug("************* Finished to create all nodes {}", yamlName);
2020 log.debug("************* Going to create all resource instances {}", yamlName);
2021 Map<String, Resource> existingNodeTypesByResourceNames = new HashMap<>();
2022 resource = createResourceInstances(yamlName, resource, null, uploadComponentInstanceInfoMap, csarInfo.getCreatedNodes(),
2023 existingNodeTypesByResourceNames);
2024 log.debug("************* Finished to create all resource instances {}", yamlName);
2025 log.debug("************* Going to create all relations {}", yamlName);
2026 resource = createResourceInstancesRelations(csarInfo.getModifier(), yamlName, resource, null, uploadComponentInstanceInfoMap,
2027 existingNodeTypesByResourceNames);
2028 log.debug("************* Finished to create all relations {}", yamlName);
2029 log.debug("************* Going to create positions {}", yamlName);
2030 compositionBusinessLogic.setPositionsForComponentInstances(resource, csarInfo.getModifier().getUserId());
2031 log.debug("************* Finished to set positions {}", yamlName);
2035 private void handleAndAddExtractedVfcsArtifacts(List<ArtifactDefinition> vfcArtifacts, List<ArtifactDefinition> artifactsToAdd) {
2036 List<String> vfcArtifactNames = vfcArtifacts.stream().map(ArtifactDataDefinition::getArtifactName).collect(toList());
2037 artifactsToAdd.forEach(a -> {
2038 if (!vfcArtifactNames.contains(a.getArtifactName())) {
2039 vfcArtifacts.add(a);
2041 log.debug("Can't upload two artifact with the same name {}. ", a.getArtifactName());
2046 @SuppressWarnings("unchecked")
2047 private void handleNodeTypes(String yamlName, Resource resource, String topologyTemplateYaml, boolean needLock,
2048 Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle,
2049 List<ArtifactDefinition> nodeTypesNewCreatedArtifacts, Map<String, NodeTypeInfo> nodeTypesInfo, CsarInfo csarInfo,
2050 String nodeName, String substitutableAsNodeType) {
2052 for (Entry<String, NodeTypeInfo> nodeTypeEntry : nodeTypesInfo.entrySet()) {
2053 if (nodeTypeEntry.getValue().isNested() && !nodeTypeAlreadyExists(nodeTypeEntry.getKey(), resource.getModel())) {
2054 handleNestedVfc(resource, nodeTypesArtifactsToHandle, nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo,
2055 nodeTypeEntry.getKey());
2056 log.trace("************* finished to create node {}", nodeTypeEntry.getKey());
2059 Map<String, Object> mappedToscaTemplate = null;
2060 if (StringUtils.isNotEmpty(nodeName) && isNotEmpty(nodeTypesInfo) && nodeTypesInfo.containsKey(nodeName)) {
2061 mappedToscaTemplate = nodeTypesInfo.get(nodeName).getMappedToscaTemplate();
2063 if (isEmpty(mappedToscaTemplate)) {
2064 mappedToscaTemplate = (Map<String, Object>) new Yaml().load(topologyTemplateYaml);
2066 createResourcesFromYamlNodeTypesList(yamlName, resource, mappedToscaTemplate, needLock, nodeTypesArtifactsToHandle,
2067 nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo, substitutableAsNodeType);
2068 } catch (ComponentException e) {
2069 ResponseFormat responseFormat =
2070 e.getResponseFormat() != null ? e.getResponseFormat() : componentsUtils.getResponseFormat(e.getActionStatus(), e.getParams());
2071 componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), resource, AuditingActionEnum.IMPORT_RESOURCE);
2073 } catch (StorageException e) {
2074 ResponseFormat responseFormat = componentsUtils
2075 .getResponseFormat(componentsUtils.convertFromStorageResponse(e.getStorageOperationStatus()));
2076 componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), resource, AuditingActionEnum.IMPORT_RESOURCE);
2081 private boolean nodeTypeAlreadyExists(final String toscaResourceName, String modelName) {
2082 return toscaOperationFacade.getLatestByToscaResourceName(toscaResourceName, modelName).isLeft();
2085 private Either<Resource, ResponseFormat> handleVfCsarArtifacts(Resource resource, CsarInfo csarInfo, List<ArtifactDefinition> createdArtifacts,
2086 ArtifactOperationInfo artifactOperation, boolean shouldLock,
2087 boolean inTransaction) {
2088 if (csarInfo.getCsar() != null) {
2089 String vendorLicenseModelId = null;
2090 String vfLicenseModelId = null;
2091 if (artifactOperation.isUpdate()) {
2092 Map<String, ArtifactDefinition> deploymentArtifactsMap = resource.getDeploymentArtifacts();
2093 if (deploymentArtifactsMap != null && !deploymentArtifactsMap.isEmpty()) {
2094 for (Entry<String, ArtifactDefinition> artifactEntry : deploymentArtifactsMap.entrySet()) {
2095 if (artifactEntry.getValue().getArtifactName().equalsIgnoreCase(Constants.VENDOR_LICENSE_MODEL)) {
2096 vendorLicenseModelId = artifactEntry.getValue().getUniqueId();
2098 if (artifactEntry.getValue().getArtifactName().equalsIgnoreCase(Constants.VF_LICENSE_MODEL)) {
2099 vfLicenseModelId = artifactEntry.getValue().getUniqueId();
2104 // Specific Behavior for license artifacts
2105 createOrUpdateSingleNonMetaArtifact(resource, csarInfo, CsarUtils.ARTIFACTS_PATH + Constants.VENDOR_LICENSE_MODEL,
2106 Constants.VENDOR_LICENSE_MODEL, ArtifactTypeEnum.VENDOR_LICENSE.getType(), ArtifactGroupTypeEnum.DEPLOYMENT,
2107 Constants.VENDOR_LICENSE_LABEL, Constants.VENDOR_LICENSE_DISPLAY_NAME, Constants.VENDOR_LICENSE_DESCRIPTION, vendorLicenseModelId,
2108 artifactOperation, null, true, shouldLock, inTransaction);
2109 createOrUpdateSingleNonMetaArtifact(resource, csarInfo, CsarUtils.ARTIFACTS_PATH + Constants.VF_LICENSE_MODEL, Constants.VF_LICENSE_MODEL,
2110 ArtifactTypeEnum.VF_LICENSE.getType(), ArtifactGroupTypeEnum.DEPLOYMENT, Constants.VF_LICENSE_LABEL,
2111 Constants.VF_LICENSE_DISPLAY_NAME, Constants.VF_LICENSE_DESCRIPTION, vfLicenseModelId, artifactOperation, null, true, shouldLock,
2113 Either<Resource, ResponseFormat> eitherCreateResult = createOrUpdateNonMetaArtifacts(csarInfo, resource, createdArtifacts, shouldLock,
2114 inTransaction, artifactOperation);
2115 if (eitherCreateResult.isRight()) {
2116 return Either.right(eitherCreateResult.right().value());
2118 Either<ImmutablePair<String, String>, ResponseFormat> artifacsMetaCsarStatus = CsarValidationUtils
2119 .getArtifactsMeta(csarInfo.getCsar(), csarInfo.getCsarUUID(), componentsUtils);
2120 if (artifacsMetaCsarStatus.isLeft()) {
2121 String artifactsFileName = artifacsMetaCsarStatus.left().value().getKey();
2122 String artifactsContents = artifacsMetaCsarStatus.left().value().getValue();
2123 Either<Resource, ResponseFormat> createArtifactsFromCsar;
2124 if (artifactOperation.isCreateOrLink()) {
2125 createArtifactsFromCsar = csarArtifactsAndGroupsBusinessLogic
2126 .createResourceArtifactsFromCsar(csarInfo, resource, artifactsContents, artifactsFileName, createdArtifacts);
2128 Either<Component, ResponseFormat> result = csarArtifactsAndGroupsBusinessLogic
2129 .updateResourceArtifactsFromCsar(csarInfo, resource, artifactsContents, artifactsFileName, createdArtifacts, shouldLock,
2131 if ((result.left().value() instanceof Resource) && result.isLeft()) {
2132 Resource service1 = (Resource) result.left().value();
2133 createArtifactsFromCsar = Either.left(service1);
2135 createArtifactsFromCsar = Either.right(result.right().value());
2138 if (createArtifactsFromCsar.isRight()) {
2139 log.debug("Couldn't create artifacts from artifacts.meta");
2140 return Either.right(createArtifactsFromCsar.right().value());
2142 return Either.left(createArtifactsFromCsar.left().value());
2144 return csarArtifactsAndGroupsBusinessLogic.deleteVFModules(resource, csarInfo, shouldLock, inTransaction);
2147 return Either.left(resource);
2150 private Either<Boolean, ResponseFormat> createOrUpdateSingleNonMetaArtifact(Resource resource, CsarInfo csarInfo, String artifactPath,
2151 String artifactFileName, String artifactType,
2152 ArtifactGroupTypeEnum artifactGroupType, String artifactLabel,
2153 String artifactDisplayName, String artifactDescription,
2154 String artifactId, ArtifactOperationInfo operation,
2155 List<ArtifactDefinition> createdArtifacts, boolean isFromCsar,
2156 boolean shouldLock, boolean inTransaction) {
2157 byte[] artifactFileBytes = null;
2158 if (csarInfo.getCsar().containsKey(artifactPath)) {
2159 artifactFileBytes = csarInfo.getCsar().get(artifactPath);
2161 Either<Boolean, ResponseFormat> result = Either.left(true);
2162 if (operation.isUpdate() || operation.isDelete()) {
2163 if (isArtifactDeletionRequired(artifactId, artifactFileBytes, isFromCsar)) {
2164 Either<ArtifactDefinition, ResponseFormat> handleDelete = artifactsBusinessLogic
2165 .handleDelete(resource.getUniqueId(), artifactId, csarInfo.getModifier(), resource, shouldLock, inTransaction);
2166 if (handleDelete.isRight()) {
2167 result = Either.right(handleDelete.right().value());
2169 ArtifactDefinition value = handleDelete.left().value();
2170 String updatedArtifactId = value.getUniqueId();
2171 if (artifactGroupType == ArtifactGroupTypeEnum.DEPLOYMENT) {
2172 resource.getDeploymentArtifacts().remove(updatedArtifactId);
2174 resource.getArtifacts().remove(updatedArtifactId);
2179 if (StringUtils.isEmpty(artifactId) && artifactFileBytes != null) {
2180 operation = new ArtifactOperationInfo(false, false, ArtifactOperationEnum.CREATE);
2183 if (artifactFileBytes != null) {
2184 Map<String, Object> vendorLicenseModelJson = ArtifactUtils
2185 .buildJsonForUpdateArtifact(artifactId, artifactFileName, artifactType, artifactGroupType, artifactLabel, artifactDisplayName,
2186 artifactDescription, artifactFileBytes, null, isFromCsar);
2187 Either<Either<ArtifactDefinition, Operation>, ResponseFormat> eitherNonMetaArtifacts = csarArtifactsAndGroupsBusinessLogic
2188 .createOrUpdateCsarArtifactFromJson(resource, csarInfo.getModifier(), vendorLicenseModelJson, operation);
2189 addNonMetaCreatedArtifactsToSupportRollback(operation, createdArtifacts, eitherNonMetaArtifacts);
2190 if (eitherNonMetaArtifacts.isRight()) {
2191 BeEcompErrorManager.getInstance().logInternalFlowError("UploadLicenseArtifact",
2192 "Failed to upload license artifact: " + artifactFileName + "With csar uuid: " + csarInfo.getCsarUUID(), ErrorSeverity.WARNING);
2193 return Either.right(eitherNonMetaArtifacts.right().value());
2195 ArtifactDefinition artifactDefinition = eitherNonMetaArtifacts.left().value().left().value();
2196 createOrUpdateResourceWithUpdatedArtifact(artifactDefinition, resource, artifactGroupType);
2201 private void createOrUpdateResourceWithUpdatedArtifact(ArtifactDefinition artifact, Resource resource, ArtifactGroupTypeEnum groupTypeEnum) {
2202 if (groupTypeEnum == ArtifactGroupTypeEnum.DEPLOYMENT) {
2203 resource.getDeploymentArtifacts().put(artifact.getArtifactLabel(), artifact);
2205 resource.getArtifacts().put(artifact.getArtifactLabel(), artifact);
2209 private boolean isArtifactDeletionRequired(String artifactId, byte[] artifactFileBytes, boolean isFromCsar) {
2210 return !StringUtils.isEmpty(artifactId) && artifactFileBytes == null && isFromCsar;
2213 private void addNonMetaCreatedArtifactsToSupportRollback(ArtifactOperationInfo operation, List<ArtifactDefinition> createdArtifacts,
2214 Either<Either<ArtifactDefinition, Operation>, ResponseFormat> eitherNonMetaArtifacts) {
2215 if (operation.isCreateOrLink() && createdArtifacts != null && eitherNonMetaArtifacts.isLeft()) {
2216 Either<ArtifactDefinition, Operation> eitherResult = eitherNonMetaArtifacts.left().value();
2217 if (eitherResult.isLeft()) {
2218 createdArtifacts.add(eitherResult.left().value());
2223 private Either<Resource, ResponseFormat> createOrUpdateNonMetaArtifacts(CsarInfo csarInfo, Resource resource,
2224 List<ArtifactDefinition> createdArtifacts, boolean shouldLock,
2225 boolean inTransaction, ArtifactOperationInfo artifactOperation) {
2226 Either<Resource, ResponseFormat> resStatus = null;
2227 Map<String, Set<List<String>>> collectedWarningMessages = new HashMap<>();
2229 Either<List<NonMetaArtifactInfo>, String> artifactPathAndNameList = getValidArtifactNames(csarInfo, collectedWarningMessages);
2230 if (artifactPathAndNameList.isRight()) {
2231 return Either.right(
2232 getComponentsUtils().getResponseFormatByArtifactId(ActionStatus.ARTIFACT_NAME_INVALID, artifactPathAndNameList.right().value(),
2233 VALID_CHARACTERS_ARTIFACT_NAME));
2235 EnumMap<ArtifactOperationEnum, List<NonMetaArtifactInfo>> vfCsarArtifactsToHandle = null;
2236 if (artifactOperation.isCreateOrLink()) {
2237 vfCsarArtifactsToHandle = new EnumMap<>(ArtifactOperationEnum.class);
2238 vfCsarArtifactsToHandle.put(artifactOperation.getArtifactOperationEnum(), artifactPathAndNameList.left().value());
2240 Either<EnumMap<ArtifactOperationEnum, List<NonMetaArtifactInfo>>, ResponseFormat> findVfCsarArtifactsToHandleRes = findVfCsarArtifactsToHandle(
2241 resource, artifactPathAndNameList.left().value(), csarInfo.getModifier());
2242 if (findVfCsarArtifactsToHandleRes.isRight()) {
2243 resStatus = Either.right(findVfCsarArtifactsToHandleRes.right().value());
2245 if (resStatus == null) {
2246 vfCsarArtifactsToHandle = findVfCsarArtifactsToHandleRes.left().value();
2249 if (resStatus == null && vfCsarArtifactsToHandle != null) {
2250 resStatus = processCsarArtifacts(csarInfo, resource, createdArtifacts, shouldLock, inTransaction, resStatus, vfCsarArtifactsToHandle);
2252 if (resStatus == null) {
2253 resStatus = Either.left(resource);
2255 } catch (Exception e) {
2256 resStatus = Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
2257 log.debug("Exception occurred in createNonMetaArtifacts, message:{}", e.getMessage(), e);
2259 CsarUtils.handleWarningMessages(collectedWarningMessages);
2264 private Either<Resource, ResponseFormat> processCsarArtifacts(CsarInfo csarInfo, Resource resource, List<ArtifactDefinition> createdArtifacts,
2265 boolean shouldLock, boolean inTransaction,
2266 Either<Resource, ResponseFormat> resStatus,
2267 EnumMap<ArtifactOperationEnum, List<NonMetaArtifactInfo>> vfCsarArtifactsToHandle) {
2268 for (Entry<ArtifactOperationEnum, List<NonMetaArtifactInfo>> currArtifactOperationPair : vfCsarArtifactsToHandle.entrySet()) {
2269 Optional<ResponseFormat> optionalCreateInDBError =
2270 // Stream of artifacts to be created
2271 currArtifactOperationPair.getValue().stream()
2272 // create each artifact
2273 .map(e -> createOrUpdateSingleNonMetaArtifact(resource, csarInfo, e.getPath(), e.getArtifactName(), e.getArtifactType(),
2274 e.getArtifactGroupType(), e.getArtifactLabel(), e.getDisplayName(), CsarUtils.ARTIFACT_CREATED_FROM_CSAR,
2275 e.getArtifactUniqueId(), new ArtifactOperationInfo(false, false, currArtifactOperationPair.getKey()), createdArtifacts,
2276 e.isFromCsar(), shouldLock, inTransaction))
2277 // filter in only error
2278 .filter(Either::isRight).
2279 // Convert the error from either to
2282 map(e -> e.right().value()).
2283 // Check if an error occurred
2285 // Error found on artifact Creation
2286 if (optionalCreateInDBError.isPresent()) {
2287 resStatus = Either.right(optionalCreateInDBError.get());
2294 private Either<List<NonMetaArtifactInfo>, String> getValidArtifactNames(CsarInfo csarInfo,
2295 Map<String, Set<List<String>>> collectedWarningMessages) {
2296 List<NonMetaArtifactInfo> artifactPathAndNameList =
2297 // Stream of file paths contained in csar
2298 csarInfo.getCsar().entrySet().stream()
2299 // Filter in only VF artifact path location
2300 .filter(e -> Pattern.compile(VF_NODE_TYPE_ARTIFACTS_PATH_PATTERN).matcher(e.getKey()).matches())
2301 // Validate and add warnings
2302 .map(e -> CsarUtils.validateNonMetaArtifact(e.getKey(), e.getValue(), collectedWarningMessages))
2303 // Filter in Non Warnings
2304 .filter(Either::isLeft)
2305 // Convert from Either to NonMetaArtifactInfo
2306 .map(e -> e.left().value())
2309 Pattern englishNumbersAndUnderScoresOnly = Pattern.compile(CsarUtils.VALID_ENGLISH_ARTIFACT_NAME);
2310 for (NonMetaArtifactInfo nonMetaArtifactInfo : artifactPathAndNameList) {
2311 if (!englishNumbersAndUnderScoresOnly.matcher(nonMetaArtifactInfo.getDisplayName()).matches()) {
2312 return Either.right(nonMetaArtifactInfo.getArtifactName());
2315 return Either.left(artifactPathAndNameList);
2318 private Either<EnumMap<ArtifactOperationEnum, List<NonMetaArtifactInfo>>, ResponseFormat> findVfCsarArtifactsToHandle(Resource resource,
2319 List<NonMetaArtifactInfo> artifactPathAndNameList,
2321 List<ArtifactDefinition> existingArtifacts = new ArrayList<>();
2322 // collect all Deployment and Informational artifacts of VF
2323 if (resource.getDeploymentArtifacts() != null && !resource.getDeploymentArtifacts().isEmpty()) {
2324 existingArtifacts.addAll(resource.getDeploymentArtifacts().values());
2326 if (resource.getArtifacts() != null && !resource.getArtifacts().isEmpty()) {
2327 existingArtifacts.addAll(resource.getArtifacts().values());
2329 existingArtifacts = existingArtifacts.stream()
2330 // filter MANDATORY artifacts, LICENSE artifacts and artifacts
2332 // was created from HEAT.meta
2333 .filter(this::isNonMetaArtifact).collect(toList());
2334 List<String> artifactsToIgnore = new ArrayList<>();
2335 // collect IDs of Artifacts of VF which belongs to any group
2336 if (resource.getGroups() != null) {
2337 resource.getGroups().forEach(g -> {
2338 if (g.getArtifacts() != null && !g.getArtifacts().isEmpty()) {
2339 artifactsToIgnore.addAll(g.getArtifacts());
2343 existingArtifacts = existingArtifacts.stream()
2344 // filter artifacts which belongs to any group
2345 .filter(a -> !artifactsToIgnore.contains(a.getUniqueId())).collect(toList());
2346 return organizeVfCsarArtifactsByArtifactOperation(artifactPathAndNameList, existingArtifacts, resource, user);
2349 private boolean isNonMetaArtifact(ArtifactDefinition artifact) {
2350 return !artifact.getMandatory() && artifact.getArtifactName() != null && isValidArtifactType(artifact);
2353 private boolean isValidArtifactType(ArtifactDefinition artifact) {
2354 return artifact.getArtifactType() != null && ArtifactTypeEnum.parse(artifact.getArtifactType()) != ArtifactTypeEnum.VENDOR_LICENSE
2355 && ArtifactTypeEnum.parse(artifact.getArtifactType()) != ArtifactTypeEnum.VF_LICENSE;
2358 private Resource createResourceInstancesRelations(User user, String yamlName, Resource resource, Resource oldResource,
2359 Map<String, UploadComponentInstanceInfo> uploadResInstancesMap,
2360 Map<String, Resource> existingNodeTypesByResourceNames) {
2361 log.debug("#createResourceInstancesRelations - Going to create relations ");
2362 loggerSupportability.log(LoggerSupportabilityActions.CREATE_RELATIONS, resource.getComponentMetadataForSupportLog(), StatusCode.STARTED,
2363 "Start to create relations");
2364 List<ComponentInstance> componentInstancesList = resource.getComponentInstances();
2365 if (isEmpty(uploadResInstancesMap) || CollectionUtils.isEmpty(componentInstancesList) &&
2366 resource.getResourceType() != ResourceTypeEnum.PNF) { // PNF can have no resource instances {
2367 log.debug("#createResourceInstancesRelations - No instances found in the resource {} is empty, yaml template file name {}, ",
2368 resource.getUniqueId(), yamlName);
2369 loggerSupportability.log(LoggerSupportabilityActions.CREATE_RELATIONS, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
2370 "No instances found in the resource: {}, is empty, yaml template file name: {}", resource.getName(), yamlName);
2371 BeEcompErrorManager.getInstance()
2372 .logInternalDataError("createResourceInstancesRelations", "No instances found in a resource or nn yaml template. ",
2373 ErrorSeverity.ERROR);
2374 throw new ByActionStatusComponentException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName);
2376 Map<String, List<ComponentInstanceProperty>> instProperties = new HashMap<>();
2377 Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> instCapabilities = new HashMap<>();
2378 Map<ComponentInstance, Map<String, List<RequirementDefinition>>> instRequirements = new HashMap<>();
2379 Map<String, Map<String, ArtifactDefinition>> instDeploymentArtifacts = new HashMap<>();
2380 Map<String, Map<String, ArtifactDefinition>> instArtifacts = new HashMap<>();
2381 Map<String, List<AttributeDefinition>> instAttributes = new HashMap<>();
2382 List<RequirementCapabilityRelDef> relations = new ArrayList<>();
2383 Map<String, List<ComponentInstanceInput>> instInputs = new HashMap<>();
2384 Resource finalResource = resource;
2385 uploadResInstancesMap.values().forEach(
2386 i -> processComponentInstance(yamlName, finalResource, componentInstancesList,
2387 componentsUtils.getAllDataTypes(applicationDataTypeCache, resource.getModel()), instProperties, instCapabilities,
2388 instRequirements, instDeploymentArtifacts, instArtifacts, instAttributes, existingNodeTypesByResourceNames, instInputs, i));
2389 resource.getComponentInstances().stream().filter(i -> !i.isCreatedFromCsar()).forEach(
2390 i -> processUiComponentInstance(oldResource, i, instCapabilities, instRequirements, instDeploymentArtifacts, instArtifacts,
2391 instProperties, instInputs, instAttributes));
2392 associateComponentInstancePropertiesToComponent(yamlName, resource, instProperties);
2393 associateComponentInstanceInputsToComponent(yamlName, resource, instInputs);
2394 associateDeploymentArtifactsToInstances(user, yamlName, resource, instDeploymentArtifacts);
2395 associateArtifactsToInstances(yamlName, resource, instArtifacts);
2396 associateOrAddCalculatedCapReq(yamlName, resource, instCapabilities, instRequirements);
2397 associateInstAttributeToComponentToInstances(yamlName, resource, instAttributes);
2398 addRelationsToRI(yamlName, resource, uploadResInstancesMap, componentInstancesList, relations);
2399 associateResourceInstances(yamlName, resource, relations);
2400 handleSubstitutionMappings(resource, uploadResInstancesMap);
2401 log.debug("************* in create relations, getResource start");
2402 loggerSupportability
2403 .log(LoggerSupportabilityActions.CREATE_RELATIONS, resource.getComponentMetadataForSupportLog(), StatusCode.COMPLETE, "create relations");
2404 Either<Resource, StorageOperationStatus> eitherGetResource = toscaOperationFacade.getToscaFullElement(resource.getUniqueId());
2405 log.debug("************* in create relations, getResource end");
2406 if (eitherGetResource.isRight()) {
2407 loggerSupportability.log(LoggerSupportabilityActions.CREATE_RELATIONS, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
2408 "ERROR while create relations");
2409 throw new ByResponseFormatComponentException(
2410 componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(eitherGetResource.right().value()), resource));
2412 return eitherGetResource.left().value();
2415 private void processUiComponentInstance(Resource oldResource, ComponentInstance instance,
2416 Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> instCapabilities,
2417 Map<ComponentInstance, Map<String, List<RequirementDefinition>>> instRequirements,
2418 Map<String, Map<String, ArtifactDefinition>> instDeploymentArtifacts,
2419 Map<String, Map<String, ArtifactDefinition>> instArtifacts,
2420 Map<String, List<ComponentInstanceProperty>> instProperties,
2421 Map<String, List<ComponentInstanceInput>> instInputs,
2422 Map<String, List<AttributeDefinition>> instAttributes) {
2423 Optional<ComponentInstance> foundInstance = findInstance(oldResource, instance);
2424 if (foundInstance.isPresent()) {
2425 if (MapUtils.isNotEmpty(foundInstance.get().getCapabilities())) {
2426 instCapabilities.put(instance, foundInstance.get().getCapabilities());
2428 if (MapUtils.isNotEmpty(foundInstance.get().getRequirements())) {
2429 instRequirements.put(instance, foundInstance.get().getRequirements());
2431 if (MapUtils.isNotEmpty(foundInstance.get().getDeploymentArtifacts())) {
2432 instDeploymentArtifacts.put(instance.getUniqueId(), foundInstance.get().getDeploymentArtifacts());
2434 if (MapUtils.isNotEmpty(foundInstance.get().getArtifacts())) {
2435 instArtifacts.put(instance.getUniqueId(), foundInstance.get().getArtifacts());
2437 if (MapUtils.isNotEmpty(oldResource.getComponentInstancesProperties()) && CollectionUtils
2438 .isNotEmpty(oldResource.getComponentInstancesProperties().get(foundInstance.get().getUniqueId()))) {
2439 instProperties.put(instance.getUniqueId(), oldResource.getComponentInstancesProperties().get(foundInstance.get().getUniqueId()));
2441 if (MapUtils.isNotEmpty(oldResource.getComponentInstancesInputs()) && CollectionUtils
2442 .isNotEmpty(oldResource.getComponentInstancesInputs().get(foundInstance.get().getUniqueId()))) {
2443 instInputs.put(instance.getUniqueId(), oldResource.getComponentInstancesInputs().get(foundInstance.get().getUniqueId()));
2445 if (MapUtils.isNotEmpty(oldResource.getComponentInstancesAttributes()) && CollectionUtils
2446 .isNotEmpty(oldResource.getComponentInstancesAttributes().get(foundInstance.get().getUniqueId()))) {
2447 instAttributes.put(instance.getUniqueId(),
2448 oldResource.getComponentInstancesAttributes().get(foundInstance.get().getUniqueId()).stream().map(AttributeDefinition::new)
2449 .collect(toList()));
2454 private Optional<ComponentInstance> findInstance(Resource oldResource, ComponentInstance instance) {
2455 if (oldResource != null && CollectionUtils.isNotEmpty(oldResource.getComponentInstances())) {
2456 return oldResource.getComponentInstances().stream().filter(i -> i.getName().equals(instance.getName())).findFirst();
2458 return Optional.empty();
2461 private void associateResourceInstances(String yamlName, Resource resource, List<RequirementCapabilityRelDef> relations) {
2462 Either<List<RequirementCapabilityRelDef>, StorageOperationStatus> relationsEither = toscaOperationFacade
2463 .associateResourceInstances(resource, resource.getUniqueId(), relations);
2464 if (relationsEither.isRight() && relationsEither.right().value() != StorageOperationStatus.NOT_FOUND) {
2465 StorageOperationStatus status = relationsEither.right().value();
2466 log.debug("failed to associate instances of resource {} status is {}", resource.getUniqueId(), status);
2467 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(status), yamlName);
2469 setResourceInstanceRelationsOnComponent(resource, relationsEither.left().value());
2473 private void associateInstAttributeToComponentToInstances(String yamlName, Resource resource,
2474 Map<String, List<AttributeDefinition>> instAttributes) {
2475 StorageOperationStatus addArtToInst;
2476 addArtToInst = toscaOperationFacade.associateInstAttributeToComponentToInstances(instAttributes, resource);
2477 if (addArtToInst != StorageOperationStatus.OK && addArtToInst != StorageOperationStatus.NOT_FOUND) {
2478 log.debug("failed to associate attributes of resource {} status is {}", resource.getUniqueId(), addArtToInst);
2479 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(addArtToInst), yamlName);
2483 private void associateOrAddCalculatedCapReq(String yamlName, Resource resource,
2484 Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> instCapabilities,
2485 Map<ComponentInstance, Map<String, List<RequirementDefinition>>> instRequirements) {
2486 StorageOperationStatus addArtToInst;
2487 addArtToInst = toscaOperationFacade.associateOrAddCalculatedCapReq(instCapabilities, instRequirements, resource);
2488 if (addArtToInst != StorageOperationStatus.OK && addArtToInst != StorageOperationStatus.NOT_FOUND) {
2489 log.debug("failed to associate cap and req of resource {} status is {}", resource.getUniqueId(), addArtToInst);
2490 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(addArtToInst), yamlName);
2494 private void associateArtifactsToInstances(String yamlName, Resource resource, Map<String, Map<String, ArtifactDefinition>> instArtifacts) {
2495 StorageOperationStatus addArtToInst;
2496 addArtToInst = toscaOperationFacade.associateArtifactsToInstances(instArtifacts, resource);
2497 if (addArtToInst != StorageOperationStatus.OK && addArtToInst != StorageOperationStatus.NOT_FOUND) {
2498 log.debug("failed to associate artifact of resource {} status is {}", resource.getUniqueId(), addArtToInst);
2499 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(addArtToInst), yamlName);
2503 private void associateDeploymentArtifactsToInstances(User user, String yamlName, Resource resource,
2504 Map<String, Map<String, ArtifactDefinition>> instDeploymentArtifacts) {
2505 StorageOperationStatus addArtToInst = toscaOperationFacade.associateDeploymentArtifactsToInstances(instDeploymentArtifacts, resource, user);
2506 if (addArtToInst != StorageOperationStatus.OK && addArtToInst != StorageOperationStatus.NOT_FOUND) {
2507 log.debug("failed to associate artifact of resource {} status is {}", resource.getUniqueId(), addArtToInst);
2508 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(addArtToInst), yamlName);
2512 private void associateComponentInstanceInputsToComponent(String yamlName, Resource resource,
2513 Map<String, List<ComponentInstanceInput>> instInputs) {
2514 if (MapUtils.isNotEmpty(instInputs)) {
2515 Either<Map<String, List<ComponentInstanceInput>>, StorageOperationStatus> addInputToInst = toscaOperationFacade
2516 .associateComponentInstanceInputsToComponent(instInputs, resource.getUniqueId());
2517 if (addInputToInst.isRight()) {
2518 StorageOperationStatus addInputToInstError = addInputToInst.right().value();
2519 log.debug("failed to associate inputs value of resource {} status is {}", resource.getUniqueId(), addInputToInstError);
2520 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(addInputToInstError), yamlName);
2522 setComponentInstanceInputsOnComponent(resource, instInputs);
2526 private void setComponentInstanceInputsOnComponent(Resource resource, Map<String, List<ComponentInstanceInput>> instInputs) {
2527 Map<String, List<ComponentInstanceInput>> componentInstancesInputs = resource.getComponentInstancesInputs();
2528 if (componentInstancesInputs == null) {
2529 componentInstancesInputs = new HashMap<>();
2531 componentInstancesInputs.putAll(instInputs);
2532 resource.setComponentInstancesInputs(componentInstancesInputs);
2535 private void associateComponentInstancePropertiesToComponent(String yamlName, Resource resource,
2536 Map<String, List<ComponentInstanceProperty>> instProperties) {
2537 Either<Map<String, List<ComponentInstanceProperty>>, StorageOperationStatus> addPropToInst = toscaOperationFacade
2538 .associateComponentInstancePropertiesToComponent(instProperties, resource.getUniqueId());
2539 if (addPropToInst.isRight()) {
2540 loggerSupportability.log(LoggerSupportabilityActions.CREATE_RELATIONS, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
2541 "ERROR while associate compnent insatnce properties of resource: {} status is: {}", resource.getName(),
2542 addPropToInst.right().value());
2543 StorageOperationStatus storageOperationStatus = addPropToInst.right().value();
2544 log.debug("failed to associate properties of resource {} status is {}", resource.getUniqueId(), storageOperationStatus);
2545 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(storageOperationStatus), yamlName);
2547 setComponentInstancePropertiesOnComponent(resource, instProperties);
2550 private void setComponentInstancePropertiesOnComponent(Resource resource, Map<String, List<ComponentInstanceProperty>> instProperties) {
2551 Map<String, List<ComponentInstanceProperty>> componentInstanceProps = resource.getComponentInstancesProperties();
2552 if (componentInstanceProps == null) {
2553 componentInstanceProps = new HashMap<>();
2555 componentInstanceProps.putAll(instProperties);
2556 resource.setComponentInstancesProperties(componentInstanceProps);
2559 private void handleSubstitutionMappings(Resource resource, Map<String, UploadComponentInstanceInfo> uploadResInstancesMap) {
2560 Either<Resource, StorageOperationStatus> getResourceRes = null;
2561 if (resource.getResourceType() == ResourceTypeEnum.CVFC) {
2562 getResourceRes = updateCalculatedCapReqWithSubstitutionMappings(resource, uploadResInstancesMap);
2563 } else if (StringUtils.isNotEmpty(resource.getModel()) && resource.getResourceType() == ResourceTypeEnum.VF) {
2564 getResourceRes = updateCalculatedCapReqWithSubstitutionMappingsForVf(resource, uploadResInstancesMap);
2566 if (getResourceRes != null && getResourceRes.isRight()) {
2567 ResponseFormat responseFormat = componentsUtils
2568 .getResponseFormatByResource(componentsUtils.convertFromStorageResponse(getResourceRes.right().value()), resource);
2569 throw new ByResponseFormatComponentException(responseFormat);
2574 private void addRelationsToRI(String yamlName, Resource resource, Map<String, UploadComponentInstanceInfo> uploadResInstancesMap,
2575 List<ComponentInstance> componentInstancesList, List<RequirementCapabilityRelDef> relations) {
2576 for (Entry<String, UploadComponentInstanceInfo> entry : uploadResInstancesMap.entrySet()) {
2577 UploadComponentInstanceInfo uploadComponentInstanceInfo = entry.getValue();
2578 ComponentInstance currentCompInstance = null;
2579 for (ComponentInstance compInstance : componentInstancesList) {
2580 if (compInstance.getName().equals(uploadComponentInstanceInfo.getName())) {
2581 currentCompInstance = compInstance;
2585 if (currentCompInstance == null) {
2586 log.debug(COMPONENT_INSTANCE_WITH_NAME_IN_RESOURCE, uploadComponentInstanceInfo.getName(), resource.getUniqueId());
2587 BeEcompErrorManager.getInstance()
2588 .logInternalDataError(COMPONENT_INSTANCE_WITH_NAME + uploadComponentInstanceInfo.getName() + IN_RESOURCE, resource.getUniqueId(),
2589 ErrorSeverity.ERROR);
2590 throw new ByActionStatusComponentException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName);
2592 ResponseFormat addRelationToRiRes = addRelationToRI(yamlName, resource, entry.getValue(), relations);
2593 if (addRelationToRiRes.getStatus() != 200) {
2594 throw new ByResponseFormatComponentException(addRelationToRiRes);
2599 private void setResourceInstanceRelationsOnComponent(Resource resource, List<RequirementCapabilityRelDef> relations) {
2600 if (resource.getComponentInstancesRelations() != null) {
2601 resource.getComponentInstancesRelations().addAll(relations);
2603 resource.setComponentInstancesRelations(relations);
2607 private void processComponentInstance(String yamlName, Resource resource, List<ComponentInstance> componentInstancesList,
2608 Map<String, DataTypeDefinition> allDataTypes,
2609 Map<String, List<ComponentInstanceProperty>> instProperties,
2610 Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> instCapabilties,
2611 Map<ComponentInstance, Map<String, List<RequirementDefinition>>> instRequirements,
2612 Map<String, Map<String, ArtifactDefinition>> instDeploymentArtifacts,
2613 Map<String, Map<String, ArtifactDefinition>> instArtifacts,
2614 Map<String, List<AttributeDefinition>> instAttributes, Map<String, Resource> originCompMap,
2615 Map<String, List<ComponentInstanceInput>> instInputs,
2616 UploadComponentInstanceInfo uploadComponentInstanceInfo) {
2617 Optional<ComponentInstance> currentCompInstanceOpt = componentInstancesList.stream()
2618 .filter(i -> i.getName().equals(uploadComponentInstanceInfo.getName())).findFirst();
2619 if (currentCompInstanceOpt.isEmpty()) {
2620 log.debug(COMPONENT_INSTANCE_WITH_NAME_IN_RESOURCE, uploadComponentInstanceInfo.getName(), resource.getUniqueId());
2621 BeEcompErrorManager.getInstance()
2622 .logInternalDataError(COMPONENT_INSTANCE_WITH_NAME + uploadComponentInstanceInfo.getName() + IN_RESOURCE, resource.getUniqueId(),
2623 ErrorSeverity.ERROR);
2624 throw new ByActionStatusComponentException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName);
2626 ComponentInstance currentCompInstance = currentCompInstanceOpt.get();
2627 String resourceInstanceId = currentCompInstance.getUniqueId();
2628 Resource originResource = getOriginResource(originCompMap, currentCompInstance);
2629 if (isNotEmpty(originResource.getRequirements())) {
2630 instRequirements.put(currentCompInstance, originResource.getRequirements());
2632 if (isNotEmpty(originResource.getCapabilities())) {
2633 processComponentInstanceCapabilities(allDataTypes, instCapabilties, uploadComponentInstanceInfo, currentCompInstance, originResource);
2635 if (originResource.getDeploymentArtifacts() != null && !originResource.getDeploymentArtifacts().isEmpty()) {
2636 instDeploymentArtifacts.put(resourceInstanceId, originResource.getDeploymentArtifacts());
2638 if (originResource.getArtifacts() != null && !originResource.getArtifacts().isEmpty()) {
2639 instArtifacts.put(resourceInstanceId, originResource.getArtifacts());
2641 if (originResource.getAttributes() != null && !originResource.getAttributes().isEmpty()) {
2642 instAttributes.put(resourceInstanceId, originResource.getAttributes());
2644 if (originResource.getResourceType() != ResourceTypeEnum.CVFC) {
2645 ResponseFormat addPropertiesValueToRiRes = addPropertyValuesToRi(uploadComponentInstanceInfo, resource, originResource,
2646 currentCompInstance, instProperties, allDataTypes);
2647 if (addPropertiesValueToRiRes.getStatus() != 200) {
2648 throw new ByResponseFormatComponentException(addPropertiesValueToRiRes);
2651 addInputsValuesToRi(uploadComponentInstanceInfo, resource, originResource, currentCompInstance, instInputs, allDataTypes);
2655 private Resource getOriginResource(Map<String, Resource> originCompMap, ComponentInstance currentCompInstance) {
2656 Resource originResource;
2657 if (!originCompMap.containsKey(currentCompInstance.getComponentUid())) {
2658 Either<Resource, StorageOperationStatus> getOriginResourceRes = toscaOperationFacade
2659 .getToscaFullElement(currentCompInstance.getComponentUid());
2660 if (getOriginResourceRes.isRight()) {
2661 log.debug("failed to fetch resource with uniqueId {} and tosca component name {} status is {}", currentCompInstance.getComponentUid(),
2662 currentCompInstance.getToscaComponentName(), getOriginResourceRes);
2663 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(getOriginResourceRes.right().value()),
2664 currentCompInstance.getComponentUid());
2666 originResource = getOriginResourceRes.left().value();
2667 originCompMap.put(originResource.getUniqueId(), originResource);
2669 originResource = originCompMap.get(currentCompInstance.getComponentUid());
2671 return originResource;
2674 private void processComponentInstanceCapabilities(Map<String, DataTypeDefinition> allDataTypes,
2675 Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> instCapabilties,
2676 UploadComponentInstanceInfo uploadComponentInstanceInfo, ComponentInstance currentCompInstance,
2677 Resource originResource) {
2678 Map<String, List<CapabilityDefinition>> originCapabilities;
2679 if (isNotEmpty(uploadComponentInstanceInfo.getCapabilities())) {
2680 originCapabilities = new HashMap<>();
2681 Map<String, Map<String, UploadPropInfo>> newPropertiesMap = new HashMap<>();
2682 originResource.getCapabilities().forEach((k, v) -> addCapabilities(originCapabilities, k, v));
2683 uploadComponentInstanceInfo.getCapabilities().values().forEach(l -> addCapabilitiesProperties(newPropertiesMap, l));
2684 updateCapabilityPropertiesValues(originCapabilities, newPropertiesMap, allDataTypes);
2686 originCapabilities = originResource.getCapabilities();
2688 instCapabilties.put(currentCompInstance, originCapabilities);
2691 private void updateCapabilityPropertiesValues(Map<String, List<CapabilityDefinition>> originCapabilities,
2692 Map<String, Map<String, UploadPropInfo>> newPropertiesMap,
2693 Map<String, DataTypeDefinition> allDataTypes) {
2694 originCapabilities.values().stream().flatMap(Collection::stream).filter(c -> newPropertiesMap.containsKey(c.getName()))
2695 .forEach(c -> updatePropertyValues(c.getProperties(), newPropertiesMap.get(c.getName()), allDataTypes));
2698 private void addCapabilitiesProperties(Map<String, Map<String, UploadPropInfo>> newPropertiesMap, List<UploadCapInfo> capabilities) {
2699 for (UploadCapInfo capability : capabilities) {
2700 if (isNotEmpty(capability.getProperties())) {
2701 newPropertiesMap.put(capability.getName(), capability.getProperties().stream().collect(toMap(UploadInfo::getName, p -> p)));
2706 private void addCapabilities(Map<String, List<CapabilityDefinition>> originCapabilities, String type, List<CapabilityDefinition> capabilities) {
2707 List<CapabilityDefinition> list = capabilities.stream().map(CapabilityDefinition::new).collect(toList());
2708 originCapabilities.put(type, list);
2711 private void updatePropertyValues(List<ComponentInstanceProperty> properties, Map<String, UploadPropInfo> newProperties,
2712 Map<String, DataTypeDefinition> allDataTypes) {
2713 properties.forEach(p -> updatePropertyValue(p, newProperties.get(p.getName()), allDataTypes));
2716 private String updatePropertyValue(ComponentInstanceProperty property, UploadPropInfo propertyInfo,
2717 Map<String, DataTypeDefinition> allDataTypes) {
2718 String value = null;
2719 List<GetInputValueDataDefinition> getInputs = null;
2720 boolean isValidate = true;
2721 if (null != propertyInfo && propertyInfo.getValue() != null) {
2722 getInputs = propertyInfo.getGet_input();
2723 isValidate = getInputs == null || getInputs.isEmpty();
2725 value = getPropertyJsonStringValue(propertyInfo.getValue(), property.getType());
2727 value = getPropertyJsonStringValue(propertyInfo.getValue(), TypeUtils.ToscaTagNamesEnum.GET_INPUT.getElementName());
2730 property.setValue(value);
2731 return validatePropValueBeforeCreate(property, value, isValidate, allDataTypes);
2734 private Either<Resource, StorageOperationStatus> updateCalculatedCapReqWithSubstitutionMappings(Resource resource,
2735 Map<String, UploadComponentInstanceInfo> uploadResInstancesMap) {
2736 Either<Resource, StorageOperationStatus> updateRes = null;
2737 Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> updatedInstCapabilities = new HashMap<>();
2738 Map<ComponentInstance, Map<String, List<RequirementDefinition>>> updatedInstRequirements = new HashMap<>();
2740 StorageOperationStatus status = toscaOperationFacade.deleteAllCalculatedCapabilitiesRequirements(resource.getUniqueId());
2741 if (status != StorageOperationStatus.OK && status != StorageOperationStatus.NOT_FOUND) {
2742 log.debug("Failed to delete all calculated capabilities and requirements of resource {} upon update. Status is {}",
2743 resource.getUniqueId(), status);
2744 updateRes = Either.right(status);
2746 if (updateRes == null) {
2747 fillUpdatedInstCapabilitiesRequirements(resource.getComponentInstances(), uploadResInstancesMap, updatedInstCapabilities,
2748 updatedInstRequirements);
2749 status = toscaOperationFacade.associateOrAddCalculatedCapReq(updatedInstCapabilities, updatedInstRequirements, resource);
2750 if (status != StorageOperationStatus.OK && status != StorageOperationStatus.NOT_FOUND) {
2752 "Failed to associate capabilities and requirementss of resource {}, updated according to a substitution mapping. Status is {}",
2753 resource.getUniqueId(), status);
2754 updateRes = Either.right(status);
2757 if (updateRes == null) {
2758 updateRes = Either.left(resource);
2763 private Either<Resource, StorageOperationStatus> updateCalculatedCapReqWithSubstitutionMappingsForVf(final Resource resource,
2764 final Map<String, UploadComponentInstanceInfo> uploadResInstancesMap) {
2765 Either<Resource, StorageOperationStatus> updateRes = null;
2766 final Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> updatedInstCapabilities = new HashMap<>();
2767 final Map<ComponentInstance, Map<String, List<RequirementDefinition>>> updatedInstRequirements = new HashMap<>();
2769 resource.getComponentInstances().forEach(i -> {
2770 setExternalCapabilities(updatedInstCapabilities, i, uploadResInstancesMap.get(i.getName()).getCapabilitiesNamesToUpdate());
2771 setExternalRequirements(updatedInstRequirements, i, uploadResInstancesMap.get(i.getName()).getRequirementsNamesToUpdate());
2774 final StorageOperationStatus status = toscaOperationFacade.updateCalculatedCapabilitiesRequirements(updatedInstCapabilities,
2775 updatedInstRequirements, resource);
2776 if (status != StorageOperationStatus.OK) {
2778 "Failed to update capabilities and requirements of resource {}. Status is {}",
2779 resource.getUniqueId(), status);
2780 updateRes = Either.right(status);
2783 if (updateRes == null) {
2784 updateRes = Either.left(resource);
2789 private void fillUpdatedInstCapabilitiesRequirements(List<ComponentInstance> componentInstances,
2790 Map<String, UploadComponentInstanceInfo> uploadResInstancesMap,
2791 Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> updatedInstCapabilities,
2792 Map<ComponentInstance, Map<String, List<RequirementDefinition>>> updatedInstRequirements) {
2793 componentInstances.forEach(i -> {
2794 fillUpdatedInstCapabilities(updatedInstCapabilities, i, uploadResInstancesMap.get(i.getName()).getCapabilitiesNamesToUpdate());
2795 fillUpdatedInstRequirements(updatedInstRequirements, i, uploadResInstancesMap.get(i.getName()).getRequirementsNamesToUpdate());
2799 private void fillUpdatedInstRequirements(Map<ComponentInstance, Map<String, List<RequirementDefinition>>> updatedInstRequirements,
2800 ComponentInstance instance, Map<String, String> requirementsNamesToUpdate) {
2801 Map<String, List<RequirementDefinition>> updatedRequirements = new HashMap<>();
2802 Set<String> updatedReqNames = new HashSet<>();
2803 if (isNotEmpty(requirementsNamesToUpdate)) {
2804 for (Map.Entry<String, List<RequirementDefinition>> requirements : instance.getRequirements().entrySet()) {
2805 updatedRequirements.put(requirements.getKey(), requirements.getValue().stream().filter(
2806 r -> requirementsNamesToUpdate.containsKey(r.getName()) && !updatedReqNames.contains(requirementsNamesToUpdate.get(r.getName())))
2808 r.setParentName(r.getName());
2809 r.setName(requirementsNamesToUpdate.get(r.getName()));
2810 updatedReqNames.add(r.getName());
2812 }).collect(toList()));
2815 if (isNotEmpty(updatedRequirements)) {
2816 updatedInstRequirements.put(instance, updatedRequirements);
2820 private void setExternalRequirements(
2821 final Map<ComponentInstance, Map<String, List<RequirementDefinition>>> updatedInstRequirements,
2822 final ComponentInstance instance, final Map<String, String> requirementsNamesToUpdate) {
2823 final Map<String, List<RequirementDefinition>> updatedRequirements = new HashMap<>();
2824 final Set<String> updatedReqNames = new HashSet<>();
2825 if (isNotEmpty(requirementsNamesToUpdate)) {
2826 for (Map.Entry<String, List<RequirementDefinition>> requirements : instance.getRequirements().entrySet()) {
2827 updatedRequirements.put(requirements.getKey(),
2828 requirements.getValue().stream()
2829 .filter(r -> requirementsNamesToUpdate.containsKey(r.getName())
2830 && !updatedReqNames.contains(requirementsNamesToUpdate.get(r.getName())))
2832 r.setExternal(true);
2833 r.setExternalName(requirementsNamesToUpdate.get(r.getName()));
2834 updatedReqNames.add(r.getName());
2836 }).collect(toList()));
2839 if (isNotEmpty(updatedRequirements)) {
2840 updatedInstRequirements.put(instance, updatedRequirements);
2844 private void setExternalCapabilities(
2845 final Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> updatedInstCapabilties,
2846 final ComponentInstance instance, Map<String, String> capabilitiesNamesToUpdate) {
2847 final Map<String, List<CapabilityDefinition>> updatedCapabilities = new HashMap<>();
2848 final Set<String> updatedCapNames = new HashSet<>();
2849 if (isNotEmpty(capabilitiesNamesToUpdate)) {
2850 for (Map.Entry<String, List<CapabilityDefinition>> requirements : instance.getCapabilities().entrySet()) {
2851 updatedCapabilities.put(requirements.getKey(),
2852 requirements.getValue().stream()
2853 .filter(c -> capabilitiesNamesToUpdate.containsKey(c.getName())
2854 && !updatedCapNames.contains(capabilitiesNamesToUpdate.get(c.getName())))
2856 c.setExternal(true);
2857 c.setExternalName(capabilitiesNamesToUpdate.get(c.getName()));
2858 updatedCapNames.add(c.getName());
2860 }).collect(toList()));
2863 if (isNotEmpty(updatedCapabilities)) {
2864 updatedInstCapabilties.put(instance, updatedCapabilities);
2868 private void fillUpdatedInstCapabilities(Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> updatedInstCapabilties,
2869 ComponentInstance instance, Map<String, String> capabilitiesNamesToUpdate) {
2870 Map<String, List<CapabilityDefinition>> updatedCapabilities = new HashMap<>();
2871 Set<String> updatedCapNames = new HashSet<>();
2872 if (isNotEmpty(capabilitiesNamesToUpdate)) {
2873 for (Map.Entry<String, List<CapabilityDefinition>> requirements : instance.getCapabilities().entrySet()) {
2874 updatedCapabilities.put(requirements.getKey(), requirements.getValue().stream().filter(
2875 c -> capabilitiesNamesToUpdate.containsKey(c.getName()) && !updatedCapNames.contains(capabilitiesNamesToUpdate.get(c.getName())))
2877 c.setParentName(c.getName());
2878 c.setName(capabilitiesNamesToUpdate.get(c.getName()));
2879 updatedCapNames.add(c.getName());
2881 }).collect(toList()));
2884 if (isNotEmpty(updatedCapabilities)) {
2885 updatedInstCapabilties.put(instance, updatedCapabilities);
2889 private ResponseFormat addRelationToRI(String yamlName, Resource resource, UploadComponentInstanceInfo nodesInfoValue,
2890 List<RequirementCapabilityRelDef> relations) {
2891 List<ComponentInstance> componentInstancesList = resource.getComponentInstances();
2892 ComponentInstance currentCompInstance = null;
2893 for (ComponentInstance compInstance : componentInstancesList) {
2894 if (compInstance.getName().equals(nodesInfoValue.getName())) {
2895 currentCompInstance = compInstance;
2899 if (currentCompInstance == null) {
2900 log.debug(COMPONENT_INSTANCE_WITH_NAME_IN_RESOURCE, nodesInfoValue.getName(), resource.getUniqueId());
2901 BeEcompErrorManager.getInstance()
2902 .logInternalDataError(COMPONENT_INSTANCE_WITH_NAME + nodesInfoValue.getName() + IN_RESOURCE, resource.getUniqueId(),
2903 ErrorSeverity.ERROR);
2904 return componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName);
2906 String resourceInstanceId = currentCompInstance.getUniqueId();
2907 Map<String, List<UploadReqInfo>> regMap = nodesInfoValue.getRequirements();
2908 if (regMap != null) {
2909 for (Entry<String, List<UploadReqInfo>> nodesRegInfoEntry : regMap.entrySet()) {
2910 List<UploadReqInfo> uploadRegInfoList = nodesRegInfoEntry.getValue();
2911 for (UploadReqInfo uploadRegInfo : uploadRegInfoList) {
2912 log.debug("Going to create relation {}", uploadRegInfo.getName());
2913 loggerSupportability
2914 .log(LoggerSupportabilityActions.CREATE_RELATIONS, resource.getComponentMetadataForSupportLog(), StatusCode.STARTED,
2915 "Started to create relations on instance: {}", uploadRegInfo.getName());
2916 String regName = uploadRegInfo.getName();
2917 RequirementCapabilityRelDef regCapRelDef = new RequirementCapabilityRelDef();
2918 regCapRelDef.setFromNode(resourceInstanceId);
2919 log.debug("try to find available requirement {} ", regName);
2920 Either<RequirementDefinition, ResponseFormat> eitherReqStatus = findAviableRequiremen(regName, yamlName, nodesInfoValue,
2921 currentCompInstance, uploadRegInfo.getCapabilityName());
2922 if (eitherReqStatus.isRight()) {
2923 log.debug("failed to find available requirement {} status is {}", regName, eitherReqStatus.right().value());
2924 loggerSupportability
2925 .log(LoggerSupportabilityActions.CREATE_RELATIONS, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
2926 "ERROR while search available requirement {} status is: {}", regName, eitherReqStatus.right().value());
2927 return eitherReqStatus.right().value();
2929 RequirementDefinition validReq = eitherReqStatus.left().value();
2930 List<CapabilityRequirementRelationship> reqAndRelationshipPairList = regCapRelDef.getRelationships();
2931 if (reqAndRelationshipPairList == null) {
2932 reqAndRelationshipPairList = new ArrayList<>();
2934 RelationshipInfo reqAndRelationshipPair = new RelationshipInfo();
2935 reqAndRelationshipPair.setRequirement(regName);
2936 reqAndRelationshipPair.setRequirementOwnerId(validReq.getOwnerId());
2937 reqAndRelationshipPair.setRequirementUid(validReq.getUniqueId());
2938 RelationshipImpl relationship = new RelationshipImpl();
2939 relationship.setType(validReq.getCapability());
2940 reqAndRelationshipPair.setRelationships(relationship);
2941 ComponentInstance currentCapCompInstance = null;
2942 for (ComponentInstance compInstance : componentInstancesList) {
2943 if (compInstance.getName().equals(uploadRegInfo.getNode())) {
2944 currentCapCompInstance = compInstance;
2948 if (currentCapCompInstance == null) {
2949 log.debug("The component instance with name {} not found on resource {} ", uploadRegInfo.getNode(), resource.getUniqueId());
2950 loggerSupportability
2951 .log(LoggerSupportabilityActions.CREATE_RELATIONS, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
2952 "ERROR component instance with name: {} not found on resource: {}", uploadRegInfo.getNode(), resource.getUniqueId());
2953 BeEcompErrorManager.getInstance()
2954 .logInternalDataError(COMPONENT_INSTANCE_WITH_NAME + uploadRegInfo.getNode() + IN_RESOURCE, resource.getUniqueId(),
2955 ErrorSeverity.ERROR);
2956 return componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName);
2958 regCapRelDef.setToNode(currentCapCompInstance.getUniqueId());
2959 log.debug("try to find aviable Capability req name is {} ", validReq.getName());
2960 CapabilityDefinition aviableCapForRel = findAvailableCapabilityByTypeOrName(validReq, currentCapCompInstance, uploadRegInfo);
2961 if (aviableCapForRel == null) {
2962 log.debug("aviable capability was not found. req name is {} component instance is {}", validReq.getName(),
2963 currentCapCompInstance.getUniqueId());
2964 loggerSupportability
2965 .log(LoggerSupportabilityActions.CREATE_RELATIONS, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
2966 "ERROR available capability was not found. req name is: {} component instance is: {}", validReq.getName(),
2967 currentCapCompInstance.getUniqueId());
2968 BeEcompErrorManager.getInstance().logInternalDataError(
2969 "aviable capability was not found. req name is " + validReq.getName() + " component instance is " + currentCapCompInstance
2970 .getUniqueId(), resource.getUniqueId(), ErrorSeverity.ERROR);
2971 return componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName);
2973 reqAndRelationshipPair.setCapability(aviableCapForRel.getName());
2974 reqAndRelationshipPair.setCapabilityUid(aviableCapForRel.getUniqueId());
2975 reqAndRelationshipPair.setCapabilityOwnerId(aviableCapForRel.getOwnerId());
2976 CapabilityRequirementRelationship capReqRel = new CapabilityRequirementRelationship();
2977 capReqRel.setRelation(reqAndRelationshipPair);
2978 reqAndRelationshipPairList.add(capReqRel);
2979 regCapRelDef.setRelationships(reqAndRelationshipPairList);
2980 relations.add(regCapRelDef);
2983 } else if (resource.getResourceType() != ResourceTypeEnum.CVFC) {
2984 return componentsUtils.getResponseFormat(ActionStatus.OK, yamlName);
2986 return componentsUtils.getResponseFormat(ActionStatus.OK);
2989 private void addInputsValuesToRi(UploadComponentInstanceInfo uploadComponentInstanceInfo, Resource resource, Resource originResource,
2990 ComponentInstance currentCompInstance, Map<String, List<ComponentInstanceInput>> instInputs,
2991 Map<String, DataTypeDefinition> allDataTypes) {
2992 Map<String, List<UploadPropInfo>> propMap = uploadComponentInstanceInfo.getProperties();
2993 if (MapUtils.isNotEmpty(propMap)) {
2994 Map<String, InputDefinition> currPropertiesMap = new HashMap<>();
2995 List<ComponentInstanceInput> instPropList = new ArrayList<>();
2996 if (CollectionUtils.isEmpty(originResource.getInputs())) {
2997 log.debug("failed to find properties ");
2998 loggerSupportability.log(LoggerSupportabilityActions.CREATE_INPUTS, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
2999 "ERROR while try to find properties");
3000 throw new ByActionStatusComponentException(ActionStatus.PROPERTY_NOT_FOUND);
3002 originResource.getInputs().forEach(p -> addInput(currPropertiesMap, p));
3003 for (List<UploadPropInfo> propertyList : propMap.values()) {
3004 processProperty(resource, currentCompInstance, allDataTypes, currPropertiesMap, instPropList, propertyList);
3006 currPropertiesMap.values().forEach(p -> instPropList.add(new ComponentInstanceInput(p)));
3007 instInputs.put(currentCompInstance.getUniqueId(), instPropList);
3011 private void processProperty(Resource resource, ComponentInstance currentCompInstance, Map<String, DataTypeDefinition> allDataTypes,
3012 Map<String, InputDefinition> currPropertiesMap, List<ComponentInstanceInput> instPropList,
3013 List<UploadPropInfo> propertyList) {
3014 UploadPropInfo propertyInfo = propertyList.get(0);
3015 String propName = propertyInfo.getName();
3016 if (!currPropertiesMap.containsKey(propName)) {
3017 loggerSupportability.log(LoggerSupportabilityActions.PROPERTY, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
3018 "ERROR failed to find property: {}", propName);
3019 log.debug("failed to find property {} ", propName);
3020 throw new ByActionStatusComponentException(ActionStatus.PROPERTY_NOT_FOUND, propName);
3022 InputDefinition curPropertyDef = currPropertiesMap.get(propName);
3023 ComponentInstanceInput property = null;
3024 String value = null;
3025 List<GetInputValueDataDefinition> getInputs = null;
3026 boolean isValidate = true;
3027 if (propertyInfo.getValue() != null) {
3028 getInputs = propertyInfo.getGet_input();
3029 isValidate = getInputs == null || getInputs.isEmpty();
3031 value = getPropertyJsonStringValue(propertyInfo.getValue(), curPropertyDef.getType());
3033 value = getPropertyJsonStringValue(propertyInfo.getValue(), TypeUtils.ToscaTagNamesEnum.GET_INPUT.getElementName());
3036 property = new ComponentInstanceInput(curPropertyDef, value, null);
3037 String validPropertyVAlue = validatePropValueBeforeCreate(property, value, isValidate, allDataTypes);
3038 property.setValue(validPropertyVAlue);
3039 if (isNotEmpty(getInputs)) {
3040 List<GetInputValueDataDefinition> getInputValues = new ArrayList<>();
3041 for (GetInputValueDataDefinition getInput : getInputs) {
3042 List<InputDefinition> inputs = resource.getInputs();
3043 if (CollectionUtils.isEmpty(inputs)) {
3044 loggerSupportability.log(LoggerSupportabilityActions.PROPERTY, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
3045 "ERROR Failed to add property: " + propName + " to resource instance: {}. Inputs list is empty ",
3046 currentCompInstance.getUniqueId());
3047 log.debug("Failed to add property {} to resource instance {}. Inputs list is empty ", property,
3048 currentCompInstance.getUniqueId());
3049 throw new ByActionStatusComponentException(ActionStatus.INVALID_CONTENT);
3051 Optional<InputDefinition> optional = inputs.stream().filter(p -> p.getName().equals(getInput.getInputName())).findAny();
3052 if (optional.isEmpty()) {
3053 loggerSupportability.log(LoggerSupportabilityActions.PROPERTY, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
3054 "ERROR Failed to find input: " + getInput.getInputName());
3055 log.debug("Failed to find input {} ", getInput.getInputName());
3056 // @@TODO error message
3057 throw new ByActionStatusComponentException(ActionStatus.INVALID_CONTENT);
3059 InputDefinition input = optional.get();
3060 getInput.setInputId(input.getUniqueId());
3061 getInputValues.add(getInput);
3062 GetInputValueDataDefinition getInputIndex = getInput.getGetInputIndex();
3063 processGetInput(getInputValues, inputs, getInputIndex);
3065 property.setGetInputValues(getInputValues);
3067 instPropList.add(property);
3068 // delete overriden property
3069 currPropertiesMap.remove(property.getName());
3072 private void processGetInput(List<GetInputValueDataDefinition> getInputValues, List<InputDefinition> inputs,
3073 GetInputValueDataDefinition getInputIndex) {
3074 Optional<InputDefinition> optional;
3075 if (getInputIndex != null) {
3076 optional = inputs.stream().filter(p -> p.getName().equals(getInputIndex.getInputName())).findAny();
3077 if (optional.isEmpty()) {
3078 log.debug("Failed to find input {} ", getInputIndex.getInputName());
3079 // @@TODO error message
3080 throw new ByActionStatusComponentException(ActionStatus.INVALID_CONTENT);
3082 InputDefinition inputIndex = optional.get();
3083 getInputIndex.setInputId(inputIndex.getUniqueId());
3084 getInputValues.add(getInputIndex);
3088 private void addInput(Map<String, InputDefinition> currPropertiesMap, InputDefinition prop) {
3089 String propName = prop.getName();
3090 if (!currPropertiesMap.containsKey(propName)) {
3091 currPropertiesMap.put(propName, prop);
3095 private ResponseFormat addPropertyValuesToRi(UploadComponentInstanceInfo uploadComponentInstanceInfo, Resource resource, Resource originResource,
3096 ComponentInstance currentCompInstance, Map<String, List<ComponentInstanceProperty>> instProperties,
3097 Map<String, DataTypeDefinition> allDataTypes) {
3098 Map<String, List<UploadPropInfo>> propMap = uploadComponentInstanceInfo.getProperties();
3099 Map<String, PropertyDefinition> currPropertiesMap = new HashMap<>();
3100 List<PropertyDefinition> listFromMap = originResource.getProperties();
3101 if ((propMap != null && !propMap.isEmpty()) && (listFromMap == null || listFromMap.isEmpty())) {
3102 loggerSupportability.log(LoggerSupportabilityActions.PROPERTY, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
3103 "ERROR Failed to find properties");
3104 log.debug("failed to find properties");
3105 return componentsUtils.getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND);
3107 if (listFromMap == null || listFromMap.isEmpty()) {
3108 return componentsUtils.getResponseFormat(ActionStatus.OK);
3110 for (PropertyDefinition prop : listFromMap) {
3111 String propName = prop.getName();
3112 if (!currPropertiesMap.containsKey(propName)) {
3113 currPropertiesMap.put(propName, prop);
3116 List<ComponentInstanceProperty> instPropList = new ArrayList<>();
3117 if (propMap != null && propMap.size() > 0) {
3118 for (List<UploadPropInfo> propertyList : propMap.values()) {
3119 UploadPropInfo propertyInfo = propertyList.get(0);
3120 String propName = propertyInfo.getName();
3121 if (!currPropertiesMap.containsKey(propName)) {
3122 log.debug("failed to find property {} ", propName);
3123 loggerSupportability.log(LoggerSupportabilityActions.PROPERTY, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
3124 "ERROR Failed to find property: {}", propName);
3125 return componentsUtils.getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND, propName);
3127 PropertyDefinition curPropertyDef = currPropertiesMap.get(propName);
3128 ComponentInstanceProperty property = null;
3129 String value = null;
3130 List<GetInputValueDataDefinition> getInputs = null;
3131 boolean isValidate = true;
3132 if (propertyInfo.getValue() != null) {
3133 getInputs = propertyInfo.getGet_input();
3134 isValidate = getInputs == null || getInputs.isEmpty();
3136 value = getPropertyJsonStringValue(propertyInfo.getValue(), curPropertyDef.getType());
3138 value = getPropertyJsonStringValue(propertyInfo.getValue(), TypeUtils.ToscaTagNamesEnum.GET_INPUT.getElementName());
3141 property = new ComponentInstanceProperty(curPropertyDef, value, null);
3142 String validatePropValue = validatePropValueBeforeCreate(property, value, isValidate, allDataTypes);
3143 property.setValue(validatePropValue);
3144 if (getInputs != null && !getInputs.isEmpty()) {
3145 List<GetInputValueDataDefinition> getInputValues = new ArrayList<>();
3146 for (GetInputValueDataDefinition getInput : getInputs) {
3147 List<InputDefinition> inputs = resource.getInputs();
3148 if (inputs == null || inputs.isEmpty()) {
3149 log.debug("Failed to add property {} to instance. Inputs list is empty ", property);
3150 loggerSupportability
3151 .log(LoggerSupportabilityActions.PROPERTY, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
3152 "Failed to add property: {} to instance. Inputs list is empty", propName);
3153 rollbackWithException(ActionStatus.INPUTS_NOT_FOUND,
3154 property.getGetInputValues().stream().map(GetInputValueDataDefinition::getInputName).collect(toList()).toString());
3156 Either<InputDefinition, RuntimeException> inputEither = findInputByName(inputs, getInput);
3157 if (inputEither.isRight()) {
3158 throw inputEither.right().value();
3160 InputDefinition input = inputEither.left().value();
3161 getInput.setInputId(input.getUniqueId());
3162 getInputValues.add(getInput);
3163 GetInputValueDataDefinition getInputIndex = getInput.getGetInputIndex();
3164 if (getInputIndex != null) {
3165 Either<InputDefinition, RuntimeException> newInputEither = findInputByName(inputs, getInputIndex);
3166 if (inputEither.isRight()) {
3167 throw newInputEither.right().value();
3169 InputDefinition newInput = newInputEither.left().value();
3170 getInputIndex.setInputId(newInput.getUniqueId());
3172 getInputValues.add(getInputIndex);
3176 property.setGetInputValues(getInputValues);
3178 instPropList.add(property);
3179 // delete overriden property
3180 currPropertiesMap.remove(property.getName());
3183 // add rest of properties
3184 if (!currPropertiesMap.isEmpty()) {
3185 for (PropertyDefinition value : currPropertiesMap.values()) {
3186 instPropList.add(new ComponentInstanceProperty(value));
3189 instProperties.put(currentCompInstance.getUniqueId(), instPropList);
3190 return componentsUtils.getResponseFormat(ActionStatus.OK);
3193 // US740820 Relate RIs according to capability name
3194 private CapabilityDefinition findAvailableCapabilityByTypeOrName(RequirementDefinition validReq, ComponentInstance currentCapCompInstance,
3195 UploadReqInfo uploadReqInfo) {
3196 if (null == uploadReqInfo.getCapabilityName() || validReq.getCapability()
3197 .equals(uploadReqInfo.getCapabilityName())) {// get
3204 return findAvailableCapability(validReq, currentCapCompInstance);
3206 return findAvailableCapability(validReq, currentCapCompInstance, uploadReqInfo);
3209 private CapabilityDefinition findAvailableCapability(RequirementDefinition validReq, ComponentInstance currentCapCompInstance,
3210 UploadReqInfo uploadReqInfo) {
3211 CapabilityDefinition cap = null;
3212 Map<String, List<CapabilityDefinition>> capMap = currentCapCompInstance.getCapabilities();
3213 if (!capMap.containsKey(validReq.getCapability())) {
3216 Optional<CapabilityDefinition> capByName = capMap.get(validReq.getCapability()).stream()
3217 .filter(p -> p.getName().equals(uploadReqInfo.getCapabilityName())).findAny();
3218 if (capByName.isEmpty()) {
3221 cap = capByName.get();
3222 if (isBoundedByOccurrences(cap)) {
3223 String leftOccurrences = cap.getLeftOccurrences();
3224 int left = Integer.parseInt(leftOccurrences);
3227 cap.setLeftOccurrences(String.valueOf(left));
3233 private CapabilityDefinition findAvailableCapability(RequirementDefinition validReq, ComponentInstance instance) {
3234 Map<String, List<CapabilityDefinition>> capMap = instance.getCapabilities();
3235 if (capMap.containsKey(validReq.getCapability())) {
3236 List<CapabilityDefinition> capList = capMap.get(validReq.getCapability());
3237 for (CapabilityDefinition cap : capList) {
3238 if (isBoundedByOccurrences(cap)) {
3239 String leftOccurrences = cap.getLeftOccurrences() != null ? cap.getLeftOccurrences() : cap.getMaxOccurrences();
3240 int left = Integer.parseInt(leftOccurrences);
3243 cap.setLeftOccurrences(String.valueOf(left));
3254 private boolean isBoundedByOccurrences(CapabilityDefinition cap) {
3255 return cap.getMaxOccurrences() != null && !cap.getMaxOccurrences().equals(CapabilityDataDefinition.MAX_OCCURRENCES);
3258 private Either<RequirementDefinition, ResponseFormat> findAviableRequiremen(String regName, String yamlName,
3259 UploadComponentInstanceInfo uploadComponentInstanceInfo,
3260 ComponentInstance currentCompInstance, String capName) {
3261 Map<String, List<RequirementDefinition>> comInstRegDefMap = currentCompInstance.getRequirements();
3262 List<RequirementDefinition> list = comInstRegDefMap.get(capName);
3263 RequirementDefinition validRegDef = null;
3265 for (Entry<String, List<RequirementDefinition>> entry : comInstRegDefMap.entrySet()) {
3266 for (RequirementDefinition reqDef : entry.getValue()) {
3267 if (reqDef.getName().equals(regName)) {
3268 if (reqDef.getMaxOccurrences() != null && !reqDef.getMaxOccurrences().equals(RequirementDataDefinition.MAX_OCCURRENCES)) {
3269 String leftOccurrences = reqDef.getLeftOccurrences();
3270 if (leftOccurrences == null) {
3271 leftOccurrences = reqDef.getMaxOccurrences();
3273 int left = Integer.parseInt(leftOccurrences);
3276 reqDef.setLeftOccurrences(String.valueOf(left));
3277 validRegDef = reqDef;
3283 validRegDef = reqDef;
3288 if (validRegDef != null) {
3293 for (RequirementDefinition reqDef : list) {
3294 if (reqDef.getName().equals(regName)) {
3295 if (reqDef.getMaxOccurrences() != null && !reqDef.getMaxOccurrences().equals(RequirementDataDefinition.MAX_OCCURRENCES)) {
3296 String leftOccurrences = reqDef.getLeftOccurrences();
3297 if (leftOccurrences == null) {
3298 leftOccurrences = reqDef.getMaxOccurrences();
3300 int left = Integer.parseInt(leftOccurrences);
3303 reqDef.setLeftOccurrences(String.valueOf(left));
3304 validRegDef = reqDef;
3310 validRegDef = reqDef;
3316 if (validRegDef == null) {
3317 ResponseFormat responseFormat = componentsUtils
3318 .getResponseFormat(ActionStatus.INVALID_NODE_TEMPLATE, yamlName, uploadComponentInstanceInfo.getName(),
3319 uploadComponentInstanceInfo.getType());
3320 return Either.right(responseFormat);
3322 return Either.left(validRegDef);
3325 private Resource createResourceInstances(String yamlName, Resource resource, Resource oldResource,
3326 Map<String, UploadComponentInstanceInfo> uploadResInstancesMap, Map<String, Resource> nodeNamespaceMap,
3327 Map<String, Resource> existingNodeTypesByResourceNames) {
3328 Either<Resource, ResponseFormat> eitherResource;
3329 log.debug("createResourceInstances is {} - going to create resource instanse from CSAR", yamlName);
3330 if (isEmpty(uploadResInstancesMap) && resource.getResourceType() != ResourceTypeEnum.PNF) { // PNF can have no resource instances
3331 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE);
3332 throw new ByResponseFormatComponentException(responseFormat);
3334 if (MapUtils.isNotEmpty(nodeNamespaceMap)) {
3335 nodeNamespaceMap.forEach((k, v) -> existingNodeTypesByResourceNames.put(v.getToscaResourceName(), v));
3337 Map<ComponentInstance, Resource> resourcesInstancesMap = new HashMap<>();
3338 uploadResInstancesMap.values().forEach(
3339 i -> createAndAddResourceInstance(i, yamlName, resource, nodeNamespaceMap, existingNodeTypesByResourceNames, resourcesInstancesMap));
3340 if (oldResource != null && oldResource.getResourceType() != ResourceTypeEnum.CVFC && oldResource.getComponentInstances() != null) {
3341 Map<String, Resource> existingNodeTypesByUids = existingNodeTypesByResourceNames.values().stream()
3342 .collect(toMap(Resource::getUniqueId, r -> r));
3343 oldResource.getComponentInstances().stream().filter(i -> !i.isCreatedFromCsar())
3344 .forEach(uiInst -> resourcesInstancesMap.put(uiInst, getOriginResource(existingNodeTypesByUids, uiInst)));
3346 if (isNotEmpty(resourcesInstancesMap)) {
3348 toscaOperationFacade.associateComponentInstancesToComponent(resource, resourcesInstancesMap, false, oldResource != null);
3349 } catch (StorageException exp) {
3350 if (exp.getStorageOperationStatus() != null && exp.getStorageOperationStatus() != StorageOperationStatus.OK) {
3351 log.debug("Failed to add component instances to container component {}", resource.getName());
3352 ResponseFormat responseFormat = componentsUtils
3353 .getResponseFormat(componentsUtils.convertFromStorageResponse(exp.getStorageOperationStatus()));
3354 eitherResource = Either.right(responseFormat);
3355 throw new ByResponseFormatComponentException(eitherResource.right().value());
3359 if (CollectionUtils.isEmpty(resource.getComponentInstances()) &&
3360 resource.getResourceType() != ResourceTypeEnum.PNF) { // PNF can have no resource instances
3361 log.debug("Error when create resource instance from csar. ComponentInstances list empty");
3362 BeEcompErrorManager.getInstance().logBeDaoSystemError("Error when create resource instance from csar. ComponentInstances list empty");
3363 throw new ByResponseFormatComponentException(componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE));
3368 private void createAndAddResourceInstance(UploadComponentInstanceInfo uploadComponentInstanceInfo, String yamlName, Resource resource,
3369 Map<String, Resource> nodeNamespaceMap, Map<String, Resource> existingnodeTypeMap,
3370 Map<ComponentInstance, Resource> resourcesInstancesMap) {
3371 Either<Resource, ResponseFormat> eitherResource;
3372 log.debug("*************Going to create resource instances {}", yamlName);
3373 // updating type if the type is node type name - we need to take the
3376 log.debug("*************Going to create resource instances {}", uploadComponentInstanceInfo.getName());
3377 if (nodeNamespaceMap.containsKey(uploadComponentInstanceInfo.getType())) {
3378 uploadComponentInstanceInfo.setType(nodeNamespaceMap.get(uploadComponentInstanceInfo.getType()).getToscaResourceName());
3380 Resource refResource = validateResourceInstanceBeforeCreate(yamlName, uploadComponentInstanceInfo, existingnodeTypeMap, resource);
3381 ComponentInstance componentInstance = new ComponentInstance();
3382 componentInstance.setComponentUid(refResource.getUniqueId());
3383 Collection<String> directives = uploadComponentInstanceInfo.getDirectives();
3384 if (directives != null && !directives.isEmpty()) {
3385 componentInstance.setDirectives(new ArrayList<>(directives));
3387 UploadNodeFilterInfo uploadNodeFilterInfo = uploadComponentInstanceInfo.getUploadNodeFilterInfo();
3388 if (uploadNodeFilterInfo != null) {
3390 .setNodeFilter(new CINodeFilterUtils().getNodeFilterDataDefinition(uploadNodeFilterInfo, componentInstance.getUniqueId()));
3392 ComponentTypeEnum containerComponentType = resource.getComponentType();
3393 NodeTypeEnum containerNodeType = containerComponentType.getNodeType();
3394 if (containerNodeType == NodeTypeEnum.Resource && isNotEmpty(uploadComponentInstanceInfo.getCapabilities()) && isNotEmpty(
3395 refResource.getCapabilities())) {
3396 setCapabilityNamesTypes(refResource.getCapabilities(), uploadComponentInstanceInfo.getCapabilities());
3397 Map<String, List<CapabilityDefinition>> validComponentInstanceCapabilities = getValidComponentInstanceCapabilities(
3398 refResource.getUniqueId(), refResource.getCapabilities(), uploadComponentInstanceInfo.getCapabilities());
3399 componentInstance.setCapabilities(validComponentInstanceCapabilities);
3401 if (isNotEmpty(uploadComponentInstanceInfo.getArtifacts())) {
3402 Map<String, Map<String, UploadArtifactInfo>> artifacts = uploadComponentInstanceInfo.getArtifacts();
3403 Map<String, ToscaArtifactDataDefinition> toscaArtifacts = new HashMap<>();
3404 Map<String, Map<String, UploadArtifactInfo>> arts = artifacts.entrySet().stream()
3405 .filter(e -> e.getKey().contains(TypeUtils.ToscaTagNamesEnum.ARTIFACTS.getElementName()))
3406 .collect(Collectors.toMap(Entry::getKey, Entry::getValue));
3407 Map<String, UploadArtifactInfo> artifact = arts.get(TypeUtils.ToscaTagNamesEnum.ARTIFACTS.getElementName());
3408 for (Map.Entry<String, UploadArtifactInfo> entry : artifact.entrySet()) {
3409 ToscaArtifactDataDefinition to = new ToscaArtifactDataDefinition();
3410 to.setFile(entry.getValue().getFile());
3411 to.setType(entry.getValue().getType());
3412 if(isNotEmpty(entry.getValue().getProperties())) {
3413 Map<String, Object> newPropertiesMap = new HashMap<>();
3414 List<UploadPropInfo> artifactPropsInfo = entry.getValue().getProperties();
3415 for(UploadPropInfo propInfo: artifactPropsInfo) {
3416 newPropertiesMap.put(propInfo.getName(), propInfo.getValue());
3418 to.setProperties(newPropertiesMap);
3420 toscaArtifacts.put(entry.getKey(), to);
3422 componentInstance.setToscaArtifacts(toscaArtifacts);
3424 if (!existingnodeTypeMap.containsKey(uploadComponentInstanceInfo.getType())) {
3425 log.debug("createResourceInstances - not found lates version for resource instance with name {} and type {}",
3426 uploadComponentInstanceInfo.getName(), uploadComponentInstanceInfo.getType());
3427 throw new ByActionStatusComponentException(ActionStatus.INVALID_NODE_TEMPLATE, yamlName, uploadComponentInstanceInfo.getName(),
3428 uploadComponentInstanceInfo.getType());
3430 Resource origResource = existingnodeTypeMap.get(uploadComponentInstanceInfo.getType());
3431 componentInstance.setName(uploadComponentInstanceInfo.getName());
3432 componentInstance.setIcon(origResource.getIcon());
3433 componentInstance.setCreatedFrom(CreatedFrom.CSAR);
3434 resourcesInstancesMap.put(componentInstance, origResource);
3437 private void setCapabilityNamesTypes(Map<String, List<CapabilityDefinition>> originCapabilities,
3438 Map<String, List<UploadCapInfo>> uploadedCapabilities) {
3439 for (Entry<String, List<UploadCapInfo>> currEntry : uploadedCapabilities.entrySet()) {
3440 if (originCapabilities.containsKey(currEntry.getKey())) {
3441 currEntry.getValue().forEach(cap -> cap.setType(currEntry.getKey()));
3444 for (Map.Entry<String, List<CapabilityDefinition>> capabilities : originCapabilities.entrySet()) {
3445 capabilities.getValue().forEach(cap -> {
3446 if (uploadedCapabilities.containsKey(cap.getName())) {
3447 uploadedCapabilities.get(cap.getName()).forEach(c -> {
3448 c.setName(cap.getName());
3449 c.setType(cap.getType());
3456 private Resource validateResourceInstanceBeforeCreate(String yamlName, UploadComponentInstanceInfo uploadComponentInstanceInfo,
3457 Map<String, Resource> nodeNamespaceMap, Resource resource) {
3458 log.debug("validateResourceInstanceBeforeCreate - going to validate resource instance with name {} and type {} before create",
3459 uploadComponentInstanceInfo.getName(), uploadComponentInstanceInfo.getType());
3460 Resource refResource;
3461 if (nodeNamespaceMap.containsKey(uploadComponentInstanceInfo.getType())) {
3462 refResource = nodeNamespaceMap.get(uploadComponentInstanceInfo.getType());
3464 Either<Resource, StorageOperationStatus> findResourceEither = StringUtils.isEmpty(resource.getModel()) ?
3465 toscaOperationFacade.getByToscaResourceNameMatchingVendorRelease(uploadComponentInstanceInfo.getType(),
3466 ((ResourceMetadataDataDefinition) resource.getComponentMetadataDefinition().getMetadataDataDefinition()).getVendorRelease()):
3467 toscaOperationFacade.getLatestByToscaResourceNameAndModel(uploadComponentInstanceInfo.getType(), resource.getModel());
3468 if (findResourceEither.isRight()) {
3469 log.debug("validateResourceInstanceBeforeCreate - not found latest version for resource instance with name {} and type {}",
3470 uploadComponentInstanceInfo.getName(), uploadComponentInstanceInfo.getType());
3471 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(findResourceEither.right().value()));
3473 refResource = findResourceEither.left().value();
3474 nodeNamespaceMap.put(refResource.getToscaResourceName(), refResource);
3476 String componentState = refResource.getComponentMetadataDefinition().getMetadataDataDefinition().getState();
3477 if (componentState.equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name())) {
3479 "validateResourceInstanceBeforeCreate - component instance of component {} can not be created because the component is in an illegal state {}.",
3480 refResource.getName(), componentState);
3481 throw new ByActionStatusComponentException(ActionStatus.ILLEGAL_COMPONENT_STATE, refResource.getComponentType().getValue(),
3482 refResource.getName(), componentState);
3484 if (!ModelConverter.isAtomicComponent(refResource) && refResource.getResourceType() != ResourceTypeEnum.CVFC) {
3485 log.debug("validateResourceInstanceBeforeCreate - ref resource type is {} ", refResource.getResourceType());
3486 throw new ByActionStatusComponentException(ActionStatus.INVALID_NODE_TEMPLATE, yamlName, uploadComponentInstanceInfo.getName(),
3487 uploadComponentInstanceInfo.getType());
3492 public Resource propagateStateToCertified(User user, Resource resource, LifecycleChangeInfoWithAction lifecycleChangeInfo, boolean inTransaction,
3493 boolean needLock, boolean forceCertificationAllowed) {
3494 boolean failed = false;
3496 if (resource.getLifecycleState() != LifecycleStateEnum.CERTIFIED && forceCertificationAllowed && lifecycleBusinessLogic
3497 .isFirstCertification(resource.getVersion())) {
3498 nodeForceCertification(resource, user, lifecycleChangeInfo, inTransaction, needLock);
3500 if (resource.getLifecycleState() == LifecycleStateEnum.CERTIFIED) {
3501 Either<ArtifactDefinition, Operation> eitherPopulated = populateToscaArtifacts(resource, user, false, inTransaction, needLock, false);
3504 return nodeFullCertification(resource.getUniqueId(), user, lifecycleChangeInfo, inTransaction, needLock);
3505 } catch (ComponentException e) {
3507 log.debug("The exception has occurred upon certification of resource {}. ", resource.getName(), e);
3511 BeEcompErrorManager.getInstance().logBeSystemError("Change LifecycleState - Certify");
3512 if (!inTransaction) {
3513 janusGraphDao.rollback();
3515 } else if (!inTransaction) {
3516 janusGraphDao.commit();
3521 private Resource nodeFullCertification(String uniqueId, User user, LifecycleChangeInfoWithAction lifecycleChangeInfo, boolean inTransaction,
3523 Either<Resource, ResponseFormat> resourceResponse = lifecycleBusinessLogic
3524 .changeState(uniqueId, user, LifeCycleTransitionEnum.CERTIFY, lifecycleChangeInfo, inTransaction, needLock);
3525 if (resourceResponse.isRight()) {
3526 throw new ByResponseFormatComponentException(resourceResponse.right().value());
3528 return resourceResponse.left().value();
3531 private Resource nodeForceCertification(Resource resource, User user, LifecycleChangeInfoWithAction lifecycleChangeInfo, boolean inTransaction,
3533 return lifecycleBusinessLogic.forceResourceCertification(resource, user, lifecycleChangeInfo, inTransaction, needLock);
3536 public ImmutablePair<Resource, ActionStatus> createOrUpdateResourceByImport(final Resource resource, final User user, final boolean isNormative,
3537 final boolean isInTransaction, final boolean needLock,
3538 final CsarInfo csarInfo, final String nodeName,
3539 final boolean isNested) {
3540 ImmutablePair<Resource, ActionStatus> result = null;
3541 // check if resource already exists (search by tosca name = type)
3542 final boolean isNestedResource = isNestedResourceUpdate(csarInfo, nodeName);
3543 final String resourceName = resource.getToscaResourceName();
3544 final Either<Resource, StorageOperationStatus> latestByToscaName = toscaOperationFacade
3545 .getLatestByToscaResourceNameAndModel(resourceName, resource.getModel());
3546 if (latestByToscaName.isLeft() && Objects.nonNull(latestByToscaName.left().value())) {
3547 final Resource foundResource = latestByToscaName.left().value();
3548 // we don't allow updating names of top level types
3549 if (!isNestedResource && !StringUtils.equals(resource.getName(), foundResource.getName())) {
3550 BeEcompErrorManager.getInstance()
3551 .logBeComponentMissingError("Create / Update resource by import", ComponentTypeEnum.RESOURCE.getValue(), resource.getName());
3552 log.debug("resource already exist new name={} old name={} same type={}", resource.getName(), foundResource.getName(),
3553 resource.getToscaResourceName());
3554 final ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.RESOURCE_ALREADY_EXISTS);
3555 componentsUtils.auditResource(responseFormat, user, resource, AuditingActionEnum.IMPORT_RESOURCE);
3556 throwComponentException(responseFormat);
3558 result = updateExistingResourceByImport(resource, foundResource, user, isNormative, needLock, isNested);
3559 } else if (isNotFound(latestByToscaName)) {
3560 if (isNestedResource) {
3561 result = createOrUpdateNestedResource(resource, user, isNormative, isInTransaction, needLock, csarInfo, isNested, nodeName);
3563 result = createResourceByImport(resource, user, isNormative, isInTransaction, csarInfo);
3566 StorageOperationStatus status = latestByToscaName.right().value();
3567 log.debug("failed to get latest version of resource {}. status={}", resource.getName(), status);
3568 ResponseFormat responseFormat = componentsUtils
3569 .getResponseFormatByResource(componentsUtils.convertFromStorageResponse(latestByToscaName.right().value()), resource);
3570 componentsUtils.auditResource(responseFormat, user, resource, AuditingActionEnum.IMPORT_RESOURCE);
3571 throwComponentException(responseFormat);
3576 private boolean isNestedResourceUpdate(CsarInfo csarInfo, String nodeName) {
3577 return csarInfo != null && csarInfo.isUpdate() && nodeName != null;
3580 private ImmutablePair<Resource, ActionStatus> createOrUpdateNestedResource(final Resource resource, final User user, final boolean isNormative,
3581 final boolean isInTransaction, final boolean needLock,
3582 final CsarInfo csarInfo, final boolean isNested,
3583 final String nodeName) {
3584 final Either<Component, StorageOperationStatus> latestByToscaName = toscaOperationFacade.getLatestByToscaResourceName(
3585 buildNestedToscaResourceName(resource.getResourceType().name(), csarInfo.getVfResourceName(), nodeName).getRight(), resource.getModel());
3586 if (latestByToscaName.isLeft()) {
3587 final Resource nestedResource = (Resource) latestByToscaName.left().value();
3588 log.debug(VALIDATE_DERIVED_BEFORE_UPDATE);
3589 final Either<Boolean, ResponseFormat> eitherValidation = validateNestedDerivedFromDuringUpdate(nestedResource, resource,
3590 ValidationUtils.hasBeenCertified(nestedResource.getVersion()));
3591 if (eitherValidation.isRight()) {
3592 return createResourceByImport(resource, user, isNormative, isInTransaction, csarInfo);
3594 return updateExistingResourceByImport(resource, nestedResource, user, isNormative, needLock, isNested);
3596 return createResourceByImport(resource, user, isNormative, isInTransaction, csarInfo);
3600 private boolean isNotFound(Either<Resource, StorageOperationStatus> getResourceEither) {
3601 return getResourceEither.isRight() && getResourceEither.right().value() == StorageOperationStatus.NOT_FOUND;
3604 private ImmutablePair<Resource, ActionStatus> createResourceByImport(Resource resource, User user, boolean isNormative, boolean isInTransaction,
3605 CsarInfo csarInfo) {
3606 log.debug("resource with name {} does not exist. create new resource", resource.getName());
3607 validateResourceBeforeCreate(resource, user, AuditingActionEnum.IMPORT_RESOURCE, isInTransaction, csarInfo);
3608 final Resource createResourceByDao = createResourceByDao(resource, user, AuditingActionEnum.IMPORT_RESOURCE, isNormative, isInTransaction);
3609 Resource createdResource = updateCatalog(createResourceByDao, ChangeTypeEnum.LIFECYCLE).left().map(r -> (Resource) r).left().value();
3610 ImmutablePair<Resource, ActionStatus> resourcePair = new ImmutablePair<>(createdResource, ActionStatus.CREATED);
3611 ASDCKpiApi.countImportResourcesKPI();
3612 return resourcePair;
3615 public boolean isResourceExist(String resourceName) {
3616 Either<Resource, StorageOperationStatus> latestByName = toscaOperationFacade.getLatestByName(resourceName, null);
3617 return latestByName.isLeft();
3620 private ImmutablePair<Resource, ActionStatus> updateExistingResourceByImport(Resource newResource, Resource oldResource, User user,
3621 boolean inTransaction, boolean needLock, boolean isNested) {
3622 String lockedResourceId = oldResource.getUniqueId();
3623 log.debug("found resource: name={}, id={}, version={}, state={}", oldResource.getName(), lockedResourceId, oldResource.getVersion(),
3624 oldResource.getLifecycleState());
3625 ImmutablePair<Resource, ActionStatus> resourcePair = null;
3627 lockComponent(lockedResourceId, oldResource, needLock, "Update Resource by Import");
3628 oldResource = prepareResourceForUpdate(oldResource, newResource, user, inTransaction, false);
3629 mergeOldResourceMetadataWithNew(oldResource, newResource);
3630 validateResourceFieldsBeforeUpdate(oldResource, newResource, inTransaction, isNested);
3631 validateCapabilityTypesCreate(user, getCapabilityTypeOperation(), newResource, AuditingActionEnum.IMPORT_RESOURCE, inTransaction);
3632 // contact info normalization
3633 newResource.setContactId(newResource.getContactId().toLowerCase());
3634 PropertyConstraintsUtils.validatePropertiesConstraints(newResource, oldResource);
3635 // non-updatable fields
3636 newResource.setCreatorUserId(user.getUserId());
3637 newResource.setCreatorFullName(user.getFullName());
3638 newResource.setLastUpdaterUserId(user.getUserId());
3639 newResource.setLastUpdaterFullName(user.getFullName());
3640 newResource.setUniqueId(oldResource.getUniqueId());
3641 newResource.setVersion(oldResource.getVersion());
3642 newResource.setInvariantUUID(oldResource.getInvariantUUID());
3643 newResource.setLifecycleState(oldResource.getLifecycleState());
3644 newResource.setUUID(oldResource.getUUID());
3645 newResource.setNormalizedName(oldResource.getNormalizedName());
3646 newResource.setSystemName(oldResource.getSystemName());
3647 newResource.setModel(oldResource.getModel());
3648 if (oldResource.getCsarUUID() != null) {
3649 newResource.setCsarUUID(oldResource.getCsarUUID());
3651 if (oldResource.getImportedToscaChecksum() != null) {
3652 newResource.setImportedToscaChecksum(oldResource.getImportedToscaChecksum());
3654 newResource.setAbstract(oldResource.isAbstract());
3655 if (CollectionUtils.isEmpty(newResource.getDerivedFrom())) {
3656 newResource.setDerivedFrom(oldResource.getDerivedFrom());
3658 if (CollectionUtils.isEmpty(newResource.getDataTypes())) {
3659 newResource.setDataTypes(oldResource.getDataTypes());
3661 if (StringUtils.isEmpty(newResource.getDerivedFromGenericType())) {
3662 newResource.setDerivedFromGenericType(oldResource.getDerivedFromGenericType());
3664 if (StringUtils.isEmpty(newResource.getDerivedFromGenericVersion())) {
3665 newResource.setDerivedFromGenericVersion(oldResource.getDerivedFromGenericVersion());
3669 // created without tosca artifacts - add the placeholders
3670 if (MapUtils.isEmpty(newResource.getToscaArtifacts())) {
3671 setToscaArtifactsPlaceHolders(newResource, user);
3673 if (MapUtils.isEmpty(newResource.getInterfaces())) {
3674 newResource.setInterfaces(oldResource.getInterfaces());
3676 if (CollectionUtils.isEmpty(newResource.getAttributes())) {
3677 newResource.setAttributes(oldResource.getAttributes());
3679 if (CollectionUtils.isEmpty(newResource.getProperties())) {
3680 newResource.setProperties(oldResource.getProperties());
3682 Either<Resource, StorageOperationStatus> overrideResource = toscaOperationFacade.overrideComponent(newResource, oldResource);
3683 if (overrideResource.isRight()) {
3684 ResponseFormat responseFormat = componentsUtils
3685 .getResponseFormatByResource(componentsUtils.convertFromStorageResponse(overrideResource.right().value()), newResource);
3686 componentsUtils.auditResource(responseFormat, user, newResource, AuditingActionEnum.IMPORT_RESOURCE);
3687 throwComponentException(responseFormat);
3689 updateCatalog(overrideResource.left().value(), ChangeTypeEnum.LIFECYCLE);
3690 log.debug("Resource updated successfully!!!");
3691 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.OK);
3692 componentsUtils.auditResource(responseFormat, user, newResource, AuditingActionEnum.IMPORT_RESOURCE,
3693 ResourceVersionInfo.newBuilder().state(oldResource.getLifecycleState().name()).version(oldResource.getVersion()).build());
3694 resourcePair = new ImmutablePair<>(overrideResource.left().value(), ActionStatus.OK);
3695 return resourcePair;
3697 if (resourcePair == null) {
3698 BeEcompErrorManager.getInstance().logBeSystemError("Change LifecycleState - Certify");
3699 janusGraphDao.rollback();
3700 } else if (!inTransaction) {
3701 janusGraphDao.commit();
3704 log.debug("unlock resource {}", lockedResourceId);
3705 graphLockOperation.unlockComponent(lockedResourceId, NodeTypeEnum.Resource);
3711 * Merge old resource with new. Keep old category and vendor name without change
3713 * @param oldResource
3714 * @param newResource
3716 private void mergeOldResourceMetadataWithNew(Resource oldResource, Resource newResource) {
3717 // keep old category and vendor name without change
3719 // merge the rest of the resource metadata
3720 if (newResource.getTags() == null || newResource.getTags().isEmpty()) {
3721 newResource.setTags(oldResource.getTags());
3723 if (newResource.getDescription() == null) {
3724 newResource.setDescription(oldResource.getDescription());
3726 if (newResource.getVendorRelease() == null) {
3727 newResource.setVendorRelease(oldResource.getVendorRelease());
3729 if (newResource.getResourceVendorModelNumber() == null) {
3730 newResource.setResourceVendorModelNumber(oldResource.getResourceVendorModelNumber());
3732 if (newResource.getModel() == null) {
3733 newResource.setModel(oldResource.getModel());
3735 if (newResource.getContactId() == null) {
3736 newResource.setContactId(oldResource.getContactId());
3738 newResource.setCategories(oldResource.getCategories());
3739 if (newResource.getVendorName() == null) {
3740 newResource.setVendorName(oldResource.getVendorName());
3742 List<GroupDefinition> oldForUpdate = oldResource.getGroups();
3743 if (CollectionUtils.isNotEmpty(oldForUpdate)) {
3744 List<GroupDefinition> groupForUpdate = oldForUpdate.stream().map(GroupDefinition::new).collect(Collectors.toList());
3745 groupForUpdate.stream().filter(GroupDataDefinition::isVspOriginated).forEach(group -> group.setName(group.getInvariantName()));
3746 newResource.setGroups(groupForUpdate);
3748 if (newResource.getResourceType().isAtomicType() && !newResource.getName().equals("Root")
3749 && newResource.getResourceType() != ResourceTypeEnum.CVFC) {
3750 ResourceTypeEnum updatedResourceType = newResource.getResourceType();
3751 Optional<Component> derivedFromResourceOptional = getParentComponent(newResource);
3752 if (derivedFromResourceOptional.isPresent() && derivedFromResourceOptional.get().getComponentType() == ComponentTypeEnum.RESOURCE) {
3753 Resource parentResource = (Resource) derivedFromResourceOptional.get();
3754 if (!(parentResource.isAbstract() && (ResourceTypeEnum.VFC == parentResource.getResourceType()
3755 || ResourceTypeEnum.ABSTRACT == parentResource.getResourceType())) && parentResource.getResourceType() != updatedResourceType
3756 && oldResource.getResourceType() != updatedResourceType) {
3757 BeEcompErrorManager.getInstance().logInternalDataError("mergeOldResourceMetadataWithNew",
3758 "resource type of the resource does not match to derived from resource type", ErrorSeverity.ERROR);
3760 "#mergeOldResourceMetadataWithNew - resource type {} of the resource {} does not match to derived from resource type {}",
3761 newResource.getResourceType(), newResource.getToscaResourceName(), parentResource.getResourceType());
3762 throw new ByActionStatusComponentException(ActionStatus.INVALID_RESOURCE_TYPE);
3768 private Optional<Component> getParentComponent(Resource newResource) {
3769 if (newResource.getDerivedFrom() == null) {
3770 return Optional.empty();
3772 String toscaResourceNameDerivedFrom = newResource.getDerivedFrom().get(0);
3773 Either<Component, StorageOperationStatus> latestByToscaResourceName = toscaOperationFacade
3774 .getLatestByToscaResourceName(toscaResourceNameDerivedFrom, newResource.getModel());
3775 if (latestByToscaResourceName.isRight()) {
3776 BeEcompErrorManager.getInstance()
3777 .logInternalDataError("mergeOldResourceMetadataWithNew", "derived from resource not found", ErrorSeverity.ERROR);
3778 log.debug("#mergeOldResourceMetadataWithNew - derived from resource {} not found", toscaResourceNameDerivedFrom);
3779 throw new ByActionStatusComponentException(ActionStatus.RESOURCE_NOT_FOUND, toscaResourceNameDerivedFrom);
3781 return Optional.of(latestByToscaResourceName.left().value());
3784 private Resource prepareResourceForUpdate(Resource oldResource, Resource newResource, User user, boolean inTransaction, boolean needLock) {
3785 if (!ComponentValidationUtils.canWorkOnResource(oldResource, user.getUserId())) {
3787 return lifecycleBusinessLogic
3788 .changeState(oldResource.getUniqueId(), user, LifeCycleTransitionEnum.CHECKOUT, new LifecycleChangeInfoWithAction("update by import"),
3789 inTransaction, needLock).left().on(response -> failOnChangeState(response, user, oldResource, newResource));
3794 private Resource failOnChangeState(ResponseFormat response, User user, Resource oldResource, Resource newResource) {
3795 log.info("resource {} cannot be updated. reason={}", oldResource.getUniqueId(), response.getFormattedMessage());
3796 componentsUtils.auditResource(response, user, newResource, AuditingActionEnum.IMPORT_RESOURCE,
3797 ResourceVersionInfo.newBuilder().state(oldResource.getLifecycleState().name()).version(oldResource.getVersion()).build());
3798 throw new ByResponseFormatComponentException(response);
3801 public Resource validateResourceBeforeCreate(Resource resource, User user, AuditingActionEnum actionEnum, boolean inTransaction,
3802 CsarInfo csarInfo) {
3803 validateResourceFieldsBeforeCreate(user, resource, actionEnum, inTransaction);
3804 validateCapabilityTypesCreate(user, getCapabilityTypeOperation(), resource, actionEnum, inTransaction);
3805 validateLifecycleTypesCreate(user, resource, actionEnum);
3806 validateResourceType(user, resource, actionEnum);
3807 resource.setCreatorUserId(user.getUserId());
3808 resource.setCreatorFullName(user.getFirstName() + " " + user.getLastName());
3809 resource.setContactId(resource.getContactId().toLowerCase());
3810 if (StringUtils.isEmpty(resource.getToscaResourceName()) && !ModelConverter.isAtomicComponent(resource)) {
3811 String resourceSystemName;
3812 if (csarInfo != null && StringUtils.isNotEmpty(csarInfo.getVfResourceName())) {
3813 resourceSystemName = ValidationUtils.convertToSystemName(csarInfo.getVfResourceName());
3815 resourceSystemName = resource.getSystemName();
3818 .setToscaResourceName(CommonBeUtils.generateToscaResourceName(resource.getResourceType().name().toLowerCase(), resourceSystemName));
3820 // Generate invariant UUID - must be here and not in operation since it
3822 // should stay constant during clone
3825 String invariantUUID = UniqueIdBuilder.buildInvariantUUID();
3826 resource.setInvariantUUID(invariantUUID);
3830 private Either<Boolean, ResponseFormat> validateResourceType(User user, Resource resource, AuditingActionEnum actionEnum) {
3831 Either<Boolean, ResponseFormat> eitherResult = Either.left(true);
3832 if (resource.getResourceType() == null) {
3833 log.debug("Invalid resource type for resource");
3834 ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT);
3835 eitherResult = Either.right(errorResponse);
3836 componentsUtils.auditResource(errorResponse, user, resource, actionEnum);
3838 return eitherResult;
3841 private Either<Boolean, ResponseFormat> validateLifecycleTypesCreate(User user, Resource resource, AuditingActionEnum actionEnum) {
3842 Either<Boolean, ResponseFormat> eitherResult = Either.left(true);
3843 if (resource.getInterfaces() != null && resource.getInterfaces().size() > 0) {
3844 log.debug("validate interface lifecycle Types Exist");
3845 Iterator<InterfaceDefinition> intItr = resource.getInterfaces().values().iterator();
3846 while (intItr.hasNext() && eitherResult.isLeft()) {
3847 InterfaceDefinition interfaceDefinition = intItr.next();
3848 String intType = interfaceDefinition.getUniqueId();
3849 Either<InterfaceDefinition, StorageOperationStatus> eitherCapTypeFound = interfaceTypeOperation.getInterface(intType);
3850 if (eitherCapTypeFound.isRight()) {
3851 if (eitherCapTypeFound.right().value() == StorageOperationStatus.NOT_FOUND) {
3852 BeEcompErrorManager.getInstance()
3853 .logBeGraphObjectMissingError("Create Resource - validateLifecycleTypesCreate", "Interface", intType);
3854 log.debug("Lifecycle Type: {} is required by resource: {} but does not exist in the DB", intType, resource.getName());
3855 BeEcompErrorManager.getInstance().logBeDaoSystemError("Create Resource - validateLifecycleTypesCreate");
3856 log.debug("request to data model failed with error: {}", eitherCapTypeFound.right().value().name());
3858 ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.MISSING_LIFECYCLE_TYPE, intType);
3859 eitherResult = Either.right(errorResponse);
3860 componentsUtils.auditResource(errorResponse, user, resource, actionEnum);
3864 return eitherResult;
3867 private Either<Boolean, ResponseFormat> validateCapabilityTypesCreate(User user, ICapabilityTypeOperation capabilityTypeOperation,
3868 Resource resource, AuditingActionEnum actionEnum, boolean inTransaction) {
3869 Either<Boolean, ResponseFormat> eitherResult = Either.left(true);
3870 if (resource.getCapabilities() != null && resource.getCapabilities().size() > 0) {
3871 log.debug("validate capability Types Exist - capabilities section");
3872 for (Entry<String, List<CapabilityDefinition>> typeEntry : resource.getCapabilities().entrySet()) {
3873 eitherResult = validateCapabilityTypeExists(user, capabilityTypeOperation, resource, actionEnum, eitherResult, typeEntry,
3875 if (eitherResult.isRight()) {
3876 return Either.right(eitherResult.right().value());
3880 if (resource.getRequirements() != null && resource.getRequirements().size() > 0) {
3881 log.debug("validate capability Types Exist - requirements section");
3882 for (String type : resource.getRequirements().keySet()) {
3883 eitherResult = validateCapabilityTypeExists(user, capabilityTypeOperation, resource, resource.getRequirements().get(type), actionEnum,
3884 eitherResult, type, inTransaction);
3885 if (eitherResult.isRight()) {
3886 return Either.right(eitherResult.right().value());
3890 return eitherResult;
3893 // @param typeObject- the object to which the validation is done
3894 private Either<Boolean, ResponseFormat> validateCapabilityTypeExists(User user, ICapabilityTypeOperation capabilityTypeOperation,
3895 Resource resource, List<?> validationObjects, AuditingActionEnum actionEnum,
3896 Either<Boolean, ResponseFormat> eitherResult, String type,
3897 boolean inTransaction) {
3898 Either<CapabilityTypeDefinition, StorageOperationStatus> eitherCapTypeFound = capabilityTypeOperation.getCapabilityType(UniqueIdBuilder.buildCapabilityTypeUid(resource.getModel(), type), inTransaction);
3899 if (eitherCapTypeFound.isRight()) {
3900 if (eitherCapTypeFound.right().value() == StorageOperationStatus.NOT_FOUND) {
3901 BeEcompErrorManager.getInstance().logBeGraphObjectMissingError(CREATE_RESOURCE_VALIDATE_CAPABILITY_TYPES, "Capability Type", type);
3902 log.debug("Capability Type: {} is required by resource: {} but does not exist in the DB", type, resource.getName());
3903 BeEcompErrorManager.getInstance().logBeDaoSystemError(CREATE_RESOURCE_VALIDATE_CAPABILITY_TYPES);
3905 log.debug("Trying to get capability type {} failed with error: {}", type, eitherCapTypeFound.right().value().name());
3906 ResponseFormat errorResponse = null;
3908 errorResponse = componentsUtils.getResponseFormat(ActionStatus.MISSING_CAPABILITY_TYPE, type);
3910 errorResponse = componentsUtils.getResponseFormatByElement(ActionStatus.MISSING_CAPABILITY_TYPE, validationObjects);
3912 eitherResult = Either.right(errorResponse);
3913 componentsUtils.auditResource(errorResponse, user, resource, actionEnum);
3915 return eitherResult;
3918 private Either<Boolean, ResponseFormat> validateCapabilityTypeExists(User user, ICapabilityTypeOperation capabilityTypeOperation,
3919 Resource resource, AuditingActionEnum actionEnum,
3920 Either<Boolean, ResponseFormat> eitherResult,
3921 Entry<String, List<CapabilityDefinition>> typeEntry, boolean inTransaction) {
3922 Either<CapabilityTypeDefinition, StorageOperationStatus> eitherCapTypeFound = capabilityTypeOperation
3923 .getCapabilityType(UniqueIdBuilder.buildCapabilityTypeUid(resource.getModel(), typeEntry.getKey()), inTransaction);
3924 if (eitherCapTypeFound.isRight()) {
3925 if (eitherCapTypeFound.right().value() == StorageOperationStatus.NOT_FOUND) {
3926 BeEcompErrorManager.getInstance()
3927 .logBeGraphObjectMissingError(CREATE_RESOURCE_VALIDATE_CAPABILITY_TYPES, "Capability Type", typeEntry.getKey());
3928 log.debug("Capability Type: {} is required by resource: {} but does not exist in the DB", typeEntry.getKey(), resource.getName());
3929 BeEcompErrorManager.getInstance().logBeDaoSystemError(CREATE_RESOURCE_VALIDATE_CAPABILITY_TYPES);
3931 log.debug("Trying to get capability type {} failed with error: {}", typeEntry.getKey(), eitherCapTypeFound.right().value().name());
3932 ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.MISSING_CAPABILITY_TYPE, typeEntry.getKey());
3933 componentsUtils.auditResource(errorResponse, user, resource, actionEnum);
3934 return Either.right(errorResponse);
3936 CapabilityTypeDefinition capabilityTypeDefinition = eitherCapTypeFound.left().value();
3937 if (capabilityTypeDefinition.getProperties() != null) {
3938 for (CapabilityDefinition capDef : typeEntry.getValue()) {
3939 List<ComponentInstanceProperty> properties = capDef.getProperties();
3940 List<ComponentInstanceProperty> changedProperties = new ArrayList<>();
3941 if (properties == null || properties.isEmpty()) {
3942 for (Entry<String, PropertyDefinition> prop : capabilityTypeDefinition.getProperties().entrySet()) {
3943 ComponentInstanceProperty newProp = new ComponentInstanceProperty(prop.getValue());
3944 changedProperties.add(newProp);
3947 List<ComponentInstanceProperty> propsToAdd = new ArrayList<>();
3948 for (Entry<String, PropertyDefinition> prop : capabilityTypeDefinition.getProperties().entrySet()) {
3949 PropertyDefinition propFromDef = prop.getValue();
3950 boolean propFound = false;
3951 for (ComponentInstanceProperty cip : properties) {
3952 if (propFromDef.getName().equals(cip.getName())) {
3953 //merge property value and property description only, ignore other fields
3954 if (cip.getDescription() != null && !cip.getDescription().equals(propFromDef.getDescription())) {
3955 propFromDef.setDescription(cip.getDescription());
3957 propertyDataValueMergeBusinessLogic.mergePropertyValue(propFromDef, cip, new ArrayList<>());
3958 if (cip.getValue() != null) {
3959 propFromDef.setValue(cip.getValue());
3961 propsToAdd.add(new ComponentInstanceProperty(propFromDef));
3963 properties.remove(cip);
3968 propsToAdd.add(new ComponentInstanceProperty(propFromDef));
3971 if (!propsToAdd.isEmpty()) {
3972 changedProperties.addAll(propsToAdd);
3975 capDef.setProperties(changedProperties);
3978 return eitherResult;
3981 public Resource createResourceByDao(Resource resource, User user, AuditingActionEnum actionEnum, boolean isNormative, boolean inTransaction) {
3984 // lock new resource name in order to avoid creation resource with same
3987 Resource createdResource = null;
3988 if (!inTransaction) {
3989 Either<Boolean, ResponseFormat> lockResult = lockComponentByName(resource.getSystemName(), resource, CREATE_RESOURCE);
3990 if (lockResult.isRight()) {
3991 ResponseFormat responseFormat = lockResult.right().value();
3992 componentsUtils.auditResource(responseFormat, user, resource, actionEnum);
3993 throw new ByResponseFormatComponentException(responseFormat);
3995 log.debug("name is locked {} status = {}", resource.getSystemName(), lockResult);
3998 if (resource.deriveFromGeneric()) {
3999 handleResourceGenericType(resource);
4001 createdResource = createResourceTransaction(resource, user, isNormative);
4002 componentsUtils.auditResource(componentsUtils.getResponseFormat(ActionStatus.CREATED), user, createdResource, actionEnum);
4003 ASDCKpiApi.countCreatedResourcesKPI();
4004 } catch (ComponentException e) {
4005 ResponseFormat responseFormat =
4006 e.getResponseFormat() == null ? componentsUtils.getResponseFormat(e.getActionStatus(), e.getParams()) : e.getResponseFormat();
4007 componentsUtils.auditResource(responseFormat, user, resource, actionEnum);
4009 } catch (StorageException e) {
4010 ResponseFormat responseFormat = componentsUtils
4011 .getResponseFormat(componentsUtils.convertFromStorageResponse(e.getStorageOperationStatus()));
4012 componentsUtils.auditResource(responseFormat, user, resource, actionEnum);
4015 if (!inTransaction) {
4016 graphLockOperation.unlockComponentByName(resource.getSystemName(), resource.getUniqueId(), NodeTypeEnum.Resource);
4019 return createdResource;
4022 private Resource createResourceTransaction(Resource resource, User user, boolean isNormative) {
4023 final String resourceName = resource.getName();
4024 final String modelName = resource.getModel();
4025 final ResourceTypeEnum resourceType = resource.getResourceType();
4026 final ComponentTypeEnum componentType = resource.getComponentType();
4027 final Either<Boolean, StorageOperationStatus> eitherValidation = toscaOperationFacade
4028 .validateComponentNameAndModelExists(resourceName, modelName, resourceType, componentType);
4029 if (eitherValidation.isRight()) {
4030 loggerSupportability.log(LoggerSupportabilityActions.VALIDATE_NAME, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
4031 "ERROR while validate component name {} Status is: {}", resource.getName(), eitherValidation.right().value());
4032 log.debug("Failed to validate component name {}. Status is {}. ", resource.getName(), eitherValidation.right().value());
4033 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(eitherValidation.right().value()));
4035 if (eitherValidation.left().value()) {
4036 log.debug("resource with name: {}, already exists", resource.getName());
4037 loggerSupportability
4038 .log(LoggerSupportabilityActions.CREATE_RESOURCE_FROM_YAML, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
4039 "resource with name: {} already exists", resource.getName());
4040 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_NAME_ALREADY_EXIST, ComponentTypeEnum.RESOURCE.getValue(),
4041 resource.getName());
4043 log.debug("send resource {} to dao for create", resource.getName());
4044 createArtifactsPlaceHolderData(resource, user);
4047 log.debug("enrich resource with creator, version and state");
4048 resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
4049 resource.setVersion(INITIAL_VERSION);
4050 resource.setHighestVersion(true);
4051 if (resource.getResourceType() != null && resource.getResourceType() != ResourceTypeEnum.CVFC) {
4052 resource.setAbstract(false);
4055 return toscaOperationFacade.createToscaComponent(resource).left().on(r -> throwComponentExceptionByResource(r, resource));
4058 private Resource throwComponentExceptionByResource(StorageOperationStatus status, Resource resource) {
4059 ResponseFormat responseFormat = componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(status), resource);
4060 throw new ByResponseFormatComponentException(responseFormat);
4063 private void createArtifactsPlaceHolderData(Resource resource, User user) {
4064 // create mandatory artifacts
4066 // TODO it must be removed after that artifact uniqueId creation will be
4068 // moved to ArtifactOperation
4069 setInformationalArtifactsPlaceHolder(resource, user);
4070 setDeploymentArtifactsPlaceHolder(resource, user);
4071 setToscaArtifactsPlaceHolders(resource, user);
4074 @SuppressWarnings("unchecked")
4076 public void setDeploymentArtifactsPlaceHolder(Component component, User user) {
4077 Resource resource = (Resource) component;
4078 Map<String, ArtifactDefinition> artifactMap = resource.getDeploymentArtifacts();
4079 if (artifactMap == null) {
4080 artifactMap = new HashMap<>();
4082 Map<String, Object> deploymentResourceArtifacts = ConfigurationManager.getConfigurationManager().getConfiguration()
4083 .getDeploymentResourceArtifacts();
4084 if (deploymentResourceArtifacts != null) {
4085 Map<String, ArtifactDefinition> finalArtifactMap = artifactMap;
4086 deploymentResourceArtifacts.forEach((k, v) -> processDeploymentResourceArtifacts(user, resource, finalArtifactMap, k, v));
4088 resource.setDeploymentArtifacts(artifactMap);
4091 private void processDeploymentResourceArtifacts(User user, Resource resource, Map<String, ArtifactDefinition> artifactMap, String k, Object v) {
4092 Map<String, Object> artifactDetails = (Map<String, Object>) v;
4093 Object object = artifactDetails.get(PLACE_HOLDER_RESOURCE_TYPES);
4094 if (object != null) {
4095 List<String> artifactTypes = (List<String>) object;
4096 if (!artifactTypes.contains(resource.getResourceType().name())) {
4100 log.info("resource types for artifact placeholder {} were not defined. default is all resources", k);
4102 if (artifactsBusinessLogic != null) {
4103 ArtifactDefinition artifactDefinition = artifactsBusinessLogic
4104 .createArtifactPlaceHolderInfo(resource.getUniqueId(), k, (Map<String, Object>) v, user, ArtifactGroupTypeEnum.DEPLOYMENT);
4105 if (artifactDefinition != null && !artifactMap.containsKey(artifactDefinition.getArtifactLabel())) {
4106 artifactMap.put(artifactDefinition.getArtifactLabel(), artifactDefinition);
4111 @SuppressWarnings("unchecked")
4112 private void setInformationalArtifactsPlaceHolder(Resource resource, User user) {
4113 Map<String, ArtifactDefinition> artifactMap = resource.getArtifacts();
4114 if (artifactMap == null) {
4115 artifactMap = new HashMap<>();
4117 String resourceUniqueId = resource.getUniqueId();
4118 List<String> exludeResourceCategory = ConfigurationManager.getConfigurationManager().getConfiguration().getExcludeResourceCategory();
4119 List<String> exludeResourceType = ConfigurationManager.getConfigurationManager().getConfiguration().getExcludeResourceType();
4120 Map<String, Object> informationalResourceArtifacts = ConfigurationManager.getConfigurationManager().getConfiguration()
4121 .getInformationalResourceArtifacts();
4122 List<CategoryDefinition> categories = resource.getCategories();
4123 boolean isCreateArtifact = true;
4124 if (exludeResourceCategory != null) {
4125 String category = categories.get(0).getName();
4126 isCreateArtifact = exludeResourceCategory.stream().noneMatch(e -> e.equalsIgnoreCase(category));
4128 if (isCreateArtifact && exludeResourceType != null) {
4129 String resourceType = resource.getResourceType().name();
4130 isCreateArtifact = exludeResourceType.stream().noneMatch(e -> e.equalsIgnoreCase(resourceType));
4132 if (informationalResourceArtifacts != null && isCreateArtifact) {
4133 Set<String> keys = informationalResourceArtifacts.keySet();
4134 for (String informationalResourceArtifactName : keys) {
4135 Map<String, Object> artifactInfoMap = (Map<String, Object>) informationalResourceArtifacts.get(informationalResourceArtifactName);
4136 ArtifactDefinition artifactDefinition = artifactsBusinessLogic
4137 .createArtifactPlaceHolderInfo(resourceUniqueId, informationalResourceArtifactName, artifactInfoMap, user,
4138 ArtifactGroupTypeEnum.INFORMATIONAL);
4139 artifactMap.put(artifactDefinition.getArtifactLabel(), artifactDefinition);
4142 resource.setArtifacts(artifactMap);
4152 public ResponseFormat deleteResource(String resourceId, User user) {
4153 ResponseFormat responseFormat;
4154 validateUserExists(user);
4155 Either<Resource, StorageOperationStatus> resourceStatus = toscaOperationFacade.getToscaElement(resourceId);
4156 if (resourceStatus.isRight()) {
4157 log.debug("failed to get resource {}", resourceId);
4158 return componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(resourceStatus.right().value()), "");
4160 Resource resource = resourceStatus.left().value();
4161 StorageOperationStatus result = StorageOperationStatus.OK;
4162 lockComponent(resourceId, resource, "Mark resource to delete");
4164 result = markComponentToDelete(resource);
4165 if (result == StorageOperationStatus.OK) {
4166 responseFormat = componentsUtils.getResponseFormat(ActionStatus.NO_CONTENT);
4168 ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(result);
4169 responseFormat = componentsUtils.getResponseFormatByResource(actionStatus, resource.getName());
4171 return responseFormat;
4173 if (!StorageOperationStatus.OK.equals(result)) {
4174 janusGraphDao.rollback();
4176 janusGraphDao.commit();
4178 graphLockOperation.unlockComponent(resourceId, NodeTypeEnum.Resource);
4182 public ResponseFormat deleteResourceByNameAndVersion(String resourceName, String version, User user) {
4183 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.NO_CONTENT);
4184 validateUserExists(user);
4185 Resource resource = null;
4186 StorageOperationStatus result = StorageOperationStatus.OK;
4187 boolean failed = false;
4189 Either<Resource, StorageOperationStatus> resourceStatus = toscaOperationFacade
4190 .getComponentByNameAndVersion(ComponentTypeEnum.RESOURCE, resourceName, version);
4191 if (resourceStatus.isRight()) {
4192 log.debug("failed to get resource {} version {}", resourceName, version);
4193 return componentsUtils
4194 .getResponseFormatByResource(componentsUtils.convertFromStorageResponse(resourceStatus.right().value()), resourceName);
4196 resource = resourceStatus.left().value();
4198 janusGraphDao.commit();
4200 if (resource != null) {
4201 lockComponent(resource.getUniqueId(), resource, DELETE_RESOURCE);
4203 result = markComponentToDelete(resource);
4204 if (result != StorageOperationStatus.OK) {
4205 ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(result);
4206 responseFormat = componentsUtils.getResponseFormatByResource(actionStatus, resource.getName());
4207 return responseFormat;
4209 } catch (ComponentException e) {
4213 if (failed || !StorageOperationStatus.OK.equals(result)) {
4214 janusGraphDao.rollback();
4216 janusGraphDao.commit();
4218 graphLockOperation.unlockComponent(resource.getUniqueId(), NodeTypeEnum.Resource);
4221 return responseFormat;
4224 public Either<Resource, ResponseFormat> getResource(String resourceId, User user) {
4226 validateUserExists(user);
4228 Either<Resource, StorageOperationStatus> storageStatus = toscaOperationFacade.getToscaElement(resourceId);
4229 if (storageStatus.isRight()) {
4230 log.debug("failed to get resource by id {}", resourceId);
4231 return Either.right(
4232 componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(storageStatus.right().value()), resourceId));
4234 if (storageStatus.left().value() == null) {
4235 return Either.right(componentsUtils
4236 .getResponseFormatByResource(componentsUtils.convertFromStorageResponse(StorageOperationStatus.NOT_FOUND), resourceId));
4238 return Either.left(storageStatus.left().value());
4241 public Either<Resource, ResponseFormat> getResourceByNameAndVersion(String resourceName, String resourceVersion, String userId) {
4242 validateUserExists(userId);
4243 Either<Resource, StorageOperationStatus> getResource = toscaOperationFacade
4244 .getComponentByNameAndVersion(ComponentTypeEnum.RESOURCE, resourceName, resourceVersion);
4245 if (getResource.isRight()) {
4246 log.debug("failed to get resource by name {} and version {}", resourceName, resourceVersion);
4247 return Either.right(
4248 componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(getResource.right().value()), resourceName));
4250 return Either.left(getResource.left().value());
4254 * updateResourceMetadata
4256 * @param user - modifier data (userId)
4257 * @param inTransaction TODO
4258 * @param resourceIdToUpdate - the resource identifier
4259 * @param newResource
4260 * @return Either<Resource, responseFormat>
4262 public Resource updateResourceMetadata(String resourceIdToUpdate, Resource newResource, Resource currentResource, User user,
4263 boolean inTransaction) {
4264 validateUserExists(user.getUserId());
4265 log.debug("Get resource with id {}", resourceIdToUpdate);
4266 boolean needToUnlock = false;
4268 if (currentResource == null) {
4269 Either<Resource, StorageOperationStatus> storageStatus = toscaOperationFacade.getToscaElement(resourceIdToUpdate);
4270 if (storageStatus.isRight()) {
4271 throw new ByResponseFormatComponentException(
4272 componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(storageStatus.right().value()), ""));
4274 currentResource = storageStatus.left().value();
4276 // verify that resource is checked-out and the user is the last
4279 if (!ComponentValidationUtils.canWorkOnResource(currentResource, user.getUserId())) {
4280 throw new ByResponseFormatComponentException(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
4283 StorageOperationStatus lockResult = graphLockOperation.lockComponent(resourceIdToUpdate, NodeTypeEnum.Resource);
4284 if (lockResult != StorageOperationStatus.OK) {
4285 BeEcompErrorManager.getInstance()
4286 .logBeFailedLockObjectError("Upload Artifact - lock ", NodeTypeEnum.Resource.getName(), resourceIdToUpdate);
4287 log.debug("Failed to lock resource: {}, error - {}", resourceIdToUpdate, lockResult);
4288 ResponseFormat responseFormat = componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(lockResult));
4289 throw new ByResponseFormatComponentException(responseFormat);
4291 needToUnlock = true;
4292 // critical section starts here
4294 // convert json to object
4296 // Update and updated resource must have a non-empty "derivedFrom"
4300 // This code is not called from import resources, because of root
4302 // VF "derivedFrom" should be null (or ignored)
4303 if (ModelConverter.isAtomicComponent(currentResource)) {
4304 validateDerivedFromNotEmpty(null, newResource, null);
4305 validateDerivedFromNotEmpty(null, currentResource, null);
4307 newResource.setDerivedFrom(null);
4309 Either<Resource, ResponseFormat> dataModelResponse = updateResourceMetadata(resourceIdToUpdate, newResource, user, currentResource, false,
4311 if (dataModelResponse.isRight()) {
4312 log.debug("failed to update resource metadata!!!");
4313 throw new ByResponseFormatComponentException(dataModelResponse.right().value());
4315 log.debug("Resource metadata updated successfully!!!");
4316 return dataModelResponse.left().value();
4317 } catch (ComponentException | StorageException e) {
4318 rollback(inTransaction, newResource, null, null);
4321 if (!inTransaction) {
4322 janusGraphDao.commit();
4325 graphLockOperation.unlockComponent(resourceIdToUpdate, NodeTypeEnum.Resource);
4330 private Either<Resource, ResponseFormat> updateResourceMetadata(String resourceIdToUpdate, Resource newResource, User user,
4331 Resource currentResource, boolean shouldLock, boolean inTransaction) {
4332 updateVfModuleGroupsNames(currentResource, newResource);
4333 validateResourceFieldsBeforeUpdate(currentResource, newResource, inTransaction, false);
4334 // Setting last updater and uniqueId
4335 newResource.setContactId(newResource.getContactId().toLowerCase());
4336 newResource.setLastUpdaterUserId(user.getUserId());
4337 newResource.setUniqueId(resourceIdToUpdate);
4338 // Cannot set highest version through UI
4339 newResource.setHighestVersion(currentResource.isHighestVersion());
4340 newResource.setCreationDate(currentResource.getCreationDate());
4341 Either<Boolean, ResponseFormat> processUpdateOfDerivedFrom = processUpdateOfDerivedFrom(currentResource, newResource, user.getUserId(),
4343 if (processUpdateOfDerivedFrom.isRight()) {
4344 log.debug("Couldn't update derived from for resource {}", resourceIdToUpdate);
4345 return Either.right(processUpdateOfDerivedFrom.right().value());
4347 log.debug("send resource {} to dao for update", newResource.getUniqueId());
4348 if (isNotEmpty(newResource.getGroups())) {
4349 for (GroupDefinition group : newResource.getGroups()) {
4350 if (DEFAULT_GROUP_VF_MODULE.equals(group.getType())) {
4352 .validateAndUpdateGroupMetadata(newResource.getComponentMetadataDefinition().getMetadataDataDefinition().getUniqueId(), user,
4353 newResource.getComponentType(), group, true, false);
4357 Either<Resource, StorageOperationStatus> dataModelResponse = toscaOperationFacade.updateToscaElement(newResource);
4358 if (dataModelResponse.isRight()) {
4359 ResponseFormat responseFormat = componentsUtils
4360 .getResponseFormatByResource(componentsUtils.convertFromStorageResponse(dataModelResponse.right().value()), newResource);
4361 return Either.right(responseFormat);
4362 } else if (dataModelResponse.left().value() == null) {
4363 log.debug("No response from updateResource");
4364 return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
4366 return Either.left(dataModelResponse.left().value());
4369 private void updateVfModuleGroupsNames(Resource currentResource, Resource newResource) {
4370 if (currentResource.getGroups() != null && !currentResource.getName().equals(newResource.getName())) {
4371 List<GroupDefinition> updatedGroups = currentResource.getGroups().stream()
4372 .map(group -> getUpdatedGroup(group, currentResource.getName(), newResource.getName())).collect(toList());
4373 newResource.setGroups(updatedGroups);
4377 private GroupDefinition getUpdatedGroup(GroupDefinition currGroup, String replacePattern, String with) {
4378 GroupDefinition updatedGroup = new GroupDefinition(currGroup);
4379 if (updatedGroup.isSamePrefix(replacePattern) && updatedGroup.getType().equals(DEFAULT_GROUP_VF_MODULE)) {
4380 String prefix = updatedGroup.getName().substring(0, replacePattern.length());
4381 String newGroupName = updatedGroup.getName().replaceFirst(prefix, with);
4382 updatedGroup.setName(newGroupName);
4384 return updatedGroup;
4388 * validateResourceFieldsBeforeCreate
4390 * @param user - modifier data (userId)
4392 private void validateResourceFieldsBeforeCreate(User user, Resource resource, AuditingActionEnum actionEnum, boolean inTransaction) {
4393 componentValidator.validate(user, resource, actionEnum);
4394 // validate category
4395 log.debug("validate category");
4396 validateCategory(user, resource, actionEnum, inTransaction);
4397 // validate vendor name & release & model number
4398 log.debug("validate vendor name");
4399 validateVendorName(user, resource, actionEnum);
4400 log.debug("validate vendor release");
4401 validateVendorReleaseName(user, resource, actionEnum);
4402 log.debug("validate resource vendor model number");
4403 validateResourceVendorModelNumber(user, resource, actionEnum);
4405 log.debug("validate cost");
4406 validateCost(resource);
4407 // validate licenseType
4408 log.debug("validate licenseType");
4409 validateLicenseType(user, resource, actionEnum);
4410 // validate template (derived from)
4411 log.debug("validate derived from");
4412 if (!ModelConverter.isAtomicComponent(resource) && resource.getResourceType() != ResourceTypeEnum.CVFC) {
4413 resource.setDerivedFrom(null);
4415 validateDerivedFromExist(user, resource, actionEnum);
4416 // warn about non-updatable fields
4417 checkComponentFieldsForOverrideAttempt(resource);
4418 String currentCreatorFullName = resource.getCreatorFullName();
4419 if (currentCreatorFullName != null) {
4420 log.debug("Resource Creator fullname is automatically set and cannot be updated");
4422 String currentLastUpdaterFullName = resource.getLastUpdaterFullName();
4423 if (currentLastUpdaterFullName != null) {
4424 log.debug("Resource LastUpdater fullname is automatically set and cannot be updated");
4426 Long currentLastUpdateDate = resource.getLastUpdateDate();
4427 if (currentLastUpdateDate != null) {
4428 log.debug("Resource last update date is automatically set and cannot be updated");
4430 Boolean currentAbstract = resource.isAbstract();
4431 if (currentAbstract != null) {
4432 log.debug("Resource abstract is automatically set and cannot be updated");
4437 * validateResourceFieldsBeforeUpdate
4439 * @param currentResource - Resource object to validate
4442 private void validateResourceFieldsBeforeUpdate(Resource currentResource, Resource updateInfoResource, boolean inTransaction, boolean isNested) {
4443 validateFields(currentResource, updateInfoResource, inTransaction, isNested);
4444 warnNonEditableFields(currentResource, updateInfoResource);
4447 private void warnNonEditableFields(Resource currentResource, Resource updateInfoResource) {
4448 String currentResourceVersion = currentResource.getVersion();
4449 String updatedResourceVersion = updateInfoResource.getVersion();
4450 if ((updatedResourceVersion != null) && (!updatedResourceVersion.equals(currentResourceVersion))) {
4451 log.debug("Resource version is automatically set and cannot be updated");
4453 String currentCreatorUserId = currentResource.getCreatorUserId();
4454 String updatedCreatorUserId = updateInfoResource.getCreatorUserId();
4455 if ((updatedCreatorUserId != null) && (!updatedCreatorUserId.equals(currentCreatorUserId))) {
4456 log.debug("Resource Creator UserId is automatically set and cannot be updated");
4458 String currentCreatorFullName = currentResource.getCreatorFullName();
4459 String updatedCreatorFullName = updateInfoResource.getCreatorFullName();
4460 if ((updatedCreatorFullName != null) && (!updatedCreatorFullName.equals(currentCreatorFullName))) {
4461 log.debug("Resource Creator fullname is automatically set and cannot be updated");
4463 String currentLastUpdaterUserId = currentResource.getLastUpdaterUserId();
4464 String updatedLastUpdaterUserId = updateInfoResource.getLastUpdaterUserId();
4465 if ((updatedLastUpdaterUserId != null) && (!updatedLastUpdaterUserId.equals(currentLastUpdaterUserId))) {
4466 log.debug("Resource LastUpdater userId is automatically set and cannot be updated");
4468 String currentLastUpdaterFullName = currentResource.getLastUpdaterFullName();
4469 String updatedLastUpdaterFullName = updateInfoResource.getLastUpdaterFullName();
4470 if ((updatedLastUpdaterFullName != null) && (!updatedLastUpdaterFullName.equals(currentLastUpdaterFullName))) {
4471 log.debug("Resource LastUpdater fullname is automatically set and cannot be updated");
4473 Long currentCreationDate = currentResource.getCreationDate();
4474 Long updatedCreationDate = updateInfoResource.getCreationDate();
4475 if ((updatedCreationDate != null) && (!updatedCreationDate.equals(currentCreationDate))) {
4476 log.debug("Resource Creation date is automatically set and cannot be updated");
4478 Long currentLastUpdateDate = currentResource.getLastUpdateDate();
4479 Long updatedLastUpdateDate = updateInfoResource.getLastUpdateDate();
4480 if ((updatedLastUpdateDate != null) && (!updatedLastUpdateDate.equals(currentLastUpdateDate))) {
4481 log.debug("Resource last update date is automatically set and cannot be updated");
4483 LifecycleStateEnum currentLifecycleState = currentResource.getLifecycleState();
4484 LifecycleStateEnum updatedLifecycleState = updateInfoResource.getLifecycleState();
4485 if ((updatedLifecycleState != null) && (!updatedLifecycleState.equals(currentLifecycleState))) {
4486 log.debug("Resource lifecycle state date is automatically set and cannot be updated");
4488 Boolean currentAbstract = currentResource.isAbstract();
4489 Boolean updatedAbstract = updateInfoResource.isAbstract();
4490 if ((updatedAbstract != null) && (!updatedAbstract.equals(currentAbstract))) {
4491 log.debug("Resource abstract is automatically set and cannot be updated");
4493 Boolean currentHighestVersion = currentResource.isHighestVersion();
4494 Boolean updatedHighestVersion = updateInfoResource.isHighestVersion();
4495 if ((updatedHighestVersion != null) && (!updatedHighestVersion.equals(currentHighestVersion))) {
4496 log.debug("Resource highest version is automatically set and cannot be updated");
4498 String currentUuid = currentResource.getUUID();
4499 String updatedUuid = updateInfoResource.getUUID();
4500 if ((updatedUuid != null) && (!updatedUuid.equals(currentUuid))) {
4501 log.debug("Resource UUID is automatically set and cannot be updated");
4503 log.debug("Resource Type cannot be updated");
4504 String currentInvariantUuid = currentResource.getInvariantUUID();
4505 String updatedInvariantUuid = updateInfoResource.getInvariantUUID();
4506 if ((updatedInvariantUuid != null) && (!updatedInvariantUuid.equals(currentInvariantUuid))) {
4507 log.debug("Resource invariant UUID is automatically set and cannot be updated");
4508 updateInfoResource.setInvariantUUID(currentInvariantUuid);
4512 private void validateFields(Resource currentResource, Resource updateInfoResource, boolean inTransaction, boolean isNested) {
4513 boolean hasBeenCertified = ValidationUtils.hasBeenCertified(currentResource.getVersion());
4514 log.debug("validate resource name before update");
4515 validateResourceName(currentResource, updateInfoResource, hasBeenCertified, isNested);
4516 log.debug("validate description before update");
4517 componentDescriptionValidator.validateAndCorrectField(null, updateInfoResource, null);
4518 log.debug("validate icon before update");
4519 validateIcon(currentResource, updateInfoResource, hasBeenCertified);
4520 log.debug("validate tags before update");
4521 componentTagsValidator.validateAndCorrectField(null, updateInfoResource, null);
4522 log.debug("validate vendor name before update");
4523 validateVendorName(null, updateInfoResource, null);
4524 log.debug("validate resource vendor model number before update");
4525 validateResourceVendorModelNumber(currentResource, updateInfoResource);
4526 log.debug("validate vendor release before update");
4527 validateVendorReleaseName(null, updateInfoResource, null);
4528 log.debug("validate contact info before update");
4529 componentContactIdValidator.validateAndCorrectField(null, updateInfoResource, null);
4530 log.debug(VALIDATE_DERIVED_BEFORE_UPDATE);
4531 validateDerivedFromDuringUpdate(currentResource, updateInfoResource, hasBeenCertified);
4532 log.debug("validate category before update");
4533 validateCategory(currentResource, updateInfoResource, hasBeenCertified, inTransaction);
4536 private boolean isResourceNameEquals(Resource currentResource, Resource updateInfoResource) {
4537 String resourceNameUpdated = updateInfoResource.getName();
4538 String resourceNameCurrent = currentResource.getName();
4539 if (resourceNameCurrent.equals(resourceNameUpdated)) {
4542 // In case of CVFC type we should support the case of old VF with CVFC
4544 // instances that were created without the "Cvfc" suffix
4545 return currentResource.getResourceType() == ResourceTypeEnum.CVFC && resourceNameUpdated
4546 .equals(addCvfcSuffixToResourceName(resourceNameCurrent));
4549 private String addCvfcSuffixToResourceName(String resourceName) {
4550 return resourceName + "Cvfc";
4553 private void validateResourceName(Resource currentResource, Resource updateInfoResource, boolean hasBeenCertified, boolean isNested) {
4554 String resourceNameUpdated = updateInfoResource.getName();
4555 if (!isResourceNameEquals(currentResource, updateInfoResource)) {
4556 if (isNested || !hasBeenCertified) {
4557 componentNameValidator.validateAndCorrectField(null, updateInfoResource, null);
4558 validateResourceNameUniqueness(updateInfoResource);
4559 currentResource.setName(resourceNameUpdated);
4560 currentResource.setNormalizedName(ValidationUtils.normaliseComponentName(resourceNameUpdated));
4561 currentResource.setSystemName(ValidationUtils.convertToSystemName(resourceNameUpdated));
4563 log.info("Resource name: {}, cannot be updated once the resource has been certified once.", resourceNameUpdated);
4564 throw new ByActionStatusComponentException(ActionStatus.RESOURCE_NAME_CANNOT_BE_CHANGED);
4569 private void validateIcon(Resource currentResource, Resource updateInfoResource, boolean hasBeenCertified) {
4570 String iconUpdated = updateInfoResource.getIcon();
4571 String iconCurrent = currentResource.getIcon();
4572 if (!iconCurrent.equals(iconUpdated)) {
4573 if (!hasBeenCertified) {
4574 componentIconValidator.validateAndCorrectField(null, updateInfoResource, null);
4576 log.info("Icon {} cannot be updated once the resource has been certified once.", iconUpdated);
4577 throw new ByActionStatusComponentException(ActionStatus.RESOURCE_ICON_CANNOT_BE_CHANGED);
4582 private void validateResourceVendorModelNumber(Resource currentResource, Resource updateInfoResource) {
4583 String updatedResourceVendorModelNumber = updateInfoResource.getResourceVendorModelNumber();
4584 String currentResourceVendorModelNumber = currentResource.getResourceVendorModelNumber();
4585 if (!currentResourceVendorModelNumber.equals(updatedResourceVendorModelNumber)) {
4586 validateResourceVendorModelNumber(null, updateInfoResource, null);
4590 private Either<Boolean, ResponseFormat> validateCategory(Resource currentResource, Resource updateInfoResource, boolean hasBeenCertified,
4591 boolean inTransaction) {
4592 validateCategory(null, updateInfoResource, null, inTransaction);
4593 if (hasBeenCertified) {
4594 CategoryDefinition currentCategory = currentResource.getCategories().get(0);
4595 SubCategoryDefinition currentSubCategory = currentCategory.getSubcategories().get(0);
4596 CategoryDefinition updateCategory = updateInfoResource.getCategories().get(0);
4597 SubCategoryDefinition updtaeSubCategory = updateCategory.getSubcategories().get(0);
4598 if (!currentCategory.getName().equals(updateCategory.getName()) || !currentSubCategory.getName().equals(updtaeSubCategory.getName())) {
4599 log.info("Category {} cannot be updated once the resource has been certified once.", currentResource.getCategories());
4600 ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.RESOURCE_CATEGORY_CANNOT_BE_CHANGED);
4601 return Either.right(errorResponse);
4604 return Either.left(true);
4607 private Either<Boolean, ResponseFormat> validateDerivedFromDuringUpdate(Resource currentResource, Resource updateInfoResource,
4608 boolean hasBeenCertified) {
4609 List<String> currentDerivedFrom = currentResource.getDerivedFrom();
4610 List<String> updatedDerivedFrom = updateInfoResource.getDerivedFrom();
4611 if (currentDerivedFrom == null || currentDerivedFrom.isEmpty() || updatedDerivedFrom == null || updatedDerivedFrom.isEmpty()) {
4612 log.trace("Update normative types");
4613 return Either.left(true);
4615 String derivedFromCurrent = currentDerivedFrom.get(0);
4616 String derivedFromUpdated = updatedDerivedFrom.get(0);
4617 if (!derivedFromCurrent.equals(derivedFromUpdated)) {
4618 if (!hasBeenCertified) {
4619 validateDerivedFromExist(null, updateInfoResource, null);
4621 Either<Boolean, ResponseFormat> validateDerivedFromExtending = validateDerivedFromExtending(null, currentResource, updateInfoResource,
4623 if (validateDerivedFromExtending.isRight() || !validateDerivedFromExtending.left().value()) {
4624 log.debug("Derived from cannot be updated if it doesnt inherits directly or extends inheritance");
4625 return validateDerivedFromExtending;
4629 // For derived from, we must know whether it was actually changed,
4631 // otherwise we must do no action.
4633 // Due to changes it inflicts on data model (remove artifacts,
4635 // properties...), it's not like a flat field which can be
4637 // overwritten if not changed.
4639 // So we must indicate that derived from is not changed
4640 updateInfoResource.setDerivedFrom(null);
4642 return Either.left(true);
4645 private Either<Boolean, ResponseFormat> validateNestedDerivedFromDuringUpdate(Resource currentResource, Resource updateInfoResource,
4646 boolean hasBeenCertified) {
4647 List<String> currentDerivedFrom = currentResource.getDerivedFrom();
4648 List<String> updatedDerivedFrom = updateInfoResource.getDerivedFrom();
4649 if (currentDerivedFrom == null || currentDerivedFrom.isEmpty() || updatedDerivedFrom == null || updatedDerivedFrom.isEmpty()) {
4650 log.trace("Update normative types");
4651 return Either.left(true);
4653 String derivedFromCurrent = currentDerivedFrom.get(0);
4654 String derivedFromUpdated = updatedDerivedFrom.get(0);
4655 if (!derivedFromCurrent.equals(derivedFromUpdated)) {
4656 if (!hasBeenCertified) {
4657 validateDerivedFromExist(null, updateInfoResource, null);
4659 Either<Boolean, ResponseFormat> validateDerivedFromExtending = validateDerivedFromExtending(null, currentResource, updateInfoResource,
4661 if (validateDerivedFromExtending.isRight() || !validateDerivedFromExtending.left().value()) {
4662 log.debug("Derived from cannot be updated if it doesnt inherits directly or extends inheritance");
4663 return validateDerivedFromExtending;
4667 return Either.left(true);
4670 private void validateDerivedFromExist(User user, Resource resource, AuditingActionEnum actionEnum) {
4671 if (resource.getDerivedFrom() == null || resource.getDerivedFrom().isEmpty()) {
4674 String templateName = resource.getDerivedFrom().get(0);
4675 Either<Boolean, StorageOperationStatus> dataModelResponse = toscaOperationFacade.validateToscaResourceNameExists(templateName);
4676 if (dataModelResponse.isRight()) {
4677 StorageOperationStatus storageStatus = dataModelResponse.right().value();
4678 BeEcompErrorManager.getInstance().logBeDaoSystemError("Create Resource - validateDerivedFromExist");
4679 log.debug("request to data model failed with error: {}", storageStatus);
4680 ResponseFormat responseFormat = componentsUtils
4681 .getResponseFormatByResource(componentsUtils.convertFromStorageResponse(storageStatus), resource);
4682 log.trace("audit before sending response");
4683 componentsUtils.auditResource(responseFormat, user, resource, actionEnum);
4684 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(storageStatus));
4685 } else if (!dataModelResponse.left().value()) {
4686 log.info("resource template with name: {}, does not exists", templateName);
4687 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.PARENT_RESOURCE_NOT_FOUND);
4688 componentsUtils.auditResource(responseFormat, user, resource, actionEnum);
4689 throw new ByActionStatusComponentException(ActionStatus.PARENT_RESOURCE_NOT_FOUND);
4693 // Tal G for extending inheritance US815447
4694 private Either<Boolean, ResponseFormat> validateDerivedFromExtending(User user, Resource currentResource, Resource updateInfoResource,
4695 AuditingActionEnum actionEnum) {
4696 String currentTemplateName = currentResource.getDerivedFrom().get(0);
4697 String updatedTemplateName = updateInfoResource.getDerivedFrom().get(0);
4698 Either<Boolean, StorageOperationStatus> dataModelResponse = toscaOperationFacade
4699 .validateToscaResourceNameExtends(currentTemplateName, updatedTemplateName, currentResource.getModel());
4700 if (dataModelResponse.isRight()) {
4701 StorageOperationStatus storageStatus = dataModelResponse.right().value();
4702 BeEcompErrorManager.getInstance().logBeDaoSystemError("Create/Update Resource - validateDerivingFromExtendingType");
4703 ResponseFormat responseFormat = componentsUtils
4704 .getResponseFormatByResource(componentsUtils.convertFromStorageResponse(storageStatus), currentResource);
4705 log.trace("audit before sending response");
4706 componentsUtils.auditResource(responseFormat, user, currentResource, actionEnum);
4707 return Either.right(responseFormat);
4709 if (!dataModelResponse.left().value()) {
4710 log.info("resource template with name {} does not inherit as original {}", updatedTemplateName, currentTemplateName);
4711 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.PARENT_RESOURCE_DOES_NOT_EXTEND);
4712 componentsUtils.auditResource(responseFormat, user, currentResource, actionEnum);
4713 return Either.right(responseFormat);
4715 return Either.left(true);
4718 public void validateDerivedFromNotEmpty(User user, Resource resource, AuditingActionEnum actionEnum) {
4719 log.debug("validate resource derivedFrom field");
4720 if ((resource.getDerivedFrom() == null) || (resource.getDerivedFrom().isEmpty()) || (resource.getDerivedFrom().get(0)) == null || (resource
4721 .getDerivedFrom().get(0).trim().isEmpty())) {
4722 log.info("derived from (template) field is missing for the resource");
4723 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.MISSING_DERIVED_FROM_TEMPLATE);
4724 componentsUtils.auditResource(responseFormat, user, resource, actionEnum);
4725 throw new ByActionStatusComponentException(ActionStatus.MISSING_DERIVED_FROM_TEMPLATE);
4729 private void validateResourceNameUniqueness(Resource resource) {
4730 Either<Boolean, StorageOperationStatus> resourceOperationResponse = toscaOperationFacade
4731 .validateComponentNameExists(resource.getName(), resource.getResourceType(), resource.getComponentType());
4732 if (resourceOperationResponse.isLeft() && resourceOperationResponse.left().value()) {
4733 log.debug("resource with name: {}, already exists", resource.getName());
4734 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_NAME_ALREADY_EXIST, ComponentTypeEnum.RESOURCE.getValue(),
4735 resource.getName());
4736 } else if (resourceOperationResponse.isRight()) {
4737 log.debug("error while validateResourceNameExists for resource: {}", resource.getName());
4738 throw new StorageException(resourceOperationResponse.right().value());
4742 private void validateCategory(User user, Resource resource, AuditingActionEnum actionEnum, boolean inTransaction) {
4743 List<CategoryDefinition> categories = resource.getCategories();
4744 if (CollectionUtils.isEmpty(categories)) {
4745 log.debug(CATEGORY_IS_EMPTY);
4746 ResponseFormat responseFormat = componentsUtils
4747 .getResponseFormat(ActionStatus.COMPONENT_MISSING_CATEGORY, ComponentTypeEnum.RESOURCE.getValue());
4748 componentsUtils.auditResource(responseFormat, user, resource, actionEnum);
4749 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_MISSING_CATEGORY, ComponentTypeEnum.RESOURCE.getValue());
4751 if (categories.size() > 1) {
4752 log.debug("Must be only one category for resource");
4753 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_TOO_MUCH_CATEGORIES, ComponentTypeEnum.RESOURCE.getValue());
4755 CategoryDefinition category = categories.get(0);
4756 List<SubCategoryDefinition> subcategories = category.getSubcategories();
4757 if (CollectionUtils.isEmpty(subcategories)) {
4758 log.debug("Missinig subcategory for resource");
4759 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_MISSING_SUBCATEGORY);
4761 if (subcategories.size() > 1) {
4762 log.debug("Must be only one sub category for resource");
4763 throw new ByActionStatusComponentException(ActionStatus.RESOURCE_TOO_MUCH_SUBCATEGORIES);
4765 SubCategoryDefinition subcategory = subcategories.get(0);
4766 if (!ValidationUtils.validateStringNotEmpty(category.getName())) {
4767 log.debug(CATEGORY_IS_EMPTY);
4768 ResponseFormat responseFormat = componentsUtils
4769 .getResponseFormat(ActionStatus.COMPONENT_MISSING_CATEGORY, ComponentTypeEnum.RESOURCE.getValue());
4770 componentsUtils.auditResource(responseFormat, user, resource, actionEnum);
4771 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_MISSING_CATEGORY, ComponentTypeEnum.RESOURCE.getValue());
4773 if (!ValidationUtils.validateStringNotEmpty(subcategory.getName())) {
4774 log.debug(CATEGORY_IS_EMPTY);
4775 ResponseFormat responseFormat = componentsUtils
4776 .getResponseFormat(ActionStatus.COMPONENT_MISSING_SUBCATEGORY, ComponentTypeEnum.RESOURCE.getValue());
4777 componentsUtils.auditResource(responseFormat, user, resource, actionEnum);
4778 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_MISSING_SUBCATEGORY, ComponentTypeEnum.RESOURCE.getValue());
4780 validateCategoryListed(category, subcategory, user, resource, actionEnum, inTransaction);
4783 private void validateCategoryListed(CategoryDefinition category, SubCategoryDefinition subcategory, User user, Resource resource,
4784 AuditingActionEnum actionEnum, boolean inTransaction) {
4785 ResponseFormat responseFormat;
4786 if (category != null && subcategory != null) {
4787 log.debug("validating resource category {} against valid categories list", category);
4788 Either<List<CategoryDefinition>, ActionStatus> categories = elementDao.getAllCategories(NodeTypeEnum.ResourceNewCategory, inTransaction);
4789 if (categories.isRight()) {
4790 log.debug("failed to retrieve resource categories from JanusGraph");
4791 responseFormat = componentsUtils.getResponseFormat(categories.right().value());
4792 componentsUtils.auditResource(responseFormat, user, resource, actionEnum);
4793 throw new ByActionStatusComponentException(categories.right().value());
4795 List<CategoryDefinition> categoryList = categories.left().value();
4796 Optional<CategoryDefinition> foundCategory = categoryList.stream().filter(cat -> cat.getName().equals(category.getName())).findFirst();
4797 if (foundCategory.isEmpty()) {
4798 log.debug("Category {} is not part of resource category group. Resource category valid values are {}", category, categoryList);
4799 failOnInvalidCategory(user, resource, actionEnum);
4800 return; // explisite output even if failOnInvalidCategory throw an exception
4802 Optional<SubCategoryDefinition> foundSubcategory = foundCategory.get().getSubcategories().stream()
4803 .filter(subcat -> subcat.getName().equals(subcategory.getName())).findFirst();
4804 if (foundSubcategory.isEmpty()) {
4805 log.debug("SubCategory {} is not part of resource category group. Resource subcategory valid values are {}", subcategory,
4806 foundCategory.get().getSubcategories());
4807 failOnInvalidCategory(user, resource, actionEnum);
4812 private void failOnInvalidCategory(User user, Resource resource, AuditingActionEnum actionEnum) {
4813 ResponseFormat responseFormat;
4814 responseFormat = componentsUtils.getResponseFormat(ActionStatus.COMPONENT_INVALID_CATEGORY, ComponentTypeEnum.RESOURCE.getValue());
4815 componentsUtils.auditResource(responseFormat, user, resource, actionEnum);
4816 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_INVALID_CATEGORY, ComponentTypeEnum.RESOURCE.getValue());
4819 public void validateVendorReleaseName(User user, Resource resource, AuditingActionEnum actionEnum) {
4820 String vendorRelease = resource.getVendorRelease();
4821 log.debug("validate vendor relese name");
4822 if (!ValidationUtils.validateStringNotEmpty(vendorRelease)) {
4823 log.info("vendor relese name is missing.");
4824 ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.MISSING_VENDOR_RELEASE);
4825 componentsUtils.auditResource(errorResponse, user, resource, actionEnum);
4826 throw new ByActionStatusComponentException(ActionStatus.MISSING_VENDOR_RELEASE);
4828 validateVendorReleaseName(vendorRelease, user, resource, actionEnum);
4831 public void validateVendorReleaseName(String vendorRelease, User user, Resource resource, AuditingActionEnum actionEnum) {
4832 if (vendorRelease != null) {
4833 if (!ValidationUtils.validateVendorReleaseLength(vendorRelease)) {
4834 log.info("vendor release exceds limit.");
4835 ResponseFormat errorResponse = componentsUtils
4836 .getResponseFormat(ActionStatus.VENDOR_RELEASE_EXCEEDS_LIMIT, "" + ValidationUtils.VENDOR_RELEASE_MAX_LENGTH);
4837 componentsUtils.auditResource(errorResponse, user, resource, actionEnum);
4838 throw new ByActionStatusComponentException(ActionStatus.VENDOR_RELEASE_EXCEEDS_LIMIT, "" + ValidationUtils.VENDOR_RELEASE_MAX_LENGTH);
4840 if (!ValidationUtils.validateVendorRelease(vendorRelease)) {
4841 log.info("vendor release is not valid.");
4842 ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.INVALID_VENDOR_RELEASE);
4843 componentsUtils.auditResource(errorResponse, user, resource, actionEnum);
4844 throw new ByActionStatusComponentException(ActionStatus.INVALID_VENDOR_RELEASE, vendorRelease);
4849 private void validateVendorName(User user, Resource resource, AuditingActionEnum actionEnum) {
4850 String vendorName = resource.getVendorName();
4851 if (!ValidationUtils.validateStringNotEmpty(vendorName)) {
4852 log.info("vendor name is missing.");
4853 ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.MISSING_VENDOR_NAME);
4854 componentsUtils.auditResource(errorResponse, user, resource, actionEnum);
4855 throw new ByActionStatusComponentException(ActionStatus.MISSING_VENDOR_NAME);
4857 validateVendorName(vendorName, user, resource, actionEnum);
4860 private void validateVendorName(String vendorName, User user, Resource resource, AuditingActionEnum actionEnum) {
4861 if (vendorName != null) {
4862 if (!ValidationUtils.validateVendorNameLength(vendorName)) {
4863 log.info("vendor name exceds limit.");
4864 ResponseFormat errorResponse = componentsUtils
4865 .getResponseFormat(ActionStatus.VENDOR_NAME_EXCEEDS_LIMIT, "" + ValidationUtils.VENDOR_NAME_MAX_LENGTH);
4866 componentsUtils.auditResource(errorResponse, user, resource, actionEnum);
4867 throw new ByActionStatusComponentException(ActionStatus.VENDOR_NAME_EXCEEDS_LIMIT, "" + ValidationUtils.VENDOR_NAME_MAX_LENGTH);
4869 if (!ValidationUtils.validateVendorName(vendorName)) {
4870 log.info("vendor name is not valid.");
4871 ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.INVALID_VENDOR_NAME);
4872 componentsUtils.auditResource(errorResponse, user, resource, actionEnum);
4873 throw new ByActionStatusComponentException(ActionStatus.INVALID_VENDOR_NAME, vendorName);
4878 private void validateResourceVendorModelNumber(User user, Resource resource, AuditingActionEnum actionEnum) {
4879 String resourceVendorModelNumber = resource.getResourceVendorModelNumber();
4880 if (StringUtils.isNotEmpty(resourceVendorModelNumber)) {
4881 if (!ValidationUtils.validateResourceVendorModelNumberLength(resourceVendorModelNumber)) {
4882 log.info("resource vendor model number exceeds limit.");
4883 ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.RESOURCE_VENDOR_MODEL_NUMBER_EXCEEDS_LIMIT,
4884 "" + ValidationUtils.RESOURCE_VENDOR_MODEL_NUMBER_MAX_LENGTH);
4885 componentsUtils.auditResource(errorResponse, user, resource, actionEnum);
4886 throw new ByActionStatusComponentException(ActionStatus.RESOURCE_VENDOR_MODEL_NUMBER_EXCEEDS_LIMIT,
4887 "" + ValidationUtils.RESOURCE_VENDOR_MODEL_NUMBER_MAX_LENGTH);
4889 // resource vendor model number is currently validated as vendor
4892 if (!ValidationUtils.validateVendorName(resourceVendorModelNumber)) {
4893 log.info("resource vendor model number is not valid.");
4894 ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.INVALID_RESOURCE_VENDOR_MODEL_NUMBER);
4895 componentsUtils.auditResource(errorResponse, user, resource, actionEnum);
4896 throw new ByActionStatusComponentException(ActionStatus.INVALID_RESOURCE_VENDOR_MODEL_NUMBER);
4901 private void validateCost(Resource resource) {
4902 String cost = resource.getCost();
4904 if (!ValidationUtils.validateCost(cost)) {
4905 log.debug("resource cost is invalid.");
4906 throw new ByActionStatusComponentException(ActionStatus.INVALID_CONTENT);
4911 private void validateLicenseType(User user, Resource resource, AuditingActionEnum actionEnum) {
4912 log.debug("validate licenseType");
4913 String licenseType = resource.getLicenseType();
4914 if (licenseType != null) {
4915 List<String> licenseTypes = ConfigurationManager.getConfigurationManager().getConfiguration().getLicenseTypes();
4916 if (!licenseTypes.contains(licenseType)) {
4917 log.debug("License type {} isn't configured", licenseType);
4918 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT);
4919 if (actionEnum != null) {
4920 // In update case, no audit is required
4921 componentsUtils.auditResource(responseFormat, user, resource, actionEnum);
4923 throw new ByActionStatusComponentException(ActionStatus.INVALID_CONTENT);
4928 private Either<Boolean, ResponseFormat> processUpdateOfDerivedFrom(Resource currentResource, Resource updatedResource, String userId,
4929 boolean inTransaction) {
4930 if (updatedResource.getDerivedFrom() != null) {
4931 log.debug("Starting derived from update for resource {}", updatedResource.getUniqueId());
4932 log.debug("1. Removing interface artifacts from graph");
4933 // Remove all interface artifacts of resource
4934 String resourceId = updatedResource.getUniqueId();
4935 Map<String, InterfaceDefinition> interfaces = currentResource.getInterfaces();
4936 if (interfaces != null) {
4937 Collection<InterfaceDefinition> values = interfaces.values();
4938 for (InterfaceDefinition interfaceDefinition : values) {
4939 String interfaceType = interfaceTypeOperation.getShortInterfaceName(interfaceDefinition);
4940 log.trace("Starting interface artifacts removal for interface type {}", interfaceType);
4941 Map<String, Operation> operations = interfaceDefinition.getOperationsMap();
4942 if (operations != null) {
4943 for (Entry<String, Operation> operationEntry : operations.entrySet()) {
4944 Operation operation = operationEntry.getValue();
4945 ArtifactDefinition implementation = operation.getImplementationArtifact();
4946 if (implementation != null) {
4947 String uniqueId = implementation.getUniqueId();
4948 log.debug("Removing interface artifact definition {}, operation {}, interfaceType {}", uniqueId,
4949 operationEntry.getKey(), interfaceType);
4950 // only thing that transacts and locks here
4951 Either<ArtifactDefinition, ResponseFormat> deleteArtifactByInterface = artifactsBusinessLogic
4952 .deleteArtifactByInterface(resourceId, userId, uniqueId, true);
4953 if (deleteArtifactByInterface.isRight()) {
4954 log.debug("Couldn't remove artifact definition with id {}", uniqueId);
4955 if (!inTransaction) {
4956 janusGraphDao.rollback();
4958 return Either.right(deleteArtifactByInterface.right().value());
4961 log.trace("No implementation found for operation {} - nothing to delete", operationEntry.getKey());
4965 log.trace("No operations found for interface type {}", interfaceType);
4969 log.debug("2. Removing properties");
4970 Either<Map<String, PropertyDefinition>, StorageOperationStatus> findPropertiesOfNode = propertyOperation
4971 .deleteAllPropertiesAssociatedToNode(NodeTypeEnum.Resource, resourceId);
4972 if (findPropertiesOfNode.isRight() && findPropertiesOfNode.right().value() != StorageOperationStatus.OK) {
4973 log.debug("Failed to remove all properties of resource");
4974 if (!inTransaction) {
4975 janusGraphDao.rollback();
4978 .right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(findPropertiesOfNode.right().value())));
4981 log.debug("Derived from wasn't changed during update");
4983 if (inTransaction) {
4984 return Either.left(true);
4986 janusGraphDao.commit();
4987 return Either.left(true);
4990 public ICapabilityTypeOperation getCapabilityTypeOperation() {
4991 return capabilityTypeOperation;
4995 public void setCapabilityTypeOperation(ICapabilityTypeOperation capabilityTypeOperation) {
4996 this.capabilityTypeOperation = capabilityTypeOperation;
4999 public Boolean validatePropertiesDefaultValues(Resource resource) {
5000 log.debug("validate resource properties default values");
5001 List<PropertyDefinition> properties = resource.getProperties();
5002 if (properties != null) {
5003 iterateOverProperties(properties, resource.getModel());
5008 public void iterateOverProperties(List<PropertyDefinition> properties, String model) {
5010 String innerType = null;
5011 for (PropertyDefinition property : properties) {
5012 if (!propertyOperation.isPropertyTypeValid(property, model)) {
5013 log.info("Invalid type for property {}", property);
5014 throw new ByActionStatusComponentException(ActionStatus.INVALID_PROPERTY_TYPE, property.getType(), property.getName());
5016 Map<String, DataTypeDefinition> allDataTypes = componentsUtils.getAllDataTypes(applicationDataTypeCache, model);
5017 type = property.getType();
5018 if (type.equals(ToscaPropertyType.LIST.getType()) || type.equals(ToscaPropertyType.MAP.getType())) {
5019 ResponseFormat responseFormat = validateMapOrListPropertyType(property, innerType, allDataTypes);
5020 if (responseFormat != null) {
5024 validateDefaultPropertyValue(property, allDataTypes, type, innerType);
5028 private void validateDefaultPropertyValue(PropertyDefinition property, Map<String, DataTypeDefinition> allDataTypes, String type,
5030 if (!propertyOperation.isPropertyDefaultValueValid(property, allDataTypes)) {
5031 log.info("Invalid default value for property {}", property);
5032 ResponseFormat responseFormat;
5033 if (type.equals(ToscaPropertyType.LIST.getType()) || type.equals(ToscaPropertyType.MAP.getType())) {
5034 throw new ByActionStatusComponentException(ActionStatus.INVALID_COMPLEX_DEFAULT_VALUE, property.getName(), type, innerType,
5035 property.getDefaultValue());
5037 throw new ByActionStatusComponentException(ActionStatus.INVALID_DEFAULT_VALUE, property.getName(), type, property.getDefaultValue());
5041 private ResponseFormat validateMapOrListPropertyType(PropertyDefinition property, String innerType,
5042 Map<String, DataTypeDefinition> allDataTypes) {
5043 ResponseFormat responseFormat = null;
5044 ImmutablePair<String, Boolean> propertyInnerTypeValid = propertyOperation.isPropertyInnerTypeValid(property, allDataTypes);
5045 innerType = propertyInnerTypeValid.getLeft();
5046 if (!propertyInnerTypeValid.getRight()) {
5047 log.info("Invalid inner type for property {}", property);
5048 responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_PROPERTY_INNER_TYPE, innerType, property.getName());
5050 return responseFormat;
5054 public Either<List<String>, ResponseFormat> deleteMarkedComponents() {
5055 return deleteMarkedComponents(ComponentTypeEnum.RESOURCE);
5059 public ComponentInstanceBusinessLogic getComponentInstanceBL() {
5060 return componentInstanceBusinessLogic;
5063 private String getComponentTypeForResponse(Component component) {
5064 String componentTypeForResponse = "SERVICE";
5065 if (component instanceof Resource) {
5066 componentTypeForResponse = ((Resource) component).getResourceType().name();
5068 return componentTypeForResponse;
5071 public Either<Resource, ResponseFormat> getLatestResourceFromCsarUuid(String csarUuid, User user) {
5074 validateUserExists(user);
5076 // get resource from csar uuid
5077 Either<Resource, StorageOperationStatus> either = toscaOperationFacade
5078 .getLatestComponentByCsarOrName(ComponentTypeEnum.RESOURCE, csarUuid, "");
5079 if (either.isRight()) {
5080 ResponseFormat resp = componentsUtils.getResponseFormat(ActionStatus.RESOURCE_FROM_CSAR_NOT_FOUND, csarUuid);
5081 return Either.right(resp);
5083 return Either.left(either.left().value());
5087 public Either<List<ComponentInstance>, ResponseFormat> getComponentInstancesFilteredByPropertiesAndInputs(String componentId, String userId) {
5091 private Map<String, List<CapabilityDefinition>> getValidComponentInstanceCapabilities(String resourceId,
5092 Map<String, List<CapabilityDefinition>> defaultCapabilities,
5093 Map<String, List<UploadCapInfo>> uploadedCapabilities) {
5094 Map<String, List<CapabilityDefinition>> validCapabilitiesMap = new HashMap<>();
5095 uploadedCapabilities.forEach((k, v) -> addValidComponentInstanceCapabilities(k, v, resourceId, defaultCapabilities, validCapabilitiesMap));
5096 return validCapabilitiesMap;
5099 private void addValidComponentInstanceCapabilities(String key, List<UploadCapInfo> capabilities, String resourceId,
5100 Map<String, List<CapabilityDefinition>> defaultCapabilities,
5101 Map<String, List<CapabilityDefinition>> validCapabilitiesMap) {
5102 String capabilityType = capabilities.get(0).getType();
5103 if (defaultCapabilities.containsKey(capabilityType)) {
5104 CapabilityDefinition defaultCapability = getCapability(resourceId, defaultCapabilities, capabilityType);
5105 validateCapabilityProperties(capabilities, resourceId, defaultCapability);
5106 List<CapabilityDefinition> validCapabilityList = new ArrayList<>();
5107 validCapabilityList.add(defaultCapability);
5108 validCapabilitiesMap.put(key, validCapabilityList);
5110 throw new ByActionStatusComponentException(ActionStatus.MISSING_CAPABILITY_TYPE, capabilityType);
5114 private void validateCapabilityProperties(List<UploadCapInfo> capabilities, String resourceId, CapabilityDefinition defaultCapability) {
5115 if (CollectionUtils.isEmpty(defaultCapability.getProperties()) && isNotEmpty(capabilities.get(0).getProperties())) {
5116 log.debug("Failed to validate capability {} of component {}. Property list is empty. ", defaultCapability.getName(), resourceId);
5117 log.debug("Failed to update capability property values. Property list of fetched capability {} is empty. ", defaultCapability.getName());
5118 throw new ByActionStatusComponentException(ActionStatus.PROPERTY_NOT_FOUND, resourceId);
5119 } else if (isNotEmpty(capabilities.get(0).getProperties())) {
5120 validateUniquenessUpdateUploadedComponentInstanceCapability(defaultCapability, capabilities.get(0));
5124 private CapabilityDefinition getCapability(String resourceId, Map<String, List<CapabilityDefinition>> defaultCapabilities,
5125 String capabilityType) {
5126 CapabilityDefinition defaultCapability;
5127 if (isNotEmpty(defaultCapabilities.get(capabilityType).get(0).getProperties())) {
5128 defaultCapability = defaultCapabilities.get(capabilityType).get(0);
5130 Either<Component, StorageOperationStatus> getFullComponentRes = toscaOperationFacade.getToscaFullElement(resourceId);
5131 if (getFullComponentRes.isRight()) {
5132 log.debug("Failed to get full component {}. Status is {}. ", resourceId, getFullComponentRes.right().value());
5133 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_NOT_FOUND, resourceId);
5135 defaultCapability = getFullComponentRes.left().value().getCapabilities().get(capabilityType).get(0);
5137 return defaultCapability;
5140 private void validateUniquenessUpdateUploadedComponentInstanceCapability(CapabilityDefinition defaultCapability,
5141 UploadCapInfo uploadedCapability) {
5142 List<ComponentInstanceProperty> validProperties = new ArrayList<>();
5143 Map<String, PropertyDefinition> defaultProperties = defaultCapability.getProperties().stream()
5144 .collect(toMap(PropertyDefinition::getName, Function.identity()));
5145 List<UploadPropInfo> uploadedProperties = uploadedCapability.getProperties();
5146 for (UploadPropInfo property : uploadedProperties) {
5147 String propertyName = property.getName().toLowerCase();
5148 String propertyType = property.getType();
5149 ComponentInstanceProperty validProperty;
5150 if (defaultProperties.containsKey(propertyName) && propertTypeEqualsTo(defaultProperties, propertyName, propertyType)) {
5151 throw new ByActionStatusComponentException(ActionStatus.PROPERTY_NAME_ALREADY_EXISTS, propertyName);
5153 validProperty = new ComponentInstanceProperty();
5154 validProperty.setName(propertyName);
5155 if (property.getValue() != null) {
5156 validProperty.setValue(property.getValue().toString());
5158 validProperty.setDescription(property.getDescription());
5159 validProperty.setPassword(property.isPassword());
5160 validProperties.add(validProperty);
5162 defaultCapability.setProperties(validProperties);
5165 private boolean propertTypeEqualsTo(Map<String, PropertyDefinition> defaultProperties, String propertyName, String propertyType) {
5166 return propertyType != null && !defaultProperties.get(propertyName).getType().equals(propertyType);
5169 private Either<EnumMap<ArtifactOperationEnum, List<NonMetaArtifactInfo>>, ResponseFormat> organizeVfCsarArtifactsByArtifactOperation(
5170 List<NonMetaArtifactInfo> artifactPathAndNameList, List<ArtifactDefinition> existingArtifactsToHandle, Resource resource, User user) {
5171 EnumMap<ArtifactOperationEnum, List<NonMetaArtifactInfo>> nodeTypeArtifactsToHandle = new EnumMap<>(ArtifactOperationEnum.class);
5172 Wrapper<ResponseFormat> responseWrapper = new Wrapper<>();
5173 Either<EnumMap<ArtifactOperationEnum, List<NonMetaArtifactInfo>>, ResponseFormat> nodeTypeArtifactsToHandleRes = Either
5174 .left(nodeTypeArtifactsToHandle);
5176 // add all found Csar artifacts to list to upload
5177 List<NonMetaArtifactInfo> artifactsToUpload = new ArrayList<>(artifactPathAndNameList);
5178 List<NonMetaArtifactInfo> artifactsToUpdate = new ArrayList<>();
5179 List<NonMetaArtifactInfo> artifactsToDelete = new ArrayList<>();
5180 for (NonMetaArtifactInfo currNewArtifact : artifactPathAndNameList) {
5181 ArtifactDefinition foundArtifact;
5182 if (!existingArtifactsToHandle.isEmpty()) {
5183 foundArtifact = existingArtifactsToHandle.stream().filter(a -> a.getArtifactName().equals(currNewArtifact.getArtifactName()))
5184 .findFirst().orElse(null);
5185 if (foundArtifact != null) {
5186 if (foundArtifact.getArtifactType().equals(currNewArtifact.getArtifactType())) {
5187 if (!foundArtifact.getArtifactChecksum().equals(currNewArtifact.getArtifactChecksum())) {
5188 currNewArtifact.setArtifactUniqueId(foundArtifact.getUniqueId());
5189 // if current artifact already exists, but has
5191 // different content, add him to the list to
5194 artifactsToUpdate.add(currNewArtifact);
5196 // remove found artifact from the list of existing
5198 // artifacts to handle, because it was already
5201 existingArtifactsToHandle.remove(foundArtifact);
5202 // and remove found artifact from the list to
5204 // upload, because it should either be updated or be
5207 artifactsToUpload.remove(currNewArtifact);
5209 log.debug("Can't upload two artifact with the same name {}.", currNewArtifact.getArtifactName());
5210 ResponseFormat responseFormat = ResponseFormatManager.getInstance()
5211 .getResponseFormat(ActionStatus.ARTIFACT_ALREADY_EXIST_IN_DIFFERENT_TYPE_IN_CSAR, currNewArtifact.getArtifactName(),
5212 currNewArtifact.getArtifactType(), foundArtifact.getArtifactType());
5213 AuditingActionEnum auditingAction = artifactsBusinessLogic
5214 .detectAuditingType(new ArtifactOperationInfo(false, false, ArtifactOperationEnum.CREATE),
5215 foundArtifact.getArtifactChecksum());
5216 artifactsBusinessLogic
5217 .handleAuditing(auditingAction, resource, resource.getUniqueId(), user, null, null, foundArtifact.getUniqueId(),
5218 responseFormat, resource.getComponentType(), null);
5219 responseWrapper.setInnerElement(responseFormat);
5225 if (responseWrapper.isEmpty()) {
5226 for (ArtifactDefinition currArtifact : existingArtifactsToHandle) {
5227 if (currArtifact.getIsFromCsar()) {
5228 artifactsToDelete.add(new NonMetaArtifactInfo(currArtifact.getArtifactName(), null, currArtifact.getArtifactType(),
5229 currArtifact.getArtifactGroupType(), null, currArtifact.getUniqueId(), currArtifact.getIsFromCsar()));
5231 artifactsToUpdate.add(new NonMetaArtifactInfo(currArtifact.getArtifactName(), null, currArtifact.getArtifactType(),
5232 currArtifact.getArtifactGroupType(), null, currArtifact.getUniqueId(), currArtifact.getIsFromCsar()));
5236 if (responseWrapper.isEmpty()) {
5237 if (!artifactsToUpload.isEmpty()) {
5238 nodeTypeArtifactsToHandle.put(ArtifactOperationEnum.CREATE, artifactsToUpload);
5240 if (!artifactsToUpdate.isEmpty()) {
5241 nodeTypeArtifactsToHandle.put(ArtifactOperationEnum.UPDATE, artifactsToUpdate);
5243 if (!artifactsToDelete.isEmpty()) {
5244 nodeTypeArtifactsToHandle.put(ArtifactOperationEnum.DELETE, artifactsToDelete);
5247 if (!responseWrapper.isEmpty()) {
5248 nodeTypeArtifactsToHandleRes = Either.right(responseWrapper.getInnerElement());
5250 } catch (Exception e) {
5251 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR);
5252 responseWrapper.setInnerElement(responseFormat);
5253 log.debug("Exception occurred when findNodeTypeArtifactsToHandle, error is:{}", e.getMessage(), e);
5255 return nodeTypeArtifactsToHandleRes;
5258 ImmutablePair<String, String> buildNestedToscaResourceName(final String nodeResourceType, final String vfResourceName,
5259 final String nodeTypeFullName) {
5261 String actualVfName;
5262 if (ResourceTypeEnum.CVFC.name().equals(nodeResourceType)) {
5263 actualVfName = vfResourceName + ResourceTypeEnum.CVFC.name();
5264 actualType = ResourceTypeEnum.VFC.name();
5266 actualVfName = vfResourceName;
5267 actualType = nodeResourceType;
5269 String nameWithouNamespacePrefix;
5271 final String nodeTypeNamePrefix = getNodeTypeNamePrefix(nodeTypeFullName);
5272 log.debug("####### buildNestedToscaResourceName nodeResourceType {}, vfResourceName {}, "
5273 + "nodeTypeFullName {}, actualType {}, vfResourceName {} ", nodeResourceType, vfResourceName, nodeTypeFullName, actualType,
5275 final StringBuilder toscaResourceName = new StringBuilder(nodeTypeNamePrefix);
5276 if (!nodeTypeFullName.contains(nodeTypeNamePrefix)) {
5277 nameWithouNamespacePrefix = nodeTypeFullName;
5279 nameWithouNamespacePrefix = nodeTypeFullName.substring(nodeTypeNamePrefix.length());
5281 final String[] findTypes = nameWithouNamespacePrefix.split("\\.");
5283 if (nodeResourceType.equalsIgnoreCase(findTypes[0])) {
5284 actualName = nameWithouNamespacePrefix.substring(nodeResourceType.length());
5286 actualName = "." + nameWithouNamespacePrefix;
5288 if (actualName.startsWith(Constants.ABSTRACT)) {
5289 toscaResourceName.append(nodeResourceType.toLowerCase()).append('.').append(ValidationUtils.convertToSystemName(actualVfName));
5291 toscaResourceName.append(actualType.toLowerCase()).append('.').append(ValidationUtils.convertToSystemName(actualVfName)).append('.')
5292 .append(Constants.ABSTRACT);
5294 final StringBuilder previousToscaResourceName = new StringBuilder(toscaResourceName);
5295 final String[] actualNames = actualName.split("\\.");
5296 if (actualNames.length < 3) {
5297 return new ImmutablePair<>(toscaResourceName.append(actualName.toLowerCase()).toString(),
5298 previousToscaResourceName.append(actualName).toString());
5300 return new ImmutablePair<>(toscaResourceName.append(actualName.toLowerCase()).toString(),
5301 previousToscaResourceName.append(actualName.substring(actualNames[1].length() + 1).toLowerCase()).toString());
5302 } catch (final Exception e) {
5303 log.debug("Exception occured when buildNestedToscaResourceName, error is:{}", e.getMessage(), e);
5304 throw new ByActionStatusComponentException(ActionStatus.INVALID_TOSCA_TEMPLATE, vfResourceName);
5309 * Extracts a Node Type Name prefix from the given Node Type Name.
5311 * @param fullName Node Type Name
5312 * @return Node Type Name Prefix
5314 private String getNodeTypeNamePrefix(final String fullName) {
5315 String tempPrefix = Constants.USER_DEFINED_RESOURCE_NAMESPACE_PREFIX;
5316 final List<String> definedNodeTypeNamespaceList = getDefinedNodeTypeNamespaceList();
5317 log.debug("************* getPrefiX fullName {} FROM {}", fullName, definedNodeTypeNamespaceList);
5318 final Optional<String> validNameSpace = validateNodeTypeNamePrefix(fullName, definedNodeTypeNamespaceList);
5319 if (validNameSpace.isPresent()) {
5320 tempPrefix = validNameSpace.get();
5322 log.debug("************* getNodeTypeNamePrefix return fullName {} ", tempPrefix);
5327 public Either<UiComponentDataTransfer, ResponseFormat> getUiComponentDataTransferByComponentId(String resourceId,
5328 List<String> dataParamsToReturn) {
5329 ComponentParametersView paramsToReturn = new ComponentParametersView(dataParamsToReturn);
5330 Either<Resource, StorageOperationStatus> resourceResultEither = toscaOperationFacade.getToscaElement(resourceId, paramsToReturn);
5331 if (resourceResultEither.isRight()) {
5332 if (resourceResultEither.right().value() == StorageOperationStatus.NOT_FOUND) {
5333 log.debug("Failed to found resource with id {} ", resourceId);
5334 Either.right(componentsUtils.getResponseFormat(ActionStatus.RESOURCE_NOT_FOUND, resourceId));
5336 log.debug("failed to get resource by id {} with filters {}", resourceId, dataParamsToReturn);
5337 return Either.right(
5338 componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(resourceResultEither.right().value()), ""));
5340 Resource resource = resourceResultEither.left().value();
5341 if (dataParamsToReturn.contains(ComponentFieldsEnum.INPUTS.getValue())) {
5342 ListUtils.emptyIfNull(resource.getInputs()).forEach(input -> input.setConstraints(setInputConstraint(input)));
5344 UiComponentDataTransfer dataTransfer = uiComponentDataConverter.getUiDataTransferFromResourceByParams(resource, dataParamsToReturn);
5345 return Either.left(dataTransfer);
5349 public Either<Component, ActionStatus> shouldUpgradeToLatestDerived(Component clonedComponent) {
5350 Resource resource = (Resource) clonedComponent;
5351 if (ModelConverter.isAtomicComponent(resource.getResourceType())) {
5352 Either<Component, StorageOperationStatus> shouldUpgradeToLatestDerived = toscaOperationFacade.shouldUpgradeToLatestDerived(resource);
5353 if (shouldUpgradeToLatestDerived.isRight()) {
5354 return Either.right(componentsUtils.convertFromStorageResponse(shouldUpgradeToLatestDerived.right().value()));
5356 return Either.left(shouldUpgradeToLatestDerived.left().value());
5358 return super.shouldUpgradeToLatestDerived(clonedComponent);