blob: 888dfe7d1349c9191f7b15e317c7505051d0dd99 [file] [log] [blame]
Jian Li9b199162019-02-10 18:00:35 +09001/*
2 * Copyright 2018-present Open Networking Foundation
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 */
16package org.onosproject.k8snetworking.web;
17
18import com.fasterxml.jackson.databind.node.ObjectNode;
19import org.onlab.packet.IpAddress;
20import org.onosproject.k8snetworking.api.DefaultK8sIpam;
21import org.onosproject.k8snetworking.api.K8sIpam;
22import org.onosproject.k8snetworking.api.K8sIpamAdminService;
23import org.onosproject.k8snetworking.api.K8sNetwork;
24import org.onosproject.k8snetworking.api.K8sNetworkService;
25import org.onosproject.rest.AbstractWebResource;
26import org.slf4j.Logger;
27import org.slf4j.LoggerFactory;
28
29import javax.ws.rs.DELETE;
30import javax.ws.rs.GET;
31import javax.ws.rs.Path;
32import javax.ws.rs.PathParam;
33import javax.ws.rs.Produces;
34import javax.ws.rs.core.MediaType;
35import javax.ws.rs.core.Response;
36
37import static org.onlab.util.Tools.nullIsNotFound;
38
39/**
40 * Handles IPAM related REST API call from CNI plugin.
41 */
42@Path("ipam")
43public class K8sIpamWebResource extends AbstractWebResource {
44
45 protected final Logger log = LoggerFactory.getLogger(getClass());
46
47 private static final String NETWORK_ID_NOT_FOUND = "Network Id is not found";
48 private static final String IP_NOT_ALLOCATED = "IP address cannot be allocated";
49
50 private static final String IPAM = "ipam";
51
52 private final K8sNetworkService networkService = get(K8sNetworkService.class);
53 private final K8sIpamAdminService ipamService = get(K8sIpamAdminService.class);
54
55 /**
56 * Requests for allocating a unique IP address of the given network ID.
57 *
58 * @param netId network identifier
59 * @return 200 OK with the serialized IPAM JSON string
60 * @onos.rsModel K8sIpam
61 */
62 @GET
63 @Path("{netId}")
64 @Produces(MediaType.APPLICATION_JSON)
65 public Response allocateIp(@PathParam("netId") String netId) {
66 log.trace("Received IP allocation request of network " + netId);
67
68 K8sNetwork network =
69 nullIsNotFound(networkService.network(netId), NETWORK_ID_NOT_FOUND);
70
71 IpAddress ip =
72 nullIsNotFound(ipamService.allocateIp(network.networkId()), IP_NOT_ALLOCATED);
73
74 ObjectNode root = mapper().createObjectNode();
75 String ipamId = network.networkId() + "-" + ip.toString();
76 K8sIpam ipam = new DefaultK8sIpam(ipamId, ip, network.networkId());
77 root.set(IPAM, codec(K8sIpam.class).encode(ipam, this));
78
79 return ok(root).build();
80 }
81
82 /**
83 * Requests for releasing the given IP address.
84 *
85 * @param netId network identifier
86 * @param ip IP address
87 * @return 204 NO CONTENT
88 */
89 @DELETE
90 @Path("{netId}/{ip}")
91 public Response releaseIp(@PathParam("netId") String netId,
92 @PathParam("ip") String ip) {
93 log.trace("Received IP release request of network " + netId);
94
95 K8sNetwork network =
96 nullIsNotFound(networkService.network(netId), NETWORK_ID_NOT_FOUND);
97
98 ipamService.releaseIp(network.networkId(), IpAddress.valueOf(ip));
99
100 return Response.noContent().build();
101 }
102}