26719592bbbe4dd7e553534a1d9fbafe89fbc097
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / util / Util.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
9 package org.opendaylight.controller.netconf.confignetconfconnector.util;
10
11 import com.google.common.base.Preconditions;
12 import org.opendaylight.controller.config.yang.store.api.YangStoreException;
13 import org.opendaylight.controller.config.yang.store.api.YangStoreService;
14 import org.opendaylight.controller.config.yang.store.api.YangStoreSnapshot;
15 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
16 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorSeverity;
17 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorTag;
18 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorType;
19
20 import java.text.ParseException;
21 import java.text.SimpleDateFormat;
22 import java.util.Date;
23
24 public final class Util {
25
26     /**
27      * Used for date <-> xml serialization
28      */
29     private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
30
31     public static String writeDate(final Date date) {
32         return dateFormat.format(date);
33     }
34
35     public static Date readDate(final String s) throws ParseException {
36         return dateFormat.parse(s);
37     }
38
39     public static void checkType(final Object value, final Class<?> clazz) {
40         Preconditions.checkArgument(clazz.isAssignableFrom(value.getClass()), "Unexpected type " + value.getClass()
41                 + " should be " + clazz);
42     }
43
44     // TODO: add message and proper error types
45     public static YangStoreSnapshot getYangStore(final YangStoreService yangStoreService)
46             throws NetconfDocumentedException {
47         try {
48             return yangStoreService.getYangStoreSnapshot();
49         } catch (final YangStoreException e) {
50             throw new NetconfDocumentedException("TODO", e, ErrorType.application, ErrorTag.bad_attribute,
51                     ErrorSeverity.error);
52         }
53     }
54
55 }