hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.odtn.utils.tapi; |
| 18 | |
| 19 | import java.lang.reflect.InvocationTargetException; |
| 20 | import java.lang.reflect.Method; |
| 21 | import java.util.Map; |
| 22 | import java.util.Map.Entry; |
| 23 | import org.onosproject.yang.gen.v1.tapicommon.rev20180307.tapicommon.globalclass.DefaultName; |
| 24 | import org.onosproject.yang.gen.v1.tapicommon.rev20180307.tapicommon.globalclass.Name; |
| 25 | import org.onosproject.yang.model.ModelObject; |
| 26 | import org.slf4j.Logger; |
| 27 | |
| 28 | import static org.slf4j.LoggerFactory.getLogger; |
| 29 | |
| 30 | /** |
| 31 | * Utility methods dealing with TAPI modelObject which includes local class grouping. |
| 32 | * <p> |
Thomas Vachuska | a01ef78 | 2018-07-25 14:07:11 -0700 | [diff] [blame] | 33 | * tapi-common@2018-03-07.yang |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 34 | * grouping local-class { |
| 35 | * leaf local-id { |
| 36 | * type string; |
| 37 | * } |
| 38 | * list name { |
| 39 | * key 'value-name'; |
| 40 | * uses name-and-value; |
| 41 | * } |
| 42 | * } |
Thomas Vachuska | a01ef78 | 2018-07-25 14:07:11 -0700 | [diff] [blame] | 43 | * </p> |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 44 | * <p> |
| 45 | * grouping name-and-value { |
| 46 | * leaf value-name { |
| 47 | * type string; |
| 48 | * description "The name of the value. The value need not have a name."; |
| 49 | * } |
| 50 | * leaf value { |
| 51 | * type string; |
| 52 | * description "The value"; |
| 53 | * } |
| 54 | * description "A scoped name-value pair"; |
| 55 | * } |
| 56 | * </p> |
| 57 | */ |
| 58 | public final class TapiLocalClassUtil { |
| 59 | |
| 60 | private static final Logger log = getLogger(TapiLocalClassUtil.class); |
| 61 | |
| 62 | private TapiLocalClassUtil() { |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Set local-id for the ModelObject. |
| 67 | * |
| 68 | * @param obj ModelObject |
| 69 | * @param localId LocalId |
| 70 | * @param <T> Type of ModelObject |
| 71 | */ |
| 72 | public static <T extends ModelObject> void setLocalId(T obj, String localId) { |
| 73 | @SuppressWarnings("unchecked") |
| 74 | Class<T> cls = (Class<T>) obj.getClass(); |
| 75 | try { |
| 76 | Method method = cls.getMethod("localId", String.class); |
| 77 | method.invoke(obj, localId); |
| 78 | } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { |
| 79 | log.error("Exception thrown", e); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Get local-id for the ModelObject. |
| 85 | * |
| 86 | * @param obj ModelObject |
| 87 | * @param <T> Type of ModelObject |
| 88 | * @return Local-id |
| 89 | */ |
| 90 | public static <T extends ModelObject> String getLocalId(T obj) { |
| 91 | String localId = null; |
| 92 | @SuppressWarnings("unchecked") |
| 93 | Class<T> cls = (Class<T>) obj.getClass(); |
| 94 | try { |
| 95 | Method method = cls.getMethod("localId"); |
| 96 | localId = (String) method.invoke(obj); |
| 97 | } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { |
| 98 | log.error("Exception thrown", e); |
| 99 | } |
| 100 | |
| 101 | return localId; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Add key-value to the ModelObject as "name-and-value" list. |
| 106 | * |
| 107 | * @param obj ModelObject |
| 108 | * @param kvs Key-value map |
| 109 | * @param <T> Type of ModelObject |
| 110 | */ |
| 111 | public static <T extends ModelObject> void addNameList(T obj, Map<String, String> kvs) { |
| 112 | |
| 113 | @SuppressWarnings("unchecked") |
| 114 | Class<T> cls = (Class<T>) obj.getClass(); |
| 115 | try { |
| 116 | Method method = cls.getMethod("addToName", Name.class); |
| 117 | |
| 118 | for (Entry<String, String> kv : kvs.entrySet()) { |
| 119 | DefaultName prop = new DefaultName(); |
| 120 | prop.valueName(kv.getKey()); |
| 121 | prop.value(kv.getValue()); |
| 122 | method.invoke(obj, prop); |
| 123 | } |
| 124 | } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { |
| 125 | log.error("Exception thrown", e); |
| 126 | } |
| 127 | } |
| 128 | } |