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