193b96a44aa90a34aa1f7008ea19962b45e40405
[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 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;
35
36 public class AaiProviderClient implements AaiService, 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 =
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());
71     }
72
73     public void onDeviceRegistered(String mountPointName, InventoryInformationDcae i) {
74         if (this.config.isOff()) {
75             return;
76         }
77         new Thread(new AaiCreateRequestRunnable(mountPointName, i.getType(), i.getModel(), i.getVendor(),
78                 i.getDeviceIpv4(), i.getInterfaceUuidList())).start();
79     }
80
81     public void onDeviceUnregistered(String mountPointName) {
82         if (this.config.isOff()) {
83             return;
84         }
85         if (this.config.doDeleteOnMountPointRemoved()) {
86             new Thread(new AaiDeleteRequestRunnable(mountPointName)).start();
87         } else {
88             LOG.debug("prevent deleting device {} by config", mountPointName);
89         }
90     }
91
92     @Override
93     public void close() throws Exception {
94         this.htconfig.unregisterConfigChangedListener(configChangedListener);
95     }
96
97     private class AaiCreateRequestRunnable implements Runnable {
98
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;
109
110         public AaiCreateRequestRunnable(String pnfId, String type, String model, String vendor, String oamIp,
111                 List<String> ifaces) {
112             this.pnfId = pnfId;
113             this.type = type;
114             this.model = model;
115             this.vendor = vendor;
116             this.oamIp = oamIp;
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());
123         }
124
125         @Override
126         public void run() {
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);
135             } else {
136                 LOG.warn("unhandled response code: {}", responseCode);
137             }
138         }
139     };
140
141     private class AaiDeleteRequestRunnable implements Runnable {
142
143         private final AaiWebApiClient mClient;
144         private final String pnfId;
145         private final int timeout;
146
147
148         public AaiDeleteRequestRunnable(String pnfId) {
149             this.pnfId = 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());
155         }
156
157         @Override
158         public void run() {
159             this.mClient.setTimeout(this.timeout);
160             this.mClient.pnfDelete(pnfId);
161         }
162     };
163
164 }