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.asdctool.impl.migration.v1707.jsonmodel;
24 import fj.data.Either;
25 import org.openecomp.sdc.be.datatypes.elements.ComponentInstanceDataDefinition;
26 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
27 import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
28 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
29 import org.openecomp.sdc.be.model.ArtifactDefinition;
30 import org.openecomp.sdc.be.model.Component;
31 import org.openecomp.sdc.be.model.ComponentInstance;
32 import org.openecomp.sdc.be.model.ComponentInstanceInput;
33 import org.openecomp.sdc.be.model.GroupDefinition;
34 import org.openecomp.sdc.be.model.GroupInstance;
35 import org.openecomp.sdc.be.model.jsontitan.datamodel.TopologyTemplate;
36 import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElement;
37 import org.openecomp.sdc.be.model.jsontitan.operations.TopologyTemplateOperation;
38 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaElementLifecycleOperation;
39 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
40 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
41 import org.openecomp.sdc.common.api.Constants;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 import javax.annotation.Resource;
46 import java.util.ArrayList;
47 import java.util.HashMap;
48 import java.util.List;
50 import java.util.Optional;
51 import java.util.stream.Collectors;
54 public class NodeTemplateMissingDataResolver <T extends Component> {
56 private static Logger LOGGER = LoggerFactory.getLogger(NodeTemplateMissingDataResolver.class);
58 @Resource(name = "tosca-element-lifecycle-operation")
59 private ToscaElementLifecycleOperation lifecycleOperation;
61 @Resource(name = "topology-template-operation")
62 private TopologyTemplateOperation topologyTemplateOperation;
64 public void resolveNodeTemplateInfo(ComponentInstanceDataDefinition vfInst, Map<String, ToscaElement> origCompMap, T component) {
65 lifecycleOperation.resolveToscaComponentName(vfInst, origCompMap);
66 if(OriginTypeEnum.VF == vfInst.getOriginType()) {
67 Map<String, List<ComponentInstanceInput>> componentInstancesInputs = Optional.ofNullable(component.getComponentInstancesInputs()).orElse(new HashMap<>());
68 collectVFInstanceInputs(componentInstancesInputs, origCompMap, vfInst);
72 private void collectVFInstanceInputs(Map<String, List<ComponentInstanceInput>> instInputs, Map<String, ToscaElement> origCompMap, ComponentInstanceDataDefinition vfInst) {
73 String ciUid = vfInst.getUniqueId();
74 String origCompUid = vfInst.getComponentUid();
75 Either<ToscaElement, StorageOperationStatus> origComp = fetchToscaElement(origCompMap, vfInst, origCompUid);
76 if(origComp.isRight())
78 Map<String, PropertyDataDefinition> origVFInputs = ((TopologyTemplate)origComp.left().value()).getInputs();
79 if (origVFInputs != null && !origVFInputs.isEmpty()) {
80 Map<String, ComponentInstanceInput> collectedVFInputs = origVFInputs.values().stream()
81 .collect(Collectors.toMap(PropertyDataDefinition::getName, ComponentInstanceInput::new));
82 List<ComponentInstanceInput> instInputList = instInputs.get(ciUid);
83 Map<String, ComponentInstanceInput> existingInstInputs = ToscaDataDefinition.listToMapByName(instInputList);
84 collectedVFInputs.putAll(existingInstInputs);
85 List<ComponentInstanceInput> mergedList = new ArrayList<>(collectedVFInputs.values());
86 instInputs.put(ciUid, mergedList);
90 private Either<ToscaElement, StorageOperationStatus> fetchToscaElement(Map<String, ToscaElement> origCompMap, ComponentInstanceDataDefinition vfInst, String origCompUid) {
91 Either<ToscaElement, StorageOperationStatus> origCompEither;
92 if (!origCompMap.containsKey(origCompUid)) {
93 origCompEither = topologyTemplateOperation.getToscaElement(origCompUid);
94 if (origCompEither.isRight()) {
95 LOGGER.error("failed to fetch Tosca element {} with id {}", vfInst.getComponentName(), origCompUid);
96 return origCompEither;
98 origCompMap.put(origCompUid, origCompEither.left().value());
100 return Either.left(origCompMap.get(origCompUid));
103 protected boolean isProblematicGroup(GroupDefinition gr, String resourceName, Map<String, ArtifactDefinition> deploymentArtifacts) {
104 List<String> artifacts = gr.getArtifacts();
105 List<String> artifactsUuid = gr.getArtifactsUuid();
107 if ((artifactsUuid == null || artifactsUuid.isEmpty()) && (artifacts == null || artifacts.isEmpty())) {
108 LOGGER.debug("No groups in resource {} ", resourceName);
111 if (artifacts.size() < artifactsUuid.size()) {
112 LOGGER.debug(" artifacts.size() < artifactsUuid.size() group {} in resource {} ", gr.getName(), resourceName);
115 if (artifacts.size() > 0 && (artifactsUuid == null || artifactsUuid.isEmpty())) {
116 LOGGER.debug(" artifacts.size() > 0 && (artifactsUuid == null || artifactsUuid.isEmpty() group {} in resource {} ", gr.getName(), resourceName);
119 if (artifactsUuid.contains(null)) {
120 LOGGER.debug(" artifactsUuid.contains(null) group {} in resource {} ", gr.getName(), resourceName);
124 for (String artifactId : artifacts) {
125 String artifactlabel = findArtifactLabelFromArtifactId(artifactId);
126 ArtifactDefinition artifactDefinition = deploymentArtifacts.get(artifactlabel);
127 if (artifactDefinition == null) {
128 LOGGER.debug(" artifactDefinition == null label {} group {} in resource {} ", artifactlabel, gr.getName(), resourceName);
131 ArtifactTypeEnum artifactType = ArtifactTypeEnum.findType(artifactDefinition.getArtifactType());
132 if (artifactType != ArtifactTypeEnum.HEAT_ENV) {
133 if (!artifactId.equals(artifactDefinition.getUniqueId())) {
134 LOGGER.debug(" !artifactId.equals(artifactDefinition.getUniqueId() artifact {} artId {} group {} in resource {} ", artifactlabel, artifactId, gr.getName(), resourceName);
137 if (!artifactsUuid.contains(artifactDefinition.getArtifactUUID())) {
138 LOGGER.debug(" artifactsUuid.contains(artifactDefinition.getArtifactUUID() label {} group {} in resource {} ", artifactlabel, gr.getName(), resourceName);
146 protected boolean isProblematicGroupInstance(GroupInstance gi, String instName, String servicename, Map<String, ArtifactDefinition> deploymentArtifacts) {
147 List<String> artifacts = gi.getArtifacts();
148 List<String> artifactsUuid = gi.getArtifactsUuid();
149 List<String> instArtifactsUuid = gi.getGroupInstanceArtifactsUuid();
151 if ((artifactsUuid == null || artifactsUuid.isEmpty()) && (artifacts == null || artifacts.isEmpty())) {
152 LOGGER.debug("No instance groups for instance {} in service {} ", instName, servicename);
155 if (artifacts.size() < artifactsUuid.size()) {
156 LOGGER.debug(" artifacts.size() < artifactsUuid.size() inst {} in service {} ", instName, servicename);
159 if (artifacts.size() > 0 && (artifactsUuid == null || artifactsUuid.isEmpty())) {
160 LOGGER.debug(" artifacts.size() > 0 && (artifactsUuid == null || artifactsUuid.isEmpty() inst {} in service {} ", instName, servicename);
163 if (artifactsUuid.contains(null)) {
164 LOGGER.debug(" artifactsUuid.contains(null) inst {} in service {} ", instName, servicename);
168 for (String artifactId : artifacts) {
169 String artifactlabel = findArtifactLabelFromArtifactId(artifactId);
170 ArtifactDefinition artifactDefinition = deploymentArtifacts.get(artifactlabel);
171 if (artifactDefinition == null) {
172 LOGGER.debug(" artifactDefinition == null label {} inst {} in service {} ", artifactlabel, instName, servicename);
175 ArtifactTypeEnum artifactType = ArtifactTypeEnum.findType(artifactDefinition.getArtifactType());
176 if (artifactType != ArtifactTypeEnum.HEAT_ENV) {
177 if (!artifactId.equals(artifactDefinition.getUniqueId())) {
178 LOGGER.debug(" !artifactId.equals(artifactDefinition.getUniqueId() artifact {} artId {} inst {} in service {} ", artifactlabel, artifactId, instName, servicename);
181 if (!artifactsUuid.contains(artifactDefinition.getArtifactUUID())) {
182 LOGGER.debug(" artifactsUuid.contains(artifactDefinition.getArtifactUUID() label {} inst {} in service {} ", artifactlabel, instName, servicename);
186 if (!instArtifactsUuid.contains(artifactDefinition.getArtifactUUID())) {
187 LOGGER.debug(" instArtifactsUuid.contains(artifactDefinition.getArtifactUUID() label {} inst {} in service {} ", artifactlabel, instName, servicename);
195 private String findArtifactLabelFromArtifactId(String artifactId) {
196 String artifactLabel = "";
198 int index = artifactId.lastIndexOf(".");
199 if (index > 0 && index + 1 < artifactId.length())
200 artifactLabel = artifactId.substring(index + 1);
201 return artifactLabel;
204 protected boolean fixVFGroups(Component component){
207 Map<String, ArtifactDefinition> deploymentArtifacts = component.getDeploymentArtifacts();
208 List<GroupDefinition> groups = component.getGroups();
209 if (groups == null || groups.isEmpty()) {
210 LOGGER.debug("No groups in component {} id {} ", component.getName(), component.getUniqueId());
214 for (GroupDefinition group : groups) {
215 if (group.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE) && deploymentArtifacts != null) {
216 if (isProblematicGroup(group, component.getName(), deploymentArtifacts)) {
217 List<String> groupArtifacts = new ArrayList<String>(group.getArtifacts());
218 group.getArtifacts().clear();
219 group.getArtifactsUuid().clear();
220 for (String artifactId : groupArtifacts) {
221 String artifactlabel = findArtifactLabelFromArtifactId(artifactId);
222 LOGGER.debug("fix group: group name {} artifactId for fix {} artifactlabel {} ", group.getName(), artifactId, artifactlabel);
223 if (!artifactlabel.isEmpty() && deploymentArtifacts.containsKey(artifactlabel)) {
224 ArtifactDefinition artifact = deploymentArtifacts.get(artifactlabel);
225 String correctArtifactId = artifact.getUniqueId();
226 String correctArtifactUUID = artifact.getArtifactUUID();
227 LOGGER.debug(" fix group: group name {} correct artifactId {} artifactUUID {} ", group.getName(), correctArtifactId, correctArtifactUUID);
228 group.getArtifacts().add(correctArtifactId);
229 if (correctArtifactUUID != null && !correctArtifactUUID.isEmpty()) {
230 group.getArtifactsUuid().add(correctArtifactUUID);
243 protected boolean fixVFGroupInstances(Component component, ComponentInstance instance){
246 Map<String, ArtifactDefinition> deploymentArtifacts = instance.getDeploymentArtifacts();
247 List<GroupInstance> groupInstances = instance.getGroupInstances();
248 if (groupInstances == null || groupInstances.isEmpty()) {
249 LOGGER.debug("No instance groups for instance {} in service {} id {} ", instance.getName(), component.getName(), component.getUniqueId());
252 for (GroupInstance group : groupInstances) {
253 if (group.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE)) {
254 if (isProblematicGroupInstance(group, instance.getName(), component.getName(), deploymentArtifacts)) {
256 LOGGER.debug("Migration1707ArtifactUuidFix fix group: resource id {}, group name {} ", component.getUniqueId(), group.getName());
257 List<String> groupArtifacts = Optional.ofNullable(group.getArtifacts()).orElse(new ArrayList<>());
259 group.setArtifacts(new ArrayList<>());
260 group.setArtifactsUuid(new ArrayList<>());
261 group.setGroupInstanceArtifacts(new ArrayList<>());
262 group.setGroupInstanceArtifactsUuid(new ArrayList<>());
264 for (String artifactId : groupArtifacts) {
265 String artifactlabel = findArtifactLabelFromArtifactId(artifactId);
266 LOGGER.debug("Migration1707ArtifactUuidFix fix group: group name {} artifactId for fix {} artifactlabel {} ", group.getName(), artifactId, artifactlabel);
267 if (!artifactlabel.isEmpty() && deploymentArtifacts.containsKey(artifactlabel)) {
268 ArtifactDefinition artifact = deploymentArtifacts.get(artifactlabel);
269 ArtifactTypeEnum artifactType = ArtifactTypeEnum.findType(artifact.getArtifactType());
270 String correctArtifactId = artifact.getUniqueId();
271 String correctArtifactUUID = artifact.getArtifactUUID();
272 if (artifactType != ArtifactTypeEnum.HEAT_ENV) {
274 LOGGER.debug("Migration1707ArtifactUuidFix fix group: group name {} correct artifactId {} artifactUUID {} ", group.getName(), correctArtifactId, correctArtifactUUID);
275 group.getArtifacts().add(correctArtifactId);
276 if (correctArtifactUUID != null && !correctArtifactUUID.isEmpty()) {
277 group.getArtifactsUuid().add(correctArtifactUUID);
280 LOGGER.debug("Migration1707ArtifactUuidFix fix group: group name {} correct artifactId {} artifactUUID {} ", group.getName(), correctArtifactId, correctArtifactUUID);
281 group.getGroupInstanceArtifacts().add(correctArtifactId);
282 if (correctArtifactUUID != null && !correctArtifactUUID.isEmpty()) {
283 group.getGroupInstanceArtifactsUuid().add(correctArtifactUUID);