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.HashMap;
13 import java.util.HashSet;
14 import java.util.List;
16 import java.util.Map.Entry;
18 import java.util.concurrent.ConcurrentHashMap;
19 import java.util.concurrent.ConcurrentMap;
21 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
22 import org.opendaylight.neutron.spi.INeutronRouterCRUD;
23 import org.opendaylight.neutron.spi.NeutronRouter;
24 import org.opendaylight.neutron.spi.NeutronRouter_Interface;
25 import org.opendaylight.neutron.spi.NeutronRouter_NetworkReference;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev141002.routers.attributes.Routers;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev141002.routers.attributes.routers.Router;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev141002.routers.attributes.routers.RouterBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev141002.routers.attributes.routers.router.ExternalGatewayInfo;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev141002.routers.attributes.routers.router.ExternalGatewayInfoBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev141002.routers.attributes.routers.router.Interfaces;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev141002.routers.attributes.routers.router.external_gateway_info.ExternalFixedIps;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150325.Neutron;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.osgi.framework.BundleContext;
36 import org.osgi.framework.ServiceRegistration;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 public class NeutronRouterInterface extends AbstractNeutronInterface<Router, NeutronRouter> implements INeutronRouterCRUD {
41 private static final Logger LOGGER = LoggerFactory.getLogger(NeutronRouterInterface.class);
42 private ConcurrentMap<String, NeutronRouter> routerDB = new ConcurrentHashMap<String, NeutronRouter>();
43 // methods needed for creating caches
46 NeutronRouterInterface(ProviderContext providerContext) {
47 super(providerContext);
51 // IfNBRouterCRUD Interface methods
54 public boolean routerExists(String uuid) {
55 return routerDB.containsKey(uuid);
59 public NeutronRouter getRouter(String uuid) {
60 if (!routerExists(uuid)) {
63 return routerDB.get(uuid);
67 public List<NeutronRouter> getAllRouters() {
68 Set<NeutronRouter> allRouters = new HashSet<NeutronRouter>();
69 for (Entry<String, NeutronRouter> entry : routerDB.entrySet()) {
70 NeutronRouter router = entry.getValue();
71 allRouters.add(router);
73 LOGGER.debug("Exiting getAllRouters, Found {} Routers", allRouters.size());
74 List<NeutronRouter> ans = new ArrayList<NeutronRouter>();
75 ans.addAll(allRouters);
80 public boolean addRouter(NeutronRouter input) {
81 if (routerExists(input.getID())) {
84 routerDB.putIfAbsent(input.getID(), input);
90 public boolean removeRouter(String uuid) {
91 if (!routerExists(uuid)) {
94 routerDB.remove(uuid);
100 public boolean updateRouter(String uuid, NeutronRouter delta) {
101 if (!routerExists(uuid)) {
104 NeutronRouter target = routerDB.get(uuid);
105 boolean rc = overwrite(target, delta);
107 updateMd(routerDB.get(uuid));
113 public boolean routerInUse(String routerUUID) {
114 if (!routerExists(routerUUID)) {
117 NeutronRouter target = routerDB.get(routerUUID);
118 return (target.getInterfaces().size() > 0);
122 protected Router toMd(NeutronRouter router) {
124 RouterBuilder routerBuilder = new RouterBuilder();
126 if (router.getRouterUUID() != null) {
127 routerBuilder.setUuid(toUuid(router.getRouterUUID()));
129 if (router.getName() != null) {
130 routerBuilder.setName(router.getName());
132 if (router.getTenantID() != null && !router.getTenantID().isEmpty()) {
133 routerBuilder.setTenantId(toUuid(router.getTenantID()));
135 if (router.getStatus() != null) {
136 routerBuilder.setStatus(router.getStatus());
138 if (router.getGatewayPortId() != null && !router.getGatewayPortId().isEmpty()) {
139 routerBuilder.setGatewayPortId(toUuid(router.getGatewayPortId()));
141 routerBuilder.setAdminStateUp(router.getAdminStateUp());
142 routerBuilder.setDistribted(router.getDistributed());
143 if (router.getRoutes() != null) {
144 List<String> routes = new ArrayList<String>();
145 for (String route : router.getRoutes()) {
148 routerBuilder.setRoutes(routes);
150 if (router.getExternalGatewayInfo() != null) {
151 ExternalGatewayInfo externalGatewayInfo = null;
152 List<NeutronRouter_NetworkReference> neutronRouter_NetworkReferences = new ArrayList<NeutronRouter_NetworkReference>();
153 neutronRouter_NetworkReferences.add(router.getExternalGatewayInfo());
154 for (NeutronRouter_NetworkReference externalGatewayInfos : neutronRouter_NetworkReferences) {
155 ExternalGatewayInfoBuilder builder = new ExternalGatewayInfoBuilder();
156 builder.setEnableSnat(externalGatewayInfos.getEnableSNAT());
157 builder.setExternalNetworkId(toUuid(externalGatewayInfos.getNetworkID()));
158 List<ExternalFixedIps> externalFixedIps = new ArrayList<ExternalFixedIps>();
159 for (int i = 0; i < externalFixedIps.size(); i++) {
160 externalFixedIps.add((ExternalFixedIps) externalGatewayInfos.getExternalFixedIPs().get(i));
162 builder.setExternalFixedIps(externalFixedIps);
163 externalGatewayInfo = builder.build();
165 routerBuilder.setExternalGatewayInfo(externalGatewayInfo);
167 if (router.getInterfaces() != null) {
168 Map<String, NeutronRouter_Interface> mapInterfaces = new HashMap<String, NeutronRouter_Interface>();
169 List<Interfaces> interfaces = new ArrayList<Interfaces>();
170 for (Entry<String, NeutronRouter_Interface> entry : mapInterfaces.entrySet()) {
171 interfaces.add((Interfaces) entry.getValue());
173 routerBuilder.setInterfaces(interfaces);
175 if (router.getID() != null) {
176 routerBuilder.setUuid(toUuid(router.getID()));
178 LOGGER.warn("Attempting to write neutron router without UUID");
180 return routerBuilder.build();
184 protected InstanceIdentifier<Router> createInstanceIdentifier(Router router) {
185 return InstanceIdentifier.create(Neutron.class).child(Routers.class).child(Router.class, router.getKey());
189 protected Router toMd(String uuid) {
190 RouterBuilder routerBuilder = new RouterBuilder();
191 routerBuilder.setUuid(toUuid(uuid));
192 return routerBuilder.build();
195 public static void registerNewInterface(BundleContext context,
196 ProviderContext providerContext,
197 List<ServiceRegistration<?>> registrations) {
198 NeutronRouterInterface neutronRouterInterface = new NeutronRouterInterface(providerContext);
199 ServiceRegistration<INeutronRouterCRUD> neutronRouterInterfaceRegistration = context.registerService(INeutronRouterCRUD.class, neutronRouterInterface, null);
200 if(neutronRouterInterfaceRegistration != null) {
201 registrations.add(neutronRouterInterfaceRegistration);