2d420d7118ecefd538d4591c6c3441255fd1d057
[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.extern.slf4j.Slf4j;
19 import org.onap.usecaseui.intentanalysis.bean.models.Intent;
20 import org.onap.usecaseui.intentanalysis.bean.models.IntentManagementFunctionRegInfo;
21 import org.onap.usecaseui.intentanalysis.common.ResponseConsts;
22 import org.onap.usecaseui.intentanalysis.exception.DataBaseException;
23 import org.onap.usecaseui.intentanalysis.intentBaseService.intentProcessService.IntentProcessService;
24 import org.onap.usecaseui.intentanalysis.service.ImfRegInfoService;
25 import org.springframework.beans.factory.annotation.Autowired;
26 import org.springframework.context.ApplicationContext;
27 import org.springframework.stereotype.Service;
28
29 import java.util.List;
30 import java.util.Optional;
31 import java.util.stream.Collectors;
32 @Slf4j
33 @Service
34 public class IntentHandleService {
35     @Autowired
36     private IntentProcessService processService;
37     @Autowired
38     private ImfRegInfoService imfRegInfoService;
39     @Autowired
40     private ApplicationContext applicationContext;
41
42     /**
43      * Process the original intent and find the corresponding IntentManagementFunction
44      *
45      * @param intent
46      */
47     public void handleOriginalIntent(Intent intent) {
48         IntentManagementFunction intentOwner = getOriginalIMF(intent);
49         handleIntent(intent, intentOwner);
50     }
51
52     public void handleIntent(Intent intent, IntentManagementFunction intentOwner) {
53         processService.setIntentRole(intentOwner, null);
54         processService.intentProcess(intent);
55     }
56
57     public IntentManagementFunction selectIntentManagementFunction(Intent intent) {
58         //select the IntentManagementFunctionRegInfo Based on the IMFRegistry information.
59         //Only internalFunction support.
60         //and based on the IntentManagementFunctionRegInfo, get the right IntentManagementFunction bean.
61         //if  no  IntentManagementFunction selected, that means this intent is not supported by this system.
62         return null;
63     }
64
65     public IntentManagementFunctionRegInfo selectIntentManagementFunctionRegInfo(Intent intent) {
66         //select the IntentManagementFunctionRegInfo Based on the IMFRegistry information.
67         //Both internalFunction and externalFunction support.
68         //This is used to get he IntentManagementFunction for a subIntent decomposition.
69         return null;
70     }
71
72     public IntentManagementFunction getOriginalIMF(Intent intent) {
73         //select IntentManagementFunction based on intent  name
74         String intentName = intent.getIntentName();
75         List<IntentManagementFunctionRegInfo> imfRegInfoList = imfRegInfoService.getImfRegInfoList();
76         List<IntentManagementFunctionRegInfo> list = imfRegInfoList.stream().filter(x -> x.getSupportArea().contains(intentName)).collect(Collectors.toList());
77         if (!Optional.ofNullable(list).isPresent()) {
78             String msg = String.format("Intent name %s doesn't exist IntentManagementFunction in database.", intent.getIntentName());
79             log.error(msg);
80             throw new DataBaseException(msg, ResponseConsts.RET_QUERY_DATA_EMPTY);
81         }
82         return (IntentManagementFunction) applicationContext.getBean(list.get(0).getHandleName());
83     }
84 }