DMAAP-83 Initial code import
[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 io.swagger.annotations.Api;
24 import io.swagger.annotations.ApiOperation;
25 import io.swagger.annotations.ApiResponse;
26 import io.swagger.annotations.ApiResponses;
27
28 import java.util.List;
29
30 import javax.ws.rs.Consumes;
31 import javax.ws.rs.GET;
32 import javax.ws.rs.HeaderParam;
33 import javax.ws.rs.Path;
34 import javax.ws.rs.Produces;
35 import javax.ws.rs.QueryParam;
36 import javax.ws.rs.core.Context;
37 import javax.ws.rs.core.MediaType;
38 import javax.ws.rs.core.Response;
39 import javax.ws.rs.core.UriInfo;
40 import javax.ws.rs.core.Response.Status;
41
42 import org.onap.dmaap.dbcapi.aaf.authentication.AuthenticationErrorException;
43 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
44 import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum;
45 import org.onap.dmaap.dbcapi.model.ApiError;
46 import org.onap.dmaap.dbcapi.model.BrTopic;
47 import org.onap.dmaap.dbcapi.model.DcaeLocation;
48 import org.onap.dmaap.dbcapi.model.Dmaap;
49 import org.onap.dmaap.dbcapi.model.MirrorMaker;
50 import org.onap.dmaap.dbcapi.service.ApiService;
51 import org.onap.dmaap.dbcapi.service.MirrorMakerService;
52
53 @Path("/bridge")
54 @Api( value= "bridge", description = "Endpoint for retreiving MR Bridge metrics" )
55 @Consumes(MediaType.APPLICATION_JSON)
56 @Produces(MediaType.APPLICATION_JSON)
57 @Authorization
58 public class BridgeResource extends BaseLoggingClass {
59         
60         private MirrorMakerService mmService = new MirrorMakerService();
61
62         @GET
63         @ApiOperation( value = "return BrTopic details", 
64         notes = "Returns array of  `BrTopic` objects.", 
65         response = BrTopic.class)
66 @ApiResponses( value = {
67     @ApiResponse( code = 200, message = "Success", response = Dmaap.class),
68     @ApiResponse( code = 400, message = "Error", response = ApiError.class )
69 })
70         public Response getBridgedTopics(@QueryParam("source") String source,
71                                                                         @QueryParam("target") String target){
72                 ApiService check = new ApiService();
73
74                 BrTopic brTopic = new BrTopic();
75                 
76                 logger.info( "getBridgeTopics():" + " source=" + source + ", target=" + target);
77 //              System.out.println("getBridgedTopics() " + "source=" + source + ", target=" + target );
78                 if (source != null && target != null) {         // get topics between 2 bridged locations
79                         brTopic.setBrSource(source);
80                         brTopic.setBrTarget(target);
81                         MirrorMaker mm = mmService.getMirrorMaker(source, target);
82                         if ( mm != null ) {
83                                 brTopic.setTopicCount( mm.getTopicCount() );
84                         } 
85
86                         logger.info( "topicCount [2 locations]: " + brTopic.getTopicCount() );
87                 }
88                 else if (source == null && target == null ) {
89                         List<String> mmList = mmService.getAllMirrorMakers();
90                         brTopic.setBrSource("all");
91                         brTopic.setBrTarget("all");
92                         int totCnt = 0;
93                         for( String key: mmList ) {
94                                 int mCnt = 0;
95                                 MirrorMaker mm = mmService.getMirrorMaker(key);
96                                 if ( mm != null ) {
97                                         mCnt = mm.getTopicCount();
98                                 }
99                                 logger.info( "Count for "+ key + ": " + mCnt);
100                                 totCnt += mCnt;
101                         }
102                         
103                         logger.info( "topicCount [all locations]: " + totCnt );
104                         brTopic.setTopicCount(totCnt);
105
106                 }
107                 else {
108
109                         logger.error( "source or target is missing");
110                         check.setCode(Status.BAD_REQUEST.getStatusCode());
111                         check.setMessage("Either 2 locations or no location must be provided");
112                         return check.error();
113                 }
114                 return check.success(brTopic);
115         }
116 }