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