2 * Copyright IBM Corporation, 2013. 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
9 package org.opendaylight.neutron.transcriber;
11 import java.util.ArrayList;
12 import java.util.HashSet;
13 import java.util.List;
14 import java.util.Map.Entry;
16 import java.util.concurrent.ConcurrentHashMap;
17 import java.util.concurrent.ConcurrentMap;
19 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
20 import org.opendaylight.neutron.spi.INeutronNetworkCRUD;
21 import org.opendaylight.neutron.spi.NeutronNetwork;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.networks.rev141002.networks.attributes.Networks;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.networks.rev141002.networks.attributes.networks.Network;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.networks.rev141002.networks.attributes.networks.NetworkBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150325.Neutron;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.osgi.framework.BundleContext;
29 import org.osgi.framework.ServiceRegistration;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 public class NeutronNetworkInterface extends AbstractNeutronInterface<Network,NeutronNetwork> implements INeutronNetworkCRUD {
34 private static final Logger LOGGER = LoggerFactory.getLogger(NeutronNetworkInterface.class);
35 private ConcurrentMap<String, NeutronNetwork> networkDB = new ConcurrentHashMap<String, NeutronNetwork>();
37 NeutronNetworkInterface(ProviderContext providerContext) {
38 super(providerContext);
41 // IfNBNetworkCRUD methods
44 public boolean networkExists(String uuid) {
45 return networkDB.containsKey(uuid);
49 public NeutronNetwork getNetwork(String uuid) {
50 if (!networkExists(uuid)) {
53 return networkDB.get(uuid);
57 public List<NeutronNetwork> getAllNetworks() {
58 Set<NeutronNetwork> allNetworks = new HashSet<NeutronNetwork>();
59 for (Entry<String, NeutronNetwork> entry : networkDB.entrySet()) {
60 NeutronNetwork network = entry.getValue();
61 allNetworks.add(network);
63 LOGGER.debug("Exiting getAllNetworks, Found {} OpenStackNetworks", allNetworks.size());
64 List<NeutronNetwork> ans = new ArrayList<NeutronNetwork>();
65 ans.addAll(allNetworks);
70 public boolean addNetwork(NeutronNetwork input) {
71 if (networkExists(input.getID())) {
74 networkDB.putIfAbsent(input.getID(), input);
76 //TODO: add code to find INeutronNetworkAware services and call newtorkCreated on them
81 public boolean removeNetwork(String uuid) {
82 if (!networkExists(uuid)) {
85 networkDB.remove(uuid);
87 //TODO: add code to find INeutronNetworkAware services and call newtorkDeleted on them
92 public boolean updateNetwork(String uuid, NeutronNetwork delta) {
93 if (!networkExists(uuid)) {
96 /* note: because what we get is *not* a delta but (at this point) the updated
97 * object, this is much simpler - just replace the value and update the mdsal
99 networkDB.replace(uuid, delta);
105 public boolean networkInUse(String netUUID) {
106 if (!networkExists(netUUID)) {
109 NeutronNetwork target = networkDB.get(netUUID);
110 if (target.getPortsOnNetwork().size() > 0) {
116 protected Network toMd(NeutronNetwork network) {
117 NetworkBuilder networkBuilder = new NetworkBuilder();
118 networkBuilder.setAdminStateUp(network.getAdminStateUp());
119 if (network.getNetworkName() != null) {
120 networkBuilder.setName(network.getNetworkName());
122 if (network.getShared() != null) {
123 networkBuilder.setShared(network.getShared());
125 if (network.getStatus() != null) {
126 networkBuilder.setStatus(network.getStatus());
128 if (network.getSubnets() != null) {
129 List<Uuid> subnets = new ArrayList<Uuid>();
130 for( String subnet : network.getSubnets()) {
131 subnets.add(toUuid(subnet));
133 networkBuilder.setSubnets(subnets);
135 if (network.getTenantID() != null) {
136 networkBuilder.setTenantId(toUuid(network.getTenantID()));
138 if (network.getNetworkUUID() != null) {
139 networkBuilder.setUuid(toUuid(network.getNetworkUUID()));
141 LOGGER.warn("Attempting to write neutron network without UUID");
143 return networkBuilder.build();
146 protected Network toMd(String uuid) {
147 NetworkBuilder networkBuilder = new NetworkBuilder();
148 networkBuilder.setUuid(toUuid(uuid));
149 return networkBuilder.build();
153 protected InstanceIdentifier<Network> createInstanceIdentifier(Network network) {
154 return InstanceIdentifier.create(Neutron.class)
155 .child(Networks.class)
156 .child(Network.class,network.getKey());
159 public static void registerNewInterface(BundleContext context,
160 ProviderContext providerContext,
161 List<ServiceRegistration<?>> registrations) throws Exception {
162 NeutronNetworkInterface neutronNetworkInterface = new NeutronNetworkInterface(providerContext);
163 ServiceRegistration<INeutronNetworkCRUD> neutronNetworkInterfaceRegistration = context.registerService(INeutronNetworkCRUD.class, neutronNetworkInterface, null);
164 if(neutronNetworkInterfaceRegistration != null) {
165 registrations.add(neutronNetworkInterfaceRegistration);