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