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.HashSet;
25 import java.util.List;
29 import javax.ws.rs.core.Response.Status;
31 import org.onap.dmaap.dbcapi.client.MrProvConnection;
32 import org.onap.dmaap.dbcapi.database.DatabaseClass;
33 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
34 import org.onap.dmaap.dbcapi.model.ApiError;
35 import org.onap.dmaap.dbcapi.model.DcaeLocation;
36 import org.onap.dmaap.dbcapi.model.MR_Cluster;
37 import org.onap.dmaap.dbcapi.model.Topic;
38 import org.onap.dmaap.dbcapi.model.DmaapObject.DmaapObject_Status;
39 import org.onap.dmaap.dbcapi.service.DcaeLocationService;
40 import org.onap.dmaap.dbcapi.util.DmaapConfig;
42 public class MR_ClusterService extends BaseLoggingClass {
44 private Map<String, MR_Cluster> mr_clusters = DatabaseClass.getMr_clusters();
45 private boolean multiSite;
47 public MR_ClusterService() {
48 logger.info( "new ClusterService");
49 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
50 multiSite = "true".equalsIgnoreCase(p.getProperty("MR.multisite", "true"));
54 public Map<String, MR_Cluster> getMR_Clusters() {
58 public List<MR_Cluster> getAllMr_Clusters() {
59 return new ArrayList<MR_Cluster>(mr_clusters.values());
62 public MR_Cluster getMr_Cluster( String key, ApiError apiError ) {
63 MR_Cluster mrc = mr_clusters.get( key );
65 apiError.setCode(Status.NOT_FOUND.getStatusCode());
66 apiError.setFields( "dcaeLocationName");
67 apiError.setMessage( "Cluster with dcaeLocationName " + key + " not found");
69 apiError.setCode(200);
72 public MR_Cluster getMr_ClusterByFQDN( String key ) {
73 for( MR_Cluster cluster: mr_clusters.values() ) {
74 if ( key.equals( cluster.getFqdn() ) ) {
81 public MR_Cluster getMr_ClusterByLoc( String loc ) {
82 return mr_clusters.get( loc );
85 public List<MR_Cluster> getCentralClusters() {
86 DcaeLocationService locations = new DcaeLocationService();
87 List<MR_Cluster> result = new ArrayList<MR_Cluster>();
88 for( MR_Cluster c: mr_clusters.values() ) {
90 if ( locations.getDcaeLocation(c.getDcaeLocationName()).isCentral() ) {
93 } catch ( NullPointerException npe ) {
94 logger.warn( "Failed test isCentral for location:" + c.getDcaeLocationName() );
100 // builds the set of unique cluster groups
101 public Set<String> getGroups() {
102 Set<String> result = new HashSet<String>();
103 for( MR_Cluster c: mr_clusters.values() ) {
105 result.add(c.getReplicationGroup());
106 } catch ( NullPointerException npe ) {
107 logger.warn( "Failed to add Group for cluster:" + c.getDcaeLocationName() );
114 public MR_Cluster addMr_Cluster( MR_Cluster cluster, ApiError apiError ) {
115 logger.info( "Entry: addMr_Cluster");
116 MR_Cluster mrc = mr_clusters.get( cluster.getDcaeLocationName() );
118 apiError.setCode(Status.CONFLICT.getStatusCode());
119 apiError.setFields( "dcaeLocationName");
120 apiError.setMessage( "Cluster with dcaeLocationName " + cluster.getDcaeLocationName() + " already exists");
123 cluster.setLastMod();
124 cluster.setStatus( addTopicsToCluster( cluster, apiError ) );
125 mr_clusters.put( cluster.getDcaeLocationName(), cluster );
126 DcaeLocationService svc = new DcaeLocationService();
127 DcaeLocation loc = svc.getDcaeLocation( cluster.getDcaeLocationName() );
128 if ( loc != null && loc.isCentral() && multiSite ) {
129 ApiError resp = TopicService.setBridgeClientPerms( cluster );
130 if ( ! resp.is2xx() ) {
131 logger.error( "Unable to provision Bridge to " + cluster.getDcaeLocationName() );
132 cluster.setLastMod();
133 cluster.setStatus(DmaapObject_Status.INVALID);
134 mr_clusters.put( cluster.getDcaeLocationName(), cluster );
137 apiError.setCode(200);
141 public MR_Cluster updateMr_Cluster( MR_Cluster cluster, ApiError apiError ) {
142 MR_Cluster mrc = mr_clusters.get( cluster.getDcaeLocationName() );
144 apiError.setCode(Status.NOT_FOUND.getStatusCode());
145 apiError.setFields( "dcaeLocationName");
146 apiError.setMessage( "Cluster with dcaeLocationName " + cluster.getDcaeLocationName() + " not found");
149 cluster.setLastMod();
150 cluster.setStatus( addTopicsToCluster( cluster, apiError ) );
151 mr_clusters.put( cluster.getDcaeLocationName(), cluster );
152 DcaeLocationService svc = new DcaeLocationService();
153 DcaeLocation loc = svc.getDcaeLocation( cluster.getDcaeLocationName() );
155 logger.error( "DcaeLocation not found for cluster in " + cluster.getDcaeLocationName() );
156 cluster.setLastMod();
157 cluster.setStatus(DmaapObject_Status.INVALID);
158 mr_clusters.put( cluster.getDcaeLocationName(), cluster );
159 } else if ( loc.isCentral() & multiSite ) {
160 ApiError resp = TopicService.setBridgeClientPerms( cluster );
161 if ( ! resp.is2xx() ) {
162 logger.error( "Unable to provision Bridge to " + cluster.getDcaeLocationName() );
163 cluster.setLastMod();
164 cluster.setStatus(DmaapObject_Status.INVALID);
165 mr_clusters.put( cluster.getDcaeLocationName(), cluster );
169 apiError.setCode(200);
173 public MR_Cluster removeMr_Cluster( String key, ApiError apiError ) {
174 MR_Cluster mrc = mr_clusters.get( key );
176 apiError.setCode(Status.NOT_FOUND.getStatusCode());
177 apiError.setFields( "dcaeLocationName");
178 apiError.setMessage( "Cluster with dcaeLocationName " + key + " not found");
181 apiError.setCode(200);
182 return mr_clusters.remove(key);
185 private DmaapObject_Status addTopicsToCluster( MR_Cluster cluster, ApiError err ){
187 TopicService ts = new TopicService();
188 MrProvConnection prov = new MrProvConnection();
189 List<Topic> topics = ts.getAllTopicsWithoutClients();
190 for( Topic topic: topics ) {
191 logger.info( "POST topic " + topic.getFqtn() + " to cluster " + cluster.getFqdn() + " in loc " + cluster.getDcaeLocationName());
192 if ( prov.makeTopicConnection(cluster)) {
193 String resp = prov.doPostTopic(topic, err);
194 logger.info( "response code: " + err.getCode() );
195 if ( ! err.is2xx() && ! (err.getCode() == 409) ) {
196 return DmaapObject_Status.INVALID;
201 return DmaapObject_Status.VALID;