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;
23 import java.util.ArrayList;
24 import java.util.EnumMap;
25 import java.util.HashMap;
26 import java.util.List;
28 import java.util.Map.Entry;
29 import java.util.stream.Collectors;
31 import javax.annotation.Resource;
33 import org.apache.commons.collections.CollectionUtils;
34 import org.openecomp.sdc.asdctool.impl.migration.v1702.DataTypesUpdate;
35 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
36 import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
37 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
38 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
39 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
40 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
41 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
42 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
43 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
44 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
45 import org.openecomp.sdc.be.model.ComponentInstance;
46 import org.openecomp.sdc.be.model.DataTypeDefinition;
47 import org.openecomp.sdc.be.model.GroupDefinition;
48 import org.openecomp.sdc.be.model.GroupInstance;
49 import org.openecomp.sdc.be.model.GroupInstanceProperty;
50 import org.openecomp.sdc.be.model.GroupProperty;
51 import org.openecomp.sdc.be.model.GroupTypeDefinition;
52 import org.openecomp.sdc.be.model.PropertyDefinition;
53 import org.openecomp.sdc.be.model.jsontitan.operations.BaseOperation;
54 import org.openecomp.sdc.be.model.jsontitan.operations.TopologyTemplateOperation;
55 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
56 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
57 import org.openecomp.sdc.be.model.operations.impl.GroupTypeOperation;
58 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
59 import org.openecomp.sdc.be.resources.data.PropertyData;
60 import org.slf4j.Logger;
61 import org.slf4j.LoggerFactory;
62 import org.springframework.beans.factory.annotation.Autowired;
63 import org.springframework.stereotype.Component;
65 import fj.data.Either;
67 @Component("vfModulesPropertiesAdding")
68 public class VfModulesPropertiesAdding {
70 private static Logger LOGGER = LoggerFactory.getLogger(VfModulesPropertiesAdding.class);
73 private ToscaOperationFacade toscaOperationFacade;
76 private TopologyTemplateOperation topologyTemplateOperation;
78 @Resource(name ="group-type-operation-mig")
79 private GroupTypeOperation groupTypeOperation;
81 @Resource(name = "property-operation-mig")
82 private PropertyOperation propertyOperation;
85 public boolean migrate(String groupsTypeYmlFilePath) {
86 LOGGER.debug("Going to add new properties to vfModules. ");
87 boolean result = true;
88 GroupTypeDefinition vfModule;
89 Either<List<GraphVertex>, TitanOperationStatus> getAllTopologyTemplatesRes = null;
90 String vfModuleUid = "org.openecomp.groups.VfModule.1.0.grouptype";
91 Either<GroupTypeDefinition, TitanOperationStatus> getGroupTypeVfModuleRes ;
92 List<PropertyDefinition> updatedProperties = null;
94 LOGGER.debug("Going to fetch {}. ", vfModuleUid);
95 getGroupTypeVfModuleRes = groupTypeOperation.getGroupTypeByUid(vfModuleUid);
97 if(getGroupTypeVfModuleRes.isRight() && getGroupTypeVfModuleRes.right().value() != TitanOperationStatus.NOT_FOUND){
98 LOGGER.debug("Failed to fetch the group type {}. The status is {}. ", vfModuleUid, getGroupTypeVfModuleRes.right().value());
101 if(getGroupTypeVfModuleRes.isRight() && getGroupTypeVfModuleRes.right().value() == TitanOperationStatus.NOT_FOUND){
102 LOGGER.debug("The group type with id {} was not found. Skipping adding the new properties. ", vfModuleUid);
106 LOGGER.debug("Going to add the new properties {} to org.openecomp.groups.VfModule.1.0.grouptype. ");
107 vfModule = getGroupTypeVfModuleRes.left().value();
108 updatedProperties = getAllVfModuleTypePropertiesFromYaml(groupsTypeYmlFilePath);
109 result = addNewPropertiesToGroupType(vfModule, getNewVfModuleTypeProperties(updatedProperties, vfModule));
111 LOGGER.debug("Failed to add the new properties {} to org.openecomp.groups.VfModule.1.0.grouptype. ");
114 if(result && CollectionUtils.isNotEmpty(updatedProperties)){
115 Map<GraphPropertyEnum, Object> propsHasNot = new EnumMap<>(GraphPropertyEnum.class);
116 propsHasNot.put(GraphPropertyEnum.IS_DELETED, true);
117 getAllTopologyTemplatesRes = toscaOperationFacade.getTitanDao().getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, null, propsHasNot, JsonParseFlagEnum.ParseAll);
118 if (getAllTopologyTemplatesRes.isRight() && getAllTopologyTemplatesRes.right().value() != TitanOperationStatus.NOT_FOUND) {
119 LOGGER.debug("Failed to fetch all non marked topology templates , propsHasNot {}, error {}", propsHasNot, getAllTopologyTemplatesRes.right().value());
123 if(result && getAllTopologyTemplatesRes!=null && getAllTopologyTemplatesRes.isLeft()){
124 result = addNewVfModulesProperties(getAllTopologyTemplatesRes.left().value(), updatedProperties);
126 } catch (Exception e){
131 toscaOperationFacade.commit();
133 toscaOperationFacade.rollback();
139 private boolean addNewVfModulesProperties(List<GraphVertex> components, List<PropertyDefinition> updatedProperties) {
140 boolean result = true;
141 for(GraphVertex component : components){
142 LOGGER.debug("Going to add the new properties {} to component {}. ", updatedProperties, component.getUniqueId());
143 result = addNewPropertiesToVfModules(component, updatedProperties);
145 LOGGER.debug("Failed to add the new properties {} to component {}. ", updatedProperties, component.getUniqueId());
148 toscaOperationFacade.commit();
153 private boolean addNewPropertiesToVfModules(GraphVertex componentV, List<PropertyDefinition> updatedProperties) {
154 boolean result = true;
155 List<GroupDefinition> vfModules = null;
156 Either<org.openecomp.sdc.be.model.Component, StorageOperationStatus> getToscaElementRes = toscaOperationFacade.getToscaElement(componentV);
157 if(getToscaElementRes.isRight()){
158 LOGGER.debug("Failed to fetch the component {}. ", componentV.getUniqueId());
161 else if(CollectionUtils.isNotEmpty(getToscaElementRes.left().value().getGroups())){
162 vfModules = getToscaElementRes.left().value().getGroups().stream().filter(g -> g.getType().equals(BaseOperation.VF_MODULE)).collect(Collectors.toList());
164 if(vfModules != null){
165 vfModules.forEach(vfModule -> addAllNewProperties(vfModule.getProperties(), updatedProperties));
166 StorageOperationStatus status = topologyTemplateOperation.updateToscaDataOfToscaElement(componentV, EdgeLabelEnum.GROUPS, VertexTypeEnum.GROUPS, vfModules, JsonPresentationFields.NAME);
167 if(status!= StorageOperationStatus.OK){
168 LOGGER.debug("Failed to add the new properties {} to groups of component {}. ", updatedProperties, componentV.getUniqueId());
172 if(result && CollectionUtils.isNotEmpty(getToscaElementRes.left().value().getComponentInstances())){
173 result = addPropertiesToVfModuleInstances(getToscaElementRes.left().value(), componentV, updatedProperties);
178 private void addAllNewProperties(List<PropertyDataDefinition> vfModuleProperties, List<PropertyDefinition> updatedProperties) {
179 Map<String, PropertyDataDefinition> propertiesMap = vfModuleProperties.stream().collect(Collectors.toMap(p->p.getName(), p->p));
181 for(PropertyDefinition property : updatedProperties){
182 if(!propertiesMap.containsKey(property.getName())){
183 vfModuleProperties.add(property);
188 private boolean addPropertiesToVfModuleInstances(org.openecomp.sdc.be.model.Component component, GraphVertex componentV, List<PropertyDefinition> updatedProperties) {
189 boolean result = true;
190 List<GroupInstance> vfModuleInstances;
191 List<String> pathKeys;
192 LOGGER.debug("Going to add the new properties {} to group instances of component {}. ", updatedProperties, componentV.getUniqueId());
193 for(ComponentInstance componentInstance : component.getComponentInstances()){
194 vfModuleInstances = null;
195 if(CollectionUtils.isNotEmpty(componentInstance.getGroupInstances())){
196 vfModuleInstances = componentInstance.getGroupInstances()
198 .filter(gi -> gi.getType().equals(BaseOperation.VF_MODULE))
199 .collect(Collectors.toList());
201 if(vfModuleInstances != null){
202 for(GroupInstance vfModuleInstance :vfModuleInstances){
203 addAllNewProperties(vfModuleInstance.getProperties(),updatedProperties);
204 pathKeys = new ArrayList<>();
205 pathKeys.add(componentInstance.getUniqueId());
206 StorageOperationStatus status = topologyTemplateOperation
207 .updateToscaDataDeepElementOfToscaElement(componentV, EdgeLabelEnum.INST_GROUPS, VertexTypeEnum.INST_GROUPS, vfModuleInstance, pathKeys, JsonPresentationFields.NAME);
208 if(status!= StorageOperationStatus.OK){
210 LOGGER.debug("Failed to add the new properties {} to group instances of component {}. ", updatedProperties, componentV.getUniqueId());
215 LOGGER.debug("Failed to add the new properties {} to group instances of component {}. ", updatedProperties, componentV.getUniqueId());
223 private boolean addNewPropertiesToGroupType(GroupTypeDefinition vfModule, List<PropertyDefinition> newProperties) {
224 boolean result = true;
225 Either<Map<String, PropertyData>, TitanOperationStatus> addPropertiesRes = propertyOperation
226 .addPropertiesToElementType(vfModule.getUniqueId(), NodeTypeEnum.GroupType, newProperties);
227 if(addPropertiesRes.isRight()){
233 private List<PropertyDefinition> getAllVfModuleTypePropertiesFromYaml(String groupsTypeYmlFilePath) {
234 List<DataTypeDefinition> groupTypes = DataTypesUpdate.extractDataTypesFromYaml(groupsTypeYmlFilePath);
235 DataTypeDefinition vfModule = groupTypes.stream().filter(g -> g.getName().equals(BaseOperation.VF_MODULE)).findFirst().orElse(null);
236 return vfModule.getProperties();
239 private List<PropertyDefinition> getNewVfModuleTypeProperties(List<PropertyDefinition> allVfModuleTypeProperties, GroupTypeDefinition vfModule) {
240 Map<String, PropertyDefinition> existingVfModuleTypeProperties = vfModule.getProperties()
242 .collect(Collectors.toMap(p -> p.getName(), p -> p));
244 List<PropertyDefinition> newGroupTypeProperties = new ArrayList<>();
245 for(PropertyDefinition property : allVfModuleTypeProperties){
246 if(!existingVfModuleTypeProperties.containsKey(property.getName())){
247 newGroupTypeProperties.add(property);
250 return newGroupTypeProperties;
253 public String description() {
254 return "vfModulesPropertiesAdding";