blob: 468d23709b9c0e56e965058898aa7156f11d0b60 [file] [log] [blame]
Jian Li5c411232015-12-16 15:29:16 -08001/*
2 * Copyright 2014-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 com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import org.onosproject.codec.CodecContext;
22import org.onosproject.codec.JsonCodec;
23import org.onosproject.core.ApplicationId;
24import org.onosproject.core.CoreService;
25import org.onosproject.net.DeviceId;
26import org.onosproject.net.meter.Band;
27import org.onosproject.net.meter.DefaultMeter;
28import org.onosproject.net.meter.Meter;
29import org.onosproject.net.meter.MeterId;
30import org.slf4j.Logger;
31
32import java.util.ArrayList;
33import java.util.List;
34import java.util.stream.IntStream;
35
36import static com.google.common.base.Preconditions.checkNotNull;
37import static org.onlab.util.Tools.nullIsIllegal;
38import static org.slf4j.LoggerFactory.getLogger;
39
40
41/**
42 * Meter JSON codec.
43 */
44public final class MeterCodec extends JsonCodec<Meter> {
45 private final Logger log = getLogger(getClass());
46
47 // JSON field names
48 private static final String ID = "id";
49 private static final String STATE = "state";
50 private static final String LIFE = "life";
51 private static final String PACKETS = "packets";
52 private static final String BYTES = "bytes";
53 private static final String REFERENCE_COUNT = "referenceCount";
54 private static final String APP_ID = "appId";
55 private static final String BURST = "burst";
56 private static final String DEVICE_ID = "deviceId";
57 private static final String UNIT = "unit";
58 private static final String BANDS = "bands";
59 public static final String REST_APP_ID = "org.onosproject.rest";
60 private static final String MISSING_MEMBER_MESSAGE = " member is required in Meter";
61
62 @Override
63 public ObjectNode encode(Meter meter, CodecContext context) {
64 checkNotNull(meter, "Meter cannot be null");
65 ObjectNode result = context.mapper().createObjectNode()
66 .put(ID, meter.id().toString())
67 .put(LIFE, meter.life())
68 .put(PACKETS, meter.packetsSeen())
69 .put(BYTES, meter.bytesSeen())
70 .put(REFERENCE_COUNT, meter.referenceCount())
71 .put(UNIT, meter.unit().toString())
72 .put(BURST, meter.isBurst())
73 .put(DEVICE_ID, meter.deviceId().toString());
74
75 if (meter.appId() != null) {
76 result.put(APP_ID, meter.appId().toString());
77 }
78
79 if (meter.state() != null) {
80 result.put(STATE, meter.state().toString());
81 }
82
83 ArrayNode bands = context.mapper().createArrayNode();
84 meter.bands().forEach(band -> {
85 ObjectNode bandJson = context.codec(Band.class).encode(band, context);
86 bands.add(bandJson);
87 });
88 result.set(BANDS, bands);
89 return result;
90 }
91
92 @Override
93 public Meter decode(ObjectNode json, CodecContext context) {
94 if (json == null || !json.isObject()) {
95 return null;
96 }
97
98 final JsonCodec<Band> meterBandCodec = context.codec(Band.class);
99 CoreService coreService = context.getService(CoreService.class);
100
101 // parse meter id
102 int meterIdInt = nullIsIllegal(json.get(ID), ID + MISSING_MEMBER_MESSAGE).asInt();
103 MeterId meterId = MeterId.meterId(meterIdInt);
104
105 // parse device id
106 DeviceId deviceId = DeviceId.deviceId(nullIsIllegal(json.get(DEVICE_ID),
107 DEVICE_ID + MISSING_MEMBER_MESSAGE).asText());
108
109 // application id
110 ApplicationId appId = coreService.registerApplication(REST_APP_ID);
111
112 // parse burst
113 boolean burst = false;
114 JsonNode burstJson = json.get("burst");
115 if (burstJson != null) {
116 burst = burstJson.asBoolean();
117 }
118
119 // parse unit type
120 String unit = nullIsIllegal(json.get(UNIT), UNIT + MISSING_MEMBER_MESSAGE).asText();
121 Meter.Unit meterUnit;
122
123 switch (unit) {
124 case "KB_PER_SEC":
125 meterUnit = Meter.Unit.KB_PER_SEC;
126 break;
127 case "PKTS_PER_SEC":
128 meterUnit = Meter.Unit.PKTS_PER_SEC;
129 break;
130 default:
131 log.warn("The requested unit {} is not defined for meter.", unit);
132 return null;
133 }
134
135 // parse meter bands
136 List<Band> bandList = new ArrayList<>();
137 JsonNode bandsJson = json.get(BANDS);
138 checkNotNull(bandsJson);
139 if (bandsJson != null) {
140 IntStream.range(0, bandsJson.size()).forEach(i -> {
141 ObjectNode bandJson = get(bandsJson, i);
142 bandList.add(meterBandCodec.decode(bandJson, context));
143 });
144 }
145
146 Meter meter;
147 if (burst) {
148 meter = DefaultMeter.builder()
149 .withId(meterId)
150 .fromApp(appId)
151 .forDevice(deviceId)
152 .withUnit(meterUnit)
153 .withBands(bandList)
154 .burst().build();
155 } else {
156 meter = DefaultMeter.builder()
157 .withId(meterId)
158 .fromApp(appId)
159 .forDevice(deviceId)
160 .withUnit(meterUnit)
161 .withBands(bandList).build();
162 }
163
164 return meter;
165 }
166}