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