Update AAF Version 1.0.0
[aaf/cadi.git] / client / src / main / java / com / att / 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 com.att.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 com.att.aft.dme2.api.DME2Client;\r
32 import com.att.aft.dme2.api.DME2Exception;\r
33 import com.att.aft.dme2.api.DME2Manager;\r
34 import com.att.aft.dme2.manager.registry.DME2Endpoint;\r
35 import com.att.aft.dme2.request.DmeUniformResource;\r
36 import com.att.cadi.CadiException;\r
37 import com.att.cadi.SecuritySetter;\r
38 import com.att.cadi.client.EClient;\r
39 import com.att.cadi.client.Rcli;\r
40 import com.att.inno.env.APIException;\r
41 import com.att.inno.env.Data.TYPE;\r
42 \r
43 /**\r
44  * DME2 Rosetta Client\r
45  * \r
46  * JAXB defined JSON or XML over DME2 middleware\r
47  * \r
48  *\r
49  * @param <T>\r
50  */\r
51 public class DRcli extends Rcli<DME2Client> {\r
52         // Can be more efficient if tied to manager, apparently.  Can pass in null.\r
53         DME2Manager manager=null;\r
54         private SecuritySetter<DME2Client> ss;\r
55         private boolean isProxy;\r
56         \r
57         public DRcli(URI uri, SecuritySetter<DME2Client> secSet) {\r
58                 this.uri = uri;\r
59                 type = TYPE.JSON;\r
60                 apiVersion = null;\r
61                 ss=secSet;\r
62         }\r
63         \r
64         @Override\r
65         protected DRcli clone(URI uri, SecuritySetter<DME2Client> ss) {\r
66                 return new DRcli(uri,ss);\r
67         }\r
68 \r
69 \r
70 \r
71         /**\r
72          * Note from Thaniga on 11/5.  DME2Client is not expected to be reused... need a fresh one\r
73          * on each transaction, which is expected to cover the Async aspects.\r
74          * \r
75          * @return\r
76          * @throws APIException \r
77          * @throws DME2Exception \r
78          */\r
79         protected EClient<DME2Client> client() throws CadiException {\r
80                 try {\r
81                         DEClient dc = new DEClient(manager,getSecuritySetter(),uri,readTimeout);\r
82                         dc.setProxy(isProxy);\r
83                         return dc;\r
84                 } catch (DME2Exception e) {\r
85                         throw new CadiException(e);\r
86                 }\r
87         }\r
88 \r
89         public DRcli setManager(DME2Manager dme2Manager) {\r
90                 manager = dme2Manager;\r
91                 return this;\r
92         }\r
93 \r
94         public List<DRcli> all() throws DME2Exception, APIException {\r
95                 ArrayList<DRcli> al = new ArrayList<DRcli>();\r
96                 \r
97                 if(manager == null) {\r
98                         manager = DME2Manager.getDefaultInstance();\r
99                 }\r
100                 try {\r
101                         DME2Endpoint[] endp = manager.getEndpoints(new DmeUniformResource(manager.getConfig(),uri));\r
102                         // Convert Searchable Endpoints to Direct Endpoints\r
103                         for(DME2Endpoint de : endp) {\r
104                                 al.add(new DRcli(\r
105                                                 new URI(uri.getScheme(),null,de.getHost(),de.getPort(),null,null,null),ss)\r
106 //                                              new URI(uri.getScheme(),null,de.getHost(),de.getPort(),uri.getPath(),null,null),ss)\r
107                                 .setManager(manager)\r
108                                 );\r
109                         }\r
110                 } catch (MalformedURLException e) {\r
111                         throw new APIException("Invalid URL",e);\r
112                 } catch (URISyntaxException e) {\r
113                         throw new APIException("Invalid URI",e);\r
114                 }\r
115                 return al;\r
116         }\r
117 \r
118         @Override\r
119         public void invalidate() throws CadiException {\r
120                 try {\r
121                         manager.refresh();\r
122                 } catch (Exception e) {\r
123                         throw new CadiException(e);\r
124                 }\r
125         }\r
126 \r
127         @Override\r
128         public void setSecuritySetter(SecuritySetter<DME2Client> ss) {\r
129                 this.ss = ss;\r
130         }\r
131 \r
132         @Override\r
133         public SecuritySetter<DME2Client> getSecuritySetter() {\r
134                 return ss;\r
135         }\r
136 \r
137         public void setProxy(boolean isProxy) {\r
138                 this.isProxy = isProxy;\r
139         }\r
140 \r
141 }\r