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.HashMap;
12 import java.util.HashSet;
13 import java.util.List;
15 import java.util.Map.Entry;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
20 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
21 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.ovsdb.openstack.netvirt.translator.NeutronPort;
24 import org.opendaylight.ovsdb.openstack.netvirt.translator.NeutronPort_AllowedAddressPairs;
25 import org.opendaylight.ovsdb.openstack.netvirt.translator.NeutronPort_ExtraDHCPOption;
26 import org.opendaylight.ovsdb.openstack.netvirt.translator.NeutronPort_VIFDetail;
27 import org.opendaylight.ovsdb.openstack.netvirt.translator.NeutronSecurityGroup;
28 import org.opendaylight.ovsdb.openstack.netvirt.translator.Neutron_IPs;
29 import org.opendaylight.ovsdb.openstack.netvirt.translator.crud.INeutronSecurityGroupCRUD;
30 import org.opendaylight.ovsdb.openstack.netvirt.translator.crud.NeutronCRUDInterfaces;
31 import org.opendaylight.ovsdb.openstack.netvirt.translator.iaware.INeutronPortAware;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.binding.rev150712.PortBindingExtension;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.binding.rev150712.binding.attributes.VifDetails;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.port.attributes.AllowedAddressPairs;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.port.attributes.ExtraDhcpOpts;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.port.attributes.FixedIps;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.Ports;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
41 import org.opendaylight.yangtools.concepts.ListenerRegistration;
42 import org.opendaylight.yangtools.yang.binding.DataObject;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
47 public class NeutronPortChangeListener implements DataChangeListener, AutoCloseable{
48 private static final Logger LOG = LoggerFactory.getLogger(NeutronPortChangeListener.class);
50 private ListenerRegistration<DataChangeListener> registration;
51 private DataBroker db;
53 public NeutronPortChangeListener(DataBroker db){
55 InstanceIdentifier<Port> path = InstanceIdentifier
56 .create(Neutron.class)
59 LOG.debug("Register listener for Neutron Port model data changes");
61 this.db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION, path, this, DataChangeScope.ONE);
66 public void onDataChanged(
67 AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes) {
68 LOG.trace("Data changes : {}",changes);
70 Object[] subscribers = NeutronIAwareUtil.getInstances(INeutronPortAware.class, this);
71 createPort(changes, subscribers);
72 updatePort(changes, subscribers);
73 deletePort(changes, subscribers);
76 private void createPort(
77 AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes,
78 Object[] subscribers) {
79 for (Entry<InstanceIdentifier<?>, DataObject> newPort : changes.getCreatedData().entrySet()) {
80 if(newPort.getValue() instanceof Port){
81 NeutronPort port = fromMd((Port)newPort.getValue());
82 for(Object entry: subscribers){
83 INeutronPortAware subscriber = (INeutronPortAware)entry;
84 subscriber.neutronPortCreated(port);
90 private void updatePort(
91 AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes,
92 Object[] subscribers) {
93 Map<String, NeutronPort> originalPortMap = getChangedPorts(changes.getOriginalData());
94 for (Entry<InstanceIdentifier<?>, DataObject> updatePort : changes.getUpdatedData().entrySet()) {
95 if (updatePort.getValue() instanceof Port) {
96 NeutronPort port = fromMd((Port)updatePort.getValue());
97 NeutronPort originalPort = originalPortMap.get(port.getID());
98 if (originalPort != null) {
99 port.setOriginalPort(originalPort);
101 LOG.warn("Original Port data is missing");
103 for (Object entry: subscribers) {
104 INeutronPortAware subscriber = (INeutronPortAware)entry;
105 subscriber.neutronPortUpdated(port);
111 private void deletePort(
112 AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes,
113 Object[] subscribers) {
114 for (InstanceIdentifier<?> deletedPortPath : changes.getRemovedPaths()) {
115 if(deletedPortPath.getTargetType().equals(Port.class)){
116 NeutronPort port = fromMd((Port)changes.getOriginalData().get(deletedPortPath));
117 for(Object entry: subscribers){
118 INeutronPortAware subscriber = (INeutronPortAware)entry;
119 subscriber.neutronPortDeleted(port);
126 * This method is borrowed from NeutronPortInterface.java class of Neutron Northbound class.
127 * We will be utilizing similar code from other classes from the same package of neutron project.
129 private NeutronPort fromMd(Port port) {
131 NeutronPort result = new NeutronPort();
132 result.setAdminStateUp(port.isAdminStateUp());
133 if (port.getAllowedAddressPairs() != null) {
134 List<NeutronPort_AllowedAddressPairs> pairs = new ArrayList<>();
135 for (AllowedAddressPairs mdPair : port.getAllowedAddressPairs()) {
136 NeutronPort_AllowedAddressPairs pair = new NeutronPort_AllowedAddressPairs();
137 pair.setIpAddress(mdPair.getIpAddress());
138 pair.setMacAddress(mdPair.getMacAddress());
139 pair.setPortID(mdPair.getPortId());
142 result.setAllowedAddressPairs(pairs);
144 result.setDeviceID(port.getDeviceId());
145 result.setDeviceOwner(port.getDeviceOwner());
146 if (port.getExtraDhcpOpts() != null) {
147 List<NeutronPort_ExtraDHCPOption> options = new ArrayList<>();
148 for (ExtraDhcpOpts opt : port.getExtraDhcpOpts()) {
149 NeutronPort_ExtraDHCPOption arg = new NeutronPort_ExtraDHCPOption();
150 arg.setName(opt.getOptName());
151 arg.setValue(opt.getOptValue());
154 result.setExtraDHCPOptions(options);
156 if (port.getFixedIps() != null) {
157 List<Neutron_IPs> ips = new ArrayList<>();
158 for (FixedIps mdIP : port.getFixedIps()) {
159 Neutron_IPs ip = new Neutron_IPs();
160 ip.setIpAddress(String.valueOf(mdIP.getIpAddress().getValue()));
161 ip.setSubnetUUID(mdIP.getSubnetId().getValue());
164 result.setFixedIPs(ips);
166 result.setMacAddress(port.getMacAddress());
167 result.setName(port.getName());
168 result.setNetworkUUID(String.valueOf(port.getNetworkId().getValue()));
169 if (port.getSecurityGroups() != null) {
170 Set<NeutronSecurityGroup> allGroups = new HashSet<>();
171 NeutronCRUDInterfaces interfaces = new NeutronCRUDInterfaces().fetchINeutronSecurityGroupCRUD(this);
172 INeutronSecurityGroupCRUD sgIf = interfaces.getSecurityGroupInterface();
173 for (Uuid sgUuid : port.getSecurityGroups()) {
174 allGroups.add(sgIf.getNeutronSecurityGroup(sgUuid.getValue()));
176 List<NeutronSecurityGroup> groups = new ArrayList<>();
177 groups.addAll(allGroups);
178 result.setSecurityGroups(groups);
180 result.setStatus(port.getStatus());
181 if (port.getTenantId() != null) {
182 result.setTenantID(String.valueOf(port.getTenantId().getValue()).replace("-", ""));
184 result.setPortUUID(String.valueOf(port.getUuid().getValue()));
185 addExtensions(port, result);
189 protected void addExtensions(Port port, NeutronPort result) {
190 PortBindingExtension binding = port.getAugmentation(PortBindingExtension.class);
191 result.setBindinghostID(binding.getHostId());
192 if (binding.getVifDetails() != null) {
193 List<NeutronPort_VIFDetail> details = new ArrayList<>();
194 for (VifDetails vifDetail : binding.getVifDetails()) {
195 NeutronPort_VIFDetail detail = new NeutronPort_VIFDetail();
196 detail.setPortFilter(vifDetail.isPortFilter());
197 detail.setOvsHybridPlug(vifDetail.isOvsHybridPlug());
200 result.setVIFDetail(details);
202 result.setBindingvifType(binding.getVifType());
203 result.setBindingvnicType(binding.getVnicType());
206 private Map<String,NeutronPort> getChangedPorts(Map<InstanceIdentifier<?>, DataObject> changedData) {
207 LOG.trace("getChangedPorts:" + changedData);
208 Map<String,NeutronPort> portMap = new HashMap<>();
209 for (Map.Entry<InstanceIdentifier<?>, DataObject> changed : changedData.entrySet()) {
210 if (changed.getValue() instanceof Port) {
211 NeutronPort port = fromMd((Port)changed.getValue());
212 portMap.put(port.getID(), port);
219 public void close() throws Exception {
220 registration.close();