aboutsummaryrefslogtreecommitdiffstats
path: root/controlloop/common/controller-frankfurt/src/main/resources/frankfurt.drl
blob: 9723cec4317e3fedeafb19a02306eeb2b52245c4 (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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
 * ============LICENSE_START=======================================================
 * ONAP
 * ================================================================================
 * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * 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.controlloop;

import org.onap.policy.controlloop.drl.legacy.ControlLoopParams;
import org.onap.policy.controlloop.CanonicalOnset;
import org.onap.policy.controlloop.VirtualControlLoopEvent;
import org.onap.policy.controlloop.VirtualControlLoopNotification;
import org.onap.policy.controlloop.ControlLoopNotificationType;
import org.onap.policy.controlloop.eventmanager.ControlLoopEventManager2;
import org.onap.policy.controlloop.eventmanager.ControlLoopEventManager2.NewEventStatus;
import org.onap.policy.controlloop.eventmanager.ControlLoopOperationManager2;
import org.onap.policy.controlloop.utils.ControlLoopUtils;
import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;

import org.slf4j.LoggerFactory;
import org.slf4j.Logger;

import org.onap.policy.drools.system.PolicyEngineConstants;

/*
*
* Called when the ControlLoopParams object has been inserted into working memory from the BRMSGW.
*
*/
rule "INSERT.PARAMS"
    when
        $params : ControlLoopParams()
    then

    Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
    logger.info("{}: {} : TOSCA-POLICY=[{}]", $params.getClosedLoopControlName(), $params.getPolicyName() + "."
        + drools.getRule().getName(), $params.getToscaPolicy());
end

/*
*
* Called when a Tosca Policy is present.
*
*/
rule "NEW.TOSCA.POLICY"
    when
        $policy : ToscaPolicy()
    then

    Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
    logger.info("{}: [{}|{}|{}|{}]: CONTENT: {}", drools.getRule().getName(),
                $policy.getType(), $policy.getTypeVersion(), $policy.getName(),
                $policy.getVersion(), $policy);

    ControlLoopParams params = ControlLoopUtils.toControlLoopParams($policy);
    if (params != null) {
        insert(params);
    }
end

/*
 * Remove Control Loop Parameters.
 */
rule "REMOVE.PARAMS"
    when
        $params : ControlLoopParams( $policyName :  getPolicyName(), $policyVersion : getPolicyVersion() )
        not ( ToscaPolicy( getName() == $policyName, getVersion() == $policyVersion ) )
    then

    Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
    logger.info("{}: [{}|{}|{}]", drools.getRule().getName(),
                $params.getPolicyScope(), $params.getPolicyName(), $params.getPolicyVersion());

    retract($params);
end

/*
*
* This rule responds to DCAE Events where there is no manager yet. Either it is
* the first ONSET, or a subsequent badly formed Event (i.e. Syntax error, or is-closed-loop-disabled)
*
*/
rule "EVENT"
    when
        $params : ControlLoopParams( $clName : getClosedLoopControlName() )
        $event : CanonicalOnset( closedLoopControlName == $clName )
        not ( ControlLoopEventManager2( closedLoopControlName == $event.getClosedLoopControlName(),
            getContext().getEvent() == $event ) )
    then

    Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
    logger.info("{}: {}.{}: event={}",
                $clName, $params.getPolicyName(), drools.getRule().getName(),
                $event);
    //
    // Retract the event from memory; it will be managed by the manager for now on
    //
    retract($event);

    VirtualControlLoopNotification notification;

    try {
        //
        // Check the event, because we need it to not be null when
        // we create the ControlLoopEventManager2. The ControlLoopEventManager2
        // will do extra syntax checking as well as check if the closed loop is disabled.
        //
        if ($event.getRequestId() == null) {
            notification = new VirtualControlLoopNotification($event);
            notification.setNotification(ControlLoopNotificationType.REJECTED);
            notification.setFrom("policy");
            notification.setMessage("Missing requestId");
            notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
            notification.setPolicyScope($params.getPolicyScope());
            notification.setPolicyVersion($params.getPolicyVersion());

        } else {
            ControlLoopEventManager2 manager = new ControlLoopEventManager2($params, $event, drools.getWorkingMemory());
            insert(manager);
            try {
                manager.start();
            } catch(Exception e) {
                retract(manager);
                throw e;
            }
            notification = manager.makeNotification();
            notification.setNotification(ControlLoopNotificationType.ACTIVE);
            notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
        }
    } catch (Exception e) {
        logger.warn("{}: {}.{}", $clName, $params.getPolicyName(), drools.getRule().getName(), e);
        notification = new VirtualControlLoopNotification($event);
        notification.setNotification(ControlLoopNotificationType.REJECTED);
        notification.setMessage("Exception occurred: " + e.getMessage());
        notification.setPolicyName($params.getPolicyName() + "." + drools.getRule().getName());
        notification.setPolicyScope($params.getPolicyScope());
        notification.setPolicyVersion($params.getPolicyVersion());
    }
    //
    // Generate notification
    //
    try {
        PolicyEngineConstants.getManager().deliver("POLICY-CL-MGT", notification);

    } catch(RuntimeException e) {
        logger.warn("{}: {}.{}: event={} exception generating notification",
                $clName, $params.getPolicyName(), drools.getRule().getName(),
                $event, e);
    }
end

/*
*
* This rule happens when we get a subsequent event.
*
*/
rule "EVENT.MANAGER.NEW.EVENT"
    when
        $event : VirtualControlLoopEvent( )
        $manager : ControlLoopEventManager2( closedLoopControlName == $event.getClosedLoopControlName(),
            getContext().getEvent() == $event )
    then

    Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
    logger.info("{}: {}.{}: event={} manager={}",
            $manager.getClosedLoopControlName(), $manager.getPolicyName(), drools.getRule().getName(),
            $event, $manager);
    //
    // Remove the event from memory
    //
    retract($event);

    //
    // Check what kind of event this is
    //
    switch($manager.onNewEvent($event)) {
        case SYNTAX_ERROR:
            //
            // Ignore any bad syntax events
            //
            logger.warn("{}: {}.{}: syntax error",
                $manager.getClosedLoopControlName(), $manager.getPolicyName(), drools.getRule().getName());
            break;

        case FIRST_ABATEMENT:
        case SUBSEQUENT_ABATEMENT:
            //
            // TODO: handle the abatement.  Currently, it's just discarded.
            //
            logger.info("{}: {}.{}: abatement",
                $manager.getClosedLoopControlName(), $manager.getPolicyName(), drools.getRule().getName());
            break;

        case FIRST_ONSET:
        case SUBSEQUENT_ONSET:
        default:
            //
            // We don't care about subsequent onsets
            //
            logger.warn("{}: {}.{}: subsequent onset",
                $manager.getClosedLoopControlName(), $manager.getPolicyName(), drools.getRule().getName());
            break;
    }
end

/*
*
* Step completed
*
*/
rule "EVENT.MANAGER.PROCESSING"
    when
        $manager : ControlLoopEventManager2( isUpdated(), isActive(), $notification : getNotification() )
    then

    Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
    logger.info("{}: {}.{}: manager={}",
            $manager.getClosedLoopControlName(), $manager.getPolicyName(), drools.getRule().getName(),
            $manager);
    //
    // Generate notification
    //
    try {
        $notification.setPolicyName($manager.getPolicyName() + "." + drools.getRule().getName());
        PolicyEngineConstants.getManager().deliver("POLICY-CL-MGT", $notification);

    } catch(RuntimeException e) {
        logger.warn("{}: {}.{}: manager={} exception generating notification",
                $manager.getClosedLoopControlName(), $manager.getPolicyName(), drools.getRule().getName(),
                $manager, e);
    }
    //
    // Generate Response notification
    //
    try {
        ControlLoopResponse clResponse = $manager.getControlLoopResponse();
        if (clResponse != null) {
            PolicyEngineConstants.getManager().deliver("DCAE_CL_RSP", clResponse);
        }

    } catch(RuntimeException e) {
        logger.warn("{}: {}.{}: manager={} exception generating Response notification",
                $manager.getClosedLoopControlName(), $manager.getPolicyName(), drools.getRule().getName(),
                $manager, e);
    }
    //
    // Discard this message and wait for the next response.
    //
    $manager.nextStep();
    update($manager);
end

/*
*
* Final step completed
*
*/
rule "EVENT.MANAGER.FINAL"
    when
        $manager : ControlLoopEventManager2( !isActive(), $notification : getNotification() )
    then

    Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
    logger.info("{}: {}.{}: manager={}",
            $manager.getClosedLoopControlName(), $manager.getPolicyName(), drools.getRule().getName(),
            $manager);
    //
    // Generate notification
    //
    try {
        $notification.setPolicyName($manager.getPolicyName() + "." + drools.getRule().getName());
        PolicyEngineConstants.getManager().deliver("POLICY-CL-MGT", $notification);

    } catch(RuntimeException e) {
        logger.warn("{}: {}.{}: manager={} exception generating notification",
                $manager.getClosedLoopControlName(), $manager.getPolicyName(), drools.getRule().getName(),
                $manager, e);
    }
    //
    // Retract and destroy the manager
    //
    retract($manager);
    $manager.destroy();
end

/*
*
* This rule will clean up any rogue events where there is no
* ControlLoopParams object corresponding to the onset event.
*
*/
rule "EVENT.CLEANUP"
    salience -1
    when
        $event : VirtualControlLoopEvent( $clName: closedLoopControlName )
    then

    Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage());
    logger.info("{}: {}", $clName, drools.getRule().getName());
    logger.debug("{}: {}: orphan event={}",
                $clName, drools.getRule().getName(), $event);
    //
    // Retract the event
    //
    retract($event);
end

/*
*
* At this point, it appears that if we prevent the rules from getting messages from
* topics, then that will also prevent the actors from getting them.  So the following
* rules are here just to discard those messages.
*
* These have a higher salience so the objects are removed before the "FINAL" message
* is processed, so that the junit test can assume things are done once they see the
* "FINAL" message.  Otherwise, tests might fail sporadically.
*
*/
rule "APPC.Response.CLEANUP"
    salience 1
    when
        $msg : org.onap.policy.appc.Response( )
    then
        retract($msg);
end

rule "APPC.Request.CLEANUP"
    salience 1
    when
        $msg : org.onap.policy.appc.Request( )
    then
        retract($msg);
end

rule "APPC-LCM.Response.CLEANUP"
    salience 1
    when
        $msg : org.onap.policy.appclcm.AppcLcmDmaapWrapper( )
    then
        retract($msg);
end

rule "SDNR.Response.CLEANUP"
    salience 1
    when
        $msg : org.onap.policy.sdnr.PciResponseWrapper( )
    then
        retract($msg);
end