re base code
[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 / LicenseAgreementDaoZusammenImpl.java
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.*;
40
41
42 public class LicenseAgreementDaoZusammenImpl implements LicenseAgreementDao {
43
44   private ZusammenAdaptor zusammenAdaptor;
45
46   public LicenseAgreementDaoZusammenImpl(ZusammenAdaptor zusammenAdaptor) {
47     this.zusammenAdaptor = zusammenAdaptor;
48   }
49
50   @Override
51   public void registerVersioning(String versionableEntityType) {
52     //no need
53   }
54
55   @Override
56   public void create(LicenseAgreementEntity licenseAgreement) {
57     ZusammenElement licenseAgreementElement =
58         buildLicenseAgreementElement(licenseAgreement, Action.CREATE);
59     ZusammenElement licenseAgreementsElement =
60         buildStructuralElement(ElementType.LicenseAgreements, Action.IGNORE);
61     licenseAgreementsElement.addSubElement(licenseAgreementElement);
62
63     SessionContext context = createSessionContext();
64     Element licenseAgreementsSavedElement = zusammenAdaptor.saveElement(context,
65         new ElementContext(licenseAgreement.getVendorLicenseModelId(),
66             licenseAgreement.getVersion().getId()), licenseAgreementsElement,
67         "Create license agreement");
68     licenseAgreement
69         .setId(licenseAgreementsSavedElement.getSubElements().iterator().next().getElementId()
70             .getValue());
71   }
72
73   @Override
74   public void update(LicenseAgreementEntity licenseAgreement) {
75     ZusammenElement licenseAgreementElement =
76         buildLicenseAgreementElement(licenseAgreement, Action.UPDATE);
77
78     SessionContext context = createSessionContext();
79     zusammenAdaptor.saveElement(context,
80         new ElementContext(licenseAgreement.getVendorLicenseModelId(),
81             licenseAgreement.getVersion().getId()), licenseAgreementElement,
82         String.format("Update license agreement with id %s", licenseAgreement.getId()));
83   }
84
85   @Override
86   public LicenseAgreementEntity get(LicenseAgreementEntity licenseAgreement) {
87     SessionContext context = createSessionContext();
88     ElementContext elementContext = new ElementContext(licenseAgreement.getVendorLicenseModelId(),
89         licenseAgreement.getVersion().getId());
90     ElementToLicenseAgreementConvertor convertor = new ElementToLicenseAgreementConvertor();
91     return zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseAgreement.getId()))
92         .map(elementInfo -> {
93           LicenseAgreementEntity entity = convertor.convert(elementInfo);
94           entity.setVendorLicenseModelId(licenseAgreement.getVendorLicenseModelId());
95           entity.setVersion(licenseAgreement.getVersion());
96           return entity;
97         })
98         .orElse(null);
99   }
100
101   @Override
102   public void delete(LicenseAgreementEntity licenseAgreement) {
103     ZusammenElement zusammenElement = buildElement(new Id(licenseAgreement.getId()), Action.DELETE);
104
105     SessionContext context = createSessionContext();
106     ElementContext elementContext = new ElementContext(licenseAgreement.getVendorLicenseModelId(),
107         licenseAgreement.getVersion().getId());
108     zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
109         "delete license agreement. id:" + licenseAgreement.getId() + ".");
110   }
111
112
113   @Override
114   public Collection<LicenseAgreementEntity> list(LicenseAgreementEntity licenseAgreement) {
115     SessionContext context = createSessionContext();
116     ElementContext elementContext = new ElementContext(licenseAgreement.getVendorLicenseModelId(),
117         licenseAgreement.getVersion().getId());
118     ElementToLicenseAgreementConvertor convertor = new ElementToLicenseAgreementConvertor();
119     return zusammenAdaptor
120         .listElementsByName(context, elementContext, null,
121             ElementType.LicenseAgreements.name())
122         .stream().map(elementInfo -> {
123           LicenseAgreementEntity entity = convertor.convert(elementInfo);
124           entity.setVendorLicenseModelId(licenseAgreement.getVendorLicenseModelId());
125           entity.setVersion(licenseAgreement.getVersion());
126           return entity;
127         })
128         .collect(Collectors.toList());
129   }
130
131   @Override
132   public long count(LicenseAgreementEntity licenseAgreement) {
133     SessionContext context = createSessionContext();
134     ElementContext elementContext = new ElementContext(licenseAgreement.getVendorLicenseModelId(),
135         licenseAgreement.getVersion().getId());
136
137     return zusammenAdaptor.listElementsByName(context, elementContext, null,
138         ElementType.LicenseAgreements.name())
139         .size();
140   }
141
142   @Override
143   public void deleteAll(LicenseAgreementEntity entity) {
144     //not supported
145   }
146
147   @Override
148   public void removeFeatureGroup(LicenseAgreementEntity licenseAgreement, String featureGroupId) {
149     SessionContext context = createSessionContext();
150     ElementContext elementContext = new ElementContext(licenseAgreement.getVendorLicenseModelId(),
151         licenseAgreement.getVersion().getId());
152
153     Optional<ElementInfo> elementInfo = zusammenAdaptor.getElementInfo(context,
154         elementContext, new Id(licenseAgreement.getId()));
155     if (elementInfo.isPresent()) {
156       ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
157       zusammenElement.setAction(Action.UPDATE);
158       zusammenElement.setRelations(elementInfo.get().getRelations().stream()
159           .filter(relation -> !featureGroupId.equals(relation.getEdge2().getElementId().getValue()))
160           .collect(Collectors.toList()));
161       zusammenAdaptor.saveElement(context, elementContext, zusammenElement, "remove feature group");
162     }
163   }
164
165   @Override
166   public void updateColumnsAndDeltaFeatureGroupIds(LicenseAgreementEntity licenseAgreement,
167                                                    Set<String> addedFeatureGroupIds,
168                                                    Set<String> removedFeatureGroupIds) {
169     ZusammenElement licenseAgreementElement =
170         buildLicenseAgreementElement(licenseAgreement, Action.UPDATE);
171
172     SessionContext context = createSessionContext();
173     ElementContext elementContext = new ElementContext(licenseAgreement.getVendorLicenseModelId(),
174         licenseAgreement.getVersion().getId());
175     ElementToLicenseAgreementConvertor convertor = new ElementToLicenseAgreementConvertor();
176     Optional<ElementInfo> elementInfo =
177         zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseAgreement.getId()));
178     if (elementInfo.isPresent()) {
179       LicenseAgreementEntity currentLicenseAgreement =
180           convertor.convert(elementInfo.get());
181       currentLicenseAgreement.setVendorLicenseModelId(licenseAgreement.getVendorLicenseModelId());
182       currentLicenseAgreement.setVersion(licenseAgreement.getVersion());
183       if (!(removedFeatureGroupIds == null)) {
184         currentLicenseAgreement.getFeatureGroupIds().removeAll(removedFeatureGroupIds);
185       }
186
187       if (!(addedFeatureGroupIds == null)) {
188         currentLicenseAgreement.getFeatureGroupIds().addAll(addedFeatureGroupIds);
189       }
190       licenseAgreementElement.setRelations(currentLicenseAgreement.getFeatureGroupIds().stream()
191           .map(relation -> VlmZusammenUtil
192               .createRelation(RelationType.LicenseAgreementToFeatureGroup, relation))
193           .collect(Collectors.toList()));
194       zusammenAdaptor.saveElement(context, elementContext, licenseAgreementElement,
195           "update license agreement");
196     }
197   }
198
199   private ZusammenElement buildLicenseAgreementElement(LicenseAgreementEntity licenseAgreement,
200                                                        Action action) {
201     ZusammenElement licenseAgreementElement =
202         buildElement(licenseAgreement.getId() == null ? null : new Id(licenseAgreement.getId()),
203             action);
204     Info info = new Info();
205     info.setName(licenseAgreement.getName());
206     info.setDescription(licenseAgreement.getDescription());
207     info.addProperty(ElementPropertyName.elementType.name(), ElementType.LicenseAgreement);
208     info.addProperty("licenseTerm", licenseAgreement.getLicenseTerm());
209     info.addProperty("requirementsAndConstrains", licenseAgreement.getRequirementsAndConstrains());
210     licenseAgreementElement.setInfo(info);
211
212     if (licenseAgreement.getFeatureGroupIds() != null &&
213         !licenseAgreement.getFeatureGroupIds().isEmpty()) {
214       licenseAgreementElement.setRelations(licenseAgreement.getFeatureGroupIds().stream()
215           .map(rel -> VlmZusammenUtil
216               .createRelation(RelationType.LicenseAgreementToFeatureGroup, rel))
217           .collect(Collectors.toList()));
218     }
219     return licenseAgreementElement;
220   }
221 }