Fix some sonar issues in drools-applications
[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-2019 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      * Factory for various objects.  May be overridden by junit tests.
38      */
39     private static Factory factory = new Factory();
40
41
42     /**
43      * retrieves control loops.
44      *
45      * @param controllerName controller name.
46      * @param sessionName session name.
47      * @return control loops.
48      */
49     public static Stream<ControlLoopParams> controlLoops(String controllerName, String sessionName) {
50         PolicyController controller = factory.getController(controllerName);
51         if (controller == null) {
52             throw new IllegalArgumentException("Invalid Controller Name");
53         }
54
55         if (controller.getDrools().getSessionNames().stream().filter(sessionName::equals).count() <= 0) {
56             throw new IllegalArgumentException("Invalid Session Name");
57         }
58
59         return controller.getDrools()
60             .facts(sessionName, ControlLoopParams.class.getName(), false)
61             .stream()
62             .filter(c -> c instanceof ControlLoopParams)
63             .map(c -> (ControlLoopParams) c);
64     }
65
66     /**
67      * retrieves a control loop.
68      *
69      * @param controllerName controller name.
70      * @param sessionName session name.
71      * @param controlLoopName control loop name
72      *
73      * @return control loops.
74      */
75     public static ControlLoopParams controlLoop(String controllerName, String sessionName, String controlLoopName) {
76         return controlLoops(controllerName, sessionName)
77             .filter(c -> c.getClosedLoopControlName().equals(controlLoopName))
78             .findFirst()
79             .orElse(null);
80     }
81
82     /**
83      * {@inheritDoc}.
84      */
85     @Override
86     public int getSequenceNumber() {
87         return SEQNO;
88     }
89
90     /**
91      * {@inheritDoc}.
92      */
93     @Override
94     public String getName() {
95         return FEATURE_NAME;
96     }
97
98     /**
99      * Factory that can be overridden by junit tests.
100      */
101     public static class Factory {
102         public PolicyController getController(String controllerName) {
103             return PolicyController.factory.get(controllerName);
104         }
105     }
106 }