3599d47d0c645f0f611ec70f9a789826494321a1
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2018 Huawei. All rights reserved.
4  * Modifications Copyright (C) 2019-2020 AT&T Intellectual Property.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  * ============LICENSE_END=========================================================
18  */
19
20 package org.onap.policy.template.demo;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
26
27 import java.io.IOException;
28 import java.time.Instant;
29 import java.util.HashMap;
30 import java.util.UUID;
31 import org.junit.BeforeClass;
32 import org.junit.Test;
33 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
34 import org.onap.policy.common.endpoints.event.comm.TopicListener;
35 import org.onap.policy.common.endpoints.event.comm.TopicSink;
36 import org.onap.policy.controlloop.ControlLoopEventStatus;
37 import org.onap.policy.controlloop.ControlLoopNotificationType;
38 import org.onap.policy.controlloop.ControlLoopTargetType;
39 import org.onap.policy.controlloop.VirtualControlLoopEvent;
40 import org.onap.policy.controlloop.VirtualControlLoopNotification;
41 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
42 import org.onap.policy.sdnc.SdncRequest;
43
44 public class CcvpnControlLoopTest extends ControlLoopBase implements TopicListener {
45
46     /**
47      * Setup the simulator.
48      */
49     @BeforeClass
50     public static void setUpBeforeClass() {
51         ControlLoopBase.setUpBeforeClass(
52             "../archetype-cl-amsterdam/src/main/resources/archetype-resources/"
53                             + "src/main/resources/__closedLoopControlName__.drl",
54             "src/test/resources/yaml/policy_ControlLoop_CCVPN.yaml",
55             "type=operational",
56             "Connectivity Reroute",
57             "2.0.0");
58     }
59
60     @Test
61     public void testSuccess() throws IOException {
62
63         /*
64          * Allows the PolicyEngine to callback to this object to notify that there is an event ready
65          * to be pulled from the queue
66          */
67         for (TopicSink sink : noopTopics) {
68             assertTrue(sink.start());
69             sink.register(this);
70         }
71
72         /*
73          * Simulate an onset event the policy engine will receive from DCAE to kick off processing
74          * through the rules
75          */
76         sendEvent(pair.first);
77
78         kieSession.fireUntilHalt();
79
80         // allow object clean-up
81         kieSession.fireAllRules();
82
83         /*
84          * The only fact in memory should be Params
85          */
86         assertEquals(1, kieSession.getFactCount());
87
88         /*
89          * Print what's left in memory
90          */
91         dumpFacts(kieSession);
92     }
93
94     @Test
95     public void testNullRequest() throws IOException {
96
97         /*
98          * Allows the PolicyEngine to callback to this object to notify that there is an event ready
99          * to be pulled from the queue
100          */
101         for (TopicSink sink : noopTopics) {
102             assertTrue(sink.start());
103             sink.register(this);
104         }
105
106         /*
107          * Simulate an onset event the policy engine will receive from DCAE to kick off processing
108          * through the rules
109          */
110
111         VirtualControlLoopEvent event = new VirtualControlLoopEvent();
112         event.setClosedLoopControlName(pair.first.getControlLoop().getControlLoopName());
113         event.setRequestId(UUID.randomUUID());
114         event.setClosedLoopEventClient("DCAE.HolmesInstance");
115         event.setTargetType(ControlLoopTargetType.VM);
116         event.setTarget("vserver.vserver-name");
117         event.setFrom("DCAE");
118         event.setClosedLoopAlarmStart(Instant.now());
119         event.setAai(new HashMap<String, String>());
120         event.getAai().put("vserver.vserver-name", "nullRequest");
121         event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
122         kieSession.insert(event);
123
124         kieSession.fireUntilHalt();
125
126         // allow object clean-up
127         kieSession.fireAllRules();
128
129         /*
130          * The only fact in memory should be Params
131          */
132         assertEquals(1, kieSession.getFactCount());
133
134         /*
135          * Print what's left in memory
136          */
137         dumpFacts(kieSession);
138     }
139
140     /*
141      * (non-Javadoc)
142      *
143      * @see org.onap.policy.drools.PolicyEngineListener#newEventNotification(java.lang.String)
144      */
145     @Override
146     public void onTopicEvent(CommInfrastructure commType, String topic, String event) {
147         /*
148          * Pull the object that was sent out to DMAAP and make sure it is a ControlLoopNoticiation
149          * of type active
150          */
151         Object obj = null;
152         if ("POLICY-CL-MGT".equals(topic)) {
153             obj = org.onap.policy.controlloop.util.Serialization.gsonJunit.fromJson(event,
154                     VirtualControlLoopNotification.class);
155         }
156         assertNotNull(obj);
157         if (obj instanceof VirtualControlLoopNotification) {
158             VirtualControlLoopNotification notification = (VirtualControlLoopNotification) obj;
159             String policyName = notification.getPolicyName();
160             if (policyName.endsWith("EVENT")) {
161                 logger.debug("Rule Fired: " + notification.getPolicyName());
162                 assertTrue(ControlLoopNotificationType.ACTIVE.equals(notification.getNotification()));
163             } else if (policyName.endsWith("GUARD_NOT_YET_QUERIED")) {
164                 logger.debug("Rule Fired: " + notification.getPolicyName());
165                 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
166                 assertNotNull(notification.getMessage());
167                 assertTrue(notification.getMessage().startsWith("Sending guard query"));
168             } else if (policyName.endsWith("GUARD.RESPONSE")) {
169                 logger.debug("Rule Fired: " + notification.getPolicyName());
170                 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
171                 assertNotNull(notification.getMessage());
172                 assertTrue(notification.getMessage().toLowerCase().endsWith("permit"));
173             } else if (policyName.endsWith("GUARD_PERMITTED")) {
174                 logger.debug("Rule Fired: " + notification.getPolicyName());
175                 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
176                 assertNotNull(notification.getMessage());
177                 assertTrue(notification.getMessage().startsWith("actor=SDNC"));
178             } else if (policyName.endsWith("OPERATION.TIMEOUT")) {
179                 logger.debug("Rule Fired: " + notification.getPolicyName());
180                 kieSession.halt();
181                 logger.debug("The operation timed out");
182                 fail("Operation Timed Out");
183             } else if (policyName.endsWith("SDNC.RESPONSE")) {
184                 logger.debug("Rule Fired: " + notification.getPolicyName());
185                 assertTrue(ControlLoopNotificationType.OPERATION_SUCCESS.equals(notification.getNotification()));
186                 assertNotNull(notification.getMessage());
187                 assertTrue(notification.getMessage().startsWith("actor=SDNC"));
188             } else if (policyName.endsWith("EVENT.MANAGER")) {
189                 logger.debug("Rule Fired: " + notification.getPolicyName());
190                 if ("nullRequest".equals(notification.getAai().get("vserver.vserver-name"))) {
191                     assertEquals(ControlLoopNotificationType.FINAL_FAILURE, notification.getNotification());
192                 } else {
193                     assertEquals(ControlLoopNotificationType.FINAL_SUCCESS, notification.getNotification());
194                 }
195                 kieSession.halt();
196             } else if (policyName.endsWith("EVENT.MANAGER.TIMEOUT")) {
197                 logger.debug("Rule Fired: " + notification.getPolicyName());
198                 kieSession.halt();
199                 logger.debug("The control loop timed out");
200                 fail("Control Loop Timed Out");
201             }
202         } else if (obj instanceof SdncRequest) {
203             logger.debug("\n============ SDNC received the request!!! ===========\n");
204         }
205     }
206
207     /**
208      * This method is used to simulate event messages from DCAE that start the control loop (onset
209      * message) or end the control loop (abatement message).
210      *
211      * @param policy the controlLoopName comes from the policy
212      */
213     protected void sendEvent(ControlLoopPolicy policy) {
214         VirtualControlLoopEvent event = new VirtualControlLoopEvent();
215         event.setClosedLoopControlName(policy.getControlLoop().getControlLoopName());
216         event.setRequestId(UUID.randomUUID());
217         event.setClosedLoopEventClient("DCAE.HolmesInstance");
218         event.setTargetType(ControlLoopTargetType.VM);
219         event.setTarget("vserver.vserver-name");
220         event.setFrom("DCAE");
221         event.setClosedLoopAlarmStart(Instant.now());
222         event.setAai(new HashMap<String, String>());
223         event.getAai().put("vserver.vserver-name", "TBD");
224         event.getAai().put("globalSubscriberId", "e151059a-d924-4629-845f-264db19e50b4");
225         event.getAai().put("serviceType", "SOTN");
226         event.getAai().put("service-instance.service-instance-id", "service-instance-id-example-1");
227         event.getAai().put("network-information.network-id", "id");
228         event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
229         kieSession.insert(event);
230     }
231 }