1 package org.openecomp.sdc.vendorlicense.dao.impl.zusammen;
3 import com.amdocs.zusammen.adaptor.inbound.api.types.item.Element;
4 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ElementInfo;
5 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ZusammenElement;
6 import com.amdocs.zusammen.datatypes.Id;
7 import com.amdocs.zusammen.datatypes.SessionContext;
8 import com.amdocs.zusammen.datatypes.item.Action;
9 import com.amdocs.zusammen.datatypes.item.ElementContext;
10 import com.amdocs.zusammen.datatypes.item.Info;
11 import org.openecomp.core.zusammen.api.ZusammenAdaptor;
12 import org.openecomp.sdc.datatypes.model.ElementType;
13 import org.openecomp.sdc.vendorlicense.dao.LimitDao;
14 import org.openecomp.sdc.vendorlicense.dao.types.AggregationFunction;
15 import org.openecomp.sdc.vendorlicense.dao.types.LimitEntity;
16 import org.openecomp.sdc.vendorlicense.dao.types.LimitType;
17 import org.openecomp.sdc.versioning.dao.types.Version;
18 import org.openecomp.types.ElementPropertyName;
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.stream.Collectors;
24 import static org.openecomp.core.zusammen.api.ZusammenUtil.*;
26 public class LimitZusammenDaoImpl implements LimitDao {
28 private static final String LIMT_TYPE = "type";
29 private static final String METRIC = "metric";
30 private static final String AGGREGATIONFUNCTION = "aggregationfunction";
31 private static final String TIME = "time";
32 private static final String UNIT = "unit";
33 private static final String VALUE = "value";
34 private ZusammenAdaptor zusammenAdaptor;
36 public LimitZusammenDaoImpl(ZusammenAdaptor zusammenAdaptor) {
37 this.zusammenAdaptor = zusammenAdaptor;
41 public void create(LimitEntity limitEntity) {
42 ZusammenElement limitElement = limitToZusammen(limitEntity, Action.CREATE);
44 ZusammenElement limitsElement = buildStructuralElement(ElementType.Limits, null);
45 limitsElement.setSubElements(Collections.singletonList(limitElement));
47 ZusammenElement epLkgElement = buildElement(new Id(limitEntity.getEpLkgId()), Action.IGNORE);
48 epLkgElement.setSubElements(Collections.singletonList(limitsElement));
50 SessionContext context = createSessionContext();
51 ElementContext elementContext =
52 new ElementContext(limitEntity.getVendorLicenseModelId(), limitEntity.getVersion().getId());
54 Element savedElement =
55 zusammenAdaptor.saveElement(context, elementContext, epLkgElement, "Create limit");
56 limitEntity.setId(savedElement.getSubElements().iterator().next()
57 .getSubElements().iterator().next().getElementId().getValue());
61 public boolean isLimitPresent(LimitEntity limitEntity) {
62 SessionContext context = createSessionContext();
63 ElementContext elementContext =
64 new ElementContext(limitEntity.getVendorLicenseModelId(), limitEntity.getVersion().getId());
66 Collection<ElementInfo> elementInfos = zusammenAdaptor.listElementsByName(context,
67 elementContext, new Id(limitEntity.getEpLkgId()), ElementType.Limits.name());
69 for (ElementInfo elementInfo : elementInfos) {
70 if (elementInfo.getId().getValue().equals(limitEntity.getId())) {
79 public Collection<LimitEntity> list(LimitEntity limitEntity) {
80 SessionContext context = createSessionContext();
81 ElementContext elementContext =
82 new ElementContext(limitEntity.getVendorLicenseModelId(), limitEntity.getVersion().getId());
84 return listLimits(context, elementContext, limitEntity);
87 private Collection<LimitEntity> listLimits(SessionContext context, ElementContext elementContext,
88 LimitEntity limitEntity) {
89 return zusammenAdaptor
90 .listElementsByName(context, elementContext, new Id(limitEntity.getEpLkgId()),
91 ElementType.Limits.name())
92 .stream().map(elementInfo -> mapElementInfoToLimit(
93 limitEntity.getVendorLicenseModelId(), limitEntity.getVersion(),
94 limitEntity.getEpLkgId(), elementInfo))
95 .collect(Collectors.toList());
98 private LimitEntity mapElementInfoToLimit(String vlmId, Version version,
99 String epLkgId, ElementInfo elementInfo) {
100 LimitEntity limitEntity =
101 new LimitEntity(vlmId, version, epLkgId, elementInfo.getId().getValue());
103 limitEntity.setName(elementInfo.getInfo().getName());
104 limitEntity.setDescription(elementInfo.getInfo().getDescription());
105 limitEntity.setType(elementInfo.getInfo().getProperties().get(LIMT_TYPE) != null ?
106 LimitType.valueOf((String) elementInfo.getInfo().getProperties().get(LIMT_TYPE)) :
108 limitEntity.setTime((String) elementInfo.getInfo().getProperties().get(TIME));
109 limitEntity.setMetric((String) elementInfo.getInfo().getProperties().get(METRIC));
110 limitEntity.setAggregationFunction(elementInfo.getInfo().getProperties().get
111 (AGGREGATIONFUNCTION) != null ?
112 AggregationFunction.valueOf((String) elementInfo.getInfo().getProperties()
113 .get(AGGREGATIONFUNCTION)) : null);
114 Object unit = elementInfo.getInfo().getProperties().get(UNIT);
115 limitEntity.setUnit((String) unit);
116 Object value = elementInfo.getInfo().getProperties().get(VALUE);
117 limitEntity.setValue((String) value);
123 public void update(LimitEntity limitEntity) {
124 ZusammenElement limitElement = limitToZusammen(limitEntity, Action.UPDATE);
126 SessionContext context = createSessionContext();
127 ElementContext elementContext =
128 new ElementContext(limitEntity.getVendorLicenseModelId(), limitEntity.getVersion().getId());
130 zusammenAdaptor.saveElement(context, elementContext, limitElement,
131 String.format("Update limit with id %s", limitEntity.getId()));
135 public LimitEntity get(LimitEntity limitEntity) {
136 SessionContext context = createSessionContext();
137 ElementContext elementContext =
138 new ElementContext(limitEntity.getVendorLicenseModelId(), limitEntity.getVersion().getId());
140 return zusammenAdaptor.getElementInfo(context, elementContext, new Id(limitEntity.getId()))
141 .map(elementInfo -> mapElementInfoToLimit(
142 limitEntity.getVendorLicenseModelId(), limitEntity.getVersion(), limitEntity
143 .getEpLkgId(), elementInfo))
148 public void delete(LimitEntity limitEntity) {
149 ZusammenElement zusammenElement = buildElement(new Id(limitEntity.getId()), Action.DELETE);
151 SessionContext context = createSessionContext();
152 ElementContext elementContext =
153 new ElementContext(limitEntity.getVendorLicenseModelId(), limitEntity.getVersion().getId());
154 zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
155 "delete limit Id:" + limitEntity.getId() + ".");
159 public void registerVersioning(String versionableEntityType) {
160 //registerVersioning not implemented for LimitZusammenDaoImpl
163 private ZusammenElement limitToZusammen(LimitEntity limit, Action action) {
164 ZusammenElement limitElement =
165 buildElement(limit.getId() == null ? null : new Id(limit.getId()), action);
166 Info info = new Info();
167 info.setName(limit.getName());
168 info.setDescription(limit.getDescription());
169 info.addProperty(ElementPropertyName.elementType.name(), ElementType.Limit);
170 info.addProperty(LIMT_TYPE, limit.getType());
171 info.addProperty(METRIC, limit.getMetric());
172 info.addProperty(AGGREGATIONFUNCTION, limit.getAggregationFunction());
173 info.addProperty(TIME, limit.getTime());
174 info.addProperty(VALUE, limit.getValue());
175 info.addProperty(UNIT, limit.getUnit());
176 limitElement.setInfo(info);