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;
35 import org.onap.dmaap.dbcapi.aaf.database.DatabaseClass;
36 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
37 import org.onap.dmaap.dbcapi.model.ApiError;
38 import org.onap.dmaap.dbcapi.model.DcaeLocation;
39 import org.onap.dmaap.dbcapi.model.MR_Cluster;
40 import org.onap.dmaap.dbcapi.model.DmaapObject.DmaapObject_Status;
42 public class MR_ClusterService extends BaseLoggingClass {
44 private Map<String, MR_Cluster> mr_clusters = DatabaseClass.getMr_clusters();
46 public Map<String, MR_Cluster> getMR_Clusters() {
50 public List<MR_Cluster> getAllMr_Clusters() {
51 return new ArrayList<MR_Cluster>(mr_clusters.values());
54 public MR_Cluster getMr_Cluster( String key, ApiError apiError ) {
55 MR_Cluster mrc = mr_clusters.get( key );
57 apiError.setCode(Status.NOT_FOUND.getStatusCode());
58 apiError.setFields( "dcaeLocationName");
59 apiError.setMessage( "Cluster with dcaeLocationName " + key + " not found");
61 apiError.setCode(200);
64 public MR_Cluster getMr_ClusterByFQDN( String key ) {
65 for( MR_Cluster cluster: mr_clusters.values() ) {
66 if ( key.equals( cluster.getFqdn() ) ) {
73 public List<MR_Cluster> getCentralClusters() {
74 DcaeLocationService locations = new DcaeLocationService();
75 List<MR_Cluster> result = new ArrayList<MR_Cluster>();
76 for( MR_Cluster c: mr_clusters.values() ) {
77 if ( locations.getDcaeLocation(c.getDcaeLocationName()).isCentral() ) {
84 public MR_Cluster addMr_Cluster( MR_Cluster cluster, ApiError apiError ) {
85 logger.info( "Entry: addMr_Cluster");
86 MR_Cluster mrc = mr_clusters.get( cluster.getDcaeLocationName() );
88 apiError.setCode(Status.CONFLICT.getStatusCode());
89 apiError.setFields( "dcaeLocationName");
90 apiError.setMessage( "Cluster with dcaeLocationName " + cluster.getDcaeLocationName() + " already exists");
94 cluster.setStatus(DmaapObject_Status.VALID);
95 mr_clusters.put( cluster.getDcaeLocationName(), cluster );
96 DcaeLocationService svc = new DcaeLocationService();
97 DcaeLocation loc = svc.getDcaeLocation( cluster.getDcaeLocationName() );
98 if ( loc != null && loc.isCentral() ) {
99 ApiError resp = TopicService.setBridgeClientPerms( cluster );
100 if ( ! resp.is2xx() ) {
101 logger.error( "Unable to provision Bridge to " + cluster.getDcaeLocationName() );
102 cluster.setLastMod();
103 cluster.setStatus(DmaapObject_Status.INVALID);
104 mr_clusters.put( cluster.getDcaeLocationName(), cluster );
107 apiError.setCode(200);
111 public MR_Cluster updateMr_Cluster( MR_Cluster cluster, ApiError apiError ) {
112 MR_Cluster mrc = mr_clusters.get( cluster.getDcaeLocationName() );
114 apiError.setCode(Status.NOT_FOUND.getStatusCode());
115 apiError.setFields( "dcaeLocationName");
116 apiError.setMessage( "Cluster with dcaeLocationName " + cluster.getDcaeLocationName() + " not found");
119 cluster.setLastMod();
120 mr_clusters.put( cluster.getDcaeLocationName(), cluster );
121 DcaeLocationService svc = new DcaeLocationService();
122 DcaeLocation loc = svc.getDcaeLocation( cluster.getDcaeLocationName() );
124 logger.error( "DcaeLocation not found for cluster in " + cluster.getDcaeLocationName() );
125 cluster.setLastMod();
126 cluster.setStatus(DmaapObject_Status.INVALID);
127 mr_clusters.put( cluster.getDcaeLocationName(), cluster );
128 } else if ( loc.isCentral() ) {
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 removeMr_Cluster( String key, ApiError apiError ) {
142 MR_Cluster mrc = mr_clusters.get( key );
144 apiError.setCode(Status.NOT_FOUND.getStatusCode());
145 apiError.setFields( "dcaeLocationName");
146 apiError.setMessage( "Cluster with dcaeLocationName " + key + " not found");
149 apiError.setCode(200);
150 return mr_clusters.remove(key);