Fix Sonar complains
[oam/nf-oam-adopter.git] / ves-nf-oam-adopter / ves-nf-oam-adopter-pm-manager / src / main / java / org / o / ran / oam / nf / oam / adopter / pm / rest / manager / AdaptersDeployer.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  O-RAN-SC
4  *  ================================================================================
5  *  Copyright © 2021 AT&T Intellectual Property. All rights reserved.
6  *  ================================================================================
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.o.ran.oam.nf.oam.adopter.pm.rest.manager;
21
22 import com.google.common.collect.ImmutableList;
23 import java.time.ZoneId;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Optional;
27 import java.util.concurrent.ConcurrentHashMap;
28 import org.o.ran.oam.nf.oam.adopter.pm.rest.manager.api.PerformanceManagementAdaptersDeployer;
29 import org.o.ran.oam.nf.oam.adopter.pm.rest.manager.exceptions.AlreadyPresentException;
30 import org.o.ran.oam.nf.oam.adopter.pm.rest.manager.exceptions.NotFoundException;
31 import org.o.ran.oam.nf.oam.adopter.pm.rest.manager.pojos.Adapter;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.stereotype.Service;
36
37 @Service
38 public final class AdaptersDeployer implements PerformanceManagementAdaptersDeployer, AutoCloseable {
39
40     private static final Logger LOG = LoggerFactory.getLogger(AdaptersDeployer.class);
41
42     private final PerformanceManagementRestAgentFactory pmRestAgentFactory;
43     private final Map<String, PerformanceManagementRestAgent> adapters = new ConcurrentHashMap<>();
44
45     @Autowired
46     public AdaptersDeployer(final PerformanceManagementRestAgentFactory pmRestAgentFactory) {
47         this.pmRestAgentFactory = pmRestAgentFactory;
48     }
49
50     @Override
51     public synchronized void create(final String hostIpAddress, final String username, final String password)
52             throws AlreadyPresentException {
53         LOG.info("Create device PM adapter {}", hostIpAddress);
54         if (adapters.get(hostIpAddress) != null) {
55             throw new AlreadyPresentException(hostIpAddress);
56         }
57         final Adapter adapter =
58                 Adapter.builder().username(username).password(password).hostIpAddress(hostIpAddress).build();
59         final PerformanceManagementRestAgent pmRestAgent =
60                 pmRestAgentFactory.createPerformanceManagementRestAgent(adapter).blockingGet();
61         pmRestAgent.init();
62         adapters.put(hostIpAddress, pmRestAgent);
63     }
64
65     @Override
66     public synchronized void delete(final String host) throws NotFoundException {
67         LOG.info("Adapter PM adapter removed {}", host);
68         final PerformanceManagementRestAgent adapter = adapters.remove(host);
69         if (adapter == null) {
70             throw new NotFoundException(host);
71         }
72         adapter.close();
73     }
74
75     @Override
76     public List<String> getAll() {
77         return ImmutableList.copyOf(adapters.keySet());
78     }
79
80     @Override
81     public ZoneId getTimeZone(final String host) {
82         LOG.debug("Read time zone for {}", host);
83         return Optional.ofNullable(adapters.get(host)).map(
84                 PerformanceManagementRestAgent::getTimeZone).orElse(null);
85     }
86
87     @Override
88     public synchronized void close() {
89         for (final String host : ImmutableList.copyOf(adapters.keySet())) {
90             try {
91                 delete(host);
92             } catch (final Exception e) {
93                 LOG.warn("Failed to delete device PM adapter {}", host);
94             }
95         }
96         adapters.clear();
97     }
98 }