2 * ============LICENSE_START=======================================================
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
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=========================================================
20 package org.openecomp.sdc.vendorlicense.dao.impl.zusammen;
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.adaptor.inbound.api.types.item.ZusammenElement;
25 import com.amdocs.zusammen.datatypes.Id;
26 import com.amdocs.zusammen.datatypes.SessionContext;
27 import com.amdocs.zusammen.datatypes.item.Action;
28 import com.amdocs.zusammen.datatypes.item.ElementContext;
29 import com.amdocs.zusammen.datatypes.item.Info;
30 import com.amdocs.zusammen.datatypes.item.Relation;
31 import org.openecomp.core.zusammen.api.ZusammenAdaptor;
32 import org.openecomp.sdc.datatypes.model.ElementType;
33 import org.openecomp.sdc.vendorlicense.dao.FeatureGroupDao;
34 import org.openecomp.sdc.vendorlicense.dao.impl.zusammen.convertor.ElementToFeatureGroupConvertor;
35 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity;
36 import org.openecomp.types.ElementPropertyName;
38 import java.util.ArrayList;
39 import java.util.Collection;
40 import java.util.Optional;
42 import java.util.stream.Collectors;
44 import static org.openecomp.core.zusammen.api.ZusammenUtil.*;
46 public class FeatureGroupDaoZusammenImpl implements FeatureGroupDao {
48 private ZusammenAdaptor zusammenAdaptor;
50 public FeatureGroupDaoZusammenImpl(ZusammenAdaptor zusammenAdaptor) {
51 this.zusammenAdaptor = zusammenAdaptor;
55 public void registerVersioning(String versionableEntityType) {
60 public void create(FeatureGroupEntity featureGroup) {
61 ZusammenElement featureGroupElement = buildFeatureGroupElement(featureGroup, Action.CREATE);
63 ZusammenElement featureGroupsElement =
64 buildStructuralElement(ElementType.FeatureGroups, Action.IGNORE);
66 featureGroupsElement.addSubElement(featureGroupElement);
68 SessionContext context = createSessionContext();
69 Element featureGroupsSavedElement = zusammenAdaptor.saveElement(context,
70 new ElementContext(featureGroup.getVendorLicenseModelId(),
71 featureGroup.getVersion().getId()), featureGroupsElement, "Create feature group");
74 featureGroupsSavedElement.getSubElements().iterator().next().getElementId().getValue());
78 public void update(FeatureGroupEntity featureGroup) {
79 ZusammenElement featureGroupElement = buildFeatureGroupElement(featureGroup, Action.UPDATE);
81 SessionContext context = createSessionContext();
82 zusammenAdaptor.saveElement(context, new ElementContext(featureGroup.getVendorLicenseModelId(),
83 featureGroup.getVersion().getId()), featureGroupElement,
84 String.format("Update feature group with id %s", featureGroup.getId()));
88 public FeatureGroupEntity get(FeatureGroupEntity featureGroup) {
89 SessionContext context = createSessionContext();
90 ElementContext elementContext = new ElementContext(featureGroup.getVendorLicenseModelId(),
91 featureGroup.getVersion().getId());
93 return zusammenAdaptor.getElementInfo(context, elementContext, new Id(featureGroup.getId()))
95 FeatureGroupEntity entity = new ElementToFeatureGroupConvertor().convert(elementInfo);
96 entity.setVendorLicenseModelId(featureGroup.getVendorLicenseModelId());
97 entity.setVersion(featureGroup.getVersion());
104 public void delete(FeatureGroupEntity featureGroup) {
105 ZusammenElement zusammenElement = buildElement(new Id(featureGroup.getId()), Action.DELETE);
107 SessionContext context = createSessionContext();
108 ElementContext elementContext = new ElementContext(featureGroup.getVendorLicenseModelId(),
109 featureGroup.getVersion().getId());
110 zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
111 "delete feature group. id:" + featureGroup.getId() + ".");
115 public Collection<FeatureGroupEntity> list(FeatureGroupEntity featureGroup) {
116 SessionContext context = createSessionContext();
117 ElementContext elementContext = new ElementContext(featureGroup.getVendorLicenseModelId(),
118 featureGroup.getVersion().getId());
120 ElementToFeatureGroupConvertor convertor = new ElementToFeatureGroupConvertor();
121 return zusammenAdaptor
122 .listElementsByName(context, elementContext, null, ElementType.FeatureGroups.name())
123 .stream().map(elementInfo -> {
124 FeatureGroupEntity entity = convertor.convert(
126 entity.setVendorLicenseModelId(featureGroup.getVendorLicenseModelId());
127 entity.setVersion(featureGroup.getVersion());
130 .collect(Collectors.toList());
134 public long count(FeatureGroupEntity featureGroup) {
135 SessionContext context = createSessionContext();
136 ElementContext elementContext = new ElementContext(featureGroup.getVendorLicenseModelId(),
137 featureGroup.getVersion().getId());
139 return zusammenAdaptor
140 .listElementsByName(context, elementContext, null, ElementType.FeatureGroups.name())
145 public void removeEntitlementPool(FeatureGroupEntity featureGroup, String entitlementPoolId) {
146 removeRelationToContainedEntity(featureGroup, entitlementPoolId, "entitlement pool");
150 public void removeLicenseKeyGroup(FeatureGroupEntity featureGroup, String licenseKeyGroupId) {
151 removeRelationToContainedEntity(featureGroup, licenseKeyGroupId, "license Key Group");
154 private void removeRelationToContainedEntity(FeatureGroupEntity featureGroup,
155 String containedEntityId,
156 String containedEntityType) {
157 SessionContext context = createSessionContext();
158 ElementContext elementContext = new ElementContext(featureGroup.getVendorLicenseModelId(),
159 featureGroup.getVersion().getId());
161 Optional<ElementInfo> elementInfo = zusammenAdaptor.getElementInfo(context,
162 elementContext, new Id(featureGroup.getId()));
163 if (elementInfo.isPresent()) {
164 ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
165 zusammenElement.setAction(Action.UPDATE);
166 zusammenElement.setRelations(elementInfo.get().getRelations().stream()
168 relation -> !containedEntityId.equals(relation.getEdge2().getElementId().getValue()))
169 .collect(Collectors.toList()));
170 zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
171 String.format("remove %s", containedEntityType));
176 public void updateFeatureGroup(FeatureGroupEntity featureGroup,
177 Set<String> addedEntitlementPools,
178 Set<String> removedEntitlementPools,
179 Set<String> addedLicenseKeyGroups,
180 Set<String> removedLicenseKeyGroups) {
181 ZusammenElement featureGroupElement = buildFeatureGroupElement(featureGroup, Action.UPDATE);
182 SessionContext context = createSessionContext();
183 ElementContext elementContext = new ElementContext(featureGroup.getVendorLicenseModelId(),
184 featureGroup.getVersion().getId());
185 ElementToFeatureGroupConvertor convertor = new ElementToFeatureGroupConvertor();
186 Optional<ElementInfo> elementInfo = zusammenAdaptor.getElementInfo(context,
187 elementContext, new Id(featureGroup.getId()));
188 if (elementInfo.isPresent()) {
189 FeatureGroupEntity currentFeatureGroup = convertor.convert(elementInfo.get());
190 currentFeatureGroup.setVendorLicenseModelId(featureGroup.getVendorLicenseModelId());
191 currentFeatureGroup.setVersion(featureGroup.getVersion());
192 if (!(removedEntitlementPools == null)) {
193 currentFeatureGroup.getEntitlementPoolIds().removeAll(removedEntitlementPools);
195 if (!(addedEntitlementPools == null)) {
196 currentFeatureGroup.getEntitlementPoolIds().addAll(addedEntitlementPools);
199 if (featureGroupElement.getRelations() == null) {
200 featureGroupElement.setRelations(new ArrayList<>());
202 featureGroupElement.getRelations()
203 .addAll(currentFeatureGroup.getEntitlementPoolIds().stream()
204 .map(relation -> VlmZusammenUtil
205 .createRelation(RelationType.FeatureGroupToEntitlmentPool, relation))
206 .collect(Collectors.toList()));
208 if (!(removedLicenseKeyGroups == null)) {
209 currentFeatureGroup.getLicenseKeyGroupIds().removeAll(removedLicenseKeyGroups);
211 if (!(addedLicenseKeyGroups == null)) {
212 currentFeatureGroup.getLicenseKeyGroupIds().addAll(addedLicenseKeyGroups);
215 featureGroupElement.getRelations()
216 .addAll(currentFeatureGroup.getLicenseKeyGroupIds().stream()
217 .map(relation -> VlmZusammenUtil
218 .createRelation(RelationType.FeatureGroupToLicenseKeyGroup, relation))
219 .collect(Collectors.toList()));
221 Collection<Relation> laRelations = elementInfo.get().getRelations().stream().filter
222 (rel -> rel.getType()
223 .equals(RelationType.FeatureGroupToReferencingLicenseAgreement.name()))
224 .map(rel -> VlmZusammenUtil.createRelation(RelationType
225 .FeatureGroupToReferencingLicenseAgreement, rel.getEdge2().getElementId().toString()))
226 .collect(Collectors.toList());
228 featureGroupElement.getRelations().addAll(laRelations);
231 .saveElement(context, elementContext, featureGroupElement, "update feature group");
236 public void deleteAll(FeatureGroupEntity featureGroup) {
242 public void addReferencingLicenseAgreement(FeatureGroupEntity featureGroup,
243 String licenseAgreementId) {
244 SessionContext context = createSessionContext();
245 ElementContext elementContext = new ElementContext(featureGroup.getVendorLicenseModelId(),
246 featureGroup.getVersion().getId());
248 Optional<ElementInfo> elementInfo =
249 zusammenAdaptor.getElementInfo(context, elementContext, new Id(featureGroup.getId()));
250 if (elementInfo.isPresent()) {
251 ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
252 zusammenElement.setAction(Action.UPDATE);
253 zusammenElement.getRelations().add(VlmZusammenUtil
254 .createRelation(RelationType.FeatureGroupToReferencingLicenseAgreement,
255 licenseAgreementId));
256 zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
257 "add referencing license agreement");
262 public void removeReferencingLicenseAgreement(FeatureGroupEntity featureGroup,
263 String licenseAgreementId) {
264 SessionContext context = createSessionContext();
265 ElementContext elementContext = new ElementContext(featureGroup.getVendorLicenseModelId(),
266 featureGroup.getVersion().getId());
268 Optional<ElementInfo> elementInfo =
269 zusammenAdaptor.getElementInfo(context, elementContext, new Id(featureGroup.getId()));
270 if (elementInfo.isPresent()) {
271 ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
272 zusammenElement.setAction(Action.UPDATE);
273 zusammenElement.setRelations(elementInfo.get().getRelations().stream()
275 relation -> !licenseAgreementId.equals(relation.getEdge2().getElementId().getValue()))
276 .collect(Collectors.toList()));
278 zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
279 "remove referencing license agreement");
283 private ZusammenElement buildFeatureGroupElement(FeatureGroupEntity featureGroup, Action action) {
284 ZusammenElement featureGroupElement =
285 buildElement(featureGroup.getId() == null ? null : new Id(featureGroup.getId()), action);
286 Info info = new Info();
287 info.setName(featureGroup.getName());
288 info.setDescription(featureGroup.getDescription());
289 info.addProperty(ElementPropertyName.elementType.name(), ElementType.FeatureGroup);
290 info.addProperty("partNumber", featureGroup.getPartNumber());
291 featureGroupElement.setInfo(info);
293 featureGroupElement.setRelations(new ArrayList<>());
295 if (featureGroup.getEntitlementPoolIds() != null &&
296 !featureGroup.getEntitlementPoolIds().isEmpty()) {
297 featureGroupElement.getRelations().addAll(featureGroup.getEntitlementPoolIds().stream()
298 .map(rel -> VlmZusammenUtil
299 .createRelation(RelationType.FeatureGroupToEntitlmentPool, rel))
300 .collect(Collectors.toList()));
303 if (featureGroup.getLicenseKeyGroupIds() != null &&
304 !featureGroup.getLicenseKeyGroupIds().isEmpty()) {
305 featureGroupElement.getRelations()
306 .addAll(featureGroup.getLicenseKeyGroupIds().stream()
307 .map(rel -> VlmZusammenUtil
308 .createRelation(RelationType.FeatureGroupToLicenseKeyGroup, rel))
309 .collect(Collectors.toList()));
312 if (featureGroup.getReferencingLicenseAgreements() != null &&
313 !featureGroup.getReferencingLicenseAgreements().isEmpty()) {
314 featureGroupElement.getRelations()
315 .addAll(featureGroup.getReferencingLicenseAgreements().stream()
316 .map(rel -> VlmZusammenUtil
317 .createRelation(RelationType.FeatureGroupToReferencingLicenseAgreement,
319 .collect(Collectors.toList()));
321 return featureGroupElement;