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