Provision topics on mr_clusters
[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.client.MrProvConnection;
32 import org.onap.dmaap.dbcapi.database.DatabaseClass;
33 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
34 import org.onap.dmaap.dbcapi.model.ApiError;
35 import org.onap.dmaap.dbcapi.model.DcaeLocation;
36 import org.onap.dmaap.dbcapi.model.MR_Cluster;
37 import org.onap.dmaap.dbcapi.model.Topic;
38 import org.onap.dmaap.dbcapi.model.DmaapObject.DmaapObject_Status;
39 import org.onap.dmaap.dbcapi.service.DcaeLocationService;
40 import org.onap.dmaap.dbcapi.util.DmaapConfig;
41
42 public class MR_ClusterService extends BaseLoggingClass {
43
44         private Map<String, MR_Cluster> mr_clusters = DatabaseClass.getMr_clusters();
45         private boolean multiSite;
46         
47         public MR_ClusterService() {
48                 logger.info( "new ClusterService");
49                 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
50                 multiSite = "true".equalsIgnoreCase(p.getProperty("MR.multisite", "true"));
51                                 
52         }
53         
54         public Map<String, MR_Cluster> getMR_Clusters() {                       
55                 return mr_clusters;
56         }
57                 
58         public List<MR_Cluster> getAllMr_Clusters() {
59                 return new ArrayList<MR_Cluster>(mr_clusters.values());
60         }
61                 
62         public MR_Cluster getMr_Cluster( String key, ApiError apiError ) {                      
63                 MR_Cluster mrc = mr_clusters.get( key );
64                 if ( mrc == null ) {
65                         apiError.setCode(Status.NOT_FOUND.getStatusCode());
66                         apiError.setFields( "dcaeLocationName");
67                         apiError.setMessage( "Cluster with dcaeLocationName " + key + " not found");
68                 }
69                 apiError.setCode(200);
70                 return mrc;
71         }
72         public MR_Cluster getMr_ClusterByFQDN( String key ) {           
73                 for( MR_Cluster cluster: mr_clusters.values() ) {
74                         if ( key.equals( cluster.getFqdn() ) ) {
75                                 return cluster;
76                         }
77                 }
78                 return null;
79         }
80         
81         public MR_Cluster getMr_ClusterByLoc( String loc ) {
82                 return mr_clusters.get( loc );
83         }
84         
85         public List<MR_Cluster> getCentralClusters() {
86                 DcaeLocationService locations = new DcaeLocationService();
87                 List<MR_Cluster> result = new ArrayList<MR_Cluster>();
88                 for( MR_Cluster c: mr_clusters.values() ) {
89                         try {
90                                 if ( locations.getDcaeLocation(c.getDcaeLocationName()).isCentral() ) {
91                                         result.add(c);
92                                 }
93                         } catch ( NullPointerException npe ) {
94                                 logger.warn( "Failed test isCentral for location:" + c.getDcaeLocationName() );
95                         }
96                 }
97                 return result;
98         }       
99         
100         // builds the set of unique cluster groups
101         public Set<String> getGroups() {
102                 Set<String> result = new HashSet<String>();
103                 for( MR_Cluster c: mr_clusters.values() ) {
104                         try {
105                                 result.add(c.getReplicationGroup());
106                         } catch ( NullPointerException npe ) {
107                                 logger.warn( "Failed to add Group for cluster:" + c.getDcaeLocationName() );
108                         }
109                 }
110                 return result;
111         }       
112
113
114         public MR_Cluster addMr_Cluster( MR_Cluster cluster, ApiError apiError ) {
115                 logger.info( "Entry: addMr_Cluster");
116                 MR_Cluster mrc = mr_clusters.get( cluster.getDcaeLocationName() );
117                 if ( mrc != null ) {
118                         apiError.setCode(Status.CONFLICT.getStatusCode());
119                         apiError.setFields( "dcaeLocationName");
120                         apiError.setMessage( "Cluster with dcaeLocationName " + cluster.getDcaeLocationName() + " already exists");
121                         return null;
122                 }
123                 cluster.setLastMod();
124                 cluster.setStatus( addTopicsToCluster( cluster, apiError ) );
125                 mr_clusters.put( cluster.getDcaeLocationName(), cluster );
126                 DcaeLocationService svc = new DcaeLocationService();
127                 DcaeLocation loc = svc.getDcaeLocation( cluster.getDcaeLocationName() );
128                 if ( loc != null && loc.isCentral() && multiSite ) {
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 );
135                         }
136                 }
137                 apiError.setCode(200);
138                 return cluster;
139         }
140                 
141         public MR_Cluster updateMr_Cluster( MR_Cluster cluster, ApiError apiError ) {
142                 MR_Cluster mrc = mr_clusters.get( cluster.getDcaeLocationName() );
143                 if ( mrc == null ) {
144                         apiError.setCode(Status.NOT_FOUND.getStatusCode());
145                         apiError.setFields( "dcaeLocationName");
146                         apiError.setMessage( "Cluster with dcaeLocationName " + cluster.getDcaeLocationName() + " not found");
147                         return null;
148                 }
149                 cluster.setLastMod();
150                 cluster.setStatus( addTopicsToCluster( cluster, apiError ) );
151                 mr_clusters.put( cluster.getDcaeLocationName(), cluster );
152                 DcaeLocationService svc = new DcaeLocationService();
153                 DcaeLocation loc = svc.getDcaeLocation( cluster.getDcaeLocationName() );
154                 if ( loc == null ) {
155                         logger.error( "DcaeLocation not found for cluster in " + cluster.getDcaeLocationName() );
156                         cluster.setLastMod();
157                         cluster.setStatus(DmaapObject_Status.INVALID);
158                         mr_clusters.put( cluster.getDcaeLocationName(), cluster );
159                 } else if ( loc.isCentral()  & multiSite ) {
160                         ApiError resp = TopicService.setBridgeClientPerms( cluster );
161                         if ( ! resp.is2xx() ) {
162                                 logger.error( "Unable to provision Bridge to " + cluster.getDcaeLocationName() );
163                                 cluster.setLastMod();
164                                 cluster.setStatus(DmaapObject_Status.INVALID);
165                                 mr_clusters.put( cluster.getDcaeLocationName(), cluster );
166                         }
167                 }
168                 
169                 apiError.setCode(200);
170                 return cluster;
171         }
172                 
173         public MR_Cluster removeMr_Cluster( String key, ApiError apiError ) {
174                 MR_Cluster mrc = mr_clusters.get( key );
175                 if ( mrc == null ) {
176                         apiError.setCode(Status.NOT_FOUND.getStatusCode());
177                         apiError.setFields( "dcaeLocationName");
178                         apiError.setMessage( "Cluster with dcaeLocationName " + key + " not found");
179                         return null;
180                 }
181                 apiError.setCode(200);
182                 return mr_clusters.remove(key);
183         }       
184         
185         private DmaapObject_Status addTopicsToCluster( MR_Cluster cluster, ApiError err  ){
186                 
187                 TopicService ts = new TopicService();
188                 MrProvConnection prov = new MrProvConnection();
189                 List<Topic>  topics = ts.getAllTopicsWithoutClients();
190                 for( Topic topic: topics ) {
191                         logger.info( "POST topic " + topic.getFqtn() + " to cluster " + cluster.getFqdn() + " in loc " + cluster.getDcaeLocationName());
192                         if ( prov.makeTopicConnection(cluster)) {
193                                 String resp = prov.doPostTopic(topic, err);
194                                 logger.info( "response code: " + err.getCode() );
195                                 if ( ! err.is2xx() && ! (err.getCode() == 409) ) {
196                                         return DmaapObject_Status.INVALID;
197                                 } 
198                         }
199                 }
200                 
201                 return DmaapObject_Status.VALID;
202         }
203 }