Change the header to SO
[so.git] / mso-api-handlers / mso-api-handler-infra / src / main / java / org / openecomp / mso / apihandlerinfra / NetworkMsoInfraRequest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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.mso.apihandlerinfra;
22
23 import java.io.StringReader;
24 import java.io.StringWriter;
25 import java.sql.Timestamp;
26 import java.text.SimpleDateFormat;
27 import java.util.Date;
28
29 import javax.ws.rs.core.Response;
30 import javax.xml.bind.JAXBContext;
31 import javax.xml.bind.JAXBException;
32 import javax.xml.bind.Marshaller;
33 import javax.xml.bind.Unmarshaller;
34 import javax.xml.transform.OutputKeys;
35 import javax.xml.transform.Transformer;
36 import javax.xml.transform.TransformerFactory;
37 import javax.xml.transform.dom.DOMSource;
38 import javax.xml.transform.sax.SAXSource;
39 import javax.xml.transform.stream.StreamResult;
40
41 import org.hibernate.Session;
42 import org.w3c.dom.Document;
43 import org.w3c.dom.Element;
44 import org.w3c.dom.Node;
45 import org.w3c.dom.NodeList;
46 import org.xml.sax.InputSource;
47
48 import org.openecomp.mso.apihandler.common.ErrorNumbers;
49 import org.openecomp.mso.apihandler.common.ValidationException;
50 import org.openecomp.mso.apihandlerinfra.networkbeans.ActionType;
51 import org.openecomp.mso.apihandlerinfra.networkbeans.NetworkInputs;
52 import org.openecomp.mso.apihandlerinfra.networkbeans.NetworkRequest;
53 import org.openecomp.mso.apihandlerinfra.networkbeans.ObjectFactory;
54 import org.openecomp.mso.apihandlerinfra.networkbeans.RequestInfo;
55 import org.openecomp.mso.apihandlerinfra.networkbeans.RequestStatusType;
56 import org.openecomp.mso.logger.MessageEnum;
57 import org.openecomp.mso.logger.MsoLogger;
58 import org.openecomp.mso.properties.MsoJavaProperties;
59 import org.openecomp.mso.db.AbstractSessionFactoryManager;
60 import org.openecomp.mso.requestsdb.InfraActiveRequests;
61 import org.openecomp.mso.requestsdb.RequestsDatabase;
62 import org.openecomp.mso.requestsdb.RequestsDbSessionFactoryManager;
63
64 public class NetworkMsoInfraRequest {
65
66     private String requestId;
67     private String requestXML;
68     private String requestUri;
69     private RequestInfo rinfo;
70     private NetworkInputs networkInputs;
71     private String networkParams;
72     private ActionType action;
73     private String errorMessage;
74     private String httpResponse;
75     private String responseBody;
76     private RequestStatusType status;
77     private long startTime;
78     private long progress = Constants.PROGRESS_REQUEST_RECEIVED;
79
80     private static MsoLogger msoLogger = MsoLogger.getMsoLogger (MsoLogger.Catalog.APIH);
81     private static final String NOT_PROVIDED = "not provided";
82
83     protected AbstractSessionFactoryManager requestsDbSessionFactoryManager = new RequestsDbSessionFactoryManager ();
84     
85     NetworkMsoInfraRequest (String requestId) {
86         this.requestId = requestId;
87         this.startTime = System.currentTimeMillis();
88         MsoLogger.setLogContext (requestId, null);
89
90     }
91
92     // Parse request XML
93     void parse (String reqXML, String version, MsoJavaProperties props) throws ValidationException {
94
95         msoLogger.debug ("Validating the request");
96
97         this.requestXML = reqXML;
98
99         NetworkRequest networkReq = null;
100         boolean isWrongRootElement = false;
101
102         try {
103             JAXBContext jaxbContext = JAXBContext.newInstance (NetworkRequest.class);
104             Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller ();
105
106             InputSource inputSource = new InputSource (new StringReader (reqXML));
107             SAXSource source = new SAXSource (inputSource);
108
109             if (reqXML.contains ("network-request") && !reqXML.contains ("vnf-request")) {
110                 networkReq = jaxbUnmarshaller.unmarshal (source, NetworkRequest.class).getValue ();
111             } else {
112                 isWrongRootElement = true;
113             }
114
115         } catch (Exception e) {
116                  msoLogger.error (MessageEnum.APIH_VNFREQUEST_VALIDATION_ERROR, "", "", MsoLogger.ErrorCode.DataError, "Exception when parsing reqXML", e);
117             throw new ValidationException ("format for network request");
118         }
119
120         if (isWrongRootElement) {
121                 msoLogger.error (MessageEnum.APIH_REQUEST_VALIDATION_ERROR_REASON, "root element is not correct", "", "", MsoLogger.ErrorCode.DataError, "root element <network-request> expected");
122             throw new ValidationException ("root element <network-request> expected");
123         }
124
125         if (networkReq == null) {
126             throw new ValidationException ("network-request");
127         }
128
129         this.rinfo = networkReq.getRequestInfo ();
130
131         if (this.rinfo == null) {
132             throw new ValidationException ("request-info");
133         }
134         
135         action = this.rinfo.getAction ();
136         if (action == null) {
137             throw new ValidationException ("action");
138         }
139         
140         if (!InfraUtils.isActionAllowed (props, "network", version, action.value ())) {
141                 throw new ValidationException ("action allowable for version " + version + " of network request");              
142         }
143
144         this.networkInputs = networkReq.getNetworkInputs ();
145         if (this.networkInputs == null) {
146             throw new ValidationException ("network-inputs");
147         }
148         
149         // Verify that the elements correspond to the version
150         
151         if (version.equals(Constants.SCHEMA_VERSION_V1)) {
152                         if (this.networkInputs.getBackoutOnFailure() != null || this.networkInputs.getAicCloudRegion() != null ||
153                                         this.networkInputs.getServiceInstanceId() != null) {
154                                 throw new ValidationException ("format for v1 version of network request");
155                         }
156         }
157         else if (version.equals(Constants.SCHEMA_VERSION_V2)) {
158                         if (this.networkInputs.getServiceType() != null || this.networkInputs.getAicNodeClli() != null ||
159                                         this.networkInputs.getServiceInstanceId() != null) {
160                                 throw new ValidationException ("format for v2 version of network request");
161                         }
162         }
163         else if (version.equals(Constants.SCHEMA_VERSION_V3)) {
164                 if (this.networkInputs.getServiceType() != null || this.networkInputs.getAicNodeClli() != null) {
165                         throw new ValidationException ("format for v3 version of network request");
166                 }
167     }
168
169         switch (action) {            
170             case UPDATE:
171             case DELETE:
172                 if (this.networkInputs.getNetworkId () == null) {
173                     throw new ValidationException ("network-id");
174                 }
175                 break;
176             default:
177                 break;
178         }
179
180         if (ActionType.CREATE.equals (action) && this.networkInputs.getNetworkName () == null) {
181             throw new ValidationException ("network-name");
182         }
183
184         if (this.networkInputs.getNetworkType () == null) {
185             throw new ValidationException ("network-type");
186         }
187         
188         if (!version.equals(Constants.SCHEMA_VERSION_V1) && this.networkInputs.getServiceId () == null) {
189                 throw new ValidationException ("service-id ");
190         }
191         
192         if (this.networkInputs.getServiceType () != null && this.networkInputs.getServiceId () != null) {
193                 throw new ValidationException ("service-type or service-id ");
194         }
195         
196         if (version.equals(Constants.SCHEMA_VERSION_V1) && this.networkInputs.getAicNodeClli () == null) {
197                 throw new ValidationException ("aic-node-clli");
198         }
199         
200         if ((version.equals (Constants.SCHEMA_VERSION_V2) || version.equals (Constants.SCHEMA_VERSION_V3)) && (this.networkInputs.getAicCloudRegion () == null || this.networkInputs.getAicCloudRegion ().isEmpty())) {
201                 throw new ValidationException ("aic-cloud-region");
202         }
203         
204         if (version.equals(Constants.SCHEMA_VERSION_V3) && this.networkInputs.getServiceInstanceId () == null) {
205                 throw new ValidationException ("service-instance-id");
206         }
207
208         if (ActionType.CREATE.equals (action)) {
209             if (this.networkInputs.getTenantId () == null) {
210                 throw new ValidationException ("tenant-id");
211             }
212         }
213
214         
215         Object vpN = networkReq.getNetworkParams ();
216
217         if (vpN != null) {
218             Node node = (Node) vpN;
219             Document doc = node.getOwnerDocument ();
220             this.networkParams = domToStr (doc);
221         }
222
223         msoLogger.debug ("NetworkParams: " + this.networkParams);
224
225         msoLogger.debug ("Request valid");
226
227         // Rebuild the request string for BPEL to include request-id
228         rinfo.setRequestId (this.requestId);
229         networkReq.setRequestInfo (rinfo);
230
231         StringWriter stringWriter = new StringWriter ();
232         try {
233             JAXBContext jaxbContext = JAXBContext.newInstance (NetworkRequest.class);
234             Marshaller jaxbMarshaller = jaxbContext.createMarshaller ();
235
236             // output pretty printed
237             jaxbMarshaller.setProperty (Marshaller.JAXB_FORMATTED_OUTPUT, true);
238
239             jaxbMarshaller.marshal (networkReq, stringWriter);
240
241         } catch (JAXBException e) {
242             msoLogger.debug ("Exception: ", e);
243         }
244
245         this.requestXML = stringWriter.toString ().replace ("http://org.openecomp/mso/infra/network-request",
246                                                             "http://org.openecomp/mso/infra/vnf-request");
247         msoLogger.debug("REQUEST XML to BPEL: " + this.requestXML);
248
249     }
250
251     public void createRequestRecord (Status status) {
252
253         long startTime = System.currentTimeMillis ();
254
255         Session session = null;
256         try {
257
258             session = requestsDbSessionFactoryManager.getSessionFactory ().openSession ();
259             session.beginTransaction ();
260
261             InfraActiveRequests aq = new InfraActiveRequests ();
262             aq.setRequestId (requestId);
263
264             Timestamp startTimeStamp = new Timestamp (System.currentTimeMillis());
265             if (rinfo != null) {
266                 if (rinfo.getAction () != null) {
267                     aq.setAction (rinfo.getAction ().value ());
268                     aq.setRequestAction (RequestActionMap.getMappedRequestAction (rinfo.getAction ().value ()));
269                 }
270                 aq.setSource (rinfo.getSource ());
271             } else {
272                 // Set up mandatory parameters
273                 aq.setAction (NOT_PROVIDED);
274                 aq.setRequestAction (NOT_PROVIDED);
275             }
276
277             aq.setRequestBody (this.requestXML);
278             aq.setRequestScope (ModelType.network.name ());
279
280             if (networkInputs != null) {
281                 if (networkInputs.getNetworkId () != null) {
282                     aq.setVnfId (networkInputs.getNetworkId ());
283                 }
284                 if (networkInputs.getNetworkName () != null) {
285                     aq.setVnfName (networkInputs.getNetworkName ());
286                 }
287                 if (networkInputs.getNetworkType () != null) {
288                     aq.setVnfType (networkInputs.getNetworkType ());
289                 }
290                 if (networkInputs.getServiceInstanceId () != null) {
291                     aq.setServiceInstanceId (networkInputs.getServiceInstanceId ());
292                 }
293                 if (networkInputs.getServiceType () != null) {
294                     aq.setServiceType (networkInputs.getServiceType ());
295                 }
296                 if (networkInputs.getServiceId () != null) {
297                     aq.setServiceType (networkInputs.getServiceId ());
298                 }
299                 if (networkInputs.getAicNodeClli () != null) {
300                     aq.setAicNodeClli (networkInputs.getAicNodeClli ());
301                 }
302                 if (networkInputs.getTenantId () != null) {
303                     aq.setTenantId (networkInputs.getTenantId ());
304                 }
305                 if (networkInputs.getProvStatus () != null) {
306                     aq.setProvStatus (networkInputs.getProvStatus ());
307                 }
308
309             }
310             aq.setStartTime (startTimeStamp);
311             aq.setRequestStatus (status.toString ());
312             aq.setLastModifiedBy (Constants.MODIFIED_BY_APIHANDLER);
313             aq.setRequestType ("NETWORK");
314
315             if (networkParams != null) {
316                 msoLogger.debug ("Storing networkParams: " + networkParams);
317                 aq.setVnfParams (this.networkParams);
318             }
319
320             if ((status == Status.FAILED) || (status == Status.COMPLETE)) {
321                 aq.setStatusMessage (this.errorMessage);
322                 aq.setResponseBody (this.responseBody);
323
324                 Timestamp endTimeStamp = new Timestamp (System.currentTimeMillis());
325                 aq.setEndTime (endTimeStamp);                
326             } 
327             aq.setProgress (this.progress);
328             
329
330             msoLogger.debug ("About to insert a record");
331
332             session.save (aq);
333             session.getTransaction ().commit ();
334             session.close ();
335             msoLogger.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "RequestDB", "saveRequest", null);
336
337         } catch (Exception e) {
338                 msoLogger.error (MessageEnum.APIH_DB_INSERT_EXC, "", "", MsoLogger.ErrorCode.SchemaError, "Exception in createRequestRecord", e);
339             msoLogger.recordMetricEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.DBAccessError, e.getMessage (), "RequestDB", "saveRequest", null);
340             if (session != null) {
341                 session.close ();
342             }
343             if (!status.equals (Status.FAILED)) {
344                 throw e;
345             }
346         }
347     }
348
349     public void updateFinalStatus (Status status) {
350         try {
351             (RequestsDatabase.getInstance()).updateInfraFinalStatus (requestId,
352                                                      status.toString (),
353                                                      this.errorMessage,
354                                                      this.progress,
355                                                      this.responseBody,
356                                                      Constants.MODIFIED_BY_APIHANDLER);
357         } catch (Exception e) {
358                 msoLogger.error (MessageEnum.APIH_DB_UPDATE_EXC, e.getMessage (), "", "", MsoLogger.ErrorCode.SchemaError, "Exception in updateFinalStatus");
359             msoLogger.debug ("Exception: ", e);
360         }
361     }
362
363     public Response buildResponse (int httpResponseCode, String errorCode, InfraActiveRequests inProgress) {
364         return buildResponseWithError (httpResponseCode, errorCode, inProgress, null);
365     }
366
367     public Response buildResponseWithError (int httpResponseCode,
368                                             String errorCode,
369                                             InfraActiveRequests inProgress,
370                                             String errorString) {
371
372         ObjectFactory beansObjectFactory = new ObjectFactory ();
373
374         NetworkRequest vr = beansObjectFactory.createNetworkRequest ();
375
376         RequestInfo ri = beansObjectFactory.createRequestInfo ();
377
378         ri.setRequestId (requestId);
379         ri.setRequestStatus (this.status);
380         ri.setAction (this.rinfo.getAction ());
381         ri.setSource (this.rinfo.getSource ());
382
383         String errorMsg = null;
384         if (errorCode != null) {
385             // If error code is actually an XML error response from BPEL, treat it specially:
386             if (!Messages.errors.containsKey (errorCode)) {
387                 if (errorCode.length () > 300) {
388                     errorMsg = errorCode.substring (0, 299);
389                 } else {
390                     errorMsg = errorCode;
391                 }
392
393             } else {
394
395                 if (inProgress == null) {
396                     if (errorCode.equals (ErrorNumbers.RECIPE_DOES_NOT_EXIST)) {
397                         errorMsg = String.format (Messages.errors.get (errorCode), "network", errorString);
398                     } else {
399                         errorMsg = String.format (Messages.errors.get (errorCode), errorString);
400                     }
401                 } else if (errorCode.equals (ErrorNumbers.LOCKED_CREATE_ON_THE_SAME_VNF_NAME_IN_PROGRESS)) {
402                     errorMsg = String.format (Messages.errors.get (errorCode),
403                                               "network",
404                                               inProgress.getVnfName (),
405                                               inProgress.getRequestStatus (),
406                                               "network");
407                 } else if (errorCode.equals (ErrorNumbers.LOCKED_SAME_ACTION_AND_VNF_ID)) {
408                     errorMsg = String.format (Messages.errors.get (errorCode),
409                                               "network",
410                                               inProgress.getVnfId (),
411                                               inProgress.getRequestStatus (),
412                                               inProgress.getAction (),
413                                               "network");
414                 }
415             }
416
417             ri.setStatusMessage (errorMsg);
418             this.errorMessage = errorMsg;
419         }
420         ri.setProgress ((int) this.progress);
421
422         Date startDate = new Date (this.startTime);
423         SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss.SSS");
424         String startTimeString = sdf.format (startDate);
425
426         ri.setStartTime (startTimeString);
427
428         vr.setRequestInfo (ri);
429         vr.setNetworkInputs (this.networkInputs);
430
431         StringWriter stringWriter = new StringWriter ();
432         try {
433             JAXBContext jaxbContext = JAXBContext.newInstance (NetworkRequest.class);
434             Marshaller jaxbMarshaller = jaxbContext.createMarshaller ();
435             jaxbMarshaller.setProperty (Marshaller.JAXB_FORMATTED_OUTPUT, true);
436
437             jaxbMarshaller.marshal (vr, stringWriter);
438
439         } catch (JAXBException e) {
440             msoLogger.debug ("Exception: ", e);
441         }
442
443         String response = stringWriter.toString ();
444
445         this.httpResponse = Integer.toString (httpResponseCode);
446         this.responseBody = response;
447
448         // Log the failed request into the MSO Requests database
449
450         return Response.status (httpResponseCode).entity (response).build ();
451
452     }
453
454     public Response buildResponseFailedValidation (int httpResponseCode, String exceptionMessage) {
455
456         ObjectFactory beansObjectFactory = new ObjectFactory ();
457         NetworkRequest vr = beansObjectFactory.createNetworkRequest ();
458
459         RequestInfo ri = beansObjectFactory.createRequestInfo ();
460         ri.setRequestId (requestId);
461
462         if (this.rinfo != null) {
463             if (this.rinfo.getAction () != null) {
464                 ri.setAction (this.rinfo.getAction ());
465             } else {
466                 ri.setAction (ActionType.NOT_PROVIDED);
467             }
468             if (this.rinfo.getSource () != null) {
469                 ri.setSource (this.rinfo.getSource ());
470             }
471         } else {
472             ri.setAction (ActionType.NOT_PROVIDED);
473         }
474
475         // Nothing more is expected for this request
476
477         String errorMsg = String.format (Messages.errors.get (ErrorNumbers.REQUEST_FAILED_SCHEMA_VALIDATION
478                                                               + "_service"),
479                                          exceptionMessage);
480         ri.setStatusMessage (errorMsg);
481         this.errorMessage = errorMsg;
482
483         ri.setProgress ((int) this.progress);
484         ri.setRequestStatus (RequestStatusType.FAILED);
485         Date startDate = new Date (this.startTime);
486         SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss.SSS");
487         String startTimeString = sdf.format (startDate);
488
489         ri.setStartTime (startTimeString);
490
491         vr.setRequestInfo (ri);
492         vr.setNetworkInputs (this.networkInputs);
493
494         StringWriter stringWriter = new StringWriter ();
495         try {
496             JAXBContext jaxbContext = JAXBContext.newInstance (NetworkRequest.class);
497             Marshaller jaxbMarshaller = jaxbContext.createMarshaller ();
498
499             // output pretty printed
500             jaxbMarshaller.setProperty (Marshaller.JAXB_FORMATTED_OUTPUT, true);
501
502             jaxbMarshaller.marshal (vr, stringWriter);
503
504         } catch (JAXBException e) {
505             msoLogger.debug ("Error marshalling", e);
506         }
507
508         String response = stringWriter.toString ();
509
510         this.httpResponse = Integer.toString (httpResponseCode);
511         this.responseBody = response;
512
513         return Response.status (httpResponseCode).entity (response).build ();
514     }
515
516     public String getRequestUri () {
517         return requestUri;
518     }
519
520     public void setRequestUri (String requestUri) {
521         this.requestUri = requestUri;
522     }
523
524     public NetworkInputs getNetworkInputs () {
525         return networkInputs;
526     }
527
528     public RequestInfo getRequestInfo () {
529         return rinfo;
530     }
531
532     public String getResponseBody () {
533         return responseBody;
534     }
535
536     public void setResponseBody (String responseBody) {
537         this.responseBody = responseBody;
538     }
539
540     public String getHttpResponse () {
541         return httpResponse;
542     }
543
544     public void setHttpResponse (String httpResponse) {
545         this.httpResponse = httpResponse;
546     }
547
548     public String getRequestId () {
549         return requestId;
550     }
551
552     public String getRequestXML () {
553         return requestXML;
554     }
555
556     public void setRequestXML (String requestXML) {
557         this.requestXML = requestXML;
558     }
559
560     public RequestStatusType getStatus () {
561         return status;
562     }
563
564     public void setStatus (RequestStatusType status) {
565         this.status = status;
566         switch (status) {
567         case FAILED:
568         case COMPLETE:
569                 this.progress = Constants.PROGRESS_REQUEST_COMPLETED;
570                 break;
571         case IN_PROGRESS:
572                 this.progress = Constants.PROGRESS_REQUEST_IN_PROGRESS;
573                 break;
574         }
575     }
576     
577     public String getServiceType () {
578         if (this.networkInputs.getServiceType () != null) 
579                 return this.networkInputs.getServiceType ();
580         if (this.networkInputs.getServiceId () != null) 
581                 return this.networkInputs.getServiceId ();
582         return null;
583     }
584
585     public static String domToStr (Document doc) {
586         if (doc == null) {
587             return null;
588         }
589
590         try {
591             StringWriter sw = new StringWriter ();
592             StreamResult sr = new StreamResult (sw);
593             TransformerFactory tf = TransformerFactory.newInstance ();
594             Transformer t = tf.newTransformer ();
595             t.setOutputProperty (OutputKeys.STANDALONE, "yes");
596             NodeList nl = doc.getDocumentElement ().getChildNodes ();
597             DOMSource source = null;
598             for (int x = 0; x < nl.getLength (); x++) {
599                 Node e = nl.item (x);
600                 if (e instanceof Element) {
601                     source = new DOMSource (e);
602                     break;
603                 }
604             }
605             if (source != null) {
606                 t.transform (source, sr);
607
608                 String s = sw.toString ();
609                 return s;
610             }
611
612             return null;
613
614         } catch (Exception e) {
615                 msoLogger.error (MessageEnum.APIH_DOM2STR_ERROR, "", "", MsoLogger.ErrorCode.DataError, "Exception in domToStr", e);
616         }
617         return null;
618     }
619 }