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;
23 import java.io.BufferedWriter;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.io.OutputStreamWriter;
27 import java.io.Writer;
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.HashSet;
31 import java.util.List;
35 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
36 import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
37 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
38 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
39 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
40 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
41 import org.openecomp.sdc.be.datatypes.elements.GroupDataDefinition;
42 import org.openecomp.sdc.be.datatypes.elements.MapGroupsDataDefinition;
43 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
44 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
45 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
46 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
47 import org.openecomp.sdc.be.model.ArtifactDefinition;
48 import org.openecomp.sdc.be.model.Component;
49 import org.openecomp.sdc.be.model.ComponentInstance;
50 import org.openecomp.sdc.be.model.ComponentParametersView;
51 import org.openecomp.sdc.be.model.DistributionStatusEnum;
52 import org.openecomp.sdc.be.model.GroupDefinition;
53 import org.openecomp.sdc.be.model.GroupInstance;
54 import org.openecomp.sdc.be.model.LifecycleStateEnum;
55 import org.openecomp.sdc.be.model.Resource;
56 import org.openecomp.sdc.be.model.Service;
57 import org.openecomp.sdc.be.model.jsontitan.datamodel.TopologyTemplate;
58 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
59 import org.openecomp.sdc.be.model.jsontitan.utils.ModelConverter;
60 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
61 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
62 import org.openecomp.sdc.common.api.Constants;
63 import org.slf4j.Logger;
64 import org.slf4j.LoggerFactory;
65 import org.springframework.beans.factory.annotation.Autowired;
67 import fj.data.Either;
69 @org.springframework.stereotype.Component("migration1707UuidFix")
70 public class Migration1707ArtifactUuidFix {
73 private TitanDao titanDao;
76 private ToscaOperationFacade toscaOperationFacade;
78 private static Logger log = LoggerFactory.getLogger(Migration1707ArtifactUuidFix.class.getName());
80 public boolean migrate(String fixComponent, String runMode) {
81 List<Resource> vfLst = new ArrayList<>();
82 List<Service> serviceList = new ArrayList<>();
84 long time = System.currentTimeMillis();
86 if (fixComponent.equals("vf_only")) {
87 if (fetchFaultVf(fixComponent, vfLst, time) == false) {
91 if (fetchServices(fixComponent, serviceList, time) == false) {
95 if (runMode.equals("service_vf") || runMode.equals("fix")) {
96 log.info("Mode {}. Find problem VFs", runMode);
97 if (fetchVf(serviceList, vfLst, time) == false) {
98 log.info("Mode {}. Find problem VFs finished with failure", runMode);
101 log.info("Mode {}. Find problem VFs finished with success", runMode);
103 if (runMode.equals("fix") || runMode.equals("fix_only_services")) {
104 log.info("Mode {}. Start fix", runMode);
105 if (fix(vfLst, serviceList) == false) {
106 log.info("Mode {}. Fix finished withh failure", runMode);
109 log.info("Mode {}. Fix finished withh success", runMode);
115 private boolean fetchFaultVf(String fixComponent, List<Resource> vfLst, long time) {
116 log.info("Find fault VF ");
117 Writer writer = null;
119 String fileName = "fault_" + time + ".csv";
120 writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "utf-8"));
121 writer.write("vf name, vf id, state, version\n");
123 Map<GraphPropertyEnum, Object> hasProps = new HashMap<>();
124 hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
125 hasProps.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.VF.name());
127 Map<GraphPropertyEnum, Object> hasNotProps = new HashMap<>();
128 hasNotProps.put(GraphPropertyEnum.IS_DELETED, true);
129 log.info("Try to fetch resources with properties {} and not {}", hasProps, hasNotProps);
131 Either<List<GraphVertex>, TitanOperationStatus> servicesByCriteria = titanDao.getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, hasProps, hasNotProps, JsonParseFlagEnum.ParseAll);
132 if (servicesByCriteria.isRight()) {
133 log.info("Failed to fetch resources {}", servicesByCriteria.right().value());
136 List<GraphVertex> resources = servicesByCriteria.left().value();
137 for (GraphVertex gv : resources) {
138 ComponentParametersView filter = new ComponentParametersView(true);
139 filter.setIgnoreComponentInstances(false);
140 filter.setIgnoreArtifacts(false);
141 filter.setIgnoreGroups(false);
143 Either<Resource, StorageOperationStatus> toscaElement = toscaOperationFacade.getToscaElement(gv.getUniqueId());
144 if (toscaElement.isRight()) {
145 log.info("Failed to fetch resources {} {}", gv.getUniqueId(), toscaElement.right().value());
149 Resource resource = toscaElement.left().value();
150 String resourceName = resource.getName();
151 Map<String, ArtifactDefinition> deploymentArtifacts = resource.getDeploymentArtifacts();
152 List<GroupDefinition> groups = resource.getGroups();
153 if (groups == null || groups.isEmpty()) {
154 log.info("No groups for resource {} id {} ", resourceName, gv.getUniqueId());
157 boolean isProblematic = false;
158 for (GroupDefinition gr : groups) {
159 if (gr.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE)) {
160 if (isProblematicGroup(gr, resourceName, deploymentArtifacts)) {
161 isProblematic = true;
168 writeModuleResultToFile(writer, resource, null);
174 } catch (Exception e) {
175 log.info("Failed to fetch vf resources ", e);
182 } catch (Exception ex) {
188 private boolean fetchVf(List<Service> serviceList, List<Resource> vfLst, long time) {
189 log.info("Find problem VF ");
190 if (serviceList.isEmpty()) {
191 log.info("No services as input");
194 Writer writer = null;
196 String fileName = "problemVf_" + time + ".csv";
197 writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "utf-8"));
198 writer.write("vf name, vf id, state, version, example service name\n");
199 Set<String> vfIds = new HashSet<>();
200 for (Service service : serviceList) {
201 List<ComponentInstance> componentInstances = service.getComponentInstances();
202 for (ComponentInstance ci : componentInstances) {
203 if (!vfIds.contains(ci.getComponentUid())) {
204 vfIds.add(ci.getComponentUid());
205 Either<Resource, StorageOperationStatus> toscaElement = toscaOperationFacade.getToscaElement(ci.getComponentUid());
206 if (toscaElement.isRight()) {
207 log.info("Failed to fetch resource {} {}", ci.getComponentUid(), toscaElement.right().value());
210 Resource resource = toscaElement.left().value();
212 writeModuleResultToFile(writer, resource, service);
218 log.info("output file with list of Vf : {}", fileName);
219 } catch (Exception e) {
220 log.info("Failed to fetch services ", e);
227 } catch (Exception ex) {
233 private boolean fetchServices(String fixServices, List<Service> serviceList, long time) {
234 log.info("Find problem Services {}", fixServices);
235 Writer writer = null;
238 String fileName = "problemService_" + time + ".csv";
239 writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "utf-8"));
240 writer.write("service name, service id, state, version\n");
242 Map<GraphPropertyEnum, Object> hasProps = new HashMap<>();
243 hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
244 if (fixServices.equals("distributed_only")) {
245 hasProps.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
246 hasProps.put(GraphPropertyEnum.DISTRIBUTION_STATUS, DistributionStatusEnum.DISTRIBUTED.name());
249 Map<GraphPropertyEnum, Object> hasNotProps = new HashMap<>();
250 hasNotProps.put(GraphPropertyEnum.IS_DELETED, true);
251 log.info("Try to fetch services with properties {} and not {}", hasProps, hasNotProps);
253 Either<List<GraphVertex>, TitanOperationStatus> servicesByCriteria = titanDao.getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, hasProps, hasNotProps, JsonParseFlagEnum.ParseAll);
254 if (servicesByCriteria.isRight()) {
255 log.info("Failed to fetch services {}", servicesByCriteria.right().value());
258 List<GraphVertex> services = servicesByCriteria.left().value();
259 for (GraphVertex gv : services) {
260 ComponentParametersView filter = new ComponentParametersView(true);
261 filter.setIgnoreComponentInstances(false);
262 filter.setIgnoreArtifacts(false);
263 filter.setIgnoreGroups(false);
265 Either<Service, StorageOperationStatus> toscaElement = toscaOperationFacade.getToscaElement(gv.getUniqueId());
266 if (toscaElement.isRight()) {
267 log.info("Failed to fetch service {} {}", gv.getUniqueId(), toscaElement.right().value());
270 Service service = toscaElement.left().value();
271 List<ComponentInstance> componentInstances = service.getComponentInstances();
272 boolean isProblematic = false;
273 if (componentInstances == null) {
274 log.info("No instances for service {} ", gv.getUniqueId());
277 String serviceName = (String) gv.getMetadataProperty(GraphPropertyEnum.NAME);
279 for (ComponentInstance ci : componentInstances) {
280 Map<String, ArtifactDefinition> deploymentArtifacts = ci.getDeploymentArtifacts();
281 List<GroupInstance> groupInstances = ci.getGroupInstances();
282 if (groupInstances == null || groupInstances.isEmpty()) {
283 log.info("No instance groups for instance {} in service {} id {} ", ci.getName(), serviceName, gv.getUniqueId());
287 for (GroupInstance gi : groupInstances) {
288 if (gi.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE)) {
289 if (isProblematicGroupInstance(gi, ci.getName(), serviceName, deploymentArtifacts)) {
290 isProblematic = true;
296 serviceList.add(service);
297 writeModuleResultToFile(writer, service, null);
304 log.info("output file with list of services : {}", fileName);
305 } catch (Exception e) {
306 log.info("Failed to fetch services ", e);
313 } catch (Exception ex) {
319 private boolean isProblematicGroup(GroupDefinition gr, String resourceName, Map<String, ArtifactDefinition> deploymentArtifacts) {
320 List<String> artifacts = gr.getArtifacts();
321 List<String> artifactsUuid = gr.getArtifactsUuid();
323 if ((artifactsUuid == null || artifactsUuid.isEmpty()) && (artifacts == null || artifacts.isEmpty())) {
324 log.info("No groups in resource {} ", resourceName);
327 if (artifacts.size() < artifactsUuid.size()) {
328 log.info(" artifacts.size() < artifactsUuid.size() group {} in resource {} ", gr.getName(), resourceName);
331 if (artifacts.size() > 0 && (artifactsUuid == null || artifactsUuid.isEmpty())) {
332 log.info(" artifacts.size() > 0 && (artifactsUuid == null || artifactsUuid.isEmpty() group {} in resource {} ", gr.getName(), resourceName);
335 if (artifactsUuid.contains(null)) {
336 log.info(" artifactsUuid.contains(null) group {} in resource {} ", gr.getName(), resourceName);
340 for (String artifactId : artifacts) {
341 String artifactlabel = findArtifactLabelFromArtifactId(artifactId);
342 ArtifactDefinition artifactDefinition = deploymentArtifacts.get(artifactlabel);
343 if (artifactDefinition == null) {
344 log.info(" artifactDefinition == null label {} group {} in resource {} ", artifactlabel, gr.getName(), resourceName);
347 ArtifactTypeEnum artifactType = ArtifactTypeEnum.findType(artifactDefinition.getArtifactType());
348 if (artifactType != ArtifactTypeEnum.HEAT_ENV) {
349 if (!artifactId.equals(artifactDefinition.getUniqueId())) {
350 log.info(" !artifactId.equals(artifactDefinition.getUniqueId() artifact {} artId {} group {} in resource {} ", artifactlabel, artifactId, gr.getName(), resourceName);
353 if (!artifactsUuid.contains(artifactDefinition.getArtifactUUID())) {
354 log.info(" artifactsUuid.contains(artifactDefinition.getArtifactUUID() label {} group {} in resource {} ", artifactlabel, gr.getName(), resourceName);
362 private boolean isProblematicGroupInstance(GroupInstance gi, String instName, String servicename, Map<String, ArtifactDefinition> deploymentArtifacts) {
363 List<String> artifacts = gi.getArtifacts();
364 List<String> artifactsUuid = gi.getArtifactsUuid();
365 List<String> instArtifactsUuid = gi.getGroupInstanceArtifactsUuid();
367 if ((artifactsUuid == null || artifactsUuid.isEmpty()) && (artifacts == null || artifacts.isEmpty())) {
368 log.info("No instance groups for instance {} in service {} ", instName, servicename);
371 if (artifacts.size() < artifactsUuid.size()) {
372 log.info(" artifacts.size() < artifactsUuid.size() inst {} in service {} ", instName, servicename);
375 if (artifacts.size() > 0 && (artifactsUuid == null || artifactsUuid.isEmpty())) {
376 log.info(" artifacts.size() > 0 && (artifactsUuid == null || artifactsUuid.isEmpty() inst {} in service {} ", instName, servicename);
379 if (artifactsUuid.contains(null)) {
380 log.info(" artifactsUuid.contains(null) inst {} in service {} ", instName, servicename);
384 for (String artifactId : artifacts) {
385 String artifactlabel = findArtifactLabelFromArtifactId(artifactId);
386 ArtifactDefinition artifactDefinition = deploymentArtifacts.get(artifactlabel);
387 if (artifactDefinition == null) {
388 log.info(" artifactDefinition == null label {} inst {} in service {} ", artifactlabel, instName, servicename);
391 ArtifactTypeEnum artifactType = ArtifactTypeEnum.findType(artifactDefinition.getArtifactType());
392 if (artifactType != ArtifactTypeEnum.HEAT_ENV) {
393 if (!artifactId.equals(artifactDefinition.getUniqueId())) {
394 log.info(" !artifactId.equals(artifactDefinition.getUniqueId() artifact {} artId {} inst {} in service {} ", artifactlabel, artifactId, instName, servicename);
397 if (!artifactsUuid.contains(artifactDefinition.getArtifactUUID())) {
398 log.info(" artifactsUuid.contains(artifactDefinition.getArtifactUUID() label {} inst {} in service {} ", artifactlabel, instName, servicename);
402 if (!instArtifactsUuid.contains(artifactDefinition.getArtifactUUID())) {
403 log.info(" instArtifactsUuid.contains(artifactDefinition.getArtifactUUID() label {} inst {} in service {} ", artifactlabel, instName, servicename);
411 private boolean fix(List<Resource> vfLst, List<Service> serviceList) {
414 if (vfLst != null && !vfLst.isEmpty()) {
417 for (Component component : vfLst) {
418 TopologyTemplate topologyTemplate = ModelConverter.convertToToscaElement(component);
419 Map<String, GroupDataDefinition> groups = topologyTemplate.getGroups();
420 res = fixDataOnGraph(component.getUniqueId(), VertexTypeEnum.GROUPS, EdgeLabelEnum.GROUPS, groups);
425 if (res == true && serviceList != null && !serviceList.isEmpty()) {
426 res = fixServices(serviceList);
428 for (Component component : serviceList) {
429 TopologyTemplate topologyTemplate = ModelConverter.convertToToscaElement(component);
430 Map<String, MapGroupsDataDefinition> groups = topologyTemplate.getInstGroups();
431 res = fixDataOnGraph(component.getUniqueId(), VertexTypeEnum.INST_GROUPS, EdgeLabelEnum.INST_GROUPS, groups);
439 private <T extends ToscaDataDefinition> boolean fixDataOnGraph(String componentId, VertexTypeEnum vertexTypeEnum, EdgeLabelEnum edgeLabelEnum, Map<String, T> groups) {
441 Either<GraphVertex, TitanOperationStatus> getResponse = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
442 if (getResponse.isRight()) {
443 log.debug("Couldn't fetch component unique id {}, error: {}", componentId, getResponse.right().value());
448 GraphVertex componentVertex = getResponse.left().value();
450 GraphVertex toscaDataVertex = null;
451 Either<GraphVertex, TitanOperationStatus> groupVertexEither = titanDao.getChildVertex(componentVertex, edgeLabelEnum, JsonParseFlagEnum.ParseJson);
452 if (groupVertexEither.isRight()) {
454 log.debug("failed to get child {} vertex for component unique id {}, error: {}", edgeLabelEnum, componentId, groupVertexEither.right().value());
457 toscaDataVertex = groupVertexEither.left().value();
458 toscaDataVertex.setJson(groups);
459 Either<GraphVertex, TitanOperationStatus> updatevertexEither = titanDao.updateVertex(toscaDataVertex);
460 if (updatevertexEither.isRight()) {
461 log.debug("failed to update vertex for component unique id {}, error: {}", componentId, updatevertexEither.right().value());
473 private boolean fixServices(List<Service> serviceList) {
474 for (Service service : serviceList) {
475 log.debug("Migration1707ArtifactUuidFix fix service: id {}, name {} ", service.getUniqueId(), service.getName());
476 List<ComponentInstance> instances = service.getComponentInstances();
477 for (ComponentInstance instance : instances) {
478 Map<String, ArtifactDefinition> artifactsMap = instance.getDeploymentArtifacts();
479 List<GroupInstance> groupsList = instance.getGroupInstances();
480 if (groupsList != null && artifactsMap != null) {
481 for (GroupInstance group : groupsList) {
482 if (group.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE)) {
483 log.debug("Migration1707ArtifactUuidFix fix group: resource id {}, group name {} ", service.getUniqueId(), group.getName());
484 List<String> groupArtifacts = new ArrayList<String>(group.getArtifacts());
486 group.getArtifacts().clear();
487 group.getArtifactsUuid().clear();
488 group.getGroupInstanceArtifacts().clear();
489 group.getGroupInstanceArtifactsUuid().clear();
491 for (String artifactId : groupArtifacts) {
492 String artifactlabel = findArtifactLabelFromArtifactId(artifactId);
493 log.debug("Migration1707ArtifactUuidFix fix group: group name {} artifactId for fix {} artifactlabel {} ", group.getName(), artifactId, artifactlabel);
494 if (!artifactlabel.isEmpty() && artifactsMap.containsKey(artifactlabel)) {
495 ArtifactDefinition artifact = artifactsMap.get(artifactlabel);
496 ArtifactTypeEnum artifactType = ArtifactTypeEnum.findType(artifact.getArtifactType());
497 String correctArtifactId = artifact.getUniqueId();
498 String correctArtifactUUID = artifact.getArtifactUUID();
499 if (artifactType != ArtifactTypeEnum.HEAT_ENV) {
501 log.debug("Migration1707ArtifactUuidFix fix group: group name {} correct artifactId {} artifactUUID {} ", group.getName(), correctArtifactId, correctArtifactUUID);
502 group.getArtifacts().add(correctArtifactId);
503 if (correctArtifactUUID != null && !correctArtifactUUID.isEmpty()) {
504 group.getArtifactsUuid().add(correctArtifactUUID);
507 log.debug("Migration1707ArtifactUuidFix fix group: group name {} correct artifactId {} artifactUUID {} ", group.getName(), correctArtifactId, correctArtifactUUID);
508 group.getGroupInstanceArtifacts().add(correctArtifactId);
509 if (correctArtifactUUID != null && !correctArtifactUUID.isEmpty()) {
510 group.getGroupInstanceArtifactsUuid().add(correctArtifactUUID);
525 private boolean fixVf(List<Resource> vfLst) {
526 for (Resource resource : vfLst) {
527 log.debug("Migration1707ArtifactUuidFix fix resource: id {}, name {} ", resource.getUniqueId(), resource.getName());
528 Map<String, ArtifactDefinition> artifactsMap = resource.getDeploymentArtifacts();
529 List<GroupDefinition> groupsList = resource.getGroups();
530 if (groupsList != null && artifactsMap != null) {
531 for (GroupDefinition group : groupsList) {
532 if (group.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE) && group.getArtifacts() != null) {
533 log.debug("Migration1707ArtifactUuidFix fix group: resource id {}, group name {} ", resource.getUniqueId(), group.getName());
534 List<String> groupArtifacts = new ArrayList<String>(group.getArtifacts());
535 group.getArtifacts().clear();
536 group.getArtifactsUuid().clear();
537 for (String artifactId : groupArtifacts) {
538 String artifactlabel = findArtifactLabelFromArtifactId(artifactId);
539 log.debug("Migration1707ArtifactUuidFix fix group: group name {} artifactId for fix {} artifactlabel {} ", group.getName(), artifactId, artifactlabel);
540 if (!artifactlabel.isEmpty() && artifactsMap.containsKey(artifactlabel)) {
541 ArtifactDefinition artifact = artifactsMap.get(artifactlabel);
542 String correctArtifactId = artifact.getUniqueId();
543 String correctArtifactUUID = artifact.getArtifactUUID();
544 log.debug("Migration1707ArtifactUuidFix fix group: group name {} correct artifactId {} artifactUUID {} ", group.getName(), correctArtifactId, correctArtifactUUID);
545 group.getArtifacts().add(correctArtifactId);
546 if (correctArtifactUUID != null && !correctArtifactUUID.isEmpty()) {
547 group.getArtifactsUuid().add(correctArtifactUUID);
561 private String findArtifactLabelFromArtifactId(String artifactId) {
562 String artifactLabel = "";
564 int index = artifactId.lastIndexOf(".");
565 if (index > 0 && index + 1 < artifactId.length())
566 artifactLabel = artifactId.substring(index + 1);
567 return artifactLabel;
570 private void writeModuleResultToFile(Writer writer, org.openecomp.sdc.be.model.Component component, Service service) {
572 // "service name, service id, state, version
573 StringBuffer sb = new StringBuffer(component.getName());
574 sb.append(",").append(component.getUniqueId()).append(",").append(component.getLifecycleState()).append(",").append(component.getVersion());
575 if (service != null) {
576 sb.append(",").append(service.getName());
579 writer.write(sb.toString());
580 } catch (IOException e) {
581 // TODO Auto-generated catch block