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.core.zusammen.api.ZusammenUtil;
13 import org.openecomp.sdc.vendorlicense.dao.LicenseKeyGroupDao;
14 import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity;
15 import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyType;
16 import org.openecomp.sdc.vendorlicense.dao.types.MultiChoiceOrOther;
17 import org.openecomp.sdc.vendorlicense.dao.types.OperationalScope;
18 import org.openecomp.sdc.vendorlicense.dao.types.ThresholdUnit;
19 import org.openecomp.sdc.versioning.dao.types.Version;
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.HashSet;
24 import java.util.List;
26 import java.util.Optional;
28 import java.util.stream.Collectors;
31 * Created by ayalaben on 3/30/2017.
33 public class LicenseKeyGroupZusammenDaoImpl implements LicenseKeyGroupDao {
34 private ZusammenAdaptor zusammenAdaptor;
36 public LicenseKeyGroupZusammenDaoImpl(ZusammenAdaptor zusammenAdaptor) {
37 this.zusammenAdaptor = zusammenAdaptor;
41 public void registerVersioning(String versionableEntityType) {
46 public void create(LicenseKeyGroupEntity licenseKeyGroup) {
47 ZusammenElement licenseKeyGroupElement =
48 buildLicenseKeyGroupElement(licenseKeyGroup, Action.CREATE);
50 ZusammenElement lkgsElement =
51 VlmZusammenUtil.buildStructuralElement(StructureElement.LicenseKeyGroups, null);
52 lkgsElement.addSubElement(licenseKeyGroupElement);
54 SessionContext context = ZusammenUtil.createSessionContext();
55 Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
56 Optional<Element> savedElement = zusammenAdaptor.saveElement(context, new ElementContext(itemId,
57 VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor)),
58 lkgsElement, "Create license Key Group");
60 savedElement.ifPresent(element -> licenseKeyGroup
61 .setId(element.getSubElements().iterator().next().getElementId().getValue()));
65 public void update(LicenseKeyGroupEntity licenseKeyGroup) {
66 ZusammenElement licenseKeyGroupElement =
67 buildLicenseKeyGroupElement(licenseKeyGroup, Action.UPDATE);
69 SessionContext context = ZusammenUtil.createSessionContext();
70 Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
72 ElementContext elementContext = new ElementContext(itemId,
73 VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor));
75 Optional<ElementInfo> lkgFromDb = zusammenAdaptor.getElementInfo(context, elementContext,
76 new Id(licenseKeyGroup.getId()));
78 if(lkgFromDb.isPresent()) {
80 if( licenseKeyGroupElement.getRelations() == null) {
81 licenseKeyGroupElement.setRelations(new ArrayList<>());
84 if (lkgFromDb.get().getRelations() != null && lkgFromDb.get().getRelations().size() > 0) {
85 licenseKeyGroupElement.getRelations().addAll(lkgFromDb.get().getRelations());
89 zusammenAdaptor.saveElement(context, elementContext,
90 licenseKeyGroupElement,
91 String.format("Update license key group with id %s", licenseKeyGroup.getId()));
95 public LicenseKeyGroupEntity get(LicenseKeyGroupEntity licenseKeyGroup) {
96 SessionContext context = ZusammenUtil.createSessionContext();
97 Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
98 ElementContext elementContext = new ElementContext(itemId,
99 VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor),
100 VlmZusammenUtil.getVersionTag(licenseKeyGroup.getVersion()));
102 return zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseKeyGroup.getId()))
103 .map(elementInfo -> mapElementInfoToLicenseKeyGroup(
104 licenseKeyGroup.getVendorLicenseModelId(), licenseKeyGroup.getVersion(), elementInfo))
109 public void delete(LicenseKeyGroupEntity licenseKeyGroup) {
110 SessionContext context = ZusammenUtil.createSessionContext();
111 ZusammenElement zusammenElement = new ZusammenElement();
112 zusammenElement.setAction(Action.DELETE);
113 zusammenElement.setElementId(new Id(licenseKeyGroup.getId()));
115 Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
116 ElementContext elementContext = new ElementContext(itemId,
117 VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor));
119 zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
120 "delete license key group. id:" + licenseKeyGroup.getId() + ".");
124 public Collection<LicenseKeyGroupEntity> list(LicenseKeyGroupEntity licenseKeyGroup) {
125 SessionContext context = ZusammenUtil.createSessionContext();
126 Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
127 ElementContext elementContext = new ElementContext(itemId,
128 VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor),
129 VlmZusammenUtil.getVersionTag(licenseKeyGroup.getVersion()));
131 return zusammenAdaptor
132 .listElementsByName(context, elementContext, null, StructureElement.LicenseKeyGroups.name())
133 .stream().map(elementInfo -> mapElementInfoToLicenseKeyGroup(
134 licenseKeyGroup.getVendorLicenseModelId(), licenseKeyGroup.getVersion(), elementInfo))
135 .collect(Collectors.toList());
139 public long count(LicenseKeyGroupEntity licenseKeyGroup) {
140 SessionContext context = ZusammenUtil.createSessionContext();
141 Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
142 ElementContext elementContext = new ElementContext(itemId,
143 VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor),
144 VlmZusammenUtil.getVersionTag(licenseKeyGroup.getVersion()));
146 return zusammenAdaptor
147 .listElementsByName(context, elementContext, null, StructureElement.LicenseKeyGroups.name())
152 public void deleteAll(LicenseKeyGroupEntity licenseKeyGroup) {
157 public void removeReferencingFeatureGroup(LicenseKeyGroupEntity licenseKeyGroup,
158 String featureGroupId) {
159 SessionContext context = ZusammenUtil.createSessionContext();
160 Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
161 ElementContext elementContext = new ElementContext(itemId,
162 VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor));
164 Optional<ElementInfo> elementInfo =
165 zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseKeyGroup.getId()));
167 if (elementInfo.isPresent()) {
168 ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
169 zusammenElement.setAction(Action.UPDATE);
170 zusammenElement.setRelations(elementInfo.get().getRelations().stream()
171 .filter(relation -> !featureGroupId
172 .equals(relation.getEdge2().getElementId().getValue()))
173 .collect(Collectors.toList()));
175 zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
176 "remove referencing feature group");
181 public void addReferencingFeatureGroup(LicenseKeyGroupEntity licenseKeyGroup,
182 String featureGroupId) {
183 SessionContext context = ZusammenUtil.createSessionContext();
184 Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
185 ElementContext elementContext = new ElementContext(itemId,
186 VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor));
188 Optional<ElementInfo> elementInfo =
189 zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseKeyGroup.getId()));
191 if (elementInfo.isPresent()) {
192 ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
193 zusammenElement.setAction(Action.UPDATE);
194 if (zusammenElement.getRelations() == null) {
195 zusammenElement.setRelations(new ArrayList<>());
197 zusammenElement.getRelations().add(VlmZusammenUtil
198 .createRelation(RelationType.LicenseKeyGroupToReferencingFeatureGroup,
201 .saveElement(context, elementContext, zusammenElement, "add referencing feature group");
205 private ZusammenElement buildLicenseKeyGroupElement(LicenseKeyGroupEntity licenseKeyGroup,
208 ZusammenElement lkgElement = new ZusammenElement();
209 lkgElement.setAction(action);
210 if (licenseKeyGroup.getId() != null) {
211 lkgElement.setElementId(new Id(licenseKeyGroup.getId()));
213 Info info = new Info();
214 info.setName(licenseKeyGroup.getName());
215 info.setDescription(licenseKeyGroup.getDescription());
216 info.addProperty("version_uuid", licenseKeyGroup.getVersionUuId());
217 info.addProperty("LicenseKeyType", licenseKeyGroup.getType());
218 info.addProperty("version_uuid", licenseKeyGroup.getVersionUuId());
219 info.addProperty("operational_scope", licenseKeyGroup.getOperationalScope());
220 info.addProperty("startDate", licenseKeyGroup.getStartDate());
221 info.addProperty("expiryDate", licenseKeyGroup.getExpiryDate());
222 info.addProperty("thresholdValue", licenseKeyGroup.getThresholdValue());
223 info.addProperty("thresholdUnits", licenseKeyGroup.getThresholdUnits());
224 info.addProperty("increments", licenseKeyGroup.getIncrements());
225 lkgElement.setInfo(info);
227 if (licenseKeyGroup.getReferencingFeatureGroups() != null
228 && licenseKeyGroup.getReferencingFeatureGroups().size() > 0) {
229 lkgElement.setRelations(licenseKeyGroup.getReferencingFeatureGroups().stream()
230 .map(rel -> VlmZusammenUtil
231 .createRelation(RelationType.LicenseKeyGroupToReferencingFeatureGroup, rel))
232 .collect(Collectors.toList()));
238 private LicenseKeyGroupEntity mapElementInfoToLicenseKeyGroup(String vlmId, Version version,
239 ElementInfo elementInfo) {
240 LicenseKeyGroupEntity licenseKeyGroup =
241 new LicenseKeyGroupEntity(vlmId, version, elementInfo.getId().getValue());
242 licenseKeyGroup.setName(elementInfo.getInfo().getName());
243 licenseKeyGroup.setDescription(elementInfo.getInfo().getDescription());
244 licenseKeyGroup.setVersionUuId(elementInfo.getInfo().getProperty("version_uuid"));
246 .setType(LicenseKeyType.valueOf(elementInfo.getInfo().getProperty("LicenseKeyType")));
247 licenseKeyGroup.setVersionUuId(elementInfo.getInfo().getProperty("version_uuid"));
248 licenseKeyGroup.setOperationalScope(getOperationalScopeMultiChoiceOrOther(
249 elementInfo.getInfo().getProperty("operational_scope")));
250 licenseKeyGroup.setStartDate(elementInfo.getInfo().getProperty("startDate"));
251 licenseKeyGroup.setExpiryDate(elementInfo.getInfo().getProperty("expiryDate"));
252 if (elementInfo.getInfo().getProperty("thresholdUnits") != null ){
253 licenseKeyGroup.setThresholdUnits(ThresholdUnit.valueOf(elementInfo
254 .getInfo().getProperty("thresholdUnits")));
256 if (elementInfo.getInfo().getProperty("thresholdValue") != null ){
257 licenseKeyGroup.setThresholdValue(toInteger(elementInfo.getInfo().getProperty
258 ("thresholdValue")));
260 licenseKeyGroup.setIncrements(elementInfo.getInfo().getProperty("increments"));
262 if (elementInfo.getRelations() != null && elementInfo.getRelations().size() > 0) {
264 .setReferencingFeatureGroups(elementInfo.getRelations().stream().map(relation -> relation
265 .getEdge2().getElementId().getValue()).collect(Collectors.toSet()));
267 return licenseKeyGroup;
270 private MultiChoiceOrOther<OperationalScope> getOperationalScopeMultiChoiceOrOther
273 if(operationalScope != null && !operationalScope.isEmpty()) {
274 Set<OperationalScope> choices = new HashSet<>();
275 ((List<String>) operationalScope.get("choices"))
276 .forEach(choice -> choices.add(OperationalScope.valueOf(choice)));
278 return new MultiChoiceOrOther<>(choices, operationalScope.get("other")==null?null:(String) operationalScope.get("other"));
283 private Integer toInteger(Object val) {
284 if (val instanceof Double) {
285 return ((Double) val).intValue();
286 } else if (val instanceof String) {
287 return new Integer((String) val);
288 } else if (val instanceof Integer) {
289 return (Integer) val;
291 throw new RuntimeException("invalid value for integer:" + val.getClass());