aboutsummaryrefslogtreecommitdiffstats
path: root/sdnr/wt/devicemanager/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/maintenance/impl/MaintenanceCalculator.java
blob: db218552b0cdd48a827b2390f30def35b9625b34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
 * ============LICENSE_START========================================================================
 * ONAP : ccsdk feature sdnr wt
 * =================================================================================================
 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
 * =================================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 * ============LICENSE_END==========================================================================
 */

package org.onap.ccsdk.features.sdnr.wt.devicemanager.maintenance.impl;

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import javax.annotation.Nonnull;

import org.onap.ccsdk.features.sdnr.wt.common.HtAssert;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DateAndTime;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.MaintenanceEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MaintenanceCalculator {

    private static final Logger LOG = LoggerFactory.getLogger(MaintenanceCalculator.class);

    private static ZoneId EsMaintenanceFilterTimeZone = ZoneId.of("UTC");
    //private static DateTimeFormatter FORMAT = DateTimeFormatter.ISO_DATE_TIME;       // "1986-04-08T12:30:00"
    private static DateTimeFormatter FORMAT = DateTimeFormatter.ISO_OFFSET_DATE_TIME; // 2011-12-03T10:15:30+01:00
    private static ZonedDateTime EMPTYDATETIME = ZonedDateTime.ofInstant(Instant.EPOCH, EsMaintenanceFilterTimeZone);

    /** Intended to be used static **/
    private MaintenanceCalculator() {}

    /**
     * Verify maintenance status
     * 
     * @param maintenance if null false, else according to settings
     * @param objectIdRef NETCONF object id
     * @param problem name that was provided
     * @param now time to verify with
     * @return true if in maintenance status
     */
    public static boolean isONFObjectInMaintenance(MaintenanceEntity maintenance, String objectIdRef, String problem,
            ZonedDateTime now) {

        if (maintenance != null) {
            Boolean isActive = maintenance.isActive();
            if (isActive != null && isActive && isInMaintenance(maintenance, objectIdRef, problem, now)) {
                return true;
            }

        }
        return false;
    }

    /** Shortcut **/
    public static boolean isONFObjectInMaintenance(MaintenanceEntity maintenance, String objectIdRef, String problem) {
        return isONFObjectInMaintenance(maintenance, objectIdRef, problem, getNow());
    }

    /*---------------------------------------------
     * private static helper functions to verify
     */

    /**
     * Get the actual time in the Filter time zone.
     * 
     * @return actual Time
     */
    private static ZonedDateTime getNow() {
        return ZonedDateTime.now(EsMaintenanceFilterTimeZone);
    }

    /**
     * Verify if the filter is active for an object
     *
     * @param now point of time to verify
     * @return if the object is covered by filter and now within point of time
     */
    private static boolean isInMaintenance(MaintenanceEntity maintenance, String objectIdRef, String problem,
            ZonedDateTime now) {
        return appliesToObjectReference(maintenance, objectIdRef, problem)
                && isInPeriod(maintenance.getStart(), maintenance.getEnd(), now);
    }

    /**
     * Compare the if probe is within the range of start and end.
     *
     * @param start of range
     * @param end of range
     * @param probe time to verify
     * @return boolean result true if (start <= probe <= end)
     */
    public static boolean isInPeriod(DateAndTime start, DateAndTime end, ZonedDateTime probe) {
        HtAssert.nonnull(start, end, probe);
        ZonedDateTime startZT = valueOf(start.getValue());
        ZonedDateTime endZT = valueOf(end.getValue());
        return startZT.compareTo(endZT) < 0 && startZT.compareTo(probe) <= 0 && endZT.compareTo(probe) >= 0;
    }

    /**
     * Verify if the definied object is matching to the referenced object
     * 
     * @param definition definition with parameters
     * @param pObjectIdRef object-id-ref of fault notification
     * @param pProblem problem of fault notification
     * @return true if if referenced
     */
    private static boolean appliesToObjectReference(@Nonnull MaintenanceEntity definition, @Nonnull String pObjectIdRef,
            @Nonnull String pProblem) {
        HtAssert.nonnull(definition, pObjectIdRef, pProblem);
        boolean res = (definition.getObjectIdRef() == null || pObjectIdRef.contains(definition.getObjectIdRef()))
                && (definition.getProblem() == null || pProblem.contains(definition.getProblem()));
        LOG.debug("Check result applies {}: {} {} against: {}", res, pObjectIdRef, pProblem, definition);
        return res;
    }

    /**
     * Convert String to time value
     * 
     * @param zoneTimeString with time
     * @return ZonedDateTime string
     */
    public static ZonedDateTime valueOf(String zoneTimeString) {
        if (zoneTimeString == null || zoneTimeString.isEmpty()) {
            LOG.warn("Null or empty zoneTimeString");
            return EMPTYDATETIME;
        }
        try {
            return ZonedDateTime.parse(zoneTimeString, FORMAT);
        } catch (DateTimeParseException e) {
            LOG.warn("Can not parse zoneTimeString '{}'", zoneTimeString);
            return EMPTYDATETIME;
        }
    }

}