Added oparent to sdc main
[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 / LimitZusammenDaoImpl.java
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 com.amdocs.zusammen.adaptor.inbound.api.types.item.Element;
23 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ElementInfo;
24 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ZusammenElement;
25 import com.amdocs.zusammen.datatypes.Id;
26 import com.amdocs.zusammen.datatypes.SessionContext;
27 import com.amdocs.zusammen.datatypes.item.Action;
28 import com.amdocs.zusammen.datatypes.item.ElementContext;
29 import com.amdocs.zusammen.datatypes.item.Info;
30 import org.openecomp.core.zusammen.api.ZusammenAdaptor;
31 import org.openecomp.sdc.datatypes.model.ElementType;
32 import org.openecomp.sdc.vendorlicense.dao.LimitDao;
33 import org.openecomp.sdc.vendorlicense.dao.types.AggregationFunction;
34 import org.openecomp.sdc.vendorlicense.dao.types.LimitEntity;
35 import org.openecomp.sdc.vendorlicense.dao.types.LimitType;
36 import org.openecomp.sdc.versioning.dao.types.Version;
37 import org.openecomp.types.ElementPropertyName;
38
39 import java.util.Collection;
40 import java.util.Collections;
41 import java.util.stream.Collectors;
42
43 import static org.openecomp.core.zusammen.api.ZusammenUtil.*;
44
45 public class LimitZusammenDaoImpl implements LimitDao {
46
47   private static final String LIMT_TYPE = "type";
48   private static final String METRIC = "metric";
49   private static final String AGGREGATIONFUNCTION = "aggregationfunction";
50   private static final String TIME = "time";
51   private static final String UNIT = "unit";
52   private static final String VALUE = "value";
53   private ZusammenAdaptor zusammenAdaptor;
54
55   public LimitZusammenDaoImpl(ZusammenAdaptor zusammenAdaptor) {
56     this.zusammenAdaptor = zusammenAdaptor;
57   }
58
59   @Override
60   public void create(LimitEntity limitEntity) {
61     ZusammenElement limitElement = limitToZusammen(limitEntity, Action.CREATE);
62
63     ZusammenElement limitsElement = buildStructuralElement(ElementType.Limits, null);
64     limitsElement.setSubElements(Collections.singletonList(limitElement));
65
66     ZusammenElement epLkgElement = buildElement(new Id(limitEntity.getEpLkgId()), Action.IGNORE);
67     epLkgElement.setSubElements(Collections.singletonList(limitsElement));
68
69     SessionContext context = createSessionContext();
70     ElementContext elementContext =
71         new ElementContext(limitEntity.getVendorLicenseModelId(), limitEntity.getVersion().getId());
72
73     Element savedElement =
74         zusammenAdaptor.saveElement(context, elementContext, epLkgElement, "Create limit");
75     limitEntity.setId(savedElement.getSubElements().iterator().next()
76         .getSubElements().iterator().next().getElementId().getValue());
77   }
78
79   @Override
80   public boolean isLimitPresent(LimitEntity limitEntity) {
81     SessionContext context = createSessionContext();
82     ElementContext elementContext =
83         new ElementContext(limitEntity.getVendorLicenseModelId(), limitEntity.getVersion().getId());
84
85     Collection<ElementInfo> elementInfos = zusammenAdaptor.listElementsByName(context,
86         elementContext, new Id(limitEntity.getEpLkgId()), ElementType.Limits.name());
87
88     for (ElementInfo elementInfo : elementInfos) {
89       if (elementInfo.getId().getValue().equals(limitEntity.getId())) {
90         return true;
91       }
92     }
93
94     return false;
95   }
96
97   @Override
98   public Collection<LimitEntity> list(LimitEntity limitEntity) {
99     SessionContext context = createSessionContext();
100     ElementContext elementContext =
101         new ElementContext(limitEntity.getVendorLicenseModelId(), limitEntity.getVersion().getId());
102
103     return listLimits(context, elementContext, limitEntity);
104   }
105
106   private Collection<LimitEntity> listLimits(SessionContext context, ElementContext elementContext,
107                                              LimitEntity limitEntity) {
108     return zusammenAdaptor
109         .listElementsByName(context, elementContext, new Id(limitEntity.getEpLkgId()),
110             ElementType.Limits.name())
111         .stream().map(elementInfo -> mapElementInfoToLimit(
112             limitEntity.getVendorLicenseModelId(), limitEntity.getVersion(),
113             limitEntity.getEpLkgId(), elementInfo))
114         .collect(Collectors.toList());
115   }
116
117   private LimitEntity mapElementInfoToLimit(String vlmId, Version version,
118                                             String epLkgId, ElementInfo elementInfo) {
119     LimitEntity limitEntity =
120         new LimitEntity(vlmId, version, epLkgId, elementInfo.getId().getValue());
121
122     limitEntity.setName(elementInfo.getInfo().getName());
123     limitEntity.setDescription(elementInfo.getInfo().getDescription());
124     limitEntity.setType(elementInfo.getInfo().getProperties().get(LIMT_TYPE) != null ?
125         LimitType.valueOf((String) elementInfo.getInfo().getProperties().get(LIMT_TYPE)) :
126         null);
127     limitEntity.setTime((String) elementInfo.getInfo().getProperties().get(TIME));
128     limitEntity.setMetric((String) elementInfo.getInfo().getProperties().get(METRIC));
129     limitEntity.setAggregationFunction(elementInfo.getInfo().getProperties().get
130         (AGGREGATIONFUNCTION) != null ?
131         AggregationFunction.valueOf((String) elementInfo.getInfo().getProperties()
132             .get(AGGREGATIONFUNCTION)) : null);
133     Object unit = elementInfo.getInfo().getProperties().get(UNIT);
134     limitEntity.setUnit((String) unit);
135     Object value = elementInfo.getInfo().getProperties().get(VALUE);
136     limitEntity.setValue((String) value);
137
138     return limitEntity;
139   }
140
141   @Override
142   public void update(LimitEntity limitEntity) {
143     ZusammenElement limitElement = limitToZusammen(limitEntity, Action.UPDATE);
144
145     SessionContext context = createSessionContext();
146     ElementContext elementContext =
147         new ElementContext(limitEntity.getVendorLicenseModelId(), limitEntity.getVersion().getId());
148
149     zusammenAdaptor.saveElement(context, elementContext, limitElement,
150         String.format("Update limit with id %s", limitEntity.getId()));
151   }
152
153   @Override
154   public LimitEntity get(LimitEntity limitEntity) {
155     SessionContext context = createSessionContext();
156     ElementContext elementContext =
157         new ElementContext(limitEntity.getVendorLicenseModelId(), limitEntity.getVersion().getId());
158
159     return zusammenAdaptor.getElementInfo(context, elementContext, new Id(limitEntity.getId()))
160         .map(elementInfo -> mapElementInfoToLimit(
161             limitEntity.getVendorLicenseModelId(), limitEntity.getVersion(), limitEntity
162                 .getEpLkgId(), elementInfo))
163         .orElse(null);
164   }
165
166   @Override
167   public void delete(LimitEntity limitEntity) {
168     ZusammenElement zusammenElement = buildElement(new Id(limitEntity.getId()), Action.DELETE);
169
170     SessionContext context = createSessionContext();
171     ElementContext elementContext =
172         new ElementContext(limitEntity.getVendorLicenseModelId(), limitEntity.getVersion().getId());
173     zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
174         "delete limit Id:" + limitEntity.getId() + ".");
175   }
176
177   @Override
178   public void registerVersioning(String versionableEntityType) {
179     //registerVersioning not implemented for LimitZusammenDaoImpl
180   }
181
182   private ZusammenElement limitToZusammen(LimitEntity limit, Action action) {
183     ZusammenElement limitElement =
184         buildElement(limit.getId() == null ? null : new Id(limit.getId()), action);
185     Info info = new Info();
186     info.setName(limit.getName());
187     info.setDescription(limit.getDescription());
188     info.addProperty(ElementPropertyName.elementType.name(), ElementType.Limit);
189     info.addProperty(LIMT_TYPE, limit.getType());
190     info.addProperty(METRIC, limit.getMetric());
191     info.addProperty(AGGREGATIONFUNCTION, limit.getAggregationFunction());
192     info.addProperty(TIME, limit.getTime());
193     info.addProperty(VALUE, limit.getValue());
194     info.addProperty(UNIT, limit.getUnit());
195     limitElement.setInfo(info);
196     return limitElement;
197   }
198 }