re base code
[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 package org.openecomp.sdc.vendorlicense.dao.impl.zusammen;
2
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;
19
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.stream.Collectors;
23
24 import static org.openecomp.core.zusammen.api.ZusammenUtil.*;
25
26 public class LimitZusammenDaoImpl implements LimitDao {
27
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;
35
36   public LimitZusammenDaoImpl(ZusammenAdaptor zusammenAdaptor) {
37     this.zusammenAdaptor = zusammenAdaptor;
38   }
39
40   @Override
41   public void create(LimitEntity limitEntity) {
42     ZusammenElement limitElement = limitToZusammen(limitEntity, Action.CREATE);
43
44     ZusammenElement limitsElement = buildStructuralElement(ElementType.Limits, null);
45     limitsElement.setSubElements(Collections.singletonList(limitElement));
46
47     ZusammenElement epLkgElement = buildElement(new Id(limitEntity.getEpLkgId()), Action.IGNORE);
48     epLkgElement.setSubElements(Collections.singletonList(limitsElement));
49
50     SessionContext context = createSessionContext();
51     ElementContext elementContext =
52         new ElementContext(limitEntity.getVendorLicenseModelId(), limitEntity.getVersion().getId());
53
54     Element savedElement =
55         zusammenAdaptor.saveElement(context, elementContext, epLkgElement, "Create limit");
56     limitEntity.setId(savedElement.getSubElements().iterator().next()
57         .getSubElements().iterator().next().getElementId().getValue());
58   }
59
60   @Override
61   public boolean isLimitPresent(LimitEntity limitEntity) {
62     SessionContext context = createSessionContext();
63     ElementContext elementContext =
64         new ElementContext(limitEntity.getVendorLicenseModelId(), limitEntity.getVersion().getId());
65
66     Collection<ElementInfo> elementInfos = zusammenAdaptor.listElementsByName(context,
67         elementContext, new Id(limitEntity.getEpLkgId()), ElementType.Limits.name());
68
69     for (ElementInfo elementInfo : elementInfos) {
70       if (elementInfo.getId().getValue().equals(limitEntity.getId())) {
71         return true;
72       }
73     }
74
75     return false;
76   }
77
78   @Override
79   public Collection<LimitEntity> list(LimitEntity limitEntity) {
80     SessionContext context = createSessionContext();
81     ElementContext elementContext =
82         new ElementContext(limitEntity.getVendorLicenseModelId(), limitEntity.getVersion().getId());
83
84     return listLimits(context, elementContext, limitEntity);
85   }
86
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());
96   }
97
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());
102
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)) :
107         null);
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);
118
119     return limitEntity;
120   }
121
122   @Override
123   public void update(LimitEntity limitEntity) {
124     ZusammenElement limitElement = limitToZusammen(limitEntity, Action.UPDATE);
125
126     SessionContext context = createSessionContext();
127     ElementContext elementContext =
128         new ElementContext(limitEntity.getVendorLicenseModelId(), limitEntity.getVersion().getId());
129
130     zusammenAdaptor.saveElement(context, elementContext, limitElement,
131         String.format("Update limit with id %s", limitEntity.getId()));
132   }
133
134   @Override
135   public LimitEntity get(LimitEntity limitEntity) {
136     SessionContext context = createSessionContext();
137     ElementContext elementContext =
138         new ElementContext(limitEntity.getVendorLicenseModelId(), limitEntity.getVersion().getId());
139
140     return zusammenAdaptor.getElementInfo(context, elementContext, new Id(limitEntity.getId()))
141         .map(elementInfo -> mapElementInfoToLimit(
142             limitEntity.getVendorLicenseModelId(), limitEntity.getVersion(), limitEntity
143                 .getEpLkgId(), elementInfo))
144         .orElse(null);
145   }
146
147   @Override
148   public void delete(LimitEntity limitEntity) {
149     ZusammenElement zusammenElement = buildElement(new Id(limitEntity.getId()), Action.DELETE);
150
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() + ".");
156   }
157
158   @Override
159   public void registerVersioning(String versionableEntityType) {
160     //registerVersioning not implemented for LimitZusammenDaoImpl
161   }
162
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);
177     return limitElement;
178   }
179 }