Separating renderers into features.
[groupbasedpolicy.git] / renderers / oc / src / main / java / org / opendaylight / groupbasedpolicy / renderer / oc / Utils.java
1 /*
2  * Copyright (c) 2015 Juniper Networks, Inc.  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.groupbasedpolicy.renderer.oc;
10
11 import java.util.regex.Pattern;
12
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15
16 public class Utils {
17
18     static final Logger LOGGER = LoggerFactory.getLogger(Utils.class);
19
20     /**
21      * Invoked to format the UUID if UUID is not in correct format.
22      *
23      * @param String
24      *            An instance of UUID string.
25      *
26      * @return Correctly formated UUID string.
27      */
28     public static String uuidFormater(String uuid) {
29         String uuidPattern = null;
30         String id1 = uuid.substring(0, 8);
31         String id2 = uuid.substring(8, 12);
32         String id3 = uuid.substring(12, 16);
33         String id4 = uuid.substring(16, 20);
34         String id5 = uuid.substring(20, 32);
35         uuidPattern = (id1 + "-" + id2 + "-" + id3 + "-" + id4 + "-" + id5);
36         return uuidPattern;
37     }
38
39     /**
40      * Invoked to check the UUID if UUID is not a valid hexa-decimal number.
41      *
42      * @param String
43      *            An instance of UUID string.
44      *
45      * @return boolean value.
46      */
47     public static boolean isValidHexNumber(String uuid) {
48         try {
49             Pattern hex = Pattern.compile("^[0-9a-f]+$");
50             uuid = uuid.replaceAll("-", "");
51             boolean valid = hex.matcher(uuid).matches();
52             if (uuid.length() != 32) {
53                 return false;
54             }
55             if (valid) {
56                 return true;
57             } else {
58                 return false;
59             }
60         } catch (NumberFormatException ex) {
61             LOGGER.error("Exception :  " + ex);
62             return false;
63         }
64     }
65
66     /**
67      * Invoked to format the UUID/Name in correct format.
68      *
69      * @param String
70      *            An instance of UUID/Name string.
71      *
72      * @return Correctly formated UUID/name string.
73      */
74
75     public static String uuidNameFormat(String value){
76         String[] pattern = value.split("=");
77         value = pattern[1].replace("]", "");
78         return value;
79     }
80 }