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 com.google.common.collect.Lists; |
| 22 | import org.onosproject.codec.CodecContext; |
| 23 | import org.onosproject.codec.JsonCodec; |
| 24 | import org.onosproject.net.pi.model.PiPipeconf; |
| 25 | |
| 26 | /** |
| 27 | * Codec for PiPipeconf. |
| 28 | */ |
| 29 | public class PiPipeconfCodec extends JsonCodec<PiPipeconf> { |
| 30 | |
| 31 | private static final String ID = "id"; |
| 32 | private static final String BEHAVIORS = "behaviors"; |
| 33 | private static final String EXTENSIONS = "extensions"; |
| 34 | |
| 35 | @Override |
| 36 | public ObjectNode encode(PiPipeconf pipeconf, CodecContext context) { |
| 37 | ObjectNode result = context.mapper().createObjectNode(); |
| 38 | result.put(ID, pipeconf.id().id()); |
| 39 | ArrayNode behaviors = result.putArray(BEHAVIORS); |
| 40 | pipeconf.behaviours().forEach(behavior -> { |
| 41 | behaviors.add(behavior.getSimpleName()); |
| 42 | }); |
| 43 | ArrayNode extensions = result.putArray(EXTENSIONS); |
| 44 | Lists.newArrayList(PiPipeconf.ExtensionType.values()).forEach(extension -> { |
| 45 | if (pipeconf.extension(extension).isPresent()) { |
| 46 | extensions.add(extension.toString()); |
| 47 | } |
| 48 | }); |
| 49 | return result; |
| 50 | } |
| 51 | } |