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 / LicenseKeyGroupZusammenDaoImpl.java
index 976db94..4e7aee3 100644 (file)
@@ -9,27 +9,22 @@ import com.amdocs.zusammen.datatypes.item.Action;
 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.LicenseKeyGroupDao;
+import org.openecomp.sdc.vendorlicense.dao.impl.zusammen.convertor.ElementToLicenseKeyGroupConvertor;
 import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity;
-import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyType;
-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/30/2017.
- */
+import static org.openecomp.core.zusammen.api.ZusammenUtil.buildElement;
+import static org.openecomp.core.zusammen.api.ZusammenUtil.buildStructuralElement;
+import static org.openecomp.core.zusammen.api.ZusammenUtil.createSessionContext;
+
+
 public class LicenseKeyGroupZusammenDaoImpl implements LicenseKeyGroupDao {
   private ZusammenAdaptor zusammenAdaptor;
 
@@ -47,18 +42,20 @@ public class LicenseKeyGroupZusammenDaoImpl implements LicenseKeyGroupDao {
     ZusammenElement licenseKeyGroupElement =
         buildLicenseKeyGroupElement(licenseKeyGroup, Action.CREATE);
 
+    ZusammenElement limitsElement = buildStructuralElement(ElementType.Limits, Action.CREATE);
+    licenseKeyGroupElement.addSubElement(limitsElement);
+
     ZusammenElement lkgsElement =
-        VlmZusammenUtil.buildStructuralElement(StructureElement.LicenseKeyGroups, null);
+        buildStructuralElement(ElementType.LicenseKeyGroups, Action.IGNORE);
     lkgsElement.addSubElement(licenseKeyGroupElement);
 
-    SessionContext context = ZusammenUtil.createSessionContext();
-    Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
-    Optional<Element> savedElement = zusammenAdaptor.saveElement(context, new ElementContext(itemId,
-            VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor)),
-        lkgsElement, "Create license Key Group");
+    SessionContext context = createSessionContext();
+    Element lkgsSavedElement = zusammenAdaptor.saveElement(context,
+        new ElementContext(licenseKeyGroup.getVendorLicenseModelId(),
+            licenseKeyGroup.getVersion().getId()), lkgsElement, "Create license Key Group");
 
-    savedElement.ifPresent(element -> licenseKeyGroup
-        .setId(element.getSubElements().iterator().next().getElementId().getValue()));
+    licenseKeyGroup
+        .setId(lkgsSavedElement.getSubElements().iterator().next().getElementId().getValue());
   }
 
   @Override
@@ -66,55 +63,50 @@ public class LicenseKeyGroupZusammenDaoImpl implements LicenseKeyGroupDao {
     ZusammenElement licenseKeyGroupElement =
         buildLicenseKeyGroupElement(licenseKeyGroup, Action.UPDATE);
 
-    SessionContext context = ZusammenUtil.createSessionContext();
-    Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
-
-    ElementContext elementContext = new ElementContext(itemId,
-        VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor));
+    SessionContext context = createSessionContext();
+    ElementContext elementContext = new ElementContext(licenseKeyGroup.getVendorLicenseModelId(),
+        licenseKeyGroup.getVersion().getId());
 
     Optional<ElementInfo> lkgFromDb = zusammenAdaptor.getElementInfo(context, elementContext,
         new Id(licenseKeyGroup.getId()));
 
-    if(lkgFromDb.isPresent()) {
+    if (lkgFromDb.isPresent()) {
 
-      iflicenseKeyGroupElement.getRelations() == null) {
-         licenseKeyGroupElement.setRelations(new ArrayList<>());
+      if (licenseKeyGroupElement.getRelations() == null) {
+        licenseKeyGroupElement.setRelations(new ArrayList<>());
       }
 
       if (lkgFromDb.get().getRelations() != null && lkgFromDb.get().getRelations().size() > 0) {
         licenseKeyGroupElement.getRelations().addAll(lkgFromDb.get().getRelations());
       }
     }
-
-    zusammenAdaptor.saveElement(context, elementContext,
-        licenseKeyGroupElement,
+    zusammenAdaptor.saveElement(context, elementContext, licenseKeyGroupElement,
         String.format("Update license key group with id %s", licenseKeyGroup.getId()));
   }
 
   @Override
   public LicenseKeyGroupEntity get(LicenseKeyGroupEntity licenseKeyGroup) {
-    SessionContext context = ZusammenUtil.createSessionContext();
-    Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
-    ElementContext elementContext = new ElementContext(itemId,
-        VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor),
-        VlmZusammenUtil.getVersionTag(licenseKeyGroup.getVersion()));
-
+    SessionContext context = createSessionContext();
+    ElementContext elementContext = new ElementContext(licenseKeyGroup.getVendorLicenseModelId(),
+        licenseKeyGroup.getVersion().getId());
+    ElementToLicenseKeyGroupConvertor convertor = new ElementToLicenseKeyGroupConvertor();
     return zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseKeyGroup.getId()))
