Batch work and client
[aaf/authz.git] / auth / auth-batch / src / main / java / org / onap / aaf / auth / batch / approvalsets / ApprovalSet.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  * ===========================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END====================================================
19  *
20  */
21 package org.onap.aaf.auth.batch.approvalsets;
22
23 import java.nio.ByteBuffer;
24 import java.security.NoSuchAlgorithmException;
25 import java.security.SecureRandom;
26 import java.util.ArrayList;
27 import java.util.GregorianCalendar;
28 import java.util.List;
29 import java.util.UUID;
30
31 import org.onap.aaf.auth.dao.cass.ApprovalDAO;
32 import org.onap.aaf.auth.dao.cass.FutureDAO;
33 import org.onap.aaf.auth.env.AuthzTrans;
34 import org.onap.aaf.auth.layer.Result;
35 import org.onap.aaf.cadi.CadiException;
36 import org.onap.aaf.misc.env.util.Chrono;
37
38 public class ApprovalSet {
39         private DataView dataview;
40         protected FutureDAO.Data fdd;
41         protected List<ApprovalDAO.Data> ladd;
42         
43         public ApprovalSet(final GregorianCalendar start, final String target, final DataView dv) throws CadiException {
44                 dataview = dv;
45                 fdd = new FutureDAO.Data();
46         try {
47                         fdd.id = newID(target);
48                 } catch (NoSuchAlgorithmException e) {
49                         throw new CadiException(e);
50                 } 
51                 fdd.target = target;
52                 fdd.start = start.getTime();
53                 ladd = new ArrayList<>();
54         }
55         
56         protected UUID newID(String target) throws NoSuchAlgorithmException {
57                 StringBuilder sb = new StringBuilder(new String(SecureRandom.getInstanceStrong().generateSeed(10)));
58         sb.append(target);
59         sb.append(System.currentTimeMillis());
60         return Chrono.dateToUUID(System.currentTimeMillis());
61         }
62
63         protected void setConstruct(final ByteBuffer bytes) {
64                 fdd.construct = bytes;
65         }
66
67         protected void setMemo(final String memo) {
68                 fdd.memo = memo;
69         }
70         
71         protected void setExpires(final GregorianCalendar expires) {
72                 fdd.expires = expires.getTime();
73         }
74         
75         public Result<Void> write(AuthzTrans trans) {
76                 StringBuilder errs = null;
77                 Result<FutureDAO.Data> rf = dataview.insert(trans, fdd);
78                 if(rf.notOK()) {
79                         errs = new StringBuilder();
80                         errs.append(rf.errorString());
81                 } else {
82                         for(ApprovalDAO.Data add : ladd) {
83                                 Result<ApprovalDAO.Data> af = dataview.insert(trans, add);
84                                 if(af.notOK()) {
85                                         if(errs==null) {
86                                                 errs = new StringBuilder();
87                                         } else {
88                                                 errs.append('\n');
89                                         }
90                                         errs.append(af.errorString());
91                                 }
92                         }
93                 }
94                 return errs==null?Result.ok():Result.err(Result.ERR_Backend,errs.toString());
95         }
96 }