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 && epFromDb.get().getRelations().size() > 0) {
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,
209 ZusammenElement entitlementPoolElement = new ZusammenElement();
210 entitlementPoolElement.setAction(action);
211 if (entitlementPool.getId() != null) {
212 entitlementPoolElement.setElementId(new Id(entitlementPool.getId()));
214 Info info = new Info();
215 info.setName(entitlementPool.getName());
216 info.setDescription(entitlementPool.getDescription());
217 info.addProperty("thresholdValue", entitlementPool.getThresholdValue());
218 info.addProperty("threshold_unit", entitlementPool.getThresholdUnit());
219 info.addProperty("entitlement_metric", entitlementPool.getEntitlementMetric());
220 info.addProperty("increments", entitlementPool.getIncrements());
221 info.addProperty("aggregation_func", entitlementPool.getAggregationFunction());
222 info.addProperty("operational_scope", entitlementPool.getOperationalScope());
223 info.addProperty("EntitlementTime", entitlementPool.getTime());
224 info.addProperty("manufacturerReferenceNumber",
225 entitlementPool.getManufacturerReferenceNumber());
226 info.addProperty("startDate", entitlementPool.getStartDate());
227 info.addProperty("expiryDate", entitlementPool.getExpiryDate());
228 entitlementPoolElement.setInfo(info);
230 if (entitlementPool.getReferencingFeatureGroups() != null
231 && entitlementPool.getReferencingFeatureGroups().size() > 0) {
232 entitlementPoolElement.setRelations(entitlementPool.getReferencingFeatureGroups().stream()
233 .map(rel -> VlmZusammenUtil
234 .createRelation(RelationType.EntitlmentPoolToReferencingFeatureGroup, rel))
235 .collect(Collectors.toList()));
237 return entitlementPoolElement;
240 private EntitlementPoolEntity mapElementInfoToEntitlementPool(String vlmId, Version version,
241 ElementInfo elementInfo) {
242 EntitlementPoolEntity entitlmentPool =
243 new EntitlementPoolEntity(vlmId, version, elementInfo.getId().getValue());
244 entitlmentPool.setName(elementInfo.getInfo().getName());
245 entitlmentPool.setDescription(elementInfo.getInfo().getDescription());
247 .setThresholdValue(elementInfo.getInfo().getProperty("thresholdValue") != null
248 ? VlmZusammenUtil.toInteger(elementInfo.getInfo().getProperty("thresholdValue")) : null);
250 Object threshold_unit = elementInfo.getInfo().getProperty("threshold_unit");
251 entitlmentPool.setThresholdUnit( threshold_unit != null ?
252 ThresholdUnit.valueOf(elementInfo.getInfo().getProperty("threshold_unit")) : null);
253 entitlmentPool.setEntitlementMetric(
254 getEntitlementMetricCoiceOrOther(elementInfo.getInfo().getProperty("entitlement_metric")));
255 entitlmentPool.setIncrements(elementInfo.getInfo().getProperty("increments"));
256 entitlmentPool.setAggregationFunction(
257 getAggregationFuncCoiceOrOther(elementInfo.getInfo().getProperty("aggregation_func")));
258 entitlmentPool.setOperationalScope(getOperationalScopeMultiChoiceOrOther(
259 elementInfo.getInfo().getProperty("operational_scope")));
260 entitlmentPool.setTime(
261 getEntitlementTimeCoiceOrOther(elementInfo.getInfo().getProperty("EntitlementTime")));
262 entitlmentPool.setManufacturerReferenceNumber(
263 elementInfo.getInfo().getProperty("manufacturerReferenceNumber"));
264 entitlmentPool.setStartDate(elementInfo.getInfo().getProperty("startDate"));
265 entitlmentPool.setExpiryDate(elementInfo.getInfo().getProperty("expiryDate"));
267 if (elementInfo.getRelations() != null && elementInfo.getRelations().size() > 0) {
269 .setReferencingFeatureGroups(elementInfo.getRelations().stream().map(relation -> relation
270 .getEdge2().getElementId().getValue()).collect(Collectors.toSet()));
272 return entitlmentPool;
275 private ChoiceOrOther<AggregationFunction> getAggregationFuncCoiceOrOther(
276 Map aggregationFunction) {
277 return new ChoiceOrOther<>
278 (AggregationFunction.valueOf((String) aggregationFunction.get("choice")),
279 (String) aggregationFunction.get("other"));
282 private ChoiceOrOther<EntitlementMetric> getEntitlementMetricCoiceOrOther(Map entitlementMetric) {
283 return new ChoiceOrOther<>(EntitlementMetric.valueOf((String) entitlementMetric.get("choice")
284 ), (String) entitlementMetric.get("other"));
287 private ChoiceOrOther<EntitlementTime> getEntitlementTimeCoiceOrOther(Map entitlementTime) {
288 return new ChoiceOrOther<>(EntitlementTime.valueOf((String) entitlementTime.get("choice")),
289 (String) entitlementTime.get("other"));
292 private MultiChoiceOrOther<OperationalScope> getOperationalScopeMultiChoiceOrOther
295 if(operationalScope != null && !operationalScope.isEmpty()) {
296 Set<OperationalScope> choices = new HashSet<>();
297 ((List<String>) operationalScope.get("choices")).
298 forEach(choice -> choices.add(OperationalScope.valueOf(choice)));
300 return new MultiChoiceOrOther<>(choices, operationalScope.get("other")==null?null:
301 (String) operationalScope.get("other"));
307 public String getManufacturerReferenceNumber(EntitlementPoolEntity entitlementPoolEntity){
308 SessionContext context = ZusammenUtil.createSessionContext();
309 Id itemId = new Id(entitlementPoolEntity.getVendorLicenseModelId());
310 ElementContext elementContext = new ElementContext(itemId,
311 VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor),
312 VlmZusammenUtil.getVersionTag(entitlementPoolEntity.getVersion()));
313 Optional<ElementInfo> elementInfo1 = zusammenAdaptor.getElementInfo(context, elementContext, new Id(entitlementPoolEntity.getId()));
314 Map<String, Object> properties = elementInfo1.get().getInfo().getProperties();
315 String manufacturerReferenceNumber = null;
316 if(properties != null && properties.containsKey("manufacturerReferenceNumber") ) {
317 manufacturerReferenceNumber = (String)properties.get("manufacturerReferenceNumber");
319 return manufacturerReferenceNumber;