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