2  * ============LICENSE_START========================================================================
 
   3  * ONAP : ccsdk feature sdnr wt
 
   4  * =================================================================================================
 
   5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
 
   6  * =================================================================================================
 
   7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 
   8  * in compliance with the License. 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 distributed under the License
 
  13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 
  14  * or implied. See the License for the specific language governing permissions and limitations under
 
  16  * ============LICENSE_END==========================================================================
 
  18 package org.onap.ccsdk.features.sdnr.wt.devicemanager.aaiconnector.impl;
 
  20 import java.util.List;
 
  21 import java.util.Optional;
 
  22 import javax.annotation.Nonnull;
 
  23 import org.onap.ccsdk.features.sdnr.wt.common.HtAssert;
 
  24 import org.onap.ccsdk.features.sdnr.wt.common.configuration.ConfigurationFileRepresentation;
 
  25 import org.onap.ccsdk.features.sdnr.wt.common.configuration.filechange.IConfigChangedListener;
 
  26 import org.onap.ccsdk.features.sdnr.wt.common.http.BaseHTTPResponse;
 
  27 import org.onap.ccsdk.features.sdnr.wt.devicemanager.aaiconnector.impl.config.AaiConfig;
 
  28 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.DeviceManagerImpl;
 
  29 import org.onap.ccsdk.features.sdnr.wt.devicemanager.ne.service.InventoryProvider;
 
  30 import org.onap.ccsdk.features.sdnr.wt.devicemanager.ne.service.NetworkElement;
 
  31 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.AaiService;
 
  32 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.InventoryInformationDcae;
 
  33 import org.slf4j.Logger;
 
  34 import org.slf4j.LoggerFactory;
 
  36 public class AaiProviderClient implements AaiService, AutoCloseable {
 
  38     private static Logger LOG = LoggerFactory.getLogger(AaiProviderClient.class);
 
  39     @SuppressWarnings("unused") // @TODO Remove code
 
  40     private static boolean reloadConfigFlag;
 
  41     private static final IConfigChangedListener configChangedListener = () -> reloadConfigFlag = true;
 
  43     private final AaiConfig config;
 
  44     private final DeviceManagerImpl deviceManager;
 
  45     private final ConfigurationFileRepresentation htconfig;
 
  48     public AaiProviderClient(@Nonnull ConfigurationFileRepresentation cfg, DeviceManagerImpl devMgr) {
 
  49         HtAssert.nonnull(cfg);
 
  50         this.config = new AaiConfig(cfg);
 
  51         LOG.debug("AaiProviderClient configuration setting: {}", this.config);
 
  53         this.htconfig.registerConfigChangedListener(configChangedListener);
 
  54         this.deviceManager = devMgr;
 
  58     public AaiConfig getConfig() {
 
  62     public void onDeviceRegistered(String mountPointName) {
 
  63         if (this.config.isOff()) {
 
  66         NetworkElement ne = this.deviceManager != null ? this.deviceManager.getNeByMountpoint(mountPointName) : null;
 
  67         Optional<InventoryProvider> oip = ne != null ? ne.getService(InventoryProvider.class) : Optional.empty();
 
  68         this.onDeviceRegistered(mountPointName,
 
  69                 oip.isPresent() ? oip.get().getInventoryInformation("MWPS") : InventoryInformationDcae.getDefault());
 
  72     public void onDeviceRegistered(String mountPointName, InventoryInformationDcae i) {
 
  73         if (this.config.isOff()) {
 
  76         new Thread(new AaiCreateRequestRunnable(mountPointName, i.getType(), i.getModel(), i.getVendor(),
 
  77                 i.getDeviceIpv4(), i.getInterfaceUuidList())).start();
 
  80     public void onDeviceUnregistered(String mountPointName) {
 
  81         if (this.config.isOff()) {
 
  84         if (this.config.doDeleteOnMountPointRemoved()) {
 
  85             new Thread(new AaiDeleteRequestRunnable(mountPointName)).start();
 
  87             LOG.debug("prevent deleting device {} by config", mountPointName);
 
  92     public void close() throws Exception {
 
  93         this.htconfig.unregisterConfigChangedListener(configChangedListener);
 
  96     private class AaiCreateRequestRunnable implements Runnable {
 
  98         private static final int RESPCODE_NOTFOUND = BaseHTTPResponse.CODE404;
 
  99         private static final int RESPCODE_FOUND = BaseHTTPResponse.CODE200;
 
 100         private final AaiWebApiClient mClient;
 
 101         private final String pnfId;
 
 102         private final String type;
 
 103         private final String model;
 
 104         private final String vendor;
 
 105         private final String oamIp;
 
 106         private final List<String> ifaces;
 
 107         private final int timeout;
 
 109         public AaiCreateRequestRunnable(String pnfId, String type, String model, String vendor, String oamIp,
 
 110                 List<String> ifaces) {
 
 114             this.vendor = vendor;
 
 116             this.ifaces = ifaces;
 
 117             this.timeout = AaiProviderClient.this.config.getConnectionTimeout();
 
 118             this.mClient = new AaiWebApiClient(AaiProviderClient.this.config.getBaseUrl(),
 
 119                     AaiProviderClient.this.config.getHeaders(), AaiProviderClient.this.config.getTrustAll(),
 
 120                     AaiProviderClient.this.config.getPcks12CertificateFilename(),
 
 121                     AaiProviderClient.this.config.getPcks12CertificatePassphrase());
 
 126             LOG.debug("check if pnfid {} exists", pnfId);
 
 127             this.mClient.setTimeout(timeout);
 
 128             int responseCode = this.mClient.pnfCheckIfExists(pnfId);
 
 129             if (responseCode == RESPCODE_NOTFOUND) {
 
 130                 LOG.debug("do pnfCreate for {}", pnfId);
 
 131                 this.mClient.pnfCreate(pnfId, type, model, vendor, oamIp, ifaces);
 
 132             } else if (responseCode == RESPCODE_FOUND) {
 
 133                 LOG.debug("pnfid {} found, nothing to do", pnfId);
 
 135                 LOG.warn("unhandled response code: {}", responseCode);
 
 140     private class AaiDeleteRequestRunnable implements Runnable {
 
 142         private final AaiWebApiClient mClient;
 
 143         private final String pnfId;
 
 144         private final int timeout;
 
 147         public AaiDeleteRequestRunnable(String pnfId) {
 
 149             this.timeout = AaiProviderClient.this.config.getConnectionTimeout();
 
 150             this.mClient = new AaiWebApiClient(AaiProviderClient.this.config.getBaseUrl(),
 
 151                     AaiProviderClient.this.config.getHeaders(), AaiProviderClient.this.config.getTrustAll(),
 
 152                     AaiProviderClient.this.config.getPcks12CertificateFilename(),
 
 153                     AaiProviderClient.this.config.getPcks12CertificatePassphrase());
 
 158             this.mClient.setTimeout(this.timeout);
 
 159             this.mClient.pnfDelete(pnfId);