Jian Li | 9b19916 | 2019-02-10 18:00:35 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.k8snetworking.web; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 19 | import org.onlab.packet.IpAddress; |
| 20 | import org.onosproject.k8snetworking.api.DefaultK8sIpam; |
| 21 | import org.onosproject.k8snetworking.api.K8sIpam; |
| 22 | import org.onosproject.k8snetworking.api.K8sIpamAdminService; |
| 23 | import org.onosproject.k8snetworking.api.K8sNetwork; |
| 24 | import org.onosproject.k8snetworking.api.K8sNetworkService; |
| 25 | import org.onosproject.rest.AbstractWebResource; |
| 26 | import org.slf4j.Logger; |
| 27 | import org.slf4j.LoggerFactory; |
| 28 | |
| 29 | import javax.ws.rs.DELETE; |
| 30 | import javax.ws.rs.GET; |
| 31 | import javax.ws.rs.Path; |
| 32 | import javax.ws.rs.PathParam; |
| 33 | import javax.ws.rs.Produces; |
| 34 | import javax.ws.rs.core.MediaType; |
| 35 | import javax.ws.rs.core.Response; |
| 36 | |
| 37 | import static org.onlab.util.Tools.nullIsNotFound; |
| 38 | |
| 39 | /** |
| 40 | * Handles IPAM related REST API call from CNI plugin. |
| 41 | */ |
| 42 | @Path("ipam") |
| 43 | public 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 | } |