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()) {
67 this.deviceManager != null ? this.deviceManager.getConnectedNeByMountpoint(mountPointName) : null;
68 Optional<InventoryProvider> oip = ne != null ? ne.getService(InventoryProvider.class) : Optional.empty();
69 this.onDeviceRegistered(mountPointName,
70 oip.isPresent() ? oip.get().getInventoryInformation("MWPS") : InventoryInformationDcae.getDefault());
73 public void onDeviceRegistered(String mountPointName, InventoryInformationDcae i) {
74 if (this.config.isOff()) {
77 new Thread(new AaiCreateRequestRunnable(mountPointName, i.getType(), i.getModel(), i.getVendor(),
78 i.getDeviceIpv4(), i.getInterfaceUuidList())).start();
81 public void onDeviceUnregistered(String mountPointName) {
82 if (this.config.isOff()) {
85 if (this.config.doDeleteOnMountPointRemoved()) {
86 new Thread(new AaiDeleteRequestRunnable(mountPointName)).start();
88 LOG.debug("prevent deleting device {} by config", mountPointName);
93 public void close() throws Exception {
94 this.htconfig.unregisterConfigChangedListener(configChangedListener);
97 private class AaiCreateRequestRunnable implements Runnable {
99 private static final int RESPCODE_NOTFOUND = BaseHTTPResponse.CODE404;
100 private static final int RESPCODE_FOUND = BaseHTTPResponse.CODE200;
101 private final AaiWebApiClient mClient;
102 private final String pnfId;
103 private final String type;
104 private final String model;
105 private final String vendor;
106 private final String oamIp;
107 private final List<String> ifaces;
108 private final int timeout;
110 public AaiCreateRequestRunnable(String pnfId, String type, String model, String vendor, String oamIp,
111 List<String> ifaces) {
115 this.vendor = vendor;
117 this.ifaces = ifaces;
118 this.timeout = AaiProviderClient.this.config.getConnectionTimeout();
119 this.mClient = new AaiWebApiClient(AaiProviderClient.this.config.getBaseUrl(),
120 AaiProviderClient.this.config.getHeaders(), AaiProviderClient.this.config.getTrustAll(),
121 AaiProviderClient.this.config.getPcks12CertificateFilename(),
122 AaiProviderClient.this.config.getPcks12CertificatePassphrase());
127 LOG.debug("check if pnfid {} exists", pnfId);
128 this.mClient.setTimeout(timeout);
129 int responseCode = this.mClient.pnfCheckIfExists(pnfId);
130 if (responseCode == RESPCODE_NOTFOUND) {
131 LOG.debug("do pnfCreate for {}", pnfId);
132 this.mClient.pnfCreate(pnfId, type, model, vendor, oamIp, ifaces);
133 } else if (responseCode == RESPCODE_FOUND) {
134 LOG.debug("pnfid {} found, nothing to do", pnfId);
136 LOG.warn("unhandled response code: {}", responseCode);
141 private class AaiDeleteRequestRunnable implements Runnable {
143 private final AaiWebApiClient mClient;
144 private final String pnfId;
145 private final int timeout;
148 public AaiDeleteRequestRunnable(String pnfId) {
150 this.timeout = AaiProviderClient.this.config.getConnectionTimeout();
151 this.mClient = new AaiWebApiClient(AaiProviderClient.this.config.getBaseUrl(),
152 AaiProviderClient.this.config.getHeaders(), AaiProviderClient.this.config.getTrustAll(),
153 AaiProviderClient.this.config.getPcks12CertificateFilename(),
154 AaiProviderClient.this.config.getPcks12CertificatePassphrase());
159 this.mClient.setTimeout(this.timeout);
160 this.mClient.pnfDelete(pnfId);