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;
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;
38 * This class acts as a facade for operations towards a policy model for event operations
41 * @author Liam Fallon (liam.fallon@ericsson.com)
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";
50 // Apex model we're working towards
51 private final ApexModel apexModel;
53 // Properties to use for the model
54 private final Properties apexProperties;
56 // Facade classes for working towards the real Apex model
57 private final KeyInformationFacade keyInformationFacade;
59 // JSON output on list/delete if set
60 private final boolean jsonMode;
63 * Constructor to create an event facade for the Model API.
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
70 public EventFacade(final ApexModel apexModel, final Properties apexProperties, final boolean jsonMode) {
71 this.apexModel = apexModel;
72 this.apexProperties = apexProperties;
73 this.jsonMode = jsonMode;
75 keyInformationFacade = new KeyInformationFacade(apexModel, apexProperties, jsonMode);
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
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) {
93 final AxArtifactKey key = new AxArtifactKey();
95 if (version != null) {
96 key.setVersion(version);
98 key.setVersion(apexProperties.getProperty("DEFAULT_CONCEPT_VERSION"));
101 if (apexModel.getPolicyModel().getEvents().getEventMap().containsKey(key)) {
102 return new ApexApiResult(ApexApiResult.Result.CONCEPT_EXISTS, CONCEPT + key.getId() + ALREADY_EXISTS);
105 final AxEvent event = new AxEvent(key);
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")));
111 apexModel.getPolicyModel().getEvents().getEventMap().put(key, event);
113 if (apexModel.getPolicyModel().getKeyInformation().getKeyInfoMap().containsKey(key)) {
114 return keyInformationFacade.updateKeyInformation(name, version, uuid, description);
116 return keyInformationFacade.createKeyInformation(name, version, uuid, description);
118 } catch (final Exception e) {
119 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
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
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) {
138 final AxEvent event = apexModel.getPolicyModel().getEvents().get(name, version);
140 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
141 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
144 if (nameSpace != null) {
145 event.setNameSpace(nameSpace);
147 if (source != null) {
148 event.setSource(source);
150 if (target != null) {
151 event.setTarget(target);
154 return keyInformationFacade.updateKeyInformation(name, version, uuid, description);
155 } catch (final Exception e) {
156 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
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
167 public ApexApiResult listEvent(final String name, final String version) {
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);
175 final ApexApiResult result = new ApexApiResult();
176 for (final AxEvent event : eventSet) {
178 new ApexModelStringWriter<AxEvent>(false).writeString(event, AxEvent.class, jsonMode));
181 } catch (final Exception e) {
182 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
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
193 public ApexApiResult deleteEvent(final String name, final String version) {
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));
202 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
203 CONCEPT + key.getId() + DOES_NOT_EXIST);
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);
213 final ApexApiResult result = new ApexApiResult();
214 for (final AxEvent event : eventSet) {
216 new ApexModelStringWriter<AxEvent>(false).writeString(event, AxEvent.class, jsonMode));
217 apexModel.getPolicyModel().getEvents().getEventMap().remove(event.getKey());
218 keyInformationFacade.deleteKeyInformation(name, version);
221 } catch (final Exception e) {
222 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
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
233 public ApexApiResult validateEvent(final String name, final String version) {
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);
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());
249 } catch (final Exception e) {
250 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
255 * Create an event parameter.
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
263 * @param optional true if the event parameter is optional, false otherwise
264 * @return result of the operation
266 public ApexApiResult createEventPar(final String name, final String version, final String parName,
267 final String contextSchemaName, final String contextSchemaVersion, final boolean optional) {
269 Assertions.argumentNotNull(parName, "parName may not be null");
271 final AxEvent event = apexModel.getPolicyModel().getEvents().get(name, version);
273 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
274 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
277 final AxReferenceKey refKey = new AxReferenceKey(event.getKey(), parName);
279 if (event.getParameterMap().containsKey(refKey.getLocalName())) {
280 return new ApexApiResult(ApexApiResult.Result.CONCEPT_EXISTS,
281 CONCEPT + refKey.getId() + ALREADY_EXISTS);
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);
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);
299 * List event parameters.
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
306 public ApexApiResult listEventPar(final String name, final String version, final String parName) {
308 final AxEvent event = apexModel.getPolicyModel().getEvents().get(name, version);
310 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
311 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
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));
320 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
321 CONCEPT + name + ':' + version + ':' + parName + DOES_NOT_EXIST);
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());
329 final ApexApiResult result = new ApexApiResult();
330 for (final AxField eventPar : event.getParameterMap().values()) {
332 new ApexModelStringWriter<AxField>(false).writeString(eventPar, AxField.class, jsonMode));
336 } catch (final Exception e) {
337 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
342 * Delete an event parameter.
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
349 public ApexApiResult deleteEventPar(final String name, final String version, final String parName) {
351 final AxEvent event = apexModel.getPolicyModel().getEvents().get(name, version);
353 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
354 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
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);
365 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
366 CONCEPT + name + ':' + version + ':' + parName + DOES_NOT_EXIST);
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());
374 for (final AxField eventPar : event.getParameterMap().values()) {
376 new ApexModelStringWriter<AxField>(false).writeString(eventPar, AxField.class, jsonMode));
378 event.getParameterMap().clear();
381 } catch (final Exception e) {
382 return new ApexApiResult(ApexApiResult.Result.FAILED, e);