2 * Copyright © 2016-2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openecomp.core.tools.itemvalidation;
19 import org.openecomp.core.tools.store.ElementCassandraLoader;
20 import org.openecomp.core.tools.store.ItemHandler;
21 import org.openecomp.core.tools.store.VersionCassandraLoader;
22 import org.openecomp.core.tools.store.VersionElementsCassandraLoader;
23 import org.openecomp.core.tools.store.zusammen.datatypes.ElementEntity;
24 import org.openecomp.core.tools.store.zusammen.datatypes.VersionElementsEntity;
25 import org.openecomp.core.tools.store.zusammen.datatypes.VersionEntity;
26 import org.openecomp.core.zusammen.impl.CassandraConnectionInitializer;
27 import org.openecomp.sdc.common.session.SessionContextProviderFactory;
28 import org.openecomp.sdc.logging.api.Logger;
29 import org.openecomp.sdc.logging.api.LoggerFactory;
31 import java.text.SimpleDateFormat;
33 import java.util.stream.Collectors;
35 import static org.openecomp.core.tools.util.Utils.printMessage;
36 import static org.openecomp.core.tools.util.Utils.logError;
38 public class ItemValidation {
39 private static final Logger logger = LoggerFactory.getLogger(ItemValidation.class);
40 private static final String NEW_LINE = System.getProperty("line.separator");
41 private static final String PUBLIC_SPACE = "public";
42 private static String itemId = null;
43 private List<String> subElementsList;
44 private List<String> validationMessage;
46 public static void main(String[] args) {
47 for (int i = 0; i < args.length; i++) {
48 if ("-i".equals(args[i])) {
49 if (args[i+1] != null) {
57 printMessage(logger, "-i 123456 ");
61 ItemValidation itemValidation = new ItemValidation();
62 itemValidation.validate();
66 private void validate() {
68 SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
69 subElementsList = new LinkedList<>();
70 validationMessage = new LinkedList<>();
73 SessionContextProviderFactory.getInstance().createInterface().create("GLOBAL_USER", "dox");
74 CassandraConnectionInitializer.setCassandraConnectionPropertiesToSystem();
76 List<String> itemList = new ItemHandler().getItemList();
77 if (itemList.stream().filter(item -> item.equals(itemId)).collect(Collectors.toList()).isEmpty()) {
78 printMessage(logger,String.format("%s No data found for itemId: %s %s", NEW_LINE, itemId, NEW_LINE));
81 printMessage(logger,String.format("%s Validation started at %s %S", NEW_LINE,
82 sdf.format(new Date()), NEW_LINE));
84 List<VersionEntity> versionEntityList = new VersionCassandraLoader().list().all().stream().
85 filter(entry -> entry.getItemId().equals(itemId)).collect(Collectors.toList());
87 versionEntityList.sort(( VersionEntity e1, VersionEntity e2) -> {
88 if (e1.getSpace().equals(PUBLIC_SPACE)) {
90 } else if (e2.getSpace().equals(PUBLIC_SPACE)) {
93 return e1.getSpace().compareTo(e2.getSpace());
97 versionEntityList.forEach((VersionEntity versionEntity) -> {
98 List<VersionElementsEntity> versionElementsEntityList = new VersionElementsCassandraLoader().
99 listVersionElementsByPK(versionEntity.getSpace(), versionEntity.getItemId(),
100 versionEntity.getVersionId()).all();
101 versionElementsEntityList.forEach(this::accept);
103 }catch (Exception ex) {
106 if (validationMessage.isEmpty()) {
107 printMessage(logger,String.format("%s Item %s is successfully validated.", NEW_LINE, itemId));
109 printMessage(logger,"\n Errors:");
110 for (String message : validationMessage) {
111 printMessage(logger,message);
114 printMessage(logger,String.format("%s Validation ended at %s %s", NEW_LINE, sdf.format(new Date()), NEW_LINE));
117 private void validateElement(String space, String itemId, String versionId,
118 String elementId, String revisionId) {
120 ElementEntity elementEntity =
121 new ElementCassandraLoader().getByPK(space, itemId, versionId, elementId, revisionId).one();
123 if (elementEntity == null) {
124 validationMessage.add(String.format(
125 "Element is defined in VERSION_ELEMENTS.element_ids is not found in ELEMENT. " +
126 "Space:%s, ItemID:%s ,VersionID:%s, ElementID:%s, element revisionID:%s",
127 space, itemId, versionId, elementId, revisionId));
131 if (elementEntity.getSubElementIds() != null) {
132 subElementsList.addAll(elementEntity.getSubElementIds());
136 private void accept(VersionElementsEntity versionElementsEntity) {
137 printMessage(logger, String.format(
138 "Found VERSION_ELEMENTS entry. Space: %s, ItemID: %s, VersionId: %s, Revision: %s",
139 versionElementsEntity.getSpace(),
140 versionElementsEntity.getItemId(),
141 versionElementsEntity.getVersionId(),
142 versionElementsEntity.getRevisionId()));
144 subElementsList.clear();
145 if (versionElementsEntity.getElementIds().isEmpty()) {
146 validationMessage.add(String.format
147 ("Empty field VERSION_ELEMENT.element_ids. Space: %s, ItemID: %s, VersionId: %s, Revision: %s",
148 versionElementsEntity.getSpace(),
149 versionElementsEntity.getItemId(),
150 versionElementsEntity.getVersionId(),
151 versionElementsEntity.getRevisionId()));
154 //loop over element_ids stored in version_elements
155 versionElementsEntity.getElementIds().forEach((key, value) ->
156 validateElement(versionElementsEntity.getSpace(),
157 versionElementsEntity.getItemId(),
158 versionElementsEntity.getVersionId(),
163 //loop over collected sub-elements to insure existence in version_elements.elements_ids
164 subElementsList.forEach((String key) -> {
165 if (!versionElementsEntity.getElementIds().containsKey(key)) {
166 validationMessage.add(String.format(
167 "Element is defined in table ELEMENT but not found in VERSION_ELEMENTS.element_ids."
168 + " Space:%s, ItemID:%s, VersionID:%s, Version RevisionID:%s, ElementID:%s",
169 versionElementsEntity.getSpace(),
170 versionElementsEntity.getItemId(),
171 versionElementsEntity.getVersionId(),
172 versionElementsEntity.getRevisionId(),