747211e1f60af461ccd4722d1e7b4b91e060b53b
[aaf/authz.git] / auth / auth-locate / src / main / java / org / onap / aaf / auth / locate / validation / LocateValidator.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.locate.validation;
23
24 import org.onap.aaf.auth.validation.Validator;
25
26 import locate.v1_0.Endpoint;
27 import locate.v1_0.Endpoints;
28 import locate.v1_0.MgmtEndpoint;
29 import locate.v1_0.MgmtEndpoint.SpecialPorts;
30 import locate.v1_0.MgmtEndpoints;
31
32 /**
33  * Validator
34  * Consistently apply content rules for content (incoming)
35  * 
36  * Note: We restrict content for usability in URLs (because RESTful service), and avoid 
37  * issues with Regular Expressions, and other enabling technologies. 
38  * @author Jonathan
39  *
40  */
41 public class LocateValidator extends Validator {
42     private LocateValidator endpoint_key(Endpoint e) {
43         if (e==null) {
44             msg("Endpoint Data is null.");
45         } else {
46             nullOrBlank("Endpoint Name", e.getName());
47             if (e.getName()!=null) {
48                 int idx = e.getName().indexOf('.');
49                 if (idx<=0) {
50                     msg("Endpoint Name (" + e.getName() + ") must prefixed by Namespace");
51                 }
52             }
53             nullOrBlank("Endpoint Hostname", e.getHostname());
54             intRange("Endpoint Port",e.getPort(),0,1000000);
55         }
56         return this;
57     }
58
59
60     public LocateValidator endpoint(Endpoint e) {
61         endpoint_key(e);
62         if (e!=null) {
63             intRange("Endpoint Major Version",e.getMajor(),0,2000);
64             intRange("Endpoint Minor Version",e.getMinor(),0,2000);
65             intRange("Endpoint Patch Version",e.getPatch(),0,2000);
66             intRange("Endpoint Pkg Version",e.getPkg(),0,2000);
67             floatRange("Endpoint Latitude",e.getLatitude(),-90f,90f);
68             floatRange("Endpoint Longitude",e.getLongitude(),-180f,180f);
69             nullOrBlank("Endpoint Protocol", e.getProtocol());
70             for (String s : e.getSubprotocol()) {
71                 nullOrBlank("Endpoint Subprotocol", s);
72             }
73         }
74         return this;
75     }
76     
77     public LocateValidator endpoints(Endpoints e, boolean emptyNotOK) {
78         if (e==null) {
79             msg("Endpoints Data is null.");
80         } else {
81             if (emptyNotOK && e.getEndpoint().size()==0) {
82                 msg("Endpoints contains no endpoints");
83             } else {
84                 for (Endpoint ep : e.getEndpoint()) {
85                     endpoint(ep);
86                 }
87             }
88         }
89         return this;
90     }
91
92     public LocateValidator mgmt_endpoint_key(MgmtEndpoints meps) {
93         if (meps==null) {
94             msg("MgmtEndpoints Data is null.");
95         } else {
96             for (MgmtEndpoint ep : meps.getMgmtEndpoint()) {
97                 endpoint_key(ep);
98             }
99         }
100         return this;
101     }
102
103     public LocateValidator mgmt_endpoints(MgmtEndpoints me, boolean emptyOK) {
104         if (me==null) {
105             msg("MgmtEndpoints Data is null.");
106         } else {
107             if (!emptyOK && me.getMgmtEndpoint().size()==0) {
108                 msg("MgmtEndpoints contains no data");
109             } else {
110                 for (MgmtEndpoint ep : me.getMgmtEndpoint()) {
111                     mgmt_endpoint(ep);
112                 }
113             }
114         }
115         return this;
116     }
117
118     private LocateValidator mgmt_endpoint(MgmtEndpoint ep) {
119         endpoint(ep);
120         for (SpecialPorts sp : ep.getSpecialPorts()) {
121             specialPorts(sp);
122         }
123         return this;
124     }
125
126     private LocateValidator specialPorts(SpecialPorts sp) {
127         if (sp==null) {
128             msg("Special Ports is null.");
129         } else {
130             nullOrBlank("Special Port Name",sp.getName());
131             nullOrBlank("Special Port Protocol",sp.getProtocol());
132             intRange("Special Port",sp.getPort(),0,1000000);
133             
134             for (String s : sp.getProtocolVersions()) {
135                 nullOrBlank("Special Port Protocol Version", s);
136             }
137         }
138         return this;
139     }
140
141 }