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; |
| 18 | import com.google.common.annotations.VisibleForTesting; |
| 19 | import java.util.Collections; |
| 20 | import java.util.List; |
| 21 | import java.util.stream.Collectors; |
| 22 | import org.onosproject.config.DynamicConfigService; |
| 23 | |
| 24 | import org.onosproject.config.Filter; |
| 25 | import org.onosproject.net.ConnectPoint; |
| 26 | import org.onosproject.net.DeviceId; |
| 27 | import org.onosproject.odtn.utils.tapi.DcsBasedTapiNepRef; |
| 28 | import org.onosproject.odtn.utils.tapi.DcsBasedTapiNodeRef; |
| 29 | import org.onosproject.odtn.utils.tapi.TapiNepRef; |
| 30 | import org.onosproject.odtn.utils.tapi.TapiNodeRef; |
| 31 | import org.onosproject.yang.gen.v1.tapicommon.rev20180307.tapicommon.DefaultContext; |
| 32 | import org.onosproject.yang.gen.v1.tapicommon.rev20180307.tapicommon.globalclass.Name; |
| 33 | import org.onosproject.yang.gen.v1.tapitopology.rev20180307.tapitopology.context.DefaultAugmentedTapiCommonContext; |
| 34 | import org.onosproject.yang.gen.v1.tapitopology.rev20180307.tapitopology.topologycontext.Topology; |
| 35 | import org.onosproject.yang.model.Augmentable; |
| 36 | import org.onosproject.yang.model.DataNode; |
| 37 | import org.onosproject.yang.model.DefaultModelObjectData; |
| 38 | import org.onosproject.yang.model.DefaultResourceData; |
| 39 | import org.onosproject.yang.model.ModelConverter; |
| 40 | import org.onosproject.yang.model.ModelObject; |
| 41 | import org.onosproject.yang.model.ModelObjectData; |
| 42 | import org.onosproject.yang.model.ModelObjectId; |
| 43 | import org.onosproject.yang.model.ResourceData; |
| 44 | import org.onosproject.yang.model.ResourceId; |
| 45 | import org.slf4j.Logger; |
| 46 | |
| 47 | import static org.onlab.osgi.DefaultServiceDirectory.getService; |
| 48 | import static org.onosproject.odtn.utils.tapi.TapiInstanceBuilder.DEVICE_ID; |
| 49 | import static org.onosproject.odtn.utils.tapi.TapiInstanceBuilder.ONOS_CP; |
| 50 | import static org.slf4j.LoggerFactory.getLogger; |
| 51 | |
| 52 | public class DcsBasedTapiDataProducer implements TapiDataProducer { |
| 53 | |
| 54 | private final Logger log = getLogger(getClass()); |
| 55 | protected DynamicConfigService dcs; |
| 56 | protected ModelConverter modelConverter; |
| 57 | |
| 58 | @Override |
| 59 | public void init() { |
| 60 | dcs = getService(DynamicConfigService.class); |
| 61 | modelConverter = getService(ModelConverter.class); |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public void updateCacheRequest(DefaultTapiResolver resolver) { |
| 66 | ModelObject context = readContextModelObject(); |
| 67 | updateCache(resolver, context); |
| 68 | } |
| 69 | |
| 70 | @VisibleForTesting |
| 71 | protected void updateCache(DefaultTapiResolver resolver, ModelObject context) { |
| 72 | updateNodes(resolver, getNodes(context)); |
| 73 | updateNeps(resolver, getNeps(context)); |
| 74 | } |
| 75 | |
| 76 | private ModelObject readContextModelObject() { |
| 77 | // read DataNode from DCS |
| 78 | ModelObjectId mid = ModelObjectId.builder().addChild(DefaultContext.class).build(); |
| 79 | DataNode node = dcs.readNode(getResourceId(mid), Filter.builder().build()); |
| 80 | |
| 81 | // convert to ModelObject |
| 82 | ResourceData data = DefaultResourceData.builder().addDataNode(node) |
| 83 | .resourceId(ResourceId.builder().build()).build(); |
| 84 | ModelObjectData modelData = modelConverter.createModel(data); |
| 85 | ModelObject context = modelData.modelObjects().get(0); |
| 86 | |
| 87 | return context; |
| 88 | } |
| 89 | |
| 90 | private List<TapiNodeRef> getNodes(ModelObject context) { |
| 91 | Augmentable augmentedContext = (Augmentable) context; |
| 92 | DefaultAugmentedTapiCommonContext topologyContext |
| 93 | = augmentedContext.augmentation(DefaultAugmentedTapiCommonContext.class); |
| 94 | Topology topology = topologyContext.topology().get(0); |
| 95 | |
| 96 | if (topology.node() == null) { |
| 97 | return Collections.emptyList(); |
| 98 | } |
| 99 | return topology.node().stream() |
| 100 | .map(node -> { |
| 101 | String deviceId = node.name().stream() |
| 102 | .filter(kv -> kv.valueName().equals(DEVICE_ID)) |
| 103 | .findFirst().map(Name::value).get(); |
| 104 | return DcsBasedTapiNodeRef.create(topology, node) |
| 105 | .setDeviceId(DeviceId.deviceId(deviceId)); |
| 106 | }) |
| 107 | .collect(Collectors.toList()); |
| 108 | } |
| 109 | |
| 110 | private List<TapiNepRef> getNeps(ModelObject context) { |
| 111 | Augmentable augmentedContext = (Augmentable) context; |
| 112 | DefaultAugmentedTapiCommonContext topologyContext |
| 113 | = augmentedContext.augmentation(DefaultAugmentedTapiCommonContext.class); |
| 114 | Topology topology = topologyContext.topology().get(0); |
| 115 | |
| 116 | if (topology.node() == null) { |
| 117 | return Collections.emptyList(); |
| 118 | } |
| 119 | List<TapiNepRef> ret = topology.node().stream() |
| 120 | .flatMap(node -> { |
| 121 | if (node.ownedNodeEdgePoint() == null) { |
| 122 | return null; |
| 123 | } |
| 124 | return node.ownedNodeEdgePoint().stream() |
| 125 | .map(nep -> { |
| 126 | String onosConnectPoint = nep.name().stream() |
| 127 | .filter(kv -> kv.valueName().equals(ONOS_CP)) |
| 128 | .findFirst().map(Name::value).get(); |
| 129 | TapiNepRef nepRef = DcsBasedTapiNepRef.create(topology, node, nep) |
| 130 | .setConnectPoint(ConnectPoint.fromString(onosConnectPoint)); |
| 131 | if (nep.mappedServiceInterfacePoint() != null) { |
| 132 | nep.mappedServiceInterfacePoint().stream() |
| 133 | .forEach(sip -> { |
| 134 | nepRef.setSipId(sip.serviceInterfacePointId().toString()); |
| 135 | }); |
| 136 | } |
| 137 | return nepRef; |
| 138 | }); |
| 139 | } |
| 140 | ).collect(Collectors.toList()); |
| 141 | return ret; |
| 142 | } |
| 143 | |
| 144 | private void updateNodes(DefaultTapiResolver resolver, List<TapiNodeRef> nodes) { |
| 145 | resolver.addNodeRefList(nodes); |
| 146 | } |
| 147 | |
| 148 | private void updateNeps(DefaultTapiResolver resolver, List<TapiNepRef> neps) { |
| 149 | resolver.addNepRefList(neps); |
| 150 | } |
| 151 | |
| 152 | |
| 153 | |
| 154 | private ResourceId getResourceId(ModelObjectId modelId) { |
| 155 | ModelObjectData data = DefaultModelObjectData.builder() |
| 156 | .identifier(modelId) |
| 157 | .build(); |
| 158 | ResourceData rnode = modelConverter.createDataNode(data); |
| 159 | return rnode.resourceId(); |
| 160 | } |
| 161 | |
| 162 | } |