Jian Li | 23c8be2 | 2018-02-13 11:34:15 +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.openstacknode.codec; |
| 17 | |
Jian Li | e631216 | 2018-03-21 21:41:00 +0900 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
Jian Li | 23c8be2 | 2018-02-13 11:34:15 +0900 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import org.onlab.packet.IpAddress; |
| 22 | import org.onosproject.codec.CodecContext; |
| 23 | import org.onosproject.codec.JsonCodec; |
| 24 | import org.onosproject.net.DeviceId; |
Jian Li | 789fadb | 2018-07-10 13:59:47 +0900 | [diff] [blame] | 25 | import org.onosproject.net.behaviour.ControllerInfo; |
| 26 | import org.onosproject.openstacknode.api.DefaultOpenstackNode; |
Daniel Park | d02d7bd | 2018-08-23 23:04:31 +0900 | [diff] [blame^] | 27 | import org.onosproject.openstacknode.api.DpdkConfig; |
Jian Li | 23c8be2 | 2018-02-13 11:34:15 +0900 | [diff] [blame] | 28 | import org.onosproject.openstacknode.api.NodeState; |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 29 | import org.onosproject.openstacknode.api.OpenstackAuth; |
Jian Li | 23c8be2 | 2018-02-13 11:34:15 +0900 | [diff] [blame] | 30 | import org.onosproject.openstacknode.api.OpenstackNode; |
Jian Li | e631216 | 2018-03-21 21:41:00 +0900 | [diff] [blame] | 31 | import org.onosproject.openstacknode.api.OpenstackPhyInterface; |
Daniel Park | deefa70 | 2018-07-17 17:55:51 +0900 | [diff] [blame] | 32 | import org.onosproject.openstacknode.api.OpenstackSshAuth; |
Jian Li | 23c8be2 | 2018-02-13 11:34:15 +0900 | [diff] [blame] | 33 | import org.slf4j.Logger; |
| 34 | |
Jian Li | e631216 | 2018-03-21 21:41:00 +0900 | [diff] [blame] | 35 | import java.util.ArrayList; |
| 36 | import java.util.List; |
| 37 | import java.util.stream.IntStream; |
| 38 | |
Jian Li | 23c8be2 | 2018-02-13 11:34:15 +0900 | [diff] [blame] | 39 | import static com.google.common.base.Preconditions.checkNotNull; |
| 40 | import static org.onlab.util.Tools.nullIsIllegal; |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 41 | import static org.onosproject.openstacknode.api.Constants.CONTROLLER; |
Jian Li | 23c8be2 | 2018-02-13 11:34:15 +0900 | [diff] [blame] | 42 | import static org.onosproject.openstacknode.api.Constants.DATA_IP; |
| 43 | import static org.onosproject.openstacknode.api.Constants.GATEWAY; |
| 44 | import static org.onosproject.openstacknode.api.Constants.HOST_NAME; |
| 45 | import static org.onosproject.openstacknode.api.Constants.MANAGEMENT_IP; |
| 46 | import static org.onosproject.openstacknode.api.Constants.UPLINK_PORT; |
| 47 | import static org.onosproject.openstacknode.api.Constants.VLAN_INTF_NAME; |
| 48 | import static org.slf4j.LoggerFactory.getLogger; |
| 49 | |
| 50 | /** |
| 51 | * Openstack node codec used for serializing and de-serializing JSON string. |
| 52 | */ |
| 53 | public final class OpenstackNodeCodec extends JsonCodec<OpenstackNode> { |
| 54 | |
| 55 | private final Logger log = getLogger(getClass()); |
| 56 | |
| 57 | private static final String TYPE = "type"; |
| 58 | private static final String INTEGRATION_BRIDGE = "integrationBridge"; |
Jian Li | 5ca0be8 | 2018-02-27 13:08:04 +0900 | [diff] [blame] | 59 | private static final String STATE = "state"; |
Jian Li | e631216 | 2018-03-21 21:41:00 +0900 | [diff] [blame] | 60 | private static final String PHYSICAL_INTERFACES = "phyIntfs"; |
Jian Li | 789fadb | 2018-07-10 13:59:47 +0900 | [diff] [blame] | 61 | private static final String CONTROLLERS = "controllers"; |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 62 | private static final String AUTHENTICATION = "authentication"; |
Jian Li | 6d41036 | 2018-08-17 09:41:08 +0900 | [diff] [blame] | 63 | private static final String END_POINT = "endpoint"; |
Daniel Park | deefa70 | 2018-07-17 17:55:51 +0900 | [diff] [blame] | 64 | private static final String SSH_AUTH = "sshAuth"; |
Daniel Park | d02d7bd | 2018-08-23 23:04:31 +0900 | [diff] [blame^] | 65 | private static final String DPDK_CONFIG = "dpdkConfig"; |
Jian Li | 23c8be2 | 2018-02-13 11:34:15 +0900 | [diff] [blame] | 66 | |
| 67 | private static final String MISSING_MESSAGE = " is required in OpenstackNode"; |
| 68 | |
| 69 | @Override |
| 70 | public ObjectNode encode(OpenstackNode node, CodecContext context) { |
| 71 | checkNotNull(node, "Openstack node cannot be null"); |
| 72 | |
| 73 | ObjectNode result = context.mapper().createObjectNode() |
| 74 | .put(HOST_NAME, node.hostname()) |
| 75 | .put(TYPE, node.type().name()) |
Jian Li | 92d42fc | 2018-05-25 16:23:49 +0900 | [diff] [blame] | 76 | .put(STATE, node.state().name()) |
Daniel Park | d02d7bd | 2018-08-23 23:04:31 +0900 | [diff] [blame^] | 77 | .put(MANAGEMENT_IP, node.managementIp().toString()); |
Jian Li | 23c8be2 | 2018-02-13 11:34:15 +0900 | [diff] [blame] | 78 | |
| 79 | OpenstackNode.NodeType type = node.type(); |
| 80 | |
| 81 | if (type == OpenstackNode.NodeType.GATEWAY) { |
| 82 | result.put(UPLINK_PORT, node.uplinkPort()); |
| 83 | } |
| 84 | |
Jian Li | e314154 | 2018-08-13 18:05:43 +0900 | [diff] [blame] | 85 | if (type == OpenstackNode.NodeType.CONTROLLER) { |
Jian Li | 6d41036 | 2018-08-17 09:41:08 +0900 | [diff] [blame] | 86 | result.put(END_POINT, node.endpoint()); |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 87 | } |
| 88 | |
Jian Li | e314154 | 2018-08-13 18:05:43 +0900 | [diff] [blame] | 89 | if (node.intgBridge() != null) { |
| 90 | result.put(INTEGRATION_BRIDGE, node.intgBridge().toString()); |
| 91 | } |
| 92 | |
Jian Li | 23c8be2 | 2018-02-13 11:34:15 +0900 | [diff] [blame] | 93 | if (node.vlanIntf() != null) { |
| 94 | result.put(VLAN_INTF_NAME, node.vlanIntf()); |
| 95 | } |
| 96 | |
| 97 | if (node.dataIp() != null) { |
| 98 | result.put(DATA_IP, node.dataIp().toString()); |
| 99 | } |
| 100 | |
Jian Li | 5b402c7 | 2018-02-27 14:27:34 +0900 | [diff] [blame] | 101 | // TODO: need to find a way to not refer to ServiceDirectory from |
| 102 | // DefaultOpenstackNode |
Jian Li | 5ca0be8 | 2018-02-27 13:08:04 +0900 | [diff] [blame] | 103 | |
Jian Li | e631216 | 2018-03-21 21:41:00 +0900 | [diff] [blame] | 104 | ArrayNode phyIntfs = context.mapper().createArrayNode(); |
| 105 | node.phyIntfs().forEach(phyIntf -> { |
| 106 | ObjectNode phyIntfJson = context.codec(OpenstackPhyInterface.class).encode(phyIntf, context); |
| 107 | phyIntfs.add(phyIntfJson); |
| 108 | }); |
| 109 | result.set(PHYSICAL_INTERFACES, phyIntfs); |
| 110 | |
Jian Li | 789fadb | 2018-07-10 13:59:47 +0900 | [diff] [blame] | 111 | ArrayNode controllers = context.mapper().createArrayNode(); |
| 112 | node.controllers().forEach(controller -> { |
| 113 | ObjectNode controllerJson = context.codec(ControllerInfo.class).encode(controller, context); |
| 114 | controllers.add(controllerJson); |
| 115 | }); |
| 116 | |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 117 | if (node.authentication() != null) { |
| 118 | ObjectNode authJson = context.codec(OpenstackAuth.class) |
| 119 | .encode(node.authentication(), context); |
Daniel Park | deefa70 | 2018-07-17 17:55:51 +0900 | [diff] [blame] | 120 | result.set(AUTHENTICATION, authJson); |
| 121 | } |
| 122 | |
| 123 | if (node.sshAuthInfo() != null) { |
| 124 | ObjectNode sshAuthJson = context.codec(OpenstackSshAuth.class) |
| 125 | .encode(node.sshAuthInfo(), context); |
| 126 | result.set(SSH_AUTH, sshAuthJson); |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 127 | } |
| 128 | |
Daniel Park | d02d7bd | 2018-08-23 23:04:31 +0900 | [diff] [blame^] | 129 | if (node.dpdkConfig() != null) { |
| 130 | ObjectNode dpdkConfigJson = context.codec(DpdkConfig.class) |
| 131 | .encode(node.dpdkConfig(), context); |
| 132 | result.set(DPDK_CONFIG, dpdkConfigJson); |
Daniel Park | 7e8c4d8 | 2018-08-13 23:47:49 +0900 | [diff] [blame] | 133 | } |
| 134 | |
Jian Li | 23c8be2 | 2018-02-13 11:34:15 +0900 | [diff] [blame] | 135 | return result; |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public OpenstackNode decode(ObjectNode json, CodecContext context) { |
| 140 | if (json == null || !json.isObject()) { |
| 141 | return null; |
| 142 | } |
| 143 | |
| 144 | String hostname = nullIsIllegal(json.get(HOST_NAME).asText(), |
| 145 | HOST_NAME + MISSING_MESSAGE); |
| 146 | String type = nullIsIllegal(json.get(TYPE).asText(), |
| 147 | TYPE + MISSING_MESSAGE); |
Jian Li | 92d42fc | 2018-05-25 16:23:49 +0900 | [diff] [blame] | 148 | String mIp = nullIsIllegal(json.get(MANAGEMENT_IP).asText(), |
| 149 | MANAGEMENT_IP + MISSING_MESSAGE); |
Jian Li | 23c8be2 | 2018-02-13 11:34:15 +0900 | [diff] [blame] | 150 | |
| 151 | DefaultOpenstackNode.Builder nodeBuilder = DefaultOpenstackNode.builder() |
| 152 | .hostname(hostname) |
| 153 | .type(OpenstackNode.NodeType.valueOf(type)) |
Jian Li | 92d42fc | 2018-05-25 16:23:49 +0900 | [diff] [blame] | 154 | .state(NodeState.INIT) |
| 155 | .managementIp(IpAddress.valueOf(mIp)); |
Jian Li | 23c8be2 | 2018-02-13 11:34:15 +0900 | [diff] [blame] | 156 | |
| 157 | if (type.equals(GATEWAY)) { |
| 158 | nodeBuilder.uplinkPort(nullIsIllegal(json.get(UPLINK_PORT).asText(), |
| 159 | UPLINK_PORT + MISSING_MESSAGE)); |
| 160 | } |
Jian Li | e314154 | 2018-08-13 18:05:43 +0900 | [diff] [blame] | 161 | if (type.equals(CONTROLLER)) { |
Jian Li | 92d42fc | 2018-05-25 16:23:49 +0900 | [diff] [blame] | 162 | String endPoint = nullIsIllegal(json.get(END_POINT).asText(), |
| 163 | END_POINT + MISSING_MESSAGE); |
Jian Li | 6d41036 | 2018-08-17 09:41:08 +0900 | [diff] [blame] | 164 | nodeBuilder.endpoint(endPoint); |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 165 | } |
Jian Li | 23c8be2 | 2018-02-13 11:34:15 +0900 | [diff] [blame] | 166 | if (json.get(VLAN_INTF_NAME) != null) { |
| 167 | nodeBuilder.vlanIntf(json.get(VLAN_INTF_NAME).asText()); |
| 168 | } |
| 169 | if (json.get(DATA_IP) != null) { |
| 170 | nodeBuilder.dataIp(IpAddress.valueOf(json.get(DATA_IP).asText())); |
| 171 | } |
| 172 | |
Jian Li | e314154 | 2018-08-13 18:05:43 +0900 | [diff] [blame] | 173 | JsonNode intBridgeJson = json.get(INTEGRATION_BRIDGE); |
| 174 | if (intBridgeJson != null) { |
| 175 | nodeBuilder.intgBridge(DeviceId.deviceId(intBridgeJson.asText())); |
| 176 | } |
| 177 | |
Jian Li | e631216 | 2018-03-21 21:41:00 +0900 | [diff] [blame] | 178 | // parse physical interfaces |
| 179 | List<OpenstackPhyInterface> phyIntfs = new ArrayList<>(); |
| 180 | JsonNode phyIntfsJson = json.get(PHYSICAL_INTERFACES); |
| 181 | if (phyIntfsJson != null) { |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 182 | |
| 183 | final JsonCodec<OpenstackPhyInterface> |
| 184 | phyIntfCodec = context.codec(OpenstackPhyInterface.class); |
| 185 | |
Jian Li | e631216 | 2018-03-21 21:41:00 +0900 | [diff] [blame] | 186 | IntStream.range(0, phyIntfsJson.size()).forEach(i -> { |
| 187 | ObjectNode intfJson = get(phyIntfsJson, i); |
| 188 | phyIntfs.add(phyIntfCodec.decode(intfJson, context)); |
| 189 | }); |
| 190 | } |
| 191 | nodeBuilder.phyIntfs(phyIntfs); |
| 192 | |
Jian Li | 789fadb | 2018-07-10 13:59:47 +0900 | [diff] [blame] | 193 | // parse customized controllers |
| 194 | List<ControllerInfo> controllers = new ArrayList<>(); |
| 195 | JsonNode controllersJson = json.get(CONTROLLERS); |
| 196 | if (controllersJson != null) { |
| 197 | |
| 198 | final JsonCodec<ControllerInfo> |
| 199 | controllerCodec = context.codec(ControllerInfo.class); |
| 200 | |
| 201 | IntStream.range(0, controllersJson.size()).forEach(i -> { |
| 202 | ObjectNode controllerJson = get(controllersJson, i); |
| 203 | controllers.add(controllerCodec.decode(controllerJson, context)); |
| 204 | }); |
| 205 | } |
| 206 | nodeBuilder.controllers(controllers); |
| 207 | |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 208 | // parse authentication |
| 209 | JsonNode authJson = json.get(AUTHENTICATION); |
Daniel Park | d02d7bd | 2018-08-23 23:04:31 +0900 | [diff] [blame^] | 210 | if (authJson != null) { |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 211 | |
| 212 | final JsonCodec<OpenstackAuth> authCodec = context.codec(OpenstackAuth.class); |
| 213 | |
| 214 | OpenstackAuth auth = authCodec.decode((ObjectNode) authJson.deepCopy(), context); |
| 215 | nodeBuilder.authentication(auth); |
| 216 | } |
| 217 | |
Daniel Park | deefa70 | 2018-07-17 17:55:51 +0900 | [diff] [blame] | 218 | // parse ssh authentication |
| 219 | JsonNode sshAuthJson = json.get(SSH_AUTH); |
Daniel Park | d02d7bd | 2018-08-23 23:04:31 +0900 | [diff] [blame^] | 220 | if (sshAuthJson != null) { |
Daniel Park | deefa70 | 2018-07-17 17:55:51 +0900 | [diff] [blame] | 221 | final JsonCodec<OpenstackSshAuth> sshAuthJsonCodec = context.codec(OpenstackSshAuth.class); |
| 222 | |
| 223 | OpenstackSshAuth sshAuth = sshAuthJsonCodec.decode((ObjectNode) sshAuthJson.deepCopy(), context); |
| 224 | nodeBuilder.sshAuthInfo(sshAuth); |
| 225 | } |
| 226 | |
Daniel Park | d02d7bd | 2018-08-23 23:04:31 +0900 | [diff] [blame^] | 227 | JsonNode dpdkConfigJson = json.get(DPDK_CONFIG); |
| 228 | if (dpdkConfigJson != null) { |
| 229 | final JsonCodec<DpdkConfig> dpdkConfigJsonCodec = context.codec(DpdkConfig.class); |
| 230 | |
| 231 | DpdkConfig dpdkConfig = dpdkConfigJsonCodec.decode((ObjectNode) dpdkConfigJson.deepCopy(), context); |
| 232 | nodeBuilder.dpdkConfig(dpdkConfig); |
| 233 | } |
| 234 | |
Jian Li | 23c8be2 | 2018-02-13 11:34:15 +0900 | [diff] [blame] | 235 | log.trace("node is {}", nodeBuilder.build().toString()); |
| 236 | |
| 237 | return nodeBuilder.build(); |
| 238 | } |
| 239 | } |