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