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==========================================================================
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;
28 import org.onap.ccsdk.features.sdnr.wt.common.HtAssert;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DateAndTime;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.MaintenanceEntity;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 public class MaintenanceCalculator {
36 private static final Logger LOG = LoggerFactory.getLogger(MaintenanceCalculator.class);
38 private static ZoneId EsMaintenanceFilterTimeZone = ZoneId.of("UTC");
39 //private static DateTimeFormatter FORMAT = DateTimeFormatter.ISO_DATE_TIME; // "1986-04-08T12:30:00"
40 private static DateTimeFormatter FORMAT = DateTimeFormatter.ISO_OFFSET_DATE_TIME; // 2011-12-03T10:15:30+01:00
41 private static ZonedDateTime EMPTYDATETIME = ZonedDateTime.ofInstant(Instant.EPOCH, EsMaintenanceFilterTimeZone);
43 /** Intended to be used static **/
44 private MaintenanceCalculator() {}
47 * Verify maintenance status
49 * @param maintenance if null false, else according to settings
50 * @param objectIdRef NETCONF object id
51 * @param problem name that was provided
52 * @param now time to verify with
53 * @return true if in maintenance status
55 public static boolean isONFObjectInMaintenance(MaintenanceEntity maintenance, String objectIdRef, String problem,
58 if (maintenance != null) {
59 Boolean isActive = maintenance.isActive();
60 if (isActive != null && isActive && isInMaintenance(maintenance, objectIdRef, problem, now)) {
69 public static boolean isONFObjectInMaintenance(MaintenanceEntity maintenance, String objectIdRef, String problem) {
70 return isONFObjectInMaintenance(maintenance, objectIdRef, problem, getNow());
73 /*---------------------------------------------
74 * private static helper functions to verify
78 * Get the actual time in the Filter time zone.
82 private static ZonedDateTime getNow() {
83 return ZonedDateTime.now(EsMaintenanceFilterTimeZone);
87 * Verify if the filter is active for an object
89 * @param now point of time to verify
90 * @return if the object is covered by filter and now within point of time
92 private static boolean isInMaintenance(MaintenanceEntity maintenance, String objectIdRef, String problem,
94 return appliesToObjectReference(maintenance, objectIdRef, problem)
95 && 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 HtAssert.nonnull(start, end, probe);
108 ZonedDateTime startZT = valueOf(start.getValue());
109 ZonedDateTime endZT = valueOf(end.getValue());
110 return startZT.compareTo(endZT) < 0 && startZT.compareTo(probe) <= 0 && endZT.compareTo(probe) >= 0;
114 * Verify if the definied object is matching to the referenced object
116 * @param definition definition with parameters
117 * @param pObjectIdRef object-id-ref of fault notification
118 * @param pProblem problem of fault notification
119 * @return true if if referenced
121 private static boolean appliesToObjectReference(@Nonnull MaintenanceEntity definition, @Nonnull String pObjectIdRef,
122 @Nonnull String pProblem) {
123 HtAssert.nonnull(definition, pObjectIdRef, pProblem);
124 boolean res = (definition.getObjectIdRef() == null || pObjectIdRef.contains(definition.getObjectIdRef()))
125 && (definition.getProblem() == null || pProblem.contains(definition.getProblem()));
126 LOG.debug("Check result applies {}: {} {} against: {}", res, pObjectIdRef, pProblem, definition);
131 * Convert String to time value
133 * @param zoneTimeString with time
134 * @return ZonedDateTime string
136 public static ZonedDateTime valueOf(String zoneTimeString) {
137 if (zoneTimeString == null || zoneTimeString.isEmpty()) {
138 LOG.warn("Null or empty zoneTimeString");
139 return EMPTYDATETIME;
142 return ZonedDateTime.parse(zoneTimeString, FORMAT);
143 } catch (DateTimeParseException e) {
144 LOG.warn("Can not parse zoneTimeString '{}'", zoneTimeString);
145 return EMPTYDATETIME;