blob: f5223e2a413f33971ae11d7e2de1d3e6bfda6ce3 [file] [log] [blame]
MaoLu819fde22017-04-20 17:17:49 -07001/*
2 * Copyright 2016 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.oplink;
18
MaoLu3308ca42017-05-10 20:18:54 -070019import com.google.common.collect.ImmutableList;
MaoLu819fde22017-04-20 17:17:49 -070020import org.apache.commons.configuration.HierarchicalConfiguration;
21import org.onosproject.drivers.utilities.XmlConfigParser;
22import org.onosproject.net.driver.DriverHandler;
MaoLu3308ca42017-05-10 20:18:54 -070023import org.onosproject.netconf.DatastoreId;
MaoLu819fde22017-04-20 17:17:49 -070024import org.onosproject.netconf.NetconfController;
25import org.onosproject.netconf.NetconfException;
26import org.onosproject.netconf.NetconfSession;
27
28import java.io.IOException;
MaoLu3308ca42017-05-10 20:18:54 -070029import java.util.List;
MaoLu819fde22017-04-20 17:17:49 -070030
31import static com.google.common.base.Preconditions.checkNotNull;
32
33/**
34 * Netconf utility for oplink netconf drivers.
35 */
36public final class OplinkNetconfUtility {
37
38 // public used nodes
MaoLu819fde22017-04-20 17:17:49 -070039 public static final String CFG_MODE_MERGE = "merge";
40 public static final String CFG_MODE_REMOVE = "remove";
41 public static final String KEY_XMLNS = "xmlns=\"http://com/att/device\"";
42 public static final String KEY_DATA = "data";
43 public static final String KEY_OPENOPTICALDEV = "open-optical-device";
44 public static final String KEY_CONNS = "connections";
45 public static final String KEY_CONNID = "connection-id";
46 public static final String KEY_PORTS = "ports";
47 public static final String KEY_PORTID = "port-id";
48 public static final String KEY_PORT = "port";
49 public static final String KEY_PORTDIRECT = "port-direction";
50 public static final String KEY_CHATT = "attenuation";
51 public static final String KEY_DATA_CONNS = String.format("%s.%s.%s", KEY_DATA, KEY_OPENOPTICALDEV, KEY_CONNS);
52 public static final String KEY_DATA_PORTS = String.format("%s.%s.%s", KEY_DATA, KEY_OPENOPTICALDEV, KEY_PORTS);
53 public static final String KEY_OPENOPTICALDEV_XMLNS = String.format("%s %s", KEY_OPENOPTICALDEV, KEY_XMLNS);
54
55 private OplinkNetconfUtility() {
56 }
57
58 /**
MaoLu3308ca42017-05-10 20:18:54 -070059 * Retrieves session reply information for get operation.
60 *
61 * @param handler parent driver handler
62 * @param filter the filter string of xml content
63 * @return the reply string
64 */
65 public static String netconfGet(DriverHandler handler, String filter) {
66 NetconfController controller = checkNotNull(handler.get(NetconfController.class));
67 NetconfSession session = controller.getNetconfDevice(handler.data().deviceId()).getSession();
68 String reply;
69 try {
70 reply = session.get(filter, null);
71 } catch (IOException e) {
72 throw new RuntimeException(new NetconfException("Failed to retrieve configuration.", e));
73 }
74 return reply;
75 }
76
77 /**
MaoLu819fde22017-04-20 17:17:49 -070078 * Retrieves session reply information for get config operation.
79 *
80 * @param handler parent driver handler
81 * @param filter the filter string of xml content
82 * @return the reply string
83 */
84 public static String netconfGetConfig(DriverHandler handler, String filter) {
85 NetconfController controller = checkNotNull(handler.get(NetconfController.class));
86 NetconfSession session = controller.getNetconfDevice(handler.data().deviceId()).getSession();
87 String reply;
88 try {
MaoLu3308ca42017-05-10 20:18:54 -070089 reply = session.getConfig(DatastoreId.RUNNING, filter);
MaoLu819fde22017-04-20 17:17:49 -070090 } catch (IOException e) {
91 throw new RuntimeException(new NetconfException("Failed to retrieve configuration.", e));
92 }
93 return reply;
94 }
95
96 /**
97 * Retrieves session reply information for edit config operation.
98 *
99 * @param handler parent driver handler
100 * @param mode selected mode to change the configuration
101 * @param cfg the new configuration to be set
102 * @return the reply string
103 */
104 public static boolean netconfEditConfig(DriverHandler handler, String mode, String cfg) {
105 NetconfController controller = checkNotNull(handler.get(NetconfController.class));
106 NetconfSession session = controller.getNetconfDevice(handler.data().deviceId()).getSession();
107 boolean reply = false;
108 try {
MaoLu3308ca42017-05-10 20:18:54 -0700109 reply = session.editConfig(DatastoreId.RUNNING, mode, cfg);
MaoLu819fde22017-04-20 17:17:49 -0700110 } catch (IOException e) {
111 throw new RuntimeException(new NetconfException("Failed to edit configuration.", e));
112 }
113 return reply;
114 }
115
116 /**
117 * Retrieves specified node hierarchical configuration from the xml information.
118 *
119 * @param content the xml information
120 * @param key the configuration key node
MaoLu3308ca42017-05-10 20:18:54 -0700121 * @return the hierarchical configuration, null if exception happens
MaoLu819fde22017-04-20 17:17:49 -0700122 */
123 public static HierarchicalConfiguration configAt(String content, String key) {
MaoLu819fde22017-04-20 17:17:49 -0700124 HierarchicalConfiguration info;
125 try {
MaoLu3308ca42017-05-10 20:18:54 -0700126 HierarchicalConfiguration cfg = XmlConfigParser.loadXmlString(content);
MaoLu819fde22017-04-20 17:17:49 -0700127 info = cfg.configurationAt(key);
128 } catch (Exception e) {
129 // Accept null for information polling
130 return null;
131 }
132 return info;
133 }
134
135 /**
MaoLu3308ca42017-05-10 20:18:54 -0700136 * Retrieves specified node hierarchical configurations from the xml information.
137 *
138 * @param content the xml information
139 * @param key the configuration key node
140 * @return the hierarchical configurations, empty if exception happens
141 */
142 public static List<HierarchicalConfiguration> configsAt(String content, String key) {
143 List<HierarchicalConfiguration> info;
144 try {
145 HierarchicalConfiguration cfg = XmlConfigParser.loadXmlString(content);
146 info = cfg.configurationsAt(key);
147 } catch (Exception e) {
148 // Accept empty for information polling
149 return ImmutableList.of();
150 }
151 return info;
152 }
153
154 /**
MaoLu819fde22017-04-20 17:17:49 -0700155 * Makes a xml format sentence.
156 *
157 * @param node the node name
158 * @param content the node content
159 * @return the xml format sentence
160 */
161 public static String xml(String node, String content) {
162 return String.format("<%s>%s</%s>", node, content, node);
163 }
164
165 /**
166 * Makes a xml format open tag.
167 *
168 * @param node the node name
169 * @return the xml head format string
170 */
171 public static String xmlOpen(String node) {
172 return String.format("<%s>", node);
173 }
174
175 /**
176 * Makes a xml format close tag.
177 *
178 * @param node the node name
179 * @return the xml end format string
180 */
181 public static String xmlClose(String node) {
182 return String.format("</%s>", node);
183 }
184
185 /**
186 * Makes a xml format empty tag.
187 *
188 * @param node the node name
189 * @return the xml format of empty tag
190 */
191 public static String xmlEmpty(String node) {
192 return String.format("<%s/>", node);
193 }
194}