blob: 77824e87cc399e84502cd87913222085673397eb [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;
26import org.onosproject.net.PortNumber;
27import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29
30import java.util.Set;
31
32public class XconnectCodec extends JsonCodec<XconnectDesc> {
33 private static final String DEVICE_ID = "deviceId";
34 private static final String VLAN_ID = "vlanId";
35 private static final String PORTS = "ports";
36
37 private static Logger log = LoggerFactory.getLogger(XconnectCodec.class);
38
39 @Override
40 public ObjectNode encode(XconnectDesc desc, CodecContext context) {
41 final ObjectNode result = context.mapper().createObjectNode();
42 result.put(DEVICE_ID, desc.key().deviceId().toString());
43 result.put(VLAN_ID, desc.key().vlanId().toString());
44 final ArrayNode portNode = result.putArray(PORTS);
45 desc.ports().forEach(port -> portNode.add(port.toString()));
46
47 return result;
48 }
49
50 @Override
51 public XconnectDesc decode(ObjectNode json, CodecContext context) {
52 DeviceId deviceId = DeviceId.deviceId(json.path(DEVICE_ID).asText());
53 VlanId vlanId = VlanId.vlanId(json.path(VLAN_ID).asText());
54
55 Set<PortNumber> ports = Sets.newHashSet();
56 JsonNode portNodes = json.get(PORTS);
57 if (portNodes != null) {
58 portNodes.forEach(portNode -> ports.add(PortNumber.portNumber(portNode.asInt())));
59 }
60
61 XconnectKey key = new XconnectKey(deviceId, vlanId);
62 return new XconnectDesc(key, ports);
63 }
64}