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 com.google.common.collect.ImmutableList;
23 import java.time.ZoneId;
24 import java.util.List;
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;
38 public final class AdaptersDeployer implements PerformanceManagementAdaptersDeployer, AutoCloseable {
40 private static final Logger LOG = LoggerFactory.getLogger(AdaptersDeployer.class);
42 private final PerformanceManagementRestAgentFactory pmRestAgentFactory;
43 private final Map<String, PerformanceManagementRestAgent> adapters = new ConcurrentHashMap<>();
46 public AdaptersDeployer(final PerformanceManagementRestAgentFactory pmRestAgentFactory) {
47 this.pmRestAgentFactory = pmRestAgentFactory;
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);
58 Adapter.builder().username(username).password(password).hostIpAddress(hostIpAddress).build();
59 final PerformanceManagementRestAgent pmRestAgent =
60 pmRestAgentFactory.createPerformanceManagementRestAgent(adapter).blockingGet();
62 adapters.put(hostIpAddress, pmRestAgent);
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);
76 public List<String> getAll() {
77 return ImmutableList.copyOf(adapters.keySet());
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);
88 public synchronized void close() {
89 for (final String host : ImmutableList.copyOf(adapters.keySet())) {
92 } catch (final Exception e) {
93 LOG.warn("Failed to delete device PM adapter {}", host);