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 ******************************************************************************/
19 package org.onap.ccsdk.features.sdnr.wt.devicemanager.maintenance.impl;
21 import java.time.Instant;
22 import java.time.ZoneId;
23 import java.time.ZonedDateTime;
24 import java.time.format.DateTimeFormatter;
25 import java.time.format.DateTimeParseException;
26 import javax.annotation.Nonnull;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DateAndTime;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.MaintenanceEntity;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 public class MaintenanceCalculator {
34 private static final Logger LOG = LoggerFactory.getLogger(MaintenanceCalculator.class);
36 private static ZoneId EsMaintenanceFilterTimeZone = ZoneId.of("UTC");
37 //private static DateTimeFormatter FORMAT = DateTimeFormatter.ISO_DATE_TIME; // "1986-04-08T12:30:00"
38 private static DateTimeFormatter FORMAT = DateTimeFormatter.ISO_OFFSET_DATE_TIME; // 2011-12-03T10:15:30+01:00
39 private static ZonedDateTime EMPTYDATETIME = ZonedDateTime.ofInstant(Instant.EPOCH, EsMaintenanceFilterTimeZone);
41 /** Intended to be used static **/
42 private MaintenanceCalculator() {
49 * Verify maintenance status
50 * @param maintenance if null false, else according to settings
51 * @param objectIdRef NETCONF object id
52 * @param problem name that was provided
53 * @param now time to verify with
54 * @return true if in maintenance status
56 public static boolean isONFObjectInMaintenance(MaintenanceEntity maintenance, String objectIdRef, String problem,
59 if (maintenance != null) {
60 Boolean isActive = maintenance.isActive();
61 if (isActive != null && isActive && isInMaintenance(maintenance, objectIdRef, problem, now)) {
70 public static boolean isONFObjectInMaintenance(MaintenanceEntity maintenance, String objectIdRef, String problem) {
71 return isONFObjectInMaintenance(maintenance, objectIdRef, problem, getNow());
75 /*---------------------------------------------
76 * private static helper functions to verify
80 * Get the actual time in the Filter time zone.
83 private static ZonedDateTime getNow() {
84 return ZonedDateTime.now(EsMaintenanceFilterTimeZone);
89 * Verify if the filter is active for an object
91 * @param now point of time to verify
92 * @return if the object is covered by filter and now within point of time
94 private static boolean isInMaintenance(MaintenanceEntity maintenance, String objectIdRef, String problem, ZonedDateTime now) {
95 return appliesToObjectReference(maintenance, objectIdRef, problem) && isInPeriod(maintenance.getStart(), maintenance.getEnd(), now);
99 * Compare the if probe is within the range of start and end.
101 * @param start of range
102 * @param end of range
103 * @param probe time to verify
104 * @return boolean result true if (start <= probe <= end)
106 public static boolean isInPeriod(DateAndTime start, DateAndTime end, ZonedDateTime probe) {
107 ZonedDateTime startZT = valueOf(start.getValue());
108 ZonedDateTime endZT = valueOf(end.getValue());
109 return startZT.compareTo(endZT) < 0 && startZT.compareTo(probe) <= 0 && endZT.compareTo(probe) >= 0;
113 * Verify if the definied object is matching to the referenced object
114 * @param definition definition with parameters
115 * @param pObjectIdRef If empty considered as true
116 * @param pProblem if empty considered as true
117 * @return true if if referenced
119 private static boolean appliesToObjectReference(@Nonnull MaintenanceEntity definition, @Nonnull String pObjectIdRef, @Nonnull String pProblem) {
120 boolean res = (pObjectIdRef.isEmpty() || pObjectIdRef.contains(definition.getObjectIdRef()))
121 && (pProblem.isEmpty() || pProblem.contains(definition.getProblem()));
122 LOG.debug("Check result applies {}: {} {} against: {}", res, pObjectIdRef, pProblem, definition);
127 * Convert String to time value
128 * @param zoneTimeString with time
129 * @return ZonedDateTime string
131 static ZonedDateTime valueOf(String zoneTimeString) {
132 if (zoneTimeString == null || zoneTimeString.isEmpty()) {
133 LOG.warn("Null or empty zoneTimeString");
134 return EMPTYDATETIME;
137 return ZonedDateTime.parse(zoneTimeString, FORMAT);
138 } catch (DateTimeParseException e) {
139 LOG.warn("Can not parse zoneTimeString '{}'",zoneTimeString);
140 return EMPTYDATETIME;