sangho
Committed by Gerrit Code Review

[ONOS-3793] Implements the Openstack Security Group REST call parser

Change-Id: Ie6665d4ebae26e363853db28fb2a535c55f1e2ec
...@@ -15,13 +15,12 @@ ...@@ -15,13 +15,12 @@
15 */ 15 */
16 package org.onosproject.openstackswitching; 16 package org.onosproject.openstackswitching;
17 17
18 -import com.google.common.collect.Lists;
19 import org.onlab.packet.Ip4Address; 18 import org.onlab.packet.Ip4Address;
20 import org.onlab.packet.MacAddress; 19 import org.onlab.packet.MacAddress;
21 20
21 +import java.util.Collection;
22 import java.util.Collections; 22 import java.util.Collections;
23 import java.util.HashMap; 23 import java.util.HashMap;
24 -import java.util.List;
25 24
26 import static com.google.common.base.Preconditions.checkNotNull; 25 import static com.google.common.base.Preconditions.checkNotNull;
27 26
...@@ -49,13 +48,13 @@ public final class OpenstackPort { ...@@ -49,13 +48,13 @@ public final class OpenstackPort {
49 // <subnet id, ip address> 48 // <subnet id, ip address>
50 private HashMap<String, Ip4Address> fixedIps; 49 private HashMap<String, Ip4Address> fixedIps;
51 private String id; 50 private String id;
52 - private List<String> securityGroups; 51 + private Collection<String> securityGroups;
53 private String deviceId; 52 private String deviceId;
54 53
55 private OpenstackPort(PortStatus status, String name, boolean adminStateUp, 54 private OpenstackPort(PortStatus status, String name, boolean adminStateUp,
56 String networkId, String tenantId, String deviceOwner, 55 String networkId, String tenantId, String deviceOwner,
57 MacAddress macAddress, HashMap fixedIps, String id, 56 MacAddress macAddress, HashMap fixedIps, String id,
58 - List<String> securityGroups, String deviceId) { 57 + Collection<String> securityGroups, String deviceId) {
59 58
60 this.status = status; 59 this.status = status;
61 this.name = name; 60 this.name = name;
...@@ -158,7 +157,7 @@ public final class OpenstackPort { ...@@ -158,7 +157,7 @@ public final class OpenstackPort {
158 * 157 *
159 * @return security group info 158 * @return security group info
160 */ 159 */
161 - public List<String> securityGroups() { 160 + public Collection<String> securityGroups() {
162 return securityGroups; 161 return securityGroups;
163 } 162 }
164 163
...@@ -187,7 +186,7 @@ public final class OpenstackPort { ...@@ -187,7 +186,7 @@ public final class OpenstackPort {
187 OpenstackPort op = new OpenstackPort(this.status, this.name, this.adminStateUp, 186 OpenstackPort op = new OpenstackPort(this.status, this.name, this.adminStateUp,
188 this.networkId, this.tenantId, this.deviceOwner, this.macAddress, 187 this.networkId, this.tenantId, this.deviceOwner, this.macAddress,
189 (HashMap) this.fixedIps.clone(), this.id, 188 (HashMap) this.fixedIps.clone(), this.id,
190 - Collections.unmodifiableList(this.securityGroups), this.deviceId); 189 + Collections.unmodifiableCollection(this.securityGroups), this.deviceId);
191 190
192 return op; 191 return op;
193 } 192 }
...@@ -209,12 +208,11 @@ public final class OpenstackPort { ...@@ -209,12 +208,11 @@ public final class OpenstackPort {
209 // list of hash map <subnet id, ip address> 208 // list of hash map <subnet id, ip address>
210 private HashMap<String, Ip4Address> fixedIps; 209 private HashMap<String, Ip4Address> fixedIps;
211 private String id; 210 private String id;
212 - private List<String> securityGroups; 211 + private Collection<String> securityGroups;
213 private String deviceId; 212 private String deviceId;
214 213
215 Builder() { 214 Builder() {
216 fixedIps = new HashMap<>(); 215 fixedIps = new HashMap<>();
217 - securityGroups = Lists.newArrayList();
218 } 216 }
219 217
220 /** 218 /**
...@@ -328,12 +326,11 @@ public final class OpenstackPort { ...@@ -328,12 +326,11 @@ public final class OpenstackPort {
328 /** 326 /**
329 * Sets security group of the port. 327 * Sets security group of the port.
330 * 328 *
331 - * @param securityGroup security group of the port 329 + * @param securityGroupList security group list of the port
332 * @return Builder object 330 * @return Builder object
333 */ 331 */
334 - public Builder securityGroup(String securityGroup) { 332 + public Builder securityGroup(Collection<String> securityGroupList) {
335 - securityGroups.add(securityGroup); 333 + this.securityGroups = securityGroupList;
336 -
337 return this; 334 return this;
338 } 335 }
339 336
......
...@@ -17,6 +17,8 @@ package org.onosproject.openstackswitching; ...@@ -17,6 +17,8 @@ package org.onosproject.openstackswitching;
17 17
18 import org.onlab.packet.Ip4Address; 18 import org.onlab.packet.Ip4Address;
19 19
20 +import java.util.Collection;
21 +import java.util.Collections;
20 import java.util.List; 22 import java.util.List;
21 23
22 import static com.google.common.base.Preconditions.checkNotNull; 24 import static com.google.common.base.Preconditions.checkNotNull;
...@@ -34,10 +36,11 @@ public final class OpenstackSubnet { ...@@ -34,10 +36,11 @@ public final class OpenstackSubnet {
34 private String gatewayIp; 36 private String gatewayIp;
35 private String cidr; 37 private String cidr;
36 private String id; 38 private String id;
39 + private Collection<String> securityGroups;
37 40
38 private OpenstackSubnet(String name, boolean enableHhcp, String networkId, 41 private OpenstackSubnet(String name, boolean enableHhcp, String networkId,
39 String tenantId, List<Ip4Address> dnsNameservers, String gatewayIp, 42 String tenantId, List<Ip4Address> dnsNameservers, String gatewayIp,
40 - String cidr, String id) { 43 + String cidr, String id, Collection<String> securityGroups) {
41 this.name = name; 44 this.name = name;
42 this.enableHhcp = enableHhcp; 45 this.enableHhcp = enableHhcp;
43 this.networkId = checkNotNull(networkId); 46 this.networkId = checkNotNull(networkId);
...@@ -46,6 +49,7 @@ public final class OpenstackSubnet { ...@@ -46,6 +49,7 @@ public final class OpenstackSubnet {
46 this.gatewayIp = gatewayIp; 49 this.gatewayIp = gatewayIp;
47 this.cidr = checkNotNull(cidr); 50 this.cidr = checkNotNull(cidr);
48 this.id = checkNotNull(id); 51 this.id = checkNotNull(id);
52 + this.securityGroups = securityGroups;
49 } 53 }
50 54
51 /** 55 /**
...@@ -89,6 +93,10 @@ public final class OpenstackSubnet { ...@@ -89,6 +93,10 @@ public final class OpenstackSubnet {
89 return id; 93 return id;
90 } 94 }
91 95
96 + public Collection<String> securityGroups() {
97 + return Collections.unmodifiableCollection(this.securityGroups);
98 + }
99 +
92 /** 100 /**
93 * OpenstackSubnet Builder class. 101 * OpenstackSubnet Builder class.
94 * 102 *
...@@ -102,6 +110,7 @@ public final class OpenstackSubnet { ...@@ -102,6 +110,7 @@ public final class OpenstackSubnet {
102 private String gatewayIp; 110 private String gatewayIp;
103 private String cidr; 111 private String cidr;
104 private String id; 112 private String id;
113 + private Collection<String> securityGroups;
105 114
106 Builder() {} 115 Builder() {}
107 116
...@@ -153,9 +162,15 @@ public final class OpenstackSubnet { ...@@ -153,9 +162,15 @@ public final class OpenstackSubnet {
153 return this; 162 return this;
154 } 163 }
155 164
165 + public Builder securityGroups(Collection<String> securityGroups) {
166 + this.securityGroups = securityGroups;
167 +
168 + return this;
169 + }
170 +
156 public OpenstackSubnet build() { 171 public OpenstackSubnet build() {
157 return new OpenstackSubnet(name, enableDhcp, networkId, tenantId, 172 return new OpenstackSubnet(name, enableDhcp, networkId, tenantId,
158 - dnsNameservers, gatewayIp, cidr, id); 173 + dnsNameservers, gatewayIp, cidr, id, securityGroups);
159 } 174 }
160 } 175 }
161 } 176 }
......
...@@ -26,6 +26,7 @@ import org.onosproject.openstackswitching.OpenstackPort; ...@@ -26,6 +26,7 @@ import org.onosproject.openstackswitching.OpenstackPort;
26 import org.onosproject.openstackswitching.OpenstackSubnet; 26 import org.onosproject.openstackswitching.OpenstackSubnet;
27 import org.onosproject.openstackswitching.web.OpenstackNetworkCodec; 27 import org.onosproject.openstackswitching.web.OpenstackNetworkCodec;
28 import org.onosproject.openstackswitching.web.OpenstackPortCodec; 28 import org.onosproject.openstackswitching.web.OpenstackPortCodec;
29 +import org.onosproject.openstackswitching.web.OpenstackSecurityGroupCodec;
29 import org.onosproject.openstackswitching.web.OpenstackSubnetCodec; 30 import org.onosproject.openstackswitching.web.OpenstackSubnetCodec;
30 import org.slf4j.Logger; 31 import org.slf4j.Logger;
31 import javax.ws.rs.core.MediaType; 32 import javax.ws.rs.core.MediaType;
...@@ -44,6 +45,21 @@ import static org.slf4j.LoggerFactory.getLogger; ...@@ -44,6 +45,21 @@ import static org.slf4j.LoggerFactory.getLogger;
44 */ 45 */
45 public class OpenstackRestHandler { 46 public class OpenstackRestHandler {
46 47
48 + private static final String URI_NETWORKS = "networks";
49 + private static final String URI_PORTS = "ports";
50 + private static final String URI_SUBNETS = "subnets";
51 + private static final String URI_SECURITY_GROUPS = "security-groups";
52 + private static final String URI_TOKENS = "tokens";
53 +
54 + private static final String PATH_NETWORKS = "networks";
55 + private static final String PATH_PORTS = "ports";
56 + private static final String PATH_SUBNETS = "subnets";
57 + private static final String PATH_ACCESS = "access";
58 + private static final String PATH_TOKEN = "token";
59 + private static final String PATH_ID = "id";
60 +
61 + private static final String HEADER_AUTH_TOKEN = "X-Auth-Token";
62 +
47 private final Logger log = getLogger(getClass()); 63 private final Logger log = getLogger(getClass());
48 private String neutronUrl; 64 private String neutronUrl;
49 private String keystoneUrl; 65 private String keystoneUrl;
...@@ -70,9 +86,9 @@ public class OpenstackRestHandler { ...@@ -70,9 +86,9 @@ public class OpenstackRestHandler {
70 */ 86 */
71 public Collection<OpenstackNetwork> getNetworks() { 87 public Collection<OpenstackNetwork> getNetworks() {
72 88
73 - WebResource.Builder builder = getClientBuilder(neutronUrl + "networks"); 89 + WebResource.Builder builder = getClientBuilder(neutronUrl + URI_NETWORKS);
74 String response = builder.accept(MediaType.APPLICATION_JSON_TYPE). 90 String response = builder.accept(MediaType.APPLICATION_JSON_TYPE).
75 - header("X-Auth-Token", getToken()).get(String.class); 91 + header(HEADER_AUTH_TOKEN, getToken()).get(String.class);
76 92
77 log.debug("networks response:" + response); 93 log.debug("networks response:" + response);
78 94
...@@ -80,7 +96,7 @@ public class OpenstackRestHandler { ...@@ -80,7 +96,7 @@ public class OpenstackRestHandler {
80 List<OpenstackNetwork> openstackNetworks = Lists.newArrayList(); 96 List<OpenstackNetwork> openstackNetworks = Lists.newArrayList();
81 try { 97 try {
82 ObjectNode node = (ObjectNode) mapper.readTree(response); 98 ObjectNode node = (ObjectNode) mapper.readTree(response);
83 - ArrayNode networkList = (ArrayNode) node.path("networks"); 99 + ArrayNode networkList = (ArrayNode) node.path(PATH_NETWORKS);
84 OpenstackNetworkCodec networkCodec = new OpenstackNetworkCodec(); 100 OpenstackNetworkCodec networkCodec = new OpenstackNetworkCodec();
85 networkList.forEach(n -> openstackNetworks.add(networkCodec.decode((ObjectNode) n, null))); 101 networkList.forEach(n -> openstackNetworks.add(networkCodec.decode((ObjectNode) n, null)));
86 } catch (IOException e) { 102 } catch (IOException e) {
...@@ -100,15 +116,15 @@ public class OpenstackRestHandler { ...@@ -100,15 +116,15 @@ public class OpenstackRestHandler {
100 */ 116 */
101 public Collection<OpenstackPort> getPorts() { 117 public Collection<OpenstackPort> getPorts() {
102 118
103 - WebResource.Builder builder = getClientBuilder(neutronUrl + "ports"); 119 + WebResource.Builder builder = getClientBuilder(neutronUrl + URI_PORTS);
104 String response = builder.accept(MediaType.APPLICATION_JSON_TYPE). 120 String response = builder.accept(MediaType.APPLICATION_JSON_TYPE).
105 - header("X-Auth-Token", getToken()).get(String.class); 121 + header(HEADER_AUTH_TOKEN, getToken()).get(String.class);
106 122
107 ObjectMapper mapper = new ObjectMapper(); 123 ObjectMapper mapper = new ObjectMapper();
108 List<OpenstackPort> openstackPorts = Lists.newArrayList(); 124 List<OpenstackPort> openstackPorts = Lists.newArrayList();
109 try { 125 try {
110 ObjectNode node = (ObjectNode) mapper.readTree(response); 126 ObjectNode node = (ObjectNode) mapper.readTree(response);
111 - ArrayNode portList = (ArrayNode) node.path("ports"); 127 + ArrayNode portList = (ArrayNode) node.path(PATH_PORTS);
112 OpenstackPortCodec portCodec = new OpenstackPortCodec(); 128 OpenstackPortCodec portCodec = new OpenstackPortCodec();
113 portList.forEach(p -> openstackPorts.add(portCodec.decode((ObjectNode) p, null))); 129 portList.forEach(p -> openstackPorts.add(portCodec.decode((ObjectNode) p, null)));
114 } catch (IOException e) { 130 } catch (IOException e) {
...@@ -128,15 +144,15 @@ public class OpenstackRestHandler { ...@@ -128,15 +144,15 @@ public class OpenstackRestHandler {
128 */ 144 */
129 public Collection<OpenstackSubnet> getSubnets() { 145 public Collection<OpenstackSubnet> getSubnets() {
130 146
131 - WebResource.Builder builder = getClientBuilder(neutronUrl + "subnets"); 147 + WebResource.Builder builder = getClientBuilder(neutronUrl + URI_SUBNETS);
132 String response = builder.accept(MediaType.APPLICATION_JSON_TYPE). 148 String response = builder.accept(MediaType.APPLICATION_JSON_TYPE).
133 - header("X-Auth-Token", getToken()).get(String.class); 149 + header(HEADER_AUTH_TOKEN, getToken()).get(String.class);
134 150
135 ObjectMapper mapper = new ObjectMapper(); 151 ObjectMapper mapper = new ObjectMapper();
136 List<OpenstackSubnet> subnets = Lists.newArrayList(); 152 List<OpenstackSubnet> subnets = Lists.newArrayList();
137 try { 153 try {
138 ObjectNode node = (ObjectNode) mapper.readTree(response); 154 ObjectNode node = (ObjectNode) mapper.readTree(response);
139 - ArrayNode subnetList = (ArrayNode) node.path("subnets"); 155 + ArrayNode subnetList = (ArrayNode) node.path(PATH_SUBNETS);
140 OpenstackSubnetCodec subnetCodec = new OpenstackSubnetCodec(); 156 OpenstackSubnetCodec subnetCodec = new OpenstackSubnetCodec();
141 subnetList.forEach(s -> subnets.add(subnetCodec.decode((ObjectNode) s, null))); 157 subnetList.forEach(s -> subnets.add(subnetCodec.decode((ObjectNode) s, null)));
142 } catch (IOException e) { 158 } catch (IOException e) {
...@@ -149,6 +165,30 @@ public class OpenstackRestHandler { ...@@ -149,6 +165,30 @@ public class OpenstackRestHandler {
149 return subnets; 165 return subnets;
150 } 166 }
151 167
168 + /**
169 + * Extracts OpenstackSecurityGroup information for the ID.
170 + *
171 + * @param id Security Group ID
172 + * @return OpenstackSecurityGroup object or null if fails
173 + */
174 + public OpenstackSecurityGroup getSecurityGroup(String id) {
175 + WebResource.Builder builder = getClientBuilder(neutronUrl + URI_SECURITY_GROUPS + "/" + id);
176 + String response = builder.accept(MediaType.APPLICATION_JSON_TYPE).
177 + header(HEADER_AUTH_TOKEN, getToken()).get(String.class);
178 +
179 + ObjectMapper mapper = new ObjectMapper();
180 + OpenstackSecurityGroup securityGroup = null;
181 + try {
182 + ObjectNode node = (ObjectNode) mapper.readTree(response);
183 + OpenstackSecurityGroupCodec sgCodec = new OpenstackSecurityGroupCodec();
184 + securityGroup = sgCodec.decode(node, null);
185 + } catch (IOException e) {
186 + log.warn("getSecurityGroup()", e);
187 + }
188 +
189 + return securityGroup;
190 + }
191 +
152 private WebResource.Builder getClientBuilder(String uri) { 192 private WebResource.Builder getClientBuilder(String uri) {
153 Client client = Client.create(); 193 Client client = Client.create();
154 WebResource resource = client.resource(uri); 194 WebResource resource = client.resource(uri);
...@@ -161,13 +201,13 @@ public class OpenstackRestHandler { ...@@ -161,13 +201,13 @@ public class OpenstackRestHandler {
161 String request = "{\"auth\": {\"tenantName\": \"admin\", " + 201 String request = "{\"auth\": {\"tenantName\": \"admin\", " +
162 "\"passwordCredentials\": {\"username\": \"" + 202 "\"passwordCredentials\": {\"username\": \"" +
163 userName + "\",\"password\": \"" + pass + "\"}}}"; 203 userName + "\",\"password\": \"" + pass + "\"}}}";
164 - WebResource.Builder builder = getClientBuilder(keystoneUrl + "tokens"); 204 + WebResource.Builder builder = getClientBuilder(keystoneUrl + URI_TOKENS);
165 String response = builder.accept(MediaType.APPLICATION_JSON).post(String.class, request); 205 String response = builder.accept(MediaType.APPLICATION_JSON).post(String.class, request);
166 206
167 ObjectMapper mapper = new ObjectMapper(); 207 ObjectMapper mapper = new ObjectMapper();
168 try { 208 try {
169 ObjectNode node = (ObjectNode) mapper.readTree(response); 209 ObjectNode node = (ObjectNode) mapper.readTree(response);
170 - tokenId = node.path("access").path("token").path("id").asText(); 210 + tokenId = node.path(PATH_ACCESS).path(PATH_TOKEN).path(PATH_ID).asText();
171 } catch (IOException e) { 211 } catch (IOException e) {
172 log.warn("getToken()", e); 212 log.warn("getToken()", e);
173 } 213 }
......
1 +/*
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 + */
16 +package org.onosproject.openstackswitching.impl;
17 +
18 +import java.util.Collection;
19 +import java.util.Collections;
20 +import java.util.Objects;
21 +
22 +/**
23 + * Represents Openstack Security Group information.
24 + */
25 +public final class OpenstackSecurityGroup {
26 +
27 + private String description;
28 + private String id;
29 + private String name;
30 + private Collection<OpenstackSecurityGroupRule> rules;
31 + private String tenantId;
32 +
33 + private OpenstackSecurityGroup(String description, String id, String name,
34 + Collection<OpenstackSecurityGroupRule> rules,
35 + String tenantId) {
36 + this.description = description;
37 + this.id = id;
38 + this.name = name;
39 + this.tenantId = tenantId;
40 + this.rules = rules;
41 + }
42 +
43 + /**
44 + * Returns the description of the security group.
45 + *
46 + * @return description
47 + */
48 + public String description() {
49 + return this.description;
50 + }
51 +
52 + /**
53 + * Returns ID of the security group.
54 + *
55 + * @return ID
56 + */
57 + public String id() {
58 + return this.id;
59 + }
60 +
61 + /**
62 + * Returns the name of the security group.
63 + *
64 + * @return name
65 + */
66 + public String name() {
67 + return this.name;
68 + }
69 +
70 + /**
71 + * Returns the list of the security group rules.
72 + *
73 + * @return Collection of OpenstackSecurityGroupRule objects
74 + */
75 + public Collection<OpenstackSecurityGroupRule> rules() {
76 + return Collections.unmodifiableCollection(rules);
77 + }
78 +
79 + /**
80 + * Returns the Tenant ID.
81 + *
82 + * @return tenant ID
83 + */
84 + public String tenantId() {
85 + return this.tenantId;
86 + }
87 +
88 + @Override
89 + public String toString() {
90 + StringBuilder sbuilder = new StringBuilder("Security Group :")
91 + .append(description + ",")
92 + .append(id + ",")
93 + .append(name + ",");
94 + rules.forEach(rule -> sbuilder.append(rule.toString()));
95 + sbuilder.append(tenantId);
96 +
97 + return sbuilder.toString();
98 + }
99 +
100 + @Override
101 + public boolean equals(Object o) {
102 + if (this == o) {
103 + return true;
104 + }
105 + if (o instanceof OpenstackSecurityGroup) {
106 + OpenstackSecurityGroup that = (OpenstackSecurityGroup) o;
107 +
108 + return this.description.equals(that.description) &&
109 + this.tenantId.equals(that.tenantId) &&
110 + this.id.equals(that.id) &&
111 + this.name.equals(that.name) &&
112 + this.rules.containsAll(that.rules);
113 + }
114 +
115 + return true;
116 + }
117 +
118 + @Override
119 + public int hashCode() {
120 + return Objects.hash(description, tenantId, id, name, rules);
121 + }
122 +
123 + /**
124 + * Returns the SecurityGroupRule builder object.
125 + *
126 + * @return builder object
127 + */
128 + public static Builder builder() {
129 + return new Builder();
130 + }
131 +
132 + /**
133 + * Represents the builder of the SecurityGroupRule.
134 + *
135 + */
136 + public static final class Builder {
137 + private String description;
138 + private String id;
139 + private String name;
140 + private Collection<OpenstackSecurityGroupRule> rules;
141 + private String tenantId;
142 +
143 + /**
144 + * Sets the description of the security group.
145 + *
146 + * @param description description
147 + * @return builder object
148 + */
149 + public Builder description(String description) {
150 + this.description = description;
151 + return this;
152 + }
153 +
154 + /**
155 + * Sets the ID of the security group.
156 + *
157 + * @param id ID
158 + * @return builder object
159 + */
160 + public Builder id(String id) {
161 + this.id = id;
162 + return this;
163 + }
164 +
165 + /**
166 + * Sets the name of the security group.
167 + *
168 + * @param name name
169 + * @return builder object
170 + */
171 + public Builder name(String name) {
172 + this.name = name;
173 + return this;
174 + }
175 +
176 + /**
177 + * Sets Security Group rules.
178 + *
179 + * @param rules security group rules
180 + * @return builder object
181 + */
182 + public Builder rules(Collection<OpenstackSecurityGroupRule> rules) {
183 + this.rules = rules;
184 + return this;
185 + }
186 +
187 + /**
188 + * Sets the tenant ID of the security group.
189 + *
190 + * @param tenantId tenant ID
191 + * @return builder object
192 + */
193 + public Builder tenantId(String tenantId) {
194 + this.tenantId = tenantId;
195 + return this;
196 + }
197 +
198 + /**
199 + * Creates the OpenstackSecurityGroup object.
200 + *
201 + * @return OpenstackSecurityGroup object
202 + */
203 + public OpenstackSecurityGroup build() {
204 + return new OpenstackSecurityGroup(description, id, name, rules, tenantId);
205 + }
206 + }
207 +}
1 +/*
2 + * Copyright 2015 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 + */
16 +package org.onosproject.openstackswitching.impl;
17 +
18 +/**
19 + * Represents Openstack Security Group Rules.
20 + */
21 +public final class OpenstackSecurityGroupRule {
22 +
23 + private String direction;
24 + private String ethertype;
25 + private String id;
26 + private String portRangeMax;
27 + private String portRangeMin;
28 + private String protocol;
29 + private String remoteGroupId;
30 + private String remoteIpPrefix;
31 + private String secuityGroupId;
32 + private String tenantId;
33 +
34 + private OpenstackSecurityGroupRule(String direction,
35 + String ethertype,
36 + String id,
37 + String portRangeMax,
38 + String portRangeMin,
39 + String protocol,
40 + String remoteGroupId,
41 + String remoteIpPrefix,
42 + String securityGroupId,
43 + String tenantId) {
44 + this.direction = direction;
45 + this.ethertype = ethertype;
46 + this.id = id;
47 + this.portRangeMax = portRangeMax;
48 + this.portRangeMin = portRangeMin;
49 + this.protocol = protocol;
50 + this.remoteGroupId = remoteGroupId;
51 + this.remoteIpPrefix = remoteIpPrefix;
52 + this.secuityGroupId = securityGroupId;
53 + this.tenantId = tenantId;
54 + }
55 +
56 + /**
57 + * Returns the builder object for the OpenstackSecurityGroupRule.
58 + *
59 + * @return OpenstackSecurityGroupRule builder object
60 + */
61 + public static OpenstackSecurityGroupRule.Builder builder() {
62 + return new Builder();
63 + }
64 +
65 + @Override
66 + public String toString() {
67 + return new StringBuilder(" [")
68 + .append(direction + ",")
69 + .append(ethertype + ",")
70 + .append(id + ",")
71 + .append(portRangeMax + ",")
72 + .append(portRangeMin + ",")
73 + .append(protocol + ",'")
74 + .append(remoteGroupId + ",")
75 + .append(remoteIpPrefix + ",")
76 + .append(secuityGroupId + ",")
77 + .append(tenantId + "] ")
78 + .toString();
79 + }
80 +
81 + /**
82 + * Represents a security group rule builder object.
83 + */
84 + public static final class Builder {
85 +
86 + private String direction;
87 + private String etherType;
88 + private String id;
89 + private String portRangeMax;
90 + private String portRangeMin;
91 + private String protocol;
92 + private String remoteGroupId;
93 + private String remoteIpPrefix;
94 + private String secuityGroupId;
95 + private String tenantId;
96 +
97 +
98 + /**
99 + * Sets the direction of the security group rule.
100 + *
101 + * @param direction direction (ingress or egress)
102 + * @return builder object
103 + */
104 + public Builder direction(String direction) {
105 + this.direction = direction;
106 + return this;
107 + }
108 +
109 + /**
110 + * Sets the Ethernet Type.
111 + *
112 + * @param etherType Ethernet Type
113 + * @return builder object
114 + */
115 + public Builder etherType(String etherType) {
116 + this.etherType = etherType;
117 + return this;
118 + }
119 +
120 + /**
121 + * Sets the Security Group Rule ID.
122 + *
123 + * @param id security group rule ID
124 + * @return builder object
125 + */
126 + public Builder id(String id) {
127 + this.id = id;
128 + return this;
129 + }
130 +
131 + /**
132 + * Sets the port range max value.
133 + *
134 + * @param portRangeMax port range max value
135 + * @return builder object
136 + */
137 + public Builder portRangeMax(String portRangeMax) {
138 + this.portRangeMax = portRangeMax;
139 + return this;
140 + }
141 +
142 + /**
143 + * Sets the port range min value.
144 + *
145 + * @param portRangeMin port range min value
146 + * @return builder object
147 + */
148 + public Builder portRangeMin(String portRangeMin) {
149 + this.portRangeMin = portRangeMin;
150 + return this;
151 + }
152 +
153 + /**
154 + * Sets the protocol.
155 + *
156 + * @param protocol protocol
157 + * @return builder object
158 + */
159 + public Builder protocol(String protocol) {
160 + this.protocol = protocol;
161 + return this;
162 + }
163 +
164 + /**
165 + * Sets the remote security group ID.
166 + *
167 + * @param remoteGroupId remote security group ID
168 + * @return builder
169 + */
170 + public Builder remoteGroupId(String remoteGroupId) {
171 + this.remoteGroupId = remoteGroupId;
172 + return this;
173 + }
174 +
175 + /**
176 + * Sets the remote IP address as prefix.
177 + *
178 + * @param remoteIpPrefix remote IP address
179 + * @return builder object
180 + */
181 + public Builder remoteIpPrefix(String remoteIpPrefix) {
182 + this.remoteIpPrefix = remoteIpPrefix;
183 + return this;
184 + }
185 +
186 + /**
187 + * Sets the Security Group ID.
188 + *
189 + * @param securityGroupId security group ID
190 + * @return builder object
191 + */
192 + public Builder securityGroupId(String securityGroupId) {
193 + this.secuityGroupId = securityGroupId;
194 + return this;
195 + }
196 +
197 + /**
198 + * Sets the tenant ID.
199 + *
200 + * @param tenantId tenant ID
201 + * @return builder object
202 + */
203 + public Builder tenantId(String tenantId) {
204 + this.tenantId = tenantId;
205 + return this;
206 + }
207 +
208 + /**
209 + * Creates a OpenstackSecurityGroupRule instance.
210 + *
211 + * @return OpenstackSecurityGroupRule object
212 + */
213 + public OpenstackSecurityGroupRule build() {
214 + return new OpenstackSecurityGroupRule(direction, etherType, id, portRangeMax,
215 + portRangeMin, protocol, remoteGroupId, remoteIpPrefix, secuityGroupId, tenantId);
216 + }
217 + }
218 +}
...@@ -176,6 +176,13 @@ public class OpenstackSwitchingManager implements OpenstackSwitchingService { ...@@ -176,6 +176,13 @@ public class OpenstackSwitchingManager implements OpenstackSwitchingService {
176 && !openstackPort.deviceOwner().equals(DEVICE_OWNER_GATEWAY)) { 176 && !openstackPort.deviceOwner().equals(DEVICE_OWNER_GATEWAY)) {
177 registerDhcpInfo(openstackPort); 177 registerDhcpInfo(openstackPort);
178 } 178 }
179 +
180 + if (!openstackPort.securityGroups().isEmpty()) {
181 + openstackPort.securityGroups().forEach(sgId -> {
182 + OpenstackSecurityGroup sg = restHandler.getSecurityGroup(sgId);
183 + log.debug("SecurityGroup : {}", sg.toString());
184 + });
185 + }
179 } 186 }
180 187
181 @Override 188 @Override
......
...@@ -18,6 +18,7 @@ package org.onosproject.openstackswitching.web; ...@@ -18,6 +18,7 @@ package org.onosproject.openstackswitching.web;
18 import com.fasterxml.jackson.databind.JsonNode; 18 import com.fasterxml.jackson.databind.JsonNode;
19 import com.fasterxml.jackson.databind.node.ArrayNode; 19 import com.fasterxml.jackson.databind.node.ArrayNode;
20 import com.fasterxml.jackson.databind.node.ObjectNode; 20 import com.fasterxml.jackson.databind.node.ObjectNode;
21 +import com.google.common.collect.Lists;
21 import org.onlab.packet.Ip4Address; 22 import org.onlab.packet.Ip4Address;
22 import org.onlab.packet.MacAddress; 23 import org.onlab.packet.MacAddress;
23 import org.onosproject.codec.CodecContext; 24 import org.onosproject.codec.CodecContext;
...@@ -26,6 +27,7 @@ import org.onosproject.openstackswitching.OpenstackPort; ...@@ -26,6 +27,7 @@ import org.onosproject.openstackswitching.OpenstackPort;
26 import org.slf4j.Logger; 27 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory; 28 import org.slf4j.LoggerFactory;
28 29
30 +import java.util.Collection;
29 import java.util.HashMap; 31 import java.util.HashMap;
30 32
31 /** 33 /**
...@@ -79,7 +81,9 @@ public class OpenstackPortCodec extends JsonCodec<OpenstackPort> { ...@@ -79,7 +81,9 @@ public class OpenstackPortCodec extends JsonCodec<OpenstackPort> {
79 } 81 }
80 } 82 }
81 String id = portInfo.path(ID).asText(); 83 String id = portInfo.path(ID).asText();
82 - String securityGroups = portInfo.path(SECURITY_GROUPS).asText(); 84 + ArrayNode securityGroupList = (ArrayNode) portInfo.path(SECURITY_GROUPS);
85 + Collection<String> securityGroupIdList = Lists.newArrayList();
86 + securityGroupList.forEach(securityGroup -> securityGroupIdList.add(securityGroup.asText()));
83 String deviceId = portInfo.path(DEVICE_ID).asText(); 87 String deviceId = portInfo.path(DEVICE_ID).asText();
84 88
85 OpenstackPort.Builder openstackPortBuilder = OpenstackPort.builder(); 89 OpenstackPort.Builder openstackPortBuilder = OpenstackPort.builder();
...@@ -96,12 +100,9 @@ public class OpenstackPortCodec extends JsonCodec<OpenstackPort> { ...@@ -96,12 +100,9 @@ public class OpenstackPortCodec extends JsonCodec<OpenstackPort> {
96 .macAddress(MacAddress.valueOf(macStr)) 100 .macAddress(MacAddress.valueOf(macStr))
97 .fixedIps(fixedIpMap) 101 .fixedIps(fixedIpMap)
98 .id(id) 102 .id(id)
99 - .deviceId(deviceId); 103 + .deviceId(deviceId)
104 + .securityGroup(securityGroupIdList);
100 105
101 - // FIX ME
102 - if (!securityGroups.isEmpty()) {
103 - openstackPortBuilder.securityGroup(securityGroups);
104 - }
105 106
106 OpenstackPort openstackPort = openstackPortBuilder.build(); 107 OpenstackPort openstackPort = openstackPortBuilder.build();
107 108
......
1 +/*
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 + */
16 +package org.onosproject.openstackswitching.web;
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;
24 +import org.onosproject.openstackswitching.impl.OpenstackSecurityGroup;
25 +import org.onosproject.openstackswitching.impl.OpenstackSecurityGroupRule;
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 +
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 +}