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.annotations.VisibleForTesting;
19 import com.google.gson.Gson;
20 import com.google.gson.GsonBuilder;
21 import com.google.gson.JsonSerializer;
22 import java.io.IOException;
23 import java.io.InputStreamReader;
24 import java.io.Reader;
25 import java.util.concurrent.CompletionException;
26 import org.apache.http.HttpResponse;
27 import org.onap.ccsdk.sli.adaptors.netbox.api.IpamException;
28 import org.onap.ccsdk.sli.adaptors.netbox.api.NetboxClient;
29 import org.onap.ccsdk.sli.adaptors.netbox.model.IPAddress;
30 import org.onap.ccsdk.sli.adaptors.netbox.model.Prefix;
31 import org.onap.ccsdk.sli.adaptors.netbox.model.Status;
33 public class NetboxClientImpl implements NetboxClient {
35 private static final String NEXT_AVAILABLE_IP_IN_PREFIX_PATH = "/api/ipam/prefixes/%s/available-ips/";
36 private static final String IP_ADDRESS_PATH = "/api/ipam/ip-addresses/%s/";
37 private static final String EMPTY_STRING = "";
38 private static final String ID_MISSING_MSG = "Id must be set";
40 private final NetboxHttpClient client;
41 private final Gson gson;
43 public NetboxClientImpl(final NetboxHttpClient client) {
45 final JsonSerializer<Status> vlanStatusDeserializer = (val, type, context) -> val.toJson();
46 gson = new GsonBuilder()
47 .registerTypeAdapter(Status.class, vlanStatusDeserializer)
52 public IPAddress assign(final Prefix prefix) throws IpamException {
53 checkArgument(prefix.getId() != null);
55 return client.post(String.format(NEXT_AVAILABLE_IP_IN_PREFIX_PATH, prefix.getId()), EMPTY_STRING)
56 .thenApply(this::getIpAddress)
57 .toCompletableFuture()
59 } catch (CompletionException e) {
60 // Unwrap the ComplettionException and wrap in IpamException
61 throw new IpamException("Fail to assign IP for Prefix(id= " + prefix.getId() + "). " + e.getMessage(),
67 public void unassign(final IPAddress ipAddress) throws IpamException {
68 checkArgument(ipAddress.getId() != null);
70 client.delete(String.format(IP_ADDRESS_PATH, ipAddress.getId()))
71 .thenAccept(this::checkResult)
72 .toCompletableFuture()
74 } catch (CompletionException e) {
75 // Unwrap the ComplettionException and wrap in IpamException
76 throw new IpamException("Fail to unassign IP for IPAddress(id= " + ipAddress.getId() + "). " + e.getMessage(),
82 IPAddress getIpAddress(final HttpResponse response) {
83 if (response.getStatusLine().getStatusCode() != 201) {
84 throw new IllegalStateException(NetboxHttpClient.getBodyAsString(response));
86 try (final Reader reader = new InputStreamReader(response.getEntity().getContent())) {
87 return gson.fromJson(reader, IPAddress.class);
88 } catch (final IOException e) {
89 throw new IllegalStateException(e);
93 private static void checkArgument(final boolean argument) throws IpamException {
95 throw new IpamException(ID_MISSING_MSG);
99 private void checkResult(final HttpResponse response) {
100 if (response.getStatusLine().getStatusCode() - 200 >= 100) {
101 throw new IllegalStateException(
102 "Netbox request failed with status: " + NetboxHttpClient.getBodyAsString(response));