6a7d7f8d6a4e7a726f601f26e2662b5db0482bb0
[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     }
92
93     @Override
94     public void receiveIntentAsHandler(Intent originalIntent, IntentGoalBean intentGoalBean, IntentManagementFunction handler) {
95         ActuationModule actuationModule = handler.getActuationModule();
96         IntentGoalType type = intentGoalBean.getIntentGoalType();
97         //before dataBase operate
98         handler.receiveIntentAsOwner(intentGoalBean);
99
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             //before delete cllBusinessIntent check subintent deleted
106             boolean deleteFinished = false;
107             Intent cllIntent = intentGoalBean.getIntent();
108             //subIntent size and delete Intent size
109             List<Context> list = cllIntent.getIntentContexts().stream().filter(x ->
110                     StringUtils.equals(x.getContextName(), "subIntent info")).collect(Collectors.toList());
111             int subIntentSize = list.get(0).getContextConditions().size();
112             while (!deleteFinished) {
113                 List<IntentEventRecord> deleteList = intentEventRecordService.getRecordByPid(cllIntent.getIntentId(),
114                         IntentGoalType.DELETE.name());
115                 if (subIntentSize <= deleteList.size()) {
116                     deleteFinished = true;
117                 }
118             }
119             actuationModule.deleteIntentToDb(intentGoalBean.getIntent());
120         }
121     }
122
123     public IntentGoalBean detection(IntentGoalBean intentGoalBean) {
124         Intent originIntent = intentGoalBean.getIntent();
125         IntentGoalType intentGoalType = intentGoalBean.getIntentGoalType();
126         if (intentGoalType == IntentGoalType.CREATE) {
127             return knowledgeModule.intentCognition(originIntent);
128         } else if (intentGoalType == IntentGoalType.UPDATE) {
129             return new IntentGoalBean(intentGoalBean.getIntent(), IntentGoalType.UPDATE);
130         } else {
131             return new IntentGoalBean(intentGoalBean.getIntent(), IntentGoalType.DELETE);
132         }
133     }
134
135     public LinkedHashMap<IntentGoalBean, IntentManagementFunction> investigation(IntentGoalBean intentGoalBean) {
136         IntentGoalType intentGoalType = intentGoalBean.getIntentGoalType();
137         if (intentGoalType == IntentGoalType.CREATE) {
138             return decisionModule.investigationCreateProcess(intentGoalBean);
139         } else if (intentGoalType == IntentGoalType.UPDATE) {
140             return decisionModule.investigationUpdateProcess(intentGoalBean);
141         } else {
142             return decisionModule.investigationDeleteProcess(intentGoalBean);
143         }
144     }
145
146     @SneakyThrows
147     public boolean implementIntent(Intent originIntent, LinkedHashMap<IntentGoalBean, IntentManagementFunction> linkedIntentMap) {
148         Iterator<Map.Entry<IntentGoalBean, IntentManagementFunction>> iterator = linkedIntentMap.entrySet().iterator();
149         while (iterator.hasNext()) {
150             Map.Entry<IntentGoalBean, IntentManagementFunction> next = iterator.next();
151             IntentGoalBean newIntentGoalBean = next.getKey();
152             IntentGoalType intentGoalType = newIntentGoalBean.getIntentGoalType();
153             IntentManagementFunction handler = next.getValue();
154             if (intentGoalType == IntentGoalType.CREATE) {
155                 Intent newIdIntent = decisionModule.intentObjectDefine(originIntent, next.getKey().getIntent());
156                 intentContextService.updateIntentOwnerHandlerContext(newIdIntent, this, next.getValue());
157                 intentContextService.updateParentIntentContext(originIntent, newIdIntent);
158                 intentContextService.updateChindIntentContext(originIntent, newIdIntent);
159                 // contextService.updateContextList(originIntent.getIntentContexts(), originIntent.getIntentId());
160                 //intent-Distribution-create
161                 boolean isAcceptCreate = intentInterfaceService.createInterface(originIntent,
162                         new IntentGoalBean(newIdIntent, IntentGoalType.CREATE), next.getValue());
163                 // TODO: 2023/3/27  isParallel status need deal before distribution
164                 boolean isParallel = false;
165                 if (!isParallel) {
166                     //Block and regularly query whether the event is published
167                     boolean isPublish = false;
168                     int count = 1;
169                     while (!isPublish) {
170                         Thread.sleep(1000);
171                         IntentEventRecord record = intentEventRecordService.getIntentEventRecordByIntentId(newIdIntent.getIntentId(), "create");
172                         count++;
173                         // it will take one hour to wait operation end
174                         if (count == 3600) {
175                             throw new CommonException("Operation took too long, failed", 500);
176                         }
177                         if (null != record) {
178                             isPublish = true;
179                         }
180                     }
181                 }
182                 // return isAcceptCreate;
183             } else if (intentGoalType == IntentGoalType.UPDATE) {
184                 //define process  just send probe interface
185                 // intent-Distribution-update
186                 boolean isAcceptupdate = intentInterfaceService.updateInterface(originIntent,
187                         next.getKey(), next.getValue());
188             } else {
189                 // actuationModule.deleteIntentToDb(next.getKey().getIntent());
190                 // intent-Distribution-delete
191                 boolean isAcceptDelete = intentInterfaceService.deleteInterface(originIntent,
192                         next.getKey(), next.getValue());
193             }
194         }
195         return false;
196     }
197
198 }