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 com.att.eelf.configuration.EELFLogger;
24 import com.att.eelf.configuration.EELFManager;
26 import java.io.UnsupportedEncodingException;
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.List;
32 import javax.ws.rs.core.Response.Status;
34 import org.onap.aai.config.SpringContextAware;
35 import org.onap.aai.exceptions.AAIException;
36 import org.onap.aai.introspection.Introspector;
37 import org.onap.aai.introspection.Loader;
38 import org.onap.aai.introspection.LoaderFactory;
39 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
40 import org.onap.aai.introspection.exceptions.AAIUnmarshallingException;
41 import org.onap.aai.logging.LogFormatTools;
42 import org.onap.aai.parsers.uri.URIToObject;
43 import org.onap.aai.setup.SchemaVersion;
44 import org.onap.aai.setup.SchemaVersions;
47 * The Class UEBNotification.
49 public class UEBNotification {
51 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(UEBNotification.class);
53 private Loader currentVersionLoader = null;
54 protected List<NotificationEvent> events = null;
55 private SchemaVersion notificationVersion = null;
58 * Instantiates a new UEB notification.
60 * @param loader the loader
62 public UEBNotification(Loader loader, LoaderFactory loaderFactory, SchemaVersions schemaVersions) {
63 events = new ArrayList<>();
64 SchemaVersion defaultVersion = schemaVersions.getDefaultVersion();
65 currentVersionLoader = loaderFactory.createLoaderForVersion(loader.getModelType(), defaultVersion);
66 notificationVersion = defaultVersion;
70 * Creates the notification event.
72 * @param transactionId the X-TransactionId
73 * @param sourceOfTruth
74 * @param status the status
77 * @param basePath base URI path
78 * @throws AAIException the AAI exception
79 * @throws IllegalArgumentException the illegal argument exception
80 * @throws UnsupportedEncodingException the unsupported encoding exception
82 public void createNotificationEvent(String transactionId, String sourceOfTruth, Status status, URI uri,
83 Introspector obj, HashMap<String, Introspector> relatedObjects, String basePath)
84 throws AAIException, UnsupportedEncodingException {
86 String action = "UPDATE";
88 if (status.equals(Status.CREATED)) {
90 } else if (status.equals(Status.OK)) {
92 } else if (status.equals(Status.NO_CONTENT)) {
97 Introspector eventHeader = currentVersionLoader.introspectorFromName("notification-event-header");
98 URIToObject parser = new URIToObject(currentVersionLoader, uri, relatedObjects);
100 String entityLink = "";
101 if ((basePath != null) && (!basePath.isEmpty())) {
102 if (!(basePath.startsWith("/"))) {
103 basePath = "/" + basePath;
105 if (!(basePath.endsWith("/"))) {
106 basePath = basePath + "/";
111 if (LOGGER.isDebugEnabled()) {
112 LOGGER.debug("Please check the schema.uri.base.path as it didn't seem to be set");
116 if (uri.toString().startsWith("/")) {
117 entityLink = basePath + notificationVersion + uri;
119 entityLink = basePath + notificationVersion + "/" + uri;
122 eventHeader.setValue("entity-link", entityLink);
123 eventHeader.setValue("action", action);
124 eventHeader.setValue("entity-type", obj.getDbName());
125 eventHeader.setValue("top-entity-type", parser.getTopEntityName());
126 eventHeader.setValue("source-name", sourceOfTruth);
127 eventHeader.setValue("version", notificationVersion.toString());
128 eventHeader.setValue("id", transactionId);
130 List<Object> parentList = parser.getParentList();
133 if (!parser.getTopEntity().equals(parser.getEntity())) {
134 Introspector child = obj;
135 if (!parser.getLoader().getVersion().equals(obj.getVersion())) {
136 String json = obj.marshal(false);
137 child = parser.getLoader().unmarshal(parser.getEntity().getName(), json);
140 // wrap the child object in its parents
141 parentList.add(child.getUnderlyingObject());
144 final Introspector eventObject;
146 // convert to most resent version
147 if (!parser.getLoader().getVersion().equals(currentVersionLoader.getVersion())) {
149 if (parser.getTopEntity().equals(parser.getEntity())) {
150 // convert the parent object passed in
151 json = obj.marshal(false);
152 eventObject = currentVersionLoader.unmarshal(obj.getName(), json);
154 // convert the object created in the parser
155 json = parser.getTopEntity().marshal(false);
156 eventObject = currentVersionLoader.unmarshal(parser.getTopEntity().getName(), json);
159 if (parser.getTopEntity().equals(parser.getEntity())) {
160 // take the top level parent object passed in
163 // take the wrapped child objects (ogres are like onions)
164 eventObject = parser.getTopEntity();
167 final NotificationEvent event =
168 new NotificationEvent(currentVersionLoader, eventHeader, eventObject, transactionId, sourceOfTruth);
170 } catch (AAIUnknownObjectException e) {
171 throw new RuntimeException("Fatal error - notification-event-header object not found!");
172 } catch (AAIUnmarshallingException e) {
174 "Unmarshalling error occurred while generating UEBNotification " + LogFormatTools.getStackTop(e));
181 * @throws AAIException the AAI exception
183 public void triggerEvents() throws AAIException {
184 for (NotificationEvent event : events) {
190 public List<NotificationEvent> getEvents() {