Remove Byte Order Mark from License text
[aai/babel.git] / src / main / java / org / onap / aai / babel / service / InfoService.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 European Software Marketing Ltd.
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 package org.onap.aai.babel.service;
22
23 import java.time.Clock;
24 import java.time.LocalDateTime;
25 import java.time.LocalTime;
26 import java.time.format.DateTimeFormatter;
27 import java.time.temporal.ChronoUnit;
28 import java.time.temporal.Temporal;
29 import javax.ws.rs.DefaultValue;
30 import javax.ws.rs.GET;
31 import javax.ws.rs.Path;
32 import javax.ws.rs.Produces;
33 import javax.ws.rs.QueryParam;
34 import org.springframework.stereotype.Service;
35
36 /**
37  * Information service for the micro-service. Return status details to the caller.
38  *
39  * @exclude
40  */
41 @Path("/core/core-service")
42 @Service
43 public class InfoService {
44
45     private Clock clock = Clock.systemDefaultZone();
46     private LocalDateTime startTime = LocalDateTime.now(clock);
47     private long infoCount = 0L;
48
49     /**
50      * @param format is an optional setting - html requests an HTML format
51      * @return a formatted status report
52      */
53     @GET
54     @Path("/info")
55     @Produces("text/plain")
56     public String getInfo(@DefaultValue("text") @QueryParam("format") String format) {
57         return "Status: Up\n" + statusReport(clock) + "\n";
58     }
59
60     /** @return a status report showing the up time for the service */
61     public String statusReport(Clock clock) {
62         Temporal reportTime = LocalDateTime.now(clock);
63         long upTime = ChronoUnit.SECONDS.between(startTime, reportTime);
64         long upTimeDays = ChronoUnit.DAYS.between(startTime, reportTime);
65
66         StringBuilder sb = new StringBuilder("Started at ");
67         sb.append(startTime).append('\n').append("Up time ");
68         if (upTimeDays > 0) {
69             sb.append(upTimeDays).append(" day");
70             if (upTimeDays > 1) {
71                 sb.append("s");
72             }
73             sb.append(" ");
74         }
75         sb.append(LocalTime.MIDNIGHT.plusSeconds(upTime).format(DateTimeFormatter.ofPattern("HH:mm:ss"))).append('\n');
76
77         sb.append('\n').append("Info Service").append('\n');
78         sb.append("total=").append(++infoCount).append('\n');
79
80         return sb.toString();
81     }
82 }