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.sdc.datatypes.model.ElementType;
33 import org.openecomp.sdc.vendorlicense.dao.LicenseAgreementDao;
34 import org.openecomp.sdc.vendorlicense.dao.impl.zusammen.convertor.ElementToLicenseAgreementConvertor;
35 import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementEntity;
36 import org.openecomp.types.ElementPropertyName;
38 import java.util.Collection;
39 import java.util.Optional;
41 import java.util.stream.Collectors;
43 import static org.openecomp.core.zusammen.api.ZusammenUtil.buildElement;
44 import static org.openecomp.core.zusammen.api.ZusammenUtil.buildStructuralElement;
45 import static org.openecomp.core.zusammen.api.ZusammenUtil.createSessionContext;
48 public class LicenseAgreementDaoZusammenImpl implements LicenseAgreementDao {
50 private ZusammenAdaptor zusammenAdaptor;
52 public LicenseAgreementDaoZusammenImpl(ZusammenAdaptor zusammenAdaptor) {
53 this.zusammenAdaptor = zusammenAdaptor;
57 public void registerVersioning(String versionableEntityType) {
62 public void create(LicenseAgreementEntity licenseAgreement) {
63 ZusammenElement licenseAgreementElement =
64 buildLicenseAgreementElement(licenseAgreement, Action.CREATE);
65 ZusammenElement licenseAgreementsElement =
66 buildStructuralElement(ElementType.LicenseAgreements, Action.IGNORE);
67 licenseAgreementsElement.addSubElement(licenseAgreementElement);
69 SessionContext context = createSessionContext();
70 Element licenseAgreementsSavedElement = zusammenAdaptor.saveElement(context,
71 new ElementContext(licenseAgreement.getVendorLicenseModelId(),
72 licenseAgreement.getVersion().getId()), licenseAgreementsElement,
73 "Create license agreement");
75 .setId(licenseAgreementsSavedElement.getSubElements().iterator().next().getElementId()
80 public void update(LicenseAgreementEntity licenseAgreement) {
81 ZusammenElement licenseAgreementElement =
82 buildLicenseAgreementElement(licenseAgreement, Action.UPDATE);
84 SessionContext context = createSessionContext();
85 zusammenAdaptor.saveElement(context,
86 new ElementContext(licenseAgreement.getVendorLicenseModelId(),
87 licenseAgreement.getVersion().getId()), licenseAgreementElement,
88 String.format("Update license agreement with id %s", licenseAgreement.getId()));
92 public LicenseAgreementEntity get(LicenseAgreementEntity licenseAgreement) {
93 SessionContext context = createSessionContext();
94 ElementContext elementContext = new ElementContext(licenseAgreement.getVendorLicenseModelId(),
95 licenseAgreement.getVersion().getId());
96 ElementToLicenseAgreementConvertor convertor = new ElementToLicenseAgreementConvertor();
97 return zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseAgreement.getId()))
99 LicenseAgreementEntity entity = convertor.convert(elementInfo);
100 entity.setVendorLicenseModelId(licenseAgreement.getVendorLicenseModelId());
101 entity.setVersion(licenseAgreement.getVersion());
108 public void delete(LicenseAgreementEntity licenseAgreement) {
109 ZusammenElement zusammenElement = buildElement(new Id(licenseAgreement.getId()), Action.DELETE);
111 SessionContext context = createSessionContext();
112 ElementContext elementContext = new ElementContext(licenseAgreement.getVendorLicenseModelId(),
113 licenseAgreement.getVersion().getId());
114 zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
115 "delete license agreement. id:" + licenseAgreement.getId() + ".");
120 public Collection<LicenseAgreementEntity> list(LicenseAgreementEntity licenseAgreement) {
121 SessionContext context = createSessionContext();
122 ElementContext elementContext = new ElementContext(licenseAgreement.getVendorLicenseModelId(),
123 licenseAgreement.getVersion().getId());
124 ElementToLicenseAgreementConvertor convertor = new ElementToLicenseAgreementConvertor();
125 return zusammenAdaptor
126 .listElementsByName(context, elementContext, null,
127 ElementType.LicenseAgreements.name())
128 .stream().map(elementInfo -> {
129 LicenseAgreementEntity entity = convertor.convert(elementInfo);
130 entity.setVendorLicenseModelId(licenseAgreement.getVendorLicenseModelId());
131 entity.setVersion(licenseAgreement.getVersion());
134 .collect(Collectors.toList());
138 public long count(LicenseAgreementEntity licenseAgreement) {
139 SessionContext context = createSessionContext();
140 ElementContext elementContext = new ElementContext(licenseAgreement.getVendorLicenseModelId(),
141 licenseAgreement.getVersion().getId());
143 return zusammenAdaptor.listElementsByName(context, elementContext, null,
144 ElementType.LicenseAgreements.name())
149 public void deleteAll(LicenseAgreementEntity entity) {
154 public void removeFeatureGroup(LicenseAgreementEntity licenseAgreement, String featureGroupId) {
155 SessionContext context = createSessionContext();
156 ElementContext elementContext = new ElementContext(licenseAgreement.getVendorLicenseModelId(),
157 licenseAgreement.getVersion().getId());
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 = createSessionContext();
179 ElementContext elementContext = new ElementContext(licenseAgreement.getVendorLicenseModelId(),
180 licenseAgreement.getVersion().getId());
181 ElementToLicenseAgreementConvertor convertor = new ElementToLicenseAgreementConvertor();
182 Optional<ElementInfo> elementInfo =
183 zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseAgreement.getId()));
184 if (elementInfo.isPresent()) {
185 LicenseAgreementEntity currentLicenseAgreement =
186 convertor.convert(elementInfo.get());
187 currentLicenseAgreement.setVendorLicenseModelId(licenseAgreement.getVendorLicenseModelId());
188 currentLicenseAgreement.setVersion(licenseAgreement.getVersion());
189 if (!(removedFeatureGroupIds == null)) {
190 currentLicenseAgreement.getFeatureGroupIds().removeAll(removedFeatureGroupIds);
193 if (!(addedFeatureGroupIds == null)) {
194 currentLicenseAgreement.getFeatureGroupIds().addAll(addedFeatureGroupIds);
196 licenseAgreementElement.setRelations(currentLicenseAgreement.getFeatureGroupIds().stream()
197 .map(relation -> VlmZusammenUtil
198 .createRelation(RelationType.LicenseAgreementToFeatureGroup, relation))
199 .collect(Collectors.toList()));
200 zusammenAdaptor.saveElement(context, elementContext, licenseAgreementElement,
201 "update license agreement");
205 private ZusammenElement buildLicenseAgreementElement(LicenseAgreementEntity licenseAgreement,
207 ZusammenElement licenseAgreementElement =
208 buildElement(licenseAgreement.getId() == null ? null : new Id(licenseAgreement.getId()),
210 Info info = new Info();
211 info.setName(licenseAgreement.getName());
212 info.setDescription(licenseAgreement.getDescription());
213 info.addProperty(ElementPropertyName.elementType.name(), ElementType.LicenseAgreement);
214 info.addProperty("licenseTerm", licenseAgreement.getLicenseTerm());
215 info.addProperty("requirementsAndConstrains", licenseAgreement.getRequirementsAndConstrains());
216 licenseAgreementElement.setInfo(info);
218 if (licenseAgreement.getFeatureGroupIds() != null &&
219 licenseAgreement.getFeatureGroupIds().size() > 0) {
220 licenseAgreementElement.setRelations(licenseAgreement.getFeatureGroupIds().stream()
221 .map(rel -> VlmZusammenUtil
222 .createRelation(RelationType.LicenseAgreementToFeatureGroup, rel))
223 .collect(Collectors.toList()));
225 return licenseAgreementElement;