1717b1fa838cff001f8d709b98a286977e2b07c2
[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 fj.data.Either;
24 import jersey.repackaged.com.google.common.collect.Sets;
25 import org.openecomp.sdc.be.model.PropertyDefinition;
26 import org.openecomp.sdc.be.model.Resource;
27 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import java.util.List;
32 import java.util.Optional;
33 import java.util.Set;
34
35 public class NormativesMigration extends ComponentMigration<Resource> {
36
37     private static Logger LOGGER = LoggerFactory.getLogger(NormativesMigration.class);
38     private static final String JCP_VERSION_PROPERTY = "jcp-version";
39     private static final Set<String> e2eMalformedVfcs = Sets.newHashSet("59da26b4-edd0-4412-a2e6-d6711f376340");
40
41     @javax.annotation.Resource(name = "normatives-resolver")
42     private NormativesResolver normativesResolver;
43
44     @javax.annotation.Resource(name = "resource-version-migration")
45     private VersionMigration<Resource> versionMigration;
46
47     @Override
48     public String description() {
49         return "migration of node types";
50     }
51
52     @Override
53     Either<List<Resource>, ?> getElementsToMigrate() {
54         return normativesResolver.getAllNodeTypeNormatives();
55     }
56
57     @Override
58     boolean save(Resource element) {
59         if (e2eMalformedVfcs.contains(element.getUniqueId())) {
60             replaceJcpVersionPropertyTypeToVersion(element);
61         }
62         return super.save(element);
63     }
64
65     private void replaceJcpVersionPropertyTypeToVersion(Resource element) {
66         getJcpIntegerProperty(element).ifPresent(propertyDefinition -> {
67             LOGGER.info("resource {} with id {}: found property jcp-version with type 'integer', changing type to 'version'", element.getName(), element.getUniqueId());
68             propertyDefinition.setType(ToscaPropertyType.VERSION.getType());
69         });
70     }
71
72     private Optional<PropertyDefinition> getJcpIntegerProperty(Resource element) {
73         if (element.getProperties() == null) return Optional.empty();
74         return element.getProperties().stream()
75                                .filter(prop -> prop.getName().equals(JCP_VERSION_PROPERTY))
76                                .filter(prop -> prop.getType().equals(ToscaPropertyType.INTEGER.getType()))
77                                .findAny();
78
79     }
80
81     @Override
82     boolean doPostMigrateOperation(List<Resource> elements) {
83         LOGGER.info("migrating node types versions");
84         return versionMigration.buildComponentsVersionChain(elements);
85     }
86 }