for policy update work
[dcaegen2/analytics/tca-gen2.git] / dcae-analytics / dcae-analytics-tca-web / src / main / java / org / onap / dcae / analytics / tca / web / controller / TcaRestController.java
1 /*
2  * ================================================================================
3  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ============LICENSE_END=========================================================
17  *
18  */
19
20 package org.onap.dcae.analytics.tca.web.controller;
21
22 import java.time.format.DateTimeFormatter;
23 import java.util.List;
24 import java.util.stream.Collectors;
25
26 import org.onap.dcae.analytics.model.TcaModelConstants;
27 import org.onap.dcae.analytics.model.common.ConfigSource;
28 import org.onap.dcae.analytics.tca.core.service.TcaExecutionContext;
29 import org.onap.dcae.analytics.tca.core.service.TcaResultContext;
30 import org.onap.dcae.analytics.tca.core.util.TcaUtils;
31 import org.onap.dcae.analytics.tca.model.policy.TcaPolicy;
32 import org.onap.dcae.analytics.tca.model.restapi.TcaExecutionRequest;
33 import org.onap.dcae.analytics.tca.model.restapi.TcaExecutionResponse;
34 import org.onap.dcae.analytics.tca.web.domain.TcaPolicyWrapper;
35 import org.onap.dcae.analytics.tca.web.service.TcaProcessingService;
36 import org.springframework.http.MediaType;
37 import org.springframework.http.ResponseEntity;
38 import org.springframework.web.bind.annotation.GetMapping;
39 import org.springframework.web.bind.annotation.PostMapping;
40 import org.springframework.web.bind.annotation.RequestBody;
41 import org.springframework.web.bind.annotation.RequestMapping;
42 import org.springframework.web.bind.annotation.RestController;
43
44 import io.swagger.annotations.Api;
45 import io.swagger.annotations.ApiOperation;
46
47 /**
48  * @author Rajiv Singla
49  */
50 @RestController
51 @RequestMapping(TcaModelConstants.TCA_REST_API_PREFIX)
52 @Api(value = "Provides endpoints for TCA micro service")
53 public class TcaRestController {
54
55     private final TcaProcessingService tcaProcessingService;
56     private final TcaPolicyWrapper tcaPolicyWrapper;
57
58
59     public TcaRestController(final TcaProcessingService tcaProcessingService,
60                              final TcaPolicyWrapper tcaPolicyWrapper) {
61         this.tcaProcessingService = tcaProcessingService;
62         this.tcaPolicyWrapper = tcaPolicyWrapper;
63     }
64
65     @GetMapping(value = TcaModelConstants.TCA_POLICY_ENDPOINT, produces = MediaType.APPLICATION_JSON_VALUE)
66     @ApiOperation(value = "Provides current TCA Policy")
67     public ResponseEntity<TcaPolicy> getTcaPolicy() {
68         return getTcaPolicyResponse(tcaPolicyWrapper);
69     }
70
71     @PostMapping(value = TcaModelConstants.TCA_POLICY_ENDPOINT, produces = MediaType.APPLICATION_JSON_VALUE,
72             consumes = MediaType.APPLICATION_JSON_VALUE)
73     @ApiOperation(value = "Sets new value for TCA Policy and returns current Policy")
74     public ResponseEntity<TcaPolicy> setTcaPolicy(@RequestBody final TcaPolicy tcaPolicy) {
75         tcaPolicyWrapper.setTcaPolicy(tcaPolicy, ConfigSource.REST_API);
76         return getTcaPolicyResponse(tcaPolicyWrapper);
77     }
78
79
80     @PostMapping(value = TcaModelConstants.TCA_EXECUTION_ENDPOINT, produces = MediaType.APPLICATION_JSON_VALUE,
81             consumes = MediaType.APPLICATION_JSON_VALUE)
82     @ApiOperation(value = "Applies TCA to provided execution request and generated TCA execution response")
83     public ResponseEntity<List<TcaExecutionResponse>> execute(@RequestBody final TcaExecutionRequest
84                                                                       tcaExecutionRequest) {
85         
86         // process tca execution request
87         final List<TcaExecutionContext> executionContexts = tcaProcessingService.getTcaExecutionResults(
88                 tcaExecutionRequest.getRequestId(), tcaExecutionRequest.getTransactionId(),
89                 tcaPolicyWrapper, TcaUtils.getCefMessagesFromEventListeners
90                         (tcaExecutionRequest.getEventListeners()));
91         // create execution response
92         final List<TcaExecutionResponse> tcaExecutionResponses = executionContexts.stream().map(tcaExecutionContext -> {
93             final TcaExecutionResponse tcaExecutionResponse = new TcaExecutionResponse();
94             tcaExecutionResponse.setRequestId(tcaExecutionContext.getRequestId());
95             tcaExecutionResponse.setTransactionId(tcaExecutionContext.getTransactionId());
96             final TcaResultContext tcaResultContext = tcaExecutionContext.getTcaResultContext();
97             tcaExecutionResponse.setViolatedMetricsPerEventName(tcaResultContext.getViolatedMetricsPerEventName());
98             tcaExecutionResponse.setTcaAlert(tcaResultContext.getTcaAlert());
99             return tcaExecutionResponse;
100         }).collect(Collectors.toList());
101
102         return ResponseEntity.ok().body(tcaExecutionResponses);
103     }
104
105
106     private static ResponseEntity<TcaPolicy> getTcaPolicyResponse(final TcaPolicyWrapper tcaPolicyWrapper) {
107         return ResponseEntity.ok()
108                 .header(TcaModelConstants.TCA_POLICY_SOURCE_HEADER_KEY, tcaPolicyWrapper.getConfigSource().name())
109                 .header(TcaModelConstants.TCA_POLICY_CREATION_HEADER_KEY,
110                         tcaPolicyWrapper.getCreationTime().format(DateTimeFormatter.ISO_OFFSET_DATE_TIME))
111                 .header(TcaModelConstants.TCA_POLICY_VERSION_HEADER_KEY, tcaPolicyWrapper.getPolicyVersion())
112                 .body(tcaPolicyWrapper.getTcaPolicy());
113     }
114
115 }