1 package org.openecomp.sdc.vendorlicense.dao.impl.zusammen;
3 import com.amdocs.zusammen.adaptor.inbound.api.types.item.Element;
4 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ElementInfo;
5 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ZusammenElement;
6 import com.amdocs.zusammen.datatypes.Id;
7 import com.amdocs.zusammen.datatypes.SessionContext;
8 import com.amdocs.zusammen.datatypes.item.Action;
9 import com.amdocs.zusammen.datatypes.item.ElementContext;
10 import com.amdocs.zusammen.datatypes.item.Info;
11 import com.amdocs.zusammen.datatypes.item.Relation;
12 import org.openecomp.core.zusammen.api.ZusammenAdaptor;
13 import org.openecomp.sdc.datatypes.model.ElementType;
14 import org.openecomp.sdc.vendorlicense.dao.FeatureGroupDao;
15 import org.openecomp.sdc.vendorlicense.dao.impl.zusammen.convertor.ElementToFeatureGroupConvertor;
16 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity;
17 import org.openecomp.types.ElementPropertyName;
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.Optional;
23 import java.util.stream.Collectors;
25 import static org.openecomp.core.zusammen.api.ZusammenUtil.*;
27 public class FeatureGroupDaoZusammenImpl implements FeatureGroupDao {
29 private ZusammenAdaptor zusammenAdaptor;
31 public FeatureGroupDaoZusammenImpl(ZusammenAdaptor zusammenAdaptor) {
32 this.zusammenAdaptor = zusammenAdaptor;
36 public void registerVersioning(String versionableEntityType) {
41 public void create(FeatureGroupEntity featureGroup) {
42 ZusammenElement featureGroupElement = buildFeatureGroupElement(featureGroup, Action.CREATE);
44 ZusammenElement featureGroupsElement =
45 buildStructuralElement(ElementType.FeatureGroups, Action.IGNORE);
47 featureGroupsElement.addSubElement(featureGroupElement);
49 SessionContext context = createSessionContext();
50 Element featureGroupsSavedElement = zusammenAdaptor.saveElement(context,
51 new ElementContext(featureGroup.getVendorLicenseModelId(),
52 featureGroup.getVersion().getId()), featureGroupsElement, "Create feature group");
55 featureGroupsSavedElement.getSubElements().iterator().next().getElementId().getValue());
59 public void update(FeatureGroupEntity featureGroup) {
60 ZusammenElement featureGroupElement = buildFeatureGroupElement(featureGroup, Action.UPDATE);
62 SessionContext context = createSessionContext();
63 zusammenAdaptor.saveElement(context, new ElementContext(featureGroup.getVendorLicenseModelId(),
64 featureGroup.getVersion().getId()), featureGroupElement,
65 String.format("Update feature group with id %s", featureGroup.getId()));
69 public FeatureGroupEntity get(FeatureGroupEntity featureGroup) {
70 SessionContext context = createSessionContext();
71 ElementContext elementContext = new ElementContext(featureGroup.getVendorLicenseModelId(),
72 featureGroup.getVersion().getId());
74 return zusammenAdaptor.getElementInfo(context, elementContext, new Id(featureGroup.getId()))
76 FeatureGroupEntity entity = new ElementToFeatureGroupConvertor().convert(elementInfo);
77 entity.setVendorLicenseModelId(featureGroup.getVendorLicenseModelId());
78 entity.setVersion(featureGroup.getVersion());
85 public void delete(FeatureGroupEntity featureGroup) {
86 ZusammenElement zusammenElement = buildElement(new Id(featureGroup.getId()), Action.DELETE);
88 SessionContext context = createSessionContext();
89 ElementContext elementContext = new ElementContext(featureGroup.getVendorLicenseModelId(),
90 featureGroup.getVersion().getId());
91 zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
92 "delete feature group. id:" + featureGroup.getId() + ".");
96 public Collection<FeatureGroupEntity> list(FeatureGroupEntity featureGroup) {
97 SessionContext context = createSessionContext();
98 ElementContext elementContext = new ElementContext(featureGroup.getVendorLicenseModelId(),
99 featureGroup.getVersion().getId());
101 ElementToFeatureGroupConvertor convertor = new ElementToFeatureGroupConvertor();
102 return zusammenAdaptor
103 .listElementsByName(context, elementContext, null, ElementType.FeatureGroups.name())
104 .stream().map(elementInfo -> {
105 FeatureGroupEntity entity = convertor.convert(
107 entity.setVendorLicenseModelId(featureGroup.getVendorLicenseModelId());
108 entity.setVersion(featureGroup.getVersion());
111 .collect(Collectors.toList());
115 public long count(FeatureGroupEntity featureGroup) {
116 SessionContext context = createSessionContext();
117 ElementContext elementContext = new ElementContext(featureGroup.getVendorLicenseModelId(),
118 featureGroup.getVersion().getId());
120 return zusammenAdaptor
121 .listElementsByName(context, elementContext, null, ElementType.FeatureGroups.name())
126 public void removeEntitlementPool(FeatureGroupEntity featureGroup, String entitlementPoolId) {
127 removeRelationToContainedEntity(featureGroup, entitlementPoolId, "entitlement pool");
131 public void removeLicenseKeyGroup(FeatureGroupEntity featureGroup, String licenseKeyGroupId) {
132 removeRelationToContainedEntity(featureGroup, licenseKeyGroupId, "license Key Group");
135 private void removeRelationToContainedEntity(FeatureGroupEntity featureGroup,
136 String containedEntityId,
137 String containedEntityType) {
138 SessionContext context = createSessionContext();
139 ElementContext elementContext = new ElementContext(featureGroup.getVendorLicenseModelId(),
140 featureGroup.getVersion().getId());
142 Optional<ElementInfo> elementInfo = zusammenAdaptor.getElementInfo(context,
143 elementContext, new Id(featureGroup.getId()));
144 if (elementInfo.isPresent()) {
145 ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
146 zusammenElement.setAction(Action.UPDATE);
147 zusammenElement.setRelations(elementInfo.get().getRelations().stream()
149 relation -> !containedEntityId.equals(relation.getEdge2().getElementId().getValue()))
150 .collect(Collectors.toList()));
151 zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
152 String.format("remove %s", containedEntityType));
157 public void updateFeatureGroup(FeatureGroupEntity featureGroup,
158 Set<String> addedEntitlementPools,
159 Set<String> removedEntitlementPools,
160 Set<String> addedLicenseKeyGroups,
161 Set<String> removedLicenseKeyGroups) {
162 ZusammenElement featureGroupElement = buildFeatureGroupElement(featureGroup, Action.UPDATE);
163 SessionContext context = createSessionContext();
164 ElementContext elementContext = new ElementContext(featureGroup.getVendorLicenseModelId(),
165 featureGroup.getVersion().getId());
166 ElementToFeatureGroupConvertor convertor = new ElementToFeatureGroupConvertor();
167 Optional<ElementInfo> elementInfo = zusammenAdaptor.getElementInfo(context,
168 elementContext, new Id(featureGroup.getId()));
169 if (elementInfo.isPresent()) {
170 FeatureGroupEntity currentFeatureGroup = convertor.convert(elementInfo.get());
171 currentFeatureGroup.setVendorLicenseModelId(featureGroup.getVendorLicenseModelId());
172 currentFeatureGroup.setVersion(featureGroup.getVersion());
173 if (!(removedEntitlementPools == null)) {
174 currentFeatureGroup.getEntitlementPoolIds().removeAll(removedEntitlementPools);
176 if (!(addedEntitlementPools == null)) {
177 currentFeatureGroup.getEntitlementPoolIds().addAll(addedEntitlementPools);
180 if (featureGroupElement.getRelations() == null) {
181 featureGroupElement.setRelations(new ArrayList<>());
183 featureGroupElement.getRelations()
184 .addAll(currentFeatureGroup.getEntitlementPoolIds().stream()
185 .map(relation -> VlmZusammenUtil
186 .createRelation(RelationType.FeatureGroupToEntitlmentPool, relation))
187 .collect(Collectors.toList()));
189 if (!(removedLicenseKeyGroups == null)) {
190 currentFeatureGroup.getLicenseKeyGroupIds().removeAll(removedLicenseKeyGroups);
192 if (!(addedLicenseKeyGroups == null)) {
193 currentFeatureGroup.getLicenseKeyGroupIds().addAll(addedLicenseKeyGroups);
196 featureGroupElement.getRelations()
197 .addAll(currentFeatureGroup.getLicenseKeyGroupIds().stream()
198 .map(relation -> VlmZusammenUtil
199 .createRelation(RelationType.FeatureGroupToLicenseKeyGroup, relation))
200 .collect(Collectors.toList()));
202 Collection<Relation> laRelations = elementInfo.get().getRelations().stream().filter
203 (rel -> rel.getType()
204 .equals(RelationType.FeatureGroupToReferencingLicenseAgreement.name()))
205 .map(rel -> VlmZusammenUtil.createRelation(RelationType
206 .FeatureGroupToReferencingLicenseAgreement, rel.getEdge2().getElementId().toString()))
207 .collect(Collectors.toList());
209 featureGroupElement.getRelations().addAll(laRelations);
212 .saveElement(context, elementContext, featureGroupElement, "update feature group");
217 public void deleteAll(FeatureGroupEntity featureGroup) {
223 public void addReferencingLicenseAgreement(FeatureGroupEntity featureGroup,
224 String licenseAgreementId) {
225 SessionContext context = createSessionContext();
226 ElementContext elementContext = new ElementContext(featureGroup.getVendorLicenseModelId(),
227 featureGroup.getVersion().getId());
229 Optional<ElementInfo> elementInfo =
230 zusammenAdaptor.getElementInfo(context, elementContext, new Id(featureGroup.getId()));
231 if (elementInfo.isPresent()) {
232 ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
233 zusammenElement.setAction(Action.UPDATE);
234 zusammenElement.getRelations().add(VlmZusammenUtil
235 .createRelation(RelationType.FeatureGroupToReferencingLicenseAgreement,
236 licenseAgreementId));
237 zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
238 "add referencing license agreement");
243 public void removeReferencingLicenseAgreement(FeatureGroupEntity featureGroup,
244 String licenseAgreementId) {
245 SessionContext context = createSessionContext();
246 ElementContext elementContext = new ElementContext(featureGroup.getVendorLicenseModelId(),
247 featureGroup.getVersion().getId());
249 Optional<ElementInfo> elementInfo =
250 zusammenAdaptor.getElementInfo(context, elementContext, new Id(featureGroup.getId()));
251 if (elementInfo.isPresent()) {
252 ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
253 zusammenElement.setAction(Action.UPDATE);
254 zusammenElement.setRelations(elementInfo.get().getRelations().stream()
256 relation -> !licenseAgreementId.equals(relation.getEdge2().getElementId().getValue()))
257 .collect(Collectors.toList()));
259 zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
260 "remove referencing license agreement");
264 private ZusammenElement buildFeatureGroupElement(FeatureGroupEntity featureGroup, Action action) {
265 ZusammenElement featureGroupElement =
266 buildElement(featureGroup.getId() == null ? null : new Id(featureGroup.getId()), action);
267 Info info = new Info();
268 info.setName(featureGroup.getName());
269 info.setDescription(featureGroup.getDescription());
270 info.addProperty(ElementPropertyName.elementType.name(), ElementType.FeatureGroup);
271 info.addProperty("partNumber", featureGroup.getPartNumber());
272 info.addProperty("manufacturerReferenceNumber", featureGroup.getManufacturerReferenceNumber());
273 featureGroupElement.setInfo(info);
275 featureGroupElement.setRelations(new ArrayList<>());
277 if (featureGroup.getEntitlementPoolIds() != null &&
278 !featureGroup.getEntitlementPoolIds().isEmpty()) {
279 featureGroupElement.getRelations().addAll(featureGroup.getEntitlementPoolIds().stream()
280 .map(rel -> VlmZusammenUtil
281 .createRelation(RelationType.FeatureGroupToEntitlmentPool, rel))
282 .collect(Collectors.toList()));
285 if (featureGroup.getLicenseKeyGroupIds() != null &&
286 !featureGroup.getLicenseKeyGroupIds().isEmpty()) {
287 featureGroupElement.getRelations()
288 .addAll(featureGroup.getLicenseKeyGroupIds().stream()
289 .map(rel -> VlmZusammenUtil
290 .createRelation(RelationType.FeatureGroupToLicenseKeyGroup, rel))
291 .collect(Collectors.toList()));
294 if (featureGroup.getReferencingLicenseAgreements() != null &&
295 !featureGroup.getReferencingLicenseAgreements().isEmpty()) {
296 featureGroupElement.getRelations()
297 .addAll(featureGroup.getReferencingLicenseAgreements().stream()
298 .map(rel -> VlmZusammenUtil
299 .createRelation(RelationType.FeatureGroupToReferencingLicenseAgreement,
301 .collect(Collectors.toList()));
303 return featureGroupElement;