-        .map(elementInfo -> mapElementInfoToLicenseKeyGroup(
-            licenseKeyGroup.getVendorLicenseModelId(), licenseKeyGroup.getVersion(), elementInfo))
+        .map(elementInfo -> {
+          LicenseKeyGroupEntity entity = convertor.convert(elementInfo);
+          entity.setVendorLicenseModelId(licenseKeyGroup.getVendorLicenseModelId());
+          entity.setVersion(licenseKeyGroup.getVersion());
+          return entity;
+        })
         .orElse(null);
   }
 
   @Override
   public void delete(LicenseKeyGroupEntity licenseKeyGroup) {
-    SessionContext context = ZusammenUtil.createSessionContext();
-    ZusammenElement zusammenElement = new ZusammenElement();
-    zusammenElement.setAction(Action.DELETE);
-    zusammenElement.setElementId(new Id(licenseKeyGroup.getId()));
+    ZusammenElement zusammenElement = buildElement(new Id(licenseKeyGroup.getId()), Action.DELETE);
 
-    Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
-    ElementContext elementContext = new ElementContext(itemId,
-        VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor));
+    SessionContext context = createSessionContext();
+    ElementContext elementContext = new ElementContext(licenseKeyGroup.getVendorLicenseModelId(),
+        licenseKeyGroup.getVersion().getId());
 
     zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
         "delete license key group. id:" + licenseKeyGroup.getId() + ".");
@@ -122,29 +114,29 @@ public class LicenseKeyGroupZusammenDaoImpl implements LicenseKeyGroupDao {
 
   @Override
   public Collection<LicenseKeyGroupEntity> list(LicenseKeyGroupEntity licenseKeyGroup) {
-    SessionContext context = ZusammenUtil.createSessionContext();
-    Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
-    ElementContext elementContext = new ElementContext(itemId,
-        VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor),
-        VlmZusammenUtil.getVersionTag(licenseKeyGroup.getVersion()));
-
+    SessionContext context = createSessionContext();
+    ElementContext elementContext = new ElementContext(licenseKeyGroup.getVendorLicenseModelId(),
+        licenseKeyGroup.getVersion().getId());
+    ElementToLicenseKeyGroupConvertor convertor = new ElementToLicenseKeyGroupConvertor();
     return zusammenAdaptor
-        .listElementsByName(context, elementContext, null, StructureElement.LicenseKeyGroups.name())
-        .stream().map(elementInfo -> mapElementInfoToLicenseKeyGroup(
-            licenseKeyGroup.getVendorLicenseModelId(), licenseKeyGroup.getVersion(), elementInfo))
+        .listElementsByName(context, elementContext, null, ElementType.LicenseKeyGroups.name())
+        .stream().map(elementInfo -> {
+          LicenseKeyGroupEntity entity = convertor.convert(elementInfo);
+          entity.setVendorLicenseModelId(licenseKeyGroup.getVendorLicenseModelId());
+          entity.setVersion(licenseKeyGroup.getVersion());
+          return entity;
+        })
         .collect(Collectors.toList());
   }
 
   @Override
   public long count(LicenseKeyGroupEntity licenseKeyGroup) {
-    SessionContext context = ZusammenUtil.createSessionContext();
-    Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
-    ElementContext elementContext = new ElementContext(itemId,
-        VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor),
-        VlmZusammenUtil.getVersionTag(licenseKeyGroup.getVersion()));
+    SessionContext context = createSessionContext();
+    ElementContext elementContext = new ElementContext(licenseKeyGroup.getVendorLicenseModelId(),
+        licenseKeyGroup.getVersion().getId());
 
     return zusammenAdaptor
-        .listElementsByName(context, elementContext, null, StructureElement.LicenseKeyGroups.name())
+        .listElementsByName(context, elementContext, null, ElementType.LicenseKeyGroups.name())
         .size();
   }
 
@@ -156,10 +148,9 @@ public class LicenseKeyGroupZusammenDaoImpl implements LicenseKeyGroupDao {
   @Override
   public void removeReferencingFeatureGroup(LicenseKeyGroupEntity licenseKeyGroup,
                                             String featureGroupId) {
-    SessionContext context = ZusammenUtil.createSessionContext();
-    Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
-    ElementContext elementContext = new ElementContext(itemId,
-        VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor));
+    SessionContext context = createSessionContext();
+    ElementContext elementContext = new ElementContext(licenseKeyGroup.getVendorLicenseModelId(),
+        licenseKeyGroup.getVersion().getId());
 
     Optional<ElementInfo> elementInfo =
         zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseKeyGroup.getId()));
@@ -180,10 +171,9 @@ public class LicenseKeyGroupZusammenDaoImpl implements LicenseKeyGroupDao {
   @Override
   public void addReferencingFeatureGroup(LicenseKeyGroupEntity licenseKeyGroup,
                                          String featureGroupId) {
-    SessionContext context = ZusammenUtil.createSessionContext();
-    Id itemId = new Id(licenseKeyGroup.getVendorLicenseModelId());
-    ElementContext elementContext = new ElementContext(itemId,
-        VlmZusammenUtil.getFirstVersionId(context, itemId, zusammenAdaptor));
+    SessionContext context = createSessionContext();
+    ElementContext elementContext = new ElementContext(licenseKeyGroup.getVendorLicenseModelId(),
+        licenseKeyGroup.getVersion().getId());
 
     Optional<ElementInfo> elementInfo =
         zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseKeyGroup.getId()));
