Integrate aai-schema-ingest library into aai-core
[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-18 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
21 package org.onap.aai.validation.edges;
22
23 import java.util.Collection;
24 import java.util.HashSet;
25 import java.util.Set;
26
27 import org.onap.aai.nodes.NodeIngestor;
28 import org.onap.aai.setup.SchemaVersion;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.stereotype.Component;
31
32 /**
33  * Validates that the node types appearing in the edge rules are valid
34  * against the ingested OXM.
35  * 
36  * (This is why the node validation has to be run before the edge validation)
37  */
38 @Component
39 public class NodeTypesValidationModule {
40         private NodeIngestor ni;
41         
42         @Autowired
43         public NodeTypesValidationModule(NodeIngestor ni) {
44                 this.ni = ni;
45         }
46         
47         /**
48          * Validate that every node type in the given set is defined in 
49          * the OXM for the given version
50          * 
51          * @param nodeTypePairs - all the node types in
52          *                              the edge rules for the given version being validated
53          * @param v - the version being validated
54          * @return empty string if all types are present in the given version's ingested OXM, else
55          *      appropriate error message
56          */
57         public String validate(Collection<String> nodeTypePairs, SchemaVersion v) {
58                 //setup
59                 Set<String> nodeTypes = new HashSet<>();
60                 for (String pair : nodeTypePairs) {
61                         String[] types = pair.split("\\|");
62                         
63                         for (String type : types) {
64                                 if (!"".equals(type)) {
65                                         nodeTypes.add(type);
66                                 }
67                         }
68                 }
69                 
70                 //validation
71                 Set<String> badTypes = new HashSet<>();
72                 for (String type : nodeTypes) {
73                         if (!ni.hasNodeType(type, v)) {
74                                 badTypes.add(type);
75                         }
76                 }
77                 
78                 if (badTypes.isEmpty()) {
79                         return "";
80                 } else {
81                         StringBuilder errorBase = new StringBuilder().append("Invalid node type(s) found: ");
82                         for (String bt : badTypes) {
83                                 errorBase.append(bt).append(" ");
84                         }
85                         return errorBase.toString();
86                 }
87         }
88 }