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