blob: a37c6745fb9fd9c6fb1880f6234d571ec145370c [file] [log] [blame]
sangho5db8e052016-01-29 16:08:23 +09001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
sangho5db8e052016-01-29 16:08:23 +09003 *
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 */
sangho93447f12016-02-24 00:33:22 +090016package org.onosproject.openstackinterface.web;
sangho5db8e052016-01-29 16:08:23 +090017
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;
sangho93447f12016-02-24 00:33:22 +090024import org.onosproject.openstackinterface.OpenstackSecurityGroup;
25import org.onosproject.openstackinterface.OpenstackSecurityGroupRule;
sangho5db8e052016-01-29 16:08:23 +090026import 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
sangho0c2a3da2016-02-16 13:39:07 +090036 private final Logger log = LoggerFactory.getLogger(getClass());
sangho5db8e052016-01-29 16:08:23 +090037
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";
sangho90088532016-02-25 18:06:12 +090051 private static final String TENANT_ID = "tenant_id";
sangho5db8e052016-01-29 16:08:23 +090052
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 =
sangho0c2a3da2016-02-16 13:39:07 +090068 new OpenstackSecurityGroupRule.Builder()
sangho5db8e052016-01-29 16:08:23 +090069 .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())
sangho90088532016-02-25 18:06:12 +090078 .tenantId(ruleInfo.path(TENANT_ID).asText())
sangho5db8e052016-01-29 16:08:23 +090079 .build();
80
81 rules.add(openstackSecurityGroupRule);
82 }
sangho90088532016-02-25 18:06:12 +090083 String tenantId = securityGroupNode.path(TENANT_ID).asText();
sangho5db8e052016-01-29 16:08:23 +090084
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}