2 * Copyright (C) 2018 Bell Canada.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onap.ccsdk.sli.adaptors.netbox.impl;
18 import com.google.common.collect.Lists;
19 import com.google.gson.JsonSyntaxException;
22 import java.io.FileInputStream;
23 import java.io.IOException;
24 import java.sql.SQLException;
25 import java.util.ArrayList;
27 import java.util.Properties;
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;
45 public class NetboxClientImpl implements NetboxClient {
47 private static final Logger LOG = LoggerFactory.getLogger(NetboxClientImpl.class);
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/";
56 private static final String ASSIGN_IP_ADDRESS_PAYLOAD = "{\n"
57 + " \"custom_fields\": {\n"
58 + " \"external-key\": \"%s\",\n"
59 + " \"resource-name\": \"%s\"\n"
63 // Service Logic Context input variables and exception
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";
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 = ?";
80 private final NetboxHttpClient client;
81 private final DbLibService dbLibService;
83 public NetboxClientImpl() {
87 public NetboxClientImpl(final NetboxHttpClient client, final DbLibService dbLibService) {
89 this.client = new NetboxHttpClient(new NetboxProperties());
94 if (dbLibService == null) {
95 Properties dblibProps = System.getProperties();
97 String cfgDir = dblibProps.getProperty("sdnc.config.dir", System.getenv("SDNC_CONFIG_DIR"));
99 if ((cfgDir == null) || (cfgDir.length() == 0)) {
100 cfgDir = "/opt/sdnc/data/properties";
103 File dblibPropFile = new File(cfgDir + "/dblib.properties");
104 if (dblibPropFile.exists()) {
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);
112 dblibProps = System.getProperties();
116 DbLibService dbSvc = null;
118 dbSvc = new DBResourceManager(dblibProps);
119 } catch (Exception e) {
120 LOG.error("Caught exception trying to create dblib service", e);
124 dbSvc = new DBResourceManager(dblibProps);
125 } catch (Exception e) {
126 LOG.error("Caught exception trying to create dblib service", e);
128 this.dbLibService = dbSvc;
131 this.dbLibService = dbLibService;
136 public QueryStatus assignIpAddress(final Map<String, String> parameters, final SvcLogicContext ctx) {
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;
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);
153 HttpResponse httpResp;
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;
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;
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;
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;
186 ArrayList<String> args = Lists.newArrayList(
189 String.valueOf(prefixId),
190 String.valueOf(ipAddress.getId()),
191 ipAddress.getAddress(),
192 IPStatus.ASSIGNED.name(),
198 dbLibService.writeData(ASSIGN_IP_SQL_STATEMENT, args, null);
199 } catch (SQLException e) {
200 LOG.error(SQL_EXCEPTION_MESSAGE, e);
201 return QueryStatus.FAILURE;
204 ctx.setAttribute("self_serve_netbox_ip_assignement.ip-address", ipAddress.getAddress());
206 return QueryStatus.SUCCESS;
210 public QueryStatus unassignIpAddress(final Map<String, String> parameters, final SvcLogicContext ctx) {
213 .checkParameters(parameters, new String[]{SERVICE_INSTANCE_ID_PROP, EXTERNAL_KEY_PROP},
215 } catch (SvcLogicException e) {
216 return QueryStatus.FAILURE;
219 final String serviceInstanceId = parameters.get(SERVICE_INSTANCE_ID_PROP);
220 final String externalKey = parameters.get(EXTERNAL_KEY_PROP);
223 ArrayList<String> args = Lists.newArrayList(
226 try (CachedRowSet row = dbLibService.getData(GET_IP_ADDRESS_ID_SQL_STATEMENT, args, null)) {
228 ipAddressId = row.getString("ip_address_id");
230 throw new SQLException("Data not found.");
232 } catch (SQLException e) {
233 LOG.error(SQL_EXCEPTION_MESSAGE, e);
234 return QueryStatus.FAILURE;
237 HttpResponse httpResp;
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(),
243 return QueryStatus.FAILURE;
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;
253 args = Lists.newArrayList(
254 IPStatus.DELETED.name(),
259 dbLibService.writeData(UNASSIGN_IP_SQL_STATEMENT, args, null);
260 } catch (SQLException e) {
261 LOG.error(SQL_EXCEPTION_MESSAGE, e);
262 return QueryStatus.FAILURE;
265 return QueryStatus.SUCCESS;