2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.aai.rest.ueb;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.onap.aai.exceptions.AAIException;
26 import org.onap.aai.introspection.Introspector;
27 import org.onap.aai.introspection.Loader;
28 import org.onap.aai.introspection.LoaderFactory;
29 import org.onap.aai.introspection.ModelType;
30 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
31 import org.onap.aai.introspection.exceptions.AAIUnmarshallingException;
32 import org.onap.aai.logging.LogFormatTools;
33 import org.onap.aai.parsers.uri.URIToObject;
34 import org.onap.aai.setup.SchemaVersion;
35 import org.onap.aai.setup.SchemaVersions;
37 import javax.ws.rs.core.Response.Status;
38 import java.io.UnsupportedEncodingException;
43 * The Class UEBNotification.
45 public class UEBNotification {
47 private static final Logger LOGGER = LoggerFactory.getLogger(UEBNotification.class);
49 private Loader currentVersionLoader = null;
50 protected Map<String, NotificationEvent> events = null;
51 private SchemaVersion notificationVersion = null;
54 * Instantiates a new UEB notification.
56 * @param loader the loader
58 public UEBNotification(Loader loader, LoaderFactory loaderFactory, SchemaVersions schemaVersions) {
59 events = new LinkedHashMap<>();
60 SchemaVersion defaultVersion = schemaVersions.getDefaultVersion();
61 currentVersionLoader = loaderFactory.createLoaderForVersion(loader.getModelType(), defaultVersion);
62 notificationVersion = defaultVersion;
66 * Instantiates a new UEB notification.
68 * @param modelType - Model type
69 * @param loaderFactory - the loader factory
70 * @param schemaVersions the schema versions bean
72 public UEBNotification(ModelType modelType, LoaderFactory loaderFactory, SchemaVersions schemaVersions) {
73 events = new LinkedHashMap<>();
74 SchemaVersion defaultVersion = schemaVersions.getDefaultVersion();
75 currentVersionLoader = loaderFactory.createLoaderForVersion(modelType, defaultVersion);
76 notificationVersion = defaultVersion;
80 * Creates the notification event.
82 * @param transactionId the X-TransactionId
83 * @param sourceOfTruth
84 * @param status the status
87 * @param basePath base URI path
88 * @throws AAIException the AAI exception
89 * @throws IllegalArgumentException the illegal argument exception
90 * @throws UnsupportedEncodingException the unsupported encoding exception
92 public void createNotificationEvent(String transactionId, String sourceOfTruth, Status status, URI uri,
93 Introspector obj, HashMap<String, Introspector> relatedObjects, String basePath)
94 throws AAIException, UnsupportedEncodingException {
96 String action = "UPDATE";
98 if (status.equals(Status.CREATED)) {
100 } else if (status.equals(Status.OK)) {
102 } else if (status.equals(Status.NO_CONTENT)) {
107 Introspector eventHeader = currentVersionLoader.introspectorFromName("notification-event-header");
108 URIToObject parser = new URIToObject(currentVersionLoader, uri, relatedObjects);
110 if ((basePath != null) && (!basePath.isEmpty())) {
111 if (!(basePath.startsWith("/"))) {
112 basePath = "/" + basePath;
114 if (!(basePath.endsWith("/"))) {
115 basePath = basePath + "/";
120 if (LOGGER.isDebugEnabled()) {
121 LOGGER.debug("Please check the schema.uri.base.path as it didn't seem to be set");
125 String uriStr = getUri(uri.toString(), basePath);
127 if (uriStr.startsWith("/")) {
128 entityLink = basePath + notificationVersion + uriStr;
130 entityLink = basePath + notificationVersion + "/" + uriStr;
133 eventHeader.setValue("entity-link", entityLink);
134 eventHeader.setValue("action", action);
135 eventHeader.setValue("entity-type", obj.getDbName());
136 eventHeader.setValue("top-entity-type", parser.getTopEntityName());
137 eventHeader.setValue("source-name", sourceOfTruth);
138 eventHeader.setValue("version", notificationVersion.toString());
139 eventHeader.setValue("id", transactionId);
141 List<Object> parentList = parser.getParentList();
144 if (!parser.getTopEntity().equals(parser.getEntity())) {
145 Introspector child = obj;
146 if (!parser.getLoader().getVersion().equals(obj.getVersion())) {
147 String json = obj.marshal(false);
148 child = parser.getLoader().unmarshal(parser.getEntity().getName(), json);
151 // wrap the child object in its parents
152 parentList.add(child.getUnderlyingObject());
155 final Introspector eventObject;
157 // convert to most resent version
158 if (!parser.getLoader().getVersion().equals(currentVersionLoader.getVersion())) {
160 if (parser.getTopEntity().equals(parser.getEntity())) {
161 // convert the parent object passed in
162 json = obj.marshal(false);
163 eventObject = currentVersionLoader.unmarshal(obj.getName(), json);
165 // convert the object created in the parser
166 json = parser.getTopEntity().marshal(false);
167 eventObject = currentVersionLoader.unmarshal(parser.getTopEntity().getName(), json);
170 if (parser.getTopEntity().equals(parser.getEntity())) {
171 // take the top level parent object passed in
174 // take the wrapped child objects (ogres are like onions)
175 eventObject = parser.getTopEntity();
178 final NotificationEvent event =
179 new NotificationEvent(currentVersionLoader, eventHeader, eventObject, transactionId, sourceOfTruth);
180 events.put(uri.toString(), event);
181 } catch (AAIUnknownObjectException e) {
182 throw new RuntimeException("Fatal error - notification-event-header object not found!");
183 } catch (AAIUnmarshallingException e) {
185 "Unmarshalling error occurred while generating UEBNotification " + LogFormatTools.getStackTop(e));
192 * @throws AAIException the AAI exception
194 public void triggerEvents() throws AAIException {
195 for (NotificationEvent event : events.values()) {
201 public List<NotificationEvent> getEvents() {
202 return new ArrayList<>(this.events.values());
204 public Map<String, NotificationEvent> getEventsMap() {
208 private String getUri(String uri, String basePath) {
209 if (uri == null || uri.isEmpty()) {
211 } else if (uri.charAt(0) != '/') {
215 if ((basePath != null) && (!basePath.isEmpty())) {
216 if (!(basePath.startsWith("/"))) {
217 basePath = "/" + basePath;
219 if (!(basePath.endsWith("/"))) {
220 basePath = basePath + "/";
224 LOGGER.trace("Notification header uri base path:'{}', uri:'{}'", basePath, uri);
226 return uri.replaceAll("^" + basePath + "v\\d+", "");
229 public void clearEvents() {