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=========================================================
20 package org.onap.aai.rest.ueb;
22 import java.io.UnsupportedEncodingException;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
28 import javax.ws.rs.core.Response.Status;
30 import org.onap.aai.config.SpringContextAware;
31 import org.onap.aai.exceptions.AAIException;
32 import org.onap.aai.introspection.Introspector;
33 import org.onap.aai.introspection.Loader;
34 import org.onap.aai.introspection.LoaderFactory;
35 import org.onap.aai.setup.SchemaVersion;
36 import org.onap.aai.setup.SchemaVersions;
37 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
38 import org.onap.aai.introspection.exceptions.AAIUnmarshallingException;
39 import org.onap.aai.logging.LogFormatTools;
40 import org.onap.aai.parsers.uri.URIToObject;
42 import com.att.eelf.configuration.EELFLogger;
43 import com.att.eelf.configuration.EELFManager;
46 * The Class UEBNotification.
48 public class UEBNotification {
50 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(UEBNotification.class);
52 private Loader currentVersionLoader = null;
53 protected List<NotificationEvent> events = null;
54 private SchemaVersion notificationVersion = null;
56 * Instantiates a new UEB notification.
58 * @param loader the loader
60 public UEBNotification(Loader loader, LoaderFactory loaderFactory, SchemaVersions schemaVersions) {
61 events = new ArrayList<>();
62 SchemaVersion defaultVersion = schemaVersions.getDefaultVersion();
63 currentVersionLoader = loaderFactory.createLoaderForVersion(loader.getModelType(), defaultVersion);
64 notificationVersion = defaultVersion;
69 * Creates the notification event.
71 * @param transactionId the X-TransactionId
72 * @param sourceOfTruth
73 * @param status the status
76 * @param basePath base URI path
77 * @throws AAIException the AAI exception
78 * @throws IllegalArgumentException the illegal argument exception
79 * @throws UnsupportedEncodingException the unsupported encoding exception
81 public void createNotificationEvent(String transactionId, String sourceOfTruth, Status status, URI uri, Introspector obj, HashMap<String, Introspector> relatedObjects, String basePath) throws AAIException, UnsupportedEncodingException {
83 String action = "UPDATE";
85 if (status.equals(Status.CREATED)) {
87 } else if (status.equals(Status.OK)) {
89 } else if (status.equals(Status.NO_CONTENT)) {
94 Introspector eventHeader = currentVersionLoader.introspectorFromName("notification-event-header");
95 URIToObject parser = new URIToObject(currentVersionLoader, uri, relatedObjects);
97 String entityLink = "";
98 if ((basePath != null) && (!basePath.isEmpty())) {
99 if (!(basePath.startsWith("/"))) {
100 basePath = "/" + basePath;
102 if (!(basePath.endsWith("/"))) {
103 basePath = basePath + "/";
108 if(LOGGER.isDebugEnabled()){
109 LOGGER.debug("Please check the schema.uri.base.path as it didn't seem to be set");
113 if (uri.toString().startsWith("/")) {
114 entityLink = basePath + notificationVersion + uri;
116 entityLink = basePath + notificationVersion + "/" + uri;
120 eventHeader.setValue("entity-link", entityLink);
121 eventHeader.setValue("action", action);
122 eventHeader.setValue("entity-type", obj.getDbName());
123 eventHeader.setValue("top-entity-type", parser.getTopEntityName());
124 eventHeader.setValue("source-name", sourceOfTruth);
125 eventHeader.setValue("version", notificationVersion.toString());
126 eventHeader.setValue("id", transactionId);
128 List<Object> parentList = parser.getParentList();
131 if (!parser.getTopEntity().equals(parser.getEntity())) {
132 Introspector child = obj;
133 if (!parser.getLoader().getVersion().equals(obj.getVersion())) {
134 String json = obj.marshal(false);
135 child = parser.getLoader().unmarshal(parser.getEntity().getName(), json);
138 //wrap the child object in its parents
139 parentList.add(child.getUnderlyingObject());
142 final Introspector eventObject;
144 //convert to most resent version
145 if (!parser.getLoader().getVersion().equals(currentVersionLoader.getVersion())) {
147 if (parser.getTopEntity().equals(parser.getEntity())) {
148 //convert the parent object passed in
149 json = obj.marshal(false);
150 eventObject = currentVersionLoader.unmarshal(obj.getName(), json);
152 //convert the object created in the parser
153 json = parser.getTopEntity().marshal(false);
154 eventObject = currentVersionLoader.unmarshal(parser.getTopEntity().getName(), json);
157 if (parser.getTopEntity().equals(parser.getEntity())) {
158 //take the top level parent object passed in
161 //take the wrapped child objects (ogres are like onions)
162 eventObject = parser.getTopEntity();
165 final NotificationEvent event = new NotificationEvent(currentVersionLoader, eventHeader, eventObject, transactionId, sourceOfTruth);
167 } catch (AAIUnknownObjectException e) {
168 throw new RuntimeException("Fatal error - notification-event-header object not found!");
169 } catch (AAIUnmarshallingException e) {
170 LOGGER.error("Unmarshalling error occurred while generating UEBNotification " + LogFormatTools.getStackTop(e));
177 * @throws AAIException the AAI exception
179 public void triggerEvents() throws AAIException {
180 for (NotificationEvent event : events) {
186 public List<NotificationEvent> getEvents() {