cc1043900bfc9c0021940d30408650891e43c38f
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.be.components.merge.instance;
22
23 import fj.data.Either;
24 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic;
25 import org.openecomp.sdc.be.components.merge.heat.HeatEnvArtifactsMergeBusinessLogic;
26 import org.openecomp.sdc.be.impl.ComponentsUtils;
27 import org.openecomp.sdc.be.model.*;
28 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
29 import org.openecomp.sdc.common.log.wrappers.Logger;
30 import org.openecomp.sdc.exception.ResponseFormat;
31 import org.springframework.beans.factory.annotation.Autowired;
32
33 import java.util.List;
34 import java.util.Map;
35
36 /**
37  * Created by chaya on 9/20/2017.
38  */
39 @org.springframework.stereotype.Component("ComponentInstanceHeatEnvMerge")
40 public class ComponentInstanceHeatEnvMerge implements ComponentInstanceMergeInterface {
41
42     private static final Logger log = Logger.getLogger(ComponentInstanceHeatEnvMerge.class);
43
44     @Autowired
45     private ArtifactsBusinessLogic artifactsBusinessLogic;
46
47     @Autowired
48     private HeatEnvArtifactsMergeBusinessLogic heatEnvArtifactsMergeBusinessLogic;
49
50     @Autowired
51     protected ComponentsUtils componentsUtils;
52
53
54     @Override
55     public void saveDataBeforeMerge(DataForMergeHolder dataHolder, Component containerComponent, ComponentInstance currentResourceInstance, Component originComponent) {
56         dataHolder.setOrigComponentInstanceHeatEnvArtifacts(containerComponent.safeGetComponentInstanceHeatArtifacts(currentResourceInstance.getUniqueId()));
57     }
58
59     @Override
60     public Either<Component, ResponseFormat> mergeDataAfterCreate(User user, DataForMergeHolder dataHolder, Component updatedContainerComponent, String newInstanceId) {
61         List<ArtifactDefinition> origCompInstHeatEnvArtifacts = dataHolder.getOrigComponentInstanceHeatEnvArtifacts();
62         List<ArtifactDefinition> newCompInstHeatEnvArtifacts = updatedContainerComponent.safeGetComponentInstanceHeatArtifacts(newInstanceId);
63         List<ArtifactDefinition> artifactsToUpdate = heatEnvArtifactsMergeBusinessLogic.mergeInstanceHeatEnvArtifacts(origCompInstHeatEnvArtifacts, newCompInstHeatEnvArtifacts);
64
65         for (ArtifactDefinition artifactInfo : artifactsToUpdate) {
66             Map<String, Object> json = artifactsBusinessLogic.buildJsonForUpdateArtifact(artifactInfo, ArtifactGroupTypeEnum.DEPLOYMENT,  null);
67
68             Either<Either<ArtifactDefinition, Operation>, ResponseFormat> uploadArtifactToService = artifactsBusinessLogic.updateResourceInstanceArtifactNoContent(newInstanceId, updatedContainerComponent, user, json,
69                     artifactsBusinessLogic.new ArtifactOperationInfo(false, false, ArtifactsBusinessLogic.ArtifactOperationEnum.UPDATE), null);
70             if (uploadArtifactToService.isRight()) {
71                 log.error("Failed to update artifact {} on resourceInstance {}", artifactInfo.getArtifactLabel(), newInstanceId);
72                 return Either.right(uploadArtifactToService.right().value());
73             }
74         }
75         return Either.left(updatedContainerComponent);
76     }
77 }