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