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