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 / EntitlementPoolZusammenDaoImpl.java
index b880c67..35182a3 100644 (file)
@@ -10,25 +10,20 @@ import com.amdocs.zusammen.datatypes.item.ElementContext;
 import com.amdocs.zusammen.datatypes.item.Info;
 import org.openecomp.core.zusammen.api.ZusammenAdaptor;
 import org.openecomp.core.zusammen.api.ZusammenUtil;
+import org.openecomp.sdc.datatypes.model.ElementType;
 import org.openecomp.sdc.vendorlicense.dao.EntitlementPoolDao;
+import org.openecomp.sdc.vendorlicense.dao.impl.zusammen.convertor.ElementToEntitlementPoolConvertor;
 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
-import org.openecomp.sdc.vendorlicense.dao.types.MultiChoiceOrOther;
-import org.openecomp.sdc.vendorlicense.dao.types.OperationalScope;
-import org.openecomp.sdc.vendorlicense.dao.types.ThresholdUnit;
-import org.openecomp.sdc.versioning.dao.types.Version;
+import org.openecomp.types.ElementPropertyName;
 
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.HashSet;
-import java.util.List;
 import java.util.Map;
 import java.util.Optional;
-import java.util.Set;
 import java.util.stream.Collectors;
 
-/**
- * Created by ayalaben on 3/28/2017.
- */
+import static org.openecomp.core.zusammen.api.ZusammenUtil.buildElement;
+
 public class EntitlementPoolZusammenDaoImpl implements EntitlementPoolDao {
 
   private ZusammenAdaptor zusammenAdaptor;
@@ -48,18 +43,22 @@ public class EntitlementPoolZusammenDaoImpl implements EntitlementPoolDao {
         buildEntitlementPoolElement(entitlementPool, Action.CREATE);
 
     ZusammenElement entitlementPoolsElement =
-        VlmZusammenUtil.buildStructuralElement(StructureElement.EntitlementPools, null);
+        ZusammenUtil.buildStructuralElement(ElementType.EntitlementPools, Action.IGNORE);
+
+    ZusammenElement limitsElement =
+        ZusammenUtil.buildStructuralElement(ElementType.Limits, Action.CREATE);
 
+    entitlementPoolElement.addSubElement(limitsElement);
     entitlementPoolsElement.addSubElement(entitlementPoolElement);
 
     SessionContext context = ZusammenUtil.createSessionContext();
-    Id itemId = new Id(entitlementPool.getVendorLicenseModelId());
-    Optional<Element> savedElement = zusammenAdaptor.saveElement(context, new ElementContext(itemId,
-            VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor)),
+    Element epsSavedElement = zusammenAdaptor.saveElement(context,
+        new ElementContext(entitlementPool.getVendorLicenseModelId(),
+            entitlementPool.getVersion().getId()),
         entitlementPoolsElement, "Create entitlement pool");
 
-    savedElement.ifPresent(element -> entitlementPool
-        .setId(element.getSubElements().iterator().next().getElementId().getValue()));
+    entitlementPool
+        .setId(epsSavedElement.getSubElements().iterator().next().getElementId().getValue());
   }
 
   @Override
@@ -68,9 +67,8 @@ public class EntitlementPoolZusammenDaoImpl implements EntitlementPoolDao {
         buildEntitlementPoolElement(entitlementPool, Action.UPDATE);
 
     SessionContext context = ZusammenUtil.createSessionContext();
-    Id itemId = new Id(entitlementPool.getVendorLicenseModelId());
-    ElementContext elementContext =  new ElementContext(itemId,
-        VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor));
+    ElementContext elementContext = new ElementContext(entitlementPool.getVendorLicenseModelId(),
+        entitlementPool.getVersion().getId());
 
     Optional<ElementInfo> epFromDb = zusammenAdaptor.getElementInfo(context, elementContext,
         new Id(entitlementPool.getId()));
@@ -84,35 +82,33 @@ public class EntitlementPoolZusammenDaoImpl implements EntitlementPoolDao {
       }
     }
 
