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 / ElementToFeatureGroupConvertor.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.impl.zusammen.RelationType;
28 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity;
29
30 import java.util.Collection;
31 import java.util.HashSet;
32 import java.util.Set;
33
34
35 public class ElementToFeatureGroupConvertor extends ElementConvertor {
36   @Override
37   public FeatureGroupEntity convert(Element element) {
38     if (element == null) {
39       return null;
40     }
41     return mapElementToFeatureGroupEntity(element);
42
43   }
44
45   @Override
46   public FeatureGroupEntity convert(ElementInfo elementInfo) {
47     if (elementInfo == null) {
48       return null;
49     }
50     return mapElementInfoToFeatureGroupEntity(elementInfo);
51
52   }
53
54
55   private FeatureGroupEntity mapElementToFeatureGroupEntity(Element element) {
56     FeatureGroupEntity featureGroup =
57         new FeatureGroupEntity();
58     featureGroup.setId(element.getElementId().getValue());
59     mapInfoToFeatureGroup(featureGroup, element.getInfo());
60     mapRelationsToFeatureGroup(featureGroup, element.getRelations());
61     return featureGroup;
62   }
63
64
65   private FeatureGroupEntity mapElementInfoToFeatureGroupEntity(ElementInfo elementInfo) {
66     FeatureGroupEntity featureGroup = new FeatureGroupEntity();
67     featureGroup.setId(elementInfo.getId().getValue());
68     mapInfoToFeatureGroup(featureGroup, elementInfo.getInfo());
69     mapRelationsToFeatureGroup(featureGroup, elementInfo.getRelations());
70     return featureGroup;
71   }
72
73   private void mapInfoToFeatureGroup(FeatureGroupEntity featureGroup, Info info) {
74     featureGroup.setName(info.getName());
75     featureGroup.setDescription(info.getDescription());
76     featureGroup.setPartNumber(info.getProperty("partNumber"));
77     featureGroup.setManufacturerReferenceNumber(info.getProperty("manufacturerReferenceNumber"));
78   }
79
80   private void mapRelationsToFeatureGroup(FeatureGroupEntity featureGroup,
81                                           Collection<Relation> relations) {
82     Set<String> entitlementPoolIds = new HashSet<>();
83     Set<String> licenseAgreements = new HashSet<>();
84     Set<String> licenseKeyGroupIds = new HashSet<>();
85
86     if (relations != null) {
87       for (Relation relation : relations) {
88         if (RelationType.FeatureGroupToEntitlmentPool.name().equals(relation.getType())) {
89           entitlementPoolIds.add(relation.getEdge2().getElementId().getValue());
90         } else if (RelationType.FeatureGroupToLicenseKeyGroup.name().equals(relation.getType())) {
91           licenseKeyGroupIds.add(relation.getEdge2().getElementId().getValue());
92         } else if (RelationType.FeatureGroupToReferencingLicenseAgreement.name()
93             .equals(relation.getType())) {
94           licenseAgreements.add(relation.getEdge2().getElementId().getValue());
95         }
96       }
97     }
98     featureGroup.setEntitlementPoolIds(entitlementPoolIds);
99     featureGroup.setLicenseKeyGroupIds(licenseKeyGroupIds);
100     featureGroup.setReferencingLicenseAgreements(licenseAgreements);
101   }
102 }