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