Support netbox-client outside OSGi container
[ccsdk/sli/adaptors.git] / netbox-client / provider / src / main / java / org / onap / ccsdk / sli / adaptors / netbox / impl / NetboxClientImpl.java
1 /*
2  * Copyright (C) 2018 Bell Canada.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.ccsdk.sli.adaptors.netbox.impl;
17
18 import com.google.common.collect.Lists;
19 import com.google.gson.JsonSyntaxException;
20
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.IOException;
24 import java.sql.SQLException;
25 import java.util.ArrayList;
26 import java.util.Map;
27 import java.util.Properties;
28
29 import javax.sql.rowset.CachedRowSet;
30 import org.apache.http.HttpResponse;
31 import org.apache.http.util.EntityUtils;
32 import org.onap.ccsdk.sli.adaptors.netbox.api.NetboxClient;
33 import org.onap.ccsdk.sli.adaptors.netbox.model.IPAddress;
34 import org.onap.ccsdk.sli.adaptors.netbox.model.IPStatus;
35 import org.onap.ccsdk.sli.adaptors.netbox.property.NetboxProperties;
36 import org.onap.ccsdk.sli.core.dblib.DBResourceManager;
37 import org.onap.ccsdk.sli.core.dblib.DbLibService;
38 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
39 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
40 import org.onap.ccsdk.sli.core.sli.SvcLogicResource.QueryStatus;
41 import org.onap.ccsdk.sli.core.slipluginutils.SliPluginUtils;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 public class NetboxClientImpl implements NetboxClient {
46
47     private static final Logger LOG = LoggerFactory.getLogger(NetboxClientImpl.class);
48
49     // Netbox URI
50
51     private static final String NEXT_AVAILABLE_IP_IN_PREFIX_PATH = "/api/ipam/prefixes/%s/available-ips/";
52     private static final String IP_ADDRESS_PATH = "/api/ipam/ip-addresses/%s/";
53
54     // Netbox Payload
55
56     private static final String ASSIGN_IP_ADDRESS_PAYLOAD = "{\n"
57         + "  \"custom_fields\": {\n"
58         + "    \"external-key\": \"%s\",\n"
59         + "    \"resource-name\": \"%s\"\n"
60         + "  }\n"
61         + "}";
62
63     // Service Logic Context input variables and exception
64
65     private static final String SERVICE_INSTANCE_ID_PROP = "service_instance_id";
66     private static final String VF_MODULE_ID_PROP = "vf_module_id";
67     private static final String EXTERNAL_KEY_PROP = "external_key";
68     private static final String SQL_EXCEPTION_MESSAGE = "Caught SQL exception";
69
70     // SQL statement
71
72     private static final String ASSIGN_IP_SQL_STATEMENT =
73         "INSERT INTO IPAM_IP_ASSIGNEMENT (service_instance_id, vf_module_id, prefix_id, ip_address_id, ip_address, ip_status, ip_response_json, external_key, ip_address_type) \n"
74             + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
75     private static final String UNASSIGN_IP_SQL_STATEMENT =
76         "UPDATE IPAM_IP_ASSIGNEMENT SET ip_status = ? WHERE service_instance_id = ? AND external_key = ?";
77     private static final String GET_IP_ADDRESS_ID_SQL_STATEMENT =
78         "SELECT ip_address_id FROM IPAM_IP_ASSIGNEMENT WHERE service_instance_id = ? AND external_key = ?";
79
80     private final NetboxHttpClient client;
81     private final DbLibService dbLibService;
82
83     public NetboxClientImpl() {
84         this(null, null);
85     }
86
87     public NetboxClientImpl(final NetboxHttpClient client, final DbLibService dbLibService) {
88         if (client == null) {
89             this.client = new NetboxHttpClient(new NetboxProperties());
90         } else {
91             this.client = client;
92         }
93
94         if (dbLibService == null) {
95             Properties dblibProps = System.getProperties();
96
97             String cfgDir = dblibProps.getProperty("sdnc.config.dir", System.getenv("SDNC_CONFIG_DIR"));
98     
99             if ((cfgDir == null) || (cfgDir.length() == 0)) {
100                 cfgDir = "/opt/sdnc/data/properties";
101             }
102     
103             File dblibPropFile = new File(cfgDir + "/dblib.properties");
104             if (dblibPropFile.exists()) {
105                 try {
106                     LOG.debug("Loading dblib properties from {}", dblibPropFile.getAbsolutePath());
107                     dblibProps = new Properties();
108                     dblibProps.load(new FileInputStream(dblibPropFile));
109                 } catch (Exception e) {
110                     LOG.warn("Could not load properties file {}", dblibPropFile.getAbsolutePath(), e);
111     
112                     dblibProps = System.getProperties();
113                 }
114             }
115             
116             DbLibService dbSvc = null;
117             try {
118                 dbSvc  = new DBResourceManager(dblibProps);
119             } catch (Exception e) {
120                 LOG.error("Caught exception trying to create dblib service", e);
121             }
122     
123             try {
124                 dbSvc  = new DBResourceManager(dblibProps);
125             } catch (Exception e) {
126                 LOG.error("Caught exception trying to create dblib service", e);
127             }
128             this.dbLibService = dbSvc;
129     
130         } else {
131             this.dbLibService = dbLibService;
132         }
133     }
134
135     @Override
136     public QueryStatus assignIpAddress(final Map<String, String> parameters, final SvcLogicContext ctx) {
137
138         try {
139             SliPluginUtils
140                 .checkParameters(parameters,
141                     new String[]{SERVICE_INSTANCE_ID_PROP, VF_MODULE_ID_PROP, "prefix_id", "resource_name",
142                         EXTERNAL_KEY_PROP}, LOG);
143         } catch (SvcLogicException e) {
144             return QueryStatus.FAILURE;
145         }
146
147         final String serviceInstanceId = parameters.get(SERVICE_INSTANCE_ID_PROP);
148         final String vfModuleId = parameters.get(VF_MODULE_ID_PROP);
149         final String prefixId = parameters.get("prefix_id");
150         final String resourceName = parameters.get("resource_name");
151         final String externalKey = parameters.get(EXTERNAL_KEY_PROP);
152
153         HttpResponse httpResp;
154         try {
155             httpResp = client
156                 .post(String.format(NEXT_AVAILABLE_IP_IN_PREFIX_PATH, prefixId),
157                     String.format(ASSIGN_IP_ADDRESS_PAYLOAD, externalKey, resourceName));
158         } catch (IOException e) {
159             LOG.error("Fail to assign IP for Prefix(id={}). {}", prefixId, e.getMessage(), e.getCause());
160             return QueryStatus.FAILURE;
161         }
162
163         String ipamRespJson;
164         try {
165             ipamRespJson = EntityUtils.toString(httpResp.getEntity(), "UTF-8");
166         } catch (IOException e) {
167             LOG.error("Fail to parse IPAM response for assign in Prefix(id={}). Response={}", prefixId,
168                 httpResp.getEntity(), e);
169             return QueryStatus.FAILURE;
170         }
171
172         if (httpResp.getStatusLine().getStatusCode() != 201) {
173             LOG.error("Fail to assign IP for Prefix(id={}). HTTP code 201!={}. Response={}", prefixId,
174                 httpResp.getStatusLine().getStatusCode(), ipamRespJson);
175             return QueryStatus.FAILURE;
176         }
177
178         IPAddress ipAddress;
179         try {
180             ipAddress = IPAddress.fromJson(ipamRespJson);
181         } catch (JsonSyntaxException e) {
182             LOG.error("Fail to parse IPAM JSON reponse to IPAddress POJO. IPAM JSON Response={}", ipamRespJson, e);
183             return QueryStatus.FAILURE;
184         }
185
186         ArrayList<String> args = Lists.newArrayList(
187             serviceInstanceId,
188             vfModuleId,
189             String.valueOf(prefixId),
190             String.valueOf(ipAddress.getId()),
191             ipAddress.getAddress(),
192             IPStatus.ASSIGNED.name(),
193             ipamRespJson,
194             externalKey,
195             resourceName);
196
197         try {
198             dbLibService.writeData(ASSIGN_IP_SQL_STATEMENT, args, null);
199         } catch (SQLException e) {
200             LOG.error(SQL_EXCEPTION_MESSAGE, e);
201             return QueryStatus.FAILURE;
202         }
203
204         ctx.setAttribute("self_serve_netbox_ip_assignement.ip-address", ipAddress.getAddress());
205
206         return QueryStatus.SUCCESS;
207     }
208
209     @Override
210     public QueryStatus unassignIpAddress(final Map<String, String> parameters, final SvcLogicContext ctx) {
211         try {
212             SliPluginUtils
213                 .checkParameters(parameters, new String[]{SERVICE_INSTANCE_ID_PROP, EXTERNAL_KEY_PROP},
214                     LOG);
215         } catch (SvcLogicException e) {
216             return QueryStatus.FAILURE;
217         }
218
219         final String serviceInstanceId = parameters.get(SERVICE_INSTANCE_ID_PROP);
220         final String externalKey = parameters.get(EXTERNAL_KEY_PROP);
221
222         String ipAddressId;
223         ArrayList<String> args = Lists.newArrayList(
224             serviceInstanceId,
225             externalKey);
226         try (CachedRowSet row = dbLibService.getData(GET_IP_ADDRESS_ID_SQL_STATEMENT, args, null)) {
227             if (row.next()) {
228                 ipAddressId = row.getString("ip_address_id");
229             } else {
230                 throw new SQLException("Data not found.");
231             }
232         } catch (SQLException e) {
233             LOG.error(SQL_EXCEPTION_MESSAGE, e);
234             return QueryStatus.FAILURE;
235         }
236
237         HttpResponse httpResp;
238         try {
239             httpResp = client.delete(String.format(IP_ADDRESS_PATH, ipAddressId));
240         } catch (IOException e) {
241             LOG.error("Fail to unassign IP for IPAddress(id= " + ipAddressId + "). " + e.getMessage(),
242                 e.getCause());
243             return QueryStatus.FAILURE;
244         }
245
246         if (httpResp.getStatusLine().getStatusCode() - 200 >= 100) {
247             LOG.error("Fail to unassign IP for IPAddress(id={}). HTTP code={}.", ipAddressId,
248                 httpResp.getStatusLine().getStatusCode());
249             return QueryStatus.FAILURE;
250         }
251
252         args.clear();
253         args = Lists.newArrayList(
254             IPStatus.DELETED.name(),
255             serviceInstanceId,
256             externalKey);
257
258         try {
259             dbLibService.writeData(UNASSIGN_IP_SQL_STATEMENT, args, null);
260         } catch (SQLException e) {
261             LOG.error(SQL_EXCEPTION_MESSAGE, e);
262             return QueryStatus.FAILURE;
263         }
264
265         return QueryStatus.SUCCESS;
266     }
267 }