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.dao.api.ActionStatus;
25 import org.openecomp.sdc.be.impl.ComponentsUtils;
26 import org.openecomp.sdc.be.model.*;
27 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
28 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
29 import org.openecomp.sdc.common.log.wrappers.Logger;
30 import org.openecomp.sdc.exception.ResponseFormat;
32 import java.util.ArrayList;
33 import java.util.List;
35 import static org.apache.commons.collections.CollectionUtils.isNotEmpty;
38 * Created by chaya on 9/20/2017.
40 @org.springframework.stereotype.Component("ComponentInstancePropsAndInputsMerge")
41 public class ComponentInstancePropsAndInputsMerge implements ComponentInstanceMergeInterface {
43 private static final Logger log = Logger.getLogger(ComponentInstancePropsAndInputsMerge.class);
45 private final ToscaOperationFacade toscaOperationFacade;
46 private final ComponentsUtils componentsUtils;
47 private final ComponentInstancePropertiesMergeBL componentInstancePropertiesMergeBL;
48 private final ComponentInstanceInputsMergeBL resourceInstanceInputsMergeBL;
49 private final ComponentInstanceInputsRedeclareHandler instanceInputsRedeclareHandler;
51 public ComponentInstancePropsAndInputsMerge(ToscaOperationFacade toscaOperationFacade, ComponentsUtils componentsUtils, ComponentInstancePropertiesMergeBL componentInstancePropertiesMergeBL, ComponentInstanceInputsMergeBL resourceInstanceInputsMergeBL, ComponentInstanceInputsRedeclareHandler instanceInputsRedeclareHandler) {
52 this.toscaOperationFacade = toscaOperationFacade;
53 this.componentsUtils = componentsUtils;
54 this.componentInstancePropertiesMergeBL = componentInstancePropertiesMergeBL;
55 this.resourceInstanceInputsMergeBL = resourceInstanceInputsMergeBL;
56 this.instanceInputsRedeclareHandler = instanceInputsRedeclareHandler;
60 public void saveDataBeforeMerge(DataForMergeHolder dataHolder, Component containerComponent, ComponentInstance currentResourceInstance, Component originComponent) {
61 dataHolder.setOrigComponentInstanceInputs(containerComponent.safeGetComponentInstanceInputsByName(currentResourceInstance.getName()));
62 dataHolder.setOrigComponentInstanceProperties(containerComponent.safeGetComponentInstanceProperties(currentResourceInstance.getUniqueId()));
63 dataHolder.setOrigComponentInputs(containerComponent.getInputs());
67 public Either<Component, ResponseFormat> mergeDataAfterCreate(User user, DataForMergeHolder dataHolder, Component updatedContainerComponent, String newInstanceId) {
68 Either<List<ComponentInstanceInput>, ActionStatus> instanceInputsEither = mergeComponentInstanceInputsIntoContainer(dataHolder, updatedContainerComponent, newInstanceId);
69 if (instanceInputsEither.isRight()) {
70 ActionStatus actionStatus = instanceInputsEither.right().value();
71 return Either.right(componentsUtils.getResponseFormat(actionStatus));
73 Either<List<ComponentInstanceProperty>, ActionStatus> instancePropsEither = mergeComponentInstancePropsIntoContainer(dataHolder, updatedContainerComponent, newInstanceId);
74 if (instancePropsEither.isRight()) {
75 ActionStatus actionStatus = instancePropsEither.right().value();
76 return Either.right(componentsUtils.getResponseFormat(actionStatus));
78 Either<List<InputDefinition>, ActionStatus> inputsEither = mergeComponentInputsIntoContainer(dataHolder, updatedContainerComponent.getUniqueId(), newInstanceId);
79 if (inputsEither.isRight()) {
80 ActionStatus actionStatus = inputsEither.right().value();
81 return Either.right(componentsUtils.getResponseFormat(actionStatus));
83 return Either.left(updatedContainerComponent);
86 private Either<List<ComponentInstanceProperty>, ActionStatus> mergeComponentInstancePropsIntoContainer(DataForMergeHolder dataHolder, Component updatedComponent, String instanceId) {
87 List<ComponentInstanceProperty> originComponentInstanceProps = dataHolder.getOrigComponentInstanceProperties();
88 List<InputDefinition> originComponentsInputs = dataHolder.getOrigComponentInputs();
89 List<ComponentInstanceProperty> newComponentInstancesProps = updatedComponent.safeGetComponentInstanceProperties(instanceId);
90 ActionStatus actionStatus = componentInstancePropertiesMergeBL.mergeComponentInstanceProperties(originComponentInstanceProps, originComponentsInputs, updatedComponent, instanceId);
92 if (actionStatus != ActionStatus.OK) {
93 log.error("Failed to update component {} with merged instance properties", updatedComponent.getUniqueId(), newComponentInstancesProps);
94 return Either.right(actionStatus);
96 return Either.left(newComponentInstancesProps);
99 private Either<List<ComponentInstanceInput>, ActionStatus> mergeComponentInstanceInputsIntoContainer(DataForMergeHolder dataHolder, Component updatedComponent, String instanceId) {
100 List<ComponentInstanceInput> originComponentInstanceInputs = dataHolder.getOrigComponentInstanceInputs();
101 List<InputDefinition> originComponentsInputs = dataHolder.getOrigComponentInputs();
102 List<ComponentInstanceInput> newComponentInstancesInputs = updatedComponent.safeGetComponentInstanceInput(instanceId);
103 ActionStatus actionStatus = resourceInstanceInputsMergeBL.mergeComponentInstanceInputs(originComponentInstanceInputs, originComponentsInputs, updatedComponent, instanceId);
104 if (actionStatus != ActionStatus.OK) {
105 log.error("Failed to update component {} with merged instance properties", updatedComponent.getUniqueId(), newComponentInstancesInputs);
106 return Either.right(actionStatus);
108 return Either.left(newComponentInstancesInputs);
111 private Either<List<InputDefinition>, ActionStatus> mergeComponentInputsIntoContainer(DataForMergeHolder dataHolder, String newContainerComponentId, String newInstanceId) {
112 List<InputDefinition> origComponentInputs = dataHolder.getOrigComponentInputs();
113 List<InputDefinition> inputsToAddToContainer = new ArrayList<>();
114 if (isNotEmpty(origComponentInputs)) {
115 // get instance inputs and properties after merge
116 Either<Component, StorageOperationStatus> componentWithInstancesInputsAndProperties = getComponentWithInstancesInputsAndProperties(newContainerComponentId);
117 if (componentWithInstancesInputsAndProperties.isRight()) {
118 log.error("Component %s was not found", newContainerComponentId);
119 return Either.right(componentsUtils.convertFromStorageResponse(componentWithInstancesInputsAndProperties.right().value()));
121 Component updatedContainerComponent = componentWithInstancesInputsAndProperties.left().value();
122 Component currInstanceOriginType = dataHolder.getCurrInstanceNode();
123 ActionStatus redeclareStatus = instanceInputsRedeclareHandler.redeclareComponentInputsForInstance(updatedContainerComponent, newInstanceId, currInstanceOriginType, origComponentInputs);
124 if (redeclareStatus != ActionStatus.OK) {
125 log.error("Failed to update component {} with merged inputs {}", newContainerComponentId, inputsToAddToContainer);
126 return Either.right(redeclareStatus);
129 return Either.left(inputsToAddToContainer);
132 private Either<Component, StorageOperationStatus> getComponentWithInstancesInputsAndProperties(String containerComponentId) {
133 ComponentParametersView filter = new ComponentParametersView(true);
134 filter.setIgnoreComponentInstances(false);
135 filter.setIgnoreComponentInstancesInputs(false);
136 filter.setIgnoreComponentInstancesProperties(false);
137 filter.setIgnoreArtifacts(false);
138 return toscaOperationFacade.getToscaElement(containerComponentId, filter);