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