03537a287ba08c27f207619966bce3daad14658f
[sdc.git] /
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       lkgFromDb.get().getRelations().forEach(relation ->
84           licenseKeyGroupElement.getRelations().add(relation));
85     }
86
87     zusammenAdaptor.saveElement(context, elementContext,
88         licenseKeyGroupElement,
89         String.format("Update license key group with id %s", licenseKeyGroup.getId()));
90   }
91
92   @Override
93   public LicenseKeyGroupEntity get(LicenseKeyGroupEntity licenseKeyGroup) {
94     SessionContext context = ZusammenUtil.createSessionContext();
95     Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
96     ElementContext elementContext = new ElementContext(itemId,
97         VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor),
98         VlmZusammenUtil.getVersionTag(licenseKeyGroup.getVersion()));
99
100     return zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseKeyGroup.getId()))
101         .map(elementInfo -> mapElementInfoToLicenseKeyGroup(
102             licenseKeyGroup.getVendorLicenseModelId(), licenseKeyGroup.getVersion(), elementInfo))
103         .orElse(null);
104   }
105
106   @Override
107   public void delete(LicenseKeyGroupEntity licenseKeyGroup) {
108     SessionContext context = ZusammenUtil.createSessionContext();
109     ZusammenElement zusammenElement = new ZusammenElement();
110     zusammenElement.setAction(Action.DELETE);
111     zusammenElement.setElementId(new Id(licenseKeyGroup.getId()));
112
113     Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
114     ElementContext elementContext = new ElementContext(itemId,
115         VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor));
116
117     zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
118         "delete license key group. id:" + licenseKeyGroup.getId() + ".");
119   }
120
121   @Override
122   public Collection<LicenseKeyGroupEntity> list(LicenseKeyGroupEntity licenseKeyGroup) {
123     SessionContext context = ZusammenUtil.createSessionContext();
124     Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
125     ElementContext elementContext = new ElementContext(itemId,
126         VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor),
127         VlmZusammenUtil.getVersionTag(licenseKeyGroup.getVersion()));
128
129     return zusammenAdaptor
130         .listElementsByName(context, elementContext, null, StructureElement.LicenseKeyGroups.name())
131         .stream().map(elementInfo -> mapElementInfoToLicenseKeyGroup(
132             licenseKeyGroup.getVendorLicenseModelId(), licenseKeyGroup.getVersion(), elementInfo))
133         .collect(Collectors.toList());
134   }
135
136   @Override
137   public long count(LicenseKeyGroupEntity licenseKeyGroup) {
138     SessionContext context = ZusammenUtil.createSessionContext();
139     Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
140     ElementContext elementContext = new ElementContext(itemId,
141         VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor),
142         VlmZusammenUtil.getVersionTag(licenseKeyGroup.getVersion()));
143
144     return zusammenAdaptor
145         .listElementsByName(context, elementContext, null, StructureElement.LicenseKeyGroups.name())
146         .size();
147   }
148
149   @Override
150   public void deleteAll(LicenseKeyGroupEntity licenseKeyGroup) {
151     //not supported
152   }
153
154   @Override
155   public void removeReferencingFeatureGroup(LicenseKeyGroupEntity licenseKeyGroup,
156                                             String featureGroupId) {
157     SessionContext context = ZusammenUtil.createSessionContext();
158     Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
159     ElementContext elementContext = new ElementContext(itemId,
160         VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor));
161
162     Optional<ElementInfo> elementInfo =
163         zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseKeyGroup.getId()));
164
165     if (elementInfo.isPresent()) {
166       ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
167       zusammenElement.setAction(Action.UPDATE);
168       zusammenElement.setRelations(elementInfo.get().getRelations().stream()
169           .filter(relation -> !featureGroupId
170               .equals(relation.getEdge2().getElementId().getValue()))
171           .collect(Collectors.toList()));
172
173       zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
174           "remove referencing feature group");
175     }
176   }
177
178   @Override
179   public void addReferencingFeatureGroup(LicenseKeyGroupEntity licenseKeyGroup,
180                                          String featureGroupId) {
181     SessionContext context = ZusammenUtil.createSessionContext();
182     Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
183     ElementContext elementContext = new ElementContext(itemId,
184         VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor));
185
186     Optional<ElementInfo> elementInfo =
187         zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseKeyGroup.getId()));
188
189     if (elementInfo.isPresent()) {
190       ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
191       zusammenElement.setAction(Action.UPDATE);
192       if (zusammenElement.getRelations() == null) {
193         zusammenElement.setRelations(new ArrayList<>());
194       }
195       zusammenElement.getRelations().add(VlmZusammenUtil
196           .createRelation(RelationType.LicenseKeyGroupToReferencingFeatureGroup,
197               featureGroupId));
198       zusammenAdaptor
199           .saveElement(context, elementContext, zusammenElement, "add referencing feature group");
200     }
201   }
202
203   private ZusammenElement buildLicenseKeyGroupElement(LicenseKeyGroupEntity licenseKeyGroup,
204                                                       Action action) {
205     ZusammenElement lkgElement = new ZusammenElement();
206     lkgElement.setAction(action);
207     if (licenseKeyGroup.getId() != null) {
208       lkgElement.setElementId(new Id(licenseKeyGroup.getId()));
209     }
210     Info info = new Info();
211     info.setName(licenseKeyGroup.getName());
212     info.setDescription(licenseKeyGroup.getDescription());
213     info.addProperty("LicenseKeyType", licenseKeyGroup.getType());
214     info.addProperty("operational_scope", licenseKeyGroup.getOperationalScope());
215     lkgElement.setInfo(info);
216
217     if (licenseKeyGroup.getReferencingFeatureGroups() != null &&
218         licenseKeyGroup.getReferencingFeatureGroups().size() > 0) {
219       lkgElement.setRelations(licenseKeyGroup.getReferencingFeatureGroups().stream()
220           .map(rel -> VlmZusammenUtil
221               .createRelation(RelationType.LicenseKeyGroupToReferencingFeatureGroup, rel))
222           .collect(Collectors.toList()));
223     }
224     return lkgElement;
225   }
226
227   private LicenseKeyGroupEntity mapElementInfoToLicenseKeyGroup(String vlmId, Version version,
228                                                                 ElementInfo elementInfo) {
229     LicenseKeyGroupEntity licenseKeyGroup =
230         new LicenseKeyGroupEntity(vlmId, version, elementInfo.getId().getValue());
231     licenseKeyGroup.setName(elementInfo.getInfo().getName());
232     licenseKeyGroup.setDescription(elementInfo.getInfo().getDescription());
233
234     licenseKeyGroup
235         .setType(LicenseKeyType.valueOf(elementInfo.getInfo().getProperty("LicenseKeyType")));
236     licenseKeyGroup.setOperationalScope(getOperationalScopeMultiChoiceOrOther(
237         elementInfo.getInfo().getProperty("operational_scope")));
238
239     if (elementInfo.getRelations() != null && elementInfo.getRelations().size() > 0) {
240       licenseKeyGroup
241           .setReferencingFeatureGroups(elementInfo.getRelations().stream().map(relation -> relation
242               .getEdge2().getElementId().getValue()).collect(Collectors.toSet()));
243     }
244     return licenseKeyGroup;
245   }
246
247   private MultiChoiceOrOther<OperationalScope> getOperationalScopeMultiChoiceOrOther
248       (Map<String, Object>
249            operationalScope) {
250
251     Set<OperationalScope> choices = new HashSet<>();
252     ((List<String>) operationalScope.get("choices")).
253         forEach(choice -> choices.add(OperationalScope.valueOf(choice)));
254
255     return new MultiChoiceOrOther<>(choices, (String) operationalScope.get("other"));
256   }
257
258
259 }