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.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;
43 import fj.data.Either;
45 @Component("vfModulesPropertiesAdding")
46 public class VfModulesPropertiesAdding {
48 private static Logger LOGGER = LoggerFactory.getLogger(ToscaTemplateRegeneration.class);
51 private ToscaOperationFacade toscaOperationFacade;
54 private TopologyTemplateOperation topologyTemplateOperation;
56 @Resource(name ="group-type-operation-mig")
57 private GroupTypeOperation groupTypeOperation;
59 @Resource(name = "property-operation-mig")
60 private PropertyOperation propertyOperation;
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;
70 Either<GroupTypeDefinition, TitanOperationStatus> getGroupTypeVfModuleRes ;
72 getGroupTypeVfModuleRes = groupTypeOperation.getGroupTypeByUid("org.openecomp.groups.VfModule.1.0.grouptype");
74 if(getGroupTypeVfModuleRes.isRight()){
78 vfModule = getGroupTypeVfModuleRes.left().value();
79 newProperties = getNewVfModuleTypeProperties(getAllVfModuleTypePropertiesFromYaml(groupsTypeYmlFilePath), vfModule);
80 result = addNewPropertiesToGroupType(vfModule, newProperties);
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());
91 if(result && getAllTopologyTemplatesRes!=null && getAllTopologyTemplatesRes.isLeft()){
92 getAllComponentsRes = getAllContainerComponents(getAllTopologyTemplatesRes.left().value());
93 if(getAllComponentsRes.isRight()){
97 if(result && getAllComponentsRes != null){
98 result = addNewVfModulesProperties(getAllComponentsRes.left().value(), newProperties);
100 } catch (Exception e){
105 toscaOperationFacade.commit();
107 toscaOperationFacade.rollback();
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);
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());
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){
137 if(result && CollectionUtils.isNotEmpty(component.getKey().getComponentInstances())){
138 result = addPropertiesToVfModuleInstances(component, newGroupTypeProperties);
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()
152 .filter(gi -> gi.getType().equals(BaseOperation.VF_MODULE))
153 .collect(Collectors.toList());
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){
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());
184 foundComponents.put(getComponentRes.left().value(), componentV);
187 result = Either.left(foundComponents);
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()){
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();
209 private List<PropertyDefinition> getNewVfModuleTypeProperties(List<PropertyDefinition> allVfModuleTypeProperties, GroupTypeDefinition vfModule) {
210 Map<String, PropertyDefinition> existingVfModuleTypeProperties = vfModule.getProperties()
212 .collect(Collectors.toMap(p -> p.getName(), p -> p));
214 List<PropertyDefinition> newGroupTypeProperties = new ArrayList<>();
215 for(PropertyDefinition property : allVfModuleTypeProperties){
216 if(!existingVfModuleTypeProperties.containsKey(property.getName())){
217 newGroupTypeProperties.add(property);
220 return newGroupTypeProperties;
223 public String description() {
224 return "vfModulesPropertiesAdding";