2c4702aec149bcb149d26db65f90a17aeabb8e0d
[sdc.git] /
1 /*
2  * Copyright © 2016-2018 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 package org.openecomp.sdc.vendorlicense.dao.impl.zusammen;
17
18 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ZusammenElement;
19 import com.amdocs.zusammen.datatypes.SessionContext;
20 import com.amdocs.zusammen.datatypes.item.Action;
21 import com.amdocs.zusammen.datatypes.item.ElementContext;
22 import com.amdocs.zusammen.datatypes.item.Info;
23 import java.util.Collection;
24 import java.util.stream.Collectors;
25 import org.openecomp.core.zusammen.api.ZusammenAdaptor;
26 import org.openecomp.core.zusammen.api.ZusammenUtil;
27 import org.openecomp.sdc.datatypes.model.ElementType;
28 import org.openecomp.sdc.vendorlicense.dao.VendorLicenseModelDao;
29 import org.openecomp.sdc.vendorlicense.dao.impl.zusammen.convertor.ElementToVLMGeneralConvertor;
30 import org.openecomp.sdc.vendorlicense.dao.types.VendorLicenseModelEntity;
31 import org.openecomp.sdc.versioning.ActionVersioningManagerFactory;
32 import org.openecomp.sdc.versioning.types.VersionableEntityMetadata;
33 import org.openecomp.sdc.versioning.types.VersionableEntityStoreType;
34
35 public class VendorLicenseModelDaoZusammenImpl implements VendorLicenseModelDao {
36
37     private ZusammenAdaptor zusammenAdaptor;
38
39     public VendorLicenseModelDaoZusammenImpl(ZusammenAdaptor zusammenAdaptor) {
40         this.zusammenAdaptor = zusammenAdaptor;
41     }
42
43     @Override
44     public void registerVersioning(String versionableEntityType) {
45         VersionableEntityMetadata metadata = new VersionableEntityMetadata(VersionableEntityStoreType.Zusammen, "VendorLicenseModel", null, null);
46         ActionVersioningManagerFactory.getInstance().createInterface().register(versionableEntityType, metadata);
47     }
48
49     @Override
50     public Collection<VendorLicenseModelEntity> list(VendorLicenseModelEntity vendorLicenseModelEntity) {
51         ElementToVLMGeneralConvertor convertor = new ElementToVLMGeneralConvertor();
52         return zusammenAdaptor.listItems(ZusammenUtil.createSessionContext()).stream()
53             .filter(item -> "VendorLicenseModel".equals(item.getInfo().getProperty("item_type"))).map(item -> {
54                 VendorLicenseModelEntity entity = convertor.convert(item);
55                 entity.setId(item.getId().getValue());
56                 entity.setVersion(null);
57                 return entity;
58             }).collect(Collectors.toList());
59     }
60
61     @Override
62     public void create(VendorLicenseModelEntity vendorLicenseModel) {
63         SessionContext context = ZusammenUtil.createSessionContext();
64         ElementContext elementContext = new ElementContext(vendorLicenseModel.getId(), vendorLicenseModel.getVersion().getId());
65         ZusammenElement generalElement = mapVlmToZusammenElement(vendorLicenseModel, Action.CREATE);
66         zusammenAdaptor.saveElement(context, elementContext, generalElement, "Create VLM General Info Element");
67         ZusammenElement licenseAgreementsElement = ZusammenUtil.buildStructuralElement(ElementType.LicenseAgreements, Action.CREATE);
68         zusammenAdaptor.saveElement(context, elementContext, licenseAgreementsElement, "Create VLM licenseAgreementsElement");
69         ZusammenElement featureGroupsElement = ZusammenUtil.buildStructuralElement(ElementType.FeatureGroups, Action.CREATE);
70         zusammenAdaptor.saveElement(context, elementContext, featureGroupsElement, "Create VLM featureGroupsElement");
71         ZusammenElement lkgsElement = ZusammenUtil.buildStructuralElement(ElementType.LicenseKeyGroups, Action.CREATE);
72         zusammenAdaptor.saveElement(context, elementContext, lkgsElement, "Create VLM lkgsElement");
73         ZusammenElement entitlementPoolsElement = ZusammenUtil.buildStructuralElement(ElementType.EntitlementPools, Action.CREATE);
74         zusammenAdaptor.saveElement(context, elementContext, entitlementPoolsElement, "Create VLM entitlementPoolsElement");
75     }
76
77     @Override
78     public void update(VendorLicenseModelEntity vendorLicenseModel) {
79         ZusammenElement generalElement = mapVlmToZusammenElement(vendorLicenseModel, Action.UPDATE);
80         SessionContext context = ZusammenUtil.createSessionContext();
81         zusammenAdaptor.saveElement(context, new ElementContext(vendorLicenseModel.getId(), vendorLicenseModel.getVersion().getId()), generalElement,
82             "Update VSP General Info Element");
83     }
84
85     @Override
86     public VendorLicenseModelEntity get(VendorLicenseModelEntity vendorLicenseModel) {
87         SessionContext context = ZusammenUtil.createSessionContext();
88         ElementContext elementContext = new ElementContext(vendorLicenseModel.getId(), vendorLicenseModel.getVersion().getId());
89         ElementToVLMGeneralConvertor convertor = new ElementToVLMGeneralConvertor();
90         return zusammenAdaptor.getElementInfoByName(context, elementContext, null, ElementType.VendorLicenseModel.name()).map(generalElementInfo -> {
91             VendorLicenseModelEntity entity = convertor.convert(generalElementInfo);
92             entity.setId(vendorLicenseModel.getId());
93             entity.setVersion(vendorLicenseModel.getVersion());
94             return entity;
95         }).orElse(null);
96     }
97
98     @Override
99     public void delete(VendorLicenseModelEntity entity) {
100         throw new UnsupportedOperationException("Delete vlm version is done using versioning manager");
101     }
102
103     private ZusammenElement mapVlmToZusammenElement(VendorLicenseModelEntity vendorLicenseModel, Action action) {
104         ZusammenElement generalElement = ZusammenUtil.buildStructuralElement(ElementType.VendorLicenseModel, action);
105         addVlmToInfo(generalElement.getInfo(), vendorLicenseModel);
106         return generalElement;
107     }
108
109     private void addVlmToInfo(Info info, VendorLicenseModelEntity vendorLicenseModel) {
110         info.addProperty(InfoPropertyName.name.name(), vendorLicenseModel.getVendorName());
111         info.addProperty(InfoPropertyName.description.name(), vendorLicenseModel.getDescription());
112         info.addProperty(InfoPropertyName.iconRef.name(), vendorLicenseModel.getIconRef());
113     }
114
115     public enum InfoPropertyName {name, description, iconRef,}
116 }