aboutsummaryrefslogtreecommitdiffstats
path: root/controlloop/templates/template.demo/src/test/java/org/onap/policy/template/demo/ControlLoopBase.java
blob: 5817dc9300053596bdbb58ac5aaf1ad79e0f2eea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*-
 * ============LICENSE_START=======================================================
 * demo
 * ================================================================================
 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
 * Modifications Copyright (C) 2019 Bell Canada.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.template.demo;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Properties;
import java.util.UUID;
import org.junit.AfterClass;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.rule.FactHandle;
import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
import org.onap.policy.common.endpoints.event.comm.TopicSink;
import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
import org.onap.policy.controlloop.policy.ControlLoopPolicy;
import org.onap.policy.drools.protocol.coders.EventProtocolCoderConstants;
import org.onap.policy.drools.protocol.coders.EventProtocolParams;
import org.onap.policy.drools.protocol.coders.JsonProtocolFilter;
import org.onap.policy.drools.system.PolicyControllerConstants;
import org.onap.policy.drools.system.PolicyEngineConstants;
import org.onap.policy.drools.utils.logging.LoggerUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Common super class used by various Control Loop test classes. It manages the simulators
 * and the kie session.
 */
public class ControlLoopBase {

    private static final String JUNIT_ARTIFACT_ID = "junit.artifactId";

    private static final String JUNIT_GROUP_ID = "junit.groupId";

    protected static final Logger logger = LoggerFactory.getLogger(ControlLoopBase.class);

    protected static List<? extends TopicSink> noopTopics;

    protected static KieSession kieSession;
    protected static SupportUtil.Pair<ControlLoopPolicy, String> pair;

    protected UUID requestId;

    /**
     * Starts the simulator and the kie session.
     *
     * @param droolsTemplate the DRL rules file
     * @param yamlFile the yaml file containing the policies
     * @param policyScope scope for policy
     * @param policyName name of the policy
     * @param policyVersion version of the policy
     */
    public static void setUpBeforeClass(String droolsTemplate, String yamlFile, String policyScope,
                    String policyName, String policyVersion) {

        /* Set environment properties */
        SupportUtil.setAaiProps();
        SupportUtil.setGuardProps();
        SupportUtil.setSdncProps();
        SupportUtil.setSoProps();
        SupportUtil.setVfcProps();
        SupportUtil.setPuProp();
        SupportUtil.setCdsProps();

        LoggerUtil.setLevel(LoggerUtil.ROOT_LOGGER, "INFO");

        PolicyEngineConstants.getManager().configure(new Properties());
        assertTrue(PolicyEngineConstants.getManager().start());
        Properties noopSinkProperties = new Properties();
        noopSinkProperties.put(PolicyEndPointProperties.PROPERTY_NOOP_SINK_TOPICS,
                        "APPC-LCM-READ,APPC-CL,SDNR-CL,POLICY-CL-MGT");
        noopSinkProperties.put("noop.sink.topics.APPC-LCM-READ.events", "org.onap.policy.appclcm.LcmRequestWrapper");
        noopSinkProperties.put("noop.sink.topics.APPC-LCM-READ.events.custom.gson",
                "org.onap.policy.appclcm.util.Serialization,gson");
        noopSinkProperties.put("noop.sink.topics.APPC-CL.events", "org.onap.policy.appc.Response");
        noopSinkProperties.put("noop.sink.topics.APPC-CL.events.custom.gson",
                "org.onap.policy.appc.util.Serialization,gsonPretty");
        noopSinkProperties.put("noop.sink.topics.POLICY-CL-MGT.events",
                "org.onap.policy.controlloop.VirtualControlLoopNotification");
        noopSinkProperties.put("noop.sink.topics.POLICY-CL-MGT.events.custom.gson",
                "org.onap.policy.controlloop.util.Serialization,gsonPretty");
        noopTopics = TopicEndpointManager.getManager().addTopicSinks(noopSinkProperties);

        EventProtocolCoderConstants.getManager().addEncoder(EventProtocolParams.builder()
                .groupId(JUNIT_GROUP_ID)
                .artifactId(JUNIT_ARTIFACT_ID)
                .topic("POLICY-CL-MGT")
                .eventClass("org.onap.policy.controlloop.VirtualControlLoopNotification")
                .protocolFilter(new JsonProtocolFilter())
                .modelClassLoaderHash(1111));
        EventProtocolCoderConstants.getManager().addEncoder(EventProtocolParams.builder()
                .groupId(JUNIT_GROUP_ID)
                .artifactId(JUNIT_ARTIFACT_ID)
                .topic("APPC-LCM-READ")
                .eventClass("org.onap.policy.appclcm.LcmRequestWrapper")
                .protocolFilter(new JsonProtocolFilter())
                .modelClassLoaderHash(1111));
        EventProtocolCoderConstants.getManager().addEncoder(EventProtocolParams.builder()
                .groupId(JUNIT_GROUP_ID)
                .artifactId(JUNIT_ARTIFACT_ID)
                .topic("APPC-CL")
                .eventClass("org.onap.policy.appc.Request")
                .protocolFilter(new JsonProtocolFilter())
                .modelClassLoaderHash(1111));
        EventProtocolCoderConstants.getManager().addEncoder(EventProtocolParams.builder()
                .groupId(JUNIT_GROUP_ID)
                .artifactId(JUNIT_ARTIFACT_ID)
                .topic("SDNR-CL")
                .eventClass("org.onap.policy.sdnr.PciRequestWrapper")
                .protocolFilter(new JsonProtocolFilter())
                .modelClassLoaderHash(1111));
        try {
            SupportUtil.buildAaiSim();
            SupportUtil.buildSdncSim();
            SupportUtil.buildSoSim();
            SupportUtil.buildVfcSim();
            SupportUtil.buildGuardSim();
        } catch (Exception e) {
            fail(e.getMessage());
        }


        /*
         * Start the kie session
         */
        try {
            kieSession = startSession(droolsTemplate, yamlFile, policyScope,
                            policyName, policyVersion);
        } catch (IOException e) {
            e.printStackTrace();
            logger.debug("Could not create kieSession");
            fail("Could not create kieSession");
        }
    }

