Refactor code to support no AAF requests
[dmaap/dbcapi.git] / src / main / java / org / onap / dmaap / dbcapi / aaf / AafService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dmaap
4  * ================================================================================
5  * Copyright (C) 2017 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.dmaap.dbcapi.aaf;
22
23 import java.io.IOException;
24
25 import org.apache.log4j.Logger;
26 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
27 import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum;
28 import org.onap.dmaap.dbcapi.util.DmaapConfig;
29
30 public class AafService extends BaseLoggingClass {
31         public enum ServiceType {
32                 AAF_Admin,
33                 AAF_TopicMgr
34         }
35         
36         private AafConnection aaf;
37         private ServiceType ctype;
38         private String aafURL ;
39         private boolean useAAF = false;
40         
41         public AafService() {
42                 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
43                 useAAF= "true".equalsIgnoreCase(p.getProperty("UseAAF", "false"));
44                 
45         }
46         
47         private String getCred( boolean wPwd ) {
48                 String mechIdProperty = null;
49                 String pwdProperty = null;
50                 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
51                 AafDecrypt decryptor = new AafDecrypt();
52
53                 if ( ctype == ServiceType.AAF_Admin ) {
54                          mechIdProperty = "aaf.AdminUser";
55                          pwdProperty = "aaf.AdminPassword";
56                 } else if ( ctype == ServiceType.AAF_TopicMgr ){        
57                          mechIdProperty = "aaf.TopicMgrUser";
58                          pwdProperty = "aaf.TopicMgrPassword";
59                 } else {
60                         logger.error( "Unexpected case for AAF credential type: " + ctype );
61                         return null;
62                 }
63                 String user = p.getProperty( mechIdProperty, "noMechId@domain.netset.com" );
64
65                 String pwd = "";
66                 String encPwd = p.getProperty( pwdProperty, "notSet" );
67
68                 
69                 pwd = decryptor.decrypt(encPwd);
70                 
71                 if ( wPwd ) {
72                         return user + ":" + pwd;
73                 } else {
74                         return user;
75                 }
76                 
77                 
78         }
79         
80         public AafService(ServiceType t ) {
81                 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
82                 aafURL = p.getProperty( "aaf.URL", "https://authentication.domain.netset.com:8095/proxy/");
83                 initAafService( t );
84         }
85         public AafService( ServiceType t, String url ) {
86                 aafURL = url;
87                 initAafService( t );
88         }
89                 
90         private void initAafService( ServiceType t ) {
91                 ctype = t;
92                 aaf = new AafConnection( getCred( true ) );
93         }
94         
95         public int addPerm(DmaapPerm perm) {
96
97                 int rc = -1;
98                 logger.info( "entry: addPerm() "  );
99                 String pURL = aafURL + "authz/perm";
100                 if ( useAAF ) {
101                         rc = aaf.postAaf( perm, pURL );
102                 } else {
103                         rc = 201;
104                 }
105         switch( rc ) {
106         case 401:
107         case 403:
108                 errorLogger.error(DmaapbcLogMessageEnum.AAF_CREDENTIAL_ERROR,  getCred( false ) );
109                 System.exit(1);
110         case 409:
111                 logger.warn( "Perm already exists. Possible conflict.");
112                 break;
113                 
114         case 201:
115                 logger.info( "expected response" );
116                 break;
117         default :
118                 logger.error( "Unexpected response: " + rc );
119                 break;
120         }
121                 
122                 return rc;
123         }
124         public int addGrant(DmaapGrant grant ) {
125
126                 int rc = -1;
127                 logger.info( "entry: addGrant() "  );
128
129                 String pURL = aafURL + "authz/role/perm";
130                 if ( useAAF ) {
131                         rc = aaf.postAaf( grant, pURL );
132                 } else {
133                         rc = 201;
134                 }
135                 
136         switch( rc ) {
137         case 401:
138         case 403:
139                 errorLogger.error(DmaapbcLogMessageEnum.AAF_CREDENTIAL_ERROR,  getCred( false ) );
140                 System.exit(1);
141                 break;
142
143         case 409:
144                 logger.warn( "Perm already exists. Possible conflict.");
145                 break;
146                 
147         case 201:
148                 logger.info( "expected response" );
149                 break;
150         default :
151                 logger.error( "Unexpected response: " + rc );
152                 break;
153         }
154                 
155                 return rc;
156         }
157
158         public int delGrant( DmaapGrant grant ) {
159                 int rc = -1;
160                 logger.info( "entry: delGrant() "  );
161
162                 String pURL = aafURL + "authz/role/:" + grant.getRole() + "/perm";
163                 
164                 if ( useAAF ) {
165                         rc = aaf.delAaf( grant, pURL );
166                 } else {
167                         rc = 200;
168                 }
169         switch( rc ) {
170         case 401:
171         case 403:
172                 errorLogger.error(DmaapbcLogMessageEnum.AAF_CREDENTIAL_ERROR,  getCred( false ) );
173                 System.exit(1);
174                 break;
175  
176         case 404:
177                 logger.warn( "Perm not found...ignore");
178                 break;
179                 
180         case 200:
181                 logger.info( "expected response" );
182                 break;
183         default :
184                 logger.error( "Unexpected response: " + rc );
185                 break;
186         }
187                 
188                 return rc;
189         }
190 }