hiroki | bca3e93 | 2018-05-15 15:25:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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 | |
| 17 | package org.onosproject.odtn.internal; |
| 18 | |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 19 | import java.util.List; |
hiroki | bca3e93 | 2018-05-15 15:25:54 -0700 | [diff] [blame] | 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; |
| 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 25 | import org.apache.felix.scr.annotations.Service; |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 26 | import org.onlab.util.XmlString; |
hiroki | bca3e93 | 2018-05-15 15:25:54 -0700 | [diff] [blame] | 27 | import org.onosproject.config.DynamicConfigService; |
| 28 | import org.onosproject.config.FailedException; |
| 29 | import org.onosproject.config.Filter; |
| 30 | import org.onosproject.d.config.DeviceResourceIds; |
hiroki | bca3e93 | 2018-05-15 15:25:54 -0700 | [diff] [blame] | 31 | |
| 32 | import static org.onosproject.d.config.DeviceResourceIds.DCS_NAMESPACE; |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 33 | |
| 34 | import org.onosproject.d.config.ResourceIds; |
| 35 | import org.onosproject.net.ConnectPoint; |
| 36 | import org.onosproject.net.Device; |
| 37 | import org.onosproject.net.DeviceId; |
| 38 | import org.onosproject.net.Link; |
| 39 | import org.onosproject.net.Port; |
| 40 | |
| 41 | import static org.onosproject.odtn.utils.YangToolUtil.toCharSequence; |
| 42 | import static org.onosproject.odtn.utils.YangToolUtil.toCompositeData; |
| 43 | import static org.onosproject.odtn.utils.YangToolUtil.toXmlCompositeStream; |
| 44 | |
| 45 | import org.onosproject.odtn.utils.tapi.TapiLinkBuilder; |
| 46 | import org.onosproject.odtn.utils.tapi.TapiNepRef; |
| 47 | import org.onosproject.odtn.utils.tapi.TapiNodeRef; |
| 48 | import org.onosproject.odtn.utils.tapi.TapiResolver; |
| 49 | import org.onosproject.odtn.utils.tapi.TapiContextBuilder; |
| 50 | import org.onosproject.odtn.utils.tapi.TapiNepBuilder; |
| 51 | import org.onosproject.odtn.utils.tapi.TapiNodeBuilder; |
| 52 | import org.onosproject.odtn.utils.tapi.TapiSipBuilder; |
| 53 | import org.onosproject.odtn.utils.tapi.TapiTopologyBuilder; |
| 54 | import org.onosproject.yang.gen.v1.tapicommon.rev20180307.tapicommon.DefaultContext; |
| 55 | import org.onosproject.yang.gen.v1.tapicommon.rev20180307.tapicommon.Uuid; |
| 56 | import org.onosproject.yang.gen.v1.tapitopology.rev20180307.tapitopology.topologycontext.DefaultTopology; |
| 57 | import org.onosproject.yang.model.DataNode; |
| 58 | import org.onosproject.yang.model.InnerNode; |
| 59 | import org.onosproject.yang.model.ModelConverter; |
| 60 | import org.onosproject.yang.model.ModelObjectData; |
| 61 | import org.onosproject.yang.model.ResourceData; |
| 62 | import org.onosproject.yang.model.ResourceId; |
| 63 | import org.slf4j.Logger; |
| 64 | |
hiroki | bca3e93 | 2018-05-15 15:25:54 -0700 | [diff] [blame] | 65 | import static org.slf4j.LoggerFactory.getLogger; |
| 66 | |
| 67 | /** |
| 68 | * OSGi Component for ODTN Tapi manager application. |
| 69 | */ |
| 70 | @Component(immediate = true) |
| 71 | @Service |
| 72 | public class DcsBasedTapiTopologyManager implements TapiTopologyManager { |
| 73 | |
| 74 | private final Logger log = getLogger(getClass()); |
| 75 | |
| 76 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 77 | protected DynamicConfigService dcs; |
| 78 | |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 79 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 80 | protected ModelConverter modelConverter; |
| 81 | |
| 82 | private DefaultContext context = new DefaultContext(); |
| 83 | private DefaultTopology topology = new DefaultTopology(); |
| 84 | |
| 85 | private TapiResolver tapiResolver = new TapiResolver(); |
| 86 | |
hiroki | bca3e93 | 2018-05-15 15:25:54 -0700 | [diff] [blame] | 87 | @Activate |
| 88 | public void activate() { |
| 89 | initDcsIfRootNotExist(); |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 90 | initDcsTapiContext(); |
| 91 | initDcsTapiTopology(); |
hiroki | bca3e93 | 2018-05-15 15:25:54 -0700 | [diff] [blame] | 92 | log.info("Started"); |
| 93 | } |
| 94 | |
| 95 | @Deactivate |
| 96 | public void deactivate() { |
| 97 | log.info("Stopped"); |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public void addDevice(Device device) { |
| 102 | log.info("Add device: {}", device); |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 103 | DeviceId deviceId = device.id(); |
| 104 | if (tapiResolver.hasNodeRef(deviceId)) { |
| 105 | return; |
| 106 | } |
| 107 | TapiNodeBuilder builder = TapiNodeBuilder.builder() |
| 108 | .setTopologyUuid(topology.uuid()) |
| 109 | .setDeviceId(deviceId); |
| 110 | addModelObjectDataToDcs(builder.build()); |
| 111 | |
| 112 | TapiNodeRef nodeRef = new TapiNodeRef(topology.uuid().toString(), builder.getUuid().toString()); |
| 113 | nodeRef.setDeviceId(deviceId); |
| 114 | tapiResolver.addNodeRef(nodeRef); |
hiroki | bca3e93 | 2018-05-15 15:25:54 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public void removeDevice(Device device) { |
| 119 | log.info("Remove device: {}", device); |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public void addLink(Link link) { |
| 124 | log.info("Add link: {}", link); |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 125 | |
| 126 | // validation check |
| 127 | |
| 128 | // src nep |
| 129 | addNep(link.src()); |
| 130 | addNep(link.dst()); |
| 131 | |
| 132 | // link |
| 133 | TapiNepRef srcNepRef = tapiResolver.getNepRef(link.src()); |
| 134 | TapiNepRef dstNepRef = tapiResolver.getNepRef(link.dst()); |
| 135 | |
| 136 | TapiLinkBuilder linkBuilder = TapiLinkBuilder.builder() |
| 137 | .setTopologyUuid(topology.uuid()) |
| 138 | .setNep(srcNepRef) |
| 139 | .setNep(dstNepRef); |
| 140 | addModelObjectDataToDcs(linkBuilder.build()); |
hiroki | bca3e93 | 2018-05-15 15:25:54 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public void removeLink(Link link) { |
| 145 | log.info("Remove link: {}", link); |
| 146 | } |
| 147 | |
| 148 | @Override |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 149 | public void addPort(Port port) { |
| 150 | log.info("Add port: {}", port); |
| 151 | if (tapiResolver.hasNepRef(new ConnectPoint(port.element().id(), port.number()))) { |
| 152 | return; |
| 153 | } |
| 154 | |
| 155 | TapiNodeRef nodeRef = tapiResolver.getNodeRef(port.element().id()); |
| 156 | String nodeId = nodeRef.getNodeId(); |
| 157 | |
| 158 | // nep |
| 159 | TapiNepBuilder nepBuilder = TapiNepBuilder.builder() |
| 160 | .setPort(port) |
| 161 | .setTopologyUuid(topology.uuid()) |
| 162 | .setNodeUuid(Uuid.fromString(nodeId)); |
| 163 | |
| 164 | TapiNepRef nepRef = new TapiNepRef(topology.uuid().toString(), nodeId, nepBuilder.getUuid().toString()); |
| 165 | nepRef.setConnectPoint(nepBuilder.getConnectPoint()); |
| 166 | |
| 167 | // sip |
| 168 | if (TapiSipBuilder.isSip(port)) { |
| 169 | TapiSipBuilder sipBuilder = TapiSipBuilder.builder().setPort(port); |
| 170 | nepBuilder.setSip(sipBuilder.getUuid()); |
| 171 | nepRef.setSipId(sipBuilder.getUuid().toString()); |
| 172 | |
| 173 | addModelObjectDataToDcs(sipBuilder.build()); |
| 174 | } |
| 175 | |
| 176 | addModelObjectDataToDcs(nepBuilder.build()); |
| 177 | tapiResolver.addNepRef(nepRef); |
hiroki | bca3e93 | 2018-05-15 15:25:54 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | @Override |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 181 | public void removePort(Port port) { |
| 182 | log.info("Remove port: {}", port); |
| 183 | } |
| 184 | |
| 185 | private void addNep(ConnectPoint cp) { |
| 186 | |
| 187 | log.info("device Id: {}", cp.deviceId()); |
| 188 | TapiNodeRef nodeRef = tapiResolver.getNodeRef(cp.deviceId()); |
| 189 | String nodeId = nodeRef.getNodeId(); |
| 190 | |
| 191 | TapiNepBuilder nepBuilder = TapiNepBuilder.builder() |
| 192 | .setConnectPoint(cp) |
| 193 | .setTopologyUuid(topology.uuid()) |
| 194 | .setNodeUuid(Uuid.fromString(nodeId)); |
| 195 | TapiNepRef nepRef = new TapiNepRef(topology.uuid().toString(), nodeId, nepBuilder.getUuid().toString()); |
| 196 | nepRef.setConnectPoint(cp); |
| 197 | |
| 198 | addModelObjectDataToDcs(nepBuilder.build()); |
| 199 | tapiResolver.addNepRef(nepRef); |
| 200 | } |
| 201 | |
| 202 | private void initDcsTapiContext() { |
| 203 | TapiContextBuilder builder = TapiContextBuilder.builder(context); |
| 204 | addModelObjectDataToDcs(builder.build()); |
| 205 | } |
| 206 | |
| 207 | private void initDcsTapiTopology() { |
| 208 | TapiTopologyBuilder builder = TapiTopologyBuilder.builder(topology); |
| 209 | addModelObjectDataToDcs(builder.build()); |
| 210 | } |
| 211 | |
| 212 | // FIXME: move DCS-related methods to DCS |
| 213 | |
| 214 | private void addModelObjectDataToDcs(ModelObjectData input) { |
| 215 | |
| 216 | ResourceData rnode = modelConverter.createDataNode(input); |
| 217 | |
| 218 | // for debug |
| 219 | CharSequence strNode = toCharSequence(toXmlCompositeStream(toCompositeData(rnode))); |
| 220 | log.info("XML:\n{}", XmlString.prettifyXml(strNode)); |
| 221 | |
| 222 | addResourceDataToDcs(rnode); |
| 223 | } |
| 224 | |
| 225 | private void addResourceDataToDcs(ResourceData input) { |
| 226 | addResourceDataToDcs(input, input.resourceId()); |
| 227 | } |
| 228 | |
| 229 | private void addResourceDataToDcs(ResourceData input, ResourceId rid) { |
| 230 | if (input == null || input.dataNodes() == null) { |
| 231 | return; |
| 232 | } |
| 233 | List<DataNode> dataNodes = input.dataNodes(); |
| 234 | for (DataNode node : dataNodes) { |
| 235 | dcs.createNode(rid, node); |
| 236 | } |
hiroki | bca3e93 | 2018-05-15 15:25:54 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | private void initDcsIfRootNotExist() { |
| 240 | |
| 241 | log.info("read root:"); |
| 242 | try { |
| 243 | DataNode all = dcs.readNode(ResourceIds.ROOT_ID, Filter.builder().build()); |
| 244 | log.info("all: {}", all); |
| 245 | } catch (FailedException e) { |
| 246 | // FIXME debug this issue |
| 247 | log.info("nothing retrievable in DCS?"); |
| 248 | //e.printStackTrace(System.out); |
| 249 | } |
| 250 | if (!dcs.nodeExist(ResourceIds.ROOT_ID)) { |
| 251 | log.info("Root node does not exist!, creating..."); |
| 252 | try { |
| 253 | log.info("create 'root' node"); |
| 254 | dcs.createNode(null, |
| 255 | InnerNode.builder(DeviceResourceIds.ROOT_NAME, DCS_NAMESPACE) |
| 256 | .type(DataNode.Type.SINGLE_INSTANCE_NODE).build()); |
| 257 | } catch (FailedException e) { |
| 258 | log.info("Failed to create root???"); |
| 259 | //e.printStackTrace(System.out); |
| 260 | } |
| 261 | } |
| 262 | if (!dcs.nodeExist(ResourceIds.ROOT_ID)) { |
| 263 | log.info("'root' was created without error, but still not there. WTF!"); |
| 264 | } |
| 265 | } |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 266 | |
hiroki | bca3e93 | 2018-05-15 15:25:54 -0700 | [diff] [blame] | 267 | } |