Include impacted changes for APPC-346,APPC-348
[appc.git] / appc-dispatcher / appc-request-handler / appc-request-handler-core / src / main / java / org / onap / appc / validationpolicy / rules / AcceptRule.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.validationpolicy.rules;
26
27 import org.onap.appc.domainmodel.lcm.VNFOperation;
28 import org.onap.appc.validationpolicy.objects.RuleResult;
29
30 import java.util.List;
31 import java.util.Set;
32 import java.util.stream.Collectors;
33
34 /**
35  * Implementation of Accept Rule
36  */
37 public class AcceptRule extends Rule {
38
39     AcceptRule(Set<VNFOperation> inclusions,Set<VNFOperation> exclusions){
40         super(inclusions,exclusions);
41     }
42
43     @Override
44     public RuleResult executeRule(String action, List<VNFOperation> inProgressActionList) {
45
46         if(inclusions!=null &&  !inclusions.isEmpty()){
47             /* new action (action-received) should be rejected,
48                if any of the in-progress request action
49                is mentioned in the in-progress-exclusion list*/
50             if(logger.isDebugEnabled()){
51                 logger.debug("Executing accept rule for action-received = "+action+" and in-progress-actions-inclusions = "+ inProgressActionList.toString());
52             }
53             Set<String> inProgressActionSet = inProgressActionList.stream().map(VNFOperation::name).collect(Collectors.toSet());
54             inProgressActionSet.removeAll(inclusions.stream().map(VNFOperation::name).collect(Collectors.toList()));
55             if(!inProgressActionSet.isEmpty()){
56                 return RuleResult.REJECT;
57             }
58             return RuleResult.ACCEPT;
59         }
60         else if(exclusions!=null && !exclusions.isEmpty()){
61             /* new action (action-received) should be accepted,
62                if all of the in-progress request actions
63                are mentioned in the in-progress-inclusion list */
64             if(logger.isDebugEnabled()){
65                 logger.debug("Executing accept rule for action-received = %s and in-progress-actions-exclusions = %s",action,inProgressActionList.toString());
66             }
67             Set<String> inProgressActionSet = inProgressActionList.stream().map(VNFOperation::name).collect(Collectors.toSet());
68             inProgressActionSet.retainAll(exclusions.stream().map(VNFOperation::name).collect(Collectors.toList()));
69             if(!inProgressActionSet.isEmpty()){
70                 return RuleResult.REJECT;
71             }
72             return RuleResult.ACCEPT;
73         }
74         return RuleResult.ACCEPT;
75     }
76 }