BUG-2453 (De)Serialize enum values as defined in yang
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / osgi / NetconfOperationServiceFactoryImpl.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
9 package org.opendaylight.controller.netconf.confignetconfconnector.osgi;
10
11 import java.lang.management.ManagementFactory;
12 import java.util.HashSet;
13 import java.util.Set;
14 import javax.management.MBeanServer;
15 import org.opendaylight.controller.config.util.ConfigRegistryJMXClient;
16 import org.opendaylight.controller.netconf.api.Capability;
17 import org.opendaylight.controller.netconf.api.monitoring.CapabilityListener;
18 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceFactory;
19 import org.opendaylight.controller.netconf.util.capability.BasicCapability;
20 import org.opendaylight.controller.netconf.util.capability.YangModuleCapability;
21 import org.opendaylight.yangtools.yang.model.api.Module;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 public class NetconfOperationServiceFactoryImpl implements NetconfOperationServiceFactory {
26
27     public static final int ATTEMPT_TIMEOUT_MS = 1000;
28     private static final int SILENT_ATTEMPTS = 30;
29
30     private final YangStoreService yangStoreService;
31     private final ConfigRegistryJMXClient jmxClient;
32
33     private static final Logger LOG = LoggerFactory.getLogger(NetconfOperationServiceFactoryImpl.class);
34
35     public NetconfOperationServiceFactoryImpl(YangStoreService yangStoreService) {
36         this(yangStoreService, ManagementFactory.getPlatformMBeanServer());
37     }
38
39     public NetconfOperationServiceFactoryImpl(YangStoreService yangStoreService, MBeanServer mBeanServer) {
40         this.yangStoreService = yangStoreService;
41
42         ConfigRegistryJMXClient configRegistryJMXClient;
43         int i = 0;
44         // Config registry might not be present yet, but will be eventually
45         while(true) {
46
47             try {
48                 configRegistryJMXClient = new ConfigRegistryJMXClient(mBeanServer);
49                 break;
50             } catch (IllegalStateException e) {
51                 ++i;
52                 if (i > SILENT_ATTEMPTS) {
53                     LOG.info("JMX client not created after {} attempts, still trying", i, e);
54                 } else {
55                     LOG.debug("JMX client could not be created, reattempting, try {}", i, e);
56                 }
57                 try {
58                     Thread.sleep(ATTEMPT_TIMEOUT_MS);
59                 } catch (InterruptedException e1) {
60                     Thread.currentThread().interrupt();
61                     throw new IllegalStateException("Interrupted while reattempting connection", e1);
62                 }
63             }
64         }
65
66         jmxClient = configRegistryJMXClient;
67         if (i > SILENT_ATTEMPTS) {
68             LOG.info("Created JMX client after {} attempts", i);
69         } else {
70             LOG.debug("Created JMX client after {} attempts", i);
71         }
72     }
73
74     @Override
75     public NetconfOperationServiceImpl createService(String netconfSessionIdForReporting) {
76         return new NetconfOperationServiceImpl(yangStoreService, jmxClient, netconfSessionIdForReporting);
77     }
78
79
80     @Override
81     public Set<Capability> getCapabilities() {
82         return setupCapabilities(yangStoreService);
83     }
84
85     @Override
86     public AutoCloseable registerCapabilityListener(final CapabilityListener listener) {
87         return yangStoreService.registerCapabilityListener(listener);
88     }
89
90     public static Set<Capability> setupCapabilities(final YangStoreContext yangStoreSnapshot) {
91         Set<Capability> capabilities = new HashSet<>();
92         // [RFC6241] 8.3.  Candidate Configuration Capability
93         capabilities.add(new BasicCapability("urn:ietf:params:netconf:capability:candidate:1.0"));
94
95         // TODO rollback on error not supported EditConfigXmlParser:100
96         // [RFC6241] 8.5.  Rollback-on-Error Capability
97         // capabilities.add(new BasicCapability("urn:ietf:params:netconf:capability:rollback-on-error:1.0"));
98
99         Set<Module> modules = yangStoreSnapshot.getModules();
100         for (Module module : modules) {
101             capabilities.add(new YangModuleCapability(module, yangStoreSnapshot.getModuleSource(module)));
102         }
103
104         return capabilities;
105     }
106
107 }