1 /*******************************************************************************
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==========================================================================
17 ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.devicemanager.aaiconnector.impl;
20 import java.util.List;
21 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.http.BaseHTTPResponse;
22 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.internalTypes.InventoryInformation;
23 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.netconf.ONFCoreNetworkElementRepresentation;
24 import org.onap.ccsdk.features.sdnr.wt.devicemanager.config.HtDevicemanagerConfiguration;
25 import org.onap.ccsdk.features.sdnr.wt.devicemanager.config.IConfigChangedListener;
26 import org.onap.ccsdk.features.sdnr.wt.devicemanager.config.impl.AaiConfig;
27 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.DeviceManagerImpl;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 public class AaiProviderClient implements AutoCloseable {
33 private static Logger LOG = LoggerFactory.getLogger(AaiProviderClient.class);
34 private static boolean reloadConfigFlag;
35 private static final IConfigChangedListener configChangedListener = () -> reloadConfigFlag = true;
37 private AaiConfig config;
38 private final DeviceManagerImpl deviceManager;
39 private final HtDevicemanagerConfiguration htconfig;
42 public AaiConfig getConfig() {
47 public AaiProviderClient(HtDevicemanagerConfiguration cfg, DeviceManagerImpl devMgr) {
48 this.config = cfg.getAai();
50 this.htconfig.registerConfigChangedListener(configChangedListener);
51 this.deviceManager = devMgr;
55 private void _reload() {
56 if (reloadConfigFlag) {
57 this.config = AaiConfig.reload();
58 LOG.info("config reloaded: {}", config == null ? "null" : config);
60 reloadConfigFlag = false;
63 public void onDeviceRegistered(String mountPointName) {
65 if (this.config.isOff()) {
68 ONFCoreNetworkElementRepresentation ne =
69 this.deviceManager != null ? this.deviceManager.getNeByMountpoint(mountPointName) : null;
70 this.onDeviceRegistered(mountPointName,
71 ne != null ? ne.getInventoryInformation("MWPS") : InventoryInformation.getDefault());
75 public void onDeviceRegistered(String mountPointName, InventoryInformation i) {
77 if (this.config.isOff()) {
80 new Thread(new AaiCreateRequestRunnable(mountPointName, i.getType(), i.getModel(), i.getVendor(),
81 i.getDeviceIpv4(), i.getInterfaceUuidList())).start();
84 public void onDeviceUnregistered(String mountPointName) {
86 if (this.config.isOff()) {
89 if (this.config.doDeleteOnMountPointRemoved()) {
90 new Thread(new AaiDeleteRequestRunnable(mountPointName)).start();
92 LOG.debug("prevent deleting device {} by config", mountPointName);
97 public void close() throws Exception {
98 this.htconfig.unregisterConfigChangedListener(configChangedListener);
101 private class AaiCreateRequestRunnable implements Runnable {
103 private static final int RESPCODE_NOTFOUND = BaseHTTPResponse.CODE404;
104 private static final int RESPCODE_FOUND = BaseHTTPResponse.CODE200;
105 private final AaiWebApiClient mClient;
106 private final String pnfId;
107 private final String type;
108 private final String model;
109 private final String vendor;
110 private final String oamIp;
111 private final List<String> ifaces;
112 private final int timeout;
114 public AaiCreateRequestRunnable(String pnfId, String type, String model, String vendor, String oamIp,
115 List<String> ifaces) {
119 this.vendor = vendor;
121 this.ifaces = ifaces;
122 this.timeout = AaiProviderClient.this.config.getConnectionTimeout();
123 this.mClient = new AaiWebApiClient(AaiProviderClient.this.config.getBaseUrl(),
124 AaiProviderClient.this.config.getHeaders(), AaiProviderClient.this.config.getTrustAll(),
125 AaiProviderClient.this.config.getPcks12CertificateFilename(),
126 AaiProviderClient.this.config.getPcks12CertificatePassphrase());
131 LOG.debug("check if pnfid {} exists", pnfId);
132 this.mClient.setTimeout(timeout);
133 int responseCode = this.mClient.pnfCheckIfExists(pnfId);
134 if (responseCode == RESPCODE_NOTFOUND) {
135 LOG.debug("do pnfCreate for {}", pnfId);
136 this.mClient.pnfCreate(pnfId, type, model, vendor, oamIp, ifaces);
137 } else if (responseCode == RESPCODE_FOUND) {
138 LOG.debug("pnfid {} found, nothing to do", pnfId);
140 LOG.warn("unhandled response code: {}", responseCode);
145 private class AaiDeleteRequestRunnable implements Runnable {
147 private final AaiWebApiClient mClient;
148 private final String pnfId;
149 private final int timeout;
152 public AaiDeleteRequestRunnable(String pnfId) {
154 this.timeout = AaiProviderClient.this.config.getConnectionTimeout();
155 this.mClient = new AaiWebApiClient(AaiProviderClient.this.config.getBaseUrl(),
156 AaiProviderClient.this.config.getHeaders(), AaiProviderClient.this.config.getTrustAll(),
157 AaiProviderClient.this.config.getPcks12CertificateFilename(),
158 AaiProviderClient.this.config.getPcks12CertificatePassphrase());
163 this.mClient.setTimeout(this.timeout);
164 this.mClient.pnfDelete(pnfId);