Remove Tabs, per Jococo
[aaf/authz.git] / auth / auth-cass / src / main / java / org / onap / aaf / auth / direct / DirectAAFLocator.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.direct;
23
24 import java.net.URI;
25 import java.net.URISyntaxException;
26 import java.net.UnknownHostException;
27 import java.util.Collections;
28 import java.util.LinkedList;
29 import java.util.List;
30
31 import org.onap.aaf.auth.dao.cass.LocateDAO;
32 import org.onap.aaf.auth.dao.cass.LocateDAO.Data;
33 import org.onap.aaf.auth.env.AuthzEnv;
34 import org.onap.aaf.auth.env.AuthzTrans;
35 import org.onap.aaf.auth.layer.Result;
36 import org.onap.aaf.cadi.LocatorException;
37 import org.onap.aaf.cadi.Access.Level;
38 import org.onap.aaf.cadi.CadiException;
39 import org.onap.aaf.cadi.aaf.v2_0.AbsAAFLocator;
40 import org.onap.aaf.cadi.config.Config;
41 import org.onap.aaf.cadi.config.RegistrationPropHolder;
42 import org.onap.aaf.misc.env.util.Split;
43
44 import locate.v1_0.Endpoint;
45
46 public class DirectAAFLocator extends AbsAAFLocator<AuthzTrans> {
47     private LocateDAO ldao;
48     private int major=-1, minor=-1, patch=-1, pkg=-1;
49     private AuthzEnv env;
50     private final URI uri;
51
52     /**
53      * 
54      * @param env
55      * @param ldao
56      * @param key  must be one or more of service, version, other in that order
57      * @throws LocatorException 
58      */
59     public DirectAAFLocator(AuthzEnv env, LocateDAO ldao, String name, String version) throws LocatorException {
60         super(env.access(), name, 1000L /* Don't hit DB more than once a second */); 
61         this.env = env;
62         this.ldao = ldao;
63         if (version!=null) {
64             try { 
65                 String[] v = Split.split('.',version);
66                 if (v.length>0) {major = Integer.parseInt(v[0]);}
67                 if (v.length>1) {minor = Integer.parseInt(v[1]);}
68                 if (v.length>2) {patch = Integer.parseInt(v[2]);}
69                 if (v.length>3) {pkg   = Integer.parseInt(v[3]);}
70             } catch (NumberFormatException e) {
71                 throw new LocatorException("Invalid Version String: " + version);
72             }
73         }
74         
75         try {
76             String aaf_url;
77             if(name.indexOf('.')>=0) {
78                 aaf_url = "https://"+Config.AAF_LOCATE_URL_TAG+'/'+name+':'+version;
79             } else {
80                    aaf_url = "https://"+Config.AAF_LOCATE_URL_TAG+"/%CNS.%NS."+name+':'+version;
81             }
82             RegistrationPropHolder rph = new RegistrationPropHolder(access,0);
83             aaf_url = rph.replacements(getClass().getSimpleName(),aaf_url, null,null);
84             access.printf(Level.INIT,"Creating DirectAAFLocator to %s",aaf_url);
85             uri = new URI(aaf_url);
86         } catch (URISyntaxException | UnknownHostException | CadiException e) {
87             throw new LocatorException(e);
88         }
89         myhostname=null;
90         myport = 0; 
91     }
92     
93     
94     @Override
95     public boolean refresh() {
96         AuthzTrans trans = env.newTransNoAvg();
97         Result<List<Data>> rl = ldao.readByName(trans, name);
98         if (rl.isOK()) {
99             LinkedList<EP> epl = new LinkedList<>();
100             for (Data d : rl.value) {
101 //                if (myhostname!=null && d.port==myport && d.hostname.equals(myhostname)) {
102 //                    continue;
103 //                }
104                 if ((major<0 || major==d.major) &&
105                    (minor<0 || minor<=d.minor) &&
106                    (patch<0 || patch==d.patch) &&
107                    (pkg<0   || pkg  ==d.pkg)) {
108                     Endpoint endpoint = new Endpoint();
109                     endpoint.setName(d.name);
110                     endpoint.setHostname(d.hostname);
111                     endpoint.setPort(d.port);
112                     endpoint.setMajor(d.major);
113                     endpoint.setMinor(d.minor);
114                     endpoint.setPatch(d.patch);
115                     endpoint.setPkg(d.pkg);
116                     endpoint.setLatitude(d.latitude);
117                     endpoint.setLongitude(d.longitude);
118                     endpoint.setProtocol(d.protocol);
119                     for (String s : d.subprotocol(false)) {
120                         endpoint.getSubprotocol().add(s);
121                     }
122                     
123                     try {
124                         epl.add(new EP(endpoint,latitude,longitude));
125                     } catch (URISyntaxException e) {
126                         e.printStackTrace();
127                     }
128                 }
129             }
130             Collections.sort(epl);
131             replace(epl);
132             return true;
133         } else {
134             access.log(Level.ERROR, rl.errorString());
135         }
136         return false;
137     }
138
139     @Override
140     protected URI getURI() {
141         return uri;
142     }
143
144 }