    /**
     * Stops the simulators and the kie session.
     */
    @AfterClass
    public static void tearDownAfterClass() {
        /*
         * Gracefully shut down the kie session
         */
        kieSession.dispose();

        PolicyEngineConstants.getManager().stop();
        HttpServletServerFactoryInstance.getServerFactory().destroy();
        PolicyControllerConstants.getFactory().shutdown();
        TopicEndpointManager.getManager().shutdown();
    }

    /**
     * This method will start a kie session and instantiate the Policy Engine.
     *
     * @param droolsTemplate the DRL rules file
     * @param yamlFile the yaml file containing the policies
     * @param policyScope scope for policy
     * @param policyName name of the policy
     * @param policyVersion version of the policy
     * @return the kieSession to be used to insert facts
     * @throws IOException IO exception
     */
    private static KieSession startSession(String droolsTemplate, String yamlFile, String policyScope,
            String policyName, String policyVersion) throws IOException {

        /*
         * Load policies from yaml
         */
        pair = SupportUtil.loadYaml(yamlFile);
        assertNotNull(pair);
        assertNotNull(pair.first);
        assertNotNull(pair.first.getControlLoop());
        assertNotNull(pair.first.getControlLoop().getControlLoopName());
        assertTrue(pair.first.getControlLoop().getControlLoopName().length() > 0);

        /*
         * Construct a kie session
         */
        final KieSession kieSession = SupportUtil.buildContainer(droolsTemplate,
                pair.first.getControlLoop().getControlLoopName(),
                policyScope, policyName, policyVersion, URLEncoder.encode(pair.second, "UTF-8"));

        /*
         * Retrieve the Policy Engine
         */

        logger.debug("============");
        logger.debug(URLEncoder.encode(pair.second, "UTF-8"));
        logger.debug("============");

        return kieSession;
    }

    /**
     * This method will dump all the facts in the working memory.
     *
     * @param kieSession the session containing the facts
     */
    public void dumpFacts(KieSession kieSession) {
        logger.debug("Fact Count: {}", kieSession.getFactCount());
        for (FactHandle handle : kieSession.getFactHandles()) {
            logger.debug("FACT: {}", handle);
        }
    }

}