bcda3679e702871912464b130575a9e7f33ef5a3
[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.onap.usecaseui.intentanalysis.bean.enums.ExpectationType;
21 import org.onap.usecaseui.intentanalysis.bean.enums.IntentGoalType;
22 import org.onap.usecaseui.intentanalysis.bean.enums.ObjectType;
23 import org.onap.usecaseui.intentanalysis.bean.models.Expectation;
24 import org.onap.usecaseui.intentanalysis.bean.models.Intent;
25 import org.onap.usecaseui.intentanalysis.bean.models.IntentGoalBean;
26 import org.onap.usecaseui.intentanalysis.bean.models.IntentManagementFunctionRegInfo;
27 import org.onap.usecaseui.intentanalysis.intentBaseService.IntentManagementFunction;
28 import org.onap.usecaseui.intentanalysis.intentBaseService.intentModule.DecisionModule;
29 import org.onap.usecaseui.intentanalysis.service.ImfRegInfoService;
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.Service;
34
35 import java.util.*;
36 import java.util.stream.Collectors;
37
38 @Log4j2
39 @Service
40 public class CLLBusinessDecisionModule implements DecisionModule {
41     @Autowired
42     private ImfRegInfoService imfRegInfoService;
43     @Autowired
44     private ApplicationContext applicationContext;
45
46     @Override
47     public void determineUltimateGoal() {
48     }
49
50     @Override
51     public IntentManagementFunction exploreIntentHandlers(IntentGoalBean intentGoalBean) {
52         //  db  filter imf  supportArea;
53         //SupportInterface> supportInterfaces;
54         IntentGoalType intentGoalType = intentGoalBean.getIntentGoalType();
55         String intentName = intentGoalBean.getIntent().getIntentName();
56         List<IntentManagementFunctionRegInfo> imfRegInfoList = imfRegInfoService.getImfRegInfoList();
57         //todo
58         List<IntentManagementFunctionRegInfo> imfList = imfRegInfoList.stream().
59                 filter(x -> x.getSupportArea().contains(intentName)
60                         && x.getSupportInterfaces().contains(intentGoalType)).collect(Collectors.toList());
61         if (!Optional.ofNullable(imfList).isPresent()) {
62             log.info("The intent name is %s not find the corresponding IntentManagementFunction", intentName);
63         }
64         return (IntentManagementFunction) applicationContext.getBean(imfList.get(0).getHandleName());
65     }
66
67     @Override
68     public void intentDefinition() {
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()).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.setIntentId(CommonUtil.getUUid());
109                 subIntent.setIntentName(objEntry.getValue().get(0).getExpectationName().replace("Expectation", "Intent"));
110                 subIntent.setIntentExpectations(objEntry.getValue());
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 -> x.getIntent().getIntentName()
123                 .equalsIgnoreCase("delivery")).collect(Collectors.toList());
124         List<IntentGoalBean> assuranceGoalList = subIntentGoalList.stream().filter(x -> x.getIntent().getIntentName()
125                 .equalsIgnoreCase("assurance")).collect(Collectors.toList());
126         List<IntentGoalBean> otherGoalList = subIntentGoalList.stream().filter(x -> !x.getIntent().getIntentName()
127                 .equalsIgnoreCase("assurance") && !x.getIntent().getIntentName()
128                 .equalsIgnoreCase("delivery")).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 List<Map<IntentGoalBean, IntentManagementFunction>> findHandler(IntentGoalBean intentGoalBean) {
141         boolean needDecompostion = needDecompostion(intentGoalBean);
142         List<Map<IntentGoalBean, IntentManagementFunction>> intentMapList = new ArrayList<>();
143         if (needDecompostion) {
144             List<IntentGoalBean> subIntentGoalList = intentDecomposition(intentGoalBean);
145             List<IntentGoalBean> sortList = intentOrchestration(subIntentGoalList);
146             for (IntentGoalBean subIntentGoal : sortList) {
147                 Map<IntentGoalBean, IntentManagementFunction> map = new HashMap<>();
148                 IntentManagementFunction imf = exploreIntentHandlers(subIntentGoal);
149                 //TODO call probe  interface  if fail  intentFulfilmentInfo throw exception
150                 map.put(subIntentGoal, imf);
151                 intentMapList.add(map);
152             }
153         } else {
154             Map<IntentGoalBean, IntentManagementFunction> map = new HashMap<>();
155             map.put(intentGoalBean, exploreIntentHandlers(intentGoalBean));
156             intentMapList.add(map);
157         }
158         return intentMapList;
159     }
160 }