f5f1e14a6df84668e7b915582d7379b86a211ddf
[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.config.SpringContextAware;
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.setup.SchemaVersion;
36 import org.onap.aai.setup.SchemaVersions;
37 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
38 import org.onap.aai.introspection.exceptions.AAIUnmarshallingException;
39 import org.onap.aai.logging.LogFormatTools;
40 import org.onap.aai.parsers.uri.URIToObject;
41
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 SchemaVersion notificationVersion = null;
55         /**
56          * Instantiates a new UEB notification.
57          *
58          * @param loader the loader
59          */
60         public UEBNotification(Loader loader, LoaderFactory loaderFactory, SchemaVersions schemaVersions) {
61                 events = new ArrayList<>();
62                 SchemaVersion defaultVersion = schemaVersions.getDefaultVersion();
63                 currentVersionLoader = loaderFactory.createLoaderForVersion(loader.getModelType(), defaultVersion);
64                 notificationVersion = defaultVersion;
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          * @param basePath base URI path
77          * @throws AAIException the AAI exception
78          * @throws IllegalArgumentException the illegal argument exception
79          * @throws UnsupportedEncodingException the unsupported encoding exception
80          */
81         public void createNotificationEvent(String transactionId, String sourceOfTruth, Status status, URI uri, Introspector obj, HashMap<String, Introspector> relatedObjects, String basePath) throws AAIException, UnsupportedEncodingException {
82                 
83                 String action = "UPDATE";
84                 
85                 if (status.equals(Status.CREATED)) {
86                         action = "CREATE";
87                 } else if (status.equals(Status.OK)) {
88                         action = "UPDATE";
89                 } else if (status.equals(Status.NO_CONTENT)) {
90                         action = "DELETE";
91                 }
92
93                 try {
94                         Introspector eventHeader = currentVersionLoader.introspectorFromName("notification-event-header");
95                         URIToObject parser = new URIToObject(currentVersionLoader, uri, relatedObjects);
96
97                         String entityLink = "";
98                         if ((basePath != null) && (!basePath.isEmpty())) {
99                                 if (!(basePath.startsWith("/"))) {
100                                         basePath = "/" + basePath;
101                                 }
102                                 if (!(basePath.endsWith("/"))) {
103                                         basePath = basePath + "/";
104                                 }
105                         } else {
106                                 // default
107                                 basePath = "/aai/";
108                                 if(LOGGER.isDebugEnabled()){
109                                         LOGGER.debug("Please check the schema.uri.base.path as it didn't seem to be set");
110                                 }
111                         }
112
113                         if (uri.toString().startsWith("/")) {
114                                 entityLink = basePath + notificationVersion + uri;
115                         } else {
116                                 entityLink = basePath + notificationVersion + "/" + uri;
117                         }
118                 
119
120                         eventHeader.setValue("entity-link", entityLink);
121                         eventHeader.setValue("action", action);
122                         eventHeader.setValue("entity-type", obj.getDbName());
123                         eventHeader.setValue("top-entity-type", parser.getTopEntityName());
124                         eventHeader.setValue("source-name", sourceOfTruth);
125                         eventHeader.setValue("version", notificationVersion.toString());
126                         eventHeader.setValue("id", transactionId);
127
128                         List<Object> parentList = parser.getParentList();
129                         parentList.clear();
130
131                         if (!parser.getTopEntity().equals(parser.getEntity())) {
132                                 Introspector child = obj;
133                                 if (!parser.getLoader().getVersion().equals(obj.getVersion())) {
134                                         String json = obj.marshal(false);
135                                         child = parser.getLoader().unmarshal(parser.getEntity().getName(), json);
136                                 }
137
138                                 //wrap the child object in its parents
139                                 parentList.add(child.getUnderlyingObject());
140                         }
141
142                         final Introspector eventObject;
143
144                         //convert to most resent version
145                         if (!parser.getLoader().getVersion().equals(currentVersionLoader.getVersion())) {
146                                 String json = "";
147                                 if (parser.getTopEntity().equals(parser.getEntity())) {
148                                         //convert the parent object passed in
149                                         json = obj.marshal(false);
150                                         eventObject = currentVersionLoader.unmarshal(obj.getName(), json);
151                                 } else {
152                                         //convert the object created in the parser
153                                         json = parser.getTopEntity().marshal(false);
154                                         eventObject = currentVersionLoader.unmarshal(parser.getTopEntity().getName(), json);
155                                 }
156                         } else {
157                                 if (parser.getTopEntity().equals(parser.getEntity())) {
158                                         //take the top level parent object passed in
159                                         eventObject = obj;
160                                 } else {
161                                         //take the wrapped child objects (ogres are like onions)
162                                         eventObject = parser.getTopEntity();
163                                 }
164                         }
165                         final NotificationEvent event = new NotificationEvent(currentVersionLoader, eventHeader, eventObject, transactionId, sourceOfTruth);
166                         events.add(event);
167                 } catch (AAIUnknownObjectException e) {
168                         throw new RuntimeException("Fatal error - notification-event-header object not found!");
169                 } catch (AAIUnmarshallingException e) {
170                         LOGGER.error("Unmarshalling error occurred while generating UEBNotification " + LogFormatTools.getStackTop(e));
171                 }
172         }
173         
174         /**
175          * Trigger events.
176          *
177          * @throws AAIException the AAI exception
178          */
179         public void triggerEvents() throws AAIException {
180                 for (NotificationEvent event : events) {
181                         event.trigger();
182                 }
183                 events.clear();
184         }
185         
186         public List<NotificationEvent> getEvents() {
187                 return this.events;
188         }
189         
190         
191
192 }