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 lombok.AllArgsConstructor;
31 import org.openecomp.core.zusammen.api.ZusammenAdaptor;
32 import org.openecomp.sdc.datatypes.model.ElementType;
33 import org.openecomp.sdc.vendorlicense.dao.LicenseKeyGroupDao;
34 import org.openecomp.sdc.vendorlicense.dao.impl.zusammen.convertor.ElementToLicenseKeyGroupConvertor;
35 import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity;
36 import org.openecomp.types.ElementPropertyName;
38 import java.util.ArrayList;
39 import java.util.Collection;
40 import java.util.Optional;
41 import java.util.stream.Collectors;
43 import static org.openecomp.core.zusammen.api.ZusammenUtil.*;
46 public class LicenseKeyGroupZusammenDaoImpl implements LicenseKeyGroupDao {
47 private ZusammenAdaptor zusammenAdaptor;
50 public void registerVersioning(String versionableEntityType) {
55 public void create(LicenseKeyGroupEntity licenseKeyGroup) {
56 ZusammenElement licenseKeyGroupElement =
57 buildLicenseKeyGroupElement(licenseKeyGroup, Action.CREATE);
59 ZusammenElement limitsElement = buildStructuralElement(ElementType.Limits, Action.CREATE);
60 licenseKeyGroupElement.addSubElement(limitsElement);
62 ZusammenElement lkgsElement =
63 buildStructuralElement(ElementType.LicenseKeyGroups, Action.IGNORE);
64 lkgsElement.addSubElement(licenseKeyGroupElement);
66 SessionContext context = createSessionContext();
67 Element lkgsSavedElement = zusammenAdaptor.saveElement(context,
68 new ElementContext(licenseKeyGroup.getVendorLicenseModelId(),
69 licenseKeyGroup.getVersion().getId()), lkgsElement, "Create license Key Group");
72 .setId(lkgsSavedElement.getSubElements().iterator().next().getElementId().getValue());
76 public void update(LicenseKeyGroupEntity licenseKeyGroup) {
77 ZusammenElement licenseKeyGroupElement =
78 buildLicenseKeyGroupElement(licenseKeyGroup, Action.UPDATE);
80 SessionContext context = createSessionContext();
81 ElementContext elementContext = new ElementContext(licenseKeyGroup.getVendorLicenseModelId(),
82 licenseKeyGroup.getVersion().getId());
84 Optional<ElementInfo> lkgFromDb = zusammenAdaptor.getElementInfo(context, elementContext,
85 new Id(licenseKeyGroup.getId()));
87 if (lkgFromDb.isPresent()) {
89 if (licenseKeyGroupElement.getRelations() == null) {
90 licenseKeyGroupElement.setRelations(new ArrayList<>());
93 if (lkgFromDb.get().getRelations() != null && !lkgFromDb.get().getRelations().isEmpty()) {
94 licenseKeyGroupElement.getRelations().addAll(lkgFromDb.get().getRelations());
97 zusammenAdaptor.saveElement(context, elementContext, licenseKeyGroupElement,
98 String.format("Update license key group with id %s", licenseKeyGroup.getId()));
102 public LicenseKeyGroupEntity get(LicenseKeyGroupEntity licenseKeyGroup) {
103 SessionContext context = createSessionContext();
104 ElementContext elementContext = new ElementContext(licenseKeyGroup.getVendorLicenseModelId(),
105 licenseKeyGroup.getVersion().getId());
106 ElementToLicenseKeyGroupConvertor convertor = new ElementToLicenseKeyGroupConvertor();
107 return zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseKeyGroup.getId()))
108 .map(elementInfo -> {
109 LicenseKeyGroupEntity entity = convertor.convert(elementInfo);
110 entity.setVendorLicenseModelId(licenseKeyGroup.getVendorLicenseModelId());
111 entity.setVersion(licenseKeyGroup.getVersion());
118 public void delete(LicenseKeyGroupEntity licenseKeyGroup) {
119 ZusammenElement zusammenElement = buildElement(new Id(licenseKeyGroup.getId()), Action.DELETE);
121 SessionContext context = createSessionContext();
122 ElementContext elementContext = new ElementContext(licenseKeyGroup.getVendorLicenseModelId(),
123 licenseKeyGroup.getVersion().getId());
125 zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
126 "delete license key group. id:" + licenseKeyGroup.getId() + ".");
130 public Collection<LicenseKeyGroupEntity> list(LicenseKeyGroupEntity licenseKeyGroup) {
131 SessionContext context = createSessionContext();
132 ElementContext elementContext = new ElementContext(licenseKeyGroup.getVendorLicenseModelId(),
133 licenseKeyGroup.getVersion().getId());
134 ElementToLicenseKeyGroupConvertor convertor = new ElementToLicenseKeyGroupConvertor();
135 return zusammenAdaptor
136 .listElementsByName(context, elementContext, null, ElementType.LicenseKeyGroups.name())
137 .stream().map(elementInfo -> {
138 LicenseKeyGroupEntity entity = convertor.convert(elementInfo);
139 entity.setVendorLicenseModelId(licenseKeyGroup.getVendorLicenseModelId());
140 entity.setVersion(licenseKeyGroup.getVersion());
143 .collect(Collectors.toList());
147 public long count(LicenseKeyGroupEntity licenseKeyGroup) {
148 SessionContext context = createSessionContext();
149 ElementContext elementContext = new ElementContext(licenseKeyGroup.getVendorLicenseModelId(),
150 licenseKeyGroup.getVersion().getId());
152 return zusammenAdaptor
153 .listElementsByName(context, elementContext, null, ElementType.LicenseKeyGroups.name())
158 public void deleteAll(LicenseKeyGroupEntity licenseKeyGroup) {
163 public void removeReferencingFeatureGroup(LicenseKeyGroupEntity licenseKeyGroup,
164 String featureGroupId) {
165 SessionContext context = createSessionContext();
166 ElementContext elementContext = new ElementContext(licenseKeyGroup.getVendorLicenseModelId(),
167 licenseKeyGroup.getVersion().getId());
169 Optional<ElementInfo> elementInfo =
170 zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseKeyGroup.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 -> !featureGroupId
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(LicenseKeyGroupEntity licenseKeyGroup,
187 String featureGroupId) {
188 SessionContext context = createSessionContext();
189 ElementContext elementContext = new ElementContext(licenseKeyGroup.getVendorLicenseModelId(),
190 licenseKeyGroup.getVersion().getId());
192 Optional<ElementInfo> elementInfo =
193 zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseKeyGroup.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.LicenseKeyGroupToReferencingFeatureGroup,
205 .saveElement(context, elementContext, zusammenElement, "add referencing feature group");
209 private ZusammenElement buildLicenseKeyGroupElement(LicenseKeyGroupEntity licenseKeyGroup,
211 ZusammenElement lkgElement =
212 buildElement(licenseKeyGroup.getId() == null ? null : new Id(licenseKeyGroup.getId()),
214 Info info = new Info();
215 info.setName(licenseKeyGroup.getName());
216 info.setDescription(licenseKeyGroup.getDescription());
217 info.addProperty(ElementPropertyName.elementType.name(), ElementType.LicenseKeyGroup);
218 info.addProperty("version_uuid", licenseKeyGroup.getVersionUuId());
219 info.addProperty("LicenseKeyType", licenseKeyGroup.getType());
220 info.addProperty("operational_scope", licenseKeyGroup.getOperationalScope());
221 info.addProperty("startDate", licenseKeyGroup.getStartDate());
222 info.addProperty("expiryDate", licenseKeyGroup.getExpiryDate());
223 info.addProperty("thresholdValue", licenseKeyGroup.getThresholdValue());
224 info.addProperty("thresholdUnits", licenseKeyGroup.getThresholdUnits());
225 info.addProperty("increments", licenseKeyGroup.getIncrements());
226 info.addProperty("manufacturerReferenceNumber", licenseKeyGroup.getManufacturerReferenceNumber());
227 lkgElement.setInfo(info);
229 if (licenseKeyGroup.getReferencingFeatureGroups() != null
230 && !licenseKeyGroup.getReferencingFeatureGroups().isEmpty()) {
231 lkgElement.setRelations(licenseKeyGroup.getReferencingFeatureGroups().stream()
232 .map(rel -> VlmZusammenUtil
233 .createRelation(RelationType.LicenseKeyGroupToReferencingFeatureGroup, rel))
234 .collect(Collectors.toList()));