Fixed bug in BridgeResource.java
[dmaap/dbcapi.git] / src / main / java / org / onap / dmaap / dbcapi / resources / BridgeResource.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dmaap
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (C) 2018 IBM.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.dmaap.dbcapi.resources;
24
25 import java.util.List;
26
27 import javax.ws.rs.Consumes;
28 import javax.ws.rs.GET;
29 import javax.ws.rs.PUT;
30 import javax.ws.rs.Path;
31 import javax.ws.rs.Produces;
32 import javax.ws.rs.QueryParam;
33 import javax.ws.rs.core.MediaType;
34 import javax.ws.rs.core.Response;
35 import javax.ws.rs.core.Response.Status;
36
37 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
38 import org.onap.dmaap.dbcapi.model.ApiError;
39 import org.onap.dmaap.dbcapi.model.BrTopic;
40 import org.onap.dmaap.dbcapi.model.Dmaap;
41 import org.onap.dmaap.dbcapi.model.MirrorMaker;
42 import org.onap.dmaap.dbcapi.service.ApiService;
43 import org.onap.dmaap.dbcapi.service.MirrorMakerService;
44
45 import io.swagger.annotations.Api;
46 import io.swagger.annotations.ApiOperation;
47 import io.swagger.annotations.ApiResponse;
48 import io.swagger.annotations.ApiResponses;
49
50 @Path("/bridge")
51 @Api( value= "bridge", description = "Endpoint for retreiving MR Bridge metrics" )
52 @Consumes(MediaType.APPLICATION_JSON)
53 @Produces(MediaType.APPLICATION_JSON)
54 @Authorization
55 public class BridgeResource extends BaseLoggingClass {
56         
57         private MirrorMakerService mmService = new MirrorMakerService();
58
59         @GET
60         @ApiOperation( value = "return BrTopic details", 
61         notes = "Returns array of  `BrTopic` objects. If source and target query params are specified, only report on that bridge.  "
62                         + "If detail param is true, list topics names, else just a count is returned.", 
63         response = BrTopic.class)
64 @ApiResponses( value = {
65     @ApiResponse( code = 200, message = "Success", response = Dmaap.class),
66     @ApiResponse( code = 400, message = "Error", response = ApiError.class )
67 })
68         public Response getBridgedTopics(@QueryParam("mmagent") String mmagent,
69                                                                         @QueryParam("detail") Boolean detailFlag ){
70                 ApiService check = new ApiService();
71                 
72                 if ( mmagent == null ) {
73                         return check.success(getMMcounts(Boolean.TRUE.equals(detailFlag)));
74
75                 }
76                 logger.info( "getBridgeTopics():" + " mmagent=" + mmagent);
77
78                 if ( ! Boolean.TRUE.equals(detailFlag)) {
79                         BrTopic brTopic = new BrTopic();
80                         
81                         // get topics between 2 bridged locations
82
83                         MirrorMaker mm = mmService.getMirrorMaker(mmagent);
84                         if ( mm == null ) {             
85                                 return check.notFound();
86                         } 
87                                         
88                         brTopic.setTopicCount( mm.getTopicCount() );
89                         brTopic.setBrSource( mm.getSourceCluster());
90                         brTopic.setBrTarget( mm.getTargetCluster());
91                         brTopic.setMmAgentName(mm.getMmName());
92                         
93                         logger.info( "topicCount [2 locations]: " + brTopic.getTopicCount() );
94                 
95                         return check.success(brTopic);
96                 } else {        
97                         logger.info( "getBridgeTopics() detail:" + " mmagent=" + mmagent);
98                         // get topics between 2 bridged locations       
99                         MirrorMaker mm = mmService.getMirrorMaker(mmagent);
100                         if ( mm == null ) {             
101                                 return check.notFound();
102                         } 
103
104                         return check.success(mm);
105                 }
106         }
107         
108         private BrTopic[] getMMcounts( Boolean showDetail ) {
109                 
110                 List<String> mmList = mmService.getAllMirrorMakers();
111                 int s = 1;
112                 if ( showDetail ) {
113                         s = mmList.size() + 1;
114                 }
115                 BrTopic[] brTopic = new BrTopic[s];
116                 
117                 int totCnt = 0;
118                 s = 0;
119                 for( String key: mmList ) {
120                         int mCnt = 0;
121                         MirrorMaker mm = mmService.getMirrorMaker(key);
122                         if ( mm != null ) {
123                                 mCnt = mm.getTopicCount();
124                         }
125                         logger.info( "Count for "+ key + ": " + mCnt);
126                         totCnt += mCnt;
127                         if (showDetail && mm!=null) {
128                                 brTopic[s] =  new BrTopic();
129                                 brTopic[s].setBrSource( mm.getSourceCluster());
130                                 brTopic[s].setBrTarget(mm.getTargetCluster());
131                                 brTopic[s].setMmAgentName(mm.getMmName());
132                                 brTopic[s].setTopicCount(mm.getTopicCount());
133                                 s++;
134                         }
135                 }
136                 
137                 logger.info( "topicCount [all locations]: " + totCnt );
138                 brTopic[s] =  new BrTopic();
139                 brTopic[s].setBrSource("all");
140                 brTopic[s].setBrTarget("all");
141                 brTopic[s].setMmAgentName("n/a");
142                 brTopic[s].setTopicCount(totCnt);
143                 return brTopic;
144         }
145         
146         @PUT
147         @ApiOperation( value = "update MirrorMaker details", 
148                 notes = "replace the topic list for a specific Bridge.  Use JSON Body for value to replace whitelist, "
149                                 + "but if refreshFlag param is true, simply refresh using existing whitelist."
150                                 + "If split param is true, spread whitelist over smaller mmagents.", 
151                 response = MirrorMaker.class)
152         @ApiResponses( value = {
153             @ApiResponse( code = 200, message = "Success", response = Dmaap.class),
154             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
155         })
156         public Response putBridgedTopics(@QueryParam("mmagent") String mmagent,
157                                                                         @QueryParam("refresh") Boolean refreshFlag,
158                                                                         @QueryParam("split") Boolean splitFlag,
159                                                                         MirrorMaker newBridge ){
160                 ApiService check = new ApiService();    
161                         
162                 logger.info( "putBridgeTopics() mmagent:" +  mmagent );
163
164                 if ( mmagent != null ) {                // put topics between 2 bridged locations
165                         
166                         MirrorMaker mm = mmService.getMirrorMaker(mmagent);
167                         if ( mm == null ) {             
168                                 return check.notFound();
169                         } 
170                         
171                         if ( splitFlag != null && splitFlag == true ) {
172                                 mm = mmService.splitMM( mm );
173                         } else if ( refreshFlag == null  ||  refreshFlag == false ) {
174                                 logger.info( "setting whitelist from message body containing mmName=" + newBridge.getMmName());
175                                 if ( ! mmagent.equals(newBridge.getMmName()) ){
176                                         logger.error( "mmagent query param does not match mmName in body");
177                                         check.setCode(Status.BAD_REQUEST.getStatusCode());
178                                         check.setMessage("mmagent query param does not match mmName in body");
179                                         return check.error();
180                                 }
181                                 mm.setTopics( newBridge.getTopics() );
182                         } else {
183                                 logger.info( "refreshing whitelist from memory");
184                         }
185                         mmService.updateMirrorMaker(mm);
186                         return check.success(mm);
187                 }
188
189                 else {
190
191                         logger.error( "mmagent is required for PUT");
192                         check.setCode(Status.BAD_REQUEST.getStatusCode());
193                         check.setMessage("mmagent is required for PUT");
194                         return check.error();
195                 }
196
197         }
198 }