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 zusammenAdaptor.saveElement(context, new ElementContext(itemId,
77 VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor)), entitlmentpoolElement,
78 String.format("Update entitlement pool with id %s", entitlementPool.getId()));
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()));
89 return zusammenAdaptor.getElementInfo(context, elementContext, new Id(entitlementPool.getId()))
90 .map(elementInfo -> mapElementInfoToEntitlementPool(
91 entitlementPool.getVendorLicenseModelId(), entitlementPool.getVersion(), elementInfo))
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()));
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() + ".");
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()));
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());
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()));
133 return zusammenAdaptor
134 .listElementsByName(context, elementContext, null, StructureElement.EntitlementPools.name())
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));
146 Optional<ElementInfo> elementInfo =
147 zusammenAdaptor.getElementInfo(context, elementContext, new Id(entitlementPool.getId()));
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()));
157 zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
158 "remove referencing feature group");
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));
170 Optional<ElementInfo> elementInfo =
171 zusammenAdaptor.getElementInfo(context, elementContext, new Id(entitlementPool.getId()));
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<>());
179 zusammenElement.getRelations().add(VlmZusammenUtil
180 .createRelation(RelationType.EntitlmentPoolToReferencingFeatureGroup,
181 referencingFeatureGroupId));
183 .saveElement(context, elementContext, zusammenElement, "add referencing feature group");
188 public void deleteAll(EntitlementPoolEntity entitlementPool) {
192 private ZusammenElement buildEntitlementPoolElement(EntitlementPoolEntity entitlementPool,
194 ZusammenElement entitlementPoolElement = new ZusammenElement();
195 entitlementPoolElement.setAction(action);
196 if (entitlementPool.getId() != null) {
197 entitlementPoolElement.setElementId(new Id(entitlementPool.getId()));
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);
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()));
220 return entitlementPoolElement;
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());
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"));
245 if (elementInfo.getRelations() != null && elementInfo.getRelations().size() > 0) {
247 .setReferencingFeatureGroups(elementInfo.getRelations().stream().map(relation -> relation
248 .getEdge2().getElementId().getValue()).collect(Collectors.toSet()));
250 return entitlmentPool;
253 private ChoiceOrOther<AggregationFunction> getAggregationFuncCoiceOrOther(
254 Map aggregationFunction) {
255 return new ChoiceOrOther<>
256 (AggregationFunction.valueOf((String) aggregationFunction.get("choice")),
257 (String) aggregationFunction.get("other"));
260 private ChoiceOrOther<EntitlementMetric> getEntitlementMetricCoiceOrOther(Map entitlementMetric) {
261 return new ChoiceOrOther<>(EntitlementMetric.valueOf((String) entitlementMetric.get("choice")
262 ), (String) entitlementMetric.get("other"));
265 private ChoiceOrOther<EntitlementTime> getEntitlementTimeCoiceOrOther(Map entitlementTime) {
266 return new ChoiceOrOther<>(EntitlementTime.valueOf((String) entitlementTime.get("choice")),
267 (String) entitlementTime.get("other"));
270 private MultiChoiceOrOther<OperationalScope> getOperationalScopeMultiChoiceOrOther
273 Set<OperationalScope> choices = new HashSet<>();
274 ((List<String>) operationalScope.get("choices")).
275 forEach(choice -> choices.add(OperationalScope.valueOf(choice)));
277 return new MultiChoiceOrOther<>(choices, (String) operationalScope.get("other"));
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;
288 throw new RuntimeException("invalid value for integer:" + val.getClass());