b64dd9aadc321b1e759d47b1241640634be49299
[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 lombok.extern.slf4j.Slf4j;
19 import org.apache.commons.lang.StringUtils;
20 import org.onap.usecaseui.intentanalysis.adapters.so.SOService;
21 import org.onap.usecaseui.intentanalysis.bean.models.*;
22 import org.onap.usecaseui.intentanalysis.bean.enums.*;
23 import org.onap.usecaseui.intentanalysis.intentBaseService.IntentManagementFunction;
24 import org.onap.usecaseui.intentanalysis.intentBaseService.intentModule.ActuationModule;
25 import org.onap.usecaseui.intentanalysis.service.ContextService;
26 import org.onap.usecaseui.intentanalysis.service.ExpectationObjectService;
27 import org.onap.usecaseui.intentanalysis.service.ExpectationService;
28 import org.onap.usecaseui.intentanalysis.service.FulfillmentInfoService;
29 import org.onap.usecaseui.intentanalysis.service.IntentService;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.stereotype.Component;
32
33 import java.util.HashMap;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.UUID;
37
38 @Component
39 @Slf4j
40 public class CLLDeliveryActuationModule extends ActuationModule {
41
42     @Autowired
43     private SOService soService;
44
45     @Autowired
46     private ExpectationObjectService expectationObjectService;
47
48     @Autowired
49     private ExpectationService expectationService;
50
51     @Autowired
52     private FulfillmentInfoService fulfillmentInfoService;
53
54     @Autowired
55     private IntentService intentService;
56     @Autowired
57     private ContextService contextService;
58
59     @Override
60     public void toNextIntentHandler(IntentGoalBean intentGoalBean, IntentManagementFunction IntentHandler) {
61
62     }
63
64     @Override
65     public void directOperation(IntentGoalBean intentGoalBean) {
66         Intent intent = intentGoalBean.getIntent();
67         if (StringUtils.equalsIgnoreCase("create", intentGoalBean.getIntentGoalType().name())) {
68             Map<String, Object> params = new HashMap<>();
69             Map<String, String> accessPointOne = new HashMap<>();
70             List<ExpectationTarget> targetList = intent.getIntentExpectations().get(0).getExpectationTargets();
71             for (ExpectationTarget target : targetList) {
72                 String conditionName = target.getTargetConditions().get(0).getConditionName();
73                 String conditionValue = target.getTargetConditions().get(0).getConditionValue();
74                 if (StringUtils.containsIgnoreCase(conditionName, "source")) {
75                     accessPointOne.put("name", conditionValue);
76                 } else if (StringUtils.containsIgnoreCase(conditionName, "destination")) {
77                     params.put("cloudPointName", conditionValue);
78                 } else if (StringUtils.containsIgnoreCase(conditionName, "bandwidth")) {
79                     accessPointOne.put("bandwidth", conditionValue);
80                 }
81             }
82             params.put("accessPointOne", accessPointOne);
83             params.put("instanceId", getInstanceId());
84             params.put("name", "cll-" + params.get("instanceId"));
85             params.put("protect", false);
86             ResultHeader resultHeader = soService.createIntentInstance(params);
87
88             // Get the expectationId of the first exception from intent which should be CLL_VPN DELIVERY
89             String expectationId = intent.getIntentExpectations().get(0).getExpectationId();
90
91             // Get the fulfillmentInfo of the first exception which need to be updated with resultHeader returned
92             FulfillmentInfo fulfillmentInfo = expectationService.getIntentExpectation(expectationId).getExpectationFulfillmentInfo();
93
94             if (fulfillmentInfo == null) {
95                 fulfillmentInfo = new FulfillmentInfo();
96             }
97
98             // Update fulfillmentInfo and write back to DB
99             // Originally set to be NOT_FULFILLED, and will change after requesting the SO operation status
100             fulfillmentInfo.setFulfillmentStatus(FulfillmentStatus.NOT_FULFILLED);
101             fulfillmentInfo.setNotFulfilledReason(resultHeader.getResult_message());
102
103             // If SO request accepted, means intent acknowledged, otherwise, means failed
104             if (resultHeader.getResult_code() == 1) {
105                 fulfillmentInfo.setNotFulfilledState(NotFulfilledState.ACKNOWLEDGED);
106             } else {
107                 fulfillmentInfo.setNotFulfilledState(NotFulfilledState.FULFILMENTFAILED);
108             }
109
110             fulfillmentInfoService.updateFulfillmentInfo(fulfillmentInfo, expectationId);
111
112             ExpectationObject expectationObject = expectationObjectService.getExpectationObject(expectationId);
113             expectationObject.setObjectInstance((String) params.get("name"));
114             expectationObjectService.updateExpectationObject(expectationObject, expectationId);
115         } else {
116             String instanceId = intent.getIntentExpectations().get(0).getExpectationObject().getObjectInstance();
117             soService.deleteIntentInstance(instanceId);
118             intentService.deleteIntent(intent.getIntentId());
119         }
120     }
121
122     @Override
123     public void interactWithIntentHandle() {
124
125     }
126
127     @Override
128     public void fulfillIntent(IntentGoalBean intentGoalBean, IntentManagementFunction intentHandler) {
129         this.directOperation(intentGoalBean);
130     }
131         
132         public String getInstanceId() {
133         int random = (int) (Math.random() * 9 + 1);
134
135         String randomString = String.valueOf(random);
136         int hashCode = UUID.randomUUID().toString().hashCode();
137         if (hashCode < 0) {
138             hashCode = -hashCode;
139         }
140         return randomString + String.format("%015d", hashCode);
141     }
142
143     public void updateIntentOperationInfo(Intent originIntent, IntentGoalBean intentGoalBean){
144         Intent subIntent = intentGoalBean.getIntent();
145         if (StringUtils.containsIgnoreCase(subIntent.getIntentName(),"delivery")) {
146             List<Expectation> deliveryIntentExpectationList = intentGoalBean.getIntent().getIntentExpectations();
147             List<Expectation> originIntentExpectationList = originIntent.getIntentExpectations();
148             ExpectationObject deliveryExpectationObject = deliveryIntentExpectationList.get(0).getExpectationObject();
149             String objectInstance = deliveryExpectationObject.getObjectInstance();
150
151             for (Expectation originExpectation : originIntentExpectationList) {
152                 ExpectationObject originExpectationObject = originExpectation.getExpectationObject();
153                 originExpectationObject.setObjectInstance(objectInstance);
154             }
155         }
156         log.info("cllDeliveryActuationModule begin to update originIntent subIntentInfo");
157     }
158 }