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