c9de92ab6f5b0e2fd8640700ba3bdd8915ab62eb
[usecase-ui/intent-analysis.git] /
1 /*
2  * Copyright (C) 2023 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.service.impl;
17
18 import lombok.extern.slf4j.Slf4j;
19 import org.apache.commons.lang.StringUtils;
20 import org.onap.usecaseui.intentanalysis.bean.enums.ExpectationType;
21 import org.onap.usecaseui.intentanalysis.bean.models.*;
22 import org.onap.usecaseui.intentanalysis.cllassuranceIntentmgt.CLLAssuranceIntentManagementFunction;
23 import org.onap.usecaseui.intentanalysis.clldeliveryIntentmgt.CLLDeliveryIntentManagementFunction;
24 import org.onap.usecaseui.intentanalysis.common.ResponseConsts;
25 import org.onap.usecaseui.intentanalysis.exception.CommonException;
26 import org.onap.usecaseui.intentanalysis.exception.DataBaseException;
27 import org.onap.usecaseui.intentanalysis.intentBaseService.IntentManagementFunction;
28 import org.onap.usecaseui.intentanalysis.intentBaseService.intentinterfaceservice.IntentInterfaceService;
29 import org.onap.usecaseui.intentanalysis.mapper.*;
30 import org.onap.usecaseui.intentanalysis.service.ComponentNotificationService;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.context.ApplicationContext;
33 import org.springframework.stereotype.Service;
34 import org.springframework.transaction.annotation.Transactional;
35 import org.springframework.util.CollectionUtils;
36
37 import java.util.List;
38
39 @Slf4j
40 @Service
41 public class ComponentNotificationServiceImpl implements ComponentNotificationService {
42
43     @Autowired
44     private ExpectationObjectMapper expectationObjectMapper;
45
46     @Autowired
47     private ExpectationMapper expectationMapper;
48
49     @Autowired
50     private ApplicationContext applicationContext;
51
52     @Autowired
53     private IntentInterfaceService intentInterfaceService;
54
55     /**
56      * Generate a new FulfillmentInfo based on third-party FulfillmentOperation
57      * and save it in the database
58      *
59      * @param eventModel param
60      */
61     @Override
62     @Transactional(rollbackFor = DataBaseException.class)
63     public void callBack(FulfillmentOperation eventModel) {
64         if (eventModel == null) {
65             String msg = "The obtained fulfillmentInfo is null";
66             throw new CommonException(msg, ResponseConsts.EMPTY_PARAM);
67         }
68         String operation = eventModel.getOperation();
69         List<String> objectInstances = eventModel.getObjectInstances();
70         if (CollectionUtils.isEmpty(objectInstances) || StringUtils.isEmpty(operation)) {
71             String msg = "The obtained objectInstances or operation are empty";
72             throw new CommonException(msg, ResponseConsts.EMPTY_PARAM);
73         }
74         log.info("Get objectInstances is {}", objectInstances);
75         List<String> expectationIds = expectationObjectMapper.getExpectationIdByObjectInstance(objectInstances.get(0));
76         if (CollectionUtils.isEmpty(expectationIds)) {
77             String msg = "Get expectationId is null from database";
78             log.error(msg);
79             return;
80         }
81         log.info("ExpectationId is {}", expectationIds);
82         String intentId = null;
83         ExpectationType expectationType = getExpectationType(operation);
84         for (String expectationId : expectationIds) {
85             intentId = expectationMapper.getIntentIdByExpectationId(expectationId, expectationType);
86             if (StringUtils.isNotEmpty(intentId)) {
87                 break;
88             }
89         }
90
91         if (StringUtils.isEmpty(intentId)) {
92             String msg = "Get intentId is null from database";
93             log.error(msg);
94             throw new DataBaseException(msg, ResponseConsts.RET_QUERY_DATA_EMPTY);
95         }
96
97         IntentManagementFunction function;
98         if (expectationType == ExpectationType.ASSURANCE) {
99             function = (IntentManagementFunction) applicationContext.getBean(CLLAssuranceIntentManagementFunction.class.getSimpleName());
100         } else {
101             function = (IntentManagementFunction) applicationContext.getBean(CLLDeliveryIntentManagementFunction.class.getSimpleName());
102         }
103         intentInterfaceService.reportInterface(function, intentId, eventModel);
104     }
105
106     private ExpectationType getExpectationType(String operation) {
107         if (operation.contains("Assurance")) {
108             return ExpectationType.ASSURANCE;
109         }
110         return ExpectationType.DELIVERY;
111     }
112 }