Jian Li | 1c10cf2 | 2021-03-05 01:32:04 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021-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 | package org.onosproject.kubevirtnetworking.codec; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 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.kubevirtnetworking.api.DefaultKubevirtSecurityGroup; |
| 24 | import org.onosproject.kubevirtnetworking.api.KubevirtSecurityGroup; |
| 25 | import org.onosproject.kubevirtnetworking.api.KubevirtSecurityGroupRule; |
| 26 | import org.slf4j.Logger; |
| 27 | |
| 28 | import java.util.HashSet; |
| 29 | import java.util.Set; |
| 30 | import java.util.stream.IntStream; |
| 31 | |
| 32 | import static com.google.common.base.Preconditions.checkNotNull; |
| 33 | import static org.onlab.util.Tools.nullIsIllegal; |
| 34 | import static org.slf4j.LoggerFactory.getLogger; |
| 35 | |
| 36 | /** |
| 37 | * Kubevirt security group codec used for serializing and de-serializing JSON string. |
| 38 | */ |
| 39 | public final class KubevirtSecurityGroupCodec extends JsonCodec<KubevirtSecurityGroup> { |
| 40 | |
| 41 | private final Logger log = getLogger(getClass()); |
| 42 | |
| 43 | private static final String ID = "id"; |
| 44 | private static final String NAME = "name"; |
| 45 | private static final String DESCRIPTION = "description"; |
| 46 | private static final String RULES = "rules"; |
| 47 | |
| 48 | private static final String MISSING_MESSAGE = " is required in KubevirtSecurityGroup"; |
| 49 | |
| 50 | @Override |
| 51 | public ObjectNode encode(KubevirtSecurityGroup sg, CodecContext context) { |
| 52 | checkNotNull(sg, "Kubevirt Security Group cannot be null"); |
| 53 | |
| 54 | ObjectNode result = context.mapper().createObjectNode() |
| 55 | .put(ID, sg.id()) |
| 56 | .put(NAME, sg.name()); |
| 57 | |
| 58 | if (sg.description() != null) { |
| 59 | result.put(DESCRIPTION, sg.description()); |
| 60 | } |
| 61 | |
| 62 | if (sg.rules() != null && !sg.rules().isEmpty()) { |
| 63 | ArrayNode rules = context.mapper().createArrayNode(); |
| 64 | sg.rules().forEach(rule -> { |
| 65 | ObjectNode ruleJson = context.codec( |
| 66 | KubevirtSecurityGroupRule.class).encode(rule, context); |
| 67 | rules.add(ruleJson); |
| 68 | }); |
| 69 | result.set(RULES, rules); |
| 70 | } |
| 71 | |
| 72 | return result; |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public KubevirtSecurityGroup decode(ObjectNode json, CodecContext context) { |
| 77 | if (json == null || !json.isObject()) { |
| 78 | return null; |
| 79 | } |
| 80 | |
| 81 | String id = nullIsIllegal(json.get(ID).asText(), ID + MISSING_MESSAGE); |
| 82 | String name = nullIsIllegal(json.get(NAME).asText(), NAME + MISSING_MESSAGE); |
| 83 | |
| 84 | KubevirtSecurityGroup.Builder builder = DefaultKubevirtSecurityGroup.builder() |
| 85 | .id(id) |
| 86 | .name(name); |
| 87 | |
| 88 | JsonNode description = json.get(DESCRIPTION); |
| 89 | if (description != null) { |
| 90 | builder.description(description.asText()); |
| 91 | } |
| 92 | |
| 93 | JsonNode rulesJson = json.get(RULES); |
| 94 | if (rulesJson != null) { |
| 95 | Set<KubevirtSecurityGroupRule> rules = new HashSet<>(); |
| 96 | IntStream.range(0, rulesJson.size()) |
| 97 | .forEach(i -> { |
| 98 | ObjectNode ruleJson = get(rulesJson, i); |
| 99 | KubevirtSecurityGroupRule rule = context.codec( |
| 100 | KubevirtSecurityGroupRule.class).decode(ruleJson, context); |
| 101 | rules.add(rule); |
| 102 | }); |
| 103 | builder.rules(rules); |
| 104 | } |
| 105 | |
| 106 | return builder.build(); |
| 107 | } |
| 108 | } |