Add UT for SouthboundMapper and SouthboundProvider
[netvirt.git] / openstack / net-virt / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / PortSecurityHandler.java
1 /*
2  * Copyright (c) 2013, 2015 Red Hat, Inc. and others. All rights reserved.
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
9 package org.opendaylight.ovsdb.openstack.netvirt;
10
11 import java.net.HttpURLConnection;
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 import org.opendaylight.ovsdb.openstack.netvirt.api.EventDispatcher;
18 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
19 import org.osgi.framework.BundleContext;
20 import org.osgi.framework.ServiceReference;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * Handle requests for OpenStack Neutron v2.0 Port Security API calls.
26  */
27 public class PortSecurityHandler extends AbstractHandler
28         implements INeutronSecurityGroupAware, INeutronSecurityRuleAware, ConfigInterface {
29
30     private static final Logger LOG = LoggerFactory.getLogger(PortSecurityHandler.class);
31
32     @Override
33     public int canCreateNeutronSecurityGroup(NeutronSecurityGroup neutronSecurityGroup) {
34         return HttpURLConnection.HTTP_CREATED;
35     }
36
37     @Override
38     public void neutronSecurityGroupCreated(NeutronSecurityGroup neutronSecurityGroup) {
39         int result = canCreateNeutronSecurityGroup(neutronSecurityGroup);
40         if (result != HttpURLConnection.HTTP_CREATED) {
41             LOG.debug("Neutron Security Group creation failed {} ", result);
42         }
43     }
44
45     @Override
46     public int canUpdateNeutronSecurityGroup(NeutronSecurityGroup delta, NeutronSecurityGroup original) {
47         return HttpURLConnection.HTTP_OK;
48     }
49
50     @Override
51     public void neutronSecurityGroupUpdated(NeutronSecurityGroup neutronSecurityGroup) {
52         // Nothing to do
53     }
54
55     @Override
56     public int canDeleteNeutronSecurityGroup(NeutronSecurityGroup neutronSecurityGroup) {
57         return HttpURLConnection.HTTP_OK;
58     }
59
60     @Override
61     public void neutronSecurityGroupDeleted(NeutronSecurityGroup neutronSecurityGroup) {
62         //TODO: Trigger flowmod removals
63         int result = canDeleteNeutronSecurityGroup(neutronSecurityGroup);
64         if  (result != HttpURLConnection.HTTP_OK) {
65             LOG.error(" delete Neutron Security Rule validation failed for result - {} ", result);
66         }
67     }
68
69     /**
70      * Invoked when a Security Rules creation is requested
71      * to indicate if the specified Rule can be created.
72      *
73      * @param neutronSecurityRule  An instance of proposed new Neutron Security Rule object.
74      * @return A HTTP status code to the creation request.
75      */
76
77     @Override
78     public int canCreateNeutronSecurityRule(NeutronSecurityRule neutronSecurityRule) {
79         return HttpURLConnection.HTTP_CREATED;
80     }
81
82     @Override
83     public void neutronSecurityRuleCreated(NeutronSecurityRule neutronSecurityRule) {
84         int result = canCreateNeutronSecurityRule(neutronSecurityRule);
85         if (result != HttpURLConnection.HTTP_CREATED) {
86             LOG.debug("Neutron Security Group creation failed {} ", result);
87         }
88     }
89
90     @Override
91     public int canUpdateNeutronSecurityRule(NeutronSecurityRule delta, NeutronSecurityRule original) {
92         return HttpURLConnection.HTTP_OK;
93     }
94
95     @Override
96     public void neutronSecurityRuleUpdated(NeutronSecurityRule neutronSecurityRule) {
97         // Nothing to do
98     }
99
100     @Override
101     public int canDeleteNeutronSecurityRule(NeutronSecurityRule neutronSecurityRule) {
102         return HttpURLConnection.HTTP_OK;
103     }
104
105     @Override
106     public void neutronSecurityRuleDeleted(NeutronSecurityRule neutronSecurityRule) {
107         int result = canDeleteNeutronSecurityRule(neutronSecurityRule);
108         if  (result != HttpURLConnection.HTTP_OK) {
109             LOG.error(" delete Neutron Security Rule validation failed for result - {} ", result);
110         }
111     }
112
113     /**
114      * Process the event.
115      *
116      * @param abstractEvent the {@link org.opendaylight.ovsdb.openstack.netvirt.AbstractEvent} event to be handled.
117      * @see org.opendaylight.ovsdb.openstack.netvirt.api.EventDispatcher
118      */
119     @Override
120     public void processEvent(AbstractEvent abstractEvent) {
121         if (!(abstractEvent instanceof NorthboundEvent)) {
122             LOG.error("Unable to process abstract event {}", abstractEvent);
123             return;
124         }
125         NorthboundEvent ev = (NorthboundEvent) abstractEvent;
126         switch (ev.getAction()) {
127             // TODO: add handling of events here, once callbacks do something
128             //       other than logging.
129             default:
130                 LOG.warn("Unable to process event action {}", ev.getAction());
131                 break;
132         }
133     }
134
135     @Override
136     public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
137         eventDispatcher =
138                 (EventDispatcher) ServiceHelper.getGlobalInstance(EventDispatcher.class, this);
139         eventDispatcher.eventHandlerAdded(
140                 bundleContext.getServiceReference(INeutronSecurityGroupAware.class.getName()), this);
141     }
142
143     @Override
144     public void setDependencies(Object impl) {}
145 }