blob: a94aa10bd6696bb5a4397d3d81368f6c65b3126c [file] [log] [blame]
Jian Li23c8be22018-02-13 11:34:15 +09001/*
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 */
16package org.onosproject.openstacknode.codec;
17
Jian Lie6312162018-03-21 21:41:00 +090018import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ArrayNode;
Jian Li23c8be22018-02-13 11:34:15 +090020import com.fasterxml.jackson.databind.node.ObjectNode;
21import org.onlab.packet.IpAddress;
22import org.onosproject.codec.CodecContext;
23import org.onosproject.codec.JsonCodec;
24import org.onosproject.net.DeviceId;
Jian Li789fadb2018-07-10 13:59:47 +090025import org.onosproject.net.behaviour.ControllerInfo;
26import org.onosproject.openstacknode.api.DefaultOpenstackNode;
Jian Li23c8be22018-02-13 11:34:15 +090027import org.onosproject.openstacknode.api.NodeState;
Jian Li27841662018-04-14 01:59:47 +090028import org.onosproject.openstacknode.api.OpenstackAuth;
Jian Li23c8be22018-02-13 11:34:15 +090029import org.onosproject.openstacknode.api.OpenstackNode;
Jian Lie6312162018-03-21 21:41:00 +090030import org.onosproject.openstacknode.api.OpenstackPhyInterface;
Daniel Parkdeefa702018-07-17 17:55:51 +090031import org.onosproject.openstacknode.api.OpenstackSshAuth;
Jian Li23c8be22018-02-13 11:34:15 +090032import org.slf4j.Logger;
33
Jian Lie6312162018-03-21 21:41:00 +090034import java.util.ArrayList;
35import java.util.List;
36import java.util.stream.IntStream;
37
Jian Li23c8be22018-02-13 11:34:15 +090038import static com.google.common.base.Preconditions.checkNotNull;
39import static org.onlab.util.Tools.nullIsIllegal;
Jian Li27841662018-04-14 01:59:47 +090040import static org.onosproject.openstacknode.api.Constants.CONTROLLER;
Jian Li23c8be22018-02-13 11:34:15 +090041import static org.onosproject.openstacknode.api.Constants.DATA_IP;
42import static org.onosproject.openstacknode.api.Constants.GATEWAY;
43import static org.onosproject.openstacknode.api.Constants.HOST_NAME;
44import static org.onosproject.openstacknode.api.Constants.MANAGEMENT_IP;
45import static org.onosproject.openstacknode.api.Constants.UPLINK_PORT;
46import static org.onosproject.openstacknode.api.Constants.VLAN_INTF_NAME;
47import static org.slf4j.LoggerFactory.getLogger;
48
49/**
50 * Openstack node codec used for serializing and de-serializing JSON string.
51 */
52public 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 Li5ca0be82018-02-27 13:08:04 +090058 private static final String STATE = "state";
Jian Lie6312162018-03-21 21:41:00 +090059 private static final String PHYSICAL_INTERFACES = "phyIntfs";
Jian Li789fadb2018-07-10 13:59:47 +090060 private static final String CONTROLLERS = "controllers";
Jian Li27841662018-04-14 01:59:47 +090061 private static final String AUTHENTICATION = "authentication";
Jian Li92d42fc2018-05-25 16:23:49 +090062 private static final String END_POINT = "endPoint";
Daniel Parkdeefa702018-07-17 17:55:51 +090063 private static final String SSH_AUTH = "sshAuth";
Jian Li23c8be22018-02-13 11:34:15 +090064
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 Li92d42fc2018-05-25 16:23:49 +090074 .put(STATE, node.state().name())
75 .put(MANAGEMENT_IP, node.managementIp().toString());
Jian Li23c8be22018-02-13 11:34:15 +090076
77 OpenstackNode.NodeType type = node.type();
78
79 if (type == OpenstackNode.NodeType.GATEWAY) {
80 result.put(UPLINK_PORT, node.uplinkPort());
81 }
82
Jian Li27841662018-04-14 01:59:47 +090083 if (type != OpenstackNode.NodeType.CONTROLLER) {
84 result.put(INTEGRATION_BRIDGE, node.intgBridge().toString());
Jian Lic07b5ca2018-05-25 14:41:48 +090085 } else {
Jian Li92d42fc2018-05-25 16:23:49 +090086 result.put(END_POINT, node.endPoint());
Jian Li27841662018-04-14 01:59:47 +090087 }
88
Jian Li23c8be22018-02-13 11:34:15 +090089 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 Li5b402c72018-02-27 14:27:34 +090097 // TODO: need to find a way to not refer to ServiceDirectory from
98 // DefaultOpenstackNode
Jian Li5ca0be82018-02-27 13:08:04 +090099
Jian Lie6312162018-03-21 21:41:00 +0900100 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 Li789fadb2018-07-10 13:59:47 +0900107 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 Li27841662018-04-14 01:59:47 +0900113 if (node.authentication() != null) {
114 ObjectNode authJson = context.codec(OpenstackAuth.class)
115 .encode(node.authentication(), context);
Daniel Parkdeefa702018-07-17 17:55:51 +0900116 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 Li27841662018-04-14 01:59:47 +0900123 }
124
Jian Li23c8be22018-02-13 11:34:15 +0900125 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 Li92d42fc2018-05-25 16:23:49 +0900138 String mIp = nullIsIllegal(json.get(MANAGEMENT_IP).asText(),
139 MANAGEMENT_IP + MISSING_MESSAGE);
Jian Li23c8be22018-02-13 11:34:15 +0900140
141 DefaultOpenstackNode.Builder nodeBuilder = DefaultOpenstackNode.builder()
142 .hostname(hostname)
143 .type(OpenstackNode.NodeType.valueOf(type))
Jian Li92d42fc2018-05-25 16:23:49 +0900144 .state(NodeState.INIT)
145 .managementIp(IpAddress.valueOf(mIp));
Jian Li23c8be22018-02-13 11:34:15 +0900146
147 if (type.equals(GATEWAY)) {
148 nodeBuilder.uplinkPort(nullIsIllegal(json.get(UPLINK_PORT).asText(),
149 UPLINK_PORT + MISSING_MESSAGE));
150 }
Jian Li27841662018-04-14 01:59:47 +0900151 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 Lic07b5ca2018-05-25 14:41:48 +0900155 } else {
Jian Li92d42fc2018-05-25 16:23:49 +0900156 String endPoint = nullIsIllegal(json.get(END_POINT).asText(),
157 END_POINT + MISSING_MESSAGE);
158 nodeBuilder.endPoint(endPoint);
Jian Li27841662018-04-14 01:59:47 +0900159 }
Jian Li23c8be22018-02-13 11:34:15 +0900160 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 Lie6312162018-03-21 21:41:00 +0900167 // parse physical interfaces
168 List<OpenstackPhyInterface> phyIntfs = new ArrayList<>();
169 JsonNode phyIntfsJson = json.get(PHYSICAL_INTERFACES);
170 if (phyIntfsJson != null) {
Jian Li27841662018-04-14 01:59:47 +0900171
172 final JsonCodec<OpenstackPhyInterface>
173 phyIntfCodec = context.codec(OpenstackPhyInterface.class);
174
Jian Lie6312162018-03-21 21:41:00 +0900175 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 Li789fadb2018-07-10 13:59:47 +0900182 // 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 Li27841662018-04-14 01:59:47 +0900197 // 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 Parkdeefa702018-07-17 17:55:51 +0900207 // 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 Li23c8be22018-02-13 11:34:15 +0900216 log.trace("node is {}", nodeBuilder.build().toString());
217
218 return nodeBuilder.build();
219 }
220}