blob: adf8bd7cf788bed714573ff5cb5fcdcd6089c805 [file] [log] [blame]
Yi Tsenga87b40c2017-09-10 00:59:03 -07001/*
2 * Copyright 2017-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 */
16
17package org.onosproject.codec.impl;
18
19import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import org.onosproject.codec.CodecContext;
22import org.onosproject.codec.JsonCodec;
Carmelo Cascone87892e22017-11-13 16:01:29 -080023import org.onosproject.net.pi.model.PiMatchFieldModel;
Yi Tsenga87b40c2017-09-10 00:59:03 -070024import org.onosproject.net.pi.model.PiTableModel;
25
26/**
27 * Codec for PiTableModel.
28 */
29public class PiTableModelCodec extends JsonCodec<PiTableModel> {
30 private static final String NAME = "name";
31 private static final String MAX_SIZE = "maxSize";
32 private static final String HAS_COUNTERS = "hasCounters";
33 private static final String SUPPORT_AGING = "supportAging";
34 private static final String ACTIONS = "actions";
35 private static final String MATCH_FIELDS = "matchFields";
36
37
38 @Override
39 public ObjectNode encode(PiTableModel table, CodecContext context) {
40
41 ObjectNode result = context.mapper().createObjectNode();
42
Carmelo Cascone87892e22017-11-13 16:01:29 -080043 result.put(NAME, table.id().toString());
Yi Tsenga87b40c2017-09-10 00:59:03 -070044 result.put(MAX_SIZE, table.maxSize());
Carmelo Cascone87892e22017-11-13 16:01:29 -080045 result.put(HAS_COUNTERS, table.counters().size() > 0);
Yi Tsenga87b40c2017-09-10 00:59:03 -070046 result.put(SUPPORT_AGING, table.supportsAging());
47
48 ArrayNode matchFields = result.putArray(MATCH_FIELDS);
49 table.matchFields().forEach(matchField -> {
50 ObjectNode matchFieldData =
Carmelo Cascone87892e22017-11-13 16:01:29 -080051 context.encode(matchField, PiMatchFieldModel.class);
Yi Tsenga87b40c2017-09-10 00:59:03 -070052 matchFields.add(matchFieldData);
53 });
54
55 ArrayNode actions = result.putArray(ACTIONS);
56 table.actions().forEach(action -> {
Carmelo Cascone87892e22017-11-13 16:01:29 -080057 actions.add(action.id().toString());
Yi Tsenga87b40c2017-09-10 00:59:03 -070058 });
59
60 return result;
61 }
62}