Entitlement Pool - Support Type Field
[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 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.openecomp.sdc.vendorlicense.dao.impl.zusammen.convertor;
21
22 import com.amdocs.zusammen.adaptor.inbound.api.types.item.Element;
23 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ElementInfo;
24 import com.amdocs.zusammen.datatypes.item.Info;
25 import com.amdocs.zusammen.datatypes.item.Relation;
26 import org.openecomp.convertor.ElementConvertor;
27 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
28 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolType;
29 import org.openecomp.sdc.vendorlicense.dao.types.MultiChoiceOrOther;
30 import org.openecomp.sdc.vendorlicense.dao.types.OperationalScope;
31 import org.openecomp.sdc.vendorlicense.dao.types.ThresholdUnit;
32
33 import java.util.Collection;
34 import java.util.HashSet;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.Set;
38 import java.util.stream.Collectors;
39
40 import static org.openecomp.sdc.vendorlicense.dao.impl.zusammen.VlmZusammenUtil.toInteger;
41
42
43 public class ElementToEntitlementPoolConvertor extends ElementConvertor {
44   @Override
45   public EntitlementPoolEntity convert(Element element) {
46     if (element == null) {
47       return null;
48     }
49     return mapElementToEntitlementPoolEntity(element);
50   }
51
52   @Override
53   public EntitlementPoolEntity convert(ElementInfo elementInfo) {
54     if (elementInfo == null) {
55       return null;
56     }
57     return mapElementInfoToEntitlementPoolEntity(elementInfo);
58   }
59
60   private EntitlementPoolEntity mapElementToEntitlementPoolEntity(Element element) {
61     EntitlementPoolEntity entitlementPool =
62         new EntitlementPoolEntity();
63     entitlementPool.setId(element.getElementId().getValue());
64     mapInfoToEntitlementPoolEntity(entitlementPool, element.getInfo());
65     mapRelationsToEntitlementPoolEntity(entitlementPool, element.getRelations());
66     return entitlementPool;
67   }
68
69   private EntitlementPoolEntity mapElementInfoToEntitlementPoolEntity(ElementInfo elementInfo) {
70     EntitlementPoolEntity entitlementPool =
71         new EntitlementPoolEntity();
72     entitlementPool.setId(elementInfo.getId().getValue());
73     mapInfoToEntitlementPoolEntity(entitlementPool, elementInfo.getInfo());
74     mapRelationsToEntitlementPoolEntity(entitlementPool, elementInfo.getRelations());
75     return entitlementPool;
76   }
77
78   private void mapInfoToEntitlementPoolEntity(EntitlementPoolEntity entitlementPool, Info info) {
79     entitlementPool.setName(info.getName());
80     entitlementPool.setDescription(info.getDescription());
81     entitlementPool.setVersionUuId(info.getProperty("version_uuid"));
82     entitlementPool.setType(EntitlementPoolType.permissiveValueOf(info.getProperty("EntitlementPoolType")));
83     entitlementPool.setThresholdValue(toInteger(info.getProperty("thresholdValue")));
84     String thresholdUnit = info.getProperty("threshold_unit");
85     entitlementPool
86         .setThresholdUnit(thresholdUnit == null ? null : ThresholdUnit.valueOf(thresholdUnit));
87     entitlementPool.setIncrements(info.getProperty("increments"));
88     entitlementPool.setOperationalScope(
89         getOperationalScopeMultiChoiceOrOther(info.getProperty("operational_scope")));
90     entitlementPool.setStartDate(info.getProperty("startDate"));
91     entitlementPool.setExpiryDate(info.getProperty("expiryDate"));
92     entitlementPool.setManufacturerReferenceNumber(info.getProperty("manufacturerReferenceNumber"));
93   }
94
95   private void mapRelationsToEntitlementPoolEntity(EntitlementPoolEntity entitlementPool,
96                                                    Collection<Relation> relations) {
97     if (relations != null && relations.size() > 0) {
98       entitlementPool
99           .setReferencingFeatureGroups(relations.stream().map(relation -> relation
100               .getEdge2().getElementId().getValue()).collect(Collectors.toSet()));
101     }
102   }
103
104   private MultiChoiceOrOther<OperationalScope> getOperationalScopeMultiChoiceOrOther(
105       Map<String, Object> operationalScope) {
106     if (operationalScope == null || operationalScope.isEmpty()) {
107       return null;
108     }
109
110     Set<OperationalScope> choices = new HashSet<>();
111     ((List<String>) operationalScope.get("choices")).
112         forEach(choice -> choices.add(OperationalScope.valueOf(choice)));
113
114     Object other = operationalScope.get("other");
115     return new MultiChoiceOrOther<>(choices, other == null ? null : (String) other);
116   }
117 }