[AAF-21] Initial code import
[aaf/cadi.git] / aaf / src / src / main / java / com / att / cadi / aaf / v2_0 / AAFConDME2.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aai\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * Copyright © 2017 Amdocs\r
7  * * ===========================================================================\r
8  * * Licensed under the Apache License, Version 2.0 (the "License");\r
9  * * you may not use this file except in compliance with the License.\r
10  * * You may obtain a copy of the License at\r
11  * * \r
12  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
13  * * \r
14  *  * Unless required by applicable law or agreed to in writing, software\r
15  * * distributed under the License is distributed on an "AS IS" BASIS,\r
16  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
17  * * See the License for the specific language governing permissions and\r
18  * * limitations under the License.\r
19  * * ============LICENSE_END====================================================\r
20  * *\r
21  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
22  * *\r
23  ******************************************************************************/\r
24 package com.att.cadi.aaf.v2_0;\r
25 \r
26 import java.io.IOException;\r
27 import java.net.ConnectException;\r
28 import java.net.URI;\r
29 import java.security.GeneralSecurityException;\r
30 import java.security.Principal;\r
31 import java.util.Properties;\r
32 \r
33 import com.att.aft.dme2.api.DME2Client;\r
34 import com.att.aft.dme2.api.DME2Exception;\r
35 import com.att.aft.dme2.api.DME2Manager;\r
36 import com.att.cadi.Access;\r
37 import com.att.cadi.CadiException;\r
38 import com.att.cadi.LocatorException;\r
39 import com.att.cadi.SecuritySetter;\r
40 import com.att.cadi.client.Rcli;\r
41 import com.att.cadi.client.Retryable;\r
42 import com.att.cadi.config.Config;\r
43 import com.att.cadi.config.SecurityInfo;\r
44 import com.att.cadi.dme2.DME2BasicAuth;\r
45 import com.att.cadi.dme2.DME2TransferSS;\r
46 import com.att.cadi.dme2.DME2x509SS;\r
47 import com.att.cadi.dme2.DRcli;\r
48 import com.att.cadi.principal.BasicPrincipal;\r
49 import com.att.inno.env.APIException;\r
50 \r
51 public class AAFConDME2 extends AAFCon<DME2Client>{\r
52         private DME2Manager manager;\r
53 \r
54         public AAFConDME2(Access access) throws CadiException, GeneralSecurityException, IOException{\r
55                 super(access,Config.AAF_URL,new SecurityInfo<DME2Client> (access));\r
56                 manager = newManager(access);\r
57         }\r
58         \r
59         public AAFConDME2(Access access, String url) throws CadiException, GeneralSecurityException, IOException{\r
60                 super(access,url,new SecurityInfo<DME2Client> (access));\r
61                 manager = newManager(access);\r
62         }\r
63 \r
64         public AAFConDME2(Access access, SecurityInfo<DME2Client> si) throws CadiException {\r
65                 super(access,Config.AAF_URL,si);\r
66                 manager = newManager(access);\r
67         }\r
68 \r
69         public AAFConDME2(Access access, String url, SecurityInfo<DME2Client> si) throws CadiException {\r
70                 super(access,url,si);\r
71                 manager = newManager(access);\r
72         }\r
73 \r
74         private DME2Manager newManager(Access access) throws CadiException {\r
75                 Properties props = new Properties();\r
76                 Config.cadiToDME2(access, props);\r
77                 try {\r
78                         return new DME2Manager("AAFCon",props);\r
79                 } catch (DME2Exception e) {\r
80                         throw new CadiException(e);\r
81                 }\r
82         }\r
83 \r
84 \r
85         /* (non-Javadoc)\r
86          * @see com.att.cadi.aaf.v2_0.AAFCon#basicAuth(java.lang.String, java.lang.String)\r
87          */\r
88         @Override\r
89         public SecuritySetter<DME2Client> basicAuth(String user, String password) throws CadiException {\r
90                 if(password.startsWith("enc:???")) {\r
91                         try {\r
92                                 password = access.decrypt(password, true);\r
93                         } catch (IOException e) {\r
94                                 throw new CadiException("Error Decrypting Password",e);\r
95                         }\r
96                 }\r
97 \r
98                 try {\r
99                         return set(new DME2BasicAuth(user,password,si));\r
100                 } catch (IOException e) {\r
101                         throw new CadiException("Error setting up DME2BasicAuth",e);\r
102                 }\r
103         }\r
104 \r
105         /* (non-Javadoc)\r
106          * @see com.att.cadi.aaf.v2_0.AAFCon#rclient(java.net.URI, com.att.cadi.SecuritySetter)\r
107          */\r
108         @Override\r
109         protected Rcli<DME2Client> rclient(URI uri, SecuritySetter<DME2Client> ss) {\r
110                 DRcli dc = new DRcli(uri, ss);\r
111                 dc.setManager(manager);\r
112                 return dc;\r
113         }\r
114 \r
115         @Override\r
116         public SecuritySetter<DME2Client> transferSS(Principal principal) throws CadiException {\r
117                 try {\r
118                         return principal==null?ss:new DME2TransferSS(principal, app);\r
119                 } catch (IOException e) {\r
120                         throw new CadiException("Error creating DME2TransferSS",e);\r
121                 }\r
122         }\r
123 \r
124         @Override\r
125         public SecuritySetter<DME2Client> basicAuthSS(BasicPrincipal principal) throws CadiException {\r
126                 try {\r
127                         return new DME2BasicAuth(principal,si);\r
128                 } catch (IOException e) {\r
129                         throw new CadiException("Error creating DME2BasicAuth",e);\r
130                 }\r
131 \r
132         }\r
133 \r
134         @Override\r
135         public SecuritySetter<DME2Client> x509Alias(String alias) throws CadiException {\r
136                 try {\r
137                         return new DME2x509SS(alias,si);\r
138                 } catch (Exception e) {\r
139                         throw new CadiException("Error creating DME2x509SS",e);\r
140                 }\r
141         }\r
142 \r
143         @Override\r
144         public <RET> RET best(Retryable<RET> retryable) throws LocatorException, CadiException, APIException {\r
145                 // NOTE: DME2 had Retry Logic embedded lower.  \r
146                 try {\r
147                         return (retryable.code(rclient(initURI,ss)));\r
148                 } catch (ConnectException e) {\r
149                         // DME2 should catch\r
150                         try {\r
151                                 manager.refresh();\r
152                         } catch (Exception e1) {\r
153                                 throw new CadiException(e1);\r
154                         }\r
155                         throw new CadiException(e);\r
156                 }\r
157         }\r
158 }\r