722fce938a4c8d26a08f2521acfc8be0d864c9e6
[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.intentModule.DecisionModule;
28 import org.onap.usecaseui.intentanalysis.service.ImfRegInfoService;
29 import org.onap.usecaseui.intentanalysis.util.CommonUtil;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.context.ApplicationContext;
32 import org.springframework.stereotype.Component;
33
34 import java.util.*;
35 import java.util.stream.Collectors;
36
37 @Log4j2
38 @Component
39 public class CLLBusinessDecisionModule extends DecisionModule {
40     @Autowired
41     private ImfRegInfoService imfRegInfoService;
42     @Autowired
43     private ApplicationContext applicationContext;
44
45     @Override
46     public void determineUltimateGoal() {
47     }
48
49     @Override
50     public IntentManagementFunction exploreIntentHandlers(IntentGoalBean intentGoalBean) {
51         //  db  filter imf  supportArea;
52         //SupportInterface> supportInterfaces;
53         IntentManagementFunctionRegInfo imfRegInfo = imfRegInfoService.getImfRegInfo(intentGoalBean);
54         return (IntentManagementFunction) applicationContext.getBean(imfRegInfo.getHandleName());
55     }
56
57
58     @Override
59     public void decideSuitableAction() {
60     }
61
62
63     public boolean needDecompostion(IntentGoalBean intentGoalBean) {
64         //different expectationType need decompostion  ExpectationType>1 or objtype>1
65         if (intentGoalBean.getIntentGoalType().equals(IntentGoalType.CREATE)) {
66             List<Expectation> intentExpectations = intentGoalBean.getIntent().getIntentExpectations();
67             List<ExpectationType> expectationTypeList = intentExpectations.stream()
68                     .map(Expectation::getExpectationType).distinct().collect(Collectors.toList());
69             if (expectationTypeList.size() > 1) {
70                 return true;
71             } else {
72                 List<ObjectType> objectTypeList = intentExpectations.stream().map(x ->
73                         x.getExpectationObject().getObjectType()).distinct().collect(Collectors.toList());
74                 if (objectTypeList.size() > 1) {
75                     return true;
76                 }
77             }
78         }
79         return false;
80     }
81
82     public List<IntentGoalBean> intentDecomposition(IntentGoalBean intentGoalBean) {
83         //ExpectationType   expectation.ExpectationObject.objtype
84         Map<ExpectationType, List<Expectation>> expectationTypeListMap = intentGoalBean.getIntent().getIntentExpectations()
85                 .stream().collect(Collectors.groupingBy(x -> x.getExpectationType()));
86         List<IntentGoalBean> subIntentGoalList = new ArrayList<>();
87         IntentGoalType intentGoalType = intentGoalBean.getIntentGoalType();
88         for (Map.Entry<ExpectationType, List<Expectation>> entry : expectationTypeListMap.entrySet()) {
89
90             Map<ObjectType, List<Expectation>> objTypeMap = entry.getValue().stream()
91                     .collect(Collectors.groupingBy(x -> x.getExpectationObject().getObjectType()));
92             for (Map.Entry<ObjectType, List<Expectation>> objEntry : objTypeMap.entrySet()) {
93                 IntentGoalBean subIntentGoalBean = new IntentGoalBean();
94                 Intent subIntent = new Intent();
95                 subIntent.setIntentName(objEntry.getValue().get(0).getExpectationName().replace("Expectation", "Intent"));
96                 subIntent.setIntentExpectations(objEntry.getValue());
97                 //List<Expectation> newExpectationList = getNewExpectationList(objEntry.getValue());
98                 //subIntent.setIntentExpectations(newExpectationList);
99                 //TODO      intentFulfilmentInfo intentContexts
100                 subIntentGoalBean.setIntentGoalType(intentGoalType);
101                 subIntentGoalBean.setIntent(subIntent);
102                 subIntentGoalList.add(subIntentGoalBean);
103             }
104         }
105         return subIntentGoalList;
106     }
107
108     public List<IntentGoalBean> intentOrchestration(List<IntentGoalBean> subIntentGoalList) {
109         List<IntentGoalBean> sortList = new ArrayList<>();
110         List<IntentGoalBean> deliveryGoalList = subIntentGoalList.stream().filter(x ->
111                 StringUtils.containsIgnoreCase(x.getIntent().getIntentName(), "delivery")).collect(Collectors.toList());
112         List<IntentGoalBean> assuranceGoalList = subIntentGoalList.stream().filter(x ->
113                 StringUtils.containsIgnoreCase(x.getIntent().getIntentName(), "assurance")).collect(Collectors.toList());
114         List<IntentGoalBean> otherGoalList = subIntentGoalList.stream().filter(x ->
115                 !StringUtils.containsIgnoreCase(x.getIntent().getIntentName(), "delivery")
116                         && !StringUtils.containsIgnoreCase(x.getIntent().getIntentName(), "assurance")).collect(Collectors.toList());
117         sortList.addAll(deliveryGoalList);
118         sortList.addAll(assuranceGoalList);
119         sortList.addAll(otherGoalList);
120         return sortList;
121     }
122
123     @Override
124     public void interactWithTemplateDb() {
125     }
126
127     @Override
128     public LinkedHashMap<IntentGoalBean, IntentManagementFunction> findHandler(IntentGoalBean intentGoalBean) {
129         boolean needDecompostion = needDecompostion(intentGoalBean);
130         LinkedHashMap<IntentGoalBean, IntentManagementFunction> intentMap = new LinkedHashMap<>();
131         if (needDecompostion) {
132             List<IntentGoalBean> subIntentGoalList = intentDecomposition(intentGoalBean);
133             List<IntentGoalBean> sortList = intentOrchestration(subIntentGoalList);
134             for (IntentGoalBean subIntentGoal : sortList) {
135                 IntentManagementFunction imf = exploreIntentHandlers(subIntentGoal);
136                 intentMap.put(subIntentGoal, imf);
137             }
138         } else {
139             intentMap.put(intentGoalBean, exploreIntentHandlers(intentGoalBean));
140         }
141         return intentMap;
142     }
143
144
145 }