2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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.merge.instance;
23 import fj.data.Either;
24 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
25 import org.openecomp.sdc.be.dao.api.ActionStatus;
26 import org.openecomp.sdc.be.impl.ComponentsUtils;
27 import org.openecomp.sdc.be.model.Component;
28 import org.openecomp.sdc.be.model.ComponentInstance;
29 import org.openecomp.sdc.be.model.ComponentInstanceInput;
30 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
31 import org.openecomp.sdc.be.model.ComponentParametersView;
32 import org.openecomp.sdc.be.model.InputDefinition;
33 import org.openecomp.sdc.be.model.User;
34 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
35 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
36 import org.openecomp.sdc.common.log.wrappers.Logger;
38 import java.util.ArrayList;
39 import java.util.List;
41 import static org.apache.commons.collections.CollectionUtils.isNotEmpty;
44 * Created by chaya on 9/20/2017.
46 @org.springframework.stereotype.Component("ComponentInstancePropsAndInputsMerge")
47 public class ComponentInstancePropsAndInputsMerge implements ComponentInstanceMergeInterface {
49 private static final Logger log = Logger.getLogger(ComponentInstancePropsAndInputsMerge.class);
51 private final ToscaOperationFacade toscaOperationFacade;
52 private final ComponentsUtils componentsUtils;
53 private final ComponentInstancePropertiesMergeBL componentInstancePropertiesMergeBL;
54 private final ComponentInstanceInputsMergeBL resourceInstanceInputsMergeBL;
55 private final ComponentInstanceInputsRedeclareHandler instanceInputsRedeclareHandler;
57 public ComponentInstancePropsAndInputsMerge(ToscaOperationFacade toscaOperationFacade, ComponentsUtils componentsUtils, ComponentInstancePropertiesMergeBL componentInstancePropertiesMergeBL, ComponentInstanceInputsMergeBL resourceInstanceInputsMergeBL, ComponentInstanceInputsRedeclareHandler instanceInputsRedeclareHandler) {
58 this.toscaOperationFacade = toscaOperationFacade;
59 this.componentsUtils = componentsUtils;
60 this.componentInstancePropertiesMergeBL = componentInstancePropertiesMergeBL;
61 this.resourceInstanceInputsMergeBL = resourceInstanceInputsMergeBL;
62 this.instanceInputsRedeclareHandler = instanceInputsRedeclareHandler;
66 public void saveDataBeforeMerge(DataForMergeHolder dataHolder, Component containerComponent, ComponentInstance currentResourceInstance, Component originComponent) {
67 dataHolder.setOrigComponentInstanceInputs(containerComponent.safeGetComponentInstanceInputsByName(currentResourceInstance.getName()));
68 dataHolder.setOrigComponentInstanceProperties(containerComponent.safeGetComponentInstanceProperties(currentResourceInstance.getUniqueId()));
69 dataHolder.setOrigComponentInputs(containerComponent.getInputs());
73 public Component mergeDataAfterCreate(User user, DataForMergeHolder dataHolder, Component updatedContainerComponent, String newInstanceId) {
74 Either<List<ComponentInstanceInput>, ActionStatus> instanceInputsEither = mergeComponentInstanceInputsIntoContainer(dataHolder, updatedContainerComponent, newInstanceId);
75 if (instanceInputsEither.isRight()) {
76 ActionStatus actionStatus = instanceInputsEither.right().value();
77 throw new ByActionStatusComponentException(actionStatus);
79 Either<List<ComponentInstanceProperty>, ActionStatus> instancePropsEither = mergeComponentInstancePropsIntoContainer(dataHolder, updatedContainerComponent, newInstanceId);
80 if (instancePropsEither.isRight()) {
81 ActionStatus actionStatus = instancePropsEither.right().value();
82 throw new ByActionStatusComponentException(actionStatus);
84 Either<List<InputDefinition>, ActionStatus> inputsEither = mergeComponentInputsIntoContainer(dataHolder, updatedContainerComponent.getUniqueId(), newInstanceId);
85 if (inputsEither.isRight()) {
86 ActionStatus actionStatus = inputsEither.right().value();
87 throw new ByActionStatusComponentException(actionStatus);
89 return updatedContainerComponent;
92 private Either<List<ComponentInstanceProperty>, ActionStatus> mergeComponentInstancePropsIntoContainer(DataForMergeHolder dataHolder, Component updatedComponent, String instanceId) {
93 List<ComponentInstanceProperty> originComponentInstanceProps = dataHolder.getOrigComponentInstanceProperties();
94 List<InputDefinition> originComponentsInputs = dataHolder.getOrigComponentInputs();
95 List<ComponentInstanceProperty> newComponentInstancesProps = updatedComponent.safeGetComponentInstanceProperties(instanceId);
96 ActionStatus actionStatus = componentInstancePropertiesMergeBL.mergeComponentInstanceProperties(originComponentInstanceProps, originComponentsInputs, updatedComponent, instanceId);
98 if (actionStatus != ActionStatus.OK) {
99 log.error("Failed to update component {} with merged instance properties", updatedComponent.getUniqueId(), newComponentInstancesProps);
100 return Either.right(actionStatus);
102 return Either.left(newComponentInstancesProps);
105 private Either<List<ComponentInstanceInput>, ActionStatus> mergeComponentInstanceInputsIntoContainer(DataForMergeHolder dataHolder, Component updatedComponent, String instanceId) {
106 List<ComponentInstanceInput> originComponentInstanceInputs = dataHolder.getOrigComponentInstanceInputs();
107 List<InputDefinition> originComponentsInputs = dataHolder.getOrigComponentInputs();
108 List<ComponentInstanceInput> newComponentInstancesInputs = updatedComponent.safeGetComponentInstanceInput(instanceId);
109 ActionStatus actionStatus = resourceInstanceInputsMergeBL.mergeComponentInstanceInputs(originComponentInstanceInputs, originComponentsInputs, updatedComponent, instanceId);
110 if (actionStatus != ActionStatus.OK) {
111 log.error("Failed to update component {} with merged instance properties", updatedComponent.getUniqueId(), newComponentInstancesInputs);
112 return Either.right(actionStatus);
114 return Either.left(newComponentInstancesInputs);
117 private Either<List<InputDefinition>, ActionStatus> mergeComponentInputsIntoContainer(DataForMergeHolder dataHolder, String newContainerComponentId, String newInstanceId) {
118 List<InputDefinition> origComponentInputs = dataHolder.getOrigComponentInputs();
119 List<InputDefinition> inputsToAddToContainer = new ArrayList<>();
120 if (isNotEmpty(origComponentInputs)) {
121 // get instance inputs and properties after merge
122 Either<Component, StorageOperationStatus> componentWithInstancesInputsAndProperties = getComponentWithInstancesInputsAndProperties(newContainerComponentId);
123 if (componentWithInstancesInputsAndProperties.isRight()) {
124 log.error("Component %s was not found", newContainerComponentId);
125 return Either.right(componentsUtils.convertFromStorageResponse(componentWithInstancesInputsAndProperties.right().value()));
127 Component updatedContainerComponent = componentWithInstancesInputsAndProperties.left().value();
128 Component currInstanceOriginType = dataHolder.getCurrInstanceNode();
129 ActionStatus redeclareStatus = instanceInputsRedeclareHandler.redeclareComponentInputsForInstance(updatedContainerComponent, newInstanceId, currInstanceOriginType, origComponentInputs);
130 if (redeclareStatus != ActionStatus.OK) {
131 log.error("Failed to update component {} with merged inputs {}", newContainerComponentId, inputsToAddToContainer);
132 return Either.right(redeclareStatus);
135 return Either.left(inputsToAddToContainer);
138 private Either<Component, StorageOperationStatus> getComponentWithInstancesInputsAndProperties(String containerComponentId) {
139 ComponentParametersView filter = new ComponentParametersView(true);
140 filter.setIgnoreComponentInstances(false);
141 filter.setIgnoreComponentInstancesInputs(false);
142 filter.setIgnoreComponentInstancesProperties(false);
143 filter.setIgnoreArtifacts(false);
144 return toscaOperationFacade.getToscaElement(containerComponentId, filter);