120350817638d0973c7c213b2cb6a0cf8b18b0ae
[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.cllBusinessIntentMgt;
17
18
19 import lombok.Data;
20 import lombok.SneakyThrows;
21 import lombok.extern.slf4j.Slf4j;
22 import org.apache.commons.lang.StringUtils;
23 import org.onap.usecaseui.intentanalysis.bean.enums.IntentGoalType;
24 import org.onap.usecaseui.intentanalysis.bean.models.Context;
25 import org.onap.usecaseui.intentanalysis.bean.models.Intent;
26 import org.onap.usecaseui.intentanalysis.bean.models.IntentEventRecord;
27 import org.onap.usecaseui.intentanalysis.bean.models.IntentGoalBean;
28 import org.onap.usecaseui.intentanalysis.intentBaseService.IntentManagementFunction;
29 import org.onap.usecaseui.intentanalysis.intentBaseService.contextService.IntentContextService;
30 import org.onap.usecaseui.intentanalysis.intentBaseService.intentEventRecord.IntentEventRecordService;
31 import org.onap.usecaseui.intentanalysis.intentBaseService.intentModule.ActuationModule;
32 import org.onap.usecaseui.intentanalysis.intentBaseService.intentModule.DecisionModule;
33 import org.onap.usecaseui.intentanalysis.intentBaseService.intentModule.KnowledgeModule;
34 import org.onap.usecaseui.intentanalysis.intentBaseService.intentinterfaceservice.IntentInterfaceService;
35 import org.onap.usecaseui.intentanalysis.service.ContextService;
36 import org.onap.usecaseui.intentanalysis.service.IntentService;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.context.ApplicationContext;
39 import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
40 import org.springframework.stereotype.Component;
41
42 import javax.annotation.Resource;
43 import java.util.Iterator;
44 import java.util.LinkedHashMap;
45 import java.util.List;
46 import java.util.Map;
47 import java.util.stream.Collectors;
48
49 @Slf4j
50 @Data
51 @Component("CLLBusinessIntentManagementFunction")
52 public class CLLBusinessIntentManagementFunction extends IntentManagementFunction {
53
54     @Resource(name = "CLLBusinessActuationModule")
55     public void setActuationModule(ActuationModule actuationModule) {
56         this.actuationModule = actuationModule;
57     }
58
59     @Resource(name = "CLLBusinessKnowledgeModule")
60     public void setKnowledgeModule(KnowledgeModule knowledgeModule) {
61         this.knowledgeModule = knowledgeModule;
62     }
63
64     @Resource(name = "CLLBusinessDecisionModule")
65     public void setDecisionModule(DecisionModule decisionModule) {
66         this.decisionModule = decisionModule;
67     }
68
69     @Autowired
70     public IntentContextService intentContextService;
71     @Autowired
72     IntentInterfaceService intentInterfaceService;
73     @Autowired
74     ApplicationContext applicationContext;
75     @Autowired
76     ContextService contextService;
77     @Autowired
78     IntentService intentService;
79     @Autowired
80     IntentEventRecordService intentEventRecordService;
81
82     @Resource(name = "intentTaskExecutor")
83     ThreadPoolTaskExecutor executor;
84
85     @Override
86     public void receiveIntentAsOwner(IntentGoalBean intentGoalBean) {
87         IntentGoalBean originIntentGoalBean = detection(intentGoalBean);
88         LinkedHashMap<IntentGoalBean, IntentManagementFunction> linkedMap = investigation(originIntentGoalBean);
89         implementIntent(intentGoalBean.getIntent(), linkedMap);
90         if (intentGoalBean.getIntentGoalType() == IntentGoalType.DELETE) {
91             List<Context> parentInfo = intentGoalBean.getIntent().getIntentContexts().stream().filter(a ->
92                     StringUtils.equalsIgnoreCase(a.getContextName(), "parentIntent info")).collect(Collectors.toList());
93
94             String userInputId = parentInfo.get(0).getContextConditions().get(0).getConditionValue();
95             intentService.deleteIntent(intentGoalBean.getIntent().getIntentId());
96             intentService.deleteIntent(userInputId);
97         }
98     }
99
100     @Override
101     public void receiveIntentAsHandler(Intent originalIntent, IntentGoalBean intentGoalBean, IntentManagementFunction handler) {
102         ActuationModule actuationModule = handler.getActuationModule();
103         IntentGoalType type = intentGoalBean.getIntentGoalType();
104         if (type == IntentGoalType.CREATE) {
105             actuationModule.saveIntentToDb(intentGoalBean.getIntent());
106         } else if (type == IntentGoalType.UPDATE) {
107             actuationModule.updateIntentToDb(intentGoalBean.getIntent());
108         } else if (type == IntentGoalType.DELETE) {
109             actuationModule.deleteIntentToDb(intentGoalBean.getIntent());
110         }
111         //update origin intent if need
112         actuationModule.updateIntentOperationInfo(originalIntent, intentGoalBean);
113         handler.receiveIntentAsOwner(intentGoalBean);
114
115     }
116
117     public IntentGoalBean detection(IntentGoalBean intentGoalBean) {
118         Intent originIntent = intentGoalBean.getIntent();
119         IntentGoalType intentGoalType = intentGoalBean.getIntentGoalType();
120         if (intentGoalType == IntentGoalType.CREATE) {
121             //return knowledgeModule.intentCognition(originIntent);
122             return intentGoalBean;
123         } else if (intentGoalType == IntentGoalType.UPDATE) {
124             return new IntentGoalBean(intentGoalBean.getIntent(), IntentGoalType.UPDATE);
125         } else {
126             return new IntentGoalBean(intentGoalBean.getIntent(), IntentGoalType.DELETE);
127         }
128     }
129
130     public LinkedHashMap<IntentGoalBean, IntentManagementFunction> investigation(IntentGoalBean intentGoalBean) {
131         IntentGoalType intentGoalType = intentGoalBean.getIntentGoalType();
132         if (intentGoalType == IntentGoalType.CREATE) {
133             return decisionModule.investigationCreateProcess(intentGoalBean);
134         } else if (intentGoalType == IntentGoalType.UPDATE) {
135             return decisionModule.investigationUpdateProcess(intentGoalBean);
136         } else {
137             return decisionModule.investigationDeleteProcess(intentGoalBean);
138         }
139     }
140
141     @SneakyThrows
142     public boolean implementIntent(Intent originIntent, LinkedHashMap<IntentGoalBean, IntentManagementFunction> linkedIntentMap) {
143         Iterator<Map.Entry<IntentGoalBean, IntentManagementFunction>> iterator = linkedIntentMap.entrySet().iterator();
144         while (iterator.hasNext()) {
145             Map.Entry<IntentGoalBean, IntentManagementFunction> next = iterator.next();
146             IntentGoalBean newIntentGoalBean = next.getKey();
147             IntentGoalType intentGoalType = newIntentGoalBean.getIntentGoalType();
148             IntentManagementFunction handler = next.getValue();
149             if (intentGoalType == IntentGoalType.CREATE) {
150                 Intent newIdIntent = decisionModule.intentObjectDefine(originIntent, next.getKey().getIntent());
151                 intentContextService.updateIntentOwnerHandlerContext(newIdIntent, this, next.getValue());
152                 intentContextService.updateParentIntentContext(originIntent, newIdIntent);
153                 intentContextService.updateChindIntentContext(originIntent, newIdIntent);
154                 contextService.updateContextList(originIntent.getIntentContexts(), originIntent.getIntentId());
155                 //intent-Distribution-create
156                 boolean isAcceptCreate = intentInterfaceService.createInterface(originIntent,
157                         new IntentGoalBean(newIdIntent, IntentGoalType.CREATE), next.getValue());
158                 // TODO: 2023/3/27  isParallel status need deal before distribution
159                 boolean isParallel = false;
160                 if (!isParallel) {
161                     //Block and regularly query whether the event is published
162                     boolean isPublish = false;
163                     while (isPublish) {
164                         Thread.sleep(1000);
165                         IntentEventRecord record = intentEventRecordService.getIntentEventRecordByntentId(newIdIntent.getIntentId(), "create");
166                         if (null != record) {
167                             isPublish = true;
168                         }
169                     }
170                 }
171                 // return isAcceptCreate;
172             } else if (intentGoalType == IntentGoalType.UPDATE) {
173                 //define process  just send probe interface
174                 // intent-Distribution-update
175                 boolean isAcceptupdate = intentInterfaceService.updateInterface(originIntent,
176                         next.getKey(), next.getValue());
177             } else {
178                 // actuationModule.deleteIntentToDb(next.getKey().getIntent());
179                 // intent-Distribution-delete
180                 boolean isAcceptDelete = intentInterfaceService.deleteInterface(originIntent,
181                         next.getKey(), next.getValue());
182             }
183         }
184         return false;
185     }
186
187 }