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.sdc.datatypes.model.ElementType;
32 import org.openecomp.sdc.vendorlicense.dao.LicenseKeyGroupDao;
33 import org.openecomp.sdc.vendorlicense.dao.impl.zusammen.convertor.ElementToLicenseKeyGroupConvertor;
34 import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity;
35 import org.openecomp.types.ElementPropertyName;
37 import java.util.ArrayList;
38 import java.util.Collection;
39 import java.util.Optional;
40 import java.util.stream.Collectors;
42 import static org.openecomp.core.zusammen.api.ZusammenUtil.*;
45 public class LicenseKeyGroupZusammenDaoImpl implements LicenseKeyGroupDao {
46 private ZusammenAdaptor zusammenAdaptor;
48 public LicenseKeyGroupZusammenDaoImpl(ZusammenAdaptor zusammenAdaptor) {
49 this.zusammenAdaptor = zusammenAdaptor;
53 public void registerVersioning(String versionableEntityType) {
58 public void create(LicenseKeyGroupEntity licenseKeyGroup) {
59 ZusammenElement licenseKeyGroupElement =
60 buildLicenseKeyGroupElement(licenseKeyGroup, Action.CREATE);
62 ZusammenElement limitsElement = buildStructuralElement(ElementType.Limits, Action.CREATE);
63 licenseKeyGroupElement.addSubElement(limitsElement);
65 ZusammenElement lkgsElement =
66 buildStructuralElement(ElementType.LicenseKeyGroups, Action.IGNORE);
67 lkgsElement.addSubElement(licenseKeyGroupElement);
69 SessionContext context = createSessionContext();
70 Element lkgsSavedElement = zusammenAdaptor.saveElement(context,
71 new ElementContext(licenseKeyGroup.getVendorLicenseModelId(),
72 licenseKeyGroup.getVersion().getId()), lkgsElement, "Create license Key Group");
75 .setId(lkgsSavedElement.getSubElements().iterator().next().getElementId().getValue());
79 public void update(LicenseKeyGroupEntity licenseKeyGroup) {
80 ZusammenElement licenseKeyGroupElement =
81 buildLicenseKeyGroupElement(licenseKeyGroup, Action.UPDATE);
83 SessionContext context = createSessionContext();
84 ElementContext elementContext = new ElementContext(licenseKeyGroup.getVendorLicenseModelId(),
85 licenseKeyGroup.getVersion().getId());
87 Optional<ElementInfo> lkgFromDb = zusammenAdaptor.getElementInfo(context, elementContext,
88 new Id(licenseKeyGroup.getId()));
90 if (lkgFromDb.isPresent()) {
92 if (licenseKeyGroupElement.getRelations() == null) {
93 licenseKeyGroupElement.setRelations(new ArrayList<>());
96 if (lkgFromDb.get().getRelations() != null && !lkgFromDb.get().getRelations().isEmpty()) {
97 licenseKeyGroupElement.getRelations().addAll(lkgFromDb.get().getRelations());
100 zusammenAdaptor.saveElement(context, elementContext, licenseKeyGroupElement,
101 String.format("Update license key group with id %s", licenseKeyGroup.getId()));
105 public LicenseKeyGroupEntity get(LicenseKeyGroupEntity licenseKeyGroup) {
106 SessionContext context = createSessionContext();
107 ElementContext elementContext = new ElementContext(licenseKeyGroup.getVendorLicenseModelId(),
108 licenseKeyGroup.getVersion().getId());
109 ElementToLicenseKeyGroupConvertor convertor = new ElementToLicenseKeyGroupConvertor();
110 return zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseKeyGroup.getId()))
111 .map(elementInfo -> {
112 LicenseKeyGroupEntity entity = convertor.convert(elementInfo);
113 entity.setVendorLicenseModelId(licenseKeyGroup.getVendorLicenseModelId());
114 entity.setVersion(licenseKeyGroup.getVersion());
121 public void delete(LicenseKeyGroupEntity licenseKeyGroup) {
122 ZusammenElement zusammenElement = buildElement(new Id(licenseKeyGroup.getId()), Action.DELETE);
124 SessionContext context = createSessionContext();
125 ElementContext elementContext = new ElementContext(licenseKeyGroup.getVendorLicenseModelId(),
126 licenseKeyGroup.getVersion().getId());
128 zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
129 "delete license key group. id:" + licenseKeyGroup.getId() + ".");
133 public Collection<LicenseKeyGroupEntity> list(LicenseKeyGroupEntity licenseKeyGroup) {
134 SessionContext context = createSessionContext();
135 ElementContext elementContext = new ElementContext(licenseKeyGroup.getVendorLicenseModelId(),
136 licenseKeyGroup.getVersion().getId());
137 ElementToLicenseKeyGroupConvertor convertor = new ElementToLicenseKeyGroupConvertor();
138 return zusammenAdaptor
139 .listElementsByName(context, elementContext, null, ElementType.LicenseKeyGroups.name())
140 .stream().map(elementInfo -> {
141 LicenseKeyGroupEntity entity = convertor.convert(elementInfo);
142 entity.setVendorLicenseModelId(licenseKeyGroup.getVendorLicenseModelId());
143 entity.setVersion(licenseKeyGroup.getVersion());
146 .collect(Collectors.toList());
150 public long count(LicenseKeyGroupEntity licenseKeyGroup) {
151 SessionContext context = createSessionContext();
152 ElementContext elementContext = new ElementContext(licenseKeyGroup.getVendorLicenseModelId(),
153 licenseKeyGroup.getVersion().getId());
155 return zusammenAdaptor
156 .listElementsByName(context, elementContext, null, ElementType.LicenseKeyGroups.name())
161 public void deleteAll(LicenseKeyGroupEntity licenseKeyGroup) {
166 public void removeReferencingFeatureGroup(LicenseKeyGroupEntity licenseKeyGroup,
167 String featureGroupId) {
168 SessionContext context = createSessionContext();
169 ElementContext elementContext = new ElementContext(licenseKeyGroup.getVendorLicenseModelId(),
170 licenseKeyGroup.getVersion().getId());
172 Optional<ElementInfo> elementInfo =
173 zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseKeyGroup.getId()));
175 if (elementInfo.isPresent()) {
176 ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
177 zusammenElement.setAction(Action.UPDATE);
178 zusammenElement.setRelations(elementInfo.get().getRelations().stream()
179 .filter(relation -> !featureGroupId
180 .equals(relation.getEdge2().getElementId().getValue()))
181 .collect(Collectors.toList()));
183 zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
184 "remove referencing feature group");
189 public void addReferencingFeatureGroup(LicenseKeyGroupEntity licenseKeyGroup,
190 String featureGroupId) {
191 SessionContext context = createSessionContext();
192 ElementContext elementContext = new ElementContext(licenseKeyGroup.getVendorLicenseModelId(),
193 licenseKeyGroup.getVersion().getId());
195 Optional<ElementInfo> elementInfo =
196 zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseKeyGroup.getId()));
198 if (elementInfo.isPresent()) {
199 ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
200 zusammenElement.setAction(Action.UPDATE);
201 if (zusammenElement.getRelations() == null) {
202 zusammenElement.setRelations(new ArrayList<>());
204 zusammenElement.getRelations().add(VlmZusammenUtil
205 .createRelation(RelationType.LicenseKeyGroupToReferencingFeatureGroup,
208 .saveElement(context, elementContext, zusammenElement, "add referencing feature group");
212 private ZusammenElement buildLicenseKeyGroupElement(LicenseKeyGroupEntity licenseKeyGroup,
214 ZusammenElement lkgElement =
215 buildElement(licenseKeyGroup.getId() == null ? null : new Id(licenseKeyGroup.getId()),
217 Info info = new Info();
218 info.setName(licenseKeyGroup.getName());
219 info.setDescription(licenseKeyGroup.getDescription());
220 info.addProperty(ElementPropertyName.elementType.name(), ElementType.LicenseKeyGroup);
221 info.addProperty("version_uuid", licenseKeyGroup.getVersionUuId());
222 info.addProperty("LicenseKeyType", licenseKeyGroup.getType());
223 info.addProperty("operational_scope", licenseKeyGroup.getOperationalScope());
224 info.addProperty("startDate", licenseKeyGroup.getStartDate());
225 info.addProperty("expiryDate", licenseKeyGroup.getExpiryDate());
226 info.addProperty("thresholdValue", licenseKeyGroup.getThresholdValue());
227 info.addProperty("thresholdUnits", licenseKeyGroup.getThresholdUnits());
228 info.addProperty("increments", licenseKeyGroup.getIncrements());
229 info.addProperty("manufacturerReferenceNumber", licenseKeyGroup.getManufacturerReferenceNumber());
230 lkgElement.setInfo(info);
232 if (licenseKeyGroup.getReferencingFeatureGroups() != null
233 && !licenseKeyGroup.getReferencingFeatureGroups().isEmpty()) {
234 lkgElement.setRelations(licenseKeyGroup.getReferencingFeatureGroups().stream()
235 .map(rel -> VlmZusammenUtil
236 .createRelation(RelationType.LicenseKeyGroupToReferencingFeatureGroup, rel))
237 .collect(Collectors.toList()));