e5a08ae93abad26fc046458647b2fce4986ced7b
[sdc.git] /
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 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;
37
38 import java.util.ArrayList;
39 import java.util.Collection;
40 import java.util.Optional;
41 import java.util.stream.Collectors;
42
43 import static org.openecomp.core.zusammen.api.ZusammenUtil.*;
44
45 @AllArgsConstructor
46 public class LicenseKeyGroupZusammenDaoImpl implements LicenseKeyGroupDao {
47   private ZusammenAdaptor zusammenAdaptor;
48
49   @Override
50   public void registerVersioning(String versionableEntityType) {
51     //no need
52   }
53
54   @Override
55   public void create(LicenseKeyGroupEntity licenseKeyGroup) {
56     ZusammenElement licenseKeyGroupElement =
57         buildLicenseKeyGroupElement(licenseKeyGroup, Action.CREATE);
58
59     ZusammenElement limitsElement = buildStructuralElement(ElementType.Limits, Action.CREATE);
60     licenseKeyGroupElement.addSubElement(limitsElement);
61
62     ZusammenElement lkgsElement =
63         buildStructuralElement(ElementType.LicenseKeyGroups, Action.IGNORE);
64     lkgsElement.addSubElement(licenseKeyGroupElement);
65
66     SessionContext context = createSessionContext();
67     Element lkgsSavedElement = zusammenAdaptor.saveElement(context,
68         new ElementContext(licenseKeyGroup.getVendorLicenseModelId(),
69             licenseKeyGroup.getVersion().getId()), lkgsElement, "Create license Key Group");
70
71     licenseKeyGroup
72         .setId(lkgsSavedElement.getSubElements().iterator().next().getElementId().getValue());
73   }
74
75   @Override
76   public void update(LicenseKeyGroupEntity licenseKeyGroup) {
77     ZusammenElement licenseKeyGroupElement =
78         buildLicenseKeyGroupElement(licenseKeyGroup, Action.UPDATE);
79
80     SessionContext context = createSessionContext();
81     ElementContext elementContext = new ElementContext(licenseKeyGroup.getVendorLicenseModelId(),
82         licenseKeyGroup.getVersion().getId());
83
84     Optional<ElementInfo> lkgFromDb = zusammenAdaptor.getElementInfo(context, elementContext,
85         new Id(licenseKeyGroup.getId()));
86
87     if (lkgFromDb.isPresent()) {
88
89       if (licenseKeyGroupElement.getRelations() == null) {
90         licenseKeyGroupElement.setRelations(new ArrayList<>());
91       }
92
93       if (lkgFromDb.get().getRelations() != null && !lkgFromDb.get().getRelations().isEmpty()) {
94         licenseKeyGroupElement.getRelations().addAll(lkgFromDb.get().getRelations());
95       }
96     }
97     zusammenAdaptor.saveElement(context, elementContext, licenseKeyGroupElement,
98         String.format("Update license key group with id %s", licenseKeyGroup.getId()));
99   }
100
101   @Override
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());
112           return entity;
113         })
114         .orElse(null);
115   }
116
117   @Override
118   public void delete(LicenseKeyGroupEntity licenseKeyGroup) {
119     ZusammenElement zusammenElement = buildElement(new Id(licenseKeyGroup.getId()), Action.DELETE);
120
121     SessionContext context = createSessionContext();
122     ElementContext elementContext = new ElementContext(licenseKeyGroup.getVendorLicenseModelId(),
123         licenseKeyGroup.getVersion().getId());
124
125     zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
126         "delete license key group. id:" + licenseKeyGroup.getId() + ".");
127   }
128
129   @Override
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());
141           return entity;
142         })
143         .collect(Collectors.toList());
144   }
145
146   @Override
147   public long count(LicenseKeyGroupEntity licenseKeyGroup) {
148     SessionContext context = createSessionContext();
149     ElementContext elementContext = new ElementContext(licenseKeyGroup.getVendorLicenseModelId(),
150         licenseKeyGroup.getVersion().getId());
151
152     return zusammenAdaptor
153         .listElementsByName(context, elementContext, null, ElementType.LicenseKeyGroups.name())
154         .size();
155   }
156
157   @Override
158   public void deleteAll(LicenseKeyGroupEntity licenseKeyGroup) {
159     //not supported
160   }
161
162   @Override
163   public void removeReferencingFeatureGroup(LicenseKeyGroupEntity licenseKeyGroup,
164                                             String featureGroupId) {
165     SessionContext context = createSessionContext();
166     ElementContext elementContext = new ElementContext(licenseKeyGroup.getVendorLicenseModelId(),
167         licenseKeyGroup.getVersion().getId());
168
169     Optional<ElementInfo> elementInfo =
170         zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseKeyGroup.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 -> !featureGroupId
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(LicenseKeyGroupEntity licenseKeyGroup,
187                                          String featureGroupId) {
188     SessionContext context = createSessionContext();
189     ElementContext elementContext = new ElementContext(licenseKeyGroup.getVendorLicenseModelId(),
190         licenseKeyGroup.getVersion().getId());
191
192     Optional<ElementInfo> elementInfo =
193         zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseKeyGroup.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.LicenseKeyGroupToReferencingFeatureGroup,
203               featureGroupId));
204       zusammenAdaptor
205           .saveElement(context, elementContext, zusammenElement, "add referencing feature group");
206     }
207   }
208
209   private ZusammenElement buildLicenseKeyGroupElement(LicenseKeyGroupEntity licenseKeyGroup,
210                                                       Action action) {
211     ZusammenElement lkgElement =
212         buildElement(licenseKeyGroup.getId() == null ? null : new Id(licenseKeyGroup.getId()),
213             action);
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);
228
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()));
235     }
236     return lkgElement;
237   }
238
239
240 }