[SDC] Onboarding 1710 rebase.
[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 && epFromDb.get().getRelations().size() > 0) {
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
209     ZusammenElement entitlementPoolElement = new ZusammenElement();
210     entitlementPoolElement.setAction(action);
211     if (entitlementPool.getId() != null) {
212       entitlementPoolElement.setElementId(new Id(entitlementPool.getId()));
213     }
214     Info info = new Info();
215     info.setName(entitlementPool.getName());
216     info.setDescription(entitlementPool.getDescription());
217     info.addProperty("thresholdValue", entitlementPool.getThresholdValue());
218     info.addProperty("threshold_unit", entitlementPool.getThresholdUnit());
219     info.addProperty("entitlement_metric", entitlementPool.getEntitlementMetric());
220     info.addProperty("increments", entitlementPool.getIncrements());
221     info.addProperty("aggregation_func", entitlementPool.getAggregationFunction());
222     info.addProperty("operational_scope", entitlementPool.getOperationalScope());
223     info.addProperty("EntitlementTime", entitlementPool.getTime());
224     info.addProperty("manufacturerReferenceNumber",
225         entitlementPool.getManufacturerReferenceNumber());
226     info.addProperty("startDate", entitlementPool.getStartDate());
227     info.addProperty("expiryDate", entitlementPool.getExpiryDate());
228     entitlementPoolElement.setInfo(info);
229
230    if (entitlementPool.getReferencingFeatureGroups() != null
231        && entitlementPool.getReferencingFeatureGroups().size() > 0) {
232       entitlementPoolElement.setRelations(entitlementPool.getReferencingFeatureGroups().stream()
233           .map(rel -> VlmZusammenUtil
234               .createRelation(RelationType.EntitlmentPoolToReferencingFeatureGroup, rel))
235           .collect(Collectors.toList()));
236     }
237     return entitlementPoolElement;
238   }
239
240   private EntitlementPoolEntity mapElementInfoToEntitlementPool(String vlmId, Version version,
241                                                                 ElementInfo elementInfo) {
242     EntitlementPoolEntity entitlmentPool =
243         new EntitlementPoolEntity(vlmId, version, elementInfo.getId().getValue());
244     entitlmentPool.setName(elementInfo.getInfo().getName());
245     entitlmentPool.setDescription(elementInfo.getInfo().getDescription());
246     entitlmentPool
247         .setThresholdValue(elementInfo.getInfo().getProperty("thresholdValue") != null
248                 ? VlmZusammenUtil.toInteger(elementInfo.getInfo().getProperty("thresholdValue")) : null);
249
250     Object threshold_unit = elementInfo.getInfo().getProperty("threshold_unit");
251     entitlmentPool.setThresholdUnit( threshold_unit != null ?
252         ThresholdUnit.valueOf(elementInfo.getInfo().getProperty("threshold_unit")) : null);
253     entitlmentPool.setEntitlementMetric(
254         getEntitlementMetricCoiceOrOther(elementInfo.getInfo().getProperty("entitlement_metric")));
255     entitlmentPool.setIncrements(elementInfo.getInfo().getProperty("increments"));
256     entitlmentPool.setAggregationFunction(
257         getAggregationFuncCoiceOrOther(elementInfo.getInfo().getProperty("aggregation_func")));
258     entitlmentPool.setOperationalScope(getOperationalScopeMultiChoiceOrOther(
259         elementInfo.getInfo().getProperty("operational_scope")));
260     entitlmentPool.setTime(
261         getEntitlementTimeCoiceOrOther(elementInfo.getInfo().getProperty("EntitlementTime")));
262     entitlmentPool.setManufacturerReferenceNumber(
263         elementInfo.getInfo().getProperty("manufacturerReferenceNumber"));
264     entitlmentPool.setStartDate(elementInfo.getInfo().getProperty("startDate"));
265     entitlmentPool.setExpiryDate(elementInfo.getInfo().getProperty("expiryDate"));
266
267     if (elementInfo.getRelations() != null && elementInfo.getRelations().size() > 0) {
268       entitlmentPool
269           .setReferencingFeatureGroups(elementInfo.getRelations().stream().map(relation -> relation
270               .getEdge2().getElementId().getValue()).collect(Collectors.toSet()));
271     }
272     return entitlmentPool;
273   }
274
275   private ChoiceOrOther<AggregationFunction> getAggregationFuncCoiceOrOther(
276       Map aggregationFunction) {
277     return new ChoiceOrOther<>
278         (AggregationFunction.valueOf((String) aggregationFunction.get("choice")),
279             (String) aggregationFunction.get("other"));
280   }
281
282   private ChoiceOrOther<EntitlementMetric> getEntitlementMetricCoiceOrOther(Map entitlementMetric) {
283     return new ChoiceOrOther<>(EntitlementMetric.valueOf((String) entitlementMetric.get("choice")
284     ), (String) entitlementMetric.get("other"));
285   }
286
287   private ChoiceOrOther<EntitlementTime> getEntitlementTimeCoiceOrOther(Map entitlementTime) {
288     return new ChoiceOrOther<>(EntitlementTime.valueOf((String) entitlementTime.get("choice")),
289         (String) entitlementTime.get("other"));
290   }
291
292   private MultiChoiceOrOther<OperationalScope> getOperationalScopeMultiChoiceOrOther
293       (Map<String, Object>
294            operationalScope) {
295     if(operationalScope != null && !operationalScope.isEmpty()) {
296       Set<OperationalScope> choices = new HashSet<>();
297       ((List<String>) operationalScope.get("choices")).
298           forEach(choice -> choices.add(OperationalScope.valueOf(choice)));
299
300       return new MultiChoiceOrOther<>(choices, operationalScope.get("other")==null?null:
301           (String) operationalScope.get("other"));
302     }
303     return null;
304   }
305
306   @Override
307   public String getManufacturerReferenceNumber(EntitlementPoolEntity entitlementPoolEntity){
308     SessionContext context = ZusammenUtil.createSessionContext();
309     Id itemId = new Id(entitlementPoolEntity.getVendorLicenseModelId());
310     ElementContext elementContext = new ElementContext(itemId,
311             VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor),
312             VlmZusammenUtil.getVersionTag(entitlementPoolEntity.getVersion()));
313     Optional<ElementInfo> elementInfo1 = zusammenAdaptor.getElementInfo(context, elementContext, new Id(entitlementPoolEntity.getId()));
314     Map<String, Object> properties = elementInfo1.get().getInfo().getProperties();
315     String manufacturerReferenceNumber = null;
316     if(properties != null && properties.containsKey("manufacturerReferenceNumber") ) {
317       manufacturerReferenceNumber = (String)properties.get("manufacturerReferenceNumber");
318     }
319     return manufacturerReferenceNumber;
320   }
321
322 }