blob: 4f89c3cfbf839f4a7284f95bd63b5c0d2d31415d [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;
25import org.onosproject.openstacknode.api.NodeState;
Jian Li27841662018-04-14 01:59:47 +090026import org.onosproject.openstacknode.api.OpenstackAuth;
Jian Li23c8be22018-02-13 11:34:15 +090027import org.onosproject.openstacknode.api.OpenstackNode;
Jian Lie6312162018-03-21 21:41:00 +090028import org.onosproject.openstacknode.api.OpenstackPhyInterface;
Jian Li5a38ab62018-07-02 22:34:11 +090029import org.onosproject.openstacknode.api.DefaultOpenstackNode;
Jian Li23c8be22018-02-13 11:34:15 +090030import org.slf4j.Logger;
31
Jian Lie6312162018-03-21 21:41:00 +090032import java.util.ArrayList;
33import java.util.List;
34import java.util.stream.IntStream;
35
Jian Li23c8be22018-02-13 11:34:15 +090036import static com.google.common.base.Preconditions.checkNotNull;
37import static org.onlab.util.Tools.nullIsIllegal;
Jian Li27841662018-04-14 01:59:47 +090038import static org.onosproject.openstacknode.api.Constants.CONTROLLER;
Jian Li23c8be22018-02-13 11:34:15 +090039import static org.onosproject.openstacknode.api.Constants.DATA_IP;
40import static org.onosproject.openstacknode.api.Constants.GATEWAY;
41import static org.onosproject.openstacknode.api.Constants.HOST_NAME;
42import static org.onosproject.openstacknode.api.Constants.MANAGEMENT_IP;
43import static org.onosproject.openstacknode.api.Constants.UPLINK_PORT;
44import static org.onosproject.openstacknode.api.Constants.VLAN_INTF_NAME;
45import static org.slf4j.LoggerFactory.getLogger;
46
47/**
48 * Openstack node codec used for serializing and de-serializing JSON string.
49 */
50public 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 Li5ca0be82018-02-27 13:08:04 +090056 private static final String STATE = "state";
Jian Lie6312162018-03-21 21:41:00 +090057 private static final String PHYSICAL_INTERFACES = "phyIntfs";
Jian Li27841662018-04-14 01:59:47 +090058 private static final String AUTHENTICATION = "authentication";
Jian Li92d42fc2018-05-25 16:23:49 +090059 private static final String END_POINT = "endPoint";
Jian Li23c8be22018-02-13 11:34:15 +090060
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 Li92d42fc2018-05-25 16:23:49 +090070 .put(STATE, node.state().name())
71 .put(MANAGEMENT_IP, node.managementIp().toString());
Jian Li23c8be22018-02-13 11:34:15 +090072
73 OpenstackNode.NodeType type = node.type();
74
75 if (type == OpenstackNode.NodeType.GATEWAY) {
76 result.put(UPLINK_PORT, node.uplinkPort());
77 }
78
Jian Li27841662018-04-14 01:59:47 +090079 if (type != OpenstackNode.NodeType.CONTROLLER) {
80 result.put(INTEGRATION_BRIDGE, node.intgBridge().toString());
Jian Lic07b5ca2018-05-25 14:41:48 +090081 } else {
Jian Li92d42fc2018-05-25 16:23:49 +090082 result.put(END_POINT, node.endPoint());
Jian Li27841662018-04-14 01:59:47 +090083 }
84
Jian Li23c8be22018-02-13 11:34:15 +090085 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 Li5b402c72018-02-27 14:27:34 +090093 // TODO: need to find a way to not refer to ServiceDirectory from
94 // DefaultOpenstackNode
Jian Li5ca0be82018-02-27 13:08:04 +090095
Jian Lie6312162018-03-21 21:41:00 +090096 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 Li27841662018-04-14 01:59:47 +0900103 if (node.authentication() != null) {
104 ObjectNode authJson = context.codec(OpenstackAuth.class)
105 .encode(node.authentication(), context);
106 result.put(AUTHENTICATION, authJson);
107 }
108
Jian Li23c8be22018-02-13 11:34:15 +0900109 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 Li92d42fc2018-05-25 16:23:49 +0900122 String mIp = nullIsIllegal(json.get(MANAGEMENT_IP).asText(),
123 MANAGEMENT_IP + MISSING_MESSAGE);
Jian Li23c8be22018-02-13 11:34:15 +0900124
125 DefaultOpenstackNode.Builder nodeBuilder = DefaultOpenstackNode.builder()
126 .hostname(hostname)
127 .type(OpenstackNode.NodeType.valueOf(type))
Jian Li92d42fc2018-05-25 16:23:49 +0900128 .state(NodeState.INIT)
129 .managementIp(IpAddress.valueOf(mIp));
Jian Li23c8be22018-02-13 11:34:15 +0900130
131 if (type.equals(GATEWAY)) {
132 nodeBuilder.uplinkPort(nullIsIllegal(json.get(UPLINK_PORT).asText(),
133 UPLINK_PORT + MISSING_MESSAGE));
134 }
Jian Li27841662018-04-14 01:59:47 +0900135 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 Lic07b5ca2018-05-25 14:41:48 +0900139 } else {
Jian Li92d42fc2018-05-25 16:23:49 +0900140 String endPoint = nullIsIllegal(json.get(END_POINT).asText(),
141 END_POINT + MISSING_MESSAGE);
142 nodeBuilder.endPoint(endPoint);
Jian Li27841662018-04-14 01:59:47 +0900143 }
Jian Li23c8be22018-02-13 11:34:15 +0900144 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 Lie6312162018-03-21 21:41:00 +0900151 // parse physical interfaces
152 List<OpenstackPhyInterface> phyIntfs = new ArrayList<>();
153 JsonNode phyIntfsJson = json.get(PHYSICAL_INTERFACES);
154 if (phyIntfsJson != null) {
Jian Li27841662018-04-14 01:59:47 +0900155
156 final JsonCodec<OpenstackPhyInterface>
157 phyIntfCodec = context.codec(OpenstackPhyInterface.class);
158
Jian Lie6312162018-03-21 21:41:00 +0900159 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 Li27841662018-04-14 01:59:47 +0900166 // 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 Li23c8be22018-02-13 11:34:15 +0900176 log.trace("node is {}", nodeBuilder.build().toString());
177
178 return nodeBuilder.build();
179 }
180}