Sean Condon | fae8e66 | 2016-12-15 10:25:13 +0000 | [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 | package org.onosproject.drivers.microsemi.yang.impl; |
| 17 | |
| 18 | import static org.onosproject.yms.ych.YangProtocolEncodingFormat.XML; |
| 19 | import static org.onosproject.yms.ydt.YmsOperationType.QUERY_REPLY; |
| 20 | |
| 21 | import java.time.OffsetDateTime; |
| 22 | import java.time.format.DateTimeFormatter; |
| 23 | import java.util.List; |
| 24 | import java.util.regex.Pattern; |
| 25 | |
| 26 | import org.apache.felix.scr.annotations.Activate; |
| 27 | import org.apache.felix.scr.annotations.Component; |
| 28 | import org.apache.felix.scr.annotations.Deactivate; |
| 29 | import org.apache.felix.scr.annotations.Service; |
| 30 | import org.onosproject.drivers.microsemi.yang.IetfSystemNetconfService; |
| 31 | import org.onosproject.netconf.NetconfException; |
| 32 | import org.onosproject.netconf.NetconfSession; |
| 33 | import org.onosproject.netconf.TargetConfig; |
| 34 | import org.onosproject.yang.gen.v1.http.www.microsemi.com.microsemi.edge.assure.msea.system.rev20160505.IetfSystemMicrosemiService; |
| 35 | import org.onosproject.yang.gen.v1.http.www.microsemi.com.microsemi.edge.assure.msea.system.rev20160505.ietfsystemmicrosemi.doupgradeandreboot.DoUpgradeAndRebootInput; |
| 36 | import org.onosproject.yang.gen.v1.http.www.microsemi.com.microsemi.edge.assure.msea.system.rev20160505.ietfsystemmicrosemi.doupgradeandreboot.DoUpgradeAndRebootOutput; |
| 37 | import org.onosproject.yang.gen.v1.http.www.microsemi.com.microsemi.edge.assure.msea.system.rev20160505.ietfsystemmicrosemi.pullupdatetarfromtftp.PullUpdateTarFromTftpInput; |
| 38 | import org.onosproject.yang.gen.v1.http.www.microsemi.com.microsemi.edge.assure.msea.system.rev20160505.ietfsystemmicrosemi.readfromsyslog.ReadFromSyslogInput; |
| 39 | import org.onosproject.yang.gen.v1.http.www.microsemi.com.microsemi.edge.assure.msea.system.rev20160505.ietfsystemmicrosemi.readfromsyslog.ReadFromSyslogOutput; |
| 40 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.system.rev20140806.IetfSystem; |
| 41 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.system.rev20140806.IetfSystemOpParam; |
| 42 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.system.rev20140806.IetfSystemService; |
| 43 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.system.rev20140806.ietfsystem.systemrestart.SystemRestartInput; |
| 44 | |
| 45 | /** |
| 46 | * Implementation of the IetfService YANG model service. |
| 47 | */ |
| 48 | @Component(immediate = true, inherit = true) |
| 49 | @Service |
| 50 | public class IetfSystemManager extends AbstractYangServiceImpl |
| 51 | implements IetfSystemNetconfService { |
| 52 | |
| 53 | protected final Pattern regexRemoveSystem = |
| 54 | Pattern.compile("(<system).*(</system>)", Pattern.DOTALL); |
| 55 | protected final Pattern regexRemoveSystemState = |
| 56 | Pattern.compile("(<system-state).*(</system-state>)", Pattern.DOTALL); |
| 57 | |
| 58 | protected static final String IETF_SYSTEM = "org.onosproject.drivers.microsemi.yang.ietfsystem"; |
| 59 | |
| 60 | @Activate |
| 61 | public void activate() { |
| 62 | appId = coreService.registerApplication(IETF_SYSTEM); |
| 63 | ych = ymsService.getYangCodecHandler(); |
| 64 | ych.addDeviceSchema(IetfSystemService.class); |
| 65 | ych.addDeviceSchema(IetfSystemMicrosemiService.class); |
| 66 | log.info("IetfSystemManager Started"); |
| 67 | } |
| 68 | |
| 69 | @Deactivate |
| 70 | public void deactivate() { |
| 71 | super.deactivate(); |
| 72 | ymsService.unRegisterService(this, IetfSystemService.class); |
| 73 | ymsService.unRegisterService(this, IetfSystemMicrosemiService.class); |
| 74 | ych = null; |
| 75 | log.info("IetfSystemManager Stopped"); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Get a filtered subset of the model. |
| 80 | * This is meant to filter the current live model |
| 81 | * against the attribute(s) given in the argument |
| 82 | * and return the filtered model. |
| 83 | * @throws NetconfException if the session has any error |
| 84 | */ |
| 85 | @Override |
| 86 | public IetfSystem getIetfSystem(IetfSystemOpParam ietfSystemFilter, NetconfSession session) |
| 87 | throws NetconfException { |
| 88 | return (IetfSystem) getNetconfObject(ietfSystemFilter, session); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public IetfSystem getIetfSystemInit(NetconfSession session) throws NetconfException { |
| 93 | if (session == null) { |
| 94 | throw new NetconfException("Session is null when calling getIetfSystemInit()"); |
| 95 | } |
| 96 | |
| 97 | String xmlResult = session.get(getInitRequestBuilder(), null); |
| 98 | |
| 99 | //The result will be a <system> followed by <system-state> |
| 100 | //YCH can decode only one at a time - split it and send half each time |
| 101 | IetfSystem.IetfSystemBuilder iBuilder = new IetfSystemOpParam.IetfSystemBuilder(); |
| 102 | |
| 103 | String xmlResultSystem = regexRemoveSystemState.matcher(xmlResult).replaceFirst(""); |
| 104 | List<Object> objectListState = ych.decode(xmlResultSystem, XML, QUERY_REPLY); |
| 105 | if (objectListState != null && objectListState.size() > 0) { |
| 106 | IetfSystem system = (IetfSystem) objectListState.get(0); |
| 107 | iBuilder.system(system.system()); |
| 108 | } |
| 109 | |
| 110 | String xmlResultSystemState = regexRemoveSystem.matcher(xmlResult).replaceFirst(""); |
| 111 | List<Object> objectListSystemState = ych.decode(xmlResultSystemState, XML, QUERY_REPLY); |
| 112 | if (objectListSystemState != null && objectListSystemState.size() > 0) { |
| 113 | IetfSystem system = (IetfSystem) objectListSystemState.get(0); |
| 114 | iBuilder.systemState(system.systemState()); |
| 115 | } |
| 116 | |
| 117 | return iBuilder.build(); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Call NETCONF edit-config with a configuration. |
| 122 | */ |
| 123 | @Override |
| 124 | public void setIetfSystem(IetfSystemOpParam ietfSystem, NetconfSession session, TargetConfig ncDs) |
| 125 | throws NetconfException { |
| 126 | setNetconfObject(ietfSystem, session, ncDs); |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public void setCurrentDatetime(OffsetDateTime date, NetconfSession session) |
| 131 | throws NetconfException { |
| 132 | String xmlQueryStr = getSetCurrentDatetimeBuilder(date); |
| 133 | log.info("Sending <get> query on NETCONF session " + session.getSessionId() + |
| 134 | ":\n" + xmlQueryStr); |
| 135 | |
| 136 | String xmlResult = session.doWrappedRpc(xmlQueryStr); |
| 137 | log.info("Result from NETCONF RPC <set-current-datetime>: {}", xmlResult); |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public void systemRestart(SystemRestartInput inputVar, NetconfSession session) { |
| 142 | throw new UnsupportedOperationException("Not yet implemented"); |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public void systemShutdown(NetconfSession session) { |
| 147 | throw new UnsupportedOperationException("Not yet implemented"); |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public DoUpgradeAndRebootOutput doUpgradeAndReboot(DoUpgradeAndRebootInput inputVar, NetconfSession session) |
| 152 | throws NetconfException { |
| 153 | throw new UnsupportedOperationException("Not yet implemented"); |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public void pullUpdateTarFromTftp(PullUpdateTarFromTftpInput inputVar, NetconfSession session) |
| 158 | throws NetconfException { |
| 159 | throw new UnsupportedOperationException("Not yet implemented"); |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public ReadFromSyslogOutput readFromSyslog(ReadFromSyslogInput inputVar, NetconfSession session) |
| 164 | throws NetconfException { |
| 165 | throw new UnsupportedOperationException("Not yet implemented"); |
| 166 | } |
| 167 | |
| 168 | |
| 169 | /** |
| 170 | * Builds a request crafted to get the configuration required to create |
| 171 | * details descriptions for the device. |
| 172 | * |
| 173 | * @return The request string. |
| 174 | */ |
| 175 | private static String getInitRequestBuilder() { |
| 176 | StringBuilder rpc = new StringBuilder(); |
| 177 | rpc.append("<system-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-system\" "); |
| 178 | rpc.append("xmlns:sysms=\"http://www.microsemi.com/microsemi-edge-assure/msea-system\">"); |
| 179 | rpc.append("<platform>"); |
| 180 | rpc.append("<os-release/>"); |
| 181 | rpc.append("<sysms:device-identification>"); |
| 182 | rpc.append("<sysms:serial-number/>"); |
| 183 | rpc.append("</sysms:device-identification>"); |
| 184 | rpc.append("</platform>"); |
| 185 | rpc.append("<clock>"); |
| 186 | rpc.append("<current-datetime/>"); |
| 187 | rpc.append("</clock>"); |
| 188 | rpc.append("</system-state>"); |
| 189 | rpc.append("<system xmlns=\"urn:ietf:params:xml:ns:yang:ietf-system\" "); |
| 190 | rpc.append("xmlns:sysms=\"http://www.microsemi.com/microsemi-edge-assure/msea-system\">"); |
| 191 | rpc.append("<sysms:longitude/>"); |
| 192 | rpc.append("<sysms:latitude/>"); |
| 193 | rpc.append("</system>"); |
| 194 | return rpc.toString(); |
| 195 | } |
| 196 | |
| 197 | private static String getSetCurrentDatetimeBuilder(OffsetDateTime date) { |
| 198 | StringBuilder rpc = new StringBuilder(); |
| 199 | rpc.append("<set-current-datetime xmlns=\"urn:ietf:params:xml:ns:yang:ietf-system\">"); |
| 200 | rpc.append("<current-datetime>"); |
| 201 | rpc.append(date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssxxx"))); |
| 202 | rpc.append("</current-datetime>"); |
| 203 | rpc.append("</set-current-datetime>"); |
| 204 | |
| 205 | return rpc.toString(); |
| 206 | } |
| 207 | } |