456c146b164f1c4aee1db6b550bb3d417d9fb35a
[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 package org.onap.ccsdk.features.sdnr.wt.devicemanager.maintenance.database.types;
19
20 import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
21 import java.time.ZonedDateTime;
22 import java.util.ArrayList;
23 import java.util.List;
24 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.database.EsObject;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.MaintenanceModeG;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.maintenance.mode.g.Filter;
27 import org.opendaylight.yangtools.yang.binding.DataContainer;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * Database record entry for maintenance mode, as specified. Used for read operations Structure: _id
33  * : Contains the mountpoint name, must be specified before read. startTime: String representing
34  * Java LocalDateTime with absolute UTC Time endTime: String representing Java LocalDateTime with
35  * absolute UTC Time JSON Structure example { "_index": "mwtn_v1", "_type": "maintenancemode",
36  * "_id": "LumpiWave-Z3", "_score": 1, "_source": { "node": "LumpiWave-Z3", "filter": [ {
37  * "definition": { "object-id-ref": "", "problem": "" }, "description": "", "start":
38  * "2018-01-01T10:00+00:00", "end": "2018-10-10T10:00+00:00" }, { "definition": { "object-id-ref":
39  * "network-element", "problem": "power-alarm" }, "description": "", "start":
40  * "2018-01-01T10:00+00:00", "end": "2018-10-10T10:00+00:00" } ] } },
41  *
42  * Two filters for all element and one for network-element power-alarm
43  */
44
45 public class EsMaintenanceMode extends EsObject implements MaintenanceModeG {
46
47     @SuppressWarnings("unused")
48     private static final Logger LOG = LoggerFactory.getLogger(EsMaintenanceMode.class);
49
50     public static final String ESDATATYPENAME = "maintenancemode";
51
52     private static final EsMaintenanceMode NOT_IN_MAINTENANCE = new EsMaintenanceMode("notinmaintenance");
53
54     private String node;
55
56     @JsonDeserialize(as = ArrayList.class)
57     private List<EsMaintenanceFilter> filter;
58
59     private boolean active;
60
61     // for jackson
62     public EsMaintenanceMode() {
63         this.active = false;
64         this.filter = new ArrayList<>();
65     }
66
67     public EsMaintenanceMode(String mountpoint) {
68         this();
69         this.setEsId(mountpoint);
70         this.node = mountpoint;
71     }
72
73     public EsMaintenanceMode(MaintenanceModeG maintenanceModeG) {
74         this.setEsId(maintenanceModeG.getMountpointName());
75         this.node = maintenanceModeG.getNodeName();
76         List<Filter> filters = maintenanceModeG.getFilter();
77         if (filter != null) {
78             for (Filter filterElement : filters) {
79                 this.filter.add(new EsMaintenanceFilter(filterElement));
80             }
81         }
82     }
83
84
85     public String getNode() {
86         return node;
87     }
88
89     public void setNode(String node) {
90         this.node = node;
91     }
92
93     public List<EsMaintenanceFilter> getFilter2() {
94         return filter;
95     }
96
97     public void setActive(boolean a) {
98         this.active = a;
99     }
100
101     /**
102      * Replace list with new one.
103      *
104      * @param filterList new filter list
105      */
106     public void setFilter(List<EsMaintenanceFilter> filterList) {
107         this.filter = filterList;
108     }
109
110     /**
111      * Add one filter to internal list
112      *
113      * @param pFilter the Filter
114      */
115     public void addFilter(EsMaintenanceFilter pFilter) {
116         this.filter.add(pFilter);
117     }
118
119     /**
120      * Verify maintenance status
121      *
122      * @param objectIdRef NETCONF object id
123      * @param problem name that was provided
124      * @param now time to verify with
125      * @return true if in maintenance status
126      */
127     public boolean isONFObjectInMaintenance(String objectIdRef, String problem, ZonedDateTime now) {
128         if (!active) {
129             return false;
130         }
131         boolean res = false;
132         if (this != NOT_IN_MAINTENANCE) {
133             for (EsMaintenanceFilter oneFilter : filter) {
134                 if (oneFilter.isInMaintenance(objectIdRef, problem, now)) {
135                     res = true;
136                     break;
137                 }
138             }
139         }
140         return res;
141     }
142
143     /** Shortcut **/
144     public boolean isONFObjectInMaintenance(String objectIdRef, String problem) {
145         return isONFObjectInMaintenance(objectIdRef, problem, EsMaintenanceFilter.getNow());
146     }
147
148     @Override
149     public String toString() {
150         return "EsMaintenanceMode [node=" + node + ", filter=" + filter + ", active=" + active + "]";
151     }
152
153     public static EsMaintenanceMode getNotInMaintenance() {
154         return NOT_IN_MAINTENANCE;
155     }
156
157     /*---------------------------------------------
158      * yang tools related functions
159      */
160
161     @Override
162     public Class<? extends DataContainer> getImplementedInterface() {
163         return MaintenanceModeG.class;
164     }
165
166     @Override
167     public String getMountpointName() {
168         return this.getEsId();
169     }
170
171     @Override
172     public String getNodeName() {
173         return node;
174     }
175
176     @Override
177     public List<Filter> getFilter() {
178         return filter.isEmpty() ? null : new ArrayList<>(filter);
179     }
180
181 }