[AAF-21] Initial code import
[aaf/authz.git] / authz-certman / src / main / java / com / att / authz / cm / mapper / Mapper1_0.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.authz.cm.mapper;\r
25 \r
26 import java.io.IOException;\r
27 import java.util.ArrayList;\r
28 import java.util.List;\r
29 \r
30 import aaf.v2_0.Error;\r
31 import certman.v1_0.Artifacts;\r
32 import certman.v1_0.Artifacts.Artifact;\r
33 import certman.v1_0.BaseRequest;\r
34 import certman.v1_0.CertInfo;\r
35 import certman.v1_0.CertificateDrop;\r
36 import certman.v1_0.CertificateRenew;\r
37 import certman.v1_0.CertificateRequest;\r
38 \r
39 import com.att.authz.cm.data.CertDrop;\r
40 import com.att.authz.cm.data.CertRenew;\r
41 import com.att.authz.cm.data.CertReq;\r
42 import com.att.authz.cm.data.CertResp;\r
43 import com.att.authz.cm.validation.Validator;\r
44 import com.att.authz.env.AuthzTrans;\r
45 import com.att.authz.layer.Result;\r
46 import com.att.cadi.aaf.v2_0.AAFCon;\r
47 import com.att.cadi.util.Vars;\r
48 import com.att.dao.aaf.cass.ArtiDAO;\r
49 import com.att.dao.aaf.cass.ArtiDAO.Data;\r
50 \r
51 \r
52 public class Mapper1_0 implements Mapper<BaseRequest,CertInfo,Artifacts,Error> {\r
53         \r
54         @Override\r
55         public Class<?> getClass(API api) {\r
56                 switch(api) {\r
57                         case CERT_REQ: return CertificateRequest.class;\r
58                         case CERT_RENEW: return CertificateRenew.class;\r
59                         case CERT_DROP: return CertificateDrop.class;\r
60                         case CERT: return CertInfo.class;\r
61                         case ARTIFACTS: return Artifacts.class;\r
62                         case ERROR: return Error.class;\r
63                         case VOID: return Void.class;\r
64                 }\r
65                 return null;\r
66         }\r
67 \r
68         @SuppressWarnings("unchecked")\r
69         @Override\r
70         public <A> A newInstance(API api) {\r
71                 switch(api) {\r
72                         case CERT_REQ: return (A) new CertificateRequest();\r
73                         case CERT_RENEW: return (A) new CertificateRenew();\r
74                         case CERT_DROP: return (A) new CertificateDrop();\r
75                         case CERT: return (A) new CertInfo();\r
76                         case ARTIFACTS: return (A) new Artifacts();\r
77                         case ERROR: return (A)new Error();\r
78                         case VOID: return null;\r
79                 }\r
80                 return null;\r
81         }\r
82 \r
83         //////////////  Mapping Functions /////////////\r
84         @Override\r
85         public Error errorFromMessage(StringBuilder holder, String msgID, String text, String... var) {\r
86                 Error err = new Error();\r
87                 err.setMessageId(msgID);\r
88                 // AT&T Restful Error Format requires numbers "%" placements\r
89                 err.setText(Vars.convert(holder, text, var));\r
90                 for(String s : var) {\r
91                         err.getVariables().add(s);\r
92                 }\r
93                 return err;\r
94         }\r
95 \r
96         /* (non-Javadoc)\r
97          * @see com.att.authz.certman.mapper.Mapper#toCert(com.att.authz.env.AuthzTrans, com.att.authz.layer.Result)\r
98          */\r
99         @Override\r
100         public Result<CertInfo> toCert(AuthzTrans trans, Result<CertResp> in, String[] trustChain) throws IOException {\r
101                 if(in.isOK()) {\r
102                         CertResp cin = in.value;\r
103                         CertInfo cout = newInstance(API.CERT);\r
104                         cout.setPrivatekey(cin.privateString());\r
105                         String value;\r
106                         if((value=cin.challenge())!=null) {\r
107                                 cout.setChallenge(value);\r
108                         }\r
109                         cout.getCerts().add(cin.asCertString());\r
110                         if(trustChain!=null) {\r
111                                 for(String c : trustChain) {\r
112                                         cout.getCerts().add(c);\r
113                                 }\r
114                         }\r
115                         if(cin.notes()!=null) {\r
116                                 boolean first = true;\r
117                                 StringBuilder sb = new StringBuilder();\r
118                                 for(String n : cin.notes()) {\r
119                                         if(first) {\r
120                                                 first = false;\r
121                                         } else {\r
122                                                 sb.append('\n');\r
123                                         }\r
124                                         sb.append(n);\r
125                                 }\r
126                                 cout.setNotes(sb.toString());\r
127                         }\r
128                         return Result.ok(cout);\r
129                 } else {\r
130                         return Result.err(in);\r
131                 }\r
132         }\r
133 \r
134         /* (non-Javadoc)\r
135          * @see com.att.authz.certman.mapper.Mapper#toReq(com.att.authz.env.AuthzTrans, java.lang.Object)\r
136          */\r
137         @Override\r
138         public Result<CertReq> toReq(AuthzTrans trans, BaseRequest req) {\r
139                 CertificateRequest in;\r
140                 try {\r
141                         in = (CertificateRequest)req;\r
142                 } catch(ClassCastException e) {\r
143                         return Result.err(Result.ERR_BadData,"Request is not a CertificateRequest");\r
144                 }\r
145 \r
146                 CertReq out = new CertReq();\r
147                 Validator v = new Validator();\r
148                 if(v.isNull("CertRequest", req)\r
149                         .nullOrBlank("MechID", out.mechid=in.getMechid())\r
150                         .nullBlankMin("FQDNs", out.fqdns=in.getFqdns(),1)\r
151                         .err()) {\r
152                         return Result.err(Result.ERR_BadData, v.errs());\r
153                 }\r
154                 out.emails = in.getEmail();\r
155                 out.sponsor=in.getSponsor();\r
156                 out.start = in.getStart();\r
157                 out.end = in.getEnd();\r
158                 return Result.ok(out);\r
159         }\r
160 \r
161         /* (non-Javadoc)\r
162          * @see com.att.authz.certman.mapper.Mapper#toRenew(com.att.authz.env.AuthzTrans, java.lang.Object)\r
163          */\r
164         @Override\r
165         public Result<CertRenew> toRenew(AuthzTrans trans, BaseRequest req) {\r
166                 return Result.err(Result.ERR_NotImplemented,"Not Implemented... yet");\r
167         }\r
168 \r
169         /* (non-Javadoc)\r
170          * @see com.att.authz.certman.mapper.Mapper#toDrop(com.att.authz.env.AuthzTrans, java.lang.Object)\r
171          */\r
172         @Override\r
173         public Result<CertDrop> toDrop(AuthzTrans trans, BaseRequest req) {\r
174                 return Result.err(Result.ERR_NotImplemented,"Not Implemented... yet");\r
175         }\r
176 \r
177         /* (non-Javadoc)\r
178          * @see com.att.authz.cm.mapper.Mapper#toArtifact(com.att.authz.env.AuthzTrans, java.lang.Object)\r
179          */\r
180         @Override\r
181         public List<ArtiDAO.Data> toArtifact(AuthzTrans trans, Artifacts artifacts) {\r
182                 List<ArtiDAO.Data> ladd = new ArrayList<ArtiDAO.Data>();\r
183                 for(Artifact arti : artifacts.getArtifact()) {\r
184                         ArtiDAO.Data data = new ArtiDAO.Data();\r
185                         data.mechid = arti.getMechid();\r
186                         data.machine = arti.getMachine();\r
187                         data.type(true).addAll(arti.getType());\r
188                         data.ca = arti.getCa();\r
189                         data.dir = arti.getDir();\r
190                         data.os_user = arti.getOsUser();\r
191                         // Optional (on way in)\r
192                         data.appName = arti.getAppName();\r
193                         data.renewDays = arti.getRenewDays();\r
194                         data.notify = arti.getNotification();\r
195                         \r
196                         // Ignored on way in for create/update\r
197                         data.sponsor = arti.getSponsor();\r
198                         data.expires = null;\r
199                         \r
200                         // Derive Optional Data from Machine (Domain) if exists\r
201                         if(data.machine!=null) {\r
202                                 if(data.ca==null) {\r
203                                         if(data.machine.endsWith(".att.com")) {\r
204                                                 data.ca = "aaf"; // default\r
205                                         }\r
206                                 }\r
207                                 if(data.appName==null ) {\r
208                                         data.appName=AAFCon.reverseDomain(data.machine);\r
209                                 }\r
210                         }\r
211 \r
212                         ladd.add(data);\r
213                 }\r
214                 return ladd;\r
215         }\r
216 \r
217         /* (non-Javadoc)\r
218          * @see com.att.authz.cm.mapper.Mapper#fromArtifacts(com.att.authz.layer.Result)\r
219          */\r
220         @Override\r
221         public Result<Artifacts> fromArtifacts(Result<List<Data>> lArtiDAO) {\r
222                 if(lArtiDAO.isOK()) {\r
223                         Artifacts artis = new Artifacts();\r
224                         for(ArtiDAO.Data arti : lArtiDAO.value) {\r
225                                 Artifact a = new Artifact();\r
226                                 a.setMechid(arti.mechid);\r
227                                 a.setMachine(arti.machine);\r
228                                 a.setSponsor(arti.sponsor);\r
229                                 a.setAppName(arti.appName);\r
230                                 a.setCa(arti.ca);\r
231                                 a.setDir(arti.dir);\r
232                                 a.getType().addAll(arti.type(false));\r
233                                 a.setOsUser(arti.os_user);\r
234                                 a.setRenewDays(arti.renewDays);\r
235                                 a.setNotification(arti.notify);\r
236                                 artis.getArtifact().add(a);\r
237                         }\r
238                         return Result.ok(artis);\r
239                 } else {\r
240                         return Result.err(lArtiDAO);\r
241                 }\r
242         }\r
243         \r
244         \r
245 \r
246 }\r