89ac6f7cb615606923eba8639bfd79310f0a50af
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.asdctool.impl.migration.v1707;
22
23 import java.util.ArrayList;
24 import java.util.EnumMap;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Map.Entry;
29 import java.util.stream.Collectors;
30
31 import javax.annotation.Resource;
32
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;
64
65 import fj.data.Either;
66
67 @Component("vfModulesPropertiesAdding")
68 public class VfModulesPropertiesAdding {
69
70         private static Logger LOGGER = LoggerFactory.getLogger(VfModulesPropertiesAdding.class);
71         
72         @Autowired
73     private ToscaOperationFacade toscaOperationFacade;
74         
75         @Autowired
76     private TopologyTemplateOperation topologyTemplateOperation;
77         
78         @Resource(name ="group-type-operation-mig")
79     private GroupTypeOperation groupTypeOperation;
80         
81         @Resource(name = "property-operation-mig")
82     private PropertyOperation propertyOperation;
83         
84         
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;
93                 try{
94                         LOGGER.debug("Going to fetch {}. ", vfModuleUid);
95                         getGroupTypeVfModuleRes = groupTypeOperation.getGroupTypeByUid(vfModuleUid);
96                         
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());
99                                 result = false;
100                         }
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);
103                                 return true;
104                         }
105                         if(result){
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));
110                                 if(!result){
111                                         LOGGER.debug("Failed to add the new properties {} to org.openecomp.groups.VfModule.1.0.grouptype. ");
112                                 }
113                         }
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());
120                                         result = false;
121                                 }
122                         }
123                         if(result && getAllTopologyTemplatesRes!=null && getAllTopologyTemplatesRes.isLeft()){
124                                 result = addNewVfModulesProperties(getAllTopologyTemplatesRes.left().value(), updatedProperties);
125                         }
126                 } catch (Exception e){
127                         result = false;
128                 }
129                 finally{
130                         if(result){
131                                 toscaOperationFacade.commit();
132                         } else {
133                                 toscaOperationFacade.rollback();
134                         }
135                 }
136                 return result;
137         }
138
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);
144                         if(!result){
145                                 LOGGER.debug("Failed to add the new properties {} to component {}. ", updatedProperties, component.getUniqueId());
146                                 break;
147                         }
148                         toscaOperationFacade.commit();
149                 }
150                 return result;
151         }
152
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());
159                         result = false;
160                 }
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());
163                 }
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());
169                                 result = false;
170                         }
171                 }
172                 if(result && CollectionUtils.isNotEmpty(getToscaElementRes.left().value().getComponentInstances())){
173                         result = addPropertiesToVfModuleInstances(getToscaElementRes.left().value(), componentV, updatedProperties);
174                 }
175                 return result;
176         }
177
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));
180                 
181                 for(PropertyDefinition property : updatedProperties){
182                         if(!propertiesMap.containsKey(property.getName())){
183                                 vfModuleProperties.add(property);
184                         }
185                 }
186         }
187
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()
197                                                 .stream()
198                                                 .filter(gi -> gi.getType().equals(BaseOperation.VF_MODULE))
199                                                 .collect(Collectors.toList());
200                         }
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){
209                                                 result = false;
210                                                 LOGGER.debug("Failed to add the new properties {} to group instances of component {}. ", updatedProperties, componentV.getUniqueId());
211                                                 break;
212                                         }
213                                 }
214                                 if(!result){
215                                         LOGGER.debug("Failed to add the new properties {} to group instances of component {}. ", updatedProperties, componentV.getUniqueId());
216                                         break;
217                                 }
218                         }
219                 }
220                 return result;
221         }
222         
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()){
228                         result = false;
229                 }
230                 return result;
231         }
232
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();
237         }
238         
239         private List<PropertyDefinition> getNewVfModuleTypeProperties(List<PropertyDefinition> allVfModuleTypeProperties, GroupTypeDefinition vfModule) {
240                 Map<String, PropertyDefinition> existingVfModuleTypeProperties = vfModule.getProperties()
241                                 .stream()
242                                 .collect(Collectors.toMap(p -> p.getName(), p -> p));
243                 
244                 List<PropertyDefinition> newGroupTypeProperties = new ArrayList<>();
245                 for(PropertyDefinition property : allVfModuleTypeProperties){
246                         if(!existingVfModuleTypeProperties.containsKey(property.getName())){
247                                 newGroupTypeProperties.add(property);
248                         }
249                 }
250                 return newGroupTypeProperties;
251         }
252
253         public String description() {
254                 return "vfModulesPropertiesAdding";
255         }
256
257 }