Update AAF Version 1.0.0
[aaf/cadi.git] / client / src / main / java / org / onap / aaf / cadi / dme2 / DRcli.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aaf\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * ===========================================================================\r
7  * * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * * you may not use this file except in compliance with the License.\r
9  * * You may obtain a copy of the License at\r
10  * * \r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * * \r
13  *  * Unless required by applicable law or agreed to in writing, software\r
14  * * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * * See the License for the specific language governing permissions and\r
17  * * limitations under the License.\r
18  * * ============LICENSE_END====================================================\r
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 package org.onap.aaf.cadi.dme2;\r
24 \r
25 import java.net.MalformedURLException;\r
26 import java.net.URI;\r
27 import java.net.URISyntaxException;\r
28 import java.util.ArrayList;\r
29 import java.util.List;\r
30 \r
31 import org.onap.aaf.cadi.CadiException;\r
32 import org.onap.aaf.cadi.SecuritySetter;\r
33 import org.onap.aaf.cadi.client.EClient;\r
34 import org.onap.aaf.cadi.client.Rcli;\r
35 \r
36 import com.att.aft.dme2.api.DME2Client;\r
37 import com.att.aft.dme2.api.DME2Exception;\r
38 import com.att.aft.dme2.api.DME2Manager;\r
39 import com.att.aft.dme2.manager.registry.DME2Endpoint;\r
40 import com.att.aft.dme2.request.DmeUniformResource;\r
41 import org.onap.aaf.inno.env.APIException;\r
42 import org.onap.aaf.inno.env.Data.TYPE;\r
43 \r
44 /**\r
45  * DME2 Rosetta Client\r
46  * \r
47  * JAXB defined JSON or XML over DME2 middleware\r
48  * \r
49  *\r
50  * @param <T>\r
51  */\r
52 public class DRcli extends Rcli<DME2Client> {\r
53         // Can be more efficient if tied to manager, apparently.  Can pass in null.\r
54         DME2Manager manager=null;\r
55         private SecuritySetter<DME2Client> ss;\r
56         private boolean isProxy;\r
57         \r
58         public DRcli(URI uri, SecuritySetter<DME2Client> secSet) {\r
59                 this.uri = uri;\r
60                 type = TYPE.JSON;\r
61                 apiVersion = null;\r
62                 ss=secSet;\r
63         }\r
64         \r
65         @Override\r
66         protected DRcli clone(URI uri, SecuritySetter<DME2Client> ss) {\r
67                 return new DRcli(uri,ss);\r
68         }\r
69 \r
70 \r
71 \r
72         /**\r
73          * Note from Thaniga on 11/5.  DME2Client is not expected to be reused... need a fresh one\r
74          * on each transaction, which is expected to cover the Async aspects.\r
75          * \r
76          * @return\r
77          * @throws APIException \r
78          * @throws DME2Exception \r
79          */\r
80         protected EClient<DME2Client> client() throws CadiException {\r
81                 try {\r
82                         DEClient dc = new DEClient(manager,getSecuritySetter(),uri,readTimeout);\r
83                         dc.setProxy(isProxy);\r
84                         return dc;\r
85                 } catch (DME2Exception e) {\r
86                         throw new CadiException(e);\r
87                 }\r
88         }\r
89 \r
90         public DRcli setManager(DME2Manager dme2Manager) {\r
91                 manager = dme2Manager;\r
92                 return this;\r
93         }\r
94 \r
95         public List<DRcli> all() throws DME2Exception, APIException {\r
96                 ArrayList<DRcli> al = new ArrayList<DRcli>();\r
97                 \r
98                 if(manager == null) {\r
99                         manager = DME2Manager.getDefaultInstance();\r
100                 }\r
101                 try {\r
102                         DME2Endpoint[] endp = manager.getEndpoints(new DmeUniformResource(manager.getConfig(),uri));\r
103                         // Convert Searchable Endpoints to Direct Endpoints\r
104                         for(DME2Endpoint de : endp) {\r
105                                 al.add(new DRcli(\r
106                                                 new URI(uri.getScheme(),null,de.getHost(),de.getPort(),null,null,null),ss)\r
107 //                                              new URI(uri.getScheme(),null,de.getHost(),de.getPort(),uri.getPath(),null,null),ss)\r
108                                 .setManager(manager)\r
109                                 );\r
110                         }\r
111                 } catch (MalformedURLException e) {\r
112                         throw new APIException("Invalid URL",e);\r
113                 } catch (URISyntaxException e) {\r
114                         throw new APIException("Invalid URI",e);\r
115                 }\r
116                 return al;\r
117         }\r
118 \r
119         @Override\r
120         public void invalidate() throws CadiException {\r
121                 try {\r
122                         manager.refresh();\r
123                 } catch (Exception e) {\r
124                         throw new CadiException(e);\r
125                 }\r
126         }\r
127 \r
128         @Override\r
129         public void setSecuritySetter(SecuritySetter<DME2Client> ss) {\r
130                 this.ss = ss;\r
131         }\r
132 \r
133         @Override\r
134         public SecuritySetter<DME2Client> getSecuritySetter() {\r
135                 return ss;\r
136         }\r
137 \r
138         public void setProxy(boolean isProxy) {\r
139                 this.isProxy = isProxy;\r
140         }\r
141 \r
142 }\r