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 if (epFromDb.get().getRelations() != null) {
87 entitlmentpoolElement.getRelations().addAll(epFromDb.get().getRelations());
91 zusammenAdaptor.saveElement(context,elementContext, entitlmentpoolElement,
92 String.format("Update entitlement pool with id %s", entitlementPool.getId()));
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()));
103 return zusammenAdaptor.getElementInfo(context, elementContext, new Id(entitlementPool.getId()))
104 .map(elementInfo -> mapElementInfoToEntitlementPool(
105 entitlementPool.getVendorLicenseModelId(), entitlementPool.getVersion(), elementInfo))
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()));
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() + ".");
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()));
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());
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()));
147 return zusammenAdaptor
148 .listElementsByName(context, elementContext, null, StructureElement.EntitlementPools.name())
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));
160 Optional<ElementInfo> elementInfo =
161 zusammenAdaptor.getElementInfo(context, elementContext, new Id(entitlementPool.getId()));
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()));
171 zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
172 "remove referencing feature group");
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));
184 Optional<ElementInfo> elementInfo =
185 zusammenAdaptor.getElementInfo(context, elementContext, new Id(entitlementPool.getId()));
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<>());
193 zusammenElement.getRelations().add(VlmZusammenUtil
194 .createRelation(RelationType.EntitlmentPoolToReferencingFeatureGroup,
195 referencingFeatureGroupId));
197 .saveElement(context, elementContext, zusammenElement, "add referencing feature group");
202 public void deleteAll(EntitlementPoolEntity entitlementPool) {
206 private ZusammenElement buildEntitlementPoolElement(EntitlementPoolEntity entitlementPool,
208 ZusammenElement entitlementPoolElement = new ZusammenElement();
209 entitlementPoolElement.setAction(action);
210 if (entitlementPool.getId() != null) {
211 entitlementPoolElement.setElementId(new Id(entitlementPool.getId()));
213 Info info = new Info();
214 info.setName(entitlementPool.getName());
215 info.setDescription(entitlementPool.getDescription());
216 info.addProperty("thresholdValue", entitlementPool.getThresholdValue());
217 info.addProperty("threshold_unit", entitlementPool.getThresholdUnit());
218 info.addProperty("entitlement_metric", entitlementPool.getEntitlementMetric());
219 info.addProperty("increments", entitlementPool.getIncrements());
220 info.addProperty("aggregation_func", entitlementPool.getAggregationFunction());
221 info.addProperty("operational_scope", entitlementPool.getOperationalScope());
222 info.addProperty("EntitlementTime", entitlementPool.getTime());
223 info.addProperty("manufacturerReferenceNumber",
224 entitlementPool.getManufacturerReferenceNumber());
225 entitlementPoolElement.setInfo(info);
227 if (entitlementPool.getReferencingFeatureGroups() != null &&
228 entitlementPool.getReferencingFeatureGroups().size() > 0) {
229 entitlementPoolElement.setRelations(entitlementPool.getReferencingFeatureGroups().stream()
230 .map(rel -> VlmZusammenUtil
231 .createRelation(RelationType.EntitlmentPoolToReferencingFeatureGroup, rel))
232 .collect(Collectors.toList()));
234 return entitlementPoolElement;
237 private EntitlementPoolEntity mapElementInfoToEntitlementPool(String vlmId, Version version,
238 ElementInfo elementInfo) {
239 EntitlementPoolEntity entitlmentPool =
240 new EntitlementPoolEntity(vlmId, version, elementInfo.getId().getValue());
241 entitlmentPool.setName(elementInfo.getInfo().getName());
242 entitlmentPool.setDescription(elementInfo.getInfo().getDescription());
244 .setThresholdValue(toInteger(elementInfo.getInfo().getProperty("thresholdValue")));
245 entitlmentPool.setThresholdUnit(
246 ThresholdUnit.valueOf(elementInfo.getInfo().getProperty("threshold_unit")));
247 entitlmentPool.setEntitlementMetric(
248 getEntitlementMetricCoiceOrOther(elementInfo.getInfo().getProperty("entitlement_metric")));
249 entitlmentPool.setIncrements(elementInfo.getInfo().getProperty("increments"));
250 entitlmentPool.setAggregationFunction(
251 getAggregationFuncCoiceOrOther(elementInfo.getInfo().getProperty("aggregation_func")));
252 entitlmentPool.setOperationalScope(getOperationalScopeMultiChoiceOrOther(
253 elementInfo.getInfo().getProperty("operational_scope")));
254 entitlmentPool.setTime(
255 getEntitlementTimeCoiceOrOther(elementInfo.getInfo().getProperty("EntitlementTime")));
256 entitlmentPool.setManufacturerReferenceNumber(
257 elementInfo.getInfo().getProperty("manufacturerReferenceNumber"));
259 if (elementInfo.getRelations() != null && elementInfo.getRelations().size() > 0) {
261 .setReferencingFeatureGroups(elementInfo.getRelations().stream().map(relation -> relation
262 .getEdge2().getElementId().getValue()).collect(Collectors.toSet()));
264 return entitlmentPool;
267 private ChoiceOrOther<AggregationFunction> getAggregationFuncCoiceOrOther(
268 Map aggregationFunction) {
269 return new ChoiceOrOther<>
270 (AggregationFunction.valueOf((String) aggregationFunction.get("choice")),
271 (String) aggregationFunction.get("other"));
274 private ChoiceOrOther<EntitlementMetric> getEntitlementMetricCoiceOrOther(Map entitlementMetric) {
275 return new ChoiceOrOther<>(EntitlementMetric.valueOf((String) entitlementMetric.get("choice")
276 ), (String) entitlementMetric.get("other"));
279 private ChoiceOrOther<EntitlementTime> getEntitlementTimeCoiceOrOther(Map entitlementTime) {
280 return new ChoiceOrOther<>(EntitlementTime.valueOf((String) entitlementTime.get("choice")),
281 (String) entitlementTime.get("other"));
284 private MultiChoiceOrOther<OperationalScope> getOperationalScopeMultiChoiceOrOther
287 Set<OperationalScope> choices = new HashSet<>();
288 ((List<String>) operationalScope.get("choices")).
289 forEach(choice -> choices.add(OperationalScope.valueOf(choice)));
291 return new MultiChoiceOrOther<>(choices, (String) operationalScope.get("other"));
294 private Integer toInteger(Object val) {
295 if (val instanceof Double) {
296 return ((Double) val).intValue();
297 } else if (val instanceof String) {
298 return new Integer((String) val);
299 } else if (val instanceof Integer) {
300 return (Integer) val;
302 throw new RuntimeException("invalid value for integer:" + val.getClass());