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 lkgFromDb.get().getRelations().forEach(relation ->
84 licenseKeyGroupElement.getRelations().add(relation));
87 zusammenAdaptor.saveElement(context, elementContext,
88 licenseKeyGroupElement,
89 String.format("Update license key group with id %s", licenseKeyGroup.getId()));
93 public LicenseKeyGroupEntity get(LicenseKeyGroupEntity licenseKeyGroup) {
94 SessionContext context = ZusammenUtil.createSessionContext();
95 Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
96 ElementContext elementContext = new ElementContext(itemId,
97 VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor),
98 VlmZusammenUtil.getVersionTag(licenseKeyGroup.getVersion()));
100 return zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseKeyGroup.getId()))
101 .map(elementInfo -> mapElementInfoToLicenseKeyGroup(
102 licenseKeyGroup.getVendorLicenseModelId(), licenseKeyGroup.getVersion(), elementInfo))
107 public void delete(LicenseKeyGroupEntity licenseKeyGroup) {
108 SessionContext context = ZusammenUtil.createSessionContext();
109 ZusammenElement zusammenElement = new ZusammenElement();
110 zusammenElement.setAction(Action.DELETE);
111 zusammenElement.setElementId(new Id(licenseKeyGroup.getId()));
113 Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
114 ElementContext elementContext = new ElementContext(itemId,
115 VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor));
117 zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
118 "delete license key group. id:" + licenseKeyGroup.getId() + ".");
122 public Collection<LicenseKeyGroupEntity> list(LicenseKeyGroupEntity licenseKeyGroup) {
123 SessionContext context = ZusammenUtil.createSessionContext();
124 Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
125 ElementContext elementContext = new ElementContext(itemId,
126 VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor),
127 VlmZusammenUtil.getVersionTag(licenseKeyGroup.getVersion()));
129 return zusammenAdaptor
130 .listElementsByName(context, elementContext, null, StructureElement.LicenseKeyGroups.name())
131 .stream().map(elementInfo -> mapElementInfoToLicenseKeyGroup(
132 licenseKeyGroup.getVendorLicenseModelId(), licenseKeyGroup.getVersion(), elementInfo))
133 .collect(Collectors.toList());
137 public long count(LicenseKeyGroupEntity licenseKeyGroup) {
138 SessionContext context = ZusammenUtil.createSessionContext();
139 Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
140 ElementContext elementContext = new ElementContext(itemId,
141 VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor),
142 VlmZusammenUtil.getVersionTag(licenseKeyGroup.getVersion()));
144 return zusammenAdaptor
145 .listElementsByName(context, elementContext, null, StructureElement.LicenseKeyGroups.name())
150 public void deleteAll(LicenseKeyGroupEntity licenseKeyGroup) {
155 public void removeReferencingFeatureGroup(LicenseKeyGroupEntity licenseKeyGroup,
156 String featureGroupId) {
157 SessionContext context = ZusammenUtil.createSessionContext();
158 Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
159 ElementContext elementContext = new ElementContext(itemId,
160 VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor));
162 Optional<ElementInfo> elementInfo =
163 zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseKeyGroup.getId()));
165 if (elementInfo.isPresent()) {
166 ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
167 zusammenElement.setAction(Action.UPDATE);
168 zusammenElement.setRelations(elementInfo.get().getRelations().stream()
169 .filter(relation -> !featureGroupId
170 .equals(relation.getEdge2().getElementId().getValue()))
171 .collect(Collectors.toList()));
173 zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
174 "remove referencing feature group");
179 public void addReferencingFeatureGroup(LicenseKeyGroupEntity licenseKeyGroup,
180 String featureGroupId) {
181 SessionContext context = ZusammenUtil.createSessionContext();
182 Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
183 ElementContext elementContext = new ElementContext(itemId,
184 VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor));
186 Optional<ElementInfo> elementInfo =
187 zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseKeyGroup.getId()));
189 if (elementInfo.isPresent()) {
190 ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
191 zusammenElement.setAction(Action.UPDATE);
192 if (zusammenElement.getRelations() == null) {
193 zusammenElement.setRelations(new ArrayList<>());
195 zusammenElement.getRelations().add(VlmZusammenUtil
196 .createRelation(RelationType.LicenseKeyGroupToReferencingFeatureGroup,
199 .saveElement(context, elementContext, zusammenElement, "add referencing feature group");
203 private ZusammenElement buildLicenseKeyGroupElement(LicenseKeyGroupEntity licenseKeyGroup,
205 ZusammenElement lkgElement = new ZusammenElement();
206 lkgElement.setAction(action);
207 if (licenseKeyGroup.getId() != null) {
208 lkgElement.setElementId(new Id(licenseKeyGroup.getId()));
210 Info info = new Info();
211 info.setName(licenseKeyGroup.getName());
212 info.setDescription(licenseKeyGroup.getDescription());
213 info.addProperty("LicenseKeyType", licenseKeyGroup.getType());
214 info.addProperty("operational_scope", licenseKeyGroup.getOperationalScope());
215 lkgElement.setInfo(info);
217 if (licenseKeyGroup.getReferencingFeatureGroups() != null &&
218 licenseKeyGroup.getReferencingFeatureGroups().size() > 0) {
219 lkgElement.setRelations(licenseKeyGroup.getReferencingFeatureGroups().stream()
220 .map(rel -> VlmZusammenUtil
221 .createRelation(RelationType.LicenseKeyGroupToReferencingFeatureGroup, rel))
222 .collect(Collectors.toList()));
227 private LicenseKeyGroupEntity mapElementInfoToLicenseKeyGroup(String vlmId, Version version,
228 ElementInfo elementInfo) {
229 LicenseKeyGroupEntity licenseKeyGroup =
230 new LicenseKeyGroupEntity(vlmId, version, elementInfo.getId().getValue());
231 licenseKeyGroup.setName(elementInfo.getInfo().getName());
232 licenseKeyGroup.setDescription(elementInfo.getInfo().getDescription());
235 .setType(LicenseKeyType.valueOf(elementInfo.getInfo().getProperty("LicenseKeyType")));
236 licenseKeyGroup.setOperationalScope(getOperationalScopeMultiChoiceOrOther(
237 elementInfo.getInfo().getProperty("operational_scope")));
239 if (elementInfo.getRelations() != null && elementInfo.getRelations().size() > 0) {
241 .setReferencingFeatureGroups(elementInfo.getRelations().stream().map(relation -> relation
242 .getEdge2().getElementId().getValue()).collect(Collectors.toSet()));
244 return licenseKeyGroup;
247 private MultiChoiceOrOther<OperationalScope> getOperationalScopeMultiChoiceOrOther
251 Set<OperationalScope> choices = new HashSet<>();
252 ((List<String>) operationalScope.get("choices")).
253 forEach(choice -> choices.add(OperationalScope.valueOf(choice)));
255 return new MultiChoiceOrOther<>(choices, (String) operationalScope.get("other"));