Enable dependency injection for some aai-common classes
[aai/aai-common.git] / aai-schema-ingest / src / main / java / org / onap / aai / validation / edges / DefaultEdgeFieldsValidationModule.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 /**
22  * 
23  */
24
25 package org.onap.aai.validation.edges;
26
27 import java.util.EnumSet;
28 import java.util.Map;
29 import java.util.Map.Entry;
30
31 import org.onap.aai.edges.enums.EdgeField;
32 import org.springframework.stereotype.Component;
33
34 /**
35  * Default core A&AI edge field validation
36  * All fields in EdgeField enum are required EXCEPT description
37  *
38  */
39 @Component
40 public class DefaultEdgeFieldsValidationModule implements EdgeFieldsValidationModule {
41
42     /*
43      * (non-Javadoc)
44      * 
45      * @see org.onap.aai.edges.EdgeFieldsValidator#verifyFields(java.util.Map)
46      */
47     @Override
48     public String verifyFields(Map<String, String> rule) {
49         EnumSet<EdgeField> missingFields = EnumSet.complementOf(EnumSet.allOf(EdgeField.class));
50
51         for (EdgeField f : EdgeField.values()) {
52             if (!rule.containsKey(f.toString()) && (f != EdgeField.DESCRIPTION) && (f != EdgeField.PRIVATE)) { // description
53                                                                                                                // is
54                                                                                                                // optional
55                 missingFields.add(f);
56             }
57         }
58
59         StringBuilder errorMsg = new StringBuilder();
60         if (!missingFields.isEmpty()) {
61             errorMsg.append("Rule ").append(ruleToString(rule)).append(" missing required fields: ");
62             for (EdgeField mf : missingFields) {
63                 errorMsg.append(mf.toString()).append(" ");
64             }
65         }
66
67         return errorMsg.toString();
68     }
69
70     private String ruleToString(Map<String, String> rule) {
71         StringBuilder sb = new StringBuilder();
72         for (Entry<String, String> fields : rule.entrySet()) {
73             sb.append(fields.getKey()).append(":").append(fields.getValue()).append(" ");
74         }
75
76         return sb.toString();
77     }
78
79 }