f5d905cf3537c7231d8b23d78106da467b8dfc7f
[sdc.git] /
1 package org.openecomp.sdc.vendorlicense.dao.impl.zusammen;
2
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;
19
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Optional;
26 import java.util.Set;
27 import java.util.stream.Collectors;
28
29 /**
30  * Created by ayalaben on 3/30/2017.
31  */
32 public class LicenseKeyGroupZusammenDaoImpl implements LicenseKeyGroupDao {
33   private ZusammenAdaptor zusammenAdaptor;
34
35   public LicenseKeyGroupZusammenDaoImpl(ZusammenAdaptor zusammenAdaptor) {
36     this.zusammenAdaptor = zusammenAdaptor;
37   }
38
39   @Override
40   public void registerVersioning(String versionableEntityType) {
41     //no need
42   }
43
44   @Override
45   public void create(LicenseKeyGroupEntity licenseKeyGroup) {
46     ZusammenElement licenseKeyGroupElement =
47         buildLicenseKeyGroupElement(licenseKeyGroup, Action.CREATE);
48
49     ZusammenElement lkgsElement =
50         VlmZusammenUtil.buildStructuralElement(StructureElement.LicenseKeyGroups, null);
51     lkgsElement.addSubElement(licenseKeyGroupElement);
52
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");
58
59     savedElement.ifPresent(element -> licenseKeyGroup
60         .setId(element.getSubElements().iterator().next().getElementId().getValue()));
61   }
62
63   @Override
64   public void update(LicenseKeyGroupEntity licenseKeyGroup) {
65     ZusammenElement licenseKeyGroupElement =
66         buildLicenseKeyGroupElement(licenseKeyGroup, Action.UPDATE);
67
68     SessionContext context = ZusammenUtil.createSessionContext();
69     Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
70
71     ElementContext elementContext = new ElementContext(itemId,
72         VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor));
73
74     Optional<ElementInfo> lkgFromDb = zusammenAdaptor.getElementInfo(context, elementContext,
75         new Id(licenseKeyGroup.getId()));
76
77     if (lkgFromDb.isPresent()) {
78
79       if (licenseKeyGroupElement.getRelations() == null) {
80         licenseKeyGroupElement.setRelations(new ArrayList<>());
81       }
82
83       if (lkgFromDb.get().getRelations() != null) {
84         licenseKeyGroupElement.getRelations().addAll(lkgFromDb.get().getRelations());
85       }
86     }
87
88     zusammenAdaptor.saveElement(context, elementContext,
89         licenseKeyGroupElement,
90         String.format("Update license key group with id %s", licenseKeyGroup.getId()));
91   }
92
93   @Override
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()));
100
101     return zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseKeyGroup.getId()))
102         .map(elementInfo -> mapElementInfoToLicenseKeyGroup(
103             licenseKeyGroup.getVendorLicenseModelId(), licenseKeyGroup.getVersion(), elementInfo))
104         .orElse(null);
105   }
106
107   @Override
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()));
113
114     Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
115     ElementContext elementContext = new ElementContext(itemId,
116         VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor));
117
118     zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
119         "delete license key group. id:" + licenseKeyGroup.getId() + ".");
120   }
121
122   @Override
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()));
129
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());
135   }
136
137   @Override
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()));
144
145     return zusammenAdaptor
146         .listElementsByName(context, elementContext, null, StructureElement.LicenseKeyGroups.name())
147         .size();
148   }
149
150   @Override
151   public void deleteAll(LicenseKeyGroupEntity licenseKeyGroup) {
152     //not supported
153   }
154
155   @Override
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));
162
163     Optional<ElementInfo> elementInfo =
164         zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseKeyGroup.getId()));
165
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()));
173
174       zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
175           "remove referencing feature group");
176     }
177   }
178
179   @Override
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));
186
187     Optional<ElementInfo> elementInfo =
188         zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseKeyGroup.getId()));
189
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<>());
195       }
196       zusammenElement.getRelations().add(VlmZusammenUtil
197           .createRelation(RelationType.LicenseKeyGroupToReferencingFeatureGroup,
198               featureGroupId));
199       zusammenAdaptor
200           .saveElement(context, elementContext, zusammenElement, "add referencing feature group");
201     }
202   }
203
204   private ZusammenElement buildLicenseKeyGroupElement(LicenseKeyGroupEntity licenseKeyGroup,
205                                                       Action action) {
206     ZusammenElement lkgElement = new ZusammenElement();
207     lkgElement.setAction(action);
208     if (licenseKeyGroup.getId() != null) {
209       lkgElement.setElementId(new Id(licenseKeyGroup.getId()));
210     }
211     Info info = new Info();
212     info.setName(licenseKeyGroup.getName());
213     info.setDescription(licenseKeyGroup.getDescription());
214     info.addProperty("LicenseKeyType", licenseKeyGroup.getType());
215     info.addProperty("operational_scope", licenseKeyGroup.getOperationalScope());
216     lkgElement.setInfo(info);
217
218     if (licenseKeyGroup.getReferencingFeatureGroups() != null &&
219         licenseKeyGroup.getReferencingFeatureGroups().size() > 0) {
220       lkgElement.setRelations(licenseKeyGroup.getReferencingFeatureGroups().stream()
221           .map(rel -> VlmZusammenUtil
222               .createRelation(RelationType.LicenseKeyGroupToReferencingFeatureGroup, rel))
223           .collect(Collectors.toList()));
224     }
225     return lkgElement;
226   }
227
228   private LicenseKeyGroupEntity mapElementInfoToLicenseKeyGroup(String vlmId, Version version,
229                                                                 ElementInfo elementInfo) {
230     LicenseKeyGroupEntity licenseKeyGroup =
231         new LicenseKeyGroupEntity(vlmId, version, elementInfo.getId().getValue());
232     licenseKeyGroup.setName(elementInfo.getInfo().getName());
233     licenseKeyGroup.setDescription(elementInfo.getInfo().getDescription());
234
235     licenseKeyGroup
236         .setType(LicenseKeyType.valueOf(elementInfo.getInfo().getProperty("LicenseKeyType")));
237     licenseKeyGroup.setOperationalScope(getOperationalScopeMultiChoiceOrOther(
238         elementInfo.getInfo().getProperty("operational_scope")));
239
240     if (elementInfo.getRelations() != null && elementInfo.getRelations().size() > 0) {
241       licenseKeyGroup
242           .setReferencingFeatureGroups(elementInfo.getRelations().stream().map(relation -> relation
243               .getEdge2().getElementId().getValue()).collect(Collectors.toSet()));
244     }
245     return licenseKeyGroup;
246   }
247
248   private MultiChoiceOrOther<OperationalScope> getOperationalScopeMultiChoiceOrOther
249       (Map<String, Object>
250            operationalScope) {
251
252     Set<OperationalScope> choices = new HashSet<>();
253     ((List<String>) operationalScope.get("choices")).
254         forEach(choice -> choices.add(OperationalScope.valueOf(choice)));
255
256     return new MultiChoiceOrOther<>(choices, (String) operationalScope.get("other"));
257   }
258
259
260 }