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