sangho
Committed by Gerrit Code Review

Follow up for OpenstackSwitching Refactoring: Updates flow rules for security group updated.

- When security groups are updated, updates the flow rules for the security groups via the SecurityGroupService.

Change-Id: I13eacd3a21f804221bdd29105a304a9a1e10f2a8
1 +/*
2 + * Copyright 2016-present 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.openstacknetworking;
17 +
18 +import org.onosproject.openstackinterface.OpenstackPort;
19 +
20 +/**
21 + * Represents OpenstackSecurityGroupService Interface.
22 + */
23 +public interface OpenstackSecurityGroupService {
24 +
25 + /**
26 + * Updates the flow rules for Security Group for the VM (OpenstackPort).
27 + *
28 + * @param osPort OpenstackPort information for the VM
29 + */
30 + void updateSecurityGroup(OpenstackPort osPort);
31 +}
...@@ -23,6 +23,7 @@ import org.apache.felix.scr.annotations.Component; ...@@ -23,6 +23,7 @@ import org.apache.felix.scr.annotations.Component;
23 import org.apache.felix.scr.annotations.Deactivate; 23 import org.apache.felix.scr.annotations.Deactivate;
24 import org.apache.felix.scr.annotations.Reference; 24 import org.apache.felix.scr.annotations.Reference;
25 import org.apache.felix.scr.annotations.ReferenceCardinality; 25 import org.apache.felix.scr.annotations.ReferenceCardinality;
26 +import org.apache.felix.scr.annotations.Service;
26 import org.onlab.packet.Ethernet; 27 import org.onlab.packet.Ethernet;
27 import org.onlab.packet.IPv4; 28 import org.onlab.packet.IPv4;
28 import org.onlab.packet.Ip4Address; 29 import org.onlab.packet.Ip4Address;
...@@ -41,6 +42,7 @@ import org.onosproject.openstackinterface.OpenstackInterfaceService; ...@@ -41,6 +42,7 @@ import org.onosproject.openstackinterface.OpenstackInterfaceService;
41 import org.onosproject.openstackinterface.OpenstackPort; 42 import org.onosproject.openstackinterface.OpenstackPort;
42 import org.onosproject.openstackinterface.OpenstackSecurityGroup; 43 import org.onosproject.openstackinterface.OpenstackSecurityGroup;
43 import org.onosproject.openstackinterface.OpenstackSecurityGroupRule; 44 import org.onosproject.openstackinterface.OpenstackSecurityGroupRule;
45 +import org.onosproject.openstacknetworking.OpenstackSecurityGroupService;
44 import org.slf4j.Logger; 46 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory; 47 import org.slf4j.LoggerFactory;
46 48
...@@ -57,7 +59,9 @@ import static org.onosproject.openstacknetworking.Constants.*; ...@@ -57,7 +59,9 @@ import static org.onosproject.openstacknetworking.Constants.*;
57 * 59 *
58 */ 60 */
59 @Component(immediate = true) 61 @Component(immediate = true)
60 -public class OpenstackSecurityGroupRulePopulator extends AbstractVmHandler { 62 +@Service
63 +public class OpenstackSecurityGroupRulePopulator extends AbstractVmHandler
64 + implements OpenstackSecurityGroupService {
61 65
62 private final Logger log = LoggerFactory.getLogger(getClass()); 66 private final Logger log = LoggerFactory.getLogger(getClass());
63 67
...@@ -84,7 +88,7 @@ public class OpenstackSecurityGroupRulePopulator extends AbstractVmHandler { ...@@ -84,7 +88,7 @@ public class OpenstackSecurityGroupRulePopulator extends AbstractVmHandler {
84 super.deactivate(); 88 super.deactivate();
85 } 89 }
86 90
87 - // TODO call this when port is updated from OpenStack 91 + @Override
88 public void updateSecurityGroup(OpenstackPort osPort) { 92 public void updateSecurityGroup(OpenstackPort osPort) {
89 if (!osPort.status().equals(OpenstackPort.PortStatus.ACTIVE)) { 93 if (!osPort.status().equals(OpenstackPort.PortStatus.ACTIVE)) {
90 return; 94 return;
......
...@@ -15,7 +15,14 @@ ...@@ -15,7 +15,14 @@
15 */ 15 */
16 package org.onosproject.openstacknetworking.web; 16 package org.onosproject.openstacknetworking.web;
17 17
18 +import com.fasterxml.jackson.databind.ObjectMapper;
19 +import com.fasterxml.jackson.databind.node.ObjectNode;
20 +import org.onosproject.openstackinterface.OpenstackPort;
21 +import org.onosproject.openstackinterface.web.OpenstackPortCodec;
22 +import org.onosproject.openstacknetworking.OpenstackSecurityGroupService;
18 import org.onosproject.rest.AbstractWebResource; 23 import org.onosproject.rest.AbstractWebResource;
24 +import org.slf4j.Logger;
25 +import org.slf4j.LoggerFactory;
19 26
20 import javax.ws.rs.Consumes; 27 import javax.ws.rs.Consumes;
21 import javax.ws.rs.DELETE; 28 import javax.ws.rs.DELETE;
...@@ -26,13 +33,20 @@ import javax.ws.rs.PathParam; ...@@ -26,13 +33,20 @@ import javax.ws.rs.PathParam;
26 import javax.ws.rs.Produces; 33 import javax.ws.rs.Produces;
27 import javax.ws.rs.core.MediaType; 34 import javax.ws.rs.core.MediaType;
28 import javax.ws.rs.core.Response; 35 import javax.ws.rs.core.Response;
36 +import java.io.IOException;
29 import java.io.InputStream; 37 import java.io.InputStream;
30 38
39 +import static com.google.common.base.Preconditions.checkNotNull;
40 +
31 /** 41 /**
32 * Handles Rest API call from Neutron ML2 plugin. 42 * Handles Rest API call from Neutron ML2 plugin.
33 */ 43 */
34 @Path("ports") 44 @Path("ports")
35 public class OpenstackPortWebResource extends AbstractWebResource { 45 public class OpenstackPortWebResource extends AbstractWebResource {
46 + private final Logger log = LoggerFactory.getLogger(getClass());
47 +
48 + private static final OpenstackPortCodec PORT_CODEC
49 + = new OpenstackPortCodec();
36 50
37 @POST 51 @POST
38 @Consumes(MediaType.APPLICATION_JSON) 52 @Consumes(MediaType.APPLICATION_JSON)
...@@ -51,8 +65,26 @@ public class OpenstackPortWebResource extends AbstractWebResource { ...@@ -51,8 +65,26 @@ public class OpenstackPortWebResource extends AbstractWebResource {
51 @Path("{id}") 65 @Path("{id}")
52 @Consumes(MediaType.APPLICATION_JSON) 66 @Consumes(MediaType.APPLICATION_JSON)
53 @Produces(MediaType.APPLICATION_JSON) 67 @Produces(MediaType.APPLICATION_JSON)
54 - public Response updatePorts(InputStream input) { 68 + public Response updatePorts(@PathParam("id") String id, InputStream input) {
55 - // TODO call security group update here 69 + checkNotNull(input);
56 - return Response.status(Response.Status.OK).build(); 70 + checkNotNull(id);
71 +
72 + try {
73 + ObjectMapper mapper = new ObjectMapper();
74 + ObjectNode portNode = (ObjectNode) mapper.readTree(input);
75 + OpenstackPort osPort = PORT_CODEC.decode(portNode, this);
76 +
77 + OpenstackSecurityGroupService sgService
78 + = getService(OpenstackSecurityGroupService.class);
79 + sgService.updateSecurityGroup(osPort);
80 +
81 + return Response.status(Response.Status.OK).build();
82 +
83 + } catch (IOException e) {
84 + log.error("UpdatePort post process failed due to {}", e.getMessage());
85 +
86 + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage())
87 + .build();
88 + }
57 } 89 }
58 } 90 }
......