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