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.onap.ccsdk.features.sdnr.wt.base.netconf.util.NetconfTimeStamp;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DateAndTime;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.MaintenanceBuilder;
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() {
48 * Provide default maintenanceinformation for a device
49 * @param mountpointId nodeId of device
50 * @return default data
52 static MaintenanceEntity getDefaultMaintenance(String mountpointId) {
54 DateAndTime now = NetconfTimeStamp.getConverter().getTimeStamp();
56 MaintenanceBuilder deviceMaintenanceModeBuilder = new MaintenanceBuilder();
57 deviceMaintenanceModeBuilder.setNodeId(mountpointId).setId(mountpointId);
58 // Use time from mountpoint creation
59 deviceMaintenanceModeBuilder.setDescription("");
60 // Use time from mountpoint creation
61 deviceMaintenanceModeBuilder.setStart(now);
62 deviceMaintenanceModeBuilder.setEnd(now);
64 deviceMaintenanceModeBuilder.setObjectIdRef("");
65 deviceMaintenanceModeBuilder.setProblem("");
66 deviceMaintenanceModeBuilder.setActive(false);
68 return deviceMaintenanceModeBuilder.build();
74 * Verify maintenance status
75 * @param maintenance if null false, else according to settings
76 * @param objectIdRef NETCONF object id
77 * @param problem name that was provided
78 * @param now time to verify with
79 * @return true if in maintenance status
81 public static boolean isONFObjectInMaintenance(MaintenanceEntity maintenance, String objectIdRef, String problem,
84 if (maintenance != null) {
85 Boolean isActive = maintenance.isActive();
86 if (isActive != null && isActive && isInMaintenance(maintenance, objectIdRef, problem, now)) {
95 public static boolean isONFObjectInMaintenance(MaintenanceEntity maintenance, String objectIdRef, String problem) {
96 return isONFObjectInMaintenance(maintenance, objectIdRef, problem, getNow());
100 /*---------------------------------------------
101 * private static helper functions to verify
105 * Get the actual time in the Filter time zone.
106 * @return actual Time
108 private static ZonedDateTime getNow() {
109 return ZonedDateTime.now(EsMaintenanceFilterTimeZone);
114 * Verify if the filter is active for an object
116 * @param now point of time to verify
117 * @return if the object is covered by filter and now within point of time
119 private static boolean isInMaintenance(MaintenanceEntity maintenance, String objectIdRef, String problem, ZonedDateTime now) {
120 return appliesToObjectReference(maintenance, objectIdRef, problem) && isInPeriod(maintenance.getStart(), maintenance.getEnd(), now);
124 * Compare the if probe is within the range of start and end.
126 * @param start of range
127 * @param end of range
128 * @param probe time to verify
129 * @return boolean result true if (start <= probe <= end)
131 public static boolean isInPeriod(DateAndTime start, DateAndTime end, ZonedDateTime probe) {
132 ZonedDateTime startZT = valueOf(start.getValue());
133 ZonedDateTime endZT = valueOf(end.getValue());
134 return startZT.compareTo(endZT) < 0 && startZT.compareTo(probe) <= 0 && endZT.compareTo(probe) >= 0;
138 * Verify if the definied object is matching to the referenced object
139 * @param definition definition with parameters
140 * @param pObjectIdRef If empty considered as true
141 * @param pProblem if empty considered as true
142 * @return true if if referenced
144 private static boolean appliesToObjectReference(@Nonnull MaintenanceEntity definition, @Nonnull String pObjectIdRef, @Nonnull String pProblem) {
145 boolean res = (pObjectIdRef.isEmpty() || pObjectIdRef.contains(definition.getObjectIdRef()))
146 && (pProblem.isEmpty() || pProblem.contains(definition.getProblem()));
147 LOG.debug("Check result applies {}: {} {} against: {}", res, pObjectIdRef, pProblem, definition);
152 * Convert String to time value
153 * @param zoneTimeString with time
154 * @return ZonedDateTime string
156 static ZonedDateTime valueOf(String zoneTimeString) {
157 if (zoneTimeString == null || zoneTimeString.isEmpty()) {
158 LOG.warn("Null or empty zoneTimeString");
159 return EMPTYDATETIME;
162 return ZonedDateTime.parse(zoneTimeString, FORMAT);
163 } catch (DateTimeParseException e) {
164 LOG.warn("Can not parse zoneTimeString '{}'",zoneTimeString);
165 return EMPTYDATETIME;