blob: a36e0487330a5f9f915b4bc4dc2ad447c15535a6 [file] [log] [blame]
janani bf7060cd2017-03-28 19:06:30 +05301/*
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
17package org.onosproject.drivers.huawei;
18
Vidyashree Rama04147ca2017-05-26 11:32:47 +053019import org.onosproject.yang.gen.v1.nel3vpnapi.rev20141225.nel3vpnapi.DefaultDevices;
20import org.onosproject.yang.gen.v1.ietfinterfaces.rev20140508.ietfinterfaces.devices.DeviceKeys;
janani bf7060cd2017-03-28 19:06:30 +053021import org.onosproject.yang.model.AtomicPath;
22import org.onosproject.yang.model.DefaultModelObjectData;
23import org.onosproject.yang.model.InnerModelObject;
janani bd821b182017-03-30 16:34:49 +053024import org.onosproject.yang.model.KeyInfo;
janani bf7060cd2017-03-28 19:06:30 +053025import org.onosproject.yang.model.ModelObject;
26import org.onosproject.yang.model.ModelObjectData;
27import org.onosproject.yang.model.ModelObjectId;
28import org.onosproject.yang.model.MultiInstanceNode;
29
30import java.util.Iterator;
31import java.util.List;
32
janani bd821b182017-03-30 16:34:49 +053033import static org.onosproject.drivers.huawei.L3VpnUtil.getDevIdFromIns;
34
janani bf7060cd2017-03-28 19:06:30 +053035/**
36 * Representation of utility for huawei driver.
37 */
38public final class DriverUtil {
39
40 /**
41 * Static constant value for devices.
42 */
43 static final String CONS_DEVICES = "Devices";
44
45 /**
46 * Error message for YANG model registry service not found.
47 */
48 static final String SERVICE_NOT_FOUND = "Service required for huawei " +
49 "driver is not found.";
50
51 /**
52 * Error message for object from the standard device model being null.
53 */
54 static final String OBJECT_NULL = "Object from device model cannot be null";
55
56 /**
57 * Error message for device object under devices from the standard model
58 * being null.
59 */
60 static final String DEVICE_NULL = "Device object from the devices of " +
61 "standard device model cannot be null";
62
63 /**
64 * Error message for device object under devices from the standard model
65 * being null.
66 */
67 static final String INS_NULL = "Instance object from the instances of " +
68 "standard device model cannot be null";
69
70 /**
71 * Error message for unsupported model id level.
72 */
73 static final String UNSUPPORTED_MODEL_LVL = "The model id level is not " +
74 "supported";
75
76 /**
77 * Error message for failure of device info retrieval.
78 */
79 static final String DEV_INFO_FAILURE = "Failed to retrieve device info.";
80
81 /**
82 * Error message for failure of interface info retrieval.
83 */
84 static final String INT_INFO_FAILURE = "Failed to retrieve Interfaces";
85
86 /**
87 * RPC message header.
88 */
89 static final String RPC_MSG = "<rpc message-id=\"101\" xmlns=\"urn:ietf:" +
90 "params:xml:ns:netconf:base:1.0\">";
91
92 /**
93 * RPC get message.
94 */
95 static final String RPC_GET = "<get>";
96
97 /**
98 * RPC subtree filter message.
99 */
100 static final String RPC_FILTER = "<filter type=\"subtree\">";
101
102 /**
103 * RPC system message with namespace.
104 */
105 static final String RPC_SYS = "<system xmlns=\"http://www.huawei.com/" +
106 "netconf/vrp\" format-version=\"1.0\" content-version=\"1.0\"/>";
107
108 /**
109 * RPC ifm message with namespace.
110 */
111 static final String RPC_IFM = "<ifm xmlns=\"http://www.huawei.com" +
112 "/netconf/vrp\" format-version=\"1.0\" content-version=\"1.0\">";
113
114 /**
115 * RPC interfaces message.
116 */
Vidyashree Ramacf117ce2017-04-14 08:54:39 +0530117 static final String RPC_IFS = "<interfaces><interface><ifPhyType>Ethernet" +
118 "</ifPhyType><ifName></ifName><ifNumber></ifNumber>" +
119 "<ifDynamicInfo></ifDynamicInfo><ifStatistics></ifStatistics>" +
120 "</interface></interfaces>";
janani bf7060cd2017-03-28 19:06:30 +0530121
122 /**
123 * RPC ifm message.
124 */
125 static final String RPC_CLOSE_IFM = "</ifm>";
126
127 /**
128 * RPC filter close message.
129 */
130 static final String RPC_CLOSE_FILTER = "</filter>";
131
132 /**
133 * RPC close message.
134 */
135 static final String RPC_CLOSE = "</rpc>";
136
137 /**
138 * RPC get close message.
139 */
140 static final String RPC_CLOSE_GET = "</get>";
141
142 /**
143 * Static constant for slash.
144 */
145 static final String SLASH = "/";
146
147 /**
148 * Static constant for devices name.
149 */
150 static final String DEVICES = "devices";
151
152 /**
153 * Static constant for devices namespace.
154 */
155 static final String NAMESPACE = "ne-l3vpn-api";
156
157 /**
158 * Error message for model object id having more than two objects.
159 */
160 private static final String MODEL_OBJ_ID_LIMIT = "The model object id " +
161 "must not have more than two objects.";
162
163 // No instantiation.
164 private DriverUtil() {
165 }
166
167 /**
168 * Returns the device id from the model object id. If model object id is
169 * not present then null is returned. If only one path is available in
170 * the list then devices value is returned.
171 *
janani bd821b182017-03-30 16:34:49 +0530172 * @param id model object id
173 * @param isIns if for VPN instance
janani bf7060cd2017-03-28 19:06:30 +0530174 * @return device id
175 */
janani bd821b182017-03-30 16:34:49 +0530176 static String getIdFromModId(ModelObjectId id, boolean isIns) {
janani bf7060cd2017-03-28 19:06:30 +0530177 if (id == null) {
178 return null;
179 }
janani bf7060cd2017-03-28 19:06:30 +0530180 List<AtomicPath> paths = id.atomicPaths();
181 int size = paths.size();
janani bd821b182017-03-30 16:34:49 +0530182
183 switch (size) {
184 case 1:
185 return CONS_DEVICES;
186
187 case 2:
188 return getDevId(paths, isIns);
189
190 default:
191 throw new IllegalArgumentException(MODEL_OBJ_ID_LIMIT);
janani bf7060cd2017-03-28 19:06:30 +0530192 }
193 }
194
195 /**
janani bd821b182017-03-30 16:34:49 +0530196 * Returns the device id from the model object id's key info.
197 *
198 * @param paths atomic path list
199 * @param isIns if VPN instance
200 * @return device id
201 */
202 private static String getDevId(List<AtomicPath> paths, boolean isIns) {
203 MultiInstanceNode info = (MultiInstanceNode) paths.get(1);
204 KeyInfo key = info.key();
205 if (!isIns) {
206 return ((DeviceKeys) key).deviceid();
207 }
208 return getDevIdFromIns(key);
209 }
210
211 /**
janani bf7060cd2017-03-28 19:06:30 +0530212 * Returns the first object from the model object data. If no objects are
213 * present then it returns null.
214 *
215 * @param modData model object data
216 * @return object
217 */
218 static Object getObjFromModData(ModelObjectData modData) {
219 List<ModelObject> obj = modData.modelObjects();
220 Iterator<ModelObject> it = obj.iterator();
221 if (it.hasNext()) {
222 return it.next();
223 }
224 return null;
225 }
226
227 /**
228 * Returns the model object id for with the devices object added to it.
229 *
230 * @return model object id
231 */
232 static ModelObjectId getModObjIdDriDevices() {
233 return ModelObjectId.builder().addChild(DefaultDevices.class).build();
234 }
235
236 /**
237 * Returns model object data built from the object that has to be added
238 * and the model object id.
239 *
240 * @param id model object id
241 * @param obj object
242 * @return model object data
243 */
244 static ModelObjectData getData(ModelObjectId id, InnerModelObject obj) {
245 return DefaultModelObjectData.builder().addModelObject(obj)
246 .identifier(id).build();
247 }
248}