-    zusammenAdaptor.saveElement(context,elementContext, entitlmentpoolElement,
+    zusammenAdaptor.saveElement(context, elementContext, entitlmentpoolElement,
         String.format("Update entitlement pool with id %s", entitlementPool.getId()));
   }
 
   @Override
   public EntitlementPoolEntity get(EntitlementPoolEntity entitlementPool) {
     SessionContext context = ZusammenUtil.createSessionContext();
-    Id itemId = new Id(entitlementPool.getVendorLicenseModelId());
-    ElementContext elementContext = new ElementContext(itemId,
-        VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor),
-        VlmZusammenUtil.getVersionTag(entitlementPool.getVersion()));
-
+    ElementContext elementContext = new ElementContext(entitlementPool.getVendorLicenseModelId(),
+        entitlementPool.getVersion().getId());
+    ElementToEntitlementPoolConvertor convertor = new ElementToEntitlementPoolConvertor();
     return zusammenAdaptor.getElementInfo(context, elementContext, new Id(entitlementPool.getId()))
-        .map(elementInfo -> mapElementInfoToEntitlementPool(
-            entitlementPool.getVendorLicenseModelId(), entitlementPool.getVersion(), elementInfo))
+        .map(elementInfo -> {
+          EntitlementPoolEntity entity = convertor.convert(elementInfo);
+          entity.setVendorLicenseModelId(entitlementPool.getVendorLicenseModelId());
+          entity.setVersion(entitlementPool.getVersion());
+          return entity;
+        })
         .orElse(null);
   }
 
   @Override
   public void delete(EntitlementPoolEntity entitlementPool) {
-    SessionContext context = ZusammenUtil.createSessionContext();
-    ZusammenElement zusammenElement = new ZusammenElement();
-    zusammenElement.setAction(Action.DELETE);
-    zusammenElement.setElementId(new Id(entitlementPool.getId()));
+    ZusammenElement zusammenElement = buildElement(new Id(entitlementPool.getId()), Action.DELETE);
 
-    Id itemId = new Id(entitlementPool.getVendorLicenseModelId());
-    ElementContext elementContext =
-        new ElementContext(itemId,
-            VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor));
+    SessionContext context = ZusammenUtil.createSessionContext();
+    ElementContext elementContext = new ElementContext(entitlementPool.getVendorLicenseModelId(),
+        entitlementPool.getVersion().getId());
     zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
         "delete entitlement pool. id:" + entitlementPool.getId() + ".");
   }
@@ -120,28 +116,27 @@ public class EntitlementPoolZusammenDaoImpl implements EntitlementPoolDao {
   @Override
   public Collection<EntitlementPoolEntity> list(EntitlementPoolEntity entitlementPool) {
     SessionContext context = ZusammenUtil.createSessionContext();
-    Id itemId = new Id(entitlementPool.getVendorLicenseModelId());
-    ElementContext elementContext = new ElementContext(itemId,
-        VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor),
-        VlmZusammenUtil.getVersionTag(entitlementPool.getVersion()));
-
+    ElementContext elementContext = new ElementContext(entitlementPool.getVendorLicenseModelId(),
+        entitlementPool.getVersion().getId());
+    ElementToEntitlementPoolConvertor convertor = new ElementToEntitlementPoolConvertor();
     return zusammenAdaptor
-        .listElementsByName(context, elementContext, null, StructureElement.EntitlementPools.name())
-        .stream().map(elementInfo -> mapElementInfoToEntitlementPool(
-            entitlementPool.getVendorLicenseModelId(), entitlementPool.getVersion(), elementInfo))
-        .collect(Collectors.toList());
+        .listElementsByName(context, elementContext, null, ElementType.EntitlementPools.name())
+        .stream().map(elementInfo -> {
+          EntitlementPoolEntity entity = convertor.convert(elementInfo);
+          entity.setVendorLicenseModelId(entitlementPool.getVendorLicenseModelId());
+          entity.setVersion(entitlementPool.getVersion());
+          return entity;
+        }).collect(Collectors.toList());
   }
 
   @Override
   public long count(EntitlementPoolEntity entitlementPool) {
     SessionContext context = ZusammenUtil.createSessionContext();
-    Id itemId = new Id(entitlementPool.getVendorLicenseModelId());
-    ElementContext elementContext = new ElementContext(itemId,
-        VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor),
-        VlmZusammenUtil.getVersionTag(entitlementPool.getVersion()));
+    ElementContext elementContext = new ElementContext(entitlementPool.getVendorLicenseModelId(),
+        entitlementPool.getVersion().getId());
 
     return zusammenAdaptor
