005397b2b984484b9403eff1a04fcc33166db6b2
[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.HashSet;
29 import java.util.List;
30 import java.util.Set;
31 import java.util.UUID;
32
33 import org.onap.aaf.auth.dao.cass.ApprovalDAO;
34 import org.onap.aaf.auth.dao.cass.FutureDAO;
35 import org.onap.aaf.auth.env.AuthzTrans;
36 import org.onap.aaf.auth.layer.Result;
37 import org.onap.aaf.cadi.CadiException;
38 import org.onap.aaf.misc.env.util.Chrono;
39
40 public class ApprovalSet {
41         private DataView dataview;
42         protected FutureDAO.Data fdd;
43         protected List<ApprovalDAO.Data> ladd;
44         
45         public ApprovalSet(final GregorianCalendar start, final String target, final DataView dv) throws CadiException {
46                 dataview = dv;
47                 fdd = new FutureDAO.Data();
48         try {
49                         fdd.id = newID(target);
50                 } catch (NoSuchAlgorithmException e) {
51                         throw new CadiException(e);
52                 } 
53                 fdd.target = target;
54                 fdd.start = start.getTime();
55                 ladd = new ArrayList<>();
56         }
57         
58         protected UUID newID(String target) throws NoSuchAlgorithmException {
59                 StringBuilder sb = new StringBuilder(new String(SecureRandom.getInstanceStrong().generateSeed(10)));
60         sb.append(target);
61         sb.append(System.currentTimeMillis());
62         return Chrono.dateToUUID(System.currentTimeMillis());
63         }
64
65         protected void setConstruct(final ByteBuffer bytes) {
66                 fdd.construct = bytes;
67         }
68
69         protected void setMemo(final String memo) {
70                 fdd.memo = memo;
71         }
72         
73         protected void setExpires(final GregorianCalendar expires) {
74                 fdd.expires = expires.getTime();
75         }
76         
77         public Result<Void> write(AuthzTrans trans) {
78                 StringBuilder errs = null;
79                 if(ladd == null || ladd.isEmpty()) {
80                         errs = new StringBuilder("No Approvers for ");
81                         errs .append(fdd.memo);
82                 } else {
83                         Result<FutureDAO.Data> rf = dataview.insert(trans, fdd);
84                         if(rf.notOK()) {
85                                 errs = new StringBuilder();
86                                 errs.append(rf.errorString());
87                         } else {
88                                 for(ApprovalDAO.Data add : ladd) {
89                                         Result<ApprovalDAO.Data> af = dataview.insert(trans, add);
90                                         if(af.notOK()) {
91                                                 if(errs==null) {
92                                                         errs = new StringBuilder();
93                                                 } else {
94                                                         errs.append('\n');
95                                                 }
96                                                 errs.append(af.errorString());
97                                         }
98                                 }
99                         }
100                 }
101                 return errs==null?Result.ok():Result.err(Result.ERR_Backend,errs.toString());
102         }
103
104         public boolean hasApprovals() {
105                 return !ladd.isEmpty();
106         }
107         
108         public Set<String> approvers() {
109                 Set<String> rv = new HashSet<>();
110                 for(ApprovalDAO.Data app : ladd) {
111                         rv.add(app.approver);
112                 }
113                 return rv;
114         }
115 }