5e4c5a42774d7691f82bdb27037e97b832d75ebb
[controller.git] / opendaylight / networkconfiguration / neutron / implementation / src / main / java / org / opendaylight / controller / networkconfig / neutron / implementation / NeutronRouterInterface.java
1 /*
2  * Copyright IBM Corporation, 2013.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.networkconfig.neutron.implementation;
10
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;
16 import java.util.Set;
17 import java.util.concurrent.ConcurrentHashMap;
18 import java.util.concurrent.ConcurrentMap;
19
20 import org.opendaylight.controller.networkconfig.neutron.INeutronRouterCRUD;
21 import org.opendaylight.controller.networkconfig.neutron.NeutronRouter;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
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
29
30
31
32     // this method uses reflection to update an object from it's delta.
33
34     private boolean overwrite(Object target, Object delta) {
35         Method[] methods = target.getClass().getMethods();
36
37         for(Method toMethod: methods){
38             if(toMethod.getDeclaringClass().equals(target.getClass())
39                     && toMethod.getName().startsWith("set")){
40
41                 String toName = toMethod.getName();
42                 String fromName = toName.replace("set", "get");
43
44                 try {
45                     Method fromMethod = delta.getClass().getMethod(fromName);
46                     Object value = fromMethod.invoke(delta, (Object[])null);
47                     if(value != null){
48                         toMethod.invoke(target, value);
49                     }
50                 } catch (Exception e) {
51                     e.printStackTrace();
52                     return false;
53                 }
54             }
55         }
56         return true;
57     }
58
59
60     // IfNBRouterCRUD Interface methods
61
62     @Override
63     public boolean routerExists(String uuid) {
64         return routerDB.containsKey(uuid);
65     }
66
67     @Override
68     public NeutronRouter getRouter(String uuid) {
69         if (!routerExists(uuid)) {
70             return null;
71         }
72         return routerDB.get(uuid);
73     }
74
75     @Override
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);
81         }
82         logger.debug("Exiting getAllRouters, Found {} Routers", allRouters.size());
83         List<NeutronRouter> ans = new ArrayList<NeutronRouter>();
84         ans.addAll(allRouters);
85         return ans;
86     }
87
88     @Override
89     public boolean addRouter(NeutronRouter input) {
90         if (routerExists(input.getID())) {
91             return false;
92         }
93         routerDB.putIfAbsent(input.getID(), input);
94         return true;
95     }
96
97     @Override
98     public boolean removeRouter(String uuid) {
99         if (!routerExists(uuid)) {
100             return false;
101         }
102         routerDB.remove(uuid);
103         return true;
104     }
105
106     @Override
107     public boolean updateRouter(String uuid, NeutronRouter delta) {
108         if (!routerExists(uuid)) {
109             return false;
110         }
111         NeutronRouter target = routerDB.get(uuid);
112         return overwrite(target, delta);
113     }
114
115     @Override
116     public boolean routerInUse(String routerUUID) {
117         if (!routerExists(routerUUID)) {
118             return true;
119         }
120         NeutronRouter target = routerDB.get(routerUUID);
121         return (target.getInterfaces().size() > 0);
122     }
123
124 }