Jian Li | 858ccd7 | 2021-02-04 17:25:01 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021-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.kubevirtnetworking.web; |
| 17 | |
| 18 | import org.onosproject.kubevirtnode.api.KubevirtNode; |
| 19 | import org.onosproject.kubevirtnode.api.KubevirtNodeAdminService; |
| 20 | import org.onosproject.rest.AbstractWebResource; |
| 21 | import org.slf4j.Logger; |
| 22 | import org.slf4j.LoggerFactory; |
| 23 | |
| 24 | import javax.ws.rs.GET; |
| 25 | import javax.ws.rs.Path; |
| 26 | import javax.ws.rs.Produces; |
| 27 | import javax.ws.rs.core.MediaType; |
| 28 | import javax.ws.rs.core.Response; |
| 29 | |
| 30 | import static java.lang.Thread.sleep; |
| 31 | import static org.onosproject.kubevirtnode.api.KubevirtNode.Type.WORKER; |
| 32 | import static org.onosproject.kubevirtnode.api.KubevirtNodeState.COMPLETE; |
| 33 | import static org.onosproject.kubevirtnode.api.KubevirtNodeState.INIT; |
| 34 | |
| 35 | /** |
| 36 | * REST interface for synchronizing kubevirt network states and rules. |
| 37 | */ |
| 38 | @Path("management") |
| 39 | public class KubevirtManagementWebResource extends AbstractWebResource { |
| 40 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 41 | |
| 42 | private static final long SLEEP_MS = 5000; // we wait 5s for init each node |
| 43 | private static final long TIMEOUT_MS = 10000; // we wait 10s |
| 44 | |
Jian Li | 858ccd7 | 2021-02-04 17:25:01 +0900 | [diff] [blame] | 45 | /** |
| 46 | * Synchronizes the flow rules. |
| 47 | * |
| 48 | * @return 200 OK with sync result, 404 not found |
| 49 | */ |
| 50 | @GET |
| 51 | @Produces(MediaType.APPLICATION_JSON) |
| 52 | @Path("sync/rules") |
| 53 | public Response syncRules() { |
| 54 | |
Jian Li | 3581300 | 2021-02-19 10:28:01 +0900 | [diff] [blame] | 55 | KubevirtNodeAdminService service = get(KubevirtNodeAdminService.class); |
| 56 | |
| 57 | service.completeNodes(WORKER).forEach(this::syncRulesBase); |
Jian Li | 858ccd7 | 2021-02-04 17:25:01 +0900 | [diff] [blame] | 58 | return ok(mapper().createObjectNode()).build(); |
| 59 | } |
| 60 | |
| 61 | private void syncRulesBase(KubevirtNode node) { |
| 62 | KubevirtNode updated = node.updateState(INIT); |
Jian Li | 3581300 | 2021-02-19 10:28:01 +0900 | [diff] [blame] | 63 | KubevirtNodeAdminService service = get(KubevirtNodeAdminService.class); |
| 64 | service.updateNode(updated); |
Jian Li | 858ccd7 | 2021-02-04 17:25:01 +0900 | [diff] [blame] | 65 | |
| 66 | boolean result = true; |
| 67 | long timeoutExpiredMs = System.currentTimeMillis() + TIMEOUT_MS; |
| 68 | |
Jian Li | 3581300 | 2021-02-19 10:28:01 +0900 | [diff] [blame] | 69 | while (service.node(node.hostname()).state() != COMPLETE) { |
Jian Li | 858ccd7 | 2021-02-04 17:25:01 +0900 | [diff] [blame] | 70 | |
| 71 | long waitMs = timeoutExpiredMs - System.currentTimeMillis(); |
| 72 | |
| 73 | try { |
| 74 | sleep(SLEEP_MS); |
| 75 | } catch (InterruptedException e) { |
| 76 | log.error("Exception caused during node synchronization..."); |
| 77 | } |
| 78 | |
Jian Li | 3581300 | 2021-02-19 10:28:01 +0900 | [diff] [blame] | 79 | if (service.node(node.hostname()).state() == COMPLETE) { |
Jian Li | 858ccd7 | 2021-02-04 17:25:01 +0900 | [diff] [blame] | 80 | break; |
| 81 | } else { |
Jian Li | 3581300 | 2021-02-19 10:28:01 +0900 | [diff] [blame] | 82 | service.updateNode(updated); |
Jian Li | 858ccd7 | 2021-02-04 17:25:01 +0900 | [diff] [blame] | 83 | log.info("Failed to synchronize flow rules, retrying..."); |
| 84 | } |
| 85 | |
| 86 | if (waitMs <= 0) { |
| 87 | result = false; |
| 88 | break; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | if (result) { |
| 93 | log.info("Successfully synchronize flow rules for node {}!", node.hostname()); |
| 94 | } else { |
| 95 | log.warn("Failed to synchronize flow rules for node {}.", node.hostname()); |
| 96 | } |
| 97 | } |
| 98 | } |