@@ -204,18 +194,15 @@ public class LicenseKeyGroupZusammenDaoImpl implements LicenseKeyGroupDao {
 
   private ZusammenElement buildLicenseKeyGroupElement(LicenseKeyGroupEntity licenseKeyGroup,
                                                       Action action) {
-
-    ZusammenElement lkgElement = new ZusammenElement();
-    lkgElement.setAction(action);
-    if (licenseKeyGroup.getId() != null) {
-      lkgElement.setElementId(new Id(licenseKeyGroup.getId()));
-    }
+    ZusammenElement lkgElement =
+        buildElement(licenseKeyGroup.getId() == null ? null : new Id(licenseKeyGroup.getId()),
+            action);
     Info info = new Info();
     info.setName(licenseKeyGroup.getName());
     info.setDescription(licenseKeyGroup.getDescription());
+    info.addProperty(ElementPropertyName.elementType.name(), ElementType.LicenseKeyGroup);
     info.addProperty("version_uuid", licenseKeyGroup.getVersionUuId());
     info.addProperty("LicenseKeyType", licenseKeyGroup.getType());
-    info.addProperty("version_uuid", licenseKeyGroup.getVersionUuId());
     info.addProperty("operational_scope", licenseKeyGroup.getOperationalScope());
     info.addProperty("startDate", licenseKeyGroup.getStartDate());
     info.addProperty("expiryDate", licenseKeyGroup.getExpiryDate());
@@ -224,70 +211,15 @@ public class LicenseKeyGroupZusammenDaoImpl implements LicenseKeyGroupDao {
     info.addProperty("increments", licenseKeyGroup.getIncrements());
     lkgElement.setInfo(info);
 
-   if (licenseKeyGroup.getReferencingFeatureGroups() != null
-       && licenseKeyGroup.getReferencingFeatureGroups().size() > 0) {
+    if (licenseKeyGroup.getReferencingFeatureGroups() != null
+        && licenseKeyGroup.getReferencingFeatureGroups().size() > 0) {
       lkgElement.setRelations(licenseKeyGroup.getReferencingFeatureGroups().stream()
           .map(rel -> VlmZusammenUtil
               .createRelation(RelationType.LicenseKeyGroupToReferencingFeatureGroup, rel))
           .collect(Collectors.toList()));
     }
-
     return lkgElement;
   }
 
-  private LicenseKeyGroupEntity mapElementInfoToLicenseKeyGroup(String vlmId, Version version,
-                                                                ElementInfo elementInfo) {
-    LicenseKeyGroupEntity licenseKeyGroup =
-        new LicenseKeyGroupEntity(vlmId, version, elementInfo.getId().getValue());
-    licenseKeyGroup.setName(elementInfo.getInfo().getName());
-    licenseKeyGroup.setDescription(elementInfo.getInfo().getDescription());
-    licenseKeyGroup.setVersionUuId(elementInfo.getInfo().getProperty("version_uuid"));
-    licenseKeyGroup
-        .setType(LicenseKeyType.valueOf(elementInfo.getInfo().getProperty("LicenseKeyType")));
-    licenseKeyGroup.setVersionUuId(elementInfo.getInfo().getProperty("version_uuid"));
-    licenseKeyGroup.setOperationalScope(getOperationalScopeMultiChoiceOrOther(
-        elementInfo.getInfo().getProperty("operational_scope")));
-    licenseKeyGroup.setStartDate(elementInfo.getInfo().getProperty("startDate"));
-    licenseKeyGroup.setExpiryDate(elementInfo.getInfo().getProperty("expiryDate"));
-    if (elementInfo.getInfo().getProperty("thresholdUnits") != null ){
-      licenseKeyGroup.setThresholdUnits(ThresholdUnit.valueOf(elementInfo
-          .getInfo().getProperty("thresholdUnits")));
-    }
-    if (elementInfo.getInfo().getProperty("thresholdValue") != null ){
-      licenseKeyGroup.setThresholdValue(toInteger(elementInfo.getInfo().getProperty
-          ("thresholdValue")));
-    }
-    licenseKeyGroup.setIncrements(elementInfo.getInfo().getProperty("increments"));
-
-    if (elementInfo.getRelations() != null && elementInfo.getRelations().size() > 0) {
-      licenseKeyGroup
-          .setReferencingFeatureGroups(elementInfo.getRelations().stream().map(relation -> relation
-              .getEdge2().getElementId().getValue()).collect(Collectors.toSet()));
-    }
-    return licenseKeyGroup;
-  }
 
-  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;
-  }
-
-  private Integer toInteger(Object val) {
-    if (val instanceof Double) {
-      return ((Double) val).intValue();
-    } else if (val instanceof String) {
-      return new Integer((String) val);
-    } else if (val instanceof Integer) {
-      return (Integer) val;
-    }
-    throw new RuntimeException("invalid value for integer:" + val.getClass());
-  }
 }