[SDC-29] rebase continue work to align source
[sdc.git] / asdctool / src / main / java / org / openecomp / sdc / asdctool / impl / migration / v1707 / ToscaNamesUpdate.java
1 package org.openecomp.sdc.asdctool.impl.migration.v1707;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.Map;
6 import java.util.function.Function;
7
8 import org.apache.commons.lang3.tuple.ImmutablePair;
9 import org.apache.commons.lang3.tuple.ImmutableTriple;
10 import org.apache.tinkerpop.gremlin.structure.Vertex;
11 import org.openecomp.sdc.asdctool.impl.migration.Migration1707Task;
12 import org.openecomp.sdc.be.dao.graph.GraphElementFactory;
13 import org.openecomp.sdc.be.dao.graph.datatype.GraphElementTypeEnum;
14 import org.openecomp.sdc.be.dao.graph.datatype.GraphNode;
15 import org.openecomp.sdc.be.dao.titan.TitanGenericDao;
16 import org.openecomp.sdc.be.dao.titan.TitanGraphClient;
17 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
18 import org.openecomp.sdc.be.datatypes.components.ResourceMetadataDataDefinition;
19 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
20 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
21 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
22 import org.openecomp.sdc.be.resources.data.AttributeData;
23 import org.openecomp.sdc.be.resources.data.AttributeValueData;
24 import org.openecomp.sdc.be.resources.data.CapabilityData;
25 import org.openecomp.sdc.be.resources.data.CapabilityTypeData;
26 import org.openecomp.sdc.be.resources.data.DataTypeData;
27 import org.openecomp.sdc.be.resources.data.GroupData;
28 import org.openecomp.sdc.be.resources.data.GroupTypeData;
29 import org.openecomp.sdc.be.resources.data.InputValueData;
30 import org.openecomp.sdc.be.resources.data.InputsData;
31 import org.openecomp.sdc.be.resources.data.PolicyTypeData;
32 import org.openecomp.sdc.be.resources.data.PropertyData;
33 import org.openecomp.sdc.be.resources.data.PropertyValueData;
34 import org.openecomp.sdc.be.resources.data.RelationshipInstData;
35 import org.openecomp.sdc.be.resources.data.RelationshipTypeData;
36 import org.openecomp.sdc.be.resources.data.RequirementData;
37 import org.openecomp.sdc.be.resources.data.ResourceMetadataData;
38
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.stereotype.Component;
43
44 import com.thinkaurelius.titan.core.TitanVertex;
45
46 import fj.data.Either;
47
48 @Component("toscaNamesUpdate")
49 public class ToscaNamesUpdate implements Migration1707Task {
50         private static Logger log = LoggerFactory.getLogger(ToscaNamesUpdate.class.getName());
51
52         @Override
53         public String description() {
54                 return "toscaNamesUpdate";
55         }
56
57         @Autowired
58         protected TitanGenericDao titanGenericDao;
59
60         @Override
61         public boolean migrate() {
62                 boolean result = true;
63                 List<ImmutableTriple<NodeTypeEnum, Class<GraphNode>, Function<GraphNode, ImmutablePair<String, GraphNode>>>> updateInfoList = new ArrayList<>();
64                 for (NodeTypeEnum nodeType : NodeTypeEnum.values()){
65                         ImmutableTriple<NodeTypeEnum, Class<GraphNode>, Function<GraphNode, ImmutablePair<String, GraphNode>>> updateInfo = getInfo(nodeType);
66                         if(null == updateInfo)
67                                 continue;
68                         updateInfoList.add(updateInfo);
69                 }
70                 
71                 for(ImmutableTriple<NodeTypeEnum, Class<GraphNode>, Function<GraphNode, ImmutablePair<String, GraphNode>>> nodeTypeData : updateInfoList){
72                         log.debug("before updating namespace on nodeType {}", nodeTypeData.left.getName());
73                         result = updateNamespaceByNodeType(nodeTypeData);
74                         if(!result){
75                                 log.debug("alignNamespace procedure failed during execution of updating namespace on nodeType {}", nodeTypeData.left.getName());
76                                 return false;
77                         }
78                 }
79                 return true;
80         }
81
82         private <T extends GraphNode> ImmutableTriple<NodeTypeEnum, Class<T>, Function<T, ImmutablePair<String, T>>> getInfo(NodeTypeEnum nodeType) {
83                 switch (nodeType) {
84                 case Resource:
85                         Function<ResourceMetadataData, ImmutablePair<String, ResourceMetadataData>> resourceFunc = r -> updateResource(r);
86                         return new ImmutableTriple(nodeType, ResourceMetadataData.class, resourceFunc);
87                 case GroupType:
88                         Function<GroupTypeData, ImmutablePair<String, GroupTypeData>> groupTypeFunc = g -> updateGroupType(g);
89                         return new ImmutableTriple(nodeType, GroupTypeData.class, groupTypeFunc);
90                 case Group:
91                         Function<GroupData, ImmutablePair<String, GroupData>> groupFunc = g -> updateGroupNode(g);
92                         return new ImmutableTriple(nodeType, GroupData.class, groupFunc);
93                 case PolicyType:
94                         Function<PolicyTypeData, ImmutablePair<String , PolicyTypeData>> policyFunc = p -> updatePolicyType(p);
95                         return new ImmutableTriple(nodeType, PolicyTypeData.class, policyFunc);
96                 case RelationshipType:
97                         Function<RelationshipTypeData, ImmutablePair<String, RelationshipTypeData>> relTypeFunc = r -> updateRelationshipType(r);
98                         return new ImmutableTriple(nodeType, RelationshipTypeData.class, relTypeFunc);
99                 case RelationshipInst:
100                         Function<RelationshipInstData, ImmutablePair<String, RelationshipInstData>> relFunc = r -> updateRelationshipNode(r);
101                         return new ImmutableTriple(nodeType, RelationshipInstData.class, relFunc);
102                 case Requirement:
103                         Function<RequirementData, ImmutablePair<String, RequirementData>> reqFunc = r -> updateRequirementType(r);
104                         return new ImmutableTriple(nodeType, RequirementData.class, reqFunc);
105                 case CapabilityType:
106                         Function<CapabilityTypeData, ImmutablePair<String, CapabilityTypeData>> capTypeFunc = c -> updateCapabilityType(c);
107                         return new ImmutableTriple(nodeType, CapabilityTypeData.class, capTypeFunc);
108                 case Capability:
109                         Function<CapabilityData, ImmutablePair<String, CapabilityData>> capFunc = c -> updateCapabilityNode(c);
110                         return new ImmutableTriple(nodeType, CapabilityData.class, capFunc);
111                 case Property:
112                         Function<PropertyData, ImmutablePair<String, PropertyData>> propFunc = p -> updatePropNode(p);
113                         return new ImmutableTriple(nodeType, PropertyData.class, propFunc);
114                 case PropertyValue:
115                         Function<PropertyValueData, ImmutablePair<String, PropertyValueData>> propValueFunc = p -> updatePropValueNode(p);
116                         return new ImmutableTriple(nodeType, PropertyValueData.class, propValueFunc);
117                 case Attribute: 
118                         Function<AttributeData, ImmutablePair<String, AttributeData>> attrFunc = a -> updateAttributeNode(a);
119                         return new ImmutableTriple(nodeType, AttributeData.class, attrFunc);
120                 case AttributeValue:
121                         Function<AttributeValueData, ImmutablePair<String, AttributeValueData>> attrValueFunc = a -> updateAttrValueNode(a);
122                         return new ImmutableTriple(nodeType, AttributeValueData.class, attrValueFunc);
123                 case Input:
124                         Function<InputsData, ImmutablePair<String, InputsData>> inputFunc = i -> updateInputNode(i);
125                         return new ImmutableTriple(nodeType, InputsData.class, inputFunc);
126                 case InputValue:
127                         Function<InputValueData, ImmutablePair<String, InputValueData>> inputValueFunc = i -> updateInputValueNode(i);
128                         return new ImmutableTriple(nodeType, InputValueData.class, inputValueFunc);
129                 case DataType:
130                         Function<DataTypeData, ImmutablePair<String, DataTypeData>> dataTypeFunc = d -> updateDataType(d);
131                         return new ImmutableTriple(nodeType, DataTypeData.class, dataTypeFunc);
132                 default:
133                         return null;
134                 }
135
136         }
137         
138         
139
140         private boolean ifRight(TitanOperationStatus status){
141                 return TitanOperationStatus.NOT_FOUND == status;
142         }
143         
144         private <T extends GraphNode> boolean ifLeft(List<T> allNodes, ImmutableTriple<NodeTypeEnum, Class<T>, Function<T, ImmutablePair<String, T>>> nodeTypeData){
145                 boolean result = true;
146                 try {
147                         for (T node : allNodes) {
148                                 ImmutablePair<String, T> nodeToUpdate = nodeTypeData.right.apply(node);
149                                 Either<T, TitanOperationStatus> updatedNode = updateNodeIncludingUID(nodeToUpdate.left, nodeToUpdate.right, nodeTypeData.middle);
150                                 if (updatedNode.isRight()) {
151                                         result = false;
152                                         break;
153                                 }
154                         }
155                 } finally {
156                         if (!result) {
157                                 titanGenericDao.rollback();
158                         } else {
159                                 titanGenericDao.commit();
160                         }
161                 }
162                 return result;
163         }
164         
165         private <T extends GraphNode> boolean updateNamespaceByNodeType(ImmutableTriple<NodeTypeEnum, Class<T>, Function<T, ImmutablePair<String, T>>> nodeTypeData) {
166                 Either<List<T>, TitanOperationStatus> getAllNodes = titanGenericDao.getByCriteria(nodeTypeData.left, null, nodeTypeData.middle);
167                 return getAllNodes.either(list -> ifLeft(list, nodeTypeData), status -> ifRight(status));
168         }
169
170         private ImmutablePair<String, ResourceMetadataData> updateResource(ResourceMetadataData resource) {
171                 String toscaResourceName = updateNamespace(((ResourceMetadataDataDefinition) resource.getMetadataDataDefinition()).getToscaResourceName());
172                 ((ResourceMetadataDataDefinition) resource.getMetadataDataDefinition()).setToscaResourceName(toscaResourceName);
173                 return new ImmutablePair<>((String) resource.getUniqueId(), resource);
174         }
175
176         private ImmutablePair<String, GroupTypeData> updateGroupType(GroupTypeData group) {
177                 String originId = group.getUniqueId();
178                 group.getGroupTypeDataDefinition().setUniqueId(updateNamespace(originId));
179                 String type = updateNamespace(group.getGroupTypeDataDefinition().getType());
180                 group.getGroupTypeDataDefinition().setType(type);
181                 return new ImmutablePair<>(originId, group);
182         }
183
184         private ImmutablePair<String, GroupData> updateGroupNode(GroupData group) {
185                 String type = updateNamespace(group.getGroupDataDefinition().getType());
186                 group.getGroupDataDefinition().setType(type);
187                 return new ImmutablePair<>((String) group.getUniqueId(), group);
188         }
189         
190
191         private ImmutablePair<String, PolicyTypeData> updatePolicyType(PolicyTypeData policy) {
192                 String originId = policy.getUniqueId();
193                 policy.getPolicyTypeDataDefinition().setUniqueId(updateNamespace(originId));
194                 String type = updateNamespace(policy.getPolicyTypeDataDefinition().getType());
195                 policy.getPolicyTypeDataDefinition().setType(type);
196                 return new ImmutablePair<>(originId, policy);
197         }
198
199         private ImmutablePair<String, RelationshipTypeData> updateRelationshipType(RelationshipTypeData relation) {
200                 String type = updateNamespace(relation.getRelationshipTypeDataDefinition().getType());
201                 relation.getRelationshipTypeDataDefinition().setType(type);
202                 List<String> validSources = relation.getRelationshipTypeDataDefinition().getValidSourceTypes();
203                 if(null != validSources){
204                         List<String> validSourceTypes = new ArrayList<>();
205                         for (String validSourceType : validSources) {
206                                 validSourceTypes.add(updateNamespace(validSourceType));
207                         }
208                         relation.getRelationshipTypeDataDefinition().setValidSourceTypes(validSourceTypes);
209                 }
210                 return new ImmutablePair<>(relation.getUniqueId(), relation);
211         }
212
213         private ImmutablePair<String, RelationshipInstData> updateRelationshipNode(RelationshipInstData relation) {
214                 String type = updateNamespace(relation.getType());
215                 relation.setType(type);
216                 return new ImmutablePair<>(relation.getUniqueId(), relation);
217         }
218
219         private ImmutablePair<String, RequirementData> updateRequirementType(RequirementData req) {
220                 String node = req.getNode();
221                 if(null != node)
222                         req.setNode(updateNamespace(node));
223                 String type = updateNamespace(req.getRelationshipType());
224                 req.setRelationshipType(type);
225                 return new ImmutablePair<>(req.getUniqueId(), req);
226         }
227
228         private ImmutablePair<String, CapabilityTypeData> updateCapabilityType(CapabilityTypeData capType) {
229                 String originId = capType.getUniqueId();
230                 capType.getCapabilityTypeDataDefinition().setUniqueId(updateNamespace(originId));
231                 String type = updateNamespace(capType.getCapabilityTypeDataDefinition().getType());
232                 capType.getCapabilityTypeDataDefinition().setType(type);
233                 List<String> validSources = capType.getCapabilityTypeDataDefinition().getValidSourceTypes();
234                 if(null != validSources){
235                         List<String> validSourceTypes = new ArrayList<>();
236                         for (String validSourceType : validSources) {
237                                 validSourceTypes.add(updateNamespace(validSourceType));
238                         }
239                         capType.getCapabilityTypeDataDefinition().setValidSourceTypes(validSourceTypes);
240                 }       
241                 return new ImmutablePair<>(originId, capType);
242
243         }
244
245         private ImmutablePair<String, CapabilityData> updateCapabilityNode(CapabilityData capNode) {
246                 List<String> validSources = capNode.getValidSourceTypes();
247                 if(null != validSources){
248                         List<String> validSourceTypes = new ArrayList<>();
249                         for (String validSourceType : validSources) {
250                                 validSourceTypes.add(updateNamespace(validSourceType));
251                         }
252                         capNode.setValidSourceTypes(validSourceTypes);
253                 }               
254                 return new ImmutablePair<>(capNode.getUniqueId(), capNode);
255         }
256
257
258         private ImmutablePair<String, PropertyData> updatePropNode(PropertyData propType) {
259                 String originId = (String)propType.getUniqueId();
260                 propType.getPropertyDataDefinition().setUniqueId(updateNamespace(originId));
261                 String type = updateNamespace(propType.getPropertyDataDefinition().getType());
262                 propType.getPropertyDataDefinition().setType(type);
263                 if ("list".equalsIgnoreCase(type) || "map".equalsIgnoreCase(type)){
264                         SchemaDefinition schema = propType.getPropertyDataDefinition().getSchema();
265                         if(null != schema && null != schema.getProperty())
266                                 handleSchemaTypeDef(schema.getProperty());
267                 }
268                 return new ImmutablePair<>(originId, propType);
269         }
270
271         private ImmutablePair<String, PropertyValueData> updatePropValueNode(PropertyValueData prop) {
272                 String type = updateNamespace(prop.getType());
273                 prop.setType(type);
274                 return new ImmutablePair<>(prop.getUniqueId(), prop);
275         }
276         
277         private ImmutablePair<String, AttributeValueData> updateAttrValueNode(AttributeValueData attr) {
278                 String type = updateNamespace(attr.getType());
279                 attr.setType(type);
280                 return new ImmutablePair<>(attr.getUniqueId(), attr);
281         }
282         
283         private ImmutablePair<String, InputValueData> updateInputValueNode(InputValueData input) {
284                 String type = updateNamespace(input.getType());
285                 input.setType(type);
286                 return new ImmutablePair<>(input.getUniqueId(), input);
287         }
288         
289         private ImmutablePair<String, InputsData> updateInputNode(InputsData input){
290                 String type = updateNamespace(input.getPropertyDataDefinition().getType());
291                 input.getPropertyDataDefinition().setType(type);
292                 if ("list".equalsIgnoreCase(type) || "map".equalsIgnoreCase(type)){
293                         SchemaDefinition schema = input.getPropertyDataDefinition().getSchema();
294                         if(null != schema && null != schema.getProperty())
295                                 handleSchemaTypeDef(schema.getProperty());
296                 }
297                 return new ImmutablePair<>((String)input.getUniqueId(), input);
298         }
299
300
301         private void handleSchemaTypeDef(PropertyDataDefinition schemaProp) {
302                 String schemaType = updateNamespace(schemaProp.getType());
303                 schemaProp.setType(schemaType);
304         }
305
306         private ImmutablePair<String, DataTypeData> updateDataType(DataTypeData dataType) {
307                 String originId = dataType.getUniqueId();
308                 dataType.getDataTypeDataDefinition().setUniqueId(updateNamespace(originId));
309                 String name = updateNamespace(dataType.getDataTypeDataDefinition().getName());
310                 dataType.getDataTypeDataDefinition().setName(name);
311                 String derivedFromName = updateNamespace(dataType.getDataTypeDataDefinition().getDerivedFromName());
312                 dataType.getDataTypeDataDefinition().setDerivedFromName(derivedFromName);
313                 return new ImmutablePair<>(originId, dataType);
314
315         }
316         
317         private ImmutablePair<String, AttributeData> updateAttributeNode(AttributeData attr){
318                 String type = updateNamespace(attr.getAttributeDataDefinition().getType());
319                 attr.getAttributeDataDefinition().setType(type);
320                 if("list".equalsIgnoreCase(type) || "map".equalsIgnoreCase(type)){
321                         SchemaDefinition schema = attr.getAttributeDataDefinition().getSchema();
322                         if(null != schema && null != schema.getProperty())
323                                 handleSchemaTypeDef(schema.getProperty());
324                 }
325                 return new ImmutablePair<>(attr.getUniqueId(), attr);
326         }
327         
328         
329
330         private String updateNamespace(String oldName) {
331                 if (oldName == null) {
332                         return null;
333                 }
334                 String name = oldName.replace("com.att.d2.", "org.openecomp.");
335                 // correcting naming convention
336                 return name.replace("org.openecomp.resources.", "org.openecomp.resource.");
337         }
338         
339         private <T extends GraphNode> T onSuccess(TitanVertex vertex, GraphNode node, Class<T> clazz){
340                 Map<String, Object> newProp = titanGenericDao.getProperties(vertex);
341                 return GraphElementFactory.createElement(node.getLabel(), GraphElementTypeEnum.Node, newProp, clazz);
342         }
343         
344         private <T extends GraphNode> Either<T, TitanOperationStatus> handleNode(Vertex vertex, GraphNode node, Class<T> clazz){
345                 try {
346                         
347                         Map<String, Object> mapProps = node.toGraphMap();
348
349                         for (Map.Entry<String, Object> entry : mapProps.entrySet()) {
350                                 vertex.property(entry.getKey(), entry.getValue());
351                         }
352
353                         Either<TitanVertex, TitanOperationStatus> vertexByPropertyAndLabel = titanGenericDao.getVertexByProperty(node.getUniqueIdKey(), node.getUniqueId());
354                         return vertexByPropertyAndLabel.either(v -> Either.left(onSuccess(v, node, clazz)), status -> Either.right(status));
355                         
356                 } catch (Exception e) {
357                         if (log.isDebugEnabled()) {
358                                 log.debug("Failed to update node for {}", node.getKeyValueId(), e);
359                         }
360                         return Either.right(TitanGraphClient.handleTitanException(e));
361                 }
362         }
363         
364         private <T extends GraphNode> Either<T, TitanOperationStatus> updateNodeIncludingUID(String originId, GraphNode node, Class<T> clazz) {
365                 Either<TitanVertex, TitanOperationStatus> vertexByProperty = titanGenericDao.getVertexByProperty(node.getUniqueIdKey(), originId);
366                 return vertexByProperty.either(vertex -> handleNode(vertex, node, clazz), status -> Either.right(status));      
367         }
368 }