2 * Copyright (C) 2020 CMCC, Inc. and others. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openecomp.sdc.be.components.impl;
19 import static java.util.stream.Collectors.joining;
20 import static java.util.stream.Collectors.toList;
21 import static java.util.stream.Collectors.toMap;
22 import static org.apache.commons.collections.CollectionUtils.isNotEmpty;
23 import static org.openecomp.sdc.be.components.impl.ImportUtils.findFirstToscaMapElement;
24 import static org.openecomp.sdc.be.components.impl.ImportUtils.findFirstToscaStringElement;
25 import static org.openecomp.sdc.be.components.impl.ImportUtils.getPropertyJsonStringValue;
26 import static org.openecomp.sdc.be.tosca.CsarUtils.VF_NODE_TYPE_ARTIFACTS_PATH_PATTERN;
28 import com.google.gson.Gson;
29 import com.google.gson.JsonElement;
30 import com.google.gson.JsonObject;
31 import com.google.gson.JsonParser;
32 import fj.data.Either;
33 import java.lang.reflect.Method;
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.Collection;
37 import java.util.Collections;
38 import java.util.EnumMap;
39 import java.util.HashMap;
40 import java.util.HashSet;
41 import java.util.Iterator;
42 import java.util.LinkedHashMap;
43 import java.util.List;
45 import java.util.Map.Entry;
46 import java.util.Optional;
48 import java.util.TreeSet;
49 import java.util.concurrent.atomic.AtomicReference;
50 import java.util.regex.Pattern;
51 import java.util.stream.Collectors;
54 import org.apache.commons.collections.CollectionUtils;
55 import org.apache.commons.collections.MapUtils;
56 import org.apache.commons.lang3.StringEscapeUtils;
57 import org.apache.commons.lang3.StringUtils;
58 import org.apache.commons.lang3.tuple.ImmutablePair;
59 import org.json.simple.JSONObject;
60 import org.openecomp.sdc.be.components.csar.CsarArtifactsAndGroupsBusinessLogic;
61 import org.openecomp.sdc.be.components.csar.CsarBusinessLogic;
62 import org.openecomp.sdc.be.components.csar.CsarInfo;
63 import org.openecomp.sdc.be.components.csar.ServiceCsarInfo;
64 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic.ArtifactOperationEnum;
65 import org.openecomp.sdc.be.components.impl.artifact.ArtifactOperationInfo;
66 import org.openecomp.sdc.be.components.impl.exceptions.BusinessLogicException;
67 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
68 import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException;
69 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
70 import org.openecomp.sdc.be.components.impl.model.ToscaTypeImportData;
71 import org.openecomp.sdc.be.components.impl.utils.CINodeFilterUtils;
72 import org.openecomp.sdc.be.components.impl.utils.CreateServiceFromYamlParameter;
73 import org.openecomp.sdc.be.components.lifecycle.LifecycleBusinessLogic;
74 import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction;
75 import org.openecomp.sdc.be.components.merge.resource.ResourceDataMergeBusinessLogic;
76 import org.openecomp.sdc.be.config.BeEcompErrorManager;
77 import org.openecomp.sdc.be.config.ConfigurationManager;
78 import org.openecomp.sdc.be.dao.api.ActionStatus;
79 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphDao;
80 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
81 import org.openecomp.sdc.be.datamodel.utils.ArtifactUtils;
82 import org.openecomp.sdc.be.datatypes.elements.CustomYamlFunction;
83 import org.openecomp.sdc.be.datatypes.elements.GetInputValueDataDefinition;
84 import org.openecomp.sdc.be.datatypes.elements.ListCapabilityDataDefinition;
85 import org.openecomp.sdc.be.datatypes.elements.ListDataDefinition;
86 import org.openecomp.sdc.be.datatypes.elements.ListRequirementDataDefinition;
87 import org.openecomp.sdc.be.datatypes.elements.OperationDataDefinition;
88 import org.openecomp.sdc.be.datatypes.elements.OperationInputDefinition;
89 import org.openecomp.sdc.be.datatypes.elements.PolicyDataDefinition;
90 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
91 import org.openecomp.sdc.be.datatypes.elements.SubPropertyToscaFunction;
92 import org.openecomp.sdc.be.datatypes.elements.SubstitutionFilterPropertyDataDefinition;
93 import org.openecomp.sdc.be.datatypes.elements.ToscaGetFunctionDataDefinition;
94 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
95 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
96 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
97 import org.openecomp.sdc.be.datatypes.tosca.ToscaGetFunctionType;
98 import org.openecomp.sdc.be.impl.ComponentsUtils;
99 import org.openecomp.sdc.be.info.NodeTypeInfoToUpdateArtifacts;
100 import org.openecomp.sdc.be.model.ArtifactDefinition;
101 import org.openecomp.sdc.be.model.ArtifactTypeDefinition;
102 import org.openecomp.sdc.be.model.AttributeDefinition;
103 import org.openecomp.sdc.be.model.CapabilityDefinition;
104 import org.openecomp.sdc.be.model.CapabilityRequirementRelationship;
105 import org.openecomp.sdc.be.model.CapabilityTypeDefinition;
106 import org.openecomp.sdc.be.model.Component;
107 import org.openecomp.sdc.be.model.ComponentInstance;
108 import org.openecomp.sdc.be.model.ComponentInstanceAttribute;
109 import org.openecomp.sdc.be.model.ComponentInstanceInput;
110 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
111 import org.openecomp.sdc.be.model.ComponentParametersView;
112 import org.openecomp.sdc.be.model.DataTypeDefinition;
113 import org.openecomp.sdc.be.model.DistributionStatusEnum;
114 import org.openecomp.sdc.be.model.GroupDefinition;
115 import org.openecomp.sdc.be.model.GroupTypeDefinition;
116 import org.openecomp.sdc.be.model.InputDefinition;
117 import org.openecomp.sdc.be.model.InterfaceDefinition;
118 import org.openecomp.sdc.be.model.LifeCycleTransitionEnum;
119 import org.openecomp.sdc.be.model.LifecycleStateEnum;
120 import org.openecomp.sdc.be.model.NodeTypeDefinition;
121 import org.openecomp.sdc.be.model.NodeTypeInfo;
122 import org.openecomp.sdc.be.model.NodeTypeMetadata;
123 import org.openecomp.sdc.be.model.NodeTypesMetadataList;
124 import org.openecomp.sdc.be.model.Operation;
125 import org.openecomp.sdc.be.model.OutputDefinition;
126 import org.openecomp.sdc.be.model.ParsedToscaYamlInfo;
127 import org.openecomp.sdc.be.model.PolicyDefinition;
128 import org.openecomp.sdc.be.model.PropertyDefinition;
129 import org.openecomp.sdc.be.model.RelationshipImpl;
130 import org.openecomp.sdc.be.model.RelationshipInfo;
131 import org.openecomp.sdc.be.model.RequirementCapabilityRelDef;
132 import org.openecomp.sdc.be.model.RequirementDefinition;
133 import org.openecomp.sdc.be.model.Resource;
134 import org.openecomp.sdc.be.model.Service;
135 import org.openecomp.sdc.be.model.UploadAttributeInfo;
136 import org.openecomp.sdc.be.model.UploadComponentInstanceInfo;
137 import org.openecomp.sdc.be.model.UploadInterfaceInfo;
138 import org.openecomp.sdc.be.model.UploadNodeFilterInfo;
139 import org.openecomp.sdc.be.model.UploadPropInfo;
140 import org.openecomp.sdc.be.model.UploadReqInfo;
141 import org.openecomp.sdc.be.model.UploadResourceInfo;
142 import org.openecomp.sdc.be.model.User;
143 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
144 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElement;
145 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
146 import org.openecomp.sdc.be.model.jsonjanusgraph.utils.ModelConverter;
147 import org.openecomp.sdc.be.model.normatives.ToscaTypeMetadata;
148 import org.openecomp.sdc.be.model.operations.StorageException;
149 import org.openecomp.sdc.be.model.operations.api.IGraphLockOperation;
150 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
151 import org.openecomp.sdc.be.model.operations.impl.ArtifactTypeOperation;
152 import org.openecomp.sdc.be.model.operations.impl.CapabilityTypeOperation;
153 import org.openecomp.sdc.be.model.operations.impl.GroupTypeOperation;
154 import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation;
155 import org.openecomp.sdc.be.model.operations.impl.ModelOperation;
156 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
157 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
158 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
159 import org.openecomp.sdc.be.tosca.CsarUtils;
160 import org.openecomp.sdc.be.tosca.ToscaExportHandler;
161 import org.openecomp.sdc.be.ui.model.OperationUi;
162 import org.openecomp.sdc.be.utils.TypeUtils;
163 import org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum;
164 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
165 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
166 import org.openecomp.sdc.common.api.Constants;
167 import org.openecomp.sdc.common.datastructure.Wrapper;
168 import org.openecomp.sdc.common.kpi.api.ASDCKpiApi;
169 import org.openecomp.sdc.common.log.wrappers.Logger;
170 import org.openecomp.sdc.common.util.ValidationUtils;
171 import org.openecomp.sdc.exception.ResponseFormat;
172 import org.springframework.beans.factory.annotation.Autowired;
173 import org.yaml.snakeyaml.Yaml;
177 @org.springframework.stereotype.Component("serviceImportBusinessLogic")
178 public class ServiceImportBusinessLogic {
180 protected static final String CREATE_RESOURCE = "Create Resource";
181 private static final String INITIAL_VERSION = "0.1";
182 private static final String IN_RESOURCE = " in resource {} ";
183 private static final String COMPONENT_INSTANCE_WITH_NAME = "component instance with name ";
184 private static final String COMPONENT_INSTANCE_WITH_NAME_IN_RESOURCE = "component instance with name {} in resource {} ";
185 private static final String CERTIFICATION_ON_IMPORT = "certification on import";
186 private static final String VALIDATE_DERIVED_BEFORE_UPDATE = "validate derived before update";
187 private static final String PLACE_HOLDER_RESOURCE_TYPES = "validForResourceTypes";
188 private static final String CREATE_RESOURCE_VALIDATE_CAPABILITY_TYPES = "Create Resource - validateCapabilityTypesCreate";
189 private static final String CATEGORY_IS_EMPTY = "Resource category is empty";
190 private static final Logger log = Logger.getLogger(ServiceImportBusinessLogic.class);
192 private final ComponentsUtils componentsUtils;
193 private final ToscaOperationFacade toscaOperationFacade;
194 private final ServiceBusinessLogic serviceBusinessLogic;
195 private final CsarBusinessLogic csarBusinessLogic;
196 private final CsarArtifactsAndGroupsBusinessLogic csarArtifactsAndGroupsBusinessLogic;
197 private final LifecycleBusinessLogic lifecycleBusinessLogic;
198 private final CompositionBusinessLogic compositionBusinessLogic;
199 private final ResourceDataMergeBusinessLogic resourceDataMergeBusinessLogic;
200 private final ServiceImportParseLogic serviceImportParseLogic;
201 private final GroupBusinessLogic groupBusinessLogic;
202 private final PolicyBusinessLogic policyBusinessLogic;
203 private final ResourceImportManager resourceImportManager;
204 private final JanusGraphDao janusGraphDao;
205 private final ArtifactsBusinessLogic artifactsBusinessLogic;
206 private final ArtifactTypeImportManager artifactTypeImportManager;
207 private final IGraphLockOperation graphLockOperation;
208 private final ToscaFunctionService toscaFunctionService;
209 private final DataTypeBusinessLogic dataTypeBusinessLogic;
210 private final ArtifactTypeOperation artifactTypeOperation;
211 private final GroupTypeImportManager groupTypeImportManager;
212 private final GroupTypeOperation groupTypeOperation;
213 private final CapabilityTypeImportManager capabilityTypeImportManager;
214 private final CapabilityTypeOperation capabilityTypeOperation;
215 private ApplicationDataTypeCache applicationDataTypeCache;
216 private final InterfaceLifecycleOperation interfaceLifecycleTypeOperation;
217 private final InterfaceLifecycleTypeImportManager interfaceLifecycleTypeImportManager;
218 private final ModelOperation modelOperation;
220 public ServiceImportBusinessLogic(final GroupBusinessLogic groupBusinessLogic, final ArtifactsBusinessLogic artifactsBusinessLogic,
221 final ComponentsUtils componentsUtils, final ToscaOperationFacade toscaOperationFacade,
222 final ServiceBusinessLogic serviceBusinessLogic, final CsarBusinessLogic csarBusinessLogic,
223 final CsarArtifactsAndGroupsBusinessLogic csarArtifactsAndGroupsBusinessLogic,
224 final LifecycleBusinessLogic lifecycleBusinessLogic, final CompositionBusinessLogic compositionBusinessLogic,
225 final ResourceDataMergeBusinessLogic resourceDataMergeBusinessLogic,
226 final ServiceImportParseLogic serviceImportParseLogic, final PolicyBusinessLogic policyBusinessLogic,
227 final ResourceImportManager resourceImportManager, final JanusGraphDao janusGraphDao,
228 final IGraphLockOperation graphLockOperation, final ToscaFunctionService toscaFunctionService,
229 final DataTypeBusinessLogic dataTypeBusinessLogic, final ArtifactTypeOperation artifactTypeOperation,
230 final ArtifactTypeImportManager artifactTypeImportManager, final GroupTypeImportManager groupTypeImportManager,
231 final GroupTypeOperation groupTypeOperation,
232 final InterfaceLifecycleOperation interfaceLifecycleTypeOperation,
233 final InterfaceLifecycleTypeImportManager interfaceLifecycleTypeImportManager,
234 final CapabilityTypeImportManager capabilityTypeImportManager,
235 final CapabilityTypeOperation capabilityTypeOperation,
236 final ModelOperation modelOperation) {
237 this.componentsUtils = componentsUtils;
238 this.toscaOperationFacade = toscaOperationFacade;
239 this.serviceBusinessLogic = serviceBusinessLogic;
240 this.csarBusinessLogic = csarBusinessLogic;
241 this.csarArtifactsAndGroupsBusinessLogic = csarArtifactsAndGroupsBusinessLogic;
242 this.lifecycleBusinessLogic = lifecycleBusinessLogic;
243 this.compositionBusinessLogic = compositionBusinessLogic;
244 this.resourceDataMergeBusinessLogic = resourceDataMergeBusinessLogic;
245 this.serviceImportParseLogic = serviceImportParseLogic;
246 this.groupBusinessLogic = groupBusinessLogic;
247 this.policyBusinessLogic = policyBusinessLogic;
248 this.resourceImportManager = resourceImportManager;
249 this.janusGraphDao = janusGraphDao;
250 this.artifactsBusinessLogic = artifactsBusinessLogic;
251 this.graphLockOperation = graphLockOperation;
252 this.toscaFunctionService = toscaFunctionService;
253 this.dataTypeBusinessLogic = dataTypeBusinessLogic;
254 this.artifactTypeOperation = artifactTypeOperation;
255 this.artifactTypeImportManager = artifactTypeImportManager;
256 this.groupTypeImportManager = groupTypeImportManager;
257 this.groupTypeOperation = groupTypeOperation;
258 this.interfaceLifecycleTypeOperation = interfaceLifecycleTypeOperation;
259 this.interfaceLifecycleTypeImportManager = interfaceLifecycleTypeImportManager;
260 this.capabilityTypeImportManager = capabilityTypeImportManager;
261 this.capabilityTypeOperation = capabilityTypeOperation;
262 this.modelOperation = modelOperation;
266 public void setApplicationDataTypeCache(ApplicationDataTypeCache applicationDataTypeCache) {
267 this.applicationDataTypeCache = applicationDataTypeCache;
270 public Service updateServiceFromToscaTemplate(final String serviceId, final User modifier, final String data) {
271 final Either<Service, ResponseFormat> serviceResponseFormatEither = serviceBusinessLogic.getService(serviceId, modifier);
272 if (serviceResponseFormatEither.isRight()) {
273 throw new ByActionStatusComponentException(ActionStatus.SERVICE_NOT_FOUND, serviceId);
275 final Service serviceOriginal = serviceResponseFormatEither.left().value();
276 final Map<String, String> metadata = (Map<String, String>) new Yaml().loadAs(data, Map.class).get("metadata");
277 validateServiceMetadataBeforeCreate(serviceOriginal, metadata);
279 final Service newService = cloneServiceIdentifications(serviceOriginal);
280 final Map<String, byte[]> payload = new HashMap<>();
281 payload.put("Definitions/service-" + metadata.get("name") + "-template.yml", data.getBytes());
282 updateServiceMetadata(newService, metadata);
283 return createService(newService, AuditingActionEnum.UPDATE_SERVICE_TOSCA_TEMPLATE, modifier, payload, null);
286 private Service cloneServiceIdentifications(final Service serviceOriginal) {
287 final Service newService = new Service(serviceOriginal.getComponentMetadataDefinition());
288 newService.setCategories(serviceOriginal.getCategories());
289 newService.setInvariantUUID(serviceOriginal.getInvariantUUID());
290 newService.setUniqueId(serviceOriginal.getUniqueId());
291 newService.setName(serviceOriginal.getName());
292 newService.setUUID(serviceOriginal.getUUID());
296 private void validateServiceMetadataBeforeCreate(final Service service, final Map<String, String> metadata) {
297 if (MapUtils.isEmpty(metadata)) {
298 throw new ByActionStatusComponentException(ActionStatus.MISSING_SERVICE_METADATA);
300 final String uuid = metadata.get("UUID");
301 if (!service.getUUID().equals(uuid)) {
302 throw new ByActionStatusComponentException(ActionStatus.UNCHANGEABLE_PROPERTY_ERROR, "UUID");
304 final String invariantUUID = metadata.get("invariantUUID");
305 if (!service.getInvariantUUID().equals(invariantUUID)) {
306 throw new ByActionStatusComponentException(ActionStatus.UNCHANGEABLE_PROPERTY_ERROR, "invariantUUID");
308 final String name = metadata.get("name");
309 if (!service.getName().equals(name)) {
310 throw new ByActionStatusComponentException(ActionStatus.UNCHANGEABLE_PROPERTY_ERROR, "name");
312 final String version = metadata.get("template_version");
313 if (!service.getVersion().equals(version)) {
314 throw new ByActionStatusComponentException(ActionStatus.UNCHANGEABLE_PROPERTY_ERROR, "template_version");
318 public Service createService(Service service, AuditingActionEnum auditingAction, User user, Map<String, byte[]> csarUIPayload,
319 String payloadName) {
320 log.debug("enter createService");
321 if (AuditingActionEnum.CREATE_SERVICE.equals(auditingAction)) {
322 service.setCreatorUserId(user.getUserId());
323 service.setState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
324 service.setVersion(INITIAL_VERSION);
325 service.setConformanceLevel(ConfigurationManager.getConfigurationManager().getConfiguration().getToscaConformanceLevel());
326 service.setDistributionStatus(DistributionStatusEnum.DISTRIBUTION_NOT_APPROVED);
329 final var serviceBeforeCreate = serviceBusinessLogic.validateServiceBeforeCreate(service, user, auditingAction);
330 if (serviceBeforeCreate.isRight()) {
331 throw new ComponentException(ActionStatus.GENERAL_ERROR);
333 log.debug("enter createService,validateServiceBeforeCreate success");
334 String csarUUID = payloadName == null ? service.getCsarUUID() : payloadName;
335 if (AuditingActionEnum.CREATE_SERVICE.equals(auditingAction)) {
336 log.debug("enter createService,get csarUUID:{}", csarUUID);
337 csarBusinessLogic.validateCsarBeforeCreate(service, csarUUID);
339 log.debug("CsarUUID is {} - going to create resource from CSAR", csarUUID);
340 return createServiceFromCsar(service, user, csarUIPayload, csarUUID, auditingAction);
341 } catch (final ComponentException e) {
342 log.debug("Exception occurred when createService: {}", e.getMessage(), e);
344 } catch (final Exception e) {
345 log.debug("Exception occurred when createService: {}", e.getMessage(), e);
346 throw new ComponentException(ActionStatus.GENERAL_ERROR);
350 private void updateServiceMetadata(final Service service, final Map<String, String> metadata) {
351 metadata.entrySet().forEach(s -> {
352 final Optional<Method> find =
353 Arrays.stream(service.getClass().getMethods()).filter(method -> method.getName().equalsIgnoreCase("set" + s.getKey())).findAny();
354 if (find.isPresent()) {
356 find.get().invoke(service, s.getValue());
357 } catch (final Exception e) {
358 log.warn("Unable to set '{}' with value '{}'", s.getKey(), s.getValue());
364 protected Service createServiceFromCsar(Service service, User user, Map<String, byte[]> csarUIPayload, String csarUUID,
365 AuditingActionEnum auditingAction) {
366 log.trace("************* created successfully from YAML, resource TOSCA ");
368 final ServiceCsarInfo csarInfo;
369 if (AuditingActionEnum.UPDATE_SERVICE_TOSCA_TEMPLATE.equals(auditingAction)) {
370 csarInfo = new ServiceCsarInfo(user, csarUUID, csarUIPayload, service.getName(), service.getModel(),
371 csarUIPayload.keySet().iterator().next(), new String(csarUIPayload.values().iterator().next()), true, modelOperation);
373 csarInfo = csarBusinessLogic.getCsarInfo(service, null, user, csarUIPayload, csarUUID, auditingAction);
375 final String serviceModel = service.getModel();
376 final Map<String, Object> dataTypesToCreate = getDatatypesToCreate(serviceModel, csarInfo);
377 if (MapUtils.isNotEmpty(dataTypesToCreate)) {
378 dataTypeBusinessLogic.createDataTypeFromYaml(new Yaml().dump(dataTypesToCreate), serviceModel, true);
379 dataTypesToCreate.keySet().forEach(key ->
380 applicationDataTypeCache.reload(serviceModel, UniqueIdBuilder.buildDataTypeUid(serviceModel, key))
384 final Map<String, Object> artifactTypesToCreate = getArtifactTypesToCreate(serviceModel, csarInfo);
385 if (MapUtils.isNotEmpty(artifactTypesToCreate)) {
386 artifactTypeImportManager.createArtifactTypes(new Yaml().dump(artifactTypesToCreate), serviceModel, true);
389 final List<NodeTypeDefinition> nodeTypesToCreate = getNodeTypesToCreate(serviceModel, csarInfo);
390 if (CollectionUtils.isNotEmpty(nodeTypesToCreate)) {
391 ParsedToscaYamlInfo parsedToscaYamlInfo = csarBusinessLogic.getParsedToscaYamlInfo(csarInfo, service);
392 createNodeTypes(nodeTypesToCreate, parsedToscaYamlInfo.getInstances(), serviceModel, csarInfo.getModifier());
395 final Map<String, Object> groupTypesToCreate = getGroupTypesToCreate(serviceModel, csarInfo);
396 if (MapUtils.isNotEmpty(groupTypesToCreate)) {
397 final Map<String, ToscaTypeMetadata> toscaTypeMetadata = fillToscaTypeMetadata(groupTypesToCreate);
398 final ToscaTypeImportData toscaTypeImportData = new ToscaTypeImportData(new Yaml().dump(groupTypesToCreate), toscaTypeMetadata);
399 groupTypeImportManager.createGroupTypes(toscaTypeImportData, serviceModel, true);
402 final Map<String, Object> interfaceTypesToCreate = getInterfaceTypesToCreate(serviceModel, csarInfo);
403 if (MapUtils.isNotEmpty(interfaceTypesToCreate)) {
404 interfaceLifecycleTypeImportManager.createLifecycleTypes(new Yaml().dump(interfaceTypesToCreate), serviceModel, true);
407 final Map<String, Object> capabilityTypesToCreate = getCapabilityTypesToCreate(serviceModel, csarInfo);
408 if (MapUtils.isNotEmpty(capabilityTypesToCreate)) {
409 capabilityTypeImportManager.createCapabilityTypes(new Yaml().dump(capabilityTypesToCreate), serviceModel, true);
412 Map<String, NodeTypeInfo> nodeTypesInfo = csarInfo.extractTypesInfo();
413 Either<Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>>, ResponseFormat> findNodeTypesArtifactsToHandleRes
414 = serviceImportParseLogic.findNodeTypesArtifactsToHandle(nodeTypesInfo, csarInfo, service);
415 if (findNodeTypesArtifactsToHandleRes.isRight()) {
416 log.debug("failed to find node types for update with artifacts during import csar {}. ", csarInfo.getCsarUUID());
417 throw new ComponentException(findNodeTypesArtifactsToHandleRes.right().value());
419 return createServiceFromYaml(service, csarInfo.getMainTemplateContent(), csarInfo.getMainTemplateName(), nodeTypesInfo, csarInfo,
420 findNodeTypesArtifactsToHandleRes.left().value(), true, false, null, user.getUserId(), auditingAction);
421 } catch (final ComponentException e) {
422 log.debug("Exception occurred when createServiceFromCsar,error is:{}", e.getMessage(), e);
424 } catch (final Exception e) {
425 log.debug("Exception occurred when createServiceFromCsar,error is:{}", e.getMessage(), e);
426 throw new ComponentException(ActionStatus.GENERAL_ERROR);
430 private Map<String, ToscaTypeMetadata> fillToscaTypeMetadata(final Map<String, Object> groupTypesToCreate) {
431 final Map<String, ToscaTypeMetadata> toscaTypeMetadata = new HashMap<>();
432 groupTypesToCreate.entrySet().forEach(entry -> {
433 final ToscaTypeMetadata metadata = new ToscaTypeMetadata();
434 metadata.setIcon(getIconFromGroupType(entry.getValue()));
435 metadata.setDisplayName(extractDisplayName(entry.getKey()));
436 toscaTypeMetadata.put(entry.getKey(), metadata);
438 return toscaTypeMetadata;
441 private String extractDisplayName(final String key) {
442 final String[] split = key.split("\\.");
443 return split[split.length - 1];
446 private String getIconFromGroupType(final Object value) {
447 final Either<GroupTypeDefinition, StorageOperationStatus> groupType = groupTypeOperation.getLatestGroupTypeByType(
448 (String) ((LinkedHashMap) value).get(ToscaTagNamesEnum.DERIVED_FROM.getElementName()), null);
449 if (groupType.isLeft()) {
450 return groupType.left().value().getIcon();
455 private Map<String, Object> getGroupTypesToCreate(final String model, final CsarInfo csarInfo) {
456 final Map<String, Object> groupTypesToCreate = new HashMap<>();
457 final Map<String, Object> groupTypes = csarInfo.getGroupTypes();
458 if (MapUtils.isNotEmpty(groupTypes)) {
459 for (final Entry<String, Object> entry : groupTypes.entrySet()) {
460 final Either<GroupTypeDefinition, StorageOperationStatus> result
461 = groupTypeOperation.getGroupTypeByUid(UniqueIdBuilder.buildGroupTypeUid(model, entry.getKey(), "1.0"));
462 if (result.isRight() && result.right().value().equals(StorageOperationStatus.NOT_FOUND)) {
463 groupTypesToCreate.put(entry.getKey(), entry.getValue());
464 log.info("Deploying new group type {} to model {} from package {}", entry.getKey(), model, csarInfo.getCsarUUID());
468 return groupTypesToCreate;
471 private Map<String, Object> getCapabilityTypesToCreate(final String model, final CsarInfo csarInfo) {
472 final Map<String, Object> capabilityTypesToCreate = new HashMap<>();
473 final Map<String, Object> capabilityTypes = csarInfo.getCapabilityTypes();
474 if (MapUtils.isNotEmpty(capabilityTypes)) {
475 for (final Entry<String, Object> entry : capabilityTypes.entrySet()) {
476 final Either<CapabilityTypeDefinition, StorageOperationStatus> result
477 = capabilityTypeOperation.getCapabilityType(UniqueIdBuilder.buildCapabilityTypeUid(model, entry.getKey()));
478 if (result.isRight() && result.right().value().equals(StorageOperationStatus.NOT_FOUND)) {
479 capabilityTypesToCreate.put(entry.getKey(), entry.getValue());
480 log.info("Deploying new capability type {} to model {} from package {}", entry.getKey(), model, csarInfo.getCsarUUID());
484 return capabilityTypesToCreate;
487 private Map<String, Object> getDatatypesToCreate(final String model, final CsarInfo csarInfo) {
488 final Map<String, Object> dataTypesToCreate = new HashMap<>();
490 for (final Entry<String, Object> dataTypeEntry : csarInfo.getDataTypes().entrySet()) {
491 final Either<DataTypeDefinition, JanusGraphOperationStatus> result = applicationDataTypeCache.get(model,
492 UniqueIdBuilder.buildDataTypeUid(model, dataTypeEntry.getKey()));
493 if (result.isRight() && result.right().value().equals(JanusGraphOperationStatus.NOT_FOUND)) {
494 dataTypesToCreate.put(dataTypeEntry.getKey(), dataTypeEntry.getValue());
495 log.info("Deploying unknown type {} to model {} from package {}", dataTypeEntry.getKey(), model, csarInfo.getCsarUUID());
497 if (hasNewProperties(result, (Map<String, Map<String, Object>>) dataTypeEntry.getValue())) {
498 dataTypesToCreate.put(dataTypeEntry.getKey(), dataTypeEntry.getValue());
499 log.info("Deploying new version of type {} to model {} from package {}", dataTypeEntry.getKey(), model, csarInfo.getCsarUUID());
502 return dataTypesToCreate;
505 private Map<String, Object> getArtifactTypesToCreate(final String model, final CsarInfo csarInfo) {
506 final Map<String, Object> artifactTypesToCreate = new HashMap<>();
507 final Map<String, Object> artifactTypesMap = csarInfo.getArtifactTypes();
508 if (MapUtils.isNotEmpty(artifactTypesMap)) {
509 for (final Entry<String, Object> artifactTypeEntry : artifactTypesMap.entrySet()) {
510 final Either<ArtifactTypeDefinition, StorageOperationStatus> result =
511 artifactTypeOperation.getArtifactTypeByUid(UniqueIdBuilder.buildArtifactTypeUid(model, artifactTypeEntry.getKey()));
512 if (result.isRight() && StorageOperationStatus.NOT_FOUND.equals(result.right().value())) {
513 artifactTypesToCreate.put(artifactTypeEntry.getKey(), artifactTypeEntry.getValue());
514 log.info("Deploying new artifact type={}, to model={}, from package={}",
515 artifactTypeEntry.getKey(), model, csarInfo.getCsarUUID());
519 return artifactTypesToCreate;
522 private Map<String, Object> getInterfaceTypesToCreate(final String model, final CsarInfo csarInfo) {
523 final Map<String, Object> interfaceTypesToCreate = new HashMap<>();
524 Map<String, Object> interfacetypeMap = csarInfo.getInterfaceTypes();
526 interfacetypeMap.entrySet().forEach(interfacetypeDef -> {
527 Either<InterfaceDefinition, StorageOperationStatus> interfaceDefinition =
528 interfaceLifecycleTypeOperation.getInterface(UniqueIdBuilder.buildInterfaceTypeUid(model, interfacetypeDef.getKey()));
529 if (interfaceDefinition.isRight() && interfaceDefinition.right().value().equals(StorageOperationStatus.NOT_FOUND)) {
530 interfaceTypesToCreate.put(interfacetypeDef.getKey(), interfacetypeDef.getValue());
533 return interfaceTypesToCreate;
536 private boolean hasNewProperties(final Either<DataTypeDefinition, JanusGraphOperationStatus> result,
537 final Map<String, Map<String, Object>> dataType) {
538 return result.isLeft() && dataType.containsKey("properties") && result.left().value().getProperties() != null
539 && result.left().value().getProperties().size() != dataType.get("properties").size();
542 private void createNodeTypes(List<NodeTypeDefinition> nodeTypesToCreate, Map<String, UploadComponentInstanceInfo> instancesFromCsar, String model,
544 NodeTypesMetadataList nodeTypesMetadataList = new NodeTypesMetadataList();
545 List<NodeTypeMetadata> nodeTypeMetadataList = new ArrayList<>();
546 final Map<String, Object> allTypesToCreate = new HashMap<>();
547 nodeTypesToCreate.forEach(nodeType -> {
548 allTypesToCreate.put(nodeType.getMappedNodeType().getKey(), nodeType.getMappedNodeType().getValue());
549 nodeTypeMetadataList.add(nodeType.getNodeTypeMetadata());
551 nodeTypesMetadataList.setNodeMetadataList(nodeTypeMetadataList);
552 resourceImportManager.importAllNormativeResource(allTypesToCreate, nodeTypesMetadataList, instancesFromCsar, user, model, true, false);
555 private List<NodeTypeDefinition> getNodeTypesToCreate(final String model, final ServiceCsarInfo csarInfo) {
556 List<NodeTypeDefinition> namesOfNodeTypesToCreate = new ArrayList<>();
558 for (final NodeTypeDefinition nodeTypeDefinition : csarInfo.getNodeTypesUsed()) {
559 Either<Component, StorageOperationStatus> result = toscaOperationFacade
560 .getLatestByToscaResourceName(nodeTypeDefinition.getMappedNodeType().getKey(), model);
561 if (result.isRight() && result.right().value().equals(StorageOperationStatus.NOT_FOUND)) {
562 namesOfNodeTypesToCreate.add(nodeTypeDefinition);
563 } else if (result.isLeft()) {
564 Resource latestResource = (Resource) result.left().value();
565 Entry<String, Object> existingMappedToscaTemplate = getResourceToscaTemplate(latestResource.getUniqueId(),
566 latestResource.getToscaArtifacts().get(ToscaExportHandler.ASSET_TOSCA_TEMPLATE), csarInfo.getModifier().getUserId());
567 Map<String, Object> newMappedToscaTemplate = (Map<String, Object>) nodeTypeDefinition.getMappedNodeType().getValue();
568 Map<String, Object> combinedMappedToscaTemplate =
569 getNewChangesToToscaTemplate(newMappedToscaTemplate, (Map<String, Object>) existingMappedToscaTemplate.getValue());
570 if (!combinedMappedToscaTemplate.equals(existingMappedToscaTemplate.getValue())) {
571 if (latestResource.getComponentMetadataDefinition().getMetadataDataDefinition().isNormative()) {
572 nodeTypeDefinition.getNodeTypeMetadata().setNormative(true);
574 existingMappedToscaTemplate.setValue(combinedMappedToscaTemplate);
575 nodeTypeDefinition.setMappedNodeType(existingMappedToscaTemplate);
576 namesOfNodeTypesToCreate.add(nodeTypeDefinition);
580 return namesOfNodeTypesToCreate;
583 private Entry<String, Object> getResourceToscaTemplate(String uniqueId, ArtifactDefinition assetToscaTemplate, String userId) {
584 String assetToToscaTemplate = assetToscaTemplate.getUniqueId();
585 ImmutablePair<String, byte[]> toscaTemplate = artifactsBusinessLogic.
586 handleDownloadRequestById(uniqueId, assetToToscaTemplate, userId, ComponentTypeEnum.RESOURCE, null, null);
587 Map<String, Object> mappedToscaTemplate = new Yaml().load(new String(toscaTemplate.right));
588 Either<Map<String, Object>, ImportUtils.ResultStatusEnum> eitherNodeTypes =
589 findFirstToscaMapElement(mappedToscaTemplate, TypeUtils.ToscaTagNamesEnum.NODE_TYPES);
590 if (eitherNodeTypes.isRight()) {
591 throw new ComponentException(ActionStatus.INVALID_TOSCA_TEMPLATE);
593 return eitherNodeTypes.left().value().entrySet().iterator().next();
596 private Map<String, Object> getNewChangesToToscaTemplate(Map<String, Object> newMappedToscaTemplate,
597 Map<String, Object> existingMappedToscaTemplate) {
598 Map<String, Object> combinedMappedToscaTemplate = new HashMap<>(existingMappedToscaTemplate);
599 combinePropertiesIntoToscaTemplate((Map<String, Object>) newMappedToscaTemplate.get("properties"),
600 (Map<String, Object>) existingMappedToscaTemplate.get("properties"), combinedMappedToscaTemplate);
601 combineAttributesIntoToscaTemplate((Map<String, Object>) newMappedToscaTemplate.get("attributes"),
602 (Map<String, Object>) existingMappedToscaTemplate.get("attributes"), combinedMappedToscaTemplate);
603 combineRequirementsIntoToscaTemplate((List<Map<String, Object>>) newMappedToscaTemplate.get("requirements"),
604 (List<Map<String, Object>>) existingMappedToscaTemplate.get("requirements"), combinedMappedToscaTemplate);
605 combineCapabilitiesIntoToscaTemplate((Map<String, Object>) newMappedToscaTemplate.get("capabilities"),
606 (Map<String, Object>) existingMappedToscaTemplate.get("capabilities"), combinedMappedToscaTemplate);
607 combineInterfacesIntoToscaTemplate((Map<String, Map<String, Object>>) newMappedToscaTemplate.get("interfaces"),
608 (Map<String, Map<String, Object>>) existingMappedToscaTemplate.get("interfaces"), combinedMappedToscaTemplate);
609 return combinedMappedToscaTemplate;
612 private void combineInterfacesIntoToscaTemplate(Map<String, Map<String, Object>> newInterfaces,
613 Map<String, Map<String, Object>> existingInterfaces,
614 Map<String, Object> combinedMappedToscaTemplate) {
615 Map<String, Map<String, Object>> combinedInterfaces = combineAdditionalInterfaces(newInterfaces, existingInterfaces);
616 if ((MapUtils.isEmpty(existingInterfaces) && MapUtils.isNotEmpty(combinedInterfaces))
617 || (MapUtils.isNotEmpty(existingInterfaces) && !existingInterfaces.equals(combinedInterfaces))) {
618 combinedMappedToscaTemplate.put("interfaces", combinedInterfaces);
622 private void combineCapabilitiesIntoToscaTemplate(Map<String, Object> newCapabilities, Map<String, Object> existingCapabilities,
623 Map<String, Object> combinedMappedToscaTemplate) {
624 Map<String, Object> combinedCapabilities = combineEntries(newCapabilities, existingCapabilities);
625 if ((MapUtils.isEmpty(existingCapabilities) && MapUtils.isNotEmpty(combinedCapabilities)) ||
626 (MapUtils.isNotEmpty(existingCapabilities) && !combinedCapabilities.equals(existingCapabilities))) {
627 combinedMappedToscaTemplate.put("capabilities", combinedCapabilities);
631 private void combineRequirementsIntoToscaTemplate(List<Map<String, Object>> newRequirements, List<Map<String, Object>> existingRequirements,
632 Map<String, Object> combinedMappedToscaTemplate) {
633 List<Map<String, Object>> combinedRequirements = combineAdditionalRequirements(newRequirements, existingRequirements);
634 if ((CollectionUtils.isEmpty(existingRequirements) && CollectionUtils.isNotEmpty(combinedRequirements))
635 || (CollectionUtils.isNotEmpty(existingRequirements) && !combinedRequirements.equals(existingRequirements))) {
636 combinedMappedToscaTemplate.put("requirements", combinedRequirements);
640 private void combineAttributesIntoToscaTemplate(Map<String, Object> newAttributes, Map<String, Object> existingAttributes,
641 Map<String, Object> combinedMappedToscaTemplate) {
642 Map<String, Object> combinedAttributes = combineEntries(newAttributes, existingAttributes);
643 if ((MapUtils.isEmpty(existingAttributes) && MapUtils.isNotEmpty(combinedAttributes)) ||
644 (MapUtils.isNotEmpty(existingAttributes) && !combinedAttributes.equals(existingAttributes))) {
645 combinedMappedToscaTemplate.put("attributes", combinedAttributes);
649 private void combinePropertiesIntoToscaTemplate(Map<String, Object> newProperties, Map<String, Object> existingProperties,
650 Map<String, Object> combinedMappedToscaTemplate) {
651 Map<String, Object> combinedProperties = combineEntries(newProperties, existingProperties);
652 if ((MapUtils.isEmpty(existingProperties) && MapUtils.isNotEmpty(combinedProperties)) ||
653 (MapUtils.isNotEmpty(existingProperties) && !combinedProperties.equals(existingProperties))) {
654 combinedMappedToscaTemplate.put("properties", combinedProperties);
658 private Map<String, Map<String, Object>> combineAdditionalInterfaces(Map<String, Map<String, Object>> newInterfaces,
659 Map<String, Map<String, Object>> existingInterfaces) {
660 if (MapUtils.isNotEmpty(newInterfaces) && MapUtils.isNotEmpty(existingInterfaces) && newInterfaces.equals(existingInterfaces)) {
661 return new HashMap<>(existingInterfaces);
663 if (MapUtils.isEmpty(newInterfaces)) {
664 newInterfaces = new HashMap<>();
666 Map<String, Map<String, Object>> combinedEntries = new HashMap<>(newInterfaces);
667 if (MapUtils.isEmpty(existingInterfaces)) {
668 return combinedEntries;
670 existingInterfaces.entrySet().forEach(interfaceDef -> {
671 combinedEntries.entrySet().stream().filter((interFace) -> interFace.getValue().get("type").equals((interfaceDef.getValue()).get("type")))
672 .findFirst().ifPresentOrElse((interFace) -> {
673 interFace.getValue().putAll(interfaceDef.getValue());
675 combinedEntries.put(interfaceDef.getKey(), interfaceDef.getValue());
678 return combinedEntries;
681 private List<Map<String, Object>> combineAdditionalRequirements(List<Map<String, Object>> newReqs,
682 List<Map<String, Object>> existingResourceReqs) {
683 if (CollectionUtils.isNotEmpty(newReqs) && CollectionUtils.isNotEmpty(existingResourceReqs) && newReqs.equals(existingResourceReqs)) {
684 return new ArrayList<>(existingResourceReqs);
686 if (CollectionUtils.isEmpty(existingResourceReqs)) {
687 existingResourceReqs = new ArrayList<>();
689 Set<Map<String, Object>> combinedReqs = new TreeSet<>((map1, map2) ->
690 map1.keySet().equals(map2.keySet()) ? 0 : map1.keySet().iterator().next().compareTo(map2.keySet().iterator().next()));
691 combinedReqs.addAll(existingResourceReqs);
692 if (CollectionUtils.isEmpty(newReqs)) {
693 return new ArrayList<>(combinedReqs);
695 combinedReqs.addAll(newReqs);
696 return new ArrayList<>(combinedReqs);
699 private Map<String, Object> combineEntries(Map<String, Object> newMap, Map<String, Object> existingMap) {
700 if (MapUtils.isNotEmpty(newMap) && MapUtils.isNotEmpty(existingMap) && newMap.equals(existingMap)) {
701 return new HashMap<>(existingMap);
703 if (MapUtils.isEmpty(newMap)) {
704 newMap = new HashMap<>();
706 Map<String, Object> combinedEntries = new HashMap<>(newMap);
707 if (MapUtils.isEmpty(existingMap)) {
708 return combinedEntries;
710 combinedEntries.putAll(existingMap);
711 return combinedEntries;
714 protected Service createServiceFromYaml(Service service, String topologyTemplateYaml, String yamlName, Map<String, NodeTypeInfo> nodeTypesInfo,
716 Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToCreate,
717 boolean shouldLock, boolean inTransaction, String nodeName, final String userId,
718 AuditingActionEnum auditingAction)
719 throws BusinessLogicException {
720 List<ArtifactDefinition> createdArtifacts = new ArrayList<>();
721 Service createdService;
722 CreateServiceFromYamlParameter csfyp = new CreateServiceFromYamlParameter();
724 ParsedToscaYamlInfo parsedToscaYamlInfo = csarBusinessLogic
725 .getParsedToscaYamlInfo(topologyTemplateYaml, yamlName, nodeTypesInfo, csarInfo, nodeName, service);
726 log.debug("#createResourceFromYaml - Going to create resource {} and RIs ", service.getName());
727 csfyp.setYamlName(yamlName);
728 csfyp.setParsedToscaYamlInfo(parsedToscaYamlInfo);
729 csfyp.setCreatedArtifacts(createdArtifacts);
730 csfyp.setTopologyTemplateYaml(topologyTemplateYaml);
731 csfyp.setNodeTypesInfo(nodeTypesInfo);
732 csfyp.setCsarInfo(csarInfo);
733 csfyp.setNodeName(nodeName);
734 createdService = createServiceAndRIsFromYaml(service, false, nodeTypesArtifactsToCreate, shouldLock, inTransaction, csfyp, userId,
736 log.debug("#createResourceFromYaml - The resource {} has been created ", service.getName());
737 } catch (ComponentException | BusinessLogicException e) {
738 log.debug("Create Service from yaml failed", e);
740 } catch (StorageException e) {
741 log.debug("create Service From Yaml failed,get StorageException:{}", e);
744 return createdService;
747 protected Service createServiceAndRIsFromYaml(Service service, boolean isNormative,
748 Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToCreate,
749 boolean shouldLock, boolean inTransaction, CreateServiceFromYamlParameter csfyp,
750 final String userId, AuditingActionEnum auditingAction)
751 throws BusinessLogicException {
753 List<ArtifactDefinition> nodeTypesNewCreatedArtifacts = new ArrayList<>();
754 String yamlName = csfyp.getYamlName();
755 ParsedToscaYamlInfo parsedToscaYamlInfo = csfyp.getParsedToscaYamlInfo();
756 List<ArtifactDefinition> createdArtifacts = csfyp.getCreatedArtifacts();
757 String topologyTemplateYaml = csfyp.getTopologyTemplateYaml();
758 Map<String, NodeTypeInfo> nodeTypesInfo = csfyp.getNodeTypesInfo();
759 CsarInfo csarInfo = csfyp.getCsarInfo();
760 String nodeName = csfyp.getNodeName();
762 Either<Boolean, ResponseFormat> lockResult = serviceBusinessLogic.lockComponentByName(service.getSystemName(), service, CREATE_RESOURCE);
763 if (lockResult.isRight()) {
764 throw new ComponentException(lockResult.right().value());
766 log.debug("name is locked {} status = {}", service.getSystemName(), lockResult);
768 boolean rollback = false;
770 log.trace("************* Adding properties to service from interface yaml {}", yamlName);
771 Map<String, PropertyDefinition> properties = parsedToscaYamlInfo.getProperties();
772 if (properties != null && !properties.isEmpty()) {
773 final List<PropertyDefinition> propertiesList = new ArrayList<>();
774 properties.forEach((propertyName, propertyDefinition) -> {
775 propertyDefinition.setName(propertyName);
776 propertiesList.add(propertyDefinition);
778 service.setProperties(propertiesList);
780 log.trace("************* createResourceFromYaml before full create resource {}", yamlName);
781 service = serviceImportParseLogic.createServiceTransaction(service, csarInfo.getModifier(), isNormative, auditingAction);
782 log.trace("************* Going to add inputs from yaml {}", yamlName);
783 Map<String, InputDefinition> inputs = parsedToscaYamlInfo.getInputs();
784 service = serviceImportParseLogic.createInputsOnService(service, inputs);
785 log.trace("************* Finished to add inputs from yaml {}", yamlName);
786 ListDataDefinition<SubstitutionFilterPropertyDataDefinition> substitutionFilterProperties =
787 parsedToscaYamlInfo.getSubstitutionFilterProperties();
788 service = serviceImportParseLogic.createSubstitutionFilterOnService(service, substitutionFilterProperties);
789 log.trace("************* Added Substitution filter from interface yaml {}", yamlName);
790 Map<String, UploadComponentInstanceInfo> uploadComponentInstanceInfoMap = parsedToscaYamlInfo.getInstances();
791 log.trace("************* Going to create nodes, RI's and Relations from yaml {}", yamlName);
792 service = createRIAndRelationsFromYaml(yamlName, service, uploadComponentInstanceInfoMap, topologyTemplateYaml,
793 nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo, nodeTypesArtifactsToCreate, nodeName);
794 log.trace("************* Finished to create nodes, RI and Relation from yaml {}", yamlName);
795 log.trace("************* Going to add outputs from yaml {}", yamlName);
796 Map<String, OutputDefinition> outputs = parsedToscaYamlInfo.getOutputs();
797 service = serviceImportParseLogic.createOutputsOnService(service, outputs, userId);
798 log.trace("************* Finished to add outputs from yaml {}", yamlName);
800 Either<Map<String, GroupDefinition>, ResponseFormat> validateUpdateVfGroupNamesRes
801 = groupBusinessLogic.validateUpdateVfGroupNames(parsedToscaYamlInfo.getGroups(), service.getSystemName());
802 if (validateUpdateVfGroupNamesRes.isRight()) {
803 throw new ComponentException(validateUpdateVfGroupNamesRes.right().value());
805 Map<String, GroupDefinition> groups;
806 log.trace("************* Going to add groups from yaml {}", yamlName);
807 if (!validateUpdateVfGroupNamesRes.left().value().isEmpty()) {
808 groups = validateUpdateVfGroupNamesRes.left().value();
810 groups = parsedToscaYamlInfo.getGroups();
812 Either<Service, ResponseFormat> createGroupsOnResource = createGroupsOnResource(service, groups);
813 if (createGroupsOnResource.isRight()) {
814 throw new ComponentException(createGroupsOnResource.right().value());
816 service = createGroupsOnResource.left().value();
818 Either<Service, ResponseFormat> createPoliciesOnResource = createPoliciesOnResource(service, parsedToscaYamlInfo.getPolicies());
819 if (createPoliciesOnResource.isRight()) {
820 throw new ComponentException(createPoliciesOnResource.right().value());
822 service = createPoliciesOnResource.left().value();
823 log.trace("************* Going to add artifacts from yaml {}", yamlName);
824 Either<Service, ResponseFormat> createArtifactsEither = createOrUpdateArtifacts(ArtifactsBusinessLogic.ArtifactOperationEnum.CREATE,
825 createdArtifacts, yamlName, csarInfo, service, inTransaction, shouldLock);
826 if (createArtifactsEither.isRight()) {
827 throw new ComponentException(createArtifactsEither.right().value());
829 service = serviceImportParseLogic.getServiceWithGroups(createArtifactsEither.left().value().getUniqueId());
830 service = updateInputs(service, userId, parsedToscaYamlInfo.getSubstitutionMappingProperties());
832 ASDCKpiApi.countCreatedResourcesKPI();
834 } catch (Exception e) {
836 serviceImportParseLogic.rollback(inTransaction, service, createdArtifacts, nodeTypesNewCreatedArtifacts);
839 if (!inTransaction) {
841 janusGraphDao.rollback();
843 janusGraphDao.commit();
847 graphLockOperation.unlockComponentByName(service.getSystemName(), service.getUniqueId(), NodeTypeEnum.Service);
852 private Service updateInputs(final Service component, final String userId, final Map<String, List<String>> substitutionMappingProperties) {
853 final List<InputDefinition> inputs = component.getInputs();
854 if (CollectionUtils.isNotEmpty(inputs)) {
855 final List<ComponentInstance> componentInstances = component.getComponentInstances();
856 final String componentUniqueId = component.getUniqueId();
857 List<String> propertyMissingNames = new ArrayList<>();
858 for (final InputDefinition input : inputs) {
859 boolean isSubMapProp = false;
860 if (substitutionMappingProperties != null && !substitutionMappingProperties.isEmpty()) {
861 isSubMapProp = substitutionMappingProperties.entrySet().stream()
862 .anyMatch(stringEntry -> stringEntry.getValue().get(0).equals(input.getName()));
864 if (!isSubMapProp && isInputFromComponentInstanceProperty(input.getName(), componentInstances)) {
865 associateInputToComponentInstanceProperty(userId, input, componentInstances, componentUniqueId);
867 String propertyName = associateInputToServiceProperty(userId, input, component, substitutionMappingProperties);
868 if (StringUtils.isNotBlank(propertyName)) {
869 propertyMissingNames.add(propertyName);
873 if (CollectionUtils.isNotEmpty(propertyMissingNames)) {
874 throw new ComponentException(
875 componentsUtils.getResponseFormat(ActionStatus.MISSING_PROPERTIES_ERROR, propertyMissingNames.toString()));
877 Either<List<InputDefinition>, StorageOperationStatus> either = toscaOperationFacade.updateInputsToComponent(inputs, componentUniqueId);
878 if (either.isRight()) {
879 throw new ComponentException(ActionStatus.GENERAL_ERROR);
886 private boolean isInputFromComponentInstanceProperty(final String inputName, final List<ComponentInstance> componentInstances) {
888 if (CollectionUtils.isNotEmpty(componentInstances)) {
889 for (ComponentInstance instance : componentInstances) {
890 for (PropertyDefinition instanceProperty : instance.getProperties()) {
891 if (CollectionUtils.isNotEmpty(instanceProperty.getGetInputValues())) {
892 for (GetInputValueDataDefinition getInputValueDataDefinition : instanceProperty.getGetInputValues()) {
893 if (inputName.equals(getInputValueDataDefinition.getInputName())) {
904 private void associateInputToComponentInstanceProperty(final String userId, final InputDefinition input,
905 final List<ComponentInstance> componentInstances,
906 String componentUniqueId) {
908 String componentInstanceId = null;
909 ComponentInstanceProperty componentInstanceProperty = new ComponentInstanceProperty();
912 for (ComponentInstance instance : componentInstances) {
913 for (PropertyDefinition instanceProperty : instance.getProperties()) {
914 if (CollectionUtils.isNotEmpty(instanceProperty.getGetInputValues())) {
915 for (GetInputValueDataDefinition getInputValueDataDefinition : instanceProperty.getGetInputValues()) {
916 if (input.getName().equals(getInputValueDataDefinition.getInputName())) {
917 componentInstanceId = instance.getUniqueId();
918 componentInstanceProperty = new ComponentInstanceProperty(instanceProperty);
926 //unmapping instance property declared inputs from substitution mapping
927 input.setMappedToComponentProperty(false);
930 updateInput(input, componentInstanceProperty, userId, componentInstanceId);
932 final Either<Map<String, List<ComponentInstanceProperty>>, StorageOperationStatus> either =
933 toscaOperationFacade.updateComponentInstancePropsToComponent(Collections.singletonMap(componentInstanceId,
934 Collections.singletonList(componentInstanceProperty)), componentUniqueId);
935 if (either.isRight()) {
936 throw new ComponentException(ActionStatus.GENERAL_ERROR);
940 private String associateInputToServiceProperty(final String userId,
941 final InputDefinition input, final Service component,
942 final Map<String, List<String>> substitutionMappingProperties) {
943 final List<PropertyDefinition> properties = component.getProperties();
944 if (CollectionUtils.isNotEmpty(properties) && MapUtils.isNotEmpty(substitutionMappingProperties)) {
945 AtomicReference<String> propertyNameFromInput = new AtomicReference<>(" ");
946 substitutionMappingProperties.entrySet().forEach(stringEntry -> {
947 if (stringEntry.getValue().get(0).equals(input.getName())) {
948 propertyNameFromInput.set(stringEntry.getKey());
952 final Optional<PropertyDefinition> propDefOptional =
953 properties.stream().filter(prop -> prop.getName().equals(propertyNameFromInput.get()))
955 if (propDefOptional.isPresent()) {
957 final String componentUniqueId = component.getUniqueId();
958 final PropertyDefinition propertyDefinition = propDefOptional.get();
959 updateProperty(propertyDefinition, input, componentUniqueId);
960 final JSONObject jsonObject = new JSONObject();
961 jsonObject.put(ToscaGetFunctionType.GET_INPUT.getFunctionName(), input.getName());
962 propertyDefinition.setValue(jsonObject.toJSONString());
963 updateInput(input, propertyDefinition, userId, componentUniqueId);
965 final Either<PropertyDefinition, StorageOperationStatus> either
966 = toscaOperationFacade.updatePropertyOfComponent(component, propertyDefinition);
967 if (either.isRight()) {
968 throw new ComponentException(ActionStatus.GENERAL_ERROR);
971 input.setMappedToComponentProperty(false);
972 return propertyNameFromInput.get();
978 private void updateProperty(final PropertyDefinition propertyDefinition, final InputDefinition input, final String componentUniqueId) {
979 propertyDefinition.setParentUniqueId(componentUniqueId);
980 final GetInputValueDataDefinition getInputValueDataDefinition = new GetInputValueDataDefinition();
981 getInputValueDataDefinition.setInputId(input.getUniqueId());
982 getInputValueDataDefinition.setInputName(input.getName());
983 getInputValueDataDefinition.setPropName(propertyDefinition.getName());
984 propertyDefinition.setGetInputValues(Collections.singletonList(getInputValueDataDefinition));
987 private void updateInput(final InputDefinition input, final PropertyDefinition propertyDefinition,
988 final String userId, final String componentUniqueId) {
989 input.setProperties(Collections.singletonList(new ComponentInstanceProperty(propertyDefinition)));
990 input.setInstanceUniqueId(componentUniqueId);
991 input.setOwnerId(userId);
992 input.setPropertyId(propertyDefinition.getUniqueId());
993 input.setParentPropertyType(propertyDefinition.getType());
996 protected Either<Resource, ResponseFormat> createOrUpdateArtifacts(ArtifactsBusinessLogic.ArtifactOperationEnum operation,
997 List<ArtifactDefinition> createdArtifacts, String yamlFileName,
998 CsarInfo csarInfo, Resource preparedResource,
999 NodeTypeInfoToUpdateArtifacts nodeTypeInfoToUpdateArtifacts,
1000 boolean inTransaction, boolean shouldLock) {
1001 String nodeName = nodeTypeInfoToUpdateArtifacts.getNodeName();
1002 Resource resource = preparedResource;
1003 Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle =
1004 nodeTypeInfoToUpdateArtifacts
1005 .getNodeTypesArtifactsToHandle();
1006 if (preparedResource.getResourceType() == ResourceTypeEnum.VF) {
1007 if (nodeName != null && nodeTypesArtifactsToHandle.get(nodeName) != null && !nodeTypesArtifactsToHandle.get(nodeName).isEmpty()) {
1008 Either<List<ArtifactDefinition>, ResponseFormat> handleNodeTypeArtifactsRes = handleNodeTypeArtifacts(preparedResource,
1009 nodeTypesArtifactsToHandle.get(nodeName), createdArtifacts, csarInfo.getModifier(), inTransaction, true);
1010 if (handleNodeTypeArtifactsRes.isRight()) {
1011 return Either.right(handleNodeTypeArtifactsRes.right().value());
1015 Either<Resource, ResponseFormat> createdCsarArtifactsEither = handleVfCsarArtifacts(preparedResource, csarInfo, createdArtifacts,
1016 new ArtifactOperationInfo(false, false, operation), shouldLock, inTransaction);
1017 log.trace("************* Finished to add artifacts from yaml {}", yamlFileName);
1018 if (createdCsarArtifactsEither.isRight()) {
1019 return createdCsarArtifactsEither;
1021 resource = createdCsarArtifactsEither.left().value();
1023 return Either.left(resource);
1026 protected Either<Resource, ResponseFormat> handleVfCsarArtifacts(Resource resource, CsarInfo csarInfo, List<ArtifactDefinition> createdArtifacts,
1027 ArtifactOperationInfo artifactOperation, boolean shouldLock,
1028 boolean inTransaction) {
1029 if (csarInfo.getCsar() != null) {
1030 createOrUpdateSingleNonMetaArtifactToComstants(resource, csarInfo, artifactOperation, shouldLock, inTransaction);
1031 Either<Resource, ResponseFormat> eitherCreateResult = createOrUpdateNonMetaArtifacts(csarInfo, resource, createdArtifacts, shouldLock,
1032 inTransaction, artifactOperation);
1033 if (eitherCreateResult.isRight()) {
1034 return Either.right(eitherCreateResult.right().value());
1036 Either<Resource, StorageOperationStatus> eitherGerResource = toscaOperationFacade.getToscaElement(resource.getUniqueId());
1037 if (eitherGerResource.isRight()) {
1038 ResponseFormat responseFormat = componentsUtils
1039 .getResponseFormatByResource(componentsUtils.convertFromStorageResponse(eitherGerResource.right().value()), resource);
1040 return Either.right(responseFormat);
1042 resource = eitherGerResource.left().value();
1043 Either<ImmutablePair<String, String>, ResponseFormat> artifacsMetaCsarStatus = CsarValidationUtils
1044 .getArtifactsMeta(csarInfo.getCsar(), csarInfo.getCsarUUID(), componentsUtils);
1045 if (artifacsMetaCsarStatus.isLeft()) {
1046 return getResourceResponseFormatEither(resource, csarInfo, createdArtifacts, artifactOperation, shouldLock, inTransaction,
1047 artifacsMetaCsarStatus);
1049 return csarArtifactsAndGroupsBusinessLogic.deleteVFModules(resource, csarInfo, shouldLock, inTransaction);
1052 return Either.left(resource);
1055 protected void createOrUpdateSingleNonMetaArtifactToComstants(Resource resource, CsarInfo csarInfo, ArtifactOperationInfo artifactOperation,
1056 boolean shouldLock, boolean inTransaction) {
1057 String vendorLicenseModelId = null;
1058 String vfLicenseModelId = null;
1059 if (artifactOperation.getArtifactOperationEnum() == ArtifactOperationEnum.UPDATE) {
1060 Map<String, ArtifactDefinition> deploymentArtifactsMap = resource.getDeploymentArtifacts();
1061 if (deploymentArtifactsMap != null && !deploymentArtifactsMap.isEmpty()) {
1062 for (Map.Entry<String, ArtifactDefinition> artifactEntry : deploymentArtifactsMap.entrySet()) {
1063 if (artifactEntry.getValue().getArtifactName().equalsIgnoreCase(Constants.VENDOR_LICENSE_MODEL)) {
1064 vendorLicenseModelId = artifactEntry.getValue().getUniqueId();
1066 if (artifactEntry.getValue().getArtifactName().equalsIgnoreCase(Constants.VF_LICENSE_MODEL)) {
1067 vfLicenseModelId = artifactEntry.getValue().getUniqueId();
1072 createOrUpdateSingleNonMetaArtifact(resource, csarInfo, CsarUtils.ARTIFACTS_PATH + Constants.VENDOR_LICENSE_MODEL,
1073 Constants.VENDOR_LICENSE_MODEL, ArtifactTypeEnum.VENDOR_LICENSE.getType(), ArtifactGroupTypeEnum.DEPLOYMENT,
1074 Constants.VENDOR_LICENSE_LABEL, Constants.VENDOR_LICENSE_DISPLAY_NAME, Constants.VENDOR_LICENSE_DESCRIPTION, vendorLicenseModelId,
1075 artifactOperation, null, true, shouldLock, inTransaction);
1076 createOrUpdateSingleNonMetaArtifact(resource, csarInfo, CsarUtils.ARTIFACTS_PATH + Constants.VF_LICENSE_MODEL, Constants.VF_LICENSE_MODEL,
1077 ArtifactTypeEnum.VF_LICENSE.getType(), ArtifactGroupTypeEnum.DEPLOYMENT, Constants.VF_LICENSE_LABEL, Constants.VF_LICENSE_DISPLAY_NAME,
1078 Constants.VF_LICENSE_DESCRIPTION, vfLicenseModelId, artifactOperation, null, true, shouldLock, inTransaction);
1081 private Either<Resource, ResponseFormat> getResourceResponseFormatEither(Resource resource, CsarInfo csarInfo,
1082 List<ArtifactDefinition> createdArtifacts,
1083 ArtifactOperationInfo artifactOperation, boolean shouldLock,
1084 boolean inTransaction,
1085 Either<ImmutablePair<String, String>, ResponseFormat> artifacsMetaCsarStatus) {
1087 String artifactsFileName = artifacsMetaCsarStatus.left().value().getKey();
1088 String artifactsContents = artifacsMetaCsarStatus.left().value().getValue();
1089 Either<Resource, ResponseFormat> createArtifactsFromCsar;
1090 if (ArtifactOperationEnum.isCreateOrLink(artifactOperation.getArtifactOperationEnum())) {
1091 createArtifactsFromCsar = csarArtifactsAndGroupsBusinessLogic
1092 .createResourceArtifactsFromCsar(csarInfo, resource, artifactsContents, artifactsFileName, createdArtifacts);
1094 Either<Component, ResponseFormat> result = csarArtifactsAndGroupsBusinessLogic
1095 .updateResourceArtifactsFromCsar(csarInfo, resource, artifactsContents, artifactsFileName, createdArtifacts, shouldLock,
1097 if ((result.left().value() instanceof Resource) && result.isLeft()) {
1098 Resource service1 = (Resource) result.left().value();
1099 createArtifactsFromCsar = Either.left(service1);
1101 createArtifactsFromCsar = Either.right(result.right().value());
1104 if (createArtifactsFromCsar.isRight()) {
1105 log.debug("Couldn't create artifacts from artifacts.meta");
1106 return Either.right(createArtifactsFromCsar.right().value());
1108 return Either.left(createArtifactsFromCsar.left().value());
1109 } catch (Exception e) {
1110 log.debug("Exception occured in getResourceResponseFormatEither, message:{}", e.getMessage(), e);
1111 return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
1115 private <T extends Component> Either<T, ResponseFormat> createOrUpdateNonMetaArtifactsComp(CsarInfo csarInfo, T component,
1116 List<ArtifactDefinition> createdArtifacts,
1117 boolean shouldLock, boolean inTransaction,
1118 ArtifactOperationInfo artifactOperation) {
1119 Either<T, ResponseFormat> resStatus = null;
1120 Map<String, Set<List<String>>> collectedWarningMessages = new HashMap<>();
1122 Either<List<CsarUtils.NonMetaArtifactInfo>, String> artifactPathAndNameList = getValidArtifactNames(csarInfo, collectedWarningMessages);
1123 if (artifactPathAndNameList.isRight()) {
1124 return Either.right(
1125 getComponentsUtils().getResponseFormatByArtifactId(ActionStatus.ARTIFACT_NAME_INVALID, artifactPathAndNameList.right().value()));
1127 EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<CsarUtils.NonMetaArtifactInfo>> vfCsarArtifactsToHandle = null;
1128 if (ArtifactsBusinessLogic.ArtifactOperationEnum.isCreateOrLink(artifactOperation.getArtifactOperationEnum())) {
1129 vfCsarArtifactsToHandle = new EnumMap<>(ArtifactsBusinessLogic.ArtifactOperationEnum.class);
1130 vfCsarArtifactsToHandle.put(artifactOperation.getArtifactOperationEnum(), artifactPathAndNameList.left().value());
1132 Either<EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<CsarUtils.NonMetaArtifactInfo>>, ResponseFormat>
1133 findVfCsarArtifactsToHandleRes = findVfCsarArtifactsToHandle(
1134 component, artifactPathAndNameList.left().value(), csarInfo.getModifier());
1135 if (findVfCsarArtifactsToHandleRes.isRight()) {
1136 resStatus = Either.right(findVfCsarArtifactsToHandleRes.right().value());
1138 if (resStatus == null) {
1139 vfCsarArtifactsToHandle = findVfCsarArtifactsToHandleRes.left().value();
1142 if (resStatus == null && vfCsarArtifactsToHandle != null) {
1143 resStatus = processCsarArtifacts(csarInfo, component, createdArtifacts, shouldLock, inTransaction, resStatus,
1144 vfCsarArtifactsToHandle);
1146 if (resStatus == null) {
1147 resStatus = Either.left(component);
1149 } catch (Exception e) {
1150 resStatus = Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
1151 log.debug("Exception occured in createNonMetaArtifacts, message:{}", e.getMessage(), e);
1153 CsarUtils.handleWarningMessages(collectedWarningMessages);
1158 protected Either<Resource, ResponseFormat> createOrUpdateNonMetaArtifacts(CsarInfo csarInfo, Resource resource,
1159 List<ArtifactDefinition> createdArtifacts, boolean shouldLock,
1160 boolean inTransaction, ArtifactOperationInfo artifactOperation) {
1161 return createOrUpdateNonMetaArtifactsComp(csarInfo, resource, createdArtifacts, shouldLock, inTransaction, artifactOperation);
1164 protected <T extends Component> Either<T, ResponseFormat> processCsarArtifacts(CsarInfo csarInfo, Component comp,
1165 List<ArtifactDefinition> createdArtifacts, boolean shouldLock,
1166 boolean inTransaction, Either<T, ResponseFormat> resStatus,
1167 EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<CsarUtils.NonMetaArtifactInfo>> vfCsarArtifactsToHandle) {
1168 for (Map.Entry<ArtifactsBusinessLogic.ArtifactOperationEnum, List<CsarUtils.NonMetaArtifactInfo>> currArtifactOperationPair : vfCsarArtifactsToHandle
1170 Optional<ResponseFormat> optionalCreateInDBError = currArtifactOperationPair.getValue().stream().map(
1171 e -> createOrUpdateSingleNonMetaArtifact(comp, csarInfo, e.getPath(), e.getArtifactName(), e.getArtifactType(),
1172 e.getArtifactGroupType(), e.getArtifactLabel(), e.getDisplayName(), CsarUtils.ARTIFACT_CREATED_FROM_CSAR, e.getArtifactUniqueId(),
1173 new ArtifactOperationInfo(false, false, currArtifactOperationPair.getKey()), createdArtifacts, e.isFromCsar(), shouldLock,
1174 inTransaction)).filter(Either::isRight).map(e -> e.right().value()).findAny();
1175 if (optionalCreateInDBError.isPresent()) {
1176 resStatus = Either.right(optionalCreateInDBError.get());
1183 protected Either<Boolean, ResponseFormat> createOrUpdateSingleNonMetaArtifact(Component component, CsarInfo csarInfo, String artifactPath,
1184 String artifactFileName, String artifactType,
1185 ArtifactGroupTypeEnum artifactGroupType, String artifactLabel,
1186 String artifactDisplayName, String artifactDescription,
1187 String artifactId, ArtifactOperationInfo operation,
1188 List<ArtifactDefinition> createdArtifacts, boolean isFromCsar,
1189 boolean shouldLock, boolean inTransaction) {
1190 byte[] artifactFileBytes = null;
1191 if (csarInfo.getCsar().containsKey(artifactPath)) {
1192 artifactFileBytes = csarInfo.getCsar().get(artifactPath);
1194 Either<Boolean, ResponseFormat> result = Either.left(true);
1195 if (operation.getArtifactOperationEnum() == ArtifactsBusinessLogic.ArtifactOperationEnum.UPDATE
1196 || operation.getArtifactOperationEnum() == ArtifactsBusinessLogic.ArtifactOperationEnum.DELETE) {
1197 if (serviceImportParseLogic.isArtifactDeletionRequired(artifactId, artifactFileBytes, isFromCsar)) {
1198 Either<ArtifactDefinition, ResponseFormat> handleDelete = artifactsBusinessLogic
1199 .handleDelete(component.getUniqueId(), artifactId, csarInfo.getModifier(), component, shouldLock, inTransaction);
1200 if (handleDelete.isRight()) {
1201 result = Either.right(handleDelete.right().value());
1205 if (org.apache.commons.lang.StringUtils.isEmpty(artifactId) && artifactFileBytes != null) {
1206 operation = new ArtifactOperationInfo(false, false, ArtifactsBusinessLogic.ArtifactOperationEnum.CREATE);
1209 if (artifactFileBytes != null) {
1210 Map<String, Object> vendorLicenseModelJson = ArtifactUtils
1211 .buildJsonForUpdateArtifact(artifactId, artifactFileName, artifactType, artifactGroupType, artifactLabel, artifactDisplayName,
1212 artifactDescription, artifactFileBytes, null, isFromCsar);
1213 Either<Either<ArtifactDefinition, Operation>, ResponseFormat> eitherNonMetaArtifacts = csarArtifactsAndGroupsBusinessLogic
1214 .createOrUpdateCsarArtifactFromJson(component, csarInfo.getModifier(), vendorLicenseModelJson, operation);
1215 serviceImportParseLogic.addNonMetaCreatedArtifactsToSupportRollback(operation, createdArtifacts, eitherNonMetaArtifacts);
1216 if (eitherNonMetaArtifacts.isRight()) {
1217 BeEcompErrorManager.getInstance().logInternalFlowError("UploadLicenseArtifact",
1218 "Failed to upload license artifact: " + artifactFileName + "With csar uuid: " + csarInfo.getCsarUUID(),
1219 BeEcompErrorManager.ErrorSeverity.WARNING);
1220 return Either.right(eitherNonMetaArtifacts.right().value());
1226 private Either<List<ArtifactDefinition>, ResponseFormat> handleNodeTypeArtifacts(Resource nodeTypeResource,
1227 Map<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>> nodeTypeArtifactsToHandle,
1228 List<ArtifactDefinition> createdArtifacts, User user,
1229 boolean inTransaction, boolean ignoreLifecycleState) {
1230 List<ArtifactDefinition> handleNodeTypeArtifactsRequestRes;
1231 Either<List<ArtifactDefinition>, ResponseFormat> handleNodeTypeArtifactsRes = null;
1232 Either<Resource, ResponseFormat> changeStateResponse;
1234 changeStateResponse = checkoutResource(nodeTypeResource, user, inTransaction);
1235 if (changeStateResponse.isRight()) {
1236 return Either.right(changeStateResponse.right().value());
1238 nodeTypeResource = changeStateResponse.left().value();
1239 List<ArtifactDefinition> handledNodeTypeArtifacts = new ArrayList<>();
1240 log.debug("************* Going to handle artifacts of node type resource {}. ", nodeTypeResource.getName());
1241 for (Map.Entry<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>> curOperationEntry : nodeTypeArtifactsToHandle
1243 ArtifactsBusinessLogic.ArtifactOperationEnum curOperation = curOperationEntry.getKey();
1244 List<ArtifactDefinition> curArtifactsToHandle = curOperationEntry.getValue();
1245 if (curArtifactsToHandle != null && !curArtifactsToHandle.isEmpty()) {
1246 log.debug("************* Going to {} artifact to vfc {}", curOperation.name(), nodeTypeResource.getName());
1247 handleNodeTypeArtifactsRequestRes = artifactsBusinessLogic
1248 .handleArtifactsRequestForInnerVfcComponent(curArtifactsToHandle, nodeTypeResource, user, createdArtifacts,
1249 new ArtifactOperationInfo(false, ignoreLifecycleState, curOperation), false, inTransaction);
1250 if (ArtifactsBusinessLogic.ArtifactOperationEnum.isCreateOrLink(curOperation)) {
1251 createdArtifacts.addAll(handleNodeTypeArtifactsRequestRes);
1253 handledNodeTypeArtifacts.addAll(handleNodeTypeArtifactsRequestRes);
1256 handleNodeTypeArtifactsRes = Either.left(handledNodeTypeArtifacts);
1257 } catch (Exception e) {
1258 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR);
1259 handleNodeTypeArtifactsRes = Either.right(responseFormat);
1260 log.debug("Exception occured when handleVfcArtifacts, error is:{}", e.getMessage(), e);
1262 return handleNodeTypeArtifactsRes;
1265 private Either<Resource, ResponseFormat> checkoutResource(Resource resource, User user, boolean inTransaction) {
1266 Either<Resource, ResponseFormat> checkoutResourceRes;
1268 if (!resource.getComponentMetadataDefinition().getMetadataDataDefinition().getState()
1269 .equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name())) {
1270 Either<? extends Component, ResponseFormat> checkoutRes = lifecycleBusinessLogic
1271 .changeComponentState(resource.getComponentType(), resource.getUniqueId(), user, LifeCycleTransitionEnum.CHECKOUT,
1272 new LifecycleChangeInfoWithAction(CERTIFICATION_ON_IMPORT,
1273 LifecycleChangeInfoWithAction.LifecycleChanceActionEnum.CREATE_FROM_CSAR), inTransaction, true);
1274 if (checkoutRes.isRight()) {
1275 checkoutResourceRes = Either.right(checkoutRes.right().value());
1277 checkoutResourceRes = Either.left((Resource) checkoutRes.left().value());
1280 checkoutResourceRes = Either.left(resource);
1282 } catch (Exception e) {
1283 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR);
1284 checkoutResourceRes = Either.right(responseFormat);
1285 log.debug("Exception occured when checkoutResource {} , error is:{}", resource.getName(), e.getMessage(), e);
1287 return checkoutResourceRes;
1290 protected Either<Service, ResponseFormat> createOrUpdateArtifacts(ArtifactOperationEnum operation, List<ArtifactDefinition> createdArtifacts,
1291 String yamlFileName, CsarInfo csarInfo, Service preparedService,
1292 boolean inTransaction, boolean shouldLock) {
1293 Either<Service, ResponseFormat> createdCsarArtifactsEither = handleVfCsarArtifacts(preparedService, csarInfo, createdArtifacts,
1294 new ArtifactOperationInfo(false, false, operation), shouldLock, inTransaction);
1295 log.trace("************* Finished to add artifacts from yaml {}", yamlFileName);
1296 if (createdCsarArtifactsEither.isRight()) {
1297 return createdCsarArtifactsEither;
1299 return Either.left(createdCsarArtifactsEither.left().value());
1302 protected Either<Service, ResponseFormat> handleVfCsarArtifacts(Service service, CsarInfo csarInfo, List<ArtifactDefinition> createdArtifacts,
1303 ArtifactOperationInfo artifactOperation, boolean shouldLock,
1304 boolean inTransaction) {
1305 if (csarInfo.getCsar() != null) {
1306 String vendorLicenseModelId = null;
1307 String vfLicenseModelId = null;
1308 if (artifactOperation.getArtifactOperationEnum() == ArtifactsBusinessLogic.ArtifactOperationEnum.UPDATE) {
1309 Map<String, ArtifactDefinition> deploymentArtifactsMap = service.getDeploymentArtifacts();
1310 if (deploymentArtifactsMap != null && !deploymentArtifactsMap.isEmpty()) {
1311 for (Map.Entry<String, ArtifactDefinition> artifactEntry : deploymentArtifactsMap.entrySet()) {
1312 if (artifactEntry.getValue().getArtifactName().equalsIgnoreCase(Constants.VENDOR_LICENSE_MODEL)) {
1313 vendorLicenseModelId = artifactEntry.getValue().getUniqueId();
1315 if (artifactEntry.getValue().getArtifactName().equalsIgnoreCase(Constants.VF_LICENSE_MODEL)) {
1316 vfLicenseModelId = artifactEntry.getValue().getUniqueId();
1321 createOrUpdateSingleNonMetaArtifact(service, csarInfo, CsarUtils.ARTIFACTS_PATH + Constants.VENDOR_LICENSE_MODEL,
1322 Constants.VENDOR_LICENSE_MODEL, ArtifactTypeEnum.VENDOR_LICENSE.getType(), ArtifactGroupTypeEnum.DEPLOYMENT,
1323 Constants.VENDOR_LICENSE_LABEL, Constants.VENDOR_LICENSE_DISPLAY_NAME, Constants.VENDOR_LICENSE_DESCRIPTION, vendorLicenseModelId,
1324 artifactOperation, null, true, shouldLock, inTransaction);
1325 createOrUpdateSingleNonMetaArtifact(service, csarInfo, CsarUtils.ARTIFACTS_PATH + Constants.VF_LICENSE_MODEL, Constants.VF_LICENSE_MODEL,
1326 ArtifactTypeEnum.VF_LICENSE.getType(), ArtifactGroupTypeEnum.DEPLOYMENT, Constants.VF_LICENSE_LABEL,
1327 Constants.VF_LICENSE_DISPLAY_NAME, Constants.VF_LICENSE_DESCRIPTION, vfLicenseModelId, artifactOperation, null, true, shouldLock,
1329 Either<Service, ResponseFormat> eitherCreateResult = createOrUpdateNonMetaArtifacts(csarInfo, service, createdArtifacts, shouldLock,
1330 inTransaction, artifactOperation);
1331 if (eitherCreateResult.isRight()) {
1332 return Either.right(eitherCreateResult.right().value());
1334 Either<Service, StorageOperationStatus> eitherGerResource = toscaOperationFacade.getToscaElement(service.getUniqueId());
1335 if (eitherGerResource.isRight()) {
1336 ResponseFormat responseFormat = componentsUtils
1337 .getResponseFormatByComponent(componentsUtils.convertFromStorageResponse(eitherGerResource.right().value()), service,
1338 ComponentTypeEnum.SERVICE);
1339 return Either.right(responseFormat);
1341 service = eitherGerResource.left().value();
1342 Either<ImmutablePair<String, String>, ResponseFormat> artifacsMetaCsarStatus = CsarValidationUtils
1343 .getArtifactsMeta(csarInfo.getCsar(), csarInfo.getCsarUUID(), componentsUtils);
1344 if (artifacsMetaCsarStatus.isLeft()) {
1345 String artifactsFileName = artifacsMetaCsarStatus.left().value().getKey();
1346 String artifactsContents = artifacsMetaCsarStatus.left().value().getValue();
1347 Either<Service, ResponseFormat> createArtifactsFromCsar;
1348 if (ArtifactsBusinessLogic.ArtifactOperationEnum.isCreateOrLink(artifactOperation.getArtifactOperationEnum())) {
1349 createArtifactsFromCsar = csarArtifactsAndGroupsBusinessLogic
1350 .createResourceArtifactsFromCsar(csarInfo, service, artifactsContents, artifactsFileName, createdArtifacts);
1352 Either<Component, ResponseFormat> result = csarArtifactsAndGroupsBusinessLogic
1353 .updateResourceArtifactsFromCsar(csarInfo, service, artifactsContents, artifactsFileName, createdArtifacts, shouldLock,
1355 if ((result.left().value() instanceof Service) && result.isLeft()) {
1356 Service service1 = (Service) result.left().value();
1357 createArtifactsFromCsar = Either.left(service1);
1359 createArtifactsFromCsar = Either.right(result.right().value());
1362 if (createArtifactsFromCsar.isRight()) {
1363 log.debug("Couldn't create artifacts from artifacts.meta");
1364 return Either.right(createArtifactsFromCsar.right().value());
1366 return Either.left(createArtifactsFromCsar.left().value());
1368 return csarArtifactsAndGroupsBusinessLogic.deleteVFModules(service, csarInfo, shouldLock, inTransaction);
1371 return Either.left(service);
1374 protected Either<Service, ResponseFormat> createOrUpdateNonMetaArtifacts(CsarInfo csarInfo, Service resource,
1375 List<ArtifactDefinition> createdArtifacts, boolean shouldLock,
1376 boolean inTransaction, ArtifactOperationInfo artifactOperation) {
1377 return createOrUpdateNonMetaArtifactsComp(csarInfo, resource, createdArtifacts, shouldLock, inTransaction, artifactOperation);
1380 protected Either<EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<CsarUtils.NonMetaArtifactInfo>>, ResponseFormat> findVfCsarArtifactsToHandle(
1381 Component component, List<CsarUtils.NonMetaArtifactInfo> artifactPathAndNameList, User user) {
1382 List<ArtifactDefinition> existingArtifacts = new ArrayList<>();
1383 if (component.getDeploymentArtifacts() != null && !component.getDeploymentArtifacts().isEmpty()) {
1384 existingArtifacts.addAll(component.getDeploymentArtifacts().values());
1386 if (component.getArtifacts() != null && !component.getArtifacts().isEmpty()) {
1387 existingArtifacts.addAll(component.getArtifacts().values());
1389 existingArtifacts = existingArtifacts.stream().filter(this::isNonMetaArtifact).collect(toList());
1390 List<String> artifactsToIgnore = new ArrayList<>();
1391 if (component.getGroups() != null) {
1392 component.getGroups().forEach(g -> {
1393 if (g.getArtifacts() != null && !g.getArtifacts().isEmpty()) {
1394 artifactsToIgnore.addAll(g.getArtifacts());
1398 existingArtifacts = existingArtifacts.stream().filter(a -> !artifactsToIgnore.contains(a.getUniqueId())).collect(toList());
1399 return organizeVfCsarArtifactsByArtifactOperation(artifactPathAndNameList, existingArtifacts, component, user);
1402 private boolean isNonMetaArtifact(ArtifactDefinition artifact) {
1403 boolean result = true;
1404 if (artifact.getMandatory() || artifact.getArtifactName() == null || !isValidArtifactType(artifact)) {
1410 private boolean isValidArtifactType(ArtifactDefinition artifact) {
1411 final String artifactType = artifact.getArtifactType();
1412 return artifactType != null
1413 && !ArtifactTypeEnum.VENDOR_LICENSE.getType().equals(ArtifactTypeEnum.findType(artifactType))
1414 && !ArtifactTypeEnum.VF_LICENSE.getType().equals(ArtifactTypeEnum.findType(artifactType));
1417 protected Either<EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<CsarUtils.NonMetaArtifactInfo>>, ResponseFormat> organizeVfCsarArtifactsByArtifactOperation(
1418 List<CsarUtils.NonMetaArtifactInfo> artifactPathAndNameList, List<ArtifactDefinition> existingArtifactsToHandle, Component component,
1420 EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<CsarUtils.NonMetaArtifactInfo>> nodeTypeArtifactsToHandle = new EnumMap<>(
1421 ArtifactsBusinessLogic.ArtifactOperationEnum.class);
1422 Wrapper<ResponseFormat> responseWrapper = new Wrapper<>();
1423 Either<EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<CsarUtils.NonMetaArtifactInfo>>, ResponseFormat>
1424 nodeTypeArtifactsToHandleRes = Either
1425 .left(nodeTypeArtifactsToHandle);
1427 List<CsarUtils.NonMetaArtifactInfo> artifactsToUpload = new ArrayList<>(artifactPathAndNameList);
1428 List<CsarUtils.NonMetaArtifactInfo> artifactsToUpdate = new ArrayList<>();
1429 List<CsarUtils.NonMetaArtifactInfo> artifactsToDelete = new ArrayList<>();
1430 for (CsarUtils.NonMetaArtifactInfo currNewArtifact : artifactPathAndNameList) {
1431 ArtifactDefinition foundArtifact;
1432 if (!existingArtifactsToHandle.isEmpty()) {
1433 foundArtifact = existingArtifactsToHandle.stream().filter(a -> a.getArtifactName().equals(currNewArtifact.getArtifactName()))
1434 .findFirst().orElse(null);
1435 if (foundArtifact != null) {
1436 if (ArtifactTypeEnum.findType(foundArtifact.getArtifactType()).equals(currNewArtifact.getArtifactType())) {
1437 if (!foundArtifact.getArtifactChecksum().equals(currNewArtifact.getArtifactChecksum())) {
1438 currNewArtifact.setArtifactUniqueId(foundArtifact.getUniqueId());
1439 artifactsToUpdate.add(currNewArtifact);
1441 existingArtifactsToHandle.remove(foundArtifact);
1442 artifactsToUpload.remove(currNewArtifact);
1444 log.debug("Can't upload two artifact with the same name {}.", currNewArtifact.getArtifactName());
1445 ResponseFormat responseFormat = ResponseFormatManager.getInstance()
1446 .getResponseFormat(ActionStatus.ARTIFACT_ALREADY_EXIST_IN_DIFFERENT_TYPE_IN_CSAR, currNewArtifact.getArtifactName(),
1447 currNewArtifact.getArtifactType(), foundArtifact.getArtifactType());
1448 AuditingActionEnum auditingAction = artifactsBusinessLogic.detectAuditingType(
1449 new ArtifactOperationInfo(false, false, ArtifactsBusinessLogic.ArtifactOperationEnum.CREATE),
1450 foundArtifact.getArtifactChecksum());
1451 artifactsBusinessLogic.handleAuditing(auditingAction, component, component.getUniqueId(), user, null, null,
1452 foundArtifact.getUniqueId(), responseFormat, component.getComponentType(), null);
1453 responseWrapper.setInnerElement(responseFormat);
1459 if (responseWrapper.isEmpty()) {
1460 for (ArtifactDefinition currArtifact : existingArtifactsToHandle) {
1461 if (currArtifact.getIsFromCsar()) {
1462 artifactsToDelete.add(new CsarUtils.NonMetaArtifactInfo(currArtifact.getArtifactName(), null,
1463 ArtifactTypeEnum.findType(currArtifact.getArtifactType()), currArtifact.getArtifactGroupType(), null,
1464 currArtifact.getUniqueId(), currArtifact.getIsFromCsar()));
1466 artifactsToUpdate.add(new CsarUtils.NonMetaArtifactInfo(currArtifact.getArtifactName(), null,
1467 ArtifactTypeEnum.findType(currArtifact.getArtifactType()), currArtifact.getArtifactGroupType(), null,
1468 currArtifact.getUniqueId(), currArtifact.getIsFromCsar()));
1472 if (responseWrapper.isEmpty()) {
1473 if (!artifactsToUpload.isEmpty()) {
1474 nodeTypeArtifactsToHandle.put(ArtifactsBusinessLogic.ArtifactOperationEnum.CREATE, artifactsToUpload);
1476 if (!artifactsToUpdate.isEmpty()) {
1477 nodeTypeArtifactsToHandle.put(ArtifactsBusinessLogic.ArtifactOperationEnum.UPDATE, artifactsToUpdate);
1479 if (!artifactsToDelete.isEmpty()) {
1480 nodeTypeArtifactsToHandle.put(ArtifactsBusinessLogic.ArtifactOperationEnum.DELETE, artifactsToDelete);
1483 if (!responseWrapper.isEmpty()) {
1484 nodeTypeArtifactsToHandleRes = Either.right(responseWrapper.getInnerElement());
1486 } catch (Exception e) {
1487 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR);
1488 responseWrapper.setInnerElement(responseFormat);
1489 log.debug("Exception occured when findNodeTypeArtifactsToHandle, error is:{}", e.getMessage(), e);
1490 nodeTypeArtifactsToHandleRes = Either.right(responseWrapper.getInnerElement());
1492 return nodeTypeArtifactsToHandleRes;
1495 protected Either<List<CsarUtils.NonMetaArtifactInfo>, String> getValidArtifactNames(CsarInfo csarInfo,
1496 Map<String, Set<List<String>>> collectedWarningMessages) {
1497 List<CsarUtils.NonMetaArtifactInfo> artifactPathAndNameList = csarInfo.getCsar().entrySet().stream()
1498 .filter(e -> Pattern.compile(VF_NODE_TYPE_ARTIFACTS_PATH_PATTERN).matcher(e.getKey()).matches())
1499 .map(e -> CsarUtils.validateNonMetaArtifact(e.getKey(), e.getValue(), collectedWarningMessages)).filter(Either::isLeft)
1500 .map(e -> e.left().value()).collect(toList());
1501 Pattern englishNumbersAndUnderScoresOnly = Pattern.compile(CsarUtils.VALID_ENGLISH_ARTIFACT_NAME);
1502 for (CsarUtils.NonMetaArtifactInfo nonMetaArtifactInfo : artifactPathAndNameList) {
1503 if (!englishNumbersAndUnderScoresOnly.matcher(nonMetaArtifactInfo.getDisplayName()).matches()) {
1504 return Either.right(nonMetaArtifactInfo.getArtifactName());
1507 return Either.left(artifactPathAndNameList);
1510 protected Either<Service, ResponseFormat> createGroupsOnResource(Service service, Map<String, GroupDefinition> groups) {
1511 if (groups != null && !groups.isEmpty()) {
1512 List<GroupDefinition> groupsAsList = updateGroupsMembersUsingResource(groups, service);
1513 serviceImportParseLogic.handleGroupsProperties(service, groups);
1514 serviceImportParseLogic.fillGroupsFinalFields(groupsAsList);
1515 Either<List<GroupDefinition>, ResponseFormat> createGroups = groupBusinessLogic.createGroups(service, groupsAsList, true);
1516 if (createGroups.isRight()) {
1517 return Either.right(createGroups.right().value());
1520 return Either.left(service);
1522 return getServiceResponseFormatEither(service);
1525 private Either<Service, ResponseFormat> createPoliciesOnResource(final Service service,
1526 final Map<String, PolicyDefinition> policies) {
1527 if (MapUtils.isEmpty(policies)) {
1528 return Either.left(service);
1530 Map<String, List<ComponentInstanceAttribute>> componentInstancesAttributes = service.getComponentInstancesAttributes();
1531 final Map<String, List<AttributeDefinition>> instanceAttributeMap = new HashMap<>();
1532 if (MapUtils.isNotEmpty(componentInstancesAttributes)) {
1533 instanceAttributeMap.putAll(componentInstancesAttributes
1534 .entrySet().stream()
1535 .collect(toMap(Entry::getKey, entry -> entry.getValue().stream().map(AttributeDefinition.class::cast).collect(toList()))));
1537 policies.values().stream()
1538 .map(PolicyDataDefinition::getProperties)
1539 .flatMap(Collection::stream)
1540 .filter(PropertyDataDefinition::isToscaFunction)
1541 .forEach(policyDefinition -> toscaFunctionService
1542 .updateFunctionWithDataFromSelfComponent(policyDefinition.getToscaFunction(), service, service.getComponentInstancesProperties(),
1543 instanceAttributeMap)
1545 policyBusinessLogic.createPolicies(service, policies);
1546 return getServiceResponseFormatEither(service);
1549 private Either<Service, ResponseFormat> getServiceResponseFormatEither(Service service) {
1550 Either<Service, StorageOperationStatus> updatedResource = toscaOperationFacade.getToscaElement(service.getUniqueId());
1551 if (updatedResource.isRight()) {
1552 ResponseFormat responseFormat = componentsUtils
1553 .getResponseFormatByComponent(componentsUtils.convertFromStorageResponse(updatedResource.right().value()), service,
1554 ComponentTypeEnum.SERVICE);
1555 return Either.right(responseFormat);
1557 return Either.left(updatedResource.left().value());
1560 protected List<GroupDefinition> updateGroupsMembersUsingResource(Map<String, GroupDefinition> groups, Service component) {
1561 List<GroupDefinition> result = new ArrayList<>();
1562 List<ComponentInstance> componentInstances = component.getComponentInstances();
1563 if (groups != null) {
1564 for (Map.Entry<String, GroupDefinition> entry : groups.entrySet()) {
1565 String groupName = entry.getKey();
1566 GroupDefinition groupDefinition = entry.getValue();
1567 GroupDefinition updatedGroupDefinition = new GroupDefinition(groupDefinition);
1568 updatedGroupDefinition.setMembers(null);
1569 Map<String, String> members = groupDefinition.getMembers();
1570 if (members != null) {
1571 serviceImportParseLogic.updateGroupMembers(groups, updatedGroupDefinition, component, componentInstances, groupName, members);
1573 result.add(updatedGroupDefinition);
1579 protected Resource createRIAndRelationsFromYaml(String yamlName, Resource resource,
1580 Map<String, UploadComponentInstanceInfo> uploadComponentInstanceInfoMap,
1581 String topologyTemplateYaml, List<ArtifactDefinition> nodeTypesNewCreatedArtifacts,
1582 Map<String, NodeTypeInfo> nodeTypesInfo, CsarInfo csarInfo,
1583 Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToCreate,
1586 log.debug("************* Going to create all nodes {}", yamlName);
1587 handleNodeTypes(yamlName, resource, topologyTemplateYaml, false, nodeTypesArtifactsToCreate, nodeTypesNewCreatedArtifacts, nodeTypesInfo,
1588 csarInfo, nodeName);
1589 log.debug("************* Going to create all resource instances {}", yamlName);
1590 resource = createResourceInstances(yamlName, resource, uploadComponentInstanceInfoMap, csarInfo.getCreatedNodes());
1591 log.debug("************* Finished to create all resource instances {}", yamlName);
1592 resource = createResourceInstancesRelations(csarInfo.getModifier(), yamlName, resource, uploadComponentInstanceInfoMap);
1593 log.debug("************* Going to create positions {}", yamlName);
1594 compositionBusinessLogic.setPositionsForComponentInstances(resource, csarInfo.getModifier().getUserId());
1595 log.debug("************* Finished to set positions {}", yamlName);
1597 } catch (Exception e) {
1598 throw new ComponentException(ActionStatus.GENERAL_ERROR);
1602 protected Resource createResourceInstancesRelations(User user, String yamlName, Resource resource,
1603 Map<String, UploadComponentInstanceInfo> uploadResInstancesMap) {
1604 log.debug("#createResourceInstancesRelations - Going to create relations ");
1605 List<ComponentInstance> componentInstancesList = resource.getComponentInstances();
1606 if (((MapUtils.isEmpty(uploadResInstancesMap) || CollectionUtils.isEmpty(componentInstancesList)) &&
1607 resource.getResourceType() != ResourceTypeEnum.PNF)) { // PNF can have no resource instances
1608 log.debug("#createResourceInstancesRelations - No instances found in the resource {} is empty, yaml template file name {}, ",
1609 resource.getUniqueId(), yamlName);
1610 BeEcompErrorManager.getInstance()
1611 .logInternalDataError("createResourceInstancesRelations", "No instances found in a resource or nn yaml template. ",
1612 BeEcompErrorManager.ErrorSeverity.ERROR);
1613 throw new ComponentException(componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName));
1615 Map<String, List<ComponentInstanceProperty>> instProperties = new HashMap<>();
1616 Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> instCapabilities = new HashMap<>();
1617 Map<ComponentInstance, Map<String, List<RequirementDefinition>>> instRequirements = new HashMap<>();
1618 Map<String, Map<String, ArtifactDefinition>> instDeploymentArtifacts = new HashMap<>();
1619 Map<String, Map<String, ArtifactDefinition>> instArtifacts = new HashMap<>();
1620 Map<String, List<AttributeDefinition>> instAttributes = new HashMap<>();
1621 Map<String, Resource> originCompMap = new HashMap<>();
1622 List<RequirementCapabilityRelDef> relations = new ArrayList<>();
1623 Map<String, List<ComponentInstanceInput>> instInputs = new HashMap<>();
1624 Map<String, UploadNodeFilterInfo> instNodeFilter = new HashMap<>();
1625 Map<String, Map<String, InterfaceDefinition>> instInterfaces = new HashMap<>();
1626 log.debug("enter ServiceImportBusinessLogic createResourceInstancesRelations#createResourceInstancesRelations - Before get all datatypes. ");
1627 final ApplicationDataTypeCache applicationDataTypeCache = serviceBusinessLogic.applicationDataTypeCache;
1628 if (applicationDataTypeCache != null) {
1629 Resource finalResource = resource;
1630 uploadResInstancesMap.values().forEach(
1631 i -> processComponentInstance(yamlName, finalResource, componentInstancesList,
1632 componentsUtils.getAllDataTypes(applicationDataTypeCache, finalResource.getModel()), instProperties, instCapabilities,
1633 instRequirements, instDeploymentArtifacts, instArtifacts, instAttributes, originCompMap, instInputs, instNodeFilter,
1634 instInterfaces, i));
1636 serviceImportParseLogic.associateComponentInstancePropertiesToComponent(yamlName, resource, instProperties);
1637 serviceImportParseLogic.associateComponentInstanceInputsToComponent(yamlName, resource, instInputs);
1638 serviceImportParseLogic.associateDeploymentArtifactsToInstances(user, yamlName, resource, instDeploymentArtifacts);
1639 serviceImportParseLogic.associateArtifactsToInstances(yamlName, resource, instArtifacts);
1640 serviceImportParseLogic.associateOrAddCalculatedCapReq(yamlName, resource, instCapabilities, instRequirements);
1641 serviceImportParseLogic.associateInstAttributeToComponentToInstances(yamlName, resource, instAttributes);
1642 resource = serviceImportParseLogic.getResourceAfterCreateRelations(resource);
1643 serviceImportParseLogic.addRelationsToRI(yamlName, resource, uploadResInstancesMap, componentInstancesList, relations);
1644 serviceImportParseLogic.associateResourceInstances(yamlName, resource, relations);
1645 handleSubstitutionMappings(resource, uploadResInstancesMap);
1646 log.debug("************* in create relations, getResource start");
1647 Either<Resource, StorageOperationStatus> eitherGetResource = toscaOperationFacade.getToscaElement(resource.getUniqueId());
1648 log.debug("************* in create relations, getResource end");
1649 if (eitherGetResource.isRight()) {
1650 throw new ComponentException(
1651 componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(eitherGetResource.right().value()), resource));
1653 return eitherGetResource.left().value();
1656 protected void processProperty(Resource resource, Map<String, DataTypeDefinition> allDataTypes,
1657 Map<String, InputDefinition> currPropertiesMap, List<ComponentInstanceInput> instPropList,
1658 List<UploadPropInfo> propertyList) {
1659 UploadPropInfo propertyInfo = propertyList.get(0);
1660 String propName = propertyInfo.getName();
1661 if (!currPropertiesMap.containsKey(propName)) {
1662 throw new ComponentException(componentsUtils.getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND, propName));
1664 processProperty(allDataTypes, currPropertiesMap, instPropList, propertyInfo, propName, resource.getInputs());
1667 private void processProperty(Map<String, DataTypeDefinition> allDataTypes, Map<String, InputDefinition> currPropertiesMap,
1668 List<ComponentInstanceInput> instPropList, UploadPropInfo propertyInfo, String propName,
1669 List<InputDefinition> inputs2) {
1670 InputDefinition curPropertyDef = currPropertiesMap.get(propName);
1671 ComponentInstanceInput property = null;
1672 String value = null;
1673 List<GetInputValueDataDefinition> getInputs = null;
1674 boolean isValidate = true;
1675 if (propertyInfo.getValue() != null) {
1676 getInputs = propertyInfo.getGet_input();
1677 isValidate = getInputs == null || getInputs.isEmpty();
1679 value = getPropertyJsonStringValue(propertyInfo.getValue(), curPropertyDef.getType());
1681 value = getPropertyJsonStringValue(propertyInfo.getValue(), TypeUtils.ToscaTagNamesEnum.GET_INPUT.getElementName());
1684 property = new ComponentInstanceInput(curPropertyDef, value, null);
1685 String validPropertyVAlue = serviceBusinessLogic.validatePropValueBeforeCreate(property, value, isValidate, allDataTypes);
1686 property.setValue(validPropertyVAlue);
1687 if (isNotEmpty(getInputs)) {
1688 List<GetInputValueDataDefinition> getInputValues = new ArrayList<>();
1689 for (GetInputValueDataDefinition getInput : getInputs) {
1690 List<InputDefinition> inputs = inputs2;
1691 if (CollectionUtils.isEmpty(inputs)) {
1692 throw new ComponentException(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT));
1694 Optional<InputDefinition> optional = inputs.stream().filter(p -> p.getName().equals(getInput.getInputName())).findAny();
1695 if (!optional.isPresent()) {
1696 throw new ComponentException(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT));
1698 InputDefinition input = optional.get();
1699 getInput.setInputId(input.getUniqueId());
1700 getInputValues.add(getInput);
1701 GetInputValueDataDefinition getInputIndex = getInput.getGetInputIndex();
1702 processGetInput(getInputValues, inputs, getInputIndex);
1704 property.setGetInputValues(getInputValues);
1706 instPropList.add(property);
1707 currPropertiesMap.remove(property.getName());
1710 protected void handleSubstitutionMappings(Resource resource, Map<String, UploadComponentInstanceInfo> uploadResInstancesMap) {
1711 if (resource.getResourceType() == ResourceTypeEnum.VF) {
1712 Either<Resource, StorageOperationStatus> getResourceRes = toscaOperationFacade.getToscaFullElement(resource.getUniqueId());
1713 if (getResourceRes.isRight()) {
1714 ResponseFormat responseFormat = componentsUtils
1715 .getResponseFormatByResource(componentsUtils.convertFromStorageResponse(getResourceRes.right().value()), resource);
1716 throw new ComponentException(responseFormat);
1718 getResourceRes = updateCalculatedCapReqWithSubstitutionMappings(getResourceRes.left().value(), uploadResInstancesMap);
1719 if (getResourceRes.isRight()) {
1720 ResponseFormat responseFormat = componentsUtils
1721 .getResponseFormatByResource(componentsUtils.convertFromStorageResponse(getResourceRes.right().value()), resource);
1722 throw new ComponentException(responseFormat);
1727 protected Resource createResourceInstances(String yamlName, Resource resource, Map<String, UploadComponentInstanceInfo> uploadResInstancesMap,
1728 Map<String, Resource> nodeNamespaceMap) {
1729 Either<Resource, ResponseFormat> eitherResource = null;
1730 log.debug("createResourceInstances is {} - going to create resource instanse from CSAR", yamlName);
1731 if (MapUtils.isEmpty(uploadResInstancesMap) && resource.getResourceType() != ResourceTypeEnum.PNF) { // PNF can have no resource instances
1732 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE);
1733 throw new ComponentException(responseFormat);
1735 Map<String, Resource> existingNodeTypeMap = new HashMap<>();
1736 if (MapUtils.isNotEmpty(nodeNamespaceMap)) {
1737 nodeNamespaceMap.forEach((k, v) -> existingNodeTypeMap.put(v.getToscaResourceName(), v));
1739 Map<ComponentInstance, Resource> resourcesInstancesMap = new HashMap<>();
1740 uploadResInstancesMap.values()
1741 .forEach(i -> createAndAddResourceInstance(i, yamlName, resource, nodeNamespaceMap, existingNodeTypeMap, resourcesInstancesMap));
1742 if (MapUtils.isNotEmpty(resourcesInstancesMap)) {
1744 toscaOperationFacade.associateComponentInstancesToComponent(resource, resourcesInstancesMap, false, false);
1745 } catch (StorageException exp) {
1746 if (exp.getStorageOperationStatus() != null && exp.getStorageOperationStatus() != StorageOperationStatus.OK) {
1747 log.debug("Failed to add component instances to container component {}", resource.getName());
1748 ResponseFormat responseFormat = componentsUtils
1749 .getResponseFormat(componentsUtils.convertFromStorageResponse(exp.getStorageOperationStatus()));
1750 eitherResource = Either.right(responseFormat);
1751 throw new ByResponseFormatComponentException(eitherResource.right().value());
1755 log.debug("*************Going to get resource {}", resource.getUniqueId());
1756 Either<Resource, StorageOperationStatus> eitherGetResource = toscaOperationFacade
1757 .getToscaElement(resource.getUniqueId(), serviceImportParseLogic.getComponentWithInstancesFilter());
1758 log.debug("*************finished to get resource {}", resource.getUniqueId());
1759 if (eitherGetResource.isRight()) {
1760 ResponseFormat responseFormat = componentsUtils
1761 .getResponseFormatByResource(componentsUtils.convertFromStorageResponse(eitherGetResource.right().value()), resource);
1762 throw new ComponentException(responseFormat);
1764 if (CollectionUtils.isEmpty(eitherGetResource.left().value().getComponentInstances()) &&
1765 resource.getResourceType() != ResourceTypeEnum.PNF) { // PNF can have no resource instances
1766 log.debug("Error when create resource instance from csar. ComponentInstances list empty");
1767 BeEcompErrorManager.getInstance().logBeDaoSystemError("Error when create resource instance from csar. ComponentInstances list empty");
1768 throw new ComponentException(componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE));
1770 return eitherGetResource.left().value();
1773 protected void handleNodeTypes(String yamlName, Resource resource, String topologyTemplateYaml, boolean needLock,
1774 Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle,
1775 List<ArtifactDefinition> nodeTypesNewCreatedArtifacts, Map<String, NodeTypeInfo> nodeTypesInfo, CsarInfo csarInfo,
1778 for (Map.Entry<String, NodeTypeInfo> nodeTypeEntry : nodeTypesInfo.entrySet()) {
1779 if (nodeTypeEntry.getValue().isNested()) {
1780 handleNestedVfc(resource, nodeTypesArtifactsToHandle, nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo,
1781 nodeTypeEntry.getKey());
1782 log.trace("************* finished to create node {}", nodeTypeEntry.getKey());
1785 Map<String, Object> mappedToscaTemplate = null;
1786 if (org.apache.commons.lang.StringUtils.isNotEmpty(nodeName) && MapUtils.isNotEmpty(nodeTypesInfo) && nodeTypesInfo
1787 .containsKey(nodeName)) {
1788 mappedToscaTemplate = nodeTypesInfo.get(nodeName).getMappedToscaTemplate();
1790 if (MapUtils.isEmpty(mappedToscaTemplate)) {
1791 mappedToscaTemplate = (Map<String, Object>) new Yaml().load(topologyTemplateYaml);
1793 createResourcesFromYamlNodeTypesList(yamlName, resource, mappedToscaTemplate, needLock, nodeTypesArtifactsToHandle,
1794 nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo);
1795 } catch (ComponentException e) {
1796 ResponseFormat responseFormat =
1797 e.getResponseFormat() != null ? e.getResponseFormat() : componentsUtils.getResponseFormat(e.getActionStatus(), e.getParams());
1798 componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), resource, AuditingActionEnum.IMPORT_RESOURCE);
1800 } catch (StorageException e) {
1801 ResponseFormat responseFormat = componentsUtils
1802 .getResponseFormat(componentsUtils.convertFromStorageResponse(e.getStorageOperationStatus()));
1803 componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), resource, AuditingActionEnum.IMPORT_RESOURCE);
1805 } catch (Exception e) {
1806 log.debug("Exception occured when handleNodeTypes, error is:{}", e.getMessage(), e);
1807 throw new ComponentException(ActionStatus.GENERAL_ERROR);
1811 protected Resource handleNestedVfc(Service service,
1812 Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodesArtifactsToHandle,
1813 List<ArtifactDefinition> createdArtifacts, Map<String, NodeTypeInfo> nodesInfo, CsarInfo csarInfo,
1816 String yamlName = nodesInfo.get(nodeName).getTemplateFileName();
1817 Map<String, Object> nestedVfcJsonMap = nodesInfo.get(nodeName).getMappedToscaTemplate();
1818 createResourcesFromYamlNodeTypesList(yamlName, service, nestedVfcJsonMap, false, nodesArtifactsToHandle, createdArtifacts, nodesInfo,
1820 log.debug("************* Finished to create node types from yaml {}", yamlName);
1821 if (nestedVfcJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.TOPOLOGY_TEMPLATE.getElementName())) {
1822 log.debug("************* Going to handle complex VFC from yaml {}", yamlName);
1823 return handleComplexVfc(nodesArtifactsToHandle, createdArtifacts, nodesInfo, csarInfo, nodeName, yamlName);
1825 return new Resource();
1826 } catch (Exception e) {
1827 log.debug("Exception occured when handleNestedVFc, error is:{}", e.getMessage(), e);
1828 throw new ComponentException(ActionStatus.GENERAL_ERROR);
1832 protected Resource handleNestedVfc(Resource resource,
1833 Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>> nodesArtifactsToHandle,
1834 List<ArtifactDefinition> createdArtifacts, Map<String, NodeTypeInfo> nodesInfo, CsarInfo csarInfo,
1836 String yamlName = nodesInfo.get(nodeName).getTemplateFileName();
1837 Map<String, Object> nestedVfcJsonMap = nodesInfo.get(nodeName).getMappedToscaTemplate();
1838 log.debug("************* Going to create node types from yaml {}", yamlName);
1839 createResourcesFromYamlNodeTypesList(yamlName, resource, nestedVfcJsonMap, false, nodesArtifactsToHandle, createdArtifacts, nodesInfo,
1841 if (nestedVfcJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.TOPOLOGY_TEMPLATE.getElementName())) {
1842 log.debug("************* Going to handle complex VFC from yaml {}", yamlName);
1843 resource = handleComplexVfc(resource, nodesArtifactsToHandle, createdArtifacts, nodesInfo, csarInfo, nodeName, yamlName);
1848 protected Resource handleComplexVfc(Resource resource,
1849 Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>> nodesArtifactsToHandle,
1850 List<ArtifactDefinition> createdArtifacts, Map<String, NodeTypeInfo> nodesInfo, CsarInfo csarInfo,
1851 String nodeName, String yamlName) {
1852 Resource oldComplexVfc = null;
1853 Resource newComplexVfc = serviceImportParseLogic.buildValidComplexVfc(resource, csarInfo, nodeName, nodesInfo);
1854 Either<Resource, StorageOperationStatus> oldComplexVfcRes = toscaOperationFacade
1855 .getFullLatestComponentByToscaResourceName(newComplexVfc.getToscaResourceName());
1856 if (oldComplexVfcRes.isRight() && oldComplexVfcRes.right().value() == StorageOperationStatus.NOT_FOUND) {
1857 oldComplexVfcRes = toscaOperationFacade.getFullLatestComponentByToscaResourceName(
1858 serviceImportParseLogic.buildNestedToscaResourceName(ResourceTypeEnum.VF.name(), csarInfo.getVfResourceName(), nodeName).getRight());
1860 if (oldComplexVfcRes.isRight() && oldComplexVfcRes.right().value() != StorageOperationStatus.NOT_FOUND) {
1861 throw new ComponentException(ActionStatus.GENERAL_ERROR);
1862 } else if (oldComplexVfcRes.isLeft()) {
1863 log.debug(VALIDATE_DERIVED_BEFORE_UPDATE);
1864 Either<Boolean, ResponseFormat> eitherValidation = serviceImportParseLogic
1865 .validateNestedDerivedFromDuringUpdate(oldComplexVfcRes.left().value(), newComplexVfc,
1866 ValidationUtils.hasBeenCertified(oldComplexVfcRes.left().value().getVersion()));
1867 if (eitherValidation.isLeft()) {
1868 oldComplexVfc = oldComplexVfcRes.left().value();
1871 newComplexVfc = handleComplexVfc(nodesArtifactsToHandle, createdArtifacts, nodesInfo, csarInfo, nodeName, yamlName, oldComplexVfc,
1873 csarInfo.getCreatedNodesToscaResourceNames().put(nodeName, newComplexVfc.getToscaResourceName());
1874 LifecycleChangeInfoWithAction lifecycleChangeInfo = new LifecycleChangeInfoWithAction(CERTIFICATION_ON_IMPORT,
1875 LifecycleChangeInfoWithAction.LifecycleChanceActionEnum.CREATE_FROM_CSAR);
1876 log.debug("Going to certify cvfc {}. ", newComplexVfc.getName());
1877 final Resource result = serviceImportParseLogic
1878 .propagateStateToCertified(csarInfo.getModifier(), newComplexVfc, lifecycleChangeInfo, true, false, true);
1879 csarInfo.getCreatedNodes().put(nodeName, result);
1880 csarInfo.removeNodeFromQueue();
1884 private Map<String, Resource> createResourcesFromYamlNodeTypesList(String yamlName, Resource resource, Map<String, Object> mappedToscaTemplate,
1886 Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle,
1887 List<ArtifactDefinition> nodeTypesNewCreatedArtifacts,
1888 Map<String, NodeTypeInfo> nodeTypesInfo, CsarInfo csarInfo) {
1889 Either<String, ImportUtils.ResultStatusEnum> toscaVersion = findFirstToscaStringElement(mappedToscaTemplate,
1890 TypeUtils.ToscaTagNamesEnum.TOSCA_VERSION);
1891 if (toscaVersion.isRight()) {
1892 throw new ComponentException(ActionStatus.INVALID_TOSCA_TEMPLATE);
1894 Map<String, Object> mapToConvert = new HashMap<>();
1895 mapToConvert.put(TypeUtils.ToscaTagNamesEnum.TOSCA_VERSION.getElementName(), toscaVersion.left().value());
1896 Map<String, Object> nodeTypes = serviceImportParseLogic.getNodeTypesFromTemplate(mappedToscaTemplate);
1897 createNodeTypes(yamlName, resource, needLock, nodeTypesArtifactsToHandle, nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo, mapToConvert,
1899 return csarInfo.getCreatedNodes();
1902 protected void createNodeTypes(String yamlName, Resource resource, boolean needLock,
1903 Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle,
1904 List<ArtifactDefinition> nodeTypesNewCreatedArtifacts, Map<String, NodeTypeInfo> nodeTypesInfo, CsarInfo csarInfo,
1905 Map<String, Object> mapToConvert, Map<String, Object> nodeTypes) {
1906 Iterator<Map.Entry<String, Object>> nodesNameValueIter = nodeTypes.entrySet().iterator();
1907 Resource vfcCreated = null;
1908 while (nodesNameValueIter.hasNext()) {
1909 Map.Entry<String, Object> nodeType = nodesNameValueIter.next();
1910 Map<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>> nodeTypeArtifactsToHandle =
1911 nodeTypesArtifactsToHandle == null || nodeTypesArtifactsToHandle.isEmpty() ? null : nodeTypesArtifactsToHandle.get(nodeType.getKey());
1912 if (nodeTypesInfo.containsKey(nodeType.getKey())) {
1913 log.trace("************* Going to handle nested vfc {}", nodeType.getKey());
1914 vfcCreated = handleNestedVfc(resource, nodeTypesArtifactsToHandle, nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo,
1916 log.trace("************* Finished to handle nested vfc {}", nodeType.getKey());
1917 } else if (csarInfo.getCreatedNodesToscaResourceNames() != null && !csarInfo.getCreatedNodesToscaResourceNames()
1918 .containsKey(nodeType.getKey())) {
1919 log.trace("************* Going to create node {}", nodeType.getKey());
1920 ImmutablePair<Resource, ActionStatus> resourceCreated = createNodeTypeResourceFromYaml(yamlName, nodeType, csarInfo.getModifier(),
1921 mapToConvert, resource, needLock, nodeTypeArtifactsToHandle, nodeTypesNewCreatedArtifacts, true, csarInfo, true);
1922 log.debug("************* Finished to create node {}", nodeType.getKey());
1923 vfcCreated = resourceCreated.getLeft();
1924 csarInfo.getCreatedNodesToscaResourceNames().put(nodeType.getKey(), vfcCreated.getToscaResourceName());
1926 if (vfcCreated != null) {
1927 csarInfo.getCreatedNodes().put(nodeType.getKey(), vfcCreated);
1929 mapToConvert.remove(TypeUtils.ToscaTagNamesEnum.NODE_TYPES.getElementName());
1933 protected ImmutablePair<Resource, ActionStatus> createNodeTypeResourceFromYaml(String yamlName, Map.Entry<String, Object> nodeNameValue,
1934 User user, Map<String, Object> mapToConvert, Resource resourceVf,
1936 Map<ArtifactOperationEnum, List<ArtifactDefinition>> nodeTypeArtifactsToHandle,
1937 List<ArtifactDefinition> nodeTypesNewCreatedArtifacts,
1938 boolean forceCertificationAllowed, CsarInfo csarInfo,
1940 final var validatedUser = serviceBusinessLogic.validateUser(user, "CheckIn Resource", resourceVf, AuditingActionEnum.CHECKIN_RESOURCE,
1942 UploadResourceInfo resourceMetaData = serviceImportParseLogic.fillResourceMetadata(yamlName, resourceVf, nodeNameValue.getKey(),
1944 String singleVfcYaml = serviceImportParseLogic.buildNodeTypeYaml(nodeNameValue, mapToConvert, resourceMetaData.getResourceType(), csarInfo);
1945 return serviceImportParseLogic.createResourceFromNodeType(singleVfcYaml, resourceMetaData, validatedUser, true, needLock,
1946 nodeTypeArtifactsToHandle,
1947 nodeTypesNewCreatedArtifacts, forceCertificationAllowed, csarInfo, nodeNameValue.getKey(), isNested);
1950 protected Service createRIAndRelationsFromYaml(String yamlName, Service service,
1951 Map<String, UploadComponentInstanceInfo> uploadComponentInstanceInfoMap,
1952 String topologyTemplateYaml, List<ArtifactDefinition> nodeTypesNewCreatedArtifacts,
1953 Map<String, NodeTypeInfo> nodeTypesInfo, CsarInfo csarInfo,
1954 Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToCreate,
1956 log.debug("************* Going to create all nodes {}", yamlName);
1957 handleServiceNodeTypes(yamlName, service, topologyTemplateYaml, false, nodeTypesArtifactsToCreate, nodeTypesNewCreatedArtifacts,
1958 nodeTypesInfo, csarInfo, nodeName);
1959 List<PropertyDefinition> serviceProperties = null != service ? service.getProperties() : Collections.emptyList();
1960 if (MapUtils.isNotEmpty(uploadComponentInstanceInfoMap)) {
1961 log.debug("************* Going to create all resource instances {}", yamlName);
1962 service = createServiceInstances(yamlName, service, uploadComponentInstanceInfoMap, csarInfo.getCreatedNodes());
1963 log.debug("************* Going to create all relations {}", yamlName);
1964 service = createServiceInstancesRelations(csarInfo.getModifier(), yamlName, service, uploadComponentInstanceInfoMap, serviceProperties);
1965 log.debug("************* Going to create positions {}", yamlName);
1966 compositionBusinessLogic.setPositionsForComponentInstances(service, csarInfo.getModifier().getUserId());
1967 log.debug("************* Finished to set positions {}", yamlName);
1972 protected Service createServiceInstancesRelations(User user, String yamlName, Service service,
1973 Map<String, UploadComponentInstanceInfo> uploadResInstancesMap,
1974 List<PropertyDefinition> serviceProperties) {
1975 log.debug("#createResourceInstancesRelations - Going to create relations ");
1976 List<ComponentInstance> componentInstancesList = service.getComponentInstances();
1977 if (MapUtils.isEmpty(uploadResInstancesMap) || CollectionUtils.isEmpty(componentInstancesList)) { // PNF can have no resource instances
1978 log.debug("#createResourceInstancesRelations - No instances found in the resource {} is empty, yaml template file name {}, ",
1979 service.getUniqueId(), yamlName);
1980 BeEcompErrorManager.getInstance()
1981 .logInternalDataError("createResourceInstancesRelations", "No instances found in a component or nn yaml template. ",
1982 BeEcompErrorManager.ErrorSeverity.ERROR);
1983 throw new ComponentException(componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName));
1985 Map<String, List<ComponentInstanceProperty>> instProperties = new HashMap<>();
1986 Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> instCapabilities = new HashMap<>();
1987 Map<ComponentInstance, Map<String, List<RequirementDefinition>>> instRequirements = new HashMap<>();
1988 Map<String, Map<String, ArtifactDefinition>> instDeploymentArtifacts = new HashMap<>();
1989 Map<String, Map<String, ArtifactDefinition>> instArtifacts = new HashMap<>();
1990 Map<String, List<AttributeDefinition>> instAttributes = new HashMap<>();
1991 Map<String, Resource> originCompMap = new HashMap<>();
1992 List<RequirementCapabilityRelDef> relations = new ArrayList<>();
1993 Map<String, List<ComponentInstanceInput>> instInputs = new HashMap<>();
1994 Map<String, UploadNodeFilterInfo> instNodeFilter = new HashMap<>();
1995 Map<String, Map<String, InterfaceDefinition>> instInterfaces = new HashMap<>();
1996 log.debug("enter ServiceImportBusinessLogic createServiceInstancesRelations#createResourceInstancesRelations - Before get all datatypes. ");
1997 final ApplicationDataTypeCache applicationDataTypeCache = serviceBusinessLogic.applicationDataTypeCache;
1998 if (applicationDataTypeCache != null) {
1999 final Map<String, DataTypeDefinition> allDataTypesMap =
2000 componentsUtils.getAllDataTypes(applicationDataTypeCache, service.getModel());
2001 final Service service1 = service;
2002 service1.setProperties(serviceProperties);
2003 uploadResInstancesMap.values().forEach(
2004 i -> processComponentInstance(yamlName, service1, componentInstancesList,
2005 allDataTypesMap, instProperties,
2006 instCapabilities, instRequirements, instDeploymentArtifacts, instArtifacts, instAttributes, originCompMap, instInputs,
2007 instNodeFilter, instInterfaces, i)
2010 updatePropertyToscaFunctionData(service, instProperties, instAttributes);
2011 serviceImportParseLogic.associateComponentInstancePropertiesToComponent(yamlName, service, instProperties);
2012 serviceImportParseLogic.associateComponentInstanceInterfacesToComponent(
2017 serviceImportParseLogic.associateComponentInstanceInputsToComponent(yamlName, service, instInputs);
2018 serviceImportParseLogic.associateCINodeFilterToComponent(yamlName, service, instNodeFilter);
2019 serviceImportParseLogic.associateDeploymentArtifactsToInstances(user, yamlName, service, instDeploymentArtifacts);
2020 serviceImportParseLogic.associateArtifactsToInstances(yamlName, service, instArtifacts);
2021 serviceImportParseLogic.associateOrAddCalculatedCapReq(yamlName, service, instCapabilities, instRequirements);
2022 log.debug("enter createServiceInstancesRelations test,instRequirements:{},instCapabilities:{}", instRequirements, instCapabilities);
2023 serviceImportParseLogic.associateInstAttributeToComponentToInstances(yamlName, service, instAttributes);
2024 ToscaElement serviceTemplate = ModelConverter.convertToToscaElement(service);
2025 Map<String, ListCapabilityDataDefinition> capabilities = serviceTemplate.getCapabilities();
2026 Map<String, ListRequirementDataDefinition> requirements = serviceTemplate.getRequirements();
2027 serviceImportParseLogic.associateCapabilitiesToService(yamlName, service, capabilities);
2028 serviceImportParseLogic.associateRequirementsToService(yamlName, service, requirements);
2029 service = getResourceAfterCreateRelations(service);
2030 addRelationsToRI(yamlName, service, uploadResInstancesMap, componentInstancesList, relations);
2031 serviceImportParseLogic.associateResourceInstances(yamlName, service, relations);
2032 log.debug("************* in create relations, getResource start");
2033 Either<Service, StorageOperationStatus> eitherGetResource = toscaOperationFacade.getToscaElement(service.getUniqueId());
2034 log.debug("************* in create relations, getResource end");
2035 if (eitherGetResource.isRight()) {
2036 throw new ComponentException(componentsUtils
2037 .getResponseFormatByComponent(componentsUtils.convertFromStorageResponse(eitherGetResource.right().value()), service,
2038 service.getComponentType()));
2040 return eitherGetResource.left().value();
2043 private void updatePropertyToscaFunctionData(final Component service,
2044 final Map<String, List<ComponentInstanceProperty>> instancePropertyMap,
2045 final Map<String, List<AttributeDefinition>> instanceAttributeMap) {
2046 final Component updatedService =
2047 toscaOperationFacade.getToscaElement(service.getUniqueId()).left()
2048 .on(storageOperationStatus -> {
2049 final ActionStatus status = componentsUtils.convertFromStorageResponse(storageOperationStatus);
2050 final ResponseFormat responseFormat =
2051 componentsUtils.getResponseFormatByComponent(status, service, service.getComponentType());
2052 throw new ComponentException(responseFormat);
2055 instancePropertyMap.values().forEach(instancePropertyList ->
2056 instancePropertyList.stream()
2057 .filter(PropertyDataDefinition::isToscaFunction)
2058 .forEach(instanceProperty -> {
2059 toscaFunctionService.updateFunctionWithDataFromSelfComponent(instanceProperty.getToscaFunction(),
2060 updatedService, instancePropertyMap, instanceAttributeMap);
2061 instanceProperty.setValue(StringEscapeUtils.unescapeJava(instanceProperty.getToscaFunction().getValue()));
2066 protected void processComponentInstance(String yamlName, Component component, List<ComponentInstance> componentInstancesList,
2067 Map<String, DataTypeDefinition> allDataTypes,
2068 Map<String, List<ComponentInstanceProperty>> instProperties,
2069 Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> instCapabilties,
2070 Map<ComponentInstance, Map<String, List<RequirementDefinition>>> instRequirements,
2071 Map<String, Map<String, ArtifactDefinition>> instDeploymentArtifacts,
2072 Map<String, Map<String, ArtifactDefinition>> instArtifacts,
2073 Map<String, List<AttributeDefinition>> instAttributes, Map<String, Resource> originCompMap,
2074 Map<String, List<ComponentInstanceInput>> instInputs,
2075 Map<String, UploadNodeFilterInfo> instNodeFilter,
2076 Map<String, Map<String, InterfaceDefinition>> instInterfaces,
2077 UploadComponentInstanceInfo uploadComponentInstanceInfo) {
2078 log.debug("enter ServiceImportBusinessLogic processComponentInstance");
2079 Optional<ComponentInstance> currentCompInstanceOpt = componentInstancesList.stream()
2080 .filter(i -> i.getName().equals(uploadComponentInstanceInfo.getName())).findFirst();
2081 if (currentCompInstanceOpt.isEmpty()) {
2082 log.debug(COMPONENT_INSTANCE_WITH_NAME_IN_RESOURCE, uploadComponentInstanceInfo.getName(), component.getUniqueId());
2083 BeEcompErrorManager.getInstance()
2084 .logInternalDataError(COMPONENT_INSTANCE_WITH_NAME + uploadComponentInstanceInfo.getName() + IN_RESOURCE, component.getUniqueId(),
2085 BeEcompErrorManager.ErrorSeverity.ERROR);
2086 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName);
2087 throw new ComponentException(responseFormat);
2089 ComponentInstance currentCompInstance = currentCompInstanceOpt.get();
2090 String resourceInstanceId = currentCompInstance.getUniqueId();
2091 Resource originResource = getOriginResource(yamlName, originCompMap, currentCompInstance);
2092 if (MapUtils.isNotEmpty(originResource.getRequirements())) {
2093 instRequirements.put(currentCompInstance, originResource.getRequirements());
2095 if (MapUtils.isNotEmpty(originResource.getCapabilities())) {
2096 processComponentInstanceCapabilities(allDataTypes, instCapabilties, uploadComponentInstanceInfo, currentCompInstance, originResource);
2098 if (originResource.getDeploymentArtifacts() != null && !originResource.getDeploymentArtifacts().isEmpty()) {
2099 instDeploymentArtifacts.put(resourceInstanceId, originResource.getDeploymentArtifacts());
2101 if (originResource.getArtifacts() != null && !originResource.getArtifacts().isEmpty()) {
2102 instArtifacts.put(resourceInstanceId, originResource.getArtifacts());
2104 if (originResource.getAttributes() != null && !originResource.getAttributes().isEmpty()) {
2105 instAttributes.put(resourceInstanceId, originResource.getAttributes());
2106 addAttributeValueToResourceInstance(instAttributes, uploadComponentInstanceInfo.getAttributes());
2108 if (uploadComponentInstanceInfo.getUploadNodeFilterInfo() == null) {
2109 instNodeFilter.put(resourceInstanceId, new UploadNodeFilterInfo());
2111 instNodeFilter.put(resourceInstanceId, uploadComponentInstanceInfo.getUploadNodeFilterInfo());
2113 if (MapUtils.isNotEmpty(uploadComponentInstanceInfo.getInterfaces())) {
2115 ResponseFormat addInterfacesToRiRes = addInterfaceValuesToRi(
2116 uploadComponentInstanceInfo,
2119 currentCompInstance,
2122 if (addInterfacesToRiRes.getStatus() != 200) {
2123 throw new ComponentException(addInterfacesToRiRes);
2126 if (originResource.getResourceType() != ResourceTypeEnum.VF) {
2127 ResponseFormat addPropertiesValueToRiRes = addPropertyValuesToRi(uploadComponentInstanceInfo, component, originResource,
2128 currentCompInstance, instProperties, allDataTypes);
2129 if (addPropertiesValueToRiRes.getStatus() != 200) {
2130 throw new ComponentException(addPropertiesValueToRiRes);
2133 addInputsValuesToRi(uploadComponentInstanceInfo, component, originResource, currentCompInstance, instInputs, allDataTypes);
2137 protected void addInputsValuesToRi(UploadComponentInstanceInfo uploadComponentInstanceInfo, Component component, Resource originResource,
2138 ComponentInstance currentCompInstance, Map<String, List<ComponentInstanceInput>> instInputs,
2139 Map<String, DataTypeDefinition> allDataTypes) {
2140 Map<String, List<UploadPropInfo>> propMap = uploadComponentInstanceInfo.getProperties();
2142 if (MapUtils.isNotEmpty(propMap)) {
2143 Map<String, InputDefinition> currPropertiesMap = new HashMap<>();
2144 List<ComponentInstanceInput> instPropList = new ArrayList<>();
2145 if (CollectionUtils.isEmpty(originResource.getInputs())) {
2146 log.debug("failed to find properties ");
2147 throw new ComponentException(componentsUtils.getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND));
2149 originResource.getInputs().forEach(p -> serviceImportParseLogic.addInput(currPropertiesMap, p));
2150 for (List<UploadPropInfo> propertyList : propMap.values()) {
2151 processProperty(component, allDataTypes, currPropertiesMap, instPropList, propertyList);
2153 currPropertiesMap.values().forEach(p -> instPropList.add(new ComponentInstanceInput(p)));
2154 instInputs.put(currentCompInstance.getUniqueId(), instPropList);
2156 } catch (Exception e) {
2157 log.debug("failed to add Inputs Values To Ri");
2158 throw new ComponentException(ActionStatus.GENERAL_ERROR);
2162 protected void processProperty(Component component, Map<String, DataTypeDefinition> allDataTypes,
2163 Map<String, InputDefinition> currPropertiesMap, List<ComponentInstanceInput> instPropList,
2164 List<UploadPropInfo> propertyList) {
2165 UploadPropInfo propertyInfo = propertyList.get(0);
2166 String propName = propertyInfo.getName();
2167 if (!currPropertiesMap.containsKey(propName)) {
2168 log.debug("failed to find property {} ", propName);
2169 throw new ComponentException(componentsUtils.getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND, propName));
2171 processProperty(allDataTypes, currPropertiesMap, instPropList, propertyInfo, propName, component.getInputs());
2174 protected void processGetInput(List<GetInputValueDataDefinition> getInputValues, List<InputDefinition> inputs,
2175 GetInputValueDataDefinition getInputIndex) {
2176 Optional<InputDefinition> optional;
2177 if (getInputIndex != null) {
2178 optional = inputs.stream().filter(p -> p.getName().equals(getInputIndex.getInputName())).findAny();
2179 if (!optional.isPresent()) {
2180 log.debug("Failed to find input {} ", getInputIndex.getInputName());
2181 throw new ComponentException(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT));
2183 InputDefinition inputIndex = optional.get();
2184 getInputIndex.setInputId(inputIndex.getUniqueId());
2185 getInputValues.add(getInputIndex);
2189 private void addAttributeValueToResourceInstance(Map<String, List<AttributeDefinition>> instAttributes,
2190 Map<String, UploadAttributeInfo> attributeMap) {
2191 if (attributeMap == null) {
2194 attributeMap.forEach((attributeName, attributeValue) -> instAttributes.values()
2195 .forEach(value -> value.stream().filter(attr -> attr.getName().equals(attributeName)).forEach(attr -> {
2196 if (attributeValue.getValue() instanceof Collection<?> || attributeValue.getValue() instanceof Map<?, ?>) {
2197 Gson gson = new Gson();
2198 String json = gson.toJson(attributeValue.getValue());
2199 attr.setValue(json);
2201 attr.setValue(String.valueOf(attributeValue.getValue()));
2206 protected ResponseFormat addPropertyValuesToRi(UploadComponentInstanceInfo uploadComponentInstanceInfo, Component component,
2207 Resource originResource, ComponentInstance currentCompInstance,
2208 Map<String, List<ComponentInstanceProperty>> instProperties,
2209 Map<String, DataTypeDefinition> allDataTypes) {
2210 Map<String, List<UploadPropInfo>> propMap = uploadComponentInstanceInfo.getProperties();
2211 Map<String, PropertyDefinition> currPropertiesMap = new HashMap<>();
2212 List<PropertyDefinition> originalPropertyList = originResource.getProperties();
2213 if (MapUtils.isNotEmpty(propMap) && CollectionUtils.isEmpty(originalPropertyList)) {
2214 log.debug("failed to find properties ");
2215 return componentsUtils.getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND);
2217 if (CollectionUtils.isEmpty(originalPropertyList)) {
2218 return componentsUtils.getResponseFormat(ActionStatus.OK);
2220 originalPropertyList.stream()
2221 .filter(property -> !currPropertiesMap.containsKey(property.getName()))
2222 .forEach(property -> currPropertiesMap.put(property.getName(), property));
2223 List<ComponentInstanceProperty> instPropList = new ArrayList<>();
2224 if (MapUtils.isNotEmpty(propMap)) {
2225 for (final List<UploadPropInfo> propertyList : propMap.values()) {
2226 UploadPropInfo propertyInfo = propertyList.get(0);
2227 String propName = propertyInfo.getName();
2228 if (!currPropertiesMap.containsKey(propName)) {
2229 log.debug("failed to find property {} ", propName);
2230 return componentsUtils.getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND, propName);
2232 PropertyDefinition curPropertyDef = currPropertiesMap.get(propName);
2233 String value = null;
2234 final List<GetInputValueDataDefinition> getInputs = new ArrayList<>();
2235 boolean isValidate = true;
2236 if (propertyInfo.getValue() != null) {
2237 getInputs.addAll(propertyInfo.getGet_input());
2238 isValidate = getInputs.isEmpty();
2240 value = getPropertyJsonStringValue(propertyInfo.getValue(), curPropertyDef.getType());
2242 value = getPropertyJsonStringValue(propertyInfo.getValue(), TypeUtils.ToscaTagNamesEnum.GET_INPUT.getElementName());
2245 final var property = new ComponentInstanceProperty(curPropertyDef, value, null);
2246 String validatedPropValue = serviceBusinessLogic.validatePropValueBeforeCreate(property, value, true, allDataTypes);
2248 addSubPropertyYamlToscaFunctions(validatedPropValue, value, property.getType(), propertyInfo, allDataTypes);
2250 if (CollectionUtils.isNotEmpty(propertyInfo.getSubPropertyToscaFunctions())) {
2251 validatedPropValue = value;
2254 property.setValue(validatedPropValue);
2256 if (tryHandlingAsYamlToscaFunction(validatedPropValue, value, propertyInfo)) {
2258 final Object yamlValue = new Yaml().loadAs(value, Object.class);
2259 CustomYamlFunction toscaFunction = new CustomYamlFunction();
2260 toscaFunction.setYamlValue(yamlValue);
2261 property.setToscaFunction(toscaFunction);
2262 } catch (Exception exception) {
2263 log.info("Cannot create YAML value for {}", propName);
2266 property.setToscaFunction(propertyInfo.getToscaFunction());
2268 property.setSubPropertyToscaFunctions(propertyInfo.getSubPropertyToscaFunctions());
2269 if (!getInputs.isEmpty() && CollectionUtils.isEmpty(property.getSubPropertyToscaFunctions())) {
2270 final List<GetInputValueDataDefinition> getInputValues = new ArrayList<>();
2271 for (final GetInputValueDataDefinition getInput : getInputs) {
2272 final List<InputDefinition> inputs = component.getInputs();
2273 if (inputs == null || inputs.isEmpty()) {
2274 log.debug("Failed to add property {} to instance. Inputs list is empty ", property);
2275 serviceBusinessLogic.rollbackWithException(ActionStatus.INPUTS_NOT_FOUND,
2276 property.getGetInputValues().stream().map(GetInputValueDataDefinition::getInputName).collect(toList()).toString());
2278 InputDefinition input = serviceImportParseLogic.findInputByName(inputs, getInput);
2279 getInput.setInputId(input.getUniqueId());
2280 getInputValues.add(getInput);
2281 GetInputValueDataDefinition getInputIndex = getInput.getGetInputIndex();
2282 if (getInputIndex != null) {
2283 input = serviceImportParseLogic.findInputByName(inputs, getInputIndex);
2284 getInputIndex.setInputId(input.getUniqueId());
2285 getInputValues.add(getInputIndex);
2288 property.setGetInputValues(getInputValues);
2290 instPropList.add(property);
2291 currPropertiesMap.remove(property.getName());
2294 if (!currPropertiesMap.isEmpty()) {
2295 for (PropertyDefinition value : currPropertiesMap.values()) {
2296 instPropList.add(new ComponentInstanceProperty(value));
2299 instProperties.put(currentCompInstance.getUniqueId(), instPropList);
2300 return componentsUtils.getResponseFormat(ActionStatus.OK);
2303 private boolean tryHandlingAsYamlToscaFunction(String validatedPropValue, String value, UploadPropInfo propertyInfo) {
2304 return StringUtils.isEmpty(validatedPropValue) && StringUtils.isNotEmpty(value) && propertyInfo.getToscaFunction() == null
2305 && CollectionUtils.isEmpty(propertyInfo.getSubPropertyToscaFunctions());
2308 private void addSubPropertyYamlToscaFunctions(final String validatedPropValue, final String value, final String propertyType,
2309 final UploadPropInfo propertyInfo, final Map<String, DataTypeDefinition> allDataTypes) {
2310 if (StringUtils.isNotEmpty(validatedPropValue) || StringUtils.isEmpty(value) || ToscaPropertyType.isValidType(propertyType) != null) {
2314 final JsonObject jsonObject = JsonParser.parseString(value).getAsJsonObject();
2316 final DataTypeDefinition dataTypeDefinition = allDataTypes.get(propertyType);
2317 final List<String> propertyNames =
2318 dataTypeDefinition.getProperties().stream().map(PropertyDataDefinition::getName).collect(Collectors.toList());
2320 boolean hasSubPropertyValues = jsonObject.entrySet().stream().allMatch(entry -> propertyNames.contains(entry.getKey()));
2322 if (hasSubPropertyValues) {
2323 for (final PropertyDefinition prop : dataTypeDefinition.getProperties()) {
2324 if (propertyInfo.getSubPropertyToscaFunctions().stream()
2325 .anyMatch(subPropertyToscaFunction -> subPropertyToscaFunction.getSubPropertyPath().get(0).equals(prop.getName()))) {
2328 Optional<SubPropertyToscaFunction> subPropertyToscaFunction = createSubPropertyYamlToscaFunction(jsonObject, prop, allDataTypes);
2329 if (subPropertyToscaFunction.isPresent()) {
2330 propertyInfo.getSubPropertyToscaFunctions().add(subPropertyToscaFunction.get());
2334 } catch (Exception exception) {
2335 log.info("Cannot create YAML value for {}", value);
2339 private Optional<SubPropertyToscaFunction> createSubPropertyYamlToscaFunction(final JsonObject jsonObject, final PropertyDefinition prop,
2340 final Map<String, DataTypeDefinition> allDataTypes) {
2341 JsonElement propJsonElement = jsonObject.get(prop.getName());
2342 if (propJsonElement != null) {
2343 final String subPropValue = propJsonElement.toString();
2344 final ComponentInstanceProperty subProperty = new ComponentInstanceProperty(prop, subPropValue, null);
2345 final String validateSubPropValue =
2346 serviceBusinessLogic.validatePropValueBeforeCreate(subProperty, subPropValue, true, allDataTypes);
2348 if (StringUtils.isEmpty(validateSubPropValue) && StringUtils.isNotEmpty(subPropValue)) {
2350 Object yamlValue = new Yaml().loadAs(subPropValue, Object.class);
2351 SubPropertyToscaFunction subPropertyToscaFunction = new SubPropertyToscaFunction();
2352 CustomYamlFunction toscaFunction = new CustomYamlFunction();
2353 toscaFunction.setYamlValue(yamlValue);
2354 subPropertyToscaFunction.setToscaFunction(toscaFunction);
2355 subPropertyToscaFunction.setSubPropertyPath(Collections.singletonList(prop.getName()));
2356 return Optional.of(subPropertyToscaFunction);
2357 } catch (Exception exception) {
2358 log.info("Cannot create YAML value for {}", subPropValue);
2362 return Optional.empty();
2365 protected ResponseFormat addInterfaceValuesToRi(
2366 UploadComponentInstanceInfo uploadComponentInstanceInfo,
2367 Component component,
2368 Resource originResource, ComponentInstance currentCompInstance,
2369 Map<String, Map<String, InterfaceDefinition>> instInterfaces
2371 Map<String, UploadInterfaceInfo> instanceInterfacesMap = uploadComponentInstanceInfo.getInterfaces();
2372 Map<String, InterfaceDefinition> currInterfacesMap = new HashMap<>();
2373 Map<String, InterfaceDefinition> interfacesFromNodeType = originResource.getInterfaces();
2374 if ((MapUtils.isNotEmpty(instanceInterfacesMap)) && (MapUtils.isEmpty(interfacesFromNodeType))) {
2375 log.debug("failed to find interfaces ");
2376 return componentsUtils.getResponseFormat(ActionStatus.INTERFACE_NOT_FOUND_IN_COMPONENT);
2378 if (interfacesFromNodeType == null || interfacesFromNodeType.isEmpty()) {
2379 return componentsUtils.getResponseFormat(ActionStatus.OK);
2381 for (Map.Entry<String, InterfaceDefinition> entryInstances : interfacesFromNodeType.entrySet()) {
2382 String interfaceName = entryInstances.getKey().substring(entryInstances.getKey().lastIndexOf(".") + 1);
2383 if (!currInterfacesMap.containsKey(interfaceName)) {
2384 currInterfacesMap.put(interfaceName, entryInstances.getValue());
2388 Map<String, InterfaceDefinition> instInterfacesMap = new HashMap<>();
2389 if (MapUtils.isNotEmpty(instanceInterfacesMap)) {
2390 for (UploadInterfaceInfo uploadInterfaceInfo : instanceInterfacesMap.values()) {
2391 String interfaceName = uploadInterfaceInfo.getName();
2392 if (!currInterfacesMap.containsKey(interfaceName)) {
2393 log.debug("failed to find interface {} ", interfaceName);
2394 return componentsUtils.getResponseFormat(ActionStatus.INTERFACE_NOT_FOUND_IN_COMPONENT, interfaceName);
2396 InterfaceDefinition currentInterfaceDef = currInterfacesMap.get(interfaceName);
2397 Map<String, OperationDataDefinition> operationsToAdd = new HashMap<>();
2399 Map<String, OperationDataDefinition> operations = uploadInterfaceInfo.getOperations();
2400 for (Map.Entry<String, OperationDataDefinition> operation : operations.entrySet()) {
2401 OperationDataDefinition instanceOperation = operation.getValue();
2402 OperationDataDefinition templateOperation = currentInterfaceDef.getOperationsMap()
2403 .getOrDefault(operation.getKey(), new Operation(instanceOperation));
2405 ListDataDefinition<OperationInputDefinition> instanceInputs = instanceOperation.getInputs();
2406 if (null != instanceInputs) {
2407 mergeOperationInputDefinitions(templateOperation.getInputs(), instanceInputs);
2408 component.getProperties()
2409 .forEach(property -> instanceInputs.getListToscaDataDefinition().stream()
2410 .filter(instanceInput ->
2411 instanceInput.getToscaFunction() instanceof ToscaGetFunctionDataDefinition &&
2412 property.getName().equals(instanceInput.getToscaFunction() != null ?
2413 ((ToscaGetFunctionDataDefinition) instanceInput.getToscaFunction()).getPropertyName() : null))
2414 .forEach(oldInput -> oldInput.setType(property.getType()))
2416 templateOperation.setInputs(instanceInputs);
2419 templateOperation.setImplementation(instanceOperation.getImplementation());
2421 templateOperation.setDescription(instanceOperation.getDescription());
2422 operationsToAdd.put(operation.getKey(), templateOperation);
2424 InterfaceDefinition interfaceDef = new InterfaceDefinition();
2425 interfaceDef.setModel(component.getModel());
2426 interfaceDef.setType(currentInterfaceDef.getType());
2427 interfaceDef.setUniqueId(currentInterfaceDef.getType());
2428 interfaceDef.setDescription(uploadInterfaceInfo.getDescription());
2429 interfaceDef.setOperations(operationsToAdd);
2430 instInterfacesMap.put(currentInterfaceDef.getType(), interfaceDef);
2431 currInterfacesMap.remove(interfaceName);
2434 if (!currInterfacesMap.isEmpty()) {
2435 for (InterfaceDefinition value : currInterfacesMap.values()) {
2436 instInterfacesMap.put(value.getUniqueId(), value);
2439 instInterfaces.put(currentCompInstance.getUniqueId(), instInterfacesMap);
2440 return componentsUtils.getResponseFormat(ActionStatus.OK);
2443 private void mergeOperationInputDefinitions(ListDataDefinition<OperationInputDefinition> inputsFromNodeType,
2444 ListDataDefinition<OperationInputDefinition> instanceInputs) {
2445 if (inputsFromNodeType == null || CollectionUtils.isEmpty(inputsFromNodeType.getListToscaDataDefinition()) || instanceInputs == null
2446 || CollectionUtils.isEmpty(instanceInputs.getListToscaDataDefinition())) {
2449 instanceInputs.getListToscaDataDefinition().forEach(
2450 instanceInput -> inputsFromNodeType.getListToscaDataDefinition().stream().filter(
2451 templateInput -> templateInput.getName().equals(instanceInput.getName())
2453 newInstanceInput -> {
2454 instanceInput.setSourceProperty(newInstanceInput.getSourceProperty());
2455 instanceInput.setSource(newInstanceInput.getSource());
2456 instanceInput.setType(newInstanceInput.getType());
2460 instanceInputs.getListToscaDataDefinition().stream()
2461 .filter(instanceInput -> inputsFromNodeType.getListToscaDataDefinition().stream().noneMatch(
2462 inputFromNodeType -> inputFromNodeType.getName().equals(instanceInput.getName())
2464 .forEach(oldInput -> oldInput.setType("string"));
2467 protected void processComponentInstanceCapabilities(Map<String, DataTypeDefinition> allDataTypes,
2468 Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> instCapabilties,
2469 UploadComponentInstanceInfo uploadComponentInstanceInfo,
2470 ComponentInstance currentCompInstance, Resource originResource) {
2471 log.debug("enter processComponentInstanceCapabilities");
2472 Map<String, List<CapabilityDefinition>> originCapabilities;
2473 if (MapUtils.isNotEmpty(uploadComponentInstanceInfo.getCapabilities())) {
2474 originCapabilities = new HashMap<>();
2475 Map<String, Map<String, UploadPropInfo>> newPropertiesMap = new HashMap<>();
2476 originResource.getCapabilities().forEach((k, v) -> serviceImportParseLogic.addCapabilities(originCapabilities, k, v));
2477 uploadComponentInstanceInfo.getCapabilities().values()
2478 .forEach(l -> serviceImportParseLogic.addCapabilitiesProperties(newPropertiesMap, l));
2479 updateCapabilityPropertiesValues(allDataTypes, originCapabilities, newPropertiesMap);
2481 originCapabilities = originResource.getCapabilities();
2483 instCapabilties.put(currentCompInstance, originCapabilities);
2486 protected void updateCapabilityPropertiesValues(Map<String, DataTypeDefinition> allDataTypes,
2487 Map<String, List<CapabilityDefinition>> originCapabilities,
2488 Map<String, Map<String, UploadPropInfo>> newPropertiesMap) {
2489 originCapabilities.values().stream().flatMap(Collection::stream).filter(c -> newPropertiesMap.containsKey(c.getName()))
2490 .forEach(c -> updatePropertyValues(c.getProperties(), newPropertiesMap.get(c.getName()), allDataTypes));
2493 protected void updatePropertyValues(List<ComponentInstanceProperty> properties, Map<String, UploadPropInfo> newProperties,
2494 Map<String, DataTypeDefinition> allDataTypes) {
2495 properties.forEach(p -> updatePropertyValue(p, newProperties.get(p.getName()), allDataTypes));
2498 protected String updatePropertyValue(ComponentInstanceProperty property, UploadPropInfo propertyInfo,
2499 Map<String, DataTypeDefinition> allDataTypes) {
2500 String value = null;
2501 List<GetInputValueDataDefinition> getInputs = null;
2502 boolean isValidate = true;
2503 if (null != propertyInfo && propertyInfo.getValue() != null) {
2504 getInputs = propertyInfo.getGet_input();
2505 isValidate = getInputs == null || getInputs.isEmpty();
2507 value = getPropertyJsonStringValue(propertyInfo.getValue(), property.getType());
2509 value = getPropertyJsonStringValue(propertyInfo.getValue(), TypeUtils.ToscaTagNamesEnum.GET_INPUT.getElementName());
2512 property.setValue(value);
2513 return serviceBusinessLogic.validatePropValueBeforeCreate(property, value, isValidate, allDataTypes);
2516 protected Resource getOriginResource(String yamlName, Map<String, Resource> originCompMap, ComponentInstance currentCompInstance) {
2517 Resource originResource;
2518 log.debug("after enter ServiceImportBusinessLogic processComponentInstance, enter getOriginResource");
2519 if (!originCompMap.containsKey(currentCompInstance.getComponentUid())) {
2520 Either<Resource, StorageOperationStatus> getOriginResourceRes = toscaOperationFacade
2521 .getToscaFullElement(currentCompInstance.getComponentUid());
2522 if (getOriginResourceRes.isRight()) {
2523 ResponseFormat responseFormat = componentsUtils
2524 .getResponseFormat(componentsUtils.convertFromStorageResponse(getOriginResourceRes.right().value()), yamlName);
2525 throw new ComponentException(responseFormat);
2527 originResource = getOriginResourceRes.left().value();
2528 originCompMap.put(originResource.getUniqueId(), originResource);
2530 originResource = originCompMap.get(currentCompInstance.getComponentUid());
2532 return originResource;
2535 protected Either<Resource, StorageOperationStatus> updateCalculatedCapReqWithSubstitutionMappings(Resource resource,
2536 Map<String, UploadComponentInstanceInfo> uploadResInstancesMap) {
2537 Either<Resource, StorageOperationStatus> updateRes = null;
2538 Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> updatedInstCapabilities = new HashMap<>();
2539 Map<ComponentInstance, Map<String, List<RequirementDefinition>>> updatedInstRequirements = new HashMap<>();
2540 StorageOperationStatus status = toscaOperationFacade.deleteAllCalculatedCapabilitiesRequirements(resource.getUniqueId());
2541 if (status != StorageOperationStatus.OK && status != StorageOperationStatus.NOT_FOUND) {
2542 log.debug("Failed to delete all calculated capabilities and requirements of resource {} upon update. Status is {}",
2543 resource.getUniqueId(), status);
2544 updateRes = Either.right(status);
2546 if (updateRes == null) {
2547 fillUpdatedInstCapabilitiesRequirements(resource.getComponentInstances(), uploadResInstancesMap, updatedInstCapabilities,
2548 updatedInstRequirements);
2549 status = toscaOperationFacade.associateOrAddCalculatedCapReq(updatedInstCapabilities, updatedInstRequirements, resource);
2550 if (status != StorageOperationStatus.OK && status != StorageOperationStatus.NOT_FOUND) {
2551 updateRes = Either.right(status);
2554 if (updateRes == null) {
2555 updateRes = Either.left(resource);
2560 protected void fillUpdatedInstCapabilitiesRequirements(List<ComponentInstance> componentInstances,
2561 Map<String, UploadComponentInstanceInfo> uploadResInstancesMap,
2562 Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> updatedInstCapabilities,
2563 Map<ComponentInstance, Map<String, List<RequirementDefinition>>> updatedInstRequirements) {
2564 componentInstances.stream().forEach(i -> {
2565 fillUpdatedInstCapabilities(updatedInstCapabilities, i, uploadResInstancesMap.get(i.getName()).getCapabilitiesNamesToUpdate());
2566 fillUpdatedInstRequirements(updatedInstRequirements, i, uploadResInstancesMap.get(i.getName()).getRequirementsNamesToUpdate());
2570 protected void fillUpdatedInstCapabilities(Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> updatedInstCapabilties,
2571 ComponentInstance instance, Map<String, String> capabilitiesNamesToUpdate) {
2572 Map<String, List<CapabilityDefinition>> updatedCapabilities = new HashMap<>();
2573 Set<String> updatedCapNames = new HashSet<>();
2574 if (MapUtils.isNotEmpty(capabilitiesNamesToUpdate)) {
2575 for (Map.Entry<String, List<CapabilityDefinition>> requirements : instance.getCapabilities().entrySet()) {
2576 updatedCapabilities.put(requirements.getKey(), requirements.getValue().stream().filter(
2577 c -> capabilitiesNamesToUpdate.containsKey(c.getName()) && !updatedCapNames.contains(capabilitiesNamesToUpdate.get(c.getName())))
2579 c.setParentName(c.getName());
2580 c.setName(capabilitiesNamesToUpdate.get(c.getName()));
2581 updatedCapNames.add(c.getName());
2583 }).collect(toList()));
2586 if (MapUtils.isNotEmpty(updatedCapabilities)) {
2587 updatedInstCapabilties.put(instance, updatedCapabilities);
2591 protected void fillUpdatedInstRequirements(Map<ComponentInstance, Map<String, List<RequirementDefinition>>> updatedInstRequirements,
2592 ComponentInstance instance, Map<String, String> requirementsNamesToUpdate) {
2593 Map<String, List<RequirementDefinition>> updatedRequirements = new HashMap<>();
2594 Set<String> updatedReqNames = new HashSet<>();
2595 if (MapUtils.isNotEmpty(requirementsNamesToUpdate)) {
2596 for (Map.Entry<String, List<RequirementDefinition>> requirements : instance.getRequirements().entrySet()) {
2597 updatedRequirements.put(requirements.getKey(), requirements.getValue().stream().filter(
2598 r -> requirementsNamesToUpdate.containsKey(r.getName()) && !updatedReqNames.contains(requirementsNamesToUpdate.get(r.getName())))
2600 r.setParentName(r.getName());
2601 r.setName(requirementsNamesToUpdate.get(r.getName()));
2602 updatedReqNames.add(r.getName());
2604 }).collect(toList()));
2607 if (MapUtils.isNotEmpty(updatedRequirements)) {
2608 updatedInstRequirements.put(instance, updatedRequirements);
2612 protected void addRelationsToRI(String yamlName, Service service, Map<String, UploadComponentInstanceInfo> uploadResInstancesMap,
2613 List<ComponentInstance> componentInstancesList, List<RequirementCapabilityRelDef> relations) {
2614 for (Map.Entry<String, UploadComponentInstanceInfo> entry : uploadResInstancesMap.entrySet()) {
2615 UploadComponentInstanceInfo uploadComponentInstanceInfo = entry.getValue();
2616 ComponentInstance currentCompInstance = null;
2617 for (ComponentInstance compInstance : componentInstancesList) {
2618 if (compInstance.getName().equals(uploadComponentInstanceInfo.getName())) {
2619 currentCompInstance = compInstance;
2623 if (currentCompInstance == null) {
2624 log.debug(COMPONENT_INSTANCE_WITH_NAME_IN_RESOURCE, uploadComponentInstanceInfo.getName(), service.getUniqueId());
2625 BeEcompErrorManager.getInstance()
2626 .logInternalDataError(COMPONENT_INSTANCE_WITH_NAME + uploadComponentInstanceInfo.getName() + IN_RESOURCE, service.getUniqueId(),
2627 BeEcompErrorManager.ErrorSeverity.ERROR);
2628 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName);
2629 throw new ComponentException(responseFormat);
2631 ResponseFormat addRelationToRiRes = addRelationToRI(yamlName, service, entry.getValue(), relations);
2632 if (addRelationToRiRes.getStatus() != 200) {
2633 throw new ComponentException(addRelationToRiRes);
2638 protected ResponseFormat addRelationToRI(String yamlName, Service service, UploadComponentInstanceInfo nodesInfoValue,
2639 List<RequirementCapabilityRelDef> relations) {
2640 List<ComponentInstance> componentInstancesList = service.getComponentInstances();
2641 ComponentInstance currentCompInstance = null;
2642 for (ComponentInstance compInstance : componentInstancesList) {
2643 if (compInstance.getName().equals(nodesInfoValue.getName())) {
2644 currentCompInstance = compInstance;
2648 if (currentCompInstance == null) {
2649 log.debug(COMPONENT_INSTANCE_WITH_NAME_IN_RESOURCE, nodesInfoValue.getName(), service.getUniqueId());
2650 BeEcompErrorManager.getInstance()
2651 .logInternalDataError(COMPONENT_INSTANCE_WITH_NAME + nodesInfoValue.getName() + IN_RESOURCE, service.getUniqueId(),
2652 BeEcompErrorManager.ErrorSeverity.ERROR);
2653 return componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName);
2655 String resourceInstanceId = currentCompInstance.getUniqueId();
2656 Map<String, List<UploadReqInfo>> regMap = nodesInfoValue.getRequirements();
2657 if (regMap != null) {
2658 Iterator<Map.Entry<String, List<UploadReqInfo>>> nodesRegValue = regMap.entrySet().iterator();
2659 while (nodesRegValue.hasNext()) {
2660 Map.Entry<String, List<UploadReqInfo>> nodesRegInfoEntry = nodesRegValue.next();
2661 List<UploadReqInfo> uploadRegInfoList = nodesRegInfoEntry.getValue();
2662 for (UploadReqInfo uploadRegInfo : uploadRegInfoList) {
2663 log.debug("Going to create relation {}", uploadRegInfo.getName());
2664 String regName = uploadRegInfo.getName();
2665 RequirementCapabilityRelDef regCapRelDef = new RequirementCapabilityRelDef();
2666 regCapRelDef.setFromNode(resourceInstanceId);
2667 log.debug("try to find available requirement {} ", regName);
2668 Either<RequirementDefinition, ResponseFormat> eitherReqStatus = serviceImportParseLogic
2669 .findAvailableRequirement(regName, yamlName, nodesInfoValue, currentCompInstance, uploadRegInfo.getCapabilityName());
2670 if (eitherReqStatus.isRight()) {
2671 log.debug("failed to find available requirement {} status is {}", regName, eitherReqStatus.right().value());
2672 return eitherReqStatus.right().value();
2674 RequirementDefinition validReq = eitherReqStatus.left().value();
2675 List<CapabilityRequirementRelationship> reqAndRelationshipPairList = regCapRelDef.getRelationships();
2676 if (reqAndRelationshipPairList == null) {
2677 reqAndRelationshipPairList = new ArrayList<>();
2679 RelationshipInfo reqAndRelationshipPair = new RelationshipInfo();
2680 reqAndRelationshipPair.setRequirement(regName);
2681 reqAndRelationshipPair.setRequirementOwnerId(validReq.getOwnerId());
2682 reqAndRelationshipPair.setRequirementUid(validReq.getUniqueId());
2683 RelationshipImpl relationship = new RelationshipImpl();
2684 relationship.setType(validReq.getCapability());
2685 reqAndRelationshipPair.setRelationships(relationship);
2686 ComponentInstance currentCapCompInstance = null;
2687 for (ComponentInstance compInstance : componentInstancesList) {
2688 if (compInstance.getName().equals(uploadRegInfo.getNode())) {
2689 currentCapCompInstance = compInstance;
2693 if (currentCapCompInstance == null) {
2694 log.debug("The component instance with name {} not found on resource {} ", uploadRegInfo.getNode(), service.getUniqueId());
2695 BeEcompErrorManager.getInstance()
2696 .logInternalDataError(COMPONENT_INSTANCE_WITH_NAME + uploadRegInfo.getNode() + IN_RESOURCE, service.getUniqueId(),
2697 BeEcompErrorManager.ErrorSeverity.ERROR);
2698 return componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName);
2700 regCapRelDef.setToNode(currentCapCompInstance.getUniqueId());
2701 log.debug("try to find aviable Capability req name is {} ", validReq.getName());
2702 CapabilityDefinition aviableCapForRel = serviceImportParseLogic
2703 .findAvailableCapabilityByTypeOrName(validReq, currentCapCompInstance, uploadRegInfo);
2704 if (aviableCapForRel == null) {
2705 BeEcompErrorManager.getInstance().logInternalDataError(
2706 "aviable capability was not found. req name is " + validReq.getName() + " component instance is " + currentCapCompInstance
2707 .getUniqueId(), service.getUniqueId(), BeEcompErrorManager.ErrorSeverity.ERROR);
2708 return componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName);
2710 reqAndRelationshipPair.setCapability(aviableCapForRel.getName());
2711 reqAndRelationshipPair.setCapabilityUid(aviableCapForRel.getUniqueId());
2712 reqAndRelationshipPair.setCapabilityOwnerId(aviableCapForRel.getOwnerId());
2713 CapabilityRequirementRelationship capReqRel = new CapabilityRequirementRelationship();
2714 capReqRel.setRelation(reqAndRelationshipPair);
2715 if (StringUtils.isNotEmpty(uploadRegInfo.getRelationshipTemplate())) {
2716 capReqRel.setOperations(getOperations(nodesInfoValue.getOperations(), uploadRegInfo.getRelationshipTemplate()));
2718 reqAndRelationshipPairList.add(capReqRel);
2719 regCapRelDef.setRelationships(reqAndRelationshipPairList);
2720 relations.add(regCapRelDef);
2724 return componentsUtils.getResponseFormat(ActionStatus.OK, yamlName);
2727 private List<OperationUi> getOperations(final Map<String, List<OperationUi>> operations, final String relationshipTemplate) {
2728 final List<OperationUi> operationUiList = new ArrayList<>();
2729 operations.forEach((operationKey, operationValues) -> {
2730 if (operationKey.equals(relationshipTemplate)) {
2731 operationUiList.addAll(operationValues);
2734 return operationUiList;
2737 protected Service getResourceAfterCreateRelations(Service service) {
2738 ComponentParametersView parametersView = serviceImportParseLogic.getComponentFilterAfterCreateRelations();
2739 Either<Service, StorageOperationStatus> eitherGetResource = toscaOperationFacade.getToscaElement(service.getUniqueId(), parametersView);
2740 if (eitherGetResource.isRight()) {
2741 serviceImportParseLogic.throwComponentExceptionByResource(eitherGetResource.right().value(), service);
2743 return eitherGetResource.left().value();
2746 protected Service createServiceInstances(String yamlName, Service service, Map<String, UploadComponentInstanceInfo> uploadResInstancesMap,
2747 Map<String, Resource> nodeNamespaceMap) {
2748 Either<Resource, ResponseFormat> eitherResource = null;
2749 log.debug("createResourceInstances is {} - going to create resource instanse from CSAR", yamlName);
2750 if (MapUtils.isEmpty(uploadResInstancesMap)) { // PNF can have no resource instances
2751 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE);
2752 throw new ComponentException(responseFormat);
2754 Map<String, Resource> existingNodeTypeMap = new HashMap<>();
2755 if (MapUtils.isNotEmpty(nodeNamespaceMap)) {
2756 nodeNamespaceMap.forEach((k, v) -> existingNodeTypeMap.put(v.getToscaResourceName(), v));
2758 Map<ComponentInstance, Resource> resourcesInstancesMap = new HashMap<>();
2759 uploadResInstancesMap.values()
2760 .forEach(i -> createAndAddResourceInstance(i, yamlName, service, nodeNamespaceMap, existingNodeTypeMap, resourcesInstancesMap));
2761 if (MapUtils.isNotEmpty(resourcesInstancesMap)) {
2763 toscaOperationFacade.associateComponentInstancesToComponent(service, resourcesInstancesMap, false, false);
2764 } catch (StorageException exp) {
2765 if (exp.getStorageOperationStatus() != null && exp.getStorageOperationStatus() != StorageOperationStatus.OK) {
2766 log.debug("Failed to add component instances to container component {}", service.getName());
2767 ResponseFormat responseFormat = componentsUtils
2768 .getResponseFormat(componentsUtils.convertFromStorageResponse(exp.getStorageOperationStatus()));
2769 eitherResource = Either.right(responseFormat);
2770 throw new ComponentException(eitherResource.right().value());
2774 Either<Service, StorageOperationStatus> eitherGetResource = toscaOperationFacade
2775 .getToscaElement(service.getUniqueId(), serviceImportParseLogic.getComponentWithInstancesFilter());
2776 log.debug("*************finished to get resource {}", service.getUniqueId());
2777 if (eitherGetResource.isRight()) {
2778 ResponseFormat responseFormat = componentsUtils
2779 .getResponseFormatByComponent(componentsUtils.convertFromStorageResponse(eitherGetResource.right().value()), service,
2780 ComponentTypeEnum.SERVICE);
2781 throw new ComponentException(responseFormat);
2783 if (CollectionUtils.isEmpty(eitherGetResource.left().value().getComponentInstances())) { // PNF can have no resource instances
2784 log.debug("Error when create resource instance from csar. ComponentInstances list empty");
2785 BeEcompErrorManager.getInstance().logBeDaoSystemError("Error when create resource instance from csar. ComponentInstances list empty");
2786 throw new ComponentException(componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE));
2788 return eitherGetResource.left().value();
2791 protected void createAndAddResourceInstance(UploadComponentInstanceInfo uploadComponentInstanceInfo, String yamlName, Component component,
2792 Map<String, Resource> nodeNamespaceMap, Map<String, Resource> existingnodeTypeMap,
2793 Map<ComponentInstance, Resource> resourcesInstancesMap) {
2794 log.debug("*************Going to create resource instances {}", uploadComponentInstanceInfo.getName());
2796 if (nodeNamespaceMap.containsKey(uploadComponentInstanceInfo.getType())) {
2797 uploadComponentInstanceInfo.setType(nodeNamespaceMap.get(uploadComponentInstanceInfo.getType()).getToscaResourceName());
2799 Resource refResource =
2800 validateResourceInstanceBeforeCreate(yamlName, component.getModel(), uploadComponentInstanceInfo, existingnodeTypeMap);
2801 ComponentInstance componentInstance = new ComponentInstance();
2802 componentInstance.setComponentUid(refResource.getUniqueId());
2803 Collection<String> directives = uploadComponentInstanceInfo.getDirectives();
2804 if (directives != null && !directives.isEmpty()) {
2805 componentInstance.setDirectives(new ArrayList<>(directives));
2807 UploadNodeFilterInfo uploadNodeFilterInfo = uploadComponentInstanceInfo.getUploadNodeFilterInfo();
2808 if (uploadNodeFilterInfo != null) {
2810 .setNodeFilter(new CINodeFilterUtils().getNodeFilterDataDefinition(uploadNodeFilterInfo, componentInstance.getUniqueId()));
2812 ComponentTypeEnum containerComponentType = component.getComponentType();
2813 NodeTypeEnum containerNodeType = containerComponentType.getNodeType();
2814 if (containerNodeType.equals(NodeTypeEnum.Resource) && MapUtils.isNotEmpty(uploadComponentInstanceInfo.getCapabilities()) && MapUtils
2815 .isNotEmpty(refResource.getCapabilities())) {
2816 serviceImportParseLogic.setCapabilityNamesTypes(refResource.getCapabilities(), uploadComponentInstanceInfo.getCapabilities());
2817 Map<String, List<CapabilityDefinition>> validComponentInstanceCapabilities = serviceImportParseLogic
2818 .getValidComponentInstanceCapabilities(refResource.getUniqueId(), refResource.getCapabilities(),
2819 uploadComponentInstanceInfo.getCapabilities());
2820 componentInstance.setCapabilities(validComponentInstanceCapabilities);
2822 if (!existingnodeTypeMap.containsKey(uploadComponentInstanceInfo.getType())) {
2823 ResponseFormat responseFormat = componentsUtils
2824 .getResponseFormat(ActionStatus.INVALID_NODE_TEMPLATE, yamlName, uploadComponentInstanceInfo.getName(),
2825 uploadComponentInstanceInfo.getType());
2826 throw new ComponentException(responseFormat);
2828 Resource origResource = existingnodeTypeMap.get(uploadComponentInstanceInfo.getType());
2829 componentInstance.setName(uploadComponentInstanceInfo.getName());
2830 componentInstance.setIcon(origResource.getIcon());
2831 componentInstance.setMinOccurrences(uploadComponentInstanceInfo.getMinOccurrences());
2832 componentInstance.setMaxOccurrences(uploadComponentInstanceInfo.getMaxOccurrences());
2833 componentInstance.setInstanceCount(uploadComponentInstanceInfo.getInstanceCount());
2834 resourcesInstancesMap.put(componentInstance, origResource);
2835 } catch (final ComponentException e) {
2837 } catch (final Exception e) {
2838 throw new ComponentException(ActionStatus.GENERAL_ERROR, e.getMessage());
2842 protected Resource validateResourceInstanceBeforeCreate(String yamlName, String model, UploadComponentInstanceInfo uploadComponentInstanceInfo,
2843 Map<String, Resource> nodeNamespaceMap) {
2844 Resource refResource;
2846 if (nodeNamespaceMap.containsKey(uploadComponentInstanceInfo.getType())) {
2847 refResource = nodeNamespaceMap.get(uploadComponentInstanceInfo.getType());
2849 final Either<Component, StorageOperationStatus> resourceEither =
2850 toscaOperationFacade.getLatestByToscaResourceName(uploadComponentInstanceInfo.getType(), model);
2851 if (resourceEither.isRight()) {
2852 ResponseFormat responseFormat = componentsUtils
2853 .getResponseFormat(componentsUtils.convertFromStorageResponse(resourceEither.right().value()));
2854 throw new ComponentException(responseFormat);
2856 refResource = (Resource) resourceEither.left().value();
2857 nodeNamespaceMap.put(refResource.getToscaResourceName(), refResource);
2859 String componentState = refResource.getComponentMetadataDefinition().getMetadataDataDefinition().getState();
2860 if (componentState.equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name())) {
2861 ResponseFormat responseFormat = componentsUtils
2862 .getResponseFormat(ActionStatus.ILLEGAL_COMPONENT_STATE, refResource.getComponentType().getValue(), refResource.getName(),
2864 throw new ComponentException(responseFormat);
2866 if (!ModelConverter.isAtomicComponent(refResource) && refResource.getResourceType() != ResourceTypeEnum.VF) {
2867 log.debug("validateResourceInstanceBeforeCreate - ref resource type is ", refResource.getResourceType());
2868 ResponseFormat responseFormat = componentsUtils
2869 .getResponseFormat(ActionStatus.INVALID_NODE_TEMPLATE, yamlName, uploadComponentInstanceInfo.getName(),
2870 uploadComponentInstanceInfo.getType());
2871 throw new ComponentException(responseFormat);
2874 } catch (final ComponentException e) {
2876 } catch (final Exception e) {
2877 throw new ComponentException(ActionStatus.GENERAL_ERROR, e.getMessage());
2881 protected void handleServiceNodeTypes(String yamlName, Service service, String topologyTemplateYaml, boolean needLock,
2882 Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle,
2883 List<ArtifactDefinition> nodeTypesNewCreatedArtifacts, Map<String, NodeTypeInfo> nodeTypesInfo,
2884 CsarInfo csarInfo, String nodeName) {
2886 for (Map.Entry<String, NodeTypeInfo> nodeTypeEntry : nodeTypesInfo.entrySet()) {
2887 boolean isResourceNotExisted = validateResourceNotExisted(nodeTypeEntry.getKey());
2888 if (nodeTypeEntry.getValue().isNested() && isResourceNotExisted) {
2889 handleNestedVF(service, nodeTypesArtifactsToHandle, nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo,
2890 nodeTypeEntry.getKey());
2891 log.trace("************* finished to create node {}", nodeTypeEntry.getKey());
2894 Map<String, Object> mappedToscaTemplate = null;
2895 if (org.apache.commons.lang.StringUtils.isNotEmpty(nodeName) && MapUtils.isNotEmpty(nodeTypesInfo) && nodeTypesInfo
2896 .containsKey(nodeName)) {
2897 mappedToscaTemplate = nodeTypesInfo.get(nodeName).getMappedToscaTemplate();
2899 if (MapUtils.isEmpty(mappedToscaTemplate)) {
2900 mappedToscaTemplate = (Map<String, Object>) new Yaml().load(topologyTemplateYaml);
2902 createResourcesFromYamlNodeTypesList(yamlName, service, mappedToscaTemplate, needLock, nodeTypesArtifactsToHandle,
2903 nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo);
2904 } catch (ComponentException | StorageException e) {
2906 } catch (Exception e) {
2907 log.debug("Exception occured when handleServiceNodeTypes, error is:{}", e.getMessage(), e);
2908 throw new ComponentException(ActionStatus.GENERAL_ERROR);
2912 protected boolean validateResourceNotExisted(String type) {
2914 Either<Resource, StorageOperationStatus> latestResource = toscaOperationFacade.getLatestResourceByToscaResourceName(type);
2915 return latestResource.isRight();
2916 } catch (Exception e) {
2917 log.debug("Exception occured when validateResourceNotExisted, error is:{}", e.getMessage(), e);
2918 throw new ComponentException(ActionStatus.GENERAL_ERROR);
2922 protected Resource handleNestedVF(Service service,
2923 Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodesArtifactsToHandle,
2924 List<ArtifactDefinition> createdArtifacts, Map<String, NodeTypeInfo> nodesInfo, CsarInfo csarInfo,
2927 String yamlName = nodesInfo.get(nodeName).getTemplateFileName();
2928 Map<String, Object> nestedVfcJsonMap = nodesInfo.get(nodeName).getMappedToscaTemplate();
2929 createResourcesFromYamlNodeTypesList(yamlName, service, nestedVfcJsonMap, false, nodesArtifactsToHandle, createdArtifacts, nodesInfo,
2931 log.debug("************* Finished to create node types from yaml {}", yamlName);
2932 if (nestedVfcJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.TOPOLOGY_TEMPLATE.getElementName())) {
2933 log.debug("************* Going to handle complex VFC from yaml {}", yamlName);
2934 return handleComplexVfc(nodesArtifactsToHandle, createdArtifacts, nodesInfo, csarInfo, nodeName, yamlName);
2936 return new Resource();
2937 } catch (Exception e) {
2938 log.debug("Exception occured when handleNestedVF, error is:{}", e.getMessage(), e);
2939 throw new ComponentException(ActionStatus.GENERAL_ERROR);
2943 protected Resource handleComplexVfc(
2944 Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodesArtifactsToHandle,
2945 List<ArtifactDefinition> createdArtifacts, Map<String, NodeTypeInfo> nodesInfo, CsarInfo csarInfo, String nodeName, String yamlName) {
2947 Resource oldComplexVfc = null;
2948 Resource newComplexVfc = serviceImportParseLogic.buildValidComplexVfc(csarInfo, nodeName, nodesInfo);
2949 Either<Resource, StorageOperationStatus> oldComplexVfcRes = toscaOperationFacade
2950 .getFullLatestComponentByToscaResourceName(newComplexVfc.getToscaResourceName());
2951 if (oldComplexVfcRes.isRight() && oldComplexVfcRes.right().value() == StorageOperationStatus.NOT_FOUND) {
2952 oldComplexVfcRes = toscaOperationFacade.getFullLatestComponentByToscaResourceName(
2953 serviceImportParseLogic.buildNestedToscaResourceName(ResourceTypeEnum.VF.name(), csarInfo.getVfResourceName(), nodeName)
2956 if (oldComplexVfcRes.isRight() && oldComplexVfcRes.right().value() != StorageOperationStatus.NOT_FOUND) {
2957 log.debug("Failed to fetch previous complex VFC by tosca resource name {}. Status is {}. ", newComplexVfc.getToscaResourceName(),
2958 oldComplexVfcRes.right().value());
2959 throw new ComponentException(ActionStatus.GENERAL_ERROR);
2960 } else if (oldComplexVfcRes.isLeft()) {
2961 log.debug(VALIDATE_DERIVED_BEFORE_UPDATE);
2962 Either<Boolean, ResponseFormat> eitherValidation = serviceImportParseLogic
2963 .validateNestedDerivedFromDuringUpdate(oldComplexVfcRes.left().value(), newComplexVfc,
2964 ValidationUtils.hasBeenCertified(oldComplexVfcRes.left().value().getVersion()));
2965 if (eitherValidation.isLeft()) {
2966 oldComplexVfc = oldComplexVfcRes.left().value();
2969 newComplexVfc = handleComplexVfc(nodesArtifactsToHandle, createdArtifacts, nodesInfo, csarInfo, nodeName, yamlName, oldComplexVfc,
2971 csarInfo.getCreatedNodesToscaResourceNames().put(nodeName, newComplexVfc.getToscaResourceName());
2972 LifecycleChangeInfoWithAction lifecycleChangeInfo = new LifecycleChangeInfoWithAction(CERTIFICATION_ON_IMPORT,
2973 LifecycleChangeInfoWithAction.LifecycleChanceActionEnum.CREATE_FROM_CSAR);
2974 log.debug("Going to certify cvfc {}. ", newComplexVfc.getName());
2975 final Resource result = serviceImportParseLogic
2976 .propagateStateToCertified(csarInfo.getModifier(), newComplexVfc, lifecycleChangeInfo, true, false, true);
2977 csarInfo.getCreatedNodes().put(nodeName, result);
2978 csarInfo.removeNodeFromQueue();
2980 } catch (Exception e) {
2981 log.debug("Exception occured when handleComplexVfc, error is:{}", e.getMessage(), e);
2982 throw new ComponentException(ActionStatus.GENERAL_ERROR);
2986 protected Resource handleComplexVfc(
2987 Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodesArtifactsToHandle,
2988 List<ArtifactDefinition> createdArtifacts, Map<String, NodeTypeInfo> nodesInfo, CsarInfo csarInfo, String nodeName, String yamlName,
2989 Resource oldComplexVfc, Resource newComplexVfc) {
2990 Resource handleComplexVfcRes;
2992 Map<String, Object> mappedToscaTemplate = nodesInfo.get(nodeName).getMappedToscaTemplate();
2993 String yamlContent = new String(csarInfo.getCsar().get(yamlName));
2994 Map<String, NodeTypeInfo> newNodeTypesInfo = nodesInfo.entrySet().stream()
2995 .collect(toMap(Map.Entry::getKey, e -> e.getValue().getUnmarkedCopy()));
2996 CsarInfo.markNestedVfc(mappedToscaTemplate, newNodeTypesInfo);
2997 if (oldComplexVfc == null) {
2998 handleComplexVfcRes = createResourceFromYaml(newComplexVfc, yamlContent, yamlName, newNodeTypesInfo, csarInfo, nodesArtifactsToHandle,
2999 false, true, nodeName);
3001 handleComplexVfcRes = updateResourceFromYaml(oldComplexVfc, newComplexVfc, AuditingActionEnum.UPDATE_RESOURCE_METADATA,
3002 createdArtifacts, yamlContent, yamlName, csarInfo, newNodeTypesInfo, nodesArtifactsToHandle, nodeName, true);
3004 return handleComplexVfcRes;
3005 } catch (Exception e) {
3006 log.debug("Exception occured when handleComplexVfc, error is:{}", e.getMessage(), e);
3007 throw new ComponentException(ActionStatus.GENERAL_ERROR);
3011 protected Resource updateResourceFromYaml(Resource oldRresource, Resource newRresource, AuditingActionEnum actionEnum,
3012 List<ArtifactDefinition> createdArtifacts, String yamlFileName, String yamlFileContent,
3013 CsarInfo csarInfo, Map<String, NodeTypeInfo> nodeTypesInfo,
3014 Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle,
3015 String nodeName, boolean isNested) {
3016 boolean inTransaction = true;
3017 boolean shouldLock = false;
3018 Resource preparedResource = null;
3019 ParsedToscaYamlInfo uploadComponentInstanceInfoMap = null;
3021 uploadComponentInstanceInfoMap = csarBusinessLogic
3022 .getParsedToscaYamlInfo(yamlFileContent, yamlFileName, nodeTypesInfo, csarInfo, nodeName, oldRresource);
3023 Map<String, UploadComponentInstanceInfo> instances = uploadComponentInstanceInfoMap.getInstances();
3024 if (MapUtils.isEmpty(instances) && newRresource.getResourceType() != ResourceTypeEnum.PNF) {
3025 throw new ComponentException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlFileName);
3027 preparedResource = updateExistingResourceByImport(newRresource, oldRresource, csarInfo.getModifier(), inTransaction, shouldLock,
3029 log.trace("YAML topology file found in CSAR, file name: {}, contents: {}", yamlFileName, yamlFileContent);
3030 serviceImportParseLogic.handleResourceGenericType(preparedResource);
3031 handleNodeTypes(yamlFileName, preparedResource, yamlFileContent, shouldLock, nodeTypesArtifactsToHandle, createdArtifacts, nodeTypesInfo,
3032 csarInfo, nodeName);
3033 preparedResource = serviceImportParseLogic.createInputsOnResource(preparedResource, uploadComponentInstanceInfoMap.getInputs());
3034 preparedResource = createResourceInstances(yamlFileName, preparedResource, instances, csarInfo.getCreatedNodes());
3035 preparedResource = createResourceInstancesRelations(csarInfo.getModifier(), yamlFileName, preparedResource, instances);
3036 } catch (ComponentException e) {
3037 ResponseFormat responseFormat =
3038 e.getResponseFormat() == null ? componentsUtils.getResponseFormat(e.getActionStatus(), e.getParams()) : e.getResponseFormat();
3039 log.debug("#updateResourceFromYaml - failed to update resource from yaml {} .The error is {}", yamlFileName, responseFormat);
3041 .auditResource(responseFormat, csarInfo.getModifier(), preparedResource == null ? oldRresource : preparedResource, actionEnum);
3043 } catch (StorageException e) {
3044 ResponseFormat responseFormat = componentsUtils
3045 .getResponseFormat(componentsUtils.convertFromStorageResponse(e.getStorageOperationStatus()));
3046 log.debug("#updateResourceFromYaml - failed to update resource from yaml {} .The error is {}", yamlFileName, responseFormat);
3048 .auditResource(responseFormat, csarInfo.getModifier(), preparedResource == null ? oldRresource : preparedResource, actionEnum);
3051 Either<Map<String, GroupDefinition>, ResponseFormat> validateUpdateVfGroupNamesRes = groupBusinessLogic.validateUpdateVfGroupNames(
3052 uploadComponentInstanceInfoMap.getGroups(), preparedResource.getSystemName());
3053 if (validateUpdateVfGroupNamesRes.isRight()) {
3054 throw new ComponentException(validateUpdateVfGroupNamesRes.right().value());
3056 Map<String, GroupDefinition> groups;
3057 if (!validateUpdateVfGroupNamesRes.left().value().isEmpty()) {
3058 groups = validateUpdateVfGroupNamesRes.left().value();
3060 groups = uploadComponentInstanceInfoMap.getGroups();
3062 serviceImportParseLogic.handleGroupsProperties(preparedResource, groups);
3063 preparedResource = serviceImportParseLogic.updateGroupsOnResource(preparedResource, groups);
3064 NodeTypeInfoToUpdateArtifacts nodeTypeInfoToUpdateArtifacts = new NodeTypeInfoToUpdateArtifacts(nodeName, nodeTypesArtifactsToHandle);
3065 Either<Resource, ResponseFormat> updateArtifactsEither = createOrUpdateArtifacts(ArtifactsBusinessLogic.ArtifactOperationEnum.UPDATE,
3066 createdArtifacts, yamlFileName, csarInfo, preparedResource, nodeTypeInfoToUpdateArtifacts, inTransaction, shouldLock);
3067 if (updateArtifactsEither.isRight()) {
3068 log.debug("failed to update artifacts {}", updateArtifactsEither.right().value());
3069 throw new ComponentException(updateArtifactsEither.right().value());
3071 preparedResource = serviceImportParseLogic.getResourceWithGroups(updateArtifactsEither.left().value().getUniqueId());
3072 ActionStatus mergingPropsAndInputsStatus = resourceDataMergeBusinessLogic.mergeResourceEntities(oldRresource, preparedResource);
3073 if (mergingPropsAndInputsStatus != ActionStatus.OK) {
3074 ResponseFormat responseFormat = componentsUtils.getResponseFormatByResource(mergingPropsAndInputsStatus, preparedResource);
3075 throw new ComponentException(responseFormat);
3077 compositionBusinessLogic.setPositionsForComponentInstances(preparedResource, csarInfo.getModifier().getUserId());
3078 return preparedResource;
3081 protected Resource createResourceFromYaml(Resource resource, String topologyTemplateYaml, String yamlName,
3082 Map<String, NodeTypeInfo> nodeTypesInfo, CsarInfo csarInfo,
3083 Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToCreate,
3084 boolean shouldLock, boolean inTransaction, String nodeName) {
3085 List<ArtifactDefinition> createdArtifacts = new ArrayList<>();
3086 Resource createdResource;
3088 ParsedToscaYamlInfo parsedToscaYamlInfo = csarBusinessLogic
3089 .getParsedToscaYamlInfo(topologyTemplateYaml, yamlName, nodeTypesInfo, csarInfo, nodeName, resource);
3090 if (MapUtils.isEmpty(parsedToscaYamlInfo.getInstances()) && resource.getResourceType() != ResourceTypeEnum.PNF) {
3091 throw new ComponentException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName);
3093 log.debug("#createResourceFromYaml - Going to create resource {} and RIs ", resource.getName());
3094 createdResource = createResourceAndRIsFromYaml(yamlName, resource, parsedToscaYamlInfo, AuditingActionEnum.IMPORT_RESOURCE, false,
3095 createdArtifacts, topologyTemplateYaml, nodeTypesInfo, csarInfo, nodeTypesArtifactsToCreate, shouldLock, inTransaction, nodeName);
3096 log.debug("#createResourceFromYaml - The resource {} has been created ", resource.getName());
3097 } catch (ComponentException e) {
3098 ResponseFormat responseFormat =
3099 e.getResponseFormat() == null ? componentsUtils.getResponseFormat(e.getActionStatus(), e.getParams()) : e.getResponseFormat();
3100 componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), resource, AuditingActionEnum.IMPORT_RESOURCE);
3102 } catch (StorageException e) {
3103 ResponseFormat responseFormat = componentsUtils
3104 .getResponseFormat(componentsUtils.convertFromStorageResponse(e.getStorageOperationStatus()));
3105 componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), resource, AuditingActionEnum.IMPORT_RESOURCE);
3108 return createdResource;
3111 protected Resource createResourceAndRIsFromYaml(String yamlName, Resource resource, ParsedToscaYamlInfo parsedToscaYamlInfo,
3112 AuditingActionEnum actionEnum, boolean isNormative, List<ArtifactDefinition> createdArtifacts,
3113 String topologyTemplateYaml, Map<String, NodeTypeInfo> nodeTypesInfo, CsarInfo csarInfo,
3114 Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToCreate,
3115 boolean shouldLock, boolean inTransaction, String nodeName) {
3116 List<ArtifactDefinition> nodeTypesNewCreatedArtifacts = new ArrayList<>();
3118 Either<Boolean, ResponseFormat> lockResult = serviceBusinessLogic
3119 .lockComponentByName(resource.getSystemName(), resource, CREATE_RESOURCE);
3120 if (lockResult.isRight()) {
3121 serviceImportParseLogic.rollback(inTransaction, resource, createdArtifacts, nodeTypesNewCreatedArtifacts);
3122 throw new ComponentException(lockResult.right().value());
3124 log.debug("name is locked {} status = {}", resource.getSystemName(), lockResult);
3127 log.trace("************* createResourceFromYaml before full create resource {}", yamlName);
3128 Resource genericResource = serviceBusinessLogic.fetchAndSetDerivedFromGenericType(resource);
3129 resource = createResourceTransaction(resource, csarInfo.getModifier(), isNormative);
3130 log.trace("************* Going to add inputs from yaml {}", yamlName);
3131 Map<String, Object> yamlMap = ImportUtils.loadYamlAsStrictMap(csarInfo.getMainTemplateContent());
3132 Map<String, Object> metadata = (Map<String, Object>) yamlMap.get("metadata");
3133 String type = (String) metadata.get("type");
3134 if (resource.shouldGenerateInputs() && !"Service".equalsIgnoreCase(type)) {
3135 serviceBusinessLogic.generateAndAddInputsFromGenericTypeProperties(resource, genericResource);
3137 Map<String, InputDefinition> inputs = parsedToscaYamlInfo.getInputs();
3138 resource = serviceImportParseLogic.createInputsOnResource(resource, inputs);
3139 Map<String, UploadComponentInstanceInfo> uploadComponentInstanceInfoMap = parsedToscaYamlInfo.getInstances();
3140 resource = createRIAndRelationsFromYaml(yamlName, resource, uploadComponentInstanceInfoMap, topologyTemplateYaml,
3141 nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo, nodeTypesArtifactsToCreate, nodeName);
3142 log.trace("************* Finished to create nodes, RI and Relation from yaml {}", yamlName);
3143 // validate update vf module group names
3144 Either<Map<String, GroupDefinition>, ResponseFormat> validateUpdateVfGroupNamesRes = groupBusinessLogic.validateUpdateVfGroupNames(
3145 parsedToscaYamlInfo.getGroups(), resource.getSystemName());
3146 if (validateUpdateVfGroupNamesRes.isRight()) {
3147 serviceImportParseLogic.rollback(inTransaction, resource, createdArtifacts, nodeTypesNewCreatedArtifacts);
3148 throw new ComponentException(validateUpdateVfGroupNamesRes.right().value());
3150 Map<String, GroupDefinition> groups;
3151 log.trace("************* Going to add groups from yaml {}", yamlName);
3152 if (!validateUpdateVfGroupNamesRes.left().value().isEmpty()) {
3153 groups = validateUpdateVfGroupNamesRes.left().value();
3155 groups = parsedToscaYamlInfo.getGroups();
3157 Either<Resource, ResponseFormat> createGroupsOnResource = createGroupsOnResource(resource, groups);
3158 if (createGroupsOnResource.isRight()) {
3159 serviceImportParseLogic.rollback(inTransaction, resource, createdArtifacts, nodeTypesNewCreatedArtifacts);
3160 throw new ComponentException(createGroupsOnResource.right().value());
3162 resource = createGroupsOnResource.left().value();
3163 log.trace("************* Going to add artifacts from yaml {}", yamlName);
3164 NodeTypeInfoToUpdateArtifacts nodeTypeInfoToUpdateArtifacts = new NodeTypeInfoToUpdateArtifacts(nodeName, nodeTypesArtifactsToCreate);
3165 Either<Resource, ResponseFormat> createArtifactsEither = createOrUpdateArtifacts(ArtifactsBusinessLogic.ArtifactOperationEnum.CREATE,
3166 createdArtifacts, yamlName, csarInfo, resource, nodeTypeInfoToUpdateArtifacts, inTransaction, shouldLock);
3167 if (createArtifactsEither.isRight()) {
3168 serviceImportParseLogic.rollback(inTransaction, resource, createdArtifacts, nodeTypesNewCreatedArtifacts);
3169 throw new ComponentException(createArtifactsEither.right().value());
3171 resource = serviceImportParseLogic.getResourceWithGroups(createArtifactsEither.left().value().getUniqueId());
3172 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.CREATED);
3173 componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), resource, actionEnum);
3174 ASDCKpiApi.countCreatedResourcesKPI();
3176 } catch (ComponentException | StorageException e) {
3177 serviceImportParseLogic.rollback(inTransaction, resource, createdArtifacts, nodeTypesNewCreatedArtifacts);
3180 if (!inTransaction) {
3181 janusGraphDao.commit();
3184 graphLockOperation.unlockComponentByName(resource.getSystemName(), resource.getUniqueId(), NodeTypeEnum.Resource);
3189 protected Either<Resource, ResponseFormat> createGroupsOnResource(Resource resource, Map<String, GroupDefinition> groups) {
3190 if (groups != null && !groups.isEmpty()) {
3191 List<GroupDefinition> groupsAsList = updateGroupsMembersUsingResource(groups, resource);
3192 serviceImportParseLogic.handleGroupsProperties(resource, groups);
3193 serviceImportParseLogic.fillGroupsFinalFields(groupsAsList);
3194 Either<List<GroupDefinition>, ResponseFormat> createGroups = groupBusinessLogic.createGroups(resource, groupsAsList, true);
3195 if (createGroups.isRight()) {
3196 return Either.right(createGroups.right().value());
3199 return Either.left(resource);
3201 Either<Resource, StorageOperationStatus> updatedResource = toscaOperationFacade.getToscaElement(resource.getUniqueId());
3202 if (updatedResource.isRight()) {
3203 ResponseFormat responseFormat = componentsUtils
3204 .getResponseFormatByResource(componentsUtils.convertFromStorageResponse(updatedResource.right().value()), resource);
3205 return Either.right(responseFormat);
3207 return Either.left(updatedResource.left().value());
3210 protected List<GroupDefinition> updateGroupsMembersUsingResource(Map<String, GroupDefinition> groups, Resource component) {
3211 List<GroupDefinition> result = new ArrayList<>();
3212 List<ComponentInstance> componentInstances = component.getComponentInstances();
3213 if (groups != null) {
3214 for (Map.Entry<String, GroupDefinition> entry : groups.entrySet()) {
3215 String groupName = entry.getKey();
3216 GroupDefinition groupDefinition = entry.getValue();
3217 GroupDefinition updatedGroupDefinition = new GroupDefinition(groupDefinition);
3218 updatedGroupDefinition.setMembers(null);
3219 Map<String, String> members = groupDefinition.getMembers();
3220 if (members != null) {
3221 updateGroupMembers(groups, updatedGroupDefinition, component, componentInstances, groupName, members);
3223 result.add(updatedGroupDefinition);
3229 protected void updateGroupMembers(Map<String, GroupDefinition> groups, GroupDefinition updatedGroupDefinition, Resource component,
3230 List<ComponentInstance> componentInstances, String groupName, Map<String, String> members) {
3231 Set<String> compInstancesNames = members.keySet();
3232 if (CollectionUtils.isEmpty(componentInstances)) {
3233 String membersAstString = compInstancesNames.stream().collect(joining(","));
3234 log.debug("The members: {}, in group: {}, cannot be found in component {}. There are no component instances.", membersAstString,
3235 groupName, component.getNormalizedName());
3236 throw new ComponentException(componentsUtils
3237 .getResponseFormat(ActionStatus.GROUP_INVALID_COMPONENT_INSTANCE, membersAstString, groupName, component.getNormalizedName(),
3238 serviceImportParseLogic.getComponentTypeForResponse(component)));
3240 Map<String, String> memberNames = componentInstances.stream().collect(toMap(ComponentInstance::getName, ComponentInstance::getUniqueId));
3241 memberNames.putAll(groups.keySet().stream().collect(toMap(g -> g, g -> "")));
3242 Map<String, String> relevantInstances = memberNames.entrySet().stream().filter(n -> compInstancesNames.contains(n.getKey()))
3243 .collect(toMap(Map.Entry::getKey, Map.Entry::getValue));
3244 if (relevantInstances == null || relevantInstances.size() != compInstancesNames.size()) {
3245 List<String> foundMembers = new ArrayList<>();
3246 if (relevantInstances != null) {
3247 foundMembers = relevantInstances.keySet().stream().collect(toList());
3249 compInstancesNames.removeAll(foundMembers);
3250 String membersAstString = compInstancesNames.stream().collect(joining(","));
3251 throw new ComponentException(componentsUtils
3252 .getResponseFormat(ActionStatus.GROUP_INVALID_COMPONENT_INSTANCE, membersAstString, groupName, component.getNormalizedName(),
3253 serviceImportParseLogic.getComponentTypeForResponse(component)));
3255 updatedGroupDefinition.setMembers(relevantInstances);
3258 protected Resource createResourceTransaction(Resource resource, User user, boolean isNormative) {
3259 Either<Boolean, StorageOperationStatus> eitherValidation = toscaOperationFacade
3260 .validateComponentNameExists(resource.getName(), resource.getResourceType(), resource.getComponentType());
3261 if (eitherValidation.isRight()) {
3262 ResponseFormat errorResponse = componentsUtils
3263 .getResponseFormat(componentsUtils.convertFromStorageResponse(eitherValidation.right().value()));
3264 throw new ComponentException(errorResponse);
3266 if (Boolean.TRUE.equals(eitherValidation.left().value())) {
3267 log.debug("resource with name: {}, already exists", resource.getName());
3268 ResponseFormat errorResponse = componentsUtils
3269 .getResponseFormat(ActionStatus.COMPONENT_NAME_ALREADY_EXIST, ComponentTypeEnum.RESOURCE.getValue(), resource.getName());
3270 throw new ComponentException(errorResponse);
3272 log.debug("send resource {} to dao for create", resource.getName());
3273 serviceImportParseLogic.createArtifactsPlaceHolderData(resource, user);
3275 log.debug("enrich resource with creator, version and state");
3276 resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
3277 resource.setVersion(INITIAL_VERSION);
3278 resource.setHighestVersion(true);
3279 if (resource.getResourceType() != null && resource.getResourceType() != ResourceTypeEnum.VF) {
3280 resource.setAbstract(false);
3283 return toscaOperationFacade.createToscaComponent(resource).left()
3284 .on(r -> serviceImportParseLogic.throwComponentExceptionByResource(r, resource));
3287 protected ImmutablePair<Resource, ActionStatus> updateExistingResourceByImport(Resource newResource, Resource oldResource, User user,
3288 boolean inTransaction, boolean needLock, boolean isNested) {
3289 String lockedResourceId = oldResource.getUniqueId();
3290 log.debug("found resource: name={}, id={}, version={}, state={}", oldResource.getName(), lockedResourceId, oldResource.getVersion(),
3291 oldResource.getLifecycleState());
3292 ImmutablePair<Resource, ActionStatus> resourcePair = null;
3294 serviceBusinessLogic.lockComponent(lockedResourceId, oldResource, needLock, "Update Resource by Import");
3295 oldResource = serviceImportParseLogic.prepareResourceForUpdate(oldResource, newResource, user, inTransaction, false);
3296 serviceImportParseLogic.mergeOldResourceMetadataWithNew(oldResource, newResource);
3297 serviceImportParseLogic.validateResourceFieldsBeforeUpdate(oldResource, newResource, inTransaction, isNested);
3298 serviceImportParseLogic.validateCapabilityTypesCreate(user, serviceImportParseLogic.getCapabilityTypeOperation(), newResource,
3299 AuditingActionEnum.IMPORT_RESOURCE, inTransaction);
3300 createNewResourceToOldResource(newResource, oldResource, user);
3301 Either<Resource, StorageOperationStatus> overrideResource = toscaOperationFacade.overrideComponent(newResource, oldResource);
3302 if (overrideResource.isRight()) {
3303 ResponseFormat responseFormat = new ResponseFormat();
3304 serviceBusinessLogic.throwComponentException(responseFormat);
3306 log.debug("Resource updated successfully!!!");
3307 resourcePair = new ImmutablePair<>(overrideResource.left().value(), ActionStatus.OK);
3308 return resourcePair;
3310 if (resourcePair == null) {
3311 BeEcompErrorManager.getInstance().logBeSystemError("Change LifecycleState - Certify");
3312 janusGraphDao.rollback();
3313 } else if (!inTransaction) {
3314 janusGraphDao.commit();
3317 log.debug("unlock resource {}", lockedResourceId);
3318 graphLockOperation.unlockComponent(lockedResourceId, NodeTypeEnum.Resource);
3323 protected void createNewResourceToOldResource(Resource newResource, Resource oldResource, User user) {
3324 newResource.setContactId(newResource.getContactId().toLowerCase());
3325 newResource.setCreatorUserId(user.getUserId());
3326 newResource.setCreatorFullName(user.getFullName());
3327 newResource.setLastUpdaterUserId(user.getUserId());
3328 newResource.setLastUpdaterFullName(user.getFullName());
3329 newResource.setUniqueId(oldResource.getUniqueId());
3330 newResource.setVersion(oldResource.getVersion());
3331 newResource.setInvariantUUID(oldResource.getInvariantUUID());
3332 newResource.setLifecycleState(oldResource.getLifecycleState());
3333 newResource.setUUID(oldResource.getUUID());
3334 newResource.setNormalizedName(oldResource.getNormalizedName());
3335 newResource.setSystemName(oldResource.getSystemName());
3336 if (oldResource.getCsarUUID() != null) {
3337 newResource.setCsarUUID(oldResource.getCsarUUID());
3339 if (oldResource.getCsarVersionId() != null) {
3340 newResource.setCsarVersionId(oldResource.getCsarVersionId());
3342 if (oldResource.getImportedToscaChecksum() != null) {
3343 newResource.setImportedToscaChecksum(oldResource.getImportedToscaChecksum());
3345 if (newResource.getDerivedFromGenericType() == null || newResource.getDerivedFromGenericType().isEmpty()) {
3346 newResource.setDerivedFromGenericType(oldResource.getDerivedFromGenericType());
3348 if (newResource.getDerivedFromGenericVersion() == null || newResource.getDerivedFromGenericVersion().isEmpty()) {
3349 newResource.setDerivedFromGenericVersion(oldResource.getDerivedFromGenericVersion());
3351 if (newResource.getToscaArtifacts() == null || newResource.getToscaArtifacts().isEmpty()) {
3352 serviceBusinessLogic.setToscaArtifactsPlaceHolders(newResource, user);
3354 if (newResource.getInterfaces() == null || newResource.getInterfaces().isEmpty()) {
3355 newResource.setInterfaces(oldResource.getInterfaces());
3357 if (CollectionUtils.isEmpty(newResource.getProperties())) {
3358 newResource.setProperties(oldResource.getProperties());
3360 if (newResource.getModel() == null) {
3361 newResource.setModel(oldResource.getModel());
3365 protected Map<String, Resource> createResourcesFromYamlNodeTypesList(String yamlName, Service service, Map<String, Object> mappedToscaTemplate,
3367 Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle,
3368 List<ArtifactDefinition> nodeTypesNewCreatedArtifacts,
3369 Map<String, NodeTypeInfo> nodeTypesInfo, CsarInfo csarInfo) {
3371 Either<String, ImportUtils.ResultStatusEnum> toscaVersion = findFirstToscaStringElement(mappedToscaTemplate,
3372 TypeUtils.ToscaTagNamesEnum.TOSCA_VERSION);
3373 if (toscaVersion.isRight()) {
3374 throw new ComponentException(ActionStatus.INVALID_TOSCA_TEMPLATE);
3376 Map<String, Object> mapToConvert = new HashMap<>();
3377 mapToConvert.put(TypeUtils.ToscaTagNamesEnum.TOSCA_VERSION.getElementName(), toscaVersion.left().value());
3378 Map<String, Object> nodeTypes = serviceImportParseLogic.getNodeTypesFromTemplate(mappedToscaTemplate);
3379 createNodeTypes(yamlName, service, needLock, nodeTypesArtifactsToHandle, nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo,
3380 mapToConvert, nodeTypes);
3381 return csarInfo.getCreatedNodes();
3382 } catch (Exception e) {
3383 log.debug("Exception occured when createResourcesFromYamlNodeTypesList,error is:{}", e.getMessage(), e);
3384 throw new ComponentException(ActionStatus.GENERAL_ERROR);
3388 protected void createNodeTypes(String yamlName, Service service, boolean needLock,
3389 Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle,
3390 List<ArtifactDefinition> nodeTypesNewCreatedArtifacts, Map<String, NodeTypeInfo> nodeTypesInfo, CsarInfo csarInfo,
3391 Map<String, Object> mapToConvert, Map<String, Object> nodeTypes) {
3392 Iterator<Map.Entry<String, Object>> nodesNameValueIter = nodeTypes.entrySet().iterator();
3393 Resource vfcCreated = null;
3394 while (nodesNameValueIter.hasNext()) {
3395 Map.Entry<String, Object> nodeType = nodesNameValueIter.next();
3396 String nodeTypeKey = nodeType.getKey();
3397 Map<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>> nodeTypeArtifactsToHandle =
3398 nodeTypesArtifactsToHandle == null || nodeTypesArtifactsToHandle.isEmpty() ? null : nodeTypesArtifactsToHandle.get(nodeTypeKey);
3399 if (nodeTypesInfo.containsKey(nodeTypeKey)) {
3400 vfcCreated = handleNestedVfc(service, nodeTypesArtifactsToHandle, nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo,
3402 log.trace("************* Finished to handle nested vfc {}", nodeTypeKey);
3403 } else if (csarInfo.getCreatedNodesToscaResourceNames() != null && !csarInfo.getCreatedNodesToscaResourceNames()
3404 .containsKey(nodeTypeKey)) {
3405 ImmutablePair<Resource, ActionStatus> resourceCreated = serviceImportParseLogic
3406 .createNodeTypeResourceFromYaml(yamlName, nodeType, csarInfo.getModifier(), mapToConvert, service, needLock,
3407 nodeTypeArtifactsToHandle, nodeTypesNewCreatedArtifacts, true, csarInfo, true);
3408 log.debug("************* Finished to create node {}", nodeTypeKey);
3409 vfcCreated = resourceCreated.getLeft();
3410 csarInfo.getCreatedNodesToscaResourceNames().put(nodeTypeKey, vfcCreated.getName());
3412 if (vfcCreated != null) {
3413 csarInfo.getCreatedNodes().put(nodeTypeKey, vfcCreated);
3415 mapToConvert.remove(TypeUtils.ToscaTagNamesEnum.NODE_TYPES.getElementName());