[SDC-29] Amdocs OnBoard 1707 initial commit.
[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     zusammenAdaptor.saveElement(context, new ElementContext(itemId,
77             VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor)), entitlmentpoolElement,
78         String.format("Update entitlement pool with id %s", entitlementPool.getId()));
79   }
80
81   @Override
82   public EntitlementPoolEntity get(EntitlementPoolEntity entitlementPool) {
83     SessionContext context = ZusammenUtil.createSessionContext();
84     Id itemId = new Id(entitlementPool.getVendorLicenseModelId());
85     ElementContext elementContext = new ElementContext(itemId,
86         VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor),
87         VlmZusammenUtil.getVersionTag(entitlementPool.getVersion()));
88
89     return zusammenAdaptor.getElementInfo(context, elementContext, new Id(entitlementPool.getId()))
90         .map(elementInfo -> mapElementInfoToEntitlementPool(
91             entitlementPool.getVendorLicenseModelId(), entitlementPool.getVersion(), elementInfo))
92         .orElse(null);
93   }
94
95   @Override
96   public void delete(EntitlementPoolEntity entitlementPool) {
97     SessionContext context = ZusammenUtil.createSessionContext();
98     ZusammenElement zusammenElement = new ZusammenElement();
99     zusammenElement.setAction(Action.DELETE);
100     zusammenElement.setElementId(new Id(entitlementPool.getId()));
101
102     Id itemId = new Id(entitlementPool.getVendorLicenseModelId());
103     ElementContext elementContext =
104         new ElementContext(itemId,
105             VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor));
106     zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
107         "delete entitlement pool. id:" + entitlementPool.getId() + ".");
108   }
109
110   @Override
111   public Collection<EntitlementPoolEntity> list(EntitlementPoolEntity entitlementPool) {
112     SessionContext context = ZusammenUtil.createSessionContext();
113     Id itemId = new Id(entitlementPool.getVendorLicenseModelId());
114     ElementContext elementContext = new ElementContext(itemId,
115         VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor),
116         VlmZusammenUtil.getVersionTag(entitlementPool.getVersion()));
117
118     return zusammenAdaptor
119         .listElementsByName(context, elementContext, null, StructureElement.EntitlementPools.name())
120         .stream().map(elementInfo -> mapElementInfoToEntitlementPool(
121             entitlementPool.getVendorLicenseModelId(), entitlementPool.getVersion(), elementInfo))
122         .collect(Collectors.toList());
123   }
124
125   @Override
126   public long count(EntitlementPoolEntity entitlementPool) {
127     SessionContext context = ZusammenUtil.createSessionContext();
128     Id itemId = new Id(entitlementPool.getVendorLicenseModelId());
129     ElementContext elementContext = new ElementContext(itemId,
130         VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor),
131         VlmZusammenUtil.getVersionTag(entitlementPool.getVersion()));
132
133     return zusammenAdaptor
134         .listElementsByName(context, elementContext, null, StructureElement.EntitlementPools.name())
135         .size();
136   }
137
138   @Override
139   public void removeReferencingFeatureGroup(EntitlementPoolEntity entitlementPool,
140                                             String referencingFeatureGroupId) {
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
146     Optional<ElementInfo> elementInfo =
147         zusammenAdaptor.getElementInfo(context, elementContext, new Id(entitlementPool.getId()));
148
149     if (elementInfo.isPresent()) {
150       ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
151       zusammenElement.setAction(Action.UPDATE);
152       zusammenElement.setRelations(elementInfo.get().getRelations().stream()
153           .filter(relation -> !referencingFeatureGroupId
154               .equals(relation.getEdge2().getElementId().getValue()))
155           .collect(Collectors.toList()));
156
157       zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
158           "remove referencing feature group");
159     }
160   }
161
162   @Override
163   public void addReferencingFeatureGroup(EntitlementPoolEntity entitlementPool,
164                                          String referencingFeatureGroupId) {
165     SessionContext context = ZusammenUtil.createSessionContext();
166     Id itemId = new Id(entitlementPool.getVendorLicenseModelId());
167     ElementContext elementContext = new ElementContext(itemId,
168         VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor));
169
170     Optional<ElementInfo> elementInfo =
171         zusammenAdaptor.getElementInfo(context, elementContext, new Id(entitlementPool.getId()));
172
173     if (elementInfo.isPresent()) {
174       ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
175       zusammenElement.setAction(Action.UPDATE);
176       if (zusammenElement.getRelations() == null) {
177         zusammenElement.setRelations(new ArrayList<>());
178       }
179       zusammenElement.getRelations().add(VlmZusammenUtil
180           .createRelation(RelationType.EntitlmentPoolToReferencingFeatureGroup,
181               referencingFeatureGroupId));
182       zusammenAdaptor
183           .saveElement(context, elementContext, zusammenElement, "add referencing feature group");
184     }
185   }
186
187   @Override
188   public void deleteAll(EntitlementPoolEntity entitlementPool) {
189     //not supported
190   }
191
192   private ZusammenElement buildEntitlementPoolElement(EntitlementPoolEntity entitlementPool,
193                                                       Action action) {
194     ZusammenElement entitlementPoolElement = new ZusammenElement();
195     entitlementPoolElement.setAction(action);
196     if (entitlementPool.getId() != null) {
197       entitlementPoolElement.setElementId(new Id(entitlementPool.getId()));
198     }
199     Info info = new Info();
200     info.setName(entitlementPool.getName());
201     info.setDescription(entitlementPool.getDescription());
202     info.addProperty("thresholdValue", entitlementPool.getThresholdValue());
203     info.addProperty("threshold_unit", entitlementPool.getThresholdUnit());
204     info.addProperty("entitlement_metric", entitlementPool.getEntitlementMetric());
205     info.addProperty("increments", entitlementPool.getIncrements());
206     info.addProperty("aggregation_func", entitlementPool.getAggregationFunction());
207     info.addProperty("operational_scope", entitlementPool.getOperationalScope());
208     info.addProperty("EntitlementTime", entitlementPool.getTime());
209     info.addProperty("manufacturerReferenceNumber",
210         entitlementPool.getManufacturerReferenceNumber());
211     entitlementPoolElement.setInfo(info);
212
213     if (entitlementPool.getReferencingFeatureGroups() != null &&
214         entitlementPool.getReferencingFeatureGroups().size() > 0) {
215       entitlementPoolElement.setRelations(entitlementPool.getReferencingFeatureGroups().stream()
216           .map(rel -> VlmZusammenUtil
217               .createRelation(RelationType.EntitlmentPoolToReferencingFeatureGroup, rel))
218           .collect(Collectors.toList()));
219     }
220     return entitlementPoolElement;
221   }
222
223   private EntitlementPoolEntity mapElementInfoToEntitlementPool(String vlmId, Version version,
224                                                                 ElementInfo elementInfo) {
225     EntitlementPoolEntity entitlmentPool =
226         new EntitlementPoolEntity(vlmId, version, elementInfo.getId().getValue());
227     entitlmentPool.setName(elementInfo.getInfo().getName());
228     entitlmentPool.setDescription(elementInfo.getInfo().getDescription());
229     entitlmentPool
230         .setThresholdValue(toInteger(elementInfo.getInfo().getProperty("thresholdValue")));
231     entitlmentPool.setThresholdUnit(
232         ThresholdUnit.valueOf(elementInfo.getInfo().getProperty("threshold_unit")));
233     entitlmentPool.setEntitlementMetric(
234         getEntitlementMetricCoiceOrOther(elementInfo.getInfo().getProperty("entitlement_metric")));
235     entitlmentPool.setIncrements(elementInfo.getInfo().getProperty("increments"));
236     entitlmentPool.setAggregationFunction(
237         getAggregationFuncCoiceOrOther(elementInfo.getInfo().getProperty("aggregation_func")));
238     entitlmentPool.setOperationalScope(getOperationalScopeMultiChoiceOrOther(
239         elementInfo.getInfo().getProperty("operational_scope")));
240     entitlmentPool.setTime(
241         getEntitlementTimeCoiceOrOther(elementInfo.getInfo().getProperty("EntitlementTime")));
242     entitlmentPool.setManufacturerReferenceNumber(
243         elementInfo.getInfo().getProperty("manufacturerReferenceNumber"));
244
245     if (elementInfo.getRelations() != null && elementInfo.getRelations().size() > 0) {
246       entitlmentPool
247           .setReferencingFeatureGroups(elementInfo.getRelations().stream().map(relation -> relation
248               .getEdge2().getElementId().getValue()).collect(Collectors.toSet()));
249     }
250     return entitlmentPool;
251   }
252
253   private ChoiceOrOther<AggregationFunction> getAggregationFuncCoiceOrOther(
254       Map aggregationFunction) {
255     return new ChoiceOrOther<>
256         (AggregationFunction.valueOf((String) aggregationFunction.get("choice")),
257             (String) aggregationFunction.get("other"));
258   }
259
260   private ChoiceOrOther<EntitlementMetric> getEntitlementMetricCoiceOrOther(Map entitlementMetric) {
261     return new ChoiceOrOther<>(EntitlementMetric.valueOf((String) entitlementMetric.get("choice")
262     ), (String) entitlementMetric.get("other"));
263   }
264
265   private ChoiceOrOther<EntitlementTime> getEntitlementTimeCoiceOrOther(Map entitlementTime) {
266     return new ChoiceOrOther<>(EntitlementTime.valueOf((String) entitlementTime.get("choice")),
267         (String) entitlementTime.get("other"));
268   }
269
270   private MultiChoiceOrOther<OperationalScope> getOperationalScopeMultiChoiceOrOther
271       (Map<String, Object>
272            operationalScope) {
273     Set<OperationalScope> choices = new HashSet<>();
274     ((List<String>) operationalScope.get("choices")).
275         forEach(choice -> choices.add(OperationalScope.valueOf(choice)));
276
277     return new MultiChoiceOrOther<>(choices, (String) operationalScope.get("other"));
278   }
279
280   private Integer toInteger(Object val) {
281     if (val instanceof Double) {
282       return ((Double) val).intValue();
283     } else if (val instanceof String) {
284       return new Integer((String) val);
285     } else if (val instanceof Integer) {
286       return (Integer) val;
287     }
288     throw new RuntimeException("invalid value for integer:" + val.getClass());
289   }
290
291 }