blob: 282560da43534d7dcb4d7dffed6e6a8f8d328c54 [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;
Jian Li23c8be22018-02-13 11:34:15 +090031import org.slf4j.Logger;
32
Jian Lie6312162018-03-21 21:41:00 +090033import java.util.ArrayList;
34import java.util.List;
35import java.util.stream.IntStream;
36
Jian Li23c8be22018-02-13 11:34:15 +090037import static com.google.common.base.Preconditions.checkNotNull;
38import static org.onlab.util.Tools.nullIsIllegal;
Jian Li27841662018-04-14 01:59:47 +090039import static org.onosproject.openstacknode.api.Constants.CONTROLLER;
Jian Li23c8be22018-02-13 11:34:15 +090040import static org.onosproject.openstacknode.api.Constants.DATA_IP;
41import static org.onosproject.openstacknode.api.Constants.GATEWAY;
42import static org.onosproject.openstacknode.api.Constants.HOST_NAME;
43import static org.onosproject.openstacknode.api.Constants.MANAGEMENT_IP;
44import static org.onosproject.openstacknode.api.Constants.UPLINK_PORT;
45import static org.onosproject.openstacknode.api.Constants.VLAN_INTF_NAME;
46import static org.slf4j.LoggerFactory.getLogger;
47
48/**
49 * Openstack node codec used for serializing and de-serializing JSON string.
50 */
51public final class OpenstackNodeCodec extends JsonCodec<OpenstackNode> {
52
53 private final Logger log = getLogger(getClass());
54
55 private static final String TYPE = "type";
56 private static final String INTEGRATION_BRIDGE = "integrationBridge";
Jian Li5ca0be82018-02-27 13:08:04 +090057 private static final String STATE = "state";
Jian Lie6312162018-03-21 21:41:00 +090058 private static final String PHYSICAL_INTERFACES = "phyIntfs";
Jian Li789fadb2018-07-10 13:59:47 +090059 private static final String CONTROLLERS = "controllers";
Jian Li27841662018-04-14 01:59:47 +090060 private static final String AUTHENTICATION = "authentication";
Jian Li92d42fc2018-05-25 16:23:49 +090061 private static final String END_POINT = "endPoint";
Jian Li23c8be22018-02-13 11:34:15 +090062
63 private static final String MISSING_MESSAGE = " is required in OpenstackNode";
64
65 @Override
66 public ObjectNode encode(OpenstackNode node, CodecContext context) {
67 checkNotNull(node, "Openstack node cannot be null");
68
69 ObjectNode result = context.mapper().createObjectNode()
70 .put(HOST_NAME, node.hostname())
71 .put(TYPE, node.type().name())
Jian Li92d42fc2018-05-25 16:23:49 +090072 .put(STATE, node.state().name())
73 .put(MANAGEMENT_IP, node.managementIp().toString());
Jian Li23c8be22018-02-13 11:34:15 +090074
75 OpenstackNode.NodeType type = node.type();
76
77 if (type == OpenstackNode.NodeType.GATEWAY) {
78 result.put(UPLINK_PORT, node.uplinkPort());
79 }
80
Jian Li27841662018-04-14 01:59:47 +090081 if (type != OpenstackNode.NodeType.CONTROLLER) {
82 result.put(INTEGRATION_BRIDGE, node.intgBridge().toString());
Jian Lic07b5ca2018-05-25 14:41:48 +090083 } else {
Jian Li92d42fc2018-05-25 16:23:49 +090084 result.put(END_POINT, node.endPoint());
Jian Li27841662018-04-14 01:59:47 +090085 }
86
Jian Li23c8be22018-02-13 11:34:15 +090087 if (node.vlanIntf() != null) {
88 result.put(VLAN_INTF_NAME, node.vlanIntf());
89 }
90
91 if (node.dataIp() != null) {
92 result.put(DATA_IP, node.dataIp().toString());
93 }
94
Jian Li5b402c72018-02-27 14:27:34 +090095 // TODO: need to find a way to not refer to ServiceDirectory from
96 // DefaultOpenstackNode
Jian Li5ca0be82018-02-27 13:08:04 +090097
Jian Lie6312162018-03-21 21:41:00 +090098 ArrayNode phyIntfs = context.mapper().createArrayNode();
99 node.phyIntfs().forEach(phyIntf -> {
100 ObjectNode phyIntfJson = context.codec(OpenstackPhyInterface.class).encode(phyIntf, context);
101 phyIntfs.add(phyIntfJson);
102 });
103 result.set(PHYSICAL_INTERFACES, phyIntfs);
104
Jian Li789fadb2018-07-10 13:59:47 +0900105 ArrayNode controllers = context.mapper().createArrayNode();
106 node.controllers().forEach(controller -> {
107 ObjectNode controllerJson = context.codec(ControllerInfo.class).encode(controller, context);
108 controllers.add(controllerJson);
109 });
110
Jian Li27841662018-04-14 01:59:47 +0900111 if (node.authentication() != null) {
112 ObjectNode authJson = context.codec(OpenstackAuth.class)
113 .encode(node.authentication(), context);
114 result.put(AUTHENTICATION, authJson);
115 }
116
Jian Li23c8be22018-02-13 11:34:15 +0900117 return result;
118 }
119
120 @Override
121 public OpenstackNode decode(ObjectNode json, CodecContext context) {
122 if (json == null || !json.isObject()) {
123 return null;
124 }
125
126 String hostname = nullIsIllegal(json.get(HOST_NAME).asText(),
127 HOST_NAME + MISSING_MESSAGE);
128 String type = nullIsIllegal(json.get(TYPE).asText(),
129 TYPE + MISSING_MESSAGE);
Jian Li92d42fc2018-05-25 16:23:49 +0900130 String mIp = nullIsIllegal(json.get(MANAGEMENT_IP).asText(),
131 MANAGEMENT_IP + MISSING_MESSAGE);
Jian Li23c8be22018-02-13 11:34:15 +0900132
133 DefaultOpenstackNode.Builder nodeBuilder = DefaultOpenstackNode.builder()
134 .hostname(hostname)
135 .type(OpenstackNode.NodeType.valueOf(type))
Jian Li92d42fc2018-05-25 16:23:49 +0900136 .state(NodeState.INIT)
137 .managementIp(IpAddress.valueOf(mIp));
Jian Li23c8be22018-02-13 11:34:15 +0900138
139 if (type.equals(GATEWAY)) {
140 nodeBuilder.uplinkPort(nullIsIllegal(json.get(UPLINK_PORT).asText(),
141 UPLINK_PORT + MISSING_MESSAGE));
142 }
Jian Li27841662018-04-14 01:59:47 +0900143 if (!type.equals(CONTROLLER)) {
144 String iBridge = nullIsIllegal(json.get(INTEGRATION_BRIDGE).asText(),
145 INTEGRATION_BRIDGE + MISSING_MESSAGE);
146 nodeBuilder.intgBridge(DeviceId.deviceId(iBridge));
Jian Lic07b5ca2018-05-25 14:41:48 +0900147 } else {
Jian Li92d42fc2018-05-25 16:23:49 +0900148 String endPoint = nullIsIllegal(json.get(END_POINT).asText(),
149 END_POINT + MISSING_MESSAGE);
150 nodeBuilder.endPoint(endPoint);
Jian Li27841662018-04-14 01:59:47 +0900151 }
Jian Li23c8be22018-02-13 11:34:15 +0900152 if (json.get(VLAN_INTF_NAME) != null) {
153 nodeBuilder.vlanIntf(json.get(VLAN_INTF_NAME).asText());
154 }
155 if (json.get(DATA_IP) != null) {
156 nodeBuilder.dataIp(IpAddress.valueOf(json.get(DATA_IP).asText()));
157 }
158
Jian Lie6312162018-03-21 21:41:00 +0900159 // parse physical interfaces
160 List<OpenstackPhyInterface> phyIntfs = new ArrayList<>();
161 JsonNode phyIntfsJson = json.get(PHYSICAL_INTERFACES);
162 if (phyIntfsJson != null) {
Jian Li27841662018-04-14 01:59:47 +0900163
164 final JsonCodec<OpenstackPhyInterface>
165 phyIntfCodec = context.codec(OpenstackPhyInterface.class);
166
Jian Lie6312162018-03-21 21:41:00 +0900167 IntStream.range(0, phyIntfsJson.size()).forEach(i -> {
168 ObjectNode intfJson = get(phyIntfsJson, i);
169 phyIntfs.add(phyIntfCodec.decode(intfJson, context));
170 });
171 }
172 nodeBuilder.phyIntfs(phyIntfs);
173
Jian Li789fadb2018-07-10 13:59:47 +0900174 // parse customized controllers
175 List<ControllerInfo> controllers = new ArrayList<>();
176 JsonNode controllersJson = json.get(CONTROLLERS);
177 if (controllersJson != null) {
178
179 final JsonCodec<ControllerInfo>
180 controllerCodec = context.codec(ControllerInfo.class);
181
182 IntStream.range(0, controllersJson.size()).forEach(i -> {
183 ObjectNode controllerJson = get(controllersJson, i);
184 controllers.add(controllerCodec.decode(controllerJson, context));
185 });
186 }
187 nodeBuilder.controllers(controllers);
188
Jian Li27841662018-04-14 01:59:47 +0900189 // parse authentication
190 JsonNode authJson = json.get(AUTHENTICATION);
191 if (json.get(AUTHENTICATION) != null) {
192
193 final JsonCodec<OpenstackAuth> authCodec = context.codec(OpenstackAuth.class);
194
195 OpenstackAuth auth = authCodec.decode((ObjectNode) authJson.deepCopy(), context);
196 nodeBuilder.authentication(auth);
197 }
198
Jian Li23c8be22018-02-13 11:34:15 +0900199 log.trace("node is {}", nodeBuilder.build().toString());
200
201 return nodeBuilder.build();
202 }
203}