add new devicemanager
[ccsdk/features.git] / sdnr / wt / devicemanager / provider / src / main / java / org / onap / ccsdk / features / sdnr / wt / devicemanager / aaiconnector / impl / AaiProviderClient.java
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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
15  * the License.
16  * ============LICENSE_END==========================================================================
17  ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.devicemanager.aaiconnector.impl;
19
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.BaseHTTPResponseFromDevicemanager;
27 import org.onap.ccsdk.features.sdnr.wt.devicemanager.DeviceManagerService;
28 import org.onap.ccsdk.features.sdnr.wt.devicemanager.InventoryProvider;
29 import org.onap.ccsdk.features.sdnr.wt.devicemanager.NetworkElement;
30 import org.onap.ccsdk.features.sdnr.wt.devicemanager.aaiconnector.impl.config.AaiConfig;
31 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.DeviceManagerImpl;
32 import org.onap.ccsdk.features.sdnr.wt.devicemanager.legacy.InventoryInformation;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public class AaiProviderClient implements DeviceManagerService, AutoCloseable {
37
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;
42
43     private final AaiConfig config;
44     private final DeviceManagerImpl deviceManager;
45     private final ConfigurationFileRepresentation htconfig;
46
47
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);
52         this.htconfig = cfg;
53         this.htconfig.registerConfigChangedListener(configChangedListener);
54         this.deviceManager = devMgr;
55
56     }
57
58     public AaiConfig getConfig() {
59         return this.config;
60     }
61
62     public void onDeviceRegistered(String mountPointName) {
63         if (this.config.isOff()) {
64             return;
65         }
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") : InventoryInformation.getDefault());
70     }
71
72     public void onDeviceRegistered(String mountPointName, InventoryInformation i) {
73         if (this.config.isOff()) {
74             return;
75         }
76         new Thread(new AaiCreateRequestRunnable(mountPointName, i.getType(), i.getModel(), i.getVendor(),
77                 i.getDeviceIpv4(), i.getInterfaceUuidList())).start();
78     }
79
80     public void onDeviceUnregistered(String mountPointName) {
81         if (this.config.isOff()) {
82             return;
83         }
84         if (this.config.doDeleteOnMountPointRemoved()) {
85             new Thread(new AaiDeleteRequestRunnable(mountPointName)).start();
86         } else {
87             LOG.debug("prevent deleting device {} by config", mountPointName);
88         }
89     }
90
91     @Override
92     public void close() throws Exception {
93         this.htconfig.unregisterConfigChangedListener(configChangedListener);
94     }
95
96     private class AaiCreateRequestRunnable implements Runnable {
97
98         private static final int RESPCODE_NOTFOUND = BaseHTTPResponseFromDevicemanager.CODE404;
99         private static final int RESPCODE_FOUND = BaseHTTPResponseFromDevicemanager.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;
108
109         public AaiCreateRequestRunnable(String pnfId, String type, String model, String vendor, String oamIp,
110                 List<String> ifaces) {
111             this.pnfId = pnfId;
112             this.type = type;
113             this.model = model;
114             this.vendor = vendor;
115             this.oamIp = oamIp;
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());
122         }
123
124         @Override
125         public void run() {
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);
134             } else {
135                 LOG.warn("unhandled response code: {}", responseCode);
136             }
137         }
138     };
139
140     private class AaiDeleteRequestRunnable implements Runnable {
141
142         private final AaiWebApiClient mClient;
143         private final String pnfId;
144         private final int timeout;
145
146
147         public AaiDeleteRequestRunnable(String pnfId) {
148             this.pnfId = 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());
154         }
155
156         @Override
157         public void run() {
158             this.mClient.setTimeout(this.timeout);
159             this.mClient.pnfDelete(pnfId);
160         }
161     };
162
163 }