X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;ds=inline;f=ves-nf-oam-adopter%2Fves-nf-oam-adopter-pm-manager%2Fsrc%2Fmain%2Fjava%2Forg%2Fo%2Fran%2Foam%2Fnf%2Foam%2Fadopter%2Fpm%2Frest%2Fmanager%2FAdaptersDeployer.java;fp=ves-nf-oam-adopter%2Fves-nf-oam-adopter-pm-manager%2Fsrc%2Fmain%2Fjava%2Forg%2Fo%2Fran%2Foam%2Fnf%2Foam%2Fadopter%2Fpm%2Frest%2Fmanager%2FAdaptersDeployer.java;h=924373dbc68e7ec7ac24c3c0103193fb4a7bf94a;hb=04d12d50d1d6144008b49f4484e879a3e145885c;hp=0000000000000000000000000000000000000000;hpb=adb95fb55dc438e9e08db9cfae9ee098b448f512;p=oam%2Fnf-oam-adopter.git diff --git a/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 b/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 new file mode 100644 index 0000000..924373d --- /dev/null +++ b/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 @@ -0,0 +1,100 @@ +/* + * ============LICENSE_START======================================================= + * O-RAN-SC + * ================================================================================ + * Copyright © 2021 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.o.ran.oam.nf.oam.adopter.pm.rest.manager; + +import static org.eclipse.jdt.annotation.Checks.requireNonNull; + +import com.google.common.collect.ImmutableList; +import java.time.ZoneId; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.ConcurrentHashMap; +import org.o.ran.oam.nf.oam.adopter.pm.rest.manager.api.PerformanceManagementAdaptersDeployer; +import org.o.ran.oam.nf.oam.adopter.pm.rest.manager.exceptions.AlreadyPresentException; +import org.o.ran.oam.nf.oam.adopter.pm.rest.manager.exceptions.NotFoundException; +import org.o.ran.oam.nf.oam.adopter.pm.rest.manager.pojos.Adapter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public final class AdaptersDeployer implements PerformanceManagementAdaptersDeployer, AutoCloseable { + + private static final Logger LOG = LoggerFactory.getLogger(AdaptersDeployer.class); + + private final PerformanceManagementRestAgentFactory pmRestAgentFactory; + private final Map adapters = new ConcurrentHashMap<>(); + + @Autowired + public AdaptersDeployer(final PerformanceManagementRestAgentFactory pmRestAgentFactory) { + this.pmRestAgentFactory = pmRestAgentFactory; + } + + @Override + public synchronized void create(final String hostIpAddress, final String username, final String password) + throws AlreadyPresentException { + LOG.info("Create device PM adapter {}", hostIpAddress); + if (adapters.get(hostIpAddress) != null) { + throw new AlreadyPresentException(hostIpAddress); + } + final Adapter adapter = + Adapter.builder().username(username).password(password).hostIpAddress(hostIpAddress).build(); + final PerformanceManagementRestAgent pmRestAgent = + pmRestAgentFactory.createPerformanceManagementRestAgent(adapter).blockingGet(); + pmRestAgent.init(); + adapters.put(hostIpAddress, pmRestAgent); + } + + @Override + public synchronized void delete(final String host) throws NotFoundException { + LOG.info("Adapter PM adapter removed {}", requireNonNull(host)); + final PerformanceManagementRestAgent adapter = adapters.remove(host); + if (adapter == null) { + throw new NotFoundException(host); + } + adapter.close(); + } + + @Override + public List getAll() { + return ImmutableList.copyOf(adapters.keySet()); + } + + @Override + public ZoneId getTimeZone(final String host) { + LOG.debug("Read time zone for {}", host); + return Optional.ofNullable(adapters.get(host)).map( + PerformanceManagementRestAgent::getTimeZone).orElse(null); + } + + @Override + public synchronized void close() { + for (final String host : ImmutableList.copyOf(adapters.keySet())) { + try { + delete(host); + } catch (final Exception e) { + LOG.warn("Failed to delete device PM adapter {}", host); + } + } + adapters.clear(); + } +}