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; |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame^] | 20 | import org.apache.commons.lang.StringUtils; |
| 21 | import org.onlab.packet.IpAddress; |
| 22 | import org.onosproject.k8snode.api.K8sApiConfig; |
| 23 | import org.onosproject.k8snode.api.K8sApiConfig.Scheme; |
Jian Li | f16e885 | 2019-01-22 22:55:31 +0900 | [diff] [blame] | 24 | import org.onosproject.k8snode.api.K8sNode; |
| 25 | import org.onosproject.net.Device; |
| 26 | import org.onosproject.net.behaviour.BridgeConfig; |
| 27 | import org.onosproject.net.behaviour.BridgeName; |
| 28 | import org.onosproject.net.device.DeviceService; |
| 29 | import org.onosproject.ovsdb.controller.OvsdbClientService; |
| 30 | import org.onosproject.ovsdb.controller.OvsdbController; |
| 31 | import org.onosproject.ovsdb.controller.OvsdbNodeId; |
| 32 | import org.slf4j.Logger; |
| 33 | import org.slf4j.LoggerFactory; |
| 34 | |
| 35 | import java.io.IOException; |
| 36 | import java.util.Dictionary; |
| 37 | |
| 38 | import static org.onlab.util.Tools.get; |
| 39 | |
| 40 | /** |
| 41 | * An utility that used in kubernetes node app. |
| 42 | */ |
| 43 | public final class K8sNodeUtil { |
| 44 | private static final Logger log = LoggerFactory.getLogger(K8sNodeUtil.class); |
| 45 | |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame^] | 46 | private static final String COLON_SLASH = "://"; |
| 47 | private static final String COLON = ":"; |
| 48 | |
Jian Li | f16e885 | 2019-01-22 22:55:31 +0900 | [diff] [blame] | 49 | /** |
| 50 | * Prevents object installation from external. |
| 51 | */ |
| 52 | private K8sNodeUtil() { |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Checks whether the controller has a connection with an OVSDB that resides |
| 57 | * inside the given kubernetes node. |
| 58 | * |
| 59 | * @param node kubernetes node |
| 60 | * @param ovsdbPort OVSDB port |
| 61 | * @param ovsdbController OVSDB controller |
| 62 | * @param deviceService device service |
| 63 | * @return true if the controller is connected to the OVSDB, false otherwise |
| 64 | */ |
| 65 | public static boolean isOvsdbConnected(K8sNode node, |
| 66 | int ovsdbPort, |
| 67 | OvsdbController ovsdbController, |
| 68 | DeviceService deviceService) { |
| 69 | OvsdbClientService client = getOvsdbClient(node, ovsdbPort, ovsdbController); |
| 70 | return deviceService.isAvailable(node.ovsdb()) && |
| 71 | client != null && |
| 72 | client.isConnected(); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Gets the ovsdb client with supplied openstack node. |
| 77 | * |
| 78 | * @param node kubernetes node |
| 79 | * @param ovsdbPort ovsdb port |
| 80 | * @param ovsdbController ovsdb controller |
| 81 | * @return ovsdb client |
| 82 | */ |
| 83 | public static OvsdbClientService getOvsdbClient(K8sNode node, |
| 84 | int ovsdbPort, |
| 85 | OvsdbController ovsdbController) { |
| 86 | OvsdbNodeId ovsdb = new OvsdbNodeId(node.managementIp(), ovsdbPort); |
| 87 | return ovsdbController.getOvsdbClient(ovsdb); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Adds or removes a network interface (aka port) into a given bridge of kubernetes node. |
| 92 | * |
| 93 | * @param k8sNode kubernetes node |
| 94 | * @param bridgeName bridge name |
| 95 | * @param intfName interface name |
| 96 | * @param deviceService device service |
| 97 | * @param addOrRemove add port is true, remove it otherwise |
| 98 | */ |
| 99 | public static synchronized void addOrRemoveSystemInterface(K8sNode k8sNode, |
| 100 | String bridgeName, |
| 101 | String intfName, |
| 102 | DeviceService deviceService, |
| 103 | boolean addOrRemove) { |
| 104 | |
| 105 | |
| 106 | Device device = deviceService.getDevice(k8sNode.ovsdb()); |
| 107 | if (device == null || !device.is(BridgeConfig.class)) { |
| 108 | log.info("device is null or this device if not ovsdb device"); |
| 109 | return; |
| 110 | } |
| 111 | BridgeConfig bridgeConfig = device.as(BridgeConfig.class); |
| 112 | |
| 113 | if (addOrRemove) { |
| 114 | bridgeConfig.addPort(BridgeName.bridgeName(bridgeName), intfName); |
| 115 | } else { |
| 116 | bridgeConfig.deletePort(BridgeName.bridgeName(bridgeName), intfName); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Gets Boolean property from the propertyName |
| 122 | * Return null if propertyName is not found. |
| 123 | * |
| 124 | * @param properties properties to be looked up |
| 125 | * @param propertyName the name of the property to look up |
| 126 | * @return value when the propertyName is defined or return null |
| 127 | */ |
| 128 | public static Boolean getBooleanProperty(Dictionary<?, ?> properties, |
| 129 | String propertyName) { |
| 130 | Boolean value; |
| 131 | try { |
| 132 | String s = get(properties, propertyName); |
| 133 | value = Strings.isNullOrEmpty(s) ? null : Boolean.valueOf(s); |
| 134 | } catch (ClassCastException e) { |
| 135 | value = null; |
| 136 | } |
| 137 | return value; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Prints out the JSON string in pretty format. |
| 142 | * |
| 143 | * @param mapper Object mapper |
| 144 | * @param jsonString JSON string |
| 145 | * @return pretty formatted JSON string |
| 146 | */ |
| 147 | public static String prettyJson(ObjectMapper mapper, String jsonString) { |
| 148 | try { |
| 149 | Object jsonObject = mapper.readValue(jsonString, Object.class); |
| 150 | return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonObject); |
| 151 | } catch (IOException e) { |
| 152 | log.debug("Json string parsing exception caused by {}", e); |
| 153 | } |
| 154 | return null; |
| 155 | } |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame^] | 156 | |
| 157 | /** |
| 158 | * Generates endpoint URL by referring to scheme, ipAddress and port. |
| 159 | * |
| 160 | * @param scheme scheme |
| 161 | * @param ipAddress IP address |
| 162 | * @param port port number |
| 163 | * @return generated endpoint URL |
| 164 | */ |
| 165 | public static String endpoint(Scheme scheme, IpAddress ipAddress, int port) { |
| 166 | StringBuilder endpoint = new StringBuilder(); |
| 167 | String protocol = StringUtils.lowerCase(scheme.name()); |
| 168 | |
| 169 | endpoint.append(protocol); |
| 170 | endpoint.append(COLON_SLASH); |
| 171 | endpoint.append(ipAddress.toString()); |
| 172 | endpoint.append(COLON); |
| 173 | endpoint.append(port); |
| 174 | |
| 175 | return endpoint.toString(); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Generates endpoint URL by referring to scheme, ipAddress and port. |
| 180 | * |
| 181 | * @param apiConfig kubernetes API config |
| 182 | * @return generated endpoint URL |
| 183 | */ |
| 184 | public static String endpoint(K8sApiConfig apiConfig) { |
| 185 | return endpoint(apiConfig.scheme(), apiConfig.ipAddress(), apiConfig.port()); |
| 186 | } |
Jian Li | f16e885 | 2019-01-22 22:55:31 +0900 | [diff] [blame] | 187 | } |