2ca3352a26cc92ff14213dc2accbcb870d3f222f
[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 import lombok.Data;
19 import lombok.SneakyThrows;
20 import lombok.extern.slf4j.Slf4j;
21 import org.apache.commons.lang.StringUtils;
22 import org.onap.usecaseui.intentanalysis.bean.enums.IntentGoalType;
23 import org.onap.usecaseui.intentanalysis.bean.models.Context;
24 import org.onap.usecaseui.intentanalysis.bean.models.FulfillmentInfo;
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.formatintentinputMgt.FormatIntentInputManagementFunction;
30 import org.onap.usecaseui.intentanalysis.intentBaseService.IntentManagementFunction;
31 import org.onap.usecaseui.intentanalysis.intentBaseService.contextService.IntentContextService;
32 import org.onap.usecaseui.intentanalysis.intentBaseService.intentEventRecord.IntentEventRecordService;
33 import org.onap.usecaseui.intentanalysis.intentBaseService.intentModule.ActuationModule;
34 import org.onap.usecaseui.intentanalysis.intentBaseService.intentModule.DecisionModule;
35 import org.onap.usecaseui.intentanalysis.intentBaseService.intentModule.KnowledgeModule;
36 import org.onap.usecaseui.intentanalysis.intentBaseService.intentinterfaceservice.IntentInterfaceService;
37 import org.onap.usecaseui.intentanalysis.service.ContextService;
38 import org.onap.usecaseui.intentanalysis.service.IntentService;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.context.ApplicationContext;
41 import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
42 import org.springframework.stereotype.Component;
43
44 import javax.annotation.Resource;
45 import java.util.Iterator;
46 import java.util.LinkedHashMap;
47 import java.util.List;
48 import java.util.Map;
49 import java.util.stream.Collectors;
50
51 @Slf4j
52 @Data
53 @Component("CLLBusinessIntentManagementFunction")
54 public class CLLBusinessIntentManagementFunction extends IntentManagementFunction {
55
56     @Resource(name = "CLLBusinessActuationModule")
57     public void setActuationModule(ActuationModule actuationModule) {
58         this.actuationModule = actuationModule;
59     }
60
61     @Resource(name = "CLLBusinessKnowledgeModule")
62     public void setKnowledgeModule(KnowledgeModule knowledgeModule) {
63         this.knowledgeModule = knowledgeModule;
64     }
65
66     @Resource(name = "CLLBusinessDecisionModule")
67     public void setDecisionModule(DecisionModule decisionModule) {
68         this.decisionModule = decisionModule;
69     }
70
71     @Autowired
72     public IntentContextService intentContextService;
73     @Autowired
74     IntentInterfaceService intentInterfaceService;
75     @Autowired
76     ApplicationContext applicationContext;
77     @Autowired
78     ContextService contextService;
79     @Autowired
80     IntentService intentService;
81     @Autowired
82     IntentEventRecordService intentEventRecordService;
83
84     @Autowired
85     private FormatIntentInputManagementFunction formatIntentInputManagementFunction;
86
87     @Resource(name = "intentTaskExecutor")
88     ThreadPoolTaskExecutor executor;
89
90     @Override
91     public void receiveIntentAsOwner(IntentGoalBean intentGoalBean) {
92         IntentGoalBean originIntentGoalBean = detection(intentGoalBean);
93         LinkedHashMap<IntentGoalBean, IntentManagementFunction> linkedMap = investigation(originIntentGoalBean);
94         implementIntent(intentGoalBean.getIntent(), linkedMap);
95     }
96
97     @Override
98     public void receiveIntentAsHandler(Intent originalIntent, IntentGoalBean intentGoalBean, IntentManagementFunction handler) {
99         ActuationModule actuationModule = handler.getActuationModule();
100         IntentGoalType type = intentGoalBean.getIntentGoalType();
101         //before dataBase operate
102         handler.receiveIntentAsOwner(intentGoalBean);
103
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             //before delete cllBusinessIntent check subintent deleted
110             boolean deleteFinished = false;
111             Intent cllIntent = intentGoalBean.getIntent();
112             //subIntent size and delete Intent size
113             List<Context> list = cllIntent.getIntentContexts().stream().filter(x ->
114                     StringUtils.equals(x.getContextName(), "subIntent info")).collect(Collectors.toList());
115             int subIntentSize = list.get(0).getContextConditions().size();
116             while (!deleteFinished) {
117                 List<IntentEventRecord> deleteList = intentEventRecordService.getRecordByPid(cllIntent.getIntentId(),
118                         IntentGoalType.DELETE.name());
119                 if (subIntentSize <= deleteList.size()) {
120                     deleteFinished = true;
121                 }
122             }
123             actuationModule.deleteIntentToDb(intentGoalBean.getIntent());
124         }
125     }
126
127     @Override
128     public void createReport(String intentId, FulfillmentInfo fulfillmentInfo) {
129         String parentIntentId = intentService.findParentByIntentId(intentId);
130         intentInterfaceService.reportInterface(formatIntentInputManagementFunction, parentIntentId, fulfillmentInfo);
131
132         saveFulfillmentAndObjectInstance(intentId, fulfillmentInfo);
133     }
134
135     public IntentGoalBean detection(IntentGoalBean intentGoalBean) {
136         Intent originIntent = intentGoalBean.getIntent();
137         IntentGoalType intentGoalType = intentGoalBean.getIntentGoalType();
138         if (intentGoalType == IntentGoalType.CREATE) {
139             return knowledgeModule.intentCognition(originIntent);
140         } else if (intentGoalType == IntentGoalType.UPDATE) {
141             return new IntentGoalBean(intentGoalBean.getIntent(), IntentGoalType.UPDATE);
142         } else {
143             return new IntentGoalBean(intentGoalBean.getIntent(), IntentGoalType.DELETE);
144         }
145     }
146
147     public LinkedHashMap<IntentGoalBean, IntentManagementFunction> investigation(IntentGoalBean intentGoalBean) {
148         IntentGoalType intentGoalType = intentGoalBean.getIntentGoalType();
149         if (intentGoalType == IntentGoalType.CREATE) {
150             return decisionModule.investigationCreateProcess(intentGoalBean);
151         } else if (intentGoalType == IntentGoalType.UPDATE) {
152             return decisionModule.investigationUpdateProcess(intentGoalBean);
153         } else {
154             return decisionModule.investigationDeleteProcess(intentGoalBean);
155         }
156     }
157
158     @SneakyThrows
159     public boolean implementIntent(Intent originIntent, LinkedHashMap<IntentGoalBean, IntentManagementFunction> linkedIntentMap) {
160         Iterator<Map.Entry<IntentGoalBean, IntentManagementFunction>> iterator = linkedIntentMap.entrySet().iterator();
161         while (iterator.hasNext()) {
162             Map.Entry<IntentGoalBean, IntentManagementFunction> next = iterator.next();
163             IntentGoalBean newIntentGoalBean = next.getKey();
164             IntentGoalType intentGoalType = newIntentGoalBean.getIntentGoalType();
165             IntentManagementFunction handler = next.getValue();
166             if (intentGoalType == IntentGoalType.CREATE) {
167                 Intent newIdIntent = decisionModule.intentObjectDefine(originIntent, next.getKey().getIntent());
168                 intentContextService.updateIntentOwnerHandlerContext(newIdIntent, this, next.getValue());
169                 intentContextService.updateParentIntentContext(originIntent, newIdIntent);
170                 intentContextService.updateChindIntentContext(originIntent, newIdIntent);
171                 // contextService.updateContextList(originIntent.getIntentContexts(), originIntent.getIntentId());
172                 //intent-Distribution-create
173                 boolean isAcceptCreate = intentInterfaceService.createInterface(originIntent,
174                         new IntentGoalBean(newIdIntent, IntentGoalType.CREATE), next.getValue());
175                 // TODO: 2023/3/27  isParallel status need deal before distribution
176                 boolean isParallel = false;
177                 if (!isParallel) {
178                     //Block and regularly query whether the event is published
179                     boolean isPublish = false;
180                     int count = 1;
181                     while (!isPublish) {
182                         // Set the timeout to be 60min
183                         if (count >= 3600) {
184                             throw new CommonException("Timed out for implementing intent", 500);
185                         }
186                         log.debug("Try to get record of intent CREATE event from DB.");
187                         IntentEventRecord record = intentEventRecordService.getIntentEventRecordByIntentId(
188                                                         newIdIntent.getIntentId(), intentGoalType.toString());
189                         if (record != null) {
190                             isPublish = true;
191                             log.debug("Successfully got Intent Event Record from DB.");
192                         } else {
193                             log.debug("Index " + count + ": None Intent Event Record been got. Will try again.");
194                         }
195                         count++;
196                         Thread.sleep(1000);
197                     }
198                 }
199                 // return isAcceptCreate;
200             } else if (intentGoalType == IntentGoalType.UPDATE) {
201                 //define process  just send probe interface
202                 // intent-Distribution-update
203                 boolean isAcceptupdate = intentInterfaceService.updateInterface(originIntent,
204                         next.getKey(), next.getValue());
205             } else {
206                 // actuationModule.deleteIntentToDb(next.getKey().getIntent());
207                 // intent-Distribution-delete
208                 boolean isAcceptDelete = intentInterfaceService.deleteInterface(originIntent,
209                         next.getKey(), next.getValue());
210             }
211         }
212         return false;
213     }
214
215 }