Yi Tseng | a87b40c | 2017-09-10 00:59:03 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.codec.impl; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import org.onosproject.codec.CodecContext; |
| 22 | import org.onosproject.codec.JsonCodec; |
| 23 | import org.onosproject.net.pi.model.PiActionModel; |
| 24 | import org.onosproject.net.pi.model.PiHeaderModel; |
| 25 | import org.onosproject.net.pi.model.PiPipelineModel; |
| 26 | import org.onosproject.net.pi.model.PiTableModel; |
| 27 | |
| 28 | /** |
| 29 | * Codec for PiPipelineModel. |
| 30 | */ |
| 31 | public 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 | } |