0520ad5e5f68ef1d4de3a7a9cbd4da63d7946105
[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.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;
32
33 public class NetboxClientImpl implements NetboxClient {
34
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";
39
40     private final NetboxHttpClient client;
41     private final Gson gson;
42
43     public NetboxClientImpl(final NetboxHttpClient client) {
44         this.client = client;
45         final JsonSerializer<Status> vlanStatusDeserializer = (val, type, context) -> val.toJson();
46         gson = new GsonBuilder()
47             .registerTypeAdapter(Status.class, vlanStatusDeserializer)
48             .create();
49     }
50
51     @Override
52     public IPAddress assign(final Prefix prefix) throws IpamException {
53         checkArgument(prefix.getId() != null);
54         try {
55             return client.post(String.format(NEXT_AVAILABLE_IP_IN_PREFIX_PATH, prefix.getId()), EMPTY_STRING)
56                 .thenApply(this::getIpAddress)
57                 .toCompletableFuture()
58                 .join();
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(),
62                 e.getCause());
63         }
64     }
65
66     @Override
67     public void unassign(final IPAddress ipAddress) throws IpamException {
68         checkArgument(ipAddress.getId() != null);
69         try {
70             client.delete(String.format(IP_ADDRESS_PATH, ipAddress.getId()))
71                 .thenAccept(this::checkResult)
72                 .toCompletableFuture()
73                 .join();
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(),
77                 e.getCause());
78         }
79     }
80
81     @VisibleForTesting
82     IPAddress getIpAddress(final HttpResponse response) {
83         if (response.getStatusLine().getStatusCode() != 201) {
84             throw new IllegalStateException(NetboxHttpClient.getBodyAsString(response));
85         }
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);
90         }
91     }
92
93     private static void checkArgument(final boolean argument) throws IpamException {
94         if (!argument) {
95             throw new IpamException(ID_MISSING_MSG);
96         }
97     }
98
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));
103         }
104     }
105 }