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