Improve support for http to MR
[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.List;
25 import java.util.Map;
26
27 import javax.ws.rs.core.Response.Status;
28
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;
35 import org.onap.dmaap.dbcapi.util.DmaapConfig;
36
37 public class MR_ClusterService extends BaseLoggingClass {
38
39         private Map<String, MR_Cluster> mr_clusters = DatabaseClass.getMr_clusters();
40         private boolean multiSite;
41         
42         public MR_ClusterService() {
43                 logger.info( "new ClusterService");
44                 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
45                 multiSite = "true".equalsIgnoreCase(p.getProperty("MR.multisite", "true"));
46                                 
47         }
48         
49         public Map<String, MR_Cluster> getMR_Clusters() {                       
50                 return mr_clusters;
51         }
52                 
53         public List<MR_Cluster> getAllMr_Clusters() {
54                 return new ArrayList<MR_Cluster>(mr_clusters.values());
55         }
56                 
57         public MR_Cluster getMr_Cluster( String key, ApiError apiError ) {                      
58                 MR_Cluster mrc = mr_clusters.get( key );
59                 if ( mrc == null ) {
60                         apiError.setCode(Status.NOT_FOUND.getStatusCode());
61                         apiError.setFields( "dcaeLocationName");
62                         apiError.setMessage( "Cluster with dcaeLocationName " + key + " not found");
63                 }
64                 apiError.setCode(200);
65                 return mrc;
66         }
67         public MR_Cluster getMr_ClusterByFQDN( String key ) {           
68                 for( MR_Cluster cluster: mr_clusters.values() ) {
69                         if ( key.equals( cluster.getFqdn() ) ) {
70                                 return cluster;
71                         }
72                 }
73                 return null;
74         }
75         
76         public List<MR_Cluster> getCentralClusters() {
77                 DcaeLocationService locations = new DcaeLocationService();
78                 List<MR_Cluster> result = new ArrayList<MR_Cluster>();
79                 for( MR_Cluster c: mr_clusters.values() ) {
80                         if ( locations.getDcaeLocation(c.getDcaeLocationName()).isCentral() ) {
81                                 result.add(c);
82                         }
83                 }
84                 return result;
85         }       
86
87         public MR_Cluster addMr_Cluster( MR_Cluster cluster, ApiError apiError ) {
88                 logger.info( "Entry: addMr_Cluster");
89                 MR_Cluster mrc = mr_clusters.get( cluster.getDcaeLocationName() );
90                 if ( mrc != null ) {
91                         apiError.setCode(Status.CONFLICT.getStatusCode());
92                         apiError.setFields( "dcaeLocationName");
93                         apiError.setMessage( "Cluster with dcaeLocationName " + cluster.getDcaeLocationName() + " already exists");
94                         return null;
95                 }
96                 cluster.setLastMod();
97                 cluster.setStatus(DmaapObject_Status.VALID);
98                 mr_clusters.put( cluster.getDcaeLocationName(), cluster );
99                 DcaeLocationService svc = new DcaeLocationService();
100                 DcaeLocation loc = svc.getDcaeLocation( cluster.getDcaeLocationName() );
101                 if ( loc != null && loc.isCentral() && multiSite ) {
102                         ApiError resp = TopicService.setBridgeClientPerms( cluster );
103                         if ( ! resp.is2xx() ) {
104                                 logger.error( "Unable to provision Bridge to " + cluster.getDcaeLocationName() );
105                                 cluster.setLastMod();
106                                 cluster.setStatus(DmaapObject_Status.INVALID);
107                                 mr_clusters.put( cluster.getDcaeLocationName(), cluster );
108                         }
109                 }
110                 apiError.setCode(200);
111                 return cluster;
112         }
113                 
114         public MR_Cluster updateMr_Cluster( MR_Cluster cluster, ApiError apiError ) {
115                 MR_Cluster mrc = mr_clusters.get( cluster.getDcaeLocationName() );
116                 if ( mrc == null ) {
117                         apiError.setCode(Status.NOT_FOUND.getStatusCode());
118                         apiError.setFields( "dcaeLocationName");
119                         apiError.setMessage( "Cluster with dcaeLocationName " + cluster.getDcaeLocationName() + " not found");
120                         return null;
121                 }
122                 cluster.setLastMod();
123                 mr_clusters.put( cluster.getDcaeLocationName(), cluster );
124                 DcaeLocationService svc = new DcaeLocationService();
125                 DcaeLocation loc = svc.getDcaeLocation( cluster.getDcaeLocationName() );
126                 if ( loc == null ) {
127                         logger.error( "DcaeLocation not found for cluster in " + cluster.getDcaeLocationName() );
128                         cluster.setLastMod();
129                         cluster.setStatus(DmaapObject_Status.INVALID);
130                         mr_clusters.put( cluster.getDcaeLocationName(), cluster );
131                 } else if ( loc.isCentral()  & multiSite ) {
132                         ApiError resp = TopicService.setBridgeClientPerms( cluster );
133                         if ( ! resp.is2xx() ) {
134                                 logger.error( "Unable to provision Bridge to " + cluster.getDcaeLocationName() );
135                                 cluster.setLastMod();
136                                 cluster.setStatus(DmaapObject_Status.INVALID);
137                                 mr_clusters.put( cluster.getDcaeLocationName(), cluster );
138                         }
139                 }
140                 apiError.setCode(200);
141                 return cluster;
142         }
143                 
144         public MR_Cluster removeMr_Cluster( String key, ApiError apiError ) {
145                 MR_Cluster mrc = mr_clusters.get( key );
146                 if ( mrc == null ) {
147                         apiError.setCode(Status.NOT_FOUND.getStatusCode());
148                         apiError.setFields( "dcaeLocationName");
149                         apiError.setMessage( "Cluster with dcaeLocationName " + key + " not found");
150                         return null;
151                 }
152                 apiError.setCode(200);
153                 return mr_clusters.remove(key);
154         }       
155         
156
157 }