blob: 6b42c8d740eefd8d728fbd9d1c7b9943a951676e [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;
24import org.onosproject.net.pi.model.PiHeaderModel;
25import org.onosproject.net.pi.model.PiPipelineModel;
26import org.onosproject.net.pi.model.PiTableModel;
27
28/**
29 * Codec for PiPipelineModel.
30 */
31public class PiPipelineModelCodec extends JsonCodec<PiPipelineModel> {
32 private static final String HEADERS = "headers";
33 private static final String ACTIONS = "actions";
34 private static final String TABLES = "tables";
35
36 @Override
37 public ObjectNode encode(PiPipelineModel pipeline, CodecContext context) {
38 ObjectNode result = context.mapper().createObjectNode();
39 ArrayNode headers = result.putArray(HEADERS);
40 pipeline.headers().stream()
41 .map(header -> context.encode(header, PiHeaderModel.class))
42 .forEach(headers::add);
43 ArrayNode actions = result.putArray(ACTIONS);
44 pipeline.actions().stream()
45 .map(action -> context.encode(action, PiActionModel.class))
46 .forEach(actions::add);
47 ArrayNode tables = result.putArray(TABLES);
48 pipeline.tables().stream()
49 .map(table -> context.encode(table, PiTableModel.class))
50 .forEach(tables::add);
51 return result;
52 }
53}