2 * Copyright (c) 2013, 2015 IBM Corporation 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
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;
19 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
20 import org.opendaylight.neutron.spi.INeutronRouterCRUD;
21 import org.opendaylight.neutron.spi.Neutron_IPs;
22 import org.opendaylight.neutron.spi.NeutronRouter;
23 import org.opendaylight.neutron.spi.NeutronRouter_Interface;
24 import org.opendaylight.neutron.spi.NeutronRouter_NetworkReference;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.Routers;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.routers.Router;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.routers.RouterBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.routers.router.ExternalGatewayInfo;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.routers.router.ExternalGatewayInfoBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.routers.router.external_gateway_info.ExternalFixedIpsBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.routers.router.Interfaces;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.routers.router.external_gateway_info.ExternalFixedIps;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 import org.osgi.framework.BundleContext;
37 import org.osgi.framework.ServiceRegistration;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 public class NeutronRouterInterface extends AbstractNeutronInterface<Router, Routers, NeutronRouter> implements INeutronRouterCRUD {
42 private static final Logger LOGGER = LoggerFactory.getLogger(NeutronRouterInterface.class);
43 // methods needed for creating caches
46 NeutronRouterInterface(ProviderContext providerContext) {
47 super(providerContext);
51 // IfNBRouterCRUD Interface methods
54 public boolean routerExists(String uuid) {
59 public NeutronRouter getRouter(String uuid) {
64 protected List<Router> getDataObjectList(Routers routers) {
65 return routers.getRouter();
69 public List<NeutronRouter> getAllRouters() {
74 public boolean addRouter(NeutronRouter input) {
79 public boolean removeRouter(String uuid) {
84 public boolean updateRouter(String uuid, NeutronRouter delta) {
85 return update(uuid, delta);
89 public boolean routerInUse(String routerUUID) {
90 if (!exists(routerUUID)) {
93 NeutronRouter target = getRouter(routerUUID);
94 return (target.getInterfaces().size() > 0);
98 protected Router toMd(NeutronRouter router) {
100 RouterBuilder routerBuilder = new RouterBuilder();
102 if (router.getID() != null) {
103 routerBuilder.setUuid(toUuid(router.getID()));
105 if (router.getName() != null) {
106 routerBuilder.setName(router.getName());
108 if (router.getTenantID() != null && !router.getTenantID().isEmpty()) {
109 routerBuilder.setTenantId(toUuid(router.getTenantID()));
111 if (router.getStatus() != null) {
112 routerBuilder.setStatus(router.getStatus());
114 if (router.getGatewayPortId() != null && !router.getGatewayPortId().isEmpty()) {
115 routerBuilder.setGatewayPortId(toUuid(router.getGatewayPortId()));
117 routerBuilder.setAdminStateUp(router.getAdminStateUp());
118 routerBuilder.setDistributed(router.getDistributed());
119 if (router.getRoutes() != null) {
120 List<String> routes = new ArrayList<String>();
121 for (String route : router.getRoutes()) {
124 routerBuilder.setRoutes(routes);
126 if (router.getExternalGatewayInfo() != null) {
127 ExternalGatewayInfo externalGatewayInfo = null;
128 List<NeutronRouter_NetworkReference> neutronRouter_NetworkReferences = new ArrayList<NeutronRouter_NetworkReference>();
129 neutronRouter_NetworkReferences.add(router.getExternalGatewayInfo());
130 for (NeutronRouter_NetworkReference externalGatewayInfos : neutronRouter_NetworkReferences) {
131 ExternalGatewayInfoBuilder builder = new ExternalGatewayInfoBuilder();
132 builder.setEnableSnat(externalGatewayInfos.getEnableSNAT());
133 builder.setExternalNetworkId(toUuid(externalGatewayInfos.getNetworkID()));
134 if (externalGatewayInfos.getExternalFixedIPs() != null) {
135 List<ExternalFixedIps> externalFixedIps = new ArrayList<ExternalFixedIps>();
136 for (Neutron_IPs eIP : externalGatewayInfos.getExternalFixedIPs()) {
137 ExternalFixedIpsBuilder eFixedIpBuilder = new ExternalFixedIpsBuilder();
138 eFixedIpBuilder.setIpAddress(new IpAddress(eIP.getIpAddress().toCharArray()));
139 eFixedIpBuilder.setSubnetId(toUuid(eIP.getSubnetUUID()));
140 externalFixedIps.add(eFixedIpBuilder.build());
142 builder.setExternalFixedIps(externalFixedIps);
144 externalGatewayInfo = builder.build();
146 routerBuilder.setExternalGatewayInfo(externalGatewayInfo);
148 if (router.getInterfaces() != null) {
149 Map<String, NeutronRouter_Interface> mapInterfaces = new HashMap<String, NeutronRouter_Interface>();
150 List<Interfaces> interfaces = new ArrayList<Interfaces>();
151 for (Entry<String, NeutronRouter_Interface> entry : mapInterfaces.entrySet()) {
152 interfaces.add((Interfaces) entry.getValue());
154 routerBuilder.setInterfaces(interfaces);
156 if (router.getID() != null) {
157 routerBuilder.setUuid(toUuid(router.getID()));
159 LOGGER.warn("Attempting to write neutron router without UUID");
161 return routerBuilder.build();
165 protected InstanceIdentifier<Router> createInstanceIdentifier(Router router) {
166 return InstanceIdentifier.create(Neutron.class)
167 .child(Routers.class)
168 .child(Router.class, router.getKey());
172 protected InstanceIdentifier<Routers> createInstanceIdentifier() {
173 return InstanceIdentifier.create(Neutron.class).child(Routers.class);
177 protected Router toMd(String uuid) {
178 RouterBuilder routerBuilder = new RouterBuilder();
179 routerBuilder.setUuid(toUuid(uuid));
180 return routerBuilder.build();
183 public static void registerNewInterface(BundleContext context,
184 ProviderContext providerContext,
185 List<ServiceRegistration<?>> registrations) {
186 NeutronRouterInterface neutronRouterInterface = new NeutronRouterInterface(providerContext);
187 ServiceRegistration<INeutronRouterCRUD> neutronRouterInterfaceRegistration = context.registerService(INeutronRouterCRUD.class, neutronRouterInterface, null);
188 if(neutronRouterInterfaceRegistration != null) {
189 registrations.add(neutronRouterInterfaceRegistration);
193 public NeutronRouter fromMd(Router router) {
194 NeutronRouter result = new NeutronRouter();
195 result.setID(router.getUuid().getValue());
196 result.setName(router.getName());
197 result.setTenantID(router.getTenantId());
198 result.setAdminStateUp(router.isAdminStateUp());
199 result.setStatus(router.getStatus());
200 result.setDistributed(router.isDistributed());
201 if (router.getGatewayPortId() != null) {
202 result.setGatewayPortId(String.valueOf(router.getGatewayPortId().getValue()));
204 if (router.getRoutes() != null) {
205 List<String> routes = new ArrayList<String>();
206 for (String route : router.getRoutes()) {
209 result.setRoutes(routes);
212 if (router.getExternalGatewayInfo() != null) {
213 NeutronRouter_NetworkReference extGwInfo = new NeutronRouter_NetworkReference();
214 extGwInfo.setNetworkID(String.valueOf(router.getExternalGatewayInfo().getExternalNetworkId().getValue()));
215 extGwInfo.setEnableSNAT(router.getExternalGatewayInfo().isEnableSnat());
216 if (router.getExternalGatewayInfo().getExternalFixedIps() != null) {
217 List<Neutron_IPs> fixedIPs = new ArrayList<Neutron_IPs>();
218 for (ExternalFixedIps mdFixedIP : router.getExternalGatewayInfo().getExternalFixedIps()) {
219 Neutron_IPs fixedIP = new Neutron_IPs();
220 fixedIP.setSubnetUUID(String.valueOf(mdFixedIP.getSubnetId().getValue()));
221 fixedIP.setIpAddress(String.valueOf(mdFixedIP.getIpAddress().getValue()));
222 fixedIPs.add(fixedIP);
224 extGwInfo.setExternalFixedIPs(fixedIPs);
226 result.setExternalGatewayInfo(extGwInfo);
229 if (router.getInterfaces() != null) {
230 Map<String, NeutronRouter_Interface> interfaces = new HashMap<String, NeutronRouter_Interface>();
231 for (Interfaces mdInterface : router.getInterfaces()) {
232 NeutronRouter_Interface pojoInterface = new NeutronRouter_Interface();
233 String id = mdInterface.getUuid().getValue();
234 pojoInterface.setID(id);
235 pojoInterface.setTenantID(mdInterface.getTenantId());
236 pojoInterface.setSubnetUUID(String.valueOf(mdInterface.getSubnetId().getValue()));
237 pojoInterface.setPortUUID(String.valueOf(mdInterface.getPortId().getValue()));
238 interfaces.put(id, pojoInterface);
240 result.setInterfaces(interfaces);