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;
23 import java.util.ArrayList;
24 import java.util.List;
26 import java.util.stream.Collectors;
28 import javax.annotation.Resource;
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;
38 import fj.data.Either;
40 public class ServicesMigration extends ComponentMigration<Service> {
42 private static final String DEFAULT_CONFORMANCE_LEVEL = "0.0";
43 private static Logger LOGGER = LoggerFactory.getLogger(ServicesMigration.class);
45 @Resource(name = "service-operation")
46 private IServiceOperation serviceOperation;
48 @Resource(name = "service-version-migration")
49 private VersionMigration<Service> versionMigration;
52 public String description() {
53 return "migrate services";
57 Either<List<Service>, ?> getElementsToMigrate() {
58 return serviceOperation.getAll();
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);
71 boolean doPostSaveOperation(Service element) {
72 return element.getComponentInstances() == null ||
73 (requirementsCapabilitiesMigrationService.associateFulfilledRequirements(element, NodeTypeEnum.Service) &&
74 requirementsCapabilitiesMigrationService.associateFulfilledCapabilities(element, NodeTypeEnum.Service));
78 boolean doPostMigrateOperation(List<Service> elements) {
79 LOGGER.info("migrating services versions");
80 return versionMigration.buildComponentsVersionChain(elements);
84 void doPreMigrationOperation(List<Service> elements) {
85 super.doPreMigrationOperation(elements);
86 setMissingTemplateInfo(elements);
89 private void filterOutDuplicatePropsAndAttrs(Service element) {
90 if (element.getComponentInstancesProperties() != null) {
91 removeDuplicatedNameProperties(element);
93 if (element.getComponentInstancesAttributes() != null) {
94 removeDuplicatedNameAttributes(element);
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));
105 private List<ComponentInstanceProperty> getUniquedNamePropertyList(Service service, List<ComponentInstanceProperty> properties) {
106 if (properties == null) {
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));
115 return uniqueNameProperties;
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);
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));
131 private List<ComponentInstanceProperty> getUniquedNameAttributeList(Service service, List<ComponentInstanceProperty> attributes) {
132 if (attributes == null) {
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));
141 return uniqueNameAttributes;
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));
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));
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));
165 // private Map<String, List<ComponentInstanceProperty>> filterOutVFInstanceProperties(Map<String, List<ComponentInstanceProperty>> instances, List<String> vfInstanceIds) {
166 // return instances.entrySet()
168 // .filter(entry -> !vfInstanceIds.contains(entry.getKey()))
169 // .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
172 // private Map<String, List<ComponentInstanceAttribute>> filterOutVFInstanceAttributes(Map<String, List<ComponentInstanceAttribute>> instances, List<String> vfInstanceIds) {
173 // return instances.entrySet()
175 // .filter(entry -> !vfInstanceIds.contains(entry.getKey()))
176 // .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));