d4c0f88698e97000a814cc0a206c6836c9fc5b55
[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.IntentInstanceService;
31 import org.onap.usecaseui.intentanalysis.service.IntentReportService;
32 import org.onap.usecaseui.intentanalysis.util.CommonUtil;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.stereotype.Service;
35 import org.springframework.transaction.annotation.Transactional;
36 import org.springframework.util.CollectionUtils;
37
38 import java.util.Collections;
39 import java.util.List;
40 import java.util.stream.Collectors;
41
42 import static org.onap.usecaseui.intentanalysis.common.ResponseConsts.RSEPONSE_SUCCESS;
43
44 @Service
45 @Slf4j
46 public class IntentReportServiceImpl implements IntentReportService {
47
48     @Autowired
49     private FulfillmentInfoService fulfillmentInfoService;
50
51     @Autowired
52     private ObjectInstanceMapper objectInstanceMapper;
53
54     @Autowired
55     private IntentReportFulfillmentInfoMapper intentReportFulfillmentInfoMapper;
56
57     @Autowired
58     private IntentReportMapper intentReportMapper;
59
60     @Autowired
61     private IntentInstanceService intentInstanceService;
62
63     @Override
64     @Transactional(rollbackFor = DataBaseException.class)
65     public ServiceResult getIntentReportByIntentId(String intentId) {
66         FulfillmentInfo fulfillmentInfo = getFulfillmentInfo(intentId);
67
68         if (fulfillmentInfo == null) {
69             return new ServiceResult(new ResultHeader(RSEPONSE_SUCCESS, "The intent has not fulfillmentInfo"),
70                     new IntentReport());
71         }
72         fulfillmentInfo.setObjectInstances(getInstances(intentId));
73         IntentReport intentReport = new IntentReport();
74         intentReport.setIntentReportId(CommonUtil.getUUid());
75         intentReport.setIntentReference(intentInstanceService.queryIntentInstanceId(intentId));
76         intentReport.setFulfillmentInfos(Collections.singletonList(fulfillmentInfo));
77         intentReport.setReportTime(CommonUtil.getTime());
78
79         saveIntentReport(intentReport, fulfillmentInfo);
80         return new ServiceResult(new ResultHeader(RSEPONSE_SUCCESS, "Get report success"),
81                 intentReport);
82     }
83
84     /**
85      * Generate intention reports on a regular basis and save them in the database
86      *
87      * @param intentId intentId
88      */
89     @Override
90     @Transactional(rollbackFor = DataBaseException.class)
91     public void saveIntentReportByIntentId(String intentId) {
92         FulfillmentInfo fulfillmentInfo = fulfillmentInfoService.getFulfillmentInfo(intentId);
93         if (fulfillmentInfo == null) {
94             log.error("The fulfillmentInfo is null");
95             return;
96         }
97         IntentReport intentReport = new IntentReport();
98         intentReport.setIntentReportId(CommonUtil.getUUid());
99         intentReport.setIntentReference(intentInstanceService.queryIntentInstanceId(intentId));
100         intentReport.setReportTime(CommonUtil.getTime());
101         saveIntentReport(intentReport, fulfillmentInfo);
102     }
103
104     @Override
105     public List<String> getIntentReportIds(String intentReference) {
106         List<String> intentReportIds = intentReportMapper.getIntentReportIds(intentReference);
107         if (CollectionUtils.isEmpty(intentReportIds)) {
108             log.error("get intentReportId is empty,intentReference is {}", intentReference);
109         }
110         return intentReportIds;
111     }
112
113     private FulfillmentInfo getFulfillmentInfo(String intentId) {
114         FulfillmentInfo fulfillmentInfo = fulfillmentInfoService.getFulfillmentInfo(intentId);
115         log.info("fulfillmentInfo is {}", fulfillmentInfo);
116         if (fulfillmentInfo == null) {
117             log.error("get fulfillmentInfo is failed,intentId is {}", intentId);
118         }
119         return fulfillmentInfo;
120     }
121
122     private List<String> getInstances(String intentId) {
123         List<String> objectInstances = objectInstanceMapper.getObjectInstances(intentId);
124         if (CollectionUtils.isEmpty(objectInstances)) {
125             log.error("get objectInstance is failed,intentId is {}", intentId);
126         }
127         return objectInstances.stream().distinct().collect(Collectors.toList());
128     }
129
130     private void saveIntentReport(IntentReport intentReport, FulfillmentInfo fulfillmentInfo) {
131         int num = intentReportMapper.insertIntentReport(intentReport);
132         if (num < 1) {
133             String msg = "Failed to insert intent report to database.";
134             log.error(msg);
135             throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL);
136         }
137         int fulfillmentNum = intentReportFulfillmentInfoMapper.insertIntentReportFulfillment(fulfillmentInfo, intentReport.getIntentReportId());
138         if (fulfillmentNum < 1) {
139             String msg = "Failed to insert fulfillmentInfo to database.";
140             log.error(msg);
141             throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL);
142         }
143     }
144 }