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.database.DatabaseClass;
32 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
33 import org.onap.dmaap.dbcapi.model.ApiError;
34 import org.onap.dmaap.dbcapi.model.DcaeLocation;
35 import org.onap.dmaap.dbcapi.model.MR_Cluster;
36 import org.onap.dmaap.dbcapi.model.DmaapObject.DmaapObject_Status;
37 import org.onap.dmaap.dbcapi.service.DcaeLocationService;
38 import org.onap.dmaap.dbcapi.util.DmaapConfig;
40 public class MR_ClusterService extends BaseLoggingClass {
42 private Map<String, MR_Cluster> mr_clusters = DatabaseClass.getMr_clusters();
43 private boolean multiSite;
45 public MR_ClusterService() {
46 logger.info( "new ClusterService");
47 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
48 multiSite = "true".equalsIgnoreCase(p.getProperty("MR.multisite", "true"));
52 public Map<String, MR_Cluster> getMR_Clusters() {
56 public List<MR_Cluster> getAllMr_Clusters() {
57 return new ArrayList<MR_Cluster>(mr_clusters.values());
60 public MR_Cluster getMr_Cluster( String key, ApiError apiError ) {
61 MR_Cluster mrc = mr_clusters.get( key );
63 apiError.setCode(Status.NOT_FOUND.getStatusCode());
64 apiError.setFields( "dcaeLocationName");
65 apiError.setMessage( "Cluster with dcaeLocationName " + key + " not found");
67 apiError.setCode(200);
70 public MR_Cluster getMr_ClusterByFQDN( String key ) {
71 for( MR_Cluster cluster: mr_clusters.values() ) {
72 if ( key.equals( cluster.getFqdn() ) ) {
79 public MR_Cluster getMr_ClusterByLoc( String loc ) {
80 return mr_clusters.get( loc );
83 public List<MR_Cluster> getCentralClusters() {
84 DcaeLocationService locations = new DcaeLocationService();
85 List<MR_Cluster> result = new ArrayList<MR_Cluster>();
86 for( MR_Cluster c: mr_clusters.values() ) {
88 if ( locations.getDcaeLocation(c.getDcaeLocationName()).isCentral() ) {
91 } catch ( NullPointerException npe ) {
92 logger.warn( "Failed test isCentral for location:" + c.getDcaeLocationName() );
98 // builds the set of unique cluster groups
99 public Set<String> getGroups() {
100 Set<String> result = new HashSet<String>();
101 for( MR_Cluster c: mr_clusters.values() ) {
103 result.add(c.getReplicationGroup());
104 } catch ( NullPointerException npe ) {
105 logger.warn( "Failed to add Group for cluster:" + c.getDcaeLocationName() );
112 public MR_Cluster addMr_Cluster( MR_Cluster cluster, ApiError apiError ) {
113 logger.info( "Entry: addMr_Cluster");
114 MR_Cluster mrc = mr_clusters.get( cluster.getDcaeLocationName() );
116 apiError.setCode(Status.CONFLICT.getStatusCode());
117 apiError.setFields( "dcaeLocationName");
118 apiError.setMessage( "Cluster with dcaeLocationName " + cluster.getDcaeLocationName() + " already exists");
121 cluster.setLastMod();
122 cluster.setStatus(DmaapObject_Status.VALID);
123 mr_clusters.put( cluster.getDcaeLocationName(), cluster );
124 DcaeLocationService svc = new DcaeLocationService();
125 DcaeLocation loc = svc.getDcaeLocation( cluster.getDcaeLocationName() );
126 if ( loc != null && loc.isCentral() && multiSite ) {
127 ApiError resp = TopicService.setBridgeClientPerms( cluster );
128 if ( ! resp.is2xx() ) {
129 logger.error( "Unable to provision Bridge to " + cluster.getDcaeLocationName() );
130 cluster.setLastMod();
131 cluster.setStatus(DmaapObject_Status.INVALID);
132 mr_clusters.put( cluster.getDcaeLocationName(), cluster );
135 apiError.setCode(200);
139 public MR_Cluster updateMr_Cluster( MR_Cluster cluster, ApiError apiError ) {
140 MR_Cluster mrc = mr_clusters.get( cluster.getDcaeLocationName() );
142 apiError.setCode(Status.NOT_FOUND.getStatusCode());
143 apiError.setFields( "dcaeLocationName");
144 apiError.setMessage( "Cluster with dcaeLocationName " + cluster.getDcaeLocationName() + " not found");
147 cluster.setLastMod();
148 mr_clusters.put( cluster.getDcaeLocationName(), cluster );
149 DcaeLocationService svc = new DcaeLocationService();
150 DcaeLocation loc = svc.getDcaeLocation( cluster.getDcaeLocationName() );
152 logger.error( "DcaeLocation not found for cluster in " + cluster.getDcaeLocationName() );
153 cluster.setLastMod();
154 cluster.setStatus(DmaapObject_Status.INVALID);
155 mr_clusters.put( cluster.getDcaeLocationName(), cluster );
156 } else if ( loc.isCentral() & multiSite ) {
157 ApiError resp = TopicService.setBridgeClientPerms( cluster );
158 if ( ! resp.is2xx() ) {
159 logger.error( "Unable to provision Bridge to " + cluster.getDcaeLocationName() );
160 cluster.setLastMod();
161 cluster.setStatus(DmaapObject_Status.INVALID);
162 mr_clusters.put( cluster.getDcaeLocationName(), cluster );
165 apiError.setCode(200);
169 public MR_Cluster removeMr_Cluster( String key, ApiError apiError ) {
170 MR_Cluster mrc = mr_clusters.get( key );
172 apiError.setCode(Status.NOT_FOUND.getStatusCode());
173 apiError.setFields( "dcaeLocationName");
174 apiError.setMessage( "Cluster with dcaeLocationName " + key + " not found");
177 apiError.setCode(200);
178 return mr_clusters.remove(key);