-
- private Optional<Persister.ConfigSnapshotHolder> loadLastConfig() {
- Optional<Persister.ConfigSnapshotHolder> maybeConfigElement;
- try {
- maybeConfigElement = persister.loadLastConfig();
- } catch (IOException e) {
- throw new RuntimeException("Unable to load configuration", e);
- }
- return maybeConfigElement;
- }
-
- private synchronized void pushLastConfig(Element persistedConfig) {
- StringBuilder response = new StringBuilder("editConfig response = {");
-
- Element configElement = persistedConfig;
- NetconfMessage message = createEditConfigMessage(configElement, "/netconfOp/editConfig.xml");
- NetconfMessage responseMessage = netconfClient.sendMessage(message);
-
- XmlElement element = XmlElement.fromDomDocument(responseMessage.getDocument());
- Preconditions.checkState(element.getName().equals(XmlNetconfConstants.RPC_REPLY_KEY));
- element = element.getOnlyChildElement();
-
- checkIsOk(element, responseMessage);
- response.append(XmlUtil.toString(responseMessage.getDocument()));
- response.append("}");
- responseMessage = netconfClient.sendMessage(getNetconfMessageFromResource("/netconfOp/commit.xml"));
-
- element = XmlElement.fromDomDocument(responseMessage.getDocument());
- Preconditions.checkState(element.getName().equals(XmlNetconfConstants.RPC_REPLY_KEY));
- element = element.getOnlyChildElement();
-
- checkIsOk(element, responseMessage);
- response.append("commit response = {");
- response.append(XmlUtil.toString(responseMessage.getDocument()));
- response.append("}");
- logger.debug("Last configuration loaded successfully");
- }
-
- private void checkIsOk(XmlElement element, NetconfMessage responseMessage) {
- if (element.getName().equals(XmlNetconfConstants.OK)) {
- return;
- } else {
- if (element.getName().equals(XmlNetconfConstants.RPC_ERROR)) {
- logger.warn("Can not load last configuration, operation failed");
- throw new IllegalStateException("Can not load last configuration, operation failed: "
- + XmlUtil.toString(responseMessage.getDocument()));
- }
- logger.warn("Can not load last configuration. Operation failed.");
- throw new IllegalStateException("Can not load last configuration. Operation failed: "
- + XmlUtil.toString(responseMessage.getDocument()));
- }
- }
-
- private NetconfMessage createEditConfigMessage(Element dataElement, String editConfigResourcename) {
- try (InputStream stream = getClass().getResourceAsStream(editConfigResourcename)) {
- Preconditions.checkNotNull(stream, "Unable to load resource " + editConfigResourcename);
-
- Document doc = XmlUtil.readXmlToDocument(stream);
-
- doc.getDocumentElement();
- XmlElement editConfigElement = XmlElement.fromDomDocument(doc).getOnlyChildElement();
- XmlElement configWrapper = editConfigElement.getOnlyChildElement(XmlNetconfConstants.CONFIG_KEY);
- editConfigElement.getDomElement().removeChild(configWrapper.getDomElement());
- for (XmlElement el : XmlElement.fromDomElement(dataElement).getChildElements()) {
- configWrapper.appendChild((Element) doc.importNode(el.getDomElement(), true));
- }
- editConfigElement.appendChild(configWrapper.getDomElement());
- return new NetconfMessage(doc);
- } catch (IOException | SAXException e) {
- throw new RuntimeException("Unable to parse message from resources " + editConfigResourcename, e);
- }
- }
-
- private NetconfMessage getNetconfMessageFromResource(String resource) {
- try (InputStream stream = getClass().getResourceAsStream(resource)) {
- Preconditions.checkNotNull(stream, "Unable to load resource " + resource);
- return new NetconfMessage(XmlUtil.readXmlToDocument(stream));
- } catch (SAXException | IOException e) {
- throw new RuntimeException("Unable to parse message from resources " + resource, e);
- }
- }
-
- @Override
- public synchronized void close() {
- // TODO persister is received from constructor, should not be closed
- // here
- try {
- persister.close();
- } catch (Exception e) {
- logger.warn("Unable to close config persister {}", persister, e);
- }
-
- if (netconfClient != null) {
- try {
- netconfClient.close();
- } catch (Exception e) {
- logger.warn("Unable to close connection to netconf {}", netconfClient, e);
- }
- }
-
- // unregister from JMX
- try {
- if (mbeanServer.isRegistered(on)) {
- mbeanServer.removeNotificationListener(on, this);
- }
- } catch (Exception e) {
- logger.warn("Unable to unregister {} as listener for {}", this, on, e);
- }
- }
-}