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