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.EntitlementPoolEntity;
15 import org.openecomp.sdc.vendorlicense.dao.types.MultiChoiceOrOther;
16 import org.openecomp.sdc.vendorlicense.dao.types.OperationalScope;
17 import org.openecomp.sdc.vendorlicense.dao.types.ThresholdUnit;
18 import org.openecomp.sdc.versioning.dao.types.Version;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.HashSet;
23 import java.util.List;
25 import java.util.Optional;
27 import java.util.stream.Collectors;
30 * Created by ayalaben on 3/28/2017.
32 public class EntitlementPoolZusammenDaoImpl implements EntitlementPoolDao {
34 private ZusammenAdaptor zusammenAdaptor;
36 public EntitlementPoolZusammenDaoImpl(ZusammenAdaptor zusammenAdaptor) {
37 this.zusammenAdaptor = zusammenAdaptor;
41 public void registerVersioning(String versionableEntityType) {
46 public void create(EntitlementPoolEntity entitlementPool) {
47 ZusammenElement entitlementPoolElement =
48 buildEntitlementPoolElement(entitlementPool, Action.CREATE);
50 ZusammenElement entitlementPoolsElement =
51 VlmZusammenUtil.buildStructuralElement(StructureElement.EntitlementPools, null);
53 entitlementPoolsElement.addSubElement(entitlementPoolElement);
55 SessionContext context = ZusammenUtil.createSessionContext();
56 Id itemId = new Id(entitlementPool.getVendorLicenseModelId());
57 Optional<Element> savedElement = zusammenAdaptor.saveElement(context, new ElementContext(itemId,
58 VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor)),
59 entitlementPoolsElement, "Create entitlement pool");
61 savedElement.ifPresent(element -> entitlementPool
62 .setId(element.getSubElements().iterator().next().getElementId().getValue()));
66 public void update(EntitlementPoolEntity entitlementPool) {
67 ZusammenElement entitlmentpoolElement =
68 buildEntitlementPoolElement(entitlementPool, Action.UPDATE);
70 SessionContext context = ZusammenUtil.createSessionContext();
71 Id itemId = new Id(entitlementPool.getVendorLicenseModelId());
72 ElementContext elementContext = new ElementContext(itemId,
73 VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor));
75 Optional<ElementInfo> epFromDb = zusammenAdaptor.getElementInfo(context, elementContext,
76 new Id(entitlementPool.getId()));
78 if (epFromDb.isPresent()) {
79 if (entitlmentpoolElement.getRelations() == null) {
80 entitlmentpoolElement.setRelations(new ArrayList<>());
82 if (epFromDb.get().getRelations() != null && epFromDb.get().getRelations().size() > 0) {
83 entitlmentpoolElement.getRelations().addAll(epFromDb.get().getRelations());
87 zusammenAdaptor.saveElement(context,elementContext, entitlmentpoolElement,
88 String.format("Update entitlement pool with id %s", entitlementPool.getId()));
92 public EntitlementPoolEntity get(EntitlementPoolEntity entitlementPool) {
93 SessionContext context = ZusammenUtil.createSessionContext();
94 Id itemId = new Id(entitlementPool.getVendorLicenseModelId());
95 ElementContext elementContext = new ElementContext(itemId,
96 VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor),
97 VlmZusammenUtil.getVersionTag(entitlementPool.getVersion()));
99 return zusammenAdaptor.getElementInfo(context, elementContext, new Id(entitlementPool.getId()))
100 .map(elementInfo -> mapElementInfoToEntitlementPool(
101 entitlementPool.getVendorLicenseModelId(), entitlementPool.getVersion(), elementInfo))
106 public void delete(EntitlementPoolEntity entitlementPool) {
107 SessionContext context = ZusammenUtil.createSessionContext();
108 ZusammenElement zusammenElement = new ZusammenElement();
109 zusammenElement.setAction(Action.DELETE);
110 zusammenElement.setElementId(new Id(entitlementPool.getId()));
112 Id itemId = new Id(entitlementPool.getVendorLicenseModelId());
113 ElementContext elementContext =
114 new ElementContext(itemId,
115 VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor));
116 zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
117 "delete entitlement pool. id:" + entitlementPool.getId() + ".");
121 public Collection<EntitlementPoolEntity> list(EntitlementPoolEntity entitlementPool) {
122 SessionContext context = ZusammenUtil.createSessionContext();
123 Id itemId = new Id(entitlementPool.getVendorLicenseModelId());
124 ElementContext elementContext = new ElementContext(itemId,
125 VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor),
126 VlmZusammenUtil.getVersionTag(entitlementPool.getVersion()));
128 return zusammenAdaptor
129 .listElementsByName(context, elementContext, null, StructureElement.EntitlementPools.name())
130 .stream().map(elementInfo -> mapElementInfoToEntitlementPool(
131 entitlementPool.getVendorLicenseModelId(), entitlementPool.getVersion(), elementInfo))
132 .collect(Collectors.toList());
136 public long count(EntitlementPoolEntity entitlementPool) {
137 SessionContext context = ZusammenUtil.createSessionContext();
138 Id itemId = new Id(entitlementPool.getVendorLicenseModelId());
139 ElementContext elementContext = new ElementContext(itemId,
140 VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor),
141 VlmZusammenUtil.getVersionTag(entitlementPool.getVersion()));
143 return zusammenAdaptor
144 .listElementsByName(context, elementContext, null, StructureElement.EntitlementPools.name())
149 public void removeReferencingFeatureGroup(EntitlementPoolEntity entitlementPool,
150 String referencingFeatureGroupId) {
151 SessionContext context = ZusammenUtil.createSessionContext();
152 Id itemId = new Id(entitlementPool.getVendorLicenseModelId());
153 ElementContext elementContext = new ElementContext(itemId,
154 VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor));
156 Optional<ElementInfo> elementInfo =
157 zusammenAdaptor.getElementInfo(context, elementContext, new Id(entitlementPool.getId()));
159 if (elementInfo.isPresent()) {
160 ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
161 zusammenElement.setAction(Action.UPDATE);
162 zusammenElement.setRelations(elementInfo.get().getRelations().stream()
163 .filter(relation -> !referencingFeatureGroupId
164 .equals(relation.getEdge2().getElementId().getValue()))
165 .collect(Collectors.toList()));
167 zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
168 "remove referencing feature group");
173 public void addReferencingFeatureGroup(EntitlementPoolEntity entitlementPool,
174 String referencingFeatureGroupId) {
175 SessionContext context = ZusammenUtil.createSessionContext();
176 Id itemId = new Id(entitlementPool.getVendorLicenseModelId());
177 ElementContext elementContext = new ElementContext(itemId,
178 VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor));
180 Optional<ElementInfo> elementInfo =
181 zusammenAdaptor.getElementInfo(context, elementContext, new Id(entitlementPool.getId()));
183 if (elementInfo.isPresent()) {
184 ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
185 zusammenElement.setAction(Action.UPDATE);
186 if (zusammenElement.getRelations() == null) {
187 zusammenElement.setRelations(new ArrayList<>());
189 zusammenElement.getRelations().add(VlmZusammenUtil
190 .createRelation(RelationType.EntitlmentPoolToReferencingFeatureGroup,
191 referencingFeatureGroupId));
193 .saveElement(context, elementContext, zusammenElement, "add referencing feature group");
198 public void deleteAll(EntitlementPoolEntity entitlementPool) {
202 private ZusammenElement buildEntitlementPoolElement(EntitlementPoolEntity entitlementPool,
205 ZusammenElement entitlementPoolElement = new ZusammenElement();
206 entitlementPoolElement.setAction(action);
207 if (entitlementPool.getId() != null) {
208 entitlementPoolElement.setElementId(new Id(entitlementPool.getId()));
210 Info info = new Info();
211 info.setName(entitlementPool.getName());
212 info.setDescription(entitlementPool.getDescription());
213 info.addProperty("version_uuid", entitlementPool.getVersionUuId());
214 info.addProperty("thresholdValue", entitlementPool.getThresholdValue());
215 info.addProperty("threshold_unit", entitlementPool.getThresholdUnit());
216 info.addProperty("increments", entitlementPool.getIncrements());
217 info.addProperty("operational_scope", entitlementPool.getOperationalScope());
218 info.addProperty("startDate", entitlementPool.getStartDate());
219 info.addProperty("expiryDate", entitlementPool.getExpiryDate());
220 entitlementPoolElement.setInfo(info);
222 if (entitlementPool.getReferencingFeatureGroups() != null
223 && entitlementPool.getReferencingFeatureGroups().size() > 0) {
224 entitlementPoolElement.setRelations(entitlementPool.getReferencingFeatureGroups().stream()
225 .map(rel -> VlmZusammenUtil
226 .createRelation(RelationType.EntitlmentPoolToReferencingFeatureGroup, rel))
227 .collect(Collectors.toList()));
229 return entitlementPoolElement;
232 private EntitlementPoolEntity mapElementInfoToEntitlementPool(String vlmId, Version version,
233 ElementInfo elementInfo) {
234 EntitlementPoolEntity entitlmentPool =
235 new EntitlementPoolEntity(vlmId, version, elementInfo.getId().getValue());
236 entitlmentPool.setName(elementInfo.getInfo().getName());
237 entitlmentPool.setDescription(elementInfo.getInfo().getDescription());
238 entitlmentPool.setVersionUuId(elementInfo.getInfo().getProperty("version_uuid"));
240 .setThresholdValue(elementInfo.getInfo().getProperty("thresholdValue") != null
241 ? VlmZusammenUtil.toInteger(elementInfo.getInfo().getProperty("thresholdValue")) : null);
243 Object threshold_unit = elementInfo.getInfo().getProperty("threshold_unit");
244 entitlmentPool.setThresholdUnit( threshold_unit != null ?
245 ThresholdUnit.valueOf(elementInfo.getInfo().getProperty("threshold_unit")) : null);
246 entitlmentPool.setIncrements(elementInfo.getInfo().getProperty("increments"));
247 entitlmentPool.setOperationalScope(getOperationalScopeMultiChoiceOrOther(
248 elementInfo.getInfo().getProperty("operational_scope")));
249 entitlmentPool.setStartDate(elementInfo.getInfo().getProperty("startDate"));
250 entitlmentPool.setExpiryDate(elementInfo.getInfo().getProperty("expiryDate"));
252 if (elementInfo.getRelations() != null && elementInfo.getRelations().size() > 0) {
254 .setReferencingFeatureGroups(elementInfo.getRelations().stream().map(relation -> relation
255 .getEdge2().getElementId().getValue()).collect(Collectors.toSet()));
257 return entitlmentPool;
260 private MultiChoiceOrOther<OperationalScope> getOperationalScopeMultiChoiceOrOther
263 if(operationalScope != null && !operationalScope.isEmpty()) {
264 Set<OperationalScope> choices = new HashSet<>();
265 ((List<String>) operationalScope.get("choices")).
266 forEach(choice -> choices.add(OperationalScope.valueOf(choice)));
268 return new MultiChoiceOrOther<>(choices, operationalScope.get("other")==null?null:
269 (String) operationalScope.get("other"));
275 public String getManufacturerReferenceNumber(EntitlementPoolEntity entitlementPoolEntity){
276 SessionContext context = ZusammenUtil.createSessionContext();
277 Id itemId = new Id(entitlementPoolEntity.getVendorLicenseModelId());
278 ElementContext elementContext = new ElementContext(itemId,
279 VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor),
280 VlmZusammenUtil.getVersionTag(entitlementPoolEntity.getVersion()));
281 Optional<ElementInfo> elementInfo1 = zusammenAdaptor.getElementInfo(context, elementContext, new Id(entitlementPoolEntity.getId()));
282 Map<String, Object> properties = elementInfo1.get().getInfo().getProperties();
283 String manufacturerReferenceNumber = null;
284 if(properties != null && properties.containsKey("manufacturerReferenceNumber") ) {
285 manufacturerReferenceNumber = (String)properties.get("manufacturerReferenceNumber");
287 return manufacturerReferenceNumber;