Moving all files to root directory
[appc.git] / appc-provider / appc-provider-bundle / src / main / java / org / openecomp / appc / provider / ResponseHeaderBuilder.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.appc.provider;
23
24 import org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.TIMESTAMP;
25 import org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.common.response.header.CommonResponseHeader;
26 import org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.common.response.header.CommonResponseHeaderBuilder;
27 import org.openecomp.appc.util.Time;
28
29 import java.text.DateFormat;
30 import java.text.SimpleDateFormat;
31
32
33 /**
34  * Builds the responses from the APP-C services according to the YANG domainmodel
35  *
36  * @since Nov 16, 2015
37  * @version $Id$
38  */
39 public class ResponseHeaderBuilder {
40
41     /**
42      * The date/time formatter to format timestamps.
43      */
44     @SuppressWarnings("nls")
45     public static final DateFormat FORMATTER = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS");
46     public static final DateFormat ZULU_FORMATTER = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SS'Z'");
47
48     /**
49      * Private default constructor prevents instantiation
50      */
51     private ResponseHeaderBuilder() {
52     }
53
54     /**
55      * This method builds the common response header and returns it to the caller for integration into the response
56      *
57      * @param success
58      *            True or false indicating the outcome of the operation. True indicates that the operation was
59      *            successful, false indicates it failed.
60      * @param requestId
61      *            The original request id for the service
62      * @param reason
63      *            The reason for the failure if the success flag is false. If success is true, the reason is not used.
64      * @param duration
65      *            The duration of the request processing
66      * @return The common response header to be returned to the caller.
67      */
68     @SuppressWarnings("nls")
69     public static CommonResponseHeader buildHeader(Boolean success, String requestId, String reason, long duration) {
70         CommonResponseHeaderBuilder builder = new CommonResponseHeaderBuilder();
71
72         TIMESTAMP timestamp = new TIMESTAMP(FORMATTER.format(Time.utcDate()));
73         builder.setServiceRequestId(requestId);
74         builder.setCompleted(timestamp);
75         builder.setDuration(duration);
76         builder.setSuccess(success);
77         
78         if (success.equals(Boolean.TRUE)) {
79             builder.setReason("Success");
80         } else {
81             builder.setReason(reason);
82         }
83
84         return builder.build();
85     }
86
87
88 }