2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 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=========================================================
21 package org.openecomp.aai.rest.ueb;
23 import java.io.UnsupportedEncodingException;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
29 import javax.ws.rs.core.Response.Status;
31 import org.openecomp.aai.db.props.AAIProperties;
32 import org.openecomp.aai.exceptions.AAIException;
33 import org.openecomp.aai.introspection.Introspector;
34 import org.openecomp.aai.introspection.Loader;
35 import org.openecomp.aai.introspection.LoaderFactory;
36 import org.openecomp.aai.introspection.Version;
37 import org.openecomp.aai.introspection.exceptions.AAIUnknownObjectException;
38 import org.openecomp.aai.introspection.exceptions.AAIUnmarshallingException;
39 import org.openecomp.aai.parsers.uri.URIToObject;
40 import org.openecomp.aai.util.AAIConfig;
41 import com.att.eelf.configuration.EELFLogger;
42 import com.att.eelf.configuration.EELFManager;
45 * The Class UEBNotification.
47 public class UEBNotification {
49 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(UEBNotification.class);
51 private Loader currentVersionLoader = null;
52 protected List<NotificationEvent> events = null;
53 private Version notificationVersion = null;
56 * Instantiates a new UEB notification.
58 * @param loader the loader
60 public UEBNotification(Loader loader) {
61 events = new ArrayList<>();
62 currentVersionLoader = LoaderFactory.createLoaderForVersion(loader.getModelType(), AAIProperties.LATEST);
63 notificationVersion = Version.valueOf(AAIConfig.get("aai.notification.current.version","v11"));
68 * Creates the notification event.
70 * @param transactionId the X-TransactionId
71 * @param sourceOfTruth
72 * @param status the status
75 * @throws AAIException the AAI exception
76 * @throws IllegalArgumentException the illegal argument exception
77 * @throws UnsupportedEncodingException the unsupported encoding exception
79 public void createNotificationEvent(String transactionId, String sourceOfTruth, Status status, URI uri, Introspector obj, HashMap<String, Introspector> relatedObjects) throws AAIException, UnsupportedEncodingException {
81 String action = "UPDATE";
83 if (status.equals(Status.CREATED)) {
85 } else if (status.equals(Status.OK)) {
87 } else if (status.equals(Status.NO_CONTENT)) {
92 Introspector eventHeader = currentVersionLoader.introspectorFromName("notification-event-header");
93 URIToObject parser = new URIToObject(currentVersionLoader, uri, relatedObjects);
95 String entityLink = "";
96 if (uri.toString().startsWith("/")) {
97 entityLink = "/aai/" + notificationVersion + uri;
99 entityLink = "/aai/" + notificationVersion + "/" + uri;
103 eventHeader.setValue("entity-link", entityLink);
104 eventHeader.setValue("action", action);
105 eventHeader.setValue("entity-type", obj.getDbName());
106 eventHeader.setValue("top-entity-type", parser.getTopEntityName());
107 eventHeader.setValue("source-name", sourceOfTruth);
108 eventHeader.setValue("version", notificationVersion.toString());
109 eventHeader.setValue("id", transactionId);
111 List<Object> parentList = parser.getParentList();
114 if (!parser.getTopEntity().equals(parser.getEntity())) {
115 Introspector child = obj;
116 if (!parser.getLoader().getVersion().equals(obj.getVersion())) {
117 String json = obj.marshal(false);
118 child = parser.getLoader().unmarshal(parser.getEntity().getName(), json);
121 //wrap the child object in its parents
122 parentList.add(child.getUnderlyingObject());
125 final Introspector eventObject;
127 //convert to most resent version
128 if (!parser.getLoader().getVersion().equals(currentVersionLoader.getVersion())) {
130 if (parser.getTopEntity().equals(parser.getEntity())) {
131 //convert the parent object passed in
132 json = obj.marshal(false);
133 eventObject = currentVersionLoader.unmarshal(obj.getName(), json);
135 //convert the object created in the parser
136 json = parser.getTopEntity().marshal(false);
137 eventObject = currentVersionLoader.unmarshal(parser.getTopEntity().getName(), json);
140 if (parser.getTopEntity().equals(parser.getEntity())) {
141 //take the top level parent object passed in
144 //take the wrapped child objects (ogres are like onions)
145 eventObject = parser.getTopEntity();
149 final NotificationEvent event = new NotificationEvent(currentVersionLoader, eventHeader, eventObject, transactionId, sourceOfTruth);
151 } catch (AAIUnknownObjectException e) {
152 throw new RuntimeException("Fatal error - notification-event-header object not found!");
153 } catch (AAIUnmarshallingException e) {
154 LOGGER.error("Unmarshalling error occurred while generating UEBNotification", e);
161 * @throws AAIException the AAI exception
163 public void triggerEvents() throws AAIException {
164 for (NotificationEvent event : events) {
170 public List<NotificationEvent> getEvents() {