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;
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;
43 import javax.annotation.Resource;
44 import java.util.Iterator;
45 import java.util.LinkedHashMap;
46 import java.util.List;
48 import java.util.stream.Collectors;
52 @Component("CLLBusinessIntentManagementFunction")
53 public class CLLBusinessIntentManagementFunction extends IntentManagementFunction {
55 @Resource(name = "CLLBusinessActuationModule")
56 public void setActuationModule(ActuationModule actuationModule) {
57 this.actuationModule = actuationModule;
60 @Resource(name = "CLLBusinessKnowledgeModule")
61 public void setKnowledgeModule(KnowledgeModule knowledgeModule) {
62 this.knowledgeModule = knowledgeModule;
65 @Resource(name = "CLLBusinessDecisionModule")
66 public void setDecisionModule(DecisionModule decisionModule) {
67 this.decisionModule = decisionModule;
71 public IntentContextService intentContextService;
73 IntentInterfaceService intentInterfaceService;
75 ApplicationContext applicationContext;
77 ContextService contextService;
79 IntentService intentService;
81 IntentEventRecordService intentEventRecordService;
83 @Resource(name = "intentTaskExecutor")
84 ThreadPoolTaskExecutor executor;
87 public void receiveIntentAsOwner(IntentGoalBean intentGoalBean) {
88 IntentGoalBean originIntentGoalBean = detection(intentGoalBean);
89 LinkedHashMap<IntentGoalBean, IntentManagementFunction> linkedMap = investigation(originIntentGoalBean);
90 implementIntent(intentGoalBean.getIntent(), linkedMap);
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);
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;
119 actuationModule.deleteIntentToDb(intentGoalBean.getIntent());
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);
131 return new IntentGoalBean(intentGoalBean.getIntent(), IntentGoalType.DELETE);
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);
142 return decisionModule.investigationDeleteProcess(intentGoalBean);
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;
166 //Block and regularly query whether the event is published
167 boolean isPublish = false;
171 IntentEventRecord record = intentEventRecordService.getIntentEventRecordByIntentId(newIdIntent.getIntentId(), "create");
173 // it will take one hour to wait operation end
175 throw new CommonException("Operation took too long, failed", 500);
177 if (null != record) {
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());
189 // actuationModule.deleteIntentToDb(next.getKey().getIntent());
190 // intent-Distribution-delete
191 boolean isAcceptDelete = intentInterfaceService.deleteInterface(originIntent,
192 next.getKey(), next.getValue());