b880c67cbca1ce7e0c132e15167a65b076f60b1c
[sdc.git] /
1 package org.openecomp.sdc.vendorlicense.dao.impl.zusammen;
2
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;
19
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Optional;
26 import java.util.Set;
27 import java.util.stream.Collectors;
28
29 /**
30  * Created by ayalaben on 3/28/2017.
31  */
32 public class EntitlementPoolZusammenDaoImpl implements EntitlementPoolDao {
33
34   private ZusammenAdaptor zusammenAdaptor;
35
36   public EntitlementPoolZusammenDaoImpl(ZusammenAdaptor zusammenAdaptor) {
37     this.zusammenAdaptor = zusammenAdaptor;
38   }
39
40   @Override
41   public void registerVersioning(String versionableEntityType) {
42     //no need
43   }
44
45   @Override
46   public void create(EntitlementPoolEntity entitlementPool) {
47     ZusammenElement entitlementPoolElement =
48         buildEntitlementPoolElement(entitlementPool, Action.CREATE);
49
50     ZusammenElement entitlementPoolsElement =
51         VlmZusammenUtil.buildStructuralElement(StructureElement.EntitlementPools, null);
52
53     entitlementPoolsElement.addSubElement(entitlementPoolElement);
54
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");
60
61     savedElement.ifPresent(element -> entitlementPool
62         .setId(element.getSubElements().iterator().next().getElementId().getValue()));
63   }
64
65   @Override
66   public void update(EntitlementPoolEntity entitlementPool) {
67     ZusammenElement entitlmentpoolElement =
68         buildEntitlementPoolElement(entitlementPool, Action.UPDATE);
69
70     SessionContext context = ZusammenUtil.createSessionContext();
71     Id itemId = new Id(entitlementPool.getVendorLicenseModelId());
72     ElementContext elementContext =  new ElementContext(itemId,
73         VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor));
74
75     Optional<ElementInfo> epFromDb = zusammenAdaptor.getElementInfo(context, elementContext,
76         new Id(entitlementPool.getId()));
77
78     if (epFromDb.isPresent()) {
79       if (entitlmentpoolElement.getRelations() == null) {
80         entitlmentpoolElement.setRelations(new ArrayList<>());
81       }
82       if (epFromDb.get().getRelations() != null && epFromDb.get().getRelations().size() > 0) {
83         entitlmentpoolElement.getRelations().addAll(epFromDb.get().getRelations());
84       }
85     }
86
87     zusammenAdaptor.saveElement(context,elementContext, entitlmentpoolElement,
88         String.format("Update entitlement pool with id %s", entitlementPool.getId()));
89   }
90
91   @Override
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()));
98
99     return zusammenAdaptor.getElementInfo(context, elementContext, new Id(entitlementPool.getId()))
100         .map(elementInfo -> mapElementInfoToEntitlementPool(
101             entitlementPool.getVendorLicenseModelId(), entitlementPool.getVersion(), elementInfo))
102         .orElse(null);
103   }
104
105   @Override
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()));
111
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() + ".");
118   }
119
120   @Override
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()));
127
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());
133   }
134
135   @Override
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()));
142
143     return zusammenAdaptor
144         .listElementsByName(context, elementContext, null, StructureElement.EntitlementPools.name())
145         .size();
146   }
147
148   @Override
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));
155
156     Optional<ElementInfo> elementInfo =
157         zusammenAdaptor.getElementInfo(context, elementContext, new Id(entitlementPool.getId()));
158
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()));
166
167       zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
168           "remove referencing feature group");
169     }
170   }
171
172   @Override
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));
179
180     Optional<ElementInfo> elementInfo =
181         zusammenAdaptor.getElementInfo(context, elementContext, new Id(entitlementPool.getId()));
182
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<>());
188       }
189       zusammenElement.getRelations().add(VlmZusammenUtil
190           .createRelation(RelationType.EntitlmentPoolToReferencingFeatureGroup,
191               referencingFeatureGroupId));
192       zusammenAdaptor
193           .saveElement(context, elementContext, zusammenElement, "add referencing feature group");
194     }
195   }
196
197   @Override
198   public void deleteAll(EntitlementPoolEntity entitlementPool) {
199     //not supported
200   }
201
202   private ZusammenElement buildEntitlementPoolElement(EntitlementPoolEntity entitlementPool,
203                                                       Action action) {
204
205     ZusammenElement entitlementPoolElement = new ZusammenElement();
206     entitlementPoolElement.setAction(action);
207     if (entitlementPool.getId() != null) {
208       entitlementPoolElement.setElementId(new Id(entitlementPool.getId()));
209     }
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);
221
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()));
228     }
229     return entitlementPoolElement;
230   }
231
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"));
239     entitlmentPool
240         .setThresholdValue(elementInfo.getInfo().getProperty("thresholdValue") != null
241                 ? VlmZusammenUtil.toInteger(elementInfo.getInfo().getProperty("thresholdValue")) : null);
242
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"));
251
252     if (elementInfo.getRelations() != null && elementInfo.getRelations().size() > 0) {
253       entitlmentPool
254           .setReferencingFeatureGroups(elementInfo.getRelations().stream().map(relation -> relation
255               .getEdge2().getElementId().getValue()).collect(Collectors.toSet()));
256     }
257     return entitlmentPool;
258   }
259
260   private MultiChoiceOrOther<OperationalScope> getOperationalScopeMultiChoiceOrOther
261       (Map<String, Object>
262            operationalScope) {
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)));
267
268       return new MultiChoiceOrOther<>(choices, operationalScope.get("other")==null?null:
269           (String) operationalScope.get("other"));
270     }
271     return null;
272   }
273
274   @Override
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");
286     }
287     return manufacturerReferenceNumber;
288   }
289
290 }