Add collaboration feature
[sdc.git] / openecomp-be / lib / openecomp-sdc-vendor-license-lib / openecomp-sdc-vendor-license-core / src / main / java / org / openecomp / sdc / vendorlicense / dao / impl / zusammen / convertor / ElementToLicenseKeyGroupConvertor.java
1 package org.openecomp.sdc.vendorlicense.dao.impl.zusammen.convertor;
2
3 import com.amdocs.zusammen.adaptor.inbound.api.types.item.Element;
4 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ElementInfo;
5 import com.amdocs.zusammen.datatypes.item.Info;
6 import com.amdocs.zusammen.datatypes.item.Relation;
7 import org.openecomp.convertor.ElementConvertor;
8 import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity;
9 import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyType;
10 import org.openecomp.sdc.vendorlicense.dao.types.MultiChoiceOrOther;
11 import org.openecomp.sdc.vendorlicense.dao.types.OperationalScope;
12 import org.openecomp.sdc.vendorlicense.dao.types.ThresholdUnit;
13
14 import java.util.Collection;
15 import java.util.HashSet;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.Set;
19 import java.util.stream.Collectors;
20
21 import static org.openecomp.sdc.vendorlicense.dao.impl.zusammen.VlmZusammenUtil.toInteger;
22
23
24 public class ElementToLicenseKeyGroupConvertor extends ElementConvertor {
25   @Override
26   public LicenseKeyGroupEntity convert(Element element) {
27     if (element == null) {
28       return null;
29     }
30     return mapElementToLicenseKeyGroupEntity(element);
31
32   }
33
34   @Override
35   public LicenseKeyGroupEntity convert(ElementInfo elementInfo) {
36     if (elementInfo == null) {
37       return null;
38     }
39     return mapElementInfoToLicenseKeyGroupEntity(elementInfo);
40
41   }
42
43   private LicenseKeyGroupEntity mapElementToLicenseKeyGroupEntity(
44       Element element) {
45     LicenseKeyGroupEntity licenseKeyGroup =
46         new LicenseKeyGroupEntity();
47     licenseKeyGroup.setId(element.getElementId().getValue());
48     mapInfoToLicenseKeyGroup(licenseKeyGroup, element.getInfo());
49     mapRelationsToLicenseKeyGroup(licenseKeyGroup, element.getRelations());
50     return licenseKeyGroup;
51   }
52
53
54   private LicenseKeyGroupEntity mapElementInfoToLicenseKeyGroupEntity(ElementInfo elementInfo) {
55     LicenseKeyGroupEntity licenseKeyGroup =
56         new LicenseKeyGroupEntity();
57     licenseKeyGroup.setId(elementInfo.getId().getValue());
58
59     mapInfoToLicenseKeyGroup(licenseKeyGroup, elementInfo.getInfo());
60     mapRelationsToLicenseKeyGroup(licenseKeyGroup, elementInfo.getRelations());
61     return licenseKeyGroup;
62   }
63
64   private void mapInfoToLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup, Info info) {
65     licenseKeyGroup.setName(info.getName());
66     licenseKeyGroup.setDescription(info.getDescription());
67     licenseKeyGroup.setVersionUuId(info.getProperty("version_uuid"));
68     licenseKeyGroup.setType(LicenseKeyType.valueOf(info.getProperty("LicenseKeyType")));
69     licenseKeyGroup.setOperationalScope(
70         getOperationalScopeMultiChoiceOrOther(info.getProperty("operational_scope")));
71     licenseKeyGroup.setStartDate(info.getProperty("startDate"));
72     licenseKeyGroup.setExpiryDate(info.getProperty("expiryDate"));
73
74     String thresholdUnit = info.getProperty("thresholdUnits");
75     licenseKeyGroup
76         .setThresholdUnits(thresholdUnit == null ? null : ThresholdUnit.valueOf(thresholdUnit));
77
78     licenseKeyGroup.setThresholdValue(toInteger(info.getProperty("thresholdValue")));
79     licenseKeyGroup.setIncrements(info.getProperty("increments"));
80   }
81
82   private void mapRelationsToLicenseKeyGroup(LicenseKeyGroupEntity licenseKeyGroup,
83                                              Collection<Relation> relations) {
84     if (relations != null && relations.size() > 0) {
85       licenseKeyGroup
86           .setReferencingFeatureGroups((relations.stream().map(relation -> relation
87               .getEdge2().getElementId().getValue()).collect(Collectors.toSet())));
88     }
89   }
90
91   private MultiChoiceOrOther<OperationalScope> getOperationalScopeMultiChoiceOrOther(
92       Map<String, Object> operationalScope) {
93     if (operationalScope == null || operationalScope.isEmpty()) {
94       return null;
95     }
96
97     Set<OperationalScope> choices = new HashSet<>();
98     ((List<String>) operationalScope.get("choices")).
99         forEach(choice -> choices.add(OperationalScope.valueOf(choice)));
100
101     Object other = operationalScope.get("other");
102     return new MultiChoiceOrOther<>(choices, other == null ? null : (String) other);
103   }
104 }