7ed852cf7cb9a7e8cdc45a737654e986a9d672aa
[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.cllBusinessIntentMgt.cllBusinessModule;
17
18 import lombok.extern.log4j.Log4j2;
19 import org.apache.commons.lang.StringUtils;
20 import org.onap.usecaseui.intentanalysis.bean.enums.ExpectationType;
21 import org.onap.usecaseui.intentanalysis.bean.enums.IntentGenerateType;
22 import org.onap.usecaseui.intentanalysis.bean.enums.IntentGoalType;
23 import org.onap.usecaseui.intentanalysis.bean.enums.ObjectType;
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.bean.models.IntentManagementFunctionRegInfo;
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.ImfRegInfoService;
32 import org.onap.usecaseui.intentanalysis.service.IntentService;
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.ArrayList;
38 import java.util.LinkedHashMap;
39 import java.util.List;
40 import java.util.Map;
41 import java.util.stream.Collectors;
42
43 @Log4j2
44 @Component
45 public class CLLBusinessDecisionModule extends DecisionModule {
46     @Autowired
47     private ImfRegInfoService imfRegInfoService;
48     @Autowired
49     private ApplicationContext applicationContext;
50
51     @Autowired
52     IntentService intentService;
53
54     @Autowired
55     IntentContextService intentContextService;
56
57     @Override
58     public void determineUltimateGoal() {
59     }
60
61     @Override
62     public IntentManagementFunction exploreIntentHandlers(IntentGoalBean intentGoalBean) {
63         //  db  filter imf  supportArea;
64         //SupportInterface> supportInterfaces;
65         IntentManagementFunctionRegInfo imfRegInfo = imfRegInfoService.getImfRegInfo(intentGoalBean);
66         return (IntentManagementFunction) applicationContext.getBean(imfRegInfo.getHandleName());
67     }
68
69
70     @Override
71     public void decideSuitableAction() {
72     }
73
74
75     public boolean needDecompostion(IntentGoalBean intentGoalBean) {
76         //different expectationType need decompostion  ExpectationType>1 or objtype>1
77         if (intentGoalBean.getIntentGoalType().equals(IntentGoalType.CREATE)) {
78             List<Expectation> intentExpectations = intentGoalBean.getIntent().getIntentExpectations();
79             List<ExpectationType> expectationTypeList = intentExpectations.stream()
80                     .map(Expectation::getExpectationType)
81                     .filter(expectationType -> !ExpectationType.REPORT.equals(expectationType)).distinct().collect(Collectors.toList());
82             if (expectationTypeList.size() > 1) {
83                 return true;
84             } else {
85                 List<ObjectType> objectTypeList = intentExpectations.stream().map(x ->
86                         x.getExpectationObject().getObjectType()).distinct().collect(Collectors.toList());
87                 return objectTypeList.size() > 1;
88             }
89         }
90         return false;
91     }
92
93     public List<IntentGoalBean> intentDecomposition(IntentGoalBean intentGoalBean) {
94         List<Expectation> intentExpectations = intentGoalBean.getIntent().getIntentExpectations();
95         List<Expectation> report = intentExpectations.stream()
96                 .filter(expectation -> ExpectationType.REPORT.equals(expectation.getExpectationType()))
97                 .collect(Collectors.toList());
98         //ExpectationType   expectation.ExpectationObject.objtype
99         Map<ExpectationType, List<Expectation>> expectationTypeListMap = intentExpectations.stream()
100                 .filter(expectation -> !ExpectationType.REPORT.equals(expectation.getExpectationType()))
101                 .collect(Collectors.groupingBy(Expectation::getExpectationType));
102         List<IntentGoalBean> subIntentGoalList = new ArrayList<>();
103         IntentGoalType intentGoalType = intentGoalBean.getIntentGoalType();
104         for (Map.Entry<ExpectationType, List<Expectation>> entry : expectationTypeListMap.entrySet()) {
105
106             Map<ObjectType, List<Expectation>> objTypeMap = entry.getValue().stream()
107                     .collect(Collectors.groupingBy(x -> x.getExpectationObject().getObjectType()));
108             for (Map.Entry<ObjectType, List<Expectation>> objEntry : objTypeMap.entrySet()) {
109                 IntentGoalBean subIntentGoalBean = new IntentGoalBean();
110                 Intent subIntent = new Intent();
111                 subIntent.setIntentName(objEntry.getValue().get(0).getExpectationName().replace("Expectation", "Intent"));
112                 List<Expectation> value = objEntry.getValue();
113                 value.addAll(report);
114                 subIntent.setIntentExpectations(value);
115                 subIntent.setIntentGenerateType(IntentGenerateType.SYSTEMGENARATE);
116                 //TODO      intentFulfillmentInfo intentContexts
117                 subIntentGoalBean.setIntentGoalType(intentGoalType);
118                 subIntentGoalBean.setIntent(subIntent);
119                 subIntentGoalList.add(subIntentGoalBean);
120             }
121         }
122         return subIntentGoalList;
123     }
124
125     public List<IntentGoalBean> intentOrchestration(List<IntentGoalBean> subIntentGoalList) {
126         List<IntentGoalBean> sortList = new ArrayList<>();
127         List<IntentGoalBean> deliveryGoalList = subIntentGoalList.stream().filter(x ->
128                 StringUtils.containsIgnoreCase(x.getIntent().getIntentName(), "delivery")).collect(Collectors.toList());
129         List<IntentGoalBean> assuranceGoalList = subIntentGoalList.stream().filter(x ->
130                 StringUtils.containsIgnoreCase(x.getIntent().getIntentName(), "assurance")).collect(Collectors.toList());
131         List<IntentGoalBean> otherGoalList = subIntentGoalList.stream().filter(x ->
132                 !StringUtils.containsIgnoreCase(x.getIntent().getIntentName(), "delivery")
133                         && !StringUtils.containsIgnoreCase(x.getIntent().getIntentName(), "assurance")).collect(Collectors.toList());
134         sortList.addAll(deliveryGoalList);
135         sortList.addAll(assuranceGoalList);
136         sortList.addAll(otherGoalList);
137         return sortList;
138     }
139
140     @Override
141     public void interactWithTemplateDb() {
142     }
143
144     @Override
145     public LinkedHashMap<IntentGoalBean, IntentManagementFunction> investigationCreateProcess(IntentGoalBean intentGoalBean) {
146         log.info("CLLBusinessIntentManagementFunction investigation create process start");
147         boolean needDecompostion = needDecompostion(intentGoalBean);
148         log.debug("CLLBusinessIntentManagementFunction need decompose :" + needDecompostion);
149         LinkedHashMap<IntentGoalBean, IntentManagementFunction> intentMap = new LinkedHashMap<>();
150         if (needDecompostion) {
151             List<IntentGoalBean> subIntentGoalList = intentDecomposition(intentGoalBean);
152             List<IntentGoalBean> sortList = intentOrchestration(subIntentGoalList);
153             for (IntentGoalBean subIntentGoal : sortList) {
154                 IntentManagementFunction imf = exploreIntentHandlers(subIntentGoal);
155                 intentMap.put(subIntentGoal, imf);
156             }
157         } else {
158             intentMap.put(intentGoalBean, exploreIntentHandlers(intentGoalBean));
159         }
160         log.info("CLLBusinessIntentManagementFunction investigation create process finished");
161         log.debug("CLLBusinessIntentManagementFunction decomposed subIntent list name :"
162                 + StringUtils.join(intentMap.keySet().stream().map(IntentGoalBean::getIntent)
163                 .map(Intent::getIntentName).collect(Collectors.toList()), ","));
164         return intentMap;
165     }
166
167
168     @Override
169     //format is
170     public LinkedHashMap<IntentGoalBean, IntentManagementFunction> investigationUpdateProcess(IntentGoalBean intentGoalBean) {
171         log.info("CLLBusinessIntentManagementFunction investigation update process start");
172         //get cll-delivery cll-assurance intent
173         Intent originIntent = intentGoalBean.getIntent();
174         List<Expectation> originIntentExpectationList = originIntent.getIntentExpectations();
175
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             boolean bFindIntent = false;
181             for (Expectation originExpectation : originIntentExpectationList) {
182                 if (intent.getIntentName().replace("Intent", "")
183                         .equals(originExpectation.getExpectationName().replace("Expectation", ""))) {
184                     bFindIntent = true;
185                     break;
186                 }
187             }
188
189             if (false == bFindIntent) {
190                 intentContextService.deleteSubIntentContext(originIntent, intent.getIntentId());
191                 IntentGoalBean subIntentGoalBean = new IntentGoalBean(intent, IntentGoalType.DELETE);
192                 intentMap.put(subIntentGoalBean, intentHandlerInfo);
193             } else {
194                 IntentGoalBean subIntentGoalBean = new IntentGoalBean(intent, IntentGoalType.UPDATE);
195                 intentMap.put(subIntentGoalBean, intentHandlerInfo);
196             }
197         }
198         log.info("CLLBusinessIntentManagementFunction investigation update process finished");
199         log.debug("CLLBusinessIntentManagementFunction investigation update process intent list name "
200                 + StringUtils.join(intentMap.keySet().stream().map(IntentGoalBean::getIntent)
201                 .map(Intent::getIntentName).collect(Collectors.toList()), ","));
202         return intentMap;
203     }
204
205     @Override
206     public LinkedHashMap<IntentGoalBean, IntentManagementFunction> investigationDeleteProcess(IntentGoalBean intentGoalBean) {
207         LinkedHashMap<IntentGoalBean, IntentManagementFunction> intentMap = new LinkedHashMap<>();
208         List<Intent> subIntentList = intentContextService.getSubIntentInfoFromContext(intentGoalBean.getIntent());
209         for (Intent intent : subIntentList) {
210             IntentManagementFunction intentHandlerInfo = intentContextService.getHandlerInfo(intent);
211             IntentGoalBean subIntentGoalBean = new IntentGoalBean(intent, IntentGoalType.DELETE);
212             intentMap.put(subIntentGoalBean, intentHandlerInfo);
213         }
214         return intentMap;
215     }
216 }