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