2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.be.components.impl;
23 import static org.openecomp.sdc.be.components.attribute.GetOutputUtils.isGetOutputValueForOutput;
24 import static org.openecomp.sdc.be.components.property.GetInputUtils.isGetInputValueForInput;
25 import static org.openecomp.sdc.be.components.utils.PropertiesUtils.getPropertyCapabilityOfChildInstance;
27 import com.google.common.collect.Sets;
28 import fj.data.Either;
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.Collection;
32 import java.util.Collections;
33 import java.util.HashMap;
34 import java.util.Iterator;
35 import java.util.List;
37 import java.util.Map.Entry;
38 import java.util.Objects;
39 import java.util.Optional;
41 import java.util.UUID;
42 import java.util.stream.Collectors;
43 import org.apache.commons.collections.CollectionUtils;
44 import org.apache.commons.collections.MapUtils;
45 import org.apache.commons.lang.StringUtils;
46 import org.apache.commons.lang3.tuple.ImmutablePair;
47 import org.openecomp.sdc.be.components.impl.exceptions.BusinessLogicException;
48 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
49 import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException;
50 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
51 import org.openecomp.sdc.be.components.impl.instance.ComponentInstanceChangeOperationOrchestrator;
52 import org.openecomp.sdc.be.components.impl.utils.DirectivesUtil;
53 import org.openecomp.sdc.be.components.merge.instance.ComponentInstanceMergeDataBusinessLogic;
54 import org.openecomp.sdc.be.components.merge.instance.DataForMergeHolder;
55 import org.openecomp.sdc.be.components.utils.PropertiesUtils;
56 import org.openecomp.sdc.be.components.validation.ComponentValidations;
57 import org.openecomp.sdc.be.config.BeEcompErrorManager;
58 import org.openecomp.sdc.be.config.BeEcompErrorManager.ErrorSeverity;
59 import org.openecomp.sdc.be.config.ConfigurationManager;
60 import org.openecomp.sdc.be.dao.api.ActionStatus;
61 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
62 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
63 import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary;
64 import org.openecomp.sdc.be.datatypes.elements.CINodeFilterDataDefinition;
65 import org.openecomp.sdc.be.datatypes.elements.CapabilityDataDefinition;
66 import org.openecomp.sdc.be.datatypes.elements.ForwardingPathDataDefinition;
67 import org.openecomp.sdc.be.datatypes.elements.GetInputValueDataDefinition;
68 import org.openecomp.sdc.be.datatypes.elements.GetOutputValueDataDefinition;
69 import org.openecomp.sdc.be.datatypes.elements.GetPolicyValueDataDefinition;
70 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
71 import org.openecomp.sdc.be.datatypes.elements.RequirementDataDefinition;
72 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
73 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
74 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
75 import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
76 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
77 import org.openecomp.sdc.be.impl.ComponentsUtils;
78 import org.openecomp.sdc.be.impl.ForwardingPathUtils;
79 import org.openecomp.sdc.be.impl.ServiceFilterUtils;
80 import org.openecomp.sdc.be.info.CreateAndAssotiateInfo;
81 import org.openecomp.sdc.be.model.ArtifactDefinition;
82 import org.openecomp.sdc.be.model.CapabilityDefinition;
83 import org.openecomp.sdc.be.model.Component;
84 import org.openecomp.sdc.be.model.ComponentInstance;
85 import org.openecomp.sdc.be.model.ComponentInstanceAttribute;
86 import org.openecomp.sdc.be.model.ComponentInstanceInput;
87 import org.openecomp.sdc.be.model.ComponentInstanceOutput;
88 import org.openecomp.sdc.be.model.ComponentInstancePropInput;
89 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
90 import org.openecomp.sdc.be.model.ComponentParametersView;
91 import org.openecomp.sdc.be.model.DataTypeDefinition;
92 import org.openecomp.sdc.be.model.GroupDefinition;
93 import org.openecomp.sdc.be.model.InputDefinition;
94 import org.openecomp.sdc.be.model.InterfaceDefinition;
95 import org.openecomp.sdc.be.model.LifecycleStateEnum;
96 import org.openecomp.sdc.be.model.PolicyDefinition;
97 import org.openecomp.sdc.be.model.PropertyDefinition;
98 import org.openecomp.sdc.be.model.RelationshipInfo;
99 import org.openecomp.sdc.be.model.RequirementCapabilityRelDef;
100 import org.openecomp.sdc.be.model.RequirementDefinition;
101 import org.openecomp.sdc.be.model.Resource;
102 import org.openecomp.sdc.be.model.Service;
103 import org.openecomp.sdc.be.model.User;
104 import org.openecomp.sdc.be.model.jsonjanusgraph.config.ContainerInstanceTypesData;
105 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ArtifactsOperations;
106 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ForwardingPathOperation;
107 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.InterfaceOperation;
108 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.NodeFilterOperation;
109 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
110 import org.openecomp.sdc.be.model.jsonjanusgraph.utils.ModelConverter;
111 import org.openecomp.sdc.be.model.operations.StorageException;
112 import org.openecomp.sdc.be.model.operations.api.IElementOperation;
113 import org.openecomp.sdc.be.model.operations.api.IGroupInstanceOperation;
114 import org.openecomp.sdc.be.model.operations.api.IGroupOperation;
115 import org.openecomp.sdc.be.model.operations.api.IGroupTypeOperation;
116 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
117 import org.openecomp.sdc.be.model.operations.impl.ComponentInstanceOperation;
118 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
119 import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation;
120 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
121 import org.openecomp.sdc.be.model.operations.utils.ComponentValidationUtils;
122 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
123 import org.openecomp.sdc.be.resources.data.ComponentInstanceData;
124 import org.openecomp.sdc.be.user.Role;
125 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
126 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
127 import org.openecomp.sdc.common.api.Constants;
128 import org.openecomp.sdc.common.datastructure.Wrapper;
129 import org.openecomp.sdc.common.jsongraph.util.CommonUtility;
130 import org.openecomp.sdc.common.jsongraph.util.CommonUtility.LogLevelEnum;
131 import org.openecomp.sdc.common.log.wrappers.Logger;
132 import org.openecomp.sdc.common.util.ValidationUtils;
133 import org.openecomp.sdc.exception.ResponseFormat;
134 import org.springframework.beans.factory.annotation.Autowired;
136 @org.springframework.stereotype.Component
137 public class ComponentInstanceBusinessLogic extends BaseBusinessLogic {
139 private static final Logger log = Logger.getLogger(ComponentInstanceBusinessLogic.class);
140 private static final String VF_MODULE = "org.openecomp.groups.VfModule";
141 private static final String TRY_TO_CREATE_ENTRY_ON_GRAPH = "Try to create entry on graph";
142 private static final String CLOUD_SPECIFIC_FIXED_KEY_WORD = "cloudtech";
143 private static final String[][] CLOUD_SPECIFIC_KEY_WORDS = {{"k8s", "azure", "aws"}, /* cloud specific technology */
144 {"charts", "day0", "configtemplate"} /*cloud specific sub type*/};
145 private static final String FAILED_TO_CREATE_ENTRY_ON_GRAPH_FOR_COMPONENT_INSTANCE = "Failed to create entry on graph for component instance {}";
146 private static final String ENTITY_ON_GRAPH_IS_CREATED = "Entity on graph is created.";
147 private static final String INVALID_COMPONENT_TYPE = "invalid component type";
148 private static final String FAILED_TO_RETRIEVE_COMPONENT_COMPONENT_ID = "Failed to retrieve component, component id {}";
149 private static final String FAILED_TO_LOCK_SERVICE = "Failed to lock service {}";
150 private static final String CREATE_OR_UPDATE_PROPERTY_VALUE = "CreateOrUpdatePropertyValue";
151 private static final String FAILED_TO_COPY_COMP_INSTANCE_TO_CANVAS = "Failed to copy the component instance to the canvas";
152 private static final String COPY_COMPONENT_INSTANCE_OK = "Copy component instance OK";
153 private static final String CANNOT_ATTACH_RESOURCE_INSTANCES_TO_CONTAINER_RESOURCE_OF_TYPE = "Cannot attach resource instances to container resource of type {}";
154 private static final String SERVICE_PROXY = "serviceProxy";
155 private static final String ASSOCIATE_RI_TO_RI = "associateRIToRI";
157 private ComponentInstanceOperation componentInstanceOperation;
158 private ArtifactsBusinessLogic artifactBusinessLogic;
159 private ComponentInstanceMergeDataBusinessLogic compInstMergeDataBL;
160 private ComponentInstanceChangeOperationOrchestrator onChangeInstanceOperationOrchestrator;
161 private ForwardingPathOperation forwardingPathOperation;
162 private NodeFilterOperation nodeFilterOperation;
165 public ComponentInstanceBusinessLogic(IElementOperation elementDao,
166 IGroupOperation groupOperation,
167 IGroupInstanceOperation groupInstanceOperation,
168 IGroupTypeOperation groupTypeOperation,
169 InterfaceOperation interfaceOperation,
170 InterfaceLifecycleOperation interfaceLifecycleTypeOperation,
171 ComponentInstanceOperation componentInstanceOperation, ArtifactsBusinessLogic artifactBusinessLogic,
172 ComponentInstanceMergeDataBusinessLogic compInstMergeDataBL,
173 ComponentInstanceChangeOperationOrchestrator onChangeInstanceOperationOrchestrator,
174 ForwardingPathOperation forwardingPathOperation, NodeFilterOperation nodeFilterOperation,
175 ArtifactsOperations artifactToscaOperation) {
176 super(elementDao, groupOperation, groupInstanceOperation, groupTypeOperation,
177 interfaceOperation, interfaceLifecycleTypeOperation, artifactToscaOperation);
178 this.componentInstanceOperation = componentInstanceOperation;
179 this.artifactBusinessLogic = artifactBusinessLogic;
180 this.compInstMergeDataBL = compInstMergeDataBL;
181 this.onChangeInstanceOperationOrchestrator = onChangeInstanceOperationOrchestrator;
182 this.forwardingPathOperation = forwardingPathOperation;
183 this.nodeFilterOperation = nodeFilterOperation;
187 private CompositionBusinessLogic compositionBusinessLogic;
190 private ContainerInstanceTypesData containerInstanceTypesData;
192 public ComponentInstance createComponentInstance(String containerComponentParam, String containerComponentId, String userId,
193 ComponentInstance resourceInstance) {
194 return createComponentInstance(containerComponentParam, containerComponentId, userId, resourceInstance, true);
197 public List<ComponentInstanceProperty> getComponentInstancePropertiesByInputId(org.openecomp.sdc.be.model.Component component,
199 List<ComponentInstanceProperty> resList = new ArrayList<>();
200 Map<String, List<ComponentInstanceProperty>> ciPropertiesMap = component.getComponentInstancesProperties();
201 if (ciPropertiesMap != null && !ciPropertiesMap.isEmpty()) {
202 ciPropertiesMap.forEach((s, ciPropList) -> {
204 Optional<ComponentInstance> ciOp = component.getComponentInstances().stream().filter(ci -> ci.getUniqueId().equals(s))
206 if (ciOp.isPresent()) {
207 ciName = ciOp.get().getName();
209 if (ciPropList != null && !ciPropList.isEmpty()) {
210 for (ComponentInstanceProperty prop : ciPropList) {
211 List<GetInputValueDataDefinition> inputsValues = prop.getGetInputValues();
212 addCompInstanceProperty(s, ciName, prop, inputsValues, inputId, resList);
221 public List<ComponentInstanceAttribute> getComponentInstanceAttributesByOutputId(final org.openecomp.sdc.be.model.Component component,
222 final String outputId) {
223 final List<ComponentInstanceAttribute> resList = new ArrayList<>();
224 final Map<String, List<ComponentInstanceAttribute>> componentInstancesAttributes = component.getComponentInstancesAttributes();
225 if (org.apache.commons.collections4.MapUtils.isNotEmpty(componentInstancesAttributes)) {
226 componentInstancesAttributes.forEach((s, componentInstanceAttributeList) -> {
228 final Optional<ComponentInstance> ciOp = component.getComponentInstances().stream().filter(ci -> ci.getUniqueId().equals(s)).findAny();
229 if (ciOp.isPresent()) {
230 ciName = ciOp.get().getName();
232 if (componentInstanceAttributeList != null && !componentInstanceAttributeList.isEmpty()) {
233 for (final ComponentInstanceAttribute compInstanceAttribute : componentInstanceAttributeList) {
234 List<GetOutputValueDataDefinition> outputValues = compInstanceAttribute.getGetOutputValues();
235 addCompInstanceAttribute(s, ciName, compInstanceAttribute, outputValues, outputId, resList);
244 private void addCompInstanceProperty(String s, String ciName, ComponentInstanceProperty prop, List<GetInputValueDataDefinition> inputsValues,
245 String inputId, List<ComponentInstanceProperty> resList) {
246 if (inputsValues != null && !inputsValues.isEmpty()) {
247 for (GetInputValueDataDefinition inputData : inputsValues) {
248 if (isGetInputValueForInput(inputData, inputId)) {
249 prop.setComponentInstanceId(s);
250 prop.setComponentInstanceName(ciName);
258 private void addCompInstanceAttribute(final String s,
260 final ComponentInstanceAttribute attribute,
261 final List<GetOutputValueDataDefinition> outputsValues,
262 final String outputId,
263 final List<ComponentInstanceAttribute> resList) {
264 if (outputsValues != null && !outputsValues.isEmpty()) {
265 for (final GetOutputValueDataDefinition outputData : outputsValues) {
266 if (isGetOutputValueForOutput(outputData, outputId)) {
267 attribute.setComponentInstanceId(s);
268 attribute.setComponentInstanceName(ciName);
269 resList.add(attribute);
276 public Optional<ComponentInstanceProperty> getComponentInstancePropertyByPolicyId(Component component,
277 PolicyDefinition policy) {
279 Optional<ComponentInstanceProperty> propertyCandidate = getComponentInstancePropertyByPolicy(component, policy);
281 if (propertyCandidate.isPresent()) {
282 ComponentInstanceProperty componentInstanceProperty = propertyCandidate.get();
283 Optional<GetPolicyValueDataDefinition> getPolicyCandidate =
284 getGetPolicyValueDataDefinition(policy, componentInstanceProperty);
286 getPolicyCandidate.ifPresent(getPolicyValue ->
287 updateComponentInstancePropertyAfterUndeclaration(componentInstanceProperty, getPolicyValue, policy));
288 return Optional.of(componentInstanceProperty);
291 return Optional.empty();
295 private void updateComponentInstancePropertyAfterUndeclaration(ComponentInstanceProperty componentInstanceProperty,
296 GetPolicyValueDataDefinition getPolicyValue, PolicyDefinition policyDefinition) {
297 componentInstanceProperty.setValue(getPolicyValue.getOrigPropertyValue());
298 List<GetPolicyValueDataDefinition> getPolicyValues = componentInstanceProperty.getGetPolicyValues();
299 if(CollectionUtils.isNotEmpty(getPolicyValues)) {
300 getPolicyValues.remove(getPolicyValue);
301 componentInstanceProperty.setGetPolicyValues(getPolicyValues);
302 policyDefinition.setGetPolicyValues(getPolicyValues);
306 private Optional<GetPolicyValueDataDefinition> getGetPolicyValueDataDefinition(PolicyDefinition policy,
307 ComponentInstanceProperty componentInstanceProperty) {
308 List<GetPolicyValueDataDefinition> getPolicyValues = policy.getGetPolicyValues();
309 return getPolicyValues.stream()
310 .filter(getPolicyValue -> getPolicyValue
312 .equals(componentInstanceProperty
317 private Optional<ComponentInstanceProperty> getComponentInstancePropertyByPolicy(Component component,
318 PolicyDefinition policy) {
319 Map<String, List<ComponentInstanceProperty>> componentInstancesProperties =
320 component.getComponentInstancesProperties();
322 if(MapUtils.isEmpty(componentInstancesProperties)) {
323 return Optional.empty();
326 String instanceUniqueId = policy.getInstanceUniqueId();
328 List<ComponentInstanceProperty> componentInstanceProperties =
329 componentInstancesProperties.containsKey(instanceUniqueId)
330 ? componentInstancesProperties.get(instanceUniqueId)
333 return componentInstanceProperties
334 .stream().filter(property -> property.getName().equals(policy.getName())).findAny();
337 public List<ComponentInstanceInput> getComponentInstanceInputsByInputId(
338 org.openecomp.sdc.be.model.Component component, String inputId) {
339 List<ComponentInstanceInput> resList = new ArrayList<>();
340 Map<String, List<ComponentInstanceInput>> ciInputsMap = component.getComponentInstancesInputs();
341 if (ciInputsMap != null && !ciInputsMap.isEmpty()) {
342 ciInputsMap.forEach((s, ciPropList) -> {
344 Optional<ComponentInstance> ciOp = component.getComponentInstances().stream().filter(ci -> ci.getUniqueId().equals(s))
346 if (ciOp.isPresent()) {
347 ciName = ciOp.get().getName();
349 if (ciPropList != null && !ciPropList.isEmpty()) {
350 for (ComponentInstanceInput prop : ciPropList) {
351 List<GetInputValueDataDefinition> inputsValues = prop.getGetInputValues();
352 addCompInstanceInput(s, ciName, prop, inputsValues, inputId, resList);
362 public List<ComponentInstanceOutput> getComponentInstanceOutputsByOutputId(final org.openecomp.sdc.be.model.Component component,
363 final String outputId) {
364 final List<ComponentInstanceOutput> resList = new ArrayList<>();
365 final Map<String, List<ComponentInstanceOutput>> ciInputsMap = component.getComponentInstancesOutputs();
366 if (ciInputsMap != null && !ciInputsMap.isEmpty()) {
367 ciInputsMap.forEach((s, ciPropList) -> {
369 final Optional<ComponentInstance> ciOp = component.getComponentInstances().stream().filter(ci -> ci.getUniqueId().equals(s)).findAny();
370 if (ciOp.isPresent()) {
371 ciName = ciOp.get().getName();
373 if (ciPropList != null && !ciPropList.isEmpty()) {
374 for (final ComponentInstanceOutput prop : ciPropList) {
375 final List<GetOutputValueDataDefinition> outputValues = prop.getGetOutputValues();
376 addCompInstanceOutput(s, ciName, prop, outputValues, outputId, resList);
386 private void addCompInstanceInput(String s, String ciName, ComponentInstanceInput prop, List<GetInputValueDataDefinition> inputsValues,
387 String inputId, List<ComponentInstanceInput> resList) {
388 if (inputsValues != null && !inputsValues.isEmpty()) {
389 for (GetInputValueDataDefinition inputData : inputsValues) {
390 if (isGetInputValueForInput(inputData, inputId)) {
391 prop.setComponentInstanceId(s);
392 prop.setComponentInstanceName(ciName);
400 private void addCompInstanceOutput(final String s,
402 final ComponentInstanceOutput prop,
403 final List<GetOutputValueDataDefinition> outputsValues,
404 final String outputId,
405 final List<ComponentInstanceOutput> resList) {
406 if (outputsValues != null && !outputsValues.isEmpty()) {
407 for (final GetOutputValueDataDefinition outputData : outputsValues) {
408 if (isGetOutputValueForOutput(outputData, outputId)) {
409 prop.setComponentInstanceId(s);
410 prop.setComponentInstanceName(ciName);
418 public ComponentInstance createComponentInstance(final String containerComponentParam,
419 final String containerComponentId, final String userId,
420 final ComponentInstance resourceInstance, final boolean needLock) {
421 final User user = validateUserExists(userId);
422 validateUserNotEmpty(user, "Create component instance");
423 validateJsonBody(resourceInstance, ComponentInstance.class);
424 final ComponentTypeEnum containerComponentType = validateComponentType(containerComponentParam);
425 final org.openecomp.sdc.be.model.Component containerComponent =
426 validateComponentExists(containerComponentId, containerComponentType, null);
428 if (ModelConverter.isAtomicComponent(containerComponent)) {
429 if (log.isDebugEnabled()) {
430 log.debug(CANNOT_ATTACH_RESOURCE_INSTANCES_TO_CONTAINER_RESOURCE_OF_TYPE, containerComponent.assetType());
432 throw new ByActionStatusComponentException(ActionStatus.RESOURCE_CANNOT_CONTAIN_RESOURCE_INSTANCES, containerComponent.assetType());
435 validateCanWorkOnComponent(containerComponent, userId);
437 Component origComponent = null;
438 if (resourceInstance != null && containerComponentType != null) {
439 final OriginTypeEnum originType = resourceInstance.getOriginType();
440 validateInstanceName(resourceInstance);
441 if (originType == OriginTypeEnum.ServiceProxy) {
442 origComponent = getOrigComponentForServiceProxy(containerComponent, resourceInstance);
443 } else if (originType == OriginTypeEnum.ServiceSubstitution) {
444 origComponent = getOrigComponentForServiceSubstitution(resourceInstance);
446 origComponent = getAndValidateOriginComponentOfComponentInstance(containerComponent, resourceInstance);
447 validateOriginAndResourceInstanceTypes(containerComponent, origComponent, originType);
449 validateResourceInstanceState(containerComponent, origComponent);
450 overrideFields(origComponent, resourceInstance);
451 compositionBusinessLogic.validateAndSetDefaultCoordinates(resourceInstance);
453 return createComponent(needLock, containerComponent,origComponent, resourceInstance, user);
457 private Component getOrigComponentForServiceProxy(org.openecomp.sdc.be.model.Component containerComponent, ComponentInstance resourceInstance) {
458 Either<Component, StorageOperationStatus> serviceProxyOrigin = toscaOperationFacade.getLatestByName(SERVICE_PROXY);
459 if (isServiceProxyOrigin(serviceProxyOrigin)) {
460 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(serviceProxyOrigin.right().value()));
462 Component origComponent = serviceProxyOrigin.left().value();
464 StorageOperationStatus fillProxyRes = fillInstanceData(resourceInstance, origComponent);
465 if (isFillProxyRes(fillProxyRes)) {
466 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(fillProxyRes));
468 validateOriginAndResourceInstanceTypes(containerComponent, origComponent, OriginTypeEnum.ServiceProxy);
469 return origComponent;
472 private Component getOrigComponentForServiceSubstitution(ComponentInstance resourceInstance) {
473 final Either<Component, StorageOperationStatus> getServiceResult = toscaOperationFacade.getToscaFullElement(resourceInstance.getComponentUid());
474 if (getServiceResult.isRight()) {
475 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(getServiceResult.right().value()));
477 final Component service = getServiceResult.left().value();
479 final Either<Component, StorageOperationStatus> getServiceDerivedFromTypeResult = toscaOperationFacade.getLatestByToscaResourceName(service.getDerivedFromGenericType());
480 if (getServiceDerivedFromTypeResult.isRight()) {
481 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(getServiceResult.right().value()));
484 Component origComponent = getServiceDerivedFromTypeResult.left().value();
486 final StorageOperationStatus fillProxyRes = fillInstanceData(resourceInstance, origComponent);
487 if (isFillProxyRes(fillProxyRes)) {
488 throw new ByActionStatusComponentException(
489 componentsUtils.convertFromStorageResponse(fillProxyRes));
491 return origComponent;
494 private ComponentInstance createComponent(boolean needLock, Component containerComponent, Component origComponent, ComponentInstance resourceInstance, User user) {
496 boolean failed = false;
499 lockIfNeed(needLock, containerComponent);
501 log.debug(TRY_TO_CREATE_ENTRY_ON_GRAPH);
502 return createComponentInstanceOnGraph(containerComponent, origComponent, resourceInstance, user);
503 }catch (ComponentException e){
508 unlockComponent(failed, containerComponent);
513 * Try using either to make a judgment
514 * @param containerComponentParam
515 * @param containerComponentId
517 * @param resourceInstance
521 public Either<ComponentInstance, ResponseFormat> createRealComponentInstance(String containerComponentParam, String containerComponentId, String userId, ComponentInstance resourceInstance) {
522 log.debug("enter createRealComponentInstance");
523 return createRealComponentInstance(containerComponentParam, containerComponentId, userId, resourceInstance, true);
527 * Try using either to make a judgment
530 * @param containerComponentParam
531 * @param containerComponentId
533 * @param resourceInstance
536 public Either<ComponentInstance, ResponseFormat> createRealComponentInstance(String containerComponentParam,
537 String containerComponentId, String userId,
538 ComponentInstance resourceInstance,
541 log.debug("enter createRealComponentInstance");
542 Component origComponent = null;
544 org.openecomp.sdc.be.model.Component containerComponent = null;
545 ComponentTypeEnum containerComponentType;
547 user = validateUserExists(userId);
548 validateUserNotEmpty(user, "Create component instance");
549 validateJsonBody(resourceInstance, ComponentInstance.class);
550 containerComponentType = validateComponentType(containerComponentParam);
551 containerComponent = validateComponentExists(containerComponentId, containerComponentType, null);
553 log.debug("enter createRealComponentInstance,validate user json success");
554 if (ModelConverter.isAtomicComponent(containerComponent)) {
555 log.debug(CANNOT_ATTACH_RESOURCE_INSTANCES_TO_CONTAINER_RESOURCE_OF_TYPE, containerComponent.assetType());
556 throw new ByActionStatusComponentException(ActionStatus.RESOURCE_CANNOT_CONTAIN_RESOURCE_INSTANCES, containerComponent.assetType());
559 validateCanWorkOnComponent(containerComponent, userId);
560 log.debug("enter createRealComponentInstance,validateCanWorkOnComponent success");
562 if (resourceInstance != null && containerComponentType != null) {
563 log.debug("enter createRealComponentInstance,start create ComponentInstance");
564 OriginTypeEnum originType = resourceInstance.getOriginType();
565 validateInstanceName(resourceInstance);
566 if (originType == OriginTypeEnum.ServiceProxy) {
568 log.debug("enter createRealComponentInstance,originType equals ServiceProxy");
569 Either<Component, StorageOperationStatus> serviceProxyOrigin = toscaOperationFacade.getLatestByName(SERVICE_PROXY);
570 if (isServiceProxyOrigin(serviceProxyOrigin)) {
571 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(serviceProxyOrigin.right().value()));
573 origComponent = serviceProxyOrigin.left().value();
575 StorageOperationStatus fillProxyRes = fillInstanceData(resourceInstance, origComponent);
576 if (isFillProxyRes(fillProxyRes)) {
577 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(fillProxyRes));
580 log.debug("enter createRealComponentInstance,originType is not ServiceProxy");
581 origComponent = getAndValidateOriginComponentOfComponentInstance(containerComponent, resourceInstance);
583 validateOriginAndResourceInstanceTypes(containerComponent, origComponent, originType);
584 validateResourceInstanceState(containerComponent, origComponent);
585 overrideFields(origComponent, resourceInstance);
586 compositionBusinessLogic.validateAndSetDefaultCoordinates(resourceInstance);
587 log.debug("enter createRealComponentInstance,final validate success");
589 return createRealComponent(needLock, containerComponent, origComponent, resourceInstance, user);
591 } catch (ComponentException e) {
592 log.debug("create Real Component Instance failed");
597 private Either<ComponentInstance, ResponseFormat> createRealComponent(boolean needLock, Component containerComponent, Component origComponent, ComponentInstance resourceInstance, User user) {
599 log.debug("enter createRealComponent");
600 boolean failed = false;
603 lockIfNeed(needLock, containerComponent);
605 log.debug(TRY_TO_CREATE_ENTRY_ON_GRAPH);
606 return createRealComponentInstanceOnGraph(containerComponent, origComponent, resourceInstance, user);
607 } catch (ComponentException e) {
612 unlockComponent(failed, containerComponent);
616 private Either<ComponentInstance, ResponseFormat> createRealComponentInstanceOnGraph(org.openecomp.sdc.be.model.Component containerComponent, Component originComponent, ComponentInstance componentInstance, User user) {
617 log.debug("enter createRealComponentInstanceOnGraph");
619 Either<ImmutablePair<Component, String>, StorageOperationStatus> result = toscaOperationFacade.addComponentInstanceToTopologyTemplate(containerComponent, originComponent, componentInstance, false, user);
621 if (result.isRight()) {
622 log.debug("enter createRealComponentInstanceOnGraph,result is right");
623 ActionStatus status = componentsUtils.convertFromStorageResponse(result.right().value());
624 log.debug(FAILED_TO_CREATE_ENTRY_ON_GRAPH_FOR_COMPONENT_INSTANCE, componentInstance.getName());
625 return Either.right(componentsUtils.getResponseFormat(status));
628 log.debug(ENTITY_ON_GRAPH_IS_CREATED);
629 log.debug("enter createRealComponentInstanceOnGraph,Entity on graph is created.");
630 Component updatedComponent = result.left().value().getLeft();
631 Map<String, String> existingEnvVersions = new HashMap<>();
632 // TODO existingEnvVersions ??
633 addComponentInstanceArtifacts(updatedComponent, componentInstance, originComponent, user, existingEnvVersions);
635 Optional<ComponentInstance> updatedInstanceOptional = updatedComponent.getComponentInstances().stream().filter(ci -> ci.getUniqueId().equals(result.left().value().getRight())).findFirst();
636 if (!updatedInstanceOptional.isPresent()) {
637 log.debug("Failed to fetch new added component instance {} from component {}", componentInstance.getName(), containerComponent.getName());
638 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER, componentInstance.getName());
640 log.debug("enter createRealComponentInstanceOnGraph,and final success");
641 return Either.left(updatedInstanceOptional.get());
644 private void overrideFields(Component origComponent, ComponentInstance resourceInstance) {
645 resourceInstance.setComponentVersion(origComponent.getVersion());
646 resourceInstance.setIcon(origComponent.getIcon());
649 private void validateInstanceName(ComponentInstance resourceInstance) {
651 String resourceInstanceName = resourceInstance.getName();
652 if (StringUtils.isEmpty(resourceInstanceName)) {
653 log.debug("ComponentInstance name is empty");
654 throw new ByActionStatusComponentException(ActionStatus.INVALID_COMPONENT_NAME, resourceInstance.getName());
657 if (!ValidationUtils.validateComponentNameLength(resourceInstanceName)) {
658 log.debug("ComponentInstance name exceeds max length {} ", ValidationUtils.COMPONENT_NAME_MAX_LENGTH);
659 throw new ByActionStatusComponentException(ActionStatus.INVALID_COMPONENT_NAME, resourceInstance.getName());
662 if (!ValidationUtils.validateComponentNamePattern(resourceInstanceName)) {
663 log.debug("ComponentInstance name {} has invalid format", resourceInstanceName);
664 throw new ByActionStatusComponentException(ActionStatus.INVALID_COMPONENT_NAME, resourceInstance.getName());
668 private void validateResourceInstanceState(Component containerComponent, Component origComponent) {
669 if (origComponent.getLifecycleState() == LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT){
670 throw new ByActionStatusComponentException(ActionStatus.CONTAINER_CANNOT_CONTAIN_INSTANCE,
671 containerComponent.getComponentType().getValue(), origComponent.getLifecycleState().toString());
675 private void validateOriginAndResourceInstanceTypes(final Component containerComponent,
676 final Component origComponent,
677 final OriginTypeEnum originType) {
678 final ResourceTypeEnum resourceType = getResourceTypeEnumFromOriginComponent(origComponent);
679 validateOriginType(originType, resourceType);
680 validateOriginComponentIsValidForContainer(containerComponent, resourceType);
683 private void validateOriginComponentIsValidForContainer(Component containerComponent, ResourceTypeEnum resourceType) {
684 switch (containerComponent.getComponentType()) {
686 if (!containerInstanceTypesData.isAllowedForServiceComponent(resourceType)) {
687 throw new ByActionStatusComponentException(ActionStatus.CONTAINER_CANNOT_CONTAIN_INSTANCE,
688 containerComponent.getComponentType().toString(), resourceType.name());
692 final ResourceTypeEnum componentResourceType = ((Resource) containerComponent).getResourceType();
693 if (!containerInstanceTypesData.isAllowedForResourceComponent(componentResourceType, resourceType)) {
694 throw new ByActionStatusComponentException(ActionStatus.CONTAINER_CANNOT_CONTAIN_INSTANCE,
695 containerComponent.getComponentType().toString(), resourceType.name());
699 throw new ByActionStatusComponentException(ActionStatus.INVALID_CONTENT);
703 private void validateOriginType(OriginTypeEnum originType, ResourceTypeEnum resourceType) {
704 ResourceTypeEnum convertedOriginType;
706 convertedOriginType = ResourceTypeEnum.getTypeIgnoreCase(originType.name());
709 throw new ByActionStatusComponentException(ActionStatus.INVALID_CONTENT);
712 if (resourceType != convertedOriginType) throw new ByActionStatusComponentException(ActionStatus.INVALID_CONTENT);
715 private ResourceTypeEnum getResourceTypeEnumFromOriginComponent(final Component origComponent) {
716 switch (origComponent.getComponentType()) {
718 return ResourceTypeEnum.ServiceProxy;
720 return ((Resource) origComponent).getResourceType();
722 throw new ByActionStatusComponentException(ActionStatus.INVALID_CONTENT);
726 private void lockIfNeed(boolean needLock, Component containerComponent) {
728 lockComponent(containerComponent, "createComponentInstance");
732 private boolean isServiceProxyOrigin(Either<Component, StorageOperationStatus> serviceProxyOrigin) {
733 if (serviceProxyOrigin.isRight()) {
734 log.debug("Failed to fetch normative service proxy resource by tosca name, error {}", serviceProxyOrigin.right().value());
740 private StorageOperationStatus fillInstanceData(ComponentInstance resourceInstance, Component origComponent) {
741 final ComponentParametersView filter = new ComponentParametersView(true);
742 filter.setIgnoreCapabilities(false);
743 filter.setIgnoreCapabiltyProperties(false);
744 filter.setIgnoreComponentInstances(false);
745 filter.setIgnoreRequirements(false);
746 filter.setIgnoreInterfaces(false);
747 filter.setIgnoreProperties(false);
748 filter.setIgnoreAttributes(false);
749 filter.setIgnoreInputs(false);
750 Either<Component, StorageOperationStatus> serviceRes =
751 toscaOperationFacade.getToscaElement(resourceInstance.getComponentUid(), filter);
752 if (serviceRes.isRight()) {
753 return serviceRes.right().value();
755 final Component service = serviceRes.left().value();
756 final Map<String, List<CapabilityDefinition>> capabilities = service.getCapabilities();
757 resourceInstance.setCapabilities(capabilities);
758 final Map<String, List<RequirementDefinition>> req = service.getRequirements();
759 resourceInstance.setRequirements(req);
760 final Map<String, InterfaceDefinition> serviceInterfaces = service.getInterfaces();
761 if(MapUtils.isNotEmpty(serviceInterfaces)) {
762 serviceInterfaces.forEach(resourceInstance::addInterface);
764 resourceInstance.setProperties(PropertiesUtils.getProperties(service));
765 resourceInstance.setAttributes(service.getAttributes());
767 final List<InputDefinition> serviceInputs = service.getInputs();
768 resourceInstance.setInputs(serviceInputs);
769 resourceInstance.setSourceModelInvariant(service.getInvariantUUID());
770 resourceInstance.setSourceModelName(service.getName());
771 resourceInstance.setSourceModelUuid(service.getUUID());
772 resourceInstance.setSourceModelUid(service.getUniqueId());
773 resourceInstance.setComponentUid(origComponent.getUniqueId());
774 resourceInstance.setComponentVersion(service.getVersion());
776 switch(resourceInstance.getOriginType()) {
778 return fillProxyInstanceData(resourceInstance, origComponent, service);
779 case ServiceSubstitution:
780 return fillServiceSubstitutableNodeTypeData(resourceInstance, service);
782 return StorageOperationStatus.OK;
786 private StorageOperationStatus fillProxyInstanceData(final ComponentInstance resourceInstance, final Component origComponent, final Component service) {
787 final String name = ValidationUtils.normalizeComponentInstanceName(service.getName()) + ToscaOperationFacade.PROXY_SUFFIX;
788 final String toscaResourceName = ((Resource) origComponent).getToscaResourceName();
789 final int lastIndexOf = toscaResourceName.lastIndexOf('.');
790 if (lastIndexOf != -1) {
791 final String proxyToscaName = toscaResourceName.substring(0, lastIndexOf + 1) + name;
792 resourceInstance.setToscaComponentName(proxyToscaName);
794 resourceInstance.setName(name);
795 resourceInstance.setIsProxy(true);
796 resourceInstance.setDescription("A Proxy for Service " + service.getName());
797 return StorageOperationStatus.OK;
800 private StorageOperationStatus fillServiceSubstitutableNodeTypeData(final ComponentInstance resourceInstance, final Component service) {
801 resourceInstance.setToscaComponentName("org.openecomp.service." + ValidationUtils.convertToSystemName(service.getName()));
802 resourceInstance.setName(ValidationUtils.normalizeComponentInstanceName(service.getName()));
803 resourceInstance.setIsProxy(false);
804 resourceInstance.setDescription("A substitutable node type for service " + service.getName());
806 return StorageOperationStatus.OK;
809 public Either<CreateAndAssotiateInfo, ResponseFormat> createAndAssociateRIToRI(String containerComponentParam, String containerComponentId, String userId, CreateAndAssotiateInfo createAndAssotiateInfo) {
811 Either<CreateAndAssotiateInfo, ResponseFormat> resultOp = null;
812 ComponentInstance resourceInstance = createAndAssotiateInfo.getNode();
813 RequirementCapabilityRelDef associationInfo = createAndAssotiateInfo.getAssociate();
815 User user = validateUserExists(userId);
817 final ComponentTypeEnum containerComponentType = validateComponentType(containerComponentParam);
819 org.openecomp.sdc.be.model.Component containerComponent = validateComponentExists(containerComponentId, containerComponentType, null);
821 if (ModelConverter.isAtomicComponent(containerComponent)) {
822 log.debug(CANNOT_ATTACH_RESOURCE_INSTANCES_TO_CONTAINER_RESOURCE_OF_TYPE, containerComponent.assetType());
823 return Either.right(componentsUtils.getResponseFormat(ActionStatus.RESOURCE_CANNOT_CONTAIN_RESOURCE_INSTANCES, containerComponent.assetType()));
826 validateCanWorkOnComponent(containerComponent, userId);
828 boolean failed = false;
830 lockComponent(containerComponent, "createAndAssociateRIToRI");
831 log.debug(TRY_TO_CREATE_ENTRY_ON_GRAPH);
832 Component origComponent = getOriginComponentFromComponentInstance(resourceInstance);
834 log.debug(ENTITY_ON_GRAPH_IS_CREATED);
835 ComponentInstance resResourceInfo = createComponentInstanceOnGraph(containerComponent, origComponent, resourceInstance, user);
836 if (associationInfo.getFromNode() == null || associationInfo.getFromNode().isEmpty()) {
837 associationInfo.setFromNode(resResourceInfo.getUniqueId());
839 associationInfo.setToNode(resResourceInfo.getUniqueId());
842 Either<RequirementCapabilityRelDef, StorageOperationStatus> resultReqCapDef = toscaOperationFacade.associateResourceInstances(containerComponent, containerComponentId, associationInfo);
843 if (resultReqCapDef.isLeft()) {
844 log.debug(ENTITY_ON_GRAPH_IS_CREATED);
845 RequirementCapabilityRelDef resReqCapabilityRelDef = resultReqCapDef.left().value();
846 CreateAndAssotiateInfo resInfo = new CreateAndAssotiateInfo(resResourceInfo, resReqCapabilityRelDef);
847 resultOp = Either.left(resInfo);
851 log.info("Failed to associate node {} with node {}", associationInfo.getFromNode(), associationInfo.getToNode());
852 resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstance(componentsUtils.convertFromStorageResponseForResourceInstance(resultReqCapDef.right().value(), true), "", null));
856 }catch (ComponentException e){
860 unlockComponent(failed, containerComponent);
864 private Component getOriginComponentFromComponentInstance(ComponentInstance componentInstance) {
865 return getOriginComponentFromComponentInstance(componentInstance.getName(), componentInstance.getComponentUid());
868 private Component getInstanceOriginNode(ComponentInstance componentInstance) {
869 return getOriginComponentFromComponentInstance(componentInstance.getName(), componentInstance.getActualComponentUid());
872 private Component getOriginComponentFromComponentInstance(String componentInstanceName, String origComponetId) {
873 Either<Component, StorageOperationStatus> eitherComponent = toscaOperationFacade.getToscaFullElement(origComponetId);
874 if (eitherComponent.isRight()) {
875 log.debug("Failed to get origin component with id {} for component instance {} ", origComponetId, componentInstanceName);
876 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(eitherComponent.right().value(), ComponentTypeEnum.RESOURCE), "", null);
878 return eitherComponent.left().value();
881 private ComponentInstance createComponentInstanceOnGraph(org.openecomp.sdc.be.model.Component containerComponent, Component originComponent, ComponentInstance componentInstance, User user) {
883 Either<ImmutablePair<Component, String>, StorageOperationStatus> result = toscaOperationFacade.addComponentInstanceToTopologyTemplate(containerComponent, originComponent, componentInstance, false, user);
885 if (result.isRight()) {
886 log.debug(FAILED_TO_CREATE_ENTRY_ON_GRAPH_FOR_COMPONENT_INSTANCE, componentInstance.getName());
887 throw new ByResponseFormatComponentException(componentsUtils.getResponseFormatForResourceInstance(componentsUtils.convertFromStorageResponseForResourceInstance(result.right().value(), true), "", null));
890 log.debug(ENTITY_ON_GRAPH_IS_CREATED);
891 Component updatedComponent = result.left().value().getLeft();
892 Map<String, String> existingEnvVersions = new HashMap<>();
893 // TODO existingEnvVersions ??
894 addComponentInstanceArtifacts(updatedComponent, componentInstance, originComponent, user, existingEnvVersions);
896 Optional<ComponentInstance> updatedInstanceOptional = updatedComponent.getComponentInstances().stream().filter(ci -> ci.getUniqueId().equals(result.left().value().getRight())).findFirst();
897 if (!updatedInstanceOptional.isPresent()) {
898 log.debug("Failed to fetch new added component instance {} from component {}", componentInstance.getName(), containerComponent.getName());
899 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER, componentInstance.getName());
901 return updatedInstanceOptional.get();
904 public boolean isCloudSpecificArtifact(String artifact) {
905 if (artifact.contains(CLOUD_SPECIFIC_FIXED_KEY_WORD)) {
906 for (int i = 0; i < CLOUD_SPECIFIC_KEY_WORDS.length; i++) {
907 if (Arrays.stream(CLOUD_SPECIFIC_KEY_WORDS[i]).noneMatch(artifact::contains)) {
918 * addResourceInstanceArtifacts - add artifacts (HEAT_ENV) to resource instance The instance artifacts are generated from the resource's artifacts
919 * @param containerComponent
920 * @param componentInstance
921 * @param originComponent
923 * @param existingEnvVersions
926 protected ActionStatus addComponentInstanceArtifacts(org.openecomp.sdc.be.model.Component containerComponent, ComponentInstance componentInstance, org.openecomp.sdc.be.model.Component originComponent, User user, Map<String, String> existingEnvVersions) {
928 log.debug("add artifacts to resource instance");
929 List<GroupDefinition> filteredGroups = new ArrayList<>();
930 ActionStatus status = setResourceArtifactsOnResourceInstance(componentInstance);
931 if (ActionStatus.OK != status) {
932 throw new ByResponseFormatComponentException(componentsUtils.getResponseFormatForResourceInstance(status, "", null));
934 StorageOperationStatus artStatus;
935 // generate heat_env if necessary
936 Map<String, ArtifactDefinition> componentDeploymentArtifacts = componentInstance.getDeploymentArtifacts();
937 if (MapUtils.isNotEmpty(componentDeploymentArtifacts)) {
939 Map<String, ArtifactDefinition> finalDeploymentArtifacts = new HashMap<>();
940 Map<String, List<ArtifactDefinition>> groupInstancesArtifacts = new HashMap<>();
941 Integer defaultHeatTimeout = ConfigurationManager.getConfigurationManager().getConfiguration()
942 .getHeatArtifactDeploymentTimeout().getDefaultMinutes();
943 List<ArtifactDefinition> listOfCloudSpecificArts = new ArrayList<>();
944 for (ArtifactDefinition artifact : componentDeploymentArtifacts.values()) {
945 String type = artifact.getArtifactType();
946 if (!type.equalsIgnoreCase(ArtifactTypeEnum.HEAT_ENV.getType())) {
947 finalDeploymentArtifacts.put(artifact.getArtifactLabel(), artifact);
949 if (type.equalsIgnoreCase(ArtifactTypeEnum.HEAT.getType()) || type.equalsIgnoreCase(ArtifactTypeEnum.HELM.getType())|| type.equalsIgnoreCase(ArtifactTypeEnum.HEAT_NET.getType()) || type.equalsIgnoreCase(ArtifactTypeEnum.HEAT_VOL.getType()) || type.equalsIgnoreCase(ArtifactTypeEnum.CLOUD_TECHNOLOGY_SPECIFIC_ARTIFACT.getType())) {
950 artifact.setTimeout(defaultHeatTimeout);
954 if (artifact.checkEsIdExist()) {
955 ArtifactDefinition artifactDefinition = artifactBusinessLogic.createHeatEnvPlaceHolder(new ArrayList<>(),
956 artifact, ArtifactsBusinessLogic.HEAT_ENV_NAME, componentInstance.getUniqueId(),
957 NodeTypeEnum.ResourceInstance, componentInstance.getName(), user, containerComponent,
958 existingEnvVersions);
960 finalDeploymentArtifacts.put(artifactDefinition.getArtifactLabel(), artifactDefinition);
962 if (CollectionUtils.isNotEmpty(originComponent.getGroups())) {
963 filteredGroups = originComponent.getGroups().stream().filter(g -> g.getType().equals(VF_MODULE)).collect(Collectors.toList());
965 if (isCloudSpecificArtifact(artifactDefinition.getArtifactName())) {
966 listOfCloudSpecificArts.add(artifact);
968 if (CollectionUtils.isNotEmpty(filteredGroups)) {
969 filteredGroups.stream().filter(g ->
972 .anyMatch(p -> p.equals(artifactDefinition.getGeneratedFromId())))
974 .ifPresent(g -> fillInstanceArtifactMap(groupInstancesArtifacts, artifactDefinition, g));
978 groupInstancesArtifacts.forEach((k,v) -> v.addAll(listOfCloudSpecificArts));
979 filteredGroups.forEach(g -> listOfCloudSpecificArts.forEach(e -> {
980 g.getArtifactsUuid().add(e.getArtifactUUID());
981 g.getArtifacts().add(e.getUniqueId());
983 artStatus = toscaOperationFacade.addDeploymentArtifactsToInstance(containerComponent.getUniqueId(), componentInstance, finalDeploymentArtifacts);
984 if (artStatus != StorageOperationStatus.OK) {
985 log.debug("Failed to add instance deployment artifacts for instance {} in conatiner {} error {}", componentInstance.getUniqueId(), containerComponent.getUniqueId(), artStatus);
986 throw new ByResponseFormatComponentException(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponseForResourceInstance(artStatus, false)));
989 StorageOperationStatus result = toscaOperationFacade.addGroupInstancesToComponentInstance(containerComponent, componentInstance, filteredGroups, groupInstancesArtifacts);
990 if (result != StorageOperationStatus.OK) {
991 log.debug("failed to update group instance for component instance {}", componentInstance.getUniqueId());
992 throw new ByResponseFormatComponentException(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(result)));
994 componentInstance.setDeploymentArtifacts(finalDeploymentArtifacts);
997 artStatus = toscaOperationFacade.addInformationalArtifactsToInstance(containerComponent.getUniqueId(), componentInstance, originComponent.getArtifacts());
998 if (artStatus != StorageOperationStatus.OK) {
999 log.debug("Failed to add informational artifacts to the instance {} belonging to the conatiner {}. Status is {}", componentInstance.getUniqueId(), containerComponent.getUniqueId(), artStatus);
1000 throw new ByResponseFormatComponentException(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponseForResourceInstance(artStatus, false)));
1003 componentInstance.setArtifacts(originComponent.getArtifacts());
1004 return ActionStatus.OK;
1007 private void fillInstanceArtifactMap(Map<String, List<ArtifactDefinition>> groupInstancesArtifacts, ArtifactDefinition artifactDefinition, GroupDefinition groupInstance) {
1008 List<ArtifactDefinition> artifactsUid;
1009 if (groupInstancesArtifacts.containsKey(groupInstance.getUniqueId())) {
1010 artifactsUid = groupInstancesArtifacts.get(groupInstance.getUniqueId());
1012 artifactsUid = new ArrayList<>();
1014 artifactsUid.add(artifactDefinition);
1015 groupInstancesArtifacts.put(groupInstance.getUniqueId(), artifactsUid);
1018 private ActionStatus setResourceArtifactsOnResourceInstance(ComponentInstance resourceInstance) {
1019 Either<Map<String, ArtifactDefinition>, StorageOperationStatus> getResourceDeploymentArtifacts =
1020 artifactBusinessLogic.getArtifacts(resourceInstance.getComponentUid(), NodeTypeEnum.Resource, ArtifactGroupTypeEnum.DEPLOYMENT, null);
1022 Map<String, ArtifactDefinition> deploymentArtifacts = new HashMap<>();
1023 if (getResourceDeploymentArtifacts.isRight()) {
1024 StorageOperationStatus status = getResourceDeploymentArtifacts.right().value();
1025 if (status != StorageOperationStatus.NOT_FOUND) {
1026 log.debug("Failed to fetch resource: {} artifacts. status is {}", resourceInstance.getComponentUid(), status);
1027 return componentsUtils.convertFromStorageResponseForResourceInstance(status, true);
1030 deploymentArtifacts = getResourceDeploymentArtifacts.left().value();
1033 if (!deploymentArtifacts.isEmpty()) {
1034 Map<String, ArtifactDefinition> tempDeploymentArtifacts = new HashMap<>(deploymentArtifacts);
1035 for (Entry<String, ArtifactDefinition> artifact : deploymentArtifacts.entrySet()) {
1036 if (!artifact.getValue().checkEsIdExist()) {
1037 tempDeploymentArtifacts.remove(artifact.getKey());
1041 resourceInstance.setDeploymentArtifacts(tempDeploymentArtifacts);
1044 return ActionStatus.OK;
1047 public Either<ComponentInstance, ResponseFormat> updateComponentInstanceMetadata(String containerComponentParam, String containerComponentId, String componentInstanceId, String userId, ComponentInstance componentInstance) {
1048 return updateComponentInstanceMetadata(containerComponentParam, containerComponentId, componentInstanceId, userId, componentInstance, true);
1051 public Either<ComponentInstance, ResponseFormat> updateComponentInstanceMetadata(
1052 final String containerComponentParam,
1053 final String containerComponentId,
1054 final String componentInstanceId,
1055 final String userId,
1056 ComponentInstance componentInstance,
1059 validateUserExists(userId);
1061 final ComponentTypeEnum containerComponentType = validateComponentType(containerComponentParam);
1063 final Component containerComponent = validateComponentExists(containerComponentId, containerComponentType, null);
1065 validateCanWorkOnComponent(containerComponent, userId);
1066 ComponentTypeEnum instanceType = getComponentType(containerComponentType);
1067 Either<Boolean, StorageOperationStatus> validateParentStatus = toscaOperationFacade.validateComponentExists(componentInstance.getComponentUid());
1068 if (validateParentStatus.isRight()) {
1069 log.debug("Failed to get component instance {} on service {}", componentInstanceId, containerComponentId);
1070 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND, componentInstance.getName(), instanceType.getValue().toLowerCase());
1072 if (!validateParentStatus.left().value()) {
1073 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER, componentInstance.getName(), instanceType.getValue().toLowerCase(), containerComponentType.getValue().toLowerCase(), containerComponentId);
1077 lockComponent(containerComponent, "updateComponentInstance");
1079 Component origComponent;
1080 boolean failed = false;
1082 origComponent = getOriginComponentFromComponentInstance(componentInstance);
1083 componentInstance = updateComponentInstanceMetadata(containerComponent, containerComponentType, origComponent, componentInstanceId, componentInstance);
1084 }catch (ComponentException e) {
1089 unlockComponent(failed, containerComponent);
1092 return Either.left(componentInstance);
1095 // New Multiple Instance Update API
1096 public List<ComponentInstance> updateComponentInstance(String containerComponentParam, Component containerComponent, String containerComponentId, String userId, List<ComponentInstance> componentInstanceList, boolean needLock) {
1098 boolean failed = false;
1100 validateUserExists(userId);
1102 final ComponentTypeEnum containerComponentType = validateComponentType(containerComponentParam);
1103 ComponentParametersView componentFilter = new ComponentParametersView();
1104 componentFilter.disableAll();
1105 componentFilter.setIgnoreUsers(false);
1106 componentFilter.setIgnoreComponentInstances(false);
1107 if (containerComponent == null) {
1108 containerComponent = validateComponentExistsByFilter(containerComponentId, containerComponentType, componentFilter);
1110 validateCanWorkOnComponent(containerComponent, userId);
1112 ComponentTypeEnum instanceType = getComponentType(containerComponentType);
1114 for (ComponentInstance componentInstance : componentInstanceList) {
1115 boolean validateParent = validateParent(containerComponent, componentInstance.getUniqueId());
1116 if (!validateParent) {
1117 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER, componentInstance.getName(),
1118 instanceType.getValue().toLowerCase(), containerComponentType.getValue().toLowerCase(),
1119 containerComponentId);
1124 lockComponent(containerComponent, "updateComponentInstance");
1127 List<ComponentInstance> updatedList = new ArrayList<>();
1128 List<ComponentInstance> instancesFromContainerComponent = containerComponent.getComponentInstances();
1129 List<ComponentInstance> listForUpdate = new ArrayList<>();
1130 if (instancesFromContainerComponent == null || instancesFromContainerComponent.isEmpty())
1131 containerComponent.setComponentInstances(componentInstanceList);
1133 Iterator<ComponentInstance> iterator = instancesFromContainerComponent.iterator();
1134 while (iterator.hasNext()) {
1135 ComponentInstance origInst = iterator.next();
1136 Optional<ComponentInstance> op = componentInstanceList.stream().filter(ci -> ci.getUniqueId().equals(origInst.getUniqueId())).findAny();
1137 if (op.isPresent()) {
1138 ComponentInstance updatedCi = op.get();
1139 updatedCi = buildComponentInstance(updatedCi, origInst);
1141 Boolean isUniqueName = validateInstanceNameUniquenessUponUpdate(containerComponent, origInst, updatedCi.getName());
1142 if (!isUniqueName) {
1143 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to update the name of the component instance {} to {}. A component instance with the same name already exists. ", origInst.getName(), updatedCi.getName());
1144 throw new ByResponseFormatComponentException(componentsUtils.getResponseFormat(ActionStatus.COMPONENT_NAME_ALREADY_EXIST, containerComponentType.getValue(), origInst.getName()));
1146 listForUpdate.add(updatedCi);
1148 listForUpdate.add(origInst);
1150 containerComponent.setComponentInstances(listForUpdate);
1152 Either<Component, StorageOperationStatus> updateStatus = toscaOperationFacade.updateComponentInstanceMetadataOfTopologyTemplate(containerComponent, componentFilter);
1153 if (updateStatus.isRight()) {
1154 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to update metadata belonging to container component {}. Status is {}. ", containerComponent.getName(), updateStatus.right().value());
1155 throw new ByResponseFormatComponentException(componentsUtils.getResponseFormatForResourceInstance(componentsUtils.convertFromStorageResponseForResourceInstance(updateStatus.right().value(), true), "", null));
1158 for (ComponentInstance updatedInstance : updateStatus.left().value().getComponentInstances()) {
1159 Optional<ComponentInstance> op = componentInstanceList.stream().filter(ci -> ci.getName().equals(updatedInstance.getName())).findAny();
1160 if (op.isPresent()) {
1161 updatedList.add(updatedInstance);
1166 }catch (ComponentException e){
1171 unlockComponent(failed, containerComponent);
1176 private boolean validateParent(org.openecomp.sdc.be.model.Component containerComponent, String nodeTemplateId) {
1177 return containerComponent.getComponentInstances().stream().anyMatch(p -> p.getUniqueId().equals(nodeTemplateId));
1180 private ComponentTypeEnum getComponentType(ComponentTypeEnum containerComponentType) {
1181 if (ComponentTypeEnum.PRODUCT == containerComponentType) {
1182 return ComponentTypeEnum.SERVICE_INSTANCE;
1184 return ComponentTypeEnum.RESOURCE_INSTANCE;
1188 private ComponentInstance updateComponentInstanceMetadata(Component containerComponent, ComponentTypeEnum containerComponentType, org.openecomp.sdc.be.model.Component origComponent, String componentInstanceId,
1189 ComponentInstance componentInstance) {
1191 Optional<ComponentInstance> componentInstanceOptional;
1192 Either<ImmutablePair<Component, String>, StorageOperationStatus> updateRes = null;
1193 ComponentInstance oldComponentInstance = null;
1194 boolean isNameChanged = false;
1196 componentInstanceOptional = containerComponent.getComponentInstances().stream().filter(ci -> ci.getUniqueId().equals(componentInstance.getUniqueId())).findFirst();
1197 if (!componentInstanceOptional.isPresent()) {
1198 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to find the component instance {} in container component {}. ", componentInstance.getName(), containerComponent.getName());
1199 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER, componentInstance.getName());
1201 String oldComponentName;
1202 oldComponentInstance = componentInstanceOptional.get();
1203 oldComponentName = oldComponentInstance.getName();
1204 String newInstanceName = componentInstance.getName();
1205 if (oldComponentName != null && !oldComponentInstance.getName().equals(newInstanceName))
1206 isNameChanged = true;
1207 Boolean isUniqueName = validateInstanceNameUniquenessUponUpdate(containerComponent, oldComponentInstance, newInstanceName);
1208 if (!isUniqueName) {
1209 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to update the name of the component instance {} to {}. A component instance with the same name already exists. ", oldComponentInstance.getName(), newInstanceName);
1210 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_NAME_ALREADY_EXIST, containerComponentType.getValue(), componentInstance.getName());
1212 if(!DirectivesUtil.isValid(componentInstance.getDirectives())) {
1213 final String directivesStr =
1214 componentInstance.getDirectives().stream().collect(Collectors.joining(" , ", " [ ", " ] "));
1215 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG,
1216 "Failed to update the directives of the component instance {} to {}. Directives data {} is invalid. ",
1217 oldComponentInstance.getName(), newInstanceName ,
1219 throw new ByActionStatusComponentException(ActionStatus.DIRECTIVES_INVALID_VALUE, containerComponentType.getValue(), componentInstance.getName());
1221 updateRes = toscaOperationFacade.updateComponentInstanceMetadataOfTopologyTemplate(containerComponent, origComponent, updateComponentInstanceMetadata(oldComponentInstance, componentInstance));
1222 if (updateRes.isRight()) {
1223 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to update metadata of component instance {} belonging to container component {}. Status is {}. ", componentInstance.getName(), containerComponent.getName(),
1224 updateRes.right().value());
1225 throw new ByResponseFormatComponentException(componentsUtils.getResponseFormatForResourceInstance(componentsUtils.convertFromStorageResponseForResourceInstance(updateRes.right().value(), true), "", null));
1227 // region - Update instance Groups
1228 if (isNameChanged) {
1229 Either<StorageOperationStatus, StorageOperationStatus> result =
1230 toscaOperationFacade.cleanAndAddGroupInstancesToComponentInstance(containerComponent, oldComponentInstance, componentInstanceId);
1231 if (result.isRight())
1232 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to rename group instances for container {}. error {} ", componentInstanceId, result.right().value());
1234 if (containerComponent instanceof Service){
1235 Either<ComponentInstance, ResponseFormat> renameEither =
1236 renameServiceFilter((Service) containerComponent, newInstanceName,
1237 oldComponentInstance.getName());
1238 if (renameEither.isRight()) {
1239 throw new ByResponseFormatComponentException(renameEither.right().value());
1242 updateForwardingPathDefinition(containerComponent, componentInstance, oldComponentName);
1247 String newInstanceId = updateRes.left().value().getRight();
1248 Optional<ComponentInstance> updatedInstanceOptional = updateRes.left().value().getLeft().getComponentInstances().stream().filter(ci -> ci.getUniqueId().equals(newInstanceId)).findFirst();
1250 if (!updatedInstanceOptional.isPresent()) {
1251 log.debug("Failed to update metadata of component instance {} of container component {}", componentInstance.getName(), containerComponent.getName());
1252 throw new ByResponseFormatComponentException(componentsUtils.getResponseFormat(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER, componentInstance.getName()));
1255 return componentInstanceOptional.get();
1258 private void updateForwardingPathDefinition(Component containerComponent, ComponentInstance componentInstance, String oldComponentName) {
1259 Collection<ForwardingPathDataDefinition> forwardingPathDataDefinitions = getForwardingPathDataDefinitions(containerComponent.getUniqueId());
1260 Set<ForwardingPathDataDefinition> updated = new ForwardingPathUtils()
1261 .updateComponentInstanceName(forwardingPathDataDefinitions, oldComponentName,
1262 componentInstance.getName());
1263 updated.forEach(fp -> {
1264 Either<ForwardingPathDataDefinition, StorageOperationStatus> resultEither = forwardingPathOperation
1265 .updateForwardingPath(containerComponent.getUniqueId(), fp);
1266 if (resultEither.isRight()){
1267 CommonUtility.addRecordToLog(log, LogLevelEnum.ERROR, "Failed to rename forwarding path for container {}. error {} ",containerComponent.getName(), resultEither.right().value());
1273 public Either<ComponentInstance, ResponseFormat> renameServiceFilter(final Service containerComponent,
1274 final String newInstanceName,
1275 final String oldInstanceName) {
1277 Map<String, CINodeFilterDataDefinition> renamedNodesFilter =
1278 ServiceFilterUtils.getRenamedNodesFilter(containerComponent, oldInstanceName, newInstanceName);
1279 for( Entry<String, CINodeFilterDataDefinition> entry : renamedNodesFilter.entrySet()){
1280 Either<CINodeFilterDataDefinition, StorageOperationStatus>
1281 renameEither = nodeFilterOperation.updateNodeFilter(
1282 containerComponent.getUniqueId(),entry.getKey(),entry.getValue());
1283 if (renameEither.isRight()){
1284 return Either.right(componentsUtils.getResponseFormatForResourceInstance(
1285 componentsUtils.convertFromStorageResponse(renameEither.right().value(), ComponentTypeEnum.SERVICE),
1286 containerComponent.getName(), null));
1290 return Either.left(null);
1295 * The normalized old vf name
1296 * @param newNormailzedPrefix-
1297 * The normalized new vf name
1298 * @param qualifiedGroupInstanceName-
1299 * old Group Instance Name
1301 // modify group names
1302 private String getNewGroupName(String oldPrefix, String newNormailzedPrefix, String qualifiedGroupInstanceName) {
1303 if (qualifiedGroupInstanceName == null) {
1304 log.info("CANNOT change group name ");
1307 if (qualifiedGroupInstanceName.startsWith(oldPrefix) || qualifiedGroupInstanceName.startsWith(ValidationUtils.normalizeComponentInstanceName(oldPrefix)))
1308 return qualifiedGroupInstanceName.replaceFirst(oldPrefix, newNormailzedPrefix);
1309 return qualifiedGroupInstanceName;
1312 private ComponentInstance updateComponentInstanceMetadata(ComponentInstance oldComponentInstance, ComponentInstance newComponentInstance) {
1313 oldComponentInstance.setName(newComponentInstance.getName());
1314 oldComponentInstance.setModificationTime(System.currentTimeMillis());
1315 oldComponentInstance.setCustomizationUUID(UUID.randomUUID().toString());
1316 oldComponentInstance.setDirectives(newComponentInstance.getDirectives());
1317 if (oldComponentInstance.getGroupInstances() != null)
1318 oldComponentInstance.getGroupInstances().forEach(group -> group.setName(getNewGroupName(oldComponentInstance.getNormalizedName(), ValidationUtils.normalizeComponentInstanceName(newComponentInstance.getName()), group.getName())));
1319 return oldComponentInstance;
1322 public ComponentInstance deleteComponentInstance(final String containerComponentParam,
1323 final String containerComponentId,
1324 final String componentInstanceId, String userId)
1325 throws BusinessLogicException {
1327 validateUserExists(userId);
1328 final ComponentTypeEnum containerComponentType = validateComponentType(containerComponentParam);
1330 final Component containerComponent = validateComponentExists(containerComponentId, containerComponentType, null);
1331 validateCanWorkOnComponent(containerComponent, userId);
1333 boolean failed = false;
1334 final Optional<ComponentInstance> componentInstanceOptional = containerComponent.getComponentInstanceById(componentInstanceId);
1335 if (!componentInstanceOptional.isPresent()) {
1336 throw new BusinessLogicException(componentsUtils
1337 .getResponseFormat(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND));
1339 ComponentInstance componentInstance = componentInstanceOptional.get();
1342 if (containerComponent instanceof Service || containerComponent instanceof Resource) {
1343 final Either<String, StorageOperationStatus> deleteServiceFilterEither =
1344 nodeFilterOperation.deleteNodeFilter(containerComponent, componentInstanceId);
1345 if (deleteServiceFilterEither.isRight()) {
1346 final ActionStatus status = componentsUtils.convertFromStorageResponse(deleteServiceFilterEither.right().value(),
1347 containerComponentType);
1348 janusGraphDao.rollback();
1349 throw new ByResponseFormatComponentException(componentsUtils.getResponseFormat(status, componentInstanceId));
1351 final Either<ComponentInstance, ResponseFormat> resultOp = deleteNodeFiltersFromComponentInstance(
1352 containerComponent, componentInstance, containerComponentType, userId);
1353 if (resultOp.isRight()) {
1354 janusGraphDao.rollback();
1355 throw new ByResponseFormatComponentException(resultOp.right().value());
1358 lockComponent(containerComponent, "deleteComponentInstance");
1359 final ComponentInstance deletedCompInstance = deleteComponentInstance(containerComponent, componentInstanceId, containerComponentType);
1361 componentInstance = deleteForwardingPathsRelatedTobeDeletedComponentInstance(containerComponentId,
1362 containerComponentType, deletedCompInstance);
1363 final ActionStatus onDeleteOperationsStatus = onChangeInstanceOperationOrchestrator.doOnDeleteInstanceOperations(containerComponent, componentInstanceId);
1364 if (ActionStatus.OK != onDeleteOperationsStatus) {
1365 throw new ByActionStatusComponentException(onDeleteOperationsStatus);
1367 } catch (final ComponentException e) {
1372 unlockComponent(failed, containerComponent);
1374 return componentInstance;
1378 * Try to modify the delete and return two cases
1380 * @param containerComponentParam
1381 * @param containerComponentId
1382 * @param componentInstanceId
1386 public Either<ComponentInstance, ResponseFormat> deleteAbstractComponentInstance(String containerComponentParam, String containerComponentId, String componentInstanceId, String userId) {
1387 log.debug("enter deleteAbstractComponentInstance");
1388 validateUserExists(userId);
1390 final ComponentTypeEnum containerComponentType = validateComponentType(containerComponentParam);
1392 org.openecomp.sdc.be.model.Component containerComponent = validateComponentExists(containerComponentId, containerComponentType, null);
1393 validateCanWorkOnComponent(containerComponent, userId);
1395 boolean failed = false;
1396 ComponentInstance deletedRelatedInst;
1398 if (containerComponent instanceof Service) {
1399 final Optional<ComponentInstance> componentInstanceById = containerComponent.getComponentInstanceById(componentInstanceId);
1400 if (componentInstanceById.isPresent()) {
1401 ComponentInstance componentInstance = componentInstanceById.get();
1402 Either<String, StorageOperationStatus> deleteServiceFilterEither =
1403 nodeFilterOperation.deleteNodeFilter(containerComponent, componentInstanceId);
1404 if (deleteServiceFilterEither.isRight()) {
1405 log.debug("enter deleteAbstractComponentInstance:deleteServiceFilterEither is right, filed");
1406 ActionStatus status = componentsUtils.convertFromStorageResponse(deleteServiceFilterEither.right().value(),
1407 containerComponentType);
1408 janusGraphDao.rollback();
1409 return Either.right(componentsUtils.getResponseFormat(status, componentInstance.getName()));
1411 Either<ComponentInstance, ResponseFormat> resultOp = deleteNodeFiltersFromComponentInstance(containerComponent,
1412 componentInstance, ComponentTypeEnum.SERVICE, userId);
1413 if (resultOp.isRight()) {
1414 log.debug("enter deleteAbstractComponentInstance:resultOp is right, filed");
1415 janusGraphDao.rollback();
1420 log.debug("enter deleteAbstractComponentInstance:");
1421 lockComponent(containerComponent, "deleteComponentInstance");
1422 ComponentInstance deletedCompInstance = deleteComponentInstance(containerComponent, componentInstanceId, containerComponentType);
1424 deletedRelatedInst = deleteForwardingPathsRelatedTobeDeletedComponentInstance(containerComponentId,
1425 containerComponentType, deletedCompInstance);
1426 ActionStatus onDeleteOperationsStatus = onChangeInstanceOperationOrchestrator.doOnDeleteInstanceOperations(containerComponent, componentInstanceId);
1427 log.debug("enter deleteAbstractComponentInstance,get onDeleteOperationsStatus:{}",onDeleteOperationsStatus);
1428 if (ActionStatus.OK != onDeleteOperationsStatus) {
1429 throw new ByActionStatusComponentException(onDeleteOperationsStatus);
1431 } catch (ComponentException e) {
1435 unlockComponent(failed, containerComponent);
1437 log.debug("enter deleteAbstractComponentInstance,deleted RelatedInst success");
1438 return Either.left(deletedRelatedInst);
1441 public Either<ComponentInstance, ResponseFormat> deleteNodeFiltersFromComponentInstance(
1442 final Component component,
1443 final ComponentInstance componentInstance,
1444 final ComponentTypeEnum containerComponentType,
1445 final String userId) {
1447 final Set<String> componentFiltersIDsToBeDeleted =
1448 getComponentFiltersRelatedToComponentInstance(component.getUniqueId(), componentInstance);
1449 if (!componentFiltersIDsToBeDeleted.isEmpty()) {
1450 final Set<String> ids = component.getComponentInstances().stream()
1451 .filter(ci -> componentFiltersIDsToBeDeleted.contains(ci.getName()))
1452 .map(ComponentInstance::getUniqueId).collect(Collectors.toSet());
1453 final Either<Set<String>, StorageOperationStatus> deleteComponentNodeFiltersEither =
1454 nodeFilterOperation.deleteNodeFilters(component, ids);
1455 if (deleteComponentNodeFiltersEither.isRight()) {
1456 final ActionStatus status = componentsUtils
1457 .convertFromStorageResponse(deleteComponentNodeFiltersEither.right().value(),
1458 containerComponentType);
1459 return Either.right(componentsUtils.getResponseFormat(status, componentInstance.getName()));
1461 for (final String id : ids) {
1462 final Optional<ComponentInstance> componentInstanceById = component.getComponentInstanceById(id);
1463 if (!componentInstanceById.isPresent()) {
1464 return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
1466 final ComponentInstance componentInstanceToBeUpdated = componentInstanceById.get();
1468 componentInstanceToBeUpdated.setDirectives(Collections.emptyList());
1469 final Either<ComponentInstance, ResponseFormat> componentInstanceResponseFormatEither =
1470 updateComponentInstanceMetadata(containerComponentType.getValue(), component.getUniqueId(),
1471 componentInstanceToBeUpdated.getUniqueId(), userId, componentInstanceToBeUpdated, false);
1472 if (componentInstanceResponseFormatEither.isRight()) {
1473 return componentInstanceResponseFormatEither;
1477 return Either.left(componentInstance);
1480 private Set<String> getComponentFiltersRelatedToComponentInstance(String containerComponentId,
1481 ComponentInstance componentInstance) {
1482 ComponentParametersView filter = new ComponentParametersView(true);
1483 filter.setIgnoreComponentInstances(false);
1484 Either<Component, StorageOperationStatus> componentFilterOrigin =
1485 toscaOperationFacade.getToscaElement(containerComponentId, filter);
1486 final Component component = componentFilterOrigin.left().value();
1487 return ComponentsUtils.getNodesFiltersToBeDeleted(component, componentInstance);
1490 ComponentInstance deleteForwardingPathsRelatedTobeDeletedComponentInstance(String containerComponentId,
1491 ComponentTypeEnum containerComponentType, ComponentInstance componentInstance) {
1492 if(containerComponentType == ComponentTypeEnum.SERVICE){
1493 List<String> pathIDsToBeDeleted = getForwardingPathsRelatedToComponentInstance(containerComponentId, componentInstance.getName());
1494 if (!pathIDsToBeDeleted.isEmpty()) {
1495 deleteForwardingPaths(containerComponentId, pathIDsToBeDeleted);
1498 return componentInstance;
1501 private void deleteForwardingPaths(String serviceId, List<String> pathIdsToDelete){
1503 Either<Service, StorageOperationStatus> storageStatus = toscaOperationFacade.getToscaElement(serviceId);
1504 if(storageStatus.isRight()) {
1505 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(storageStatus.right().value()));
1507 Either<Set<String>, StorageOperationStatus> result = forwardingPathOperation.deleteForwardingPath(storageStatus.left().value(),
1508 Sets.newHashSet(pathIdsToDelete));
1510 if(result.isRight()) {
1511 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(result.right().value()));
1515 private List<String> getForwardingPathsRelatedToComponentInstance(String containerComponentId, String componentInstanceId){
1516 Collection<ForwardingPathDataDefinition> allPaths = getForwardingPathDataDefinitions(containerComponentId);
1517 List<String> pathIDsToBeDeleted = new ArrayList<>();
1519 allPaths.stream().filter(path -> isPathRelatedToComponent(path,componentInstanceId ))
1520 .forEach(path -> pathIDsToBeDeleted.add(path.getUniqueId()));
1522 return pathIDsToBeDeleted;
1525 private Collection<ForwardingPathDataDefinition> getForwardingPathDataDefinitions(String containerComponentId) {
1526 ComponentParametersView filter = new ComponentParametersView(true);
1527 filter.setIgnoreServicePath(false);
1528 Either<Service, StorageOperationStatus> forwardingPathOrigin = toscaOperationFacade
1529 .getToscaElement(containerComponentId, filter);
1530 return forwardingPathOrigin.left().value().getForwardingPaths().values();
1533 private boolean isPathRelatedToComponent(ForwardingPathDataDefinition pathDataDefinition,
1534 String componentInstanceId){
1535 return pathDataDefinition.getPathElements().getListToscaDataDefinition()
1536 .stream().anyMatch(elementDataDefinition -> elementDataDefinition.getFromNode().equalsIgnoreCase(componentInstanceId) ||
1537 elementDataDefinition.getToNode()
1538 .equalsIgnoreCase(componentInstanceId));
1541 private ComponentInstance deleteComponentInstance(Component containerComponent, String componentInstanceId, ComponentTypeEnum containerComponentType) {
1542 Either<ImmutablePair<Component, String>, StorageOperationStatus> deleteRes = toscaOperationFacade.deleteComponentInstanceFromTopologyTemplate(containerComponent, componentInstanceId);
1543 if (deleteRes.isRight()) {
1544 log.debug("Failed to delete entry on graph for resourceInstance {}", componentInstanceId);
1545 ActionStatus status = componentsUtils.convertFromStorageResponse(deleteRes.right().value(), containerComponentType);
1546 throw new ByActionStatusComponentException(status, componentInstanceId);
1548 log.debug("The component instance {} has been removed from container component {}. ", componentInstanceId, containerComponent);
1549 ComponentInstance deletedInstance = findAndRemoveComponentInstanceFromContainerComponent(componentInstanceId, containerComponent);
1551 if (CollectionUtils.isNotEmpty(containerComponent.getInputs())) {
1552 List<InputDefinition> inputsToDelete = containerComponent.getInputs().stream().filter(i -> i.getInstanceUniqueId() != null && i.getInstanceUniqueId().equals(componentInstanceId)).collect(Collectors.toList());
1553 if (CollectionUtils.isNotEmpty(inputsToDelete)) {
1554 StorageOperationStatus deleteInputsRes = toscaOperationFacade.deleteComponentInstanceInputsFromTopologyTemplate(containerComponent, inputsToDelete);
1555 if (deleteInputsRes != StorageOperationStatus.OK) {
1556 log.debug("Failed to delete inputs of the component instance {} from container component. ", componentInstanceId);
1557 throw new ByActionStatusComponentException(
1558 componentsUtils.convertFromStorageResponse(deleteInputsRes, containerComponentType), componentInstanceId);
1562 return deletedInstance;
1565 private ComponentInstance findAndRemoveComponentInstanceFromContainerComponent(String componentInstanceId, Component containerComponent) {
1566 ComponentInstance foundInstance = null;
1567 for (ComponentInstance instance : containerComponent.getComponentInstances()) {
1568 if (instance.getUniqueId().equals(componentInstanceId)) {
1569 foundInstance = instance;
1570 containerComponent.getComponentInstances().remove(instance);
1574 findAndRemoveComponentInstanceRelations(componentInstanceId, containerComponent);
1575 return foundInstance;
1578 private void findAndRemoveComponentInstanceRelations(String componentInstanceId, Component containerComponent) {
1579 if(CollectionUtils.isNotEmpty(containerComponent.getComponentInstancesRelations())){
1580 containerComponent.setComponentInstancesRelations(containerComponent.getComponentInstancesRelations().stream().filter(r -> isNotBelongingRelation(componentInstanceId, r)).collect(Collectors.toList()));
1584 private boolean isNotBelongingRelation(String componentInstanceId, RequirementCapabilityRelDef relation) {
1585 return !relation.getToNode().equals(componentInstanceId) && !relation.getFromNode().equals(componentInstanceId);
1588 public RequirementCapabilityRelDef associateRIToRI(String componentId, String userId, RequirementCapabilityRelDef requirementDef, ComponentTypeEnum componentTypeEnum) {
1589 return associateRIToRI(componentId, userId, requirementDef, componentTypeEnum, true);
1592 public RequirementCapabilityRelDef associateRIToRI(String componentId, String userId, RequirementCapabilityRelDef requirementDef,
1593 ComponentTypeEnum componentTypeEnum, boolean needLock) {
1595 validateUserExists(userId);
1597 RequirementCapabilityRelDef requirementCapabilityRelDef;
1599 org.openecomp.sdc.be.model.Component containerComponent = validateComponentExists(componentId, componentTypeEnum, null);
1601 validateCanWorkOnComponent(containerComponent, userId);
1602 boolean failed = false;
1605 lockComponent(containerComponent, ASSOCIATE_RI_TO_RI);
1607 requirementCapabilityRelDef = associateRIToRIOnGraph(containerComponent, requirementDef);
1608 }catch (ComponentException e){
1613 unlockComponent(failed, containerComponent);
1615 return requirementCapabilityRelDef;
1618 public RequirementCapabilityRelDef associateRIToRIOnGraph(Component containerComponent, RequirementCapabilityRelDef requirementDef) {
1620 log.debug(TRY_TO_CREATE_ENTRY_ON_GRAPH);
1622 Either<RequirementCapabilityRelDef, StorageOperationStatus> result = toscaOperationFacade.associateResourceInstances(null, containerComponent.getUniqueId(), requirementDef);
1624 if (result.isLeft()) {
1625 log.debug(ENTITY_ON_GRAPH_IS_CREATED);
1626 return result.left().value();
1628 log.debug("Failed to associate node: {} with node {}", requirementDef.getFromNode(), requirementDef.getToNode());
1629 String fromNameOrId = "";
1630 String toNameOrId = "";
1631 Either<ComponentInstance, StorageOperationStatus> fromResult = getResourceInstanceById(containerComponent, requirementDef.getFromNode());
1632 Either<ComponentInstance, StorageOperationStatus> toResult = getResourceInstanceById(containerComponent, requirementDef.getToNode());
1634 toNameOrId = requirementDef.getFromNode();
1635 fromNameOrId = requirementDef.getFromNode();
1636 if (fromResult.isLeft()) {
1637 fromNameOrId = fromResult.left().value().getName();
1639 if (toResult.isLeft()) {
1640 toNameOrId = toResult.left().value().getName();
1643 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponseForResourceInstance
1644 (result.right().value(), true), fromNameOrId, toNameOrId,
1645 requirementDef.getRelationships().get(0).getRelation().getRequirement());
1651 * @param componentId
1653 * @param requirementDefList
1654 * @param componentTypeEnum
1657 public List<RequirementCapabilityRelDef> batchDissociateRIFromRI(
1660 List<RequirementCapabilityRelDef> requirementDefList,
1661 ComponentTypeEnum componentTypeEnum) {
1663 validateUserExists(userId);
1664 org.openecomp.sdc.be.model.Component containerComponent = validateComponentExists(componentId, componentTypeEnum, null);
1665 validateCanWorkOnComponent(containerComponent, userId);
1666 boolean failed = false;
1667 List<RequirementCapabilityRelDef> delOkResult = new ArrayList<>();
1669 lockComponent(containerComponent, ASSOCIATE_RI_TO_RI);
1670 for (RequirementCapabilityRelDef requirementDef : requirementDefList) {
1671 RequirementCapabilityRelDef requirementCapabilityRelDef = dissociateRIFromRI(
1672 componentId, userId, requirementDef, containerComponent.getComponentType());
1673 delOkResult.add(requirementCapabilityRelDef);
1675 }catch (ComponentException e){
1679 unlockComponent(failed, containerComponent);
1684 public RequirementCapabilityRelDef dissociateRIFromRI(String componentId, String userId, RequirementCapabilityRelDef requirementDef, ComponentTypeEnum componentTypeEnum) {
1685 validateUserExists(userId);
1687 org.openecomp.sdc.be.model.Component containerComponent = validateComponentExists(componentId, componentTypeEnum, null);
1689 validateCanWorkOnComponent(containerComponent, userId);
1690 boolean failed = false;
1692 lockComponent(containerComponent, ASSOCIATE_RI_TO_RI);
1693 log.debug(TRY_TO_CREATE_ENTRY_ON_GRAPH);
1694 Either<RequirementCapabilityRelDef, StorageOperationStatus> result = toscaOperationFacade.dissociateResourceInstances(componentId, requirementDef);
1695 if (result.isLeft()) {
1696 log.debug(ENTITY_ON_GRAPH_IS_CREATED);
1697 return result.left().value();
1700 log.debug("Failed to dissocaite node {} from node {}", requirementDef.getFromNode(), requirementDef.getToNode());
1701 String fromNameOrId = "";
1702 String toNameOrId = "";
1703 Either<ComponentInstance, StorageOperationStatus> fromResult = getResourceInstanceById(containerComponent, requirementDef.getFromNode());
1704 Either<ComponentInstance, StorageOperationStatus> toResult = getResourceInstanceById(containerComponent, requirementDef.getToNode());
1706 toNameOrId = requirementDef.getFromNode();
1707 fromNameOrId = requirementDef.getFromNode();
1708 if (fromResult.isLeft()) {
1709 fromNameOrId = fromResult.left().value().getName();
1711 if (toResult.isLeft()) {
1712 toNameOrId = toResult.left().value().getName();
1715 throw new ByActionStatusComponentException(
1716 componentsUtils.convertFromStorageResponseForResourceInstance(result.right().value(), true),
1717 fromNameOrId, toNameOrId, requirementDef.getRelationships().get(0).getRelation().getRequirement());
1719 }catch (ComponentException e){
1723 unlockComponent(failed, containerComponent);
1727 * Allows to get relation contained in specified component according to received Id
1728 * @param componentId
1731 * @param componentTypeEnum
1734 public Either<RequirementCapabilityRelDef, ResponseFormat> getRelationById(String componentId, String relationId, String userId, ComponentTypeEnum componentTypeEnum) {
1736 Either<RequirementCapabilityRelDef, ResponseFormat> resultOp = null;
1738 org.openecomp.sdc.be.model.Component containerComponent = null;
1739 RequirementCapabilityRelDef foundRelation = null;
1741 validateUserExists(userId);
1742 containerComponent = validateComponentExists(componentId, componentTypeEnum, null);
1743 List<RequirementCapabilityRelDef> requirementCapabilityRelations = containerComponent.getComponentInstancesRelations();
1744 foundRelation = findRelation(relationId, requirementCapabilityRelations);
1745 if(foundRelation == null){
1746 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.RELATION_NOT_FOUND, relationId, componentId);
1747 log.debug("Relation with id {} was not found on the component", relationId, componentId);
1748 resultOp = Either.right(responseFormat);
1750 if(resultOp == null){
1751 resultOp = setRelatedCapability(foundRelation, containerComponent);
1753 if(resultOp.isLeft()){
1754 resultOp = setRelatedRequirement(foundRelation, containerComponent);
1756 } catch (Exception e) {
1757 log.error("The exception {} occured upon get relation {} of the component {} ", e, relationId, componentId);
1758 resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
1763 private RequirementCapabilityRelDef findRelation(String relationId, List<RequirementCapabilityRelDef> requirementCapabilityRelations) {
1764 for(RequirementCapabilityRelDef relationship : requirementCapabilityRelations){
1765 if (relationship.getRelationships().stream().anyMatch(r -> r.getRelation().getId().equals(relationId))) {
1766 return relationship;
1772 private Either<RequirementCapabilityRelDef, ResponseFormat> setRelatedRequirement(RequirementCapabilityRelDef foundRelation, Component containerComponent) {
1773 Either<RequirementCapabilityRelDef, ResponseFormat> result = null;
1774 RelationshipInfo relationshipInfo = foundRelation.resolveSingleRelationship().getRelation();
1775 String instanceId = foundRelation.getFromNode();
1776 Optional<RequirementDefinition> foundRequirement;
1777 Optional<ComponentInstance> instance = containerComponent.getComponentInstances().stream()
1778 .filter(i -> i.getUniqueId().equals(instanceId)).findFirst();
1779 if (!instance.isPresent()) {
1780 ResponseFormat responseFormat = componentsUtils
1781 .getResponseFormat(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER, instanceId, "instance",
1782 containerComponent.getComponentType().getValue(), containerComponent.getName());
1783 log.debug("Component instance with id {} was not found on the component", instanceId, containerComponent.getUniqueId());
1784 result = Either.right(responseFormat);
1786 if (result == null && instance.isPresent()) {
1787 for (List<RequirementDefinition> requirements : instance.get().getRequirements().values()) {
1788 foundRequirement = requirements.stream()
1789 .filter(r -> isBelongingCalcRequirement(relationshipInfo, r, containerComponent.getLifecycleState())).findFirst();
1790 if (foundRequirement.isPresent()) {
1791 foundRelation.resolveSingleRelationship().setRequirement(foundRequirement.get());
1792 result = Either.left(foundRelation);
1796 if (result == null) {
1797 Either<RequirementDataDefinition, StorageOperationStatus> getfulfilledRequirementRes = toscaOperationFacade
1798 .getFulfilledRequirementByRelation(containerComponent.getUniqueId(), instanceId, foundRelation,
1799 this::isBelongingFullRequirement);
1800 if (getfulfilledRequirementRes.isRight()) {
1801 ResponseFormat responseFormat = componentsUtils
1802 .getResponseFormat(ActionStatus.REQUIREMENT_OF_INSTANCE_NOT_FOUND_ON_CONTAINER, relationshipInfo.getRequirement(),
1803 instanceId, containerComponent.getUniqueId());
1804 log.debug("Requirement {} of instance {} was not found on the container {}. ", relationshipInfo.getCapability(), instanceId,
1805 containerComponent.getUniqueId());
1806 result = Either.right(responseFormat);
1808 foundRelation.resolveSingleRelationship().setRequirement(getfulfilledRequirementRes.left().value());
1811 if (result == null) {
1812 result = Either.left(foundRelation);
1817 private boolean isBelongingFullRequirement(RelationshipInfo relationshipInfo, RequirementDataDefinition req) {
1818 return req.getName().equals(relationshipInfo.getRequirement()) &&
1819 req.getUniqueId().equals(relationshipInfo.getRequirementUid()) &&
1820 req.getOwnerId().equals(relationshipInfo.getRequirementOwnerId());
1823 private boolean isBelongingCalcRequirement(RelationshipInfo relationshipInfo, RequirementDataDefinition req, LifecycleStateEnum state) {
1824 return nameMatches(relationshipInfo.getRequirement(), req.getName(), req.getPreviousName(), state) &&
1825 req.getUniqueId().equals(relationshipInfo.getRequirementUid()) &&
1826 req.getOwnerId().equals(relationshipInfo.getRequirementOwnerId());
1829 private Either<RequirementCapabilityRelDef, ResponseFormat> setRelatedCapability(RequirementCapabilityRelDef foundRelation, Component containerComponent) {
1830 Either<RequirementCapabilityRelDef, ResponseFormat> result = null;
1831 RelationshipInfo relationshipInfo = foundRelation.resolveSingleRelationship().getRelation();
1832 String instanceId = foundRelation.getToNode();
1833 Optional<CapabilityDefinition> foundCapability;
1834 Optional<ComponentInstance> instance = containerComponent.getComponentInstances().stream()
1835 .filter(i -> i.getUniqueId().equals(instanceId)).findFirst();
1836 if (!instance.isPresent()) {
1837 ResponseFormat responseFormat = componentsUtils
1838 .getResponseFormat(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER, instanceId, "instance",
1839 containerComponent.getComponentType().getValue(), containerComponent.getName());
1840 log.debug("Component instance with id {} was not found on the component", instanceId, containerComponent.getUniqueId());
1841 result = Either.right(responseFormat);
1843 if (result == null && instance.isPresent()) {
1844 for (List<CapabilityDefinition> capabilities : instance.get().getCapabilities().values()) {
1845 foundCapability = capabilities.stream()
1846 .filter(c -> isBelongingCalcCapability(relationshipInfo, c, containerComponent.getLifecycleState())).findFirst();
1847 if (foundCapability.isPresent()) {
1848 foundRelation.resolveSingleRelationship().setCapability(foundCapability.get());
1849 result = Either.left(foundRelation);
1853 if (result == null) {
1854 Either<CapabilityDataDefinition, StorageOperationStatus> getfulfilledRequirementRes =
1855 toscaOperationFacade.getFulfilledCapabilityByRelation(containerComponent.getUniqueId(), instanceId, foundRelation,
1856 this::isBelongingFullCapability);
1857 if (getfulfilledRequirementRes.isRight()) {
1858 ResponseFormat responseFormat = componentsUtils
1859 .getResponseFormat(ActionStatus.CAPABILITY_OF_INSTANCE_NOT_FOUND_ON_CONTAINER, relationshipInfo.getCapability(),
1860 instanceId, containerComponent.getUniqueId());
1861 log.debug("Capability {} of instance {} was not found on the container {}. ", relationshipInfo.getCapability(), instanceId,
1862 containerComponent.getUniqueId());
1863 result = Either.right(responseFormat);
1865 foundRelation.resolveSingleRelationship().setCapability(getfulfilledRequirementRes.left().value());
1869 result = Either.left(foundRelation);
1874 private boolean isBelongingFullCapability(RelationshipInfo relationshipInfo, CapabilityDataDefinition cap) {
1875 return cap.getName().equals(relationshipInfo.getCapability()) &&
1876 cap.getUniqueId().equals(relationshipInfo.getCapabilityUid()) &&
1877 cap.getOwnerId().equals(relationshipInfo.getCapabilityOwnerId());
1880 private boolean isBelongingCalcCapability(RelationshipInfo relationshipInfo, CapabilityDataDefinition cap, LifecycleStateEnum state) {
1881 return nameMatches(relationshipInfo.getCapability(), cap.getName(), cap.getPreviousName(), state) &&
1882 cap.getUniqueId().equals(relationshipInfo.getCapabilityUid()) &&
1883 cap.getOwnerId().equals(relationshipInfo.getCapabilityOwnerId());
1886 private boolean nameMatches(String nameFromRelationship, String currName, String previousName, LifecycleStateEnum state) {
1887 return state == LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT ?
1888 currName.equals(nameFromRelationship):
1889 previousName!= null && previousName.equals(nameFromRelationship);
1892 private Either<ComponentInstanceProperty, ResponseFormat> updateAttributeValue(ComponentInstanceProperty attribute, String resourceInstanceId) {
1893 Either<ComponentInstanceProperty, StorageOperationStatus> eitherAttribute = componentInstanceOperation.updateAttributeValueInResourceInstance(attribute, resourceInstanceId, true);
1894 Either<ComponentInstanceProperty, ResponseFormat> result;
1895 if (eitherAttribute.isLeft()) {
1896 log.debug("Attribute value {} was updated on graph.", attribute.getValueUniqueUid());
1897 ComponentInstanceProperty instanceAttribute = eitherAttribute.left().value();
1899 result = Either.left(instanceAttribute);
1902 log.debug("Failed to update attribute value {} in resource instance {}", attribute, resourceInstanceId);
1904 ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(eitherAttribute.right().value());
1906 result = Either.right(componentsUtils.getResponseFormat(actionStatus, ""));
1912 private Either<ComponentInstanceProperty, ResponseFormat> createAttributeValue(ComponentInstanceProperty attribute, String resourceInstanceId) {
1914 Either<ComponentInstanceProperty, ResponseFormat> result;
1916 Wrapper<Integer> indexCounterWrapper = new Wrapper<>();
1917 Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
1918 validateIncrementCounter(resourceInstanceId, GraphPropertiesDictionary.ATTRIBUTE_COUNTER, indexCounterWrapper, errorWrapper);
1920 if (!errorWrapper.isEmpty()) {
1921 result = Either.right(errorWrapper.getInnerElement());
1923 Either<ComponentInstanceProperty, StorageOperationStatus> eitherAttribute = componentInstanceOperation.addAttributeValueToResourceInstance(attribute, resourceInstanceId, indexCounterWrapper.getInnerElement(), true);
1924 if (eitherAttribute.isLeft()) {
1925 log.debug("Attribute value was added to resource instance {}", resourceInstanceId);
1926 ComponentInstanceProperty instanceAttribute = eitherAttribute.left().value();
1927 result = Either.left(instanceAttribute);
1930 log.debug("Failed to add attribute value {} to resource instance {}", attribute, resourceInstanceId);
1932 ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(eitherAttribute.right().value());
1933 result = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
1941 * Create Or Updates Attribute Instance
1943 * @param componentTypeEnum
1944 * @param componentId
1945 * @param resourceInstanceId
1950 public Either<ComponentInstanceProperty, ResponseFormat> createOrUpdateAttributeValue(ComponentTypeEnum componentTypeEnum, String componentId, String resourceInstanceId, ComponentInstanceProperty attribute, String userId) {
1951 Either<ComponentInstanceProperty, ResponseFormat> result = null;
1952 Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
1954 validateUserExists(userId);
1955 if (errorWrapper.isEmpty()) {
1956 validateComponentTypeEnum(componentTypeEnum, "CreateOrUpdateAttributeValue", errorWrapper);
1958 if (errorWrapper.isEmpty()) {
1959 validateCanWorkOnComponent(componentId, componentTypeEnum, userId, errorWrapper);
1961 if (errorWrapper.isEmpty()) {
1962 validateComponentLock(componentId, componentTypeEnum, errorWrapper);
1966 if (errorWrapper.isEmpty()) {
1967 final boolean isCreate = Objects.isNull(attribute.getValueUniqueUid());
1969 result = createAttributeValue(attribute, resourceInstanceId);
1971 result = updateAttributeValue(attribute, resourceInstanceId);
1974 result = Either.right(errorWrapper.getInnerElement());
1980 if (result == null || result.isRight()) {
1981 janusGraphDao.rollback();
1983 janusGraphDao.commit();
1986 graphLockOperation.unlockComponent(componentId, componentTypeEnum.getNodeType());
1990 public Either<List<ComponentInstanceProperty>, ResponseFormat> createOrUpdatePropertiesValues(ComponentTypeEnum componentTypeEnum, String componentId, String resourceInstanceId, List<ComponentInstanceProperty> properties, String userId) {
1992 Either<List<ComponentInstanceProperty>, ResponseFormat> resultOp = null;
1994 /*-------------------------------Validations---------------------------------*/
1995 validateUserExists(userId);
1997 if (componentTypeEnum == null) {
1998 BeEcompErrorManager.getInstance().logInvalidInputError("CreateOrUpdatePropertiesValues", INVALID_COMPONENT_TYPE, ErrorSeverity.INFO);
1999 resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.NOT_ALLOWED));
2002 Either<Component, StorageOperationStatus> getResourceResult = toscaOperationFacade.getToscaElement(componentId, JsonParseFlagEnum.ParseAll);
2004 if (getResourceResult.isRight()) {
2005 log.debug(FAILED_TO_RETRIEVE_COMPONENT_COMPONENT_ID, componentId);
2006 ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(getResourceResult.right().value(), componentTypeEnum);
2007 return Either.right(componentsUtils.getResponseFormat(actionStatus, componentId));
2009 Component containerComponent = getResourceResult.left().value();
2011 if (!ComponentValidationUtils.canWorkOnComponent(containerComponent, userId)) {
2012 if (containerComponent.isArchived()) {
2013 log.info("Component is archived. Component id: {}", componentId);
2014 return Either.right(componentsUtils.getResponseFormat(ActionStatus.COMPONENT_IS_ARCHIVED, containerComponent.getName()));
2016 log.info("Restricted operation for user: {} on service {}", userId, componentId);
2017 return Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
2020 Either<ComponentInstance, StorageOperationStatus> resourceInstanceStatus = getResourceInstanceById(containerComponent, resourceInstanceId);
2021 if (resourceInstanceStatus.isRight()) {
2022 return Either.right(componentsUtils.getResponseFormat(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER,
2023 resourceInstanceId, "resource instance", "service", componentId));
2025 ComponentInstance foundResourceInstance = resourceInstanceStatus.left().value();
2028 StorageOperationStatus lockStatus = graphLockOperation.lockComponent(componentId, componentTypeEnum.getNodeType());
2029 if (lockStatus != StorageOperationStatus.OK) {
2030 log.debug(FAILED_TO_LOCK_SERVICE, componentId);
2031 return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(lockStatus)));
2033 List <ComponentInstanceProperty> updatedProperties = new ArrayList<>();
2035 for (ComponentInstanceProperty property: properties) {
2036 validateMandatoryFields(property);
2037 ComponentInstanceProperty componentInstanceProperty = validatePropertyExistsOnComponent(property, containerComponent, foundResourceInstance);
2038 String propertyParentUniqueId = property.getParentUniqueId();
2039 Either<String, ResponseFormat> updatedPropertyValue = updatePropertyObjectValue(property, false);
2040 if (updatedPropertyValue.isRight()) {
2041 log.error("Failed to update property object value of property: {}",
2043 throw new ByResponseFormatComponentException(updatedPropertyValue.right().value());
2045 Optional<CapabilityDefinition>
2046 capPropDefinition = getPropertyCapabilityOfChildInstance(propertyParentUniqueId, foundResourceInstance.getCapabilities());
2047 if (capPropDefinition.isPresent()) {
2048 updatedPropertyValue
2049 .bimap(updatedValue -> updateCapabilityPropFromUpdateInstProp(property, updatedValue,
2050 containerComponent, foundResourceInstance, capPropDefinition.get().getType(),
2051 capPropDefinition.get().getName()), Either::right);
2053 updatedPropertyValue.bimap(
2055 componentInstanceProperty.setValue(updatedValue);
2056 return updatePropertyOnContainerComponent(property, updatedValue,
2057 containerComponent, foundResourceInstance);
2059 updatedProperties.add(componentInstanceProperty);
2063 Either<Component, StorageOperationStatus> updateContainerRes = toscaOperationFacade.updateComponentInstanceMetadataOfTopologyTemplate(containerComponent);
2064 if (updateContainerRes.isRight()) {
2065 ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(updateContainerRes.right().value());
2066 resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
2069 resultOp = Either.left(updatedProperties);
2073 if (resultOp == null || resultOp.isRight()) {
2074 janusGraphDao.rollback();
2076 janusGraphDao.commit();
2079 graphLockOperation.unlockComponent(componentId, componentTypeEnum.getNodeType());
2083 private void validateMandatoryFields(PropertyDataDefinition property) {
2084 if (StringUtils.isEmpty(property.getName())) {
2085 throw new ByActionStatusComponentException (ActionStatus.MISSING_PROPERTY_NAME);
2089 private ComponentInstanceProperty validatePropertyExistsOnComponent(ComponentInstanceProperty property, Component containerComponent, ComponentInstance foundResourceInstance) {
2090 List<ComponentInstanceProperty> instanceProperties = containerComponent.getComponentInstancesProperties().get(foundResourceInstance.getUniqueId());
2091 Optional<ComponentInstanceProperty> instanceProperty = instanceProperties.stream().filter(p -> p.getName().equals(property.getName())).findAny();
2092 if (!instanceProperty.isPresent()) {
2093 throw new ByActionStatusComponentException(ActionStatus.PROPERTY_NOT_FOUND, property.getName());
2095 return instanceProperty.get();
2098 private ResponseFormat updateCapabilityPropertyOnContainerComponent(ComponentInstanceProperty property,
2099 String newValue, Component containerComponent, ComponentInstance foundResourceInstance,
2100 String capabilityType, String capabilityName) {
2101 String componentInstanceUniqueId = foundResourceInstance.getUniqueId();
2102 ResponseFormat actionStatus = updateCapPropOnContainerComponent(property, newValue, containerComponent,
2103 foundResourceInstance, capabilityType, capabilityName, componentInstanceUniqueId);
2104 if (actionStatus != null) {
2105 return actionStatus;
2108 return componentsUtils.getResponseFormat(ActionStatus.OK);
2111 private ResponseFormat updateCapabilityPropFromUpdateInstProp(ComponentInstanceProperty property,
2112 String newValue, Component containerComponent,
2113 ComponentInstance foundResourceInstance,
2114 String capabilityType, String capabilityName) {
2115 String componentInstanceUniqueId = foundResourceInstance.getUniqueId();
2116 Either<Component, StorageOperationStatus> getComponentRes =
2117 toscaOperationFacade.getToscaFullElement(foundResourceInstance.getComponentUid());
2118 if(getComponentRes.isRight()) {
2119 return componentsUtils.getResponseFormat(getComponentRes.right().value());
2122 ResponseFormat actionStatus = updateCapPropOnContainerComponent(property, newValue, containerComponent,
2123 foundResourceInstance, capabilityType, capabilityName, componentInstanceUniqueId);
2124 if (actionStatus != null) {
2125 return actionStatus;
2128 return componentsUtils.getResponseFormat(ActionStatus.OK);
2131 private ResponseFormat updateCapPropOnContainerComponent(ComponentInstanceProperty property, String newValue,
2132 Component containerComponent,
2133 ComponentInstance foundResourceInstance,
2134 String capabilityType, String capabilityName,
2135 String componentInstanceUniqueId) {
2136 Map<String, List<CapabilityDefinition>> capabilities =
2137 Optional.ofNullable(foundResourceInstance.getCapabilities()).orElse(Collections.emptyMap());
2138 List<CapabilityDefinition> capPerType =
2139 Optional.ofNullable(capabilities.get(capabilityType)).orElse(Collections.emptyList());
2140 Optional<CapabilityDefinition> cap =
2141 capPerType.stream().filter(c -> c.getName().equals(capabilityName)).findAny();
2142 if (cap.isPresent()) {
2143 List<ComponentInstanceProperty> capProperties = cap.get().getProperties();
2144 if (capProperties != null) {
2145 Optional<ComponentInstanceProperty> instanceProperty =
2146 capProperties.stream().filter(p -> p.getUniqueId().equals(property.getUniqueId())).findAny();
2147 StorageOperationStatus status;
2148 if (instanceProperty.isPresent()) {
2149 String capKey = ModelConverter.buildCapabilityPropertyKey(foundResourceInstance.getOriginType().isAtomicType(), capabilityType, capabilityName, componentInstanceUniqueId, cap.get());
2150 instanceProperty.get().setValue(newValue);
2151 List<String> path = new ArrayList<>();
2152 path.add(componentInstanceUniqueId);
2154 instanceProperty.get().setPath(path);
2155 status = toscaOperationFacade.updateComponentInstanceCapabiltyProperty(containerComponent,
2156 componentInstanceUniqueId, capKey, instanceProperty.get());
2157 if (status != StorageOperationStatus.OK) {
2158 ActionStatus actionStatus =
2159 componentsUtils.convertFromStorageResponseForResourceInstanceProperty(status);
2160 return componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, "");
2163 foundResourceInstance.setCustomizationUUID(UUID.randomUUID().toString());
2170 private ResponseFormat updatePropertyOnContainerComponent(ComponentInstanceProperty instanceProperty, String newValue, Component containerComponent, ComponentInstance foundResourceInstance) {
2171 StorageOperationStatus status;
2172 instanceProperty.setValue(newValue);
2173 status = toscaOperationFacade.updateComponentInstanceProperty(containerComponent, foundResourceInstance.getUniqueId(), instanceProperty);
2174 if (status != StorageOperationStatus.OK) {
2175 ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(status);
2176 return componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, "");
2178 foundResourceInstance.setCustomizationUUID(UUID.randomUUID().toString());
2179 return componentsUtils.getResponseFormat(ActionStatus.OK);
2182 private <T extends PropertyDefinition> Either<String,ResponseFormat> validatePropertyObjectValue(T property, String newValue, boolean isInput) {
2183 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> allDataTypesEither = dataTypeCache.getAll();
2184 if (allDataTypesEither.isRight()) {
2185 JanusGraphOperationStatus status = allDataTypesEither.right().value();
2186 BeEcompErrorManager.getInstance().logInternalFlowError("UpdatePropertyValueOnComponentInstance", "Failed to update property value on instance. Status is " + status, ErrorSeverity.ERROR);
2187 return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status))));
2189 Map<String, DataTypeDefinition> allDataTypes = allDataTypesEither.left().value();
2190 String propertyType = property.getType();
2191 String innerType = getInnerType(property);
2193 // Specific Update Logic
2194 Either<Object, Boolean> isValid = propertyOperation.validateAndUpdatePropertyValue(property.getType(), newValue, true, innerType, allDataTypes);
2195 if (isValid.isRight()) {
2196 Boolean res = isValid.right().value();
2198 log.error("Invalid value {} of property {} ", newValue, property.getName());
2199 return Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT));
2202 Object object = isValid.left().value();
2203 if (object != null) {
2204 newValue = object.toString();
2207 if (validateAndUpdateRules(property, isInput, allDataTypes, innerType, propertyType))
2208 return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(JanusGraphOperationStatus.ILLEGAL_ARGUMENT))));
2209 return Either.left(newValue);
2212 private <T extends PropertyDefinition> boolean validateAndUpdateRules(T property, boolean isInput, Map<String, DataTypeDefinition> allDataTypes, String innerType, String propertyType) {
2214 ImmutablePair<String, Boolean> pair = propertyOperation.validateAndUpdateRules(propertyType, ((ComponentInstanceProperty) property).getRules(), innerType, allDataTypes, true);
2215 if (pair.getRight() != null && !pair.getRight()) {
2216 BeEcompErrorManager.getInstance().logBeInvalidValueError("Add property value", pair.getLeft(), property.getName(), propertyType);
2223 private <T extends PropertyDefinition> Either<String,ResponseFormat> updatePropertyObjectValue(T property, boolean isInput) {
2224 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> allDataTypesEither = dataTypeCache.getAll();
2225 if (allDataTypesEither.isRight()) {
2226 JanusGraphOperationStatus status = allDataTypesEither.right().value();
2227 BeEcompErrorManager.getInstance().logInternalFlowError("UpdatePropertyValueOnComponentInstance", "Failed to update property value on instance. Status is " + status, ErrorSeverity.ERROR);
2228 return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status))));
2230 Map<String, DataTypeDefinition> allDataTypes = allDataTypesEither.left().value();
2231 String innerType = null;
2232 String propertyType = property.getType();
2233 ToscaPropertyType type = ToscaPropertyType.isValidType(propertyType);
2234 log.debug("The type of the property {} is {}", property.getUniqueId(), propertyType);
2236 if (type == ToscaPropertyType.LIST || type == ToscaPropertyType.MAP) {
2237 SchemaDefinition def = property.getSchema();
2239 log.debug("Schema doesn't exists for property of type {}", type);
2240 return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(StorageOperationStatus.INVALID_VALUE)));
2242 PropertyDataDefinition propDef = def.getProperty();
2243 if (propDef == null) {
2244 log.debug("Property in Schema Definition inside property of type {} doesn't exist", type);
2245 return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(StorageOperationStatus.INVALID_VALUE)));
2247 innerType = propDef.getType();
2249 // Specific Update Logic
2250 Either<Object, Boolean> isValid = propertyOperation.validateAndUpdatePropertyValue(propertyType, property.getValue(), true, innerType, allDataTypes);
2251 String newValue = property.getValue();
2252 if (isValid.isRight()) {
2253 Boolean res = isValid.right().value();
2255 log.debug("validate and update property value has failed with value: {}", property.getValue());
2256 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(JanusGraphOperationStatus.ILLEGAL_ARGUMENT)));
2259 Object object = isValid.left().value();
2260 if (object != null) {
2261 newValue = object.toString();
2265 ImmutablePair<String, Boolean> pair = propertyOperation.validateAndUpdateRules(propertyType, ((ComponentInstanceProperty) property).getRules(), innerType, allDataTypes, true);
2266 if (pair.getRight() != null && pair.getRight() == false) {
2267 BeEcompErrorManager.getInstance().logBeInvalidValueError("Add property value", pair.getLeft(), property.getName(), propertyType);
2268 return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(JanusGraphOperationStatus.ILLEGAL_ARGUMENT))));
2271 return Either.left(newValue);
2274 private ResponseFormat updateInputOnContainerComponent(ComponentInstanceInput input, String newValue, Component containerComponent, ComponentInstance foundResourceInstance) {
2275 StorageOperationStatus status;
2276 input.setValue(newValue);
2277 status = toscaOperationFacade.updateComponentInstanceInput(containerComponent, foundResourceInstance.getUniqueId(), input);
2278 if (status != StorageOperationStatus.OK) {
2279 ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(status);
2280 return componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, "");
2282 foundResourceInstance.setCustomizationUUID(UUID.randomUUID().toString());
2283 return componentsUtils.getResponseFormat(ActionStatus.OK);
2286 public Either<List<ComponentInstanceInput>, ResponseFormat> createOrUpdateInstanceInputValues(ComponentTypeEnum componentTypeEnum, String componentId, String resourceInstanceId, List<ComponentInstanceInput> inputs, String userId) {
2288 Either<List<ComponentInstanceInput>, ResponseFormat> resultOp = null;
2290 validateUserExists(userId);
2292 if (componentTypeEnum == null) {
2293 BeEcompErrorManager.getInstance().logInvalidInputError(CREATE_OR_UPDATE_PROPERTY_VALUE, INVALID_COMPONENT_TYPE, ErrorSeverity.INFO);
2294 resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.NOT_ALLOWED));
2297 Either<Component, StorageOperationStatus> getResourceResult = toscaOperationFacade.getToscaElement(componentId, JsonParseFlagEnum.ParseAll);
2299 if (getResourceResult.isRight()) {
2300 log.debug(FAILED_TO_RETRIEVE_COMPONENT_COMPONENT_ID, componentId);
2301 ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(getResourceResult.right().value(), componentTypeEnum);
2302 return Either.right(componentsUtils.getResponseFormat(actionStatus, componentId));
2304 Component containerComponent = getResourceResult.left().value();
2306 if (!ComponentValidationUtils.canWorkOnComponent(containerComponent, userId)) {
2307 if (containerComponent.isArchived()) {
2308 log.info("Component is archived. Component id: {}", componentId);
2309 return Either.right(componentsUtils.getResponseFormat(ActionStatus.COMPONENT_IS_ARCHIVED, containerComponent.getName()));
2311 log.info("Restricted operation for user: {} on service {}", userId, componentId);
2312 resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
2315 Either<ComponentInstance, StorageOperationStatus> resourceInstanceStatus = getResourceInstanceById(containerComponent, resourceInstanceId);
2316 if (resourceInstanceStatus.isRight()) {
2317 return Either.right(componentsUtils.getResponseFormat(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER,
2318 resourceInstanceId, "resource instance", "service", componentId));
2321 ComponentInstance foundResourceInstance = resourceInstanceStatus.left().value();
2324 StorageOperationStatus lockStatus = graphLockOperation.lockComponent(componentId, componentTypeEnum.getNodeType());
2325 if (lockStatus != StorageOperationStatus.OK) {
2326 log.debug(FAILED_TO_LOCK_SERVICE, componentId);
2327 return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(lockStatus)));
2329 List <ComponentInstanceInput> updatedInputs = new ArrayList<>();
2331 for (ComponentInstanceInput input: inputs) {
2332 validateMandatoryFields(input);
2333 ComponentInstanceInput componentInstanceInput = validateInputExistsOnComponent(input, containerComponent, foundResourceInstance);
2334 Either<String, ResponseFormat> validatedInputValue = validatePropertyObjectValue(componentInstanceInput, input.getValue(), true);
2335 if (validatedInputValue.isRight()){
2336 throw new ByActionStatusComponentException(ActionStatus.INVALID_CONTENT, input.getName());
2338 updateInputOnContainerComponent(componentInstanceInput, validatedInputValue.left().value(), containerComponent, foundResourceInstance);
2339 updatedInputs.add(componentInstanceInput);
2341 Either<Component, StorageOperationStatus> updateContainerRes = toscaOperationFacade.updateComponentInstanceMetadataOfTopologyTemplate(containerComponent);
2342 if (updateContainerRes.isRight()) {
2343 ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(updateContainerRes.right().value());
2344 resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
2347 resultOp = Either.left(updatedInputs);
2351 if (resultOp == null || resultOp.isRight()) {
2352 janusGraphDao.rollback();
2354 janusGraphDao.commit();
2357 graphLockOperation.unlockComponent(componentId, componentTypeEnum.getNodeType());
2362 private ComponentInstanceInput validateInputExistsOnComponent(ComponentInstanceInput input, Component containerComponent, ComponentInstance foundResourceInstance) {
2363 List<ComponentInstanceInput> instanceProperties = containerComponent.getComponentInstancesInputs().get(foundResourceInstance.getUniqueId());
2364 Optional<ComponentInstanceInput> instanceInput = instanceProperties.stream().filter(p -> p.getName().equals(input.getName())).findAny();
2365 if (!instanceInput.isPresent()) {
2366 throw new ByActionStatusComponentException(ActionStatus.PROPERTY_NOT_FOUND, input.getName());
2368 return instanceInput.get();
2371 public Either<ComponentInstanceProperty, ResponseFormat> createOrUpdateGroupInstancePropertyValue(ComponentTypeEnum componentTypeEnum, String componentId, String resourceInstanceId, String groupInstanceId, ComponentInstanceProperty property, String userId) {
2373 Either<ComponentInstanceProperty, ResponseFormat> resultOp = null;
2375 validateUserExists(userId);
2377 if (componentTypeEnum == null) {
2378 BeEcompErrorManager.getInstance().logInvalidInputError(CREATE_OR_UPDATE_PROPERTY_VALUE, INVALID_COMPONENT_TYPE, ErrorSeverity.INFO);
2379 resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.NOT_ALLOWED));
2383 if (!ComponentValidationUtils.canWorkOnComponent(componentId, toscaOperationFacade, userId)) {
2384 log.info("Restricted operation for user: {} on service: {}", userId, componentId);
2385 resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
2389 StorageOperationStatus lockStatus = graphLockOperation.lockComponent(componentId, componentTypeEnum.getNodeType());
2390 if (lockStatus != StorageOperationStatus.OK) {
2391 log.debug(FAILED_TO_LOCK_SERVICE, componentId);
2392 resultOp = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(lockStatus)));
2396 String propertyValueUid = property.getValueUniqueUid();
2398 if (propertyValueUid == null) {
2400 Either<Integer, StorageOperationStatus> counterRes = groupInstanceOperation.increaseAndGetGroupInstancePropertyCounter(groupInstanceId);
2402 if (counterRes.isRight()) {
2403 log.debug("increaseAndGetResourcePropertyCounter failed resource instance: {} property: {}", resourceInstanceId, property);
2404 StorageOperationStatus status = counterRes.right().value();
2405 ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(status);
2406 resultOp = Either.right(componentsUtils.getResponseFormat(actionStatus));
2408 Integer index = counterRes.left().value();
2409 Either<ComponentInstanceProperty, StorageOperationStatus> result = groupInstanceOperation.addPropertyValueToGroupInstance(property, resourceInstanceId, index, true);
2411 if (result.isLeft()) {
2412 log.trace("Property value was added to resource instance {}", resourceInstanceId);
2413 ComponentInstanceProperty instanceProperty = result.left().value();
2415 resultOp = Either.left(instanceProperty);
2418 log.debug("Failed to add property value: {} to resource instance {}", property, resourceInstanceId);
2420 ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(result.right().value());
2422 resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
2426 Either<ComponentInstanceProperty, StorageOperationStatus> result = groupInstanceOperation.updatePropertyValueInGroupInstance(property, resourceInstanceId, true);
2428 if (result.isLeft()) {
2429 log.debug("Property value {} was updated on graph.", property.getValueUniqueUid());
2430 ComponentInstanceProperty instanceProperty = result.left().value();
2432 resultOp = Either.left(instanceProperty);
2435 log.debug("Failed to update property value: {}, in resource instance {}", property, resourceInstanceId);
2437 ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(result.right().value());
2439 resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
2442 if (resultOp.isLeft()) {
2443 StorageOperationStatus updateCustomizationUUID = componentInstanceOperation.updateCustomizationUUID(resourceInstanceId);
2444 if (updateCustomizationUUID != StorageOperationStatus.OK) {
2445 ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(updateCustomizationUUID);
2447 resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
2454 if (resultOp == null || resultOp.isRight()) {
2455 janusGraphDao.rollback();
2457 janusGraphDao.commit();
2460 graphLockOperation.unlockComponent(componentId, componentTypeEnum.getNodeType());
2465 public Either<ComponentInstanceProperty, ResponseFormat> deletePropertyValue(ComponentTypeEnum componentTypeEnum, String serviceId, String resourceInstanceId, String propertyValueId, String userId) {
2467 validateUserExists(userId);
2469 Either<ComponentInstanceProperty, ResponseFormat> resultOp = null;
2471 if (componentTypeEnum == null) {
2472 BeEcompErrorManager.getInstance().logInvalidInputError(CREATE_OR_UPDATE_PROPERTY_VALUE, INVALID_COMPONENT_TYPE, ErrorSeverity.INFO);
2473 resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.NOT_ALLOWED));
2477 if (!ComponentValidationUtils.canWorkOnComponent(serviceId, toscaOperationFacade, userId)) {
2478 log.info("Restricted operation for user {} on service {}", userId, serviceId);
2479 resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
2483 StorageOperationStatus lockStatus = graphLockOperation.lockComponent(serviceId, componentTypeEnum.getNodeType());
2484 if (lockStatus != StorageOperationStatus.OK) {
2485 log.debug(FAILED_TO_LOCK_SERVICE, serviceId);
2486 resultOp = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(lockStatus)));
2490 Either<ComponentInstanceProperty, StorageOperationStatus> result = propertyOperation.removePropertyValueFromResourceInstance(propertyValueId, resourceInstanceId, true);
2492 if (result.isLeft()) {
2493 log.debug("Property value {} was removed from graph.", propertyValueId);
2494 ComponentInstanceProperty instanceProperty = result.left().value();
2496 resultOp = Either.left(instanceProperty);
2500 log.debug("Failed to remove property value {} in resource instance {}", propertyValueId, resourceInstanceId);
2502 ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(result.right().value());
2504 resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
2510 if (resultOp == null || resultOp.isRight()) {
2511 janusGraphDao.rollback();
2513 janusGraphDao.commit();
2516 graphLockOperation.unlockComponent(serviceId, componentTypeEnum.getNodeType());
2521 private Component getAndValidateOriginComponentOfComponentInstance(Component containerComponent, ComponentInstance componentInstance) {
2523 ComponentTypeEnum componentType = getComponentTypeByParentComponentType(containerComponent.getComponentType());
2524 Component component;
2525 Either<Component, StorageOperationStatus> getComponentRes = toscaOperationFacade.getToscaFullElement(componentInstance.getComponentUid());
2526 if (getComponentRes.isRight()) {
2527 log.debug("Failed to get the component with id {} for component instance {} creation. ", componentInstance.getComponentUid(), componentInstance.getName());
2528 ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(getComponentRes.right().value(), componentType);
2529 throw new ByActionStatusComponentException(actionStatus, Constants.EMPTY_STRING);
2531 component = getComponentRes.left().value();
2532 LifecycleStateEnum resourceCurrState = component.getLifecycleState();
2533 if (resourceCurrState == LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT) {
2534 ActionStatus actionStatus = ActionStatus.CONTAINER_CANNOT_CONTAIN_COMPONENT_IN_STATE;
2535 throw new ByActionStatusComponentException(actionStatus, containerComponent.getComponentType().toString(), resourceCurrState.toString());
2537 if (component.isArchived() == true){
2538 ActionStatus actionStatus = ActionStatus.COMPONENT_IS_ARCHIVED;
2539 throw new ByActionStatusComponentException(actionStatus, component.getName());
2541 final Map<String, InterfaceDefinition> componentInterfaces = component.getInterfaces();
2542 if(MapUtils.isNotEmpty(componentInterfaces)) {
2543 componentInterfaces.forEach(componentInstance::addInterface);
2548 public Either<Set<String>, ResponseFormat> forwardingPathOnVersionChange(String containerComponentParam,
2549 String containerComponentId,
2550 String componentInstanceId,
2551 ComponentInstance newComponentInstance) {
2552 Either<Set<String>, ResponseFormat> resultOp;
2553 final ComponentTypeEnum containerComponentType = validateComponentType(containerComponentParam);
2554 ComponentParametersView componentParametersView = getComponentParametersViewForForwardingPath();
2557 Component containerComponent = validateComponentExists(containerComponentId, containerComponentType, componentParametersView);
2559 //Fetch current component instance
2560 Either<ComponentInstance, StorageOperationStatus> eitherResourceInstance =
2561 getResourceInstanceById(containerComponent, componentInstanceId);
2562 if (eitherResourceInstance.isRight()) {
2563 resultOp = Either.right(componentsUtils.getResponseFormat(
2564 ActionStatus.RESOURCE_INSTANCE_NOT_FOUND_ON_SERVICE, componentInstanceId, containerComponentId));
2567 ComponentInstance currentResourceInstance = eitherResourceInstance.left().value();
2569 //Check whether new componentInstance exists
2570 String resourceId = newComponentInstance.getComponentUid();
2571 Either<Boolean, StorageOperationStatus> componentExistsRes = toscaOperationFacade.validateComponentExists(resourceId);
2572 if (componentExistsRes.isRight()) {
2573 log.debug("Failed to find resource {}", resourceId);
2574 resultOp = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse
2575 (componentExistsRes.right().value()), resourceId));
2577 } else if (!componentExistsRes.left().value()) {
2578 log.debug("The resource {} not found ", resourceId);
2579 resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.RESOURCE_NOT_FOUND, resourceId));
2583 //Fetch component using new component instance uid
2584 Component updatedContainerComponent=getOriginComponentFromComponentInstance(newComponentInstance);
2585 Set<String> toDeleteForwardingPaths = getForwardingPaths(containerComponent,
2586 currentResourceInstance, updatedContainerComponent);
2587 resultOp=Either.left(toDeleteForwardingPaths);
2592 private Set<String> getForwardingPaths(Component containerComponent, ComponentInstance currentResourceInstance,
2593 Component updatedContainerComponent) {
2594 DataForMergeHolder dataForMergeHolder=new DataForMergeHolder();
2595 dataForMergeHolder.setOrigComponentInstId(currentResourceInstance.getName());
2597 Service service = (Service) containerComponent;
2598 ForwardingPathUtils forwardingPathUtils = new ForwardingPathUtils();
2600 return forwardingPathUtils.
2601 getForwardingPathsToBeDeletedOnVersionChange(service,dataForMergeHolder,updatedContainerComponent);
2604 private ComponentParametersView getComponentParametersViewForForwardingPath() {
2605 ComponentParametersView componentParametersView = new ComponentParametersView();
2606 componentParametersView.setIgnoreCapabiltyProperties(false);
2607 componentParametersView.setIgnoreServicePath(false);
2608 return componentParametersView;
2611 public ComponentInstance changeComponentInstanceVersion(String containerComponentParam, String containerComponentId, String componentInstanceId, String userId, ComponentInstance newComponentInstance) {
2613 User user = validateUserExists(userId);
2614 final ComponentTypeEnum containerComponentType = validateComponentType(containerComponentParam);
2615 ComponentParametersView componentParametersView = new ComponentParametersView();
2616 componentParametersView.setIgnoreCapabiltyProperties(false);
2618 org.openecomp.sdc.be.model.Component containerComponent = validateComponentExists(containerComponentId, containerComponentType, componentParametersView);
2620 validateCanWorkOnComponent(containerComponent, userId);
2622 Either<ComponentInstance, StorageOperationStatus> resourceInstanceStatus = getResourceInstanceById(containerComponent, componentInstanceId);
2623 if (resourceInstanceStatus.isRight()) {
2624 throw new ByActionStatusComponentException(ActionStatus.RESOURCE_INSTANCE_NOT_FOUND_ON_SERVICE, componentInstanceId, containerComponentId);
2627 ComponentInstance currentResourceInstance = resourceInstanceStatus.left().value();
2629 return changeInstanceVersion(containerComponent, currentResourceInstance, newComponentInstance, user, containerComponentType );
2632 public ComponentInstance changeInstanceVersion(org.openecomp.sdc.be.model.Component containerComponent,
2633 ComponentInstance currentResourceInstance,
2634 ComponentInstance newComponentInstance,
2636 final ComponentTypeEnum containerComponentType) {
2637 boolean failed = false;
2638 Either<ComponentInstance, StorageOperationStatus> resourceInstanceStatus;
2641 lockComponent(containerComponent, "changeComponentInstanceVersion");
2642 String containerComponentId = containerComponent.getUniqueId();
2643 String componentInstanceId = currentResourceInstance.getUniqueId();
2644 if (currentResourceInstance.getComponentUid().equals(newComponentInstance.getComponentUid())) {
2645 return currentResourceInstance;
2647 String resourceId = newComponentInstance.getComponentUid();
2649 Either<Boolean, StorageOperationStatus> componentExistsRes = toscaOperationFacade
2650 .validateComponentExists(resourceId);
2651 if (componentExistsRes.isRight()) {
2652 StorageOperationStatus errorStatus = componentExistsRes.right().value();
2654 log.debug("Failed to validate existing of the component {}. Status is {} ", resourceId, errorStatus);
2655 throw new ByActionStatusComponentException(
2656 componentsUtils.convertFromStorageResponse(errorStatus), resourceId);
2657 } else if (!componentExistsRes.left().value()) {
2658 log.debug("The resource {} not found ", resourceId);
2659 throw new ByActionStatusComponentException(ActionStatus.RESOURCE_NOT_FOUND, resourceId);
2662 Component eitherOriginComponent = getInstanceOriginNode(currentResourceInstance);
2663 DataForMergeHolder dataHolder = compInstMergeDataBL
2664 .saveAllDataBeforeDeleting(containerComponent, currentResourceInstance, eitherOriginComponent);
2665 ComponentInstance resResourceInfo = deleteComponentInstance(containerComponent, componentInstanceId,
2666 containerComponentType);
2668 if (resResourceInfo == null) {
2669 log.error("Calling `deleteComponentInstance` for the resource {} returned a null", resourceId);
2670 throw new ByActionStatusComponentException(ActionStatus.RESOURCE_NOT_FOUND, resourceId);
2672 Component origComponent = null;
2673 OriginTypeEnum originType = currentResourceInstance.getOriginType();
2674 newComponentInstance.setOriginType(originType);
2675 if (originType == OriginTypeEnum.ServiceProxy) {
2676 Either<Component, StorageOperationStatus> serviceProxyOrigin = toscaOperationFacade
2677 .getLatestByName(SERVICE_PROXY);
2678 if (isServiceProxyOrigin(serviceProxyOrigin)) {
2679 throw new ByActionStatusComponentException(
2680 componentsUtils.convertFromStorageResponse(serviceProxyOrigin.right().value()));
2682 origComponent = serviceProxyOrigin.left().value();
2684 StorageOperationStatus fillProxyRes = fillInstanceData(newComponentInstance, origComponent);
2686 if (isFillProxyRes(fillProxyRes)) {
2687 throw new ByActionStatusComponentException(
2688 componentsUtils.convertFromStorageResponse(fillProxyRes));
2690 } else if (originType == OriginTypeEnum.ServiceSubstitution){
2691 final Either<Component, StorageOperationStatus> getServiceResult = toscaOperationFacade.getToscaFullElement(newComponentInstance.getComponentUid());
2692 if (getServiceResult.isRight()) {
2693 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(getServiceResult.right().value()));
2695 final Component service = getServiceResult.left().value();
2697 final Either<Component, StorageOperationStatus> getServiceDerivedFromTypeResult = toscaOperationFacade.getLatestByToscaResourceName(service.getDerivedFromGenericType());
2698 if (getServiceDerivedFromTypeResult.isRight()) {
2699 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(getServiceResult.right().value()));
2702 origComponent = getServiceDerivedFromTypeResult.left().value();
2704 final StorageOperationStatus fillProxyRes = fillInstanceData(newComponentInstance, origComponent);
2705 if (isFillProxyRes(fillProxyRes)) {
2706 throw new ByActionStatusComponentException(
2707 componentsUtils.convertFromStorageResponse(fillProxyRes));
2710 origComponent = getOriginComponentFromComponentInstance(newComponentInstance);
2711 newComponentInstance.setName(resResourceInfo.getName());
2714 newComponentInstance.setInvariantName(resResourceInfo.getInvariantName());
2715 newComponentInstance.setPosX(resResourceInfo.getPosX());
2716 newComponentInstance.setPosY(resResourceInfo.getPosY());
2717 newComponentInstance.setDescription(resResourceInfo.getDescription());
2719 ComponentInstance updatedComponentInstance =
2720 createComponentInstanceOnGraph(containerComponent, origComponent, newComponentInstance, user);
2721 dataHolder.setCurrInstanceNode(origComponent);
2723 .mergeComponentUserOrigData(user, dataHolder, containerComponent, containerComponentId, newComponentInstance.getUniqueId());
2725 ActionStatus postChangeVersionResult = onChangeInstanceOperationOrchestrator
2726 .doPostChangeVersionOperations(containerComponent, currentResourceInstance, newComponentInstance);
2727 if (postChangeVersionResult != ActionStatus.OK) {
2728 throw new ByActionStatusComponentException(postChangeVersionResult);
2731 ComponentParametersView filter = new ComponentParametersView(true);
2732 filter.setIgnoreComponentInstances(false);
2733 Either<Component, StorageOperationStatus> updatedComponentRes = toscaOperationFacade.getToscaElement(containerComponentId, filter);
2734 if (updatedComponentRes.isRight()) {
2735 StorageOperationStatus storageOperationStatus = updatedComponentRes.right().value();
2736 ActionStatus actionStatus = componentsUtils
2737 .convertFromStorageResponse(storageOperationStatus, containerComponent.getComponentType());
2738 log.debug("Component with id {} was not found", containerComponentId);
2739 throw new ByActionStatusComponentException(actionStatus, Constants.EMPTY_STRING);
2741 resourceInstanceStatus = getResourceInstanceById(updatedComponentRes.left().value(),
2742 updatedComponentInstance.getUniqueId());
2743 if (resourceInstanceStatus.isRight()) {
2744 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse
2745 (resourceInstanceStatus.right().value()), updatedComponentInstance.getUniqueId());
2747 return resourceInstanceStatus.left().value();
2749 } catch (ComponentException e) {
2753 unlockComponent(failed, containerComponent);
2757 private boolean isFillProxyRes(StorageOperationStatus fillProxyRes) {
2758 if (fillProxyRes != StorageOperationStatus.OK) {
2759 log.debug("Failed to fill service proxy resource data with data from service, error {}", fillProxyRes);
2766 public List<ComponentInstanceProperty> getComponentInstancePropertiesById(String containerComponentTypeParam, String containerComponentId, String componentInstanceUniqueId, String userId) {
2767 Component containerComponent = null;
2769 boolean failed = false;
2771 validateUserExists(userId);
2772 validateComponentType(containerComponentTypeParam);
2774 Either<Component, StorageOperationStatus> validateContainerComponentExists = toscaOperationFacade.getToscaElement(containerComponentId);
2775 if (validateContainerComponentExists.isRight()) {
2776 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(validateContainerComponentExists.right().value()));
2778 containerComponent = validateContainerComponentExists.left().value();
2780 Either<ComponentInstance, StorageOperationStatus> resourceInstanceStatus = getResourceInstanceById(containerComponent, componentInstanceUniqueId);
2781 if (resourceInstanceStatus.isRight()) {
2782 throw new ByActionStatusComponentException(ActionStatus.RESOURCE_INSTANCE_NOT_FOUND_ON_SERVICE, componentInstanceUniqueId, containerComponentId);
2785 List<ComponentInstanceProperty> instanceProperties = containerComponent.getComponentInstancesProperties().get(componentInstanceUniqueId);
2786 if (CollectionUtils.isEmpty(instanceProperties)) {
2787 instanceProperties = new ArrayList<>();
2789 return instanceProperties;
2790 } catch (ComponentException e) {
2794 unlockComponent(failed, containerComponent);
2798 public List<ComponentInstanceAttribute> getComponentInstanceAttributesById(final String containerComponentTypeParam,
2799 final String containerComponentId,
2800 final String componentInstanceUniqueId,
2801 final String userId) {
2802 Component containerComponent = null;
2804 boolean failed = false;
2806 validateUserExists(userId);
2807 validateComponentType(containerComponentTypeParam);
2809 final Either<Component, StorageOperationStatus> validateContainerComponentExists =
2810 toscaOperationFacade.getToscaElement(containerComponentId);
2811 if (validateContainerComponentExists.isRight()) {
2812 throw new ByActionStatusComponentException(
2813 componentsUtils.convertFromStorageResponse(validateContainerComponentExists.right().value()));
2815 containerComponent = validateContainerComponentExists.left().value();
2817 if (getResourceInstanceById(containerComponent, componentInstanceUniqueId).isRight()) {
2818 throw new ByActionStatusComponentException(
2819 ActionStatus.RESOURCE_INSTANCE_NOT_FOUND_ON_SERVICE, componentInstanceUniqueId, containerComponentId);
2822 final Map<String, List<ComponentInstanceAttribute>> componentInstancesAttributes = containerComponent.getComponentInstancesAttributes();
2823 return componentInstancesAttributes == null ? new ArrayList<>()
2824 : componentInstancesAttributes.getOrDefault(componentInstanceUniqueId, new ArrayList<>());
2825 } catch (final ComponentException e) {
2829 unlockComponent(failed, containerComponent);
2833 protected void validateIncrementCounter(String resourceInstanceId, GraphPropertiesDictionary counterType,
2834 Wrapper<Integer> instaceCounterWrapper,
2835 Wrapper<ResponseFormat> errorWrapper) {
2836 Either<Integer, StorageOperationStatus> counterRes = componentInstanceOperation
2837 .increaseAndGetResourceInstanceSpecificCounter(resourceInstanceId, counterType, true);
2839 if (counterRes.isRight()) {
2840 log.debug("increase And Get {} failed resource instance {}", counterType, resourceInstanceId);
2841 StorageOperationStatus status = counterRes.right().value();
2842 ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(status);
2843 errorWrapper.setInnerElement(componentsUtils.getResponseFormat(actionStatus));
2845 instaceCounterWrapper.setInnerElement(counterRes.left().value());
2851 * updates componentInstance modificationTime
2853 * @param componentInstance
2854 * @param componentInstanceType
2855 * @param modificationTime
2856 * @param inTransaction
2859 public Either<ComponentInstanceData, ResponseFormat> updateComponentInstanceModificationTimeAndCustomizationUuid(ComponentInstance componentInstance, NodeTypeEnum componentInstanceType, Long modificationTime, boolean inTransaction) {
2860 Either<ComponentInstanceData, ResponseFormat> result;
2861 Either<ComponentInstanceData, StorageOperationStatus> updateComponentInstanceRes = componentInstanceOperation.updateComponentInstanceModificationTimeAndCustomizationUuidOnGraph(componentInstance, componentInstanceType, modificationTime,
2863 if (updateComponentInstanceRes.isRight()) {
2864 log.debug("Failed to update component instance {} with new last update date and mofifier. Status is {}. ", componentInstance.getName(), updateComponentInstanceRes.right().value());
2865 result = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(updateComponentInstanceRes.right().value())));
2867 result = Either.left(updateComponentInstanceRes.left().value());
2872 public Either<ComponentInstance, ResponseFormat> deleteServiceProxy() {
2873 // TODO Add implementation
2874 return Either.left(new ComponentInstance());
2877 public Either<ComponentInstance, ResponseFormat> createServiceProxy() {
2878 // TODO Add implementation
2879 return Either.left(new ComponentInstance());
2882 public Either<ComponentInstance, ResponseFormat> changeServiceProxyVersion() {
2883 // TODO Add implementation
2884 return Either.left(new ComponentInstance());
2887 private Boolean validateInstanceNameUniquenessUponUpdate(Component containerComponent, ComponentInstance oldComponentInstance, String newInstanceName) {
2888 return ComponentValidations.validateNameIsUniqueInComponent(oldComponentInstance.getName(), newInstanceName, containerComponent);
2891 private Either<ComponentInstance, StorageOperationStatus> getResourceInstanceById(Component containerComponent, String instanceId) {
2893 Either<ComponentInstance, StorageOperationStatus> result = Either.right(StorageOperationStatus.NOT_FOUND);
2894 List<ComponentInstance> instances = containerComponent.getComponentInstances();
2895 Optional<ComponentInstance> foundInstance = Optional.empty();
2896 if (!CollectionUtils.isEmpty(instances)) {
2897 if (result.isRight()) {
2898 foundInstance = instances.stream().filter(i -> i.getUniqueId().equals(instanceId)).findFirst();
2899 if (!foundInstance.isPresent()) {
2900 result = Either.right(StorageOperationStatus.NOT_FOUND);
2903 if (result.isRight() && foundInstance.isPresent()) {
2904 result = Either.left(foundInstance.get());
2910 private ComponentInstance buildComponentInstance(ComponentInstance resourceInstanceForUpdate, ComponentInstance origInstanceForUpdate) {
2912 Long creationDate = origInstanceForUpdate.getCreationTime();
2914 Long modificationTime = System.currentTimeMillis();
2915 resourceInstanceForUpdate.setCreationTime(creationDate);
2916 resourceInstanceForUpdate.setModificationTime(modificationTime);
2918 resourceInstanceForUpdate.setCustomizationUUID(origInstanceForUpdate.getCustomizationUUID());
2920 if (StringUtils.isEmpty(resourceInstanceForUpdate.getName()) && StringUtils.isNotEmpty(origInstanceForUpdate.getName())) {
2921 resourceInstanceForUpdate.setName(origInstanceForUpdate.getName());
2924 resourceInstanceForUpdate.setNormalizedName(ValidationUtils.normalizeComponentInstanceName(resourceInstanceForUpdate.getName()));
2926 if (StringUtils.isEmpty(resourceInstanceForUpdate.getIcon()))
2927 resourceInstanceForUpdate.setIcon(origInstanceForUpdate.getIcon());
2929 if (StringUtils.isEmpty(resourceInstanceForUpdate.getComponentVersion()))
2930 resourceInstanceForUpdate.setComponentVersion(origInstanceForUpdate.getComponentVersion());
2932 if (StringUtils.isEmpty(resourceInstanceForUpdate.getComponentName()))
2933 resourceInstanceForUpdate.setComponentName(origInstanceForUpdate.getComponentName());
2935 if (StringUtils.isEmpty(resourceInstanceForUpdate.getToscaComponentName()))
2936 resourceInstanceForUpdate.setToscaComponentName(origInstanceForUpdate.getToscaComponentName());
2938 if (resourceInstanceForUpdate.getOriginType() == null) {
2939 resourceInstanceForUpdate.setOriginType(origInstanceForUpdate.getOriginType());
2941 if(resourceInstanceForUpdate.getOriginType() == OriginTypeEnum.ServiceProxy)
2942 resourceInstanceForUpdate.setIsProxy(true);
2943 if (resourceInstanceForUpdate.getSourceModelInvariant() == null) {
2944 resourceInstanceForUpdate.setSourceModelInvariant(origInstanceForUpdate.getSourceModelInvariant());
2946 if (resourceInstanceForUpdate.getSourceModelName() == null) {
2947 resourceInstanceForUpdate.setSourceModelName(origInstanceForUpdate.getSourceModelName());
2949 if (resourceInstanceForUpdate.getSourceModelUuid() == null) {
2950 resourceInstanceForUpdate.setSourceModelUuid(origInstanceForUpdate.getSourceModelUuid());
2952 if (resourceInstanceForUpdate.getSourceModelUid() == null) {
2953 resourceInstanceForUpdate.setSourceModelUid(origInstanceForUpdate.getSourceModelUid());
2955 if (resourceInstanceForUpdate.getCreatedFrom() == null) {
2956 resourceInstanceForUpdate.setCreatedFrom(origInstanceForUpdate.getCreatedFrom());
2958 return resourceInstanceForUpdate;
2961 * Returns list of ComponentInstanceProperty belonging to component instance capability specified by name, type and ownerId
2962 * @param containerComponentType
2963 * @param containerComponentId
2964 * @param componentInstanceUniqueId
2965 * @param capabilityType
2966 * @param capabilityName
2971 public List<ComponentInstanceProperty> getComponentInstanceCapabilityPropertiesById(String containerComponentType, String containerComponentId, String componentInstanceUniqueId, String capabilityType, String capabilityName, String ownerId, String userId) {
2973 Component containerComponent = null;
2975 List<ComponentInstanceProperty> resultOp = null;
2977 validateUserExists(userId);
2978 validateComponentType(containerComponentType);
2979 containerComponent = toscaOperationFacade.getToscaFullElement(containerComponentId).left().on(this::componentException);
2980 ComponentInstance resourceInstanceStatus = getResourceInstanceById(containerComponent, componentInstanceUniqueId).left()
2981 .on(this::componentInstanceException);
2982 resultOp = findCapabilityOfInstance(containerComponentId, componentInstanceUniqueId, capabilityType, capabilityName, ownerId,
2983 resourceInstanceStatus.getCapabilities());
2984 } catch (StorageException | ComponentException e) {
2985 unlockRollbackWithException(containerComponent, e);
2986 } catch (Exception e) {
2987 unlockRollbackWithException(containerComponent, new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR));
2989 unlockWithCommit(containerComponent);
2993 private List<ComponentInstanceProperty> findCapabilityOfInstance( String componentId, String instanceId, String capabilityType, String capabilityName, String ownerId, Map<String, List<CapabilityDefinition>> instanceCapabilities) {
2994 CapabilityDefinition foundCapability;
2995 if (MapUtils.isNotEmpty(instanceCapabilities)) {
2996 List<CapabilityDefinition> capabilitiesPerType = instanceCapabilities.get(capabilityType);
2997 if (capabilitiesPerType != null) {
2998 Optional<CapabilityDefinition> capabilityOpt = capabilitiesPerType.stream().filter(c -> c.getName().equals(capabilityName) && c.getOwnerId().equals(ownerId)).findFirst();
2999 if (capabilityOpt.isPresent()) {
3000 foundCapability = capabilityOpt.get();
3001 return foundCapability.getProperties() == null ? new ArrayList<>() : foundCapability.getProperties();
3005 return fetchComponentInstanceCapabilityProperties(componentId, instanceId, capabilityType, capabilityName, ownerId);
3008 private List<ComponentInstanceProperty> fetchComponentInstanceCapabilityProperties(String componentId, String instanceId, String capabilityType, String capabilityName, String ownerId) {
3010 return toscaOperationFacade.getComponentInstanceCapabilityProperties(componentId, instanceId, capabilityName, capabilityType, ownerId)
3012 .on(this::componentInstancePropertyListException);
3013 } catch(Exception e){
3014 log.debug("The exception {} occurred upon the component {} instance {} capability {} properties retrieving. ", componentId, instanceId, capabilityName, e);
3015 throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR);
3019 public Either<RequirementDefinition, ResponseFormat> updateInstanceRequirement(ComponentTypeEnum componentTypeEnum, String containerComponentId,
3020 String componentInstanceUniqueId,
3021 RequirementDefinition requirementDef, String userId) {
3023 Either<RequirementDefinition, ResponseFormat> resultOp = null;
3025 validateUserExists(userId);
3026 if (componentTypeEnum == null) {
3027 BeEcompErrorManager.getInstance().logInvalidInputError("updateInstanceRequirement", INVALID_COMPONENT_TYPE, ErrorSeverity.INFO);
3028 return Either.right(componentsUtils.getResponseFormat(ActionStatus.NOT_ALLOWED));
3030 Either<Component, StorageOperationStatus> getResourceResult = toscaOperationFacade.getToscaFullElement(containerComponentId);
3032 if (getResourceResult.isRight()) {
3033 log.debug(FAILED_TO_RETRIEVE_COMPONENT_COMPONENT_ID, containerComponentId);
3034 return Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
3036 Component containerComponent = getResourceResult.left().value();
3038 if (!ComponentValidationUtils.canWorkOnComponent(containerComponent, userId)) {
3039 log.info("Restricted operation for user: {} on component {}", userId, containerComponentId);
3040 return Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
3042 Either<ComponentInstance, StorageOperationStatus> resourceInstanceStatus = getResourceInstanceById(containerComponent, componentInstanceUniqueId);
3043 if (resourceInstanceStatus.isRight()) {
3044 return Either.right(componentsUtils.getResponseFormat(ActionStatus.RESOURCE_INSTANCE_NOT_FOUND_ON_SERVICE, componentInstanceUniqueId, containerComponentId));
3047 StorageOperationStatus lockStatus = graphLockOperation.lockComponent(containerComponentId, componentTypeEnum.getNodeType());
3048 if (lockStatus != StorageOperationStatus.OK) {
3049 log.debug("Failed to lock component {}", containerComponentId);
3050 return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(lockStatus)));
3054 StorageOperationStatus updateRequirementStatus = toscaOperationFacade.updateComponentInstanceRequirement(containerComponentId, componentInstanceUniqueId, requirementDef);
3055 if (updateRequirementStatus != StorageOperationStatus.OK) {
3056 log.debug("Failed to update component instance requirement on instance {} in container {}", componentInstanceUniqueId, containerComponentId);
3057 return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(updateRequirementStatus)));
3059 Either<Component, StorageOperationStatus> updateContainerRes = toscaOperationFacade.updateComponentInstanceMetadataOfTopologyTemplate(containerComponent);
3061 if (updateContainerRes.isRight()) {
3062 ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(updateContainerRes.right().value());
3063 resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
3066 resultOp = Either.left(requirementDef);
3070 if (resultOp == null || resultOp.isRight()) {
3071 janusGraphDao.rollback();
3073 janusGraphDao.commit();
3076 graphLockOperation.unlockComponent(containerComponentId, componentTypeEnum.getNodeType());
3080 public Either<List<ComponentInstanceProperty>, ResponseFormat> updateInstanceCapabilityProperties(ComponentTypeEnum componentTypeEnum, String containerComponentId, String componentInstanceUniqueId, String capabilityType, String capabilityName,
3081 List<ComponentInstanceProperty> properties, String userId) {
3082 Either<List<ComponentInstanceProperty>, ResponseFormat> resultOp = null;
3084 validateUserExists(userId);
3085 if (componentTypeEnum == null) {
3086 BeEcompErrorManager.getInstance().logInvalidInputError("updateInstanceCapabilityProperty", INVALID_COMPONENT_TYPE, ErrorSeverity.INFO);
3087 return Either.right(componentsUtils.getResponseFormat(ActionStatus.NOT_ALLOWED));
3089 Either<Component, StorageOperationStatus> getResourceResult = toscaOperationFacade.getToscaFullElement(containerComponentId);
3091 if (getResourceResult.isRight()) {
3092 log.debug(FAILED_TO_RETRIEVE_COMPONENT_COMPONENT_ID, containerComponentId);
3093 return Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
3095 Component containerComponent = getResourceResult.left().value();
3097 if (!ComponentValidationUtils.canWorkOnComponent(containerComponent, userId)) {
3098 log.info("Restricted operation for user: {} on component {}", userId, containerComponentId);
3099 return Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
3101 Either<ComponentInstance, StorageOperationStatus> resourceInstanceStatus = getResourceInstanceById(containerComponent, componentInstanceUniqueId);
3102 if (resourceInstanceStatus.isRight()) {
3103 return Either.right(componentsUtils.getResponseFormat(ActionStatus.RESOURCE_INSTANCE_NOT_FOUND_ON_SERVICE, componentInstanceUniqueId, containerComponentId));
3105 ComponentInstance foundResourceInstance = resourceInstanceStatus.left().value();
3107 StorageOperationStatus lockStatus = graphLockOperation.lockComponent(containerComponentId, componentTypeEnum.getNodeType());
3108 if (lockStatus != StorageOperationStatus.OK) {
3109 log.debug("Failed to lock component {}", containerComponentId);
3110 return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(lockStatus)));
3114 for (ComponentInstanceProperty property : properties) {
3115 Either<String, ResponseFormat> newPropertyValueEither = validatePropertyObjectValue(property, property.getValue(), false);
3116 newPropertyValueEither.bimap(updatedValue ->
3117 updateCapabilityPropertyOnContainerComponent(property,updatedValue, containerComponent, foundResourceInstance, capabilityType, capabilityName),
3120 Either<Component, StorageOperationStatus> updateContainerRes = toscaOperationFacade.updateComponentInstanceMetadataOfTopologyTemplate(containerComponent);
3122 if (updateContainerRes.isRight()) {
3123 ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(updateContainerRes.right().value());
3124 resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
3127 resultOp = Either.left(properties);
3131 if (resultOp == null || resultOp.isRight()) {
3132 janusGraphDao.rollback();
3134 janusGraphDao.commit();
3137 graphLockOperation.unlockComponent(containerComponentId, componentTypeEnum.getNodeType());
3141 public Either<Map<String, ComponentInstance>, ResponseFormat> copyComponentInstance(ComponentInstance inputComponentInstance,
3142 String containerComponentId,
3143 String componentInstanceId,
3146 Map<String, ComponentInstance> resultMap = new HashMap<>();
3147 Either<Component, StorageOperationStatus> getOrigComponent = toscaOperationFacade.getToscaElement(containerComponentId);
3148 if (getOrigComponent.isRight()) {
3149 log.error("Failed to get the original component information");
3150 return Either.right(componentsUtils.getResponseFormat(
3151 ActionStatus.USER_DEFINED, FAILED_TO_COPY_COMP_INSTANCE_TO_CANVAS));
3154 Component origComponent = getOrigComponent.left().value();
3157 lockComponent(origComponent, "copyComponentInstance");
3159 } catch (ComponentException e) {
3160 log.error("destComponentInstance's data is {}", origComponent.toString());
3161 return Either.right(componentsUtils.getResponseFormat(
3162 ActionStatus.USER_DEFINED, "Failed to lock component destComponentInstance's data is {}", origComponent.toString()));
3165 boolean failed = false;
3166 ComponentInstance actionResponse = null;
3169 actionResponse = createComponentInstance(
3170 "services", containerComponentId, userId, inputComponentInstance, false);
3172 } catch (ComponentException e) {
3174 // on failure of the create instance unlock the resource and rollback the transaction.
3175 return Either.right(componentsUtils.getResponseFormat(ActionStatus.USER_DEFINED, FAILED_TO_COPY_COMP_INSTANCE_TO_CANVAS));
3178 // on failure of the create instance unlock the resource and rollback the transaction.
3179 if (null == actionResponse) {
3180 log.error(FAILED_TO_COPY_COMP_INSTANCE_TO_CANVAS);
3182 unlockComponent(failed, origComponent);
3186 Either<String, ResponseFormat> resultOp = null;
3189 ComponentInstance destComponentInstance = actionResponse;
3190 log.debug("destComponentInstance's data is {}", destComponentInstance);
3192 resultOp = deepCopyComponentInstance(origComponent, containerComponentId, componentInstanceId, destComponentInstance, userId);
3194 resultMap.put("componentInstance", destComponentInstance);
3198 if (resultOp == null || resultOp.isRight()) {
3199 unlockComponent(true, origComponent);
3200 janusGraphDao.rollback();
3201 log.error("Failed to deep copy component instance");
3203 unlockComponent(false, origComponent);
3204 janusGraphDao.commit();
3205 log.debug("Success trasaction commit");
3208 if (resultOp == null || resultOp.isRight()) {
3209 return Either.right(componentsUtils.getResponseFormat(ActionStatus.USER_DEFINED, "Failed to deep copy the component instance to the canvas"));
3211 return Either.left(resultMap);
3216 private Either<String, ResponseFormat> deepCopyComponentInstance(
3217 Component sourceComponent, String containerComponentId, String sourceComponentInstanceId,
3218 ComponentInstance destComponentInstance, String userId) {
3220 Either<Component, StorageOperationStatus> getDestComponent = toscaOperationFacade.getToscaElement(containerComponentId);
3221 if (getDestComponent.isRight()) {
3222 log.error("Failed to get the dest component information");
3223 return Either.right(componentsUtils.getResponseFormat(ActionStatus.USER_DEFINED, FAILED_TO_COPY_COMP_INSTANCE_TO_CANVAS));
3226 Component destComponent = getDestComponent.left().value();
3228 Either<String, ResponseFormat> copyComponentInstanceWithPropertiesAndInputs = copyComponentInstanceWithPropertiesAndInputs(
3229 sourceComponent, destComponent, sourceComponentInstanceId, destComponentInstance);
3230 if (copyComponentInstanceWithPropertiesAndInputs.isRight()) {
3231 log.error("Failed to copy component instance with properties and inputs as part of deep copy");
3232 return Either.right(componentsUtils.getResponseFormat(
3233 ActionStatus.USER_DEFINED, "Failed to copy the component instance with properties and inputs as part of deep copy"));
3236 Either<String, ResponseFormat> copyComponentInstanceWithAttributes = copyComponentInstanceWithAttributes(
3237 sourceComponent, destComponent, sourceComponentInstanceId, destComponentInstance, userId);
3238 if (copyComponentInstanceWithAttributes.isRight()) {
3239 log.error("Failed to copy component instance with attributes as part of deep copy");
3240 return Either.right(componentsUtils.getResponseFormat(
3241 ActionStatus.USER_DEFINED, "Failed to copy the component instance with attributes as part of deep copy"));
3243 return Either.left(COPY_COMPONENT_INSTANCE_OK);
3246 private Either<String, ResponseFormat> copyComponentInstanceWithPropertiesAndInputs(
3247 Component sourceComponent, Component destComponent, String sourceComponentInstanceId,
3248 ComponentInstance destComponentInstance) {
3249 log.debug("start to copy ComponentInstance with properties and inputs");
3251 List<ComponentInstanceProperty> sourcePropList = null;
3252 if (sourceComponent.getComponentInstancesProperties() != null
3253 && sourceComponent.getComponentInstancesProperties().get(sourceComponentInstanceId) != null) {
3254 sourcePropList = sourceComponent.getComponentInstancesProperties().get(sourceComponentInstanceId);
3255 log.debug("sourcePropList");
3258 List<ComponentInstanceProperty> destPropList = null;
3259 String destComponentInstanceId = destComponentInstance.getUniqueId();
3260 log.debug("destComponentInstanceId: {}", destComponentInstance.getUniqueId());
3261 if (destComponent.getComponentInstancesProperties() != null
3262 && destComponent.getComponentInstancesProperties().get(destComponentInstanceId) != null) {
3263 destPropList = destComponent.getComponentInstancesProperties().get(destComponentInstanceId);
3264 log.debug("destPropList {}");
3267 List<ComponentInstancePropInput> componentInstancePropInputList = new ArrayList<>();
3269 if (null != destPropList && null != sourcePropList) {
3270 log.debug("start to set property and attribute");
3271 for (ComponentInstanceProperty destProp : destPropList) {
3272 String destPropertyName = destProp.getName();
3273 for (ComponentInstanceProperty sourceProp : sourcePropList) {
3274 if (!destPropertyName.equals(sourceProp.getName())) {
3277 log.debug("now set property");
3278 final List<GetInputValueDataDefinition> getInputValues = sourceProp.getGetInputValues();
3279 if (getInputValues == null && !StringUtils.isEmpty(sourceProp.getValue())
3280 && (destProp.getValue() == null || !destProp.getValue().equals(sourceProp.getValue()))) {
3281 log.debug("Now starting to copy the property {} in value {}", destPropertyName, sourceProp.getValue());
3283 destProp.setValue(sourceProp.getValue());
3284 Either<String, ResponseFormat> updatePropertyValueEither = updateComponentInstanceProperty(
3285 destComponent.getUniqueId(), destComponentInstanceId, destProp);
3286 if (updatePropertyValueEither.isRight()) {
3287 log.error("Failed to copy the property {}", destPropertyName);
3288 return Either.right(componentsUtils.getResponseFormat(
3289 ActionStatus.INVALID_CONTENT_PARAM, "Failed to paste component instance to the canvas, property copy"));
3294 log.debug("Now start to update inputs");
3296 if (getInputValues != null) {
3297 if (getInputValues.isEmpty()) {
3298 log.debug("property is return from input, set by man");
3301 log.debug("Now starting to copy the {} property", destPropertyName);
3303 Either<String, ResponseFormat> getSourceInputDefaultValue = getInputListDefaultValue(
3304 sourceComponent, getInputValues.get(0).getInputId());
3305 if (getSourceInputDefaultValue.isRight()) {
3306 return Either.right(getSourceInputDefaultValue.right().value());
3308 componentInstancePropInputList.add(new ComponentInstancePropInput(destProp));
3313 return Either.left(COPY_COMPONENT_INSTANCE_OK);
3316 private Either<String, ResponseFormat> copyComponentInstanceWithAttributes(Component sourceComponent,
3317 Component destComponent,
3318 String sourceComponentInstanceId,
3319 ComponentInstance destComponentInstance,
3321 String destComponentInstanceId = destComponentInstance.getUniqueId();
3323 log.info("start to copy component instance with attributes");
3325 List<ComponentInstanceAttribute> sourceAttributeList = null;
3326 if (sourceComponent.getComponentInstancesAttributes() != null
3327 && sourceComponent.getComponentInstancesAttributes().get(sourceComponentInstanceId) != null) {
3328 sourceAttributeList = sourceComponent.getComponentInstancesAttributes().get(sourceComponentInstanceId);
3329 log.info("sourceAttributes {}");
3332 List<ComponentInstanceAttribute> destAttributeList = null;
3333 if (destComponent.getComponentInstancesAttributes() != null
3334 && destComponent.getComponentInstancesAttributes().get(destComponentInstanceId) != null) {
3335 destAttributeList = destComponent.getComponentInstancesAttributes().get(destComponentInstanceId);
3336 log.info("destAttributeList {}");
3338 if (null != sourceAttributeList && null != destAttributeList) {
3339 log.info("set attribute");
3341 for (ComponentInstanceAttribute sourceAttribute : sourceAttributeList) {
3342 String sourceAttributeName = sourceAttribute.getName();
3343 for (ComponentInstanceAttribute destAttribute : destAttributeList) {
3344 if (sourceAttributeName.equals(destAttribute.getName())) {
3345 // if (sourceAttribute.getValue() != null && !sourceAttribute.getValue().isEmpty()) {
3346 log.debug("Start to copy the attribute exists {}", sourceAttributeName);
3348 sourceAttribute.setUniqueId(
3349 UniqueIdBuilder.buildResourceInstanceUniuqeId(
3350 "attribute" , destComponentInstanceId.split("\\.")[1] , sourceAttributeName));
3352 Either<ComponentInstanceAttribute, ResponseFormat> updateAttributeValueEither =
3353 createOrUpdateAttributeValueForCopyPaste(ComponentTypeEnum.SERVICE,
3354 destComponent.getUniqueId(), destComponentInstanceId, sourceAttribute,
3356 if (updateAttributeValueEither.isRight()) {
3357 log.error("Failed to copy the attribute");
3358 return Either.right(componentsUtils
3359 .getResponseFormat(ActionStatus.INVALID_CONTENT_PARAM,
3360 "Failed to paste component instance to the canvas, attribute copy"));
3369 return Either.left(COPY_COMPONENT_INSTANCE_OK);
3372 private Either<ComponentInstanceAttribute, ResponseFormat> createOrUpdateAttributeValueForCopyPaste(ComponentTypeEnum componentTypeEnum,
3374 String resourceInstanceId,
3375 ComponentInstanceAttribute attribute,
3378 Either<ComponentInstanceAttribute, ResponseFormat> resultOp = null;
3380 validateUserExists(userId);
3382 if (componentTypeEnum == null) {
3383 BeEcompErrorManager.getInstance().logInvalidInputError(
3384 "createOrUpdateAttributeValue", INVALID_COMPONENT_TYPE, ErrorSeverity.INFO);
3385 resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.NOT_ALLOWED));
3389 Either<Component, StorageOperationStatus> getResourceResult = toscaOperationFacade.getToscaElement(componentId, JsonParseFlagEnum.ParseAll);
3391 if (getResourceResult.isRight()) {
3392 log.info("Failed to retrieve component id {}", componentId);
3393 resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
3397 Component containerComponent = getResourceResult.left().value();
3399 Either<ComponentInstance, StorageOperationStatus> resourceInstanceStatus = getResourceInstanceById(containerComponent, resourceInstanceId);
3401 if (resourceInstanceStatus.isRight()) {
3402 resultOp = Either.right(componentsUtils.getResponseFormat(
3403 ActionStatus.RESOURCE_INSTANCE_NOT_FOUND_ON_SERVICE, resourceInstanceId, componentId));
3407 ComponentInstance foundResourceInstance = resourceInstanceStatus.left().value();
3410 String propertyType = attribute.getType();
3411 ToscaPropertyType type = ToscaPropertyType.isValidType(propertyType);
3412 log.info("The type of attribute id{},is {} ", attribute.getUniqueId(), propertyType);
3414 if (type == ToscaPropertyType.LIST || type == ToscaPropertyType.MAP) {
3415 SchemaDefinition def = attribute.getSchema();
3417 log.info("Schema doesn't exists for attribute of type {}", type);
3418 return Either.right(componentsUtils.getResponseFormat(
3419 componentsUtils.convertFromStorageResponse(StorageOperationStatus.INVALID_VALUE)));
3421 PropertyDataDefinition propDef = def.getProperty();
3422 if (propDef == null) {
3423 log.info("Attribute in Schema Definition inside attribute of type {} doesn't exist", type);
3424 return Either.right(componentsUtils.getResponseFormat(
3425 componentsUtils.convertFromStorageResponse(StorageOperationStatus.INVALID_VALUE)));
3429 List<ComponentInstanceAttribute> instanceAttributes = containerComponent.getComponentInstancesAttributes().get(resourceInstanceId);
3430 Optional<ComponentInstanceAttribute> instanceAttribute = instanceAttributes.stream().filter(p -> p.getUniqueId().equals(attribute.getUniqueId())).findAny();
3431 StorageOperationStatus status;
3433 if (instanceAttribute.isPresent()) {
3434 log.info("updateComponentInstanceAttribute");
3435 status = toscaOperationFacade.updateComponentInstanceAttribute(containerComponent, foundResourceInstance.getUniqueId(), attribute);
3437 log.info("addComponentInstanceAttribute");
3438 status = toscaOperationFacade.addComponentInstanceAttribute(containerComponent, foundResourceInstance.getUniqueId(), attribute);
3440 if (status != StorageOperationStatus.OK) {
3441 ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(status);
3442 resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
3445 List<String> path = new ArrayList<>();
3446 path.add(foundResourceInstance.getUniqueId());
3447 attribute.setPath(path);
3449 foundResourceInstance.setCustomizationUUID(UUID.randomUUID().toString());
3450 Either<Component, StorageOperationStatus> updateContainerRes = toscaOperationFacade.
3451 updateComponentInstanceMetadataOfTopologyTemplate(containerComponent);
3453 if (updateContainerRes.isRight()) {
3454 ActionStatus actionStatus = componentsUtils.
3455 convertFromStorageResponseForResourceInstanceProperty(updateContainerRes.right().value());
3456 resultOp = Either.right(componentsUtils.
3457 getResponseFormatForResourceInstanceProperty(actionStatus, ""));
3460 resultOp = Either.left(attribute);
3464 private Either<String, ResponseFormat> updateComponentInstanceProperty(String containerComponentId,
3465 String componentInstanceId,
3466 ComponentInstanceProperty property) {
3467 Either<String, ResponseFormat> resultOp;
3468 Either<Component, StorageOperationStatus> getComponent = toscaOperationFacade.getToscaElement(containerComponentId);
3470 if (getComponent.isRight()) {
3471 log.error("Failed to get the component information");
3472 return Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(
3473 ActionStatus.INVALID_CONTENT_PARAM, "Failed to get the component information"));
3476 Component containerComponent = getComponent.left().value();
3478 StorageOperationStatus status = toscaOperationFacade.updateComponentInstanceProperty(
3479 containerComponent, componentInstanceId, property);
3480 if (status != StorageOperationStatus.OK) {
3481 ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(status);
3482 resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
3486 Either<Component, StorageOperationStatus> updateContainerRes = toscaOperationFacade.
3487 updateComponentInstanceMetadataOfTopologyTemplate(containerComponent);
3489 if (updateContainerRes.isRight()) {
3490 ActionStatus actionStatus = componentsUtils.
3491 convertFromStorageResponseForResourceInstanceProperty(updateContainerRes.right().value());
3492 resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
3496 return Either.left("Update OK");
3499 private Either<String, ResponseFormat> getInputListDefaultValue(Component component, String inputId) {
3500 List<InputDefinition> inputList = component.getInputs();
3501 for (InputDefinition input : inputList) {
3502 if (input.getUniqueId().equals(inputId)) {
3503 if (input.getDefaultValue() == null) {
3504 log.debug("The input's default value is null");
3505 return Either.left(null);
3507 return Either.left(input.getDefaultValue());
3510 log.error("The input's default value with id {} is not found", inputId);
3511 return Either.right(componentsUtils.getResponseFormat(
3512 ActionStatus.USER_DEFINED, "Failed to paste component instance to the canvas"));
3516 * Method to delete selected nodes and edges on composition page
3517 * @param containerComponentType
3518 * @param componentId
3519 * @param componentInstanceIdList
3523 public Map<String, List<String>> batchDeleteComponentInstance(String containerComponentType,
3525 List<String> componentInstanceIdList,
3528 List<String> deleteErrorIds = new ArrayList<>();
3529 Map<String, List<String>> deleteErrorMap = new HashMap<>();
3530 validateUserExists(userId);
3531 org.openecomp.sdc.be.model.Component containerComponent = validateComponentExists(componentId, ComponentTypeEnum.findByParamName(containerComponentType), null);
3533 boolean failed = false;
3535 lockComponent(containerComponent, "batchDeleteComponentInstance");
3536 for (String eachInstanceId : componentInstanceIdList) {
3537 Either<ComponentInstance, ResponseFormat> actionResponse = batchDeleteComponentInstance(
3538 containerComponent, containerComponentType, eachInstanceId);
3539 log.debug("batchDeleteResourceInstances actionResponse is {}", actionResponse);
3540 if (actionResponse.isRight()) {
3541 log.error("Failed to delete ComponentInstance [{}]", eachInstanceId);
3542 deleteErrorIds.add(eachInstanceId);
3545 //sending the ids of the error nodes that were not deleted to UI
3546 deleteErrorMap.put("deleteFailedIds", deleteErrorIds);
3547 return deleteErrorMap;
3548 }catch (ComponentException e){
3552 unlockComponent(failed, containerComponent);
3556 private Either<ComponentInstance, ResponseFormat> batchDeleteComponentInstance(Component containerComponent,
3557 String containerComponentType,
3558 String componentInstanceId) {
3560 ComponentInstance resultOp;
3561 final ComponentTypeEnum containerComponentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
3564 resultOp = deleteComponentInstance(containerComponent, componentInstanceId, containerComponentTypeEnum);
3565 log.info("Successfully deleted instance with id {}", componentInstanceId);
3566 return Either.left(resultOp);
3568 catch (ComponentException e){
3569 log.error("Failed to deleteComponentInstance with instanceId[{}]", componentInstanceId);
3570 return Either.right(new ResponseFormat());
3574 public void validateUser(final String userId) {
3575 final User user = userValidations.validateUserExists(userId);
3576 userValidations.validateUserRole(user, Arrays.asList(Role.DESIGNER, Role.ADMIN));