5b1441903fe161af296157b8d0b6b9a15f7ea1a7
[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.types.EdgeLabelEnum;
17 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
18 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
19 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
20 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
21 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
22 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
23 import org.openecomp.sdc.be.model.ComponentInstance;
24 import org.openecomp.sdc.be.model.DataTypeDefinition;
25 import org.openecomp.sdc.be.model.GroupDefinition;
26 import org.openecomp.sdc.be.model.GroupInstance;
27 import org.openecomp.sdc.be.model.GroupInstanceProperty;
28 import org.openecomp.sdc.be.model.GroupProperty;
29 import org.openecomp.sdc.be.model.GroupTypeDefinition;
30 import org.openecomp.sdc.be.model.PropertyDefinition;
31 import org.openecomp.sdc.be.model.jsontitan.operations.BaseOperation;
32 import org.openecomp.sdc.be.model.jsontitan.operations.TopologyTemplateOperation;
33 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
34 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
35 import org.openecomp.sdc.be.model.operations.impl.GroupTypeOperation;
36 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
37 import org.openecomp.sdc.be.resources.data.PropertyData;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.stereotype.Component;
42
43 import fj.data.Either;
44
45 @Component("vfModulesPropertiesAdding")
46 public class VfModulesPropertiesAdding {
47
48         private static Logger LOGGER = LoggerFactory.getLogger(ToscaTemplateRegeneration.class);
49         
50         @Autowired
51     private ToscaOperationFacade toscaOperationFacade;
52         
53         @Autowired
54     private TopologyTemplateOperation topologyTemplateOperation;
55         
56         @Resource(name ="group-type-operation-mig")
57     private GroupTypeOperation groupTypeOperation;
58         
59         @Resource(name = "property-operation-mig")
60     private PropertyOperation propertyOperation;
61         
62         
63         public boolean migrate(String groupsTypeYmlFilePath) {
64                 boolean result = true;
65                 Either<Map<org.openecomp.sdc.be.model.Component, GraphVertex>, StorageOperationStatus> getAllComponentsRes = null;
66                 GroupTypeDefinition vfModule;
67                 Either<List<GraphVertex>, TitanOperationStatus> getAllTopologyTemplatesRes = null;
68                 List<PropertyDefinition> newProperties = null;
69
70                 Either<GroupTypeDefinition, TitanOperationStatus> getGroupTypeVfModuleRes ;
71                 try{
72                         getGroupTypeVfModuleRes = groupTypeOperation.getGroupTypeByUid("org.openecomp.groups.VfModule.1.0.grouptype");
73                         
74                         if(getGroupTypeVfModuleRes.isRight()){
75                                  result = false;
76                         }
77                         if(result){
78                                 vfModule = getGroupTypeVfModuleRes.left().value();
79                                 newProperties = getNewVfModuleTypeProperties(getAllVfModuleTypePropertiesFromYaml(groupsTypeYmlFilePath), vfModule);
80                                 result = addNewPropertiesToGroupType(vfModule, newProperties);
81                         }
82                         if(result && CollectionUtils.isNotEmpty(newProperties)){
83                                 Map<GraphPropertyEnum, Object> propsHasNot = new EnumMap<>(GraphPropertyEnum.class);
84                                 propsHasNot.put(GraphPropertyEnum.IS_DELETED, true);
85                                 getAllTopologyTemplatesRes = toscaOperationFacade.getTitanDao().getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, null, propsHasNot, JsonParseFlagEnum.ParseAll);
86                                 if (getAllTopologyTemplatesRes.isRight() && getAllTopologyTemplatesRes.right().value() != TitanOperationStatus.NOT_FOUND) {
87                                         LOGGER.debug("Failed to fetch all non marked topology templates , propsHasNot {}, error {}", propsHasNot, getAllTopologyTemplatesRes.right().value());
88                                         result = false;
89                                 }
90                         }
91                         if(result && getAllTopologyTemplatesRes!=null && getAllTopologyTemplatesRes.isLeft()){
92                                 getAllComponentsRes = getAllContainerComponents(getAllTopologyTemplatesRes.left().value());
93                                 if(getAllComponentsRes.isRight()){
94                                         result = false;
95                                 }
96                         }
97                         if(result && getAllComponentsRes != null){
98                                 result = addNewVfModulesProperties(getAllComponentsRes.left().value(), newProperties);
99                         }
100                 } catch (Exception e){
101                         result = false;
102                 }
103                 finally{
104                         if(result){
105                                 toscaOperationFacade.commit();
106                         } else {
107                                 toscaOperationFacade.rollback();
108                         }
109                 }
110                 return result;
111         }
112
113         private boolean addNewVfModulesProperties(Map<org.openecomp.sdc.be.model.Component, GraphVertex> components, List<PropertyDefinition> newGroupTypeProperties) {
114                 boolean result = true;
115                 for(Map.Entry<org.openecomp.sdc.be.model.Component, GraphVertex> component : components.entrySet()){
116                         result = addNewPropertiesToVfModules(component, newGroupTypeProperties);
117                         if(!result){
118                                 break;
119                         }
120                 }
121                 return result;
122         }
123
124         private boolean addNewPropertiesToVfModules(Entry<org.openecomp.sdc.be.model.Component, GraphVertex> component, List<PropertyDefinition> newGroupTypeProperties) {
125                 boolean result = true;
126                 List<GroupDefinition> vfModules = null;
127                 if(CollectionUtils.isNotEmpty(component.getKey().getGroups())){
128                         vfModules = component.getKey().getGroups().stream().filter(g -> g.getType().equals(BaseOperation.VF_MODULE)).collect(Collectors.toList());
129                 }
130                 if(vfModules != null){
131                         vfModules.forEach(vfModule -> vfModule.getProperties().addAll(newGroupTypeProperties));
132                         StorageOperationStatus status = topologyTemplateOperation.updateToscaDataOfToscaElement(component.getValue(), EdgeLabelEnum.GROUPS, VertexTypeEnum.GROUPS, vfModules, JsonPresentationFields.NAME);
133                         if(status!= StorageOperationStatus.OK){
134                                 result = false;
135                         }
136                 }
137                 if(result && CollectionUtils.isNotEmpty(component.getKey().getComponentInstances())){
138                         result = addPropertiesToVfModuleInstances(component, newGroupTypeProperties);
139                 }
140                 return result;
141         }
142
143         private boolean addPropertiesToVfModuleInstances(Entry<org.openecomp.sdc.be.model.Component, GraphVertex> component, List<PropertyDefinition> newGroupTypeProperties) {
144                 boolean result = true;
145                 List<GroupInstance> vfModuleInstances;
146                 List<String> pathKeys;
147                 for(ComponentInstance componentInstance : component.getKey().getComponentInstances()){
148                         vfModuleInstances = null;
149                         if(CollectionUtils.isNotEmpty(componentInstance.getGroupInstances())){
150                                 vfModuleInstances = componentInstance.getGroupInstances()
151                                                 .stream()
152                                                 .filter(gi -> gi.getType().equals(BaseOperation.VF_MODULE))
153                                                 .collect(Collectors.toList());
154                         }
155                         if(vfModuleInstances != null){
156                                 for(GroupInstance vfModuleInstance :vfModuleInstances){
157                                         vfModuleInstance.getProperties().addAll(newGroupTypeProperties);
158                                         pathKeys = new ArrayList<>();
159                                         pathKeys.add(componentInstance.getUniqueId());
160                                         StorageOperationStatus status = topologyTemplateOperation
161                                                         .updateToscaDataDeepElementOfToscaElement(component.getValue(), EdgeLabelEnum.INST_GROUPS, VertexTypeEnum.INST_GROUPS, vfModuleInstance, pathKeys, JsonPresentationFields.NAME);
162                                         if(status!= StorageOperationStatus.OK){
163                                                 result = false;
164                                                 break;
165                                         }
166                                 }
167                                 if(!result){
168                                         break;
169                                 }
170                         }
171                 }
172                 return result;
173         }
174
175         private Either<Map<org.openecomp.sdc.be.model.Component, GraphVertex>, StorageOperationStatus> getAllContainerComponents(List<GraphVertex> componentsV) {
176                 Map<org.openecomp.sdc.be.model.Component, GraphVertex> foundComponents = new HashMap<>();
177                 Either<Map<org.openecomp.sdc.be.model.Component, GraphVertex>, StorageOperationStatus> result = null;
178                 for(GraphVertex componentV : componentsV){
179                         Either<org.openecomp.sdc.be.model.Component, StorageOperationStatus> getComponentRes = toscaOperationFacade.getToscaElement(componentV);
180                         if(getComponentRes.isRight()){
181                                 result = Either.right(getComponentRes.right().value());
182                                 break;
183                         }
184                         foundComponents.put(getComponentRes.left().value(), componentV);
185                 }
186                 if(result == null){
187                         result = Either.left(foundComponents);
188                 }
189                 return result;
190         }
191         
192         
193         private boolean addNewPropertiesToGroupType(GroupTypeDefinition vfModule, List<PropertyDefinition> newProperties) {
194                 boolean result = true;
195                 Either<Map<String, PropertyData>, TitanOperationStatus> addPropertiesRes = propertyOperation
196                                 .addPropertiesToElementType(vfModule.getUniqueId(), NodeTypeEnum.GroupType, newProperties);
197                 if(addPropertiesRes.isRight()){
198                         result = false;
199                 }
200                 return result;
201         }
202
203         private List<PropertyDefinition> getAllVfModuleTypePropertiesFromYaml(String groupsTypeYmlFilePath) {
204                 List<DataTypeDefinition> groupTypes = DataTypesUpdate.extractDataTypesFromYaml(groupsTypeYmlFilePath);
205                 DataTypeDefinition vfModule = groupTypes.stream().filter(g -> g.getName().equals(BaseOperation.VF_MODULE)).findFirst().orElse(null);
206                 return vfModule.getProperties();
207         }
208         
209         private List<PropertyDefinition> getNewVfModuleTypeProperties(List<PropertyDefinition> allVfModuleTypeProperties, GroupTypeDefinition vfModule) {
210                 Map<String, PropertyDefinition> existingVfModuleTypeProperties = vfModule.getProperties()
211                                 .stream()
212                                 .collect(Collectors.toMap(p -> p.getName(), p -> p));
213                 
214                 List<PropertyDefinition> newGroupTypeProperties = new ArrayList<>();
215                 for(PropertyDefinition property : allVfModuleTypeProperties){
216                         if(!existingVfModuleTypeProperties.containsKey(property.getName())){
217                                 newGroupTypeProperties.add(property);
218                         }
219                 }
220                 return newGroupTypeProperties;
221         }
222
223         public String description() {
224                 return "vfModulesPropertiesAdding";
225         }
226
227 }