01e36345e19dd501f8f102bb4cf5930f0da89208
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
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.asdctool.impl.migration.v1707.jsonmodel;
22
23
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;
44
45 import javax.annotation.Resource;
46 import java.util.ArrayList;
47 import java.util.HashMap;
48 import java.util.List;
49 import java.util.Map;
50 import java.util.Optional;
51 import java.util.stream.Collectors;
52
53
54 public class NodeTemplateMissingDataResolver <T extends Component> {
55         
56         private static Logger LOGGER = LoggerFactory.getLogger(NodeTemplateMissingDataResolver.class);
57         
58         @Resource(name = "tosca-element-lifecycle-operation")
59     private ToscaElementLifecycleOperation lifecycleOperation;
60         
61         @Resource(name = "topology-template-operation")
62     private TopologyTemplateOperation topologyTemplateOperation;
63         
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);
69                 }
70         }
71         
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())
77                 return;
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);      
87                 }
88         }
89
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;                
97                         }
98                         origCompMap.put(origCompUid, origCompEither.left().value());
99                 }
100                 return Either.left(origCompMap.get(origCompUid));
101         }
102         
103         protected boolean isProblematicGroup(GroupDefinition gr, String resourceName, Map<String, ArtifactDefinition> deploymentArtifacts) {
104                 List<String> artifacts = gr.getArtifacts();
105                 List<String> artifactsUuid = gr.getArtifactsUuid();
106
107                 if ((artifactsUuid == null || artifactsUuid.isEmpty()) && (artifacts == null || artifacts.isEmpty())) {
108                         LOGGER.debug("No groups in resource {} ", resourceName);
109                         return false;
110                 }
111                 if (artifacts.size() < artifactsUuid.size()) {
112                         LOGGER.debug(" artifacts.size() < artifactsUuid.size() group {} in resource {} ", gr.getName(), resourceName);
113                         return true;
114                 }
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);
117                         return true;
118                 }
119                 if (artifactsUuid.contains(null)) {
120                         LOGGER.debug(" artifactsUuid.contains(null) group {} in resource {} ", gr.getName(), resourceName);
121                         return true;
122                 }
123
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);
129                                 return true;
130                         }
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);
135                                         return true;
136                                 }
137                                 if (!artifactsUuid.contains(artifactDefinition.getArtifactUUID())) {
138                                         LOGGER.debug(" artifactsUuid.contains(artifactDefinition.getArtifactUUID() label {} group {} in resource {} ", artifactlabel, gr.getName(), resourceName);
139                                         return true;
140                                 }
141                         }
142                 }
143                 return false;
144         }
145
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();
150
151                 if ((artifactsUuid == null || artifactsUuid.isEmpty()) && (artifacts == null || artifacts.isEmpty())) {
152                         LOGGER.debug("No instance groups for instance {} in service {} ", instName, servicename);
153                         return false;
154                 }
155                 if (artifacts.size() < artifactsUuid.size()) {
156                         LOGGER.debug(" artifacts.size() < artifactsUuid.size() inst {} in service {} ", instName, servicename);
157                         return true;
158                 }
159                 if (artifacts.size() > 0 && (artifactsUuid == null || artifactsUuid.isEmpty())) {
160                         LOGGER.debug(" artifacts.size() > 0 && (artifactsUuid == null || artifactsUuid.isEmpty() inst {} in service {} ", instName, servicename);
161                         return true;
162                 }
163                 if (artifactsUuid.contains(null)) {
164                         LOGGER.debug(" artifactsUuid.contains(null) inst {} in service {} ", instName, servicename);
165                         return true;
166                 }
167
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);
173                                 return true;
174                         }
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);
179                                         return true;
180                                 }
181                                 if (!artifactsUuid.contains(artifactDefinition.getArtifactUUID())) {
182                                         LOGGER.debug(" artifactsUuid.contains(artifactDefinition.getArtifactUUID() label {} inst {} in service {} ", artifactlabel, instName, servicename);
183                                         return true;
184                                 }
185                         } else {
186                                 if (!instArtifactsUuid.contains(artifactDefinition.getArtifactUUID())) {
187                                         LOGGER.debug(" instArtifactsUuid.contains(artifactDefinition.getArtifactUUID() label {} inst {} in service {} ", artifactlabel, instName, servicename);
188                                         return true;
189                                 }
190                         }
191                 }
192                 return false;
193         }
194         
195         private String findArtifactLabelFromArtifactId(String artifactId) {
196                 String artifactLabel = "";
197
198                 int index = artifactId.lastIndexOf(".");
199                 if (index > 0 && index + 1 < artifactId.length())
200                         artifactLabel = artifactId.substring(index + 1);
201                 return artifactLabel;
202         }
203         
204         protected boolean fixVFGroups(Component component){
205                 boolean res = true;
206                 
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());
211                         return res;
212                 }       
213                                 
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);
231                                                         }
232
233                                                 }
234                                         }
235                                 }
236                         }
237                         
238                 }               
239                 
240                 return res;
241         }
242         
243         protected boolean fixVFGroupInstances(Component component, ComponentInstance instance){
244                 boolean res = true;
245                 
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());
250                         return res;
251                 }               
252                 for (GroupInstance group : groupInstances) {
253                         if (group.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE)) {
254                                 if (isProblematicGroupInstance(group, instance.getName(), component.getName(), deploymentArtifacts)) {
255
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<>());
258
259                                         group.setArtifacts(new ArrayList<>());
260                                         group.setArtifactsUuid(new ArrayList<>());
261                                         group.setGroupInstanceArtifacts(new ArrayList<>());
262                                         group.setGroupInstanceArtifactsUuid(new ArrayList<>());
263
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) {
273
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);
278                                                                 }
279                                                         } else {
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);
284                                                                 }
285                                                         }
286                                                 }
287                                         }
288
289                                 }
290                         }
291                 }
292                 
293                 return res;
294         }
295
296 }