bd71d5ba18f3c11e59b4db96875d20448816b5e9
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt
4  * =================================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6  * =================================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  * ============LICENSE_END==========================================================================
17  */
18 package org.onap.ccsdk.features.sdnr.wt.dataprovider.model.types;
19
20 import java.time.LocalDateTime;
21 import java.time.OffsetDateTime;
22 import java.time.ZoneOffset;
23 import java.time.ZonedDateTime;
24 import java.time.format.DateTimeFormatter;
25 import java.time.format.DateTimeParseException;
26 import java.util.Date;
27 import org.eclipse.jdt.annotation.Nullable;
28 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.NetconfTimeStamp;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DateAndTime;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * 2019/06/17 Redesign to ZonedDateTime because of sync problems.
35  *
36  * Function is handling the NETCONF and the format used by database and restconf communication.
37  *
38  * Input supported for the formats used in NETCONF messages:
39  *
40  * Format1 ISO 8601 2017-01-18T11:44:49.482-05:00
41  *
42  * Format2 NETCONF - pattern from ietf-yang-types "2013-07-15" Pattern:
43  * "\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[\+\-](\d{2}):(\d{2}))"
44  *
45  * Format3 NETCONF DateAndTime CoreModel-CoreFoundationModule-TypeDefinitions vom 2016-07-01 Example1: 20170118114449.1Z
46  * Example2: 20170118114449.1-0500 Pattern: "\d{4}\d{2}\d{2}\d{2}\d{2}\d{2}.\d+?(Z|[\+\-](\d{2})(\d{2}))" typedef
47  * DateAndTime { description "This primitive type defines the date and time according to the following structure:
48  * 'yyyyMMddhhmmss.s[Z|{+|-}HHMm]' where: yyyy '0000'..'9999' year MM '01'..'12' month dd '01'..'31' day hh '00'..'23'
49  * hour mm '00'..'59' minute ss '00'..'59' second s '.0'..'.9' tenth of second (set to '.0' if EMS or NE cannot support
50  * this granularity) Z 'Z' indicates UTC (rather than local time) {+|-} '+' or '-' delta from UTC HH '00'..'23' time
51  * zone difference in hours Mm '00'..'59' time zone difference in minutes."; type string; } Format4 E/// specific
52  * Example1: 2017-01-23T13:32:38-05:00 Example2: 2017-01-23T13:32-05:00
53  *
54  * Input formats netconfTime as String according the formats given above
55  *
56  * Return format is String in ISO8601 Format for database and presentation.
57  *
58  * Example formats: 1) ISO8601. Example 2017-01-18T11:44:49.482-05:00 2) Microwave ONF. Examples 20170118114449.1Z,
59  * 20170118114449.1-0500 3.1) Ericson. Example: 2017-01-23T13:32:38-05:00 3.2) Ericson. Example: 2017-01-23T13:32-05:00
60  * Always 10 Groups, 1:Year 2:Month 3:day 4:Hour 5:minute 6:optional sec 7:optional ms 8:optional Z or 9:offset
61  * signedhour 10:min
62  *
63  * Template: private static final NetconfTimeStamp NETCONFTIME_CONVERTER = NetconfTimeStamp.getConverter();
64  */
65
66 public class NetconfTimeStampImpl implements NetconfTimeStamp {
67     private static final Logger LOG = LoggerFactory.getLogger(NetconfTimeStamp.class);
68
69     private static final NetconfTimeStamp CONVERTER = new NetconfTimeStampImpl();
70     private static final String TESTPATTERNSTRING = "2017-01-01T00:00:00.0Z";
71     //private static final String INITIALPATTERN = "0000-00-00T00:00:00.0Z";
72
73     private static final DateAndTime TESTPATTERN = new DateAndTime(TESTPATTERNSTRING);
74
75     /**
76      * Specify the input format expected from netconf, and from specific devices.
77      */
78     private static DateTimeFormatter formatterInput =
79             DateTimeFormatter.ofPattern("" + "[yyyy-MM-dd'T'HH:mm[:ss][.SSS][.SS][.S][xxx][xx][X][Z]]"
80                     + "[yyyyMMddHHmmss[.SSS][.SS][.S][xxx][xx][X][Z]]").withZone(ZoneOffset.UTC);
81
82     /**
83      * Specify output format that is used internally
84      */
85     private static DateTimeFormatter formatterOutput =
86             DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.S'Z'").withZone(ZoneOffset.UTC);
87
88     /**
89      * Use static access
90      */
91     private NetconfTimeStampImpl() {}
92
93     /*
94      * ------------------------------------ Public function
95      */
96
97     /**
98      * Use this function to get the converter
99      *
100      * @return global converter
101      */
102     public static NetconfTimeStamp getConverter() {
103         return CONVERTER;
104     }
105
106     /**
107      * Get actual timestamp as NETCONF specific type NETCONF/YANG 1.0 Format
108      *
109      * @return String with Date in NETCONF/YANG Format Version 1.0.
110      */
111     @Override
112     public String getTimeStampAsNetconfString() {
113         return ZonedDateTime.now(ZoneOffset.UTC).format(formatterOutput);
114     }
115
116     /**
117      * Get actual timestamp as NETCONF specific type NETCONF/YANG 1.0 Format
118      *
119      * @return String with Date in NETCONF/YANG Format Version 1.0.
120      */
121     @Override
122     public String getTimeStampAsNetconfString(Date date) {
123         return ZonedDateTime.ofInstant(date.toInstant(), ZoneOffset.UTC).format(formatterOutput);
124     }
125
126
127
128     /**
129      * Get actual timestamp as NETCONF specific type NETCONF/YANG 1.0 Format in GMT
130      *
131      * @return DateAndTime Type 1.0. Date in NETCONF/YANG Format Version 1.0.
132      */
133     @Override
134     public DateAndTime getTimeStamp() {
135         return DateAndTime.getDefaultInstance(getTimeStampAsNetconfString());
136     }
137
138     /**
139      * Get time from date as NETCONF specific type NETCONF/YANG 1.0 Format in GMT
140      *
141      * @param date specifying the date and time
142      * @return DateAndTime Type 1.0. Date in NETCONF/YANG Format Version 1.0.
143      */
144     @Override
145     public DateAndTime getTimeStamp(Date date) {
146         return DateAndTime.getDefaultInstance(getTimeStampAsNetconfString(date));
147     }
148
149     /**
150      * Get time from date as NETCONF specific type NETCONF/YANG 1.0 Format in GMT
151      *
152      * @param date specifying the date and time
153      * @return DateAndTime Type 1.0. Date in NETCONF/YANG Format Version 1.0.
154      */
155     @Override
156     public DateAndTime getTimeStamp(String date) {
157         return DateAndTime.getDefaultInstance(date);
158     }
159
160     /**
161      * Return the String with a NETCONF time converted to long
162      *
163      * @param netconfTime as String according the formats given above
164      * @return Epoch milliseconds
165      * @throws IllegalArgumentException In case of no compliant time format definition for the string
166      */
167     @Override
168     public long getTimeStampFromNetconfAsMilliseconds(String netconfTime) throws IllegalArgumentException {
169         try {
170             long utcMillis = doParse(netconfTime).toInstant().toEpochMilli();
171             return utcMillis;
172         } catch (DateTimeParseException e) {
173             throw new IllegalArgumentException(
174                     "No pattern for NETCONF data string: " + netconfTime + " Msg:" + e.getMessage());
175         }
176     }
177
178     /**
179      * Deliver String result.
180      *
181      * @param netconfTime as String according the formats given above
182      * @return If successful: String in ISO8601 Format for database and presentation. If "wrong formed input" the Input
183      *         string with the prefix "Maleformed date" is delivered back.
184      */
185     @Override
186     public String getTimeStampFromNetconf(String netconfTime) {
187         try {
188             String inputUTC = doParse(netconfTime).format(formatterOutput);
189             return inputUTC;
190         } catch (Exception e) {
191             LOG.info(e.getMessage());
192         }
193         LOG.debug("No pattern for NETCONF data string: {}", netconfTime);
194         return "Malformed date: " + netconfTime; // Error handling
195     }
196
197     /**
198      * Return testpattern
199      * @return
200      */
201     public static @Nullable DateAndTime getTestpatternDateAndTime() {
202         return TESTPATTERN;
203     }
204
205     /*----------------------------------------------------
206      * Private functions
207      */
208
209     private OffsetDateTime doParse(String netconfTime) {
210         return OffsetDateTime.parse(netconfTime, formatterInput);
211     }
212
213     @Override
214     public Date getDateFromNetconf(String netconfTime) {
215         return Date.from(LocalDateTime.parse(netconfTime, formatterInput).atZone(ZoneOffset.UTC).toInstant());
216     }
217
218     @Override
219     public String getTimeStampAsNetconfString(LocalDateTime dt) {
220         return formatterOutput.format(dt);
221     }
222
223 }