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.buildElement;
26 import static org.openecomp.core.zusammen.api.ZusammenUtil.buildStructuralElement;
27 import static org.openecomp.core.zusammen.api.ZusammenUtil.createSessionContext;
29 public class FeatureGroupDaoZusammenImpl implements FeatureGroupDao {
31 private ZusammenAdaptor zusammenAdaptor;
33 public FeatureGroupDaoZusammenImpl(ZusammenAdaptor zusammenAdaptor) {
34 this.zusammenAdaptor = zusammenAdaptor;
38 public void registerVersioning(String versionableEntityType) {
43 public void create(FeatureGroupEntity featureGroup) {
44 ZusammenElement featureGroupElement = buildFeatureGroupElement(featureGroup, Action.CREATE);
46 ZusammenElement featureGroupsElement =
47 buildStructuralElement(ElementType.FeatureGroups, Action.IGNORE);
49 featureGroupsElement.addSubElement(featureGroupElement);
51 SessionContext context = createSessionContext();
52 Element featureGroupsSavedElement = zusammenAdaptor.saveElement(context,
53 new ElementContext(featureGroup.getVendorLicenseModelId(),
54 featureGroup.getVersion().getId()), featureGroupsElement, "Create feature group");
57 featureGroupsSavedElement.getSubElements().iterator().next().getElementId().getValue());
61 public void update(FeatureGroupEntity featureGroup) {
62 ZusammenElement featureGroupElement = buildFeatureGroupElement(featureGroup, Action.UPDATE);
64 SessionContext context = createSessionContext();
65 zusammenAdaptor.saveElement(context, new ElementContext(featureGroup.getVendorLicenseModelId(),
66 featureGroup.getVersion().getId()), featureGroupElement,
67 String.format("Update feature group with id %s", featureGroup.getId()));
71 public FeatureGroupEntity get(FeatureGroupEntity featureGroup) {
72 SessionContext context = createSessionContext();
73 ElementContext elementContext = new ElementContext(featureGroup.getVendorLicenseModelId(),
74 featureGroup.getVersion().getId());
76 return zusammenAdaptor.getElementInfo(context, elementContext, new Id(featureGroup.getId()))
78 FeatureGroupEntity entity = new ElementToFeatureGroupConvertor().convert(elementInfo);
79 entity.setVendorLicenseModelId(featureGroup.getVendorLicenseModelId());
80 entity.setVersion(featureGroup.getVersion());
87 public void delete(FeatureGroupEntity featureGroup) {
88 ZusammenElement zusammenElement = buildElement(new Id(featureGroup.getId()), Action.DELETE);
90 SessionContext context = createSessionContext();
91 ElementContext elementContext = new ElementContext(featureGroup.getVendorLicenseModelId(),
92 featureGroup.getVersion().getId());
93 zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
94 "delete feature group. id:" + featureGroup.getId() + ".");
98 public Collection<FeatureGroupEntity> list(FeatureGroupEntity featureGroup) {
99 SessionContext context = createSessionContext();
100 ElementContext elementContext = new ElementContext(featureGroup.getVendorLicenseModelId(),
101 featureGroup.getVersion().getId());
103 ElementToFeatureGroupConvertor convertor = new ElementToFeatureGroupConvertor();
104 return zusammenAdaptor
105 .listElementsByName(context, elementContext, null, ElementType.FeatureGroups.name())
106 .stream().map(elementInfo -> {
107 FeatureGroupEntity entity = convertor.convert(
109 entity.setVendorLicenseModelId(featureGroup.getVendorLicenseModelId());
110 entity.setVersion(featureGroup.getVersion());
113 .collect(Collectors.toList());
117 public long count(FeatureGroupEntity featureGroup) {
118 SessionContext context = createSessionContext();
119 ElementContext elementContext = new ElementContext(featureGroup.getVendorLicenseModelId(),
120 featureGroup.getVersion().getId());
122 return zusammenAdaptor
123 .listElementsByName(context, elementContext, null, ElementType.FeatureGroups.name())
128 public void removeEntitlementPool(FeatureGroupEntity featureGroup, String entitlementPoolId) {
129 removeRelationToContainedEntity(featureGroup, entitlementPoolId, "entitlement pool");
133 public void removeLicenseKeyGroup(FeatureGroupEntity featureGroup, String licenseKeyGroupId) {
134 removeRelationToContainedEntity(featureGroup, licenseKeyGroupId, "license Key Group");
137 private void removeRelationToContainedEntity(FeatureGroupEntity featureGroup,
138 String containedEntityId,
139 String containedEntityType) {
140 SessionContext context = createSessionContext();
141 ElementContext elementContext = new ElementContext(featureGroup.getVendorLicenseModelId(),
142 featureGroup.getVersion().getId());
144 Optional<ElementInfo> elementInfo = zusammenAdaptor.getElementInfo(context,
145 elementContext, new Id(featureGroup.getId()));
146 if (elementInfo.isPresent()) {
147 ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
148 zusammenElement.setAction(Action.UPDATE);
149 zusammenElement.setRelations(elementInfo.get().getRelations().stream()
151 relation -> !containedEntityId.equals(relation.getEdge2().getElementId().getValue()))
152 .collect(Collectors.toList()));
153 zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
154 String.format("remove %s", containedEntityType));
159 public void updateFeatureGroup(FeatureGroupEntity featureGroup,
160 Set<String> addedEntitlementPools,
161 Set<String> removedEntitlementPools,
162 Set<String> addedLicenseKeyGroups,
163 Set<String> removedLicenseKeyGroups) {
164 ZusammenElement featureGroupElement = buildFeatureGroupElement(featureGroup, Action.UPDATE);
165 SessionContext context = createSessionContext();
166 ElementContext elementContext = new ElementContext(featureGroup.getVendorLicenseModelId(),
167 featureGroup.getVersion().getId());
168 ElementToFeatureGroupConvertor convertor = new ElementToFeatureGroupConvertor();
169 Optional<ElementInfo> elementInfo = zusammenAdaptor.getElementInfo(context,
170 elementContext, new Id(featureGroup.getId()));
171 if (elementInfo.isPresent()) {
172 FeatureGroupEntity currentFeatureGroup = convertor.convert(elementInfo.get());
173 currentFeatureGroup.setVendorLicenseModelId(featureGroup.getVendorLicenseModelId());
174 currentFeatureGroup.setVersion(featureGroup.getVersion());
175 if (!(removedEntitlementPools == null)) {
176 currentFeatureGroup.getEntitlementPoolIds().removeAll(removedEntitlementPools);
178 if (!(addedEntitlementPools == null)) {
179 currentFeatureGroup.getEntitlementPoolIds().addAll(addedEntitlementPools);
182 if (featureGroupElement.getRelations() == null) {
183 featureGroupElement.setRelations(new ArrayList<>());
185 featureGroupElement.getRelations()
186 .addAll(currentFeatureGroup.getEntitlementPoolIds().stream()
187 .map(relation -> VlmZusammenUtil
188 .createRelation(RelationType.FeatureGroupToEntitlmentPool, relation))
189 .collect(Collectors.toList()));
191 if (!(removedLicenseKeyGroups == null)) {
192 currentFeatureGroup.getLicenseKeyGroupIds().removeAll(removedLicenseKeyGroups);
194 if (!(addedLicenseKeyGroups == null)) {
195 currentFeatureGroup.getLicenseKeyGroupIds().addAll(addedLicenseKeyGroups);
198 featureGroupElement.getRelations()
199 .addAll(currentFeatureGroup.getLicenseKeyGroupIds().stream()
200 .map(relation -> VlmZusammenUtil
201 .createRelation(RelationType.FeatureGroupToLicenseKeyGroup, relation))
202 .collect(Collectors.toList()));
204 Collection<Relation> LaRelations = elementInfo.get().getRelations().stream().filter
205 (rel -> rel.getType()
206 .equals(RelationType.FeatureGroupToReferencingLicenseAgreement.name()))
207 .map(rel -> VlmZusammenUtil.createRelation(RelationType
208 .FeatureGroupToReferencingLicenseAgreement, rel.getEdge2().getElementId().toString()))
209 .collect(Collectors.toList());
211 featureGroupElement.getRelations().addAll(LaRelations);
214 .saveElement(context, elementContext, featureGroupElement, "update feature group");
219 public void deleteAll(FeatureGroupEntity featureGroup) {
225 public void addReferencingLicenseAgreement(FeatureGroupEntity featureGroup,
226 String licenseAgreementId) {
227 SessionContext context = createSessionContext();
228 ElementContext elementContext = new ElementContext(featureGroup.getVendorLicenseModelId(),
229 featureGroup.getVersion().getId());
231 Optional<ElementInfo> elementInfo =
232 zusammenAdaptor.getElementInfo(context, elementContext, new Id(featureGroup.getId()));
233 if (elementInfo.isPresent()) {
234 ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
235 zusammenElement.setAction(Action.UPDATE);
236 zusammenElement.getRelations().add(VlmZusammenUtil
237 .createRelation(RelationType.FeatureGroupToReferencingLicenseAgreement,
238 licenseAgreementId));
239 zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
240 "add referencing license agreement");
245 public void removeReferencingLicenseAgreement(FeatureGroupEntity featureGroup,
246 String licenseAgreementId) {
247 SessionContext context = createSessionContext();
248 ElementContext elementContext = new ElementContext(featureGroup.getVendorLicenseModelId(),
249 featureGroup.getVersion().getId());
251 Optional<ElementInfo> elementInfo =
252 zusammenAdaptor.getElementInfo(context, elementContext, new Id(featureGroup.getId()));
253 if (elementInfo.isPresent()) {
254 ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
255 zusammenElement.setAction(Action.UPDATE);
256 zusammenElement.setRelations(elementInfo.get().getRelations().stream()
258 relation -> !licenseAgreementId.equals(relation.getEdge2().getElementId().getValue()))
259 .collect(Collectors.toList()));
261 zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
262 "remove referencing license agreement");
266 private ZusammenElement buildFeatureGroupElement(FeatureGroupEntity featureGroup, Action action) {
267 ZusammenElement featureGroupElement =
268 buildElement(featureGroup.getId() == null ? null : new Id(featureGroup.getId()), action);
269 Info info = new Info();
270 info.setName(featureGroup.getName());
271 info.setDescription(featureGroup.getDescription());
272 info.addProperty(ElementPropertyName.elementType.name(), ElementType.FeatureGroup);
273 info.addProperty("partNumber", featureGroup.getPartNumber());
274 info.addProperty("manufacturerReferenceNumber", featureGroup.getManufacturerReferenceNumber());
275 featureGroupElement.setInfo(info);
277 featureGroupElement.setRelations(new ArrayList<>());
279 if (featureGroup.getEntitlementPoolIds() != null &&
280 featureGroup.getEntitlementPoolIds().size() > 0) {
281 featureGroupElement.getRelations().addAll(featureGroup.getEntitlementPoolIds().stream()
282 .map(rel -> VlmZusammenUtil
283 .createRelation(RelationType.FeatureGroupToEntitlmentPool, rel))
284 .collect(Collectors.toList()));
287 if (featureGroup.getLicenseKeyGroupIds() != null &&
288 featureGroup.getLicenseKeyGroupIds().size() > 0) {
289 featureGroupElement.getRelations()
290 .addAll(featureGroup.getLicenseKeyGroupIds().stream()
291 .map(rel -> VlmZusammenUtil
292 .createRelation(RelationType.FeatureGroupToLicenseKeyGroup, rel))
293 .collect(Collectors.toList()));
296 if (featureGroup.getReferencingLicenseAgreements() != null &&
297 featureGroup.getReferencingLicenseAgreements().size() > 0) {
298 featureGroupElement.getRelations()
299 .addAll(featureGroup.getReferencingLicenseAgreements().stream()
300 .map(rel -> VlmZusammenUtil
301 .createRelation(RelationType.FeatureGroupToReferencingLicenseAgreement,
303 .collect(Collectors.toList()));
305 return featureGroupElement;