aa90df83899c38e0bc8684cca09d4cb61c544e63
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.openecomp.sdc.vendorlicense.dao.impl.zusammen;
21
22 import static org.openecomp.core.zusammen.api.ZusammenUtil.buildElement;
23
24 import com.amdocs.zusammen.adaptor.inbound.api.types.item.Element;
25 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ElementInfo;
26 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ZusammenElement;
27 import com.amdocs.zusammen.datatypes.Id;
28 import com.amdocs.zusammen.datatypes.SessionContext;
29 import com.amdocs.zusammen.datatypes.item.Action;
30 import com.amdocs.zusammen.datatypes.item.ElementContext;
31 import com.amdocs.zusammen.datatypes.item.Info;
32 import java.util.ArrayList;
33 import java.util.Collection;
34 import java.util.Map;
35 import java.util.Optional;
36 import java.util.stream.Collectors;
37 import org.openecomp.core.zusammen.api.ZusammenAdaptor;
38 import org.openecomp.core.zusammen.api.ZusammenUtil;
39 import org.openecomp.sdc.datatypes.model.ElementType;
40 import org.openecomp.sdc.vendorlicense.dao.EntitlementPoolDao;
41 import org.openecomp.sdc.vendorlicense.dao.impl.zusammen.convertor.ElementToEntitlementPoolConvertor;
42 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
43 import org.openecomp.types.ElementPropertyName;
44
45 public class EntitlementPoolZusammenDaoImpl implements EntitlementPoolDao {
46
47     private ZusammenAdaptor zusammenAdaptor;
48
49     public EntitlementPoolZusammenDaoImpl(ZusammenAdaptor zusammenAdaptor) {
50         this.zusammenAdaptor = zusammenAdaptor;
51     }
52
53     @Override
54     public void registerVersioning(String versionableEntityType) {
55         //no need
56     }
57
58     @Override
59     public void create(EntitlementPoolEntity entitlementPool) {
60         ZusammenElement entitlementPoolElement = buildEntitlementPoolElement(entitlementPool, Action.CREATE);
61         ZusammenElement entitlementPoolsElement = ZusammenUtil.buildStructuralElement(ElementType.EntitlementPools, Action.IGNORE);
62         ZusammenElement limitsElement = ZusammenUtil.buildStructuralElement(ElementType.Limits, Action.CREATE);
63         entitlementPoolElement.addSubElement(limitsElement);
64         entitlementPoolsElement.addSubElement(entitlementPoolElement);
65         SessionContext context = ZusammenUtil.createSessionContext();
66         Element epsSavedElement = zusammenAdaptor
67             .saveElement(context, new ElementContext(entitlementPool.getVendorLicenseModelId(), entitlementPool.getVersion().getId()),
68                 entitlementPoolsElement, "Create entitlement pool");
69         entitlementPool.setId(epsSavedElement.getSubElements().iterator().next().getElementId().getValue());
70     }
71
72     @Override
73     public void update(EntitlementPoolEntity entitlementPool) {
74         ZusammenElement entitlmentpoolElement = buildEntitlementPoolElement(entitlementPool, Action.UPDATE);
75         SessionContext context = ZusammenUtil.createSessionContext();
76         ElementContext elementContext = new ElementContext(entitlementPool.getVendorLicenseModelId(), entitlementPool.getVersion().getId());
77         Optional<ElementInfo> epFromDb = zusammenAdaptor.getElementInfo(context, elementContext, new Id(entitlementPool.getId()));
78         if (epFromDb.isPresent()) {
79             if (entitlmentpoolElement.getRelations() == null) {
80                 entitlmentpoolElement.setRelations(new ArrayList<>());
81             }
82             if (epFromDb.get().getRelations() != null && !epFromDb.get().getRelations().isEmpty()) {
83                 entitlmentpoolElement.getRelations().addAll(epFromDb.get().getRelations());
84             }
85         }
86         zusammenAdaptor.saveElement(context, elementContext, entitlmentpoolElement,
87             String.format("Update entitlement pool with id %s", entitlementPool.getId()));
88     }
89
90     @Override
91     public EntitlementPoolEntity get(EntitlementPoolEntity entitlementPool) {
92         SessionContext context = ZusammenUtil.createSessionContext();
93         ElementContext elementContext = new ElementContext(entitlementPool.getVendorLicenseModelId(), entitlementPool.getVersion().getId());
94         ElementToEntitlementPoolConvertor convertor = new ElementToEntitlementPoolConvertor();
95         return zusammenAdaptor.getElementInfo(context, elementContext, new Id(entitlementPool.getId())).map(elementInfo -> {
96             EntitlementPoolEntity entity = convertor.convert(elementInfo);
97             entity.setVendorLicenseModelId(entitlementPool.getVendorLicenseModelId());
98             entity.setVersion(entitlementPool.getVersion());
99             return entity;
100         }).orElse(null);
101     }
102
103     @Override
104     public void delete(EntitlementPoolEntity entitlementPool) {
105         ZusammenElement zusammenElement = buildElement(new Id(entitlementPool.getId()), Action.DELETE);
106         SessionContext context = ZusammenUtil.createSessionContext();
107         ElementContext elementContext = new ElementContext(entitlementPool.getVendorLicenseModelId(), entitlementPool.getVersion().getId());
108         zusammenAdaptor.saveElement(context, elementContext, zusammenElement, "delete entitlement pool. id:" + entitlementPool.getId() + ".");
109     }
110
111     @Override
112     public Collection<EntitlementPoolEntity> list(EntitlementPoolEntity entitlementPool) {
113         SessionContext context = ZusammenUtil.createSessionContext();
114         ElementContext elementContext = new ElementContext(entitlementPool.getVendorLicenseModelId(), entitlementPool.getVersion().getId());
115         ElementToEntitlementPoolConvertor convertor = new ElementToEntitlementPoolConvertor();
116         return zusammenAdaptor.listElementsByName(context, elementContext, null, ElementType.EntitlementPools.name()).stream().map(elementInfo -> {
117             EntitlementPoolEntity entity = convertor.convert(elementInfo);
118             entity.setVendorLicenseModelId(entitlementPool.getVendorLicenseModelId());
119             entity.setVersion(entitlementPool.getVersion());
120             return entity;
121         }).collect(Collectors.toList());
122     }
123
124     @Override
125     public long count(EntitlementPoolEntity entitlementPool) {
126         SessionContext context = ZusammenUtil.createSessionContext();
127         ElementContext elementContext = new ElementContext(entitlementPool.getVendorLicenseModelId(), entitlementPool.getVersion().getId());
128         return zusammenAdaptor.listElementsByName(context, elementContext, null, ElementType.EntitlementPools.name()).size();
129     }
130
131     @Override
132     public void removeReferencingFeatureGroup(EntitlementPoolEntity entitlementPool, String referencingFeatureGroupId) {
133         SessionContext context = ZusammenUtil.createSessionContext();
134         ElementContext elementContext = new ElementContext(entitlementPool.getVendorLicenseModelId(), entitlementPool.getVersion().getId());
135         Optional<ElementInfo> elementInfo = zusammenAdaptor.getElementInfo(context, elementContext, new Id(entitlementPool.getId()));
136         if (elementInfo.isPresent()) {
137             ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
138             zusammenElement.setAction(Action.UPDATE);
139             zusammenElement.setRelations(elementInfo.get().getRelations().stream()
140                 .filter(relation -> !referencingFeatureGroupId.equals(relation.getEdge2().getElementId().getValue())).collect(Collectors.toList()));
141             zusammenAdaptor.saveElement(context, elementContext, zusammenElement, "remove referencing feature group");
142         }
143     }
144
145     @Override
146     public void addReferencingFeatureGroup(EntitlementPoolEntity entitlementPool, String referencingFeatureGroupId) {
147         SessionContext context = ZusammenUtil.createSessionContext();
148         ElementContext elementContext = new ElementContext(entitlementPool.getVendorLicenseModelId(), entitlementPool.getVersion().getId());
149         Optional<ElementInfo> elementInfo = zusammenAdaptor.getElementInfo(context, elementContext, new Id(entitlementPool.getId()));
150         if (elementInfo.isPresent()) {
151             ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
152             zusammenElement.setAction(Action.UPDATE);
153             if (zusammenElement.getRelations() == null) {
154                 zusammenElement.setRelations(new ArrayList<>());
155             }
156             zusammenElement.getRelations()
157                 .add(VlmZusammenUtil.createRelation(RelationType.EntitlmentPoolToReferencingFeatureGroup, referencingFeatureGroupId));
158             zusammenAdaptor.saveElement(context, elementContext, zusammenElement, "add referencing feature group");
159         }
160     }
161
162     @Override
163     public void deleteAll(EntitlementPoolEntity entitlementPool) {
164         //not supported
165     }
166
167     @Override
168     public String getManufacturerReferenceNumber(EntitlementPoolEntity entitlementPoolEntity) {
169         SessionContext context = ZusammenUtil.createSessionContext();
170         ElementContext elementContext = new ElementContext(entitlementPoolEntity.getVendorLicenseModelId(),
171             entitlementPoolEntity.getVersion().getId());
172         Optional<ElementInfo> elementInfo1 = zusammenAdaptor.getElementInfo(context, elementContext, new Id(entitlementPoolEntity.getId()));
173         Map<String, Object> properties = elementInfo1.get().getInfo().getProperties();
174         String manufacturerReferenceNumber = null;
175         if (properties != null && properties.containsKey("manufacturerReferenceNumber")) {
176             manufacturerReferenceNumber = (String) properties.get("manufacturerReferenceNumber");
177         }
178         return manufacturerReferenceNumber;
179     }
180
181     private ZusammenElement buildEntitlementPoolElement(EntitlementPoolEntity entitlementPool, Action action) {
182         ZusammenElement entitlementPoolElement = buildElement(entitlementPool.getId() == null ? null : new Id(entitlementPool.getId()), action);
183         Info info = new Info();
184         info.setName(entitlementPool.getName());
185         info.setDescription(entitlementPool.getDescription());
186         info.addProperty(ElementPropertyName.elementType.name(), ElementType.EntitlementPool);
187         info.addProperty("version_uuid", entitlementPool.getVersionUuId());
188         info.addProperty("EntitlementPoolType", entitlementPool.getType());
189         info.addProperty("thresholdValue", entitlementPool.getThresholdValue());
190         info.addProperty("threshold_unit", entitlementPool.getThresholdUnit());
191         info.addProperty("increments", entitlementPool.getIncrements());
192         info.addProperty("operational_scope", entitlementPool.getOperationalScope());
193         info.addProperty("startDate", entitlementPool.getStartDate());
194         info.addProperty("expiryDate", entitlementPool.getExpiryDate());
195         info.addProperty("manufacturerReferenceNumber", entitlementPool.getManufacturerReferenceNumber());
196         entitlementPoolElement.setInfo(info);
197         if (entitlementPool.getReferencingFeatureGroups() != null && !entitlementPool.getReferencingFeatureGroups().isEmpty()) {
198             entitlementPoolElement.setRelations(entitlementPool.getReferencingFeatureGroups().stream()
199                 .map(rel -> VlmZusammenUtil.createRelation(RelationType.EntitlmentPoolToReferencingFeatureGroup, rel)).collect(Collectors.toList()));
200         }
201         return entitlementPoolElement;
202     }
203 }