bce8f52dec3faf8c7076fda59afac67b3ace1117
[ccsdk/features.git] /
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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
15  * the License.
16  * ============LICENSE_END==========================================================================
17  */
18
19 package org.onap.ccsdk.features.sdnr.wt.devicemanager.maintenance.impl;
20
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
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;
33
34 public class MaintenanceCalculator {
35
36         private static final Logger LOG = LoggerFactory.getLogger(MaintenanceCalculator.class);
37
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);
42
43         /** Intended to be used static **/
44         private MaintenanceCalculator() {
45         }
46
47         /**
48          * 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
54          */
55         public static boolean isONFObjectInMaintenance(MaintenanceEntity maintenance, String objectIdRef, String problem,
56                         ZonedDateTime now) {
57
58                 if (maintenance != null) {
59                         Boolean isActive = maintenance.isActive();
60                         if (isActive != null && isActive && isInMaintenance(maintenance, objectIdRef, problem, now)) {
61                                 return true;
62                         }
63
64                 }
65                 return false;
66         }
67
68         /** Shortcut **/
69         public static boolean isONFObjectInMaintenance(MaintenanceEntity maintenance, String objectIdRef, String problem) {
70                 return isONFObjectInMaintenance(maintenance, objectIdRef, problem, getNow());
71         }
72
73         /*---------------------------------------------
74          * private static helper functions to verify
75          */
76
77         /**
78          * Get the actual time in the Filter time zone.
79          * @return actual Time
80          */
81         private static ZonedDateTime getNow() {
82                 return ZonedDateTime.now(EsMaintenanceFilterTimeZone);
83         }
84
85         /**
86          * Verify if the filter is active for an object
87          *
88          * @param now point of time to verify
89          * @return if the object is covered by filter and now within point of time
90          */
91         private static boolean isInMaintenance(MaintenanceEntity maintenance, String objectIdRef, String problem,
92                         ZonedDateTime now) {
93                 return appliesToObjectReference(maintenance, objectIdRef, problem)
94                                 && isInPeriod(maintenance.getStart(), maintenance.getEnd(), now);
95         }
96
97         /**
98          * Compare the if probe is within the range of start and end.
99          *
100          * @param start of range
101          * @param end   of range
102          * @param probe time to verify
103          * @return boolean result true if (start <= probe <= end)
104          */
105         public static boolean isInPeriod(DateAndTime start, DateAndTime end, ZonedDateTime probe) {
106                 HtAssert.nonnull(start,end,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;
110         }
111
112         /**
113          * Verify if the definied object is matching to the referenced object
114          * @param definition definition with parameters
115          * @param pObjectIdRef object-id-ref of fault notification
116          * @param pProblem problem of fault notification
117          * @return true if if referenced
118          */
119         private static boolean appliesToObjectReference(@Nonnull MaintenanceEntity definition, @Nonnull String pObjectIdRef,
120                         @Nonnull String pProblem) {
121                 HtAssert.nonnull(definition,pObjectIdRef,pProblem);
122                 boolean res = (definition.getObjectIdRef()==null ||  pObjectIdRef.contains(definition.getObjectIdRef()))
123                                 && (definition.getProblem()==null || pProblem.contains(definition.getProblem()));
124                 LOG.debug("Check result applies {}: {} {} against: {}", res, pObjectIdRef, pProblem, definition);
125                 return res;
126         }
127
128         /**
129          * Convert String to time value
130          * @param zoneTimeString with time
131          * @return ZonedDateTime string
132          */
133         public static ZonedDateTime valueOf(String zoneTimeString) {
134                 if (zoneTimeString == null || zoneTimeString.isEmpty()) {
135                         LOG.warn("Null or empty zoneTimeString");
136                         return EMPTYDATETIME;
137                 }
138                 try {
139                         return ZonedDateTime.parse(zoneTimeString, FORMAT);
140                 } catch (DateTimeParseException e) {
141                         LOG.warn("Can not parse zoneTimeString '{}'", zoneTimeString);
142                         return EMPTYDATETIME;
143                 }
144         }
145
146 }