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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.model.modelapi.impl;
24 import java.util.Properties;
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;
39 * This class acts as a facade for operations towards a policy model for event operations
42 * @author Liam Fallon (liam.fallon@ericsson.com)
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";
51 // Apex model we're working towards
52 private final ApexModel apexModel;
54 // Properties to use for the model
55 private final Properties apexProperties;
57 // Facade classes for working towards the real Apex model
58 private final KeyInformationFacade keyInformationFacade;
60 // JSON output on list/delete if set
61 private final boolean jsonMode;
64 * Constructor to create an event facade for the Model API.
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
71 public EventFacade(final ApexModel apexModel, final Properties apexProperties, final boolean jsonMode) {
72 this.apexModel = apexModel;
73 this.apexProperties = apexProperties;
74 this.jsonMode = jsonMode;
76 keyInformationFacade = new KeyInformationFacade(apexModel, apexProperties, jsonMode);
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
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) {
94 final AxArtifactKey key = new AxArtifactKey();
96 if (version != null) {
97 key.setVersion(version);
99 key.setVersion(apexProperties.getProperty("DEFAULT_CONCEPT_VERSION"));
102 if (apexModel.getPolicyModel().getEvents().getEventMap().containsKey(key)) {
103 return new ApexApiResult(ApexApiResult.Result.CONCEPT_EXISTS, CONCEPT + key.getId() + ALREADY_EXISTS);
106 final AxEvent event = new AxEvent(key);
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")));
112 apexModel.getPolicyModel().getEvents().getEventMap().put(key, event);
114 if (apexModel.getPolicyModel().getKeyInformation().getKeyInfoMap().containsKey(key)) {
115 return keyInformationFacade.updateKeyInformation(name, version, uuid, description);
117 return keyInformationFacade.createKeyInformation(name, version, uuid, description);
119 } catch (final Exception e) {
120 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
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
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) {
139 final AxEvent event = apexModel.getPolicyModel().getEvents().get(name, version);
141 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
142 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
145 if (nameSpace != null) {
146 event.setNameSpace(nameSpace);
148 if (source != null) {
149 event.setSource(source);
151 if (target != null) {
152 event.setTarget(target);
155 return keyInformationFacade.updateKeyInformation(name, version, uuid, description);
156 } catch (final Exception e) {
157 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
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
168 public ApexApiResult listEvent(final String name, final String version) {
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);
176 final ApexApiResult result = new ApexApiResult();
177 for (final AxEvent event : eventSet) {
179 new ApexModelStringWriter<AxEvent>(false).writeString(event, AxEvent.class, jsonMode));
182 } catch (final Exception e) {
183 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
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
194 public ApexApiResult deleteEvent(final String name, final String version) {
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));
203 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
204 CONCEPT + key.getId() + DOES_NOT_EXIST);
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);
214 final ApexApiResult result = new ApexApiResult();
215 for (final AxEvent event : eventSet) {
217 new ApexModelStringWriter<AxEvent>(false).writeString(event, AxEvent.class, jsonMode));
218 apexModel.getPolicyModel().getEvents().getEventMap().remove(event.getKey());
219 keyInformationFacade.deleteKeyInformation(name, version);
222 } catch (final Exception e) {
223 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
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
234 public ApexApiResult validateEvent(final String name, final String version) {
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);
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());
250 } catch (final Exception e) {
251 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
256 * Create an event parameter.
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
264 * @param optional true if the event parameter is optional, false otherwise
265 * @return result of the operation
267 public ApexApiResult createEventPar(final String name, final String version, final String parName,
268 final String contextSchemaName, final String contextSchemaVersion, final boolean optional) {
270 Assertions.argumentNotNull(parName, "parName may not be null");
272 final AxEvent event = apexModel.getPolicyModel().getEvents().get(name, version);
274 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
275 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
278 final AxReferenceKey refKey = new AxReferenceKey(event.getKey(), parName);
280 if (event.getParameterMap().containsKey(refKey.getLocalName())) {
281 return new ApexApiResult(ApexApiResult.Result.CONCEPT_EXISTS,
282 CONCEPT + refKey.getId() + ALREADY_EXISTS);
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);
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);
300 * List event parameters.
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
307 public ApexApiResult listEventPar(final String name, final String version, final String parName) {
309 final AxEvent event = apexModel.getPolicyModel().getEvents().get(name, version);
311 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
312 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
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));
321 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
322 CONCEPT + name + ':' + version + ':' + parName + DOES_NOT_EXIST);
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());
330 final ApexApiResult result = new ApexApiResult();
331 for (final AxField eventPar : event.getParameterMap().values()) {
333 new ApexModelStringWriter<AxField>(false).writeString(eventPar, AxField.class, jsonMode));
337 } catch (final Exception e) {
338 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
343 * Delete an event parameter.
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
350 public ApexApiResult deleteEventPar(final String name, final String version, final String parName) {
352 final AxEvent event = apexModel.getPolicyModel().getEvents().get(name, version);
354 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
355 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
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);
366 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
367 CONCEPT + name + ':' + version + ':' + parName + DOES_NOT_EXIST);
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());
375 for (final AxField eventPar : event.getParameterMap().values()) {
377 new ApexModelStringWriter<AxField>(false).writeString(eventPar, AxField.class, jsonMode));
379 event.getParameterMap().clear();
382 } catch (final Exception e) {
383 return new ApexApiResult(ApexApiResult.Result.FAILED, e);