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 static org.openecomp.core.zusammen.api.ZusammenUtil.buildElement;
24 import com.amdocs.zusammen.adaptor.inbound.api.types.item.Element;
25 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ElementInfo;
26 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ZusammenElement;
27 import com.amdocs.zusammen.datatypes.Id;
28 import com.amdocs.zusammen.datatypes.SessionContext;
29 import com.amdocs.zusammen.datatypes.item.Action;
30 import com.amdocs.zusammen.datatypes.item.ElementContext;
31 import com.amdocs.zusammen.datatypes.item.Info;
32 import java.util.ArrayList;
33 import java.util.Collection;
35 import java.util.Optional;
36 import java.util.stream.Collectors;
37 import org.openecomp.core.zusammen.api.ZusammenAdaptor;
38 import org.openecomp.core.zusammen.api.ZusammenUtil;
39 import org.openecomp.sdc.datatypes.model.ElementType;
40 import org.openecomp.sdc.vendorlicense.dao.EntitlementPoolDao;
41 import org.openecomp.sdc.vendorlicense.dao.impl.zusammen.convertor.ElementToEntitlementPoolConvertor;
42 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
43 import org.openecomp.types.ElementPropertyName;
45 public class EntitlementPoolZusammenDaoImpl implements EntitlementPoolDao {
47 private ZusammenAdaptor zusammenAdaptor;
49 public EntitlementPoolZusammenDaoImpl(ZusammenAdaptor zusammenAdaptor) {
50 this.zusammenAdaptor = zusammenAdaptor;
54 public void registerVersioning(String versionableEntityType) {
59 public void create(EntitlementPoolEntity entitlementPool) {
60 ZusammenElement entitlementPoolElement = buildEntitlementPoolElement(entitlementPool, Action.CREATE);
61 ZusammenElement entitlementPoolsElement = ZusammenUtil.buildStructuralElement(ElementType.EntitlementPools, Action.IGNORE);
62 ZusammenElement limitsElement = ZusammenUtil.buildStructuralElement(ElementType.Limits, Action.CREATE);
63 entitlementPoolElement.addSubElement(limitsElement);
64 entitlementPoolsElement.addSubElement(entitlementPoolElement);
65 SessionContext context = ZusammenUtil.createSessionContext();
66 Element epsSavedElement = zusammenAdaptor
67 .saveElement(context, new ElementContext(entitlementPool.getVendorLicenseModelId(), entitlementPool.getVersion().getId()),
68 entitlementPoolsElement, "Create entitlement pool");
69 entitlementPool.setId(epsSavedElement.getSubElements().iterator().next().getElementId().getValue());
73 public void update(EntitlementPoolEntity entitlementPool) {
74 ZusammenElement entitlmentpoolElement = buildEntitlementPoolElement(entitlementPool, Action.UPDATE);
75 SessionContext context = ZusammenUtil.createSessionContext();
76 ElementContext elementContext = new ElementContext(entitlementPool.getVendorLicenseModelId(), entitlementPool.getVersion().getId());
77 Optional<ElementInfo> epFromDb = zusammenAdaptor.getElementInfo(context, elementContext, new Id(entitlementPool.getId()));
78 if (epFromDb.isPresent()) {
79 if (entitlmentpoolElement.getRelations() == null) {
80 entitlmentpoolElement.setRelations(new ArrayList<>());
82 if (epFromDb.get().getRelations() != null && !epFromDb.get().getRelations().isEmpty()) {
83 entitlmentpoolElement.getRelations().addAll(epFromDb.get().getRelations());
86 zusammenAdaptor.saveElement(context, elementContext, entitlmentpoolElement,
87 String.format("Update entitlement pool with id %s", entitlementPool.getId()));
91 public EntitlementPoolEntity get(EntitlementPoolEntity entitlementPool) {
92 SessionContext context = ZusammenUtil.createSessionContext();
93 ElementContext elementContext = new ElementContext(entitlementPool.getVendorLicenseModelId(), entitlementPool.getVersion().getId());
94 ElementToEntitlementPoolConvertor convertor = new ElementToEntitlementPoolConvertor();
95 return zusammenAdaptor.getElementInfo(context, elementContext, new Id(entitlementPool.getId())).map(elementInfo -> {
96 EntitlementPoolEntity entity = convertor.convert(elementInfo);
97 entity.setVendorLicenseModelId(entitlementPool.getVendorLicenseModelId());
98 entity.setVersion(entitlementPool.getVersion());
104 public void delete(EntitlementPoolEntity entitlementPool) {
105 ZusammenElement zusammenElement = buildElement(new Id(entitlementPool.getId()), Action.DELETE);
106 SessionContext context = ZusammenUtil.createSessionContext();
107 ElementContext elementContext = new ElementContext(entitlementPool.getVendorLicenseModelId(), entitlementPool.getVersion().getId());
108 zusammenAdaptor.saveElement(context, elementContext, zusammenElement, "delete entitlement pool. id:" + entitlementPool.getId() + ".");
112 public Collection<EntitlementPoolEntity> list(EntitlementPoolEntity entitlementPool) {
113 SessionContext context = ZusammenUtil.createSessionContext();
114 ElementContext elementContext = new ElementContext(entitlementPool.getVendorLicenseModelId(), entitlementPool.getVersion().getId());
115 ElementToEntitlementPoolConvertor convertor = new ElementToEntitlementPoolConvertor();
116 return zusammenAdaptor.listElementsByName(context, elementContext, null, ElementType.EntitlementPools.name()).stream().map(elementInfo -> {
117 EntitlementPoolEntity entity = convertor.convert(elementInfo);
118 entity.setVendorLicenseModelId(entitlementPool.getVendorLicenseModelId());
119 entity.setVersion(entitlementPool.getVersion());
121 }).collect(Collectors.toList());
125 public long count(EntitlementPoolEntity entitlementPool) {
126 SessionContext context = ZusammenUtil.createSessionContext();
127 ElementContext elementContext = new ElementContext(entitlementPool.getVendorLicenseModelId(), entitlementPool.getVersion().getId());
128 return zusammenAdaptor.listElementsByName(context, elementContext, null, ElementType.EntitlementPools.name()).size();
132 public void removeReferencingFeatureGroup(EntitlementPoolEntity entitlementPool, String referencingFeatureGroupId) {
133 SessionContext context = ZusammenUtil.createSessionContext();
134 ElementContext elementContext = new ElementContext(entitlementPool.getVendorLicenseModelId(), entitlementPool.getVersion().getId());
135 Optional<ElementInfo> elementInfo = zusammenAdaptor.getElementInfo(context, elementContext, new Id(entitlementPool.getId()));
136 if (elementInfo.isPresent()) {
137 ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
138 zusammenElement.setAction(Action.UPDATE);
139 zusammenElement.setRelations(elementInfo.get().getRelations().stream()
140 .filter(relation -> !referencingFeatureGroupId.equals(relation.getEdge2().getElementId().getValue())).collect(Collectors.toList()));
141 zusammenAdaptor.saveElement(context, elementContext, zusammenElement, "remove referencing feature group");
146 public void addReferencingFeatureGroup(EntitlementPoolEntity entitlementPool, String referencingFeatureGroupId) {
147 SessionContext context = ZusammenUtil.createSessionContext();
148 ElementContext elementContext = new ElementContext(entitlementPool.getVendorLicenseModelId(), entitlementPool.getVersion().getId());
149 Optional<ElementInfo> elementInfo = zusammenAdaptor.getElementInfo(context, elementContext, new Id(entitlementPool.getId()));
150 if (elementInfo.isPresent()) {
151 ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
152 zusammenElement.setAction(Action.UPDATE);
153 if (zusammenElement.getRelations() == null) {
154 zusammenElement.setRelations(new ArrayList<>());
156 zusammenElement.getRelations()
157 .add(VlmZusammenUtil.createRelation(RelationType.EntitlmentPoolToReferencingFeatureGroup, referencingFeatureGroupId));
158 zusammenAdaptor.saveElement(context, elementContext, zusammenElement, "add referencing feature group");
163 public void deleteAll(EntitlementPoolEntity entitlementPool) {
168 public String getManufacturerReferenceNumber(EntitlementPoolEntity entitlementPoolEntity) {
169 SessionContext context = ZusammenUtil.createSessionContext();
170 ElementContext elementContext = new ElementContext(entitlementPoolEntity.getVendorLicenseModelId(),
171 entitlementPoolEntity.getVersion().getId());
172 Optional<ElementInfo> elementInfo1 = zusammenAdaptor.getElementInfo(context, elementContext, new Id(entitlementPoolEntity.getId()));
173 Map<String, Object> properties = elementInfo1.get().getInfo().getProperties();
174 String manufacturerReferenceNumber = null;
175 if (properties != null && properties.containsKey("manufacturerReferenceNumber")) {
176 manufacturerReferenceNumber = (String) properties.get("manufacturerReferenceNumber");
178 return manufacturerReferenceNumber;
181 private ZusammenElement buildEntitlementPoolElement(EntitlementPoolEntity entitlementPool, Action action) {
182 ZusammenElement entitlementPoolElement = buildElement(entitlementPool.getId() == null ? null : new Id(entitlementPool.getId()), action);
183 Info info = new Info();
184 info.setName(entitlementPool.getName());
185 info.setDescription(entitlementPool.getDescription());
186 info.addProperty(ElementPropertyName.elementType.name(), ElementType.EntitlementPool);
187 info.addProperty("version_uuid", entitlementPool.getVersionUuId());
188 info.addProperty("EntitlementPoolType", entitlementPool.getType());
189 info.addProperty("thresholdValue", entitlementPool.getThresholdValue());
190 info.addProperty("threshold_unit", entitlementPool.getThresholdUnit());
191 info.addProperty("increments", entitlementPool.getIncrements());
192 info.addProperty("operational_scope", entitlementPool.getOperationalScope());
193 info.addProperty("startDate", entitlementPool.getStartDate());
194 info.addProperty("expiryDate", entitlementPool.getExpiryDate());
195 info.addProperty("manufacturerReferenceNumber", entitlementPool.getManufacturerReferenceNumber());
196 entitlementPoolElement.setInfo(info);
197 if (entitlementPool.getReferencingFeatureGroups() != null && !entitlementPool.getReferencingFeatureGroups().isEmpty()) {
198 entitlementPoolElement.setRelations(entitlementPool.getReferencingFeatureGroups().stream()
199 .map(rel -> VlmZusammenUtil.createRelation(RelationType.EntitlmentPoolToReferencingFeatureGroup, rel)).collect(Collectors.toList()));
201 return entitlementPoolElement;