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;
20 import java.io.IOException;
21 import java.sql.SQLException;
22 import java.util.ArrayList;
24 import javax.sql.rowset.CachedRowSet;
25 import org.apache.http.HttpResponse;
26 import org.apache.http.util.EntityUtils;
27 import org.onap.ccsdk.sli.adaptors.netbox.api.NetboxClient;
28 import org.onap.ccsdk.sli.adaptors.netbox.model.IPAddress;
29 import org.onap.ccsdk.sli.adaptors.netbox.model.IPStatus;
30 import org.onap.ccsdk.sli.core.dblib.DbLibService;
31 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
33 import org.onap.ccsdk.sli.core.sli.SvcLogicResource.QueryStatus;
34 import org.onap.ccsdk.sli.core.slipluginutils.SliPluginUtils;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * THIS CLASS IS A COPY OF {@link NetboxClientImpl} WITH REMOVED OSGi DEPENDENCIES
41 public class NetboxClientImplLighty implements NetboxClient {
43 private static final Logger LOG = LoggerFactory.getLogger(NetboxClientImplLighty.class);
47 private static final String NEXT_AVAILABLE_IP_IN_PREFIX_PATH = "/api/ipam/prefixes/%s/available-ips/";
48 private static final String IP_ADDRESS_PATH = "/api/ipam/ip-addresses/%s/";
52 private static final String ASSIGN_IP_ADDRESS_PAYLOAD = "{\n"
53 + " \"custom_fields\": {\n"
54 + " \"external-key\": \"%s\",\n"
55 + " \"resource-name\": \"%s\"\n"
59 // Service Logic Context input variables and exception
61 private static final String SERVICE_INSTANCE_ID_PROP = "service_instance_id";
62 private static final String VF_MODULE_ID_PROP = "vf_module_id";
63 private static final String EXTERNAL_KEY_PROP = "external_key";
64 private static final String SQL_EXCEPTION_MESSAGE = "Caught SQL exception";
68 private static final String ASSIGN_IP_SQL_STATEMENT =
69 "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"
70 + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
71 private static final String UNASSIGN_IP_SQL_STATEMENT =
72 "UPDATE IPAM_IP_ASSIGNEMENT SET ip_status = ? WHERE service_instance_id = ? AND external_key = ?";
73 private static final String GET_IP_ADDRESS_ID_SQL_STATEMENT =
74 "SELECT ip_address_id FROM IPAM_IP_ASSIGNEMENT WHERE service_instance_id = ? AND external_key = ?";
76 private final NetboxHttpClientLighty client;
77 private final DbLibService dbLibService;
79 public NetboxClientImplLighty(final NetboxHttpClientLighty client, final DbLibService dbLibService) {
81 this.dbLibService = dbLibService;
85 public QueryStatus assignIpAddress(final Map<String, String> parameters, final SvcLogicContext ctx) {
89 .checkParameters(parameters,
90 new String[]{SERVICE_INSTANCE_ID_PROP, VF_MODULE_ID_PROP, "prefix_id", "resource_name",
91 EXTERNAL_KEY_PROP}, LOG);
92 } catch (SvcLogicException e) {
93 return QueryStatus.FAILURE;
96 final String serviceInstanceId = parameters.get(SERVICE_INSTANCE_ID_PROP);
97 final String vfModuleId = parameters.get(VF_MODULE_ID_PROP);
98 final String prefixId = parameters.get("prefix_id");
99 final String resourceName = parameters.get("resource_name");
100 final String externalKey = parameters.get(EXTERNAL_KEY_PROP);
102 HttpResponse httpResp;
105 .post(String.format(NEXT_AVAILABLE_IP_IN_PREFIX_PATH, prefixId),
106 String.format(ASSIGN_IP_ADDRESS_PAYLOAD, externalKey, resourceName));
107 } catch (IOException e) {
108 LOG.error("Fail to assign IP for Prefix(id={}). {}", prefixId, e.getMessage(), e.getCause());
109 return QueryStatus.FAILURE;
114 ipamRespJson = EntityUtils.toString(httpResp.getEntity(), "UTF-8");
115 } catch (IOException e) {
116 LOG.error("Fail to parse IPAM response for assign in Prefix(id={}). Response={}", prefixId,
117 httpResp.getEntity(), e);
118 return QueryStatus.FAILURE;
121 if (httpResp.getStatusLine().getStatusCode() != 201) {
122 LOG.error("Fail to assign IP for Prefix(id={}). HTTP code 201!={}. Response={}", prefixId,
123 httpResp.getStatusLine().getStatusCode(), ipamRespJson);
124 return QueryStatus.FAILURE;
129 ipAddress = IPAddress.fromJson(ipamRespJson);
130 } catch (JsonSyntaxException e) {
131 LOG.error("Fail to parse IPAM JSON reponse to IPAddress POJO. IPAM JSON Response={}", ipamRespJson, e);
132 return QueryStatus.FAILURE;
135 ArrayList<String> args = Lists.newArrayList(
138 String.valueOf(prefixId),
139 String.valueOf(ipAddress.getId()),
140 ipAddress.getAddress(),
141 IPStatus.ASSIGNED.name(),
147 dbLibService.writeData(ASSIGN_IP_SQL_STATEMENT, args, null);
148 } catch (SQLException e) {
149 LOG.error(SQL_EXCEPTION_MESSAGE, e);
150 return QueryStatus.FAILURE;
153 ctx.setAttribute("self_serve_netbox_ip_assignement.ip-address", ipAddress.getAddress());
155 return QueryStatus.SUCCESS;
159 public QueryStatus unassignIpAddress(final Map<String, String> parameters, final SvcLogicContext ctx) {
162 .checkParameters(parameters, new String[]{SERVICE_INSTANCE_ID_PROP, EXTERNAL_KEY_PROP},
164 } catch (SvcLogicException e) {
165 return QueryStatus.FAILURE;
168 final String serviceInstanceId = parameters.get(SERVICE_INSTANCE_ID_PROP);
169 final String externalKey = parameters.get(EXTERNAL_KEY_PROP);
172 ArrayList<String> args = Lists.newArrayList(
175 try (CachedRowSet row = dbLibService.getData(GET_IP_ADDRESS_ID_SQL_STATEMENT, args, null)) {
177 ipAddressId = row.getString("ip_address_id");
179 throw new SQLException("Data not found.");
181 } catch (SQLException e) {
182 LOG.error(SQL_EXCEPTION_MESSAGE, e);
183 return QueryStatus.FAILURE;
186 HttpResponse httpResp;
188 httpResp = client.delete(String.format(IP_ADDRESS_PATH, ipAddressId));
189 } catch (IOException e) {
190 LOG.error("Fail to unassign IP for IPAddress(id= " + ipAddressId + "). " + e.getMessage(),
192 return QueryStatus.FAILURE;
195 if (httpResp.getStatusLine().getStatusCode() - 200 >= 100) {
196 LOG.error("Fail to unassign IP for IPAddress(id={}). HTTP code={}.", ipAddressId,
197 httpResp.getStatusLine().getStatusCode());
198 return QueryStatus.FAILURE;
202 args = Lists.newArrayList(
203 IPStatus.DELETED.name(),
208 dbLibService.writeData(UNASSIGN_IP_SQL_STATEMENT, args, null);
209 } catch (SQLException e) {
210 LOG.error(SQL_EXCEPTION_MESSAGE, e);
211 return QueryStatus.FAILURE;
214 return QueryStatus.SUCCESS;