hiroki | d8fd786 | 2018-10-09 15:24:24 +0900 | [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.cli.impl; |
| 18 | |
| 19 | import org.apache.karaf.shell.commands.Command; |
| 20 | import org.onlab.util.XmlString; |
| 21 | import org.onosproject.cli.AbstractShellCommand; |
| 22 | import org.onosproject.config.DynamicConfigService; |
| 23 | import org.onosproject.net.ConnectPoint; |
| 24 | import org.onosproject.net.DeviceId; |
| 25 | import org.onosproject.net.PortNumber; |
| 26 | import org.onosproject.odtn.utils.tapi.TapiCepHandler; |
| 27 | import org.onosproject.odtn.utils.tapi.TapiCepRefHandler; |
| 28 | import org.onosproject.odtn.utils.tapi.TapiConnectionHandler; |
| 29 | import org.onosproject.odtn.utils.tapi.TapiConnectivityContextHandler; |
| 30 | import org.onosproject.odtn.utils.tapi.TapiConnectivityServiceHandler; |
| 31 | import org.onosproject.odtn.utils.tapi.TapiContextHandler; |
| 32 | import org.onosproject.odtn.utils.tapi.TapiNepHandler; |
| 33 | import org.onosproject.odtn.utils.tapi.TapiNodeHandler; |
| 34 | import org.onosproject.odtn.utils.tapi.TapiSepHandler; |
| 35 | import org.onosproject.odtn.utils.tapi.TapiSipHandler; |
| 36 | import org.onosproject.odtn.utils.tapi.TapiTopologyContextHandler; |
| 37 | import org.onosproject.odtn.utils.tapi.TapiTopologyHandler; |
| 38 | import org.onosproject.yang.gen.v1.tapicommon.rev20181016.tapicommon.DefaultContext; |
| 39 | import org.onosproject.yang.gen.v1.tapicommon.rev20181016.tapicommon.Uuid; |
| 40 | import org.onosproject.yang.gen.v1.tapicommon.rev20181016.tapicommon.tapicontext.DefaultServiceInterfacePoint; |
| 41 | import org.onosproject.yang.gen.v1.tapiconnectivity.rev20181016.tapiconnectivity.connectivitycontext.DefaultConnection; |
| 42 | import org.onosproject.yang.gen.v1.tapiconnectivity.rev20181016.tapiconnectivity.connectivitycontext.DefaultConnectivityService; |
| 43 | import org.onosproject.yang.gen.v1.tapiconnectivity.rev20181016.tapiconnectivity.connectivityservice.DefaultEndPoint; |
| 44 | import org.onosproject.yang.gen.v1.tapiconnectivity.rev20181016.tapiconnectivity.context.augmentedtapicommoncontext.DefaultConnectivityContext; |
| 45 | import org.onosproject.yang.gen.v1.tapitopology.rev20181016.tapitopology.context.augmentedtapicommoncontext.DefaultTopologyContext; |
| 46 | import org.onosproject.yang.gen.v1.tapitopology.rev20181016.tapitopology.node.DefaultOwnedNodeEdgePoint; |
| 47 | import org.onosproject.yang.gen.v1.tapitopology.rev20181016.tapitopology.topology.DefaultNode; |
| 48 | import org.onosproject.yang.gen.v1.tapitopology.rev20181016.tapitopology.topologycontext.DefaultTopology; |
| 49 | import org.onosproject.yang.model.Augmentable; |
| 50 | import org.onosproject.yang.model.DataNode; |
| 51 | import org.onosproject.yang.model.ModelConverter; |
| 52 | import org.onosproject.yang.model.ResourceId; |
| 53 | import org.slf4j.Logger; |
| 54 | |
| 55 | import java.io.BufferedReader; |
| 56 | import java.io.IOException; |
| 57 | import java.io.InputStream; |
| 58 | import java.io.InputStreamReader; |
| 59 | import java.util.regex.Pattern; |
| 60 | |
| 61 | import static org.onosproject.odtn.utils.YangToolUtil.*; |
| 62 | import static org.slf4j.LoggerFactory.getLogger; |
| 63 | |
| 64 | @Command(scope = "onos", name = "odtn-tapi-handlers-test") |
| 65 | public class OdtnTapiHandlersTestCommand extends AbstractShellCommand { |
| 66 | |
| 67 | private static final Logger log = getLogger(OdtnDcsModelCheckCommand.class); |
| 68 | private DynamicConfigService dcs; |
| 69 | private ModelConverter modelConverter; |
| 70 | private TapiContextHandler contextHandler; |
| 71 | |
| 72 | @Override |
| 73 | public void execute() { |
| 74 | dcs = get(DynamicConfigService.class); |
| 75 | modelConverter = get(ModelConverter.class); |
| 76 | |
| 77 | setupTapiContext(); |
| 78 | DataNode data = contextHandler.getDataNode(); |
| 79 | |
| 80 | ResourceId empty = ResourceId.builder().build(); |
| 81 | CharSequence strNode = toCharSequence(toXmlCompositeStream(toCompositeData(toResourceData(empty, data)))); |
| 82 | StringBuilder exp = loadXml("/test-tapi-context.xml"); |
| 83 | |
| 84 | if (XmlString.prettifyXml(strNode).toString().contentEquals(exp)) { |
| 85 | log.info("result: ok"); |
| 86 | } else { |
| 87 | log.info("result: failed"); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | private void printlog(String format, Object... objs) { |
| 92 | print(format.replaceAll(Pattern.quote("{}"), "%s"), objs); |
| 93 | log.info(format, objs); |
| 94 | } |
| 95 | |
| 96 | private static StringBuilder loadXml(final String fileName) { |
| 97 | |
| 98 | InputStream inputStream = OdtnTapiHandlersTestCommand.class.getResourceAsStream(fileName); |
| 99 | BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); |
| 100 | StringBuilder result = new StringBuilder(); |
| 101 | try { |
| 102 | String line; |
| 103 | while ((line = reader.readLine()) != null) { |
| 104 | result.append(line).append("\n"); |
| 105 | } |
| 106 | } catch (IOException e) { |
| 107 | e.printStackTrace(); |
| 108 | } |
| 109 | return result; |
| 110 | } |
| 111 | |
| 112 | private void setupTapiContext() { |
| 113 | |
| 114 | DeviceId did1 = DeviceId.deviceId("netconf:127.0.0.1:11001"); |
| 115 | DeviceId did2 = DeviceId.deviceId("netconf:127.0.0.1:11002"); |
| 116 | |
| 117 | ConnectPoint cp11 = new ConnectPoint(did1, PortNumber.portNumber(1, "TRANSCEIVER")); |
| 118 | ConnectPoint cp12 = new ConnectPoint(did1, PortNumber.portNumber(2, "TRANSCEIVER")); |
| 119 | ConnectPoint cp21 = new ConnectPoint(did2, PortNumber.portNumber(1, "TRANSCEIVER")); |
| 120 | ConnectPoint cp22 = new ConnectPoint(did2, PortNumber.portNumber(2, "TRANSCEIVER")); |
| 121 | |
| 122 | // context |
| 123 | contextHandler = TapiContextHandler.create(); |
| 124 | DefaultContext context = contextHandler.getModelObject(); |
| 125 | |
| 126 | // context augmentation |
| 127 | Augmentable augmentableContext = context; |
| 128 | |
| 129 | // context augmentation with topologyContext |
| 130 | org.onosproject.yang.gen.v1.tapitopology.rev20181016.tapitopology.context |
| 131 | .DefaultAugmentedTapiCommonContext augmentedTopologyContext |
| 132 | = new org.onosproject.yang.gen.v1.tapitopology.rev20181016.tapitopology |
| 133 | .context.DefaultAugmentedTapiCommonContext(); |
| 134 | augmentableContext.addAugmentation(augmentedTopologyContext); |
| 135 | |
| 136 | // context augmentation with connectivityServiceContext |
| 137 | org.onosproject.yang.gen.v1.tapiconnectivity.rev20181016.tapiconnectivity.context |
| 138 | .DefaultAugmentedTapiCommonContext augmentedConnectivityContext |
| 139 | = new org.onosproject.yang.gen.v1.tapiconnectivity.rev20181016.tapiconnectivity |
| 140 | .context.DefaultAugmentedTapiCommonContext(); |
| 141 | augmentableContext.addAugmentation(augmentedConnectivityContext); |
| 142 | |
| 143 | // topology context |
| 144 | DefaultTopologyContext topologyContext = TapiTopologyContextHandler.create().getModelObject(); |
| 145 | augmentedTopologyContext.topologyContext(topologyContext); |
| 146 | |
| 147 | // topology |
| 148 | TapiTopologyHandler topologyHandler = TapiTopologyHandler.create(); |
| 149 | topologyHandler.setId(Uuid.of("00000000-0000-0000-0000-000000000001")); |
| 150 | DefaultTopology topology = topologyHandler.getModelObject(); |
| 151 | topologyContext.addToTopology(topology); |
| 152 | |
| 153 | // nodes |
| 154 | TapiNodeHandler nodeHandler1 = TapiNodeHandler.create(); |
| 155 | nodeHandler1.setId(Uuid.of("00000000-0000-0000-0001-000000000001")); |
| 156 | DefaultNode node1 = nodeHandler1.setTopologyUuid(topology.uuid()) |
| 157 | .setDeviceId(did1) |
| 158 | .getModelObject(); |
| 159 | topology.addToNode(node1); |
| 160 | |
| 161 | TapiNodeHandler nodeHandler2 = TapiNodeHandler.create(); |
| 162 | nodeHandler2.setId(Uuid.of("00000000-0000-0000-0001-000000000002")); |
| 163 | DefaultNode node2 = nodeHandler2.setTopologyUuid(topology.uuid()) |
| 164 | .setDeviceId(did2) |
| 165 | .getModelObject(); |
| 166 | topology.addToNode(node2); |
| 167 | |
| 168 | // sips |
| 169 | TapiSipHandler sipHandler1 = TapiSipHandler.create(); |
| 170 | sipHandler1.setId(Uuid.of("00000000-0000-0000-0002-000000000001")); |
| 171 | DefaultServiceInterfacePoint sip1 = sipHandler1.setConnectPoint(cp11) |
| 172 | .getModelObject(); |
| 173 | context.addToServiceInterfacePoint(sip1); |
| 174 | |
| 175 | TapiSipHandler sipHandler2 = TapiSipHandler.create(); |
| 176 | sipHandler2.setId(Uuid.of("00000000-0000-0000-0002-000000000002")); |
| 177 | DefaultServiceInterfacePoint sip2 = sipHandler2.setConnectPoint(cp21) |
| 178 | .getModelObject(); |
| 179 | context.addToServiceInterfacePoint(sip2); |
| 180 | |
| 181 | // neps |
| 182 | TapiNepHandler nepHandler11 = TapiNepHandler.create(); |
| 183 | nepHandler11.setId(Uuid.of("00000000-0000-0000-0003-000000000011")); |
| 184 | DefaultOwnedNodeEdgePoint nep11 = nepHandler11 |
| 185 | .setTopologyUuid(topology.uuid()) |
| 186 | .setNodeUuid(node1.uuid()) |
| 187 | .setConnectPoint(cp11) |
| 188 | .addSip(sip1.uuid()) |
| 189 | .getModelObject(); |
| 190 | nodeHandler1.addNep(nep11); |
| 191 | |
| 192 | TapiNepHandler nepHandler12 = TapiNepHandler.create(); |
| 193 | nepHandler12.setId(Uuid.of("00000000-0000-0000-0003-000000000012")); |
| 194 | DefaultOwnedNodeEdgePoint nep12 = nepHandler12 |
| 195 | .setTopologyUuid(topology.uuid()) |
| 196 | .setNodeUuid(node1.uuid()) |
| 197 | .setConnectPoint(cp12) |
| 198 | .getModelObject(); |
| 199 | nodeHandler1.addNep(nep12); |
| 200 | |
| 201 | TapiNepHandler nepHandler21 = TapiNepHandler.create(); |
| 202 | nepHandler21.setId(Uuid.of("00000000-0000-0000-0003-000000000021")); |
| 203 | DefaultOwnedNodeEdgePoint nep21 = nepHandler21 |
| 204 | .setTopologyUuid(topology.uuid()) |
| 205 | .setNodeUuid(node2.uuid()) |
| 206 | .setConnectPoint(cp21) |
| 207 | .addSip(sip2.uuid()) |
| 208 | .getModelObject(); |
| 209 | nodeHandler2.addNep(nep21); |
| 210 | |
| 211 | TapiNepHandler nepHandler22 = TapiNepHandler.create(); |
| 212 | nepHandler22.setId(Uuid.of("00000000-0000-0000-0003-000000000022")); |
| 213 | DefaultOwnedNodeEdgePoint nep22 = nepHandler22 |
| 214 | .setTopologyUuid(topology.uuid()) |
| 215 | .setNodeUuid(node2.uuid()) |
| 216 | .setConnectPoint(cp22) |
| 217 | .getModelObject(); |
| 218 | nodeHandler2.addNep(nep22); |
| 219 | |
| 220 | // connectivity context |
| 221 | DefaultConnectivityContext connectivityContext = TapiConnectivityContextHandler.create().getModelObject(); |
| 222 | augmentedConnectivityContext.connectivityContext(connectivityContext); |
| 223 | |
| 224 | // connectivityService |
| 225 | TapiConnectivityServiceHandler connectivityServiceHandler = TapiConnectivityServiceHandler.create(); |
| 226 | connectivityServiceHandler.setId(Uuid.of("00000000-0000-0000-0004-000000000001")); |
| 227 | DefaultConnectivityService connectivityService = connectivityServiceHandler.getModelObject(); |
| 228 | connectivityContext.addToConnectivityService(connectivityService); |
| 229 | |
| 230 | // connection |
| 231 | TapiConnectionHandler connectionHandler = TapiConnectionHandler.create(); |
| 232 | connectionHandler.setId(Uuid.of("00000000-0000-0000-0005-000000000001")); |
| 233 | DefaultConnection connection1 = connectionHandler.getModelObject(); |
| 234 | connectivityServiceHandler.addConnection(connection1.uuid()); |
| 235 | connectivityContext.addToConnection(connection1); |
| 236 | |
| 237 | |
| 238 | // seps |
| 239 | TapiSepHandler sepHandler1 = TapiSepHandler.create(); |
| 240 | sepHandler1.setId(Uuid.of("00000000-0000-0000-0006-000000000001")); |
| 241 | DefaultEndPoint sep1 = sepHandler1.getModelObject(); |
| 242 | connectivityServiceHandler.addSep(sep1); |
| 243 | |
| 244 | TapiSepHandler sepHandler2 = TapiSepHandler.create(); |
| 245 | sepHandler2.setId(Uuid.of("00000000-0000-0000-0006-000000000002")); |
| 246 | DefaultEndPoint sep2 = sepHandler2.getModelObject(); |
| 247 | connectivityServiceHandler.addSep(sep2); |
| 248 | |
| 249 | // ceps |
| 250 | TapiCepHandler cepHandler11 = TapiCepHandler.create(); |
| 251 | cepHandler11.setId(Uuid.of("00000000-0000-0000-0007-000000000011")); |
| 252 | org.onosproject.yang.gen.v1.tapiconnectivity.rev20181016.tapiconnectivity |
| 253 | .ceplist.DefaultConnectionEndPoint |
| 254 | cep11 = cepHandler11 |
| 255 | .setTopologyUuid(topology.uuid()) |
| 256 | .setNodeUuid(node1.uuid()) |
| 257 | .setNepUuid(nep11.uuid()) |
| 258 | .setParentNep() |
| 259 | .getModelObject(); |
| 260 | nepHandler11.addCep(cep11); |
| 261 | org.onosproject.yang.gen.v1.tapiconnectivity.rev20181016.tapiconnectivity |
| 262 | .connection.DefaultConnectionEndPoint |
| 263 | cepRef11 = TapiCepRefHandler.create() |
| 264 | .setCep(cep11) |
| 265 | .getModelObject(); |
| 266 | connectionHandler.addCep(cepRef11); |
| 267 | |
| 268 | TapiCepHandler cepHandler21 = TapiCepHandler.create(); |
| 269 | cepHandler21.setId(Uuid.of("00000000-0000-0000-0007-000000000021")); |
| 270 | org.onosproject.yang.gen.v1.tapiconnectivity.rev20181016.tapiconnectivity |
| 271 | .ceplist.DefaultConnectionEndPoint |
| 272 | cep21 = cepHandler21 |
| 273 | .setTopologyUuid(topology.uuid()) |
| 274 | .setNodeUuid(node1.uuid()) |
| 275 | .setNepUuid(nep21.uuid()) |
| 276 | .setParentNep() |
| 277 | .getModelObject(); |
| 278 | nepHandler21.addCep(cep21); |
| 279 | org.onosproject.yang.gen.v1.tapiconnectivity.rev20181016.tapiconnectivity |
| 280 | .connection.DefaultConnectionEndPoint |
| 281 | cepRef21 = TapiCepRefHandler.create() |
| 282 | .setCep(cep21) |
| 283 | .getModelObject(); |
| 284 | connectionHandler.addCep(cepRef21); |
| 285 | |
| 286 | } |
| 287 | } |