efd09a2a096e704bade7ba3b0e2b0ae67cd45e5c
[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.IntentGoalType;
21 import org.onap.usecaseui.intentanalysis.bean.models.Expectation;
22 import org.onap.usecaseui.intentanalysis.bean.models.Intent;
23 import org.onap.usecaseui.intentanalysis.bean.models.IntentGoalBean;
24 import org.onap.usecaseui.intentanalysis.cllBusinessIntentMgt.CLLBusinessIntentManagementFunction;
25 import org.onap.usecaseui.intentanalysis.common.ResponseConsts;
26 import org.onap.usecaseui.intentanalysis.exception.IntentInputException;
27 import org.onap.usecaseui.intentanalysis.intentBaseService.IntentManagementFunction;
28 import org.onap.usecaseui.intentanalysis.intentBaseService.contextService.IntentContextService;
29 import org.onap.usecaseui.intentanalysis.intentBaseService.intentModule.DecisionModule;
30 import org.onap.usecaseui.intentanalysis.service.IntentService;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.context.ApplicationContext;
33 import org.springframework.stereotype.Component;
34
35 import java.util.ArrayList;
36 import java.util.LinkedHashMap;
37 import java.util.List;
38 import java.util.Locale;
39 import java.util.stream.Collectors;
40 @Slf4j
41 @Component
42 public class FormatIntentInputDecisionModule extends DecisionModule {
43     @Autowired
44     ApplicationContext applicationContext;
45
46     @Autowired
47     public IntentContextService intentContextService;
48
49     @Autowired
50     public IntentService intentService;
51
52     @Override
53     public void determineUltimateGoal() {
54     }
55
56     @Override
57     public IntentManagementFunction exploreIntentHandlers(IntentGoalBean intentGoalBean) {
58         // if intentName contain cll  return
59         if (intentGoalBean.getIntent().getIntentName().toLowerCase(Locale.ROOT).contains("cll")) {
60             return (IntentManagementFunction) applicationContext.getBean(CLLBusinessIntentManagementFunction.class.getSimpleName());
61         }else{
62             String msg = String.format("intentName is: %s can't find corresponding IntentManagementFunction,please check Intent Name",intentGoalBean.getIntent().getIntentName());
63             log.error(msg);
64             throw new IntentInputException(msg, ResponseConsts.RET_FIND_CORRESPONDING_FAIL);
65         }
66     }
67
68
69     @Override
70     public void decideSuitableAction() {
71     }
72
73     @Override
74     public void interactWithTemplateDb() {
75     }
76
77     @Override
78     public LinkedHashMap<IntentGoalBean, IntentManagementFunction> investigationCreateProcess(IntentGoalBean intentGoalBean) {
79        log.info("FormatIntentInputMgt investigation create process start");
80         LinkedHashMap<IntentGoalBean, IntentManagementFunction> intentMap = new LinkedHashMap<>();
81         boolean needDecompostion = needDecompostion(intentGoalBean);
82         log.debug("FormatIntentInputMgt need decompose :"+ needDecompostion);
83         if (needDecompostion) {
84             intentDecomposition(intentGoalBean);
85         } else {
86             intentMap.put(intentGoalBean, exploreIntentHandlers(intentGoalBean));
87         }
88         log.info("FormatIntentInputMgt investigation create process finished");
89         return intentMap;
90     }
91
92     public boolean needDecompostion(IntentGoalBean intentGoalBean) {
93         //expectationName just contain cll and slicing need decompost
94         List<Expectation> intentExpectations = intentGoalBean.getIntent().getIntentExpectations();
95         List<String> expectationNameList = intentExpectations.stream().map(Expectation::getExpectationName)
96                 .distinct().collect(Collectors.toList());
97         if (expectationNameList.size() > 1) {
98             List<String> cllList = expectationNameList.stream().filter(x -> StringUtils.containsIgnoreCase(x, "cll")).collect(Collectors.toList());
99             List<String> slicingList = expectationNameList.stream().filter(x -> StringUtils.containsIgnoreCase(x, "slicing")).collect(Collectors.toList());
100             if (cllList.size() > 0 && slicingList.size() > 0) {
101                 return true;
102             }
103         }
104         return false;
105     }
106
107     public List<IntentGoalBean> intentDecomposition(IntentGoalBean intentGoalBean) {
108         //todo
109         return null;
110     }
111
112     @Override
113     //format is
114     public LinkedHashMap<IntentGoalBean, IntentManagementFunction> investigationUpdateProcess(IntentGoalBean intentGoalBean) {
115         log.info("FormatIntentInputMgt investigation update process start");
116         //get format-cll intent
117         LinkedHashMap<IntentGoalBean, IntentManagementFunction> intentMap = new LinkedHashMap<>();
118         // update format-cll intentContext
119         Intent intent1 = intentService.getIntent(intentGoalBean.getIntent().getIntentId());
120         intentGoalBean.getIntent().setIntentContexts(intent1.getIntentContexts());
121         List<Intent> subIntentList = intentContextService.getSubIntentInfoFromContext(intentGoalBean.getIntent());
122         for (Intent intent : subIntentList) {
123             IntentManagementFunction intentHandlerInfo = intentContextService.getHandlerInfo(intent);
124            // UpdateIntentInfo(intentGoalBean.getIntent(), intent);//new process move  to defineProcess
125             IntentGoalBean subIntentGoalBean = new IntentGoalBean(intent, IntentGoalType.UPDATE);
126             intentMap.put(subIntentGoalBean, intentHandlerInfo);
127         }
128         log.info("FormatIntentInputMgt investigation update process finished");
129         return intentMap;
130     }
131
132     public void UpdateIntentInfo(Intent originIntent, Intent intent){
133
134         List<Expectation> originIntentExpectationList = originIntent.getIntentExpectations();
135         List<Expectation> intentExpectationList = intent.getIntentExpectations();
136         int newIntentExpectationNum = originIntentExpectationList.size();
137         int oldIntentExpectationNum = intentExpectationList.size();
138
139         List<Expectation> changeList = new ArrayList<>();
140         if (newIntentExpectationNum != oldIntentExpectationNum){
141             if (newIntentExpectationNum < oldIntentExpectationNum){
142
143                 for (Expectation oldExpectation : intentExpectationList) {//search
144                     boolean bFindExpectation = false;
145                     for (Expectation newExpectation : originIntentExpectationList) {//param
146                         if (oldExpectation.getExpectationName().equals(newExpectation.getExpectationName())){
147                             bFindExpectation = true;
148                             break;
149                         }
150                     }
151                     if (bFindExpectation){
152                         changeList.add(oldExpectation);
153                     }
154                 }
155             }
156         }
157         intent.setIntentExpectations(changeList);
158     }
159
160     @Override
161     public LinkedHashMap<IntentGoalBean, IntentManagementFunction> investigationDeleteProcess(IntentGoalBean intentGoalBean) {
162         LinkedHashMap<IntentGoalBean, IntentManagementFunction> intentMap = new LinkedHashMap<>();
163         List<Intent> subIntentList = intentContextService.getSubIntentInfoFromContext(intentGoalBean.getIntent());
164         for (Intent intent : subIntentList) {
165             IntentManagementFunction intentHandlerInfo = intentContextService.getHandlerInfo(intent);
166             IntentGoalBean subIntentGoalBean = new IntentGoalBean(intent, IntentGoalType.DELETE);
167             intentMap.put(subIntentGoalBean, intentHandlerInfo);
168         }
169         return intentMap;
170     }
171
172 }