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.buildElement;
24 import static org.openecomp.core.zusammen.api.ZusammenUtil.buildStructuralElement;
25 import static org.openecomp.core.zusammen.api.ZusammenUtil.createSessionContext;
28 public class LicenseKeyGroupZusammenDaoImpl implements LicenseKeyGroupDao {
29 private ZusammenAdaptor zusammenAdaptor;
31 public LicenseKeyGroupZusammenDaoImpl(ZusammenAdaptor zusammenAdaptor) {
32 this.zusammenAdaptor = zusammenAdaptor;
36 public void registerVersioning(String versionableEntityType) {
41 public void create(LicenseKeyGroupEntity licenseKeyGroup) {
42 ZusammenElement licenseKeyGroupElement =
43 buildLicenseKeyGroupElement(licenseKeyGroup, Action.CREATE);
45 ZusammenElement limitsElement = buildStructuralElement(ElementType.Limits, Action.CREATE);
46 licenseKeyGroupElement.addSubElement(limitsElement);
48 ZusammenElement lkgsElement =
49 buildStructuralElement(ElementType.LicenseKeyGroups, Action.IGNORE);
50 lkgsElement.addSubElement(licenseKeyGroupElement);
52 SessionContext context = createSessionContext();
53 Element lkgsSavedElement = zusammenAdaptor.saveElement(context,
54 new ElementContext(licenseKeyGroup.getVendorLicenseModelId(),
55 licenseKeyGroup.getVersion().getId()), lkgsElement, "Create license Key Group");
58 .setId(lkgsSavedElement.getSubElements().iterator().next().getElementId().getValue());
62 public void update(LicenseKeyGroupEntity licenseKeyGroup) {
63 ZusammenElement licenseKeyGroupElement =
64 buildLicenseKeyGroupElement(licenseKeyGroup, Action.UPDATE);
66 SessionContext context = createSessionContext();
67 ElementContext elementContext = new ElementContext(licenseKeyGroup.getVendorLicenseModelId(),
68 licenseKeyGroup.getVersion().getId());
70 Optional<ElementInfo> lkgFromDb = zusammenAdaptor.getElementInfo(context, elementContext,
71 new Id(licenseKeyGroup.getId()));
73 if (lkgFromDb.isPresent()) {
75 if (licenseKeyGroupElement.getRelations() == null) {
76 licenseKeyGroupElement.setRelations(new ArrayList<>());
79 if (lkgFromDb.get().getRelations() != null && lkgFromDb.get().getRelations().size() > 0) {
80 licenseKeyGroupElement.getRelations().addAll(lkgFromDb.get().getRelations());
83 zusammenAdaptor.saveElement(context, elementContext, licenseKeyGroupElement,
84 String.format("Update license key group with id %s", licenseKeyGroup.getId()));
88 public LicenseKeyGroupEntity get(LicenseKeyGroupEntity licenseKeyGroup) {
89 SessionContext context = createSessionContext();
90 ElementContext elementContext = new ElementContext(licenseKeyGroup.getVendorLicenseModelId(),
91 licenseKeyGroup.getVersion().getId());
92 ElementToLicenseKeyGroupConvertor convertor = new ElementToLicenseKeyGroupConvertor();
93 return zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseKeyGroup.getId()))
95 LicenseKeyGroupEntity entity = convertor.convert(elementInfo);
96 entity.setVendorLicenseModelId(licenseKeyGroup.getVendorLicenseModelId());
97 entity.setVersion(licenseKeyGroup.getVersion());
104 public void delete(LicenseKeyGroupEntity licenseKeyGroup) {
105 ZusammenElement zusammenElement = buildElement(new Id(licenseKeyGroup.getId()), Action.DELETE);
107 SessionContext context = createSessionContext();
108 ElementContext elementContext = new ElementContext(licenseKeyGroup.getVendorLicenseModelId(),
109 licenseKeyGroup.getVersion().getId());
111 zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
112 "delete license key group. id:" + licenseKeyGroup.getId() + ".");
116 public Collection<LicenseKeyGroupEntity> list(LicenseKeyGroupEntity licenseKeyGroup) {
117 SessionContext context = createSessionContext();
118 ElementContext elementContext = new ElementContext(licenseKeyGroup.getVendorLicenseModelId(),
119 licenseKeyGroup.getVersion().getId());
120 ElementToLicenseKeyGroupConvertor convertor = new ElementToLicenseKeyGroupConvertor();
121 return zusammenAdaptor
122 .listElementsByName(context, elementContext, null, ElementType.LicenseKeyGroups.name())
123 .stream().map(elementInfo -> {
124 LicenseKeyGroupEntity entity = convertor.convert(elementInfo);
125 entity.setVendorLicenseModelId(licenseKeyGroup.getVendorLicenseModelId());
126 entity.setVersion(licenseKeyGroup.getVersion());
129 .collect(Collectors.toList());
133 public long count(LicenseKeyGroupEntity licenseKeyGroup) {
134 SessionContext context = createSessionContext();
135 ElementContext elementContext = new ElementContext(licenseKeyGroup.getVendorLicenseModelId(),
136 licenseKeyGroup.getVersion().getId());
138 return zusammenAdaptor
139 .listElementsByName(context, elementContext, null, ElementType.LicenseKeyGroups.name())
144 public void deleteAll(LicenseKeyGroupEntity licenseKeyGroup) {
149 public void removeReferencingFeatureGroup(LicenseKeyGroupEntity licenseKeyGroup,
150 String featureGroupId) {
151 SessionContext context = createSessionContext();
152 ElementContext elementContext = new ElementContext(licenseKeyGroup.getVendorLicenseModelId(),
153 licenseKeyGroup.getVersion().getId());
155 Optional<ElementInfo> elementInfo =
156 zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseKeyGroup.getId()));
158 if (elementInfo.isPresent()) {
159 ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
160 zusammenElement.setAction(Action.UPDATE);
161 zusammenElement.setRelations(elementInfo.get().getRelations().stream()
162 .filter(relation -> !featureGroupId
163 .equals(relation.getEdge2().getElementId().getValue()))
164 .collect(Collectors.toList()));
166 zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
167 "remove referencing feature group");
172 public void addReferencingFeatureGroup(LicenseKeyGroupEntity licenseKeyGroup,
173 String featureGroupId) {
174 SessionContext context = createSessionContext();
175 ElementContext elementContext = new ElementContext(licenseKeyGroup.getVendorLicenseModelId(),
176 licenseKeyGroup.getVersion().getId());
178 Optional<ElementInfo> elementInfo =
179 zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseKeyGroup.getId()));
181 if (elementInfo.isPresent()) {
182 ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
183 zusammenElement.setAction(Action.UPDATE);
184 if (zusammenElement.getRelations() == null) {
185 zusammenElement.setRelations(new ArrayList<>());
187 zusammenElement.getRelations().add(VlmZusammenUtil
188 .createRelation(RelationType.LicenseKeyGroupToReferencingFeatureGroup,
191 .saveElement(context, elementContext, zusammenElement, "add referencing feature group");
195 private ZusammenElement buildLicenseKeyGroupElement(LicenseKeyGroupEntity licenseKeyGroup,
197 ZusammenElement lkgElement =
198 buildElement(licenseKeyGroup.getId() == null ? null : new Id(licenseKeyGroup.getId()),
200 Info info = new Info();
201 info.setName(licenseKeyGroup.getName());
202 info.setDescription(licenseKeyGroup.getDescription());
203 info.addProperty(ElementPropertyName.elementType.name(), ElementType.LicenseKeyGroup);
204 info.addProperty("version_uuid", licenseKeyGroup.getVersionUuId());
205 info.addProperty("LicenseKeyType", licenseKeyGroup.getType());
206 info.addProperty("operational_scope", licenseKeyGroup.getOperationalScope());
207 info.addProperty("startDate", licenseKeyGroup.getStartDate());
208 info.addProperty("expiryDate", licenseKeyGroup.getExpiryDate());
209 info.addProperty("thresholdValue", licenseKeyGroup.getThresholdValue());
210 info.addProperty("thresholdUnits", licenseKeyGroup.getThresholdUnits());
211 info.addProperty("increments", licenseKeyGroup.getIncrements());
212 lkgElement.setInfo(info);
214 if (licenseKeyGroup.getReferencingFeatureGroups() != null
215 && licenseKeyGroup.getReferencingFeatureGroups().size() > 0) {
216 lkgElement.setRelations(licenseKeyGroup.getReferencingFeatureGroups().stream()
217 .map(rel -> VlmZusammenUtil
218 .createRelation(RelationType.LicenseKeyGroupToReferencingFeatureGroup, rel))
219 .collect(Collectors.toList()));