3f8fb479334c12e805040ff7f0038fdfa3766c28
[aaf/cadi.git] / client / src / main / java / com / att / cadi / client / AAFClient.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.client;\r
24 \r
25 import java.net.HttpURLConnection;\r
26 import java.util.HashMap;\r
27 import java.util.Map;\r
28 \r
29 import com.att.aft.dme2.api.DME2Manager;\r
30 import com.att.cadi.Access;\r
31 import com.att.cadi.config.Config;\r
32 import com.att.cadi.config.SecurityInfoC;\r
33 import com.att.cadi.http.HBasicAuthSS;\r
34 import com.att.cadi.http.HMangr;\r
35 import com.att.cadi.locator.DME2Locator;\r
36 import com.att.inno.env.APIException;\r
37 import com.att.rosetta.env.RosettaDF;\r
38 import com.att.rosetta.env.RosettaEnv;\r
39 \r
40 public class AAFClient {\r
41         private RosettaEnv env;\r
42         private Map<Class<?>,RosettaDF<?>> map = new HashMap<Class<?>,RosettaDF<?>>();\r
43         HMangr hman;\r
44         HBasicAuthSS ss;\r
45 \r
46         public AAFClient(RosettaEnv env) throws Exception {\r
47                 this.env = env;\r
48                 Access access = new EnvAccess(env);\r
49                 String user = access.getProperty(Config.AAF_MECHID,null);\r
50                 String password = access.decrypt(access.getProperty(Config.AAF_MECHPASS,null), true);\r
51                 \r
52                 SecurityInfoC<HttpURLConnection> si = new SecurityInfoC<HttpURLConnection>(access);\r
53                 DME2Manager dm = new DME2Manager("APIclient DME2Manager", System.getProperties());\r
54                 DME2Locator loc = new DME2Locator(access, dm, access.getProperty(Config.AAF_URL,null));\r
55 \r
56                 int TIMEOUT = Integer.parseInt(access.getProperty(Config.AAF_CONN_TIMEOUT, "30000"));\r
57 \r
58                 hman = new HMangr(access, loc).readTimeout(TIMEOUT).apiVersion("2.0");\r
59                 ss = new HBasicAuthSS(user, password, si);\r
60         }\r
61 \r
62         public AAFClient(RosettaEnv env, DME2Manager dm) throws Exception {\r
63                 this.env = env;\r
64                 Access access = new EnvAccess(env);\r
65                 String user = access.getProperty(Config.AAF_MECHID,null);\r
66                 String password = access.decrypt(access.getProperty(Config.AAF_MECHPASS,null), true);\r
67                 \r
68                 SecurityInfoC<HttpURLConnection> si = new SecurityInfoC<HttpURLConnection>(access);\r
69                 DME2Locator loc = new DME2Locator(access, dm, access.getProperty(Config.AAF_URL,null));\r
70 \r
71                 int TIMEOUT = Integer.parseInt(access.getProperty(Config.AAF_CONN_TIMEOUT, "30000"));\r
72 \r
73                 hman = new HMangr(access, loc).readTimeout(TIMEOUT).apiVersion("2.0");\r
74                 ss = new HBasicAuthSS(user, password, si);\r
75         }\r
76         \r
77         @SuppressWarnings("unchecked")\r
78         private synchronized<T> RosettaDF<T> getDF(Class<T> cls) throws APIException {\r
79                 RosettaDF<?> rdf;\r
80                 synchronized (env) {\r
81                         rdf = map.get(cls);\r
82                         if(rdf==null) {\r
83                                 rdf = env.newDataFactory(cls);\r
84                                 map.put(cls, rdf);\r
85                         }\r
86                 }\r
87                 return (RosettaDF<T>)rdf;\r
88         }\r
89 \r
90         // Package on purpose\r
91         static class Call<T> {\r
92                 protected final static String VOID_CONTENT_TYPE="application/Void+json;version=2.0";\r
93                 \r
94                 protected RosettaDF<T> df;\r
95                 protected AAFClient client;\r
96 \r
97                 public Call(AAFClient ac, RosettaDF<T> df) {\r
98                         this.client = ac;\r
99                         this.df = df;\r
100                 }\r
101         }\r
102         \r
103 \r
104         ///////////  Calls /////////////////\r
105         /**\r
106          * Returns a Get Object... same as "get"\r
107          * \r
108          * @param cls\r
109          * @return\r
110          * @throws APIException\r
111          */\r
112         public<T> Get<T> read(Class<T> cls) throws APIException {\r
113                 return new Get<T>(this,getDF(cls));\r
114         }\r
115 \r
116         /**\r
117          * Returns a Get Object... same as "read"\r
118          * \r
119          * @param cls\r
120          * @return\r
121          * @throws APIException\r
122          */\r
123         public<T> Get<T> get(Class<T> cls) throws APIException {\r
124                 return new Get<T>(this,getDF(cls));\r
125         }\r
126 \r
127         /**\r
128          * Returns a Post Object... same as "create"\r
129          * \r
130          * @param cls\r
131          * @return\r
132          * @throws APIException\r
133          */\r
134         public<T> Post<T> post(Class<T> cls) throws APIException {\r
135                 return new Post<T>(this,getDF(cls));\r
136         }\r
137 \r
138         /**\r
139          * Returns a Post Object... same as "post"\r
140          * \r
141          * @param cls\r
142          * @return\r
143          * @throws APIException\r
144          */\r
145         public<T> Post<T> create(Class<T> cls) throws APIException {\r
146                 return new Post<T>(this,getDF(cls));\r
147         }\r
148 \r
149         /**\r
150          * Returns a Put Object... same as "update"\r
151          * \r
152          * @param cls\r
153          * @return\r
154          * @throws APIException\r
155          */\r
156         public<T> Put<T> put(Class<T> cls) throws APIException {\r
157                 return new Put<T>(this,getDF(cls));\r
158         }\r
159 \r
160         /**\r
161          * Returns a Put Object... same as "put"\r
162          * \r
163          * @param cls\r
164          * @return\r
165          * @throws APIException\r
166          */\r
167         public<T> Put<T> update(Class<T> cls) throws APIException {\r
168                 return new Put<T>(this,getDF(cls));\r
169         }\r
170 \r
171         /**\r
172          * Returns a Delete Object\r
173          * \r
174          * @param cls\r
175          * @return\r
176          * @throws APIException\r
177          */\r
178         public<T> Delete<T> delete(Class<T> cls) throws APIException {\r
179                 return new Delete<T>(this,getDF(cls));\r
180         }\r
181 \r
182         /**\r
183          * Returns a Delete Object\r
184          * \r
185          * @param cls\r
186          * @return\r
187          * @throws APIException\r
188          */\r
189         public Delete<Void> delete() throws APIException {\r
190                 return new Delete<Void>(this,null);\r
191         }\r
192 \r
193         public Put<Void> put() {\r
194                 return new Put<Void>(this,null);        \r
195         }\r
196         \r
197 \r
198 }\r