d5030c1c7c428f1b591ea1a0ea3b7643472e1a9f
[ccsdk/features.git] /
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 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;
30
31 public class AaiProviderClient implements AutoCloseable {
32
33     private static Logger LOG = LoggerFactory.getLogger(AaiProviderClient.class);
34     private static boolean reloadConfigFlag;
35     private static final IConfigChangedListener configChangedListener = () -> reloadConfigFlag = true;
36
37     private AaiConfig config;
38     private final DeviceManagerImpl deviceManager;
39     private final HtDevicemanagerConfiguration htconfig;
40
41
42     public AaiConfig getConfig() {
43         return this.config;
44     }
45
46
47     public AaiProviderClient(HtDevicemanagerConfiguration cfg, DeviceManagerImpl devMgr) {
48         this.config = cfg.getAai();
49         this.htconfig = cfg;
50         this.htconfig.registerConfigChangedListener(configChangedListener);
51         this.deviceManager = devMgr;
52
53     }
54
55     private void _reload() {
56         if (reloadConfigFlag) {
57             this.config = AaiConfig.reload();
58             LOG.info("config reloaded: {}", config == null ? "null" : config);
59         }
60         reloadConfigFlag = false;
61     }
62
63     public void onDeviceRegistered(String mountPointName) {
64         this._reload();
65         if (this.config.isOff()) {
66             return;
67         }
68         ONFCoreNetworkElementRepresentation ne =
69                 this.deviceManager != null ? this.deviceManager.getNeByMountpoint(mountPointName) : null;
70         this.onDeviceRegistered(mountPointName,
71                 ne != null ? ne.getInventoryInformation("MWPS") : InventoryInformation.getDefault());
72
73     }
74
75     public void onDeviceRegistered(String mountPointName, InventoryInformation i) {
76         this._reload();
77         if (this.config.isOff()) {
78             return;
79         }
80         new Thread(new AaiCreateRequestRunnable(mountPointName, i.getType(), i.getModel(), i.getVendor(),
81                 i.getDeviceIpv4(), i.getInterfaceUuidList())).start();
82     }
83
84     public void onDeviceUnregistered(String mountPointName) {
85         this._reload();
86         if (this.config.isOff()) {
87             return;
88         }
89         if (this.config.doDeleteOnMountPointRemoved()) {
90             new Thread(new AaiDeleteRequestRunnable(mountPointName)).start();
91         } else {
92             LOG.debug("prevent deleting device {} by config", mountPointName);
93         }
94     }
95
96     @Override
97     public void close() throws Exception {
98         this.htconfig.unregisterConfigChangedListener(configChangedListener);
99     }
100
101     private class AaiCreateRequestRunnable implements Runnable {
102
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;
113
114         public AaiCreateRequestRunnable(String pnfId, String type, String model, String vendor, String oamIp,
115                 List<String> ifaces) {
116             this.pnfId = pnfId;
117             this.type = type;
118             this.model = model;
119             this.vendor = vendor;
120             this.oamIp = oamIp;
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());
127         }
128
129         @Override
130         public void run() {
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);
139             } else {
140                 LOG.warn("unhandled response code: {}", responseCode);
141             }
142         }
143     };
144
145     private class AaiDeleteRequestRunnable implements Runnable {
146
147         private final AaiWebApiClient mClient;
148         private final String pnfId;
149         private final int timeout;
150
151
152         public AaiDeleteRequestRunnable(String pnfId) {
153             this.pnfId = 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());
159         }
160
161         @Override
162         public void run() {
163             this.mClient.setTimeout(this.timeout);
164             this.mClient.pnfDelete(pnfId);
165         }
166     };
167
168 }