608423e04124e19a33d4ec8798d59799ac6a4a11
[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.transaction.annotation.Transactional;
31 import org.springframework.util.CollectionUtils;
32
33 import java.util.List;
34 import java.util.stream.Collectors;
35
36 @Slf4j
37 @Service
38 public class ComponentNotificationServiceImpl implements ComponentNotificationService {
39
40     @Autowired
41     private ExpectationObjectMapper expectationObjectMapper;
42
43     @Autowired
44     private ExpectationMapper expectationMapper;
45
46     @Autowired
47     private ContextMapper contextMapper;
48
49     @Autowired
50     private ConditionMapper conditionMapper;
51
52     @Autowired
53     private FulfillmentInfoMapper fulfillmentInfoMapper;
54
55     @Autowired
56     private ObjectInstanceMapper objectInstanceMapper;
57
58     /**
59      * Generate a new FulfillmentInfo based on third-party FulfillmentOperation
60      * and save it in the database
61      *
62      * @param eventModel param
63      */
64     @Override
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);
70         }
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);
76         }
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";
81             log.error(msg);
82             return;
83         }
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)) {
90                 break;
91             }
92         }
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         if (num < 1) {
115             String msg = "Failed to insert fulfillmentInfo to database.";
116             log.error(msg);
117             throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL);
118         }
119         List<String> instances = eventModel.getObjectInstances();
120         List<String> objectInstancesDb = objectInstanceMapper.getObjectInstances(intentId);
121         if (!CollectionUtils.isEmpty(objectInstancesDb)) {
122             instances.removeAll(objectInstancesDb);
123             if (CollectionUtils.isEmpty(instances)) {
124                 log.info("The objectInstances already exist in the database");
125                 return;
126             }
127         }
128         int objectInstanceNum = objectInstanceMapper.insertObjectInstanceList(instances, intentId);
129         if (objectInstanceNum < 1) {
130             String msg = "Failed to insert objectInstances to database.";
131             log.error(msg);
132             throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL);
133         }
134     }
135
136     public String findParentByIntentId(String intentId) {
137         List<Context> contexts = contextMapper.selectContextList(intentId);
138         if (CollectionUtils.isEmpty(contexts)) {
139             log.error("Get context is empty,intentId is {}", intentId);
140             String msg = "Get Contexts is empty from database";
141             throw new DataBaseException(msg, ResponseConsts.RET_QUERY_DATA_EMPTY);
142         }
143         List<Context> collect = contexts.stream()
144                 .filter(context -> "parentIntent info".equalsIgnoreCase(context.getContextName()))
145                 .collect(Collectors.toList());
146         if (CollectionUtils.isEmpty(collect) || collect.size() != 1) {
147             log.error("This intent has not parent intent,intentId is {}", intentId);
148             String msg = "Get Context is empty from database";
149             throw new DataBaseException(msg, ResponseConsts.RET_QUERY_DATA_EMPTY);
150         }
151         Context context = collect.get(0);
152         List<Condition> conditions = conditionMapper.selectConditionList(context.getContextId());
153         if (CollectionUtils.isEmpty(conditions) || StringUtils.isEmpty(conditions.get(0).getConditionValue())) {
154             String msg = "Get conditions is empty from database";
155             throw new DataBaseException(msg, ResponseConsts.RET_QUERY_DATA_EMPTY);
156         }
157         return conditions.get(0).getConditionValue();
158     }
159
160     private ExpectationType getExpectationType(String operation) {
161         if (operation.contains("Assurance")) {
162             return ExpectationType.ASSURANCE;
163         }
164         return ExpectationType.DELIVERY;
165     }
166 }