[SDC-29] Amdocs OnBoard 1707 initial commit.
[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 / LicenseAgreementDaoZusammenImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 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
21 package org.openecomp.sdc.vendorlicense.dao.impl.zusammen;
22
23 import com.amdocs.zusammen.adaptor.inbound.api.types.item.Element;
24 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ElementInfo;
25 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ZusammenElement;
26 import com.amdocs.zusammen.datatypes.Id;
27 import com.amdocs.zusammen.datatypes.SessionContext;
28 import com.amdocs.zusammen.datatypes.item.Action;
29 import com.amdocs.zusammen.datatypes.item.ElementContext;
30 import com.amdocs.zusammen.datatypes.item.Info;
31 import org.openecomp.core.zusammen.api.ZusammenAdaptor;
32 import org.openecomp.core.zusammen.api.ZusammenUtil;
33 import org.openecomp.sdc.vendorlicense.dao.LicenseAgreementDao;
34 import org.openecomp.sdc.vendorlicense.dao.types.ChoiceOrOther;
35 import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementEntity;
36 import org.openecomp.sdc.vendorlicense.dao.types.LicenseTerm;
37 import org.openecomp.sdc.versioning.dao.types.Version;
38
39 import java.util.Collection;
40 import java.util.Map;
41 import java.util.Optional;
42 import java.util.Set;
43 import java.util.stream.Collectors;
44
45
46 public class LicenseAgreementDaoZusammenImpl implements LicenseAgreementDao {
47
48   private ZusammenAdaptor zusammenAdaptor;
49
50   public LicenseAgreementDaoZusammenImpl(ZusammenAdaptor zusammenAdaptor) {
51     this.zusammenAdaptor = zusammenAdaptor;
52   }
53
54   @Override
55   public void registerVersioning(String versionableEntityType) {
56     //no need
57   }
58
59   @Override
60   public void create(LicenseAgreementEntity licenseAgreement) {
61     ZusammenElement licenseAgreementElement =
62         buildLicenseAgreementElement(licenseAgreement, Action.CREATE);
63     ZusammenElement licenseAgreementsElement =
64         VlmZusammenUtil.buildStructuralElement(StructureElement.LicenseAgreements, null);
65     licenseAgreementsElement.addSubElement(licenseAgreementElement);
66
67     SessionContext context = ZusammenUtil.createSessionContext();
68     Id itemId = new Id(licenseAgreement.getVendorLicenseModelId());
69     Optional<Element> savedElement = zusammenAdaptor.saveElement(context, new ElementContext(itemId,
70             VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor)),
71         licenseAgreementsElement, "Create license agreement");
72     savedElement.ifPresent(element -> licenseAgreement
73         .setId(element.getSubElements().iterator().next().getElementId().getValue()));
74   }
75
76   @Override
77   public void update(LicenseAgreementEntity licenseAgreement) {
78     ZusammenElement licenseAgreementElement =
79         buildLicenseAgreementElement(licenseAgreement, Action.UPDATE);
80
81     SessionContext context = ZusammenUtil.createSessionContext();
82     Id itemId = new Id(licenseAgreement.getVendorLicenseModelId());
83     zusammenAdaptor.saveElement(context, new ElementContext(itemId,
84             VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor)),
85         licenseAgreementElement,
86         String.format("Update license agreement with id %s", licenseAgreement.getId()));
87   }
88
89   @Override
90   public LicenseAgreementEntity get(LicenseAgreementEntity licenseAgreement) {
91     SessionContext context = ZusammenUtil.createSessionContext();
92     Id itemId = new Id(licenseAgreement.getVendorLicenseModelId());
93     ElementContext elementContext = new ElementContext(itemId,
94         VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor),
95         VlmZusammenUtil.getVersionTag(licenseAgreement.getVersion()));
96
97     return zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseAgreement.getId()))
98         .map(elementInfo -> mapElementInfoToLicenseAgreement(
99             licenseAgreement.getVendorLicenseModelId(), licenseAgreement.getVersion(), elementInfo))
100         .orElse(null);
101   }
102
103   @Override
104   public void delete(LicenseAgreementEntity licenseAgreement) {
105     SessionContext context = ZusammenUtil.createSessionContext();
106     ZusammenElement zusammenElement = new ZusammenElement();
107     zusammenElement.setAction(Action.DELETE);
108     zusammenElement.setElementId(new Id(licenseAgreement.getId()));
109
110     Id itemId = new Id(licenseAgreement.getVendorLicenseModelId());
111     ElementContext elementContext = new ElementContext(itemId,
112         VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor));
113     zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
114         "delete license agreement. id:" + licenseAgreement.getId() + ".");
115   }
116
117
118   @Override
119   public Collection<LicenseAgreementEntity> list(LicenseAgreementEntity licenseAgreement) {
120     SessionContext context = ZusammenUtil.createSessionContext();
121     Id itemId = new Id(licenseAgreement.getVendorLicenseModelId());
122     ElementContext elementContext = new ElementContext(itemId,
123         VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor),
124         VlmZusammenUtil.getVersionTag(licenseAgreement.getVersion()));
125
126     return zusammenAdaptor
127         .listElementsByName(context, elementContext, null,
128             StructureElement.LicenseAgreements.name())
129         .stream().map(elementInfo -> mapElementInfoToLicenseAgreement(
130             licenseAgreement.getVendorLicenseModelId(), licenseAgreement.getVersion(), elementInfo))
131         .collect(Collectors.toList());
132   }
133
134   @Override
135   public long count(LicenseAgreementEntity licenseAgreement) {
136     SessionContext context = ZusammenUtil.createSessionContext();
137     Id itemId = new Id(licenseAgreement.getVendorLicenseModelId());
138     ElementContext elementContext = new ElementContext(itemId,
139         VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor),
140         VlmZusammenUtil.getVersionTag(licenseAgreement.getVersion()));
141
142     return zusammenAdaptor.listElementsByName(context, elementContext, null,
143         StructureElement.LicenseAgreements.name())
144         .size();
145   }
146
147   @Override
148   public void deleteAll(LicenseAgreementEntity entity) {
149     //not supported
150   }
151
152   @Override
153   public void removeFeatureGroup(LicenseAgreementEntity licenseAgreement, String featureGroupId) {
154     SessionContext context = ZusammenUtil.createSessionContext();
155     Id itemId = new Id(licenseAgreement.getVendorLicenseModelId());
156     ElementContext elementContext = new ElementContext(itemId,
157         VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor));
158
159     Optional<ElementInfo> elementInfo = zusammenAdaptor.getElementInfo(context,
160         elementContext, new Id(licenseAgreement.getId()));
161     if (elementInfo.isPresent()) {
162       ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
163       zusammenElement.setAction(Action.UPDATE);
164       zusammenElement.setRelations(elementInfo.get().getRelations().stream()
165           .filter(relation -> !featureGroupId.equals(relation.getEdge2().getElementId().getValue()))
166           .collect(Collectors.toList()));
167       zusammenAdaptor.saveElement(context, elementContext, zusammenElement, "remove feature group");
168     }
169   }
170
171   @Override
172   public void updateColumnsAndDeltaFeatureGroupIds(LicenseAgreementEntity licenseAgreement,
173                                                    Set<String> addedFeatureGroupIds,
174                                                    Set<String> removedFeatureGroupIds) {
175     ZusammenElement licenseAgreementElement =
176         buildLicenseAgreementElement(licenseAgreement, Action.UPDATE);
177
178     SessionContext context = ZusammenUtil.createSessionContext();
179     Id itemId = new Id(licenseAgreement.getVendorLicenseModelId());
180     ElementContext elementContext = new ElementContext(itemId,
181         VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor));
182
183     Optional<ElementInfo> elementInfo =
184         zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseAgreement.getId()));
185     if (elementInfo.isPresent()) {
186       LicenseAgreementEntity currentLicenseAgreement =
187           mapElementInfoToLicenseAgreement(licenseAgreement.getId(), licenseAgreement.getVersion(),
188               elementInfo.get());
189
190       currentLicenseAgreement.getFeatureGroupIds().removeAll(removedFeatureGroupIds);
191       currentLicenseAgreement.getFeatureGroupIds().addAll(addedFeatureGroupIds);
192       licenseAgreementElement.setRelations(currentLicenseAgreement.getFeatureGroupIds().stream()
193           .map(relation -> VlmZusammenUtil
194               .createRelation(RelationType.LicenseAgreementToFeatureGroup, relation))
195           .collect(Collectors.toList()));
196       zusammenAdaptor.saveElement(context, elementContext, licenseAgreementElement,
197           "update license agreement");
198     }
199   }
200
201   private LicenseAgreementEntity mapElementInfoToLicenseAgreement(String vlmId, Version version,
202                                                                   ElementInfo elementInfo) {
203     LicenseAgreementEntity licenseAgreement =
204         new LicenseAgreementEntity(vlmId, version, elementInfo.getId().getValue());
205     licenseAgreement.setName(elementInfo.getInfo().getName());
206     licenseAgreement.setDescription(elementInfo.getInfo().getDescription());
207
208     licenseAgreement
209         .setLicenseTerm(getCoiceOrOther(elementInfo.getInfo().getProperty("licenseTerm")));
210     licenseAgreement.setRequirementsAndConstrains(
211         elementInfo.getInfo().getProperty("requirementsAndConstrains"));
212     if (elementInfo.getRelations() != null && elementInfo.getRelations().size() > 0) {
213       licenseAgreement.setFeatureGroupIds(elementInfo.getRelations().stream()
214           .map(relation -> relation.getEdge2().getElementId().getValue())
215           .collect(Collectors.toSet()));
216     }
217     return licenseAgreement;
218   }
219
220   private ChoiceOrOther<LicenseTerm> getCoiceOrOther(Map licenseTerm) {
221     return new ChoiceOrOther(LicenseTerm.valueOf((String) licenseTerm.get("choice")),
222         (String) licenseTerm.get("other"));
223   }
224
225
226   private ZusammenElement buildLicenseAgreementElement(LicenseAgreementEntity licenseAgreement,
227                                                        Action action) {
228     ZusammenElement licenseAgreementElement = new ZusammenElement();
229     licenseAgreementElement.setAction(action);
230     if (licenseAgreement.getId() != null) {
231       licenseAgreementElement.setElementId(new Id(licenseAgreement.getId()));
232     }
233     Info info = new Info();
234     info.setName(licenseAgreement.getName());
235     info.setDescription(licenseAgreement.getDescription());
236     info.addProperty("licenseTerm", licenseAgreement.getLicenseTerm());
237     info.addProperty("requirementsAndConstrains", licenseAgreement.getRequirementsAndConstrains());
238     licenseAgreementElement.setInfo(info);
239
240     if (licenseAgreement.getFeatureGroupIds() != null &&
241         licenseAgreement.getFeatureGroupIds().size() > 0) {
242       licenseAgreementElement.setRelations(licenseAgreement.getFeatureGroupIds().stream()
243           .map(rel -> VlmZusammenUtil
244               .createRelation(RelationType.LicenseAgreementToFeatureGroup, rel))
245           .collect(Collectors.toList()));
246     }
247     return licenseAgreementElement;
248   }
249 }