33d05f9e3af6b089df1721f1d2eac8a1e025df66
[usecase-ui/intent-analysis.git] /
1 /*
2  * Copyright (C) 2022 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.clldeliveryIntentmgt.clldeliverymodule;
17
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;
35
36 import java.util.Arrays;
37 import java.util.HashMap;
38 import java.util.List;
39 import java.util.Map;
40 import java.util.UUID;
41 import java.util.stream.Collectors;
42
43 @Component
44 @Slf4j
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";
49
50     @Autowired
51     private SOService soService;
52
53     @Autowired
54     private ExpectationObjectService expectationObjectService;
55
56     @Autowired
57     private ExpectationService expectationService;
58
59     @Autowired
60     private FulfillmentInfoService fulfillmentInfoService;
61
62     @Autowired
63     private IntentService intentService;
64
65     @Override
66     public void toNextIntentHandler(IntentGoalBean intentGoalBean, IntentManagementFunction IntentHandler) {
67
68     }
69
70     @Override
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);
90                 }
91             }
92             params.put("accessPointOne", accessPointOne);
93             params.put("instanceId", getInstanceId());
94             params.put("name", "cll-" + params.get("instanceId"));
95             params.put("protect", false);
96
97             updateFulfillment(params, deliveryExpectation.getExpectationId());
98
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());
106             });
107         } else if (StringUtils.equalsIgnoreCase("delete", intentGoalBean.getIntentGoalType().name())) {
108             String instanceId = deliveryExpectation.getExpectationObject().getObjectInstance();
109             soService.deleteIntentInstance(instanceId);
110         } else {
111             String instanceId = deliveryExpectation.getExpectationObject().getObjectInstance();
112             soService.deleteIntentInstance(instanceId);
113             intentService.deleteIntent(intent.getIntentId());
114         }
115     }
116
117     @Override
118     public void interactWithIntentHandle() {
119
120     }
121
122     @Override
123     public void fulfillIntent(IntentGoalBean intentGoalBean, IntentManagementFunction intentHandler) {
124         this.directOperation(intentGoalBean);
125     }
126
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;
135             }
136         }
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());
142
143         // If SO request accepted, means intent acknowledged, otherwise, means failed
144         if (resultHeader.getResult_code() == 1) {
145             fulfillmentInfo.setNotFulfilledState(NotFulfilledState.ACKNOWLEDGED);
146         } else {
147             fulfillmentInfo.setNotFulfilledState(NotFulfilledState.FULFILMENTFAILED);
148         }
149         fulfillmentInfoService.updateFulfillmentInfo(fulfillmentInfo, expectationId);
150     }
151
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)){
161             return value;
162         }
163         JSONObject jsonObject = JSON.parseObject(result);
164         return jsonObject.getString("Value");
165     }
166         
167         public String getInstanceId() {
168         int random = (int) (Math.random() * 9 + 1);
169
170         String randomString = String.valueOf(random);
171         int hashCode = UUID.randomUUID().toString().hashCode();
172         if (hashCode < 0) {
173             hashCode = -hashCode;
174         }
175         return randomString + String.format("%015d", hashCode);
176     }
177
178     /**
179      * update objectInstance of the previous layer’s intent
180      *
181      * @param originIntent intent of the previous layer
182      * @param intentGoalBean intent of this layer
183      */
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);
195             }
196         }
197         log.info("cllDeliveryActuationModule end to update originIntent subIntentInfo");
198     }
199 }