Sean Condon | fae8e66 | 2016-12-15 10:25:13 +0000 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Sean Condon | fae8e66 | 2016-12-15 10:25:13 +0000 | [diff] [blame] | 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 | |
Sean Condon | 06613e9 | 2017-06-09 15:14:01 +0100 | [diff] [blame] | 18 | import java.io.ByteArrayInputStream; |
Sean Condon | fae8e66 | 2016-12-15 10:25:13 +0000 | [diff] [blame] | 19 | import java.time.OffsetDateTime; |
| 20 | import java.time.format.DateTimeFormatter; |
Sean Condon | fae8e66 | 2016-12-15 10:25:13 +0000 | [diff] [blame] | 21 | |
| 22 | import org.apache.felix.scr.annotations.Activate; |
| 23 | import org.apache.felix.scr.annotations.Component; |
| 24 | import org.apache.felix.scr.annotations.Deactivate; |
| 25 | import org.apache.felix.scr.annotations.Service; |
| 26 | import org.onosproject.drivers.microsemi.yang.IetfSystemNetconfService; |
Sean Condon | 06613e9 | 2017-06-09 15:14:01 +0100 | [diff] [blame] | 27 | import org.onosproject.netconf.DatastoreId; |
Sean Condon | fae8e66 | 2016-12-15 10:25:13 +0000 | [diff] [blame] | 28 | import org.onosproject.netconf.NetconfException; |
| 29 | import org.onosproject.netconf.NetconfSession; |
Sean Condon | 06613e9 | 2017-06-09 15:14:01 +0100 | [diff] [blame] | 30 | import org.onosproject.yang.gen.v1.ietfsystem.rev20140806.ietfsystem.DefaultSystem; |
| 31 | import org.onosproject.yang.gen.v1.ietfsystem.rev20140806.ietfsystem.DefaultSystemState; |
Sean Condon | 06613e9 | 2017-06-09 15:14:01 +0100 | [diff] [blame] | 32 | import org.onosproject.yang.gen.v1.ietfsystem.rev20140806.IetfSystem; |
| 33 | import org.onosproject.yang.gen.v1.ietfsystem.rev20140806.IetfSystemOpParam; |
Sean Condon | 06613e9 | 2017-06-09 15:14:01 +0100 | [diff] [blame] | 34 | import org.onosproject.yang.model.DefaultModelObjectData; |
| 35 | import org.onosproject.yang.model.ModelConverter; |
| 36 | import org.onosproject.yang.model.ModelObject; |
| 37 | import org.onosproject.yang.model.ModelObjectData; |
| 38 | import org.onosproject.yang.runtime.CompositeData; |
| 39 | import org.onosproject.yang.runtime.DefaultCompositeStream; |
Sean Condon | fae8e66 | 2016-12-15 10:25:13 +0000 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Implementation of the IetfService YANG model service. |
| 43 | */ |
| 44 | @Component(immediate = true, inherit = true) |
| 45 | @Service |
| 46 | public class IetfSystemManager extends AbstractYangServiceImpl |
| 47 | implements IetfSystemNetconfService { |
| 48 | |
Sean Condon | fae8e66 | 2016-12-15 10:25:13 +0000 | [diff] [blame] | 49 | protected static final String IETF_SYSTEM = "org.onosproject.drivers.microsemi.yang.ietfsystem"; |
| 50 | |
| 51 | @Activate |
| 52 | public void activate() { |
Sean Condon | 06613e9 | 2017-06-09 15:14:01 +0100 | [diff] [blame] | 53 | super.activate(); |
Sean Condon | fae8e66 | 2016-12-15 10:25:13 +0000 | [diff] [blame] | 54 | appId = coreService.registerApplication(IETF_SYSTEM); |
Sean Condon | fae8e66 | 2016-12-15 10:25:13 +0000 | [diff] [blame] | 55 | log.info("IetfSystemManager Started"); |
| 56 | } |
| 57 | |
| 58 | @Deactivate |
| 59 | public void deactivate() { |
| 60 | super.deactivate(); |
Sean Condon | fae8e66 | 2016-12-15 10:25:13 +0000 | [diff] [blame] | 61 | log.info("IetfSystemManager Stopped"); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Get a filtered subset of the model. |
| 66 | * This is meant to filter the current live model |
| 67 | * against the attribute(s) given in the argument |
| 68 | * and return the filtered model. |
| 69 | * @throws NetconfException if the session has any error |
| 70 | */ |
| 71 | @Override |
| 72 | public IetfSystem getIetfSystem(IetfSystemOpParam ietfSystemFilter, NetconfSession session) |
| 73 | throws NetconfException { |
Sean Condon | 06613e9 | 2017-06-09 15:14:01 +0100 | [diff] [blame] | 74 | |
| 75 | ModelObjectData moQuery = DefaultModelObjectData.builder() |
| 76 | .addModelObject((ModelObject) ietfSystemFilter.system()) |
| 77 | .build(); |
| 78 | |
| 79 | ModelObjectData moReply = getNetconfObject(moQuery, session); |
| 80 | |
| 81 | IetfSystemOpParam ietfSystem = new IetfSystemOpParam(); |
| 82 | for (ModelObject mo:moReply.modelObjects()) { |
| 83 | if (mo instanceof DefaultSystem) { |
| 84 | ietfSystem.system((DefaultSystem) mo); |
| 85 | } else if (mo instanceof DefaultSystemState) { |
| 86 | ietfSystem.systemState((DefaultSystemState) mo); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | return ietfSystem; |
Sean Condon | fae8e66 | 2016-12-15 10:25:13 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public IetfSystem getIetfSystemInit(NetconfSession session) throws NetconfException { |
| 95 | if (session == null) { |
| 96 | throw new NetconfException("Session is null when calling getIetfSystemInit()"); |
| 97 | } |
| 98 | |
| 99 | String xmlResult = session.get(getInitRequestBuilder(), null); |
| 100 | |
Sean Condon | 06613e9 | 2017-06-09 15:14:01 +0100 | [diff] [blame] | 101 | xmlResult = removeRpcReplyData(xmlResult); |
| 102 | DefaultCompositeStream resultDcs = new DefaultCompositeStream( |
| 103 | null, new ByteArrayInputStream(xmlResult.getBytes())); |
| 104 | CompositeData compositeData = xSer.decode(resultDcs, yCtx); |
Sean Condon | fae8e66 | 2016-12-15 10:25:13 +0000 | [diff] [blame] | 105 | |
Sean Condon | 06613e9 | 2017-06-09 15:14:01 +0100 | [diff] [blame] | 106 | ModelObjectData mod = ((ModelConverter) yangModelRegistry).createModel(compositeData.resourceData()); |
| 107 | |
| 108 | IetfSystemOpParam ietfSystem = new IetfSystemOpParam(); |
| 109 | for (ModelObject mo:mod.modelObjects()) { |
| 110 | if (mo instanceof DefaultSystem) { |
| 111 | ietfSystem.system((DefaultSystem) mo); |
| 112 | } else if (mo instanceof DefaultSystemState) { |
| 113 | ietfSystem.systemState((DefaultSystemState) mo); |
| 114 | } |
Sean Condon | fae8e66 | 2016-12-15 10:25:13 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Sean Condon | 06613e9 | 2017-06-09 15:14:01 +0100 | [diff] [blame] | 117 | return ietfSystem; |
Sean Condon | fae8e66 | 2016-12-15 10:25:13 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Call NETCONF edit-config with a configuration. |
| 122 | */ |
| 123 | @Override |
Sean Condon | 06613e9 | 2017-06-09 15:14:01 +0100 | [diff] [blame] | 124 | public boolean setIetfSystem(IetfSystemOpParam ietfSystem, NetconfSession session, |
| 125 | DatastoreId ncDs) throws NetconfException { |
| 126 | ModelObjectData mo = DefaultModelObjectData.builder() |
| 127 | .addModelObject(ietfSystem).build(); |
| 128 | return setNetconfObject(mo, session, ncDs, null); |
Sean Condon | fae8e66 | 2016-12-15 10:25:13 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public void setCurrentDatetime(OffsetDateTime date, NetconfSession session) |
| 133 | throws NetconfException { |
| 134 | String xmlQueryStr = getSetCurrentDatetimeBuilder(date); |
| 135 | log.info("Sending <get> query on NETCONF session " + session.getSessionId() + |
| 136 | ":\n" + xmlQueryStr); |
| 137 | |
| 138 | String xmlResult = session.doWrappedRpc(xmlQueryStr); |
| 139 | log.info("Result from NETCONF RPC <set-current-datetime>: {}", xmlResult); |
| 140 | } |
| 141 | |
| 142 | @Override |
Sean Condon | fae8e66 | 2016-12-15 10:25:13 +0000 | [diff] [blame] | 143 | public void systemShutdown(NetconfSession session) { |
| 144 | throw new UnsupportedOperationException("Not yet implemented"); |
| 145 | } |
| 146 | |
Sean Condon | fae8e66 | 2016-12-15 10:25:13 +0000 | [diff] [blame] | 147 | /** |
| 148 | * Builds a request crafted to get the configuration required to create |
| 149 | * details descriptions for the device. |
| 150 | * |
| 151 | * @return The request string. |
| 152 | */ |
| 153 | private static String getInitRequestBuilder() { |
| 154 | StringBuilder rpc = new StringBuilder(); |
| 155 | rpc.append("<system-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-system\" "); |
| 156 | rpc.append("xmlns:sysms=\"http://www.microsemi.com/microsemi-edge-assure/msea-system\">"); |
| 157 | rpc.append("<platform>"); |
| 158 | rpc.append("<os-release/>"); |
Sean Condon | cf2751f | 2017-11-28 17:29:30 +0000 | [diff] [blame^] | 159 | rpc.append("<sysms:device-identification>"); |
| 160 | rpc.append("<sysms:serial-number/>"); |
| 161 | rpc.append("</sysms:device-identification>"); |
Sean Condon | fae8e66 | 2016-12-15 10:25:13 +0000 | [diff] [blame] | 162 | rpc.append("</platform>"); |
| 163 | rpc.append("<clock>"); |
| 164 | rpc.append("<current-datetime/>"); |
| 165 | rpc.append("</clock>"); |
| 166 | rpc.append("</system-state>"); |
| 167 | rpc.append("<system xmlns=\"urn:ietf:params:xml:ns:yang:ietf-system\" "); |
| 168 | rpc.append("xmlns:sysms=\"http://www.microsemi.com/microsemi-edge-assure/msea-system\">"); |
| 169 | rpc.append("<sysms:longitude/>"); |
| 170 | rpc.append("<sysms:latitude/>"); |
| 171 | rpc.append("</system>"); |
| 172 | return rpc.toString(); |
| 173 | } |
| 174 | |
| 175 | private static String getSetCurrentDatetimeBuilder(OffsetDateTime date) { |
| 176 | StringBuilder rpc = new StringBuilder(); |
| 177 | rpc.append("<set-current-datetime xmlns=\"urn:ietf:params:xml:ns:yang:ietf-system\">"); |
| 178 | rpc.append("<current-datetime>"); |
| 179 | rpc.append(date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssxxx"))); |
| 180 | rpc.append("</current-datetime>"); |
| 181 | rpc.append("</set-current-datetime>"); |
| 182 | |
| 183 | return rpc.toString(); |
| 184 | } |
| 185 | } |