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