diff options
author | olegb <olegb@amdocs.com> | 2018-06-25 08:53:07 +0300 |
---|---|---|
committer | Oren Kleks <orenkle@amdocs.com> | 2018-06-27 05:59:44 +0000 |
commit | c928dd43210e96b4bbd5054d52a143778e43bf7a (patch) | |
tree | a150fa7fa5ec6b7416dd30387478ff53670af97f /openecomp-be/tools/zusammen-tools | |
parent | a1987465fcd32f9c70a6675e39acf684fa7d8a89 (diff) |
Utility for version elements validation
Change-Id: I44faca312045dc69966639f65fa9719b0a924027
Issue-ID: SDC-1443
Signed-off-by: Oleg Beltz <olegb@amdocs.com>
Diffstat (limited to 'openecomp-be/tools/zusammen-tools')
8 files changed, 399 insertions, 37 deletions
diff --git a/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/itemvalidation/ItemValidation.java b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/itemvalidation/ItemValidation.java new file mode 100644 index 0000000000..855c668070 --- /dev/null +++ b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/itemvalidation/ItemValidation.java @@ -0,0 +1,178 @@ +/* +* Copyright © 2016-2018 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.itemvalidation; + +import org.openecomp.core.tools.store.ElementCassandraLoader; +import org.openecomp.core.tools.store.ItemHandler; +import org.openecomp.core.tools.store.VersionCassandraLoader; +import org.openecomp.core.tools.store.VersionElementsCassandraLoader; +import org.openecomp.core.tools.store.zusammen.datatypes.ElementEntity; +import org.openecomp.core.tools.store.zusammen.datatypes.VersionElementsEntity; +import org.openecomp.core.tools.store.zusammen.datatypes.VersionEntity; +import org.openecomp.core.zusammen.impl.CassandraConnectionInitializer; +import org.openecomp.sdc.common.session.SessionContextProviderFactory; +import org.openecomp.sdc.logging.api.Logger; +import org.openecomp.sdc.logging.api.LoggerFactory; + +import java.text.SimpleDateFormat; +import java.util.*; +import java.util.stream.Collectors; + +import static org.openecomp.core.tools.util.Utils.printMessage; +import static org.openecomp.core.tools.util.Utils.logError; + +public class ItemValidation { + private static final Logger logger = LoggerFactory.getLogger(ItemValidation.class); + private static final String NEW_LINE = System.getProperty("line.separator"); + private static final String PUBLIC_SPACE = "public"; + private static String itemId = null; + private List<String> subElementsList; + private List<String> validationMessage; + + public static void main(String[] args) { + for (int i = 0; i < args.length; i++) { + if ("-i".equals(args[i])) { + if (args[i+1] != null) { + itemId = args[i + 1]; + } + break; + } + } + + if (itemId == null) { + printMessage(logger, "-i 123456 "); + System.exit(-1); + } + + ItemValidation itemValidation = new ItemValidation(); + itemValidation.validate(); + System.exit(1); + } + + private void validate() { + + SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); + subElementsList = new LinkedList<>(); + validationMessage = new LinkedList<>(); + + try { + SessionContextProviderFactory.getInstance().createInterface().create("GLOBAL_USER", "dox"); + CassandraConnectionInitializer.setCassandraConnectionPropertiesToSystem(); + + List<String> itemList = new ItemHandler().getItemList(); + if (itemList.stream().filter(item -> item.equals(itemId)).collect(Collectors.toList()).isEmpty()) { + printMessage(logger,String.format("%s No data found for itemId: %s %s", NEW_LINE, itemId, NEW_LINE)); + return; + } + printMessage(logger,String.format("%s Validation started at %s %S", NEW_LINE, + sdf.format(new Date()), NEW_LINE)); + + List<VersionEntity> versionEntityList = new VersionCassandraLoader().list().all().stream(). + filter(entry -> entry.getItemId().equals(itemId)).collect(Collectors.toList()); + + versionEntityList.sort(( VersionEntity e1, VersionEntity e2) -> { + if (e1.getSpace().equals(PUBLIC_SPACE)) { + return -1; + } else if (e2.getSpace().equals(PUBLIC_SPACE)) { + return 1; + } else { + return e1.getSpace().compareTo(e2.getSpace()); + } + }); + + versionEntityList.forEach((VersionEntity versionEntity) -> { + List<VersionElementsEntity> versionElementsEntityList = new VersionElementsCassandraLoader(). + listVersionElementsByPK(versionEntity.getSpace(), versionEntity.getItemId(), + versionEntity.getVersionId()).all(); + versionElementsEntityList.forEach(this::accept); + }); + }catch (Exception ex) { + logError(logger,ex); + } + if (validationMessage.isEmpty()) { + printMessage(logger,String.format("%s Item %s is successfully validated.", NEW_LINE, itemId)); + } else { + printMessage(logger,"\n Errors:"); + for (String message : validationMessage) { + printMessage(logger,message); + } + } + printMessage(logger,String.format("%s Validation ended at %s %s", NEW_LINE, sdf.format(new Date()), NEW_LINE)); + } + + private void validateElement(String space, String itemId, String versionId, + String elementId, String revisionId) { + + ElementEntity elementEntity = + new ElementCassandraLoader().getByPK(space, itemId, versionId, elementId, revisionId).one(); + + if (elementEntity == null) { + validationMessage.add(String.format( + "Element is defined in VERSION_ELEMENTS.element_ids is not found in ELEMENT. " + + "Space:%s, ItemID:%s ,VersionID:%s, ElementID:%s, element revisionID:%s", + space, itemId, versionId, elementId, revisionId)); + return; + } + + if (elementEntity.getSubElementIds() != null) { + subElementsList.addAll(elementEntity.getSubElementIds()); + } + } + + private void accept(VersionElementsEntity versionElementsEntity) { + printMessage(logger, String.format( + "Found VERSION_ELEMENTS entry. Space: %s, ItemID: %s, VersionId: %s, Revision: %s", + versionElementsEntity.getSpace(), + versionElementsEntity.getItemId(), + versionElementsEntity.getVersionId(), + versionElementsEntity.getRevisionId())); + + subElementsList.clear(); + if (versionElementsEntity.getElementIds().isEmpty()) { + validationMessage.add(String.format + ("Empty field VERSION_ELEMENT.element_ids. Space: %s, ItemID: %s, VersionId: %s, Revision: %s", + versionElementsEntity.getSpace(), + versionElementsEntity.getItemId(), + versionElementsEntity.getVersionId(), + versionElementsEntity.getRevisionId())); + } else { + + //loop over element_ids stored in version_elements + versionElementsEntity.getElementIds().forEach((key, value) -> + validateElement(versionElementsEntity.getSpace(), + versionElementsEntity.getItemId(), + versionElementsEntity.getVersionId(), + key, + value) + ); + + //loop over collected sub-elements to insure existence in version_elements.elements_ids + subElementsList.forEach((String key) -> { + if (!versionElementsEntity.getElementIds().containsKey(key)) { + validationMessage.add(String.format( + "Element is defined in table ELEMENT but not found in VERSION_ELEMENTS.element_ids." + + " Space:%s, ItemID:%s, VersionID:%s, Version RevisionID:%s, ElementID:%s", + versionElementsEntity.getSpace(), + versionElementsEntity.getItemId(), + versionElementsEntity.getVersionId(), + versionElementsEntity.getRevisionId(), + key)); + } + }); + } + } +}
\ No newline at end of file diff --git a/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/store/ElementCassandraLoader.java b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/store/ElementCassandraLoader.java index 1c7e185e18..d7f2ba63d6 100644 --- a/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/store/ElementCassandraLoader.java +++ b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/store/ElementCassandraLoader.java @@ -1,26 +1,22 @@ -/*- - * ============LICENSE_START======================================================= - * SDC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * 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. - * ============LICENSE_END========================================================= - */ +/* +* Copyright © 2016-2018 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.datastax.driver.mapping.Mapper; import com.datastax.driver.mapping.Result; import com.datastax.driver.mapping.annotations.Accessor; import com.datastax.driver.mapping.annotations.Query; @@ -35,17 +31,13 @@ import java.util.Set; public class ElementCassandraLoader { private static NoSqlDb noSqlDb = NoSqlDbFactory.getInstance().createInterface(); - private static Mapper<ElementEntity> mapper = noSqlDb.getMappingManager().mapper(ElementEntity.class); private static ElementAccessor accessor = noSqlDb.getMappingManager().createAccessor(ElementAccessor.class); - private String[] columns = {"space", "item_id", "version_id", "element_id", "data", "info", "namespace", "parent_id", - "relations", "searchable_data", "sub_element_ids"}; - public void createEntity(ElementEntity elementEntity) { accessor.insertElement(elementEntity.getSpace(), elementEntity.getItemId(), elementEntity.getVersionId(), - elementEntity.getElement_id(), + elementEntity.getElementId(), elementEntity.getData(), elementEntity.getInfo(), elementEntity.getNamespace(), @@ -58,17 +50,26 @@ public class ElementCassandraLoader { public Result<ElementEntity> list() { return accessor.getAll(); } - + public Result<ElementEntity> getByPK(String space, String itemId, String versionId, String elementId, + String revisionId) { + return accessor.getByPK(space, itemId, versionId, elementId, revisionId); + } @Accessor interface ElementAccessor { - @Query("insert into zusammen_dox.element (space,item_id,version_id,element_id,data,info,namespace,parent_id,relations,searchable_data,sub_element_ids) values (?,?,?,?,?,?,?,?,?,?,?)") - void insertElement(String space, String itemId, String versionId, String elementId, ByteBuffer data, String info, String namespaceStr, - String parentId, String relations, ByteBuffer searchable, Set<String> subElementsIds); + @Query("insert into zusammen_dox.element (space,item_id,version_id,element_id,data,info," + + "namespace,parent_id,relations,searchable_data,sub_element_ids) values (?,?,?,?,?,?,?,?,?,?,?)") + void insertElement(String space, String itemId, String versionId, String elementId, ByteBuffer data, + String info, String namespaceStr, String parentId, String relations, ByteBuffer searchable, + Set<String> subElementsIds); @Query("select * from zusammen_dox.element ") @QueryParameters(fetchSize = 100) Result<ElementEntity> getAll(); + @Query("select * from zusammen_dox.element where space = ? and item_id = ? and version_id = ? and " + + "element_id = ? and revision_id = ?") + Result<ElementEntity> getByPK(String space, String itemId, String versionId, String elementId, + String revisionId); } } diff --git a/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/store/ElementNamespaceHandler.java b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/store/ElementNamespaceHandler.java index f1ed970b4d..93897da4c9 100644 --- a/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/store/ElementNamespaceHandler.java +++ b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/store/ElementNamespaceHandler.java @@ -14,7 +14,7 @@ public class ElementNamespaceHandler { private static ElementNamespaceAccessor accessor = nnoSqlDb.getMappingManager().createAccessor(ElementNamespaceAccessor.class); public void createElementNamespace(ElementEntity elementEntity) { - accessor.create(elementEntity.getSpace(),elementEntity.getItemId(),elementEntity.getElement_id(),elementEntity.getNamespace()); + accessor.create(elementEntity.getSpace(),elementEntity.getItemId(),elementEntity.getElementId(),elementEntity.getNamespace()); } @Accessor diff --git a/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/store/VersionCassandraLoader.java b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/store/VersionCassandraLoader.java index fd9dc25daa..dc7d5cb40f 100644 --- a/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/store/VersionCassandraLoader.java +++ b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/store/VersionCassandraLoader.java @@ -42,7 +42,7 @@ public class VersionCassandraLoader { private static VersionAccessor accessor = noSqlDb.getMappingManager().createAccessor(VersionAccessor.class); public void insertElementToVersion(ElementEntity elementEntity) { - accessor.addElements(Sets.newHashSet(elementEntity.getElement_id()), elementEntity.getSpace(), elementEntity.getItemId(), elementEntity.getVersionId()); + accessor.addElements(Sets.newHashSet(elementEntity.getElementId()), elementEntity.getSpace(), elementEntity.getItemId(), elementEntity.getVersionId()); } public void insertVersion(VersionEntity versionEntity) { diff --git a/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/store/VersionElementsCassandraLoader.java b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/store/VersionElementsCassandraLoader.java new file mode 100644 index 0000000000..5c02af408f --- /dev/null +++ b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/store/VersionElementsCassandraLoader.java @@ -0,0 +1,45 @@ +/* +* Copyright © 2016-2018 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.datastax.driver.mapping.Result; +import com.datastax.driver.mapping.annotations.Accessor; +import com.datastax.driver.mapping.annotations.Query; +import com.datastax.driver.mapping.annotations.QueryParameters; +import org.openecomp.core.nosqldb.api.NoSqlDb; +import org.openecomp.core.nosqldb.factory.NoSqlDbFactory; +import org.openecomp.core.tools.store.zusammen.datatypes.VersionElementsEntity; + +public class VersionElementsCassandraLoader { + private static NoSqlDb noSqlDb = NoSqlDbFactory.getInstance().createInterface(); + private static VersionElementsAccessor accessor = noSqlDb.getMappingManager(). + createAccessor(VersionElementsAccessor.class); + + public Result<VersionElementsEntity> listVersionElementsByPK(String space, String itemId, String versionId) { + return accessor.getByPK(space, itemId, versionId); + } + + @Accessor + interface VersionElementsAccessor { + + @Query("SELECT space, item_id, version_id, revision_id, element_ids " + + "FROM zusammen_dox.version_elements WHERE space=? and item_id=? and version_id=?") + + @QueryParameters(fetchSize = 400) + Result<VersionElementsEntity> getByPK(String space, String itemId, String versionId); + } +} diff --git a/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/store/zusammen/datatypes/ElementEntity.java b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/store/zusammen/datatypes/ElementEntity.java index f4f450db70..d1fd93dd71 100644 --- a/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/store/zusammen/datatypes/ElementEntity.java +++ b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/store/zusammen/datatypes/ElementEntity.java @@ -1,3 +1,19 @@ +/* +* Copyright © 2016-2018 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.zusammen.datatypes; import com.datastax.driver.mapping.annotations.Column; @@ -26,7 +42,7 @@ import java.util.Set; */ @Table( keyspace = "zusammen_dox", - name = "version" + name = "element" ) public class ElementEntity { @Column( name = "space" ) @@ -43,7 +59,7 @@ public class ElementEntity { @Column(name = "element_id") @PartitionKey(3) - private String element_id; + private String elementId; @Column(name = "data") private ByteBuffer data; @@ -94,12 +110,12 @@ public class ElementEntity { this.versionId = versionId; } - public String getElement_id() { - return element_id; + public String getElementId() { + return elementId; } - public void setElement_id(String element_id) { - this.element_id = element_id; + public void setElementId(String elementId) { + this.elementId = elementId; } public ByteBuffer getData() { diff --git a/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/store/zusammen/datatypes/VersionElementsEntity.java b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/store/zusammen/datatypes/VersionElementsEntity.java new file mode 100644 index 0000000000..936d7198c8 --- /dev/null +++ b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/store/zusammen/datatypes/VersionElementsEntity.java @@ -0,0 +1,101 @@ +/* +* Copyright © 2016-2018 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.zusammen.datatypes; + +import com.datastax.driver.mapping.annotations.ClusteringColumn; +import com.datastax.driver.mapping.annotations.Column; +import com.datastax.driver.mapping.annotations.PartitionKey; +import com.datastax.driver.mapping.annotations.Table; + +import java.util.Map; + +/** + * CREATE TABLE zusammen_dox.version_elements ( + * space text, + * item_id text, + * version_id text, + * revision_id text, + * conflict_element_ids set<text>, + * dirty_element_ids set<text>, + * element_ids map<text, text>, + * message text, + * publish_time timestamp, + * stage_element_ids set<text>, + * user text, + * PRIMARY KEY ((space, item_id, version_id), revision_id)) + * WITH CLUSTERING ORDER BY (revision_id ASC) + */ + +@Table( + keyspace = "zusammen_dox", + name = "version_elements" +) +public class VersionElementsEntity { + @Column( name = "space" ) + @PartitionKey(0) + private String space; + + @Column( name = "item_id" ) + @PartitionKey(1) + private String itemId; + + @Column( name = "version_id" ) + @PartitionKey(2) + private String versionId; + + @Column(name = "revision_id") + @ClusteringColumn + private String revisionId; + + @Column(name = "element_ids") + private Map<String,String> elementIds; + + public void setSpace(String space) { + this.space = space; + } + public String getSpace() { + return space; + } + + public void setItemId(String itemId) { + this.itemId = itemId; + } + public String getItemId() { + return itemId; + } + + public void setVersionId(String versionId) { + this.versionId = versionId; + } + public String getVersionId() { + return versionId; + } + + public void setRevisionId(String revisionId) { + this.revisionId = revisionId; + } + public String getRevisionId() { + return revisionId; + } + + public void setElementIds(Map<String,String> elementIds) { + this.elementIds = elementIds; + } + public Map<String,String> getElementIds() { + return elementIds; + } +} diff --git a/openecomp-be/tools/zusammen-tools/src/main/resources/itemValidation.sh b/openecomp-be/tools/zusammen-tools/src/main/resources/itemValidation.sh new file mode 100644 index 0000000000..24e4523938 --- /dev/null +++ b/openecomp-be/tools/zusammen-tools/src/main/resources/itemValidation.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +########################################################################################################## +# script name - itemValidation.sh +# run script - ./itemValidation.sh -i ${itemId} +########################################################################################################## + +OSTYPE=`uname -a | grep -iq ubuntu; echo $?` +echo "${OSTYPE}" + +if [ ${OSTYPE} -eq 0 ] +then + CONF_FILE_LOCATION="/opt/app/jetty/base/be/config/catalog-be/configuration.yaml" +else + CONF_FILE_LOCATION="/apps/jetty/base/be/config/catalog-be/configuration.yaml" +fi +echo "Configuration file location: ${CONF_FILE_LOCATION}" +mv lib/openecomp-zusammen-tools*.jar openecomp-zusammen-tools.jar &>/dev/null + +java -Dconfig.home=/opt/app/jetty/base/be/config -Dlog.home=/apps/jetty/base/be/logs -Dconfiguration.yaml=${CONF_FILE_LOCATION} -classpath openecomp-zusammen-tools.jar:lib/* org.openecomp.core.tools.itemvalidation.ItemValidation $1 $2 +STATUS="${?}" echo "${STATUS}" |