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