Alternative MR replication method
[dmaap/dbcapi.git] / src / main / java / org / onap / dmaap / dbcapi / service / MR_ClusterService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dmaap
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.dmaap.dbcapi.service;
22
23 import java.util.ArrayList;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Set;
28
29 import javax.ws.rs.core.Response.Status;
30
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;
39
40 public class MR_ClusterService extends BaseLoggingClass {
41
42         private Map<String, MR_Cluster> mr_clusters = DatabaseClass.getMr_clusters();
43         private boolean multiSite;
44         
45         public MR_ClusterService() {
46                 logger.info( "new ClusterService");
47                 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
48                 multiSite = "true".equalsIgnoreCase(p.getProperty("MR.multisite", "true"));
49                                 
50         }
51         
52         public Map<String, MR_Cluster> getMR_Clusters() {                       
53                 return mr_clusters;
54         }
55                 
56         public List<MR_Cluster> getAllMr_Clusters() {
57                 return new ArrayList<MR_Cluster>(mr_clusters.values());
58         }
59                 
60         public MR_Cluster getMr_Cluster( String key, ApiError apiError ) {                      
61                 MR_Cluster mrc = mr_clusters.get( key );
62                 if ( mrc == null ) {
63                         apiError.setCode(Status.NOT_FOUND.getStatusCode());
64                         apiError.setFields( "dcaeLocationName");
65                         apiError.setMessage( "Cluster with dcaeLocationName " + key + " not found");
66                 }
67                 apiError.setCode(200);
68                 return mrc;
69         }
70         public MR_Cluster getMr_ClusterByFQDN( String key ) {           
71                 for( MR_Cluster cluster: mr_clusters.values() ) {
72                         if ( key.equals( cluster.getFqdn() ) ) {
73                                 return cluster;
74                         }
75                 }
76                 return null;
77         }
78         
79         public MR_Cluster getMr_ClusterByLoc( String loc ) {
80                 return mr_clusters.get( loc );
81         }
82         
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() ) {
87                         try {
88                                 if ( locations.getDcaeLocation(c.getDcaeLocationName()).isCentral() ) {
89                                         result.add(c);
90                                 }
91                         } catch ( NullPointerException npe ) {
92                                 logger.warn( "Failed test isCentral for location:" + c.getDcaeLocationName() );
93                         }
94                 }
95                 return result;
96         }       
97         
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() ) {
102                         try {
103                                 result.add(c.getReplicationGroup());
104                         } catch ( NullPointerException npe ) {
105                                 logger.warn( "Failed to add Group for cluster:" + c.getDcaeLocationName() );
106                         }
107                 }
108                 return result;
109         }       
110
111
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() );
115                 if ( mrc != null ) {
116                         apiError.setCode(Status.CONFLICT.getStatusCode());
117                         apiError.setFields( "dcaeLocationName");
118                         apiError.setMessage( "Cluster with dcaeLocationName " + cluster.getDcaeLocationName() + " already exists");
119                         return null;
120                 }
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 );
133                         }
134                 }
135                 apiError.setCode(200);
136                 return cluster;
137         }
138                 
139         public MR_Cluster updateMr_Cluster( MR_Cluster cluster, ApiError apiError ) {
140                 MR_Cluster mrc = mr_clusters.get( cluster.getDcaeLocationName() );
141                 if ( mrc == null ) {
142                         apiError.setCode(Status.NOT_FOUND.getStatusCode());
143                         apiError.setFields( "dcaeLocationName");
144                         apiError.setMessage( "Cluster with dcaeLocationName " + cluster.getDcaeLocationName() + " not found");
145                         return null;
146                 }
147                 cluster.setLastMod();
148                 mr_clusters.put( cluster.getDcaeLocationName(), cluster );
149                 DcaeLocationService svc = new DcaeLocationService();
150                 DcaeLocation loc = svc.getDcaeLocation( cluster.getDcaeLocationName() );
151                 if ( loc == null ) {
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 );
163                         }
164                 }
165                 apiError.setCode(200);
166                 return cluster;
167         }
168                 
169         public MR_Cluster removeMr_Cluster( String key, ApiError apiError ) {
170                 MR_Cluster mrc = mr_clusters.get( key );
171                 if ( mrc == null ) {
172                         apiError.setCode(Status.NOT_FOUND.getStatusCode());
173                         apiError.setFields( "dcaeLocationName");
174                         apiError.setMessage( "Cluster with dcaeLocationName " + key + " not found");
175                         return null;
176                 }
177                 apiError.setCode(200);
178                 return mr_clusters.remove(key);
179         }       
180         
181
182 }