2 * Copyright (c) 2014 Cisco Systems, Inc. and others. 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
8 package org.opendaylight.controller.sample.zeromq.provider;
10 import org.opendaylight.controller.sal.common.util.RpcErrors;
11 import org.opendaylight.controller.sal.common.util.Rpcs;
12 import org.opendaylight.controller.sal.core.api.AbstractProvider;
13 import org.opendaylight.controller.sal.core.api.Broker.ProviderSession;
14 import org.opendaylight.controller.sal.core.api.Broker.RpcRegistration;
15 import org.opendaylight.controller.sal.core.api.RpcImplementation;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.common.RpcError;
18 import org.opendaylight.yangtools.yang.common.RpcResult;
19 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
20 import org.opendaylight.yangtools.yang.data.api.Node;
21 import org.opendaylight.yangtools.yang.data.impl.CompositeNodeTOImpl;
22 import org.opendaylight.yangtools.yang.data.impl.SimpleNodeTOImpl;
23 import org.osgi.framework.BundleContext;
24 import org.osgi.framework.ServiceRegistration;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
31 public class ExampleProvider extends AbstractProvider implements RpcImplementation {
33 private final URI namespace = URI.create("http://cisco.com/example");
34 private final QName QNAME = new QName(namespace, "heartbeat");
35 private RpcRegistration reg;
37 private ServiceRegistration thisReg;
39 private ProviderSession session;
40 private Logger _logger = LoggerFactory.getLogger(ExampleProvider.class);
43 public void onSessionInitiated(ProviderSession session) {
44 this.session = session;
48 public Set<QName> getSupportedRpcs() {
49 Set<QName> supportedRpcs = new HashSet<QName>();
50 supportedRpcs.add(QNAME);
55 public RpcResult<CompositeNode> invokeRpc(final QName rpc, CompositeNode input) {
56 boolean success = false;
57 CompositeNode output = null;
58 Collection<RpcError> errors = new ArrayList<>();
60 // Only handle supported RPC calls
61 if (getSupportedRpcs().contains(rpc)) {
63 errors.add(RpcErrors.getRpcError("app", "tag", "info", RpcError.ErrorSeverity.WARNING, "message:null input", RpcError.ErrorType.RPC, null));
66 if (isErroneousInput(input)) {
67 errors.add(RpcErrors.getRpcError("app", "tag", "info", RpcError.ErrorSeverity.ERROR, "message:error", RpcError.ErrorType.RPC, null));
71 output = addSuccessNode(input);
75 return Rpcs.getRpcResult(success, output, errors);
78 // Examines input -- dives into CompositeNodes and finds any value equal to "error"
79 private boolean isErroneousInput(CompositeNode input) {
80 for (Node<?> n : input.getChildren()) {
81 if (n instanceof CompositeNode) {
82 if (isErroneousInput((CompositeNode)n)) {
87 if ((input.getChildren().get(0).getValue()).equals("error")) {
95 // Adds a child SimpleNode containing the value "success" to the input CompositeNode
96 private CompositeNode addSuccessNode(CompositeNode input) {
97 List<Node<?>> list = new ArrayList<Node<?>>(input.getChildren());
98 SimpleNodeTOImpl<String> simpleNode = new SimpleNodeTOImpl<String>(QNAME, input, "success");
100 return new CompositeNodeTOImpl(QNAME, null, list);
104 protected void startImpl(BundleContext context) {
105 thisReg = context.registerService(ExampleProvider.class, this, new Hashtable<String, String>());
109 protected void stopImpl(BundleContext context) {
113 thisReg.unregister();
114 } catch (Exception e) {
115 // TODO Auto-generated catch block
121 public void announce(QName name) {
122 _logger.debug("Announcing [{}]\n\n\n", name);
123 reg = this.session.addRpcImplementation(name, this);