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.versioning.dao.types.Version;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.HashSet;
23 import java.util.List;
25 import java.util.Optional;
27 import java.util.stream.Collectors;
30 * Created by ayalaben on 3/30/2017.
32 public class LicenseKeyGroupZusammenDaoImpl implements LicenseKeyGroupDao {
33 private ZusammenAdaptor zusammenAdaptor;
35 public LicenseKeyGroupZusammenDaoImpl(ZusammenAdaptor zusammenAdaptor) {
36 this.zusammenAdaptor = zusammenAdaptor;
40 public void registerVersioning(String versionableEntityType) {
45 public void create(LicenseKeyGroupEntity licenseKeyGroup) {
46 ZusammenElement licenseKeyGroupElement =
47 buildLicenseKeyGroupElement(licenseKeyGroup, Action.CREATE);
49 ZusammenElement lkgsElement =
50 VlmZusammenUtil.buildStructuralElement(StructureElement.LicenseKeyGroups, null);
51 lkgsElement.addSubElement(licenseKeyGroupElement);
53 SessionContext context = ZusammenUtil.createSessionContext();
54 Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
55 Optional<Element> savedElement = zusammenAdaptor.saveElement(context, new ElementContext(itemId,
56 VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor)),
57 lkgsElement, "Create license Key Group");
59 savedElement.ifPresent(element -> licenseKeyGroup
60 .setId(element.getSubElements().iterator().next().getElementId().getValue()));
64 public void update(LicenseKeyGroupEntity licenseKeyGroup) {
65 ZusammenElement licenseKeyGroupElement =
66 buildLicenseKeyGroupElement(licenseKeyGroup, Action.UPDATE);
68 SessionContext context = ZusammenUtil.createSessionContext();
69 Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
71 ElementContext elementContext = new ElementContext(itemId,
72 VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor));
74 Optional<ElementInfo> lkgFromDb = zusammenAdaptor.getElementInfo(context, elementContext,
75 new Id(licenseKeyGroup.getId()));
77 if(lkgFromDb.isPresent()) {
79 if( licenseKeyGroupElement.getRelations() == null) {
80 licenseKeyGroupElement.setRelations(new ArrayList<>());
83 if (lkgFromDb.get().getRelations() != null && lkgFromDb.get().getRelations().size() > 0) {
84 licenseKeyGroupElement.getRelations().addAll(lkgFromDb.get().getRelations());
88 zusammenAdaptor.saveElement(context, elementContext,
89 licenseKeyGroupElement,
90 String.format("Update license key group with id %s", licenseKeyGroup.getId()));
94 public LicenseKeyGroupEntity get(LicenseKeyGroupEntity licenseKeyGroup) {
95 SessionContext context = ZusammenUtil.createSessionContext();
96 Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
97 ElementContext elementContext = new ElementContext(itemId,
98 VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor),
99 VlmZusammenUtil.getVersionTag(licenseKeyGroup.getVersion()));
101 return zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseKeyGroup.getId()))
102 .map(elementInfo -> mapElementInfoToLicenseKeyGroup(
103 licenseKeyGroup.getVendorLicenseModelId(), licenseKeyGroup.getVersion(), elementInfo))
108 public void delete(LicenseKeyGroupEntity licenseKeyGroup) {
109 SessionContext context = ZusammenUtil.createSessionContext();
110 ZusammenElement zusammenElement = new ZusammenElement();
111 zusammenElement.setAction(Action.DELETE);
112 zusammenElement.setElementId(new Id(licenseKeyGroup.getId()));
114 Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
115 ElementContext elementContext = new ElementContext(itemId,
116 VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor));
118 zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
119 "delete license key group. id:" + licenseKeyGroup.getId() + ".");
123 public Collection<LicenseKeyGroupEntity> list(LicenseKeyGroupEntity licenseKeyGroup) {
124 SessionContext context = ZusammenUtil.createSessionContext();
125 Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
126 ElementContext elementContext = new ElementContext(itemId,
127 VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor),
128 VlmZusammenUtil.getVersionTag(licenseKeyGroup.getVersion()));
130 return zusammenAdaptor
131 .listElementsByName(context, elementContext, null, StructureElement.LicenseKeyGroups.name())
132 .stream().map(elementInfo -> mapElementInfoToLicenseKeyGroup(
133 licenseKeyGroup.getVendorLicenseModelId(), licenseKeyGroup.getVersion(), elementInfo))
134 .collect(Collectors.toList());
138 public long count(LicenseKeyGroupEntity licenseKeyGroup) {
139 SessionContext context = ZusammenUtil.createSessionContext();
140 Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
141 ElementContext elementContext = new ElementContext(itemId,
142 VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor),
143 VlmZusammenUtil.getVersionTag(licenseKeyGroup.getVersion()));
145 return zusammenAdaptor
146 .listElementsByName(context, elementContext, null, StructureElement.LicenseKeyGroups.name())
151 public void deleteAll(LicenseKeyGroupEntity licenseKeyGroup) {
156 public void removeReferencingFeatureGroup(LicenseKeyGroupEntity licenseKeyGroup,
157 String featureGroupId) {
158 SessionContext context = ZusammenUtil.createSessionContext();
159 Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
160 ElementContext elementContext = new ElementContext(itemId,
161 VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor));
163 Optional<ElementInfo> elementInfo =
164 zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseKeyGroup.getId()));
166 if (elementInfo.isPresent()) {
167 ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
168 zusammenElement.setAction(Action.UPDATE);
169 zusammenElement.setRelations(elementInfo.get().getRelations().stream()
170 .filter(relation -> !featureGroupId
171 .equals(relation.getEdge2().getElementId().getValue()))
172 .collect(Collectors.toList()));
174 zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
175 "remove referencing feature group");
180 public void addReferencingFeatureGroup(LicenseKeyGroupEntity licenseKeyGroup,
181 String featureGroupId) {
182 SessionContext context = ZusammenUtil.createSessionContext();
183 Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
184 ElementContext elementContext = new ElementContext(itemId,
185 VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor));
187 Optional<ElementInfo> elementInfo =
188 zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseKeyGroup.getId()));
190 if (elementInfo.isPresent()) {
191 ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
192 zusammenElement.setAction(Action.UPDATE);
193 if (zusammenElement.getRelations() == null) {
194 zusammenElement.setRelations(new ArrayList<>());
196 zusammenElement.getRelations().add(VlmZusammenUtil
197 .createRelation(RelationType.LicenseKeyGroupToReferencingFeatureGroup,
200 .saveElement(context, elementContext, zusammenElement, "add referencing feature group");
204 private ZusammenElement buildLicenseKeyGroupElement(LicenseKeyGroupEntity licenseKeyGroup,
207 ZusammenElement lkgElement = new ZusammenElement();
208 lkgElement.setAction(action);
209 if (licenseKeyGroup.getId() != null) {
210 lkgElement.setElementId(new Id(licenseKeyGroup.getId()));
212 Info info = new Info();
213 info.setName(licenseKeyGroup.getName());
214 info.setDescription(licenseKeyGroup.getDescription());
215 info.addProperty("LicenseKeyType", licenseKeyGroup.getType());
216 info.addProperty("operational_scope", licenseKeyGroup.getOperationalScope());
217 lkgElement.setInfo(info);
219 if (licenseKeyGroup.getReferencingFeatureGroups() != null
220 && licenseKeyGroup.getReferencingFeatureGroups().size() > 0) {
221 lkgElement.setRelations(licenseKeyGroup.getReferencingFeatureGroups().stream()
222 .map(rel -> VlmZusammenUtil
223 .createRelation(RelationType.LicenseKeyGroupToReferencingFeatureGroup, rel))
224 .collect(Collectors.toList()));
230 private LicenseKeyGroupEntity mapElementInfoToLicenseKeyGroup(String vlmId, Version version,
231 ElementInfo elementInfo) {
232 LicenseKeyGroupEntity licenseKeyGroup =
233 new LicenseKeyGroupEntity(vlmId, version, elementInfo.getId().getValue());
234 licenseKeyGroup.setName(elementInfo.getInfo().getName());
235 licenseKeyGroup.setDescription(elementInfo.getInfo().getDescription());
238 .setType(LicenseKeyType.valueOf(elementInfo.getInfo().getProperty("LicenseKeyType")));
239 licenseKeyGroup.setOperationalScope(getOperationalScopeMultiChoiceOrOther(
240 elementInfo.getInfo().getProperty("operational_scope")));
242 if (elementInfo.getRelations() != null && elementInfo.getRelations().size() > 0) {
244 .setReferencingFeatureGroups(elementInfo.getRelations().stream().map(relation -> relation
245 .getEdge2().getElementId().getValue()).collect(Collectors.toSet()));
247 return licenseKeyGroup;
250 private MultiChoiceOrOther<OperationalScope> getOperationalScopeMultiChoiceOrOther
253 if(operationalScope != null && !operationalScope.isEmpty()) {
254 Set<OperationalScope> choices = new HashSet<>();
255 ((List<String>) operationalScope.get("choices"))
256 .forEach(choice -> choices.add(OperationalScope.valueOf(choice)));
258 return new MultiChoiceOrOther<>(choices, operationalScope.get("other")==null?null:(String) operationalScope.get("other"));