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