hiroki | 684aa2f | 2018-05-19 20:48:49 -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; |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame^] | 18 | |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 19 | import com.google.common.annotations.VisibleForTesting; |
| 20 | import java.util.Collections; |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame^] | 21 | import java.util.HashMap; |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 22 | import java.util.List; |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame^] | 23 | import java.util.Map; |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 24 | import java.util.stream.Collectors; |
| 25 | import org.onosproject.config.DynamicConfigService; |
| 26 | |
| 27 | import org.onosproject.config.Filter; |
| 28 | import org.onosproject.net.ConnectPoint; |
| 29 | import org.onosproject.net.DeviceId; |
| 30 | import org.onosproject.odtn.utils.tapi.DcsBasedTapiNepRef; |
| 31 | import org.onosproject.odtn.utils.tapi.DcsBasedTapiNodeRef; |
| 32 | import org.onosproject.odtn.utils.tapi.TapiNepRef; |
| 33 | import org.onosproject.odtn.utils.tapi.TapiNodeRef; |
| 34 | import org.onosproject.yang.gen.v1.tapicommon.rev20180307.tapicommon.DefaultContext; |
| 35 | import org.onosproject.yang.gen.v1.tapicommon.rev20180307.tapicommon.globalclass.Name; |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame^] | 36 | import org.onosproject.yang.gen.v1.tapiconnectivity.rev20180307.tapiconnectivity.context.topology.node.ownednodeedgepoint.DefaultAugmentedTapiTopologyOwnedNodeEdgePoint; |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 37 | import org.onosproject.yang.gen.v1.tapitopology.rev20180307.tapitopology.context.DefaultAugmentedTapiCommonContext; |
| 38 | import org.onosproject.yang.gen.v1.tapitopology.rev20180307.tapitopology.topologycontext.Topology; |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 39 | import org.onosproject.yang.model.DataNode; |
| 40 | import org.onosproject.yang.model.DefaultModelObjectData; |
| 41 | import org.onosproject.yang.model.DefaultResourceData; |
| 42 | import org.onosproject.yang.model.ModelConverter; |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 43 | import org.onosproject.yang.model.ModelObjectData; |
| 44 | import org.onosproject.yang.model.ModelObjectId; |
| 45 | import org.onosproject.yang.model.ResourceData; |
| 46 | import org.onosproject.yang.model.ResourceId; |
| 47 | import org.slf4j.Logger; |
| 48 | |
| 49 | import static org.onlab.osgi.DefaultServiceDirectory.getService; |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame^] | 50 | import static org.onosproject.odtn.utils.tapi.TapiObjectHandler.DEVICE_ID; |
| 51 | import static org.onosproject.odtn.utils.tapi.TapiObjectHandler.ODTN_PORT_TYPE; |
| 52 | import static org.onosproject.odtn.utils.tapi.TapiObjectHandler.ONOS_CP; |
| 53 | import static org.onosproject.odtn.behaviour.OdtnDeviceDescriptionDiscovery.CONNECTION_ID; |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 54 | import static org.slf4j.LoggerFactory.getLogger; |
| 55 | |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame^] | 56 | /** |
| 57 | * DCS-dependent Tapi Data producer implementation. |
| 58 | */ |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 59 | public class DcsBasedTapiDataProducer implements TapiDataProducer { |
| 60 | |
| 61 | private final Logger log = getLogger(getClass()); |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame^] | 62 | |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 63 | protected DynamicConfigService dcs; |
| 64 | protected ModelConverter modelConverter; |
| 65 | |
| 66 | @Override |
| 67 | public void init() { |
| 68 | dcs = getService(DynamicConfigService.class); |
| 69 | modelConverter = getService(ModelConverter.class); |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public void updateCacheRequest(DefaultTapiResolver resolver) { |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame^] | 74 | updateCache(resolver, readContextModelObject()); |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 75 | } |
| 76 | |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame^] | 77 | /** |
| 78 | * Update resolver's cache with Tapi context modelObject. |
| 79 | * |
| 80 | * @param resolver TapiResolver |
| 81 | * @param context Context ModelObject which has all data nodes of Tapi DataTree in Dcs store |
| 82 | */ |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 83 | @VisibleForTesting |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame^] | 84 | protected void updateCache(DefaultTapiResolver resolver, DefaultContext context) { |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 85 | updateNodes(resolver, getNodes(context)); |
| 86 | updateNeps(resolver, getNeps(context)); |
| 87 | } |
| 88 | |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame^] | 89 | /** |
| 90 | * Get Tapi context modelObject from Dcs. |
| 91 | * |
| 92 | * @return Tapi context modelObject in Dcs store |
| 93 | */ |
| 94 | // FIXME update this method using TapiContextHandler |
| 95 | private DefaultContext readContextModelObject() { |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 96 | // read DataNode from DCS |
| 97 | ModelObjectId mid = ModelObjectId.builder().addChild(DefaultContext.class).build(); |
| 98 | DataNode node = dcs.readNode(getResourceId(mid), Filter.builder().build()); |
| 99 | |
| 100 | // convert to ModelObject |
| 101 | ResourceData data = DefaultResourceData.builder().addDataNode(node) |
| 102 | .resourceId(ResourceId.builder().build()).build(); |
| 103 | ModelObjectData modelData = modelConverter.createModel(data); |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame^] | 104 | DefaultContext context = (DefaultContext) modelData.modelObjects().get(0); |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 105 | |
| 106 | return context; |
| 107 | } |
| 108 | |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame^] | 109 | /** |
| 110 | * Extract Tapi Nodes from context modelObject and convert them to NodeRefs. |
| 111 | * |
| 112 | * @param context |
| 113 | * @return List of NodeRef |
| 114 | */ |
| 115 | private List<TapiNodeRef> getNodes(DefaultContext context) { |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 116 | DefaultAugmentedTapiCommonContext topologyContext |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame^] | 117 | = context.augmentation(DefaultAugmentedTapiCommonContext.class); |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 118 | Topology topology = topologyContext.topology().get(0); |
| 119 | |
| 120 | if (topology.node() == null) { |
| 121 | return Collections.emptyList(); |
| 122 | } |
| 123 | return topology.node().stream() |
| 124 | .map(node -> { |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame^] | 125 | DcsBasedTapiNodeRef nodeRef = DcsBasedTapiNodeRef.create(topology, node); |
| 126 | if (node.name() != null) { |
| 127 | String deviceId = node.name().stream() |
| 128 | .filter(kv -> kv.valueName().equals(DEVICE_ID)) |
| 129 | .findFirst().map(Name::value).get(); |
| 130 | nodeRef.setDeviceId(DeviceId.deviceId(deviceId)); |
| 131 | } |
| 132 | return nodeRef; |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 133 | }) |
| 134 | .collect(Collectors.toList()); |
| 135 | } |
| 136 | |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame^] | 137 | /** |
| 138 | * Extract Tapi Neps from context modelObject and convert them to NepRefs. |
| 139 | * |
| 140 | * @param context |
| 141 | * @return List of TapiNepRef |
| 142 | */ |
| 143 | private List<TapiNepRef> getNeps(DefaultContext context) { |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 144 | DefaultAugmentedTapiCommonContext topologyContext |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame^] | 145 | = context.augmentation(DefaultAugmentedTapiCommonContext.class); |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 146 | Topology topology = topologyContext.topology().get(0); |
| 147 | |
| 148 | if (topology.node() == null) { |
| 149 | return Collections.emptyList(); |
| 150 | } |
| 151 | List<TapiNepRef> ret = topology.node().stream() |
| 152 | .flatMap(node -> { |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame^] | 153 | if (node.ownedNodeEdgePoint() == null) { |
| 154 | return null; |
| 155 | } |
| 156 | return node.ownedNodeEdgePoint().stream() |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 157 | .map(nep -> { |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame^] | 158 | TapiNepRef nepRef = DcsBasedTapiNepRef.create(topology, node, nep); |
| 159 | if (nep.name() != null) { |
| 160 | Map<String, String> kvs = new HashMap<>(); |
| 161 | nep.name().forEach(kv -> kvs.put(kv.valueName(), kv.value())); |
| 162 | |
| 163 | String onosConnectPoint = kvs.getOrDefault(ONOS_CP, null); |
| 164 | String portType = kvs.getOrDefault(ODTN_PORT_TYPE, null); |
| 165 | String connectionId = kvs.getOrDefault(CONNECTION_ID, null); |
| 166 | nepRef.setConnectPoint(ConnectPoint.fromString(onosConnectPoint)) |
| 167 | .setPortType(portType) |
| 168 | .setConnectionId(connectionId); |
| 169 | } |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 170 | if (nep.mappedServiceInterfacePoint() != null) { |
| 171 | nep.mappedServiceInterfacePoint().stream() |
| 172 | .forEach(sip -> { |
| 173 | nepRef.setSipId(sip.serviceInterfacePointId().toString()); |
| 174 | }); |
| 175 | } |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame^] | 176 | |
| 177 | DefaultAugmentedTapiTopologyOwnedNodeEdgePoint augmentNep = |
| 178 | nep.augmentation(DefaultAugmentedTapiTopologyOwnedNodeEdgePoint.class); |
| 179 | try { |
| 180 | if (augmentNep.connectionEndPoint() != null) { |
| 181 | List<String> cepIds = augmentNep.connectionEndPoint().stream() |
| 182 | .map(cep -> cep.uuid().toString()).collect(Collectors.toList()); |
| 183 | nepRef.setCepIds(cepIds); |
| 184 | } |
| 185 | } catch (NullPointerException e) { |
| 186 | log.warn("Augmented ownedNodeEdgePoint is not found."); |
| 187 | } |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 188 | return nepRef; |
| 189 | }); |
| 190 | } |
| 191 | ).collect(Collectors.toList()); |
| 192 | return ret; |
| 193 | } |
| 194 | |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame^] | 195 | /** |
| 196 | * Update resolver's NodeRef list. |
| 197 | * |
| 198 | * @param resolver TapiResolver |
| 199 | * @param nodes List of NodeRef for update |
| 200 | */ |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 201 | private void updateNodes(DefaultTapiResolver resolver, List<TapiNodeRef> nodes) { |
| 202 | resolver.addNodeRefList(nodes); |
| 203 | } |
| 204 | |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame^] | 205 | /** |
| 206 | * Update resolver's NepRef list. |
| 207 | * |
| 208 | * @param resolver TapiResolver |
| 209 | * @param neps List of NepRef for update |
| 210 | */ |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 211 | private void updateNeps(DefaultTapiResolver resolver, List<TapiNepRef> neps) { |
| 212 | resolver.addNepRefList(neps); |
| 213 | } |
| 214 | |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 215 | private ResourceId getResourceId(ModelObjectId modelId) { |
| 216 | ModelObjectData data = DefaultModelObjectData.builder() |
| 217 | .identifier(modelId) |
| 218 | .build(); |
| 219 | ResourceData rnode = modelConverter.createDataNode(data); |
| 220 | return rnode.resourceId(); |
| 221 | } |
| 222 | |
| 223 | } |