2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
7 * Modifications Copyright (C) 2019 IBM.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.dmaap.dbcapi.service;
25 import org.onap.dmaap.dbcapi.aaf.AafService;
26 import org.onap.dmaap.dbcapi.aaf.AafService.ServiceType;
27 import org.onap.dmaap.dbcapi.aaf.AafServiceImpl;
28 import org.onap.dmaap.dbcapi.aaf.AafUserRole;
29 import org.onap.dmaap.dbcapi.aaf.DmaapGrant;
30 import org.onap.dmaap.dbcapi.aaf.DmaapPerm;
31 import org.onap.dmaap.dbcapi.client.MrProvConnection;
32 import org.onap.dmaap.dbcapi.database.DatabaseClass;
33 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
34 import org.onap.dmaap.dbcapi.model.ApiError;
35 import org.onap.dmaap.dbcapi.model.DcaeLocation;
36 import org.onap.dmaap.dbcapi.model.DmaapObject.DmaapObject_Status;
37 import org.onap.dmaap.dbcapi.model.MR_Client;
38 import org.onap.dmaap.dbcapi.model.MR_Cluster;
39 import org.onap.dmaap.dbcapi.model.Topic;
40 import org.onap.dmaap.dbcapi.util.DmaapConfig;
42 import javax.ws.rs.core.Response.Status;
43 import java.util.ArrayList;
44 import java.util.List;
47 public class MR_ClientService extends BaseLoggingClass {
49 private static final String MR_CLIENT_ID = "mrClientId";
50 private int deleteLevel;
51 private Map<String, MR_Client> mr_clients = DatabaseClass.getMr_clients();
52 private Map<String, MR_Cluster> clusters = DatabaseClass.getMr_clusters();
53 private Map<String, DcaeLocation> locations = DatabaseClass.getDcaeLocations();
54 private DmaapService dmaap = new DmaapService();
55 private String centralCname;
57 public MR_ClientService() {
58 DmaapConfig p = (DmaapConfig) DmaapConfig.getConfig();
60 centralCname = p.getProperty("MR.CentralCname", "MRcname.not.set");
61 deleteLevel = Integer.valueOf(p.getProperty("MR.ClientDeleteLevel", "0"));
64 public List<MR_Client> getAllMr_Clients() {
65 return new ArrayList<>(mr_clients.values());
68 List<MR_Client> getAllMrClients(String fqtn) {
69 ArrayList<MR_Client> results = new ArrayList<>();
70 for (Map.Entry<String, MR_Client> entry : mr_clients.entrySet()) {
71 MR_Client client = entry.getValue();
72 if (fqtn.equals(client.getFqtn())) {
79 List<MR_Client> getClientsByLocation(String location) {
80 List<MR_Client> results = new ArrayList<>();
81 for (Map.Entry<String, MR_Client> entry : mr_clients.entrySet()) {
82 MR_Client client = entry.getValue();
83 if (location.equals(client.getDcaeLocationName())) {
91 public MR_Client getMr_Client(String key, ApiError apiError) {
92 MR_Client c = mr_clients.get(key);
94 apiError.setCode(Status.NOT_FOUND.getStatusCode());
95 apiError.setFields(MR_CLIENT_ID);
96 apiError.setMessage(MR_CLIENT_ID + " " + key + " not found");
98 apiError.setCode(200);
103 public MR_Client addMr_Client(MR_Client client, Topic topic, ApiError err) {
104 if (client.getDcaeLocationName().isEmpty()) {
105 logger.info("Client dcaeLocation that doesn't exist or not specified");
108 // original style: clients specified Role. This has precedence for backwards
110 // ONAP style: clients specify Identity to be assigned to generated Role
111 String role = client.getClientRole();
113 grantClientRolePerms(client, err);
114 } else if (client.hasClientIdentity()) {
115 if (client.isSubscriber()) {
116 role = topic.getSubscriberRole();
117 assignIdentityToRole(client, role, err);
119 if (client.isPublisher()) {
120 role = topic.getPublisherRole();
121 assignIdentityToRole(client, role, err);
124 if (!client.isStatusValid()) {
127 String centralFqdn = null;
128 DcaeLocation candidate = locations.get(client.getDcaeLocationName());
130 MR_Cluster cluster = clusters.get(client.getDcaeLocationName());
131 if (cluster != null && candidate != null) {
132 if (candidate.isCentral() && !topic.getReplicationCase().involvesFQDN()) {
133 centralFqdn = centralCname;
135 client.setTopicURL(cluster.genTopicURL(centralFqdn, client.getFqtn()));
136 if (centralFqdn == null) {
137 client.setStatus(addTopicToCluster(cluster, topic, err));
138 if (!err.is2xx() && err.getCode() != 409) {
139 topic.setFqtn(err.getMessage());
144 MR_ClusterService clusters = new MR_ClusterService();
145 // MM should only exist for edge-to-central
146 // we use a cname for the central target (default resiliency with no replicationGroup set)
147 // but still need to provision topics on all central MRs
148 for (MR_Cluster central : clusters.getCentralClusters()) {
149 client.setStatus(addTopicToCluster(central, topic, err));
150 if (!err.is2xx() && err.getCode() != 409) {
151 topic.setFqtn(err.getMessage());
158 logger.warn("Client references a dcaeLocation that doesn't exist:" + client.getDcaeLocationName());
159 client.setStatus(DmaapObject_Status.STAGED);
162 mr_clients.put(client.getMrClientId(), client);
169 private DmaapObject_Status addTopicToCluster(MR_Cluster cluster, Topic topic, ApiError err) {
171 MrProvConnection prov = new MrProvConnection();
172 logger.info("POST topic " + topic.getFqtn() + " to cluster " + cluster.getFqdn() + " in loc " + cluster.getDcaeLocationName());
173 if (prov.makeTopicConnection(cluster)) {
174 prov.doPostTopic(topic, err);
175 logger.info("response code: " + err.getCode());
176 if (err.is2xx() || err.getCode() == 409) {
177 return DmaapObject_Status.VALID;
180 return DmaapObject_Status.INVALID;
183 private void grantClientRolePerms(MR_Client client, ApiError err) {
184 AafService aaf = new AafServiceImpl(ServiceType.AAF_TopicMgr);
186 String instance = ":topic." + client.getFqtn();
187 client.setStatus(DmaapObject_Status.VALID);
188 String role = client.getClientRole();
189 for (String want : client.getAction()) {
191 DmaapPerm perm = new DmaapPerm(dmaap.getTopicPerm(), instance, want);
193 DmaapGrant g = new DmaapGrant(perm, role);
194 rc = aaf.addGrant(g);
195 if (rc != 201 && rc != 409) {
196 client.setStatus(DmaapObject_Status.INVALID);
198 err.setMessage("Grant of " + dmaap.getTopicPerm() + "|" + instance + "|" + want + " failed for " + role);
199 logger.warn(err.getMessage());
203 logger.warn("No Grant of " + dmaap.getTopicPerm() + "|" + instance + "|" + want + " because role is null ");
208 private void assignIdentityToRole(MR_Client client, String role, ApiError err) {
209 AafService aaf = new AafServiceImpl(ServiceType.AAF_TopicMgr);
211 AafUserRole ur = new AafUserRole(client.getClientIdentity(), role);
212 int rc = aaf.addUserRole(ur);
213 if (rc != 201 && rc != 409) {
214 client.setStatus(DmaapObject_Status.INVALID);
216 err.setMessage("Failed to add user " + client.getClientIdentity() + " to " + role);
217 logger.warn(err.getMessage());
220 client.setStatus(DmaapObject_Status.VALID);
224 private void revokeClientPerms(MR_Client client, ApiError err) {
225 AafService aaf = new AafServiceImpl(ServiceType.AAF_TopicMgr);
227 String instance = ":topic." + client.getFqtn();
228 client.setStatus(DmaapObject_Status.VALID);
229 for (String want : client.getAction()) {
231 DmaapPerm perm = new DmaapPerm(dmaap.getTopicPerm(), instance, want);
232 DmaapGrant g = new DmaapGrant(perm, client.getClientRole());
233 rc = aaf.delGrant(g);
234 if (rc != 200 && rc != 404) {
235 client.setStatus(DmaapObject_Status.INVALID);
237 err.setMessage("Revoke of " + dmaap.getTopicPerm() + "|" + instance + "|" + want + " failed for " + client.getClientRole());
238 logger.warn(err.getMessage());
244 public MR_Client updateMr_Client(MR_Client client, ApiError apiError) {
245 MR_Client c = mr_clients.get(client.getMrClientId());
247 apiError.setCode(Status.NOT_FOUND.getStatusCode());
248 apiError.setFields(MR_CLIENT_ID);
249 apiError.setMessage("mrClientId " + client.getMrClientId() + " not found");
251 apiError.setCode(200);
253 mr_clients.put(client.getMrClientId(), client);
257 public void removeMr_Client(String key, boolean updateTopicView, ApiError apiError) {
258 MR_Client client = mr_clients.get(key);
259 if (client == null) {
260 apiError.setCode(Status.NOT_FOUND.getStatusCode());
261 apiError.setFields(MR_CLIENT_ID);
262 apiError.setMessage("mrClientId " + key + " not found");
265 apiError.setCode(200);
268 if (updateTopicView) {
270 TopicService topics = new TopicService();
272 Topic t = topics.getTopic(client.getFqtn(), apiError);
274 List<MR_Client> tc = t.getClients();
275 for (MR_Client c : tc) {
276 if (c.getMrClientId().equals(client.getMrClientId())) {
282 topics.updateTopic(t, apiError);
288 if (deleteLevel >= 2) {
289 revokeClientPerms(client, apiError);
290 if (!apiError.is2xx()) {
295 if (deleteLevel >= 1) {
296 mr_clients.remove(key);