[AAF-21] Initial code import
[aaf/authz.git] / authz-certman / src / main / java / com / att / authz / cm / validation / Validator.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aai\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * Copyright © 2017 Amdocs\r
7  * * ===========================================================================\r
8  * * Licensed under the Apache License, Version 2.0 (the "License");\r
9  * * you may not use this file except in compliance with the License.\r
10  * * You may obtain a copy of the License at\r
11  * * \r
12  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
13  * * \r
14  *  * Unless required by applicable law or agreed to in writing, software\r
15  * * distributed under the License is distributed on an "AS IS" BASIS,\r
16  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
17  * * See the License for the specific language governing permissions and\r
18  * * limitations under the License.\r
19  * * ============LICENSE_END====================================================\r
20  * *\r
21  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
22  * *\r
23  ******************************************************************************/\r
24 package com.att.authz.cm.validation;\r
25 \r
26 import java.util.List;\r
27 \r
28 import com.att.authz.layer.Result;\r
29 import com.att.dao.aaf.cass.ArtiDAO;\r
30 import com.att.dao.aaf.cass.ArtiDAO.Data;\r
31 \r
32 /**\r
33  * Validator\r
34  * Consistently apply content rules for content (incoming)\r
35  * \r
36  * Note: We restrict content for usability in URLs (because RESTful service), and avoid \r
37  * issues with Regular Expressions, and other enabling technologies. \r
38  *\r
39  */\r
40 public class Validator {\r
41         // Repeated Msg fragments\r
42         private static final String MECHID = "mechid";\r
43         private static final String MACHINE = "machine";\r
44         private static final String ARTIFACT_LIST_IS_NULL = "Artifact List is null.";\r
45         private static final String Y = "y.";\r
46         private static final String IES = "ies.";\r
47         private static final String ENTR = " entr";\r
48         private static final String MUST_HAVE_AT_LEAST = " must have at least ";\r
49         private static final String IS_NULL = " is null.";\r
50         private static final String ARTIFACTS_MUST_HAVE_AT_LEAST = "Artifacts must have at least ";\r
51         private StringBuilder msgs;\r
52 \r
53         public Validator nullOrBlank(String name, String str) {\r
54                 if(str==null) {\r
55                         msg(name + IS_NULL);\r
56                 } else if(str.length()==0) {\r
57                         msg(name + " is blank.");\r
58                 }\r
59                 return this;\r
60         }\r
61         \r
62         private void msg(String ... strs) {\r
63                 if(msgs==null) {\r
64                         msgs=new StringBuilder();\r
65                 }\r
66                 for(String str : strs) {\r
67                         msgs.append(str);\r
68                 }\r
69                 msgs.append('\n');\r
70         }\r
71         \r
72         public boolean err() {\r
73                 return msgs!=null;\r
74         }\r
75         \r
76         public String errs() {\r
77                 return msgs.toString();\r
78         }\r
79 \r
80         public Validator notOK(Result<?> res) {\r
81                 if(res==null) {\r
82                         msgs.append("Result object is blank");\r
83                 } else if(res.notOK()) {\r
84                         msgs.append(res.getClass().getSimpleName() + " is not OK");\r
85                 }\r
86                 return this;\r
87         }\r
88 \r
89         public Validator isNull(String name, Object obj) {\r
90                 if(obj==null) {\r
91                         msg(name + IS_NULL);\r
92                 } \r
93                 return this;\r
94         }\r
95 \r
96         public Validator nullBlankMin(String name, List<String> list, int min) {\r
97                 if(list==null) {\r
98                         msg(name + IS_NULL);\r
99                 } else {\r
100                         if(list.size()<min) {\r
101                                 msg(name + MUST_HAVE_AT_LEAST + min + ENTR + (min==1?Y:IES));\r
102                         } else {\r
103                                 for(String s : list) {\r
104                                         nullOrBlank("List Item",s);\r
105                                 }\r
106                         }\r
107                 }\r
108                 return this;\r
109         }\r
110 \r
111         public Validator artisRequired(List<ArtiDAO.Data> list, int min) {\r
112                 if(list==null) {\r
113                         msg(ARTIFACT_LIST_IS_NULL);\r
114                 } else {\r
115                         if(list.size()<min) {\r
116                                 msg(ARTIFACTS_MUST_HAVE_AT_LEAST + min + ENTR + (min==1?Y:IES));\r
117                         } else {\r
118                                 for(ArtiDAO.Data a : list) {\r
119                                         allRequired(a);\r
120                                 }\r
121                         }\r
122                 }\r
123                 return this;\r
124         }\r
125 \r
126         public Validator artisKeys(List<ArtiDAO.Data> list, int min) {\r
127                 if(list==null) {\r
128                         msg(ARTIFACT_LIST_IS_NULL);\r
129                 } else {\r
130                         if(list.size()<min) {\r
131                                 msg(ARTIFACTS_MUST_HAVE_AT_LEAST + min + ENTR + (min==1?Y:IES));\r
132                         } else {\r
133                                 for(ArtiDAO.Data a : list) {\r
134                                         keys(a);\r
135                                 }\r
136                         }\r
137                 }\r
138                 return this;\r
139         }\r
140 \r
141 \r
142         public Validator keys(ArtiDAO.Data add) {\r
143                 if(add==null) {\r
144                         msg("Artifact is null.");\r
145                 } else {\r
146                         nullOrBlank(MECHID, add.mechid);\r
147                         nullOrBlank(MACHINE, add.machine);\r
148                 }\r
149                 return this;\r
150         }\r
151         \r
152         private Validator allRequired(Data a) {\r
153                 if(a==null) {\r
154                         msg("Artifact is null.");\r
155                 } else {\r
156                         nullOrBlank(MECHID, a.mechid);\r
157                         nullOrBlank(MACHINE, a.machine);\r
158                         nullOrBlank("ca",a.ca);\r
159                         nullOrBlank("dir",a.dir);\r
160                         nullOrBlank("os_user",a.os_user);\r
161                         // Note: AppName, Notify & Sponsor are currently not required\r
162                 }\r
163                 return this;\r
164         }\r
165 \r
166 }\r