ce36d6d3f9bf7a53a2a432041366ea72f1ee2157
[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 java.util.ArrayList;
19 import org.apache.commons.lang.StringUtils;
20 import org.onap.usecaseui.intentanalysis.bean.enums.IntentGoalType;
21 import org.onap.usecaseui.intentanalysis.bean.enums.OperatorType;
22 import org.onap.usecaseui.intentanalysis.bean.models.Condition;
23 import org.onap.usecaseui.intentanalysis.bean.models.Context;
24 import org.onap.usecaseui.intentanalysis.bean.models.Expectation;
25 import org.onap.usecaseui.intentanalysis.bean.models.Intent;
26 import org.onap.usecaseui.intentanalysis.bean.models.IntentGoalBean;
27 import org.onap.usecaseui.intentanalysis.cllBusinessIntentMgt.CLLBusinessIntentManagementFunction;
28 import org.onap.usecaseui.intentanalysis.intentBaseService.IntentManagementFunction;
29 import org.onap.usecaseui.intentanalysis.intentBaseService.contextService.IntentContextService;
30 import org.onap.usecaseui.intentanalysis.intentBaseService.intentModule.DecisionModule;
31 import org.onap.usecaseui.intentanalysis.service.IntentService;
32 import org.onap.usecaseui.intentanalysis.util.CommonUtil;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.context.ApplicationContext;
35 import org.springframework.stereotype.Component;
36
37 import java.util.LinkedHashMap;
38 import java.util.List;
39 import java.util.Locale;
40 import java.util.stream.Collectors;
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         }
62         return null;
63     }
64
65
66     @Override
67     public void decideSuitableAction() {
68     }
69
70     @Override
71     public void interactWithTemplateDb() {
72     }
73
74     @Override
75     public LinkedHashMap<IntentGoalBean, IntentManagementFunction> investigationCreateProcess(IntentGoalBean intentGoalBean) {
76         LinkedHashMap<IntentGoalBean, IntentManagementFunction> intentMap = new LinkedHashMap<>();
77         boolean needDecompostion = needDecompostion(intentGoalBean);
78         if (needDecompostion) {
79             intentDecomposition(intentGoalBean);
80         } else {
81             intentMap.put(intentGoalBean, exploreIntentHandlers(intentGoalBean));
82         }
83         return intentMap;
84     }
85
86     public boolean needDecompostion(IntentGoalBean intentGoalBean) {
87         //expectationName just contain cll and slicing need decompost
88         List<Expectation> intentExpectations = intentGoalBean.getIntent().getIntentExpectations();
89         List<String> expectationNameList = intentExpectations.stream().map(Expectation::getExpectationName)
90                 .distinct().collect(Collectors.toList());
91         if (expectationNameList.size() > 1) {
92             List<String> cllList = expectationNameList.stream().filter(x -> StringUtils.containsIgnoreCase(x, "cll")).collect(Collectors.toList());
93             List<String> slicingList = expectationNameList.stream().filter(x -> StringUtils.containsIgnoreCase(x, "slicing")).collect(Collectors.toList());
94             if (cllList.size() > 0 && slicingList.size() > 0) {
95                 return true;
96             }
97         }
98         return false;
99     }
100
101     public List<IntentGoalBean> intentDecomposition(IntentGoalBean intentGoalBean) {
102         //todo
103         return null;
104     }
105
106     @Override
107     //format is
108     public LinkedHashMap<IntentGoalBean, IntentManagementFunction> investigationUpdateProcess(IntentGoalBean intentGoalBean) {
109         //get format-cll intent
110         LinkedHashMap<IntentGoalBean, IntentManagementFunction> intentMap = new LinkedHashMap<>();
111         List<Intent> subIntentList = intentContextService.getSubIntentInfoFromContext(intentGoalBean.getIntent());
112         for (Intent intent : subIntentList) {
113             IntentManagementFunction intentHandlerInfo = intentContextService.getHandlerInfo(intent);
114             UpdateIntentInfo(intentGoalBean.getIntent(), intent);
115             IntentGoalBean subIntentGoalBean = new IntentGoalBean(intent, IntentGoalType.UPDATE);
116             intentMap.put(subIntentGoalBean, intentHandlerInfo);
117         }
118         return intentMap;
119     }
120
121     public void UpdateIntentInfo(Intent originIntent, Intent intent){
122
123         List<Expectation> originIntentExpectationList = originIntent.getIntentExpectations();
124         List<Expectation> intentExpectationList = intent.getIntentExpectations();
125         int newIntentExpectationNum = originIntentExpectationList.size();
126         int oldIntentExpectationNum = intentExpectationList.size();
127
128         if (newIntentExpectationNum != oldIntentExpectationNum){
129             if (newIntentExpectationNum < oldIntentExpectationNum){
130                 boolean bFindExpectation = false;
131                 for (Expectation oldExpectation : intentExpectationList) {
132                     for (Expectation newExpectation : originIntentExpectationList) {
133                         if (oldExpectation.getExpectationName().equals(newExpectation.getExpectationName())){
134                             bFindExpectation = true;
135                         }
136                     }
137                     if (bFindExpectation == false){
138                         intentExpectationList.remove(oldExpectation);
139                     }
140                 }
141             }
142         }
143
144     }
145
146     @Override
147     public LinkedHashMap<IntentGoalBean, IntentManagementFunction> investigationDeleteProcess(IntentGoalBean intentGoalBean) {
148         LinkedHashMap<IntentGoalBean, IntentManagementFunction> intentMap = new LinkedHashMap<>();
149         List<Intent> subIntentList = intentContextService.getSubIntentInfoFromContext(intentGoalBean.getIntent());
150         for (Intent intent : subIntentList) {
151             IntentManagementFunction intentHandlerInfo = intentContextService.getHandlerInfo(intent);
152             IntentGoalBean subIntentGoalBean = new IntentGoalBean(intent, IntentGoalType.DELETE);
153             intentMap.put(subIntentGoalBean, intentHandlerInfo);
154         }
155         return intentMap;
156     }
157
158 }