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