Update project structure for aaf/cadi
[aaf/cadi.git] / client / src / main / java / org / onap / aaf / cadi / client / AAFClient.java
diff --git a/client/src/main/java/org/onap/aaf/cadi/client/AAFClient.java b/client/src/main/java/org/onap/aaf/cadi/client/AAFClient.java
new file mode 100644 (file)
index 0000000..cb6299b
--- /dev/null
@@ -0,0 +1,199 @@
+/*******************************************************************************\r
+ * ============LICENSE_START====================================================\r
+ * * org.onap.aaf\r
+ * * ===========================================================================\r
+ * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
+ * * ===========================================================================\r
+ * * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * * you may not use this file except in compliance with the License.\r
+ * * You may obtain a copy of the License at\r
+ * * \r
+ *  *      http://www.apache.org/licenses/LICENSE-2.0\r
+ * * \r
+ *  * Unless required by applicable law or agreed to in writing, software\r
+ * * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * * See the License for the specific language governing permissions and\r
+ * * limitations under the License.\r
+ * * ============LICENSE_END====================================================\r
+ * *\r
+ * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
+ * *\r
+ ******************************************************************************/\r
+package org.onap.aaf.cadi.client;\r
+\r
+import java.net.HttpURLConnection;\r
+import java.util.HashMap;\r
+import java.util.Map;\r
+\r
+import org.onap.aaf.cadi.Access;\r
+import org.onap.aaf.cadi.config.Config;\r
+import org.onap.aaf.cadi.config.SecurityInfoC;\r
+import org.onap.aaf.cadi.http.HBasicAuthSS;\r
+import org.onap.aaf.cadi.http.HMangr;\r
+import org.onap.aaf.cadi.locator.DME2Locator;\r
+\r
+import com.att.aft.dme2.api.DME2Manager;\r
+import org.onap.aaf.inno.env.APIException;\r
+import org.onap.aaf.rosetta.env.RosettaDF;\r
+import org.onap.aaf.rosetta.env.RosettaEnv;\r
+\r
+public class AAFClient {\r
+       private RosettaEnv env;\r
+       private Map<Class<?>,RosettaDF<?>> map = new HashMap<Class<?>,RosettaDF<?>>();\r
+       HMangr hman;\r
+       HBasicAuthSS ss;\r
+\r
+       public AAFClient(RosettaEnv env) throws Exception {\r
+               this.env = env;\r
+               Access access = new EnvAccess(env);\r
+               String user = access.getProperty(Config.AAF_MECHID,null);\r
+               String password = access.decrypt(access.getProperty(Config.AAF_MECHPASS,null), true);\r
+               \r
+               SecurityInfoC<HttpURLConnection> si = new SecurityInfoC<HttpURLConnection>(access);\r
+               DME2Manager dm = new DME2Manager("APIclient DME2Manager", System.getProperties());\r
+               DME2Locator loc = new DME2Locator(access, dm, access.getProperty(Config.AAF_URL,null));\r
+\r
+               int TIMEOUT = Integer.parseInt(access.getProperty(Config.AAF_CONN_TIMEOUT, "30000"));\r
+\r
+               hman = new HMangr(access, loc).readTimeout(TIMEOUT).apiVersion("2.0");\r
+               ss = new HBasicAuthSS(user, password, si);\r
+       }\r
+\r
+       public AAFClient(RosettaEnv env, DME2Manager dm) throws Exception {\r
+               this.env = env;\r
+               Access access = new EnvAccess(env);\r
+               String user = access.getProperty(Config.AAF_MECHID,null);\r
+               String password = access.decrypt(access.getProperty(Config.AAF_MECHPASS,null), true);\r
+               \r
+               SecurityInfoC<HttpURLConnection> si = new SecurityInfoC<HttpURLConnection>(access);\r
+               DME2Locator loc = new DME2Locator(access, dm, access.getProperty(Config.AAF_URL,null));\r
+\r
+               int TIMEOUT = Integer.parseInt(access.getProperty(Config.AAF_CONN_TIMEOUT, "30000"));\r
+\r
+               hman = new HMangr(access, loc).readTimeout(TIMEOUT).apiVersion("2.0");\r
+               ss = new HBasicAuthSS(user, password, si);\r
+       }\r
+       \r
+       @SuppressWarnings("unchecked")\r
+       private synchronized<T> RosettaDF<T> getDF(Class<T> cls) throws APIException {\r
+               RosettaDF<?> rdf;\r
+               synchronized (env) {\r
+                       rdf = map.get(cls);\r
+                       if(rdf==null) {\r
+                               rdf = env.newDataFactory(cls);\r
+                               map.put(cls, rdf);\r
+                       }\r
+               }\r
+               return (RosettaDF<T>)rdf;\r
+       }\r
+\r
+       // Package on purpose\r
+       static class Call<T> {\r
+               protected final static String VOID_CONTENT_TYPE="application/Void+json;version=2.0";\r
+               \r
+               protected RosettaDF<T> df;\r
+               protected AAFClient client;\r
+\r
+               public Call(AAFClient ac, RosettaDF<T> df) {\r
+                       this.client = ac;\r
+                       this.df = df;\r
+               }\r
+       }\r
+       \r
+\r
+       ///////////  Calls /////////////////\r
+       /**\r
+        * Returns a Get Object... same as "get"\r
+        * \r
+        * @param cls\r
+        * @return\r
+        * @throws APIException\r
+        */\r
+       public<T> Get<T> read(Class<T> cls) throws APIException {\r
+               return new Get<T>(this,getDF(cls));\r
+       }\r
+\r
+       /**\r
+        * Returns a Get Object... same as "read"\r
+        * \r
+        * @param cls\r
+        * @return\r
+        * @throws APIException\r
+        */\r
+       public<T> Get<T> get(Class<T> cls) throws APIException {\r
+               return new Get<T>(this,getDF(cls));\r
+       }\r
+\r
+       /**\r
+        * Returns a Post Object... same as "create"\r
+        * \r
+        * @param cls\r
+        * @return\r
+        * @throws APIException\r
+        */\r
+       public<T> Post<T> post(Class<T> cls) throws APIException {\r
+               return new Post<T>(this,getDF(cls));\r
+       }\r
+\r
+       /**\r
+        * Returns a Post Object... same as "post"\r
+        * \r
+        * @param cls\r
+        * @return\r
+        * @throws APIException\r
+        */\r
+       public<T> Post<T> create(Class<T> cls) throws APIException {\r
+               return new Post<T>(this,getDF(cls));\r
+       }\r
+\r
+       /**\r
+        * Returns a Put Object... same as "update"\r
+        * \r
+        * @param cls\r
+        * @return\r
+        * @throws APIException\r
+        */\r
+       public<T> Put<T> put(Class<T> cls) throws APIException {\r
+               return new Put<T>(this,getDF(cls));\r
+       }\r
+\r
+       /**\r
+        * Returns a Put Object... same as "put"\r
+        * \r
+        * @param cls\r
+        * @return\r
+        * @throws APIException\r
+        */\r
+       public<T> Put<T> update(Class<T> cls) throws APIException {\r
+               return new Put<T>(this,getDF(cls));\r
+       }\r
+\r
+       /**\r
+        * Returns a Delete Object\r
+        * \r
+        * @param cls\r
+        * @return\r
+        * @throws APIException\r
+        */\r
+       public<T> Delete<T> delete(Class<T> cls) throws APIException {\r
+               return new Delete<T>(this,getDF(cls));\r
+       }\r
+\r
+       /**\r
+        * Returns a Delete Object\r
+        * \r
+        * @param cls\r
+        * @return\r
+        * @throws APIException\r
+        */\r
+       public Delete<Void> delete() throws APIException {\r
+               return new Delete<Void>(this,null);\r
+       }\r
+\r
+       public Put<Void> put() {\r
+               return new Put<Void>(this,null);        \r
+       }\r
+       \r
+\r
+}\r