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