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