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;
29 import org.onap.dmaap.dbcapi.database.DatabaseClass;
30 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
31 import org.onap.dmaap.dbcapi.model.ApiError;
32 import org.onap.dmaap.dbcapi.model.DcaeLocation;
33 import org.onap.dmaap.dbcapi.model.MR_Cluster;
34 import org.onap.dmaap.dbcapi.model.DmaapObject.DmaapObject_Status;
35 import org.onap.dmaap.dbcapi.util.DmaapConfig;
37 public class MR_ClusterService extends BaseLoggingClass {
39 private Map<String, MR_Cluster> mr_clusters = DatabaseClass.getMr_clusters();
40 private boolean multiSite;
42 public MR_ClusterService() {
43 logger.info( "new ClusterService");
44 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
45 multiSite = "true".equalsIgnoreCase(p.getProperty("MR.multisite", "true"));
49 public Map<String, MR_Cluster> getMR_Clusters() {
53 public List<MR_Cluster> getAllMr_Clusters() {
54 return new ArrayList<MR_Cluster>(mr_clusters.values());
57 public MR_Cluster getMr_Cluster( String key, ApiError apiError ) {
58 MR_Cluster mrc = mr_clusters.get( key );
60 apiError.setCode(Status.NOT_FOUND.getStatusCode());
61 apiError.setFields( "dcaeLocationName");
62 apiError.setMessage( "Cluster with dcaeLocationName " + key + " not found");
64 apiError.setCode(200);
67 public MR_Cluster getMr_ClusterByFQDN( String key ) {
68 for( MR_Cluster cluster: mr_clusters.values() ) {
69 if ( key.equals( cluster.getFqdn() ) ) {
76 public List<MR_Cluster> getCentralClusters() {
77 DcaeLocationService locations = new DcaeLocationService();
78 List<MR_Cluster> result = new ArrayList<MR_Cluster>();
79 for( MR_Cluster c: mr_clusters.values() ) {
80 if ( locations.getDcaeLocation(c.getDcaeLocationName()).isCentral() ) {
87 public MR_Cluster addMr_Cluster( MR_Cluster cluster, ApiError apiError ) {
88 logger.info( "Entry: addMr_Cluster");
89 MR_Cluster mrc = mr_clusters.get( cluster.getDcaeLocationName() );
91 apiError.setCode(Status.CONFLICT.getStatusCode());
92 apiError.setFields( "dcaeLocationName");
93 apiError.setMessage( "Cluster with dcaeLocationName " + cluster.getDcaeLocationName() + " already exists");
97 cluster.setStatus(DmaapObject_Status.VALID);
98 mr_clusters.put( cluster.getDcaeLocationName(), cluster );
99 DcaeLocationService svc = new DcaeLocationService();
100 DcaeLocation loc = svc.getDcaeLocation( cluster.getDcaeLocationName() );
101 if ( loc != null && loc.isCentral() && multiSite ) {
102 ApiError resp = TopicService.setBridgeClientPerms( cluster );
103 if ( ! resp.is2xx() ) {
104 logger.error( "Unable to provision Bridge to " + cluster.getDcaeLocationName() );
105 cluster.setLastMod();
106 cluster.setStatus(DmaapObject_Status.INVALID);
107 mr_clusters.put( cluster.getDcaeLocationName(), cluster );
110 apiError.setCode(200);
114 public MR_Cluster updateMr_Cluster( MR_Cluster cluster, ApiError apiError ) {
115 MR_Cluster mrc = mr_clusters.get( cluster.getDcaeLocationName() );
117 apiError.setCode(Status.NOT_FOUND.getStatusCode());
118 apiError.setFields( "dcaeLocationName");
119 apiError.setMessage( "Cluster with dcaeLocationName " + cluster.getDcaeLocationName() + " not found");
122 cluster.setLastMod();
123 mr_clusters.put( cluster.getDcaeLocationName(), cluster );
124 DcaeLocationService svc = new DcaeLocationService();
125 DcaeLocation loc = svc.getDcaeLocation( cluster.getDcaeLocationName() );
127 logger.error( "DcaeLocation not found for cluster in " + cluster.getDcaeLocationName() );
128 cluster.setLastMod();
129 cluster.setStatus(DmaapObject_Status.INVALID);
130 mr_clusters.put( cluster.getDcaeLocationName(), cluster );
131 } else if ( loc.isCentral() & multiSite ) {
132 ApiError resp = TopicService.setBridgeClientPerms( cluster );
133 if ( ! resp.is2xx() ) {
134 logger.error( "Unable to provision Bridge to " + cluster.getDcaeLocationName() );
135 cluster.setLastMod();
136 cluster.setStatus(DmaapObject_Status.INVALID);
137 mr_clusters.put( cluster.getDcaeLocationName(), cluster );
140 apiError.setCode(200);
144 public MR_Cluster removeMr_Cluster( String key, ApiError apiError ) {
145 MR_Cluster mrc = mr_clusters.get( key );
147 apiError.setCode(Status.NOT_FOUND.getStatusCode());
148 apiError.setFields( "dcaeLocationName");
149 apiError.setMessage( "Cluster with dcaeLocationName " + key + " not found");
152 apiError.setCode(200);
153 return mr_clusters.remove(key);