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