2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.provider;
26 import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.TIMESTAMP;
27 import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.common.response.header.CommonResponseHeader;
28 import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.common.response.header.CommonResponseHeaderBuilder;
29 import org.onap.appc.util.Time;
31 import java.text.DateFormat;
32 import java.text.SimpleDateFormat;
36 * Builds the responses from the APP-C services according to the YANG domainmodel
41 public class ResponseHeaderBuilder {
44 * The date/time formatter to format timestamps.
46 @SuppressWarnings("nls")
47 public static final DateFormat FORMATTER = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS");
48 public static final DateFormat ZULU_FORMATTER = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SS'Z'");
51 * Private default constructor prevents instantiation
53 private ResponseHeaderBuilder() {
57 * This method builds the common response header and returns it to the caller for integration into the response
60 * True or false indicating the outcome of the operation. True indicates that the operation was
61 * successful, false indicates it failed.
63 * The original request id for the service
65 * The reason for the failure if the success flag is false. If success is true, the reason is not used.
67 * The duration of the request processing
68 * @return The common response header to be returned to the caller.
70 @SuppressWarnings("nls")
71 public static CommonResponseHeader buildHeader(Boolean success, String requestId, String reason, long duration) {
72 CommonResponseHeaderBuilder builder = new CommonResponseHeaderBuilder();
74 TIMESTAMP timestamp = new TIMESTAMP(FORMATTER.format(Time.utcDate()));
75 builder.setServiceRequestId(requestId);
76 builder.setCompleted(timestamp);
77 builder.setDuration(duration);
78 builder.setSuccess(success);
80 if (success.equals(Boolean.TRUE)) {
81 builder.setReason("Success");
83 builder.setReason(reason);
86 return builder.build();