2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.aai.rest.ueb;
24 import java.io.UnsupportedEncodingException;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
30 import javax.ws.rs.core.Response.Status;
32 import org.onap.aai.db.props.AAIProperties;
33 import org.onap.aai.exceptions.AAIException;
34 import org.onap.aai.introspection.Introspector;
35 import org.onap.aai.introspection.Loader;
36 import org.onap.aai.introspection.LoaderFactory;
37 import org.onap.aai.introspection.Version;
38 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
39 import org.onap.aai.introspection.exceptions.AAIUnmarshallingException;
40 import org.onap.aai.parsers.uri.URIToObject;
41 import org.onap.aai.util.AAIConfig;
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 Version notificationVersion = null;
57 * Instantiates a new UEB notification.
59 * @param loader the loader
61 public UEBNotification(Loader loader) {
62 events = new ArrayList<>();
63 currentVersionLoader = LoaderFactory.createLoaderForVersion(loader.getModelType(), AAIProperties.LATEST);
64 notificationVersion = Version.valueOf(AAIConfig.get("aai.notification.current.version","v12"));
69 * Creates the notification event.
71 * @param transactionId the X-TransactionId
72 * @param sourceOfTruth
73 * @param status the status
76 * @throws AAIException the AAI exception
77 * @throws IllegalArgumentException the illegal argument exception
78 * @throws UnsupportedEncodingException the unsupported encoding exception
80 public void createNotificationEvent(String transactionId, String sourceOfTruth, Status status, URI uri, Introspector obj, HashMap<String, Introspector> relatedObjects) throws AAIException, UnsupportedEncodingException {
82 String action = "UPDATE";
84 if (status.equals(Status.CREATED)) {
86 } else if (status.equals(Status.OK)) {
88 } else if (status.equals(Status.NO_CONTENT)) {
93 Introspector eventHeader = currentVersionLoader.introspectorFromName("notification-event-header");
94 URIToObject parser = new URIToObject(currentVersionLoader, uri, relatedObjects);
96 String entityLink = "";
97 if (uri.toString().startsWith("/")) {
98 entityLink = "/aai/" + notificationVersion + uri;
100 entityLink = "/aai/" + notificationVersion + "/" + uri;
104 eventHeader.setValue("entity-link", entityLink);
105 eventHeader.setValue("action", action);
106 eventHeader.setValue("entity-type", obj.getDbName());
107 eventHeader.setValue("top-entity-type", parser.getTopEntityName());
108 eventHeader.setValue("source-name", sourceOfTruth);
109 eventHeader.setValue("version", notificationVersion.toString());
110 eventHeader.setValue("id", transactionId);
112 List<Object> parentList = parser.getParentList();
115 if (!parser.getTopEntity().equals(parser.getEntity())) {
116 Introspector child = obj;
117 if (!parser.getLoader().getVersion().equals(obj.getVersion())) {
118 String json = obj.marshal(false);
119 child = parser.getLoader().unmarshal(parser.getEntity().getName(), json);
122 //wrap the child object in its parents
123 parentList.add(child.getUnderlyingObject());
126 final Introspector eventObject;
128 //convert to most resent version
129 if (!parser.getLoader().getVersion().equals(currentVersionLoader.getVersion())) {
131 if (parser.getTopEntity().equals(parser.getEntity())) {
132 //convert the parent object passed in
133 json = obj.marshal(false);
134 eventObject = currentVersionLoader.unmarshal(obj.getName(), json);
136 //convert the object created in the parser
137 json = parser.getTopEntity().marshal(false);
138 eventObject = currentVersionLoader.unmarshal(parser.getTopEntity().getName(), json);
141 if (parser.getTopEntity().equals(parser.getEntity())) {
142 //take the top level parent object passed in
145 //take the wrapped child objects (ogres are like onions)
146 eventObject = parser.getTopEntity();
150 final NotificationEvent event = new NotificationEvent(currentVersionLoader, eventHeader, eventObject, transactionId, sourceOfTruth);
152 } catch (AAIUnknownObjectException e) {
153 throw new RuntimeException("Fatal error - notification-event-header object not found!");
154 } catch (AAIUnmarshallingException e) {
155 LOGGER.error("Unmarshalling error occurred while generating UEBNotification", e);
162 * @throws AAIException the AAI exception
164 public void triggerEvents() throws AAIException {
165 for (NotificationEvent event : events) {
171 public List<NotificationEvent> getEvents() {