Add collaboration feature
[sdc.git] / openecomp-be / lib / openecomp-sdc-vendor-license-lib / openecomp-sdc-vendor-license-core / src / main / java / org / openecomp / sdc / vendorlicense / dao / impl / zusammen / EntitlementPoolZusammenDaoImpl.java
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.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;
18
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.Map;
22 import java.util.Optional;
23 import java.util.stream.Collectors;
24
25 import static org.openecomp.core.zusammen.api.ZusammenUtil.buildElement;
26
27 public class EntitlementPoolZusammenDaoImpl implements EntitlementPoolDao {
28
29   private ZusammenAdaptor zusammenAdaptor;
30
31   public EntitlementPoolZusammenDaoImpl(ZusammenAdaptor zusammenAdaptor) {
32     this.zusammenAdaptor = zusammenAdaptor;
33   }
34
35   @Override
36   public void registerVersioning(String versionableEntityType) {
37     //no need
38   }
39
40   @Override
41   public void create(EntitlementPoolEntity entitlementPool) {
42     ZusammenElement entitlementPoolElement =
43         buildEntitlementPoolElement(entitlementPool, Action.CREATE);
44
45     ZusammenElement entitlementPoolsElement =
46         ZusammenUtil.buildStructuralElement(ElementType.EntitlementPools, Action.IGNORE);
47
48     ZusammenElement limitsElement =
49         ZusammenUtil.buildStructuralElement(ElementType.Limits, Action.CREATE);
50
51     entitlementPoolElement.addSubElement(limitsElement);
52     entitlementPoolsElement.addSubElement(entitlementPoolElement);
53
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");
59
60     entitlementPool
61         .setId(epsSavedElement.getSubElements().iterator().next().getElementId().getValue());
62   }
63
64   @Override
65   public void update(EntitlementPoolEntity entitlementPool) {
66     ZusammenElement entitlmentpoolElement =
67         buildEntitlementPoolElement(entitlementPool, Action.UPDATE);
68
69     SessionContext context = ZusammenUtil.createSessionContext();
70     ElementContext elementContext = new ElementContext(entitlementPool.getVendorLicenseModelId(),
71         entitlementPool.getVersion().getId());
72
73     Optional<ElementInfo> epFromDb = zusammenAdaptor.getElementInfo(context, elementContext,
74         new Id(entitlementPool.getId()));
75
76     if (epFromDb.isPresent()) {
77       if (entitlmentpoolElement.getRelations() == null) {
78         entitlmentpoolElement.setRelations(new ArrayList<>());
79       }
80       if (epFromDb.get().getRelations() != null && epFromDb.get().getRelations().size() > 0) {
81         entitlmentpoolElement.getRelations().addAll(epFromDb.get().getRelations());
82       }
83     }
84
85     zusammenAdaptor.saveElement(context, elementContext, entitlmentpoolElement,
86         String.format("Update entitlement pool with id %s", entitlementPool.getId()));
87   }
88
89   @Override
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()))
96         .map(elementInfo -> {
97           EntitlementPoolEntity entity = convertor.convert(elementInfo);
98           entity.setVendorLicenseModelId(entitlementPool.getVendorLicenseModelId());
99           entity.setVersion(entitlementPool.getVersion());
100           return entity;
101         })
102         .orElse(null);
103   }
104
105   @Override
106   public void delete(EntitlementPoolEntity entitlementPool) {
107     ZusammenElement zusammenElement = buildElement(new Id(entitlementPool.getId()), Action.DELETE);
108
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() + ".");
114   }
115
116   @Override
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());
128           return entity;
129         }).collect(Collectors.toList());
130   }
131
132   @Override
133   public long count(EntitlementPoolEntity entitlementPool) {
134     SessionContext context = ZusammenUtil.createSessionContext();
135     ElementContext elementContext = new ElementContext(entitlementPool.getVendorLicenseModelId(),
136         entitlementPool.getVersion().getId());
137
138     return zusammenAdaptor
139         .listElementsByName(context, elementContext, null, ElementType.EntitlementPools.name())
140         .size();
141   }
142
143   @Override
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());
149
150     Optional<ElementInfo> elementInfo =
151         zusammenAdaptor.getElementInfo(context, elementContext, new Id(entitlementPool.getId()));
152
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()));
160
161       zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
162           "remove referencing feature group");
163     }
164   }
165
166   @Override
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());
172
173     Optional<ElementInfo> elementInfo =
174         zusammenAdaptor.getElementInfo(context, elementContext, new Id(entitlementPool.getId()));
175
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<>());
181       }
182       zusammenElement.getRelations().add(VlmZusammenUtil
183           .createRelation(RelationType.EntitlmentPoolToReferencingFeatureGroup,
184               referencingFeatureGroupId));
185       zusammenAdaptor
186           .saveElement(context, elementContext, zusammenElement, "add referencing feature group");
187     }
188   }
189
190   @Override
191   public void deleteAll(EntitlementPoolEntity entitlementPool) {
192     //not supported
193   }
194
195   @Override
196   public String getManufacturerReferenceNumber(EntitlementPoolEntity entitlementPoolEntity) {
197     SessionContext context = ZusammenUtil.createSessionContext();
198     ElementContext elementContext =
199         new ElementContext(entitlementPoolEntity.getVendorLicenseModelId(),
200             entitlementPoolEntity.getVersion().getId());
201
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");
208     }
209     return manufacturerReferenceNumber;
210   }
211
212   private ZusammenElement buildEntitlementPoolElement(EntitlementPoolEntity entitlementPool,
213                                                       Action action) {
214     ZusammenElement entitlementPoolElement =
215         buildElement(entitlementPool.getId() == null ? null : new Id(entitlementPool.getId()),
216             action);
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);
229
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()));
236     }
237     return entitlementPoolElement;
238   }
239
240
241 }