Added oparent to sdc main
[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 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.openecomp.sdc.vendorlicense.dao.impl.zusammen;
21
22 import com.amdocs.zusammen.adaptor.inbound.api.types.item.Element;
23 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ElementInfo;
24 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ZusammenElement;
25 import com.amdocs.zusammen.datatypes.Id;
26 import com.amdocs.zusammen.datatypes.SessionContext;
27 import com.amdocs.zusammen.datatypes.item.Action;
28 import com.amdocs.zusammen.datatypes.item.ElementContext;
29 import com.amdocs.zusammen.datatypes.item.Info;
30 import org.openecomp.core.zusammen.api.ZusammenAdaptor;
31 import org.openecomp.core.zusammen.api.ZusammenUtil;
32 import org.openecomp.sdc.datatypes.model.ElementType;
33 import org.openecomp.sdc.vendorlicense.dao.EntitlementPoolDao;
34 import org.openecomp.sdc.vendorlicense.dao.impl.zusammen.convertor.ElementToEntitlementPoolConvertor;
35 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
36 import org.openecomp.types.ElementPropertyName;
37
38 import java.util.ArrayList;
39 import java.util.Collection;
40 import java.util.Map;
41 import java.util.Optional;
42 import java.util.stream.Collectors;
43
44 import static org.openecomp.core.zusammen.api.ZusammenUtil.buildElement;
45
46 public class EntitlementPoolZusammenDaoImpl implements EntitlementPoolDao {
47
48   private ZusammenAdaptor zusammenAdaptor;
49
50   public EntitlementPoolZusammenDaoImpl(ZusammenAdaptor zusammenAdaptor) {
51     this.zusammenAdaptor = zusammenAdaptor;
52   }
53
54   @Override
55   public void registerVersioning(String versionableEntityType) {
56     //no need
57   }
58
59   @Override
60   public void create(EntitlementPoolEntity entitlementPool) {
61     ZusammenElement entitlementPoolElement =
62         buildEntitlementPoolElement(entitlementPool, Action.CREATE);
63
64     ZusammenElement entitlementPoolsElement =
65         ZusammenUtil.buildStructuralElement(ElementType.EntitlementPools, Action.IGNORE);
66
67     ZusammenElement limitsElement =
68         ZusammenUtil.buildStructuralElement(ElementType.Limits, Action.CREATE);
69
70     entitlementPoolElement.addSubElement(limitsElement);
71     entitlementPoolsElement.addSubElement(entitlementPoolElement);
72
73     SessionContext context = ZusammenUtil.createSessionContext();
74     Element epsSavedElement = zusammenAdaptor.saveElement(context,
75         new ElementContext(entitlementPool.getVendorLicenseModelId(),
76             entitlementPool.getVersion().getId()),
77         entitlementPoolsElement, "Create entitlement pool");
78
79     entitlementPool
80         .setId(epsSavedElement.getSubElements().iterator().next().getElementId().getValue());
81   }
82
83   @Override
84   public void update(EntitlementPoolEntity entitlementPool) {
85     ZusammenElement entitlmentpoolElement =
86         buildEntitlementPoolElement(entitlementPool, Action.UPDATE);
87
88     SessionContext context = ZusammenUtil.createSessionContext();
89     ElementContext elementContext = new ElementContext(entitlementPool.getVendorLicenseModelId(),
90         entitlementPool.getVersion().getId());
91
92     Optional<ElementInfo> epFromDb = zusammenAdaptor.getElementInfo(context, elementContext,
93         new Id(entitlementPool.getId()));
94
95     if (epFromDb.isPresent()) {
96       if (entitlmentpoolElement.getRelations() == null) {
97         entitlmentpoolElement.setRelations(new ArrayList<>());
98       }
99       if (epFromDb.get().getRelations() != null && !epFromDb.get().getRelations().isEmpty()) {
100         entitlmentpoolElement.getRelations().addAll(epFromDb.get().getRelations());
101       }
102     }
103
104     zusammenAdaptor.saveElement(context, elementContext, entitlmentpoolElement,
105         String.format("Update entitlement pool with id %s", entitlementPool.getId()));
106   }
107
108   @Override
109   public EntitlementPoolEntity get(EntitlementPoolEntity entitlementPool) {
110     SessionContext context = ZusammenUtil.createSessionContext();
111     ElementContext elementContext = new ElementContext(entitlementPool.getVendorLicenseModelId(),
112         entitlementPool.getVersion().getId());
113     ElementToEntitlementPoolConvertor convertor = new ElementToEntitlementPoolConvertor();
114     return zusammenAdaptor.getElementInfo(context, elementContext, new Id(entitlementPool.getId()))
115         .map(elementInfo -> {
116           EntitlementPoolEntity entity = convertor.convert(elementInfo);
117           entity.setVendorLicenseModelId(entitlementPool.getVendorLicenseModelId());
118           entity.setVersion(entitlementPool.getVersion());
119           return entity;
120         })
121         .orElse(null);
122   }
123
124   @Override
125   public void delete(EntitlementPoolEntity entitlementPool) {
126     ZusammenElement zusammenElement = buildElement(new Id(entitlementPool.getId()), Action.DELETE);
127
128     SessionContext context = ZusammenUtil.createSessionContext();
129     ElementContext elementContext = new ElementContext(entitlementPool.getVendorLicenseModelId(),
130         entitlementPool.getVersion().getId());
131     zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
132         "delete entitlement pool. id:" + entitlementPool.getId() + ".");
133   }
134
135   @Override
136   public Collection<EntitlementPoolEntity> list(EntitlementPoolEntity entitlementPool) {
137     SessionContext context = ZusammenUtil.createSessionContext();
138     ElementContext elementContext = new ElementContext(entitlementPool.getVendorLicenseModelId(),
139         entitlementPool.getVersion().getId());
140     ElementToEntitlementPoolConvertor convertor = new ElementToEntitlementPoolConvertor();
141     return zusammenAdaptor
142         .listElementsByName(context, elementContext, null, ElementType.EntitlementPools.name())
143         .stream().map(elementInfo -> {
144           EntitlementPoolEntity entity = convertor.convert(elementInfo);
145           entity.setVendorLicenseModelId(entitlementPool.getVendorLicenseModelId());
146           entity.setVersion(entitlementPool.getVersion());
147           return entity;
148         }).collect(Collectors.toList());
149   }
150
151   @Override
152   public long count(EntitlementPoolEntity entitlementPool) {
153     SessionContext context = ZusammenUtil.createSessionContext();
154     ElementContext elementContext = new ElementContext(entitlementPool.getVendorLicenseModelId(),
155         entitlementPool.getVersion().getId());
156
157     return zusammenAdaptor
158         .listElementsByName(context, elementContext, null, ElementType.EntitlementPools.name())
159         .size();
160   }
161
162   @Override
163   public void removeReferencingFeatureGroup(EntitlementPoolEntity entitlementPool,
164                                             String referencingFeatureGroupId) {
165     SessionContext context = ZusammenUtil.createSessionContext();
166     ElementContext elementContext = new ElementContext(entitlementPool.getVendorLicenseModelId(),
167         entitlementPool.getVersion().getId());
168
169     Optional<ElementInfo> elementInfo =
170         zusammenAdaptor.getElementInfo(context, elementContext, new Id(entitlementPool.getId()));
171
172     if (elementInfo.isPresent()) {
173       ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
174       zusammenElement.setAction(Action.UPDATE);
175       zusammenElement.setRelations(elementInfo.get().getRelations().stream()
176           .filter(relation -> !referencingFeatureGroupId
177               .equals(relation.getEdge2().getElementId().getValue()))
178           .collect(Collectors.toList()));
179
180       zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
181           "remove referencing feature group");
182     }
183   }
184
185   @Override
186   public void addReferencingFeatureGroup(EntitlementPoolEntity entitlementPool,
187                                          String referencingFeatureGroupId) {
188     SessionContext context = ZusammenUtil.createSessionContext();
189     ElementContext elementContext = new ElementContext(entitlementPool.getVendorLicenseModelId(),
190         entitlementPool.getVersion().getId());
191
192     Optional<ElementInfo> elementInfo =
193         zusammenAdaptor.getElementInfo(context, elementContext, new Id(entitlementPool.getId()));
194
195     if (elementInfo.isPresent()) {
196       ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
197       zusammenElement.setAction(Action.UPDATE);
198       if (zusammenElement.getRelations() == null) {
199         zusammenElement.setRelations(new ArrayList<>());
200       }
201       zusammenElement.getRelations().add(VlmZusammenUtil
202           .createRelation(RelationType.EntitlmentPoolToReferencingFeatureGroup,
203               referencingFeatureGroupId));
204       zusammenAdaptor
205           .saveElement(context, elementContext, zusammenElement, "add referencing feature group");
206     }
207   }
208
209   @Override
210   public void deleteAll(EntitlementPoolEntity entitlementPool) {
211     //not supported
212   }
213
214   @Override
215   public String getManufacturerReferenceNumber(EntitlementPoolEntity entitlementPoolEntity) {
216     SessionContext context = ZusammenUtil.createSessionContext();
217     ElementContext elementContext =
218         new ElementContext(entitlementPoolEntity.getVendorLicenseModelId(),
219             entitlementPoolEntity.getVersion().getId());
220
221     Optional<ElementInfo> elementInfo1 = zusammenAdaptor
222         .getElementInfo(context, elementContext, new Id(entitlementPoolEntity.getId()));
223     Map<String, Object> properties = elementInfo1.get().getInfo().getProperties();
224     String manufacturerReferenceNumber = null;
225     if (properties != null && properties.containsKey("manufacturerReferenceNumber")) {
226       manufacturerReferenceNumber = (String) properties.get("manufacturerReferenceNumber");
227     }
228     return manufacturerReferenceNumber;
229   }
230
231   private ZusammenElement buildEntitlementPoolElement(EntitlementPoolEntity entitlementPool,
232                                                       Action action) {
233     ZusammenElement entitlementPoolElement =
234         buildElement(entitlementPool.getId() == null ? null : new Id(entitlementPool.getId()),
235             action);
236     Info info = new Info();
237     info.setName(entitlementPool.getName());
238     info.setDescription(entitlementPool.getDescription());
239     info.addProperty(ElementPropertyName.elementType.name(), ElementType.EntitlementPool);
240     info.addProperty("version_uuid", entitlementPool.getVersionUuId());
241     info.addProperty("thresholdValue", entitlementPool.getThresholdValue());
242     info.addProperty("threshold_unit", entitlementPool.getThresholdUnit());
243     info.addProperty("increments", entitlementPool.getIncrements());
244     info.addProperty("operational_scope", entitlementPool.getOperationalScope());
245     info.addProperty("startDate", entitlementPool.getStartDate());
246     info.addProperty("expiryDate", entitlementPool.getExpiryDate());
247     info.addProperty("manufacturerReferenceNumber", entitlementPool.getManufacturerReferenceNumber());
248     entitlementPoolElement.setInfo(info);
249
250     if (entitlementPool.getReferencingFeatureGroups() != null
251         && !entitlementPool.getReferencingFeatureGroups().isEmpty()) {
252       entitlementPoolElement.setRelations(entitlementPool.getReferencingFeatureGroups().stream()
253           .map(rel -> VlmZusammenUtil
254               .createRelation(RelationType.EntitlmentPoolToReferencingFeatureGroup, rel))
255           .collect(Collectors.toList()));
256     }
257     return entitlementPoolElement;
258   }
259
260
261 }