Adjust for Binding RPC codegen changes
[controller.git] / opendaylight / md-sal / messagebus-util / src / main / java / org / opendaylight / controller / messagebus / app / util / Util.java
1 /*
2  * Copyright (c) 2015 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.messagebus.app.util;
10
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.ArrayList;
14 import java.util.List;
15 import java.util.regex.Pattern;
16 import org.opendaylight.yangtools.yang.common.RpcResult;
17 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
18 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
19
20 public final class Util {
21     private Util() {
22     }
23
24     public static <T> ListenableFuture<RpcResult<T>> resultRpcSuccessFor(final T output) {
25         return Futures.immediateFuture(RpcResultBuilder.success(output).build());
26     }
27
28     /**
29      * Method filters qnames based on wildcard strings.
30      *
31      * @param list list of SchemaPaths
32      * @param pattern matching pattern
33      * @return list of filtered qnames
34      */
35     public static List<SchemaPath> expandQname(final List<SchemaPath> list, final Pattern pattern) {
36         final List<SchemaPath> matchingQnames = new ArrayList<>();
37
38         for (final SchemaPath notification : list) {
39             final String namespace = notification.getLastComponent().getNamespace().toString();
40             if (pattern.matcher(namespace).matches()) {
41                 matchingQnames.add(notification);
42             }
43         }
44         return matchingQnames;
45     }
46
47     /**
48      * CREDIT to http://www.rgagnon.com/javadetails/java-0515.html.
49      */
50     public static String wildcardToRegex(final String wildcard) {
51         final StringBuilder s = new StringBuilder(wildcard.length());
52         s.append('^');
53         for (final char c : wildcard.toCharArray()) {
54             switch (c) {
55                 case '*':
56                     s.append(".*");
57                     break;
58                 case '?':
59                     s.append('.');
60                     break;
61                 // escape special regexp-characters
62                 case '(':
63                 case ')':
64                 case '[':
65                 case ']':
66                 case '$':
67                 case '^':
68                 case '.':
69                 case '{':
70                 case '}':
71                 case '|':
72                 case '\\':
73                     s.append("\\");
74                     s.append(c);
75                     break;
76                 default:
77                     s.append(c);
78                     break;
79             }
80         }
81         s.append('$');
82         return s.toString();
83     }
84 }