3487b4bc2cc6adcf6288b85685f47774048cb68e
[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         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;
175                 if (!isParallel) {
176                     //Block and regularly query whether the event is published
177                     boolean isPublish = false;
178                     int count = 1;
179                     while (!isPublish) {
180                         // Set the timeout to be 60min
181                         if (count >= 3600) {
182                             throw new CommonException("Timed out for implementing intent", 500);
183                         }
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) {
188                             isPublish = true;
189                             log.debug("Successfully got Intent Event Record from DB.");
190                         } else {
191                             log.debug("Index " + count + ": None Intent Event Record been got. Will try again.");
192                         }
193                         count++;
194                         Thread.sleep(1000);
195                     }
196                 }
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);
203             } else {
204                 // actuationModule.deleteIntentToDb(next.getKey().getIntent());
205                 // intent-Distribution-delete
206                 boolean isAcceptDelete = intentInterfaceService.deleteInterface(originIntent,
207                         next.getKey(), handler);
208             }
209         }
210         return false;
211     }
212
213 }