479621750840433748410512862e6f21bbb5386b
[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.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;
42
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;
53
54 @Path("/bridge")
55 @Api( value= "bridge", description = "Endpoint for retreiving MR Bridge metrics" )
56 @Consumes(MediaType.APPLICATION_JSON)
57 @Produces(MediaType.APPLICATION_JSON)
58 @Authorization
59 public class BridgeResource extends BaseLoggingClass {
60         
61         private MirrorMakerService mmService = new MirrorMakerService();
62
63         @GET
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 )
70 })
71         public Response getBridgedTopics(@QueryParam("source") String source,
72                                                                         @QueryParam("target") String target,
73                                                                         @QueryParam("detail") Boolean detailFlag ){
74                 ApiService check = new ApiService();
75
76                 if ( ! Boolean.TRUE.equals(detailFlag)) {
77                         BrTopic brTopic = new BrTopic();
78                         
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);
85                                 if ( mm != null ) {             
86                                                 brTopic.setTopicCount( mm.getTopicCount() );
87                                 } 
88         
89                                 logger.info( "topicCount [2 locations]: " + brTopic.getTopicCount() );
90                         }
91                         else if (source == null && target == null ) {
92                                 List<String> mmList = mmService.getAllMirrorMakers();
93                                 brTopic.setBrSource("all");
94                                 brTopic.setBrTarget("all");
95                                 int totCnt = 0;
96                                 for( String key: mmList ) {
97                                         int mCnt = 0;
98                                         MirrorMaker mm = mmService.getMirrorMaker(key);
99                                         if ( mm != null ) {
100                                                 mCnt = mm.getTopicCount();
101                                         }
102                                         logger.info( "Count for "+ key + ": " + mCnt);
103                                         totCnt += mCnt;
104                                 }
105                                 
106                                 logger.info( "topicCount [all locations]: " + totCnt );
107                                 brTopic.setTopicCount(totCnt);
108         
109                         }
110                         else {
111         
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();
116                         }
117                         return check.success(brTopic);
118                 } else {
119                         
120                         
121                         logger.info( "getBridgeTopics() detail:" + " source=" + source + ", target=" + target);
122         
123                         if (source != null && target != null) {         // get topics between 2 bridged locations
124                                 
125                                 MirrorMaker mm = mmService.getMirrorMaker(source, target);
126                                 if ( mm == null ) {             
127                                         return check.notFound();
128                                 } 
129         
130                                 return check.success(mm);
131                         }
132
133                         else {
134         
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();
139                         }
140                 }
141         }
142         
143         @PUT
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 )
150         })
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();    
156                         
157                 logger.info( "putBridgeTopics() detail:" + " source=" + source + ", target=" + target);
158
159                 if (source != null && target != null) {         // get topics between 2 bridged locations
160                         
161                         MirrorMaker mm = mmService.getMirrorMaker(source, target);
162                         if ( mm == null ) {             
163                                 return check.notFound();
164                         } 
165                         if ( refreshFlag != null  &&  refreshFlag == false ) {
166                                 logger.info( "setting whitelist from message body");
167                                 mm.setTopics( newBridge.getTopics() );
168                         } else {
169                                 logger.info( "refreshing whitelist from memory");
170                         }
171                         mmService.updateMirrorMaker(mm);
172                         return check.success(mm);
173                 }
174
175                 else {
176
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();
181                 }
182
183         }
184 }