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 org.openecomp.core.zusammen.api.ZusammenAdaptor;
12 import org.openecomp.sdc.datatypes.model.ElementType;
13 import org.openecomp.sdc.vendorlicense.dao.LicenseKeyGroupDao;
14 import org.openecomp.sdc.vendorlicense.dao.impl.zusammen.convertor.ElementToLicenseKeyGroupConvertor;
15 import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity;
16 import org.openecomp.types.ElementPropertyName;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.Optional;
21 import java.util.stream.Collectors;
23 import static org.openecomp.core.zusammen.api.ZusammenUtil.*;
26 public class LicenseKeyGroupZusammenDaoImpl implements LicenseKeyGroupDao {
27 private ZusammenAdaptor zusammenAdaptor;
29 public LicenseKeyGroupZusammenDaoImpl(ZusammenAdaptor zusammenAdaptor) {
30 this.zusammenAdaptor = zusammenAdaptor;
34 public void registerVersioning(String versionableEntityType) {
39 public void create(LicenseKeyGroupEntity licenseKeyGroup) {
40 ZusammenElement licenseKeyGroupElement =
41 buildLicenseKeyGroupElement(licenseKeyGroup, Action.CREATE);
43 ZusammenElement limitsElement = buildStructuralElement(ElementType.Limits, Action.CREATE);
44 licenseKeyGroupElement.addSubElement(limitsElement);
46 ZusammenElement lkgsElement =
47 buildStructuralElement(ElementType.LicenseKeyGroups, Action.IGNORE);
48 lkgsElement.addSubElement(licenseKeyGroupElement);
50 SessionContext context = createSessionContext();
51 Element lkgsSavedElement = zusammenAdaptor.saveElement(context,
52 new ElementContext(licenseKeyGroup.getVendorLicenseModelId(),
53 licenseKeyGroup.getVersion().getId()), lkgsElement, "Create license Key Group");
56 .setId(lkgsSavedElement.getSubElements().iterator().next().getElementId().getValue());
60 public void update(LicenseKeyGroupEntity licenseKeyGroup) {
61 ZusammenElement licenseKeyGroupElement =
62 buildLicenseKeyGroupElement(licenseKeyGroup, Action.UPDATE);
64 SessionContext context = createSessionContext();
65 ElementContext elementContext = new ElementContext(licenseKeyGroup.getVendorLicenseModelId(),
66 licenseKeyGroup.getVersion().getId());
68 Optional<ElementInfo> lkgFromDb = zusammenAdaptor.getElementInfo(context, elementContext,
69 new Id(licenseKeyGroup.getId()));
71 if (lkgFromDb.isPresent()) {
73 if (licenseKeyGroupElement.getRelations() == null) {
74 licenseKeyGroupElement.setRelations(new ArrayList<>());
77 if (lkgFromDb.get().getRelations() != null && !lkgFromDb.get().getRelations().isEmpty()) {
78 licenseKeyGroupElement.getRelations().addAll(lkgFromDb.get().getRelations());
81 zusammenAdaptor.saveElement(context, elementContext, licenseKeyGroupElement,
82 String.format("Update license key group with id %s", licenseKeyGroup.getId()));
86 public LicenseKeyGroupEntity get(LicenseKeyGroupEntity licenseKeyGroup) {
87 SessionContext context = createSessionContext();
88 ElementContext elementContext = new ElementContext(licenseKeyGroup.getVendorLicenseModelId(),
89 licenseKeyGroup.getVersion().getId());
90 ElementToLicenseKeyGroupConvertor convertor = new ElementToLicenseKeyGroupConvertor();
91 return zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseKeyGroup.getId()))
93 LicenseKeyGroupEntity entity = convertor.convert(elementInfo);
94 entity.setVendorLicenseModelId(licenseKeyGroup.getVendorLicenseModelId());
95 entity.setVersion(licenseKeyGroup.getVersion());
102 public void delete(LicenseKeyGroupEntity licenseKeyGroup) {
103 ZusammenElement zusammenElement = buildElement(new Id(licenseKeyGroup.getId()), Action.DELETE);
105 SessionContext context = createSessionContext();
106 ElementContext elementContext = new ElementContext(licenseKeyGroup.getVendorLicenseModelId(),
107 licenseKeyGroup.getVersion().getId());
109 zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
110 "delete license key group. id:" + licenseKeyGroup.getId() + ".");
114 public Collection<LicenseKeyGroupEntity> list(LicenseKeyGroupEntity licenseKeyGroup) {
115 SessionContext context = createSessionContext();
116 ElementContext elementContext = new ElementContext(licenseKeyGroup.getVendorLicenseModelId(),
117 licenseKeyGroup.getVersion().getId());
118 ElementToLicenseKeyGroupConvertor convertor = new ElementToLicenseKeyGroupConvertor();
119 return zusammenAdaptor
120 .listElementsByName(context, elementContext, null, ElementType.LicenseKeyGroups.name())
121 .stream().map(elementInfo -> {
122 LicenseKeyGroupEntity entity = convertor.convert(elementInfo);
123 entity.setVendorLicenseModelId(licenseKeyGroup.getVendorLicenseModelId());
124 entity.setVersion(licenseKeyGroup.getVersion());
127 .collect(Collectors.toList());
131 public long count(LicenseKeyGroupEntity licenseKeyGroup) {
132 SessionContext context = createSessionContext();
133 ElementContext elementContext = new ElementContext(licenseKeyGroup.getVendorLicenseModelId(),
134 licenseKeyGroup.getVersion().getId());
136 return zusammenAdaptor
137 .listElementsByName(context, elementContext, null, ElementType.LicenseKeyGroups.name())
142 public void deleteAll(LicenseKeyGroupEntity licenseKeyGroup) {
147 public void removeReferencingFeatureGroup(LicenseKeyGroupEntity licenseKeyGroup,
148 String featureGroupId) {
149 SessionContext context = createSessionContext();
150 ElementContext elementContext = new ElementContext(licenseKeyGroup.getVendorLicenseModelId(),
151 licenseKeyGroup.getVersion().getId());
153 Optional<ElementInfo> elementInfo =
154 zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseKeyGroup.getId()));
156 if (elementInfo.isPresent()) {
157 ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
158 zusammenElement.setAction(Action.UPDATE);
159 zusammenElement.setRelations(elementInfo.get().getRelations().stream()
160 .filter(relation -> !featureGroupId
161 .equals(relation.getEdge2().getElementId().getValue()))
162 .collect(Collectors.toList()));
164 zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
165 "remove referencing feature group");
170 public void addReferencingFeatureGroup(LicenseKeyGroupEntity licenseKeyGroup,
171 String featureGroupId) {
172 SessionContext context = createSessionContext();
173 ElementContext elementContext = new ElementContext(licenseKeyGroup.getVendorLicenseModelId(),
174 licenseKeyGroup.getVersion().getId());
176 Optional<ElementInfo> elementInfo =
177 zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseKeyGroup.getId()));
179 if (elementInfo.isPresent()) {
180 ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
181 zusammenElement.setAction(Action.UPDATE);
182 if (zusammenElement.getRelations() == null) {
183 zusammenElement.setRelations(new ArrayList<>());
185 zusammenElement.getRelations().add(VlmZusammenUtil
186 .createRelation(RelationType.LicenseKeyGroupToReferencingFeatureGroup,
189 .saveElement(context, elementContext, zusammenElement, "add referencing feature group");
193 private ZusammenElement buildLicenseKeyGroupElement(LicenseKeyGroupEntity licenseKeyGroup,
195 ZusammenElement lkgElement =
196 buildElement(licenseKeyGroup.getId() == null ? null : new Id(licenseKeyGroup.getId()),
198 Info info = new Info();
199 info.setName(licenseKeyGroup.getName());
200 info.setDescription(licenseKeyGroup.getDescription());
201 info.addProperty(ElementPropertyName.elementType.name(), ElementType.LicenseKeyGroup);
202 info.addProperty("version_uuid", licenseKeyGroup.getVersionUuId());
203 info.addProperty("LicenseKeyType", licenseKeyGroup.getType());
204 info.addProperty("operational_scope", licenseKeyGroup.getOperationalScope());
205 info.addProperty("startDate", licenseKeyGroup.getStartDate());
206 info.addProperty("expiryDate", licenseKeyGroup.getExpiryDate());
207 info.addProperty("thresholdValue", licenseKeyGroup.getThresholdValue());
208 info.addProperty("thresholdUnits", licenseKeyGroup.getThresholdUnits());
209 info.addProperty("increments", licenseKeyGroup.getIncrements());
210 info.addProperty("manufacturerReferenceNumber", licenseKeyGroup.getManufacturerReferenceNumber());
211 lkgElement.setInfo(info);
213 if (licenseKeyGroup.getReferencingFeatureGroups() != null
214 && !licenseKeyGroup.getReferencingFeatureGroups().isEmpty()) {
215 lkgElement.setRelations(licenseKeyGroup.getReferencingFeatureGroups().stream()
216 .map(rel -> VlmZusammenUtil
217 .createRelation(RelationType.LicenseKeyGroupToReferencingFeatureGroup, rel))
218 .collect(Collectors.toList()));