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