[SDC] Onboarding 1710 rebase.
[sdc.git] / openecomp-be / tools / migration / 1702_to_1707_zusammen / src / main / java / org / openecomp / core / migration / convertors / FeatureGroupConvertor.java
1 package org.openecomp.core.migration.convertors;
2
3 import com.amdocs.zusammen.datatypes.item.ElementContext;
4 import com.amdocs.zusammen.datatypes.item.Info;
5 import com.amdocs.zusammen.datatypes.item.Relation;
6 import com.amdocs.zusammen.plugin.statestore.cassandra.dao.types.ElementEntityContext;
7 import com.amdocs.zusammen.sdk.collaboration.types.CollaborationElement;
8 import org.openecomp.core.migration.store.ElementHandler;
9 import org.openecomp.sdc.logging.api.Logger;
10 import org.openecomp.sdc.logging.api.LoggerFactory;
11 import org.openecomp.sdc.vendorlicense.dao.impl.zusammen.RelationType;
12 import org.openecomp.sdc.vendorlicense.dao.impl.zusammen.StructureElement;
13 import org.openecomp.sdc.vendorlicense.dao.impl.zusammen.VlmZusammenUtil;
14 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity;
15
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.HashSet;
19 import java.util.List;
20 import java.util.Set;
21 import java.util.stream.Collectors;
22
23 /**
24  * Created by ayalaben on 4/25/2017
25  */
26 public class FeatureGroupConvertor {
27
28     private static Logger logger = LoggerFactory.getLogger(FeatureGroupConvertor.class);
29     private static Set<String> FeatureGroupsLoaded = new HashSet<>();
30
31     public static ElementEntityContext convertFeatureGroupToElementContext(FeatureGroupEntity featureGroupEntity) {
32
33         return new ElementEntityContext("GLOBAL_USER", new
34                 ElementContext(featureGroupEntity.getVendorLicenseModelId(), featureGroupEntity.getVersion().toString()));
35     }
36
37     public static CollaborationElement[] convertFeatureGroupToElement(FeatureGroupEntity featureGroupEntity) {
38 //        printMessage(logger, "source FeatureGroupEntity -> " + featureGroupEntity.toString());
39         CollaborationElement[] elements;
40         List<String> featureGroupNamespace = getFeatureGroupNamespace(featureGroupEntity);
41
42         int index = 0;
43         String featureGroupsEntityId = StructureElement.FeatureGroups.name();
44         String uniqueId = featureGroupEntity.getVendorLicenseModelId() + "_" + featureGroupEntity.getVersion().toString();
45
46         if (FeatureGroupsLoaded.contains(uniqueId)) {
47             elements = new CollaborationElement[1];
48         } else {
49             FeatureGroupsLoaded.add(uniqueId);
50             elements = new CollaborationElement[2];
51             elements[index] = ElementHandler.getElementEntity(
52                     featureGroupEntity.getVendorLicenseModelId(), featureGroupEntity.getVersion().toString(),
53                     featureGroupsEntityId, featureGroupNamespace,
54                     ElementHandler.getStructuralElementInfo(StructureElement.FeatureGroups.name()),
55                     null, null, null);
56             index++;
57         }
58
59         featureGroupNamespace.add(featureGroupsEntityId);
60
61         elements[index] = ElementHandler.getElementEntity(
62                 featureGroupEntity.getVendorLicenseModelId(), featureGroupEntity.getVersion().toString(),
63                 featureGroupEntity.getId(), featureGroupNamespace, getFeatureGroupInfo(featureGroupEntity),
64                 getAllFeatureGroupRelations(featureGroupEntity), null, null);
65
66         return elements;
67     }
68
69     private static Collection<Relation> getAllFeatureGroupRelations(FeatureGroupEntity featureGroup) {
70         Collection<Relation> relations = new ArrayList<>();
71
72         relations.addAll(featureGroup.getEntitlementPoolIds().stream().map(rel ->
73                 VlmZusammenUtil.createRelation( RelationType.FeatureGroupToEntitlmentPool, rel))
74                 .collect(Collectors.toList()));
75
76         relations.addAll(featureGroup.getLicenseKeyGroupIds().stream().map(rel ->
77                 VlmZusammenUtil.createRelation( RelationType.FeatureGroupToLicenseKeyGroup, rel))
78                 .collect(Collectors.toList()));
79
80         relations.addAll(featureGroup.getReferencingLicenseAgreements().stream().map(rel ->
81                 VlmZusammenUtil.createRelation( RelationType.FeatureGroupToReferencingLicenseAgreement,
82                         rel)).collect(Collectors.toList()));
83
84         return relations;
85     }
86
87     private static Info getFeatureGroupInfo(FeatureGroupEntity featureGroup) {
88
89         Info info = new Info();
90         info.setName(featureGroup.getName());
91         info.setDescription(featureGroup.getDescription());
92         info.addProperty("partNumber", featureGroup.getPartNumber());
93         info.addProperty("manufacturerReferenceNumber", featureGroup.getManufacturerReferenceNumber());
94
95         return info;
96     }
97
98
99     private static List<String> getFeatureGroupNamespace(FeatureGroupEntity featureGroupEntity) {
100         return ElementHandler.getElementPath("");
101     }
102 }