27d7bb408b1f849f8a932a77b67afe5dbc9be453
[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.common.ResponseConsts;
23 import org.onap.usecaseui.intentanalysis.exception.CommonException;
24 import org.onap.usecaseui.intentanalysis.exception.DataBaseException;
25 import org.onap.usecaseui.intentanalysis.mapper.*;
26 import org.onap.usecaseui.intentanalysis.service.ComponentNotificationService;
27 import org.springframework.beans.BeanUtils;
28 import org.springframework.beans.factory.annotation.Autowired;
29 import org.springframework.stereotype.Service;
30 import org.springframework.util.CollectionUtils;
31
32 import java.util.List;
33 import java.util.stream.Collectors;
34
35 @Slf4j
36 @Service
37 public class ComponentNotificationServiceImpl implements ComponentNotificationService {
38
39     @Autowired
40     private ExpectationObjectMapper expectationObjectMapper;
41
42     @Autowired
43     private ExpectationMapper expectationMapper;
44
45     @Autowired
46     private ContextMapper contextMapper;
47
48     @Autowired
49     private ConditionMapper conditionMapper;
50
51     @Autowired
52     private FulfillmentInfoMapper fulfillmentInfoMapper;
53
54     @Autowired
55     private ObjectInstanceMapper objectInstanceMapper;
56
57     /**
58      * Generate a new FulfillmentInfo based on third-party FulfillmentOperation
59      * and save it in the database
60      *
61      * @param eventModel param
62      */
63     @Override
64     public void callBack(FulfillmentOperation eventModel) {
65         if (eventModel == null) {
66             String msg = "The obtained fulfillmentInfo is null";
67             throw new CommonException(msg, ResponseConsts.EMPTY_PARAM);
68         }
69         String operation = eventModel.getOperation();
70         List<String> objectInstances = eventModel.getObjectInstances();
71         if (CollectionUtils.isEmpty(objectInstances) || StringUtils.isEmpty(operation)) {
72             String msg = "The obtained objectInstances or operation are empty";
73             throw new CommonException(msg, ResponseConsts.EMPTY_PARAM);
74         }
75         log.info("Get objectInstances is {}", objectInstances);
76         List<String> expectationIds = expectationObjectMapper.getExpectationIdByObjectInstance(objectInstances.get(0));
77         if (CollectionUtils.isEmpty(expectationIds)) {
78             String msg = "Get expectationId is null from database";
79             log.error(msg);
80             return;
81         }
82         log.info("ExpectationId is {}", expectationIds);
83         String intentId = null;
84         for (String expectationId : expectationIds) {
85             // TODO
86             ExpectationType expectationType = getExpectationType(operation);
87             intentId = expectationMapper.getIntentIdByExpectationId(expectationId, expectationType);
88             if (StringUtils.isNotEmpty(intentId)) {
89                 break;
90             }
91         }
92         log.error("The intentId is {}", intentId);
93
94         if (StringUtils.isEmpty(intentId)) {
95             String msg = "Get intentId is null from database";
96             log.error(msg);
97             throw new DataBaseException(msg, ResponseConsts.RET_QUERY_DATA_EMPTY);
98         }
99
100         String parentByIntentId = findParentByIntentId(findParentByIntentId(intentId));
101         log.info("The parentByIntentId is {}", parentByIntentId);
102
103         saveFulfillmentInfo(parentByIntentId, eventModel);
104     }
105
106     private void saveFulfillmentInfo(String intentId, FulfillmentOperation eventModel) {
107         FulfillmentInfo fulfillmentInfo = fulfillmentInfoMapper.selectFulfillmentInfo(intentId);
108         if (fulfillmentInfo != null) {
109             fulfillmentInfoMapper.deleteFulfillmentInfo(intentId);
110         }
111         FulfillmentInfo newInfo = new FulfillmentInfo();
112         BeanUtils.copyProperties(eventModel, newInfo);
113         int num = fulfillmentInfoMapper.insertFulfillmentInfo(newInfo, intentId);
114         FulfillmentInfo fulfillmentInfo1 = fulfillmentInfoMapper.selectFulfillmentInfo(intentId);
115         if (num < 1) {
116             String msg = "Failed to insert fulfillmentInfo to database.";
117             log.error(msg);
118             throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL);
119         }
120         int objectInstanceNum = objectInstanceMapper.insertObjectInstanceList(eventModel.getObjectInstances(), intentId);
121         if (objectInstanceNum < 1) {
122             String msg = "Failed to insert objectInstances to database.";
123             log.error(msg);
124             throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL);
125         }
126     }
127
128     public String findParentByIntentId(String intentId) {
129         List<Context> contexts = contextMapper.selectContextList(intentId);
130         if (CollectionUtils.isEmpty(contexts)) {
131             log.error("Get context is empty,intentId is {}", intentId);
132             String msg = "Get Contexts is empty from database";
133             throw new DataBaseException(msg, ResponseConsts.RET_QUERY_DATA_EMPTY);
134         }
135         List<Context> collect = contexts.stream()
136                 .filter(context -> "parentIntent info".equalsIgnoreCase(context.getContextName()))
137                 .collect(Collectors.toList());
138         if (CollectionUtils.isEmpty(collect) || collect.size() != 1) {
139             log.error("This intent has not parent intent,intentId is {}", intentId);
140             String msg = "Get Context is empty from database";
141             throw new DataBaseException(msg, ResponseConsts.RET_QUERY_DATA_EMPTY);
142         }
143         Context context = collect.get(0);
144         List<Condition> conditions = conditionMapper.selectConditionList(context.getContextId());
145         if (CollectionUtils.isEmpty(conditions) || StringUtils.isEmpty(conditions.get(0).getConditionValue())) {
146             log.error("");
147             String msg = "Get conditions is empty from database";
148             throw new DataBaseException(msg, ResponseConsts.RET_QUERY_DATA_EMPTY);
149         }
150         return conditions.get(0).getConditionValue();
151     }
152
153     private ExpectationType getExpectationType(String operation) {
154         if (operation.contains("Assurance")) {
155             return ExpectationType.ASSURANCE;
156         }
157         return ExpectationType.DELIVERY;
158     }
159 }