f225d60c980f44702f82481ac6895161a08b3806
[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 import org.apache.commons.collections.CollectionUtils;
19 import org.apache.commons.lang.StringUtils;
20 import org.onap.usecaseui.intentanalysis.bean.models.*;
21 import org.onap.usecaseui.intentanalysis.service.IntentService;
22 import org.springframework.beans.factory.annotation.Autowired;
23
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.stream.Collectors;
27
28 public abstract class KnowledgeModule {
29     @Autowired
30     private IntentService intentService;
31
32     //Parse, decompose, orchestrate the original intent
33     public abstract IntentGoalBean intentCognition(Intent intent);
34
35     // in distribution, ask permission from imf
36     public abstract boolean recieveCreateIntent();
37
38     public abstract boolean recieveUpdateIntent();
39
40     public abstract boolean recieveDeleteIntent();
41
42     public List<String> intentResolution(Intent intent) {
43         //db contain original intent
44         List<Intent> sameNameIntentList = intentService.getIntentByName(intent.getIntentName());
45         List<String> intentIdList = new ArrayList<>();
46         if (CollectionUtils.isNotEmpty(sameNameIntentList)) {
47             //remove context.condition  ownerName = formatIntentInputManagementFunction
48             List<Intent> filterIntentList = filterIntent(sameNameIntentList);
49             List<Expectation> expectationList = intent.getIntentExpectations();
50             for (Intent dbIntent : filterIntentList) {
51                 String intentId = dbIntent.getIntentId();
52                 int count = 0;
53                 for (Expectation expectation : expectationList) { //original expectations
54                     //Determine if there is the same ObjectType
55                     List<Expectation> sameObjTypeList = dbIntent.getIntentExpectations().stream()
56                             .filter(x -> x.getExpectationObject().getObjectType().equals(expectation.getExpectationObject().getObjectType()))
57                             .collect(Collectors.toList());
58                     if (CollectionUtils.isNotEmpty(sameObjTypeList)) {
59                         //Determine the targetName of the Expectation which hava same ObjectType
60                         List<String> targetNameList = expectation.getExpectationTargets()
61                                 .stream().map(ExpectationTarget::getTargetName).collect(Collectors.toList());
62                         for (Expectation dbExpectation : sameObjTypeList) {
63                             List<String> dbTargetNameList = dbExpectation.getExpectationTargets()
64                                     .stream().map(ExpectationTarget::getTargetName).collect(Collectors.toList());
65                             //todo name compare need ai
66                             if (dbTargetNameList.containsAll(targetNameList)) {
67                                 count++;
68                                 break;
69                             }
70                         }
71                     }
72                     if (count == expectationList.size()) {
73                         intentIdList.add(intentId);
74                         break;
75                     }
76                 }
77             }
78         }
79         return intentIdList;
80     }
81
82     public List<Intent> filterIntent(List<Intent> list) {
83         List<Intent> fiterList = new ArrayList<>();
84         for (Intent intent : list) {
85             //filter the intent which save first time  in controller
86             List<Context> ownerIdContextList = intent.getIntentContexts().stream().filter(x ->
87                     StringUtils.equalsIgnoreCase(x.getContextName(), "ownerId")).collect(Collectors.toList());
88             if (CollectionUtils.isEmpty(ownerIdContextList)) {
89                 fiterList.add(intent);
90                 continue;
91             }
92             // condition   ownerName = foramtIntentInput
93             List<Context> ownerInfo = intent.getIntentContexts().stream().filter(x ->
94                     StringUtils.equalsIgnoreCase(x.getContextName(), "owner Info")).collect(Collectors.toList());
95             if (CollectionUtils.isNotEmpty(ownerInfo)) {
96                 for (Context context : ownerInfo) {
97                     List<Condition> contextConditions = context.getContextConditions();
98                     boolean equals = false;
99                     for (Condition condition : contextConditions) {
100                         String conditionstr = "owner class name equal to formatIntentInputManagementFunction";
101                         String conValue = condition.getConditionValue();
102                         String conditionValue = conValue.substring(conValue.lastIndexOf(".")+1);
103                         String concatStr = condition.getConditionName() + condition.getOperator().name() + conditionValue;
104                         if (StringUtils.equalsIgnoreCase(concatStr.replaceAll(" ",""), conditionstr.replaceAll(" ", ""))) {
105                             fiterList.add(intent);
106                             equals = true;
107                             break;
108                         }
109                     }
110                     if (equals == true) {
111                         break;
112                     }
113                 }
114             }
115         }
116         list.removeAll(fiterList);
117         return list;
118     }
119 }