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