1 package org.openecomp.sdc.vendorlicense.dao.impl.zusammen;
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;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.HashSet;
27 import java.util.List;
29 import java.util.Optional;
31 import java.util.stream.Collectors;
34 * Created by ayalaben on 3/28/2017.
36 public class EntitlementPoolZusammenDaoImpl implements EntitlementPoolDao {
38 private ZusammenAdaptor zusammenAdaptor;
40 public EntitlementPoolZusammenDaoImpl(ZusammenAdaptor zusammenAdaptor) {
41 this.zusammenAdaptor = zusammenAdaptor;
45 public void registerVersioning(String versionableEntityType) {
50 public void create(EntitlementPoolEntity entitlementPool) {
51 ZusammenElement entitlementPoolElement =
52 buildEntitlementPoolElement(entitlementPool, Action.CREATE);
54 ZusammenElement entitlementPoolsElement =
55 VlmZusammenUtil.buildStructuralElement(StructureElement.EntitlementPools, null);
57 entitlementPoolsElement.addSubElement(entitlementPoolElement);
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");
65 savedElement.ifPresent(element -> entitlementPool
66 .setId(element.getSubElements().iterator().next().getElementId().getValue()));
70 public void update(EntitlementPoolEntity entitlementPool) {
71 ZusammenElement entitlmentpoolElement =
72 buildEntitlementPoolElement(entitlementPool, Action.UPDATE);
74 SessionContext context = ZusammenUtil.createSessionContext();
75 Id itemId = new Id(entitlementPool.getVendorLicenseModelId());
76 ElementContext elementContext = new ElementContext(itemId,
77 VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor));
79 Optional<ElementInfo> epFromDb = zusammenAdaptor.getElementInfo(context, elementContext,
80 new Id(entitlementPool.getId()));
82 if (epFromDb.isPresent()) {
83 if (entitlmentpoolElement.getRelations() == null) {
84 entitlmentpoolElement.setRelations(new ArrayList<>());
86 entitlmentpoolElement.getRelations().addAll(epFromDb.get().getRelations());
89 zusammenAdaptor.saveElement(context,elementContext, entitlmentpoolElement,
90 String.format("Update entitlement pool with id %s", entitlementPool.getId()));
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()));
101 return zusammenAdaptor.getElementInfo(context, elementContext, new Id(entitlementPool.getId()))
102 .map(elementInfo -> mapElementInfoToEntitlementPool(
103 entitlementPool.getVendorLicenseModelId(), entitlementPool.getVersion(), elementInfo))
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()));
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() + ".");
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()));
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());
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()));
145 return zusammenAdaptor
146 .listElementsByName(context, elementContext, null, StructureElement.EntitlementPools.name())
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));
158 Optional<ElementInfo> elementInfo =
159 zusammenAdaptor.getElementInfo(context, elementContext, new Id(entitlementPool.getId()));
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()));
169 zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
170 "remove referencing feature group");
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));
182 Optional<ElementInfo> elementInfo =
183 zusammenAdaptor.getElementInfo(context, elementContext, new Id(entitlementPool.getId()));
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<>());
191 zusammenElement.getRelations().add(VlmZusammenUtil
192 .createRelation(RelationType.EntitlmentPoolToReferencingFeatureGroup,
193 referencingFeatureGroupId));
195 .saveElement(context, elementContext, zusammenElement, "add referencing feature group");
200 public void deleteAll(EntitlementPoolEntity entitlementPool) {
204 private ZusammenElement buildEntitlementPoolElement(EntitlementPoolEntity entitlementPool,
206 ZusammenElement entitlementPoolElement = new ZusammenElement();
207 entitlementPoolElement.setAction(action);
208 if (entitlementPool.getId() != null) {
209 entitlementPoolElement.setElementId(new Id(entitlementPool.getId()));
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);
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()));
232 return entitlementPoolElement;
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());
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"));
257 if (elementInfo.getRelations() != null && elementInfo.getRelations().size() > 0) {
259 .setReferencingFeatureGroups(elementInfo.getRelations().stream().map(relation -> relation
260 .getEdge2().getElementId().getValue()).collect(Collectors.toSet()));
262 return entitlmentPool;
265 private ChoiceOrOther<AggregationFunction> getAggregationFuncCoiceOrOther(
266 Map aggregationFunction) {
267 return new ChoiceOrOther<>
268 (AggregationFunction.valueOf((String) aggregationFunction.get("choice")),
269 (String) aggregationFunction.get("other"));
272 private ChoiceOrOther<EntitlementMetric> getEntitlementMetricCoiceOrOther(Map entitlementMetric) {
273 return new ChoiceOrOther<>(EntitlementMetric.valueOf((String) entitlementMetric.get("choice")
274 ), (String) entitlementMetric.get("other"));
277 private ChoiceOrOther<EntitlementTime> getEntitlementTimeCoiceOrOther(Map entitlementTime) {
278 return new ChoiceOrOther<>(EntitlementTime.valueOf((String) entitlementTime.get("choice")),
279 (String) entitlementTime.get("other"));
282 private MultiChoiceOrOther<OperationalScope> getOperationalScopeMultiChoiceOrOther
285 Set<OperationalScope> choices = new HashSet<>();
286 ((List<String>) operationalScope.get("choices")).
287 forEach(choice -> choices.add(OperationalScope.valueOf(choice)));
289 return new MultiChoiceOrOther<>(choices, (String) operationalScope.get("other"));
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;
300 throw new RuntimeException("invalid value for integer:" + val.getClass());