[SDC-29] rebase continue work to align source
[sdc.git] / asdctool / src / main / java / org / openecomp / sdc / asdctool / impl / migration / v1707 / jsonmodel / ServicesMigration.java
1 package org.openecomp.sdc.asdctool.impl.migration.v1707.jsonmodel;
2
3 import fj.data.Either;
4 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
5 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
6 import org.openecomp.sdc.be.model.Service;
7 import org.openecomp.sdc.be.model.operations.api.IServiceOperation;
8 import org.openecomp.sdc.be.model.operations.migration.MigrationMalformedDataLogger;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11
12 import javax.annotation.Resource;
13 import java.util.ArrayList;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.stream.Collectors;
17
18 public class ServicesMigration extends ComponentMigration<Service> {
19
20     private static final String DEFAULT_CONFORMANCE_LEVEL = "0.0";
21     private static Logger LOGGER = LoggerFactory.getLogger(ServicesMigration.class);
22
23     @Resource(name = "service-operation")
24     private IServiceOperation serviceOperation;
25
26     @Resource(name = "service-version-migration")
27     private VersionMigration<Service> versionMigration;
28
29     @Override
30     public String description() {
31         return "migrate services";
32     }
33
34     @Override
35     Either<List<Service>, ?> getElementsToMigrate() {
36         return serviceOperation.getAll();
37     }
38
39     @Override
40     boolean save(Service element) {
41         MigrationMalformedDataLogger.logIfServiceUsingMalformedVfs(element);
42         filterOutDuplicatePropsAndAttrs(element);
43         element.setConformanceLevel(DEFAULT_CONFORMANCE_LEVEL);
44         requirementsCapabilitiesMigrationService.overrideInstanceCapabilitiesRequirements(element);
45         return super.save(element);
46     }
47
48     @Override
49     boolean doPostSaveOperation(Service element) {
50         return element.getComponentInstances() == null ||
51                (requirementsCapabilitiesMigrationService.associateFulfilledRequirements(element, NodeTypeEnum.Service) &&
52                 requirementsCapabilitiesMigrationService.associateFulfilledCapabilities(element, NodeTypeEnum.Service));
53     }
54
55     @Override
56     boolean doPostMigrateOperation(List<Service> elements) {
57         LOGGER.info("migrating services versions");
58         return versionMigration.buildComponentsVersionChain(elements);
59     }
60
61     private void filterOutDuplicatePropsAndAttrs(Service element) {
62         if (element.getComponentInstancesProperties() != null) {
63             removeDuplicatedNameProperties(element);
64         }
65         if (element.getComponentInstancesAttributes() != null) {
66             removeDuplicatedNameAttributes(element);
67         }
68     }
69
70     private void removeDuplicatedNameProperties(Service service) {
71         Map<String, List<ComponentInstanceProperty>> componentInstancesProperties = service.getComponentInstancesProperties();
72         componentInstancesProperties.forEach((uid, properties) ->  {
73             componentInstancesProperties.put(uid, getUniquedNamePropertyList(service, properties));
74         });
75     }
76
77     private List<ComponentInstanceProperty> getUniquedNamePropertyList(Service service, List<ComponentInstanceProperty> properties) {
78         if (properties == null) {
79             return null;
80         }
81         List<ComponentInstanceProperty> uniqueNameProperties = new ArrayList<>();
82         Map<String, List<ComponentInstanceProperty>> collect = properties.stream().collect(Collectors.groupingBy(ComponentInstanceProperty::getName));
83         collect.forEach((name, duplicatedProperties) -> {
84             logServiceDuplicateProperties(service, name, duplicatedProperties);
85             uniqueNameProperties.add(duplicatedProperties.get(0));
86         });
87         return uniqueNameProperties;
88     }
89
90     private void logServiceDuplicateProperties(Service service, String name, List<ComponentInstanceProperty> duplicatedProperties) {
91         if (duplicatedProperties.size() > 1) {
92             LOGGER.debug("service {} with id {} has instance {} with duplicate property {}", service.getName(), service.getUniqueId(), duplicatedProperties.get(0).getUniqueId(), name);
93         }
94     }
95
96     private void removeDuplicatedNameAttributes(Service service) {
97         Map<String, List<ComponentInstanceProperty>> componentInstancesAttributes = service.getComponentInstancesAttributes();
98         componentInstancesAttributes.forEach((uid, attributes) ->  {
99             componentInstancesAttributes.put(uid, getUniquedNameAttributeList(service, attributes));
100         });
101     }
102
103     private List<ComponentInstanceProperty> getUniquedNameAttributeList(Service service, List<ComponentInstanceProperty> attributes) {
104         if (attributes == null) {
105             return null;
106         }
107         List<ComponentInstanceProperty> uniqueNameAttributes = new ArrayList<>();
108         Map<String, List<ComponentInstanceProperty>> collect = attributes.stream().collect(Collectors.groupingBy(ComponentInstanceProperty::getName));
109         collect.forEach((name, duplicatedAttributess) -> {
110             logServiceMalformedAttributes(service, name, duplicatedAttributess);
111             uniqueNameAttributes.add(duplicatedAttributess.get(0));
112         });
113         return uniqueNameAttributes;
114     }
115
116     private void logServiceMalformedAttributes(Service service, String name, List<ComponentInstanceProperty> duplicatedAttributess) {
117         if (duplicatedAttributess.size() > 1) {
118             MigrationMalformedDataLogger.logMalformedDataMsg(String.format("service %s with id %s has instance %s with duplicate attribute %s",
119                     service.getName(), service.getUniqueId(), duplicatedAttributess.get(0).getUniqueId(), name));
120         }
121     }
122
123     //    private void filterOutVFInstanceAttrs(Service element, List<String> vfInstancesIds) {
124 //        Map<String, List<ComponentInstanceAttribute>> componentInstancesAttributes = element.getComponentInstancesAttributes();
125 //        if (componentInstancesAttributes != null) {
126 //            element.setComponentInstancesAttributes(filterOutVFInstanceAttributes(componentInstancesAttributes, vfInstancesIds));
127 //        }
128 //    }
129 //
130 //    private void filterOutVFInstacnecProps(Service element, List<String> vfInstancesIds) {
131 //        Map<String, List<ComponentInstanceProperty>> componentInstancesProperties = element.getComponentInstancesProperties();
132 //        if (componentInstancesProperties != null) {
133 //            element.setComponentInstancesProperties(filterOutVFInstanceProperties(componentInstancesProperties, vfInstancesIds));
134 //        }
135 //    }
136 //
137 //    private Map<String, List<ComponentInstanceProperty>> filterOutVFInstanceProperties(Map<String, List<ComponentInstanceProperty>> instances, List<String> vfInstanceIds) {
138 //        return instances.entrySet()
139 //                .stream()
140 //                .filter(entry -> !vfInstanceIds.contains(entry.getKey()))
141 //                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
142 //    }
143 //
144 //    private Map<String, List<ComponentInstanceAttribute>> filterOutVFInstanceAttributes(Map<String, List<ComponentInstanceAttribute>> instances, List<String> vfInstanceIds) {
145 //        return instances.entrySet()
146 //                .stream()
147 //                .filter(entry -> !vfInstanceIds.contains(entry.getKey()))
148 //                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
149 //    }
150
151
152 }