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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
16 * ============LICENSE_END==========================================================================
17 ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.devicemanager.maintenance.database.types;
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;
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;
38 * One filter element to describe a maintenance status for an object referenced by definition.
39 * TODO: Merge Filter and ES driven implementation
41 public class EsMaintenanceFilter implements Filter {
43 private static final Logger LOG = LoggerFactory.getLogger(EsMaintenanceFilter.class);
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 = "";
53 private final Map<java.lang.Class<? extends Augmentation<Filter>>, Augmentation<Filter>> augmentation = Collections.emptyMap();
56 private ZonedDateTime start = EMPTYDATETIME;
58 private ZonedDateTime end = EMPTYDATETIME;
60 private EsMaintenanceFilterDefinition definition = new EsMaintenanceFilterDefinition();
61 private String description = EMPTY;
64 public EsMaintenanceFilter() {
67 public EsMaintenanceFilter(Filter filter) {
68 setStartAsString(filter.getStartDate());
69 setEndAsString(filter.getEndDate());
70 description = filter.getDescription();
71 definition = new EsMaintenanceFilterDefinition(filter.getDefinition());
77 public ZonedDateTime getStart() {
81 public String getStartAsString() {
82 return toString(start);
84 public void setStart(ZonedDateTime start) {
88 public void setStartAsString(String startAsString) {
89 this.start = valueOf(startAsString);
95 public ZonedDateTime getEnd() {
99 public String getEndAsString() {
100 return toString(end);
102 public void setEnd(ZonedDateTime end) {
106 public void setEndAsString(String endAsString) {
107 this.end = valueOf(endAsString);
114 public EsMaintenanceFilterDefinition getDefinition2() {
117 public void setDefinition(EsMaintenanceFilterDefinition definition) {
118 this.definition = definition;
121 public String getDescription() {
124 public void setDescription(String description) {
125 this.description = description;
129 * Get the actual time in the Filter time zone.
130 * @return actual Time
132 public static ZonedDateTime getNow() {
133 return ZonedDateTime.now(EsMaintenanceFilterTimeZone);
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
142 public boolean isInMaintenance(String objectIdRef, String problem, ZonedDateTime now) {
143 return definition.appliesToObjectReference(objectIdRef, problem) && isInPeriod(start, end, now);
147 public String toString() {
148 return "EsMaintenanceFilter [start=" + start + ", end=" + end + ", definition=" + definition + ", description="
152 /*---------------------------------------------
153 * YANG tools related functions for interface
157 public Class<? extends DataContainer> getImplementedInterface() {
161 @SuppressWarnings("unchecked")
163 public <E extends Augmentation<Filter>> E augmentation(Class<E> augmentationType) {
164 return (E) augmentation.get(augmentationType);
168 public Definition getDefinition() {
173 public String getStartDate() {
174 return getStartAsString();
178 public String getEndDate() {
179 return getEndAsString();
182 /*---------------------------------------------
183 * private static helper functions to verify
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)
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;
198 * Convert to time value to String
199 * @param ldt ZonedDateTime format
200 * @return String output
202 public static String toString(ZonedDateTime ldt) {
206 return ldt.format(FORMAT);
210 * Convert String to time value
211 * @param zoneTimeString with time
212 * @return ZonedDateTime string
214 public static ZonedDateTime valueOf(String zoneTimeString) {
215 if (zoneTimeString == null || zoneTimeString.isEmpty()) {
216 LOG.warn("Null or empty zoneTimeString");
217 return EMPTYDATETIME;
220 return ZonedDateTime.parse(zoneTimeString, FORMAT);
221 } catch (DateTimeParseException e) {
222 LOG.warn("Can not parse zoneTimeString '{}'",zoneTimeString);
223 return EMPTYDATETIME;