2 * Copyright (c) 2015 Brocade Communications Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.netvirt.openstack.netvirt.translator.iaware.impl;
10 import java.util.Map.Entry;
12 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataChangeListener;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
15 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
16 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.netvirt.openstack.netvirt.translator.iaware.INeutronSecurityRuleAware;
19 import org.opendaylight.netvirt.openstack.netvirt.translator.NeutronSecurityRule;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.DirectionBase;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.DirectionEgress;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.DirectionIngress;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.EthertypeBase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.EthertypeV4;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.EthertypeV6;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.ProtocolBase;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.ProtocolIcmp;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.ProtocolIcmpV6;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.ProtocolTcp;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.ProtocolUdp;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.SecurityRules;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.security.rules.SecurityRule;
34 import org.opendaylight.yangtools.concepts.ListenerRegistration;
35 import org.opendaylight.yangtools.yang.binding.DataObject;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 import com.google.common.collect.ImmutableBiMap;
42 public class NeutronSecurityRuleDataChangeListener implements ClusteredDataChangeListener, AutoCloseable {
44 private static final Logger LOG = LoggerFactory.getLogger(NeutronSecurityRuleDataChangeListener.class);
46 private static final ImmutableBiMap<Class<? extends DirectionBase>, String> DIRECTION_MAP = ImmutableBiMap.of(
47 DirectionEgress.class, NeutronSecurityRule.DIRECTION_EGRESS,
48 DirectionIngress.class, NeutronSecurityRule.DIRECTION_INGRESS);
49 private static final ImmutableBiMap<Class<? extends ProtocolBase>, String> PROTOCOL_MAP = ImmutableBiMap.of(
50 ProtocolIcmp.class, NeutronSecurityRule.PROTOCOL_ICMP,
51 ProtocolTcp.class, NeutronSecurityRule.PROTOCOL_TCP,
52 ProtocolUdp.class, NeutronSecurityRule.PROTOCOL_UDP,
53 ProtocolIcmpV6.class, NeutronSecurityRule.PROTOCOL_ICMPV6);
54 private static final ImmutableBiMap<Class<? extends EthertypeBase>,String> ETHERTYPE_MAP = ImmutableBiMap.of(
55 EthertypeV4.class, NeutronSecurityRule.ETHERTYPE_IPV4,
56 EthertypeV6.class, NeutronSecurityRule.ETHERTYPE_IPV6);
58 private ListenerRegistration<DataChangeListener> registration;
60 public NeutronSecurityRuleDataChangeListener(DataBroker db) {
61 InstanceIdentifier<SecurityRule> path = InstanceIdentifier
62 .create(Neutron.class).child(SecurityRules.class)
63 .child(SecurityRule.class);
64 LOG.debug("Register listener for Neutron Secutiry rules model data changes");
65 registration = db.registerDataChangeListener(
66 LogicalDatastoreType.CONFIGURATION, path, this,
72 public void onDataChanged(
73 AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes) {
74 LOG.trace("Data changes : {}", changes);
76 Object[] subscribers = NeutronIAwareUtil.getInstances(
77 INeutronSecurityRuleAware.class, this);
78 createSecurityRule(changes, subscribers);
79 updateSecurityRule(changes, subscribers);
80 deleteSecurityRule(changes, subscribers);
83 private void createSecurityRule(
84 AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes,
85 Object[] subscribers) {
86 for (Entry<InstanceIdentifier<?>, DataObject> newSecutiryRule : changes
87 .getCreatedData().entrySet()) {
88 if (newSecutiryRule.getValue() instanceof SecurityRule) {
89 NeutronSecurityRule secutiryRule = fromMd((SecurityRule) newSecutiryRule
91 for (Object entry : subscribers) {
92 INeutronSecurityRuleAware subscriber = (INeutronSecurityRuleAware) entry;
93 subscriber.neutronSecurityRuleCreated(secutiryRule);
100 private void updateSecurityRule(
101 AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes,
102 Object[] subscribers) {
103 for (Entry<InstanceIdentifier<?>, DataObject> updateSecurityRule : changes
104 .getUpdatedData().entrySet()) {
105 if (updateSecurityRule.getValue() instanceof SecurityRule) {
106 NeutronSecurityRule securityRule = fromMd((SecurityRule) updateSecurityRule
108 for (Object entry : subscribers) {
109 INeutronSecurityRuleAware subscriber = (INeutronSecurityRuleAware) entry;
110 subscriber.neutronSecurityRuleUpdated(securityRule);
116 private void deleteSecurityRule(
117 AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes,
118 Object[] subscribers) {
119 for (InstanceIdentifier<?> deletedSecurityRule : changes
120 .getRemovedPaths()) {
121 if (deletedSecurityRule.getTargetType().equals(SecurityRule.class)) {
122 NeutronSecurityRule securityRule = fromMd((SecurityRule) changes
123 .getOriginalData().get(deletedSecurityRule));
124 for (Object entry : subscribers) {
125 INeutronSecurityRuleAware subscriber = (INeutronSecurityRuleAware) entry;
126 subscriber.neutronSecurityRuleDeleted(securityRule);
132 private NeutronSecurityRule fromMd(SecurityRule rule) {
133 NeutronSecurityRule answer = new NeutronSecurityRule();
134 if (rule.getTenantId() != null) {
135 answer.setSecurityRuleTenantID(rule.getTenantId().getValue()
138 if (rule.getDirection() != null) {
139 answer.setSecurityRuleDirection(DIRECTION_MAP.get(rule
142 if (rule.getSecurityGroupId() != null) {
143 answer.setSecurityRuleGroupID(rule.getSecurityGroupId().getValue());
145 if (rule.getRemoteGroupId() != null) {
146 answer.setSecurityRemoteGroupID(rule.getRemoteGroupId().getValue());
148 if (rule.getRemoteIpPrefix() != null) {
149 answer.setSecurityRuleRemoteIpPrefix(rule.getRemoteIpPrefix().getIpv4Prefix()!= null?
150 rule.getRemoteIpPrefix().getIpv4Prefix().getValue():rule.getRemoteIpPrefix().getIpv6Prefix().getValue());
152 if (rule.getProtocol() != null) {
153 answer.setSecurityRuleProtocol(PROTOCOL_MAP.get(rule.getProtocol()));
155 if (rule.getEthertype() != null) {
156 answer.setSecurityRuleEthertype(ETHERTYPE_MAP.get(rule
159 if (rule.getPortRangeMin() != null) {
160 answer.setSecurityRulePortMin(rule.getPortRangeMin());
162 if (rule.getPortRangeMax() != null) {
163 answer.setSecurityRulePortMax(rule.getPortRangeMax());
165 if (rule.getId() != null) {
166 answer.setID(rule.getId().getValue());
172 public void close() throws Exception {
173 registration.close();