sangho | 5db8e05 | 2016-01-29 16:08:23 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
sangho | 5db8e05 | 2016-01-29 16:08:23 +0900 | [diff] [blame] | 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 | */ |
sangho | 93447f1 | 2016-02-24 00:33:22 +0900 | [diff] [blame] | 16 | package org.onosproject.openstackinterface.web; |
sangho | 5db8e05 | 2016-01-29 16:08:23 +0900 | [diff] [blame] | 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 com.google.common.collect.Lists; |
| 22 | import org.onosproject.codec.CodecContext; |
| 23 | import org.onosproject.codec.JsonCodec; |
sangho | 93447f1 | 2016-02-24 00:33:22 +0900 | [diff] [blame] | 24 | import org.onosproject.openstackinterface.OpenstackSecurityGroup; |
| 25 | import org.onosproject.openstackinterface.OpenstackSecurityGroupRule; |
sangho | 5db8e05 | 2016-01-29 16:08:23 +0900 | [diff] [blame] | 26 | import org.slf4j.Logger; |
| 27 | import org.slf4j.LoggerFactory; |
| 28 | |
| 29 | import java.util.Collection; |
| 30 | |
| 31 | /** |
| 32 | * Encodes and decodes the Openstack Security Group. |
| 33 | */ |
| 34 | public class OpenstackSecurityGroupCodec extends JsonCodec<OpenstackSecurityGroup> { |
| 35 | |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 36 | private final Logger log = LoggerFactory.getLogger(getClass()); |
sangho | 5db8e05 | 2016-01-29 16:08:23 +0900 | [diff] [blame] | 37 | |
| 38 | private static final String SECURITY_GROUP = "security_group"; |
| 39 | private static final String DESCRIPTION = "description"; |
| 40 | private static final String ID = "id"; |
| 41 | private static final String NAME = "name"; |
| 42 | private static final String SECURITY_GROUP_RULES = "security_group_rules"; |
| 43 | private static final String DIRECTION = "direction"; |
| 44 | private static final String EHTERTYPE = "ethertype"; |
| 45 | private static final String PORT_RANGE_MAX = "port_range_max"; |
| 46 | private static final String PORT_RANGE_MIN = "port_range_min"; |
| 47 | private static final String PROTOCOL = "protocol"; |
| 48 | private static final String REMOTE_GROUP_ID = "remote_group_id"; |
| 49 | private static final String REMOTE_IP_PREFIX = "remote_ip_prefix"; |
| 50 | private static final String SECURITY_GROUP_ID = "security_group_id"; |
sangho | 9008853 | 2016-02-25 18:06:12 +0900 | [diff] [blame] | 51 | private static final String TENANT_ID = "tenant_id"; |
sangho | 5db8e05 | 2016-01-29 16:08:23 +0900 | [diff] [blame] | 52 | |
| 53 | @Override |
| 54 | public OpenstackSecurityGroup decode(ObjectNode json, CodecContext context) { |
| 55 | JsonNode securityGroupNode = json.get(SECURITY_GROUP); |
| 56 | if (securityGroupNode == null) { |
| 57 | log.warn("SecurityGroup Json data is null"); |
| 58 | return null; |
| 59 | } |
| 60 | |
| 61 | String description = securityGroupNode.path(DESCRIPTION).asText(); |
| 62 | String id = securityGroupNode.path(ID).asText(); |
| 63 | String name = securityGroupNode.path(NAME).asText(); |
| 64 | ArrayNode ruleInfoList = (ArrayNode) securityGroupNode.path(SECURITY_GROUP_RULES); |
| 65 | Collection<OpenstackSecurityGroupRule> rules = Lists.newArrayList(); |
| 66 | for (JsonNode ruleInfo: ruleInfoList) { |
| 67 | OpenstackSecurityGroupRule openstackSecurityGroupRule = |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 68 | new OpenstackSecurityGroupRule.Builder() |
sangho | 5db8e05 | 2016-01-29 16:08:23 +0900 | [diff] [blame] | 69 | .direction(ruleInfo.path(DIRECTION).asText()) |
| 70 | .etherType(ruleInfo.path(EHTERTYPE).asText()) |
| 71 | .id(ruleInfo.path(ID).asText()) |
| 72 | .portRangeMax(ruleInfo.path(PORT_RANGE_MAX).asText()) |
| 73 | .portRangeMin(ruleInfo.path(PORT_RANGE_MIN).asText()) |
| 74 | .protocol(ruleInfo.path(PROTOCOL).asText()) |
| 75 | .remoteGroupId(ruleInfo.path(REMOTE_GROUP_ID).asText()) |
| 76 | .remoteIpPrefix(ruleInfo.path(REMOTE_IP_PREFIX).asText()) |
| 77 | .securityGroupId(ruleInfo.path(SECURITY_GROUP_ID).asText()) |
sangho | 9008853 | 2016-02-25 18:06:12 +0900 | [diff] [blame] | 78 | .tenantId(ruleInfo.path(TENANT_ID).asText()) |
sangho | 5db8e05 | 2016-01-29 16:08:23 +0900 | [diff] [blame] | 79 | .build(); |
| 80 | |
| 81 | rules.add(openstackSecurityGroupRule); |
| 82 | } |
sangho | 9008853 | 2016-02-25 18:06:12 +0900 | [diff] [blame] | 83 | String tenantId = securityGroupNode.path(TENANT_ID).asText(); |
sangho | 5db8e05 | 2016-01-29 16:08:23 +0900 | [diff] [blame] | 84 | |
| 85 | OpenstackSecurityGroup openstackSecurityGroup = OpenstackSecurityGroup.builder() |
| 86 | .description(description) |
| 87 | .id(id) |
| 88 | .name(name) |
| 89 | .rules(rules) |
| 90 | .tenantId(tenantId) |
| 91 | .build(); |
| 92 | |
| 93 | return openstackSecurityGroup; |
| 94 | } |
| 95 | } |