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