2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.vendorlicense.dao.impl.zusammen;
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;
39 import java.util.Collection;
41 import java.util.Optional;
43 import java.util.stream.Collectors;
46 public class LicenseAgreementDaoZusammenImpl implements LicenseAgreementDao {
48 private ZusammenAdaptor zusammenAdaptor;
50 public LicenseAgreementDaoZusammenImpl(ZusammenAdaptor zusammenAdaptor) {
51 this.zusammenAdaptor = zusammenAdaptor;
55 public void registerVersioning(String versionableEntityType) {
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);
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()));
77 public void update(LicenseAgreementEntity licenseAgreement) {
78 ZusammenElement licenseAgreementElement =
79 buildLicenseAgreementElement(licenseAgreement, Action.UPDATE);
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()));
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()));
97 return zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseAgreement.getId()))
98 .map(elementInfo -> mapElementInfoToLicenseAgreement(
99 licenseAgreement.getVendorLicenseModelId(), licenseAgreement.getVersion(), elementInfo))
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()));
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() + ".");
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()));
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());
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()));
142 return zusammenAdaptor.listElementsByName(context, elementContext, null,
143 StructureElement.LicenseAgreements.name())
148 public void deleteAll(LicenseAgreementEntity entity) {
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));
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");
172 public void updateColumnsAndDeltaFeatureGroupIds(LicenseAgreementEntity licenseAgreement,
173 Set<String> addedFeatureGroupIds,
174 Set<String> removedFeatureGroupIds) {
175 ZusammenElement licenseAgreementElement =
176 buildLicenseAgreementElement(licenseAgreement, Action.UPDATE);
178 SessionContext context = ZusammenUtil.createSessionContext();
179 Id itemId = new Id(licenseAgreement.getVendorLicenseModelId());
180 ElementContext elementContext = new ElementContext(itemId,
181 VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor));
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(),
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");
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());
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()));
217 return licenseAgreement;
220 private ChoiceOrOther<LicenseTerm> getCoiceOrOther(Map licenseTerm) {
221 return new ChoiceOrOther(LicenseTerm.valueOf((String) licenseTerm.get("choice")),
222 (String) licenseTerm.get("other"));
226 private ZusammenElement buildLicenseAgreementElement(LicenseAgreementEntity licenseAgreement,
228 ZusammenElement licenseAgreementElement = new ZusammenElement();
229 licenseAgreementElement.setAction(action);
230 if (licenseAgreement.getId() != null) {
231 licenseAgreementElement.setElementId(new Id(licenseAgreement.getId()));
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);
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()));
247 return licenseAgreementElement;