OAM NF Adopter REST PM
[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 static org.eclipse.jdt.annotation.Checks.requireNonNull;
23
24 import com.google.common.collect.ImmutableList;
25 import java.time.ZoneId;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Optional;
29 import java.util.concurrent.ConcurrentHashMap;
30 import org.o.ran.oam.nf.oam.adopter.pm.rest.manager.api.PerformanceManagementAdaptersDeployer;
31 import org.o.ran.oam.nf.oam.adopter.pm.rest.manager.exceptions.AlreadyPresentException;
32 import org.o.ran.oam.nf.oam.adopter.pm.rest.manager.exceptions.NotFoundException;
33 import org.o.ran.oam.nf.oam.adopter.pm.rest.manager.pojos.Adapter;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.stereotype.Service;
38
39 @Service
40 public final class AdaptersDeployer implements PerformanceManagementAdaptersDeployer, AutoCloseable {
41
42     private static final Logger LOG = LoggerFactory.getLogger(AdaptersDeployer.class);
43
44     private final PerformanceManagementRestAgentFactory pmRestAgentFactory;
45     private final Map<String, PerformanceManagementRestAgent> adapters = new ConcurrentHashMap<>();
46
47     @Autowired
48     public AdaptersDeployer(final PerformanceManagementRestAgentFactory pmRestAgentFactory) {
49         this.pmRestAgentFactory = pmRestAgentFactory;
50     }
51
52     @Override
53     public synchronized void create(final String hostIpAddress, final String username, final String password)
54             throws AlreadyPresentException {
55         LOG.info("Create device PM adapter {}", hostIpAddress);
56         if (adapters.get(hostIpAddress) != null) {
57             throw new AlreadyPresentException(hostIpAddress);
58         }
59         final Adapter adapter =
60                 Adapter.builder().username(username).password(password).hostIpAddress(hostIpAddress).build();
61         final PerformanceManagementRestAgent pmRestAgent =
62                 pmRestAgentFactory.createPerformanceManagementRestAgent(adapter).blockingGet();
63         pmRestAgent.init();
64         adapters.put(hostIpAddress, pmRestAgent);
65     }
66
67     @Override
68     public synchronized void delete(final String host) throws NotFoundException {
69         LOG.info("Adapter PM adapter removed {}", requireNonNull(host));
70         final PerformanceManagementRestAgent adapter = adapters.remove(host);
71         if (adapter == null) {
72             throw new NotFoundException(host);
73         }
74         adapter.close();
75     }
76
77     @Override
78     public List<String> getAll() {
79         return ImmutableList.copyOf(adapters.keySet());
80     }
81
82     @Override
83     public ZoneId getTimeZone(final String host) {
84         LOG.debug("Read time zone for {}", host);
85         return Optional.ofNullable(adapters.get(host)).map(
86                 PerformanceManagementRestAgent::getTimeZone).orElse(null);
87     }
88
89     @Override
90     public synchronized void close() {
91         for (final String host : ImmutableList.copyOf(adapters.keySet())) {
92             try {
93                 delete(host);
94             } catch (final Exception e) {
95                 LOG.warn("Failed to delete device PM adapter {}", host);
96             }
97         }
98         adapters.clear();
99     }
100 }