Merge "YANG Model update for A1 Adapter"
[ccsdk/features.git] / sdnr / wt / devicemanager / provider / src / main / java / org / onap / ccsdk / features / sdnr / wt / devicemanager / maintenance / impl / MaintenanceCalculator.java
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 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;
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      * Provide default maintenanceinformation for a device
49      * @param mountpointId nodeId of device
50      * @return default data
51      */
52     static MaintenanceEntity getDefaultMaintenance(String mountpointId) {
53
54         DateAndTime now = NetconfTimeStamp.getConverter().getTimeStamp();
55
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);
63         // Reference to all
64         deviceMaintenanceModeBuilder.setObjectIdRef("");
65         deviceMaintenanceModeBuilder.setProblem("");
66         deviceMaintenanceModeBuilder.setActive(false);
67
68         return deviceMaintenanceModeBuilder.build();
69    }
70
71
72
73     /**
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
80      */
81     public static boolean isONFObjectInMaintenance(MaintenanceEntity maintenance, String objectIdRef, String problem,
82             ZonedDateTime now) {
83
84         if (maintenance != null) {
85             Boolean isActive = maintenance.isActive();
86             if (isActive != null && isActive && isInMaintenance(maintenance, objectIdRef, problem, now)) {
87                 return true;
88             }
89
90         }
91         return false;
92     }
93
94     /** Shortcut **/
95     public static boolean isONFObjectInMaintenance(MaintenanceEntity maintenance, String objectIdRef, String problem) {
96         return isONFObjectInMaintenance(maintenance, objectIdRef, problem, getNow());
97     }
98
99
100     /*---------------------------------------------
101      * private static helper functions to verify
102      */
103
104     /**
105      * Get the actual time in the Filter time zone.
106      * @return actual Time
107      */
108     private static ZonedDateTime getNow() {
109         return ZonedDateTime.now(EsMaintenanceFilterTimeZone);
110     }
111
112
113     /**
114      * Verify if the filter is active for an object
115      *
116      * @param now point of time to verify
117      * @return if the object is covered by filter and now within point of time
118      */
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);
121     }
122
123     /**
124      * Compare the if probe is within the range of start and end.
125      *
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)
130      */
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;
135     }
136
137     /**
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
143      */
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);
148         return res;
149     }
150
151     /**
152      * Convert String to time value
153      * @param zoneTimeString with time
154      * @return ZonedDateTime string
155      */
156     static ZonedDateTime valueOf(String zoneTimeString) {
157         if (zoneTimeString == null || zoneTimeString.isEmpty()) {
158             LOG.warn("Null or empty zoneTimeString");
159             return EMPTYDATETIME;
160         }
161         try {
162             return ZonedDateTime.parse(zoneTimeString, FORMAT);
163         } catch (DateTimeParseException e) {
164             LOG.warn("Can not parse zoneTimeString '{}'",zoneTimeString);
165             return EMPTYDATETIME;
166         }
167     }
168
169 }