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