aa8ceb8e9702d6d099c92cf6b34781b1937cfd5a
[sdc.git] /
1 /*
2  * Copyright © 2016-2017 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.vendorlicense.dao.impl.zusammen;
18
19 import com.amdocs.zusammen.adaptor.inbound.api.types.item.Element;
20 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ElementInfo;
21 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ZusammenElement;
22 import com.amdocs.zusammen.datatypes.Id;
23 import com.amdocs.zusammen.datatypes.SessionContext;
24 import com.amdocs.zusammen.datatypes.item.Action;
25 import com.amdocs.zusammen.datatypes.item.ElementContext;
26 import com.amdocs.zusammen.datatypes.item.Info;
27 import org.openecomp.core.zusammen.api.ZusammenAdaptor;
28 import org.openecomp.sdc.datatypes.model.ElementType;
29 import org.openecomp.sdc.vendorlicense.dao.LicenseAgreementDao;
30 import org.openecomp.sdc.vendorlicense.dao.impl.zusammen.convertor.ElementToLicenseAgreementConvertor;
31 import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementEntity;
32 import org.openecomp.types.ElementPropertyName;
33
34 import java.util.Collection;
35 import java.util.Optional;
36 import java.util.Set;
37 import java.util.stream.Collectors;
38
39 import static org.openecomp.core.zusammen.api.ZusammenUtil.buildElement;
40 import static org.openecomp.core.zusammen.api.ZusammenUtil.buildStructuralElement;
41 import static org.openecomp.core.zusammen.api.ZusammenUtil.createSessionContext;
42
43
44 public class LicenseAgreementDaoZusammenImpl implements LicenseAgreementDao {
45
46   private ZusammenAdaptor zusammenAdaptor;
47
48   public LicenseAgreementDaoZusammenImpl(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(LicenseAgreementEntity licenseAgreement) {
59     ZusammenElement licenseAgreementElement =
60         buildLicenseAgreementElement(licenseAgreement, Action.CREATE);
61     ZusammenElement licenseAgreementsElement =
62         buildStructuralElement(ElementType.LicenseAgreements, Action.IGNORE);
63     licenseAgreementsElement.addSubElement(licenseAgreementElement);
64
65     SessionContext context = createSessionContext();
66     Element licenseAgreementsSavedElement = zusammenAdaptor.saveElement(context,
67         new ElementContext(licenseAgreement.getVendorLicenseModelId(),
68             licenseAgreement.getVersion().getId()), licenseAgreementsElement,
69         "Create license agreement");
70     licenseAgreement
71         .setId(licenseAgreementsSavedElement.getSubElements().iterator().next().getElementId()
72             .getValue());
73   }
74
75   @Override
76   public void update(LicenseAgreementEntity licenseAgreement) {
77     ZusammenElement licenseAgreementElement =
78         buildLicenseAgreementElement(licenseAgreement, Action.UPDATE);
79
80     SessionContext context = createSessionContext();
81     zusammenAdaptor.saveElement(context,
82         new ElementContext(licenseAgreement.getVendorLicenseModelId(),
83             licenseAgreement.getVersion().getId()), licenseAgreementElement,
84         String.format("Update license agreement with id %s", licenseAgreement.getId()));
85   }
86
87   @Override
88   public LicenseAgreementEntity get(LicenseAgreementEntity licenseAgreement) {
89     SessionContext context = createSessionContext();
90     ElementContext elementContext = new ElementContext(licenseAgreement.getVendorLicenseModelId(),
91         licenseAgreement.getVersion().getId());
92     ElementToLicenseAgreementConvertor convertor = new ElementToLicenseAgreementConvertor();
93     return zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseAgreement.getId()))
94         .map(elementInfo -> {
95           LicenseAgreementEntity entity = convertor.convert(elementInfo);
96           entity.setVendorLicenseModelId(licenseAgreement.getVendorLicenseModelId());
97           entity.setVersion(licenseAgreement.getVersion());
98           return entity;
99         })
100         .orElse(null);
101   }
102
103   @Override
104   public void delete(LicenseAgreementEntity licenseAgreement) {
105     ZusammenElement zusammenElement = buildElement(new Id(licenseAgreement.getId()), Action.DELETE);
106
107     SessionContext context = createSessionContext();
108     ElementContext elementContext = new ElementContext(licenseAgreement.getVendorLicenseModelId(),
109         licenseAgreement.getVersion().getId());
110     zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
111         "delete license agreement. id:" + licenseAgreement.getId() + ".");
112   }
113
114
115   @Override
116   public Collection<LicenseAgreementEntity> list(LicenseAgreementEntity licenseAgreement) {
117     SessionContext context = createSessionContext();
118     ElementContext elementContext = new ElementContext(licenseAgreement.getVendorLicenseModelId(),
119         licenseAgreement.getVersion().getId());
120     ElementToLicenseAgreementConvertor convertor = new ElementToLicenseAgreementConvertor();
121     return zusammenAdaptor
122         .listElementsByName(context, elementContext, null,
123             ElementType.LicenseAgreements.name())
124         .stream().map(elementInfo -> {
125           LicenseAgreementEntity entity = convertor.convert(elementInfo);
126           entity.setVendorLicenseModelId(licenseAgreement.getVendorLicenseModelId());
127           entity.setVersion(licenseAgreement.getVersion());
128           return entity;
129         })
130         .collect(Collectors.toList());
131   }
132
133   @Override
134   public long count(LicenseAgreementEntity licenseAgreement) {
135     SessionContext context = createSessionContext();
136     ElementContext elementContext = new ElementContext(licenseAgreement.getVendorLicenseModelId(),
137         licenseAgreement.getVersion().getId());
138
139     return zusammenAdaptor.listElementsByName(context, elementContext, null,
140         ElementType.LicenseAgreements.name())
141         .size();
142   }
143
144   @Override
145   public void deleteAll(LicenseAgreementEntity entity) {
146     //not supported
147   }
148
149   @Override
150   public void removeFeatureGroup(LicenseAgreementEntity licenseAgreement, String featureGroupId) {
151     SessionContext context = createSessionContext();
152     ElementContext elementContext = new ElementContext(licenseAgreement.getVendorLicenseModelId(),
153         licenseAgreement.getVersion().getId());
154
155     Optional<ElementInfo> elementInfo = zusammenAdaptor.getElementInfo(context,
156         elementContext, new Id(licenseAgreement.getId()));
157     if (elementInfo.isPresent()) {
158       ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
159       zusammenElement.setAction(Action.UPDATE);
160       zusammenElement.setRelations(elementInfo.get().getRelations().stream()
161           .filter(relation -> !featureGroupId.equals(relation.getEdge2().getElementId().getValue()))
162           .collect(Collectors.toList()));
163       zusammenAdaptor.saveElement(context, elementContext, zusammenElement, "remove feature group");
164     }
165   }
166
167   @Override
168   public void updateColumnsAndDeltaFeatureGroupIds(LicenseAgreementEntity licenseAgreement,
169                                                    Set<String> addedFeatureGroupIds,
170                                                    Set<String> removedFeatureGroupIds) {
171     ZusammenElement licenseAgreementElement =
172         buildLicenseAgreementElement(licenseAgreement, Action.UPDATE);
173
174     SessionContext context = createSessionContext();
175     ElementContext elementContext = new ElementContext(licenseAgreement.getVendorLicenseModelId(),
176         licenseAgreement.getVersion().getId());
177     ElementToLicenseAgreementConvertor convertor = new ElementToLicenseAgreementConvertor();
178     Optional<ElementInfo> elementInfo =
179         zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseAgreement.getId()));
180     if (elementInfo.isPresent()) {
181       LicenseAgreementEntity currentLicenseAgreement =
182           convertor.convert(elementInfo.get());
183       currentLicenseAgreement.setVendorLicenseModelId(licenseAgreement.getVendorLicenseModelId());
184       currentLicenseAgreement.setVersion(licenseAgreement.getVersion());
185       if (!(removedFeatureGroupIds == null)) {
186         currentLicenseAgreement.getFeatureGroupIds().removeAll(removedFeatureGroupIds);
187       }
188
189       if (!(addedFeatureGroupIds == null)) {
190         currentLicenseAgreement.getFeatureGroupIds().addAll(addedFeatureGroupIds);
191       }
192       licenseAgreementElement.setRelations(currentLicenseAgreement.getFeatureGroupIds().stream()
193           .map(relation -> VlmZusammenUtil
194               .createRelation(RelationType.LicenseAgreementToFeatureGroup, relation))
195           .collect(Collectors.toList()));
196       zusammenAdaptor.saveElement(context, elementContext, licenseAgreementElement,
197           "update license agreement");
198     }
199   }
200
201   private ZusammenElement buildLicenseAgreementElement(LicenseAgreementEntity licenseAgreement,
202                                                        Action action) {
203     ZusammenElement licenseAgreementElement =
204         buildElement(licenseAgreement.getId() == null ? null : new Id(licenseAgreement.getId()),
205             action);
206     Info info = new Info();
207     info.setName(licenseAgreement.getName());
208     info.setDescription(licenseAgreement.getDescription());
209     info.addProperty(ElementPropertyName.elementType.name(), ElementType.LicenseAgreement);
210     info.addProperty("licenseTerm", licenseAgreement.getLicenseTerm());
211     info.addProperty("requirementsAndConstrains", licenseAgreement.getRequirementsAndConstrains());
212     licenseAgreementElement.setInfo(info);
213
214     if (licenseAgreement.getFeatureGroupIds() != null &&
215         !licenseAgreement.getFeatureGroupIds().isEmpty()) {
216       licenseAgreementElement.setRelations(licenseAgreement.getFeatureGroupIds().stream()
217           .map(rel -> VlmZusammenUtil
218               .createRelation(RelationType.LicenseAgreementToFeatureGroup, rel))
219           .collect(Collectors.toList()));
220     }
221     return licenseAgreementElement;
222   }
223 }