8e1223b6047779493a0b6c97f6be10325c4b4956
[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.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;
36
37 import java.util.Arrays;
38 import java.util.HashMap;
39 import java.util.List;
40 import java.util.Map;
41 import java.util.UUID;
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 + ":43011";
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     @Autowired
65     private ContextService contextService;
66
67     @Override
68     public void toNextIntentHandler(IntentGoalBean intentGoalBean, IntentManagementFunction IntentHandler) {
69
70     }
71
72     @Override
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);
89                 }
90             }
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);
96
97             // Get the expectationId of the first exception from intent which should be CLL_VPN DELIVERY
98             String expectationId = intent.getIntentExpectations().get(0).getExpectationId();
99
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;
107                 }
108             }
109
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());
114
115             // If SO request accepted, means intent acknowledged, otherwise, means failed
116             if (resultHeader.getResult_code() == 1) {
117                 fulfillmentInfo.setNotFulfilledState(NotFulfilledState.ACKNOWLEDGED);
118             } else {
119                 fulfillmentInfo.setNotFulfilledState(NotFulfilledState.FULFILMENTFAILED);
120             }
121
122             fulfillmentInfoService.updateFulfillmentInfo(fulfillmentInfo, expectationId);
123
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);
131         } else {
132             String instanceId = intent.getIntentExpectations().get(0).getExpectationObject().getObjectInstance();
133             soService.deleteIntentInstance(instanceId);
134             intentService.deleteIntent(intent.getIntentId());
135         }
136     }
137
138     @Override
139     public void interactWithIntentHandle() {
140
141     }
142
143     @Override
144     public void fulfillIntent(IntentGoalBean intentGoalBean, IntentManagementFunction intentHandler) {
145         this.directOperation(intentGoalBean);
146     }
147
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)){
157             return value;
158         }
159         JSONObject jsonObject = JSON.parseObject(result);
160         return jsonObject.getString("Value");
161     }
162         
163         public String getInstanceId() {
164         int random = (int) (Math.random() * 9 + 1);
165
166         String randomString = String.valueOf(random);
167         int hashCode = UUID.randomUUID().toString().hashCode();
168         if (hashCode < 0) {
169             hashCode = -hashCode;
170         }
171         return randomString + String.format("%015d", hashCode);
172     }
173
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();
181
182             for (Expectation originExpectation : originIntentExpectationList) {
183                 ExpectationObject originExpectationObject = originExpectation.getExpectationObject();
184                 originExpectationObject.setObjectInstance(objectInstance);
185             }
186         }
187         log.info("cllDeliveryActuationModule begin to update originIntent subIntentInfo");
188     }
189 }