blob: 08558a8ef40a92ccd7b645cb2cf96e6f71d229d9 [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
31public class XconnectCodec extends JsonCodec<XconnectDesc> {
32 private static final String DEVICE_ID = "deviceId";
33 private static final String VLAN_ID = "vlanId";
34 private static final String PORTS = "ports";
35
36 private static Logger log = LoggerFactory.getLogger(XconnectCodec.class);
37
38 @Override
39 public ObjectNode encode(XconnectDesc desc, CodecContext context) {
40 final ObjectNode result = context.mapper().createObjectNode();
41 result.put(DEVICE_ID, desc.key().deviceId().toString());
42 result.put(VLAN_ID, desc.key().vlanId().toString());
43 final ArrayNode portNode = result.putArray(PORTS);
44 desc.ports().forEach(port -> portNode.add(port.toString()));
45
46 return result;
47 }
48
49 @Override
50 public XconnectDesc decode(ObjectNode json, CodecContext context) {
51 DeviceId deviceId = DeviceId.deviceId(json.path(DEVICE_ID).asText());
52 VlanId vlanId = VlanId.vlanId(json.path(VLAN_ID).asText());
53
Charles Chan48df9ad2018-10-30 18:08:59 -070054 Set<String> ports = Sets.newHashSet();
Charles Chan8d316332018-06-19 20:31:57 -070055 JsonNode portNodes = json.get(PORTS);
56 if (portNodes != null) {
Charles Chan48df9ad2018-10-30 18:08:59 -070057 portNodes.forEach(portNode -> ports.add(portNode.asText()));
Charles Chan8d316332018-06-19 20:31:57 -070058 }
59
60 XconnectKey key = new XconnectKey(deviceId, vlanId);
61 return new XconnectDesc(key, ports);
62 }
63}