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.ovsdb.openstack.netvirt.translator.iaware.impl;
10 import java.util.ArrayList;
11 import java.util.List;
12 import java.util.Map.Entry;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
16 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
17 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.ovsdb.openstack.netvirt.translator.NeutronSecurityGroup;
20 import org.opendaylight.ovsdb.openstack.netvirt.translator.NeutronSecurityRule;
21 import org.opendaylight.ovsdb.openstack.netvirt.translator.crud.INeutronSecurityRuleCRUD;
22 import org.opendaylight.ovsdb.openstack.netvirt.translator.crud.NeutronCRUDInterfaces;
23 import org.opendaylight.ovsdb.openstack.netvirt.translator.iaware.INeutronSecurityGroupAware;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.groups.attributes.SecurityGroups;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.groups.attributes.security.groups.SecurityGroup;
28 import org.opendaylight.yangtools.concepts.ListenerRegistration;
29 import org.opendaylight.yangtools.yang.binding.DataObject;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 public class NeutronSecurityGroupDataChangeListener implements
35 DataChangeListener, AutoCloseable {
36 private static final Logger LOG = LoggerFactory
37 .getLogger(NeutronSecurityGroupDataChangeListener.class);
39 private ListenerRegistration<DataChangeListener> registration;
40 private DataBroker db;
42 public NeutronSecurityGroupDataChangeListener(DataBroker db) {
44 InstanceIdentifier<SecurityGroup> path = InstanceIdentifier
45 .create(Neutron.class).child(SecurityGroups.class)
46 .child(SecurityGroup.class);
47 LOG.debug("Register listener for Neutron Secutiry group model data changes");
48 registration = this.db.registerDataChangeListener(
49 LogicalDatastoreType.CONFIGURATION, path, this,
55 public void onDataChanged(
56 AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes) {
57 LOG.trace("Data changes : {}", changes);
59 Object[] subscribers = NeutronIAwareUtil.getInstances(
60 INeutronSecurityGroupAware.class, this);
61 createSecurityGroup(changes, subscribers);
62 updateSecurityGroup(changes, subscribers);
63 deleteSecurityGroup(changes, subscribers);
66 private void createSecurityGroup(
67 AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes,
68 Object[] subscribers) {
69 for (Entry<InstanceIdentifier<?>, DataObject> newSecutiryGroup : changes
70 .getCreatedData().entrySet()) {
71 if (newSecutiryGroup.getValue() instanceof SecurityGroup) {
72 NeutronSecurityGroup secutiryGroup = fromMd((SecurityGroup) newSecutiryGroup
74 for (Object entry : subscribers) {
75 INeutronSecurityGroupAware subscriber = (INeutronSecurityGroupAware) entry;
76 subscriber.neutronSecurityGroupCreated(secutiryGroup);
83 private void updateSecurityGroup(
84 AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes,
85 Object[] subscribers) {
86 for (Entry<InstanceIdentifier<?>, DataObject> updateSecurityGroup : changes
87 .getUpdatedData().entrySet()) {
88 if (updateSecurityGroup.getValue() instanceof SecurityGroup) {
89 NeutronSecurityGroup securityGroup = fromMd((SecurityGroup) updateSecurityGroup
91 for (Object entry : subscribers) {
92 INeutronSecurityGroupAware subscriber = (INeutronSecurityGroupAware) entry;
93 subscriber.neutronSecurityGroupUpdated(securityGroup);
99 private void deleteSecurityGroup(
100 AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes,
101 Object[] subscribers) {
102 for (InstanceIdentifier<?> deletedSecurityGroup : changes
103 .getRemovedPaths()) {
104 if (deletedSecurityGroup.getTargetType()
105 .equals(SecurityGroup.class)) {
106 NeutronSecurityGroup securityGroup = fromMd((SecurityGroup) changes
107 .getOriginalData().get(deletedSecurityGroup));
108 for (Object entry : subscribers) {
109 INeutronSecurityGroupAware subscriber = (INeutronSecurityGroupAware) entry;
110 subscriber.neutronSecurityGroupDeleted(securityGroup);
116 private NeutronSecurityGroup fromMd(SecurityGroup group) {
117 NeutronSecurityGroup answer = new NeutronSecurityGroup();
118 if (group.getName() != null) {
119 answer.setSecurityGroupName(group.getName());
121 if (group.getDescription() != null) {
122 answer.setSecurityGroupDescription(group.getDescription());
124 if (group.getTenantId() != null) {
125 answer.setSecurityGroupTenantID(group.getTenantId().getValue()
128 if (group.getSecurityRules() != null) {
129 NeutronCRUDInterfaces interfaces = new NeutronCRUDInterfaces()
130 .fetchINeutronSecurityRuleCRUD(this);
131 INeutronSecurityRuleCRUD srCrud = interfaces
132 .getSecurityRuleInterface();
134 List<NeutronSecurityRule> rules = new ArrayList<>();
135 for (Uuid uuid : group.getSecurityRules()) {
136 rules.add(srCrud.getNeutronSecurityRule(uuid.getValue()));
138 answer.setSecurityRules(rules);
140 if (group.getUuid() != null) {
141 answer.setID(group.getUuid().getValue());
147 public void close() throws Exception {
148 registration.close();