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