-        .listElementsByName(context, elementContext, null, StructureElement.EntitlementPools.name())
+        .listElementsByName(context, elementContext, null, ElementType.EntitlementPools.name())
         .size();
   }
 
@@ -149,9 +144,8 @@ public class EntitlementPoolZusammenDaoImpl implements EntitlementPoolDao {
   public void removeReferencingFeatureGroup(EntitlementPoolEntity entitlementPool,
                                             String referencingFeatureGroupId) {
     SessionContext context = ZusammenUtil.createSessionContext();
-    Id itemId = new Id(entitlementPool.getVendorLicenseModelId());
-    ElementContext elementContext = new ElementContext(itemId,
-        VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor));
+    ElementContext elementContext = new ElementContext(entitlementPool.getVendorLicenseModelId(),
+        entitlementPool.getVersion().getId());
 
     Optional<ElementInfo> elementInfo =
         zusammenAdaptor.getElementInfo(context, elementContext, new Id(entitlementPool.getId()));
@@ -173,9 +167,8 @@ public class EntitlementPoolZusammenDaoImpl implements EntitlementPoolDao {
   public void addReferencingFeatureGroup(EntitlementPoolEntity entitlementPool,
                                          String referencingFeatureGroupId) {
     SessionContext context = ZusammenUtil.createSessionContext();
-    Id itemId = new Id(entitlementPool.getVendorLicenseModelId());
-    ElementContext elementContext = new ElementContext(itemId,
-        VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor));
+    ElementContext elementContext = new ElementContext(entitlementPool.getVendorLicenseModelId(),
+        entitlementPool.getVersion().getId());
 
     Optional<ElementInfo> elementInfo =
         zusammenAdaptor.getElementInfo(context, elementContext, new Id(entitlementPool.getId()));
@@ -199,17 +192,32 @@ public class EntitlementPoolZusammenDaoImpl implements EntitlementPoolDao {
     //not supported
   }
 
-  private ZusammenElement buildEntitlementPoolElement(EntitlementPoolEntity entitlementPool,
-                                                      Action action) {
+  @Override
+  public String getManufacturerReferenceNumber(EntitlementPoolEntity entitlementPoolEntity) {
+    SessionContext context = ZusammenUtil.createSessionContext();
+    ElementContext elementContext =
+        new ElementContext(entitlementPoolEntity.getVendorLicenseModelId(),
+            entitlementPoolEntity.getVersion().getId());
 
-    ZusammenElement entitlementPoolElement = new ZusammenElement();
-    entitlementPoolElement.setAction(action);
-    if (entitlementPool.getId() != null) {
-      entitlementPoolElement.setElementId(new Id(entitlementPool.getId()));
+    Optional<ElementInfo> elementInfo1 = zusammenAdaptor
+        .getElementInfo(context, elementContext, new Id(entitlementPoolEntity.getId()));
+    Map<String, Object> properties = elementInfo1.get().getInfo().getProperties();
+    String manufacturerReferenceNumber = null;
+    if (properties != null && properties.containsKey("manufacturerReferenceNumber")) {
+      manufacturerReferenceNumber = (String) properties.get("manufacturerReferenceNumber");
     }
+    return manufacturerReferenceNumber;
+  }
+
+  private ZusammenElement buildEntitlementPoolElement(EntitlementPoolEntity entitlementPool,
+                                                      Action action) {
+    ZusammenElement entitlementPoolElement =
+        buildElement(entitlementPool.getId() == null ? null : new Id(entitlementPool.getId()),
+            action);
     Info info = new Info();
     info.setName(entitlementPool.getName());
     info.setDescription(entitlementPool.getDescription());
+    info.addProperty(ElementPropertyName.elementType.name(), ElementType.EntitlementPool);
     info.addProperty("version_uuid", entitlementPool.getVersionUuId());
     info.addProperty("thresholdValue", entitlementPool.getThresholdValue());
     info.addProperty("threshold_unit", entitlementPool.getThresholdUnit());
@@ -219,8 +227,8 @@ public class EntitlementPoolZusammenDaoImpl implements EntitlementPoolDao {
     info.addProperty("expiryDate", entitlementPool.getExpiryDate());
     entitlementPoolElement.setInfo(info);
 
-   if (entitlementPool.getReferencingFeatureGroups() != null
-       && entitlementPool.getReferencingFeatureGroups().size() > 0) {
+    if (entitlementPool.getReferencingFeatureGroups() != null
+        && entitlementPool.getReferencingFeatureGroups().size() > 0) {
       entitlementPoolElement.setRelations(entitlementPool.getReferencingFeatureGroups().stream()
           .map(rel -> VlmZusammenUtil
               .createRelation(RelationType.EntitlmentPoolToReferencingFeatureGroup, rel))
@@ -229,62 +237,5 @@ public class EntitlementPoolZusammenDaoImpl implements EntitlementPoolDao {
     return entitlementPoolElement;
   }
 
-  private EntitlementPoolEntity mapElementInfoToEntitlementPool(String vlmId, Version version,
-                                                                ElementInfo elementInfo) {
-    EntitlementPoolEntity entitlmentPool =
-        new EntitlementPoolEntity(vlmId, version, elementInfo.getId().getValue());
-    entitlmentPool.setName(elementInfo.getInfo().getName());
-    entitlmentPool.setDescription(elementInfo.getInfo().getDescription());
-    entitlmentPool.setVersionUuId(elementInfo.getInfo().getProperty("version_uuid"));
-    entitlmentPool
-        .setThresholdValue(elementInfo.getInfo().getProperty("thresholdValue") != null
-                ? VlmZusammenUtil.toInteger(elementInfo.getInfo().getProperty("thresholdValue")) : null);
-
-    Object threshold_unit = elementInfo.getInfo().getProperty("threshold_unit");
-    entitlmentPool.setThresholdUnit( threshold_unit != null ?
-        ThresholdUnit.valueOf(elementInfo.getInfo().getProperty("threshold_unit")) : null);
-    entitlmentPool.setIncrements(elementInfo.getInfo().getProperty("increments"));
-    entitlmentPool.setOperationalScope(getOperationalScopeMultiChoiceOrOther(
-        elementInfo.getInfo().getProperty("operational_scope")));
-    entitlmentPool.setStartDate(elementInfo.getInfo().getProperty("startDate"));
-    entitlmentPool.setExpiryDate(elementInfo.getInfo().getProperty("expiryDate"));
-
-    if (elementInfo.getRelations() != null && elementInfo.getRelations().size() > 0) {
-      entitlmentPool
-          .setReferencingFeatureGroups(elementInfo.getRelations().stream().map(relation -> relation
-              .getEdge2().getElementId().getValue()).collect(Collectors.toSet()));
-    }
-    return entitlmentPool;
-  }
-
-  private MultiChoiceOrOther<OperationalScope> getOperationalScopeMultiChoiceOrOther
-      (Map<String, Object>
-           operationalScope) {
-    if(operationalScope != null && !operationalScope.isEmpty()) {
-      Set<OperationalScope> choices = new HashSet<>();
-      ((List<String>) operationalScope.get("choices")).
-          forEach(choice -> choices.add(OperationalScope.valueOf(choice)));
-
-      return new MultiChoiceOrOther<>(choices, operationalScope.get("other")==null?null:
-          (String) operationalScope.get("other"));
-    }
-    return null;
-  }
-
-  @Override
-  public String getManufacturerReferenceNumber(EntitlementPoolEntity entitlementPoolEntity){
-    SessionContext context = ZusammenUtil.createSessionContext();
-    Id itemId = new Id(entitlementPoolEntity.getVendorLicenseModelId());
-    ElementContext elementContext = new ElementContext(itemId,
-            VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor),
-            VlmZusammenUtil.getVersionTag(entitlementPoolEntity.getVersion()));
-    Optional<ElementInfo> elementInfo1 = zusammenAdaptor.getElementInfo(context, elementContext, new Id(entitlementPoolEntity.getId()));
-    Map<String, Object> properties = elementInfo1.get().getInfo().getProperties();
-    String manufacturerReferenceNumber = null;
-    if(properties != null && properties.containsKey("manufacturerReferenceNumber") ) {
-      manufacturerReferenceNumber = (String)properties.get("manufacturerReferenceNumber");
-    }
-    return manufacturerReferenceNumber;
-  }
 
 }