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