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 java.util.ArrayList;
26 import java.util.List;
29 import javax.ws.rs.core.Response.Status;
39 import org.onap.dmaap.dbcapi.aaf.AafService;
40 import org.onap.dmaap.dbcapi.aaf.DmaapGrant;
41 import org.onap.dmaap.dbcapi.aaf.DmaapPerm;
42 import org.onap.dmaap.dbcapi.aaf.AafService.ServiceType;
43 import org.onap.dmaap.dbcapi.aaf.AafUserRole;
44 import org.onap.dmaap.dbcapi.client.MrProvConnection;
45 import org.onap.dmaap.dbcapi.database.DatabaseClass;
46 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
47 import org.onap.dmaap.dbcapi.model.ApiError;
48 import org.onap.dmaap.dbcapi.model.DcaeLocation;
49 import org.onap.dmaap.dbcapi.model.MR_Client;
50 import org.onap.dmaap.dbcapi.model.MR_Cluster;
51 import org.onap.dmaap.dbcapi.model.Topic;
52 import org.onap.dmaap.dbcapi.model.DmaapObject.DmaapObject_Status;
53 import org.onap.dmaap.dbcapi.util.DmaapConfig;
55 public class MR_ClientService extends BaseLoggingClass{
57 private static final String MR_CLIENT_ID = "mrClientId";
58 private int deleteLevel;
59 private Map<String, MR_Client> mr_clients = DatabaseClass.getMr_clients();
60 private Map<String, MR_Cluster> clusters = DatabaseClass.getMr_clusters();
61 private Map<String, DcaeLocation> locations = DatabaseClass.getDcaeLocations();
62 private DmaapService dmaap = new DmaapService();
63 private String centralCname;
65 public MR_ClientService() {
66 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
68 centralCname = p.getProperty("MR.CentralCname", "MRcname.not.set");
69 deleteLevel = Integer.valueOf(p.getProperty("MR.ClientDeleteLevel", "0" ));
72 public List<MR_Client> getAllMr_Clients() {
73 return new ArrayList<>(mr_clients.values());
76 List<MR_Client> getAllMrClients(String fqtn) {
77 ArrayList<MR_Client> results = new ArrayList<>();
78 for (Map.Entry<String, MR_Client> entry : mr_clients.entrySet())
80 MR_Client client = entry.getValue();
81 if ( fqtn.equals(client.getFqtn() ) ) {
82 results.add( client );
88 List<MR_Client> getClientsByLocation(String location) {
89 List<MR_Client> results = new ArrayList<>();
90 for (Map.Entry<String, MR_Client> entry : mr_clients.entrySet())
92 MR_Client client = entry.getValue();
93 if ( location.equals(client.getDcaeLocationName() ) ) {
94 results.add( client );
101 public MR_Client getMr_Client( String key, ApiError apiError ) {
102 MR_Client c = mr_clients.get( key );
104 apiError.setCode(Status.NOT_FOUND.getStatusCode());
105 apiError.setFields(MR_CLIENT_ID);
106 apiError.setMessage(MR_CLIENT_ID+ " " + key + " not found" );
108 apiError.setCode(200);
113 public MR_Client addMr_Client( MR_Client client, Topic topic, ApiError err ) {
114 if ( client.getDcaeLocationName().isEmpty()) {
115 logger.info( "Client dcaeLocation that doesn't exist or not specified" );
118 // original style: clients specified Role. This has precedence for backwards
120 // ONAP style: clients specify Identity to be assigned to generated Role
121 String role = client.getClientRole();
122 if ( role != null ) {
123 grantClientRolePerms( client, err);
124 } else if ( client.hasClientIdentity() ){
125 if ( client.isSubscriber() ) {
126 role = topic.getSubscriberRole();
127 assignIdentityToRole( client, role, err );
129 if (client.isPublisher() ) {
130 role = topic.getPublisherRole();
131 assignIdentityToRole( client, role, err );
134 if ( ! client.isStatusValid()) {
137 String centralFqdn = null;
138 DcaeLocation candidate = locations.get(client.getDcaeLocationName());
140 MR_Cluster cluster = clusters.get( client.getDcaeLocationName());
141 if ( cluster != null && candidate != null ) {
142 if ( candidate.isCentral() && ! topic.getReplicationCase().involvesFQDN() ) {
143 centralFqdn = centralCname;
145 client.setTopicURL(cluster.genTopicURL(centralFqdn, client.getFqtn()));
146 if ( centralFqdn == null ) {
147 client.setStatus( addTopicToCluster( cluster, topic, err));
148 if( ! err.is2xx() && err.getCode() != 409 ) {
149 topic.setFqtn(err.getMessage());
154 MR_ClusterService clusters = new MR_ClusterService();
155 // MM should only exist for edge-to-central
156 // we use a cname for the central target (default resiliency with no replicationGroup set)
157 // but still need to provision topics on all central MRs
158 for( MR_Cluster central: clusters.getCentralClusters() ) {
159 client.setStatus( addTopicToCluster( central, topic, err));
160 if( ! err.is2xx() && err.getCode() != 409 ) {
161 topic.setFqtn(err.getMessage());
168 logger.warn( "Client references a dcaeLocation that doesn't exist:" + client.getDcaeLocationName());
169 client.setStatus( DmaapObject_Status.STAGED);
172 mr_clients.put( client.getMrClientId(), client );
179 private DmaapObject_Status addTopicToCluster( MR_Cluster cluster, Topic topic, ApiError err ){
181 MrProvConnection prov = new MrProvConnection();
182 logger.info( "POST topic " + topic.getFqtn() + " to cluster " + cluster.getFqdn() + " in loc " + cluster.getDcaeLocationName());
183 if ( prov.makeTopicConnection(cluster)) {
184 prov.doPostTopic(topic, err);
185 logger.info( "response code: " + err.getCode() );
186 if ( err.is2xx() || err.getCode() == 409 ) {
187 return DmaapObject_Status.VALID;
190 return DmaapObject_Status.INVALID;
193 private void grantClientRolePerms( MR_Client client, ApiError err) {
194 AafService aaf = new AafService(ServiceType.AAF_TopicMgr);
196 String instance = ":topic." + client.getFqtn();
197 client.setStatus( DmaapObject_Status.VALID);
198 String role = client.getClientRole();
199 for( String want : client.getAction() ) {
201 DmaapPerm perm = new DmaapPerm( dmaap.getTopicPerm(), instance, want );
202 if ( role != null ) {
203 DmaapGrant g = new DmaapGrant( perm, role );
204 rc = aaf.addGrant( g );
205 if ( rc != 201 && rc != 409 ) {
206 client.setStatus( DmaapObject_Status.INVALID);
208 err.setMessage( "Grant of " + dmaap.getTopicPerm() + "|" + instance + "|" + want + " failed for " + role );
209 logger.warn( err.getMessage());
213 logger.warn( "No Grant of " + dmaap.getTopicPerm() + "|" + instance + "|" + want + " because role is null " );
218 private void assignIdentityToRole( MR_Client client, String role, ApiError err ) {
219 AafService aaf = new AafService(ServiceType.AAF_TopicMgr);
221 AafUserRole ur = new AafUserRole( client.getClientIdentity(), role );
222 int rc = aaf.addUserRole( ur );
223 if ( rc != 201 && rc != 409 ) {
224 client.setStatus( DmaapObject_Status.INVALID);
226 err.setMessage( "Failed to add user " + client.getClientIdentity()+ " to " + role );
227 logger.warn( err.getMessage());
230 client.setStatus( DmaapObject_Status.VALID);
233 private void revokeClientPerms( MR_Client client, ApiError err) {
234 AafService aaf = new AafService(ServiceType.AAF_TopicMgr);
236 String instance = ":topic." + client.getFqtn();
237 client.setStatus( DmaapObject_Status.VALID);
238 for( String want : client.getAction() ) {
240 DmaapPerm perm = new DmaapPerm( dmaap.getTopicPerm(), instance, want );
241 DmaapGrant g = new DmaapGrant( perm, client.getClientRole() );
242 rc = aaf.delGrant( g );
243 if ( rc != 200 && rc != 404 ) {
244 client.setStatus( DmaapObject_Status.INVALID);
246 err.setMessage( "Revoke of " + dmaap.getTopicPerm() + "|" + instance + "|" + want + " failed for " + client.getClientRole() );
247 logger.warn( err.getMessage());
253 public MR_Client updateMr_Client( MR_Client client, ApiError apiError ) {
254 MR_Client c = mr_clients.get( client.getMrClientId());
256 apiError.setCode(Status.NOT_FOUND.getStatusCode());
257 apiError.setFields(MR_CLIENT_ID);
258 apiError.setMessage("mrClientId " + client.getMrClientId() + " not found" );
260 apiError.setCode(200);
262 mr_clients.put( client.getMrClientId(), client );
266 public void removeMr_Client( String key, boolean updateTopicView, ApiError apiError ) {
267 MR_Client client = mr_clients.get( key );
268 if ( client == null ) {
269 apiError.setCode(Status.NOT_FOUND.getStatusCode());
270 apiError.setFields(MR_CLIENT_ID);
271 apiError.setMessage("mrClientId " + key + " not found" );
274 apiError.setCode(200);
277 if (updateTopicView) {
279 TopicService topics = new TopicService();
281 Topic t = topics.getTopic(client.getFqtn(), apiError );
283 List<MR_Client> tc = t.getClients();
284 for( MR_Client c: tc) {
285 if ( c.getMrClientId().equals(client.getMrClientId())) {
291 topics.updateTopic( t, apiError );
297 if ( deleteLevel >= 2 ) {
298 revokeClientPerms( client, apiError );
299 if ( ! apiError.is2xx()) {
304 if ( deleteLevel >= 1 ) {
305 mr_clients.remove(key);