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.HashSet;
27 import java.util.List;
31 import javax.ws.rs.core.Response.Status;
33 import org.onap.dmaap.dbcapi.client.MrProvConnection;
34 import org.onap.dmaap.dbcapi.database.DatabaseClass;
35 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
36 import org.onap.dmaap.dbcapi.model.ApiError;
37 import org.onap.dmaap.dbcapi.model.DcaeLocation;
38 import org.onap.dmaap.dbcapi.model.MR_Cluster;
39 import org.onap.dmaap.dbcapi.model.Topic;
40 import org.onap.dmaap.dbcapi.model.DmaapObject.DmaapObject_Status;
41 import org.onap.dmaap.dbcapi.service.DcaeLocationService;
42 import org.onap.dmaap.dbcapi.util.DmaapConfig;
44 public class MR_ClusterService extends BaseLoggingClass {
46 private Map<String, MR_Cluster> mr_clusters = DatabaseClass.getMr_clusters();
47 private boolean multiSite;
49 public MR_ClusterService() {
50 logger.info( "new ClusterService");
51 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
52 multiSite = "true".equalsIgnoreCase(p.getProperty("MR.multisite", "true"));
56 public Map<String, MR_Cluster> getMR_Clusters() {
60 public List<MR_Cluster> getAllMr_Clusters() {
61 return new ArrayList<MR_Cluster>(mr_clusters.values());
64 public MR_Cluster getMr_Cluster( String key, ApiError apiError ) {
65 MR_Cluster mrc = mr_clusters.get( key );
67 apiError.setCode(Status.NOT_FOUND.getStatusCode());
68 apiError.setFields( "dcaeLocationName");
69 apiError.setMessage( "Cluster with dcaeLocationName " + key + " not found");
71 apiError.setCode(200);
74 public MR_Cluster getMr_ClusterByFQDN( String key ) {
75 for( MR_Cluster cluster: mr_clusters.values() ) {
76 if ( key.equals( cluster.getFqdn() ) ) {
83 public MR_Cluster getMr_ClusterByLoc( String loc ) {
84 return mr_clusters.get( loc );
87 public List<MR_Cluster> getCentralClusters() {
88 DcaeLocationService locations = new DcaeLocationService();
89 List<MR_Cluster> result = new ArrayList<MR_Cluster>();
90 for( MR_Cluster c: mr_clusters.values() ) {
92 if ( locations.getDcaeLocation(c.getDcaeLocationName()).isCentral() ) {
95 } catch ( NullPointerException npe ) {
96 logger.warn( "Failed test isCentral for location:" + c.getDcaeLocationName() );
102 // builds the set of unique cluster groups
103 public Set<String> getGroups() {
104 Set<String> result = new HashSet<String>();
105 for( MR_Cluster c: mr_clusters.values() ) {
107 result.add(c.getReplicationGroup());
108 } catch ( NullPointerException npe ) {
109 logger.warn( "Failed to add Group for cluster:" + c.getDcaeLocationName() );
116 public MR_Cluster addMr_Cluster( MR_Cluster cluster, ApiError apiError ) {
117 logger.info( "Entry: addMr_Cluster");
118 MR_Cluster mrc = mr_clusters.get( cluster.getDcaeLocationName() );
120 apiError.setCode(Status.CONFLICT.getStatusCode());
121 apiError.setFields( "dcaeLocationName");
122 apiError.setMessage( "Cluster with dcaeLocationName " + cluster.getDcaeLocationName() + " already exists");
125 cluster.setLastMod();
126 cluster.setStatus( addTopicsToCluster( cluster, apiError ) );
127 mr_clusters.put( cluster.getDcaeLocationName(), cluster );
128 DcaeLocationService svc = new DcaeLocationService();
129 DcaeLocation loc = svc.getDcaeLocation( cluster.getDcaeLocationName() );
130 if ( loc != null && loc.isCentral() && multiSite ) {
131 ApiError resp = TopicService.setBridgeClientPerms( cluster );
132 if ( ! resp.is2xx() ) {
133 logger.error( "Unable to provision Bridge to " + cluster.getDcaeLocationName() );
134 cluster.setLastMod();
135 cluster.setStatus(DmaapObject_Status.INVALID);
136 mr_clusters.put( cluster.getDcaeLocationName(), cluster );
139 apiError.setCode(200);
143 public MR_Cluster updateMr_Cluster( MR_Cluster cluster, ApiError apiError ) {
144 MR_Cluster mrc = mr_clusters.get( cluster.getDcaeLocationName() );
146 apiError.setCode(Status.NOT_FOUND.getStatusCode());
147 apiError.setFields( "dcaeLocationName");
148 apiError.setMessage( "Cluster with dcaeLocationName " + cluster.getDcaeLocationName() + " not found");
151 cluster.setLastMod();
152 cluster.setStatus( addTopicsToCluster( cluster, apiError ) );
153 mr_clusters.put( cluster.getDcaeLocationName(), cluster );
154 DcaeLocationService svc = new DcaeLocationService();
155 DcaeLocation loc = svc.getDcaeLocation( cluster.getDcaeLocationName() );
157 logger.error( "DcaeLocation not found for cluster in " + cluster.getDcaeLocationName() );
158 cluster.setLastMod();
159 cluster.setStatus(DmaapObject_Status.INVALID);
160 mr_clusters.put( cluster.getDcaeLocationName(), cluster );
161 } else if ( loc.isCentral() && multiSite ) {
162 ApiError resp = TopicService.setBridgeClientPerms( cluster );
163 if ( ! resp.is2xx() ) {
164 logger.error( "Unable to provision Bridge to " + cluster.getDcaeLocationName() );
165 cluster.setLastMod();
166 cluster.setStatus(DmaapObject_Status.INVALID);
167 mr_clusters.put( cluster.getDcaeLocationName(), cluster );
171 apiError.setCode(200);
175 public MR_Cluster removeMr_Cluster( String key, ApiError apiError ) {
176 MR_Cluster mrc = mr_clusters.get( key );
178 apiError.setCode(Status.NOT_FOUND.getStatusCode());
179 apiError.setFields( "dcaeLocationName");
180 apiError.setMessage( "Cluster with dcaeLocationName " + key + " not found");
183 apiError.setCode(200);
184 return mr_clusters.remove(key);
187 private DmaapObject_Status addTopicsToCluster( MR_Cluster cluster, ApiError err ){
189 TopicService ts = new TopicService();
190 MrProvConnection prov = new MrProvConnection();
191 List<Topic> topics = ts.getAllTopicsWithoutClients();
192 for( Topic topic: topics ) {
193 logger.info( "POST topic " + topic.getFqtn() + " to cluster " + cluster.getFqdn() + " in loc " + cluster.getDcaeLocationName());
194 if ( prov.makeTopicConnection(cluster)) {
195 String resp = prov.doPostTopic(topic, err);
196 logger.info( "response code: " + err.getCode() );
197 if ( ! err.is2xx() && ! (err.getCode() == 409) ) {
198 return DmaapObject_Status.INVALID;
203 return DmaapObject_Status.VALID;