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.lang.reflect.Method;
12 import java.util.ArrayList;
13 import java.util.HashMap;
14 import java.util.HashSet;
15 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.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 public class NeutronRouterInterface extends AbstractNeutronInterface<Router, NeutronRouter> implements INeutronRouterCRUD {
39 private static final Logger logger = LoggerFactory.getLogger(NeutronRouterInterface.class);
40 private ConcurrentMap<String, NeutronRouter> routerDB = new ConcurrentHashMap<String, NeutronRouter>();
41 // methods needed for creating caches
44 NeutronRouterInterface(ProviderContext providerContext) {
45 super(providerContext);
48 // this method uses reflection to update an object from it's delta.
50 private boolean overwrite(Object target, Object delta) {
51 Method[] methods = target.getClass().getMethods();
53 for(Method toMethod: methods){
54 if(toMethod.getDeclaringClass().equals(target.getClass())
55 && toMethod.getName().startsWith("set")){
57 String toName = toMethod.getName();
58 String fromName = toName.replace("set", "get");
61 Method fromMethod = delta.getClass().getMethod(fromName);
62 Object value = fromMethod.invoke(delta, (Object[])null);
64 toMethod.invoke(target, value);
66 } catch (Exception e) {
76 // IfNBRouterCRUD Interface methods
79 public boolean routerExists(String uuid) {
80 return routerDB.containsKey(uuid);
84 public NeutronRouter getRouter(String uuid) {
85 if (!routerExists(uuid)) {
88 return routerDB.get(uuid);
92 public List<NeutronRouter> getAllRouters() {
93 Set<NeutronRouter> allRouters = new HashSet<NeutronRouter>();
94 for (Entry<String, NeutronRouter> entry : routerDB.entrySet()) {
95 NeutronRouter router = entry.getValue();
96 allRouters.add(router);
98 logger.debug("Exiting getAllRouters, Found {} Routers", allRouters.size());
99 List<NeutronRouter> ans = new ArrayList<NeutronRouter>();
100 ans.addAll(allRouters);
105 public boolean addRouter(NeutronRouter input) {
106 if (routerExists(input.getID())) {
109 routerDB.putIfAbsent(input.getID(), input);
115 public boolean removeRouter(String uuid) {
116 if (!routerExists(uuid)) {
119 routerDB.remove(uuid);
120 removeMd(toMd(uuid));
125 public boolean updateRouter(String uuid, NeutronRouter delta) {
126 if (!routerExists(uuid)) {
129 NeutronRouter target = routerDB.get(uuid);
130 boolean rc = overwrite(target, delta);
132 updateMd(routerDB.get(uuid));
138 public boolean routerInUse(String routerUUID) {
139 if (!routerExists(routerUUID)) {
142 NeutronRouter target = routerDB.get(routerUUID);
143 return (target.getInterfaces().size() > 0);
147 protected Router toMd(NeutronRouter router) {
149 RouterBuilder routerBuilder = new RouterBuilder();
151 if (router.getRouterUUID() != null) {
152 routerBuilder.setUuid(toUuid(router.getRouterUUID()));
154 if (router.getName() != null) {
155 routerBuilder.setName(router.getName());
157 if (router.getTenantID() != null && !router.getTenantID().isEmpty()) {
158 routerBuilder.setTenantId(toUuid(router.getTenantID()));
160 if (router.getStatus() != null) {
161 routerBuilder.setStatus(router.getStatus());
163 if (router.getGatewayPortId() != null && !router.getGatewayPortId().isEmpty()) {
164 routerBuilder.setGatewayPortId(toUuid(router.getGatewayPortId()));
166 routerBuilder.setAdminStateUp(router.getAdminStateUp());
167 routerBuilder.setDistribted(router.getDistributed());
168 if (router.getRoutes() != null) {
169 List<String> routes = new ArrayList<String>();
170 for (String route : router.getRoutes()) {
173 routerBuilder.setRoutes(routes);
175 if (router.getExternalGatewayInfo() != null) {
176 ExternalGatewayInfo externalGatewayInfo = null;
177 List<NeutronRouter_NetworkReference> neutronRouter_NetworkReferences = new ArrayList<NeutronRouter_NetworkReference>();
178 neutronRouter_NetworkReferences.add(router.getExternalGatewayInfo());
179 for (NeutronRouter_NetworkReference externalGatewayInfos : neutronRouter_NetworkReferences) {
180 ExternalGatewayInfoBuilder builder = new ExternalGatewayInfoBuilder();
181 builder.setEnableSnat(externalGatewayInfos.getEnableSNAT());
182 builder.setExternalNetworkId(toUuid(externalGatewayInfos.getNetworkID()));
183 List<ExternalFixedIps> externalFixedIps = new ArrayList<ExternalFixedIps>();
184 for (int i = 0; i < externalFixedIps.size(); i++) {
185 externalFixedIps.add((ExternalFixedIps) externalGatewayInfos.getExternalFixedIPs().get(i));
187 builder.setExternalFixedIps(externalFixedIps);
188 externalGatewayInfo = builder.build();
190 routerBuilder.setExternalGatewayInfo(externalGatewayInfo);
192 if (router.getInterfaces() != null) {
193 HashMap<String, NeutronRouter_Interface> mapInterfaces = new HashMap<String, NeutronRouter_Interface>();
194 List<Interfaces> interfaces = new ArrayList<Interfaces>();
195 for (Entry<String, NeutronRouter_Interface> entry : mapInterfaces.entrySet()) {
196 interfaces.add((Interfaces) entry.getValue());
198 routerBuilder.setInterfaces(interfaces);
200 if (router.getID() != null) {
201 routerBuilder.setUuid(toUuid(router.getID()));
203 logger.warn("Attempting to write neutron router without UUID");
205 return routerBuilder.build();
209 protected InstanceIdentifier<Router> createInstanceIdentifier(Router router) {
210 return InstanceIdentifier.create(Neutron.class).child(Routers.class).child(Router.class, router.getKey());
214 protected Router toMd(String uuid) {
215 RouterBuilder routerBuilder = new RouterBuilder();
216 routerBuilder.setUuid(toUuid(uuid));
217 return routerBuilder.build();