Jian Li | f16e885 | 2019-01-22 22:55:31 +0900 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2019-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.k8snode.util; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 19 | import com.google.common.base.Strings; |
| 20 | import org.onosproject.k8snode.api.K8sNode; |
| 21 | import org.onosproject.net.Device; |
| 22 | import org.onosproject.net.behaviour.BridgeConfig; |
| 23 | import org.onosproject.net.behaviour.BridgeName; |
| 24 | import org.onosproject.net.device.DeviceService; |
| 25 | import org.onosproject.ovsdb.controller.OvsdbClientService; |
| 26 | import org.onosproject.ovsdb.controller.OvsdbController; |
| 27 | import org.onosproject.ovsdb.controller.OvsdbNodeId; |
| 28 | import org.slf4j.Logger; |
| 29 | import org.slf4j.LoggerFactory; |
| 30 | |
| 31 | import java.io.IOException; |
| 32 | import java.util.Dictionary; |
| 33 | |
| 34 | import static org.onlab.util.Tools.get; |
| 35 | |
| 36 | /** |
| 37 | * An utility that used in kubernetes node app. |
| 38 | */ |
| 39 | public final class K8sNodeUtil { |
| 40 | private static final Logger log = LoggerFactory.getLogger(K8sNodeUtil.class); |
| 41 | |
| 42 | /** |
| 43 | * Prevents object installation from external. |
| 44 | */ |
| 45 | private K8sNodeUtil() { |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Checks whether the controller has a connection with an OVSDB that resides |
| 50 | * inside the given kubernetes node. |
| 51 | * |
| 52 | * @param node kubernetes node |
| 53 | * @param ovsdbPort OVSDB port |
| 54 | * @param ovsdbController OVSDB controller |
| 55 | * @param deviceService device service |
| 56 | * @return true if the controller is connected to the OVSDB, false otherwise |
| 57 | */ |
| 58 | public static boolean isOvsdbConnected(K8sNode node, |
| 59 | int ovsdbPort, |
| 60 | OvsdbController ovsdbController, |
| 61 | DeviceService deviceService) { |
| 62 | OvsdbClientService client = getOvsdbClient(node, ovsdbPort, ovsdbController); |
| 63 | return deviceService.isAvailable(node.ovsdb()) && |
| 64 | client != null && |
| 65 | client.isConnected(); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Gets the ovsdb client with supplied openstack node. |
| 70 | * |
| 71 | * @param node kubernetes node |
| 72 | * @param ovsdbPort ovsdb port |
| 73 | * @param ovsdbController ovsdb controller |
| 74 | * @return ovsdb client |
| 75 | */ |
| 76 | public static OvsdbClientService getOvsdbClient(K8sNode node, |
| 77 | int ovsdbPort, |
| 78 | OvsdbController ovsdbController) { |
| 79 | OvsdbNodeId ovsdb = new OvsdbNodeId(node.managementIp(), ovsdbPort); |
| 80 | return ovsdbController.getOvsdbClient(ovsdb); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Adds or removes a network interface (aka port) into a given bridge of kubernetes node. |
| 85 | * |
| 86 | * @param k8sNode kubernetes node |
| 87 | * @param bridgeName bridge name |
| 88 | * @param intfName interface name |
| 89 | * @param deviceService device service |
| 90 | * @param addOrRemove add port is true, remove it otherwise |
| 91 | */ |
| 92 | public static synchronized void addOrRemoveSystemInterface(K8sNode k8sNode, |
| 93 | String bridgeName, |
| 94 | String intfName, |
| 95 | DeviceService deviceService, |
| 96 | boolean addOrRemove) { |
| 97 | |
| 98 | |
| 99 | Device device = deviceService.getDevice(k8sNode.ovsdb()); |
| 100 | if (device == null || !device.is(BridgeConfig.class)) { |
| 101 | log.info("device is null or this device if not ovsdb device"); |
| 102 | return; |
| 103 | } |
| 104 | BridgeConfig bridgeConfig = device.as(BridgeConfig.class); |
| 105 | |
| 106 | if (addOrRemove) { |
| 107 | bridgeConfig.addPort(BridgeName.bridgeName(bridgeName), intfName); |
| 108 | } else { |
| 109 | bridgeConfig.deletePort(BridgeName.bridgeName(bridgeName), intfName); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Gets Boolean property from the propertyName |
| 115 | * Return null if propertyName is not found. |
| 116 | * |
| 117 | * @param properties properties to be looked up |
| 118 | * @param propertyName the name of the property to look up |
| 119 | * @return value when the propertyName is defined or return null |
| 120 | */ |
| 121 | public static Boolean getBooleanProperty(Dictionary<?, ?> properties, |
| 122 | String propertyName) { |
| 123 | Boolean value; |
| 124 | try { |
| 125 | String s = get(properties, propertyName); |
| 126 | value = Strings.isNullOrEmpty(s) ? null : Boolean.valueOf(s); |
| 127 | } catch (ClassCastException e) { |
| 128 | value = null; |
| 129 | } |
| 130 | return value; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Prints out the JSON string in pretty format. |
| 135 | * |
| 136 | * @param mapper Object mapper |
| 137 | * @param jsonString JSON string |
| 138 | * @return pretty formatted JSON string |
| 139 | */ |
| 140 | public static String prettyJson(ObjectMapper mapper, String jsonString) { |
| 141 | try { |
| 142 | Object jsonObject = mapper.readValue(jsonString, Object.class); |
| 143 | return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonObject); |
| 144 | } catch (IOException e) { |
| 145 | log.debug("Json string parsing exception caused by {}", e); |
| 146 | } |
| 147 | return null; |
| 148 | } |
| 149 | } |