a31210a81ad538af3b4479e5d47e5859a39352fa
[sdc.git] / openecomp-be / lib / openecomp-sdc-vendor-license-lib / openecomp-sdc-vendor-license-core / src / main / java / org / openecomp / sdc / vendorlicense / dao / impl / zusammen / EntitlementPoolZusammenDaoImpl.java
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.EntitlementPoolDao;
14 import org.openecomp.sdc.vendorlicense.dao.types.AggregationFunction;
15 import org.openecomp.sdc.vendorlicense.dao.types.ChoiceOrOther;
16 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementMetric;
17 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
18 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementTime;
19 import org.openecomp.sdc.vendorlicense.dao.types.MultiChoiceOrOther;
20 import org.openecomp.sdc.vendorlicense.dao.types.OperationalScope;
21 import org.openecomp.sdc.vendorlicense.dao.types.ThresholdUnit;
22 import org.openecomp.sdc.versioning.dao.types.Version;
23
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.HashSet;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Optional;
30 import java.util.Set;
31 import java.util.stream.Collectors;
32
33 /**
34  * Created by ayalaben on 3/28/2017.
35  */
36 public class EntitlementPoolZusammenDaoImpl implements EntitlementPoolDao {
37
38   private ZusammenAdaptor zusammenAdaptor;
39
40   public EntitlementPoolZusammenDaoImpl(ZusammenAdaptor zusammenAdaptor) {
41     this.zusammenAdaptor = zusammenAdaptor;
42   }
43
44   @Override
45   public void registerVersioning(String versionableEntityType) {
46     //no need
47   }
48
49   @Override
50   public void create(EntitlementPoolEntity entitlementPool) {
51     ZusammenElement entitlementPoolElement =
52         buildEntitlementPoolElement(entitlementPool, Action.CREATE);
53
54     ZusammenElement entitlementPoolsElement =
55         VlmZusammenUtil.buildStructuralElement(StructureElement.EntitlementPools, null);
56
57     entitlementPoolsElement.addSubElement(entitlementPoolElement);
58
59     SessionContext context = ZusammenUtil.createSessionContext();
60     Id itemId = new Id(entitlementPool.getVendorLicenseModelId());
61     Optional<Element> savedElement = zusammenAdaptor.saveElement(context, new ElementContext(itemId,
62             VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor)),
63         entitlementPoolsElement, "Create entitlement pool");
64
65     savedElement.ifPresent(element -> entitlementPool
66         .setId(element.getSubElements().iterator().next().getElementId().getValue()));
67   }
68
69   @Override
70   public void update(EntitlementPoolEntity entitlementPool) {
71     ZusammenElement entitlmentpoolElement =
72         buildEntitlementPoolElement(entitlementPool, Action.UPDATE);
73
74     SessionContext context = ZusammenUtil.createSessionContext();
75     Id itemId = new Id(entitlementPool.getVendorLicenseModelId());
76     ElementContext elementContext =  new ElementContext(itemId,
77         VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor));
78
79     Optional<ElementInfo> epFromDb = zusammenAdaptor.getElementInfo(context, elementContext,
80         new Id(entitlementPool.getId()));
81
82     if (epFromDb.isPresent()) {
83       if (entitlmentpoolElement.getRelations() == null) {
84         entitlmentpoolElement.setRelations(new ArrayList<>());
85       }
86       if (epFromDb.get().getRelations() != null) {
87         entitlmentpoolElement.getRelations().addAll(epFromDb.get().getRelations());
88       }
89     }
90
91     zusammenAdaptor.saveElement(context,elementContext, entitlmentpoolElement,
92         String.format("Update entitlement pool with id %s", entitlementPool.getId()));
93   }
94
95   @Override
96   public EntitlementPoolEntity get(EntitlementPoolEntity entitlementPool) {
97     SessionContext context = ZusammenUtil.createSessionContext();
98     Id itemId = new Id(entitlementPool.getVendorLicenseModelId());
99     ElementContext elementContext = new ElementContext(itemId,
100         VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor),
101         VlmZusammenUtil.getVersionTag(entitlementPool.getVersion()));
102
103     return zusammenAdaptor.getElementInfo(context, elementContext, new Id(entitlementPool.getId()))
104         .map(elementInfo -> mapElementInfoToEntitlementPool(
105             entitlementPool.getVendorLicenseModelId(), entitlementPool.getVersion(), elementInfo))
106         .orElse(null);
107   }
108
109   @Override
110   public void delete(EntitlementPoolEntity entitlementPool) {
111     SessionContext context = ZusammenUtil.createSessionContext();
112     ZusammenElement zusammenElement = new ZusammenElement();
113     zusammenElement.setAction(Action.DELETE);
114     zusammenElement.setElementId(new Id(entitlementPool.getId()));
115
116     Id itemId = new Id(entitlementPool.getVendorLicenseModelId());
117     ElementContext elementContext =
118         new ElementContext(itemId,
119             VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor));
120     zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
121         "delete entitlement pool. id:" + entitlementPool.getId() + ".");
122   }
123
124   @Override
125   public Collection<EntitlementPoolEntity> list(EntitlementPoolEntity entitlementPool) {
126     SessionContext context = ZusammenUtil.createSessionContext();
127     Id itemId = new Id(entitlementPool.getVendorLicenseModelId());
128     ElementContext elementContext = new ElementContext(itemId,
129         VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor),
130         VlmZusammenUtil.getVersionTag(entitlementPool.getVersion()));
131
132     return zusammenAdaptor
133         .listElementsByName(context, elementContext, null, StructureElement.EntitlementPools.name())
134         .stream().map(elementInfo -> mapElementInfoToEntitlementPool(
135             entitlementPool.getVendorLicenseModelId(), entitlementPool.getVersion(), elementInfo))
136         .collect(Collectors.toList());
137   }
138
139   @Override
140   public long count(EntitlementPoolEntity entitlementPool) {
141     SessionContext context = ZusammenUtil.createSessionContext();
142     Id itemId = new Id(entitlementPool.getVendorLicenseModelId());
143     ElementContext elementContext = new ElementContext(itemId,
144         VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor),
145         VlmZusammenUtil.getVersionTag(entitlementPool.getVersion()));
146
147     return zusammenAdaptor
148         .listElementsByName(context, elementContext, null, StructureElement.EntitlementPools.name())
149         .size();
150   }
151
152   @Override
153   public void removeReferencingFeatureGroup(EntitlementPoolEntity entitlementPool,
154                                             String referencingFeatureGroupId) {
155     SessionContext context = ZusammenUtil.createSessionContext();
156     Id itemId = new Id(entitlementPool.getVendorLicenseModelId());
157     ElementContext elementContext = new ElementContext(itemId,
158         VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor));
159
160     Optional<ElementInfo> elementInfo =
161         zusammenAdaptor.getElementInfo(context, elementContext, new Id(entitlementPool.getId()));
162
163     if (elementInfo.isPresent()) {
164       ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
165       zusammenElement.setAction(Action.UPDATE);
166       zusammenElement.setRelations(elementInfo.get().getRelations().stream()
167           .filter(relation -> !referencingFeatureGroupId
168               .equals(relation.getEdge2().getElementId().getValue()))
169           .collect(Collectors.toList()));
170
171       zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
172           "remove referencing feature group");
173     }
174   }
175
176   @Override
177   public void addReferencingFeatureGroup(EntitlementPoolEntity entitlementPool,
178                                          String referencingFeatureGroupId) {
179     SessionContext context = ZusammenUtil.createSessionContext();
180     Id itemId = new Id(entitlementPool.getVendorLicenseModelId());
181     ElementContext elementContext = new ElementContext(itemId,
182         VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor));
183
184     Optional<ElementInfo> elementInfo =
185         zusammenAdaptor.getElementInfo(context, elementContext, new Id(entitlementPool.getId()));
186
187     if (elementInfo.isPresent()) {
188       ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
189       zusammenElement.setAction(Action.UPDATE);
190       if (zusammenElement.getRelations() == null) {
191         zusammenElement.setRelations(new ArrayList<>());
192       }
193       zusammenElement.getRelations().add(VlmZusammenUtil
194           .createRelation(RelationType.EntitlmentPoolToReferencingFeatureGroup,
195               referencingFeatureGroupId));
196       zusammenAdaptor
197           .saveElement(context, elementContext, zusammenElement, "add referencing feature group");
198     }
199   }
200
201   @Override
202   public void deleteAll(EntitlementPoolEntity entitlementPool) {
203     //not supported
204   }
205
206   private ZusammenElement buildEntitlementPoolElement(EntitlementPoolEntity entitlementPool,
207                                                       Action action) {
208     ZusammenElement entitlementPoolElement = new ZusammenElement();
209     entitlementPoolElement.setAction(action);
210     if (entitlementPool.getId() != null) {
211       entitlementPoolElement.setElementId(new Id(entitlementPool.getId()));
212     }
213     Info info = new Info();
214     info.setName(entitlementPool.getName());
215     info.setDescription(entitlementPool.getDescription());
216     info.addProperty("thresholdValue", entitlementPool.getThresholdValue());
217     info.addProperty("threshold_unit", entitlementPool.getThresholdUnit());
218     info.addProperty("entitlement_metric", entitlementPool.getEntitlementMetric());
219     info.addProperty("increments", entitlementPool.getIncrements());
220     info.addProperty("aggregation_func", entitlementPool.getAggregationFunction());
221     info.addProperty("operational_scope", entitlementPool.getOperationalScope());
222     info.addProperty("EntitlementTime", entitlementPool.getTime());
223     info.addProperty("manufacturerReferenceNumber",
224         entitlementPool.getManufacturerReferenceNumber());
225     entitlementPoolElement.setInfo(info);
226
227     if (entitlementPool.getReferencingFeatureGroups() != null &&
228         entitlementPool.getReferencingFeatureGroups().size() > 0) {
229       entitlementPoolElement.setRelations(entitlementPool.getReferencingFeatureGroups().stream()
230           .map(rel -> VlmZusammenUtil
231               .createRelation(RelationType.EntitlmentPoolToReferencingFeatureGroup, rel))
232           .collect(Collectors.toList()));
233     }
234     return entitlementPoolElement;
235   }
236
237   private EntitlementPoolEntity mapElementInfoToEntitlementPool(String vlmId, Version version,
238                                                                 ElementInfo elementInfo) {
239     EntitlementPoolEntity entitlmentPool =
240         new EntitlementPoolEntity(vlmId, version, elementInfo.getId().getValue());
241     entitlmentPool.setName(elementInfo.getInfo().getName());
242     entitlmentPool.setDescription(elementInfo.getInfo().getDescription());
243     entitlmentPool
244         .setThresholdValue(toInteger(elementInfo.getInfo().getProperty("thresholdValue")));
245     entitlmentPool.setThresholdUnit(
246         ThresholdUnit.valueOf(elementInfo.getInfo().getProperty("threshold_unit")));
247     entitlmentPool.setEntitlementMetric(
248         getEntitlementMetricCoiceOrOther(elementInfo.getInfo().getProperty("entitlement_metric")));
249     entitlmentPool.setIncrements(elementInfo.getInfo().getProperty("increments"));
250     entitlmentPool.setAggregationFunction(
251         getAggregationFuncCoiceOrOther(elementInfo.getInfo().getProperty("aggregation_func")));
252     entitlmentPool.setOperationalScope(getOperationalScopeMultiChoiceOrOther(
253         elementInfo.getInfo().getProperty("operational_scope")));
254     entitlmentPool.setTime(
255         getEntitlementTimeCoiceOrOther(elementInfo.getInfo().getProperty("EntitlementTime")));
256     entitlmentPool.setManufacturerReferenceNumber(
257         elementInfo.getInfo().getProperty("manufacturerReferenceNumber"));
258
259     if (elementInfo.getRelations() != null && elementInfo.getRelations().size() > 0) {
260       entitlmentPool
261           .setReferencingFeatureGroups(elementInfo.getRelations().stream().map(relation -> relation
262               .getEdge2().getElementId().getValue()).collect(Collectors.toSet()));
263     }
264     return entitlmentPool;
265   }
266
267   private ChoiceOrOther<AggregationFunction> getAggregationFuncCoiceOrOther(
268       Map aggregationFunction) {
269     return new ChoiceOrOther<>
270         (AggregationFunction.valueOf((String) aggregationFunction.get("choice")),
271             (String) aggregationFunction.get("other"));
272   }
273
274   private ChoiceOrOther<EntitlementMetric> getEntitlementMetricCoiceOrOther(Map entitlementMetric) {
275     return new ChoiceOrOther<>(EntitlementMetric.valueOf((String) entitlementMetric.get("choice")
276     ), (String) entitlementMetric.get("other"));
277   }
278
279   private ChoiceOrOther<EntitlementTime> getEntitlementTimeCoiceOrOther(Map entitlementTime) {
280     return new ChoiceOrOther<>(EntitlementTime.valueOf((String) entitlementTime.get("choice")),
281         (String) entitlementTime.get("other"));
282   }
283
284   private MultiChoiceOrOther<OperationalScope> getOperationalScopeMultiChoiceOrOther
285       (Map<String, Object>
286            operationalScope) {
287     Set<OperationalScope> choices = new HashSet<>();
288     ((List<String>) operationalScope.get("choices")).
289         forEach(choice -> choices.add(OperationalScope.valueOf(choice)));
290
291     return new MultiChoiceOrOther<>(choices, (String) operationalScope.get("other"));
292   }
293
294   private Integer toInteger(Object val) {
295     if (val instanceof Double) {
296       return ((Double) val).intValue();
297     } else if (val instanceof String) {
298       return new Integer((String) val);
299     } else if (val instanceof Integer) {
300       return (Integer) val;
301     }
302     throw new RuntimeException("invalid value for integer:" + val.getClass());
303   }
304
305 }