2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.openecomp.sdc.vendorlicense.dao.impl.zusammen;
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;
39 import java.util.Collection;
40 import java.util.Collections;
41 import java.util.stream.Collectors;
43 import static org.openecomp.core.zusammen.api.ZusammenUtil.*;
45 public class LimitZusammenDaoImpl implements LimitDao {
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;
55 public LimitZusammenDaoImpl(ZusammenAdaptor zusammenAdaptor) {
56 this.zusammenAdaptor = zusammenAdaptor;
60 public void create(LimitEntity limitEntity) {
61 ZusammenElement limitElement = limitToZusammen(limitEntity, Action.CREATE);
63 ZusammenElement limitsElement = buildStructuralElement(ElementType.Limits, null);
64 limitsElement.setSubElements(Collections.singletonList(limitElement));
66 ZusammenElement epLkgElement = buildElement(new Id(limitEntity.getEpLkgId()), Action.IGNORE);
67 epLkgElement.setSubElements(Collections.singletonList(limitsElement));
69 SessionContext context = createSessionContext();
70 ElementContext elementContext =
71 new ElementContext(limitEntity.getVendorLicenseModelId(), limitEntity.getVersion().getId());
73 Element savedElement =
74 zusammenAdaptor.saveElement(context, elementContext, epLkgElement, "Create limit");
75 limitEntity.setId(savedElement.getSubElements().iterator().next()
76 .getSubElements().iterator().next().getElementId().getValue());
80 public boolean isLimitPresent(LimitEntity limitEntity) {
81 SessionContext context = createSessionContext();
82 ElementContext elementContext =
83 new ElementContext(limitEntity.getVendorLicenseModelId(), limitEntity.getVersion().getId());
85 Collection<ElementInfo> elementInfos = zusammenAdaptor.listElementsByName(context,
86 elementContext, new Id(limitEntity.getEpLkgId()), ElementType.Limits.name());
88 for (ElementInfo elementInfo : elementInfos) {
89 if (elementInfo.getId().getValue().equals(limitEntity.getId())) {
98 public Collection<LimitEntity> list(LimitEntity limitEntity) {
99 SessionContext context = createSessionContext();
100 ElementContext elementContext =
101 new ElementContext(limitEntity.getVendorLicenseModelId(), limitEntity.getVersion().getId());
103 return listLimits(context, elementContext, limitEntity);
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());
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());
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)) :
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);
142 public void update(LimitEntity limitEntity) {
143 ZusammenElement limitElement = limitToZusammen(limitEntity, Action.UPDATE);
145 SessionContext context = createSessionContext();
146 ElementContext elementContext =
147 new ElementContext(limitEntity.getVendorLicenseModelId(), limitEntity.getVersion().getId());
149 zusammenAdaptor.saveElement(context, elementContext, limitElement,
150 String.format("Update limit with id %s", limitEntity.getId()));
154 public LimitEntity get(LimitEntity limitEntity) {
155 SessionContext context = createSessionContext();
156 ElementContext elementContext =
157 new ElementContext(limitEntity.getVendorLicenseModelId(), limitEntity.getVersion().getId());
159 return zusammenAdaptor.getElementInfo(context, elementContext, new Id(limitEntity.getId()))
160 .map(elementInfo -> mapElementInfoToLimit(
161 limitEntity.getVendorLicenseModelId(), limitEntity.getVersion(), limitEntity
162 .getEpLkgId(), elementInfo))
167 public void delete(LimitEntity limitEntity) {
168 ZusammenElement zusammenElement = buildElement(new Id(limitEntity.getId()), Action.DELETE);
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() + ".");
178 public void registerVersioning(String versionableEntityType) {
179 //registerVersioning not implemented for LimitZusammenDaoImpl
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);