blob: 1154e408363432ed0c550d00ed1f18423462ba0f [file] [log] [blame]
Charles Chan8d316332018-06-19 20:31:57 -07001/*
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.segmentrouting.xconnect.api;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import com.google.common.collect.Sets;
22import org.onlab.packet.VlanId;
23import org.onosproject.codec.CodecContext;
24import org.onosproject.codec.JsonCodec;
25import org.onosproject.net.DeviceId;
Charles Chan8d316332018-06-19 20:31:57 -070026import org.slf4j.Logger;
27import org.slf4j.LoggerFactory;
28
29import java.util.Set;
30
Charles Chan445659f2019-01-02 13:46:16 -080031/**
32 * Codec for Xconnect.
33 */
Charles Chan8d316332018-06-19 20:31:57 -070034public class XconnectCodec extends JsonCodec<XconnectDesc> {
35 private static final String DEVICE_ID = "deviceId";
36 private static final String VLAN_ID = "vlanId";
Charles Chan445659f2019-01-02 13:46:16 -080037 private static final String ENDPOINTS = "endpoints";
Charles Chan8d316332018-06-19 20:31:57 -070038
39 private static Logger log = LoggerFactory.getLogger(XconnectCodec.class);
40
41 @Override
42 public ObjectNode encode(XconnectDesc desc, CodecContext context) {
43 final ObjectNode result = context.mapper().createObjectNode();
44 result.put(DEVICE_ID, desc.key().deviceId().toString());
45 result.put(VLAN_ID, desc.key().vlanId().toString());
Charles Chan445659f2019-01-02 13:46:16 -080046 final ArrayNode portNode = result.putArray(ENDPOINTS);
47 desc.endpoints().forEach(endpoint -> portNode.add(endpoint.toString()));
Charles Chan8d316332018-06-19 20:31:57 -070048
49 return result;
50 }
51
52 @Override
53 public XconnectDesc decode(ObjectNode json, CodecContext context) {
54 DeviceId deviceId = DeviceId.deviceId(json.path(DEVICE_ID).asText());
55 VlanId vlanId = VlanId.vlanId(json.path(VLAN_ID).asText());
56
Charles Chan445659f2019-01-02 13:46:16 -080057 Set<XconnectEndpoint> endpoints = Sets.newHashSet();
58 JsonNode endpointNodes = json.get(ENDPOINTS);
59 if (endpointNodes != null) {
60 XconnectEndpoint endpoint = XconnectEndpoint.fromString(endpointNodes.asText());
61 endpointNodes.forEach(endpointNode -> endpoints.add(endpoint));
Charles Chan8d316332018-06-19 20:31:57 -070062 }
63
64 XconnectKey key = new XconnectKey(deviceId, vlanId);
Charles Chan445659f2019-01-02 13:46:16 -080065 return new XconnectDesc(key, endpoints);
Charles Chan8d316332018-06-19 20:31:57 -070066 }
67}