976db94f398fcbb4d3a5d38e3b2540a894abc456
[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.vendorlicense.dao.types.ThresholdUnit;
19 import org.openecomp.sdc.versioning.dao.types.Version;
20
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.HashSet;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Optional;
27 import java.util.Set;
28 import java.util.stream.Collectors;
29
30 /**
31  * Created by ayalaben on 3/30/2017.
32  */
33 public class LicenseKeyGroupZusammenDaoImpl implements LicenseKeyGroupDao {
34   private ZusammenAdaptor zusammenAdaptor;
35
36   public LicenseKeyGroupZusammenDaoImpl(ZusammenAdaptor zusammenAdaptor) {
37     this.zusammenAdaptor = zusammenAdaptor;
38   }
39
40   @Override
41   public void registerVersioning(String versionableEntityType) {
42     //no need
43   }
44
45   @Override
46   public void create(LicenseKeyGroupEntity licenseKeyGroup) {
47     ZusammenElement licenseKeyGroupElement =
48         buildLicenseKeyGroupElement(licenseKeyGroup, Action.CREATE);
49
50     ZusammenElement lkgsElement =
51         VlmZusammenUtil.buildStructuralElement(StructureElement.LicenseKeyGroups, null);
52     lkgsElement.addSubElement(licenseKeyGroupElement);
53
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");
59
60     savedElement.ifPresent(element -> licenseKeyGroup
61         .setId(element.getSubElements().iterator().next().getElementId().getValue()));
62   }
63
64   @Override
65   public void update(LicenseKeyGroupEntity licenseKeyGroup) {
66     ZusammenElement licenseKeyGroupElement =
67         buildLicenseKeyGroupElement(licenseKeyGroup, Action.UPDATE);
68
69     SessionContext context = ZusammenUtil.createSessionContext();
70     Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
71
72     ElementContext elementContext = new ElementContext(itemId,
73         VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor));
74
75     Optional<ElementInfo> lkgFromDb = zusammenAdaptor.getElementInfo(context, elementContext,
76         new Id(licenseKeyGroup.getId()));
77
78     if(lkgFromDb.isPresent()) {
79
80       if( licenseKeyGroupElement.getRelations() == null) {
81          licenseKeyGroupElement.setRelations(new ArrayList<>());
82       }
83
84       if (lkgFromDb.get().getRelations() != null && lkgFromDb.get().getRelations().size() > 0) {
85         licenseKeyGroupElement.getRelations().addAll(lkgFromDb.get().getRelations());
86       }
87     }
88
89     zusammenAdaptor.saveElement(context, elementContext,
90         licenseKeyGroupElement,
91         String.format("Update license key group with id %s", licenseKeyGroup.getId()));
92   }
93
94   @Override
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()));
101
102     return zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseKeyGroup.getId()))
103         .map(elementInfo -> mapElementInfoToLicenseKeyGroup(
104             licenseKeyGroup.getVendorLicenseModelId(), licenseKeyGroup.getVersion(), elementInfo))
105         .orElse(null);
106   }
107
108   @Override
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()));
114
115     Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
116     ElementContext elementContext = new ElementContext(itemId,
117         VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor));
118
119     zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
120         "delete license key group. id:" + licenseKeyGroup.getId() + ".");
121   }
122
123   @Override
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()));
130
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());
136   }
137
138   @Override
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()));
145
146     return zusammenAdaptor
147         .listElementsByName(context, elementContext, null, StructureElement.LicenseKeyGroups.name())
148         .size();
149   }
150
151   @Override
152   public void deleteAll(LicenseKeyGroupEntity licenseKeyGroup) {
153     //not supported
154   }
155
156   @Override
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));
163
164     Optional<ElementInfo> elementInfo =
165         zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseKeyGroup.getId()));
166
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()));
174
175       zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
176           "remove referencing feature group");
177     }
178   }
179
180   @Override
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));
187
188     Optional<ElementInfo> elementInfo =
189         zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseKeyGroup.getId()));
190
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<>());
196       }
197       zusammenElement.getRelations().add(VlmZusammenUtil
198           .createRelation(RelationType.LicenseKeyGroupToReferencingFeatureGroup,
199               featureGroupId));
200       zusammenAdaptor
201           .saveElement(context, elementContext, zusammenElement, "add referencing feature group");
202     }
203   }
204
205   private ZusammenElement buildLicenseKeyGroupElement(LicenseKeyGroupEntity licenseKeyGroup,
206                                                       Action action) {
207
208     ZusammenElement lkgElement = new ZusammenElement();
209     lkgElement.setAction(action);
210     if (licenseKeyGroup.getId() != null) {
211       lkgElement.setElementId(new Id(licenseKeyGroup.getId()));
212     }
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);
226
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()));
233     }
234
235     return lkgElement;
236   }
237
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"));
245     licenseKeyGroup
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")));
255     }
256     if (elementInfo.getInfo().getProperty("thresholdValue") != null ){
257       licenseKeyGroup.setThresholdValue(toInteger(elementInfo.getInfo().getProperty
258           ("thresholdValue")));
259     }
260     licenseKeyGroup.setIncrements(elementInfo.getInfo().getProperty("increments"));
261
262     if (elementInfo.getRelations() != null && elementInfo.getRelations().size() > 0) {
263       licenseKeyGroup
264           .setReferencingFeatureGroups(elementInfo.getRelations().stream().map(relation -> relation
265               .getEdge2().getElementId().getValue()).collect(Collectors.toSet()));
266     }
267     return licenseKeyGroup;
268   }
269
270   private MultiChoiceOrOther<OperationalScope> getOperationalScopeMultiChoiceOrOther
271       (Map<String, Object>
272            operationalScope) {
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)));
277
278     return new MultiChoiceOrOther<>(choices, operationalScope.get("other")==null?null:(String) operationalScope.get("other"));
279   }
280   return null;
281   }
282
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;
290     }
291     throw new RuntimeException("invalid value for integer:" + val.getClass());
292   }
293 }