blob: 4935d9923b2fbeab98e8eb14c371829ad5be3201 [file] [log] [blame]
Ray Milkey39616f32015-05-14 15:43:00 -07001/*
2 * Copyright 2015 Open Networking Laboratory
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.codec.impl;
17
18import org.onosproject.codec.CodecContext;
19import org.onosproject.codec.JsonCodec;
20import org.onosproject.net.driver.Driver;
21
22import com.fasterxml.jackson.databind.node.ArrayNode;
23import com.fasterxml.jackson.databind.node.ObjectNode;
24
25import static com.google.common.base.Preconditions.checkNotNull;
26
27/**
28 * JSON codec for the Driver class.
29 */
30public final class DriverCodec extends JsonCodec<Driver> {
31 private static final String PARENT = "parent";
32 private static final String NAME = "name";
33 private static final String MANUFACTURER = "manufacturer";
34 private static final String HW_VERSION = "hwVersion";
35 private static final String SW_VERSION = "swVersion";
36 private static final String BEHAVIOURS = "behaviours";
37 private static final String BEHAVIORS_NAME = "name";
38 private static final String BEHAVIORS_IMPLEMENTATION_NAME = "implementationName";
39 private static final String PROPERTIES = "properties";
40
41 @Override
42 public ObjectNode encode(Driver driver, CodecContext context) {
43 checkNotNull(driver, "Driver cannot be null");
44
45 ObjectNode result = context.mapper().createObjectNode()
46 .put(NAME, driver.name())
47 .put(MANUFACTURER, driver.manufacturer())
48 .put(HW_VERSION, driver.hwVersion())
49 .put(SW_VERSION, driver.swVersion());
50
51 if (driver.parent() != null) {
52 result.put(PARENT, driver.parent().name());
53 }
54
55 ArrayNode behaviours = context.mapper().createArrayNode();
56 driver.behaviours().forEach(behaviour -> {
57 ObjectNode entry = context.mapper().createObjectNode()
58 .put(BEHAVIORS_NAME, behaviour.getCanonicalName())
59 .put(BEHAVIORS_IMPLEMENTATION_NAME,
60 driver.implementation(behaviour).getCanonicalName());
61
62 behaviours.add(entry);
63 });
64 result.set(BEHAVIOURS, behaviours);
65
66 ArrayNode properties = context.mapper().createArrayNode();
67 driver.properties().forEach((name, value) -> {
68 ObjectNode entry = context.mapper().createObjectNode()
69 .put("name", name)
70 .put("value", value);
71
72 properties.add(entry);
73 });
74 result.set(PROPERTIES, properties);
75
76 return result;
77 }
78}