e9fb811c79118e31c58f0909b5785979ed1750a8
[policy/drools-applications.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.drools.apps.controlloop.feature.management;
22
23 import java.util.stream.Stream;
24 import org.onap.policy.controlloop.params.ControlLoopParams;
25 import org.onap.policy.drools.features.PolicyEngineFeatureAPI;
26 import org.onap.policy.drools.system.PolicyController;
27
28 /**
29  * Control Loop Management Feature.
30  */
31 public class ControlLoopManagementFeature implements PolicyEngineFeatureAPI {
32
33     private static final String FEATURE_NAME = "controlloop-management";
34     private static final int SEQNO = 1000;
35
36     /**
37      * retrieves control loops.
38      *
39      * @param controllerName controller name.
40      * @param sessionName session name.
41      * @return control loops.
42      */
43     public static Stream<ControlLoopParams> controlLoops(String controllerName, String sessionName) {
44         PolicyController controller = PolicyController.factory.get(controllerName);
45         if (controller == null) {
46             throw new IllegalArgumentException("Invalid Controller Name");
47         }
48
49         if (controller.getDrools().getSessionNames().stream().filter(sessionName::equals).count() <= 0) {
50             throw new IllegalArgumentException("Invalid Session Name");
51         }
52
53         return controller.getDrools()
54             .facts(sessionName, ControlLoopParams.class.getCanonicalName(), false)
55             .stream()
56             .filter(c -> c instanceof ControlLoopParams)
57             .map(c -> (ControlLoopParams) c);
58     }
59
60     /**
61      * retrieves a control loop.
62      *
63      * @param controllerName controller name.
64      * @param sessionName session name.
65      * @param controlLoopName control loop name
66      *
67      * @return control loops.
68      */
69     public static ControlLoopParams controlLoop(String controllerName, String sessionName, String controlLoopName) {
70         return controlLoops(controllerName, sessionName)
71             .filter(c -> c.getClosedLoopControlName().equals(controlLoopName))
72             .findFirst()
73             .orElse(null);
74     }
75
76     /**
77      * {@inheritDoc}.
78      */
79     @Override
80     public int getSequenceNumber() {
81         return SEQNO;
82     }
83
84     /**
85      * {@inheritDoc}.
86      */
87     @Override
88     public String getName() {
89         return FEATURE_NAME;
90     }
91
92 }