4d16d92ec9aac5314ff7d7f4e4db265b8abd7961
[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.formatintentinputMgt;
17
18 import lombok.Data;
19 import lombok.extern.slf4j.Slf4j;
20 import org.onap.usecaseui.intentanalysis.bean.enums.IntentGenerateType;
21 import org.onap.usecaseui.intentanalysis.bean.enums.IntentGoalType;
22 import org.onap.usecaseui.intentanalysis.bean.models.Expectation;
23 import org.onap.usecaseui.intentanalysis.bean.models.Intent;
24 import org.onap.usecaseui.intentanalysis.bean.models.IntentGoalBean;
25 import org.onap.usecaseui.intentanalysis.intentBaseService.IntentManagementFunction;
26 import org.onap.usecaseui.intentanalysis.intentBaseService.contextService.IntentContextService;
27 import org.onap.usecaseui.intentanalysis.intentBaseService.intentModule.ActuationModule;
28 import org.onap.usecaseui.intentanalysis.intentBaseService.intentModule.DecisionModule;
29 import org.onap.usecaseui.intentanalysis.intentBaseService.intentModule.KnowledgeModule;
30 import org.onap.usecaseui.intentanalysis.intentBaseService.intentinterfaceservice.IntentInterfaceService;
31 import org.onap.usecaseui.intentanalysis.service.IntentService;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.context.ApplicationContext;
34 import org.springframework.stereotype.Component;
35
36 import javax.annotation.Resource;
37 import java.util.*;
38
39 @Slf4j
40 @Data
41 @Component("formatIntentInputManagementFunction")
42 public class FormatIntentInputManagementFunction extends IntentManagementFunction {
43     @Autowired
44     public IntentContextService intentContextService;
45
46     @Resource(name = "formatIntentInputKnowledgeModule")
47     public void setKnowledgeModule(KnowledgeModule knowledgeModule) {
48         this.knowledgeModule = knowledgeModule;
49     }
50
51     @Resource(name = "formatIntentInputActuationModule")
52     public void setActuationModule(ActuationModule actuationModule) {
53
54         this.actuationModule = actuationModule;
55     }
56
57     @Resource(name = "formatIntentInputDecisionModule")
58     public void setDecisionModule(DecisionModule decisionModule) {
59
60         this.decisionModule = decisionModule;
61     }
62
63     @Autowired
64     IntentInterfaceService intentInterfaceService;
65     @Autowired
66     ApplicationContext applicationContext;
67     @Autowired
68     IntentService intentService;
69
70     @Override
71     public void receiveIntentAsOwner(IntentGoalBean intentGoalBean) {
72
73         IntentGoalBean originIntentGoalBean = detection(intentGoalBean);
74         LinkedHashMap<IntentGoalBean, IntentManagementFunction> linkedMap = investigation(originIntentGoalBean);
75         implementIntent(intentGoalBean.getIntent(), linkedMap);
76     }
77
78     @Override
79     public void receiveIntentAsHandler(Intent originalIntent, IntentGoalBean intentGoalBean, IntentManagementFunction handler) {
80     }
81
82     public IntentGoalBean detection(IntentGoalBean intentGoalBean) {
83         Intent originIntent = intentGoalBean.getIntent();
84         IntentGoalType intentGoalType = intentGoalBean.getIntentGoalType();
85         if (intentGoalType == IntentGoalType.CREATE) {
86             return knowledgeModule.intentCognition(originIntent);
87         } else if (intentGoalType == IntentGoalType.UPDATE) {
88             return new IntentGoalBean(intentGoalBean.getIntent(), IntentGoalType.UPDATE);
89         } else {
90             return new IntentGoalBean(intentGoalBean.getIntent(), IntentGoalType.DELETE);
91         }
92     }
93
94     public LinkedHashMap<IntentGoalBean, IntentManagementFunction> investigation(IntentGoalBean intentGoalBean) {
95         IntentGoalType intentGoalType = intentGoalBean.getIntentGoalType();
96         if (intentGoalType == IntentGoalType.CREATE) {
97             return decisionModule.investigationCreateProcess(intentGoalBean);
98         } else if (intentGoalType == IntentGoalType.UPDATE) {
99             return decisionModule.investigationUpdateProcess(intentGoalBean);
100         } else {
101             return decisionModule.investigationDeleteProcess(intentGoalBean);
102         }
103     }
104
105     public boolean implementIntent(Intent originIntent, LinkedHashMap<IntentGoalBean, IntentManagementFunction> linkedIntentMap) {
106         Iterator<Map.Entry<IntentGoalBean, IntentManagementFunction>> iterator = linkedIntentMap.entrySet().iterator();
107         while (iterator.hasNext()) {
108             Map.Entry<IntentGoalBean, IntentManagementFunction> next = iterator.next();
109             IntentGoalBean newIntentGoalBean = next.getKey();
110             IntentGoalType intentGoalType = newIntentGoalBean.getIntentGoalType();
111             if (intentGoalType == IntentGoalType.CREATE) {
112                 Intent newIdIntent = decisionModule.intentObjectDefine(originIntent, next.getKey().getIntent());
113                 intentContextService.updateIntentOwnerHandlerContext(newIdIntent, this, next.getValue());
114                 intentContextService.updateParentIntentContext(originIntent, newIdIntent);
115                 intentContextService.updateChindIntentContext(originIntent, newIdIntent);
116                 //intent-Distribution-create
117                 boolean isAcceptCreate = intentInterfaceService.createInterface(originIntent,
118                         new IntentGoalBean(newIdIntent, IntentGoalType.CREATE), next.getValue());
119                 originIntent.setIntentGenerateType(IntentGenerateType.USERINPUT);
120                 //save user input intent
121                 intentService.createIntent(originIntent);
122                 return isAcceptCreate;
123             } else if (intentGoalType == IntentGoalType.UPDATE) {
124                 log.info("formatIntentInputIMF UPDATE");
125                 //update cllBusinessIntent's expectation
126                 Intent subIntent = newIntentGoalBean.getIntent();
127                 updateIntentInfo(originIntent, subIntent);
128                 // intent-Distribution and operate  |update cllBusiness intent
129                 boolean isAcceptUpdate = intentInterfaceService.updateInterface(originIntent,
130                         new IntentGoalBean(subIntent, IntentGoalType.UPDATE), next.getValue());
131                 //update userInput intent
132                 intentService.updateIntent(originIntent);
133             } else {
134                 // intent-Distribution-delete
135                 boolean isAcceptDelete = intentInterfaceService.deleteInterface(originIntent, next.getKey(), next.getValue());
136             }
137         }
138         return true;
139     }
140
141     public void updateIntentInfo(Intent originIntent, Intent intent) {
142
143         List<Expectation> originIntentExpectationList = originIntent.getIntentExpectations();
144         List<Expectation> intentExpectationList = intent.getIntentExpectations();
145         int newIntentExpectationNum = originIntentExpectationList.size();
146         int oldIntentExpectationNum = intentExpectationList.size();
147
148         List<Expectation> changeList = new ArrayList<>();
149         if (newIntentExpectationNum != oldIntentExpectationNum) {
150             if (newIntentExpectationNum < oldIntentExpectationNum) {
151
152                 for (Expectation oldExpectation : intentExpectationList) {//search
153                     boolean bFindExpectation = false;
154                     for (Expectation newExpectation : originIntentExpectationList) {//param
155                         if (oldExpectation.getExpectationName().equals(newExpectation.getExpectationName())) {
156                             bFindExpectation = true;
157                             break;
158                         }
159                     }
160                     if (bFindExpectation) {
161                         changeList.add(oldExpectation);
162                     }
163                 }
164             }
165         }
166         intent.setIntentExpectations(changeList);
167     }
168 }