Add config system API to recreate a module instance
[controller.git] / opendaylight / config / config-util / src / test / java / org / opendaylight / controller / config / util / TestingConfigTransactionController.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  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 package org.opendaylight.controller.config.util;
9
10 import com.google.common.collect.Sets;
11 import java.util.Collections;
12 import java.util.HashMap;
13 import java.util.Map;
14 import java.util.Set;
15 import javax.management.InstanceAlreadyExistsException;
16 import javax.management.InstanceNotFoundException;
17 import javax.management.ObjectName;
18 import org.opendaylight.controller.config.api.ValidationException;
19 import org.opendaylight.controller.config.api.jmx.ConfigTransactionControllerMXBean;
20 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
21
22 public class TestingConfigTransactionController implements
23         ConfigTransactionControllerMXBean {
24
25     public final ObjectName conf1, conf2, conf3;
26     public ObjectName conf4;
27     public String check;
28     Map<String, ObjectName> mapSub;
29     Map<String, Map<String, ObjectName>> map;
30
31     public static final String moduleName1 = "moduleA";
32     public static final String moduleName2 = "moduleB";
33     public static final String instName1 = "instA";
34     public static final String instName2 = "instB";
35
36     public TestingConfigTransactionController() throws Exception {
37         conf1 = ObjectNameUtil.createON(ObjectNameUtil.ON_DOMAIN
38                 + ":type=Module," + ObjectNameUtil.MODULE_FACTORY_NAME_KEY
39                 + "=" + moduleName1);
40         conf2 = ObjectNameUtil.createON(ObjectNameUtil.ON_DOMAIN
41                 + ":type=Module," + ObjectNameUtil.MODULE_FACTORY_NAME_KEY
42                 + "=" + moduleName1 + "," + ObjectNameUtil.INSTANCE_NAME_KEY
43                 + "=" + instName1);
44         conf3 = ObjectNameUtil.createON(ObjectNameUtil.ON_DOMAIN
45                 + ":type=Module," + ObjectNameUtil.MODULE_FACTORY_NAME_KEY
46                 + "=" + moduleName2 + "," + ObjectNameUtil.INSTANCE_NAME_KEY
47                 + "=" + instName2);
48         conf4 = ObjectNameUtil.createON(ObjectNameUtil.ON_DOMAIN
49                 + ":type=Module," + ObjectNameUtil.MODULE_FACTORY_NAME_KEY
50                 + "=" + moduleName2 + "," + ObjectNameUtil.INSTANCE_NAME_KEY
51                 + "=" + instName2);
52         mapSub = new HashMap<String, ObjectName>();
53         map = new HashMap<String, Map<String,ObjectName>>();
54     }
55
56     @Override
57     public ObjectName createModule(String moduleName, String instanceName)
58             throws InstanceAlreadyExistsException {
59         //return null;
60         return ObjectNameUtil.createON(ObjectNameUtil.ON_DOMAIN
61                 + ":type=Module," + ObjectNameUtil.MODULE_FACTORY_NAME_KEY
62                 + "=" + moduleName);
63     }
64
65     @Override
66     public void reCreateModule(ObjectName objectName) {
67     }
68
69     @Override
70     public void destroyModule(ObjectName objectName)
71             throws InstanceNotFoundException {
72         if(objectName != null){
73             conf4 = null;
74         }
75     }
76
77     @Override
78     public void abortConfig() {
79     }
80
81     @Override
82     public void validateConfig() throws ValidationException {
83     }
84
85     @Override
86     public String getTransactionName() {
87         //return null;
88         return "transactionName";
89     }
90
91     @Override
92     public Set<String> getAvailableModuleNames() {
93         return null;
94     }
95
96     @Override
97     public Set<ObjectName> lookupConfigBeans() {
98         return Sets.newHashSet(conf1, conf2, conf3);
99     }
100
101     @Override
102     public Set<ObjectName> lookupConfigBeans(String moduleName) {
103         if (moduleName.equals(moduleName1)) {
104             return Sets.newHashSet(conf1, conf2);
105         } else if (moduleName.equals(moduleName2)) {
106             return Sets.newHashSet(conf3);
107         } else {
108             return null;
109         }
110     }
111
112     @Override
113     public ObjectName lookupConfigBean(String moduleName, String instanceName)
114             throws InstanceNotFoundException {
115         if (moduleName.equals(InstanceNotFoundException.class.getSimpleName())) {
116             throw new InstanceNotFoundException();
117         }
118         return conf3;
119     }
120
121     @Override
122     public Set<ObjectName> lookupConfigBeans(String moduleName,
123             String instanceName) {
124         if (moduleName.equals(moduleName1) && instanceName.equals(instName1)) {
125             return Sets.newHashSet(conf2);
126         } else if (moduleName.equals(moduleName2)
127                 && instanceName.equals(instName2)) {
128             return Sets.newHashSet(conf3);
129         } else {
130             return null;
131         }
132     }
133
134     @Override
135     public void checkConfigBeanExists(ObjectName objectName) throws InstanceNotFoundException {
136         check = "configBeanExists";
137     }
138
139     @Override
140     public ObjectName saveServiceReference(String serviceInterfaceName, String refName, ObjectName moduleON) throws InstanceNotFoundException {
141         return moduleON;
142     }
143
144     @Override
145     public void removeServiceReference(String serviceInterfaceName, String refName) {
146         check = refName;
147     }
148
149     @Override
150     public void removeAllServiceReferences() {
151         check = null;
152     }
153
154     @Override
155     public ObjectName lookupConfigBeanByServiceInterfaceName(String serviceInterfaceQName, String refName) {
156         return conf3;
157     }
158
159     @Override
160     public Map<String, Map<String, ObjectName>> getServiceMapping() {
161         mapSub.put("A",conf2);
162         map.put("AA", mapSub);
163         return map;
164     }
165
166     @Override
167     public Map<String, ObjectName> lookupServiceReferencesByServiceInterfaceName(String serviceInterfaceQName) {
168         mapSub.put("A",conf2);
169         return mapSub;
170     }
171
172     @Override
173     public Set<String> lookupServiceInterfaceNames(ObjectName objectName) throws InstanceNotFoundException {
174         return Sets.newHashSet("setA");
175     }
176
177     @Override
178     public String getServiceInterfaceName(String namespace, String localName) {
179         return check=namespace+localName;
180     }
181
182     @Override
183     public boolean removeServiceReferences(ObjectName objectName) throws InstanceNotFoundException {
184         return true;
185     }
186
187     @Override
188     public Set<String> getAvailableModuleFactoryQNames() {
189         return Sets.newHashSet("availableModuleFactoryQNames");
190     }
191
192     @Override
193     public Set<ObjectName> lookupRuntimeBeans() {
194         return Collections.emptySet();
195     }
196
197     @Override
198     public Set<ObjectName> lookupRuntimeBeans(final String moduleName, final String instanceName) {
199         return Collections.emptySet();
200     }
201
202     @Override
203     public ObjectName getServiceReference(String serviceInterfaceQName, String refName) throws InstanceNotFoundException {
204         return conf3;
205     }
206
207     @Override
208     public void checkServiceReferenceExists(ObjectName objectName) throws InstanceNotFoundException {
209         check = "referenceExist";
210     }
211 }