26d029f5ee6b000aabfb33ea4b05783253517c63
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / rest / ueb / UEBNotification.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  *
20  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.rest.ueb;
23
24 import java.io.UnsupportedEncodingException;
25 import java.net.URI;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
29
30 import javax.ws.rs.core.Response.Status;
31
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;
44
45 /**
46  * The Class UEBNotification.
47  */
48 public class UEBNotification {
49
50         private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(UEBNotification.class);
51
52         private Loader currentVersionLoader = null;
53         protected List<NotificationEvent> events = null;
54         private Version notificationVersion = null;
55         
56         /**
57          * Instantiates a new UEB notification.
58          *
59          * @param loader the loader
60          */
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"));
65         }
66         
67         
68         /**
69          * Creates the notification event.
70          *
71          * @param transactionId the X-TransactionId
72          * @param sourceOfTruth 
73          * @param status the status
74          * @param uri the uri
75          * @param obj the obj
76          * @throws AAIException the AAI exception
77          * @throws IllegalArgumentException the illegal argument exception
78          * @throws UnsupportedEncodingException the unsupported encoding exception
79          */
80         public void createNotificationEvent(String transactionId, String sourceOfTruth, Status status, URI uri, Introspector obj, HashMap<String, Introspector> relatedObjects) throws AAIException, UnsupportedEncodingException {
81                 
82                 String action = "UPDATE";
83                 
84                 if (status.equals(Status.CREATED)) {
85                         action = "CREATE";
86                 } else if (status.equals(Status.OK)) {
87                         action = "UPDATE";
88                 } else if (status.equals(Status.NO_CONTENT)) {
89                         action = "DELETE";
90                 }
91
92                 try {
93                         Introspector eventHeader = currentVersionLoader.introspectorFromName("notification-event-header");
94                         URIToObject parser = new URIToObject(currentVersionLoader, uri, relatedObjects);
95
96                 String entityLink = "";
97                 if (uri.toString().startsWith("/")) {
98                         entityLink = "/aai/" + notificationVersion + uri;
99                 } else {
100                         entityLink = "/aai/" + notificationVersion + "/" + uri;
101                 }
102                 
103
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);
111
112                         List<Object> parentList = parser.getParentList();
113                         parentList.clear();
114
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);
120                                 }
121
122                                 //wrap the child object in its parents
123                                 parentList.add(child.getUnderlyingObject());
124                         }
125
126                         final Introspector eventObject;
127
128                         //convert to most resent version
129                         if (!parser.getLoader().getVersion().equals(currentVersionLoader.getVersion())) {
130                                 String json = "";
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);
135                                 } else {
136                                         //convert the object created in the parser
137                                         json = parser.getTopEntity().marshal(false);
138                                         eventObject = currentVersionLoader.unmarshal(parser.getTopEntity().getName(), json);
139                                 }
140                         } else {
141                                 if (parser.getTopEntity().equals(parser.getEntity())) {
142                                         //take the top level parent object passed in
143                                         eventObject = obj;
144                                 } else {
145                                         //take the wrapped child objects (ogres are like onions)
146                                         eventObject = parser.getTopEntity();
147                                 }
148                         }
149
150                         final NotificationEvent event = new NotificationEvent(currentVersionLoader, eventHeader, eventObject, transactionId, sourceOfTruth);
151                         events.add(event);
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);
156                 }
157         }
158         
159         /**
160          * Trigger events.
161          *
162          * @throws AAIException the AAI exception
163          */
164         public void triggerEvents() throws AAIException {
165                 for (NotificationEvent event : events) {
166                         event.trigger();
167                 }
168                 events.clear();
169         }
170         
171         public List<NotificationEvent> getEvents() {
172                 return this.events;
173         }
174         
175         
176
177 }