4ca532e425dbf6708a0234653ec0db0078ca836c
[usecase-ui/intent-analysis.git] /
1 /*
2  * Copyright (C) 2022 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 package org.onap.usecaseui.intentanalysis.formatintentinputMgt.formatintentinputModule;
17
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;
36
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;
45
46 @Slf4j
47 @Component
48 public class FormatIntentInputDecisionModule extends DecisionModule {
49     @Autowired
50     ApplicationContext applicationContext;
51
52     @Autowired
53     public IntentContextService intentContextService;
54
55     @Autowired
56     public IntentService intentService;
57
58     @Autowired
59     private IntentReportService intentReportService;
60
61     @Resource(name = "intentReportExecutor")
62     private ScheduledThreadPoolExecutor executor;
63
64     @Override
65     public void determineUltimateGoal() {
66     }
67
68     @Override
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());
73         } else {
74             String msg = String.format("intentName is: %s can't find corresponding IntentManagementFunction,please check Intent Name", intentGoalBean.getIntent().getIntentName());
75             log.error(msg);
76             throw new IntentInputException(msg, ResponseConsts.RET_FIND_CORRESPONDING_FAIL);
77         }
78     }
79
80
81     @Override
82     public void decideSuitableAction() {
83     }
84
85     @Override
86     public void interactWithTemplateDb() {
87     }
88
89     @Override
90     public LinkedHashMap<IntentGoalBean, IntentManagementFunction> investigationCreateProcess(IntentGoalBean intentGoalBean) {
91         log.info("FormatIntentInputMgt investigation create process start");
92         LinkedHashMap<IntentGoalBean, IntentManagementFunction> intentMap = new LinkedHashMap<>();
93
94         IntentGoalBean newIntentGoalBean = removeReportExpectation(intentGoalBean);
95
96         boolean needDecompostion = needDecompostion(newIntentGoalBean);
97         log.debug("FormatIntentInputMgt need decompose :" + needDecompostion);
98         if (needDecompostion) {
99             intentDecomposition(newIntentGoalBean);
100         } else {
101             intentMap.put(newIntentGoalBean, exploreIntentHandlers(newIntentGoalBean));
102         }
103         log.info("FormatIntentInputMgt investigation create process finished");
104         return intentMap;
105     }
106
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) {
116                 return true;
117             }
118         }
119         return false;
120     }
121
122     public List<IntentGoalBean> intentDecomposition(IntentGoalBean intentGoalBean) {
123         //todo
124         return null;
125     }
126
127     @Override
128     //format is
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);
141         }
142         log.info("FormatIntentInputMgt investigation update process finished");
143         return intentMap;
144     }
145
146     public void UpdateIntentInfo(Intent originIntent, Intent intent) {
147
148         List<Expectation> originIntentExpectationList = originIntent.getIntentExpectations();
149         List<Expectation> intentExpectationList = intent.getIntentExpectations();
150         int newIntentExpectationNum = originIntentExpectationList.size();
151         int oldIntentExpectationNum = intentExpectationList.size();
152
153         List<Expectation> changeList = new ArrayList<>();
154         if (newIntentExpectationNum != oldIntentExpectationNum) {
155             if (newIntentExpectationNum < oldIntentExpectationNum) {
156
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;
162                             break;
163                         }
164                     }
165                     if (bFindExpectation) {
166                         changeList.add(oldExpectation);
167                     }
168                 }
169             }
170         }
171         intent.setIntentExpectations(changeList);
172     }
173
174     @Override
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);
182         }
183         return intentMap;
184     }
185
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;
195         }
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());
204     }
205
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());
210             return;
211         }
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());
216             return;
217         }
218         Condition condition = targetConditions.get(0);
219         try {
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());
225         }
226     }
227 }