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 locenseKeyGroupElement =
66 buildLicenseKeyGroupElement(licenseKeyGroup, Action.UPDATE);
68 SessionContext context = ZusammenUtil.createSessionContext();
69 Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
70 zusammenAdaptor.saveElement(context, new ElementContext(itemId,
71 VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor)),
72 locenseKeyGroupElement,
73 String.format("Update license key group with id %s", licenseKeyGroup.getId()));
77 public LicenseKeyGroupEntity get(LicenseKeyGroupEntity licenseKeyGroup) {
78 SessionContext context = ZusammenUtil.createSessionContext();
79 Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
80 ElementContext elementContext = new ElementContext(itemId,
81 VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor),
82 VlmZusammenUtil.getVersionTag(licenseKeyGroup.getVersion()));
84 return zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseKeyGroup.getId()))
85 .map(elementInfo -> mapElementInfoToLicenseKeyGroup(
86 licenseKeyGroup.getVendorLicenseModelId(), licenseKeyGroup.getVersion(), elementInfo))
91 public void delete(LicenseKeyGroupEntity licenseKeyGroup) {
92 SessionContext context = ZusammenUtil.createSessionContext();
93 ZusammenElement zusammenElement = new ZusammenElement();
94 zusammenElement.setAction(Action.DELETE);
95 zusammenElement.setElementId(new Id(licenseKeyGroup.getId()));
97 Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
98 ElementContext elementContext = new ElementContext(itemId,
99 VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor));
101 zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
102 "delete license key group. id:" + licenseKeyGroup.getId() + ".");
106 public Collection<LicenseKeyGroupEntity> list(LicenseKeyGroupEntity licenseKeyGroup) {
107 SessionContext context = ZusammenUtil.createSessionContext();
108 Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
109 ElementContext elementContext = new ElementContext(itemId,
110 VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor),
111 VlmZusammenUtil.getVersionTag(licenseKeyGroup.getVersion()));
113 return zusammenAdaptor
114 .listElementsByName(context, elementContext, null, StructureElement.LicenseKeyGroups.name())
115 .stream().map(elementInfo -> mapElementInfoToLicenseKeyGroup(
116 licenseKeyGroup.getVendorLicenseModelId(), licenseKeyGroup.getVersion(), elementInfo))
117 .collect(Collectors.toList());
121 public long count(LicenseKeyGroupEntity licenseKeyGroup) {
122 SessionContext context = ZusammenUtil.createSessionContext();
123 Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
124 ElementContext elementContext = new ElementContext(itemId,
125 VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor),
126 VlmZusammenUtil.getVersionTag(licenseKeyGroup.getVersion()));
128 return zusammenAdaptor
129 .listElementsByName(context, elementContext, null, StructureElement.LicenseKeyGroups.name())
134 public void deleteAll(LicenseKeyGroupEntity licenseKeyGroup) {
139 public void removeReferencingFeatureGroup(LicenseKeyGroupEntity licenseKeyGroup,
140 String featureGroupId) {
141 SessionContext context = ZusammenUtil.createSessionContext();
142 Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
143 ElementContext elementContext = new ElementContext(itemId,
144 VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor));
146 Optional<ElementInfo> elementInfo =
147 zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseKeyGroup.getId()));
149 if (elementInfo.isPresent()) {
150 ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
151 zusammenElement.setAction(Action.UPDATE);
152 zusammenElement.setRelations(elementInfo.get().getRelations().stream()
153 .filter(relation -> !featureGroupId
154 .equals(relation.getEdge2().getElementId().getValue()))
155 .collect(Collectors.toList()));
157 zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
158 "remove referencing feature group");
163 public void addReferencingFeatureGroup(LicenseKeyGroupEntity licenseKeyGroup,
164 String featureGroupId) {
165 SessionContext context = ZusammenUtil.createSessionContext();
166 Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
167 ElementContext elementContext = new ElementContext(itemId,
168 VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor));
170 Optional<ElementInfo> elementInfo =
171 zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseKeyGroup.getId()));
173 if (elementInfo.isPresent()) {
174 ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
175 zusammenElement.setAction(Action.UPDATE);
176 if (zusammenElement.getRelations() == null) {
177 zusammenElement.setRelations(new ArrayList<>());
179 zusammenElement.getRelations().add(VlmZusammenUtil
180 .createRelation(RelationType.LicenseKeyGroupToReferencingFeatureGroup,
183 .saveElement(context, elementContext, zusammenElement, "add referencing feature group");
187 private ZusammenElement buildLicenseKeyGroupElement(LicenseKeyGroupEntity licenseKeyGroup,
189 ZusammenElement lkgElement = new ZusammenElement();
190 lkgElement.setAction(action);
191 if (licenseKeyGroup.getId() != null) {
192 lkgElement.setElementId(new Id(licenseKeyGroup.getId()));
194 Info info = new Info();
195 info.setName(licenseKeyGroup.getName());
196 info.setDescription(licenseKeyGroup.getDescription());
197 info.addProperty("LicenseKeyType", licenseKeyGroup.getType());
198 info.addProperty("operational_scope", licenseKeyGroup.getOperationalScope());
199 lkgElement.setInfo(info);
201 if (licenseKeyGroup.getReferencingFeatureGroups() != null &&
202 licenseKeyGroup.getReferencingFeatureGroups().size() > 0) {
203 lkgElement.setRelations(licenseKeyGroup.getReferencingFeatureGroups().stream()
204 .map(rel -> VlmZusammenUtil
205 .createRelation(RelationType.LicenseKeyGroupToReferencingFeatureGroup, rel))
206 .collect(Collectors.toList()));
211 private LicenseKeyGroupEntity mapElementInfoToLicenseKeyGroup(String vlmId, Version version,
212 ElementInfo elementInfo) {
213 LicenseKeyGroupEntity licenseKeyGroup =
214 new LicenseKeyGroupEntity(vlmId, version, elementInfo.getId().getValue());
215 licenseKeyGroup.setName(elementInfo.getInfo().getName());
216 licenseKeyGroup.setDescription(elementInfo.getInfo().getDescription());
219 .setType(LicenseKeyType.valueOf(elementInfo.getInfo().getProperty("LicenseKeyType")));
220 licenseKeyGroup.setOperationalScope(getOperationalScopeMultiChoiceOrOther(
221 elementInfo.getInfo().getProperty("operational_scope")));
223 if (elementInfo.getRelations() != null && elementInfo.getRelations().size() > 0) {
225 .setReferencingFeatureGroups(elementInfo.getRelations().stream().map(relation -> relation
226 .getEdge2().getElementId().getValue()).collect(Collectors.toSet()));
228 return licenseKeyGroup;
231 private MultiChoiceOrOther<OperationalScope> getOperationalScopeMultiChoiceOrOther
235 Set<OperationalScope> choices = new HashSet<>();
236 ((List<String>) operationalScope.get("choices")).
237 forEach(choice -> choices.add(OperationalScope.valueOf(choice)));
239 return new MultiChoiceOrOther<>(choices, (String) operationalScope.get("other"));