2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 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.migration.tasks.mig1902;
23 import org.openecomp.sdc.asdctool.migration.core.DBVersion;
24 import org.openecomp.sdc.asdctool.migration.core.task.Migration;
25 import org.openecomp.sdc.asdctool.migration.core.task.MigrationResult;
26 import org.openecomp.sdc.asdctool.migration.tasks.InstanceMigrationBase;
27 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
28 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
29 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
30 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
31 import org.openecomp.sdc.be.datatypes.elements.GroupDataDefinition;
32 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
33 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
34 import org.openecomp.sdc.be.model.GroupTypeDefinition;
35 import org.openecomp.sdc.be.model.PropertyDefinition;
36 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
37 import org.openecomp.sdc.be.model.operations.impl.GroupTypeOperation;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.stereotype.Component;
42 import java.math.BigInteger;
43 import java.util.Arrays;
44 import java.util.HashMap;
45 import java.util.List;
49 public class SdcGroupsMigration extends InstanceMigrationBase implements Migration {
51 private static final Logger log = LoggerFactory.getLogger(SdcGroupsMigration.class);
53 private final GroupTypeOperation groupTypeOperation;
55 private Map<String, GroupTypeDefinition> latestGroupTypeMap = new HashMap<>();
57 public enum GroupsForUpgrade {
58 NW_COLLECTION_GROUP_NAME("org.openecomp.groups.NetworkCollection"),
59 VFC_INSTANCE_GROUP_NAME("org.openecomp.groups.VfcInstanceGroup");
61 private String toscaType;
63 GroupsForUpgrade(String toscaType) {
64 this.toscaType = toscaType;
67 public static boolean containsToscaType(String type) {
69 return Arrays.stream(values()).anyMatch(g->g.getToscaType().equals(type));
71 catch (IllegalArgumentException ex) {
76 public String getToscaType() {
81 public SdcGroupsMigration(JanusGraphDao janusGraphDao, GroupTypeOperation groupTypeOperation) {
83 this.groupTypeOperation = groupTypeOperation;
87 public String description() {
88 return "update derived from field value for NetworkCollection and VfcInstanceGroup group instances ";
92 public DBVersion getVersion() {
93 return DBVersion.from(BigInteger.valueOf(1902), BigInteger.valueOf(0));
97 public MigrationResult migrate() {
98 loadLatestGroupTypeDefinitions();
99 StorageOperationStatus status = upgradeTopologyTemplates();
100 return status == StorageOperationStatus.OK ?
101 MigrationResult.success() : MigrationResult.error("failed to update derived from value for NetworkCollection and VfcInstanceGroup group instances. Error : " + status);
104 void loadLatestGroupTypeDefinitions() {
105 Arrays.stream(GroupsForUpgrade.values()).forEach(this::getLatestGroupTypeDefinition);
109 protected StorageOperationStatus handleOneContainer(GraphVertex containerVorig) {
110 StorageOperationStatus status = StorageOperationStatus.NOT_FOUND;
111 GraphVertex containerV = getVertexById(containerVorig.getUniqueId());
114 status = janusGraphDao.getChildVertex(containerV, EdgeLabelEnum.GROUPS, JsonParseFlagEnum.ParseAll)
115 .either(this::updateGroupPropertiesIfRequired, this::handleError);
117 catch (Exception e) {
118 log.error("Exception occurred:", e);
119 status = StorageOperationStatus.GENERAL_ERROR;
122 if (status != StorageOperationStatus.OK) {
123 janusGraphDao.rollback();
124 if (status == StorageOperationStatus.NOT_FOUND) {
125 //it is happy flow as well
126 status = StorageOperationStatus.OK;
129 if (log.isInfoEnabled()) {
130 log.info("Upgrade status is <{}> for topology template <{}> uniqueId <{}>",
131 status.name(), containerV.getMetadataProperties().get(GraphPropertyEnum.NAME),
132 containerV.getMetadataProperties().get(GraphPropertyEnum.UNIQUE_ID));
138 private StorageOperationStatus updateGroupPropertiesIfRequired(GraphVertex vertex) {
139 StorageOperationStatus status = StorageOperationStatus.NOT_FOUND;
140 boolean isUpdated = false;
141 Map<String, GroupDataDefinition> groupDefinitionMap = (Map<String, GroupDataDefinition>) vertex.getJson();
142 for (GroupDataDefinition groupDef : groupDefinitionMap.values()) {
143 if (GroupsForUpgrade.containsToscaType(groupDef.getType())) {
144 if (log.isDebugEnabled()) {
145 log.debug("Group instance named <{}> of type <{}> is supposed to be updated on vertex <{}>",
146 groupDef.getName(), groupDef.getType(), vertex.getUniqueId());
148 isUpdated = isGroupPropertiesUpdateDone(groupDef.getProperties(), latestGroupTypeMap.get(groupDef.getType()).getProperties());
149 if (log.isDebugEnabled()) {
150 String result = isUpdated ? "has been updated" : "is up to date ";
151 log.debug("Group instance named <{}> of type <{}> uniqueID <{}> {} on vertex <{}>",
152 groupDef.getName(), groupDef.getType(), groupDef.getUniqueId(), result, vertex.getUniqueId());
157 vertex.setJson(groupDefinitionMap);
158 status = updateVertexAndCommit(vertex);
159 if (status == StorageOperationStatus.OK && log.isDebugEnabled()) {
160 log.debug("Group properties change is committed on vertex <{}>", vertex.getUniqueId());
166 private boolean isGroupPropertiesUpdateDone(List<PropertyDataDefinition> curPropDefList, List<PropertyDefinition> latestGroupDefList) {
167 boolean isUpdated = false;
168 for (PropertyDefinition prop: latestGroupDefList) {
169 if (curPropDefList.stream().noneMatch(l->l.getName().equals(prop.getName()))) {
170 curPropDefList.add(prop);
177 StorageOperationStatus getLatestGroupTypeDefinition(GroupsForUpgrade groupsForUpgrade) {
178 return groupTypeOperation.getLatestGroupTypeByType(groupsForUpgrade.getToscaType(), false)
180 latestGroupTypeMap.put(groupsForUpgrade.getToscaType(), g);
181 return StorageOperationStatus.OK;