[SDC] Onboarding 1710 rebase.
[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 package org.openecomp.sdc.vendorlicense.dao.impl.zusammen;
2
3 import com.amdocs.zusammen.adaptor.inbound.api.types.item.Element;
4 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ElementInfo;
5 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ZusammenElement;
6 import com.amdocs.zusammen.datatypes.Id;
7 import com.amdocs.zusammen.datatypes.SessionContext;
8 import com.amdocs.zusammen.datatypes.item.Action;
9 import com.amdocs.zusammen.datatypes.item.ElementContext;
10 import com.amdocs.zusammen.datatypes.item.Info;
11 import org.openecomp.core.zusammen.api.ZusammenAdaptor;
12 import org.openecomp.core.zusammen.api.ZusammenUtil;
13 import org.openecomp.sdc.vendorlicense.dao.LicenseKeyGroupDao;
14 import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity;
15 import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyType;
16 import org.openecomp.sdc.vendorlicense.dao.types.MultiChoiceOrOther;
17 import org.openecomp.sdc.vendorlicense.dao.types.OperationalScope;
18 import org.openecomp.sdc.versioning.dao.types.Version;
19
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Optional;
26 import java.util.Set;
27 import java.util.stream.Collectors;
28
29 /**
30  * Created by ayalaben on 3/30/2017.
31  */
32 public class LicenseKeyGroupZusammenDaoImpl implements LicenseKeyGroupDao {
33   private ZusammenAdaptor zusammenAdaptor;
34
35   public LicenseKeyGroupZusammenDaoImpl(ZusammenAdaptor zusammenAdaptor) {
36     this.zusammenAdaptor = zusammenAdaptor;
37   }
38
39   @Override
40   public void registerVersioning(String versionableEntityType) {
41     //no need
42   }
43
44   @Override
45   public void create(LicenseKeyGroupEntity licenseKeyGroup) {
46     ZusammenElement licenseKeyGroupElement =
47         buildLicenseKeyGroupElement(licenseKeyGroup, Action.CREATE);
48
49     ZusammenElement lkgsElement =
50         VlmZusammenUtil.buildStructuralElement(StructureElement.LicenseKeyGroups, null);
51     lkgsElement.addSubElement(licenseKeyGroupElement);
52
53     SessionContext context = ZusammenUtil.createSessionContext();
54     Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
55     Optional<Element> savedElement = zusammenAdaptor.saveElement(context, new ElementContext(itemId,
56             VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor)),
57         lkgsElement, "Create license Key Group");
58
59     savedElement.ifPresent(element -> licenseKeyGroup
60         .setId(element.getSubElements().iterator().next().getElementId().getValue()));
61   }
62
63   @Override
64   public void update(LicenseKeyGroupEntity licenseKeyGroup) {
65     ZusammenElement licenseKeyGroupElement =
66         buildLicenseKeyGroupElement(licenseKeyGroup, Action.UPDATE);
67
68     SessionContext context = ZusammenUtil.createSessionContext();
69     Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
70
71     ElementContext elementContext = new ElementContext(itemId,
72         VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor));
73
74     Optional<ElementInfo> lkgFromDb = zusammenAdaptor.getElementInfo(context, elementContext,
75         new Id(licenseKeyGroup.getId()));
76
77     if(lkgFromDb.isPresent()) {
78
79       if( licenseKeyGroupElement.getRelations() == null) {
80          licenseKeyGroupElement.setRelations(new ArrayList<>());
81       }
82
83       if (lkgFromDb.get().getRelations() != null && lkgFromDb.get().getRelations().size() > 0) {
84         licenseKeyGroupElement.getRelations().addAll(lkgFromDb.get().getRelations());
85       }
86     }
87
88     zusammenAdaptor.saveElement(context, elementContext,
89         licenseKeyGroupElement,
90         String.format("Update license key group with id %s", licenseKeyGroup.getId()));
91   }
92
93   @Override
94   public LicenseKeyGroupEntity get(LicenseKeyGroupEntity licenseKeyGroup) {
95     SessionContext context = ZusammenUtil.createSessionContext();
96     Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
97     ElementContext elementContext = new ElementContext(itemId,
98         VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor),
99         VlmZusammenUtil.getVersionTag(licenseKeyGroup.getVersion()));
100
101     return zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseKeyGroup.getId()))
102         .map(elementInfo -> mapElementInfoToLicenseKeyGroup(
103             licenseKeyGroup.getVendorLicenseModelId(), licenseKeyGroup.getVersion(), elementInfo))
104         .orElse(null);
105   }
106
107   @Override
108   public void delete(LicenseKeyGroupEntity licenseKeyGroup) {
109     SessionContext context = ZusammenUtil.createSessionContext();
110     ZusammenElement zusammenElement = new ZusammenElement();
111     zusammenElement.setAction(Action.DELETE);
112     zusammenElement.setElementId(new Id(licenseKeyGroup.getId()));
113
114     Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
115     ElementContext elementContext = new ElementContext(itemId,
116         VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor));
117
118     zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
119         "delete license key group. id:" + licenseKeyGroup.getId() + ".");
120   }
121
122   @Override
123   public Collection<LicenseKeyGroupEntity> list(LicenseKeyGroupEntity licenseKeyGroup) {
124     SessionContext context = ZusammenUtil.createSessionContext();
125     Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
126     ElementContext elementContext = new ElementContext(itemId,
127         VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor),
128         VlmZusammenUtil.getVersionTag(licenseKeyGroup.getVersion()));
129
130     return zusammenAdaptor
131         .listElementsByName(context, elementContext, null, StructureElement.LicenseKeyGroups.name())
132         .stream().map(elementInfo -> mapElementInfoToLicenseKeyGroup(
133             licenseKeyGroup.getVendorLicenseModelId(), licenseKeyGroup.getVersion(), elementInfo))
134         .collect(Collectors.toList());
135   }
136
137   @Override
138   public long count(LicenseKeyGroupEntity licenseKeyGroup) {
139     SessionContext context = ZusammenUtil.createSessionContext();
140     Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
141     ElementContext elementContext = new ElementContext(itemId,
142         VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor),
143         VlmZusammenUtil.getVersionTag(licenseKeyGroup.getVersion()));
144
145     return zusammenAdaptor
146         .listElementsByName(context, elementContext, null, StructureElement.LicenseKeyGroups.name())
147         .size();
148   }
149
150   @Override
151   public void deleteAll(LicenseKeyGroupEntity licenseKeyGroup) {
152     //not supported
153   }
154
155   @Override
156   public void removeReferencingFeatureGroup(LicenseKeyGroupEntity licenseKeyGroup,
157                                             String featureGroupId) {
158     SessionContext context = ZusammenUtil.createSessionContext();
159     Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
160     ElementContext elementContext = new ElementContext(itemId,
161         VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor));
162
163     Optional<ElementInfo> elementInfo =
164         zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseKeyGroup.getId()));
165
166     if (elementInfo.isPresent()) {
167       ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
168       zusammenElement.setAction(Action.UPDATE);
169       zusammenElement.setRelations(elementInfo.get().getRelations().stream()
170           .filter(relation -> !featureGroupId
171               .equals(relation.getEdge2().getElementId().getValue()))
172           .collect(Collectors.toList()));
173
174       zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
175           "remove referencing feature group");
176     }
177   }
178
179   @Override
180   public void addReferencingFeatureGroup(LicenseKeyGroupEntity licenseKeyGroup,
181                                          String featureGroupId) {
182     SessionContext context = ZusammenUtil.createSessionContext();
183     Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
184     ElementContext elementContext = new ElementContext(itemId,
185         VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor));
186
187     Optional<ElementInfo> elementInfo =
188         zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseKeyGroup.getId()));
189
190     if (elementInfo.isPresent()) {
191       ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
192       zusammenElement.setAction(Action.UPDATE);
193       if (zusammenElement.getRelations() == null) {
194         zusammenElement.setRelations(new ArrayList<>());
195       }
196       zusammenElement.getRelations().add(VlmZusammenUtil
197           .createRelation(RelationType.LicenseKeyGroupToReferencingFeatureGroup,
198               featureGroupId));
199       zusammenAdaptor
200           .saveElement(context, elementContext, zusammenElement, "add referencing feature group");
201     }
202   }
203
204   private ZusammenElement buildLicenseKeyGroupElement(LicenseKeyGroupEntity licenseKeyGroup,
205                                                       Action action) {
206
207     ZusammenElement lkgElement = new ZusammenElement();
208     lkgElement.setAction(action);
209     if (licenseKeyGroup.getId() != null) {
210       lkgElement.setElementId(new Id(licenseKeyGroup.getId()));
211     }
212     Info info = new Info();
213     info.setName(licenseKeyGroup.getName());
214     info.setDescription(licenseKeyGroup.getDescription());
215     info.addProperty("LicenseKeyType", licenseKeyGroup.getType());
216     info.addProperty("operational_scope", licenseKeyGroup.getOperationalScope());
217     lkgElement.setInfo(info);
218
219    if (licenseKeyGroup.getReferencingFeatureGroups() != null
220        && licenseKeyGroup.getReferencingFeatureGroups().size() > 0) {
221       lkgElement.setRelations(licenseKeyGroup.getReferencingFeatureGroups().stream()
222           .map(rel -> VlmZusammenUtil
223               .createRelation(RelationType.LicenseKeyGroupToReferencingFeatureGroup, rel))
224           .collect(Collectors.toList()));
225     }
226
227     return lkgElement;
228   }
229
230   private LicenseKeyGroupEntity mapElementInfoToLicenseKeyGroup(String vlmId, Version version,
231                                                                 ElementInfo elementInfo) {
232     LicenseKeyGroupEntity licenseKeyGroup =
233         new LicenseKeyGroupEntity(vlmId, version, elementInfo.getId().getValue());
234     licenseKeyGroup.setName(elementInfo.getInfo().getName());
235     licenseKeyGroup.setDescription(elementInfo.getInfo().getDescription());
236
237     licenseKeyGroup
238         .setType(LicenseKeyType.valueOf(elementInfo.getInfo().getProperty("LicenseKeyType")));
239     licenseKeyGroup.setOperationalScope(getOperationalScopeMultiChoiceOrOther(
240         elementInfo.getInfo().getProperty("operational_scope")));
241
242     if (elementInfo.getRelations() != null && elementInfo.getRelations().size() > 0) {
243       licenseKeyGroup
244           .setReferencingFeatureGroups(elementInfo.getRelations().stream().map(relation -> relation
245               .getEdge2().getElementId().getValue()).collect(Collectors.toSet()));
246     }
247     return licenseKeyGroup;
248   }
249
250   private MultiChoiceOrOther<OperationalScope> getOperationalScopeMultiChoiceOrOther
251       (Map<String, Object>
252            operationalScope) {
253   if(operationalScope != null && !operationalScope.isEmpty()) {
254     Set<OperationalScope> choices = new HashSet<>();
255     ((List<String>) operationalScope.get("choices"))
256         .forEach(choice -> choices.add(OperationalScope.valueOf(choice)));
257
258     return new MultiChoiceOrOther<>(choices, operationalScope.get("other")==null?null:(String) operationalScope.get("other"));
259   }
260   return null;
261   }
262 }