2 * Copyright (C) 2022 CMCC, Inc. and others. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onap.usecaseui.intentanalysis.cllBusinessIntentMgt;
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;
44 import javax.annotation.Resource;
45 import java.util.Iterator;
46 import java.util.LinkedHashMap;
47 import java.util.List;
49 import java.util.stream.Collectors;
53 @Component("CLLBusinessIntentManagementFunction")
54 public class CLLBusinessIntentManagementFunction extends IntentManagementFunction {
56 @Resource(name = "CLLBusinessActuationModule")
57 public void setActuationModule(ActuationModule actuationModule) {
58 this.actuationModule = actuationModule;
61 @Resource(name = "CLLBusinessKnowledgeModule")
62 public void setKnowledgeModule(KnowledgeModule knowledgeModule) {
63 this.knowledgeModule = knowledgeModule;
66 @Resource(name = "CLLBusinessDecisionModule")
67 public void setDecisionModule(DecisionModule decisionModule) {
68 this.decisionModule = decisionModule;
72 public IntentContextService intentContextService;
74 IntentInterfaceService intentInterfaceService;
76 ApplicationContext applicationContext;
78 ContextService contextService;
80 IntentService intentService;
82 IntentEventRecordService intentEventRecordService;
85 private FormatIntentInputManagementFunction formatIntentInputManagementFunction;
87 @Resource(name = "intentTaskExecutor")
88 ThreadPoolTaskExecutor executor;
91 public void receiveIntentAsOwner(IntentGoalBean intentGoalBean) {
92 IntentGoalBean originIntentGoalBean = detection(intentGoalBean);
93 LinkedHashMap<IntentGoalBean, IntentManagementFunction> linkedMap = investigation(originIntentGoalBean);
94 implementIntent(intentGoalBean.getIntent(), linkedMap);
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);
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;
123 actuationModule.deleteIntentToDb(intentGoalBean.getIntent());
128 public void createReport(String intentId, FulfillmentInfo fulfillmentInfo) {
129 String parentIntentId = intentService.findParentByIntentId(intentId);
130 intentInterfaceService.reportInterface(formatIntentInputManagementFunction, parentIntentId, fulfillmentInfo);
132 saveFulfillmentAndObjectInstance(intentId, fulfillmentInfo);
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);
143 return new IntentGoalBean(intentGoalBean.getIntent(), IntentGoalType.DELETE);
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);
154 return decisionModule.investigationDeleteProcess(intentGoalBean);
159 public boolean implementIntent(Intent originIntent, LinkedHashMap<IntentGoalBean, IntentManagementFunction> linkedIntentMap) {
160 for (Map.Entry<IntentGoalBean, IntentManagementFunction> next : linkedIntentMap.entrySet()) {
161 IntentGoalBean newIntentGoalBean = next.getKey();
162 IntentGoalType intentGoalType = newIntentGoalBean.getIntentGoalType();
163 IntentManagementFunction handler = next.getValue();
164 if (intentGoalType == IntentGoalType.CREATE) {
165 Intent newIdIntent = decisionModule.intentObjectDefine(originIntent, next.getKey().getIntent());
166 intentContextService.updateIntentOwnerHandlerContext(newIdIntent, this, handler);
167 intentContextService.updateParentIntentContext(originIntent, newIdIntent);
168 intentContextService.updateChindIntentContext(originIntent, newIdIntent);
169 // contextService.updateContextList(originIntent.getIntentContexts(), originIntent.getIntentId());
170 //intent-Distribution-create
171 boolean isAcceptCreate = intentInterfaceService.createInterface(originIntent,
172 new IntentGoalBean(newIdIntent, IntentGoalType.CREATE), handler);
173 // TODO: 2023/3/27 isParallel status need deal before distribution
174 boolean isParallel = false;
176 //Block and regularly query whether the event is published
177 boolean isPublish = false;
180 // Set the timeout to be 60min
182 throw new CommonException("Timed out for implementing intent", 500);
184 log.debug("Try to get record of intent CREATE event from DB.");
185 IntentEventRecord record = intentEventRecordService.getIntentEventRecordByIntentId(
186 newIdIntent.getIntentId(), intentGoalType.toString());
187 if (record != null) {
189 log.debug("Successfully got Intent Event Record from DB.");
191 log.debug("Index " + count + ": None Intent Event Record been got. Will try again.");
197 // return isAcceptCreate;
198 } else if (intentGoalType == IntentGoalType.UPDATE) {
199 //define process just send probe interface
200 // intent-Distribution-update
201 boolean isAcceptupdate = intentInterfaceService.updateInterface(originIntent,
202 next.getKey(), handler);
204 // actuationModule.deleteIntentToDb(next.getKey().getIntent());
205 // intent-Distribution-delete
206 boolean isAcceptDelete = intentInterfaceService.deleteInterface(originIntent,
207 next.getKey(), handler);