7dbb4ad231eb40b7d9a4abbcfb662421826fbc96
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
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  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.examples.adaptive.model;
23
24 import org.onap.policy.apex.model.basicmodel.concepts.ApexRuntimeException;
25 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
26 import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInformation;
27 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
28 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
29 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum;
30 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbums;
31 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
32 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
33 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
34 import org.onap.policy.apex.model.eventmodel.concepts.AxEvents;
35 import org.onap.policy.apex.model.eventmodel.concepts.AxField;
36 import org.onap.policy.apex.model.policymodel.concepts.AxLogicReader;
37 import org.onap.policy.apex.model.policymodel.concepts.AxPolicies;
38 import org.onap.policy.apex.model.policymodel.concepts.AxPolicy;
39 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
40 import org.onap.policy.apex.model.policymodel.concepts.AxState;
41 import org.onap.policy.apex.model.policymodel.concepts.AxStateOutput;
42 import org.onap.policy.apex.model.policymodel.concepts.AxStateTaskOutputType;
43 import org.onap.policy.apex.model.policymodel.concepts.AxStateTaskReference;
44 import org.onap.policy.apex.model.policymodel.concepts.AxTask;
45 import org.onap.policy.apex.model.policymodel.concepts.AxTaskLogic;
46 import org.onap.policy.apex.model.policymodel.concepts.AxTaskSelectionLogic;
47 import org.onap.policy.apex.model.policymodel.concepts.AxTasks;
48 import org.onap.policy.apex.model.policymodel.handling.PolicyLogicReader;
49
50 /**
51  * The Class AdaptiveDomainModelFactory.
52  */
53 public class AdaptiveDomainModelFactory {
54     // Recurring string constants
55     private static final String LAST_MONITORED_VALUE = "LastMonitoredValue";
56     private static final String TASK_SELECTION_LOGIC = "TaskSelectionLogic";
57     private static final String DEFAULT_STATE_LOGIC = "DefaultState_Logic";
58     private static final String TASK_LOGIC = "TaskLogic";
59     private static final String DECIDE = "Decide";
60     private static final String ESTABLISH = "Establish";
61     private static final String MATCH = "Match";
62     private static final String EXTERNAL = "External";
63     private static final String DEFAULT_NAMESPACE = "org.onap.policy.apex.examples.adaptive.events";
64     private static final String ITERATION2 = "Iteration";
65     private static final String DEFAULT_VERSION = "0.0.1";
66     private static final String MONITORED_VALUE = "MonitoredValue";
67
68     /**
69      * Gets the anomaly detection policy model.
70      *
71      * @return the anomaly detection policy model
72      */
73     // CHECKSTYLE:OFF: checkstyle:maximumMethodLength
74     public AxPolicyModel getAnomalyDetectionPolicyModel() {
75         // CHECKSTYLE:ON: checkstyle:maximumMethodLength
76         // Data types for event parameters
77         final AxContextSchema monitoredValue = new AxContextSchema(new AxArtifactKey(MONITORED_VALUE, DEFAULT_VERSION),
78                         "Java", "java.lang.Double");
79         final AxContextSchema iteration = new AxContextSchema(new AxArtifactKey(ITERATION2, DEFAULT_VERSION), "Java",
80                         "java.lang.Integer");
81
82         final AxContextSchemas adContextSchemas = new AxContextSchemas(
83                         new AxArtifactKey("AADMDatatypes", DEFAULT_VERSION));
84         adContextSchemas.getSchemasMap().put(monitoredValue.getKey(), monitoredValue);
85         adContextSchemas.getSchemasMap().put(iteration.getKey(), iteration);
86
87         final AxEvent anomalyDetectionTriggerEvent = new AxEvent(
88                         new AxArtifactKey("AnomalyDetectionTriggerEvent", DEFAULT_VERSION),
89                         DEFAULT_NAMESPACE);
90         anomalyDetectionTriggerEvent.setSource(EXTERNAL);
91         anomalyDetectionTriggerEvent.setTarget(MATCH);
92         anomalyDetectionTriggerEvent.getParameterMap().put(MONITORED_VALUE,
93                         new AxField(new AxReferenceKey(anomalyDetectionTriggerEvent.getKey(), MONITORED_VALUE),
94                                         monitoredValue.getKey()));
95         anomalyDetectionTriggerEvent.getParameterMap().put(ITERATION2, new AxField(
96                         new AxReferenceKey(anomalyDetectionTriggerEvent.getKey(), ITERATION2), iteration.getKey()));
97
98         final AxEvent anomalyDetectionMatchEvent = new AxEvent(
99                         new AxArtifactKey("AnomalyDetectionMatchEvent", DEFAULT_VERSION),
100                         DEFAULT_NAMESPACE);
101         anomalyDetectionMatchEvent.setSource(MATCH);
102         anomalyDetectionMatchEvent.setTarget(ESTABLISH);
103         anomalyDetectionMatchEvent.getParameterMap().put(MONITORED_VALUE,
104                         new AxField(new AxReferenceKey(anomalyDetectionMatchEvent.getKey(), MONITORED_VALUE),
105                                         monitoredValue.getKey()));
106         anomalyDetectionMatchEvent.getParameterMap().put(ITERATION2, new AxField(
107                         new AxReferenceKey(anomalyDetectionMatchEvent.getKey(), ITERATION2), iteration.getKey()));
108
109         final AxEvent anomalyDetectionEstablishEvent = new AxEvent(
110                         new AxArtifactKey("AnomalyDetectionEstablishEvent", DEFAULT_VERSION),
111                         DEFAULT_NAMESPACE);
112         anomalyDetectionEstablishEvent.setSource(ESTABLISH);
113         anomalyDetectionEstablishEvent.setTarget(DECIDE);
114         anomalyDetectionEstablishEvent.getParameterMap().put(MONITORED_VALUE,
115                         new AxField(new AxReferenceKey(anomalyDetectionEstablishEvent.getKey(), MONITORED_VALUE),
116                                         monitoredValue.getKey()));
117         anomalyDetectionEstablishEvent.getParameterMap().put(ITERATION2, new AxField(
118                         new AxReferenceKey(anomalyDetectionEstablishEvent.getKey(), ITERATION2), iteration.getKey()));
119
120         final AxEvent anomalyDetectionDecideEvent = new AxEvent(
121                         new AxArtifactKey("AnomalyDetectionDecideEvent", DEFAULT_VERSION),
122                         DEFAULT_NAMESPACE);
123         anomalyDetectionDecideEvent.setSource(DECIDE);
124         anomalyDetectionDecideEvent.setTarget("Act");
125         anomalyDetectionDecideEvent.getParameterMap().put(MONITORED_VALUE,
126                         new AxField(new AxReferenceKey(anomalyDetectionDecideEvent.getKey(), MONITORED_VALUE),
127                                         monitoredValue.getKey()));
128         anomalyDetectionDecideEvent.getParameterMap().put(ITERATION2, new AxField(
129                         new AxReferenceKey(anomalyDetectionDecideEvent.getKey(), ITERATION2), iteration.getKey()));
130
131         final AxEvent anomalyDetectionActEvent = new AxEvent(
132                         new AxArtifactKey("AnomalyDetectionActEvent", DEFAULT_VERSION),
133                         DEFAULT_NAMESPACE);
134         anomalyDetectionActEvent.setSource("Act");
135         anomalyDetectionActEvent.setTarget(EXTERNAL);
136         anomalyDetectionActEvent.getParameterMap().put(MONITORED_VALUE,
137                         new AxField(new AxReferenceKey(anomalyDetectionActEvent.getKey(), MONITORED_VALUE),
138                                         monitoredValue.getKey()));
139         anomalyDetectionActEvent.getParameterMap().put(ITERATION2, new AxField(
140                         new AxReferenceKey(anomalyDetectionActEvent.getKey(), ITERATION2), iteration.getKey()));
141
142         final AxEvents anomalyDetectionEvents = new AxEvents(
143                         new AxArtifactKey("AnomalyDetectionEvents", DEFAULT_VERSION));
144         anomalyDetectionEvents.getEventMap().put(anomalyDetectionTriggerEvent.getKey(), anomalyDetectionTriggerEvent);
145         anomalyDetectionEvents.getEventMap().put(anomalyDetectionMatchEvent.getKey(), anomalyDetectionMatchEvent);
146         anomalyDetectionEvents.getEventMap().put(anomalyDetectionEstablishEvent.getKey(),
147                         anomalyDetectionEstablishEvent);
148         anomalyDetectionEvents.getEventMap().put(anomalyDetectionDecideEvent.getKey(), anomalyDetectionDecideEvent);
149         anomalyDetectionEvents.getEventMap().put(anomalyDetectionActEvent.getKey(), anomalyDetectionActEvent);
150
151         // Data types for context
152         final AxContextSchema anomalyDetection = new AxContextSchema(
153                         new AxArtifactKey("AnomalyDetection", DEFAULT_VERSION), "Java",
154                         "org.onap.policy.apex.examples.adaptive.concepts.AnomalyDetection");
155         adContextSchemas.getSchemasMap().put(anomalyDetection.getKey(), anomalyDetection);
156
157         // One context map
158         final AxContextAlbum anomalyDetectionAlbum = new AxContextAlbum(
159                         new AxArtifactKey("AnomalyDetectionAlbum", DEFAULT_VERSION), "APPLICATION", true,
160                         anomalyDetection.getKey());
161         final AxContextAlbums anomalyDetectionAlbums = new AxContextAlbums(
162                         new AxArtifactKey("AnomalyDetectionAlbums", DEFAULT_VERSION));
163         anomalyDetectionAlbums.getAlbumsMap().put(anomalyDetectionAlbum.getKey(), anomalyDetectionAlbum);
164
165         // Tasks
166         final AxLogicReader logicReader = new PolicyLogicReader()
167                         .setLogicPackage(this.getClass().getPackage().getName())
168                         .setDefaultLogic("DefaultAnomalyDetectionTask_Logic");
169
170         final AxTask anomalyDetectionMatchTask = new AxTask(
171                         new AxArtifactKey("AnomalyDetectionMatchTask", DEFAULT_VERSION));
172         anomalyDetectionMatchTask.setTaskLogic(
173                         new AxTaskLogic(anomalyDetectionMatchTask.getKey(), TASK_LOGIC, "MVEL", logicReader));
174
175         final AxTask anomalyDetectionEstablishTask = new AxTask(
176                         new AxArtifactKey("AnomalyDetectionEstablishTask", DEFAULT_VERSION));
177         anomalyDetectionEstablishTask.setTaskLogic(
178                         new AxTaskLogic(anomalyDetectionEstablishTask.getKey(), TASK_LOGIC, "MVEL", logicReader));
179
180         final AxTask anomalyDetectionDecideTask0 = new AxTask(
181                         new AxArtifactKey("AnomalyDetectionDecideTask0", DEFAULT_VERSION));
182         anomalyDetectionDecideTask0.setTaskLogic(
183                         new AxTaskLogic(anomalyDetectionDecideTask0.getKey(), TASK_LOGIC, "MVEL", logicReader));
184
185         final AxTask anomalyDetectionDecideTask1 = new AxTask(
186                         new AxArtifactKey("AnomalyDetectionDecideTask1", DEFAULT_VERSION));
187         anomalyDetectionDecideTask1.setTaskLogic(
188                         new AxTaskLogic(anomalyDetectionDecideTask1.getKey(), TASK_LOGIC, "MVEL", logicReader));
189
190         final AxTask anomalyDetectionDecideTask2 = new AxTask(
191                         new AxArtifactKey("AnomalyDetectionDecideTask2", DEFAULT_VERSION));
192         anomalyDetectionDecideTask2.setTaskLogic(
193                         new AxTaskLogic(anomalyDetectionDecideTask2.getKey(), TASK_LOGIC, "MVEL", logicReader));
194
195         final AxTask anomalyDetectionActTask = new AxTask(
196                         new AxArtifactKey("AnomalyDetectionActTask", DEFAULT_VERSION));
197         anomalyDetectionActTask.setTaskLogic(
198                         new AxTaskLogic(anomalyDetectionActTask.getKey(), TASK_LOGIC, "MVEL", logicReader));
199
200         final AxTasks anomalyDetectionTasks = new AxTasks(new AxArtifactKey("AnomalyDetectionTasks", DEFAULT_VERSION));
201         anomalyDetectionTasks.getTaskMap().put(anomalyDetectionMatchTask.getKey(), anomalyDetectionMatchTask);
202         anomalyDetectionTasks.getTaskMap().put(anomalyDetectionEstablishTask.getKey(), anomalyDetectionEstablishTask);
203         anomalyDetectionTasks.getTaskMap().put(anomalyDetectionDecideTask0.getKey(), anomalyDetectionDecideTask0);
204         anomalyDetectionTasks.getTaskMap().put(anomalyDetectionDecideTask1.getKey(), anomalyDetectionDecideTask1);
205         anomalyDetectionTasks.getTaskMap().put(anomalyDetectionDecideTask2.getKey(), anomalyDetectionDecideTask2);
206         anomalyDetectionTasks.getTaskMap().put(anomalyDetectionActTask.getKey(), anomalyDetectionActTask);
207
208         // Policies
209         logicReader.setDefaultLogic(DEFAULT_STATE_LOGIC);
210
211         final AxPolicy anomalyDetectionPolicy = new AxPolicy(
212                         new AxArtifactKey("AnomalyDetectionPolicy", DEFAULT_VERSION));
213         anomalyDetectionPolicy.setTemplate("MEDA");
214
215         final AxState anomalyDetectionActState = new AxState(
216                         new AxReferenceKey(anomalyDetectionPolicy.getKey(), "Act"));
217         anomalyDetectionActState.setTrigger(anomalyDetectionDecideEvent.getKey());
218         final AxStateOutput adAct2Out = new AxStateOutput(anomalyDetectionActState.getKey(),
219                         AxReferenceKey.getNullKey(), anomalyDetectionActEvent.getKey());
220         anomalyDetectionActState.getStateOutputs().put(adAct2Out.getKey().getLocalName(), adAct2Out);
221         anomalyDetectionActState.setTaskSelectionLogic(new AxTaskSelectionLogic(anomalyDetectionActState.getKey(),
222                         TASK_SELECTION_LOGIC, "MVEL", logicReader));
223         anomalyDetectionActState.setDefaultTask(anomalyDetectionActTask.getKey());
224         anomalyDetectionActState.getTaskReferences().put(anomalyDetectionActTask.getKey(),
225                         new AxStateTaskReference(anomalyDetectionActState.getKey(), anomalyDetectionActTask.getKey(),
226                                         AxStateTaskOutputType.DIRECT, adAct2Out.getKey()));
227
228         logicReader.setDefaultLogic(null);
229
230         final AxState anomalyDetectionDecideState = new AxState(
231                         new AxReferenceKey(anomalyDetectionPolicy.getKey(), DECIDE));
232         anomalyDetectionDecideState.setTrigger(anomalyDetectionEstablishEvent.getKey());
233         final AxStateOutput adDec2Act = new AxStateOutput(anomalyDetectionDecideState.getKey(),
234                         anomalyDetectionActState.getKey(), anomalyDetectionDecideEvent.getKey());
235         anomalyDetectionDecideState.getStateOutputs().put(adDec2Act.getKey().getLocalName(), adDec2Act);
236         anomalyDetectionDecideState.setTaskSelectionLogic(new AxTaskSelectionLogic(anomalyDetectionDecideState.getKey(),
237                         TASK_SELECTION_LOGIC, "JAVA", logicReader));
238         anomalyDetectionDecideState.setDefaultTask(anomalyDetectionDecideTask0.getKey());
239         anomalyDetectionDecideState.getContextAlbumReferences().add(anomalyDetectionAlbum.getKey());
240         anomalyDetectionDecideState.getTaskReferences().put(anomalyDetectionDecideTask0.getKey(),
241                         new AxStateTaskReference(anomalyDetectionDecideState.getKey(),
242                                         anomalyDetectionDecideTask0.getKey(), AxStateTaskOutputType.DIRECT,
243                                         adDec2Act.getKey()));
244         anomalyDetectionDecideState.getTaskReferences().put(anomalyDetectionDecideTask1.getKey(),
245                         new AxStateTaskReference(anomalyDetectionDecideState.getKey(),
246                                         anomalyDetectionDecideTask1.getKey(), AxStateTaskOutputType.DIRECT,
247                                         adDec2Act.getKey()));
248         anomalyDetectionDecideState.getTaskReferences().put(anomalyDetectionDecideTask2.getKey(),
249                         new AxStateTaskReference(anomalyDetectionDecideState.getKey(),
250                                         anomalyDetectionDecideTask2.getKey(), AxStateTaskOutputType.DIRECT,
251                                         adDec2Act.getKey()));
252
253         logicReader.setDefaultLogic(DEFAULT_STATE_LOGIC);
254
255         final AxState anomalyDetectionEstablishState = new AxState(
256                         new AxReferenceKey(anomalyDetectionPolicy.getKey(), ESTABLISH));
257         anomalyDetectionEstablishState.setTrigger(anomalyDetectionMatchEvent.getKey());
258         final AxStateOutput adEst2Dec = new AxStateOutput(anomalyDetectionEstablishState.getKey(),
259                         anomalyDetectionDecideState.getKey(), anomalyDetectionEstablishEvent.getKey());
260         anomalyDetectionEstablishState.getStateOutputs().put(adEst2Dec.getKey().getLocalName(), adEst2Dec);
261         anomalyDetectionEstablishState.setTaskSelectionLogic(new AxTaskSelectionLogic(
262                         anomalyDetectionEstablishState.getKey(), TASK_SELECTION_LOGIC, "MVEL", logicReader));
263         anomalyDetectionEstablishState.setDefaultTask(anomalyDetectionEstablishTask.getKey());
264         anomalyDetectionEstablishState.getTaskReferences().put(anomalyDetectionEstablishTask.getKey(),
265                         new AxStateTaskReference(anomalyDetectionEstablishState.getKey(),
266                                         anomalyDetectionEstablishTask.getKey(), AxStateTaskOutputType.DIRECT,
267                                         adEst2Dec.getKey()));
268
269         final AxState anomalyDetectionMatchState = new AxState(
270                         new AxReferenceKey(anomalyDetectionPolicy.getKey(), MATCH));
271         anomalyDetectionMatchState.setTrigger(anomalyDetectionTriggerEvent.getKey());
272         final AxStateOutput adMat2Est = new AxStateOutput(anomalyDetectionMatchState.getKey(),
273                         anomalyDetectionEstablishState.getKey(), anomalyDetectionMatchEvent.getKey());
274         anomalyDetectionMatchState.getStateOutputs().put(adMat2Est.getKey().getLocalName(), adMat2Est);
275         anomalyDetectionMatchState.setTaskSelectionLogic(new AxTaskSelectionLogic(anomalyDetectionMatchState.getKey(),
276                         TASK_SELECTION_LOGIC, "MVEL", logicReader));
277         anomalyDetectionMatchState.setDefaultTask(anomalyDetectionMatchTask.getKey());
278         anomalyDetectionMatchState.getTaskReferences().put(anomalyDetectionMatchTask.getKey(),
279                         new AxStateTaskReference(anomalyDetectionMatchState.getKey(),
280                                         anomalyDetectionMatchTask.getKey(), AxStateTaskOutputType.DIRECT,
281                                         adMat2Est.getKey()));
282
283         anomalyDetectionPolicy.setFirstState(anomalyDetectionMatchState.getKey().getLocalName());
284         anomalyDetectionPolicy.getStateMap().put(anomalyDetectionMatchState.getKey().getLocalName(),
285                         anomalyDetectionMatchState);
286         anomalyDetectionPolicy.getStateMap().put(anomalyDetectionEstablishState.getKey().getLocalName(),
287                         anomalyDetectionEstablishState);
288         anomalyDetectionPolicy.getStateMap().put(anomalyDetectionDecideState.getKey().getLocalName(),
289                         anomalyDetectionDecideState);
290         anomalyDetectionPolicy.getStateMap().put(anomalyDetectionActState.getKey().getLocalName(),
291                         anomalyDetectionActState);
292
293         final AxPolicies anomalyDetectionPolicies = new AxPolicies(
294                         new AxArtifactKey("AnomalyDetectionPolicies", DEFAULT_VERSION));
295         anomalyDetectionPolicies.getPolicyMap().put(anomalyDetectionPolicy.getKey(), anomalyDetectionPolicy);
296
297         final AxKeyInformation keyInformation = new AxKeyInformation(
298                         new AxArtifactKey("AnomalyDetectionKeyInformation", DEFAULT_VERSION));
299         final AxPolicyModel anomalyDetectionPolicyModel = new AxPolicyModel(
300                         new AxArtifactKey("AnomalyDetectionPolicyModel", DEFAULT_VERSION));
301         anomalyDetectionPolicyModel.setPolicies(anomalyDetectionPolicies);
302         anomalyDetectionPolicyModel.setEvents(anomalyDetectionEvents);
303         anomalyDetectionPolicyModel.setTasks(anomalyDetectionTasks);
304         anomalyDetectionPolicyModel.setAlbums(anomalyDetectionAlbums);
305         anomalyDetectionPolicyModel.setSchemas(adContextSchemas);
306         anomalyDetectionPolicyModel.setKeyInformation(keyInformation);
307         anomalyDetectionPolicyModel.getKeyInformation().generateKeyInfo(anomalyDetectionPolicyModel);
308
309         final AxValidationResult result = anomalyDetectionPolicyModel.validate(new AxValidationResult());
310         if (!result.getValidationResult().equals(AxValidationResult.ValidationResult.VALID)) {
311             throw new ApexRuntimeException("model " + anomalyDetectionPolicyModel.getId() + " is not valid" + result);
312         }
313         return anomalyDetectionPolicyModel;
314     }
315
316     /**
317      * Gets the auto learn policy model.
318      *
319      * @return the auto learn policy model
320      */
321     // CHECKSTYLE:OFF: checkstyle:maximumMethodLength
322     public AxPolicyModel getAutoLearnPolicyModel() {
323         // CHECKSTYLE:ON: checkstyle:maximumMethodLength
324         // Data types for event parameters
325         final AxContextSchema monitoredValue = new AxContextSchema(new AxArtifactKey(MONITORED_VALUE, DEFAULT_VERSION),
326                         "Java", "java.lang.Double");
327
328         final AxContextSchemas alContextSchemas = new AxContextSchemas(
329                         new AxArtifactKey("ALDatatypes", DEFAULT_VERSION));
330         alContextSchemas.getSchemasMap().put(monitoredValue.getKey(), monitoredValue);
331
332         final AxEvent autoLearnTriggerEvent = new AxEvent(new AxArtifactKey("AutoLearnTriggerEvent", DEFAULT_VERSION),
333                         DEFAULT_NAMESPACE);
334         autoLearnTriggerEvent.setSource(EXTERNAL);
335         autoLearnTriggerEvent.setTarget(MATCH);
336         autoLearnTriggerEvent.getParameterMap().put(MONITORED_VALUE, new AxField(
337                         new AxReferenceKey(autoLearnTriggerEvent.getKey(), MONITORED_VALUE), monitoredValue.getKey()));
338         autoLearnTriggerEvent.getParameterMap().put(LAST_MONITORED_VALUE,
339                         new AxField(new AxReferenceKey(autoLearnTriggerEvent.getKey(), LAST_MONITORED_VALUE),
340                                         monitoredValue.getKey()));
341
342         final AxEvent autoLearnMatchEvent = new AxEvent(new AxArtifactKey("AutoLearnMatchEvent", DEFAULT_VERSION),
343                         DEFAULT_NAMESPACE);
344         autoLearnMatchEvent.setSource(MATCH);
345         autoLearnMatchEvent.setTarget(ESTABLISH);
346         autoLearnMatchEvent.getParameterMap().put(MONITORED_VALUE, new AxField(
347                         new AxReferenceKey(autoLearnMatchEvent.getKey(), MONITORED_VALUE), monitoredValue.getKey()));
348         autoLearnMatchEvent.getParameterMap().put(LAST_MONITORED_VALUE,
349                         new AxField(new AxReferenceKey(autoLearnMatchEvent.getKey(), LAST_MONITORED_VALUE),
350                                         monitoredValue.getKey()));
351
352         final AxEvent autoLearnEstablishEvent = new AxEvent(
353                         new AxArtifactKey("AutoLearnEstablishEvent", DEFAULT_VERSION),
354                         DEFAULT_NAMESPACE);
355         autoLearnEstablishEvent.setSource(ESTABLISH);
356         autoLearnEstablishEvent.setTarget(DECIDE);
357         autoLearnEstablishEvent.getParameterMap().put(MONITORED_VALUE,
358                         new AxField(new AxReferenceKey(autoLearnEstablishEvent.getKey(), MONITORED_VALUE),
359                                         monitoredValue.getKey()));
360         autoLearnEstablishEvent.getParameterMap().put(LAST_MONITORED_VALUE,
361                         new AxField(new AxReferenceKey(autoLearnEstablishEvent.getKey(), LAST_MONITORED_VALUE),
362                                         monitoredValue.getKey()));
363
364         final AxEvent autoLearnDecideEvent = new AxEvent(new AxArtifactKey("AutoLearnDecideEvent", DEFAULT_VERSION),
365                         DEFAULT_NAMESPACE);
366         autoLearnDecideEvent.setSource(DECIDE);
367         autoLearnDecideEvent.setTarget("Act");
368         autoLearnDecideEvent.getParameterMap().put(MONITORED_VALUE, new AxField(
369                         new AxReferenceKey(autoLearnDecideEvent.getKey(), MONITORED_VALUE), monitoredValue.getKey()));
370         autoLearnDecideEvent.getParameterMap().put(LAST_MONITORED_VALUE,
371                         new AxField(new AxReferenceKey(autoLearnDecideEvent.getKey(), LAST_MONITORED_VALUE),
372                                         monitoredValue.getKey()));
373
374         final AxEvent autoLearnActEvent = new AxEvent(new AxArtifactKey("AutoLearnActEvent", DEFAULT_VERSION),
375                         DEFAULT_NAMESPACE);
376         autoLearnActEvent.setSource("Act");
377         autoLearnActEvent.setTarget(EXTERNAL);
378         autoLearnActEvent.getParameterMap().put(MONITORED_VALUE, new AxField(
379                         new AxReferenceKey(autoLearnActEvent.getKey(), MONITORED_VALUE), monitoredValue.getKey()));
380         autoLearnActEvent.getParameterMap().put(LAST_MONITORED_VALUE, new AxField(
381                         new AxReferenceKey(autoLearnActEvent.getKey(), LAST_MONITORED_VALUE), monitoredValue.getKey()));
382
383         final AxEvents autoLearnEvents = new AxEvents(new AxArtifactKey("AutoLearnEvents", DEFAULT_VERSION));
384         autoLearnEvents.getEventMap().put(autoLearnTriggerEvent.getKey(), autoLearnTriggerEvent);
385         autoLearnEvents.getEventMap().put(autoLearnMatchEvent.getKey(), autoLearnMatchEvent);
386         autoLearnEvents.getEventMap().put(autoLearnEstablishEvent.getKey(), autoLearnEstablishEvent);
387         autoLearnEvents.getEventMap().put(autoLearnDecideEvent.getKey(), autoLearnDecideEvent);
388         autoLearnEvents.getEventMap().put(autoLearnActEvent.getKey(), autoLearnActEvent);
389
390         // Data types for context
391         final AxContextSchema autoLearn = new AxContextSchema(new AxArtifactKey("AutoLearn", DEFAULT_VERSION), "Java",
392                         "org.onap.policy.apex.examples.adaptive.concepts.AutoLearn");
393         alContextSchemas.getSchemasMap().put(autoLearn.getKey(), autoLearn);
394
395         // One context map
396         final AxContextAlbum autoLearnAlbum = new AxContextAlbum(new AxArtifactKey("AutoLearnAlbum", DEFAULT_VERSION),
397                         "APPLICATION", true, autoLearn.getKey());
398
399         final AxContextAlbums autoLearnAlbums = new AxContextAlbums(
400                         new AxArtifactKey("AutoLearnContext", DEFAULT_VERSION));
401         autoLearnAlbums.getAlbumsMap().put(autoLearnAlbum.getKey(), autoLearnAlbum);
402
403         // Tasks
404         final AxLogicReader logicReader = new PolicyLogicReader()
405                         .setLogicPackage(this.getClass().getPackage().getName())
406                         .setDefaultLogic("DefaultAutoLearnTask_Logic");
407
408         final AxTask autoLearnMatchTask = new AxTask(new AxArtifactKey("AutoLearnMatchTask", DEFAULT_VERSION));
409         autoLearnMatchTask.setTaskLogic(new AxTaskLogic(autoLearnMatchTask.getKey(), TASK_LOGIC, "MVEL", logicReader));
410
411         final AxTask autoLearnEstablishTask = new AxTask(new AxArtifactKey("AutoLearnEstablishTask", DEFAULT_VERSION));
412         autoLearnEstablishTask.setTaskLogic(
413                         new AxTaskLogic(autoLearnEstablishTask.getKey(), TASK_LOGIC, "MVEL", logicReader));
414
415         logicReader.setDefaultLogic(null);
416
417         final AxTask autoLearnDecideTask0 = new AxTask(new AxArtifactKey("AutoLearnDecideTask0", DEFAULT_VERSION));
418         autoLearnDecideTask0
419                         .setTaskLogic(new AxTaskLogic(autoLearnDecideTask0.getKey(), TASK_LOGIC, "MVEL", logicReader));
420
421         final AxTask autoLearnDecideTask1 = new AxTask(new AxArtifactKey("AutoLearnDecideTask1", DEFAULT_VERSION));
422         autoLearnDecideTask1
423                         .setTaskLogic(new AxTaskLogic(autoLearnDecideTask1.getKey(), TASK_LOGIC, "MVEL", logicReader));
424
425         final AxTask autoLearnDecideTask2 = new AxTask(new AxArtifactKey("AutoLearnDecideTask2", DEFAULT_VERSION));
426         autoLearnDecideTask2
427                         .setTaskLogic(new AxTaskLogic(autoLearnDecideTask2.getKey(), TASK_LOGIC, "MVEL", logicReader));
428
429         final AxTask autoLearnDecideTask3 = new AxTask(new AxArtifactKey("AutoLearnDecideTask3", DEFAULT_VERSION));
430         autoLearnDecideTask3
431                         .setTaskLogic(new AxTaskLogic(autoLearnDecideTask3.getKey(), TASK_LOGIC, "MVEL", logicReader));
432
433         final AxTask autoLearnDecideTask4 = new AxTask(new AxArtifactKey("AutoLearnDecideTask4", DEFAULT_VERSION));
434         autoLearnDecideTask4
435                         .setTaskLogic(new AxTaskLogic(autoLearnDecideTask4.getKey(), TASK_LOGIC, "MVEL", logicReader));
436
437         final AxTask autoLearnDecideTask5 = new AxTask(new AxArtifactKey("AutoLearnDecideTask5", DEFAULT_VERSION));
438         autoLearnDecideTask5
439                         .setTaskLogic(new AxTaskLogic(autoLearnDecideTask5.getKey(), TASK_LOGIC, "MVEL", logicReader));
440
441         final AxTask autoLearnDecideTask6 = new AxTask(new AxArtifactKey("AutoLearnDecideTask6", DEFAULT_VERSION));
442         autoLearnDecideTask6
443                         .setTaskLogic(new AxTaskLogic(autoLearnDecideTask6.getKey(), TASK_LOGIC, "MVEL", logicReader));
444
445         logicReader.setDefaultLogic("DefaultAutoLearnTask_Logic");
446
447         final AxTask autoLearnActTask = new AxTask(new AxArtifactKey("AutoLearnActTask", DEFAULT_VERSION));
448         autoLearnActTask.setTaskLogic(new AxTaskLogic(autoLearnActTask.getKey(), TASK_LOGIC, "MVEL", logicReader));
449
450         final AxTasks autoLearnTasks = new AxTasks(new AxArtifactKey("AutoLearnTasks", DEFAULT_VERSION));
451         autoLearnTasks.getTaskMap().put(autoLearnMatchTask.getKey(), autoLearnMatchTask);
452         autoLearnTasks.getTaskMap().put(autoLearnEstablishTask.getKey(), autoLearnEstablishTask);
453         autoLearnTasks.getTaskMap().put(autoLearnDecideTask0.getKey(), autoLearnDecideTask0);
454         autoLearnTasks.getTaskMap().put(autoLearnDecideTask1.getKey(), autoLearnDecideTask1);
455         autoLearnTasks.getTaskMap().put(autoLearnDecideTask2.getKey(), autoLearnDecideTask2);
456         autoLearnTasks.getTaskMap().put(autoLearnDecideTask3.getKey(), autoLearnDecideTask3);
457         autoLearnTasks.getTaskMap().put(autoLearnDecideTask4.getKey(), autoLearnDecideTask4);
458         autoLearnTasks.getTaskMap().put(autoLearnDecideTask5.getKey(), autoLearnDecideTask5);
459         autoLearnTasks.getTaskMap().put(autoLearnDecideTask6.getKey(), autoLearnDecideTask6);
460         autoLearnTasks.getTaskMap().put(autoLearnActTask.getKey(), autoLearnActTask);
461
462         // Policies
463         logicReader.setDefaultLogic(DEFAULT_STATE_LOGIC);
464
465         final AxPolicy autoLearnPolicy = new AxPolicy(new AxArtifactKey("AutoLearnPolicy", DEFAULT_VERSION));
466         autoLearnPolicy.setTemplate("MEDA");
467
468         final AxState autoLearnActState = new AxState(new AxReferenceKey(autoLearnPolicy.getKey(), "Act"));
469         autoLearnActState.setTrigger(autoLearnDecideEvent.getKey());
470         final AxStateOutput alAct2Out = new AxStateOutput(autoLearnActState.getKey(), AxReferenceKey.getNullKey(),
471                         autoLearnActEvent.getKey());
472         autoLearnActState.getStateOutputs().put(alAct2Out.getKey().getLocalName(), alAct2Out);
473         autoLearnActState.setTaskSelectionLogic(new AxTaskSelectionLogic(autoLearnActState.getKey(),
474                         TASK_SELECTION_LOGIC, "MVEL", logicReader));
475         autoLearnActState.setDefaultTask(autoLearnActTask.getKey());
476         autoLearnActState.getTaskReferences().put(autoLearnActTask.getKey(),
477                         new AxStateTaskReference(autoLearnActState.getKey(), autoLearnActTask.getKey(),
478                                         AxStateTaskOutputType.DIRECT, alAct2Out.getKey()));
479
480         logicReader.setDefaultLogic(null);
481
482         final AxState autoLearnDecideState = new AxState(new AxReferenceKey(autoLearnPolicy.getKey(), DECIDE));
483         autoLearnDecideState.setTrigger(autoLearnEstablishEvent.getKey());
484         final AxStateOutput alDec2Act = new AxStateOutput(autoLearnDecideState.getKey(), autoLearnActState.getKey(),
485                         autoLearnDecideEvent.getKey());
486         autoLearnDecideState.getStateOutputs().put(alDec2Act.getKey().getLocalName(), alDec2Act);
487         autoLearnDecideState.getContextAlbumReferences().add(autoLearnAlbum.getKey());
488         autoLearnDecideState.setTaskSelectionLogic(new AxTaskSelectionLogic(autoLearnDecideState.getKey(),
489                         TASK_SELECTION_LOGIC, "JAVA", logicReader));
490         autoLearnDecideState.setDefaultTask(autoLearnDecideTask0.getKey());
491         autoLearnDecideState.getTaskReferences().put(autoLearnDecideTask0.getKey(),
492                         new AxStateTaskReference(autoLearnDecideState.getKey(), autoLearnDecideTask0.getKey(),
493                                         AxStateTaskOutputType.DIRECT, alDec2Act.getKey()));
494         autoLearnDecideState.getTaskReferences().put(autoLearnDecideTask1.getKey(),
495                         new AxStateTaskReference(autoLearnDecideState.getKey(), autoLearnDecideTask1.getKey(),
496                                         AxStateTaskOutputType.DIRECT, alDec2Act.getKey()));
497         autoLearnDecideState.getTaskReferences().put(autoLearnDecideTask2.getKey(),
498                         new AxStateTaskReference(autoLearnDecideState.getKey(), autoLearnDecideTask2.getKey(),
499                                         AxStateTaskOutputType.DIRECT, alDec2Act.getKey()));
500         autoLearnDecideState.getTaskReferences().put(autoLearnDecideTask3.getKey(),
501                         new AxStateTaskReference(autoLearnDecideState.getKey(), autoLearnDecideTask3.getKey(),
502                                         AxStateTaskOutputType.DIRECT, alDec2Act.getKey()));
503         autoLearnDecideState.getTaskReferences().put(autoLearnDecideTask4.getKey(),
504                         new AxStateTaskReference(autoLearnDecideState.getKey(), autoLearnDecideTask4.getKey(),
505                                         AxStateTaskOutputType.DIRECT, alDec2Act.getKey()));
506         autoLearnDecideState.getTaskReferences().put(autoLearnDecideTask5.getKey(),
507                         new AxStateTaskReference(autoLearnDecideState.getKey(), autoLearnDecideTask5.getKey(),
508                                         AxStateTaskOutputType.DIRECT, alDec2Act.getKey()));
509         autoLearnDecideState.getTaskReferences().put(autoLearnDecideTask6.getKey(),
510                         new AxStateTaskReference(autoLearnDecideState.getKey(), autoLearnDecideTask6.getKey(),
511                                         AxStateTaskOutputType.DIRECT, alDec2Act.getKey()));
512
513         logicReader.setDefaultLogic(DEFAULT_STATE_LOGIC);
514
515         final AxState autoLearnEstablishState = new AxState(new AxReferenceKey(autoLearnPolicy.getKey(), ESTABLISH));
516         autoLearnEstablishState.setTrigger(autoLearnMatchEvent.getKey());
517         final AxStateOutput alEst2Dec = new AxStateOutput(autoLearnEstablishState.getKey(),
518                         autoLearnDecideState.getKey(), autoLearnEstablishEvent.getKey());
519         autoLearnEstablishState.getStateOutputs().put(alEst2Dec.getKey().getLocalName(), alEst2Dec);
520         autoLearnEstablishState.setTaskSelectionLogic(new AxTaskSelectionLogic(autoLearnEstablishState.getKey(),
521                         TASK_SELECTION_LOGIC, "MVEL", logicReader));
522         autoLearnEstablishState.setDefaultTask(autoLearnEstablishTask.getKey());
523         autoLearnEstablishState.getTaskReferences().put(autoLearnEstablishTask.getKey(),
524                         new AxStateTaskReference(autoLearnEstablishState.getKey(), autoLearnEstablishTask.getKey(),
525                                         AxStateTaskOutputType.DIRECT, alEst2Dec.getKey()));
526
527         final AxState autoLearnMatchState = new AxState(new AxReferenceKey(autoLearnPolicy.getKey(), MATCH));
528         autoLearnMatchState.setTrigger(autoLearnTriggerEvent.getKey());
529         final AxStateOutput alMat2Est = new AxStateOutput(autoLearnMatchState.getKey(),
530                         autoLearnEstablishState.getKey(), autoLearnMatchEvent.getKey());
531         autoLearnMatchState.getStateOutputs().put(alMat2Est.getKey().getLocalName(), alMat2Est);
532         autoLearnMatchState.setTaskSelectionLogic(new AxTaskSelectionLogic(autoLearnMatchState.getKey(),
533                         TASK_SELECTION_LOGIC, "MVEL", logicReader));
534         autoLearnMatchState.setDefaultTask(autoLearnMatchTask.getKey());
535         autoLearnMatchState.getTaskReferences().put(autoLearnMatchTask.getKey(),
536                         new AxStateTaskReference(autoLearnMatchState.getKey(), autoLearnMatchTask.getKey(),
537                                         AxStateTaskOutputType.DIRECT, alMat2Est.getKey()));
538
539         autoLearnPolicy.setFirstState(autoLearnMatchState.getKey().getLocalName());
540         autoLearnPolicy.getStateMap().put(autoLearnMatchState.getKey().getLocalName(), autoLearnMatchState);
541         autoLearnPolicy.getStateMap().put(autoLearnEstablishState.getKey().getLocalName(), autoLearnEstablishState);
542         autoLearnPolicy.getStateMap().put(autoLearnDecideState.getKey().getLocalName(), autoLearnDecideState);
543         autoLearnPolicy.getStateMap().put(autoLearnActState.getKey().getLocalName(), autoLearnActState);
544
545         final AxPolicies autoLearnPolicies = new AxPolicies(new AxArtifactKey("AutoLearnPolicies", DEFAULT_VERSION));
546         autoLearnPolicies.getPolicyMap().put(autoLearnPolicy.getKey(), autoLearnPolicy);
547
548         final AxKeyInformation keyInformation = new AxKeyInformation(
549                         new AxArtifactKey("AutoLearnKeyInformation", DEFAULT_VERSION));
550         final AxPolicyModel autoLearnPolicyModel = new AxPolicyModel(
551                         new AxArtifactKey("AutoLearnPolicyModel", DEFAULT_VERSION));
552         autoLearnPolicyModel.setPolicies(autoLearnPolicies);
553         autoLearnPolicyModel.setEvents(autoLearnEvents);
554         autoLearnPolicyModel.setTasks(autoLearnTasks);
555         autoLearnPolicyModel.setAlbums(autoLearnAlbums);
556         autoLearnPolicyModel.setSchemas(alContextSchemas);
557         autoLearnPolicyModel.setKeyInformation(keyInformation);
558         autoLearnPolicyModel.getKeyInformation().generateKeyInfo(autoLearnPolicyModel);
559
560         final AxValidationResult result = autoLearnPolicyModel.validate(new AxValidationResult());
561         if (!result.getValidationResult().equals(AxValidationResult.ValidationResult.VALID)) {
562             throw new ApexRuntimeException("model " + autoLearnPolicyModel.getId() + " is not valid" + result);
563         }
564         return autoLearnPolicyModel;
565     }
566
567 }