135b41d41a3b5c832d0497870519e2c81204e0e4
[dcaegen2/services.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * BBS-RELOCATION-CPE-AUTHENTICATION-HANDLER
4  * ================================================================================
5  * Copyright (C) 2019 NOKIA 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.bbs.event.processor.controllers;
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.concurrent.Executors;
29
30 import org.onap.bbs.event.processor.pipelines.CpeAuthenticationPipeline;
31 import org.onap.bbs.event.processor.pipelines.ReRegistrationPipeline;
32 import org.onap.bbs.event.processor.pipelines.Scheduler;
33 import org.onap.bbs.event.processor.utilities.LoggingUtil;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.http.HttpStatus;
38 import org.springframework.http.ResponseEntity;
39 import org.springframework.web.bind.annotation.GetMapping;
40 import org.springframework.web.bind.annotation.PathVariable;
41 import org.springframework.web.bind.annotation.PostMapping;
42 import org.springframework.web.bind.annotation.RestController;
43
44 import reactor.core.publisher.Mono;
45
46 @RestController
47 @Api(value = "BbsEventProcessorController", description = "Manage bbs-event-processor microService")
48 public class BbsEventProcessorController {
49
50     private ReRegistrationPipeline reRegistrationPipeline;
51     private CpeAuthenticationPipeline cpeAuthenticationPipeline;
52     private Scheduler scheduler;
53
54     /**
55      * Constructs BBE event processor REST controller.
56      * @param reRegistrationPipeline processing pipeline for polling DMaaP for PNF re-registration events
57      * @param cpeAuthenticationPipeline processing pipeline for polling DMaaP for CPE authentication events
58      * @param scheduler application scheduler
59      */
60     @Autowired
61     public BbsEventProcessorController(ReRegistrationPipeline reRegistrationPipeline,
62                                        CpeAuthenticationPipeline cpeAuthenticationPipeline,
63                                        Scheduler scheduler) {
64         this.reRegistrationPipeline = reRegistrationPipeline;
65         this.cpeAuthenticationPipeline = cpeAuthenticationPipeline;
66         this.scheduler = scheduler;
67     }
68
69     private static final Logger LOGGER = LoggerFactory.getLogger(BbsEventProcessorController.class);
70
71     /**
72      * Responds to health-check heartbeats.
73      * @return Proper HTTP response based on application health
74      */
75     @GetMapping("heartbeat")
76     @ApiOperation(value = "Returns liveness of bbs-event-processor microService")
77     @ApiResponses(value = {
78             @ApiResponse(code = 200, message = "bbs-event-processor microService is alive"),
79             @ApiResponse(code = 401, message = "Not authorized to view the resource"),
80             @ApiResponse(code = 403, message = "Resource access is forbidden"),
81             @ApiResponse(code = 404, message = "Resource is not found")})
82     public Mono<ResponseEntity<String>> handleHeartBeat() {
83         LOGGER.debug("bbs-event-processor has received a heartbeat request");
84         return Mono.defer(() ->
85                 Mono.just(new ResponseEntity<>("bbs-event-processor is alive\n", HttpStatus.OK))
86         );
87     }
88
89     /**
90      * Polls DMaaP for PNF re-registration events just once.
91      * @return Proper HTTP response based on request submission result
92      */
93     @PostMapping("poll-reregistration-events")
94     @ApiOperation(value = "Returns result of request submission. PNF re-registration polling will occur asynchronously")
95     @ApiResponses(value = {
96             @ApiResponse(code = 200, message = "Polling Re-registration events task submitted successfully"),
97             @ApiResponse(code = 401, message = "Not authorized to view the resource"),
98             @ApiResponse(code = 403, message = "Resource access is forbidden"),
99             @ApiResponse(code = 404, message = "Resource is not found")})
100     public Mono<ResponseEntity<String>> handleReRegistrationRestCall() {
101         LOGGER.debug("bbs-event-processor has received a re-registration handling request");
102         Executors.newSingleThreadExecutor().submit(() -> reRegistrationPipeline.processPnfReRegistrationEvents());
103         return Mono.defer(() ->
104                 Mono.just(new ResponseEntity<>("Request submitted\n", HttpStatus.OK))
105         );
106     }
107
108     /**
109      * Polls DMaaP for CPE authentication events just once.
110      * @return Proper HTTP response based on request submission result
111      */
112     @PostMapping("poll-cpe-authentication-events")
113     @ApiOperation(value = "Returns result of request submission. CPE authentication polling will occur asynchronously")
114     @ApiResponses(value = {
115             @ApiResponse(code = 200, message = "CPE authentication task submitted successfully"),
116             @ApiResponse(code = 401, message = "Not authorized to view the resource"),
117             @ApiResponse(code = 403, message = "Resource access is forbidden"),
118             @ApiResponse(code = 404, message = "Resource is not found")})
119     public Mono<ResponseEntity<String>> handleCpeAuthenticationRestCall() {
120         LOGGER.debug("bbs-event-processor has received a cpe-authentication handling request");
121         Executors.newSingleThreadExecutor().submit(() -> cpeAuthenticationPipeline.processPnfCpeAuthenticationEvents());
122         return Mono.defer(() ->
123                 Mono.just(new ResponseEntity<>("Request submitted\n", HttpStatus.OK))
124         );
125     }
126
127     /**
128      * Reschedules DMaaP polling tasks.
129      * @return Proper HTTP response based on rescheduling result
130      */
131     @PostMapping("start-tasks")
132     @ApiOperation(value = "Returns result of request to start microservice tasks")
133     @ApiResponses(value = {
134             @ApiResponse(code = 200, message = "Tasks were successfully started"),
135             @ApiResponse(code = 401, message = "Not authorized to view the resource"),
136             @ApiResponse(code = 403, message = "Resource access is forbidden"),
137             @ApiResponse(code = 404, message = "Resource is not found"),
138             @ApiResponse(code = 406, message = "Task initiation failed. Check logs")})
139     public Mono<ResponseEntity<String>> reScheduleTasks() {
140         LOGGER.trace("bbs-event-processor has received a request to reschedule all running tasks");
141         if (scheduler.reScheduleProcessingTasks()) {
142             return Mono.defer(() ->
143                     Mono.just(new ResponseEntity<>("Initiation of tasks was successful\n", HttpStatus.OK))
144             );
145         } else {
146             return Mono.defer(() ->
147                     Mono.just(new ResponseEntity<>("Initiation of tasks failed\n", HttpStatus.NOT_ACCEPTABLE))
148             );
149         }
150     }
151
152     /**
153      * Cancels DMaaP polling tasks.
154      * @return Proper HTTP response based on cancellation result
155      */
156     @PostMapping("cancel-tasks")
157     @ApiOperation(value = "Returns result of request to cancel running microservice tasks")
158     @ApiResponses(value = {
159             @ApiResponse(code = 200, message = "Tasks were successfully cancelled"),
160             @ApiResponse(code = 401, message = "Not authorized to view the resource"),
161             @ApiResponse(code = 403, message = "Resource access is forbidden"),
162             @ApiResponse(code = 404, message = "Resource is not found"),
163             @ApiResponse(code = 406, message = "Cancellation failed. Check logs")})
164     public Mono<ResponseEntity<String>> cancelTasks() {
165         LOGGER.debug("bbs-event-processor has received a request to cancel all running tasks");
166         if (scheduler.cancelScheduledProcessingTasks()) {
167             return Mono.defer(() ->
168                     Mono.just(new ResponseEntity<>("Cancellation was successful\n", HttpStatus.OK))
169             );
170         } else {
171             return Mono.defer(() ->
172                     Mono.just(new ResponseEntity<>("Cancellation failed\n", HttpStatus.NOT_ACCEPTABLE))
173             );
174         }
175     }
176
177     /**
178      * Change logging level for BBS code.
179      * @param level new logging level
180      * @return Proper HTTP response based on change logging level result
181      */
182     @PostMapping("logging/{level}")
183     public Mono<ResponseEntity<String>> changeLoggingLevel(@PathVariable String level) {
184         return Mono.defer(() ->  {
185                 if (LoggingUtil.changeLoggingLevel(level)) {
186                     LOGGER.info("Changed logging level to {}", level);
187                     return Mono.just(new ResponseEntity<>("Changed BBS event processor logging level\n",
188                             HttpStatus.OK));
189                 } else {
190                     return Mono.just(new ResponseEntity<>("Unacceptable logging level\n",
191                             HttpStatus.NOT_ACCEPTABLE));
192                 }
193             }
194         );
195     }
196 }