blob: c38984b773d509d9ac53baf54db2a1ac6dfd4db1 [file] [log] [blame]
Jian Li07c27f32020-10-08 02:57:45 +09001/*
2 * Copyright 2020-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.openstacknetworking.web;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import org.onlab.packet.IpAddress;
20import org.onlab.packet.IpPrefix;
21import org.onlab.packet.MacAddress;
22import org.onosproject.openstacknetworking.api.OpenstackK8sIntegrationService;
23import org.onosproject.rest.AbstractWebResource;
24import org.slf4j.Logger;
25import org.slf4j.LoggerFactory;
26
27import javax.ws.rs.Consumes;
28import javax.ws.rs.PUT;
29import javax.ws.rs.Path;
30import javax.ws.rs.Produces;
31import javax.ws.rs.core.MediaType;
32import javax.ws.rs.core.Response;
33import java.io.IOException;
34import java.io.InputStream;
35
36import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT;
37import static org.onlab.util.Tools.readTreeFromStream;
38
39/**
40 * REST interface for integrating openstack and kubernetes.
41 */
42@Path("integration")
43public class OpenstackK8sIntegrationWebResource extends AbstractWebResource {
44 private final Logger log = LoggerFactory.getLogger(getClass());
45
46 private static final String K8S_NODE_IP = "k8sNodeIp";
47 private static final String OS_K8S_INT_PORT_NAME = "osK8sIntPortName";
48 private static final String POD_CIDR = "podCidr";
49 private static final String SERVICE_CIDR = "serviceCidr";
50 private static final String POD_GW_IP = "podGwIp";
51 private static final String K8S_INT_OS_PORT_MAC = "k8sIntOsPortMac";
52
53 private final OpenstackK8sIntegrationService intService =
54 get(OpenstackK8sIntegrationService.class);
55
56 /**
57 * Installs CNI pass-through related flow rules for each kubernetes nodes.
58 *
59 * @param input JSON string
60 * @return 200 ok, 400 BAD_REQUEST if the json is malformed
61 * @throws IOException exception
62 */
63 @PUT
64 @Path("node/pt-install")
65 @Consumes(MediaType.APPLICATION_JSON)
66 @Produces(MediaType.APPLICATION_JSON)
67 public Response installCniPtNodeRules(InputStream input) throws IOException {
68 log.trace("Install K8S CNI pass-through node rules");
69
70 JsonNode json = readTreeFromStream(mapper().enable(INDENT_OUTPUT), input);
71 IpAddress k8sNodeIp = IpAddress.valueOf(json.get(K8S_NODE_IP).asText());
72 IpPrefix podCidr = IpPrefix.valueOf(json.get(POD_CIDR).asText());
73 IpPrefix serviceCidr = IpPrefix.valueOf(json.get(SERVICE_CIDR).asText());
74 IpAddress podGwIp = IpAddress.valueOf(json.get(POD_GW_IP).asText());
75 String osK8sIntPortName = json.get(OS_K8S_INT_PORT_NAME).asText();
76 MacAddress k8sIntOsPortMac = MacAddress.valueOf(json.get(K8S_INT_OS_PORT_MAC).asText());
77
78 intService.installCniPtNodeRules(k8sNodeIp, podCidr, serviceCidr,
79 podGwIp, osK8sIntPortName, k8sIntOsPortMac);
80
81 return Response.ok().build();
82 }
83
84 /**
85 * Installs CNI pass-through related flow rules for each kubernetes nodes.
86 *
87 * @param input JSON string
88 * @return 200 ok, 400 BAD_REQUEST if the json is malformed
89 * @throws IOException exception
90 */
91 @PUT
92 @Path("node/pt-uninstall")
93 @Consumes(MediaType.APPLICATION_JSON)
94 @Produces(MediaType.APPLICATION_JSON)
95 public Response uninstallCniPtNodeRules(InputStream input) throws IOException {
96 log.trace("Uninstall K8S CNI pass-through node rules");
97
98 JsonNode json = readTreeFromStream(mapper().enable(INDENT_OUTPUT), input);
99 IpAddress k8sNodeIp = IpAddress.valueOf(json.get(K8S_NODE_IP).asText());
100 IpPrefix podCidr = IpPrefix.valueOf(json.get(POD_CIDR).asText());
101 IpPrefix serviceCidr = IpPrefix.valueOf(json.get(SERVICE_CIDR).asText());
102 IpAddress podGwIp = IpAddress.valueOf(json.get(POD_GW_IP).asText());
103 String osK8sIntPortName = json.get(OS_K8S_INT_PORT_NAME).asText();
104 MacAddress k8sIntOsPortMac = MacAddress.valueOf(json.get(K8S_INT_OS_PORT_MAC).asText());
105
106 intService.uninstallCniPtNodeRules(k8sNodeIp, podCidr, serviceCidr,
107 podGwIp, osK8sIntPortName, k8sIntOsPortMac);
108
109 return Response.ok().build();
110 }
111}