1ea7f9dac1bd0e5a90ece614eabfaf76a8b4ca5b
[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.intentBaseService.IntentManagementFunction;
23 import org.onap.usecaseui.intentanalysis.intentBaseService.intentModule.ActuationModule;
24 import org.onap.usecaseui.intentanalysis.service.ContextService;
25 import org.onap.usecaseui.intentanalysis.service.ExpectationObjectService;
26 import org.onap.usecaseui.intentanalysis.service.IntentService;
27 import org.springframework.beans.factory.annotation.Autowired;
28 import org.springframework.stereotype.Component;
29
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.UUID;
34
35 @Component
36 @Slf4j
37 public class CLLDeliveryActuationModule extends ActuationModule {
38
39     @Autowired
40     private SOService soService;
41
42     @Autowired
43     private ExpectationObjectService expectationObjectService;
44
45     @Autowired
46     private IntentService intentService;
47     @Autowired
48     private ContextService contextService;
49
50     @Override
51     public void toNextIntentHandler(IntentGoalBean intentGoalBean, IntentManagementFunction IntentHandler) {
52
53     }
54
55     @Override
56     public void directOperation(IntentGoalBean intentGoalBean) {
57         Intent intent = intentGoalBean.getIntent();
58         if (StringUtils.equalsIgnoreCase("create", intentGoalBean.getIntentGoalType().name())) {
59             Map<String, Object> params = new HashMap<>();
60             Map<String, String> accessPointOne = new HashMap<>();
61             List<ExpectationTarget> targetList = intent.getIntentExpectations().get(0).getExpectationTargets();
62             for (ExpectationTarget target : targetList) {
63                 String conditionName = target.getTargetConditions().get(0).getConditionName();
64                 String conditionValue = target.getTargetConditions().get(0).getConditionValue();
65                 if (StringUtils.containsIgnoreCase(conditionName, "source")) {
66                     accessPointOne.put("name", conditionValue);
67                 } else if (StringUtils.containsIgnoreCase(conditionName, "destination")) {
68                     params.put("cloudPointName", conditionValue);
69                 } else if (StringUtils.containsIgnoreCase(conditionName, "bandwidth")) {
70                     accessPointOne.put("bandwidth", conditionValue);
71                 }
72             }
73             params.put("accessPointOne", accessPointOne);
74             params.put("instanceId", getInstanceId());
75             params.put("name", "cll-" + params.get("instanceId"));
76             params.put("protect", false);
77             soService.createIntentInstance(params);
78             String expectationId = intent.getIntentExpectations().get(0).getExpectationId();
79             ExpectationObject expectationObject = expectationObjectService.getExpectationObject(expectationId);
80             expectationObject.setObjectInstance((String) params.get("name"));
81             expectationObjectService.updateExpectationObject(expectationObject, expectationId);
82         } else {
83             String instanceId = intent.getIntentExpectations().get(0).getExpectationObject().getObjectInstance();
84             soService.deleteIntentInstance(instanceId);
85             intentService.deleteIntent(intent.getIntentId());
86         }
87     }
88
89     @Override
90     public void interactWithIntentHandle() {
91
92     }
93
94     @Override
95     public void fulfillIntent(IntentGoalBean intentGoalBean, IntentManagementFunction intentHandler) {
96         this.directOperation(intentGoalBean);
97     }
98         
99         public String getInstanceId() {
100         int random = (int) (Math.random() * 9 + 1);
101
102         String randomString = String.valueOf(random);
103         int hashCode = UUID.randomUUID().toString().hashCode();
104         if (hashCode < 0) {
105             hashCode = -hashCode;
106         }
107         return randomString + String.format("%015d", hashCode);
108     }
109
110     public void updateIntentOperationInfo(Intent originIntent, IntentGoalBean intentGoalBean){
111         Intent subIntent = intentGoalBean.getIntent();
112           if (StringUtils.containsIgnoreCase(subIntent.getIntentName(),"delivery")) {
113             List<Expectation> deliveryIntentExpectationList = intentGoalBean.getIntent().getIntentExpectations();
114             List<Expectation> originIntentExpectationList = originIntent.getIntentExpectations();
115             ExpectationObject deliveryExpectationObject = deliveryIntentExpectationList.get(0).getExpectationObject();
116             String objectInstance = deliveryExpectationObject.getObjectInstance();
117
118             for (Expectation originExpectation : originIntentExpectationList) {
119                 ExpectationObject originExpectationObject = originExpectation.getExpectationObject();
120                 originExpectationObject.setObjectInstance(objectInstance);
121             }
122         }
123        log.info("cllDeliveryActuationModule begin to update originIntent subIntentInfo");
124     }
125 }