Added oparent to sdc main
[sdc.git] / openecomp-be / tools / zusammen-tools / src / main / java / org / openecomp / core / tools / itemvalidation / ItemValidation.java
1 /*
2 * Copyright © 2016-2018 European Support Limited
3 *
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
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 package org.openecomp.core.tools.itemvalidation;
18
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;
30
31 import java.text.SimpleDateFormat;
32 import java.util.*;
33 import java.util.stream.Collectors;
34
35 import static org.openecomp.core.tools.util.Utils.printMessage;
36 import static org.openecomp.core.tools.util.Utils.logError;
37
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;
45
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) {
50                     itemId = args[i + 1];
51                 }
52                 break;
53             }
54         }
55
56         if (itemId == null) {
57             printMessage(logger, "-i 123456 ");
58             System.exit(-1);
59         }
60
61         ItemValidation itemValidation = new ItemValidation();
62         itemValidation.validate();
63         System.exit(1);
64     }
65
66     private void validate() {
67
68         SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
69         subElementsList = new LinkedList<>();
70         validationMessage = new LinkedList<>();
71
72         try {
73             SessionContextProviderFactory.getInstance().createInterface().create("GLOBAL_USER", "dox");
74             CassandraConnectionInitializer.setCassandraConnectionPropertiesToSystem();
75
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));
79                 return;
80             }
81             printMessage(logger,String.format("%s Validation started at %s %S", NEW_LINE,
82                     sdf.format(new Date()), NEW_LINE));
83
84             List<VersionEntity> versionEntityList = new VersionCassandraLoader().list().all().stream().
85                     filter(entry -> entry.getItemId().equals(itemId)).collect(Collectors.toList());
86
87             versionEntityList.sort(( VersionEntity e1, VersionEntity e2) -> {
88                      if (e1.getSpace().equals(PUBLIC_SPACE)) {
89                          return -1;
90                      } else if (e2.getSpace().equals(PUBLIC_SPACE)) {
91                          return 1;
92                      } else {
93                          return e1.getSpace().compareTo(e2.getSpace());
94                      }
95             });
96
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);
102             });
103          }catch (Exception ex) {
104             logError(logger,ex);
105         }
106         if (validationMessage.isEmpty()) {
107             printMessage(logger,String.format("%s Item %s is successfully validated.", NEW_LINE, itemId));
108         } else {
109             printMessage(logger,"\n Errors:");
110             for (String message : validationMessage) {
111                 printMessage(logger,message);
112             }
113         }
114         printMessage(logger,String.format("%s Validation ended at %s %s", NEW_LINE, sdf.format(new Date()), NEW_LINE));
115     }
116
117     private void validateElement(String space, String itemId, String versionId,
118                                  String elementId, String revisionId) {
119
120         ElementEntity elementEntity =
121                 new ElementCassandraLoader().getByPK(space, itemId, versionId, elementId, revisionId).one();
122
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));
128             return;
129         }
130
131         if (elementEntity.getSubElementIds() != null) {
132             subElementsList.addAll(elementEntity.getSubElementIds());
133         }
134     }
135
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()));
143
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()));
152         } else {
153
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(),
159                         key,
160                         value)
161             );
162
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(),
173                             key));
174                 }
175             });
176         }
177     }
178 }