65f40cb39de12780bf3aece9932b83efec4fbd70
[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.intentBaseService;
17
18 import lombok.Data;
19 import lombok.extern.log4j.Log4j2;
20 import org.onap.usecaseui.intentanalysis.bean.enums.ExpectationType;
21 import org.onap.usecaseui.intentanalysis.bean.models.Condition;
22 import org.onap.usecaseui.intentanalysis.bean.models.Expectation;
23 import org.onap.usecaseui.intentanalysis.bean.models.ExpectationTarget;
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.IntentGoalBean;
27 import org.onap.usecaseui.intentanalysis.intentBaseService.intentModule.ActuationModule;
28 import org.onap.usecaseui.intentanalysis.intentBaseService.intentModule.DecisionModule;
29 import org.onap.usecaseui.intentanalysis.intentBaseService.intentModule.KnowledgeModule;
30 import org.onap.usecaseui.intentanalysis.service.FulfillmentInfoService;
31 import org.onap.usecaseui.intentanalysis.service.IntentReportService;
32 import org.onap.usecaseui.intentanalysis.service.ObjectInstanceService;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.context.annotation.Configuration;
35 import org.springframework.stereotype.Component;
36 import org.springframework.util.CollectionUtils;
37
38 import javax.annotation.Resource;
39 import java.util.List;
40 import java.util.concurrent.ScheduledThreadPoolExecutor;
41 import java.util.concurrent.TimeUnit;
42 import java.util.stream.Collectors;
43
44 import static org.onap.usecaseui.intentanalysis.bean.enums.IntentGoalType.CREATE;
45
46 @Log4j2
47 @Data
48 @Configuration
49 @Component
50 public class IntentManagementFunction {
51     protected ActuationModule actuationModule;
52     protected DecisionModule decisionModule;
53     protected KnowledgeModule knowledgeModule;
54
55     @Autowired
56     private FulfillmentInfoService fulfillmentInfoService;
57
58     @Autowired
59     private ObjectInstanceService objectInstanceService;
60
61     @Autowired
62     private IntentReportService intentReportService;
63
64     @Resource(name = "intentReportExecutor")
65     private ScheduledThreadPoolExecutor scheduledThreadPoolExecutor;
66
67     public void receiveIntentAsOwner(IntentGoalBean intentGoalBean){};
68     public void receiveIntentAsHandler(Intent originalIntent, IntentGoalBean intentGoalBean, IntentManagementFunction handler){};
69     public void createReport(String intentId, FulfillmentInfo fulfillmentInfo){};
70
71     protected void saveFulfillmentAndObjectInstance(String intentId, FulfillmentInfo fulfillmentInfo) {
72         // save fulfillmentInfo and objectInstance
73         fulfillmentInfoService.saveFulfillmentInfo(intentId, fulfillmentInfo);
74         objectInstanceService.saveObjectInstances(intentId, fulfillmentInfo);
75     }
76
77     protected void generationIntentReport(IntentGoalBean intentGoalBean) {
78         if (CREATE != intentGoalBean.getIntentGoalType()) {
79             return;
80         }
81         Intent intent = intentGoalBean.getIntent();
82         List<Expectation> intentExpectations = intent.getIntentExpectations();
83         List<Expectation> report = intentExpectations.stream()
84                 .filter(expectation -> ExpectationType.REPORT.equals(expectation.getExpectationType()))
85                 .collect(Collectors.toList());
86         if (CollectionUtils.isEmpty(report)) {
87             log.info("No expectation of type report is entered");
88             return;
89         }
90         List<ExpectationTarget> expectationTargets = report.get(0).getExpectationTargets();
91         if (CollectionUtils.isEmpty(expectationTargets)) {
92             log.error("The expectation target is empty,expectationId is {}", report.get(0).getExpectationId());
93             return;
94         }
95         ExpectationTarget expectationTarget = expectationTargets.get(0);
96         List<Condition> targetConditions = expectationTarget.getTargetConditions();
97         if (CollectionUtils.isEmpty(targetConditions)) {
98             log.error("The target condition is empty,expectationId is {}", report.get(0).getExpectationId());
99             return;
100         }
101         Condition condition = targetConditions.get(0);
102         try {
103             int conditionValue = Integer.parseInt(condition.getConditionValue());
104             log.info("Start executing scheduled intent report generation task ");
105             scheduledThreadPoolExecutor.scheduleAtFixedRate(() -> intentReportService.saveIntentReportByIntentId(intent.getIntentId()), 2, conditionValue, TimeUnit.SECONDS);
106         } catch (Exception e) {
107             log.error("The exception is {}", e.getMessage());
108         }
109     }
110 }