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