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