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() ) {
81 if ( locations.getDcaeLocation(c.getDcaeLocationName()).isCentral() ) {
84 } catch ( NullPointerException npe ) {
85 logger.warn( "Failed test isCentral for location:" + c.getDcaeLocationName() );
91 public MR_Cluster addMr_Cluster( MR_Cluster cluster, ApiError apiError ) {
92 logger.info( "Entry: addMr_Cluster");
93 MR_Cluster mrc = mr_clusters.get( cluster.getDcaeLocationName() );
95 apiError.setCode(Status.CONFLICT.getStatusCode());
96 apiError.setFields( "dcaeLocationName");
97 apiError.setMessage( "Cluster with dcaeLocationName " + cluster.getDcaeLocationName() + " already exists");
100 cluster.setLastMod();
101 cluster.setStatus(DmaapObject_Status.VALID);
102 mr_clusters.put( cluster.getDcaeLocationName(), cluster );
103 DcaeLocationService svc = new DcaeLocationService();
104 DcaeLocation loc = svc.getDcaeLocation( cluster.getDcaeLocationName() );
105 if ( loc != null && loc.isCentral() && multiSite ) {
106 ApiError resp = TopicService.setBridgeClientPerms( cluster );
107 if ( ! resp.is2xx() ) {
108 logger.error( "Unable to provision Bridge to " + cluster.getDcaeLocationName() );
109 cluster.setLastMod();
110 cluster.setStatus(DmaapObject_Status.INVALID);
111 mr_clusters.put( cluster.getDcaeLocationName(), cluster );
114 apiError.setCode(200);
118 public MR_Cluster updateMr_Cluster( MR_Cluster cluster, ApiError apiError ) {
119 MR_Cluster mrc = mr_clusters.get( cluster.getDcaeLocationName() );
121 apiError.setCode(Status.NOT_FOUND.getStatusCode());
122 apiError.setFields( "dcaeLocationName");
123 apiError.setMessage( "Cluster with dcaeLocationName " + cluster.getDcaeLocationName() + " not found");
126 cluster.setLastMod();
127 mr_clusters.put( cluster.getDcaeLocationName(), cluster );
128 DcaeLocationService svc = new DcaeLocationService();
129 DcaeLocation loc = svc.getDcaeLocation( cluster.getDcaeLocationName() );
131 logger.error( "DcaeLocation not found for cluster in " + cluster.getDcaeLocationName() );
132 cluster.setLastMod();
133 cluster.setStatus(DmaapObject_Status.INVALID);
134 mr_clusters.put( cluster.getDcaeLocationName(), cluster );
135 } else if ( loc.isCentral() & multiSite ) {
136 ApiError resp = TopicService.setBridgeClientPerms( cluster );
137 if ( ! resp.is2xx() ) {
138 logger.error( "Unable to provision Bridge to " + cluster.getDcaeLocationName() );
139 cluster.setLastMod();
140 cluster.setStatus(DmaapObject_Status.INVALID);
141 mr_clusters.put( cluster.getDcaeLocationName(), cluster );
144 apiError.setCode(200);
148 public MR_Cluster removeMr_Cluster( String key, ApiError apiError ) {
149 MR_Cluster mrc = mr_clusters.get( key );
151 apiError.setCode(Status.NOT_FOUND.getStatusCode());
152 apiError.setFields( "dcaeLocationName");
153 apiError.setMessage( "Cluster with dcaeLocationName " + key + " not found");
156 apiError.setCode(200);
157 return mr_clusters.remove(key);