Merge "Added controller is-connected code"
[netvirt.git] / openstack / net-virt / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / PortSecurityHandler.java
1 /*
2  * Copyright (C) 2013 Red Hat, Inc.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Authors : Brent Salisbury, Madhu Venugopal
9  */
10
11 package org.opendaylight.ovsdb.openstack.netvirt;
12
13 import org.opendaylight.neutron.spi.INeutronSecurityGroupAware;
14 import org.opendaylight.neutron.spi.INeutronSecurityRuleAware;
15 import org.opendaylight.neutron.spi.NeutronSecurityGroup;
16 import org.opendaylight.neutron.spi.NeutronSecurityRule;
17
18 import org.opendaylight.ovsdb.openstack.netvirt.api.EventDispatcher;
19 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
20 import org.osgi.framework.BundleContext;
21 import org.osgi.framework.ServiceReference;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 import java.net.HttpURLConnection;
26
27 /**
28  * Handle requests for OpenStack Neutron v2.0 Port Security API calls.
29  */
30 public class PortSecurityHandler extends AbstractHandler
31         implements INeutronSecurityGroupAware, INeutronSecurityRuleAware, ConfigInterface {
32
33     static final Logger logger = LoggerFactory.getLogger(PortSecurityHandler.class);
34     private volatile EventDispatcher eventDispatcher;
35
36     @Override
37     public int canCreateNeutronSecurityGroup(NeutronSecurityGroup neutronSecurityGroup) {
38         return HttpURLConnection.HTTP_CREATED;
39     }
40
41     @Override
42     public void neutronSecurityGroupCreated(NeutronSecurityGroup neutronSecurityGroup) {
43         int result = HttpURLConnection.HTTP_BAD_REQUEST;
44
45         result = canCreateNeutronSecurityGroup(neutronSecurityGroup);
46         if (result != HttpURLConnection.HTTP_CREATED) {
47             logger.debug("Neutron Security Group creation failed {} ", result);
48             return;
49         }
50     }
51
52     @Override
53     public int canUpdateNeutronSecurityGroup(NeutronSecurityGroup delta, NeutronSecurityGroup original) {
54         return HttpURLConnection.HTTP_OK;
55     }
56
57     @Override
58     public void neutronSecurityGroupUpdated(NeutronSecurityGroup neutronSecurityGroup) {
59         return;
60     }
61
62     @Override
63     public int canDeleteNeutronSecurityGroup(NeutronSecurityGroup neutronSecurityGroup) {
64         return HttpURLConnection.HTTP_OK;
65     }
66
67     @Override
68     public void neutronSecurityGroupDeleted(NeutronSecurityGroup neutronSecurityGroup) {
69         //TODO: Trigger flowmod removals
70         int result = canDeleteNeutronSecurityGroup(neutronSecurityGroup);
71         if  (result != HttpURLConnection.HTTP_OK) {
72             logger.error(" delete Neutron Security Rule validation failed for result - {} ", result);
73             return;
74         }
75     }
76
77     /**
78      * Invoked when a Security Rules creation is requested
79      * to indicate if the specified Rule can be created.
80      *
81      * @param neutronSecurityRule  An instance of proposed new Neutron Security Rule object.
82      * @return A HTTP status code to the creation request.
83      */
84
85     @Override
86     public int canCreateNeutronSecurityRule(NeutronSecurityRule neutronSecurityRule) {
87         return HttpURLConnection.HTTP_CREATED;
88     }
89
90     @Override
91     public void neutronSecurityRuleCreated(NeutronSecurityRule neutronSecurityRule) {
92         int result = HttpURLConnection.HTTP_BAD_REQUEST;
93
94         result = canCreateNeutronSecurityRule(neutronSecurityRule);
95         if (result != HttpURLConnection.HTTP_CREATED) {
96             logger.debug("Neutron Security Group creation failed {} ", result);
97             return;
98         }
99     }
100
101     @Override
102     public int canUpdateNeutronSecurityRule(NeutronSecurityRule delta, NeutronSecurityRule original) {
103         return HttpURLConnection.HTTP_OK;
104     }
105
106     @Override
107     public void neutronSecurityRuleUpdated(NeutronSecurityRule neutronSecurityRule) {
108         return;
109     }
110
111     @Override
112     public int canDeleteNeutronSecurityRule(NeutronSecurityRule neutronSecurityRule) {
113         return HttpURLConnection.HTTP_OK;
114     }
115
116     @Override
117     public void neutronSecurityRuleDeleted(NeutronSecurityRule neutronSecurityRule) {
118         int result = canDeleteNeutronSecurityRule(neutronSecurityRule);
119         if  (result != HttpURLConnection.HTTP_OK) {
120             logger.error(" delete Neutron Security Rule validation failed for result - {} ", result);
121             return;
122         }
123     }
124
125     /**
126      * Process the event.
127      *
128      * @param abstractEvent the {@link org.opendaylight.ovsdb.openstack.netvirt.AbstractEvent} event to be handled.
129      * @see org.opendaylight.ovsdb.openstack.netvirt.api.EventDispatcher
130      */
131     @Override
132     public void processEvent(AbstractEvent abstractEvent) {
133         if (!(abstractEvent instanceof NorthboundEvent)) {
134             logger.error("Unable to process abstract event " + abstractEvent);
135             return;
136         }
137         NorthboundEvent ev = (NorthboundEvent) abstractEvent;
138         switch (ev.getAction()) {
139             // TODO: add handling of events here, once callbacks do something
140             //       other than logging.
141             default:
142                 logger.warn("Unable to process event action " + ev.getAction());
143                 break;
144         }
145     }
146
147     @Override
148     public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
149         eventDispatcher =
150                 (EventDispatcher) ServiceHelper.getGlobalInstance(EventDispatcher.class, this);
151         eventDispatcher.eventHandlerAdded(
152                 bundleContext.getServiceReference(INeutronSecurityGroupAware.class.getName()), this);
153         super.setDispatcher(eventDispatcher);
154     }
155
156     @Override
157     public void setDependencies(Object impl) {}
158 }