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