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
9 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.dcae.analytics.tca.web.controller;
22 import io.swagger.annotations.Api;
23 import io.swagger.annotations.ApiOperation;
25 import java.time.format.DateTimeFormatter;
26 import java.util.List;
27 import java.util.stream.Collectors;
29 import org.onap.dcae.analytics.model.TcaModelConstants;
30 import org.onap.dcae.analytics.model.common.ConfigSource;
31 import org.onap.dcae.analytics.tca.core.service.TcaExecutionContext;
32 import org.onap.dcae.analytics.tca.core.service.TcaResultContext;
33 import org.onap.dcae.analytics.tca.model.policy.TcaPolicy;
34 import org.onap.dcae.analytics.tca.model.restapi.TcaExecutionRequest;
35 import org.onap.dcae.analytics.tca.model.restapi.TcaExecutionResponse;
36 import org.onap.dcae.analytics.tca.web.domain.TcaPolicyWrapper;
37 import org.onap.dcae.analytics.tca.web.service.TcaProcessingService;
38 import org.onap.dcae.analytics.tca.web.util.TcaUtils;
39 import org.springframework.http.MediaType;
40 import org.springframework.http.ResponseEntity;
41 import org.springframework.web.bind.annotation.GetMapping;
42 import org.springframework.web.bind.annotation.PostMapping;
43 import org.springframework.web.bind.annotation.RequestBody;
44 import org.springframework.web.bind.annotation.RequestMapping;
45 import org.springframework.web.bind.annotation.RestController;
48 * @author Rajiv Singla
51 @RequestMapping(TcaModelConstants.TCA_REST_API_PREFIX)
52 @Api(value = "Provides endpoints for TCA micro service")
53 public class TcaRestController {
55 private final TcaProcessingService tcaProcessingService;
56 private final TcaPolicyWrapper tcaPolicyWrapper;
59 public TcaRestController(final TcaProcessingService tcaProcessingService,
60 final TcaPolicyWrapper tcaPolicyWrapper) {
61 this.tcaProcessingService = tcaProcessingService;
62 this.tcaPolicyWrapper = tcaPolicyWrapper;
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);
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);
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 // process tca execution request
86 final List<TcaExecutionContext> executionContexts = tcaProcessingService.getTcaExecutionResults(
87 tcaExecutionRequest.getRequestId(), tcaExecutionRequest.getTransactionId(),
88 tcaExecutionRequest.getTcaPolicy(), TcaUtils.getCefMessagesFromEventListeners
89 (tcaExecutionRequest.getEventListeners()));
90 // create execution response
91 final List<TcaExecutionResponse> tcaExecutionResponses = executionContexts.stream().map(tcaExecutionContext -> {
92 final TcaExecutionResponse tcaExecutionResponse = new TcaExecutionResponse();
93 tcaExecutionResponse.setRequestId(tcaExecutionContext.getRequestId());
94 tcaExecutionResponse.setTransactionId(tcaExecutionContext.getTransactionId());
95 final TcaResultContext tcaResultContext = tcaExecutionContext.getTcaResultContext();
96 tcaExecutionResponse.setViolatedMetricsPerEventName(tcaResultContext.getViolatedMetricsPerEventName());
97 tcaExecutionResponse.setTcaAlert(tcaResultContext.getTcaAlert());
98 return tcaExecutionResponse;
99 }).collect(Collectors.toList());
101 return ResponseEntity.ok().body(tcaExecutionResponses);
105 private static ResponseEntity<TcaPolicy> getTcaPolicyResponse(final TcaPolicyWrapper tcaPolicyWrapper) {
106 return ResponseEntity.ok()
107 .header(TcaModelConstants.TCA_POLICY_SOURCE_HEADER_KEY, tcaPolicyWrapper.getConfigSource().name())
108 .header(TcaModelConstants.TCA_POLICY_CREATION_HEADER_KEY,
109 tcaPolicyWrapper.getCreationTime().format(DateTimeFormatter.ISO_OFFSET_DATE_TIME))
110 .header(TcaModelConstants.TCA_POLICY_VERSION_HEADER_KEY, tcaPolicyWrapper.getPolicyVersion())
111 .body(tcaPolicyWrapper.getTcaPolicy());