aboutsummaryrefslogtreecommitdiffstats
path: root/netbox-client/provider/src/main/java/org/onap/ccsdk/sli/adaptors/netbox/impl/NetboxClientImpl.java
diff options
context:
space:
mode:
Diffstat (limited to 'netbox-client/provider/src/main/java/org/onap/ccsdk/sli/adaptors/netbox/impl/NetboxClientImpl.java')
-rw-r--r--netbox-client/provider/src/main/java/org/onap/ccsdk/sli/adaptors/netbox/impl/NetboxClientImpl.java194
1 files changed, 115 insertions, 79 deletions
diff --git a/netbox-client/provider/src/main/java/org/onap/ccsdk/sli/adaptors/netbox/impl/NetboxClientImpl.java b/netbox-client/provider/src/main/java/org/onap/ccsdk/sli/adaptors/netbox/impl/NetboxClientImpl.java
index 036ff44d..54700f6c 100644
--- a/netbox-client/provider/src/main/java/org/onap/ccsdk/sli/adaptors/netbox/impl/NetboxClientImpl.java
+++ b/netbox-client/provider/src/main/java/org/onap/ccsdk/sli/adaptors/netbox/impl/NetboxClientImpl.java
@@ -15,127 +15,163 @@
*/
package org.onap.ccsdk.sli.adaptors.netbox.impl;
-import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Lists;
-import com.google.gson.Gson;
-import com.google.gson.GsonBuilder;
-import com.google.gson.JsonSerializer;
+import com.google.gson.JsonSyntaxException;
import java.io.IOException;
-import java.io.InputStreamReader;
-import java.io.Reader;
import java.sql.SQLException;
import java.util.ArrayList;
-import java.util.concurrent.CompletionException;
+import java.util.Map;
import org.apache.http.HttpResponse;
-import org.onap.ccsdk.sli.adaptors.netbox.api.IpamException;
+import org.apache.http.util.EntityUtils;
import org.onap.ccsdk.sli.adaptors.netbox.api.NetboxClient;
import org.onap.ccsdk.sli.adaptors.netbox.model.IPAddress;
-import org.onap.ccsdk.sli.adaptors.netbox.model.Prefix;
-import org.onap.ccsdk.sli.adaptors.netbox.model.Status;
+import org.onap.ccsdk.sli.adaptors.netbox.model.IPStatus;
import org.onap.ccsdk.sli.core.dblib.DbLibService;
-import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
+import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
+import org.onap.ccsdk.sli.core.sli.SvcLogicException;
+import org.onap.ccsdk.sli.core.sli.SvcLogicResource.QueryStatus;
+import org.onap.ccsdk.sli.core.slipluginutils.SliPluginUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
-public class NetboxClientImpl implements NetboxClient, SvcLogicJavaPlugin {
+public class NetboxClientImpl implements NetboxClient {
+
+ private static final Logger LOG = LoggerFactory.getLogger(NetboxClientImpl.class);
private static final String NEXT_AVAILABLE_IP_IN_PREFIX_PATH = "/api/ipam/prefixes/%s/available-ips/";
private static final String IP_ADDRESS_PATH = "/api/ipam/ip-addresses/%s/";
private static final String EMPTY_STRING = "";
- private static final String ID_MISSING_MSG = "Id must be set";
+ private static final String SERVICE_INSTANCE_ID_PROP = "service_instance_id";
+ private static final String VF_MODULE_ID_PROP = "vf_module_id";
private static final String ASSIGN_IP_SQL_STATEMENT =
- "INSERT INTO IPAM_IP_ASSIGNEMENT (service_instance_id, vf_module_id, prefix_id, ip_address_id, ip_adress, ip_status) \n"
- + "VALUES (?, ?, ?, ?, ?, ?)";
+ "INSERT INTO IPAM_IP_ASSIGNEMENT (service_instance_id, vf_module_id, prefix_id, ip_address_id, ip_address, ip_status, ip_response_json) \n"
+ + "VALUES (?, ?, ?, ?, ?, ?, ?)";
private static final String UNASSIGN_IP_SQL_STATEMENT =
- "DELETE FROM IPAM_IP_ASSIGNEMENT WHERE service_instance_id = ? AND vf_module_id = ? AND ip_address_id = ?";
+ "UPDATE IPAM_IP_ASSIGNEMENT SET ip_status = ? WHERE service_instance_id = ? AND vf_module_id = ? AND ip_address_id = ?";
private final NetboxHttpClient client;
- private final Gson gson;
+
private final DbLibService dbLibService;
public NetboxClientImpl(final NetboxHttpClient client, final DbLibService dbLibService) {
this.client = client;
this.dbLibService = dbLibService;
- final JsonSerializer<Status> vlanStatusDeserializer = (val, type, context) -> val.toJson();
- gson = new GsonBuilder()
- .registerTypeAdapter(Status.class, vlanStatusDeserializer)
- .create();
}
@Override
- public IPAddress assign(final Prefix prefix, final String serviceInstanceId, final String vfModuleId)
- throws IpamException, SQLException {
+ public QueryStatus assignIpAddress(final Map<String, String> parameters, final SvcLogicContext ctx) {
- checkArgument(prefix.getId() != null);
try {
- IPAddress ipAddress = client
- .post(String.format(NEXT_AVAILABLE_IP_IN_PREFIX_PATH, prefix.getId()), EMPTY_STRING)
- .thenApply(this::getIpAddress)
- .toCompletableFuture()
- .join();
-
- ArrayList<String> args = Lists.newArrayList(serviceInstanceId,
- vfModuleId,
- String.valueOf(prefix.getId()),
- String.valueOf(ipAddress.getId()),
- ipAddress.getAddress(),
- ipAddress.getStatus().getLabel());
- dbLibService.writeData(ASSIGN_IP_SQL_STATEMENT, args, null);
+ SliPluginUtils
+ .checkParameters(parameters, new String[]{SERVICE_INSTANCE_ID_PROP, VF_MODULE_ID_PROP, "prefix_id"},
+ LOG);
+ } catch (SvcLogicException e) {
+ return QueryStatus.FAILURE;
+ }
- return ipAddress;
- } catch (CompletionException e) {
- // Unwrap the CompletionException and wrap in IpamException
- throw new IpamException("Fail to assign IP for Prefix(id= " + prefix.getId() + "). " + e.getMessage(),
- e.getCause());
+ final String serviceInstanceId = parameters.get(SERVICE_INSTANCE_ID_PROP);
+ LOG.trace("assignIpAddress: service_instance_id = {}", serviceInstanceId);
+ final String vfModuleId = parameters.get(VF_MODULE_ID_PROP);
+ LOG.trace("assignIpAddress: vf_module_id = {}", vfModuleId);
+ final String prefixId = parameters.get("prefix_id");
+ LOG.trace("assignIpAddress: prefix_id = {}", prefixId);
+
+ HttpResponse httpResp;
+ try {
+ httpResp = client
+ .post(String.format(NEXT_AVAILABLE_IP_IN_PREFIX_PATH, prefixId), EMPTY_STRING);
+ } catch (IOException e) {
+ LOG.error("Fail to assign IP for Prefix(id={}). {}", prefixId, e.getMessage(), e.getCause());
+ return QueryStatus.FAILURE;
}
- }
- @Override
- public void unassign(final IPAddress ipAddress, final String serviceInstanceId, final String vfModuleId)
- throws IpamException, SQLException {
+ String ipamRespJson;
+ try {
+ ipamRespJson = EntityUtils.toString(httpResp.getEntity(), "UTF-8");
+ } catch (IOException e) {
+ LOG.error("Fail to parse IPAM response for assign in Prefix(id={}). Response={}", prefixId,
+ httpResp.getEntity(), e);
+ return QueryStatus.FAILURE;
+ }
+
+ if (httpResp.getStatusLine().getStatusCode() != 201) {
+ LOG.error("Fail to assign IP for Prefix(id={}). HTTP code 201!={}. Response={}", prefixId,
+ httpResp.getStatusLine().getStatusCode(), ipamRespJson);
+ return QueryStatus.FAILURE;
+ }
- checkArgument(ipAddress.getId() != null);
+ IPAddress ipAddress;
try {
- client.delete(String.format(IP_ADDRESS_PATH, ipAddress.getId()))
- .thenAccept(this::checkResult)
- .toCompletableFuture()
- .join();
-
- ArrayList<String> args = Lists.newArrayList(
- serviceInstanceId,
- vfModuleId,
- String.valueOf(ipAddress.getId()));
- dbLibService.writeData(UNASSIGN_IP_SQL_STATEMENT, args, null);
+ ipAddress = IPAddress.fromJson(ipamRespJson);
+ } catch (JsonSyntaxException e) {
+ LOG.error("Fail to parse IPAM JSON reponse to IPAddress POJO. IPAM JSON Response={}", ipamRespJson, e);
+ return QueryStatus.FAILURE;
+ }
- } catch (CompletionException e) {
- // Unwrap the CompletionException and wrap in IpamException
- throw new IpamException(
- "Fail to unassign IP for IPAddress(id= " + ipAddress.getId() + "). " + e.getMessage(),
- e.getCause());
+ ArrayList<String> args = Lists.newArrayList(serviceInstanceId,
+ vfModuleId,
+ String.valueOf(prefixId),
+ String.valueOf(ipAddress.getId()),
+ ipAddress.getAddress(),
+ IPStatus.ASSIGNED.name(),
+ ipamRespJson);
+
+ try {
+ dbLibService.writeData(ASSIGN_IP_SQL_STATEMENT, args, null);
+ } catch (SQLException e) {
+ LOG.error("Caught SQL exception", e);
+ return QueryStatus.FAILURE;
}
+
+ ctx.setAttribute("self_serve_netbox_ip_assignement.ip-address", ipAddress.getAddress());
+
+ return QueryStatus.SUCCESS;
}
- @VisibleForTesting
- IPAddress getIpAddress(final HttpResponse response) {
- if (response.getStatusLine().getStatusCode() != 201) {
- throw new IllegalStateException(NetboxHttpClient.getBodyAsString(response));
+ @Override
+ public QueryStatus unassignIpAddress(final Map<String, String> parameters, final SvcLogicContext ctx) {
+ try {
+ SliPluginUtils
+ .checkParameters(parameters, new String[]{SERVICE_INSTANCE_ID_PROP, VF_MODULE_ID_PROP, "ip_address_id"},
+ LOG);
+ } catch (SvcLogicException e) {
+ return QueryStatus.FAILURE;
}
- try (final Reader reader = new InputStreamReader(response.getEntity().getContent())) {
- return gson.fromJson(reader, IPAddress.class);
- } catch (final IOException e) {
- throw new IllegalStateException(e);
+
+ final String serviceInstanceId = parameters.get(SERVICE_INSTANCE_ID_PROP);
+ LOG.trace("assignIpAddress: service_instance_id = {}", serviceInstanceId);
+ final String vfModuleId = parameters.get(VF_MODULE_ID_PROP);
+ LOG.trace("assignIpAddress: vf_module_id = {}", vfModuleId);
+ final String ipAddressId = parameters.get("ip_address_id");
+ LOG.trace("assignIpAddress: ip_address_id = {}", ipAddressId);
+ HttpResponse httpResp;
+ try {
+ httpResp = client.delete(String.format(IP_ADDRESS_PATH, ipAddressId));
+ } catch (IOException e) {
+ LOG.error("Fail to unassign IP for IPAddress(id= " + ipAddressId + "). " + e.getMessage(),
+ e.getCause());
+ return QueryStatus.FAILURE;
}
- }
- private static void checkArgument(final boolean argument) throws IpamException {
- if (!argument) {
- throw new IpamException(ID_MISSING_MSG);
+ if (httpResp.getStatusLine().getStatusCode() - 200 >= 100) {
+ LOG.error("Fail to unassign IP for IPAddress(id={}). HTTP code={}.", ipAddressId,
+ httpResp.getStatusLine().getStatusCode());
+ return QueryStatus.FAILURE;
}
- }
- private void checkResult(final HttpResponse response) {
- if (response.getStatusLine().getStatusCode() - 200 >= 100) {
- throw new IllegalStateException(
- "Netbox request failed with status: " + NetboxHttpClient.getBodyAsString(response));
+ ArrayList<String> args = Lists.newArrayList(
+ IPStatus.UNASSIGNED.name(),
+ serviceInstanceId,
+ vfModuleId,
+ String.valueOf(ipAddressId));
+ try {
+ dbLibService.writeData(UNASSIGN_IP_SQL_STATEMENT, args, null);
+ } catch (SQLException e) {
+ LOG.error("Caught SQL exception", e);
+ return QueryStatus.FAILURE;
}
+
+ return QueryStatus.SUCCESS;
}
}