diff options
Diffstat (limited to 'openecomp-be/tools/zusammen-tools/src/main')
4 files changed, 445 insertions, 76 deletions
diff --git a/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/Commands/ResetOldVersion.java b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/Commands/ResetOldVersion.java index 031d1328b5..1725df7e93 100644 --- a/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/Commands/ResetOldVersion.java +++ b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/Commands/ResetOldVersion.java @@ -3,24 +3,36 @@ package org.openecomp.core.tools.Commands; import com.amdocs.zusammen.datatypes.Id; import com.amdocs.zusammen.datatypes.SessionContext; import com.amdocs.zusammen.plugin.statestore.cassandra.dao.impl.VersionCassandraDao; +import com.google.common.collect.Sets; +import org.openecomp.core.tools.store.ElementHandler; import org.openecomp.core.tools.store.VersionInfoCassandraLoader; import org.openecomp.core.tools.store.VspGeneralLoader; -import org.openecomp.core.tools.store.ElementHandler; import org.openecomp.core.zusammen.impl.CassandraConnectionInitializer; import org.openecomp.core.zusammen.plugin.dao.types.ElementEntity; import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants; +import org.openecomp.sdc.versioning.dao.types.Version; import org.openecomp.sdc.versioning.dao.types.VersionInfoEntity; -import java.util.*; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import static org.openecomp.core.tools.store.VspGeneralLoader.buildKey; public class ResetOldVersion { + public static final String OLD_VERSION = "oldVersion"; + private static Map<String, List<String>> itemVersionMap = new HashMap<>(); + private static Map<String, List<String>> itemChangeRefMap = new HashMap<>(); - private static int count =0; - public static void reset(SessionContext context, String oldVersion,String emptyOldVersion) { + private static int count = 0; + public static void reset(SessionContext context, String oldVersion, String emptyOldVersion) { CassandraConnectionInitializer.setCassandraConnectionPropertiesToSystem(); @@ -28,64 +40,102 @@ public class ResetOldVersion { loadItemVersionInfo(context); Map<String, ElementEntity> generalElementMap = - VspGeneralLoader.load(context, - itemVersionMap); + VspGeneralLoader.load(context, + itemVersionMap, itemChangeRefMap); generalElementMap.values().forEach(elementEntity -> updateOldVersionFlag(elementEntity, - oldVersion,"true".equals(emptyOldVersion))); + oldVersion, Boolean.TRUE.toString().equals(emptyOldVersion))); + + + itemVersionMap.entrySet().forEach(entry -> updateElements(context, generalElementMap, entry)); + + itemChangeRefMap.entrySet().forEach(entry -> updateElements(context, generalElementMap, entry)); + System.out.println("number of element updated:" + count); - itemVersionMap.entrySet().forEach(entry->entry.getValue().stream().filter - (version->generalElementMap.containsKey(context.getUser().getUserName()+"_"+entry.getKey() - +"_"+version)).forEach(version->ElementHandler.update(context, - entry.getKey(),version,generalElementMap.get(context.getUser().getUserName()+"_"+entry.getKey() - +"_"+version)))); + } + + private static void updateElements(SessionContext context, Map<String, + ElementEntity> generalElementMap, Map.Entry<String, List<String>> entry) { - System.out.println("nymber of element updated:"+count); + entry.getValue().stream().filter + (changeRef -> generalElementMap + .containsKey(buildKey(context, entry, changeRef))). + forEach(changeref -> ElementHandler.update(context, entry.getKey(), changeref, changeref, + generalElementMap.get(buildKey(context, entry, changeref)))); } private static void updateOldVersionFlag(ElementEntity elementEntity, String oldVersion, boolean emptyOldVersion) { - if(!emptyOldVersion){ - elementEntity.getInfo().addProperty("oldVersion",oldVersion); + if (!emptyOldVersion) { + elementEntity.getInfo().addProperty(OLD_VERSION, oldVersion); count++; - }else if(elementEntity.getInfo().getProperty("oldVersion")== null || "" - .equals(elementEntity.getInfo().getProperty("oldVersion"))){ - elementEntity.getInfo().addProperty("oldVersion",oldVersion); + } else if (elementEntity.getInfo().getProperty(OLD_VERSION) == null + || "".equals(elementEntity.getInfo().getProperty(OLD_VERSION))) { + elementEntity.getInfo().addProperty(OLD_VERSION, oldVersion); count++; } - - } - private static void loadItemVersionInfo(SessionContext context) { List<String> items = new ArrayList<>(); System.setProperty("cassandra.dox.keystore", "dox"); VersionInfoCassandraLoader versionInfoCassandraLoader = new VersionInfoCassandraLoader(); - Collection<VersionInfoEntity> versions = - versionInfoCassandraLoader.list(); + Collection<VersionInfoEntity> versions = versionInfoCassandraLoader.list(); + + versions.stream().filter(versionInfoEntity -> versionInfoEntity.getEntityType() + .equals(VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE)).forEach + (versionInfoEntity -> { + handleVersionInfoEntity(items, versionInfoEntity); + }); - versions.stream().filter(versionInfoEntity -> versionInfoEntity.getEntityType().equals - (VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE)).forEach - (versionInfoEntity - -> items.add(versionInfoEntity.getEntityId())); System.setProperty("cassandra.dox.keystore", "zusammen_dox"); VersionCassandraDao versionCassandraDao = new VersionCassandraDao(); - items - .forEach(itemId -> versionCassandraDao.list(context, context.getUser().getUserName(), new Id - (itemId)).forEach(itemVersion -> addItemVersion(itemId, itemVersion.getId()))); + items.forEach(itemId -> versionCassandraDao.list(context, context.getUser().getUserName(), + new Id(itemId)).forEach(itemVersion -> addItemVersion(itemId, itemVersion.getId()))); } + private static void handleVersionInfoEntity(List<String> items, VersionInfoEntity versionInfoEntity) { + items.add(versionInfoEntity.getEntityId()); + Set<Version> viewableVersions; + if (versionInfoEntity.getViewableVersions() != null + && !versionInfoEntity.getViewableVersions().isEmpty()) { + viewableVersions = versionInfoEntity.getViewableVersions(); + } else { + viewableVersions = Sets.newHashSet(versionInfoEntity.getActiveVersion()); + } + addItemChangeRef(versionInfoEntity.getEntityId(), maxChangeRef(viewableVersions)); + } + + private static Id maxChangeRef(Set<Version> viewableVersions) { + + return new Id(viewableVersions.stream().max((o1, o2) -> o1.getMajor() > o2.getMajor() + ?1:o1.getMajor + () == o2.getMajor() ? (o1.getMinor() > o2.getMinor() ? 1: o1.getMinor() == o2.getMinor() + ? 0 : -1) : -1).get().toString()); + + } + + private static void addItemChangeRef(String itemId, Id changeRef) { + addItemVersion(itemChangeRefMap, itemId, changeRef); + } + private static void addItemVersion(String itemId, Id versionId) { - if (!itemVersionMap.containsKey(itemId)) { - itemVersionMap.put(itemId, new ArrayList<>()); + addItemVersion(itemVersionMap, itemId, versionId); + } + + private static void addItemVersion(Map<String, List<String>> itemVersions, String itemId, Id id) { + + if (!itemVersions.containsKey(itemId)) { + itemVersions.put(itemId, new ArrayList<>()); } - itemVersionMap.get(itemId).add(versionId.getValue()); + + itemVersions.get(itemId).add(id.getValue()); } } + diff --git a/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/store/CassandraElementRepository.java b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/store/CassandraElementRepository.java new file mode 100644 index 0000000000..c41b46ea14 --- /dev/null +++ b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/store/CassandraElementRepository.java @@ -0,0 +1,267 @@ +/* + * Copyright © 2016-2017 European Support Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.openecomp.core.tools.store; + +import com.amdocs.zusammen.datatypes.Id; +import com.amdocs.zusammen.datatypes.Namespace; +import com.amdocs.zusammen.datatypes.SessionContext; +import com.amdocs.zusammen.datatypes.item.Info; +import com.amdocs.zusammen.datatypes.item.Relation; +import com.amdocs.zusammen.plugin.statestore.cassandra.dao.types.ElementEntityContext; +import com.amdocs.zusammen.utils.fileutils.json.JsonUtil; +import com.datastax.driver.core.ResultSet; +import com.datastax.driver.core.Row; +import com.datastax.driver.mapping.annotations.Accessor; +import com.datastax.driver.mapping.annotations.Param; +import com.datastax.driver.mapping.annotations.Query; +import com.google.gson.reflect.TypeToken; +import org.openecomp.core.nosqldb.factory.NoSqlDbFactory; +import org.openecomp.core.zusammen.plugin.dao.types.ElementEntity; + +import java.lang.reflect.Type; +import java.nio.ByteBuffer; +import java.util.*; +import java.util.stream.Collectors; + +public class CassandraElementRepository { + + + public Collection<ElementEntity> list(SessionContext context, + ElementEntityContext elementContext) { + Set<String> elementIds = getVersionElementIds(context, elementContext); + + return elementIds.stream() + .map(elementId -> get(context, elementContext, new ElementEntity(new Id(elementId))).get()) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + } + + + public void update(SessionContext context, ElementEntityContext elementContext, + ElementEntity element) { + updateElement(context, elementContext, element); + } + + + public Optional<ElementEntity> get(SessionContext context, ElementEntityContext elementContext, + ElementEntity element) { + Row row = getElementAccessor(context).get( + elementContext.getSpace(), + elementContext.getItemId().toString(), + getVersionId(elementContext), + element.getId().toString()).one(); + + return row == null ? Optional.empty() : Optional.of(getElementEntity(element, row)); + } + + + private String getVersionId(ElementEntityContext elementContext) { + return elementContext.getChangeRef() == null + ? elementContext.getVersionId().toString() + : elementContext.getChangeRef(); + } + + + private ElementAccessor getElementAccessor(SessionContext context) { + return NoSqlDbFactory.getInstance().createInterface().getMappingManager().createAccessor + (ElementAccessor.class); + + } + + private VersionElementsAccessor getVersionElementsAccessor() { + return NoSqlDbFactory.getInstance().createInterface().getMappingManager().createAccessor + (VersionElementsAccessor.class); + + } + + + private void updateElement(SessionContext context, ElementEntityContext elementContext, + ElementEntity element) { + + if (elementContext.getChangeRef() == null) { + + getElementAccessor(context).update( + JsonUtil.object2Json(element.getInfo()), + JsonUtil.object2Json(element.getRelations()), + element.getData(), + element.getSearchableData(), + element.getVisualization(), + elementContext.getSpace(), + elementContext.getItemId().toString(), + elementContext.getVersionId().toString(), + element.getId().toString()); + } else { + getElementAccessor(context).update( + JsonUtil.object2Json(element.getInfo()), + JsonUtil.object2Json(element.getRelations()), + element.getData(), + element.getSearchableData(), + element.getVisualization(), + elementContext.getSpace(), + elementContext.getItemId().toString(), + elementContext.getChangeRef(), + element.getId().toString()); + } + } + + + private ElementEntity getElementEntity(ElementEntity element, Row row) { + element.setNamespace(getNamespace(row.getString(ElementField.NAMESPACE))); + element.setParentId(new Id(row.getString(ElementField.PARENT_ID))); + element.setInfo(json2Object(row.getString(ElementField.INFO), Info.class)); + element.setRelations( + json2Object(row.getString(ElementField.RELATIONS), new TypeToken<ArrayList<Relation>>() { + }.getType())); + element.setData(row.getBytes(ElementField.DATA)); + element.setSearchableData(row.getBytes(ElementField.SEARCHABLE_DATA)); + element.setVisualization(row.getBytes(ElementField.VISUALIZATION)); + element.setSubElementIds(row.getSet(ElementField.SUB_ELEMENT_IDS, String.class) + .stream().map(Id::new).collect(Collectors.toSet())); + return element; + } + + private Namespace getNamespace(String namespaceStr) { + Namespace namespace = new Namespace(); + if (namespaceStr != null) { + namespace.setValue(namespaceStr); + } + return namespace; + } + + private static <T> T json2Object(String json, Type typeOfT) { + return json == null ? null : JsonUtil.json2Object(json, typeOfT); + } + + private Set<String> getVersionElementIds(SessionContext context, + ElementEntityContext elementContext) { + Row row = getVersionElementsAccessor().get( + elementContext.getSpace(), + elementContext.getItemId().toString(), + getVersionId(elementContext)).one(); + return row == null + ? new HashSet<>() + : row.getSet(CassandraElementRepository.VersionElementsField.ELEMENT_IDS, String.class); + } + + /* +CREATE TABLE IF NOT EXISTS element_namespace ( + space text, + item_id text, + element_id text, + namespace text, + PRIMARY KEY (( space, item_id, element_id )) +); + */ + @Accessor + interface ElementNamespaceAccessor { + @Query( + "UPDATE element_namespace SET namespace=:ns " + + "WHERE space=:space AND item_id=:item AND element_id=:id ") + void create(@Param("space") String space, + @Param("item") String itemId, + @Param("id") String elementId, + @Param("ns") String namespace); + } + + /* +CREATE TABLE IF NOT EXISTS element ( + space text, + item_id text, + version_id text, + element_id text, + parent_id text, + namespace text, + info text, + relations text, + data blob, + searchable_data blob, + visualization blob, + sub_element_ids set<text>, + PRIMARY KEY (( space, item_id, version_id, element_id )) +); + */ + @Accessor + interface ElementAccessor { + @Query( + "UPDATE zusammen_dox.element SET parent_id=:parentId, namespace=:ns, info=:info, " + + "relations=:rels, " + + "data=:data, searchable_data=:searchableData, visualization=:visualization, " + + "sub_element_ids=sub_element_ids+:subs " + + "WHERE space=:space AND item_id=:item AND version_id=:ver AND element_id=:id ") + void create(@Param("space") String space, + @Param("item") String itemId, + @Param("ver") String versionId, + @Param("id") String elementId, + @Param("parentId") String parentElementId, + @Param("ns") String namespace, + @Param("info") String info, + @Param("rels") String relations, + @Param("data") ByteBuffer data, + @Param("searchableData") ByteBuffer searchableData, + @Param("visualization") ByteBuffer visualization, + @Param("subs") Set<String> subElementIds); + + @Query("UPDATE zusammen_dox.element SET info=?, relations=?, data=?, searchable_data=?, " + + "visualization=?" + + " WHERE space=? AND item_id=? AND version_id=? AND element_id=? ") + void update(String info, String relations, ByteBuffer data, ByteBuffer searchableData, + ByteBuffer visualization, String space, String itemId, String versionId, + String elementId); + + + @Query("SELECT parent_id, namespace, info, relations, data, searchable_data, visualization, " + + "sub_element_ids FROM zusammen_dox.element " + + "WHERE space=? AND item_id=? AND version_id=? AND element_id=? ") + ResultSet get(String space, String itemId, String versionId, String elementId); + + + } + + private static final class ElementField { + private static final String NAMESPACE = "namespace"; + private static final String PARENT_ID = "parent_id"; + private static final String INFO = "info"; + private static final String RELATIONS = "relations"; + private static final String DATA = "data"; + private static final String SEARCHABLE_DATA = "searchable_data"; + private static final String VISUALIZATION = "visualization"; + private static final String SUB_ELEMENT_IDS = "sub_element_ids"; + } + + /* + CREATE TABLE IF NOT EXISTS version_elements ( + space text, + item_id text, + version_id text, + element_ids set<text>, + PRIMARY KEY (( space, item_id, version_id )) + ); + */ + @Accessor + interface VersionElementsAccessor { + + + @Query("SELECT element_ids FROM zusammen_dox.version_elements WHERE space=? AND item_id=? AND version_id=?") + ResultSet get(String space, String itemId, String versionId); + + + } + + private static final class VersionElementsField { + private static final String ELEMENT_IDS = "element_ids"; + } +} diff --git a/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/store/ElementHandler.java b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/store/ElementHandler.java index 2690130974..5c72c95f49 100644 --- a/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/store/ElementHandler.java +++ b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/store/ElementHandler.java @@ -2,25 +2,32 @@ package org.openecomp.core.tools.store; import com.amdocs.zusammen.datatypes.Id; import com.amdocs.zusammen.datatypes.SessionContext; +import com.amdocs.zusammen.datatypes.item.ElementContext; import com.amdocs.zusammen.plugin.statestore.cassandra.dao.types.ElementEntityContext; -import org.openecomp.core.zusammen.plugin.dao.impl.CassandraElementRepository; import org.openecomp.core.zusammen.plugin.dao.types.ElementEntity; public class ElementHandler { - private static final String GLOBAL_USER = "GLOBAL_USER"; + private static final String GLOBAL_USER = "GLOBAL_USER"; - public static void update(SessionContext context, + public static void update(SessionContext context, + String itemId, String versionId, String changeRef, + ElementEntity elementEntity) { - String itemId, String versionId, - ElementEntity elementEntity) { + ElementEntityContext elementContext; + CassandraElementRepository cassandraElementRepository = new CassandraElementRepository(); + if (changeRef == null) { - ElementEntityContext elementContext; - elementContext = new ElementEntityContext(GLOBAL_USER, new Id(itemId), - new Id(versionId)); - CassandraElementRepository cassandraElementRepository = new CassandraElementRepository(); - cassandraElementRepository.update(context, elementContext, elementEntity); + elementContext = new ElementEntityContext(GLOBAL_USER, new Id(itemId), + new Id(versionId)); - } + } else { + elementContext = new ElementEntityContext(GLOBAL_USER, new ElementContext(new Id(itemId), + new Id(versionId), + changeRef)); + } + cassandraElementRepository.update(context, elementContext, elementEntity); + + } } diff --git a/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/store/VspGeneralLoader.java b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/store/VspGeneralLoader.java index b5d09daf3f..fbfde7431e 100644 --- a/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/store/VspGeneralLoader.java +++ b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/store/VspGeneralLoader.java @@ -12,67 +12,114 @@ import java.util.Map; import java.util.Optional; public class VspGeneralLoader { + + public static final String NAME = "name"; + public static final String GENERAL = "General"; + private static CassandraElementRepository cassandraElementRepository = - new CassandraElementRepository(); + new CassandraElementRepository(); public static Map<String, ElementEntity> load(SessionContext context, - Map<String, List<String>> vspItemVersionsMap) { + Map<String, List<String>> vspItemVersionsMap, + Map<String, List<String>> vspItemChangeRefssMap) { Map<String, ElementEntity> elementEntityMap = new HashMap<>(); System.setProperty("cassandra.dox.keystore", "zusammen_dox"); Id entityId; Id itemId; - Id versionId; + Id changeRefId; for (Map.Entry<String, List<String>> entry : vspItemVersionsMap.entrySet()) { for (String version : entry.getValue()) { itemId = new Id(entry.getKey()); - versionId = new Id(version); - entityId = getEntityIdByInfoNameValue(context, itemId, versionId, null, "name", - "General"); + changeRefId = new Id(version); + entityId = getEntityIdByInfoNameValue(context, itemId, changeRefId, null, null, NAME, + GENERAL); + if (entityId != null) { + Optional<ElementEntity> result = + cassandraElementRepository.get(context, new ElementEntityContext( + context.getUser().getUserName(), + itemId, + changeRefId), + new ElementEntity(entityId)); + if (result.isPresent()) { + elementEntityMap.put(buildKey(context, entry, version), result.get()); + } + } + } + } + + + for (Map.Entry<String, List<String>> entry : vspItemChangeRefssMap.entrySet()) { + + for (String changeRef : entry.getValue()) { + + + itemId = new Id(entry.getKey()); + + entityId = getEntityIdByInfoNameValue(context, itemId, null, changeRef,null, NAME, + GENERAL); if (entityId != null) { + ElementEntityContext elementContext = new ElementEntityContext( + context.getUser().getUserName(), + itemId, + null); + elementContext.setChangeRef(changeRef); Optional<ElementEntity> result = - cassandraElementRepository.get(context, new ElementEntityContext( - context.getUser().getUserName(), - itemId, - versionId), - new ElementEntity(entityId)); + cassandraElementRepository.get(context, elementContext, + new ElementEntity(entityId)); if (result.isPresent()) { - elementEntityMap.put(context.getUser().getUserName() + "_" + entry.getKey() - + "_" + version, result.get()); + elementEntityMap.put(buildKey(context, entry, changeRef), result.get()); } } } } + return elementEntityMap; } - private static Id getEntityIdByInfoNameValue(SessionContext context, Id itemId, Id versionId, - Id elementId, String - name, String value) { + public static String buildKey(SessionContext context, Map.Entry<String, List<String>> entry, String version) { + return String.format("%s_%s_%s", context.getUser().getUserName(), entry.getKey(), version); + } - Id id; + private static Id getEntityIdByInfoNameValue(SessionContext context, + Id itemId, + Id versionId, + String changeRef, + Id elementId, + String name, + String value) { + + + ElementEntityContext elementContext = new ElementEntityContext( + context.getUser().getUserName(), + itemId, + versionId); + if (changeRef != null) { + elementContext.setChangeRef(changeRef); + } Optional<ElementEntity> result = - cassandraElementRepository.get(context, new ElementEntityContext( - context.getUser().getUserName(), - itemId, - versionId), - new ElementEntity(Id.ZERO)); + cassandraElementRepository.get(context, elementContext, + new ElementEntity(Id.ZERO)); if (result.isPresent()) { ElementEntity elementEntity = result.get(); return elementEntity.getSubElementIds().stream().filter(subelementId -> { + ElementEntityContext subElementContext = new ElementEntityContext( + context.getUser().getUserName(), + itemId, + versionId); + if(changeRef!= null){ + subElementContext.setChangeRef(changeRef); + } Optional<ElementEntity> subElementEntity = - cassandraElementRepository.get(context, new ElementEntityContext( - context.getUser().getUserName(), - itemId, - versionId), - new ElementEntity(subelementId)); + cassandraElementRepository.get(context, subElementContext, + new ElementEntity(subelementId)); if (subElementEntity.isPresent()) { - if("name".equals(name)){ - if(value.equals(subElementEntity.get().getInfo().getName())){ + if (NAME.equals(name)) { + if (value.equals(subElementEntity.get().getInfo().getName())) { return true; } } @@ -87,9 +134,7 @@ public class VspGeneralLoader { return null; + } - - - } } |