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