cfa7df228ab0ffdc13b99e12b3bc15eed935c004
[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     }
80
81     @Override
82     public void receiveIntentAsHandler(Intent originalIntent, IntentGoalBean intentGoalBean, IntentManagementFunction handler) {
83     }
84
85     @Override
86     public void createReport(String intentId, FulfillmentInfo fulfillmentInfo) {
87         saveFulfillmentAndObjectInstance(intentId, fulfillmentInfo);
88     }
89
90     public IntentGoalBean detection(IntentGoalBean intentGoalBean) {
91         Intent originIntent = intentGoalBean.getIntent();
92         IntentGoalType intentGoalType = intentGoalBean.getIntentGoalType();
93         if (intentGoalType == IntentGoalType.CREATE) {
94             return knowledgeModule.intentCognition(originIntent);
95         } else if (intentGoalType == IntentGoalType.UPDATE) {
96             return new IntentGoalBean(intentGoalBean.getIntent(), IntentGoalType.UPDATE);
97         } else {
98             return new IntentGoalBean(intentGoalBean.getIntent(), IntentGoalType.DELETE);
99         }
100     }
101
102     public LinkedHashMap<IntentGoalBean, IntentManagementFunction> investigation(IntentGoalBean intentGoalBean) {
103         IntentGoalType intentGoalType = intentGoalBean.getIntentGoalType();
104         if (intentGoalType == IntentGoalType.CREATE) {
105             return decisionModule.investigationCreateProcess(intentGoalBean);
106         } else if (intentGoalType == IntentGoalType.UPDATE) {
107             return decisionModule.investigationUpdateProcess(intentGoalBean);
108         } else {
109             return decisionModule.investigationDeleteProcess(intentGoalBean);
110         }
111     }
112
113     public boolean implementIntent(Intent originIntent, LinkedHashMap<IntentGoalBean, IntentManagementFunction> linkedIntentMap) {
114         Iterator<Map.Entry<IntentGoalBean, IntentManagementFunction>> iterator = linkedIntentMap.entrySet().iterator();
115         while (iterator.hasNext()) {
116             Map.Entry<IntentGoalBean, IntentManagementFunction> next = iterator.next();
117             IntentGoalBean newIntentGoalBean = next.getKey();
118             IntentGoalType intentGoalType = newIntentGoalBean.getIntentGoalType();
119             if (intentGoalType == IntentGoalType.CREATE) {
120                 Intent newIdIntent = decisionModule.intentObjectDefine(originIntent, next.getKey().getIntent());
121                 intentContextService.updateIntentOwnerHandlerContext(newIdIntent, this, next.getValue());
122                 intentContextService.updateParentIntentContext(originIntent, newIdIntent);
123                 intentContextService.updateChindIntentContext(originIntent, newIdIntent);
124                 //intent-Distribution-create
125                 boolean isAcceptCreate = intentInterfaceService.createInterface(originIntent,
126                         new IntentGoalBean(newIdIntent, IntentGoalType.CREATE), next.getValue());
127                 originIntent.setIntentGenerateType(IntentGenerateType.USERINPUT);
128                 //save user input intent
129                 intentService.createIntent(originIntent);
130                 return isAcceptCreate;
131             } else if (intentGoalType == IntentGoalType.UPDATE) {
132                 log.info("formatIntentInputIMF UPDATE");
133                 //update cllBusinessIntent's expectation
134                 Intent subIntent = newIntentGoalBean.getIntent();
135                 updateIntentInfo(originIntent, subIntent);
136                 //update userInput intent
137                 intentService.updateIntent(originIntent);
138                 // intent-Distribution and operate  |update cllBusiness intent
139                 boolean isAcceptUpdate = intentInterfaceService.updateInterface(originIntent,
140                         new IntentGoalBean(subIntent, IntentGoalType.UPDATE), next.getValue());
141             } else {
142                 //deal with userInput intent
143                 intentService.deleteIntent(originIntent.getIntentId());
144                 // intent-Distribution-delete
145                 boolean isAcceptDelete = intentInterfaceService.deleteInterface(originIntent, next.getKey(), next.getValue());
146             }
147         }
148         return true;
149     }
150
151     public void updateIntentInfo(Intent originIntent, Intent intent) {
152
153         List<Expectation> originIntentExpectationList = originIntent.getIntentExpectations();
154         List<Expectation> intentExpectationList = intent.getIntentExpectations();
155         int newIntentExpectationNum = originIntentExpectationList.size();
156         int oldIntentExpectationNum = intentExpectationList.size();
157
158         List<Expectation> changeList = new ArrayList<>();
159         if (newIntentExpectationNum != oldIntentExpectationNum) {
160             if (newIntentExpectationNum < oldIntentExpectationNum) {
161
162                 for (Expectation oldExpectation : intentExpectationList) {//search
163                     boolean bFindExpectation = false;
164                     for (Expectation newExpectation : originIntentExpectationList) {//param
165                         if (oldExpectation.getExpectationName().equals(newExpectation.getExpectationName())) {
166                             bFindExpectation = true;
167                             break;
168                         }
169                     }
170                     if (bFindExpectation) {
171                         changeList.add(oldExpectation);
172                     }
173                 }
174             }
175         }
176         intent.setIntentExpectations(changeList);
177     }
178 }