334a6d3f45633e9444740b04a379e8b03e26ea8c
[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.*;
22 import org.onap.usecaseui.intentanalysis.cllBusinessIntentMgt.CLLBusinessIntentManagementFunction;
23 import org.onap.usecaseui.intentanalysis.common.ResponseConsts;
24 import org.onap.usecaseui.intentanalysis.exception.IntentInputException;
25 import org.onap.usecaseui.intentanalysis.intentBaseService.IntentManagementFunction;
26 import org.onap.usecaseui.intentanalysis.intentBaseService.contextService.IntentContextService;
27 import org.onap.usecaseui.intentanalysis.intentBaseService.intentModule.DecisionModule;
28 import org.onap.usecaseui.intentanalysis.service.IntentService;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.context.ApplicationContext;
31 import org.springframework.stereotype.Component;
32
33 import java.util.ArrayList;
34 import java.util.LinkedHashMap;
35 import java.util.List;
36 import java.util.Locale;
37 import java.util.stream.Collectors;
38
39 @Slf4j
40 @Component
41 public class FormatIntentInputDecisionModule extends DecisionModule {
42     @Autowired
43     ApplicationContext applicationContext;
44
45     @Autowired
46     public IntentContextService intentContextService;
47
48     @Autowired
49     public IntentService intentService;
50
51     @Override
52     public void determineUltimateGoal() {
53     }
54
55     @Override
56     public IntentManagementFunction exploreIntentHandlers(IntentGoalBean intentGoalBean) {
57         // if intentName contain cll  return
58         if (intentGoalBean.getIntent().getIntentName().toLowerCase(Locale.ROOT).contains("cll")) {
59             return (IntentManagementFunction) applicationContext.getBean(CLLBusinessIntentManagementFunction.class.getSimpleName());
60         } else {
61             String msg = String.format("intentName is: %s can't find corresponding IntentManagementFunction,please check Intent Name", intentGoalBean.getIntent().getIntentName());
62             log.error(msg);
63             throw new IntentInputException(msg, ResponseConsts.RET_FIND_CORRESPONDING_FAIL);
64         }
65     }
66
67
68     @Override
69     public void decideSuitableAction() {
70     }
71
72     @Override
73     public void interactWithTemplateDb() {
74     }
75
76     @Override
77     public LinkedHashMap<IntentGoalBean, IntentManagementFunction> investigationCreateProcess(IntentGoalBean intentGoalBean) {
78         log.info("FormatIntentInputMgt investigation create process start");
79         LinkedHashMap<IntentGoalBean, IntentManagementFunction> intentMap = new LinkedHashMap<>();
80
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             IntentGoalBean subIntentGoalBean = new IntentGoalBean(intent, IntentGoalType.UPDATE);
125             intentMap.put(subIntentGoalBean, intentHandlerInfo);
126         }
127         log.info("FormatIntentInputMgt investigation update process finished");
128         return intentMap;
129     }
130
131     public void UpdateIntentInfo(Intent originIntent, Intent intent) {
132
133         List<Expectation> originIntentExpectationList = originIntent.getIntentExpectations();
134         List<Expectation> intentExpectationList = intent.getIntentExpectations();
135         int newIntentExpectationNum = originIntentExpectationList.size();
136         int oldIntentExpectationNum = intentExpectationList.size();
137
138         List<Expectation> changeList = new ArrayList<>();
139         if (newIntentExpectationNum != oldIntentExpectationNum) {
140             if (newIntentExpectationNum < oldIntentExpectationNum) {
141
142                 for (Expectation oldExpectation : intentExpectationList) {//search
143                     boolean bFindExpectation = false;
144                     for (Expectation newExpectation : originIntentExpectationList) {//param
145                         if (oldExpectation.getExpectationName().equals(newExpectation.getExpectationName())) {
146                             bFindExpectation = true;
147                             break;
148                         }
149                     }
150                     if (bFindExpectation) {
151                         changeList.add(oldExpectation);
152                     }
153                 }
154             }
155         }
156         intent.setIntentExpectations(changeList);
157     }
158
159     @Override
160     public LinkedHashMap<IntentGoalBean, IntentManagementFunction> investigationDeleteProcess(IntentGoalBean intentGoalBean) {
161         LinkedHashMap<IntentGoalBean, IntentManagementFunction> intentMap = new LinkedHashMap<>();
162         List<Intent> subIntentList = intentContextService.getSubIntentInfoFromContext(intentGoalBean.getIntent());
163         for (Intent intent : subIntentList) {
164             IntentManagementFunction intentHandlerInfo = intentContextService.getHandlerInfo(intent);
165             IntentGoalBean subIntentGoalBean = new IntentGoalBean(intent, IntentGoalType.DELETE);
166             intentMap.put(subIntentGoalBean, intentHandlerInfo);
167         }
168         return intentMap;
169     }
170 }