2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.dmaap.dbcapi.service;
23 import java.util.ArrayList;
24 import java.util.List;
27 import javax.ws.rs.core.Response.Status;
37 import org.onap.dmaap.dbcapi.aaf.AafService;
38 import org.onap.dmaap.dbcapi.aaf.DmaapGrant;
39 import org.onap.dmaap.dbcapi.aaf.DmaapPerm;
40 import org.onap.dmaap.dbcapi.aaf.AafService.ServiceType;
41 import org.onap.dmaap.dbcapi.aaf.AafUserRole;
42 import org.onap.dmaap.dbcapi.client.MrProvConnection;
43 import org.onap.dmaap.dbcapi.database.DatabaseClass;
44 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
45 import org.onap.dmaap.dbcapi.model.ApiError;
46 import org.onap.dmaap.dbcapi.model.DcaeLocation;
47 import org.onap.dmaap.dbcapi.model.MR_Client;
48 import org.onap.dmaap.dbcapi.model.MR_Cluster;
49 import org.onap.dmaap.dbcapi.model.Topic;
50 import org.onap.dmaap.dbcapi.model.DmaapObject.DmaapObject_Status;
51 import org.onap.dmaap.dbcapi.util.DmaapConfig;
53 public class MR_ClientService extends BaseLoggingClass{
55 private int deleteLevel;
56 private Map<String, MR_Client> mr_clients = DatabaseClass.getMr_clients();
57 private Map<String, MR_Cluster> clusters = DatabaseClass.getMr_clusters();
58 private Map<String, Topic> topics = DatabaseClass.getTopics();
59 private Map<String, DcaeLocation> locations = DatabaseClass.getDcaeLocations();
60 private DmaapService dmaap = new DmaapService();
61 private String centralCname;
63 public MR_ClientService() {
64 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
66 centralCname = p.getProperty("MR.CentralCname", "MRcname.not.set");
67 deleteLevel = Integer.valueOf(p.getProperty("MR.ClientDeleteLevel", "0" ));
70 public Map<String, MR_Client> getMR_Clients() {
74 public List<MR_Client> getAllMr_Clients() {
75 return new ArrayList<MR_Client>(mr_clients.values());
78 public ArrayList<MR_Client> getAllMrClients(String fqtn) {
79 ArrayList<MR_Client> results = new ArrayList<MR_Client>();
80 for (Map.Entry<String, MR_Client> entry : mr_clients.entrySet())
82 MR_Client client = entry.getValue();
83 if ( fqtn.equals(client.getFqtn() ) ) {
84 results.add( client );
90 public ArrayList<MR_Client> getClientsByLocation(String location) {
91 ArrayList<MR_Client> results = new ArrayList<MR_Client>();
92 for (Map.Entry<String, MR_Client> entry : mr_clients.entrySet())
94 MR_Client client = entry.getValue();
95 if ( location.equals(client.getDcaeLocationName() ) ) {
96 results.add( client );
102 public void refreshClients( String location ) {
103 ApiError err = new ApiError();
104 ArrayList<MR_Client> clients = getClientsByLocation( location );
105 for( MR_Client client : clients ) {
106 Topic topic = topics.get(client.getFqtn());
107 if ( topic != null ) {
108 addMr_Client( client, topic, err);
115 public MR_Client getMr_Client( String key, ApiError apiError ) {
116 MR_Client c = mr_clients.get( key );
118 apiError.setCode(Status.NOT_FOUND.getStatusCode());
119 apiError.setFields( "mrClientId");
120 apiError.setMessage("mrClientId " + key + " not found" );
122 apiError.setCode(200);
127 public MR_Client addMr_Client( MR_Client client, Topic topic, ApiError err ) {
128 if ( client.getDcaeLocationName().isEmpty()) {
129 logger.info( "Client dcaeLocation that doesn't exist or not specified" );
132 // original style: clients specified Role. This has precedence for backwards
134 // ONAP style: clients specify Identity to be assigned to generated Role
135 String role = client.getClientRole();
136 if ( role != null ) {
137 grantClientRolePerms( client, err);
138 } else if ( client.hasClientIdentity() ){
139 if ( client.isSubscriber() ) {
140 role = topic.getSubscriberRole();
141 assignIdentityToRole( client, role, err );
143 if (client.isPublisher() ) {
144 role = topic.getPublisherRole();
145 assignIdentityToRole( client, role, err );
148 if ( ! client.isStatusValid()) {
151 String centralFqdn = null;
152 DcaeLocation candidate = locations.get(client.getDcaeLocationName());
154 MR_Cluster cluster = clusters.get( client.getDcaeLocationName());
155 if ( cluster != null && candidate != null ) {
156 if ( candidate.isCentral() && ! topic.getReplicationCase().involvesFQDN() ) {
157 centralFqdn = centralCname;
159 client.setTopicURL(cluster.genTopicURL(centralFqdn, client.getFqtn()));
160 if ( centralFqdn == null ) {
161 client.setStatus( addTopicToCluster( cluster, topic, err));
162 if( ! err.is2xx() && err.getCode() != 409 ) {
163 topic.setFqtn(err.getMessage());
168 MR_ClusterService clusters = new MR_ClusterService();
169 // MM should only exist for edge-to-central
170 // we use a cname for the central target (default resiliency with no replicationGroup set)
171 // but still need to provision topics on all central MRs
172 for( MR_Cluster central: clusters.getCentralClusters() ) {
173 client.setStatus( addTopicToCluster( central, topic, err));
174 if( ! err.is2xx() && err.getCode() != 409 ) {
175 topic.setFqtn(err.getMessage());
182 logger.warn( "Client references a dcaeLocation that doesn't exist:" + client.getDcaeLocationName());
183 client.setStatus( DmaapObject_Status.STAGED);
187 mr_clients.put( client.getMrClientId(), client );
194 private DmaapObject_Status addTopicToCluster( MR_Cluster cluster, Topic topic, ApiError err ){
196 MrProvConnection prov = new MrProvConnection();
197 logger.info( "POST topic " + topic.getFqtn() + " to cluster " + cluster.getFqdn() + " in loc " + cluster.getDcaeLocationName());
198 if ( prov.makeTopicConnection(cluster)) {
199 String resp = prov.doPostTopic(topic, err);
200 logger.info( "response code: " + err.getCode() );
201 if ( err.is2xx() || err.getCode() == 409 ) {
202 return DmaapObject_Status.VALID;
205 return DmaapObject_Status.INVALID;
208 private void grantClientRolePerms( MR_Client client, ApiError err) {
209 AafService aaf = new AafService(ServiceType.AAF_TopicMgr);
211 String instance = ":topic." + client.getFqtn();
212 client.setStatus( DmaapObject_Status.VALID);
213 String role = client.getClientRole();
214 for( String want : client.getAction() ) {
216 DmaapPerm perm = new DmaapPerm( dmaap.getTopicPerm(), instance, want );
217 if ( role != null ) {
218 DmaapGrant g = new DmaapGrant( perm, role );
219 rc = aaf.addGrant( g );
220 if ( rc != 201 && rc != 409 ) {
221 client.setStatus( DmaapObject_Status.INVALID);
223 err.setMessage( "Grant of " + dmaap.getTopicPerm() + "|" + instance + "|" + want + " failed for " + role );
224 logger.warn( err.getMessage());
228 logger.warn( "No Grant of " + dmaap.getTopicPerm() + "|" + instance + "|" + want + " because role is null " );
233 private void assignIdentityToRole( MR_Client client, String role, ApiError err ) {
234 AafService aaf = new AafService(ServiceType.AAF_TopicMgr);
236 AafUserRole ur = new AafUserRole( client.getClientIdentity(), role );
237 int rc = aaf.addUserRole( ur );
238 if ( rc != 201 && rc != 409 ) {
239 client.setStatus( DmaapObject_Status.INVALID);
241 err.setMessage( "Failed to add user " + client.getClientIdentity()+ " to " + role );
242 logger.warn( err.getMessage());
245 client.setStatus( DmaapObject_Status.VALID);
248 private void revokeClientPerms( MR_Client client, ApiError err) {
249 AafService aaf = new AafService(ServiceType.AAF_TopicMgr);
251 String instance = ":topic." + client.getFqtn();
252 client.setStatus( DmaapObject_Status.VALID);
253 for( String want : client.getAction() ) {
255 DmaapPerm perm = new DmaapPerm( dmaap.getTopicPerm(), instance, want );
256 DmaapGrant g = new DmaapGrant( perm, client.getClientRole() );
257 rc = aaf.delGrant( g );
258 if ( rc != 200 && rc != 404 ) {
259 client.setStatus( DmaapObject_Status.INVALID);
261 err.setMessage( "Revoke of " + dmaap.getTopicPerm() + "|" + instance + "|" + want + " failed for " + client.getClientRole() );
262 logger.warn( err.getMessage());
268 public MR_Client updateMr_Client( MR_Client client, ApiError apiError ) {
269 MR_Client c = mr_clients.get( client.getMrClientId());
271 apiError.setCode(Status.NOT_FOUND.getStatusCode());
272 apiError.setFields( "mrClientId");
273 apiError.setMessage("mrClientId " + client.getMrClientId() + " not found" );
275 apiError.setCode(200);
277 mr_clients.put( client.getMrClientId(), client );
281 public void removeMr_Client( String key, boolean updateTopicView, ApiError apiError ) {
282 MR_Client client = mr_clients.get( key );
283 if ( client == null ) {
284 apiError.setCode(Status.NOT_FOUND.getStatusCode());
285 apiError.setFields( "mrClientId");
286 apiError.setMessage("mrClientId " + key + " not found" );
289 apiError.setCode(200);
292 if ( updateTopicView == true ) {
294 TopicService topics = new TopicService();
296 Topic t = topics.getTopic(client.getFqtn(), apiError );
298 ArrayList<MR_Client> tc = t.getClients();
299 for( MR_Client c: tc) {
300 if ( c.getMrClientId().equals(client.getMrClientId())) {
306 topics.updateTopic( t, apiError );
313 if ( deleteLevel >= 2 ) {
314 revokeClientPerms( client, apiError );
315 if ( ! apiError.is2xx()) {
320 if ( deleteLevel >= 1 ) {
321 mr_clients.remove(key);