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 / FeatureGroupDaoZusammenImpl.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 com.amdocs.zusammen.datatypes.item.Relation;
31 import org.openecomp.core.zusammen.api.ZusammenAdaptor;
32 import org.openecomp.sdc.datatypes.model.ElementType;
33 import org.openecomp.sdc.vendorlicense.dao.FeatureGroupDao;
34 import org.openecomp.sdc.vendorlicense.dao.impl.zusammen.convertor.ElementToFeatureGroupConvertor;
35 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity;
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.Set;
42 import java.util.stream.Collectors;
43
44 import static org.openecomp.core.zusammen.api.ZusammenUtil.*;
45
46 public class FeatureGroupDaoZusammenImpl implements FeatureGroupDao {
47
48   private ZusammenAdaptor zusammenAdaptor;
49
50   public FeatureGroupDaoZusammenImpl(ZusammenAdaptor zusammenAdaptor) {
51     this.zusammenAdaptor = zusammenAdaptor;
52   }
53
54   @Override
55   public void registerVersioning(String versionableEntityType) {
56     //no need
57   }
58
59   @Override
60   public void create(FeatureGroupEntity featureGroup) {
61     ZusammenElement featureGroupElement = buildFeatureGroupElement(featureGroup, Action.CREATE);
62
63     ZusammenElement featureGroupsElement =
64         buildStructuralElement(ElementType.FeatureGroups, Action.IGNORE);
65
66     featureGroupsElement.addSubElement(featureGroupElement);
67
68     SessionContext context = createSessionContext();
69     Element featureGroupsSavedElement = zusammenAdaptor.saveElement(context,
70         new ElementContext(featureGroup.getVendorLicenseModelId(),
71             featureGroup.getVersion().getId()), featureGroupsElement, "Create feature group");
72
73     featureGroup.setId(
74         featureGroupsSavedElement.getSubElements().iterator().next().getElementId().getValue());
75   }
76
77   @Override
78   public void update(FeatureGroupEntity featureGroup) {
79     ZusammenElement featureGroupElement = buildFeatureGroupElement(featureGroup, Action.UPDATE);
80
81     SessionContext context = createSessionContext();
82     zusammenAdaptor.saveElement(context, new ElementContext(featureGroup.getVendorLicenseModelId(),
83             featureGroup.getVersion().getId()), featureGroupElement,
84         String.format("Update feature group with id %s", featureGroup.getId()));
85   }
86
87   @Override
88   public FeatureGroupEntity get(FeatureGroupEntity featureGroup) {
89     SessionContext context = createSessionContext();
90     ElementContext elementContext = new ElementContext(featureGroup.getVendorLicenseModelId(),
91         featureGroup.getVersion().getId());
92
93     return zusammenAdaptor.getElementInfo(context, elementContext, new Id(featureGroup.getId()))
94         .map(elementInfo -> {
95           FeatureGroupEntity entity = new ElementToFeatureGroupConvertor().convert(elementInfo);
96           entity.setVendorLicenseModelId(featureGroup.getVendorLicenseModelId());
97           entity.setVersion(featureGroup.getVersion());
98           return entity;
99         })
100         .orElse(null);
101   }
102
103   @Override
104   public void delete(FeatureGroupEntity featureGroup) {
105     ZusammenElement zusammenElement = buildElement(new Id(featureGroup.getId()), Action.DELETE);
106
107     SessionContext context = createSessionContext();
108     ElementContext elementContext = new ElementContext(featureGroup.getVendorLicenseModelId(),
109         featureGroup.getVersion().getId());
110     zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
111         "delete feature group. id:" + featureGroup.getId() + ".");
112   }
113
114   @Override
115   public Collection<FeatureGroupEntity> list(FeatureGroupEntity featureGroup) {
116     SessionContext context = createSessionContext();
117     ElementContext elementContext = new ElementContext(featureGroup.getVendorLicenseModelId(),
118         featureGroup.getVersion().getId());
119
120     ElementToFeatureGroupConvertor convertor = new ElementToFeatureGroupConvertor();
121     return zusammenAdaptor
122         .listElementsByName(context, elementContext, null, ElementType.FeatureGroups.name())
123         .stream().map(elementInfo -> {
124           FeatureGroupEntity entity = convertor.convert(
125               elementInfo);
126           entity.setVendorLicenseModelId(featureGroup.getVendorLicenseModelId());
127           entity.setVersion(featureGroup.getVersion());
128           return entity;
129         })
130         .collect(Collectors.toList());
131   }
132
133   @Override
134   public long count(FeatureGroupEntity featureGroup) {
135     SessionContext context = createSessionContext();
136     ElementContext elementContext = new ElementContext(featureGroup.getVendorLicenseModelId(),
137         featureGroup.getVersion().getId());
138
139     return zusammenAdaptor
140         .listElementsByName(context, elementContext, null, ElementType.FeatureGroups.name())
141         .size();
142   }
143
144   @Override
145   public void removeEntitlementPool(FeatureGroupEntity featureGroup, String entitlementPoolId) {
146     removeRelationToContainedEntity(featureGroup, entitlementPoolId, "entitlement pool");
147   }
148
149   @Override
150   public void removeLicenseKeyGroup(FeatureGroupEntity featureGroup, String licenseKeyGroupId) {
151     removeRelationToContainedEntity(featureGroup, licenseKeyGroupId, "license Key Group");
152   }
153
154   private void removeRelationToContainedEntity(FeatureGroupEntity featureGroup,
155                                                String containedEntityId,
156                                                String containedEntityType) {
157     SessionContext context = createSessionContext();
158     ElementContext elementContext = new ElementContext(featureGroup.getVendorLicenseModelId(),
159         featureGroup.getVersion().getId());
160
161     Optional<ElementInfo> elementInfo = zusammenAdaptor.getElementInfo(context,
162         elementContext, new Id(featureGroup.getId()));
163     if (elementInfo.isPresent()) {
164       ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
165       zusammenElement.setAction(Action.UPDATE);
166       zusammenElement.setRelations(elementInfo.get().getRelations().stream()
167           .filter(
168               relation -> !containedEntityId.equals(relation.getEdge2().getElementId().getValue()))
169           .collect(Collectors.toList()));
170       zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
171           String.format("remove %s", containedEntityType));
172     }
173   }
174
175   @Override
176   public void updateFeatureGroup(FeatureGroupEntity featureGroup,
177                                  Set<String> addedEntitlementPools,
178                                  Set<String> removedEntitlementPools,
179                                  Set<String> addedLicenseKeyGroups,
180                                  Set<String> removedLicenseKeyGroups) {
181     ZusammenElement featureGroupElement = buildFeatureGroupElement(featureGroup, Action.UPDATE);
182     SessionContext context = createSessionContext();
183     ElementContext elementContext = new ElementContext(featureGroup.getVendorLicenseModelId(),
184         featureGroup.getVersion().getId());
185     ElementToFeatureGroupConvertor convertor = new ElementToFeatureGroupConvertor();
186     Optional<ElementInfo> elementInfo = zusammenAdaptor.getElementInfo(context,
187         elementContext, new Id(featureGroup.getId()));
188     if (elementInfo.isPresent()) {
189       FeatureGroupEntity currentFeatureGroup = convertor.convert(elementInfo.get());
190       currentFeatureGroup.setVendorLicenseModelId(featureGroup.getVendorLicenseModelId());
191       currentFeatureGroup.setVersion(featureGroup.getVersion());
192       if (!(removedEntitlementPools == null)) {
193         currentFeatureGroup.getEntitlementPoolIds().removeAll(removedEntitlementPools);
194       }
195       if (!(addedEntitlementPools == null)) {
196         currentFeatureGroup.getEntitlementPoolIds().addAll(addedEntitlementPools);
197       }
198
199       if (featureGroupElement.getRelations() == null) {
200         featureGroupElement.setRelations(new ArrayList<>());
201       }
202       featureGroupElement.getRelations()
203           .addAll(currentFeatureGroup.getEntitlementPoolIds().stream()
204               .map(relation -> VlmZusammenUtil
205                   .createRelation(RelationType.FeatureGroupToEntitlmentPool, relation))
206               .collect(Collectors.toList()));
207
208       if (!(removedLicenseKeyGroups == null)) {
209         currentFeatureGroup.getLicenseKeyGroupIds().removeAll(removedLicenseKeyGroups);
210       }
211       if (!(addedLicenseKeyGroups == null)) {
212         currentFeatureGroup.getLicenseKeyGroupIds().addAll(addedLicenseKeyGroups);
213       }
214
215       featureGroupElement.getRelations()
216           .addAll(currentFeatureGroup.getLicenseKeyGroupIds().stream()
217               .map(relation -> VlmZusammenUtil
218                   .createRelation(RelationType.FeatureGroupToLicenseKeyGroup, relation))
219               .collect(Collectors.toList()));
220
221       Collection<Relation> laRelations = elementInfo.get().getRelations().stream().filter
222           (rel -> rel.getType()
223               .equals(RelationType.FeatureGroupToReferencingLicenseAgreement.name()))
224           .map(rel -> VlmZusammenUtil.createRelation(RelationType
225               .FeatureGroupToReferencingLicenseAgreement, rel.getEdge2().getElementId().toString()))
226           .collect(Collectors.toList());
227
228       featureGroupElement.getRelations().addAll(laRelations);
229
230       zusammenAdaptor
231           .saveElement(context, elementContext, featureGroupElement, "update feature group");
232     }
233   }
234
235   @Override
236   public void deleteAll(FeatureGroupEntity featureGroup) {
237     //not supported
238   }
239
240
241   @Override
242   public void addReferencingLicenseAgreement(FeatureGroupEntity featureGroup,
243                                              String licenseAgreementId) {
244     SessionContext context = createSessionContext();
245     ElementContext elementContext = new ElementContext(featureGroup.getVendorLicenseModelId(),
246         featureGroup.getVersion().getId());
247
248     Optional<ElementInfo> elementInfo =
249         zusammenAdaptor.getElementInfo(context, elementContext, new Id(featureGroup.getId()));
250     if (elementInfo.isPresent()) {
251       ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
252       zusammenElement.setAction(Action.UPDATE);
253       zusammenElement.getRelations().add(VlmZusammenUtil
254           .createRelation(RelationType.FeatureGroupToReferencingLicenseAgreement,
255               licenseAgreementId));
256       zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
257           "add referencing license agreement");
258     }
259   }
260
261   @Override
262   public void removeReferencingLicenseAgreement(FeatureGroupEntity featureGroup,
263                                                 String licenseAgreementId) {
264     SessionContext context = createSessionContext();
265     ElementContext elementContext = new ElementContext(featureGroup.getVendorLicenseModelId(),
266         featureGroup.getVersion().getId());
267
268     Optional<ElementInfo> elementInfo =
269         zusammenAdaptor.getElementInfo(context, elementContext, new Id(featureGroup.getId()));
270     if (elementInfo.isPresent()) {
271       ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
272       zusammenElement.setAction(Action.UPDATE);
273       zusammenElement.setRelations(elementInfo.get().getRelations().stream()
274           .filter(
275               relation -> !licenseAgreementId.equals(relation.getEdge2().getElementId().getValue()))
276           .collect(Collectors.toList()));
277
278       zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
279           "remove referencing license agreement");
280     }
281   }
282
283   private ZusammenElement buildFeatureGroupElement(FeatureGroupEntity featureGroup, Action action) {
284     ZusammenElement featureGroupElement =
285         buildElement(featureGroup.getId() == null ? null : new Id(featureGroup.getId()), action);
286     Info info = new Info();
287     info.setName(featureGroup.getName());
288     info.setDescription(featureGroup.getDescription());
289     info.addProperty(ElementPropertyName.elementType.name(), ElementType.FeatureGroup);
290     info.addProperty("partNumber", featureGroup.getPartNumber());
291     featureGroupElement.setInfo(info);
292
293     featureGroupElement.setRelations(new ArrayList<>());
294
295     if (featureGroup.getEntitlementPoolIds() != null &&
296         !featureGroup.getEntitlementPoolIds().isEmpty()) {
297       featureGroupElement.getRelations().addAll(featureGroup.getEntitlementPoolIds().stream()
298           .map(rel -> VlmZusammenUtil
299               .createRelation(RelationType.FeatureGroupToEntitlmentPool, rel))
300           .collect(Collectors.toList()));
301     }
302
303     if (featureGroup.getLicenseKeyGroupIds() != null &&
304         !featureGroup.getLicenseKeyGroupIds().isEmpty()) {
305       featureGroupElement.getRelations()
306           .addAll(featureGroup.getLicenseKeyGroupIds().stream()
307               .map(rel -> VlmZusammenUtil
308                   .createRelation(RelationType.FeatureGroupToLicenseKeyGroup, rel))
309               .collect(Collectors.toList()));
310     }
311
312     if (featureGroup.getReferencingLicenseAgreements() != null &&
313         !featureGroup.getReferencingLicenseAgreements().isEmpty()) {
314       featureGroupElement.getRelations()
315           .addAll(featureGroup.getReferencingLicenseAgreements().stream()
316               .map(rel -> VlmZusammenUtil
317                   .createRelation(RelationType.FeatureGroupToReferencingLicenseAgreement,
318                       rel))
319               .collect(Collectors.toList()));
320     }
321     return featureGroupElement;
322   }
323 }