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