de30c3dbef53213d6c19535a7587c9f358043fac
[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         for (Map.Entry<IntentGoalBean, IntentManagementFunction> next : linkedIntentMap.entrySet()) {
116             IntentGoalBean newIntentGoalBean = next.getKey();
117             IntentGoalType intentGoalType = newIntentGoalBean.getIntentGoalType();
118             if (intentGoalType == IntentGoalType.CREATE) {
119                 Intent newIdIntent = decisionModule.intentObjectDefine(originIntent, next.getKey().getIntent());
120                 intentContextService.updateIntentOwnerHandlerContext(newIdIntent, this, next.getValue());
121                 intentContextService.updateParentIntentContext(originIntent, newIdIntent);
122                 intentContextService.updateChindIntentContext(originIntent, newIdIntent);
123                 //intent-Distribution-create
124                 boolean isAcceptCreate = intentInterfaceService.createInterface(originIntent,
125                         new IntentGoalBean(newIdIntent, IntentGoalType.CREATE), next.getValue());
126                 originIntent.setIntentGenerateType(IntentGenerateType.USERINPUT);
127                 //save user input intent
128                 actuationModule.saveIntentToDb(originIntent);
129                 return isAcceptCreate;
130             } else if (intentGoalType == IntentGoalType.UPDATE) {
131                 log.info("formatIntentInputIMF UPDATE");
132                 //update cllBusinessIntent's expectation
133                 Intent subIntent = newIntentGoalBean.getIntent();
134                 updateIntentInfo(originIntent, subIntent);
135                 //update userInput intent
136                 actuationModule.updateIntentToDb(originIntent);
137                 // intent-Distribution and operate  |update cllBusiness intent
138                 boolean isAcceptUpdate = intentInterfaceService.updateInterface(originIntent,
139                         new IntentGoalBean(subIntent, IntentGoalType.UPDATE), next.getValue());
140             } else {
141                 //deal with userInput intent
142                 actuationModule.deleteIntentToDb(originIntent);
143                 // intent-Distribution-delete
144                 boolean isAcceptDelete = intentInterfaceService.deleteInterface(originIntent, next.getKey(), next.getValue());
145             }
146         }
147         return true;
148     }
149
150     public void updateIntentInfo(Intent originIntent, Intent intent) {
151
152         List<Expectation> originIntentExpectationList = originIntent.getIntentExpectations();
153         List<Expectation> intentExpectationList = intent.getIntentExpectations();
154         int newIntentExpectationNum = originIntentExpectationList.size();
155         int oldIntentExpectationNum = intentExpectationList.size();
156
157         List<Expectation> changeList = new ArrayList<>();
158         if (newIntentExpectationNum != oldIntentExpectationNum) {
159             if (newIntentExpectationNum < oldIntentExpectationNum) {
160
161                 for (Expectation oldExpectation : intentExpectationList) {//search
162                     boolean bFindExpectation = false;
163                     for (Expectation newExpectation : originIntentExpectationList) {//param
164                         if (oldExpectation.getExpectationName().equals(newExpectation.getExpectationName())) {
165                             bFindExpectation = true;
166                             break;
167                         }
168                     }
169                     if (bFindExpectation) {
170                         changeList.add(oldExpectation);
171                     }
172                 }
173             }
174         }
175         intent.setIntentExpectations(changeList);
176     }
177 }