2 * Copyright (C) 2022 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.clldeliveryIntentmgt.clldeliverymodule;
18 import com.alibaba.fastjson.JSON;
19 import com.alibaba.fastjson.JSONArray;
20 import com.alibaba.fastjson.JSONObject;
21 import lombok.extern.slf4j.Slf4j;
22 import org.apache.commons.lang.StringUtils;
23 import org.onap.usecaseui.intentanalysis.adapters.so.SOService;
24 import org.onap.usecaseui.intentanalysis.bean.models.*;
25 import org.onap.usecaseui.intentanalysis.bean.enums.*;
26 import org.onap.usecaseui.intentanalysis.intentBaseService.IntentManagementFunction;
27 import org.onap.usecaseui.intentanalysis.intentBaseService.intentModule.ActuationModule;
28 import org.onap.usecaseui.intentanalysis.service.ExpectationObjectService;
29 import org.onap.usecaseui.intentanalysis.service.ExpectationService;
30 import org.onap.usecaseui.intentanalysis.service.FulfillmentInfoService;
31 import org.onap.usecaseui.intentanalysis.service.IntentService;
32 import org.onap.usecaseui.intentanalysis.util.HttpUtil;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.stereotype.Component;
36 import java.util.Arrays;
37 import java.util.Collections;
38 import java.util.HashMap;
39 import java.util.List;
41 import java.util.UUID;
42 import java.util.stream.Collectors;
46 public class CLLDeliveryActuationModule extends ActuationModule {
47 public final static String NLP_HOST = "http://uui-nlp";
48 public final static String NLP_ONLINE_URL_BASE = NLP_HOST + ":33011";
49 public final static String PREDICT_URL = NLP_ONLINE_URL_BASE + "/api/online/predict";
52 private SOService soService;
55 private ExpectationObjectService expectationObjectService;
58 private ExpectationService expectationService;
61 private FulfillmentInfoService fulfillmentInfoService;
64 private IntentService intentService;
67 public void toNextIntentHandler(IntentGoalBean intentGoalBean, IntentManagementFunction IntentHandler) {
72 public void directOperation(IntentGoalBean intentGoalBean) {
73 Intent intent = intentGoalBean.getIntent();
74 Expectation deliveryExpectation = intent.getIntentExpectations().stream()
75 .filter(expectation -> !ExpectationType.REPORT.equals(expectation.getExpectationType()))
76 .collect(Collectors.toList()).get(0);
77 if (StringUtils.equalsIgnoreCase("create", intentGoalBean.getIntentGoalType().name())) {
78 Map<String, Object> params = new HashMap<>();
79 Map<String, String> accessPointOne = new HashMap<>();
80 List<ExpectationTarget> targetList = deliveryExpectation.getExpectationTargets();
81 for (ExpectationTarget target : targetList) {
82 String conditionName = target.getTargetConditions().get(0).getConditionName();
83 String conditionValue = target.getTargetConditions().get(0).getConditionValue();
84 String value = getPredictValue(conditionName, conditionValue);
85 if (StringUtils.containsIgnoreCase(conditionName, "source")) {
86 accessPointOne.put("name", value);
87 } else if (StringUtils.containsIgnoreCase(conditionName, "destination")) {
88 params.put("cloudPointName", value);
89 } else if (StringUtils.containsIgnoreCase(conditionName, "bandwidth")) {
90 accessPointOne.put("bandwidth", value);
93 params.put("accessPointOne", accessPointOne);
94 params.put("instanceId", getInstanceId());
95 params.put("name", "cll-" + params.get("instanceId"));
96 params.put("protect", false);
98 updateFulfillment(params, deliveryExpectation.getExpectationId());
100 // fill and update the objectInstance of intent expectation(include delivery and report)
101 List<String> objectInstance = Collections.singletonList((String) params.get("name"));
102 intent.getIntentExpectations().forEach(expectation -> {
103 ExpectationObject expectationObject = expectationObjectService.getExpectationObject(expectation.getExpectationId());
104 expectationObject.setObjectInstance(objectInstance);
105 expectation.getExpectationObject().setObjectInstance(objectInstance);
106 expectationObjectService.updateExpectationObject(expectationObject, expectation.getExpectationId());
108 } else if (StringUtils.equalsIgnoreCase("delete", intentGoalBean.getIntentGoalType().name())) {
109 List<String> objectInstance = deliveryExpectation.getExpectationObject().getObjectInstance();
110 objectInstance.forEach(instanceId->soService.deleteIntentInstance(instanceId));
112 List<String> objectInstance = deliveryExpectation.getExpectationObject().getObjectInstance();
113 objectInstance.forEach(instanceId->soService.deleteIntentInstance(instanceId));
114 intentService.deleteIntent(intent.getIntentId());
119 public void interactWithIntentHandle() {
124 public void fulfillIntent(IntentGoalBean intentGoalBean, IntentManagementFunction intentHandler) {
125 this.directOperation(intentGoalBean);
128 private void updateFulfillment(Map<String, Object> params,String expectationId){
129 // Get the fulfillmentInfo of the first exception which need to be updated with resultHeader returned
130 FulfillmentInfo fulfillmentInfo = new FulfillmentInfo();
131 Expectation intentExpectation = expectationService.getIntentExpectation(expectationId);
132 if (intentExpectation != null) {
133 FulfillmentInfo expectationFulfillmentInfo = intentExpectation.getExpectationFulfillmentInfo();
134 if (expectationFulfillmentInfo != null) {
135 fulfillmentInfo = expectationFulfillmentInfo;
138 ResultHeader resultHeader = soService.createIntentInstance(params);
139 // Update fulfillmentInfo and write back to DB
140 // Originally set to be NOT_FULFILLED, and will change after requesting the SO operation status
141 fulfillmentInfo.setFulfillmentStatus(FulfillmentStatus.NOT_FULFILLED);
142 fulfillmentInfo.setNotFulfilledReason(resultHeader.getResult_message());
144 // If SO request accepted, means intent acknowledged, otherwise, means failed
145 if (resultHeader.getResult_code() == 1) {
146 fulfillmentInfo.setNotFulfilledState(NotFulfilledState.ACKNOWLEDGED);
148 fulfillmentInfo.setNotFulfilledState(NotFulfilledState.FULFILMENTFAILED);
150 fulfillmentInfoService.updateFulfillmentInfo(fulfillmentInfo, expectationId);
153 private String getPredictValue(String name, String value) {
154 String text = "expectationName is cloud leased line Delivery Expectation, " +
155 "firstName is " + name + ",firstValue is " + value;
156 String[] questions = {"expectationName", "Name", "Value"};
157 String bodyStr = "{\"title\": \"predict\", \"text\": \"" + text
158 + "\", \"questions\":" + new JSONArray().toJSONString(Arrays.asList(questions)) + "}";
159 HashMap<String, String> headers = new HashMap<>();
160 String result = HttpUtil.sendPostRequestByJson(PREDICT_URL, headers, bodyStr);
161 if("failed".equals(result)){
164 JSONObject jsonObject = JSON.parseObject(result);
165 return jsonObject.getString("Value");
168 public String getInstanceId() {
169 int random = (int) (Math.random() * 9 + 1);
171 String randomString = String.valueOf(random);
172 int hashCode = UUID.randomUUID().toString().hashCode();
174 hashCode = -hashCode;
176 return randomString + String.format("%015d", hashCode);
180 * update objectInstance of the previous layer’s intent
182 * @param originIntent intent of the previous layer
183 * @param intentGoalBean intent of this layer
185 public void updateIntentOperationInfo(Intent originIntent, IntentGoalBean intentGoalBean){
186 Intent subIntent = intentGoalBean.getIntent();
187 if (StringUtils.containsIgnoreCase(subIntent.getIntentName(),"delivery")) {
188 List<Expectation> deliveryIntentExpectationList = subIntent.getIntentExpectations().stream()
189 .filter(expectation -> ExpectationType.DELIVERY.equals(expectation.getExpectationType()))
190 .collect(Collectors.toList());
191 List<Expectation> originIntentExpectationList = originIntent.getIntentExpectations();
192 List<String> objectInstance = deliveryIntentExpectationList.get(0).getExpectationObject().getObjectInstance();
193 for (Expectation originExpectation : originIntentExpectationList) {
194 ExpectationObject originExpectationObject = originExpectation.getExpectationObject();
195 originExpectationObject.setObjectInstance(objectInstance);
198 log.info("cllDeliveryActuationModule end to update originIntent subIntentInfo");