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