AT&T 2.0.19 Code drop, stage 3
[aaf/authz.git] / auth / auth-certman / src / main / java / org / onap / aaf / auth / cm / validation / CertmanValidator.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 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 package org.onap.aaf.auth.cm.validation;
23
24 import java.util.List;
25
26 import org.onap.aaf.auth.dao.cass.ArtiDAO;
27 import org.onap.aaf.auth.dao.cass.ArtiDAO.Data;
28 import org.onap.aaf.auth.validation.Validator;
29
30 /**
31  * Validator
32  * Consistently apply content rules for content (incoming)
33  * 
34  * Note: We restrict content for usability in URLs (because RESTful service), and avoid 
35  * issues with Regular Expressions, and other enabling technologies. 
36  * @author Jonathan
37  *
38  */
39 public class CertmanValidator extends Validator{
40         // Repeated Msg fragments
41         private static final String MECHID = "mechid";
42         private static final String MACHINE = "machine";
43         private static final String ARTIFACT_LIST_IS_NULL = "Artifact List is null.";
44         private static final String Y = "y.";
45         private static final String IES = "ies.";
46         private static final String ENTR = " entr";
47         private static final String MUST_HAVE_AT_LEAST = " must have at least ";
48         private static final String IS_NULL = " is null.";
49         private static final String ARTIFACTS_MUST_HAVE_AT_LEAST = "Artifacts must have at least ";
50
51         public CertmanValidator nullBlankMin(String name, List<String> list, int min) {
52                 if(list==null) {
53                         msg(name + IS_NULL);
54                 } else {
55                         if(list.size()<min) {
56                                 msg(name + MUST_HAVE_AT_LEAST + min + ENTR + (min==1?Y:IES));
57                         } else {
58                                 for(String s : list) {
59                                         nullOrBlank("List Item",s);
60                                 }
61                         }
62                 }
63                 return this;
64         }
65
66         public CertmanValidator artisRequired(List<ArtiDAO.Data> list, int min) {
67                 if(list==null) {
68                         msg(ARTIFACT_LIST_IS_NULL);
69                 } else {
70                         if(list.size()<min) {
71                                 msg(ARTIFACTS_MUST_HAVE_AT_LEAST + min + ENTR + (min==1?Y:IES));
72                         } else {
73                                 for(ArtiDAO.Data a : list) {
74                                         allRequired(a);
75                                 }
76                         }
77                 }
78                 return this;
79         }
80
81         public CertmanValidator artisKeys(List<ArtiDAO.Data> list, int min) {
82                 if(list==null) {
83                         msg(ARTIFACT_LIST_IS_NULL);
84                 } else {
85                         if(list.size()<min) {
86                                 msg(ARTIFACTS_MUST_HAVE_AT_LEAST + min + ENTR + (min==1?Y:IES));
87                         } else {
88                                 for(ArtiDAO.Data a : list) {
89                                         keys(a);
90                                 }
91                         }
92                 }
93                 return this;
94         }
95
96
97         public CertmanValidator keys(ArtiDAO.Data add) {
98                 if(add==null) {
99                         msg("Artifact is null.");
100                 } else {
101                         nullOrBlank(MECHID, add.mechid);
102                         nullOrBlank(MACHINE, add.machine);
103                 }
104                 return this;
105         }
106         
107         private CertmanValidator allRequired(Data a) {
108                 if(a==null) {
109                         msg("Artifact is null.");
110                 } else {
111                         nullOrBlank(MECHID, a.mechid);
112                         nullOrBlank(MACHINE, a.machine);
113                         nullOrBlank("ca",a.ca);
114                         nullOrBlank("dir",a.dir);
115                         nullOrBlank("os_user",a.os_user);
116                         // Note: AppName, Notify & Sponsor are currently not required
117                 }
118                 return this;
119         }
120
121 }