Refactor code to support no AAF requests
[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  * 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.service;
22
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Map;
26
27 import javax.ws.rs.core.Response.Status;
28
29
30
31
32
33
34
35
36
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.client.MrProvConnection;
42 import org.onap.dmaap.dbcapi.database.DatabaseClass;
43 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
44 import org.onap.dmaap.dbcapi.model.ApiError;
45 import org.onap.dmaap.dbcapi.model.DcaeLocation;
46 import org.onap.dmaap.dbcapi.model.MR_Client;
47 import org.onap.dmaap.dbcapi.model.MR_Cluster;
48 import org.onap.dmaap.dbcapi.model.Topic;
49 import org.onap.dmaap.dbcapi.model.DmaapObject.DmaapObject_Status;
50 import org.onap.dmaap.dbcapi.util.DmaapConfig;
51
52 public class MR_ClientService extends BaseLoggingClass{
53
54         private int deleteLevel;
55         private Map<String, MR_Client> mr_clients = DatabaseClass.getMr_clients();
56         private Map<String, MR_Cluster> clusters = DatabaseClass.getMr_clusters();
57         private Map<String, Topic> topics = DatabaseClass.getTopics();
58         private Map<String, DcaeLocation> locations = DatabaseClass.getDcaeLocations();
59         private DmaapService dmaap = new DmaapService();
60         
61         public MR_ClientService() {
62                 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
63                 
64                 deleteLevel = Integer.valueOf(p.getProperty("MR.ClientDeleteLevel", "0" ));
65         }
66         
67         public Map<String, MR_Client> getMR_Clients() {                 
68                 return mr_clients;
69         }
70                 
71         public List<MR_Client> getAllMr_Clients() {
72                 return new ArrayList<MR_Client>(mr_clients.values());
73         }
74         
75         public ArrayList<MR_Client> getAllMrClients(String fqtn) {
76                 ArrayList<MR_Client> results = new ArrayList<MR_Client>();
77                 for (Map.Entry<String, MR_Client> entry : mr_clients.entrySet())
78                 {
79                         MR_Client client = entry.getValue();
80                     if ( fqtn.equals(client.getFqtn() ) ) {
81                         results.add( client );
82                     }
83                 }
84                 return results;
85         }       
86
87         public ArrayList<MR_Client> getClientsByLocation(String location) {
88                 ArrayList<MR_Client> results = new ArrayList<MR_Client>();
89                 for (Map.Entry<String, MR_Client> entry : mr_clients.entrySet())
90                 {
91                         MR_Client client = entry.getValue();
92                     if ( location.equals(client.getDcaeLocationName() ) ) {
93                         results.add( client );
94                     }
95                 }
96                 return results;
97         }       
98         
99         public void refreshClients( String location ) {
100                 ApiError err = new ApiError();
101                 ArrayList<MR_Client> clients = getClientsByLocation( location );
102                 for( MR_Client client : clients ) {
103                         Topic topic = topics.get(client.getFqtn());
104                         if ( topic != null ) {
105                                 addMr_Client( client, topic, err);
106                         }
107                         
108                         
109                 }
110         }
111         
112         public MR_Client getMr_Client( String key, ApiError apiError ) {                        
113                 MR_Client c =  mr_clients.get( key );
114                 if ( c == null ) {
115                         apiError.setCode(Status.NOT_FOUND.getStatusCode());
116                         apiError.setFields( "mrClientId");
117                         apiError.setMessage("mrClientId " + key + " not found" );
118                 } else {
119                         apiError.setCode(200);
120                 }
121                 return c;
122         }
123
124         public MR_Client addMr_Client( MR_Client client, Topic topic, ApiError err ) {
125                 if ( client.getDcaeLocationName().isEmpty()) {
126                         logger.info( "Client  dcaeLocation that doesn't exist or not specified" );
127                         return null;
128                 }
129                 grantClientPerms(  client,  err);
130                 if ( ! client.isStatusValid()) {
131                         return null;
132                 }
133                 String centralFqdn = null;
134                 DcaeLocation candidate = locations.get(client.getDcaeLocationName());
135                 if ( candidate != null && candidate.isCentral() ) {
136                         DmaapConfig p = ( DmaapConfig)DmaapConfig.getConfig();
137                         centralFqdn = p.getProperty("MR.CentralCname");
138                 }
139                 MR_Cluster cluster = clusters.get( client.getDcaeLocationName());
140                 if (  cluster != null ) {
141                         client.setTopicURL(cluster.genTopicURL(centralFqdn, client.getFqtn()));
142                         if ( centralFqdn == null ) {
143                                 client.setStatus( addTopicToCluster( cluster, topic, err));
144                                 if( ! err.is2xx() && err.getCode() != 409 ) {
145                                         topic.setFqtn(err.getMessage());
146                                         return null;
147                                 }
148                         
149                         } else {
150                                 MR_ClusterService clusters = new MR_ClusterService();   
151                                 // in 1610, MM should only exist for edge-to-central
152                                 //  we use a cname for the central target
153                                 // but still need to provision topics on all central MRs
154                                 for( MR_Cluster central: clusters.getCentralClusters() ) {
155                                         client.setStatus( addTopicToCluster( central, topic, err));
156                                         if( ! err.is2xx() && err.getCode() != 409 ) {
157                                                 topic.setFqtn(err.getMessage());
158                                                 return null;
159                                         }
160                                 }
161                         }
162                         
163                 } else {
164                         logger.info( "Client references a dcaeLocation that doesn't exist:" + client.getDcaeLocationName());
165                         client.setStatus( DmaapObject_Status.STAGED);
166                         //return null;
167                 }
168
169                 mr_clients.put( client.getMrClientId(), client );
170
171                 err.setCode(200);
172                 
173                 return client;
174         }
175         
176         private DmaapObject_Status addTopicToCluster( MR_Cluster cluster, Topic topic, ApiError err  ){
177                 
178                 MrProvConnection prov = new MrProvConnection();
179                 logger.info( "POST topic " + topic.getFqtn() + " to cluster " + cluster.getFqdn() + " in loc " + cluster.getDcaeLocationName());
180                 if ( prov.makeTopicConnection(cluster)) {
181                         String resp = prov.doPostTopic(topic, err);
182                         logger.info( "response code: " + err.getCode() );
183                         if ( err.is2xx() || err.getCode() == 409 ) {
184                                 return DmaapObject_Status.VALID;
185                         } 
186                 }
187                 return DmaapObject_Status.INVALID;
188         }
189         
190         private void grantClientPerms( MR_Client client, ApiError err) {
191                 AafService aaf = new AafService(ServiceType.AAF_TopicMgr);
192                 
193                 String instance = ":topic." + client.getFqtn();
194                 client.setStatus( DmaapObject_Status.VALID);
195                 for( String want : client.getAction() ) {
196                         int rc;
197                         DmaapPerm perm = new DmaapPerm( dmaap.getTopicPerm(), instance, want );
198                         DmaapGrant g = new DmaapGrant( perm, client.getClientRole() );
199                         rc = aaf.addGrant( g );
200                         if ( rc != 201 && rc != 409 ) {
201                                 client.setStatus( DmaapObject_Status.INVALID);
202                                 err.setCode(rc);
203                                 err.setMessage( "Grant of " + dmaap.getTopicPerm() + "|" + instance + "|" + want + " failed for " + client.getClientRole() );
204                                 logger.warn( err.getMessage());
205                                 return;
206                         } 
207                 }
208         }
209         
210         private void revokeClientPerms( MR_Client client, ApiError err) {
211                 AafService aaf = new AafService(ServiceType.AAF_TopicMgr);
212                 
213                 String instance = ":topic." + client.getFqtn();
214                 client.setStatus( DmaapObject_Status.VALID);
215                 for( String want : client.getAction() ) {
216                         int rc;
217                         DmaapPerm perm = new DmaapPerm( dmaap.getTopicPerm(), instance, want );
218                         DmaapGrant g = new DmaapGrant( perm, client.getClientRole() );
219                         rc = aaf.delGrant( g );
220                         if ( rc != 200 && rc != 404 ) {
221                                 client.setStatus( DmaapObject_Status.INVALID);
222                                 err.setCode(rc);
223                                 err.setMessage( "Revoke of " + dmaap.getTopicPerm() + "|" + instance + "|" + want + " failed for " + client.getClientRole() );
224                                 logger.warn( err.getMessage());
225                                 return;
226                         } 
227                 }
228         }
229         
230         public MR_Client updateMr_Client( MR_Client client, ApiError apiError ) {
231                 MR_Client c =  mr_clients.get( client.getMrClientId());
232                 if ( c == null ) {
233                         apiError.setCode(Status.NOT_FOUND.getStatusCode());
234                         apiError.setFields( "mrClientId");
235                         apiError.setMessage("mrClientId " + client.getMrClientId() + " not found" );
236                 } else {
237                         apiError.setCode(200);
238                 }
239                 mr_clients.put( client.getMrClientId(), client );
240                 return client;
241         }
242                 
243         public void removeMr_Client( String key, boolean updateTopicView, ApiError apiError ) {
244                 MR_Client client =  mr_clients.get( key );
245                 if ( client == null ) {
246                         apiError.setCode(Status.NOT_FOUND.getStatusCode());
247                         apiError.setFields( "mrClientId");
248                         apiError.setMessage("mrClientId " + key + " not found" );
249                         return;
250                 } else {
251                         apiError.setCode(200);
252                 }
253                 
254                 if ( updateTopicView == true ) {
255
256                         TopicService topics = new TopicService();
257                         
258                         Topic t = topics.getTopic(client.getFqtn(), apiError );
259                         if ( t != null ) {      
260                                 ArrayList<MR_Client> tc = t.getClients();
261                                 for( MR_Client c: tc) {
262                                         if ( c.getMrClientId().equals(client.getMrClientId())) {
263                                                 tc.remove(c);
264                                                 break;
265                                         }
266                                 }
267                                 t.setClients(tc);
268                                 topics.updateTopic( t, apiError );
269                         }
270
271                 }
272
273                 
274                 // remove from AAF
275                 if ( deleteLevel >= 2 ) {
276                         revokeClientPerms( client, apiError );
277                         if ( ! apiError.is2xx()) {
278                                 return;
279                         }
280                 }
281                 // remove from DB 
282                 if ( deleteLevel >= 1 ) {
283                         mr_clients.remove(key);
284                 }
285
286                 return;
287         }
288         
289 }