2 * Copyright (C) 2022 CMCC, Inc. and others. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onap.usecaseui.intentanalysis.formatintentinputMgt.formatintentinputModule;
18 import lombok.extern.slf4j.Slf4j;
19 import org.apache.commons.lang.StringUtils;
20 import org.onap.usecaseui.intentanalysis.bean.enums.ExpectationType;
21 import org.onap.usecaseui.intentanalysis.bean.enums.IntentGoalType;
22 import org.onap.usecaseui.intentanalysis.bean.models.*;
23 import org.onap.usecaseui.intentanalysis.cllBusinessIntentMgt.CLLBusinessIntentManagementFunction;
24 import org.onap.usecaseui.intentanalysis.common.ResponseConsts;
25 import org.onap.usecaseui.intentanalysis.exception.IntentInputException;
26 import org.onap.usecaseui.intentanalysis.intentBaseService.IntentManagementFunction;
27 import org.onap.usecaseui.intentanalysis.intentBaseService.contextService.IntentContextService;
28 import org.onap.usecaseui.intentanalysis.intentBaseService.intentModule.DecisionModule;
29 import org.onap.usecaseui.intentanalysis.service.IntentReportService;
30 import org.onap.usecaseui.intentanalysis.service.IntentService;
31 import org.springframework.beans.BeanUtils;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.context.ApplicationContext;
34 import org.springframework.stereotype.Component;
35 import org.springframework.util.CollectionUtils;
37 import javax.annotation.Resource;
38 import java.util.ArrayList;
39 import java.util.LinkedHashMap;
40 import java.util.List;
41 import java.util.Locale;
42 import java.util.concurrent.ScheduledThreadPoolExecutor;
43 import java.util.concurrent.TimeUnit;
44 import java.util.stream.Collectors;
48 public class FormatIntentInputDecisionModule extends DecisionModule {
50 ApplicationContext applicationContext;
53 public IntentContextService intentContextService;
56 public IntentService intentService;
59 private IntentReportService intentReportService;
61 @Resource(name = "intentReportExecutor")
62 private ScheduledThreadPoolExecutor executor;
65 public void determineUltimateGoal() {
69 public IntentManagementFunction exploreIntentHandlers(IntentGoalBean intentGoalBean) {
70 // if intentName contain cll return
71 if (intentGoalBean.getIntent().getIntentName().toLowerCase(Locale.ROOT).contains("cll")) {
72 return (IntentManagementFunction) applicationContext.getBean(CLLBusinessIntentManagementFunction.class.getSimpleName());
74 String msg = String.format("intentName is: %s can't find corresponding IntentManagementFunction,please check Intent Name", intentGoalBean.getIntent().getIntentName());
76 throw new IntentInputException(msg, ResponseConsts.RET_FIND_CORRESPONDING_FAIL);
82 public void decideSuitableAction() {
86 public void interactWithTemplateDb() {
90 public LinkedHashMap<IntentGoalBean, IntentManagementFunction> investigationCreateProcess(IntentGoalBean intentGoalBean) {
91 log.info("FormatIntentInputMgt investigation create process start");
92 LinkedHashMap<IntentGoalBean, IntentManagementFunction> intentMap = new LinkedHashMap<>();
94 IntentGoalBean newIntentGoalBean = removeReportExpectation(intentGoalBean);
96 boolean needDecompostion = needDecompostion(newIntentGoalBean);
97 log.debug("FormatIntentInputMgt need decompose :" + needDecompostion);
98 if (needDecompostion) {
99 intentDecomposition(newIntentGoalBean);
101 intentMap.put(newIntentGoalBean, exploreIntentHandlers(newIntentGoalBean));
103 log.info("FormatIntentInputMgt investigation create process finished");
107 public boolean needDecompostion(IntentGoalBean intentGoalBean) {
108 //expectationName just contain cll and slicing need decompost
109 List<Expectation> intentExpectations = intentGoalBean.getIntent().getIntentExpectations();
110 List<String> expectationNameList = intentExpectations.stream().map(Expectation::getExpectationName)
111 .distinct().collect(Collectors.toList());
112 if (expectationNameList.size() > 1) {
113 List<String> cllList = expectationNameList.stream().filter(x -> StringUtils.containsIgnoreCase(x, "cll")).collect(Collectors.toList());
114 List<String> slicingList = expectationNameList.stream().filter(x -> StringUtils.containsIgnoreCase(x, "slicing")).collect(Collectors.toList());
115 if (cllList.size() > 0 && slicingList.size() > 0) {
122 public List<IntentGoalBean> intentDecomposition(IntentGoalBean intentGoalBean) {
129 public LinkedHashMap<IntentGoalBean, IntentManagementFunction> investigationUpdateProcess(IntentGoalBean intentGoalBean) {
130 log.info("FormatIntentInputMgt investigation update process start");
131 //get format-cll intent
132 LinkedHashMap<IntentGoalBean, IntentManagementFunction> intentMap = new LinkedHashMap<>();
133 // update format-cll intentContext
134 Intent intent1 = intentService.getIntent(intentGoalBean.getIntent().getIntentId());
135 intentGoalBean.getIntent().setIntentContexts(intent1.getIntentContexts());
136 List<Intent> subIntentList = intentContextService.getSubIntentInfoFromContext(intentGoalBean.getIntent());
137 for (Intent intent : subIntentList) {
138 IntentManagementFunction intentHandlerInfo = intentContextService.getHandlerInfo(intent);
139 IntentGoalBean subIntentGoalBean = new IntentGoalBean(intent, IntentGoalType.UPDATE);
140 intentMap.put(subIntentGoalBean, intentHandlerInfo);
142 log.info("FormatIntentInputMgt investigation update process finished");
146 public void UpdateIntentInfo(Intent originIntent, Intent intent) {
148 List<Expectation> originIntentExpectationList = originIntent.getIntentExpectations();
149 List<Expectation> intentExpectationList = intent.getIntentExpectations();
150 int newIntentExpectationNum = originIntentExpectationList.size();
151 int oldIntentExpectationNum = intentExpectationList.size();
153 List<Expectation> changeList = new ArrayList<>();
154 if (newIntentExpectationNum != oldIntentExpectationNum) {
155 if (newIntentExpectationNum < oldIntentExpectationNum) {
157 for (Expectation oldExpectation : intentExpectationList) {//search
158 boolean bFindExpectation = false;
159 for (Expectation newExpectation : originIntentExpectationList) {//param
160 if (oldExpectation.getExpectationName().equals(newExpectation.getExpectationName())) {
161 bFindExpectation = true;
165 if (bFindExpectation) {
166 changeList.add(oldExpectation);
171 intent.setIntentExpectations(changeList);
175 public LinkedHashMap<IntentGoalBean, IntentManagementFunction> investigationDeleteProcess(IntentGoalBean intentGoalBean) {
176 LinkedHashMap<IntentGoalBean, IntentManagementFunction> intentMap = new LinkedHashMap<>();
177 List<Intent> subIntentList = intentContextService.getSubIntentInfoFromContext(intentGoalBean.getIntent());
178 for (Intent intent : subIntentList) {
179 IntentManagementFunction intentHandlerInfo = intentContextService.getHandlerInfo(intent);
180 IntentGoalBean subIntentGoalBean = new IntentGoalBean(intent, IntentGoalType.DELETE);
181 intentMap.put(subIntentGoalBean, intentHandlerInfo);
186 private IntentGoalBean removeReportExpectation(IntentGoalBean intentGoalBean) {
187 Intent intent = intentGoalBean.getIntent();
188 List<Expectation> intentExpectations = intent.getIntentExpectations();
189 List<Expectation> report = intentExpectations.stream()
190 .filter(expectation -> ExpectationType.REPORT.equals(expectation.getExpectationType()))
191 .collect(Collectors.toList());
192 if (CollectionUtils.isEmpty(report)) {
193 log.info("No expectation of type report is entered");
194 return intentGoalBean;
196 generationIntentReport(report.get(0), intent.getIntentId());
197 Intent newIntent = new Intent();
198 BeanUtils.copyProperties(intent, newIntent);
199 List<Expectation> notReport = intentExpectations.stream()
200 .filter(expectation -> !ExpectationType.REPORT.equals(expectation.getExpectationType()))
201 .collect(Collectors.toList());
202 newIntent.setIntentExpectations(notReport);
203 return new IntentGoalBean(newIntent, intentGoalBean.getIntentGoalType());
206 private void generationIntentReport(Expectation expectation, String intentId) {
207 List<ExpectationTarget> expectationTargets = expectation.getExpectationTargets();
208 if (CollectionUtils.isEmpty(expectationTargets)) {
209 log.error("The expectation target is empty,expectationId is {}", expectation.getExpectationId());
212 ExpectationTarget expectationTarget = expectationTargets.get(0);
213 List<Condition> targetConditions = expectationTarget.getTargetConditions();
214 if (CollectionUtils.isEmpty(targetConditions)) {
215 log.error("The target condition is empty,expectationId is {}", expectation.getExpectationId());
218 Condition condition = targetConditions.get(0);
220 int conditionValue = Integer.parseInt(condition.getConditionValue());
221 log.info("Start executing scheduled intent report generation task ");
222 executor.scheduleAtFixedRate(() -> intentReportService.saveIntentReportByIntentId(intentId), 2, conditionValue, TimeUnit.SECONDS);
223 } catch (Exception e) {
224 log.error("The exception is {}", e.getMessage());