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;
36 public class MR_ClusterService extends BaseLoggingClass {
38 private Map<String, MR_Cluster> mr_clusters = DatabaseClass.getMr_clusters();
40 public Map<String, MR_Cluster> getMR_Clusters() {
44 public List<MR_Cluster> getAllMr_Clusters() {
45 return new ArrayList<MR_Cluster>(mr_clusters.values());
48 public MR_Cluster getMr_Cluster( String key, ApiError apiError ) {
49 MR_Cluster mrc = mr_clusters.get( key );
51 apiError.setCode(Status.NOT_FOUND.getStatusCode());
52 apiError.setFields( "dcaeLocationName");
53 apiError.setMessage( "Cluster with dcaeLocationName " + key + " not found");
55 apiError.setCode(200);
58 public MR_Cluster getMr_ClusterByFQDN( String key ) {
59 for( MR_Cluster cluster: mr_clusters.values() ) {
60 if ( key.equals( cluster.getFqdn() ) ) {
67 public List<MR_Cluster> getCentralClusters() {
68 DcaeLocationService locations = new DcaeLocationService();
69 List<MR_Cluster> result = new ArrayList<MR_Cluster>();
70 for( MR_Cluster c: mr_clusters.values() ) {
71 if ( locations.getDcaeLocation(c.getDcaeLocationName()).isCentral() ) {
78 public MR_Cluster addMr_Cluster( MR_Cluster cluster, ApiError apiError ) {
79 logger.info( "Entry: addMr_Cluster");
80 MR_Cluster mrc = mr_clusters.get( cluster.getDcaeLocationName() );
82 apiError.setCode(Status.CONFLICT.getStatusCode());
83 apiError.setFields( "dcaeLocationName");
84 apiError.setMessage( "Cluster with dcaeLocationName " + cluster.getDcaeLocationName() + " already exists");
88 cluster.setStatus(DmaapObject_Status.VALID);
89 mr_clusters.put( cluster.getDcaeLocationName(), cluster );
90 DcaeLocationService svc = new DcaeLocationService();
91 DcaeLocation loc = svc.getDcaeLocation( cluster.getDcaeLocationName() );
92 if ( loc != null && loc.isCentral() ) {
93 ApiError resp = TopicService.setBridgeClientPerms( cluster );
94 if ( ! resp.is2xx() ) {
95 logger.error( "Unable to provision Bridge to " + cluster.getDcaeLocationName() );
97 cluster.setStatus(DmaapObject_Status.INVALID);
98 mr_clusters.put( cluster.getDcaeLocationName(), cluster );
101 apiError.setCode(200);
105 public MR_Cluster updateMr_Cluster( MR_Cluster cluster, ApiError apiError ) {
106 MR_Cluster mrc = mr_clusters.get( cluster.getDcaeLocationName() );
108 apiError.setCode(Status.NOT_FOUND.getStatusCode());
109 apiError.setFields( "dcaeLocationName");
110 apiError.setMessage( "Cluster with dcaeLocationName " + cluster.getDcaeLocationName() + " not found");
113 cluster.setLastMod();
114 mr_clusters.put( cluster.getDcaeLocationName(), cluster );
115 DcaeLocationService svc = new DcaeLocationService();
116 DcaeLocation loc = svc.getDcaeLocation( cluster.getDcaeLocationName() );
118 logger.error( "DcaeLocation not found for cluster in " + cluster.getDcaeLocationName() );
119 cluster.setLastMod();
120 cluster.setStatus(DmaapObject_Status.INVALID);
121 mr_clusters.put( cluster.getDcaeLocationName(), cluster );
122 } else if ( loc.isCentral() ) {
123 ApiError resp = TopicService.setBridgeClientPerms( cluster );
124 if ( ! resp.is2xx() ) {
125 logger.error( "Unable to provision Bridge to " + cluster.getDcaeLocationName() );
126 cluster.setLastMod();
127 cluster.setStatus(DmaapObject_Status.INVALID);
128 mr_clusters.put( cluster.getDcaeLocationName(), cluster );
131 apiError.setCode(200);
135 public MR_Cluster removeMr_Cluster( String key, ApiError apiError ) {
136 MR_Cluster mrc = mr_clusters.get( key );
138 apiError.setCode(Status.NOT_FOUND.getStatusCode());
139 apiError.setFields( "dcaeLocationName");
140 apiError.setMessage( "Cluster with dcaeLocationName " + key + " not found");
143 apiError.setCode(200);
144 return mr_clusters.remove(key);