ca1ed2b2a87542f51ea117622d091828177dcd5d
[sdc.git] /
1 package org.openecomp.sdc.asdctool.impl.migration.v1707;
2
3 import java.util.ArrayList;
4 import java.util.EnumMap;
5 import java.util.HashMap;
6 import java.util.List;
7 import java.util.Map;
8 import java.util.Map.Entry;
9 import java.util.stream.Collectors;
10
11 import javax.annotation.Resource;
12
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;
44
45 import fj.data.Either;
46
47 @Component("vfModulesPropertiesAdding")
48 public class VfModulesPropertiesAdding {
49
50         private static Logger LOGGER = LoggerFactory.getLogger(VfModulesPropertiesAdding.class);
51         
52         @Autowired
53     private ToscaOperationFacade toscaOperationFacade;
54         
55         @Autowired
56     private TopologyTemplateOperation topologyTemplateOperation;
57         
58         @Resource(name ="group-type-operation-mig")
59     private GroupTypeOperation groupTypeOperation;
60         
61         @Resource(name = "property-operation-mig")
62     private PropertyOperation propertyOperation;
63         
64         
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;
73                 try{
74                         LOGGER.debug("Going to fetch {}. ", vfModuleUid);
75                         getGroupTypeVfModuleRes = groupTypeOperation.getGroupTypeByUid(vfModuleUid);
76                         
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());
79                                 result = false;
80                         }
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);
83                                 return true;
84                         }
85                         if(result){
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));
90                                 if(!result){
91                                         LOGGER.debug("Failed to add the new properties {} to org.openecomp.groups.VfModule.1.0.grouptype. ");
92                                 }
93                         }
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());
100                                         result = false;
101                                 }
102                         }
103                         if(result && getAllTopologyTemplatesRes!=null && getAllTopologyTemplatesRes.isLeft()){
104                                 result = addNewVfModulesProperties(getAllTopologyTemplatesRes.left().value(), updatedProperties);
105                         }
106                 } catch (Exception e){
107                         result = false;
108                 }
109                 finally{
110                         if(result){
111                                 toscaOperationFacade.commit();
112                         } else {
113                                 toscaOperationFacade.rollback();
114                         }
115                 }
116                 return result;
117         }
118
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);
124                         if(!result){
125                                 LOGGER.debug("Failed to add the new properties {} to component {}. ", updatedProperties, component.getUniqueId());
126                                 break;
127                         }
128                         toscaOperationFacade.commit();
129                 }
130                 return result;
131         }
132
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());
139                         result = false;
140                 }
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());
143                 }
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());
149                                 result = false;
150                         }
151                 }
152                 if(result && CollectionUtils.isNotEmpty(getToscaElementRes.left().value().getComponentInstances())){
153                         result = addPropertiesToVfModuleInstances(getToscaElementRes.left().value(), componentV, updatedProperties);
154                 }
155                 return result;
156         }
157
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));
160                 
161                 for(PropertyDefinition property : updatedProperties){
162                         if(!propertiesMap.containsKey(property.getName())){
163                                 vfModuleProperties.add(property);
164                         }
165                 }
166         }
167
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()
177                                                 .stream()
178                                                 .filter(gi -> gi.getType().equals(BaseOperation.VF_MODULE))
179                                                 .collect(Collectors.toList());
180                         }
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){
189                                                 result = false;
190                                                 LOGGER.debug("Failed to add the new properties {} to group instances of component {}. ", updatedProperties, componentV.getUniqueId());
191                                                 break;
192                                         }
193                                 }
194                                 if(!result){
195                                         LOGGER.debug("Failed to add the new properties {} to group instances of component {}. ", updatedProperties, componentV.getUniqueId());
196                                         break;
197                                 }
198                         }
199                 }
200                 return result;
201         }
202         
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()){
208                         result = false;
209                 }
210                 return result;
211         }
212
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();
217         }
218         
219         private List<PropertyDefinition> getNewVfModuleTypeProperties(List<PropertyDefinition> allVfModuleTypeProperties, GroupTypeDefinition vfModule) {
220                 Map<String, PropertyDefinition> existingVfModuleTypeProperties = vfModule.getProperties()
221                                 .stream()
222                                 .collect(Collectors.toMap(p -> p.getName(), p -> p));
223                 
224                 List<PropertyDefinition> newGroupTypeProperties = new ArrayList<>();
225                 for(PropertyDefinition property : allVfModuleTypeProperties){
226                         if(!existingVfModuleTypeProperties.containsKey(property.getName())){
227                                 newGroupTypeProperties.add(property);
228                         }
229                 }
230                 return newGroupTypeProperties;
231         }
232
233         public String description() {
234                 return "vfModulesPropertiesAdding";
235         }
236
237 }