AafServiceFactory implementation
[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
26 public class AafServiceImpl extends BaseLoggingClass implements AafService {
27
28     private String aafUrl;
29     private String identity;
30     private boolean useAAF;
31     private AafConnection aafConnection;
32
33     AafServiceImpl(boolean useAaf, String aafUrl, String identity, AafConnection aafConnection) {
34         this.useAAF = useAaf;
35         this.aafUrl = aafUrl;
36         this.identity = identity;
37         this.aafConnection = aafConnection;
38     }
39
40     @Override
41     public String getIdentity() {
42         return identity;
43     }
44
45     @Override
46     public int addPerm(DmaapPerm perm) {
47         logger.info("entry: addPerm() ");
48         return doPost(perm, "authz/perm", 201);
49     }
50
51     @Override
52     public int delPerm(DmaapPerm perm) {
53         return 200;
54     }
55
56     @Override
57     public int addGrant(DmaapGrant grant) {
58         logger.info("entry: addGrant() ");
59         return doPost(grant, "authz/role/perm", 201);
60     }
61
62     @Override
63     public int addUserRole(AafUserRole ur) {
64         logger.info("entry: addUserRole() ");
65         return doPost(ur, "authz/userRole", 201);
66     }
67
68     @Override
69     public int delGrant(DmaapGrant grant) {
70         logger.info("entry: delGrant() ");
71         return doDelete(grant, "authz/role/:" + grant.getRole() + "/perm", 200);
72     }
73
74     @Override
75     public int addRole(AafRole role) {
76         logger.info("entry: addRole() ");
77         return doPost(role, "authz/role", 201);
78     }
79
80     @Override
81     public int addNamespace(AafNamespace ns) {
82         logger.info("entry: addNamespace() ");
83         return doPost(ns, "authz/ns", 201);
84     }
85
86     @Override
87     public int delNamespace(AafNamespace ns) {
88         return 200;
89     }
90
91     private int doPost(AafObject obj, String uri, int expect) {
92         int rc;
93         logger.info("entry: doPost() ");
94         String pURL = aafUrl + uri;
95         logger.info("doPost: useAAF=" + useAAF);
96         if (useAAF) {
97             logger.info("doPost: " + obj.toJSON());
98             rc = aafConnection.postAaf(obj, pURL);
99         } else {
100             rc = expect;
101         }
102         switch (rc) {
103             case 401:
104             case 403:
105                 errorLogger.error(DmaapbcLogMessageEnum.AAF_CREDENTIAL_ERROR, identity);
106                 break;
107             case 409:
108                 logger.warn("Object for " + uri + " already exists. Possible conflict.");
109                 break;
110             default:
111                 if (rc == expect) {
112                     logger.info("expected response: " + rc);
113                 } else {
114                     logger.error("Unexpected response: " + rc);
115                 }
116                 break;
117         }
118
119         return rc;
120     }
121
122     private int doDelete(AafObject obj, String uri, int expect) {
123         int rc;
124         String pURL = aafUrl + uri;
125         if (useAAF) {
126             logger.info("doDelete: " + obj.toJSON());
127             rc = aafConnection.delAaf(obj, pURL);
128         } else {
129             rc = expect;
130         }
131         switch (rc) {
132             case 401:
133             case 403:
134                 errorLogger.error(DmaapbcLogMessageEnum.AAF_CREDENTIAL_ERROR, identity);
135                 break;
136             case 404:
137                 logger.warn("Object not found...ignore");
138                 break;
139             case 200:
140                 logger.info("expected response");
141                 break;
142             default:
143                 logger.error("Unexpected response: " + rc);
144                 break;
145         }
146
147         return rc;
148     }
149
150     String getAafUrl() {
151         return aafUrl;
152     }
153
154     boolean isUseAAF() {
155         return useAAF;
156     }
157
158 }