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