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