not used methods were removed, minor sonar fixes
[dmaap/dbcapi.git] / src / main / java / org / onap / dmaap / dbcapi / service / MR_ClientService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dmaap
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  *
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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=========================================================
21  */
22
23 package org.onap.dmaap.dbcapi.service;
24
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.Map;
28
29 import javax.ws.rs.core.Response.Status;
30
31
32
33
34
35
36
37
38
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;
54
55 public class MR_ClientService extends BaseLoggingClass{
56
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;
64         
65         public MR_ClientService() {
66                 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
67         
68                 centralCname = p.getProperty("MR.CentralCname", "MRcname.not.set");
69                 deleteLevel = Integer.valueOf(p.getProperty("MR.ClientDeleteLevel", "0" ));
70         }
71                 
72         public List<MR_Client> getAllMr_Clients() {
73                 return new ArrayList<>(mr_clients.values());
74         }
75         
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())
79                 {
80                         MR_Client client = entry.getValue();
81                     if ( fqtn.equals(client.getFqtn() ) ) {
82                         results.add( client );
83                     }
84                 }
85                 return results;
86         }       
87
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())
91                 {
92                         MR_Client client = entry.getValue();
93                     if ( location.equals(client.getDcaeLocationName() ) ) {
94                         results.add( client );
95                     }
96                 }
97                 return results;
98         }       
99
100         
101         public MR_Client getMr_Client( String key, ApiError apiError ) {                        
102                 MR_Client c =  mr_clients.get( key );
103                 if ( c == null ) {
104                         apiError.setCode(Status.NOT_FOUND.getStatusCode());
105                         apiError.setFields(MR_CLIENT_ID);
106                         apiError.setMessage(MR_CLIENT_ID+ " " + key + " not found" );
107                 } else {
108                         apiError.setCode(200);
109                 }
110                 return c;
111         }
112
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" );
116                         return null;
117                 }
118                 // original style: clients specified Role.  This has precedence for backwards
119                 //                 compatibility.
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 );
128                         } 
129                         if (client.isPublisher() ) {
130                                 role = topic.getPublisherRole();
131                                 assignIdentityToRole( client, role, err );
132                         }       
133                 } 
134                 if ( ! client.isStatusValid()) {
135                         return null;
136                 }
137                 String centralFqdn = null;
138                 DcaeLocation candidate = locations.get(client.getDcaeLocationName());
139
140                 MR_Cluster cluster = clusters.get( client.getDcaeLocationName());
141                 if (  cluster != null && candidate != null ) {
142                         if ( candidate.isCentral() && ! topic.getReplicationCase().involvesFQDN() ) {
143                                 centralFqdn = centralCname;
144                         }
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());
150                                         return null;
151                                 }
152                         
153                         } else {
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());
162                                                 return null;
163                                         }
164                                 }
165                         }
166                         
167                 } else {
168                         logger.warn( "Client references a dcaeLocation that doesn't exist:" + client.getDcaeLocationName());
169                         client.setStatus( DmaapObject_Status.STAGED);
170                 }
171
172                 mr_clients.put( client.getMrClientId(), client );
173
174                 err.setCode(200);
175                 
176                 return client;
177         }
178         
179         private DmaapObject_Status addTopicToCluster( MR_Cluster cluster, Topic topic, ApiError err  ){
180                 
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;
188                         } 
189                 }
190                 return DmaapObject_Status.INVALID;
191         }
192         
193         private void grantClientRolePerms( MR_Client client, ApiError err) {
194                 AafService aaf = new AafService(ServiceType.AAF_TopicMgr);
195                 
196                 String instance = ":topic." + client.getFqtn();
197                 client.setStatus( DmaapObject_Status.VALID);
198                 String role = client.getClientRole();
199                 for( String want : client.getAction() ) {
200                         int rc;
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);
207                                         err.setCode(rc);
208                                         err.setMessage( "Grant of " + dmaap.getTopicPerm() + "|" + instance + "|" + want + " failed for " + role );
209                                         logger.warn( err.getMessage());
210                                         return;
211                                 } 
212                         } else {
213                                 logger.warn( "No Grant of " + dmaap.getTopicPerm() + "|" + instance + "|" + want + " because role is null " );
214                         }
215                 }
216         }
217         
218         private void assignIdentityToRole( MR_Client client, String role, ApiError err ) {
219                 AafService aaf = new AafService(ServiceType.AAF_TopicMgr);
220
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);
225                         err.setCode(rc);
226                         err.setMessage( "Failed to add user " + client.getClientIdentity()+  "  to " + role );
227                         logger.warn( err.getMessage());
228                         return;
229                 }
230                 client.setStatus( DmaapObject_Status.VALID);
231
232         }
233         private void revokeClientPerms( MR_Client client, ApiError err) {
234                 AafService aaf = new AafService(ServiceType.AAF_TopicMgr);
235                 
236                 String instance = ":topic." + client.getFqtn();
237                 client.setStatus( DmaapObject_Status.VALID);
238                 for( String want : client.getAction() ) {
239                         int rc;
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);
245                                 err.setCode(rc);
246                                 err.setMessage( "Revoke of " + dmaap.getTopicPerm() + "|" + instance + "|" + want + " failed for " + client.getClientRole() );
247                                 logger.warn( err.getMessage());
248                                 return;
249                         } 
250                 }
251         }
252         
253         public MR_Client updateMr_Client( MR_Client client, ApiError apiError ) {
254                 MR_Client c =  mr_clients.get( client.getMrClientId());
255                 if ( c == null ) {
256                         apiError.setCode(Status.NOT_FOUND.getStatusCode());
257                         apiError.setFields(MR_CLIENT_ID);
258                         apiError.setMessage("mrClientId " + client.getMrClientId() + " not found" );
259                 } else {
260                         apiError.setCode(200);
261                 }
262                 mr_clients.put( client.getMrClientId(), client );
263                 return client;
264         }
265                 
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" );
272                         return;
273                 } else {
274                         apiError.setCode(200);
275                 }
276                 
277                 if (updateTopicView) {
278
279                         TopicService topics = new TopicService();
280                         
281                         Topic t = topics.getTopic(client.getFqtn(), apiError );
282                         if ( t != null ) {      
283                                 List<MR_Client> tc = t.getClients();
284                                 for( MR_Client c: tc) {
285                                         if ( c.getMrClientId().equals(client.getMrClientId())) {
286                                                 tc.remove(c);
287                                                 break;
288                                         }
289                                 }
290                                 t.setClients(tc);
291                                 topics.updateTopic( t, apiError );
292                         }
293
294                 }
295
296                 // remove from AAF
297                 if ( deleteLevel >= 2 ) {
298                         revokeClientPerms( client, apiError );
299                         if ( ! apiError.is2xx()) {
300                                 return;
301                         }
302                 }
303                 // remove from DB 
304                 if ( deleteLevel >= 1 ) {
305                         mr_clients.remove(key);
306                 }
307         }
308         
309 }