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 io.swagger.annotations.Api;
24 import io.swagger.annotations.ApiOperation;
25 import io.swagger.annotations.ApiResponse;
26 import io.swagger.annotations.ApiResponses;
28 import java.util.List;
30 import javax.ws.rs.Consumes;
31 import javax.ws.rs.GET;
32 import javax.ws.rs.PUT;
33 import javax.ws.rs.HeaderParam;
34 import javax.ws.rs.Path;
35 import javax.ws.rs.Produces;
36 import javax.ws.rs.QueryParam;
37 import javax.ws.rs.core.Context;
38 import javax.ws.rs.core.MediaType;
39 import javax.ws.rs.core.Response;
40 import javax.ws.rs.core.UriInfo;
41 import javax.ws.rs.core.Response.Status;
43 import org.onap.dmaap.dbcapi.aaf.authentication.AuthenticationErrorException;
44 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
45 import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum;
46 import org.onap.dmaap.dbcapi.model.ApiError;
47 import org.onap.dmaap.dbcapi.model.BrTopic;
48 import org.onap.dmaap.dbcapi.model.DcaeLocation;
49 import org.onap.dmaap.dbcapi.model.Dmaap;
50 import org.onap.dmaap.dbcapi.model.MirrorMaker;
51 import org.onap.dmaap.dbcapi.service.ApiService;
52 import org.onap.dmaap.dbcapi.service.MirrorMakerService;
55 @Api( value= "bridge", description = "Endpoint for retreiving MR Bridge metrics" )
56 @Consumes(MediaType.APPLICATION_JSON)
57 @Produces(MediaType.APPLICATION_JSON)
59 public class BridgeResource extends BaseLoggingClass {
61 private MirrorMakerService mmService = new MirrorMakerService();
64 @ApiOperation( value = "return BrTopic details",
65 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",
66 response = BrTopic.class)
67 @ApiResponses( value = {
68 @ApiResponse( code = 200, message = "Success", response = Dmaap.class),
69 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
71 public Response getBridgedTopics(@QueryParam("source") String source,
72 @QueryParam("target") String target,
73 @QueryParam("detail") Boolean detailFlag ){
74 ApiService check = new ApiService();
76 if ( ! Boolean.TRUE.equals(detailFlag)) {
77 BrTopic brTopic = new BrTopic();
79 logger.info( "getBridgeTopics():" + " source=" + source + ", target=" + target);
80 // System.out.println("getBridgedTopics() " + "source=" + source + ", target=" + target );
81 if (source != null && target != null) { // get topics between 2 bridged locations
82 brTopic.setBrSource(source);
83 brTopic.setBrTarget(target);
84 MirrorMaker mm = mmService.getMirrorMaker(source, target);
86 brTopic.setTopicCount( mm.getTopicCount() );
89 logger.info( "topicCount [2 locations]: " + brTopic.getTopicCount() );
91 else if (source == null && target == null ) {
92 List<String> mmList = mmService.getAllMirrorMakers();
93 brTopic.setBrSource("all");
94 brTopic.setBrTarget("all");
96 for( String key: mmList ) {
98 MirrorMaker mm = mmService.getMirrorMaker(key);
100 mCnt = mm.getTopicCount();
102 logger.info( "Count for "+ key + ": " + mCnt);
106 logger.info( "topicCount [all locations]: " + totCnt );
107 brTopic.setTopicCount(totCnt);
112 logger.error( "source or target is missing");
113 check.setCode(Status.BAD_REQUEST.getStatusCode());
114 check.setMessage("Either both source and target or neither must be provided");
115 return check.error();
117 return check.success(brTopic);
121 logger.info( "getBridgeTopics() detail:" + " source=" + source + ", target=" + target);
123 if (source != null && target != null) { // get topics between 2 bridged locations
125 MirrorMaker mm = mmService.getMirrorMaker(source, target);
127 return check.notFound();
130 return check.success(mm);
135 logger.error( "source and target are required when detail=true");
136 check.setCode(Status.BAD_REQUEST.getStatusCode());
137 check.setMessage("source and target are required when detail=true");
138 return check.error();
144 @ApiOperation( value = "update MirrorMaker details",
145 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",
146 response = MirrorMaker.class)
147 @ApiResponses( value = {
148 @ApiResponse( code = 200, message = "Success", response = Dmaap.class),
149 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
151 public Response putBridgedTopics(@QueryParam("source") String source,
152 @QueryParam("target") String target,
153 @QueryParam("refresh") Boolean refreshFlag,
154 MirrorMaker newBridge ){
155 ApiService check = new ApiService();
157 logger.info( "putBridgeTopics() detail:" + " source=" + source + ", target=" + target);
159 if (source != null && target != null) { // get topics between 2 bridged locations
161 MirrorMaker mm = mmService.getMirrorMaker(source, target);
163 return check.notFound();
165 if ( refreshFlag != null && refreshFlag == false ) {
166 logger.info( "setting whitelist from message body");
167 mm.setTopics( newBridge.getTopics() );
169 logger.info( "refreshing whitelist from memory");
171 mmService.updateMirrorMaker(mm);
172 return check.success(mm);
177 logger.error( "source and target are required when detail=true");
178 check.setCode(Status.BAD_REQUEST.getStatusCode());
179 check.setMessage("source and target are required when detail=true");
180 return check.error();