added schema validation tools
[aai/aai-common.git] / aai-schema-ingest / src / main / java / org / onap / aai / validation / edges / NodeTypesValidationModule.java
1 /** 
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  *
20  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22
23 package org.onap.aai.validation.edges;
24
25 import java.util.Collection;
26 import java.util.HashSet;
27 import java.util.Set;
28
29 import org.onap.aai.nodes.NodeIngestor;
30 import org.onap.aai.setup.Version;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.stereotype.Component;
33
34 /**
35  * Validates that the node types appearing in the edge rules are valid
36  * against the ingested OXM.
37  * 
38  * (This is why the node validation has to be run before the edge validation)
39  */
40 @Component
41 public class NodeTypesValidationModule {
42         private NodeIngestor ni;
43         
44         @Autowired
45         public NodeTypesValidationModule(NodeIngestor ni) {
46                 this.ni = ni;
47         }
48         
49         /**
50          * Validate that every node type in the given set is defined in 
51          * the OXM for the given version
52          * 
53          * @param Collection<String> nodeTypes - all the node types in
54          *                              the edge rules for the given version being validated
55          * @param Version v - the version being validated 
56          * @return empty string if all types are present in the given version's ingested OXM, else
57          *      appropriate error message
58          */
59         public String validate(Collection<String> nodeTypePairs, Version v) {
60                 //setup
61                 Set<String> nodeTypes = new HashSet<>();
62                 for (String pair : nodeTypePairs) {
63                         String[] types = pair.split("\\|");
64                         
65                         for (String type : types) {
66                                 if (!"".equals(type)) {
67                                         nodeTypes.add(type);
68                                 }
69                         }
70                 }
71                 
72                 //validation
73                 Set<String> badTypes = new HashSet<>();
74                 for (String type : nodeTypes) {
75                         if (!ni.hasNodeType(type, v)) {
76                                 badTypes.add(type);
77                         }
78                 }
79                 
80                 if (badTypes.isEmpty()) {
81                         return "";
82                 } else {
83                         StringBuilder errorBase = new StringBuilder().append("Invalid node type(s) found: ");
84                         for (String bt : badTypes) {
85                                 errorBase.append(bt).append(" ");
86                         }
87                         return errorBase.toString();
88                 }
89         }
90 }