for policy update work
[dcaegen2/analytics/tca-gen2.git] / dcae-analytics / dcae-analytics-tca-web / src / main / java / org / onap / dcae / analytics / tca / web / service / TcaProcessingServiceImpl.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.service;
21
22 import static org.onap.dcae.analytics.web.util.AnalyticsWebUtils.RANDOM_ID_SUPPLIER;
23 import static org.onap.dcae.analytics.web.util.AnalyticsWebUtils.REQUEST_ID_SUPPLIER;
24 import static org.onap.dcae.analytics.web.util.ValidationUtils.isPresent;
25
26 import java.util.List;
27 import java.util.stream.Collectors;
28 import java.util.stream.IntStream;
29
30 import org.onap.dcae.analytics.model.AnalyticsModelConstants;
31 import org.onap.dcae.analytics.tca.core.service.GenericTcaExecutionContext;
32 import org.onap.dcae.analytics.tca.core.service.GenericTcaProcessingContext;
33 import org.onap.dcae.analytics.tca.core.service.GenericTcaResultContext;
34 import org.onap.dcae.analytics.tca.core.service.TcaAaiEnrichmentContext;
35 import org.onap.dcae.analytics.tca.core.service.TcaAbatementContext;
36 import org.onap.dcae.analytics.tca.core.service.TcaExecutionContext;
37 import org.onap.dcae.analytics.tca.core.util.TcaUtils;
38 import org.onap.dcae.analytics.tca.core.util.function.calculation.TcaCalculator;
39 import org.onap.dcae.analytics.tca.model.policy.TcaPolicy;
40 import org.onap.dcae.analytics.tca.web.domain.TcaPolicyWrapper;
41
42 /**
43  * @author Rajiv Singla
44  */
45 public class TcaProcessingServiceImpl implements TcaProcessingService {
46
47     private final TcaAbatementContext tcaAbatementContext;
48     private final TcaAaiEnrichmentContext tcaAaiEnrichmentContext;
49
50     public TcaProcessingServiceImpl(final TcaAbatementContext tcaAbatementContext,
51                                     final TcaAaiEnrichmentContext tcaAaiEnrichmentContext) {
52         this.tcaAbatementContext = tcaAbatementContext;
53         this.tcaAaiEnrichmentContext = tcaAaiEnrichmentContext;
54     }
55
56
57     @Override
58     public List<TcaExecutionContext> getTcaExecutionResults(final String requestId,
59                                                             final String transactionId,
60                                                             final TcaPolicyWrapper tcaPolicyWrapper,
61                                                             final List<String> cefMessages) {
62         // create tca policy deep copy as it should be same for current execution
63         final TcaPolicy tcaPolicyDeepCopy = TcaUtils.getTcaPolicyDeepCopy(tcaPolicyWrapper.getTcaPolicy());
64         // create new request id if not present
65         final String executionRequestId = isPresent(requestId) ? requestId : REQUEST_ID_SUPPLIER.get();
66         // create transaction id if not present
67         final String executionTransactionId = isPresent(transactionId) ? transactionId : RANDOM_ID_SUPPLIER.get();
68
69         return IntStream.range(0, cefMessages.size())
70                 // generate initial Processing contexts
71                 .mapToObj(cefMessageIndex -> GenericTcaExecutionContext.builder()
72                         .requestId(executionRequestId +
73                                 AnalyticsModelConstants.ANALYTICS_REQUEST_ID_DELIMITER + cefMessageIndex)
74                         .transactionId(executionTransactionId)
75                         .messageIndex(cefMessageIndex)
76                         .cefMessage(cefMessages.get(cefMessageIndex))
77                         .tcaPolicy(tcaPolicyDeepCopy)
78                         .tcaProcessingContext(new GenericTcaProcessingContext())
79                         .tcaAbatementContext(tcaAbatementContext)
80                         .tcaAaiEnrichmentContext(tcaAaiEnrichmentContext)
81                         .tcaResultContext(new GenericTcaResultContext())
82                         .build())
83                 // apply tca calculator
84                 .map(new TcaCalculator())
85                 // return result
86                 .collect(Collectors.toList());
87     }
88 }