[DMAAP-BC] Consolidate bus controller repos
[dmaap/buscontroller.git] / dmaap-bc / 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
36 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
37 import org.onap.dmaap.dbcapi.model.ApiError;
38 import org.onap.dmaap.dbcapi.model.BrTopic;
39 import org.onap.dmaap.dbcapi.model.MirrorMaker;
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 import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
48
49 @Path("/bridge")
50 @Api( value= "bridge", description = "Endpoint for retreiving MR Bridge metrics" )
51 @Consumes(MediaType.APPLICATION_JSON)
52 @Produces(MediaType.APPLICATION_JSON)
53 @Authorization
54 public class BridgeResource extends BaseLoggingClass {
55         
56         private MirrorMakerService mmService = new MirrorMakerService();
57         private ResponseBuilder responseBuilder = new ResponseBuilder();
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 = BrTopic.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
71                 if ( mmagent == null ) {
72                         return responseBuilder.success(getMMcounts(Boolean.TRUE.equals(detailFlag)));
73
74                 }
75                 logger.info( "getBridgeTopics():" + " mmagent=" + mmagent);
76
77                 if ( ! Boolean.TRUE.equals(detailFlag)) {
78                         BrTopic brTopic = new BrTopic();
79                         
80                         // get topics between 2 bridged locations
81
82                         MirrorMaker mm = mmService.getMirrorMaker(mmagent);
83                         if ( mm == null ) {             
84                                 return responseBuilder.notFound();
85                         } 
86                                         
87                         brTopic.setTopicCount( mm.getTopicCount() );
88                         brTopic.setBrSource( mm.getSourceCluster());
89                         brTopic.setBrTarget( mm.getTargetCluster());
90                         brTopic.setMmAgentName(mm.getMmName());
91                         
92                         logger.info( "topicCount [2 locations]: " + brTopic.getTopicCount() );
93                 
94                         return responseBuilder.success(brTopic);
95                 } else {        
96                         logger.info( "getBridgeTopics() detail:" + " mmagent=" + mmagent);
97                         // get topics between 2 bridged locations       
98                         MirrorMaker mm = mmService.getMirrorMaker(mmagent);
99                         if ( mm == null ) {             
100                                 return responseBuilder.notFound();
101                         } 
102
103                         return responseBuilder.success(mm);
104                 }
105         }
106         
107         private BrTopic[] getMMcounts( Boolean showDetail ) {
108                 
109                 List<String> mmList = mmService.getAllMirrorMakers();
110                 int s = 1;
111                 if ( showDetail ) {
112                         s = mmList.size() + 1;
113                 }
114                 BrTopic[] brTopic = new BrTopic[s];
115                 
116                 int totCnt = 0;
117                 s = 0;
118                 for( String key: mmList ) {
119                         int mCnt = 0;
120                         MirrorMaker mm = mmService.getMirrorMaker(key);
121                         if ( mm != null ) {
122                                 mCnt = mm.getTopicCount();
123                         }
124                         logger.info( "Count for "+ key + ": " + mCnt);
125                         totCnt += mCnt;
126                         if (showDetail && mm!=null) {
127                                 brTopic[s] =  new BrTopic();
128                                 brTopic[s].setBrSource( mm.getSourceCluster());
129                                 brTopic[s].setBrTarget(mm.getTargetCluster());
130                                 brTopic[s].setMmAgentName(mm.getMmName());
131                                 brTopic[s].setTopicCount(mm.getTopicCount());
132                                 s++;
133                         }
134                 }
135                 
136                 logger.info( "topicCount [all locations]: " + totCnt );
137                 brTopic[s] =  new BrTopic();
138                 brTopic[s].setBrSource("all");
139                 brTopic[s].setBrTarget("all");
140                 brTopic[s].setMmAgentName("n/a");
141                 brTopic[s].setTopicCount(totCnt);
142                 return brTopic;
143         }
144         
145         @PUT
146         @ApiOperation( value = "update MirrorMaker details", 
147                 notes = "replace the topic list for a specific Bridge.  Use JSON Body for value to replace whitelist, "
148                                 + "but if refreshFlag param is true, simply refresh using existing whitelist."
149                                 + "If split param is true, spread whitelist over smaller mmagents.", 
150                 response = MirrorMaker.class)
151         @ApiResponses( value = {
152             @ApiResponse( code = 200, message = "Success", response = BrTopic.class),
153             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
154         })
155         public Response putBridgedTopics(@QueryParam("mmagent") String mmagent,
156                                                                         @QueryParam("refresh") Boolean refreshFlag,
157                                                                         @QueryParam("split") Boolean splitFlag,
158                                                                         MirrorMaker newBridge ){
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 responseBuilder.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                                         return responseBuilder.error(new ApiError(BAD_REQUEST.getStatusCode(),
175                                                         "mmagent query param does not match mmName in body"));
176                                 }
177                                 mm.setTopics( newBridge.getTopics() );
178                         } else {
179                                 logger.info( "refreshing whitelist from memory");
180                         }
181                         mmService.updateMirrorMaker(mm);
182                         return responseBuilder.success(mm);
183                 }
184
185                 else {
186                         logger.error( "mmagent is required for PUT");
187                         return responseBuilder.error(new ApiError(BAD_REQUEST.getStatusCode(), "mmagent is required for PUT"));
188                 }
189
190         }
191 }