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.datatypes.model.ElementType;
14 import org.openecomp.sdc.vendorlicense.dao.EntitlementPoolDao;
15 import org.openecomp.sdc.vendorlicense.dao.impl.zusammen.convertor.ElementToEntitlementPoolConvertor;
16 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
17 import org.openecomp.types.ElementPropertyName;
19 import java.util.ArrayList;
20 import java.util.Collection;
22 import java.util.Optional;
23 import java.util.stream.Collectors;
25 import static org.openecomp.core.zusammen.api.ZusammenUtil.buildElement;
27 public class EntitlementPoolZusammenDaoImpl implements EntitlementPoolDao {
29 private ZusammenAdaptor zusammenAdaptor;
31 public EntitlementPoolZusammenDaoImpl(ZusammenAdaptor zusammenAdaptor) {
32 this.zusammenAdaptor = zusammenAdaptor;
36 public void registerVersioning(String versionableEntityType) {
41 public void create(EntitlementPoolEntity entitlementPool) {
42 ZusammenElement entitlementPoolElement =
43 buildEntitlementPoolElement(entitlementPool, Action.CREATE);
45 ZusammenElement entitlementPoolsElement =
46 ZusammenUtil.buildStructuralElement(ElementType.EntitlementPools, Action.IGNORE);
48 ZusammenElement limitsElement =
49 ZusammenUtil.buildStructuralElement(ElementType.Limits, Action.CREATE);
51 entitlementPoolElement.addSubElement(limitsElement);
52 entitlementPoolsElement.addSubElement(entitlementPoolElement);
54 SessionContext context = ZusammenUtil.createSessionContext();
55 Element epsSavedElement = zusammenAdaptor.saveElement(context,
56 new ElementContext(entitlementPool.getVendorLicenseModelId(),
57 entitlementPool.getVersion().getId()),
58 entitlementPoolsElement, "Create entitlement pool");
61 .setId(epsSavedElement.getSubElements().iterator().next().getElementId().getValue());
65 public void update(EntitlementPoolEntity entitlementPool) {
66 ZusammenElement entitlmentpoolElement =
67 buildEntitlementPoolElement(entitlementPool, Action.UPDATE);
69 SessionContext context = ZusammenUtil.createSessionContext();
70 ElementContext elementContext = new ElementContext(entitlementPool.getVendorLicenseModelId(),
71 entitlementPool.getVersion().getId());
73 Optional<ElementInfo> epFromDb = zusammenAdaptor.getElementInfo(context, elementContext,
74 new Id(entitlementPool.getId()));
76 if (epFromDb.isPresent()) {
77 if (entitlmentpoolElement.getRelations() == null) {
78 entitlmentpoolElement.setRelations(new ArrayList<>());
80 if (epFromDb.get().getRelations() != null && epFromDb.get().getRelations().size() > 0) {
81 entitlmentpoolElement.getRelations().addAll(epFromDb.get().getRelations());
85 zusammenAdaptor.saveElement(context, elementContext, entitlmentpoolElement,
86 String.format("Update entitlement pool with id %s", entitlementPool.getId()));
90 public EntitlementPoolEntity get(EntitlementPoolEntity entitlementPool) {
91 SessionContext context = ZusammenUtil.createSessionContext();
92 ElementContext elementContext = new ElementContext(entitlementPool.getVendorLicenseModelId(),
93 entitlementPool.getVersion().getId());
94 ElementToEntitlementPoolConvertor convertor = new ElementToEntitlementPoolConvertor();
95 return zusammenAdaptor.getElementInfo(context, elementContext, new Id(entitlementPool.getId()))
97 EntitlementPoolEntity entity = convertor.convert(elementInfo);
98 entity.setVendorLicenseModelId(entitlementPool.getVendorLicenseModelId());
99 entity.setVersion(entitlementPool.getVersion());
106 public void delete(EntitlementPoolEntity entitlementPool) {
107 ZusammenElement zusammenElement = buildElement(new Id(entitlementPool.getId()), Action.DELETE);
109 SessionContext context = ZusammenUtil.createSessionContext();
110 ElementContext elementContext = new ElementContext(entitlementPool.getVendorLicenseModelId(),
111 entitlementPool.getVersion().getId());
112 zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
113 "delete entitlement pool. id:" + entitlementPool.getId() + ".");
117 public Collection<EntitlementPoolEntity> list(EntitlementPoolEntity entitlementPool) {
118 SessionContext context = ZusammenUtil.createSessionContext();
119 ElementContext elementContext = new ElementContext(entitlementPool.getVendorLicenseModelId(),
120 entitlementPool.getVersion().getId());
121 ElementToEntitlementPoolConvertor convertor = new ElementToEntitlementPoolConvertor();
122 return zusammenAdaptor
123 .listElementsByName(context, elementContext, null, ElementType.EntitlementPools.name())
124 .stream().map(elementInfo -> {
125 EntitlementPoolEntity entity = convertor.convert(elementInfo);
126 entity.setVendorLicenseModelId(entitlementPool.getVendorLicenseModelId());
127 entity.setVersion(entitlementPool.getVersion());
129 }).collect(Collectors.toList());
133 public long count(EntitlementPoolEntity entitlementPool) {
134 SessionContext context = ZusammenUtil.createSessionContext();
135 ElementContext elementContext = new ElementContext(entitlementPool.getVendorLicenseModelId(),
136 entitlementPool.getVersion().getId());
138 return zusammenAdaptor
139 .listElementsByName(context, elementContext, null, ElementType.EntitlementPools.name())
144 public void removeReferencingFeatureGroup(EntitlementPoolEntity entitlementPool,
145 String referencingFeatureGroupId) {
146 SessionContext context = ZusammenUtil.createSessionContext();
147 ElementContext elementContext = new ElementContext(entitlementPool.getVendorLicenseModelId(),
148 entitlementPool.getVersion().getId());
150 Optional<ElementInfo> elementInfo =
151 zusammenAdaptor.getElementInfo(context, elementContext, new Id(entitlementPool.getId()));
153 if (elementInfo.isPresent()) {
154 ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
155 zusammenElement.setAction(Action.UPDATE);
156 zusammenElement.setRelations(elementInfo.get().getRelations().stream()
157 .filter(relation -> !referencingFeatureGroupId
158 .equals(relation.getEdge2().getElementId().getValue()))
159 .collect(Collectors.toList()));
161 zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
162 "remove referencing feature group");
167 public void addReferencingFeatureGroup(EntitlementPoolEntity entitlementPool,
168 String referencingFeatureGroupId) {
169 SessionContext context = ZusammenUtil.createSessionContext();
170 ElementContext elementContext = new ElementContext(entitlementPool.getVendorLicenseModelId(),
171 entitlementPool.getVersion().getId());
173 Optional<ElementInfo> elementInfo =
174 zusammenAdaptor.getElementInfo(context, elementContext, new Id(entitlementPool.getId()));
176 if (elementInfo.isPresent()) {
177 ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
178 zusammenElement.setAction(Action.UPDATE);
179 if (zusammenElement.getRelations() == null) {
180 zusammenElement.setRelations(new ArrayList<>());
182 zusammenElement.getRelations().add(VlmZusammenUtil
183 .createRelation(RelationType.EntitlmentPoolToReferencingFeatureGroup,
184 referencingFeatureGroupId));
186 .saveElement(context, elementContext, zusammenElement, "add referencing feature group");
191 public void deleteAll(EntitlementPoolEntity entitlementPool) {
196 public String getManufacturerReferenceNumber(EntitlementPoolEntity entitlementPoolEntity) {
197 SessionContext context = ZusammenUtil.createSessionContext();
198 ElementContext elementContext =
199 new ElementContext(entitlementPoolEntity.getVendorLicenseModelId(),
200 entitlementPoolEntity.getVersion().getId());
202 Optional<ElementInfo> elementInfo1 = zusammenAdaptor
203 .getElementInfo(context, elementContext, new Id(entitlementPoolEntity.getId()));
204 Map<String, Object> properties = elementInfo1.get().getInfo().getProperties();
205 String manufacturerReferenceNumber = null;
206 if (properties != null && properties.containsKey("manufacturerReferenceNumber")) {
207 manufacturerReferenceNumber = (String) properties.get("manufacturerReferenceNumber");
209 return manufacturerReferenceNumber;
212 private ZusammenElement buildEntitlementPoolElement(EntitlementPoolEntity entitlementPool,
214 ZusammenElement entitlementPoolElement =
215 buildElement(entitlementPool.getId() == null ? null : new Id(entitlementPool.getId()),
217 Info info = new Info();
218 info.setName(entitlementPool.getName());
219 info.setDescription(entitlementPool.getDescription());
220 info.addProperty(ElementPropertyName.elementType.name(), ElementType.EntitlementPool);
221 info.addProperty("version_uuid", entitlementPool.getVersionUuId());
222 info.addProperty("thresholdValue", entitlementPool.getThresholdValue());
223 info.addProperty("threshold_unit", entitlementPool.getThresholdUnit());
224 info.addProperty("increments", entitlementPool.getIncrements());
225 info.addProperty("operational_scope", entitlementPool.getOperationalScope());
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;