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