dc63ba5c688384f1016558d9b614164075c9dfb6
[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 = new FulfillmentInfo();
93             Expectation intentExpectation = expectationService.getIntentExpectation(expectationId);
94             if (intentExpectation != null) {
95                 FulfillmentInfo expectationFulfillmentInfo = intentExpectation.getExpectationFulfillmentInfo();
96                 if (expectationFulfillmentInfo != null) {
97                     fulfillmentInfo = expectationFulfillmentInfo;
98                 }
99             }
100
101             // Update fulfillmentInfo and write back to DB
102             // Originally set to be NOT_FULFILLED, and will change after requesting the SO operation status
103             fulfillmentInfo.setFulfillmentStatus(FulfillmentStatus.NOT_FULFILLED);
104             fulfillmentInfo.setNotFulfilledReason(resultHeader.getResult_message());
105
106             // If SO request accepted, means intent acknowledged, otherwise, means failed
107             if (resultHeader.getResult_code() == 1) {
108                 fulfillmentInfo.setNotFulfilledState(NotFulfilledState.ACKNOWLEDGED);
109             } else {
110                 fulfillmentInfo.setNotFulfilledState(NotFulfilledState.FULFILMENTFAILED);
111             }
112
113             fulfillmentInfoService.updateFulfillmentInfo(fulfillmentInfo, expectationId);
114
115             ExpectationObject expectationObject = expectationObjectService.getExpectationObject(expectationId);
116             expectationObject.setObjectInstance((String) params.get("name"));
117             intent.getIntentExpectations().get(0).getExpectationObject().setObjectInstance((String) params.get("name"));
118             expectationObjectService.updateExpectationObject(expectationObject, expectationId);
119         } else if (StringUtils.equalsIgnoreCase("delete", intentGoalBean.getIntentGoalType().name())) {
120             String instanceId = intent.getIntentExpectations().get(0).getExpectationObject().getObjectInstance();
121             soService.deleteIntentInstance(instanceId);
122         } else {
123             String instanceId = intent.getIntentExpectations().get(0).getExpectationObject().getObjectInstance();
124             soService.deleteIntentInstance(instanceId);
125             intentService.deleteIntent(intent.getIntentId());
126         }
127     }
128
129     @Override
130     public void interactWithIntentHandle() {
131
132     }
133
134     @Override
135     public void fulfillIntent(IntentGoalBean intentGoalBean, IntentManagementFunction intentHandler) {
136         this.directOperation(intentGoalBean);
137     }
138         
139         public String getInstanceId() {
140         int random = (int) (Math.random() * 9 + 1);
141
142         String randomString = String.valueOf(random);
143         int hashCode = UUID.randomUUID().toString().hashCode();
144         if (hashCode < 0) {
145             hashCode = -hashCode;
146         }
147         return randomString + String.format("%015d", hashCode);
148     }
149
150     public void updateIntentOperationInfo(Intent originIntent, IntentGoalBean intentGoalBean){
151         Intent subIntent = intentGoalBean.getIntent();
152         if (StringUtils.containsIgnoreCase(subIntent.getIntentName(),"delivery")) {
153             List<Expectation> deliveryIntentExpectationList = intentGoalBean.getIntent().getIntentExpectations();
154             List<Expectation> originIntentExpectationList = originIntent.getIntentExpectations();
155             ExpectationObject deliveryExpectationObject = deliveryIntentExpectationList.get(0).getExpectationObject();
156             String objectInstance = deliveryExpectationObject.getObjectInstance();
157
158             for (Expectation originExpectation : originIntentExpectationList) {
159                 ExpectationObject originExpectationObject = originExpectation.getExpectationObject();
160                 originExpectationObject.setObjectInstance(objectInstance);
161             }
162         }
163         log.info("cllDeliveryActuationModule begin to update originIntent subIntentInfo");
164     }
165 }