2 * ============LICENSE_START=======================================================
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
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=========================================================
20 package org.o.ran.oam.nf.oam.adopter.pm.rest.manager;
22 import static org.eclipse.jdt.annotation.Checks.requireNonNull;
24 import com.google.common.collect.ImmutableList;
25 import java.time.ZoneId;
26 import java.util.List;
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;
40 public final class AdaptersDeployer implements PerformanceManagementAdaptersDeployer, AutoCloseable {
42 private static final Logger LOG = LoggerFactory.getLogger(AdaptersDeployer.class);
44 private final PerformanceManagementRestAgentFactory pmRestAgentFactory;
45 private final Map<String, PerformanceManagementRestAgent> adapters = new ConcurrentHashMap<>();
48 public AdaptersDeployer(final PerformanceManagementRestAgentFactory pmRestAgentFactory) {
49 this.pmRestAgentFactory = pmRestAgentFactory;
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);
59 final Adapter adapter =
60 Adapter.builder().username(username).password(password).hostIpAddress(hostIpAddress).build();
61 final PerformanceManagementRestAgent pmRestAgent =
62 pmRestAgentFactory.createPerformanceManagementRestAgent(adapter).blockingGet();
64 adapters.put(hostIpAddress, pmRestAgent);
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);
78 public List<String> getAll() {
79 return ImmutableList.copyOf(adapters.keySet());
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);
90 public synchronized void close() {
91 for (final String host : ImmutableList.copyOf(adapters.keySet())) {
94 } catch (final Exception e) {
95 LOG.warn("Failed to delete device PM adapter {}", host);