blob: 28a7010002206e95fb76d61d7eade349d1ec29a3 [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;
23import org.onosproject.net.pi.model.PiActionModel;
Yi Tsenga87b40c2017-09-10 00:59:03 -070024import org.onosproject.net.pi.model.PiPipelineModel;
25import org.onosproject.net.pi.model.PiTableModel;
26
27/**
28 * Codec for PiPipelineModel.
29 */
30public class PiPipelineModelCodec extends JsonCodec<PiPipelineModel> {
Yi Tsenga87b40c2017-09-10 00:59:03 -070031 private static final String ACTIONS = "actions";
32 private static final String TABLES = "tables";
33
34 @Override
35 public ObjectNode encode(PiPipelineModel pipeline, CodecContext context) {
36 ObjectNode result = context.mapper().createObjectNode();
Yi Tsenga87b40c2017-09-10 00:59:03 -070037 ArrayNode actions = result.putArray(ACTIONS);
Carmelo Cascone87892e22017-11-13 16:01:29 -080038 pipeline.tables()
39 .stream()
40 .flatMap(piTableModel -> piTableModel.actions().stream())
41 .distinct()
Yi Tsenga87b40c2017-09-10 00:59:03 -070042 .map(action -> context.encode(action, PiActionModel.class))
43 .forEach(actions::add);
44 ArrayNode tables = result.putArray(TABLES);
45 pipeline.tables().stream()
46 .map(table -> context.encode(table, PiTableModel.class))
47 .forEach(tables::add);
48 return result;
49 }
50}