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.HashSet;
14 import java.util.List;
15 import java.util.Map.Entry;
17 import java.util.concurrent.ConcurrentHashMap;
18 import java.util.concurrent.ConcurrentMap;
20 import org.opendaylight.neutron.spi.INeutronRouterCRUD;
21 import org.opendaylight.neutron.spi.NeutronRouter;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 public class NeutronRouterInterface implements INeutronRouterCRUD {
26 private static final Logger logger = LoggerFactory.getLogger(NeutronRouterInterface.class);
27 private ConcurrentMap<String, NeutronRouter> routerDB = new ConcurrentHashMap<String, NeutronRouter>();
28 // methods needed for creating caches
32 // this method uses reflection to update an object from it's delta.
34 private boolean overwrite(Object target, Object delta) {
35 Method[] methods = target.getClass().getMethods();
37 for(Method toMethod: methods){
38 if(toMethod.getDeclaringClass().equals(target.getClass())
39 && toMethod.getName().startsWith("set")){
41 String toName = toMethod.getName();
42 String fromName = toName.replace("set", "get");
45 Method fromMethod = delta.getClass().getMethod(fromName);
46 Object value = fromMethod.invoke(delta, (Object[])null);
48 toMethod.invoke(target, value);
50 } catch (Exception e) {
60 // IfNBRouterCRUD Interface methods
63 public boolean routerExists(String uuid) {
64 return routerDB.containsKey(uuid);
68 public NeutronRouter getRouter(String uuid) {
69 if (!routerExists(uuid)) {
72 return routerDB.get(uuid);
76 public List<NeutronRouter> getAllRouters() {
77 Set<NeutronRouter> allRouters = new HashSet<NeutronRouter>();
78 for (Entry<String, NeutronRouter> entry : routerDB.entrySet()) {
79 NeutronRouter router = entry.getValue();
80 allRouters.add(router);
82 logger.debug("Exiting getAllRouters, Found {} Routers", allRouters.size());
83 List<NeutronRouter> ans = new ArrayList<NeutronRouter>();
84 ans.addAll(allRouters);
89 public boolean addRouter(NeutronRouter input) {
90 if (routerExists(input.getID())) {
93 routerDB.putIfAbsent(input.getID(), input);
98 public boolean removeRouter(String uuid) {
99 if (!routerExists(uuid)) {
102 routerDB.remove(uuid);
107 public boolean updateRouter(String uuid, NeutronRouter delta) {
108 if (!routerExists(uuid)) {
111 NeutronRouter target = routerDB.get(uuid);
112 return overwrite(target, delta);
116 public boolean routerInUse(String routerUUID) {
117 if (!routerExists(routerUUID)) {
120 NeutronRouter target = routerDB.get(routerUUID);
121 return (target.getInterfaces().size() > 0);