No topic dups in mmagent whitelist
[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                         try {
81                                 if ( locations.getDcaeLocation(c.getDcaeLocationName()).isCentral() ) {
82                                         result.add(c);
83                                 }
84                         } catch ( NullPointerException npe ) {
85                                 logger.warn( "Failed test isCentral for location:" + c.getDcaeLocationName() );
86                         }
87                 }
88                 return result;
89         }       
90
91         public MR_Cluster addMr_Cluster( MR_Cluster cluster, ApiError apiError ) {
92                 logger.info( "Entry: addMr_Cluster");
93                 MR_Cluster mrc = mr_clusters.get( cluster.getDcaeLocationName() );
94                 if ( mrc != null ) {
95                         apiError.setCode(Status.CONFLICT.getStatusCode());
96                         apiError.setFields( "dcaeLocationName");
97                         apiError.setMessage( "Cluster with dcaeLocationName " + cluster.getDcaeLocationName() + " already exists");
98                         return null;
99                 }
100                 cluster.setLastMod();
101                 cluster.setStatus(DmaapObject_Status.VALID);
102                 mr_clusters.put( cluster.getDcaeLocationName(), cluster );
103                 DcaeLocationService svc = new DcaeLocationService();
104                 DcaeLocation loc = svc.getDcaeLocation( cluster.getDcaeLocationName() );
105                 if ( loc != null && loc.isCentral() && multiSite ) {
106                         ApiError resp = TopicService.setBridgeClientPerms( cluster );
107                         if ( ! resp.is2xx() ) {
108                                 logger.error( "Unable to provision Bridge to " + cluster.getDcaeLocationName() );
109                                 cluster.setLastMod();
110                                 cluster.setStatus(DmaapObject_Status.INVALID);
111                                 mr_clusters.put( cluster.getDcaeLocationName(), cluster );
112                         }
113                 }
114                 apiError.setCode(200);
115                 return cluster;
116         }
117                 
118         public MR_Cluster updateMr_Cluster( MR_Cluster cluster, ApiError apiError ) {
119                 MR_Cluster mrc = mr_clusters.get( cluster.getDcaeLocationName() );
120                 if ( mrc == null ) {
121                         apiError.setCode(Status.NOT_FOUND.getStatusCode());
122                         apiError.setFields( "dcaeLocationName");
123                         apiError.setMessage( "Cluster with dcaeLocationName " + cluster.getDcaeLocationName() + " not found");
124                         return null;
125                 }
126                 cluster.setLastMod();
127                 mr_clusters.put( cluster.getDcaeLocationName(), cluster );
128                 DcaeLocationService svc = new DcaeLocationService();
129                 DcaeLocation loc = svc.getDcaeLocation( cluster.getDcaeLocationName() );
130                 if ( loc == null ) {
131                         logger.error( "DcaeLocation not found for cluster in " + cluster.getDcaeLocationName() );
132                         cluster.setLastMod();
133                         cluster.setStatus(DmaapObject_Status.INVALID);
134                         mr_clusters.put( cluster.getDcaeLocationName(), cluster );
135                 } else if ( loc.isCentral()  & multiSite ) {
136                         ApiError resp = TopicService.setBridgeClientPerms( cluster );
137                         if ( ! resp.is2xx() ) {
138                                 logger.error( "Unable to provision Bridge to " + cluster.getDcaeLocationName() );
139                                 cluster.setLastMod();
140                                 cluster.setStatus(DmaapObject_Status.INVALID);
141                                 mr_clusters.put( cluster.getDcaeLocationName(), cluster );
142                         }
143                 }
144                 apiError.setCode(200);
145                 return cluster;
146         }
147                 
148         public MR_Cluster removeMr_Cluster( String key, ApiError apiError ) {
149                 MR_Cluster mrc = mr_clusters.get( key );
150                 if ( mrc == null ) {
151                         apiError.setCode(Status.NOT_FOUND.getStatusCode());
152                         apiError.setFields( "dcaeLocationName");
153                         apiError.setMessage( "Cluster with dcaeLocationName " + key + " not found");
154                         return null;
155                 }
156                 apiError.setCode(200);
157                 return mr_clusters.remove(key);
158         }       
159         
160
161 }