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