Merge "Use MD-SAL events to react to OVSDB changes"
[ovsdb.git] / openstack / net-virt / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / translator / iaware / impl / NeutronSecurityRuleDataChangeListener.java
1 /*
2  * Copyright (c) 2015 Brocade Communications Systems, 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 package org.opendaylight.ovsdb.openstack.netvirt.translator.iaware.impl;
9
10 import java.util.Map.Entry;
11
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
14 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
15 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.ovsdb.openstack.netvirt.translator.NeutronSecurityRule;
18 import org.opendaylight.ovsdb.openstack.netvirt.translator.iaware.INeutronSecurityRuleAware;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.DirectionBase;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.DirectionEgress;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.DirectionIngress;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.EthertypeBase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.EthertypeV4;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.EthertypeV6;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.ProtocolBase;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.ProtocolHttp;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.ProtocolHttps;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.ProtocolIcmp;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.ProtocolTcp;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.SecurityRules;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.security.rules.SecurityRule;
33 import org.opendaylight.yangtools.concepts.ListenerRegistration;
34 import org.opendaylight.yangtools.yang.binding.DataObject;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 import com.google.common.collect.ImmutableBiMap;
40
41 public class NeutronSecurityRuleDataChangeListener implements DataChangeListener, AutoCloseable {
42
43     private static final Logger LOG = LoggerFactory.getLogger(NeutronSecurityRuleDataChangeListener.class);
44
45     private static final ImmutableBiMap<Class<? extends DirectionBase>, String> DIRECTION_MAP
46             = new ImmutableBiMap.Builder<Class<? extends DirectionBase>, String>()
47             .put(DirectionEgress.class, "egress")
48             .put(DirectionIngress.class, "ingress").build();
49     private static final ImmutableBiMap<Class<? extends ProtocolBase>, String> PROTOCOL_MAP
50             = new ImmutableBiMap.Builder<Class<? extends ProtocolBase>, String>()
51             .put(ProtocolHttp.class, "HTTP")
52             .put(ProtocolHttps.class, "HTTPS")
53             .put(ProtocolIcmp.class, "ICMP")
54             .put(ProtocolTcp.class, "TCP")
55             .build();
56     private static final ImmutableBiMap<Class<? extends EthertypeBase>, String> ETHERTYPE_MAP
57             = new ImmutableBiMap.Builder<Class<? extends EthertypeBase>, String>()
58             .put(EthertypeV4.class, "v4")
59             .put(EthertypeV6.class, "v6")
60             .build();
61
62     private ListenerRegistration<DataChangeListener> registration;
63     private DataBroker db;
64
65     public NeutronSecurityRuleDataChangeListener(DataBroker db) {
66         this.db = db;
67         InstanceIdentifier<SecurityRule> path = InstanceIdentifier
68                 .create(Neutron.class).child(SecurityRules.class)
69                 .child(SecurityRule.class);
70         LOG.debug("Register listener for Neutron Secutiry rules model data changes");
71         registration = this.db.registerDataChangeListener(
72                 LogicalDatastoreType.CONFIGURATION, path, this,
73                 DataChangeScope.ONE);
74
75     }
76
77     @Override
78     public void onDataChanged(
79             AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes) {
80         LOG.trace("Data changes : {}", changes);
81
82         Object[] subscribers = NeutronIAwareUtil.getInstances(
83                 INeutronSecurityRuleAware.class, this);
84         createSecurityRule(changes, subscribers);
85         updateSecurityRule(changes, subscribers);
86         deleteSecurityRule(changes, subscribers);
87     }
88
89     private void createSecurityRule(
90             AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes,
91             Object[] subscribers) {
92         for (Entry<InstanceIdentifier<?>, DataObject> newSecutiryRule : changes
93                 .getCreatedData().entrySet()) {
94             if (newSecutiryRule.getValue() instanceof SecurityRule) {
95                 NeutronSecurityRule secutiryRule = fromMd((SecurityRule) newSecutiryRule
96                         .getValue());
97                 for (Object entry : subscribers) {
98                     INeutronSecurityRuleAware subscriber = (INeutronSecurityRuleAware) entry;
99                     subscriber.neutronSecurityRuleCreated(secutiryRule);
100                 }
101             }
102         }
103
104     }
105
106     private void updateSecurityRule(
107             AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes,
108             Object[] subscribers) {
109         for (Entry<InstanceIdentifier<?>, DataObject> updateSecurityRule : changes
110                 .getUpdatedData().entrySet()) {
111             if (updateSecurityRule.getValue() instanceof SecurityRule) {
112                 NeutronSecurityRule securityRule = fromMd((SecurityRule) updateSecurityRule
113                         .getValue());
114                 for (Object entry : subscribers) {
115                     INeutronSecurityRuleAware subscriber = (INeutronSecurityRuleAware) entry;
116                     subscriber.neutronSecurityRuleUpdated(securityRule);
117                 }
118             }
119         }
120     }
121
122     private void deleteSecurityRule(
123             AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes,
124             Object[] subscribers) {
125         for (InstanceIdentifier<?> deletedSecurityRule : changes
126                 .getRemovedPaths()) {
127             if (deletedSecurityRule.getTargetType().equals(SecurityRule.class)) {
128                 NeutronSecurityRule securityRule = fromMd((SecurityRule) changes
129                         .getOriginalData().get(deletedSecurityRule));
130                 for (Object entry : subscribers) {
131                     INeutronSecurityRuleAware subscriber = (INeutronSecurityRuleAware) entry;
132                     subscriber.neutronSecurityRuleDeleted(securityRule);
133                 }
134             }
135         }
136     }
137
138     private NeutronSecurityRule fromMd(SecurityRule rule) {
139         NeutronSecurityRule answer = new NeutronSecurityRule();
140         if (rule.getTenantId() != null) {
141             answer.setSecurityRuleTenantID(rule.getTenantId().getValue()
142                     .replace("-", ""));
143         }
144         if (rule.getDirection() != null) {
145             answer.setSecurityRuleDirection(DIRECTION_MAP.get(rule
146                     .getDirection()));
147         }
148         if (rule.getSecurityGroupId() != null) {
149             answer.setSecurityRuleGroupID(rule.getSecurityGroupId().getValue());
150         }
151         if (rule.getRemoteGroupId() != null) {
152             answer.setSecurityRemoteGroupID(rule.getRemoteGroupId().getValue());
153         }
154         if (rule.getRemoteIpPrefix() != null) {
155             answer.setSecurityRuleRemoteIpPrefix(rule.getRemoteIpPrefix().getIpv4Prefix()!= null?
156                     rule.getRemoteIpPrefix().getIpv4Prefix().getValue():rule.getRemoteIpPrefix().getIpv6Prefix().getValue());
157         }
158         if (rule.getProtocol() != null) {
159             answer.setSecurityRuleProtocol(PROTOCOL_MAP.get(rule.getProtocol()));
160         }
161         if (rule.getEthertype() != null) {
162             answer.setSecurityRuleEthertype(ETHERTYPE_MAP.get(rule
163                     .getEthertype()));
164         }
165         if (rule.getPortRangeMin() != null) {
166             answer.setSecurityRulePortMin(Integer.valueOf(rule
167                     .getPortRangeMin()));
168         }
169         if (rule.getPortRangeMax() != null) {
170             answer.setSecurityRulePortMax(Integer.valueOf(rule
171                     .getPortRangeMax()));
172         }
173         if (rule.getId() != null) {
174             answer.setID(rule.getId().getValue());
175         }
176         return answer;
177     }
178
179     @Override
180     public void close() throws Exception {
181         registration.close();
182     }
183 }