2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.openecomp.sdc.vendorlicense.dao.impl.zusammen;
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;
38 import java.util.ArrayList;
39 import java.util.Collection;
41 import java.util.Optional;
42 import java.util.stream.Collectors;
44 import static org.openecomp.core.zusammen.api.ZusammenUtil.buildElement;
46 public class EntitlementPoolZusammenDaoImpl implements EntitlementPoolDao {
48 private ZusammenAdaptor zusammenAdaptor;
50 public EntitlementPoolZusammenDaoImpl(ZusammenAdaptor zusammenAdaptor) {
51 this.zusammenAdaptor = zusammenAdaptor;
55 public void registerVersioning(String versionableEntityType) {
60 public void create(EntitlementPoolEntity entitlementPool) {
61 ZusammenElement entitlementPoolElement =
62 buildEntitlementPoolElement(entitlementPool, Action.CREATE);
64 ZusammenElement entitlementPoolsElement =
65 ZusammenUtil.buildStructuralElement(ElementType.EntitlementPools, Action.IGNORE);
67 ZusammenElement limitsElement =
68 ZusammenUtil.buildStructuralElement(ElementType.Limits, Action.CREATE);
70 entitlementPoolElement.addSubElement(limitsElement);
71 entitlementPoolsElement.addSubElement(entitlementPoolElement);
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");
80 .setId(epsSavedElement.getSubElements().iterator().next().getElementId().getValue());
84 public void update(EntitlementPoolEntity entitlementPool) {
85 ZusammenElement entitlmentpoolElement =
86 buildEntitlementPoolElement(entitlementPool, Action.UPDATE);
88 SessionContext context = ZusammenUtil.createSessionContext();
89 ElementContext elementContext = new ElementContext(entitlementPool.getVendorLicenseModelId(),
90 entitlementPool.getVersion().getId());
92 Optional<ElementInfo> epFromDb = zusammenAdaptor.getElementInfo(context, elementContext,
93 new Id(entitlementPool.getId()));
95 if (epFromDb.isPresent()) {
96 if (entitlmentpoolElement.getRelations() == null) {
97 entitlmentpoolElement.setRelations(new ArrayList<>());
99 if (epFromDb.get().getRelations() != null && !epFromDb.get().getRelations().isEmpty()) {
100 entitlmentpoolElement.getRelations().addAll(epFromDb.get().getRelations());
104 zusammenAdaptor.saveElement(context, elementContext, entitlmentpoolElement,
105 String.format("Update entitlement pool with id %s", entitlementPool.getId()));
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());
125 public void delete(EntitlementPoolEntity entitlementPool) {
126 ZusammenElement zusammenElement = buildElement(new Id(entitlementPool.getId()), Action.DELETE);
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() + ".");
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());
148 }).collect(Collectors.toList());
152 public long count(EntitlementPoolEntity entitlementPool) {
153 SessionContext context = ZusammenUtil.createSessionContext();
154 ElementContext elementContext = new ElementContext(entitlementPool.getVendorLicenseModelId(),
155 entitlementPool.getVersion().getId());
157 return zusammenAdaptor
158 .listElementsByName(context, elementContext, null, ElementType.EntitlementPools.name())
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());
169 Optional<ElementInfo> elementInfo =
170 zusammenAdaptor.getElementInfo(context, elementContext, new Id(entitlementPool.getId()));
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()));
180 zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
181 "remove referencing feature group");
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());
192 Optional<ElementInfo> elementInfo =
193 zusammenAdaptor.getElementInfo(context, elementContext, new Id(entitlementPool.getId()));
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<>());
201 zusammenElement.getRelations().add(VlmZusammenUtil
202 .createRelation(RelationType.EntitlmentPoolToReferencingFeatureGroup,
203 referencingFeatureGroupId));
205 .saveElement(context, elementContext, zusammenElement, "add referencing feature group");
210 public void deleteAll(EntitlementPoolEntity entitlementPool) {
215 public String getManufacturerReferenceNumber(EntitlementPoolEntity entitlementPoolEntity) {
216 SessionContext context = ZusammenUtil.createSessionContext();
217 ElementContext elementContext =
218 new ElementContext(entitlementPoolEntity.getVendorLicenseModelId(),
219 entitlementPoolEntity.getVersion().getId());
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");
228 return manufacturerReferenceNumber;
231 private ZusammenElement buildEntitlementPoolElement(EntitlementPoolEntity entitlementPool,
233 ZusammenElement entitlementPoolElement =
234 buildElement(entitlementPool.getId() == null ? null : new Id(entitlementPool.getId()),
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);
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()));
257 return entitlementPoolElement;