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.buildElement;
 
  25 import static org.openecomp.core.zusammen.api.ZusammenUtil.buildStructuralElement;
 
  26 import static org.openecomp.core.zusammen.api.ZusammenUtil.createSessionContext;
 
  28 public class LimitZusammenDaoImpl implements LimitDao {
 
  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;
 
  38   public LimitZusammenDaoImpl(ZusammenAdaptor zusammenAdaptor) {
 
  39     this.zusammenAdaptor = zusammenAdaptor;
 
  43   public void create(LimitEntity limitEntity) {
 
  44     ZusammenElement limitElement = limitToZusammen(limitEntity, Action.CREATE);
 
  46     ZusammenElement limitsElement = buildStructuralElement(ElementType.Limits, null);
 
  47     limitsElement.setSubElements(Collections.singletonList(limitElement));
 
  49     ZusammenElement epLkgElement = buildElement(new Id(limitEntity.getEpLkgId()), Action.IGNORE);
 
  50     epLkgElement.setSubElements(Collections.singletonList(limitsElement));
 
  52     SessionContext context = createSessionContext();
 
  53     ElementContext elementContext =
 
  54         new ElementContext(limitEntity.getVendorLicenseModelId(), limitEntity.getVersion().getId());
 
  56     Element savedElement =
 
  57         zusammenAdaptor.saveElement(context, elementContext, epLkgElement, "Create limit");
 
  58     limitEntity.setId(savedElement.getSubElements().iterator().next()
 
  59         .getSubElements().iterator().next().getElementId().getValue());
 
  63   public boolean isLimitPresent(LimitEntity limitEntity) {
 
  64     SessionContext context = createSessionContext();
 
  65     ElementContext elementContext =
 
  66         new ElementContext(limitEntity.getVendorLicenseModelId(), limitEntity.getVersion().getId());
 
  68     Collection<ElementInfo> elementInfos = zusammenAdaptor.listElementsByName(context,
 
  69         elementContext, new Id(limitEntity.getEpLkgId()), ElementType.Limits.name());
 
  71     for (ElementInfo elementInfo : elementInfos) {
 
  72       if (elementInfo.getId().getValue().equals(limitEntity.getId())) {
 
  81   public Collection<LimitEntity> list(LimitEntity limitEntity) {
 
  82     SessionContext context = createSessionContext();
 
  83     ElementContext elementContext =
 
  84         new ElementContext(limitEntity.getVendorLicenseModelId(), limitEntity.getVersion().getId());
 
  86     return listLimits(context, elementContext, limitEntity);
 
  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());
 
 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());
 
 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)) :
 
 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);
 
 125   public void update(LimitEntity limitEntity) {
 
 126     ZusammenElement limitElement = limitToZusammen(limitEntity, Action.UPDATE);
 
 128     SessionContext context = createSessionContext();
 
 129     ElementContext elementContext =
 
 130         new ElementContext(limitEntity.getVendorLicenseModelId(), limitEntity.getVersion().getId());
 
 132     zusammenAdaptor.saveElement(context, elementContext, limitElement,
 
 133         String.format("Update limit with id %s", limitEntity.getId()));
 
 137   public LimitEntity get(LimitEntity limitEntity) {
 
 138     SessionContext context = createSessionContext();
 
 139     ElementContext elementContext =
 
 140         new ElementContext(limitEntity.getVendorLicenseModelId(), limitEntity.getVersion().getId());
 
 142     return zusammenAdaptor.getElementInfo(context, elementContext, new Id(limitEntity.getId()))
 
 143         .map(elementInfo -> mapElementInfoToLimit(
 
 144             limitEntity.getVendorLicenseModelId(), limitEntity.getVersion(), limitEntity
 
 145                 .getEpLkgId(), elementInfo))
 
 150   public void delete(LimitEntity limitEntity) {
 
 151     ZusammenElement zusammenElement = buildElement(new Id(limitEntity.getId()), Action.DELETE);
 
 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() + ".");
 
 161   public void registerVersioning(String versionableEntityType) {
 
 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);