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.HashMap;
38 import java.util.List;
40 import java.util.UUID;
41 import java.util.stream.Collectors;
45 public class CLLDeliveryActuationModule extends ActuationModule {
46 public final static String NLP_HOST = "http://uui-nlp";
47 public final static String NLP_ONLINE_URL_BASE = NLP_HOST + ":33011";
48 public final static String PREDICT_URL = NLP_ONLINE_URL_BASE + "/api/online/predict";
51 private SOService soService;
54 private ExpectationObjectService expectationObjectService;
57 private ExpectationService expectationService;
60 private FulfillmentInfoService fulfillmentInfoService;
63 private IntentService intentService;
66 public void toNextIntentHandler(IntentGoalBean intentGoalBean, IntentManagementFunction IntentHandler) {
71 public void directOperation(IntentGoalBean intentGoalBean) {
72 Intent intent = intentGoalBean.getIntent();
73 Expectation deliveryExpectation = intent.getIntentExpectations().stream()
74 .filter(expectation -> !ExpectationType.REPORT.equals(expectation.getExpectationType()))
75 .collect(Collectors.toList()).get(0);
76 if (StringUtils.equalsIgnoreCase("create", intentGoalBean.getIntentGoalType().name())) {
77 Map<String, Object> params = new HashMap<>();
78 Map<String, String> accessPointOne = new HashMap<>();
79 List<ExpectationTarget> targetList = deliveryExpectation.getExpectationTargets();
80 for (ExpectationTarget target : targetList) {
81 String conditionName = target.getTargetConditions().get(0).getConditionName();
82 String conditionValue = target.getTargetConditions().get(0).getConditionValue();
83 String value = getPredictValue(conditionName, conditionValue);
84 if (StringUtils.containsIgnoreCase(conditionName, "source")) {
85 accessPointOne.put("name", value);
86 } else if (StringUtils.containsIgnoreCase(conditionName, "destination")) {
87 params.put("cloudPointName", value);
88 } else if (StringUtils.containsIgnoreCase(conditionName, "bandwidth")) {
89 accessPointOne.put("bandwidth", value);
92 params.put("accessPointOne", accessPointOne);
93 params.put("instanceId", getInstanceId());
94 params.put("name", "cll-" + params.get("instanceId"));
95 params.put("protect", false);
97 updateFulfillment(params, deliveryExpectation.getExpectationId());
99 // fill and update the objectInstance of intent expectation(include delivery and report)
100 String objectInstance = (String) params.get("name");
101 intent.getIntentExpectations().forEach(expectation -> {
102 ExpectationObject expectationObject = expectationObjectService.getExpectationObject(expectation.getExpectationId());
103 expectationObject.setObjectInstance(objectInstance);
104 expectation.getExpectationObject().setObjectInstance(objectInstance);
105 expectationObjectService.updateExpectationObject(expectationObject, expectation.getExpectationId());
107 } else if (StringUtils.equalsIgnoreCase("delete", intentGoalBean.getIntentGoalType().name())) {
108 String instanceId = deliveryExpectation.getExpectationObject().getObjectInstance();
109 soService.deleteIntentInstance(instanceId);
111 String instanceId = deliveryExpectation.getExpectationObject().getObjectInstance();
112 soService.deleteIntentInstance(instanceId);
113 intentService.deleteIntent(intent.getIntentId());
118 public void interactWithIntentHandle() {
123 public void fulfillIntent(IntentGoalBean intentGoalBean, IntentManagementFunction intentHandler) {
124 this.directOperation(intentGoalBean);
127 private void updateFulfillment(Map<String, Object> params,String expectationId){
128 // Get the fulfillmentInfo of the first exception which need to be updated with resultHeader returned
129 FulfillmentInfo fulfillmentInfo = new FulfillmentInfo();
130 Expectation intentExpectation = expectationService.getIntentExpectation(expectationId);
131 if (intentExpectation != null) {
132 FulfillmentInfo expectationFulfillmentInfo = intentExpectation.getExpectationFulfillmentInfo();
133 if (expectationFulfillmentInfo != null) {
134 fulfillmentInfo = expectationFulfillmentInfo;
137 ResultHeader resultHeader = soService.createIntentInstance(params);
138 // Update fulfillmentInfo and write back to DB
139 // Originally set to be NOT_FULFILLED, and will change after requesting the SO operation status
140 fulfillmentInfo.setFulfillmentStatus(FulfillmentStatus.NOT_FULFILLED);
141 fulfillmentInfo.setNotFulfilledReason(resultHeader.getResult_message());
143 // If SO request accepted, means intent acknowledged, otherwise, means failed
144 if (resultHeader.getResult_code() == 1) {
145 fulfillmentInfo.setNotFulfilledState(NotFulfilledState.ACKNOWLEDGED);
147 fulfillmentInfo.setNotFulfilledState(NotFulfilledState.FULFILMENTFAILED);
149 fulfillmentInfoService.updateFulfillmentInfo(fulfillmentInfo, expectationId);
152 private String getPredictValue(String name, String value) {
153 String text = "expectationName is cloud leased line Delivery Expectation, " +
154 "firstName is " + name + ",firstValue is " + value;
155 String[] questions = {"expectationName", "Name", "Value"};
156 String bodyStr = "{\"title\": \"predict\", \"text\": \"" + text
157 + "\", \"questions\":" + new JSONArray().toJSONString(Arrays.asList(questions)) + "}";
158 HashMap<String, String> headers = new HashMap<>();
159 String result = HttpUtil.sendPostRequestByJson(PREDICT_URL, headers, bodyStr);
160 if("failed".equals(result)){
163 JSONObject jsonObject = JSON.parseObject(result);
164 return jsonObject.getString("Value");
167 public String getInstanceId() {
168 int random = (int) (Math.random() * 9 + 1);
170 String randomString = String.valueOf(random);
171 int hashCode = UUID.randomUUID().toString().hashCode();
173 hashCode = -hashCode;
175 return randomString + String.format("%015d", hashCode);
179 * update objectInstance of the previous layer’s intent
181 * @param originIntent intent of the previous layer
182 * @param intentGoalBean intent of this layer
184 public void updateIntentOperationInfo(Intent originIntent, IntentGoalBean intentGoalBean){
185 Intent subIntent = intentGoalBean.getIntent();
186 if (StringUtils.containsIgnoreCase(subIntent.getIntentName(),"delivery")) {
187 List<Expectation> deliveryIntentExpectationList = subIntent.getIntentExpectations().stream()
188 .filter(expectation -> ExpectationType.DELIVERY.equals(expectation.getExpectationType()))
189 .collect(Collectors.toList());
190 List<Expectation> originIntentExpectationList = originIntent.getIntentExpectations();
191 String objectInstance = deliveryIntentExpectationList.get(0).getExpectationObject().getObjectInstance();
192 for (Expectation originExpectation : originIntentExpectationList) {
193 ExpectationObject originExpectationObject = originExpectation.getExpectationObject();
194 originExpectationObject.setObjectInstance(objectInstance);
197 log.info("cllDeliveryActuationModule end to update originIntent subIntentInfo");