76ff2d2a2ccf9feccc72c0ba02d9c407e48f8a70
[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.intentBaseService.contextService;
17
18 import lombok.extern.slf4j.Slf4j;
19 import org.apache.commons.collections.CollectionUtils;
20 import org.apache.commons.lang.StringUtils;
21 import org.onap.usecaseui.intentanalysis.bean.enums.OperatorType;
22 import org.onap.usecaseui.intentanalysis.bean.models.Condition;
23 import org.onap.usecaseui.intentanalysis.bean.models.Context;
24 import org.onap.usecaseui.intentanalysis.bean.models.Intent;
25 import org.onap.usecaseui.intentanalysis.intentBaseService.IntentManagementFunction;
26 import org.onap.usecaseui.intentanalysis.service.IntentService;
27 import org.onap.usecaseui.intentanalysis.util.CommonUtil;
28 import org.springframework.beans.factory.annotation.Autowired;
29 import org.springframework.context.ApplicationContext;
30 import org.springframework.stereotype.Service;
31
32 import java.util.ArrayList;
33 import java.util.List;
34 @Slf4j
35 @Service
36 public class IntentContextService {
37
38     @Autowired
39     private IntentService intentService;
40
41     @Autowired
42     ApplicationContext applicationContext;
43
44     public void updateChindIntentContext(Intent originIntent, Intent intent){
45         List<Context> contextList = intent.getIntentContexts();
46         if (CollectionUtils.isEmpty(contextList)) {
47             contextList = new ArrayList<>();
48         }
49         Condition condition1 = new Condition();
50         condition1.setConditionId(CommonUtil.getUUid());
51         condition1.setConditionName(originIntent.getIntentName() + "id");
52         condition1.setOperator(OperatorType.EQUALTO);
53         condition1.setConditionValue(originIntent.getIntentId());
54
55         Context context = new Context();
56         context.setContextName("parentIntent info");
57         context.setContextId(CommonUtil.getUUid());
58         List<Condition> conditionList = new ArrayList<>();
59         conditionList.add(condition1);
60         context.setContextConditions(conditionList);
61         contextList.add(context);
62         intent.setIntentContexts(contextList);
63
64     }
65
66     public void updateParentIntentContext(Intent originIntent, Intent intent){
67         List<Context> contextList = originIntent.getIntentContexts();
68         if (CollectionUtils.isEmpty(contextList)) {
69             contextList = new ArrayList<>();
70         }
71
72         Condition condition1 = new Condition();
73         condition1.setConditionId(CommonUtil.getUUid());
74         condition1.setConditionName(intent.getIntentName() + "id");
75         condition1.setOperator(OperatorType.EQUALTO);
76         condition1.setConditionValue(intent.getIntentId());
77
78         boolean isSubIntentInfoExist = false;
79         for (Context context : contextList) {
80             if (context.getContextName().contains("subIntent info")){
81                 List<Condition> conditionList = context.getContextConditions();
82                 if (CollectionUtils.isEmpty(conditionList)) {
83                     conditionList = new ArrayList<>();
84                 }
85                 conditionList.add(condition1);
86                 context.setContextConditions(conditionList);
87                 isSubIntentInfoExist = true;
88             }
89         }
90
91         if (isSubIntentInfoExist != true){
92             Context context = new Context();
93             context.setContextName("subIntent info");
94             context.setContextId(CommonUtil.getUUid());
95             List<Condition> conditionList = new ArrayList<>();
96             conditionList.add(condition1);
97             context.setContextConditions(conditionList);
98             contextList.add(context);
99         }
100         originIntent.setIntentContexts(contextList);
101     }
102
103     public void updateIntentOwnerHandlerContext(Intent intent, IntentManagementFunction intentOwner, IntentManagementFunction intentHandler){
104         List<Context> contextList = intent.getIntentContexts();
105         if (CollectionUtils.isEmpty(contextList)){
106             contextList = new ArrayList<>();
107         }
108
109         Condition condition1 = new Condition();
110         condition1.setConditionId(CommonUtil.getUUid());
111         condition1.setConditionName("owner class name");
112         condition1.setOperator(OperatorType.EQUALTO);
113         condition1.setConditionValue(intentOwner.getClass().getName());
114
115         Context context = new Context();
116         context.setContextName("owner info");
117         context.setContextId(CommonUtil.getUUid());
118         List<Condition> conditionList = new ArrayList<>();
119         conditionList.add(condition1);
120         context.setContextConditions(conditionList);
121         contextList.add(context);
122
123         Condition condition2 = new Condition();
124         condition2.setConditionId(CommonUtil.getUUid());
125         condition2.setConditionName("handler class name");
126         condition2.setOperator(OperatorType.EQUALTO);
127         condition2.setConditionValue(intentHandler.getClass().getName());
128
129         Context context2 = new Context();
130         context2.setContextName("handler info");
131         context2.setContextId(CommonUtil.getUUid());
132         List<Condition> conditionList2 = new ArrayList<>();
133         conditionList2.add(condition2);
134         context2.setContextConditions(conditionList2);
135         contextList.add(context2);
136
137         intent.setIntentContexts(contextList);
138     }
139
140     public List<Intent> getSubIntentInfoFromContext(Intent originIntent){
141         List<Intent> subIntentList = new ArrayList<>();
142         //form db
143       //  List<Context> contextList = originIntent.getIntentContexts();
144         Intent dbIntent = intentService.getIntent(originIntent.getIntentId());
145         List<Context> contextList = dbIntent.getIntentContexts();
146         for (Context context : contextList) {
147             if (context.getContextName().contains("subIntent info")){
148                 List<Condition> conditionList = context.getContextConditions();
149                 for (Condition condition : conditionList) {
150                     String subIntentId = condition.getConditionValue();
151                     Intent subInent = intentService.getIntent(subIntentId);
152                     subIntentList.add(subInent);
153                 }
154             }
155         }
156         return subIntentList;
157     }
158
159     public IntentManagementFunction getHandlerInfo(Intent intent){
160         List<Context> contextList = intent.getIntentContexts();
161         IntentManagementFunction handler = new IntentManagementFunction();
162
163         for (Context context : contextList) {
164             if (context.getContextName().contains("handler info")) {
165                 List<Condition> conditionList = context.getContextConditions();
166                 String handlerClassName = conditionList.get(0).getConditionValue();
167                 String handleName = StringUtils.substringAfterLast(handlerClassName,".");
168                 handler = (IntentManagementFunction) applicationContext
169                     .getBean(handleName);
170             }
171         }
172         return handler;
173     }
174
175     public void deleteSubIntentContext(Intent intent, String deleteIntentId){
176         List<Context> contextList = intent.getIntentContexts();
177         for (Context context : contextList) {
178             if (context.getContextName().contains("subIntent info")) {
179                 List<Condition> conditionList = context.getContextConditions();
180                 List<Condition> newConditionList = new ArrayList<>();
181                 for (Condition condition : conditionList) {
182                     if (!StringUtils.equals(condition.getConditionValue(), deleteIntentId)){
183                         newConditionList.add(condition);
184                     }
185                 }
186                 context.setContextConditions(newConditionList);
187             }
188         }
189         log.info("deleteSubIntentContext from intent finished");
190     }
191
192 }