2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.dmaap.dbcapi.resources;
23 import java.util.List;
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;
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;
43 import io.swagger.annotations.Api;
44 import io.swagger.annotations.ApiOperation;
45 import io.swagger.annotations.ApiResponse;
46 import io.swagger.annotations.ApiResponses;
49 @Api( value= "bridge", description = "Endpoint for retreiving MR Bridge metrics" )
50 @Consumes(MediaType.APPLICATION_JSON)
51 @Produces(MediaType.APPLICATION_JSON)
53 public class BridgeResource extends BaseLoggingClass {
55 private MirrorMakerService mmService = new MirrorMakerService();
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. "
60 + "If detail param is true, list topics names, else just a count is returned.",
61 response = BrTopic.class)
62 @ApiResponses( value = {
63 @ApiResponse( code = 200, message = "Success", response = Dmaap.class),
64 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
66 public Response getBridgedTopics(@QueryParam("mmagent") String mmagent,
67 @QueryParam("detail") Boolean detailFlag ){
68 ApiService check = new ApiService();
70 if ( mmagent == null ) {
71 return check.success(getMMcounts(Boolean.TRUE.equals(detailFlag)));
74 logger.info( "getBridgeTopics():" + " mmagent=" + mmagent);
76 if ( ! Boolean.TRUE.equals(detailFlag)) {
77 BrTopic brTopic = new BrTopic();
79 // get topics between 2 bridged locations
81 MirrorMaker mm = mmService.getMirrorMaker(mmagent);
83 return check.notFound();
86 brTopic.setTopicCount( mm.getTopicCount() );
87 brTopic.setBrSource( mm.getSourceCluster());
88 brTopic.setBrTarget( mm.getTargetCluster());
89 brTopic.setMmAgentName(mm.getMmName());
91 logger.info( "topicCount [2 locations]: " + brTopic.getTopicCount() );
93 return check.success(brTopic);
95 logger.info( "getBridgeTopics() detail:" + " mmagent=" + mmagent);
96 // get topics between 2 bridged locations
97 MirrorMaker mm = mmService.getMirrorMaker(mmagent);
99 return check.notFound();
102 return check.success(mm);
106 private BrTopic[] getMMcounts( Boolean showDetail ) {
108 List<String> mmList = mmService.getAllMirrorMakers();
111 s = mmList.size() + 1;
113 BrTopic[] brTopic = new BrTopic[s];
117 for( String key: mmList ) {
119 MirrorMaker mm = mmService.getMirrorMaker(key);
121 mCnt = mm.getTopicCount();
123 logger.info( "Count for "+ key + ": " + mCnt);
126 brTopic[s] = new BrTopic();
127 brTopic[s].setBrSource( mm.getSourceCluster());
128 brTopic[s].setBrTarget(mm.getTargetCluster());
129 brTopic[s].setMmAgentName(mm.getMmName());
130 brTopic[s].setTopicCount(mm.getTopicCount());
135 logger.info( "topicCount [all locations]: " + totCnt );
136 brTopic[s] = new BrTopic();
137 brTopic[s].setBrSource("all");
138 brTopic[s].setBrTarget("all");
139 brTopic[s].setMmAgentName("n/a");
140 brTopic[s].setTopicCount(totCnt);
145 @ApiOperation( value = "update MirrorMaker details",
146 notes = "replace the topic list for a specific Bridge. Use JSON Body for value to replace whitelist, "
147 + "but if refreshFlag param is true, simply refresh using existing whitelist."
148 + "If split param is true, spread whitelist over smaller mmagents.",
149 response = MirrorMaker.class)
150 @ApiResponses( value = {
151 @ApiResponse( code = 200, message = "Success", response = Dmaap.class),
152 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
154 public Response putBridgedTopics(@QueryParam("mmagent") String mmagent,
155 @QueryParam("refresh") Boolean refreshFlag,
156 @QueryParam("split") Boolean splitFlag,
157 MirrorMaker newBridge ){
158 ApiService check = new ApiService();
160 logger.info( "putBridgeTopics() mmagent:" + mmagent );
162 if ( mmagent != null ) { // put topics between 2 bridged locations
164 MirrorMaker mm = mmService.getMirrorMaker(mmagent);
166 return check.notFound();
169 if ( splitFlag != null && splitFlag == true ) {
170 mm = mmService.splitMM( mm );
171 } else if ( refreshFlag == null || refreshFlag == false ) {
172 logger.info( "setting whitelist from message body containing mmName=" + newBridge.getMmName());
173 if ( ! mmagent.equals(newBridge.getMmName()) ){
174 logger.error( "mmagent query param does not match mmName in body");
175 check.setCode(Status.BAD_REQUEST.getStatusCode());
176 check.setMessage("mmagent query param does not match mmName in body");
177 return check.error();
179 mm.setTopics( newBridge.getTopics() );
181 logger.info( "refreshing whitelist from memory");
183 mmService.updateMirrorMaker(mm);
184 return check.success(mm);
189 logger.error( "mmagent is required for PUT");
190 check.setCode(Status.BAD_REQUEST.getStatusCode());
191 check.setMessage("mmagent is required for PUT");
192 return check.error();