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