Alternative MR replication method
[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.Dmaap;
39 import org.onap.dmaap.dbcapi.model.MirrorMaker;
40 import org.onap.dmaap.dbcapi.service.ApiService;
41 import org.onap.dmaap.dbcapi.service.MirrorMakerService;
42
43 import io.swagger.annotations.Api;
44 import io.swagger.annotations.ApiOperation;
45 import io.swagger.annotations.ApiResponse;
46 import io.swagger.annotations.ApiResponses;
47
48 @Path("/bridge")
49 @Api( value= "bridge", description = "Endpoint for retreiving MR Bridge metrics" )
50 @Consumes(MediaType.APPLICATION_JSON)
51 @Produces(MediaType.APPLICATION_JSON)
52 @Authorization
53 public class BridgeResource extends BaseLoggingClass {
54         
55         private MirrorMakerService mmService = new MirrorMakerService();
56
57         @GET
58         @ApiOperation( value = "return BrTopic details", 
59         notes = "Returns array of  `BrTopic` objects. If source and target query params are specified, only report on that bridge.  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 = Dmaap.class),
63     @ApiResponse( code = 400, message = "Error", response = ApiError.class )
64 })
65         public Response getBridgedTopics(@QueryParam("source") String source,
66                                                                         @QueryParam("target") String target,
67                                                                         @QueryParam("detail") Boolean detailFlag ){
68                 ApiService check = new ApiService();
69
70                 if ( ! Boolean.TRUE.equals(detailFlag)) {
71                         BrTopic brTopic = new BrTopic();
72                         
73                         logger.info( "getBridgeTopics():" + " source=" + source + ", target=" + target);
74         
75                         if (source != null && target != null) {         // get topics between 2 bridged locations
76                                 brTopic.setBrSource(source);
77                                 brTopic.setBrTarget(target);
78                                 MirrorMaker mm = mmService.getMirrorMaker(source, target);
79                                 if ( mm != null ) {             
80                                                 brTopic.setTopicCount( mm.getTopicCount() );
81                                 } 
82         
83                                 logger.info( "topicCount [2 locations]: " + brTopic.getTopicCount() );
84                         }
85                         else if (source == null && target == null ) {
86                                 List<String> mmList = mmService.getAllMirrorMakers();
87                                 brTopic.setBrSource("all");
88                                 brTopic.setBrTarget("all");
89                                 int totCnt = 0;
90                                 for( String key: mmList ) {
91                                         int mCnt = 0;
92                                         MirrorMaker mm = mmService.getMirrorMaker(key);
93                                         if ( mm != null ) {
94                                                 mCnt = mm.getTopicCount();
95                                         }
96                                         logger.info( "Count for "+ key + ": " + mCnt);
97                                         totCnt += mCnt;
98                                 }
99                                 
100                                 logger.info( "topicCount [all locations]: " + totCnt );
101                                 brTopic.setTopicCount(totCnt);
102         
103                         }
104                         else {
105         
106                                 logger.error( "source or target is missing");
107                                 check.setCode(Status.BAD_REQUEST.getStatusCode());
108                                 check.setMessage("Either both source and target or neither must be provided");
109                                 return check.error();
110                         }
111                         return check.success(brTopic);
112                 } else {
113                         
114                         
115                         logger.info( "getBridgeTopics() detail:" + " source=" + source + ", target=" + target);
116         
117                         if (source != null && target != null) {         // get topics between 2 bridged locations
118                                 
119                                 MirrorMaker mm = mmService.getMirrorMaker(source, target);
120                                 if ( mm == null ) {             
121                                         return check.notFound();
122                                 } 
123         
124                                 return check.success(mm);
125                         }
126
127                         else {
128         
129                                 logger.error( "source and target are required when detail=true");
130                                 check.setCode(Status.BAD_REQUEST.getStatusCode());
131                                 check.setMessage("source and target are required when detail=true");
132                                 return check.error();
133                         }
134                 }
135         }
136         
137         @PUT
138         @ApiOperation( value = "update MirrorMaker details", 
139                 notes = "replace the topic list for a specific Bridge.  Use JSON Body for value to replace whitelist, but if refreshFlag param is true, simply refresh using existing whitelist", 
140                 response = MirrorMaker.class)
141         @ApiResponses( value = {
142             @ApiResponse( code = 200, message = "Success", response = Dmaap.class),
143             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
144         })
145         public Response putBridgedTopics(@QueryParam("source") String source,
146                                                                         @QueryParam("target") String target,
147                                                                         @QueryParam("refresh") Boolean refreshFlag,
148                                                                         MirrorMaker newBridge ){
149                 ApiService check = new ApiService();    
150                         
151                 logger.info( "putBridgeTopics() detail:" + " source=" + source + ", target=" + target);
152
153                 if (source != null && target != null) {         // get topics between 2 bridged locations
154                         
155                         MirrorMaker mm = mmService.getMirrorMaker(source, target);
156                         if ( mm == null ) {             
157                                 return check.notFound();
158                         } 
159                         if ( refreshFlag != null  &&  refreshFlag == false ) {
160                                 logger.info( "setting whitelist from message body");
161                                 mm.setTopics( newBridge.getTopics() );
162                         } else {
163                                 logger.info( "refreshing whitelist from memory");
164                         }
165                         mmService.updateMirrorMaker(mm);
166                         return check.success(mm);
167                 }
168
169                 else {
170
171                         logger.error( "source and target are required when detail=true");
172                         check.setCode(Status.BAD_REQUEST.getStatusCode());
173                         check.setMessage("source and target are required when detail=true");
174                         return check.error();
175                 }
176
177         }
178 }