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.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;
52 public class MR_ClientService extends BaseLoggingClass{
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();
61 public MR_ClientService() {
62 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
64 deleteLevel = Integer.valueOf(p.getProperty("MR.ClientDeleteLevel", "0" ));
67 public Map<String, MR_Client> getMR_Clients() {
71 public List<MR_Client> getAllMr_Clients() {
72 return new ArrayList<MR_Client>(mr_clients.values());
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())
79 MR_Client client = entry.getValue();
80 if ( fqtn.equals(client.getFqtn() ) ) {
81 results.add( client );
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())
91 MR_Client client = entry.getValue();
92 if ( location.equals(client.getDcaeLocationName() ) ) {
93 results.add( client );
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);
112 public MR_Client getMr_Client( String key, ApiError apiError ) {
113 MR_Client c = mr_clients.get( key );
115 apiError.setCode(Status.NOT_FOUND.getStatusCode());
116 apiError.setFields( "mrClientId");
117 apiError.setMessage("mrClientId " + key + " not found" );
119 apiError.setCode(200);
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" );
129 grantClientPerms( client, err);
130 if ( ! client.isStatusValid()) {
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");
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());
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());
164 logger.info( "Client references a dcaeLocation that doesn't exist:" + client.getDcaeLocationName());
165 client.setStatus( DmaapObject_Status.STAGED);
169 mr_clients.put( client.getMrClientId(), client );
176 private DmaapObject_Status addTopicToCluster( MR_Cluster cluster, Topic topic, ApiError err ){
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;
187 return DmaapObject_Status.INVALID;
190 private void grantClientPerms( MR_Client client, ApiError err) {
191 AafService aaf = new AafService(ServiceType.AAF_TopicMgr);
193 String instance = ":topic." + client.getFqtn();
194 client.setStatus( DmaapObject_Status.VALID);
195 for( String want : client.getAction() ) {
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);
203 err.setMessage( "Grant of " + dmaap.getTopicPerm() + "|" + instance + "|" + want + " failed for " + client.getClientRole() );
204 logger.warn( err.getMessage());
210 private void revokeClientPerms( MR_Client client, ApiError err) {
211 AafService aaf = new AafService(ServiceType.AAF_TopicMgr);
213 String instance = ":topic." + client.getFqtn();
214 client.setStatus( DmaapObject_Status.VALID);
215 for( String want : client.getAction() ) {
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);
223 err.setMessage( "Revoke of " + dmaap.getTopicPerm() + "|" + instance + "|" + want + " failed for " + client.getClientRole() );
224 logger.warn( err.getMessage());
230 public MR_Client updateMr_Client( MR_Client client, ApiError apiError ) {
231 MR_Client c = mr_clients.get( client.getMrClientId());
233 apiError.setCode(Status.NOT_FOUND.getStatusCode());
234 apiError.setFields( "mrClientId");
235 apiError.setMessage("mrClientId " + client.getMrClientId() + " not found" );
237 apiError.setCode(200);
239 mr_clients.put( client.getMrClientId(), client );
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" );
251 apiError.setCode(200);
254 if ( updateTopicView == true ) {
256 TopicService topics = new TopicService();
258 Topic t = topics.getTopic(client.getFqtn(), apiError );
260 ArrayList<MR_Client> tc = t.getClients();
261 for( MR_Client c: tc) {
262 if ( c.getMrClientId().equals(client.getMrClientId())) {
268 topics.updateTopic( t, apiError );
275 if ( deleteLevel >= 2 ) {
276 revokeClientPerms( client, apiError );
277 if ( ! apiError.is2xx()) {
282 if ( deleteLevel >= 1 ) {
283 mr_clients.remove(key);