bc07b792f330b7f8dc8a39ca6958598d530ffd55
[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.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.intentModule.DecisionModule;
30 import org.onap.usecaseui.intentanalysis.service.ImfRegInfoService;
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     @Override
48     public void determineUltimateGoal() {
49     }
50
51     @Override
52     public IntentManagementFunction exploreIntentHandlers(IntentGoalBean intentGoalBean) {
53         //  db  filter imf  supportArea;
54         //SupportInterface> supportInterfaces;
55         IntentManagementFunctionRegInfo imfRegInfo = imfRegInfoService.getImfRegInfo(intentGoalBean);
56         return (IntentManagementFunction) applicationContext.getBean(imfRegInfo.getHandleName());
57     }
58
59     @Override
60     public void intentDefinition() {
61     }
62
63     @Override
64     public void decideSuitableAction() {
65     }
66
67
68     public boolean needDecompostion(IntentGoalBean intentGoalBean) {
69         //different expectationType need decompostion  ExpectationType>1 or objtype>1
70         if (intentGoalBean.getIntentGoalType().equals(IntentGoalType.CREATE)) {
71             List<Expectation> intentExpectations = intentGoalBean.getIntent().getIntentExpectations();
72             List<ExpectationType> expectationTypeList = intentExpectations.stream()
73                     .map(Expectation::getExpectationType).distinct().collect(Collectors.toList());
74             if (expectationTypeList.size() > 1) {
75                 return true;
76             } else {
77                 List<ObjectType> objectTypeList = intentExpectations.stream().map(x ->
78                         x.getExpectationObject().getObjectType()).distinct().collect(Collectors.toList());
79                 if (objectTypeList.size() > 1) {
80                     return true;
81                 }
82             }
83         }
84         return false;
85     }
86
87     public List<IntentGoalBean> intentDecomposition(IntentGoalBean intentGoalBean) {
88         //ExpectationType   expectation.ExpectationObject.objtype
89         Map<ExpectationType, List<Expectation>> expectationTypeListMap = intentGoalBean.getIntent().getIntentExpectations()
90                 .stream().collect(Collectors.groupingBy(x -> x.getExpectationType()));
91         List<IntentGoalBean> subIntentGoalList = new ArrayList<>();
92         IntentGoalType intentGoalType = intentGoalBean.getIntentGoalType();
93         for (Map.Entry<ExpectationType, List<Expectation>> entry : expectationTypeListMap.entrySet()) {
94
95             Map<ObjectType, List<Expectation>> objTypeMap = entry.getValue().stream()
96                     .collect(Collectors.groupingBy(x -> x.getExpectationObject().getObjectType()));
97             for (Map.Entry<ObjectType, List<Expectation>> objEntry : objTypeMap.entrySet()) {
98                 IntentGoalBean subIntentGoalBean = new IntentGoalBean();
99                 Intent subIntent = new Intent();
100                 subIntent.setIntentId(CommonUtil.getUUid());
101                 subIntent.setIntentName(objEntry.getValue().get(0).getExpectationName().replace("Expectation", "Intent"));
102                 subIntent.setIntentExpectations(objEntry.getValue());
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                 Map<IntentGoalBean, IntentManagementFunction> map = new HashMap<>();
140                 IntentManagementFunction imf = exploreIntentHandlers(subIntentGoal);
141                 map.put(subIntentGoal, imf);
142             }
143         } else {
144             intentMap.put(intentGoalBean, exploreIntentHandlers(intentGoalBean));
145         }
146         return intentMap;
147     }
148
149 }