janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Laboratory |
| 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.drivers.huawei; |
| 18 | |
| 19 | import org.onosproject.yang.gen.v1.ne.l3vpn.api.rev20141225.nel3vpnapi.DefaultDevices; |
| 20 | import org.onosproject.yang.model.AtomicPath; |
| 21 | import org.onosproject.yang.model.DefaultModelObjectData; |
| 22 | import org.onosproject.yang.model.InnerModelObject; |
| 23 | import org.onosproject.yang.model.ModelObject; |
| 24 | import org.onosproject.yang.model.ModelObjectData; |
| 25 | import org.onosproject.yang.model.ModelObjectId; |
| 26 | import org.onosproject.yang.model.MultiInstanceNode; |
| 27 | |
| 28 | import java.util.Iterator; |
| 29 | import java.util.List; |
| 30 | |
| 31 | /** |
| 32 | * Representation of utility for huawei driver. |
| 33 | */ |
| 34 | public final class DriverUtil { |
| 35 | |
| 36 | /** |
| 37 | * Static constant value for devices. |
| 38 | */ |
| 39 | static final String CONS_DEVICES = "Devices"; |
| 40 | |
| 41 | /** |
| 42 | * Error message for YANG model registry service not found. |
| 43 | */ |
| 44 | static final String SERVICE_NOT_FOUND = "Service required for huawei " + |
| 45 | "driver is not found."; |
| 46 | |
| 47 | /** |
| 48 | * Error message for object from the standard device model being null. |
| 49 | */ |
| 50 | static final String OBJECT_NULL = "Object from device model cannot be null"; |
| 51 | |
| 52 | /** |
| 53 | * Error message for device object under devices from the standard model |
| 54 | * being null. |
| 55 | */ |
| 56 | static final String DEVICE_NULL = "Device object from the devices of " + |
| 57 | "standard device model cannot be null"; |
| 58 | |
| 59 | /** |
| 60 | * Error message for device object under devices from the standard model |
| 61 | * being null. |
| 62 | */ |
| 63 | static final String INS_NULL = "Instance object from the instances of " + |
| 64 | "standard device model cannot be null"; |
| 65 | |
| 66 | /** |
| 67 | * Error message for unsupported model id level. |
| 68 | */ |
| 69 | static final String UNSUPPORTED_MODEL_LVL = "The model id level is not " + |
| 70 | "supported"; |
| 71 | |
| 72 | /** |
| 73 | * Error message for failure of device info retrieval. |
| 74 | */ |
| 75 | static final String DEV_INFO_FAILURE = "Failed to retrieve device info."; |
| 76 | |
| 77 | /** |
| 78 | * Error message for failure of interface info retrieval. |
| 79 | */ |
| 80 | static final String INT_INFO_FAILURE = "Failed to retrieve Interfaces"; |
| 81 | |
| 82 | /** |
| 83 | * RPC message header. |
| 84 | */ |
| 85 | static final String RPC_MSG = "<rpc message-id=\"101\" xmlns=\"urn:ietf:" + |
| 86 | "params:xml:ns:netconf:base:1.0\">"; |
| 87 | |
| 88 | /** |
| 89 | * RPC get message. |
| 90 | */ |
| 91 | static final String RPC_GET = "<get>"; |
| 92 | |
| 93 | /** |
| 94 | * RPC subtree filter message. |
| 95 | */ |
| 96 | static final String RPC_FILTER = "<filter type=\"subtree\">"; |
| 97 | |
| 98 | /** |
| 99 | * RPC system message with namespace. |
| 100 | */ |
| 101 | static final String RPC_SYS = "<system xmlns=\"http://www.huawei.com/" + |
| 102 | "netconf/vrp\" format-version=\"1.0\" content-version=\"1.0\"/>"; |
| 103 | |
| 104 | /** |
| 105 | * RPC ifm message with namespace. |
| 106 | */ |
| 107 | static final String RPC_IFM = "<ifm xmlns=\"http://www.huawei.com" + |
| 108 | "/netconf/vrp\" format-version=\"1.0\" content-version=\"1.0\">"; |
| 109 | |
| 110 | /** |
| 111 | * RPC interfaces message. |
| 112 | */ |
| 113 | static final String RPC_IFS = "<interfaces/>"; |
| 114 | |
| 115 | /** |
| 116 | * RPC ifm message. |
| 117 | */ |
| 118 | static final String RPC_CLOSE_IFM = "</ifm>"; |
| 119 | |
| 120 | /** |
| 121 | * RPC filter close message. |
| 122 | */ |
| 123 | static final String RPC_CLOSE_FILTER = "</filter>"; |
| 124 | |
| 125 | /** |
| 126 | * RPC close message. |
| 127 | */ |
| 128 | static final String RPC_CLOSE = "</rpc>"; |
| 129 | |
| 130 | /** |
| 131 | * RPC get close message. |
| 132 | */ |
| 133 | static final String RPC_CLOSE_GET = "</get>"; |
| 134 | |
| 135 | /** |
| 136 | * Static constant for slash. |
| 137 | */ |
| 138 | static final String SLASH = "/"; |
| 139 | |
| 140 | /** |
| 141 | * Static constant for devices name. |
| 142 | */ |
| 143 | static final String DEVICES = "devices"; |
| 144 | |
| 145 | /** |
| 146 | * Static constant for devices namespace. |
| 147 | */ |
| 148 | static final String NAMESPACE = "ne-l3vpn-api"; |
| 149 | |
| 150 | /** |
| 151 | * Error message for model object id having more than two objects. |
| 152 | */ |
| 153 | private static final String MODEL_OBJ_ID_LIMIT = "The model object id " + |
| 154 | "must not have more than two objects."; |
| 155 | |
| 156 | // No instantiation. |
| 157 | private DriverUtil() { |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Returns the device id from the model object id. If model object id is |
| 162 | * not present then null is returned. If only one path is available in |
| 163 | * the list then devices value is returned. |
| 164 | * |
| 165 | * @param id model object data |
| 166 | * @return device id |
| 167 | */ |
| 168 | static String getIdFromModId(ModelObjectId id) { |
| 169 | if (id == null) { |
| 170 | return null; |
| 171 | } |
| 172 | |
| 173 | List<AtomicPath> paths = id.atomicPaths(); |
| 174 | int size = paths.size(); |
| 175 | if (size == 1) { |
| 176 | return CONS_DEVICES; |
| 177 | } else if (size == 2) { |
| 178 | return ((MultiInstanceNode) paths.get(1)).key().toString(); |
| 179 | } else { |
| 180 | throw new IllegalArgumentException(MODEL_OBJ_ID_LIMIT); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Returns the first object from the model object data. If no objects are |
| 186 | * present then it returns null. |
| 187 | * |
| 188 | * @param modData model object data |
| 189 | * @return object |
| 190 | */ |
| 191 | static Object getObjFromModData(ModelObjectData modData) { |
| 192 | List<ModelObject> obj = modData.modelObjects(); |
| 193 | Iterator<ModelObject> it = obj.iterator(); |
| 194 | if (it.hasNext()) { |
| 195 | return it.next(); |
| 196 | } |
| 197 | return null; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Returns the model object id for with the devices object added to it. |
| 202 | * |
| 203 | * @return model object id |
| 204 | */ |
| 205 | static ModelObjectId getModObjIdDriDevices() { |
| 206 | return ModelObjectId.builder().addChild(DefaultDevices.class).build(); |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Returns model object data built from the object that has to be added |
| 211 | * and the model object id. |
| 212 | * |
| 213 | * @param id model object id |
| 214 | * @param obj object |
| 215 | * @return model object data |
| 216 | */ |
| 217 | static ModelObjectData getData(ModelObjectId id, InnerModelObject obj) { |
| 218 | return DefaultModelObjectData.builder().addModelObject(obj) |
| 219 | .identifier(id).build(); |
| 220 | } |
| 221 | } |