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.ContextService;
29 import org.onap.usecaseui.intentanalysis.service.ExpectationObjectService;
30 import org.onap.usecaseui.intentanalysis.service.ExpectationService;
31 import org.onap.usecaseui.intentanalysis.service.FulfillmentInfoService;
32 import org.onap.usecaseui.intentanalysis.service.IntentService;
33 import org.onap.usecaseui.intentanalysis.util.HttpUtil;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.stereotype.Component;
37 import java.util.Arrays;
38 import java.util.HashMap;
39 import java.util.List;
41 import java.util.UUID;
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 + ":43011";
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;
65 private ContextService contextService;
68 public void toNextIntentHandler(IntentGoalBean intentGoalBean, IntentManagementFunction IntentHandler) {
73 public void directOperation(IntentGoalBean intentGoalBean) {
74 Intent intent = intentGoalBean.getIntent();
75 if (StringUtils.equalsIgnoreCase("create", intentGoalBean.getIntentGoalType().name())) {
76 Map<String, Object> params = new HashMap<>();
77 Map<String, String> accessPointOne = new HashMap<>();
78 List<ExpectationTarget> targetList = intent.getIntentExpectations().get(0).getExpectationTargets();
79 for (ExpectationTarget target : targetList) {
80 String conditionName = target.getTargetConditions().get(0).getConditionName();
81 String conditionValue = target.getTargetConditions().get(0).getConditionValue();
82 String value = getPredictValue(conditionName, conditionValue);
83 if (StringUtils.containsIgnoreCase(conditionName, "source")) {
84 accessPointOne.put("name", value);
85 } else if (StringUtils.containsIgnoreCase(conditionName, "destination")) {
86 params.put("cloudPointName", value);
87 } else if (StringUtils.containsIgnoreCase(conditionName, "bandwidth")) {
88 accessPointOne.put("bandwidth", value);
91 params.put("accessPointOne", accessPointOne);
92 params.put("instanceId", getInstanceId());
93 params.put("name", "cll-" + params.get("instanceId"));
94 params.put("protect", false);
95 ResultHeader resultHeader = soService.createIntentInstance(params);
97 // Get the expectationId of the first exception from intent which should be CLL_VPN DELIVERY
98 String expectationId = intent.getIntentExpectations().get(0).getExpectationId();
100 // Get the fulfillmentInfo of the first exception which need to be updated with resultHeader returned
101 FulfillmentInfo fulfillmentInfo = new FulfillmentInfo();
102 Expectation intentExpectation = expectationService.getIntentExpectation(expectationId);
103 if (intentExpectation != null) {
104 FulfillmentInfo expectationFulfillmentInfo = intentExpectation.getExpectationFulfillmentInfo();
105 if (expectationFulfillmentInfo != null) {
106 fulfillmentInfo = expectationFulfillmentInfo;
110 // Update fulfillmentInfo and write back to DB
111 // Originally set to be NOT_FULFILLED, and will change after requesting the SO operation status
112 fulfillmentInfo.setFulfillmentStatus(FulfillmentStatus.NOT_FULFILLED);
113 fulfillmentInfo.setNotFulfilledReason(resultHeader.getResult_message());
115 // If SO request accepted, means intent acknowledged, otherwise, means failed
116 if (resultHeader.getResult_code() == 1) {
117 fulfillmentInfo.setNotFulfilledState(NotFulfilledState.ACKNOWLEDGED);
119 fulfillmentInfo.setNotFulfilledState(NotFulfilledState.FULFILMENTFAILED);
122 fulfillmentInfoService.updateFulfillmentInfo(fulfillmentInfo, expectationId);
124 ExpectationObject expectationObject = expectationObjectService.getExpectationObject(expectationId);
125 expectationObject.setObjectInstance((String) params.get("name"));
126 intent.getIntentExpectations().get(0).getExpectationObject().setObjectInstance((String) params.get("name"));
127 expectationObjectService.updateExpectationObject(expectationObject, expectationId);
128 } else if (StringUtils.equalsIgnoreCase("delete", intentGoalBean.getIntentGoalType().name())) {
129 String instanceId = intent.getIntentExpectations().get(0).getExpectationObject().getObjectInstance();
130 soService.deleteIntentInstance(instanceId);
132 String instanceId = intent.getIntentExpectations().get(0).getExpectationObject().getObjectInstance();
133 soService.deleteIntentInstance(instanceId);
134 intentService.deleteIntent(intent.getIntentId());
139 public void interactWithIntentHandle() {
144 public void fulfillIntent(IntentGoalBean intentGoalBean, IntentManagementFunction intentHandler) {
145 this.directOperation(intentGoalBean);
148 private String getPredictValue(String name, String value) {
149 String text = "expectationName is cloud leased line Delivery Expectation, " +
150 "firstName is " + name + ",firstValue is " + value;
151 String[] questions = {"expectationName", "Name", "Value"};
152 String bodyStr = "{\"title\": \"predict\", \"text\": \"" + text
153 + "\", \"questions\":" + new JSONArray().toJSONString(Arrays.asList(questions)) + "}";
154 HashMap<String, String> headers = new HashMap<>();
155 String result = HttpUtil.sendPostRequestByJson(PREDICT_URL, headers, bodyStr);
156 if("failed".equals(result)){
159 JSONObject jsonObject = JSON.parseObject(result);
160 return jsonObject.getString("Value");
163 public String getInstanceId() {
164 int random = (int) (Math.random() * 9 + 1);
166 String randomString = String.valueOf(random);
167 int hashCode = UUID.randomUUID().toString().hashCode();
169 hashCode = -hashCode;
171 return randomString + String.format("%015d", hashCode);
174 public void updateIntentOperationInfo(Intent originIntent, IntentGoalBean intentGoalBean){
175 Intent subIntent = intentGoalBean.getIntent();
176 if (StringUtils.containsIgnoreCase(subIntent.getIntentName(),"delivery")) {
177 List<Expectation> deliveryIntentExpectationList = intentGoalBean.getIntent().getIntentExpectations();
178 List<Expectation> originIntentExpectationList = originIntent.getIntentExpectations();
179 ExpectationObject deliveryExpectationObject = deliveryIntentExpectationList.get(0).getExpectationObject();
180 String objectInstance = deliveryExpectationObject.getObjectInstance();
182 for (Expectation originExpectation : originIntentExpectationList) {
183 ExpectationObject originExpectationObject = originExpectation.getExpectationObject();
184 originExpectationObject.setObjectInstance(objectInstance);
187 log.info("cllDeliveryActuationModule begin to update originIntent subIntentInfo");