2 * Copyright (C) 2023 CMCC, Inc. and others. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onap.usecaseui.intentanalysis.service.impl;
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.transaction.annotation.Transactional;
31 import org.springframework.util.CollectionUtils;
33 import java.util.List;
34 import java.util.stream.Collectors;
38 public class ComponentNotificationServiceImpl implements ComponentNotificationService {
41 private ExpectationObjectMapper expectationObjectMapper;
44 private ExpectationMapper expectationMapper;
47 private ContextMapper contextMapper;
50 private ConditionMapper conditionMapper;
53 private FulfillmentInfoMapper fulfillmentInfoMapper;
56 private ObjectInstanceMapper objectInstanceMapper;
59 * Generate a new FulfillmentInfo based on third-party FulfillmentOperation
60 * and save it in the database
62 * @param eventModel param
65 @Transactional(rollbackFor = DataBaseException.class)
66 public void callBack(FulfillmentOperation eventModel) {
67 if (eventModel == null) {
68 String msg = "The obtained fulfillmentInfo is null";
69 throw new CommonException(msg, ResponseConsts.EMPTY_PARAM);
71 String operation = eventModel.getOperation();
72 List<String> objectInstances = eventModel.getObjectInstances();
73 if (CollectionUtils.isEmpty(objectInstances) || StringUtils.isEmpty(operation)) {
74 String msg = "The obtained objectInstances or operation are empty";
75 throw new CommonException(msg, ResponseConsts.EMPTY_PARAM);
77 log.info("Get objectInstances is {}", objectInstances);
78 List<String> expectationIds = expectationObjectMapper.getExpectationIdByObjectInstance(objectInstances.get(0));
79 if (CollectionUtils.isEmpty(expectationIds)) {
80 String msg = "Get expectationId is null from database";
84 log.info("ExpectationId is {}", expectationIds);
85 String intentId = null;
86 for (String expectationId : expectationIds) {
87 ExpectationType expectationType = getExpectationType(operation);
88 intentId = expectationMapper.getIntentIdByExpectationId(expectationId, expectationType);
89 if (StringUtils.isNotEmpty(intentId)) {
94 if (StringUtils.isEmpty(intentId)) {
95 String msg = "Get intentId is null from database";
97 throw new DataBaseException(msg, ResponseConsts.RET_QUERY_DATA_EMPTY);
100 String parentByIntentId = findParentByIntentId(findParentByIntentId(intentId));
101 log.info("The parentByIntentId is {}", parentByIntentId);
103 saveFulfillmentInfo(parentByIntentId, eventModel);
106 private void saveFulfillmentInfo(String intentId, FulfillmentOperation eventModel) {
107 FulfillmentInfo fulfillmentInfo = fulfillmentInfoMapper.selectFulfillmentInfo(intentId);
108 if (fulfillmentInfo != null) {
109 fulfillmentInfoMapper.deleteFulfillmentInfo(intentId);
111 FulfillmentInfo newInfo = new FulfillmentInfo();
112 BeanUtils.copyProperties(eventModel, newInfo);
113 int num = fulfillmentInfoMapper.insertFulfillmentInfo(newInfo, intentId);
115 String msg = "Failed to insert fulfillmentInfo to database.";
117 throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL);
119 int objectInstanceNum = objectInstanceMapper.insertObjectInstanceList(eventModel.getObjectInstances(), intentId);
120 if (objectInstanceNum < 1) {
121 String msg = "Failed to insert objectInstances to database.";
123 throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL);
127 public String findParentByIntentId(String intentId) {
128 List<Context> contexts = contextMapper.selectContextList(intentId);
129 if (CollectionUtils.isEmpty(contexts)) {
130 log.error("Get context is empty,intentId is {}", intentId);
131 String msg = "Get Contexts is empty from database";
132 throw new DataBaseException(msg, ResponseConsts.RET_QUERY_DATA_EMPTY);
134 List<Context> collect = contexts.stream()
135 .filter(context -> "parentIntent info".equalsIgnoreCase(context.getContextName()))
136 .collect(Collectors.toList());
137 if (CollectionUtils.isEmpty(collect) || collect.size() != 1) {
138 log.error("This intent has not parent intent,intentId is {}", intentId);
139 String msg = "Get Context is empty from database";
140 throw new DataBaseException(msg, ResponseConsts.RET_QUERY_DATA_EMPTY);
142 Context context = collect.get(0);
143 List<Condition> conditions = conditionMapper.selectConditionList(context.getContextId());
144 if (CollectionUtils.isEmpty(conditions) || StringUtils.isEmpty(conditions.get(0).getConditionValue())) {
145 String msg = "Get conditions is empty from database";
146 throw new DataBaseException(msg, ResponseConsts.RET_QUERY_DATA_EMPTY);
148 return conditions.get(0).getConditionValue();
151 private ExpectationType getExpectationType(String operation) {
152 if (operation.contains("Assurance")) {
153 return ExpectationType.ASSURANCE;
155 return ExpectationType.DELIVERY;