Added oparent to sdc main
[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.MultiChoiceOrOther;
29 import org.openecomp.sdc.vendorlicense.dao.types.OperationalScope;
30 import org.openecomp.sdc.vendorlicense.dao.types.ThresholdUnit;
31
32 import java.util.Collection;
33 import java.util.HashSet;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.Set;
37 import java.util.stream.Collectors;
38
39 import static org.openecomp.sdc.vendorlicense.dao.impl.zusammen.VlmZusammenUtil.toInteger;
40
41
42 public class ElementToEntitlementPoolConvertor extends ElementConvertor {
43   @Override
44   public EntitlementPoolEntity convert(Element element) {
45     if (element == null) {
46       return null;
47     }
48     return mapElementToEntitlementPoolEntity(element);
49   }
50
51   @Override
52   public EntitlementPoolEntity convert(ElementInfo elementInfo) {
53     if (elementInfo == null) {
54       return null;
55     }
56     return mapElementInfoToEntitlementPoolEntity(elementInfo);
57   }
58
59   private EntitlementPoolEntity mapElementToEntitlementPoolEntity(Element element) {
60     EntitlementPoolEntity entitlementPool =
61         new EntitlementPoolEntity();
62     entitlementPool.setId(element.getElementId().getValue());
63     mapInfoToEntitlementPoolEntity(entitlementPool, element.getInfo());
64     mapRelationsToEntitlementPoolEntity(entitlementPool, element.getRelations());
65     return entitlementPool;
66   }
67
68   private EntitlementPoolEntity mapElementInfoToEntitlementPoolEntity(ElementInfo elementInfo) {
69     EntitlementPoolEntity entitlementPool =
70         new EntitlementPoolEntity();
71     entitlementPool.setId(elementInfo.getId().getValue());
72     mapInfoToEntitlementPoolEntity(entitlementPool, elementInfo.getInfo());
73     mapRelationsToEntitlementPoolEntity(entitlementPool, elementInfo.getRelations());
74     return entitlementPool;
75   }
76
77   private void mapInfoToEntitlementPoolEntity(EntitlementPoolEntity entitlementPool, Info info) {
78     entitlementPool.setName(info.getName());
79     entitlementPool.setDescription(info.getDescription());
80     entitlementPool.setVersionUuId(info.getProperty("version_uuid"));
81     entitlementPool.setThresholdValue(toInteger(info.getProperty("thresholdValue")));
82     String thresholdUnit = info.getProperty("threshold_unit");
83     entitlementPool
84         .setThresholdUnit(thresholdUnit == null ? null : ThresholdUnit.valueOf(thresholdUnit));
85     entitlementPool.setIncrements(info.getProperty("increments"));
86     entitlementPool.setOperationalScope(
87         getOperationalScopeMultiChoiceOrOther(info.getProperty("operational_scope")));
88     entitlementPool.setStartDate(info.getProperty("startDate"));
89     entitlementPool.setExpiryDate(info.getProperty("expiryDate"));
90     entitlementPool.setManufacturerReferenceNumber(info.getProperty("manufacturerReferenceNumber"));
91   }
92
93   private void mapRelationsToEntitlementPoolEntity(EntitlementPoolEntity entitlementPool,
94                                                    Collection<Relation> relations) {
95     if (relations != null && relations.size() > 0) {
96       entitlementPool
97           .setReferencingFeatureGroups(relations.stream().map(relation -> relation
98               .getEdge2().getElementId().getValue()).collect(Collectors.toSet()));
99     }
100   }
101
102   private MultiChoiceOrOther<OperationalScope> getOperationalScopeMultiChoiceOrOther(
103       Map<String, Object> operationalScope) {
104     if (operationalScope == null || operationalScope.isEmpty()) {
105       return null;
106     }
107
108     Set<OperationalScope> choices = new HashSet<>();
109     ((List<String>) operationalScope.get("choices")).
110         forEach(choice -> choices.add(OperationalScope.valueOf(choice)));
111
112     Object other = operationalScope.get("other");
113     return new MultiChoiceOrOther<>(choices, other == null ? null : (String) other);
114   }
115 }