8df43f99fc7ffc585a61461fcf1531ed2f1630f4
[policy/apex-pdp.git] / model / model-api / src / main / java / org / onap / policy / apex / model / modelapi / impl / EventFacade.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019 Nordix Foundation.
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.model.modelapi.impl;
23
24 import java.util.Properties;
25 import java.util.Set;
26
27 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
28 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
29 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
30 import org.onap.policy.apex.model.basicmodel.handling.ApexModelStringWriter;
31 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
32 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
33 import org.onap.policy.apex.model.eventmodel.concepts.AxField;
34 import org.onap.policy.apex.model.modelapi.ApexApiResult;
35 import org.onap.policy.apex.model.modelapi.ApexModel;
36 import org.onap.policy.common.utils.validation.Assertions;
37
38 /**
39  * This class acts as a facade for operations towards a policy model for event operations
40  * operations.
41  *
42  * @author Liam Fallon (liam.fallon@ericsson.com)
43  */
44 public class EventFacade {
45     private static final String CONCEPT = "concept ";
46     private static final String CONCEPT_S = "concept(s) ";
47     private static final String DOES_NOT_EXIST = " does not exist";
48     private static final String DO_ES_NOT_EXIST = " do(es) not exist";
49     private static final String ALREADY_EXISTS = " already exists";
50
51     // Apex model we're working towards
52     private final ApexModel apexModel;
53
54     // Properties to use for the model
55     private final Properties apexProperties;
56
57     // Facade classes for working towards the real Apex model
58     private final KeyInformationFacade keyInformationFacade;
59
60     // JSON output on list/delete if set
61     private final boolean jsonMode;
62
63     /**
64      * Constructor to create an event facade for the Model API.
65      *
66      * @param apexModel the apex model
67      * @param apexProperties Properties for the model
68      * @param jsonMode set to true to return JSON strings in list and delete operations, otherwise
69      *        set to false
70      */
71     public EventFacade(final ApexModel apexModel, final Properties apexProperties, final boolean jsonMode) {
72         this.apexModel = apexModel;
73         this.apexProperties = apexProperties;
74         this.jsonMode = jsonMode;
75
76         keyInformationFacade = new KeyInformationFacade(apexModel, apexProperties, jsonMode);
77     }
78
79     /**
80      * Create an event.
81      *
82      * @param name name of the event
83      * @param version version of the event, set to null to use the default version
84      * @param nameSpace of the event, set to null to use the default value
85      * @param source of the event, set to null to use the default value
86      * @param target of the event, set to null to use the default value
87      * @param uuid event UUID, set to null to generate a UUID
88      * @param description event description, set to null to generate a description
89      * @return result of the operation
90      */
91     public ApexApiResult createEvent(final String name, final String version, final String nameSpace,
92             final String source, final String target, final String uuid, final String description) {
93         try {
94             final AxArtifactKey key = new AxArtifactKey();
95             key.setName(name);
96             if (version != null) {
97                 key.setVersion(version);
98             } else {
99                 key.setVersion(apexProperties.getProperty("DEFAULT_CONCEPT_VERSION"));
100             }
101
102             if (apexModel.getPolicyModel().getEvents().getEventMap().containsKey(key)) {
103                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_EXISTS, CONCEPT + key.getId() + ALREADY_EXISTS);
104             }
105
106             final AxEvent event = new AxEvent(key);
107
108             event.setNameSpace((nameSpace != null ? nameSpace : apexProperties.getProperty("DEFAULT_EVENT_NAMESPACE")));
109             event.setSource((source != null ? source : apexProperties.getProperty("DEFAULT_EVENT_SOURCE")));
110             event.setTarget((target != null ? target : apexProperties.getProperty("DEFAULT_EVENT_TARGET")));
111
112             apexModel.getPolicyModel().getEvents().getEventMap().put(key, event);
113
114             if (apexModel.getPolicyModel().getKeyInformation().getKeyInfoMap().containsKey(key)) {
115                 return keyInformationFacade.updateKeyInformation(name, version, uuid, description);
116             } else {
117                 return keyInformationFacade.createKeyInformation(name, version, uuid, description);
118             }
119         } catch (final Exception e) {
120             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
121         }
122     }
123
124     /**
125      * Update an event.
126      *
127      * @param name name of the event
128      * @param version version of the event, set to null to use the latest version
129      * @param nameSpace of the event, set to null to not update
130      * @param source of the event, set to null to not update
131      * @param target of the event, set to null to not update
132      * @param uuid event UUID, set to null to not update
133      * @param description event description, set to null to not update
134      * @return result of the operation
135      */
136     public ApexApiResult updateEvent(final String name, final String version, final String nameSpace,
137             final String source, final String target, final String uuid, final String description) {
138         try {
139             final AxEvent event = apexModel.getPolicyModel().getEvents().get(name, version);
140             if (event == null) {
141                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
142                         CONCEPT + name + ':' + version + DOES_NOT_EXIST);
143             }
144
145             if (nameSpace != null) {
146                 event.setNameSpace(nameSpace);
147             }
148             if (source != null) {
149                 event.setSource(source);
150             }
151             if (target != null) {
152                 event.setTarget(target);
153             }
154
155             return keyInformationFacade.updateKeyInformation(name, version, uuid, description);
156         } catch (final Exception e) {
157             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
158         }
159     }
160
161     /**
162      * List events.
163      *
164      * @param name name of the event, set to null to list all
165      * @param version starting version of the event, set to null to list all versions
166      * @return result of the operation
167      */
168     public ApexApiResult listEvent(final String name, final String version) {
169         try {
170             final Set<AxEvent> eventSet = apexModel.getPolicyModel().getEvents().getAll(name, version);
171             if (name != null && eventSet.isEmpty()) {
172                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
173                         CONCEPT_S + name + ':' + version + DO_ES_NOT_EXIST);
174             }
175
176             final ApexApiResult result = new ApexApiResult();
177             for (final AxEvent event : eventSet) {
178                 result.addMessage(
179                         new ApexModelStringWriter<AxEvent>(false).writeString(event, AxEvent.class, jsonMode));
180             }
181             return result;
182         } catch (final Exception e) {
183             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
184         }
185     }
186
187     /**
188      * Delete an event.
189      *
190      * @param name name of the event
191      * @param version version of the event, set to null to delete all versions
192      * @return result of the operation
193      */
194     public ApexApiResult deleteEvent(final String name, final String version) {
195         try {
196             if (version != null) {
197                 final AxArtifactKey key = new AxArtifactKey(name, version);
198                 final AxEvent removedEvent = apexModel.getPolicyModel().getEvents().getEventMap().remove(key);
199                 if (removedEvent != null) {
200                     return new ApexApiResult(ApexApiResult.Result.SUCCESS, new ApexModelStringWriter<AxEvent>(false)
201                             .writeString(removedEvent, AxEvent.class, jsonMode));
202                 } else {
203                     return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
204                             CONCEPT + key.getId() + DOES_NOT_EXIST);
205                 }
206             }
207
208             final Set<AxEvent> eventSet = apexModel.getPolicyModel().getEvents().getAll(name, version);
209             if (eventSet.isEmpty()) {
210                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
211                         CONCEPT_S + name + ':' + version + DO_ES_NOT_EXIST);
212             }
213
214             final ApexApiResult result = new ApexApiResult();
215             for (final AxEvent event : eventSet) {
216                 result.addMessage(
217                         new ApexModelStringWriter<AxEvent>(false).writeString(event, AxEvent.class, jsonMode));
218                 apexModel.getPolicyModel().getEvents().getEventMap().remove(event.getKey());
219                 keyInformationFacade.deleteKeyInformation(name, version);
220             }
221             return result;
222         } catch (final Exception e) {
223             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
224         }
225     }
226
227     /**
228      * Validate events.
229      *
230      * @param name name of the event, set to null to list all
231      * @param version starting version of the event, set to null to list all versions
232      * @return result of the operation
233      */
234     public ApexApiResult validateEvent(final String name, final String version) {
235         try {
236             final Set<AxEvent> eventSet = apexModel.getPolicyModel().getEvents().getAll(name, version);
237             if (eventSet.isEmpty()) {
238                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
239                         CONCEPT_S + name + ':' + version + DO_ES_NOT_EXIST);
240             }
241
242             final ApexApiResult result = new ApexApiResult();
243             for (final AxEvent event : eventSet) {
244                 final AxValidationResult validationResult = event.validate(new AxValidationResult());
245                 result.addMessage(new ApexModelStringWriter<AxArtifactKey>(false).writeString(event.getKey(),
246                         AxArtifactKey.class, jsonMode));
247                 result.addMessage(validationResult.toString());
248             }
249             return result;
250         } catch (final Exception e) {
251             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
252         }
253     }
254
255     /**
256      * Create an event parameter.
257      *
258      * @param name name of the event
259      * @param version version of the event, set to null to use the latest version
260      * @param parName of the parameter
261      * @param contextSchemaName name of the parameter context schema
262      * @param contextSchemaVersion version of the parameter context schema, set to null to use the
263      *        latest version
264      * @param optional true if the event parameter is optional, false otherwise
265      * @return result of the operation
266      */
267     public ApexApiResult createEventPar(final String name, final String version, final String parName,
268             final String contextSchemaName, final String contextSchemaVersion, final boolean optional) {
269         try {
270             Assertions.argumentNotNull(parName, "parName may not be null");
271
272             final AxEvent event = apexModel.getPolicyModel().getEvents().get(name, version);
273             if (event == null) {
274                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
275                         CONCEPT + name + ':' + version + DOES_NOT_EXIST);
276             }
277
278             final AxReferenceKey refKey = new AxReferenceKey(event.getKey(), parName);
279
280             if (event.getParameterMap().containsKey(refKey.getLocalName())) {
281                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_EXISTS,
282                         CONCEPT + refKey.getId() + ALREADY_EXISTS);
283             }
284
285             final AxContextSchema schema =
286                     apexModel.getPolicyModel().getSchemas().get(contextSchemaName, contextSchemaVersion);
287             if (schema == null) {
288                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
289                         CONCEPT + contextSchemaName + ':' + contextSchemaVersion + DOES_NOT_EXIST);
290             }
291
292             event.getParameterMap().put(refKey.getLocalName(), new AxField(refKey, schema.getKey(), optional));
293             return new ApexApiResult();
294         } catch (final Exception e) {
295             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
296         }
297     }
298
299     /**
300      * List event parameters.
301      *
302      * @param name name of the event
303      * @param version version of the event, set to null to list latest version
304      * @param parName name of the parameter, set to null to list all parameters of the event
305      * @return result of the operation
306      */
307     public ApexApiResult listEventPar(final String name, final String version, final String parName) {
308         try {
309             final AxEvent event = apexModel.getPolicyModel().getEvents().get(name, version);
310             if (event == null) {
311                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
312                         CONCEPT + name + ':' + version + DOES_NOT_EXIST);
313             }
314
315             if (parName != null) {
316                 final AxField eventField = event.getParameterMap().get(parName);
317                 if (eventField != null) {
318                     return new ApexApiResult(ApexApiResult.Result.SUCCESS,
319                             new ApexModelStringWriter<AxField>(false).writeString(eventField, AxField.class, jsonMode));
320                 } else {
321                     return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
322                             CONCEPT + name + ':' + version + ':' + parName + DOES_NOT_EXIST);
323                 }
324             } else {
325                 if (event.getParameterMap().size() == 0) {
326                     return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
327                             "no parameters defined on event " + event.getKey().getId());
328                 }
329
330                 final ApexApiResult result = new ApexApiResult();
331                 for (final AxField eventPar : event.getParameterMap().values()) {
332                     result.addMessage(
333                             new ApexModelStringWriter<AxField>(false).writeString(eventPar, AxField.class, jsonMode));
334                 }
335                 return result;
336             }
337         } catch (final Exception e) {
338             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
339         }
340     }
341
342     /**
343      * Delete an event parameter.
344      *
345      * @param name name of the event
346      * @param version version of the event, set to null to use the latest version
347      * @param parName of the parameter, set to null to delete all parameters
348      * @return result of the operation
349      */
350     public ApexApiResult deleteEventPar(final String name, final String version, final String parName) {
351         try {
352             final AxEvent event = apexModel.getPolicyModel().getEvents().get(name, version);
353             if (event == null) {
354                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
355                         CONCEPT + name + ':' + version + DOES_NOT_EXIST);
356             }
357
358             final ApexApiResult result = new ApexApiResult();
359             if (parName != null) {
360                 if (event.getParameterMap().containsKey(parName)) {
361                     result.addMessage(new ApexModelStringWriter<AxField>(false)
362                             .writeString(event.getParameterMap().get(parName), AxField.class, jsonMode));
363                     event.getParameterMap().remove(parName);
364                     return result;
365                 } else {
366                     return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
367                             CONCEPT + name + ':' + version + ':' + parName + DOES_NOT_EXIST);
368                 }
369             } else {
370                 if (event.getParameterMap().size() == 0) {
371                     return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
372                             "no parameters defined on event " + event.getKey().getId());
373                 }
374
375                 for (final AxField eventPar : event.getParameterMap().values()) {
376                     result.addMessage(
377                             new ApexModelStringWriter<AxField>(false).writeString(eventPar, AxField.class, jsonMode));
378                 }
379                 event.getParameterMap().clear();
380                 return result;
381             }
382         } catch (final Exception e) {
383             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
384         }
385     }
386 }