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.db.props.AAIProperties;
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.introspection.Version;
36 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
37 import org.onap.aai.introspection.exceptions.AAIUnmarshallingException;
38 import org.onap.aai.parsers.uri.URIToObject;
39 import org.onap.aai.util.AAIConfig;
40 import com.att.eelf.configuration.EELFLogger;
41 import com.att.eelf.configuration.EELFManager;
44 * The Class UEBNotification.
46 public class UEBNotification {
48 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(UEBNotification.class);
50 private Loader currentVersionLoader = null;
51 protected List<NotificationEvent> events = null;
52 private Version notificationVersion = null;
55 * Instantiates a new UEB notification.
57 * @param loader the loader
59 public UEBNotification(Loader loader) {
60 events = new ArrayList<>();
61 currentVersionLoader = LoaderFactory.createLoaderForVersion(loader.getModelType(), AAIProperties.LATEST);
62 notificationVersion = Version.valueOf(AAIConfig.get("aai.notification.current.version","v12"));
67 * Creates the notification event.
69 * @param transactionId the X-TransactionId
70 * @param sourceOfTruth
71 * @param status the status
74 * @throws AAIException the AAI exception
75 * @throws IllegalArgumentException the illegal argument exception
76 * @throws UnsupportedEncodingException the unsupported encoding exception
78 public void createNotificationEvent(String transactionId, String sourceOfTruth, Status status, URI uri, Introspector obj, HashMap<String, Introspector> relatedObjects) throws AAIException, UnsupportedEncodingException {
80 String action = "UPDATE";
82 if (status.equals(Status.CREATED)) {
84 } else if (status.equals(Status.OK)) {
86 } else if (status.equals(Status.NO_CONTENT)) {
91 Introspector eventHeader = currentVersionLoader.introspectorFromName("notification-event-header");
92 URIToObject parser = new URIToObject(currentVersionLoader, uri, relatedObjects);
94 String entityLink = "";
95 if (uri.toString().startsWith("/")) {
96 entityLink = "/aai/" + notificationVersion + uri;
98 entityLink = "/aai/" + notificationVersion + "/" + uri;
102 eventHeader.setValue("entity-link", entityLink);
103 eventHeader.setValue("action", action);
104 eventHeader.setValue("entity-type", obj.getDbName());
105 eventHeader.setValue("top-entity-type", parser.getTopEntityName());
106 eventHeader.setValue("source-name", sourceOfTruth);
107 eventHeader.setValue("version", notificationVersion.toString());
108 eventHeader.setValue("id", transactionId);
110 List<Object> parentList = parser.getParentList();
113 if (!parser.getTopEntity().equals(parser.getEntity())) {
114 Introspector child = obj;
115 if (!parser.getLoader().getVersion().equals(obj.getVersion())) {
116 String json = obj.marshal(false);
117 child = parser.getLoader().unmarshal(parser.getEntity().getName(), json);
120 //wrap the child object in its parents
121 parentList.add(child.getUnderlyingObject());
124 final Introspector eventObject;
126 //convert to most resent version
127 if (!parser.getLoader().getVersion().equals(currentVersionLoader.getVersion())) {
129 if (parser.getTopEntity().equals(parser.getEntity())) {
130 //convert the parent object passed in
131 json = obj.marshal(false);
132 eventObject = currentVersionLoader.unmarshal(obj.getName(), json);
134 //convert the object created in the parser
135 json = parser.getTopEntity().marshal(false);
136 eventObject = currentVersionLoader.unmarshal(parser.getTopEntity().getName(), json);
139 if (parser.getTopEntity().equals(parser.getEntity())) {
140 //take the top level parent object passed in
143 //take the wrapped child objects (ogres are like onions)
144 eventObject = parser.getTopEntity();
148 final NotificationEvent event = new NotificationEvent(currentVersionLoader, eventHeader, eventObject, transactionId, sourceOfTruth);
150 } catch (AAIUnknownObjectException e) {
151 throw new RuntimeException("Fatal error - notification-event-header object not found!");
152 } catch (AAIUnmarshallingException e) {
153 LOGGER.error("Unmarshalling error occurred while generating UEBNotification", e);
160 * @throws AAIException the AAI exception
162 public void triggerEvents() throws AAIException {
163 for (NotificationEvent event : events) {
169 public List<NotificationEvent> getEvents() {