a8beef37b4191f2fbada44b9fb51af42cc6c045d
[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.intentModule;
17
18
19 import org.apache.commons.collections.CollectionUtils;
20 import org.onap.usecaseui.intentanalysis.bean.models.*;
21 import org.onap.usecaseui.intentanalysis.intentBaseService.IntentManagementFunction;
22 import org.onap.usecaseui.intentanalysis.util.CommonUtil;
23
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.LinkedHashMap;
27 import java.util.List;
28
29 public abstract class DecisionModule {
30     public abstract void determineUltimateGoal();
31
32     // find intentManageFunction
33     public abstract IntentManagementFunction exploreIntentHandlers(IntentGoalBean intentGoalBean);
34
35     public Intent intentDefinition(Intent originIntent, Intent intent) {
36         intent.setIntentId(CommonUtil.getUUid());
37         List<Expectation> originalExpectationList = intent.getIntentExpectations();
38         List<Expectation> newExpectationList = getNewExpectationList(originalExpectationList);
39         intent.setIntentExpectations(newExpectationList);
40         updateIntentWithOriginIntent(originIntent, intent);
41         return intent;
42     }
43
44     public abstract void updateIntentWithOriginIntent(Intent originIntent, Intent intent);
45
46     public abstract void decideSuitableAction();
47
48     public abstract void interactWithTemplateDb();
49
50     public abstract void updateIntentInfo(Intent originIntent, IntentGoalBean intentGoalBean);
51
52
53     public abstract LinkedHashMap<IntentGoalBean, IntentManagementFunction> findHandler(IntentGoalBean intentGoalBean);
54
55     /**
56      * build new Intent with uuid
57      *
58      * @param originalExpectationList
59      * @return
60      */
61     public List<Expectation> getNewExpectationList(List<Expectation> originalExpectationList) {
62         if (CollectionUtils.isEmpty(originalExpectationList)) {
63             return Collections.emptyList();
64         }
65         List<Expectation> newExpectations = new ArrayList<>();
66         for (Expectation expectation : originalExpectationList) {
67             expectation.setExpectationId(CommonUtil.getUUid());
68             //ExpectationObject
69             ExpectationObject expectationObject = expectation.getExpectationObject();
70             ExpectationObject newExpectationObject = getNewExpectationObject(expectationObject);
71             expectation.setExpectationObject(newExpectationObject);
72             //ExpectationTarget
73             List<ExpectationTarget> expectationTargets = expectation.getExpectationTargets();
74             if (CollectionUtils.isNotEmpty(expectationTargets)) {
75                 for (ExpectationTarget expectationTarget : expectationTargets) {
76                     expectationTarget.setTargetId(CommonUtil.getUUid());
77                     //targetContexts
78                     List<Context> targetContexts = expectationTarget.getTargetContexts();
79                     if (CollectionUtils.isNotEmpty(targetContexts)) {
80                         List<Context> newTargetContexts = new ArrayList<>();
81                         for (Context context : targetContexts) {
82                             Context newContext = getNewContext(context);
83                             newTargetContexts.add(newContext);
84                         }
85                         expectationTarget.setTargetContexts(newTargetContexts);
86                     }
87                     //targetConditions
88                     List<Condition> targetConditions = expectationTarget.getTargetConditions();
89                     if (CollectionUtils.isNotEmpty(targetConditions)) {
90                         List<Condition> newTargetConditions = new ArrayList<>();
91                         for (Condition condition : targetConditions) {
92                             Condition newCondition = getNewCondition(condition);
93                             newTargetConditions.add(newCondition);
94                         }
95                         expectationTarget.setTargetConditions(newTargetConditions);
96                     }
97                 }
98             }
99             //expectationContexts
100             List<Context> expectationContexts = expectation.getExpectationContexts();
101             if (CollectionUtils.isNotEmpty(expectationContexts)) {
102                 List<Context> newEexpectationContexts = new ArrayList<>();
103                 for (Context context : expectationContexts) {
104                     Context newContext = getNewContext(context);
105                     newEexpectationContexts.add(newContext);
106                 }
107                 expectation.setExpectationContexts(newEexpectationContexts);
108             }
109             newExpectations.add(expectation);
110         }
111         return newExpectations;
112     }
113
114     public ExpectationObject getNewExpectationObject(ExpectationObject expectationObject) {
115         if (null != expectationObject) {
116             List<Context> objectContexts = expectationObject.getObjectContexts();
117             if (CollectionUtils.isNotEmpty(objectContexts)) {
118                 List<Context> newObjectContexts = new ArrayList<>();
119                 for (Context context : objectContexts) {
120                     Context newContext = getNewContext(context);
121                     newObjectContexts.add(newContext);
122                 }
123                 expectationObject.setObjectContexts(newObjectContexts);
124                 return expectationObject;
125             }
126         }
127         return expectationObject;
128     }
129
130     public Condition getNewCondition(Condition condition) {
131         condition.setConditionId(CommonUtil.getUUid());
132         List<Condition> conditionList = condition.getConditionList();
133         if (CollectionUtils.isEmpty(conditionList)) {
134             return condition;
135         } else {
136             for (Condition subCondition : conditionList) {
137                 getNewCondition(subCondition);
138             }
139         }
140         return condition;
141     }
142
143     public Context getNewContext(Context context) {
144         context.setContextId(CommonUtil.getUUid());
145         List<Condition> contextConditions = context.getContextConditions();
146         if (CollectionUtils.isNotEmpty(contextConditions)) {
147             List<Condition> newConditionList = new ArrayList<>();
148             for (Condition condition : contextConditions) {
149                 newConditionList.add(getNewCondition(condition));
150             }
151             context.setContextConditions(newConditionList);
152         }
153         return context;
154     }
155 }