1 package org.openecomp.sdc.asdctool.impl.migration.v1707;
3 import java.util.ArrayList;
4 import java.util.EnumMap;
5 import java.util.HashMap;
8 import java.util.Map.Entry;
9 import java.util.stream.Collectors;
11 import javax.annotation.Resource;
13 import org.apache.commons.collections.CollectionUtils;
14 import org.openecomp.sdc.asdctool.impl.migration.v1702.DataTypesUpdate;
15 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
16 import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
17 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
18 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
19 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
20 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
21 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
22 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
23 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
24 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
25 import org.openecomp.sdc.be.model.ComponentInstance;
26 import org.openecomp.sdc.be.model.DataTypeDefinition;
27 import org.openecomp.sdc.be.model.GroupDefinition;
28 import org.openecomp.sdc.be.model.GroupInstance;
29 import org.openecomp.sdc.be.model.GroupInstanceProperty;
30 import org.openecomp.sdc.be.model.GroupProperty;
31 import org.openecomp.sdc.be.model.GroupTypeDefinition;
32 import org.openecomp.sdc.be.model.PropertyDefinition;
33 import org.openecomp.sdc.be.model.jsontitan.operations.BaseOperation;
34 import org.openecomp.sdc.be.model.jsontitan.operations.TopologyTemplateOperation;
35 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
36 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
37 import org.openecomp.sdc.be.model.operations.impl.GroupTypeOperation;
38 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
39 import org.openecomp.sdc.be.resources.data.PropertyData;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.stereotype.Component;
45 import fj.data.Either;
47 @Component("vfModulesPropertiesAdding")
48 public class VfModulesPropertiesAdding {
50 private static Logger LOGGER = LoggerFactory.getLogger(VfModulesPropertiesAdding.class);
53 private ToscaOperationFacade toscaOperationFacade;
56 private TopologyTemplateOperation topologyTemplateOperation;
58 @Resource(name ="group-type-operation-mig")
59 private GroupTypeOperation groupTypeOperation;
61 @Resource(name = "property-operation-mig")
62 private PropertyOperation propertyOperation;
65 public boolean migrate(String groupsTypeYmlFilePath) {
66 LOGGER.debug("Going to add new properties to vfModules. ");
67 boolean result = true;
68 GroupTypeDefinition vfModule;
69 Either<List<GraphVertex>, TitanOperationStatus> getAllTopologyTemplatesRes = null;
70 String vfModuleUid = "org.openecomp.groups.VfModule.1.0.grouptype";
71 Either<GroupTypeDefinition, TitanOperationStatus> getGroupTypeVfModuleRes ;
72 List<PropertyDefinition> updatedProperties = null;
74 LOGGER.debug("Going to fetch {}. ", vfModuleUid);
75 getGroupTypeVfModuleRes = groupTypeOperation.getGroupTypeByUid(vfModuleUid);
77 if(getGroupTypeVfModuleRes.isRight() && getGroupTypeVfModuleRes.right().value() != TitanOperationStatus.NOT_FOUND){
78 LOGGER.debug("Failed to fetch the group type {}. The status is {}. ", vfModuleUid, getGroupTypeVfModuleRes.right().value());
81 if(getGroupTypeVfModuleRes.isRight() && getGroupTypeVfModuleRes.right().value() == TitanOperationStatus.NOT_FOUND){
82 LOGGER.debug("The group type with id {} was not found. Skipping adding the new properties. ", vfModuleUid);
86 LOGGER.debug("Going to add the new properties {} to org.openecomp.groups.VfModule.1.0.grouptype. ");
87 vfModule = getGroupTypeVfModuleRes.left().value();
88 updatedProperties = getAllVfModuleTypePropertiesFromYaml(groupsTypeYmlFilePath);
89 result = addNewPropertiesToGroupType(vfModule, getNewVfModuleTypeProperties(updatedProperties, vfModule));
91 LOGGER.debug("Failed to add the new properties {} to org.openecomp.groups.VfModule.1.0.grouptype. ");
94 if(result && CollectionUtils.isNotEmpty(updatedProperties)){
95 Map<GraphPropertyEnum, Object> propsHasNot = new EnumMap<>(GraphPropertyEnum.class);
96 propsHasNot.put(GraphPropertyEnum.IS_DELETED, true);
97 getAllTopologyTemplatesRes = toscaOperationFacade.getTitanDao().getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, null, propsHasNot, JsonParseFlagEnum.ParseAll);
98 if (getAllTopologyTemplatesRes.isRight() && getAllTopologyTemplatesRes.right().value() != TitanOperationStatus.NOT_FOUND) {
99 LOGGER.debug("Failed to fetch all non marked topology templates , propsHasNot {}, error {}", propsHasNot, getAllTopologyTemplatesRes.right().value());
103 if(result && getAllTopologyTemplatesRes!=null && getAllTopologyTemplatesRes.isLeft()){
104 result = addNewVfModulesProperties(getAllTopologyTemplatesRes.left().value(), updatedProperties);
106 } catch (Exception e){
111 toscaOperationFacade.commit();
113 toscaOperationFacade.rollback();
119 private boolean addNewVfModulesProperties(List<GraphVertex> components, List<PropertyDefinition> updatedProperties) {
120 boolean result = true;
121 for(GraphVertex component : components){
122 LOGGER.debug("Going to add the new properties {} to component {}. ", updatedProperties, component.getUniqueId());
123 result = addNewPropertiesToVfModules(component, updatedProperties);
125 LOGGER.debug("Failed to add the new properties {} to component {}. ", updatedProperties, component.getUniqueId());
128 toscaOperationFacade.commit();
133 private boolean addNewPropertiesToVfModules(GraphVertex componentV, List<PropertyDefinition> updatedProperties) {
134 boolean result = true;
135 List<GroupDefinition> vfModules = null;
136 Either<org.openecomp.sdc.be.model.Component, StorageOperationStatus> getToscaElementRes = toscaOperationFacade.getToscaElement(componentV);
137 if(getToscaElementRes.isRight()){
138 LOGGER.debug("Failed to fetch the component {}. ", componentV.getUniqueId());
141 else if(CollectionUtils.isNotEmpty(getToscaElementRes.left().value().getGroups())){
142 vfModules = getToscaElementRes.left().value().getGroups().stream().filter(g -> g.getType().equals(BaseOperation.VF_MODULE)).collect(Collectors.toList());
144 if(vfModules != null){
145 vfModules.forEach(vfModule -> addAllNewProperties(vfModule.getProperties(), updatedProperties));
146 StorageOperationStatus status = topologyTemplateOperation.updateToscaDataOfToscaElement(componentV, EdgeLabelEnum.GROUPS, VertexTypeEnum.GROUPS, vfModules, JsonPresentationFields.NAME);
147 if(status!= StorageOperationStatus.OK){
148 LOGGER.debug("Failed to add the new properties {} to groups of component {}. ", updatedProperties, componentV.getUniqueId());
152 if(result && CollectionUtils.isNotEmpty(getToscaElementRes.left().value().getComponentInstances())){
153 result = addPropertiesToVfModuleInstances(getToscaElementRes.left().value(), componentV, updatedProperties);
158 private void addAllNewProperties(List<PropertyDataDefinition> vfModuleProperties, List<PropertyDefinition> updatedProperties) {
159 Map<String, PropertyDataDefinition> propertiesMap = vfModuleProperties.stream().collect(Collectors.toMap(p->p.getName(), p->p));
161 for(PropertyDefinition property : updatedProperties){
162 if(!propertiesMap.containsKey(property.getName())){
163 vfModuleProperties.add(property);
168 private boolean addPropertiesToVfModuleInstances(org.openecomp.sdc.be.model.Component component, GraphVertex componentV, List<PropertyDefinition> updatedProperties) {
169 boolean result = true;
170 List<GroupInstance> vfModuleInstances;
171 List<String> pathKeys;
172 LOGGER.debug("Going to add the new properties {} to group instances of component {}. ", updatedProperties, componentV.getUniqueId());
173 for(ComponentInstance componentInstance : component.getComponentInstances()){
174 vfModuleInstances = null;
175 if(CollectionUtils.isNotEmpty(componentInstance.getGroupInstances())){
176 vfModuleInstances = componentInstance.getGroupInstances()
178 .filter(gi -> gi.getType().equals(BaseOperation.VF_MODULE))
179 .collect(Collectors.toList());
181 if(vfModuleInstances != null){
182 for(GroupInstance vfModuleInstance :vfModuleInstances){
183 addAllNewProperties(vfModuleInstance.getProperties(),updatedProperties);
184 pathKeys = new ArrayList<>();
185 pathKeys.add(componentInstance.getUniqueId());
186 StorageOperationStatus status = topologyTemplateOperation
187 .updateToscaDataDeepElementOfToscaElement(componentV, EdgeLabelEnum.INST_GROUPS, VertexTypeEnum.INST_GROUPS, vfModuleInstance, pathKeys, JsonPresentationFields.NAME);
188 if(status!= StorageOperationStatus.OK){
190 LOGGER.debug("Failed to add the new properties {} to group instances of component {}. ", updatedProperties, componentV.getUniqueId());
195 LOGGER.debug("Failed to add the new properties {} to group instances of component {}. ", updatedProperties, componentV.getUniqueId());
203 private boolean addNewPropertiesToGroupType(GroupTypeDefinition vfModule, List<PropertyDefinition> newProperties) {
204 boolean result = true;
205 Either<Map<String, PropertyData>, TitanOperationStatus> addPropertiesRes = propertyOperation
206 .addPropertiesToElementType(vfModule.getUniqueId(), NodeTypeEnum.GroupType, newProperties);
207 if(addPropertiesRes.isRight()){
213 private List<PropertyDefinition> getAllVfModuleTypePropertiesFromYaml(String groupsTypeYmlFilePath) {
214 List<DataTypeDefinition> groupTypes = DataTypesUpdate.extractDataTypesFromYaml(groupsTypeYmlFilePath);
215 DataTypeDefinition vfModule = groupTypes.stream().filter(g -> g.getName().equals(BaseOperation.VF_MODULE)).findFirst().orElse(null);
216 return vfModule.getProperties();
219 private List<PropertyDefinition> getNewVfModuleTypeProperties(List<PropertyDefinition> allVfModuleTypeProperties, GroupTypeDefinition vfModule) {
220 Map<String, PropertyDefinition> existingVfModuleTypeProperties = vfModule.getProperties()
222 .collect(Collectors.toMap(p -> p.getName(), p -> p));
224 List<PropertyDefinition> newGroupTypeProperties = new ArrayList<>();
225 for(PropertyDefinition property : allVfModuleTypeProperties){
226 if(!existingVfModuleTypeProperties.containsKey(property.getName())){
227 newGroupTypeProperties.add(property);
230 return newGroupTypeProperties;
233 public String description() {
234 return "vfModulesPropertiesAdding";