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