7a22226a9a15403ec2879635d2667f9944b07e8c
[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.cllassuranceIntentmgt.cllassurancemodule;
17
18 import lombok.extern.slf4j.Slf4j;
19 import org.apache.commons.lang.StringUtils;
20 import org.onap.usecaseui.intentanalysis.adapters.policy.PolicyService;
21 import org.onap.usecaseui.intentanalysis.bean.enums.IntentGoalType;
22 import org.onap.usecaseui.intentanalysis.bean.models.*;
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.IntentService;
27 import org.springframework.beans.factory.annotation.Autowired;
28 import org.springframework.stereotype.Component;
29 import org.springframework.util.CollectionUtils;
30
31 import java.util.List;
32 import java.util.stream.Collectors;
33 @Slf4j
34 @Component
35 public class CLLAssuranceActuationModule extends ActuationModule {
36     @Autowired
37     private IntentService intentService;
38
39     @Autowired
40     private PolicyService policyService;
41     @Autowired
42     private ContextService contextService;
43
44     @Override
45     public void toNextIntentHandler(IntentGoalBean intentGoalBean, IntentManagementFunction IntentHandler) {
46
47     }
48
49     @Override
50     public void directOperation(IntentGoalBean intentGoalBean) {
51         Intent intent = intentGoalBean.getIntent();
52         String cllId = getCLLId(intent);
53         String bandwidth = getBandwidth(cllId);
54         IntentGoalType intentGoalType = intentGoalBean.getIntentGoalType();
55         if (StringUtils.equalsIgnoreCase("create", intentGoalType.name())) {
56             policyService.updateIntentConfigPolicy(cllId, bandwidth, "true");
57         } else if (StringUtils.equalsIgnoreCase("update", intentGoalType.name())) {
58             policyService.updateIntentConfigPolicy(cllId, bandwidth, "false");
59         } else if (StringUtils.equalsIgnoreCase("delete", intentGoalType.name())) {
60             policyService.updateIntentConfigPolicy(cllId, bandwidth, "false");
61         }
62     }
63
64     @Override
65     public void interactWithIntentHandle() {
66
67     }
68
69     @Override
70     public void fulfillIntent(IntentGoalBean intentGoalBean, IntentManagementFunction intentHandler) {
71         directOperation(intentGoalBean);
72     }
73
74     @Override
75     public void updateIntentOperationInfo(Intent originIntent, IntentGoalBean intentGoalBean){
76     }
77
78     private String getBandwidth(String cllId) {
79         List<Intent> deliveryIntentList = intentService.getIntentByName("CLL Delivery Intent");
80         if (CollectionUtils.isEmpty(deliveryIntentList)) {
81             log.info("get CLL Delivery Intent from the database is empty");
82             return null;
83         }
84         for (Intent deliveryIntent : deliveryIntentList) {
85             List<Expectation> deliveryExpectationList = deliveryIntent.getIntentExpectations();
86             if (CollectionUtils.isEmpty(deliveryExpectationList)) {
87                 log.info("expectation is empty,intentId is {}", deliveryIntent.getIntentId());
88                 continue;
89             }
90             for (Expectation deliveryExpectation : deliveryExpectationList) {
91                 ExpectationObject expectationObject = deliveryExpectation.getExpectationObject();
92                 if (expectationObject == null) {
93                     log.info("expectationObject is empty,expectationId is {}", deliveryExpectation.getExpectationId());
94                     continue;
95                 }
96                 String objectInstance = expectationObject.getObjectInstance();
97                 if (!StringUtils.equalsIgnoreCase(cllId, objectInstance)) {
98                     log.info("cllId and objectInstance are not equal,cllId is {},objectInstance is {}", cllId, objectInstance);
99                     continue;
100                 }
101                 List<ExpectationTarget> deliveryTargetList = deliveryExpectation.getExpectationTargets();
102                 List<ExpectationTarget> bandwidth = deliveryTargetList.stream()
103                         .filter(target -> StringUtils.equalsIgnoreCase("bandwidth", target.getTargetName()))
104                         .collect(Collectors.toList());
105                 if (CollectionUtils.isEmpty(bandwidth)) {
106                     log.info("bandwidth target is empty,expectation is {}", deliveryExpectation.getExpectationId());
107                     continue;
108                 }
109                 List<Condition> deliveryConditionList = bandwidth.get(0).getTargetConditions();
110                 List<Condition> collect = deliveryConditionList.stream()
111                         .filter(condition -> StringUtils.equalsIgnoreCase("condition of the cll service bandwidth", condition.getConditionName()))
112                         .collect(Collectors.toList());
113                 if (CollectionUtils.isEmpty(collect)) {
114                     log.info("condition of the cll service bandwidth is empty,targetId is {}", bandwidth.get(0).getTargetId());
115                     continue;
116                 }
117                 return collect.get(0).getConditionValue();
118             }
119         }
120         return null;
121     }
122
123     public String getCLLId(Intent intent) {
124         List<Expectation> expectationList = intent.getIntentExpectations();
125         for (Expectation expectation : expectationList) {
126             if (StringUtils.equalsIgnoreCase("assurance", expectation.getExpectationType().name())) {
127                 return expectation.getExpectationObject().getObjectInstance();
128             }
129         }
130         return null;
131     }
132 }