b814c6f4a48151c6d3f9cd63dddbd18abd1bbea1
[policy/drools-applications.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2018-2020 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.drl.legacy.ControlLoopParams;
25 import org.onap.policy.drools.features.PolicyEngineFeatureApi;
26 import org.onap.policy.drools.system.PolicyController;
27 import org.onap.policy.drools.system.PolicyControllerConstants;
28
29 /**
30  * Control Loop Management Feature.
31  */
32 public class ControlLoopManagementFeature implements PolicyEngineFeatureApi {
33
34     private static final String FEATURE_NAME = "controlloop-management";
35     private static final int SEQNO = 1000;
36
37     /**
38      * Factory for various objects.  May be overridden by junit tests.
39      */
40     private static Factory factory = new Factory();
41
42
43     /**
44      * retrieves control loops.
45      *
46      * @param controllerName controller name.
47      * @param sessionName session name.
48      * @return control loops.
49      */
50     public static Stream<ControlLoopParams> controlLoops(String controllerName, String sessionName) {
51         PolicyController controller = factory.getController(controllerName);
52         if (controller == null) {
53             throw new IllegalArgumentException("Invalid Controller Name");
54         }
55
56         if (controller.getDrools().getSessionNames().stream().filter(sessionName::equals).count() <= 0) {
57             throw new IllegalArgumentException("Invalid Session Name");
58         }
59
60         return controller.getDrools()
61             .facts(sessionName, ControlLoopParams.class.getName(), false)
62             .stream()
63             .filter(c -> c instanceof ControlLoopParams)
64             .map(c -> (ControlLoopParams) c);
65     }
66
67     /**
68      * retrieves a control loop.
69      *
70      * @param controllerName controller name.
71      * @param sessionName session name.
72      * @param controlLoopName control loop name
73      *
74      * @return control loops.
75      */
76     public static ControlLoopParams controlLoop(String controllerName, String sessionName, String controlLoopName) {
77         return controlLoops(controllerName, sessionName)
78             .filter(c -> c.getClosedLoopControlName().equals(controlLoopName))
79             .findFirst()
80             .orElse(null);
81     }
82
83     /**
84      * {@inheritDoc}.
85      */
86     @Override
87     public int getSequenceNumber() {
88         return SEQNO;
89     }
90
91     /**
92      * {@inheritDoc}.
93      */
94     @Override
95     public String getName() {
96         return FEATURE_NAME;
97     }
98
99     /**
100      * Factory that can be overridden by junit tests.
101      */
102     public static class Factory {
103         public PolicyController getController(String controllerName) {
104             return PolicyControllerConstants.getFactory().get(controllerName);
105         }
106     }
107 }