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