Merge "Refactor Additional header for netconf hello message."
[controller.git] / opendaylight / md-sal / sal-restconf-broker / src / main / java / org / opendaylight / controller / sal / restconf / broker / tools / RemoteStreamTools.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.sal.restconf.broker.tools;
9
10 import com.google.common.util.concurrent.ListenableFuture;
11 import java.util.HashMap;
12 import java.util.Iterator;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.Set;
16 import java.util.concurrent.ExecutionException;
17 import java.util.concurrent.Future;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.remote.rev140114.CreateNotificationStreamInputBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.remote.rev140114.CreateNotificationStreamOutput;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.remote.rev140114.QName;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.remote.rev140114.SalRemoteService;
22 import org.opendaylight.yangtools.restconf.client.api.RestconfClientContext;
23 import org.opendaylight.yangtools.restconf.client.api.event.EventStreamInfo;
24 import org.opendaylight.yangtools.yang.common.RpcResult;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 public class RemoteStreamTools {
29     private static final Logger logger = LoggerFactory.getLogger(RemoteStreamTools.class.toString());
30
31     public static String createNotificationStream(SalRemoteService salRemoteService,List<QName> notifications){
32         CreateNotificationStreamInputBuilder notificationStreamInputBuilder = new CreateNotificationStreamInputBuilder();
33
34         if (null == notifications){
35             notificationStreamInputBuilder.setNotifications(notifications);
36         }
37
38         Future<RpcResult<CreateNotificationStreamOutput>> notificationStream = salRemoteService.createNotificationStream(notificationStreamInputBuilder.build());
39
40         String nofiticationStreamIdentifier = "";
41         try {
42             if (notificationStream.get().isSuccessful()){
43                 nofiticationStreamIdentifier  = notificationStream.get().getResult().getNotificationStreamIdentifier();
44             }
45         } catch (InterruptedException e) {
46             logger.trace("Interrupted while resolving notification stream identifier due to {}",e);
47         } catch (ExecutionException e) {
48             logger.trace("Execution exception while resolving notification stream identifier due to {}",e);
49         }
50         return nofiticationStreamIdentifier;
51     }
52
53     public static Map<String,EventStreamInfo> createEventStream(RestconfClientContext restconfClientContext, String desiredStreamName){
54         ListenableFuture<Set<EventStreamInfo>> availableEventStreams = restconfClientContext.getAvailableEventStreams();
55         final Map<String,EventStreamInfo> desiredEventStream = new HashMap<String,EventStreamInfo>();
56
57         try {
58             Iterator<EventStreamInfo> it = availableEventStreams.get().iterator();
59             while (it.hasNext()){
60                 if (it.next().getIdentifier().equals(desiredStreamName)){
61                     desiredEventStream.put(desiredStreamName,it.next());
62                 }
63             }
64         } catch (InterruptedException e) {
65             logger.trace("Resolving of event stream interrupted due to  {}",e);
66         } catch (ExecutionException e) {
67             logger.trace("Resolving of event stream failed due to {}",e);
68         }
69         return desiredEventStream;
70     }
71 }