c443f9ea19af56e1dec7825d2292084d9112e32b
[usecase-ui/intent-analysis.git] /
1 /*
2  * Copyright (C) 2023 CMCC, Inc. and others. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.usecaseui.intentanalysis.service.impl;
18
19 import lombok.extern.slf4j.Slf4j;
20 import org.onap.usecaseui.intentanalysis.bean.models.FulfillmentInfo;
21 import org.onap.usecaseui.intentanalysis.bean.models.IntentReport;
22 import org.onap.usecaseui.intentanalysis.bean.models.ResultHeader;
23 import org.onap.usecaseui.intentanalysis.bean.models.ServiceResult;
24 import org.onap.usecaseui.intentanalysis.common.ResponseConsts;
25 import org.onap.usecaseui.intentanalysis.exception.DataBaseException;
26 import org.onap.usecaseui.intentanalysis.mapper.IntentReportFulfillmentInfoMapper;
27 import org.onap.usecaseui.intentanalysis.mapper.IntentReportMapper;
28 import org.onap.usecaseui.intentanalysis.mapper.ObjectInstanceMapper;
29 import org.onap.usecaseui.intentanalysis.service.FulfillmentInfoService;
30 import org.onap.usecaseui.intentanalysis.service.IntentReportService;
31 import org.onap.usecaseui.intentanalysis.util.CommonUtil;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.stereotype.Service;
34 import org.springframework.transaction.annotation.Transactional;
35 import org.springframework.util.CollectionUtils;
36
37 import java.util.Collections;
38 import java.util.List;
39 import java.util.stream.Collectors;
40
41 import static org.onap.usecaseui.intentanalysis.common.ResponseConsts.RSEPONSE_SUCCESS;
42
43 @Service
44 @Slf4j
45 public class IntentReportServiceImpl implements IntentReportService {
46
47     @Autowired
48     private FulfillmentInfoService fulfillmentInfoService;
49
50     @Autowired
51     private ObjectInstanceMapper objectInstanceMapper;
52
53     @Autowired
54     private IntentReportFulfillmentInfoMapper intentReportFulfillmentInfoMapper;
55
56     @Autowired
57     private IntentReportMapper intentReportMapper;
58
59     @Override
60     @Transactional(rollbackFor = DataBaseException.class)
61     public ServiceResult getIntentReportByIntentId(String intentId) {
62         FulfillmentInfo fulfillmentInfo = getFulfillmentInfo(intentId);
63
64         if (fulfillmentInfo == null) {
65             return new ServiceResult(new ResultHeader(RSEPONSE_SUCCESS, "The intent has not fulfillmentInfo"),
66                     new IntentReport());
67         }
68         fulfillmentInfo.setObjectInstances(getInstances(intentId));
69         IntentReport intentReport = new IntentReport();
70         intentReport.setIntentReportId(CommonUtil.getUUid());
71         intentReport.setIntentReference("intentReference");
72         intentReport.setFulfillmentInfos(Collections.singletonList(fulfillmentInfo));
73         intentReport.setReportTime(CommonUtil.getTime());
74
75         saveIntentReport(intentReport, fulfillmentInfo);
76         return new ServiceResult(new ResultHeader(RSEPONSE_SUCCESS, "Get report success"),
77                 intentReport);
78     }
79
80     @Override
81     @Transactional(rollbackFor = DataBaseException.class)
82     public void saveIntentReportByIntentId(String intentId) {
83         FulfillmentInfo fulfillmentInfo = fulfillmentInfoService.getFulfillmentInfo(intentId);
84         if (fulfillmentInfo == null) {
85             log.error("The fulfillmentInfo is null");
86             return;
87         }
88         IntentReport intentReport = new IntentReport();
89         intentReport.setIntentReportId(CommonUtil.getUUid());
90         intentReport.setIntentReference("intentReference");
91         intentReport.setReportTime(CommonUtil.getTime());
92         saveIntentReport(intentReport, fulfillmentInfo);
93     }
94
95     private FulfillmentInfo getFulfillmentInfo(String intentId) {
96         FulfillmentInfo fulfillmentInfo = fulfillmentInfoService.getFulfillmentInfo(intentId);
97         log.info("fulfillmentInfo is {}", fulfillmentInfo);
98         if (fulfillmentInfo == null) {
99             log.error("get fulfillmentInfo is failed,intentId is {}", intentId);
100         }
101         return fulfillmentInfo;
102     }
103
104     private List<String> getInstances(String intentId) {
105         List<String> objectInstances = objectInstanceMapper.getObjectInstances(intentId);
106         if (CollectionUtils.isEmpty(objectInstances)) {
107             log.error("get objectInstance is failed,intentId is {}", intentId);
108         }
109         return objectInstances.stream().distinct().collect(Collectors.toList());
110     }
111
112     private void saveIntentReport(IntentReport intentReport, FulfillmentInfo fulfillmentInfo) {
113         int num = intentReportMapper.insertIntentReport(intentReport);
114         if (num < 1) {
115             String msg = "Failed to insert intent report to database.";
116             log.error(msg);
117             throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL);
118         }
119         int fulfillmentNum = intentReportFulfillmentInfoMapper.insertIntentReportFulfillment(fulfillmentInfo, intentReport.getIntentReportId());
120         if (fulfillmentNum < 1) {
121             String msg = "Failed to insert fulfillmentInfo to database.";
122             log.error(msg);
123             throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL);
124         }
125     }
126 }