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.util.CollectionUtils;
32 import java.util.List;
33 import java.util.stream.Collectors;
37 public class ComponentNotificationServiceImpl implements ComponentNotificationService {
40 private ExpectationObjectMapper expectationObjectMapper;
43 private ExpectationMapper expectationMapper;
46 private ContextMapper contextMapper;
49 private ConditionMapper conditionMapper;
52 private FulfillmentInfoMapper fulfillmentInfoMapper;
55 private ObjectInstanceMapper objectInstanceMapper;
58 * Generate a new FulfillmentInfo based on third-party FulfillmentOperation
59 * and save it in the database
61 * @param eventModel param
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);
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);
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";
82 log.info("ExpectationId is {}", expectationIds);
83 String intentId = null;
84 for (String expectationId : expectationIds) {
86 ExpectationType expectationType = getExpectationType(operation);
87 intentId = expectationMapper.getIntentIdByExpectationId(expectationId, expectationType);
88 if (StringUtils.isNotEmpty(intentId)) {
92 log.error("The intentId is {}", 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);
114 FulfillmentInfo fulfillmentInfo1 = fulfillmentInfoMapper.selectFulfillmentInfo(intentId);
116 String msg = "Failed to insert fulfillmentInfo to database.";
118 throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL);
120 int objectInstanceNum = objectInstanceMapper.insertObjectInstanceList(eventModel.getObjectInstances(), intentId);
121 if (objectInstanceNum < 1) {
122 String msg = "Failed to insert objectInstances to database.";
124 throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL);
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);
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);
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())) {
147 String msg = "Get conditions is empty from database";
148 throw new DataBaseException(msg, ResponseConsts.RET_QUERY_DATA_EMPTY);
150 return conditions.get(0).getConditionValue();
153 private ExpectationType getExpectationType(String operation) {
154 if (operation.contains("Assurance")) {
155 return ExpectationType.ASSURANCE;
157 return ExpectationType.DELIVERY;