Fix object references to show fields
[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  * 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.resources;
22
23 import java.util.List;
24
25 import javax.ws.rs.Consumes;
26 import javax.ws.rs.GET;
27 import javax.ws.rs.PUT;
28 import javax.ws.rs.Path;
29 import javax.ws.rs.Produces;
30 import javax.ws.rs.QueryParam;
31 import javax.ws.rs.core.MediaType;
32 import javax.ws.rs.core.Response;
33 import javax.ws.rs.core.Response.Status;
34
35 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
36 import org.onap.dmaap.dbcapi.model.ApiError;
37 import org.onap.dmaap.dbcapi.model.BrTopic;
38 import org.onap.dmaap.dbcapi.model.MirrorMaker;
39 import org.onap.dmaap.dbcapi.service.ApiService;
40 import org.onap.dmaap.dbcapi.service.MirrorMakerService;
41
42 import io.swagger.annotations.Api;
43 import io.swagger.annotations.ApiOperation;
44 import io.swagger.annotations.ApiResponse;
45 import io.swagger.annotations.ApiResponses;
46
47 @Path("/bridge")
48 @Api( value= "bridge", description = "Endpoint for retreiving MR Bridge metrics" )
49 @Consumes(MediaType.APPLICATION_JSON)
50 @Produces(MediaType.APPLICATION_JSON)
51 @Authorization
52 public class BridgeResource extends BaseLoggingClass {
53         
54         private MirrorMakerService mmService = new MirrorMakerService();
55
56         @GET
57         @ApiOperation( value = "return BrTopic details", 
58         notes = "Returns array of  `BrTopic` objects. If source and target query params are specified, only report on that bridge.  "
59                         + "If detail param is true, list topics names, else just a count is returned.", 
60         response = BrTopic.class)
61 @ApiResponses( value = {
62     @ApiResponse( code = 200, message = "Success", response = BrTopic.class),
63     @ApiResponse( code = 400, message = "Error", response = ApiError.class )
64 })
65         public Response getBridgedTopics(@QueryParam("mmagent") String mmagent,
66                                                                         @QueryParam("detail") Boolean detailFlag ){
67                 ApiService check = new ApiService();
68                 
69                 if ( mmagent == null ) {
70                         return check.success(getMMcounts(Boolean.TRUE.equals(detailFlag)));
71
72                 }
73                 logger.info( "getBridgeTopics():" + " mmagent=" + mmagent);
74
75                 if ( ! Boolean.TRUE.equals(detailFlag)) {
76                         BrTopic brTopic = new BrTopic();
77                         
78                         // get topics between 2 bridged locations
79
80                         MirrorMaker mm = mmService.getMirrorMaker(mmagent);
81                         if ( mm == null ) {             
82                                 return check.notFound();
83                         } 
84                                         
85                         brTopic.setTopicCount( mm.getTopicCount() );
86                         brTopic.setBrSource( mm.getSourceCluster());
87                         brTopic.setBrTarget( mm.getTargetCluster());
88                         brTopic.setMmAgentName(mm.getMmName());
89                         
90                         logger.info( "topicCount [2 locations]: " + brTopic.getTopicCount() );
91                 
92                         return check.success(brTopic);
93                 } else {        
94                         logger.info( "getBridgeTopics() detail:" + " mmagent=" + mmagent);
95                         // get topics between 2 bridged locations       
96                         MirrorMaker mm = mmService.getMirrorMaker(mmagent);
97                         if ( mm == null ) {             
98                                 return check.notFound();
99                         } 
100
101                         return check.success(mm);
102                 }
103         }
104         
105         private BrTopic[] getMMcounts( Boolean showDetail ) {
106                 
107                 List<String> mmList = mmService.getAllMirrorMakers();
108                 int s = 1;
109                 if ( showDetail ) {
110                         s = mmList.size() + 1;
111                 }
112                 BrTopic[] brTopic = new BrTopic[s];
113                 
114                 int totCnt = 0;
115                 s = 0;
116                 for( String key: mmList ) {
117                         int mCnt = 0;
118                         MirrorMaker mm = mmService.getMirrorMaker(key);
119                         if ( mm != null ) {
120                                 mCnt = mm.getTopicCount();
121                         }
122                         logger.info( "Count for "+ key + ": " + mCnt);
123                         totCnt += mCnt;
124                         if (showDetail) {
125                                 brTopic[s] =  new BrTopic();
126                                 brTopic[s].setBrSource( mm.getSourceCluster());
127                                 brTopic[s].setBrTarget(mm.getTargetCluster());
128                                 brTopic[s].setMmAgentName(mm.getMmName());
129                                 brTopic[s].setTopicCount(mm.getTopicCount());
130                                 s++;
131                         }
132                 }
133                 
134                 logger.info( "topicCount [all locations]: " + totCnt );
135                 brTopic[s] =  new BrTopic();
136                 brTopic[s].setBrSource("all");
137                 brTopic[s].setBrTarget("all");
138                 brTopic[s].setMmAgentName("n/a");
139                 brTopic[s].setTopicCount(totCnt);
140                 return brTopic;
141         }
142         
143         @PUT
144         @ApiOperation( value = "update MirrorMaker details", 
145                 notes = "replace the topic list for a specific Bridge.  Use JSON Body for value to replace whitelist, "
146                                 + "but if refreshFlag param is true, simply refresh using existing whitelist."
147                                 + "If split param is true, spread whitelist over smaller mmagents.", 
148                 response = MirrorMaker.class)
149         @ApiResponses( value = {
150             @ApiResponse( code = 200, message = "Success", response = BrTopic.class),
151             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
152         })
153         public Response putBridgedTopics(@QueryParam("mmagent") String mmagent,
154                                                                         @QueryParam("refresh") Boolean refreshFlag,
155                                                                         @QueryParam("split") Boolean splitFlag,
156                                                                         MirrorMaker newBridge ){
157                 ApiService check = new ApiService();    
158                         
159                 logger.info( "putBridgeTopics() mmagent:" +  mmagent );
160
161                 if ( mmagent != null ) {                // put topics between 2 bridged locations
162                         
163                         MirrorMaker mm = mmService.getMirrorMaker(mmagent);
164                         if ( mm == null ) {             
165                                 return check.notFound();
166                         } 
167                         
168                         if ( splitFlag != null && splitFlag == true ) {
169                                 mm = mmService.splitMM( mm );
170                         } else if ( refreshFlag == null  ||  refreshFlag == false ) {
171                                 logger.info( "setting whitelist from message body containing mmName=" + newBridge.getMmName());
172                                 if ( ! mmagent.equals(newBridge.getMmName()) ){
173                                         logger.error( "mmagent query param does not match mmName in body");
174                                         check.setCode(Status.BAD_REQUEST.getStatusCode());
175                                         check.setMessage("mmagent query param does not match mmName in body");
176                                         return check.error();
177                                 }
178                                 mm.setTopics( newBridge.getTopics() );
179                         } else {
180                                 logger.info( "refreshing whitelist from memory");
181                         }
182                         mmService.updateMirrorMaker(mm);
183                         return check.success(mm);
184                 }
185
186                 else {
187
188                         logger.error( "mmagent is required for PUT");
189                         check.setCode(Status.BAD_REQUEST.getStatusCode());
190                         check.setMessage("mmagent is required for PUT");
191                         return check.error();
192                 }
193
194         }
195 }