YANG Model update for A1 Adapter
[ccsdk/features.git] / sdnr / wt / devicemanager / provider / src / main / java / org / onap / ccsdk / features / sdnr / wt / devicemanager / maintenance / database / types / EsMaintenanceFilter.java
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.devicemanager.maintenance.database.types;
19
20 import com.fasterxml.jackson.annotation.JsonGetter;
21 import com.fasterxml.jackson.annotation.JsonIgnore;
22 import com.fasterxml.jackson.annotation.JsonSetter;
23 import java.time.Instant;
24 import java.time.ZoneId;
25 import java.time.ZonedDateTime;
26 import java.time.format.DateTimeFormatter;
27 import java.time.format.DateTimeParseException;
28 import java.util.Collections;
29 import java.util.Map;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.maintenance.mode.g.Filter;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.maintenance.mode.g.filter.Definition;
32 import org.opendaylight.yangtools.yang.binding.Augmentation;
33 import org.opendaylight.yangtools.yang.binding.DataContainer;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * One filter element to describe a maintenance status for an object referenced by definition.
39  * TODO: Merge Filter and ES driven implementation
40  */
41 public class EsMaintenanceFilter implements Filter {
42
43     private static final Logger LOG = LoggerFactory.getLogger(EsMaintenanceFilter.class);
44
45     private static ZoneId EsMaintenanceFilterTimeZone = ZoneId.of("UTC");
46     //private static DateTimeFormatter FORMAT = DateTimeFormatter.ISO_DATE_TIME;       // "1986-04-08T12:30:00"
47     private static DateTimeFormatter FORMAT = DateTimeFormatter.ISO_OFFSET_DATE_TIME;       // 2011-12-03T10:15:30+01:00
48     private static ZonedDateTime EMPTYDATETIME = ZonedDateTime.ofInstant(Instant.EPOCH, EsMaintenanceFilterTimeZone);
49     private static String EMPTY = "";
50
51     //yang tools
52     @JsonIgnore
53     private final Map<java.lang.Class<? extends Augmentation<Filter>>, Augmentation<Filter>> augmentation = Collections.emptyMap();
54
55     @JsonIgnore
56     private ZonedDateTime start = EMPTYDATETIME;
57     @JsonIgnore
58     private ZonedDateTime end = EMPTYDATETIME;
59
60     private EsMaintenanceFilterDefinition definition = new EsMaintenanceFilterDefinition();
61     private String description = EMPTY;
62
63     // For jackson
64     public EsMaintenanceFilter() {
65     }
66
67     public EsMaintenanceFilter(Filter filter) {
68         setStartAsString(filter.getStartDate());
69         setEndAsString(filter.getEndDate());
70         description = filter.getDescription();
71         definition = new EsMaintenanceFilterDefinition(filter.getDefinition());
72     }
73
74     /*------
75      * start
76      */
77     public ZonedDateTime getStart() {
78         return start;
79     }
80     @JsonGetter("start")
81     public String getStartAsString() {
82         return toString(start);
83     }
84     public void setStart(ZonedDateTime start) {
85         this.start = start;
86     }
87     @JsonSetter("start")
88     public void setStartAsString(String startAsString) {
89         this.start = valueOf(startAsString);
90     }
91
92     /*------
93      * end
94      */
95     public ZonedDateTime getEnd() {
96         return end;
97     }
98     @JsonGetter("end")
99     public String getEndAsString() {
100         return toString(end);
101     }
102     public void setEnd(ZonedDateTime end) {
103         this.end = end;
104     }
105     @JsonSetter("end")
106     public void setEndAsString(String endAsString) {
107         this.end = valueOf(endAsString);
108     }
109
110     /*-----------------
111      * other parameters
112      */
113
114     public EsMaintenanceFilterDefinition getDefinition2() {
115         return definition;
116     }
117     public void setDefinition(EsMaintenanceFilterDefinition definition) {
118         this.definition = definition;
119     }
120     @Override
121     public String getDescription() {
122         return description;
123     }
124     public void setDescription(String description) {
125         this.description = description;
126     }
127
128     /**
129      * Get the actual time in the Filter time zone.
130      * @return actual Time
131      */
132     public static ZonedDateTime getNow() {
133         return ZonedDateTime.now(EsMaintenanceFilterTimeZone);
134     }
135
136
137     /**
138      * Verify if the filter is active for an object
139      * @param now point of time to verify
140      * @return if the object is covered by filter and now within point of time
141      */
142     public boolean isInMaintenance(String objectIdRef, String problem, ZonedDateTime now) {
143         return definition.appliesToObjectReference(objectIdRef, problem) && isInPeriod(start, end, now);
144     }
145
146     @Override
147     public String toString() {
148         return "EsMaintenanceFilter [start=" + start + ", end=" + end + ", definition=" + definition + ", description="
149                 + description + "]";
150     }
151
152     /*---------------------------------------------
153      * YANG tools related functions for interface
154      */
155
156     @Override
157     public Class<? extends DataContainer> getImplementedInterface() {
158         return Filter.class;
159     }
160
161     @SuppressWarnings("unchecked")
162     @Override
163     public <E extends Augmentation<Filter>> E augmentation(Class<E> augmentationType) {
164         return (E) augmentation.get(augmentationType);
165     }
166
167     @Override
168     public Definition getDefinition() {
169         return definition;
170     }
171
172     @Override
173     public String getStartDate() {
174         return getStartAsString();
175     }
176
177     @Override
178     public String getEndDate() {
179         return getEndAsString();
180     }
181
182     /*---------------------------------------------
183      * private static helper functions to verify
184      */
185
186     /**
187      * Compare the if probe is within the range of start and end.
188      * @param start of range
189      * @param end of range
190      * @param probe time to verify
191      * @return boolean result true if (start <= probe <= end)
192      */
193     public static boolean isInPeriod(ZonedDateTime start, ZonedDateTime end, ZonedDateTime probe) {
194           return start.compareTo(end) < 0 && start.compareTo(probe) <= 0 && end.compareTo(probe) >= 0;
195         }
196
197     /**
198      * Convert to time value to String
199      * @param ldt ZonedDateTime format
200      * @return String output
201      */
202     public static String toString(ZonedDateTime ldt) {
203         if(ldt==null) {
204             return "";
205         }
206         return ldt.format(FORMAT);
207     }
208
209     /**
210      * Convert String to time value
211      * @param zoneTimeString with time
212      * @return ZonedDateTime string
213      */
214     public static ZonedDateTime valueOf(String zoneTimeString) {
215         if (zoneTimeString == null || zoneTimeString.isEmpty()) {
216             LOG.warn("Null or empty zoneTimeString");
217             return EMPTYDATETIME;
218         }
219         try {
220             return ZonedDateTime.parse(zoneTimeString, FORMAT);
221         } catch (DateTimeParseException e) {
222             LOG.warn("Can not parse zoneTimeString '{}'",zoneTimeString);
223             return EMPTYDATETIME;
224         }
225     }
226
227 }