[DMAAP-BC] Consolidate bus controller repos
[dmaap/buscontroller.git] / dmaap-bc / 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  * Modifications Copyright (C) 2019 IBM.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.dmaap.dbcapi.service;
24
25 import java.util.ArrayList;
26 import java.util.HashSet;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Set;
30
31 import javax.ws.rs.core.Response.Status;
32
33 import org.onap.dmaap.dbcapi.client.MrProvConnection;
34 import org.onap.dmaap.dbcapi.database.DatabaseClass;
35 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
36 import org.onap.dmaap.dbcapi.model.ApiError;
37 import org.onap.dmaap.dbcapi.model.DcaeLocation;
38 import org.onap.dmaap.dbcapi.model.MR_Cluster;
39 import org.onap.dmaap.dbcapi.model.Topic;
40 import org.onap.dmaap.dbcapi.model.DmaapObject.DmaapObject_Status;
41 import org.onap.dmaap.dbcapi.service.DcaeLocationService;
42 import org.onap.dmaap.dbcapi.util.DmaapConfig;
43
44 public class MR_ClusterService extends BaseLoggingClass {
45
46         private Map<String, MR_Cluster> mr_clusters = DatabaseClass.getMr_clusters();
47         private boolean multiSite;
48         
49         public MR_ClusterService() {
50                 logger.info( "new ClusterService");
51                 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
52                 multiSite = "true".equalsIgnoreCase(p.getProperty("MR.multisite", "true"));
53                                 
54         }
55         
56         public Map<String, MR_Cluster> getMR_Clusters() {                       
57                 return mr_clusters;
58         }
59                 
60         public List<MR_Cluster> getAllMr_Clusters() {
61                 return new ArrayList<MR_Cluster>(mr_clusters.values());
62         }
63                 
64         public MR_Cluster getMr_Cluster( String key, ApiError apiError ) {                      
65                 MR_Cluster mrc = mr_clusters.get( key );
66                 if ( mrc == null ) {
67                         apiError.setCode(Status.NOT_FOUND.getStatusCode());
68                         apiError.setFields( "dcaeLocationName");
69                         apiError.setMessage( "Cluster with dcaeLocationName " + key + " not found");
70                 }
71                 apiError.setCode(200);
72                 return mrc;
73         }
74         public MR_Cluster getMr_ClusterByFQDN( String key ) {           
75                 for( MR_Cluster cluster: mr_clusters.values() ) {
76                         if ( key.equals( cluster.getFqdn() ) ) {
77                                 return cluster;
78                         }
79                 }
80                 return null;
81         }
82         
83         public MR_Cluster getMr_ClusterByLoc( String loc ) {
84                 return mr_clusters.get( loc );
85         }
86         
87         public List<MR_Cluster> getCentralClusters() {
88                 DcaeLocationService locations = new DcaeLocationService();
89                 List<MR_Cluster> result = new ArrayList<MR_Cluster>();
90                 for( MR_Cluster c: mr_clusters.values() ) {
91                         try {
92                                 if ( locations.getDcaeLocation(c.getDcaeLocationName()).isCentral() ) {
93                                         result.add(c);
94                                 }
95                         } catch ( NullPointerException npe ) {
96                                 logger.warn( "Failed test isCentral for location:" + c.getDcaeLocationName() );
97                         }
98                 }
99                 return result;
100         }       
101         
102         // builds the set of unique cluster groups
103         public Set<String> getGroups() {
104                 Set<String> result = new HashSet<String>();
105                 for( MR_Cluster c: mr_clusters.values() ) {
106                         try {
107                                 result.add(c.getReplicationGroup());
108                         } catch ( NullPointerException npe ) {
109                                 logger.warn( "Failed to add Group for cluster:" + c.getDcaeLocationName() );
110                         }
111                 }
112                 return result;
113         }       
114
115
116         public MR_Cluster addMr_Cluster( MR_Cluster cluster, ApiError apiError ) {
117                 logger.info( "Entry: addMr_Cluster");
118                 MR_Cluster mrc = mr_clusters.get( cluster.getDcaeLocationName() );
119                 if ( mrc != null ) {
120                         apiError.setCode(Status.CONFLICT.getStatusCode());
121                         apiError.setFields( "dcaeLocationName");
122                         apiError.setMessage( "Cluster with dcaeLocationName " + cluster.getDcaeLocationName() + " already exists");
123                         return null;
124                 }
125                 cluster.setLastMod();
126                 cluster.setStatus( addTopicsToCluster( cluster, apiError ) );
127                 mr_clusters.put( cluster.getDcaeLocationName(), cluster );
128                 DcaeLocationService svc = new DcaeLocationService();
129                 DcaeLocation loc = svc.getDcaeLocation( cluster.getDcaeLocationName() );
130                 if ( loc != null && loc.isCentral() && multiSite ) {
131                         ApiError resp = TopicService.setBridgeClientPerms( cluster );
132                         if ( ! resp.is2xx() ) {
133                                 logger.error( "Unable to provision Bridge to " + cluster.getDcaeLocationName() );
134                                 cluster.setLastMod();
135                                 cluster.setStatus(DmaapObject_Status.INVALID);
136                                 mr_clusters.put( cluster.getDcaeLocationName(), cluster );
137                         }
138                 }
139                 apiError.setCode(200);
140                 return cluster;
141         }
142                 
143         public MR_Cluster updateMr_Cluster( MR_Cluster cluster, ApiError apiError ) {
144                 MR_Cluster mrc = mr_clusters.get( cluster.getDcaeLocationName() );
145                 if ( mrc == null ) {
146                         apiError.setCode(Status.NOT_FOUND.getStatusCode());
147                         apiError.setFields( "dcaeLocationName");
148                         apiError.setMessage( "Cluster with dcaeLocationName " + cluster.getDcaeLocationName() + " not found");
149                         return null;
150                 }
151                 cluster.setLastMod();
152                 cluster.setStatus( addTopicsToCluster( cluster, apiError ) );
153                 mr_clusters.put( cluster.getDcaeLocationName(), cluster );
154                 DcaeLocationService svc = new DcaeLocationService();
155                 DcaeLocation loc = svc.getDcaeLocation( cluster.getDcaeLocationName() );
156                 if ( loc == null ) {
157                         logger.error( "DcaeLocation not found for cluster in " + cluster.getDcaeLocationName() );
158                         cluster.setLastMod();
159                         cluster.setStatus(DmaapObject_Status.INVALID);
160                         mr_clusters.put( cluster.getDcaeLocationName(), cluster );
161                 } else if ( loc.isCentral() && multiSite ) {
162                         ApiError resp = TopicService.setBridgeClientPerms( cluster );
163                         if ( ! resp.is2xx() ) {
164                                 logger.error( "Unable to provision Bridge to " + cluster.getDcaeLocationName() );
165                                 cluster.setLastMod();
166                                 cluster.setStatus(DmaapObject_Status.INVALID);
167                                 mr_clusters.put( cluster.getDcaeLocationName(), cluster );
168                         }
169                 }
170                 
171                 apiError.setCode(200);
172                 return cluster;
173         }
174                 
175         public MR_Cluster removeMr_Cluster( String key, ApiError apiError ) {
176                 MR_Cluster mrc = mr_clusters.get( key );
177                 if ( mrc == null ) {
178                         apiError.setCode(Status.NOT_FOUND.getStatusCode());
179                         apiError.setFields( "dcaeLocationName");
180                         apiError.setMessage( "Cluster with dcaeLocationName " + key + " not found");
181                         return null;
182                 }
183                 apiError.setCode(200);
184                 return mr_clusters.remove(key);
185         }       
186         
187         private DmaapObject_Status addTopicsToCluster( MR_Cluster cluster, ApiError err  ){
188                 
189                 TopicService ts = new TopicService();
190                 MrProvConnection prov = new MrProvConnection();
191                 List<Topic>  topics = ts.getAllTopicsWithoutClients();
192                 for( Topic topic: topics ) {
193                         logger.info( "POST topic " + topic.getFqtn() + " to cluster " + cluster.getFqdn() + " in loc " + cluster.getDcaeLocationName());
194                         if ( prov.makeTopicConnection(cluster)) {
195                                 String resp = prov.doPostTopic(topic, err);
196                                 logger.info( "response code: " + err.getCode() );
197                                 if ( ! err.is2xx() && ! (err.getCode() == 409) ) {
198                                         return DmaapObject_Status.INVALID;
199                                 } 
200                         }
201                 }
202                 
203                 return DmaapObject_Status.VALID;
204         }
205 }