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.util.ArrayList;
22 import java.util.List;
23 import org.eclipse.jdt.annotation.NonNull;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.onap.ccsdk.features.sdnr.wt.common.HtAssert;
26 import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient;
27 import org.onap.ccsdk.features.sdnr.wt.database.EsDataObjectReaderWriter2;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.Entity;
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.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.NetworkElementConnectionBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.NetworkElementConnectionEntity;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 public class HtDatabaseMaintenanceService {
38 private static final Logger LOG = LoggerFactory.getLogger(HtDatabaseMaintenanceService.class);
40 private final EsDataObjectReaderWriter2<MaintenanceEntity> maintenanceRW;
41 private final EsDataObjectReaderWriter2<NetworkElementConnectionEntity> requiredNeRW;
43 HtDatabaseMaintenanceService(@NonNull HtDatabaseClient client) throws ClassNotFoundException {
44 HtAssert.nonnull(client);
46 // Create control structure
47 maintenanceRW = new EsDataObjectReaderWriter2<>(client, Entity.Maintenancemode, MaintenanceEntity.class,
48 MaintenanceBuilder.class).setEsIdAttributeName("_id");
50 requiredNeRW = new EsDataObjectReaderWriter2<>(client, Entity.NetworkelementConnection,
51 NetworkElementConnectionEntity.class, NetworkElementConnectionBuilder.class)
52 .setEsIdAttributeName("_id");
57 * Get existing object for mountpoint to manage maintenance mode
58 * @return Object with configuration
60 @Nullable MaintenanceEntity getMaintenance(String mountpointId) {
61 MaintenanceEntity deviceMaintenanceMode = null;
62 if (maintenanceRW != null || mountpointId != null) {
63 deviceMaintenanceMode = maintenanceRW.read(mountpointId);
65 return deviceMaintenanceMode;
68 MaintenanceEntity setMaintenance(MaintenanceEntity m) {
69 if (maintenanceRW != null) {
70 if (maintenanceRW.write(m, m.getNodeId() ) == null) {
71 throw new IllegalArgumentException("Problem writing to database: "+m.getId());
73 LOG.info("Wrote maintenance object {}", m.toString());
78 List<MaintenanceEntity> getAll() {
79 return maintenanceRW != null ? maintenanceRW.doReadAll().getHits() : new ArrayList<>();
82 MaintenanceEntity createIfNotExists(String mountpointId) {
83 MaintenanceEntity deviceMaintenanceMode = null;
84 if (maintenanceRW != null) {
85 deviceMaintenanceMode = maintenanceRW.read(mountpointId);
86 if (deviceMaintenanceMode == null) {
87 LOG.debug("creating empty maintenance object in database");
88 deviceMaintenanceMode = MaintenanceCalculator.getDefaultMaintenance(mountpointId);
89 maintenanceRW.write(deviceMaintenanceMode, mountpointId);
91 LOG.debug("maintenance object already exists in database");
94 LOG.warn("cannot create maintenance obj. db reader/writer is null");
96 return deviceMaintenanceMode;
99 void deleteIfNotRequired(String mountPointNodeName) {
101 if (!this.isRequireNe(mountPointNodeName)) {
102 if (maintenanceRW != null) {
103 LOG.debug("removing maintenance object in database for " + mountPointNodeName);
104 maintenanceRW.remove(mountPointNodeName);
106 LOG.warn("cannot create maintenance obj. db reader/writer is null");
115 * Check in required ne if entry exists for mountpointNodeName
117 * @param mountPointNodeName
120 private boolean isRequireNe(String mountPointNodeName) {
121 NetworkElementConnectionEntity ne = null;
122 if (requiredNeRW != null) {
123 LOG.debug("searching for entry in required-networkelement for " + mountPointNodeName);
124 ne = requiredNeRW.read(mountPointNodeName);
126 LOG.warn("cannot read db. no db reader writer initialized");