Yixiao Chen | 39828a6 | 2016-09-14 14:37:06 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Open Networking Laboratory |
| 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 | package org.onosproject.teyang.utils.topology; |
| 17 | |
Hesam Rahimi | 39bdc00 | 2016-11-10 15:01:26 -0500 | [diff] [blame^] | 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | |
| 20 | import java.util.List; |
| 21 | |
| 22 | import org.onosproject.tetopology.management.api.KeyId; |
| 23 | import org.onosproject.tetopology.management.api.node.TerminationPointKey; |
| 24 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev20151208.ietfnetwork.NetworkId; |
| 25 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev20151208.ietfnetwork.NodeId; |
| 26 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev20151208 |
| 27 | .ietfnetworktopology.TpId; |
| 28 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev20151208 |
| 29 | .ietfnetworktopology.networks.network.node.augmentedndnode.DefaultTerminationPoint; |
| 30 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev20151208 |
| 31 | .ietfnetworktopology.networks.network.node.augmentedndnode.TerminationPoint; |
| 32 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev20151208 |
| 33 | .ietfnetworktopology.networks.network.node.augmentedndnode.terminationpoint |
| 34 | .DefaultSupportingTerminationPoint; |
| 35 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev20151208 |
| 36 | .ietfnetworktopology.networks.network.node.augmentedndnode.terminationpoint |
| 37 | .SupportingTerminationPoint; |
| 38 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.topology.rev20160708.ietftetopology.interfaceswitchingcapabilitylist.DefaultInterfaceSwitchingCapability; |
| 39 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.topology.rev20160708.ietftetopology.interfaceswitchingcapabilitylist.InterfaceSwitchingCapability; |
| 40 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.topology.rev20160708.ietftetopology.interfaceswitchingcapabilitylist.interfaceswitchingcapability.DefaultMaxLspBandwidth; |
| 41 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.topology.rev20160708.ietftetopology.interfaceswitchingcapabilitylist.interfaceswitchingcapability.MaxLspBandwidth; |
| 42 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.topology.rev20160708 |
| 43 | .ietftetopology.networks.network.node.terminationpoint.AugmentedNtTerminationPoint; |
| 44 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.topology.rev20160708 |
| 45 | .ietftetopology.networks.network.node.terminationpoint.DefaultAugmentedNtTerminationPoint; |
| 46 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.topology.rev20160708.ietftetopology.teterminationpointaugment.DefaultTe; |
| 47 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.topology.rev20160708.ietftetopology.teterminationpointaugment.DefaultTe.TeBuilder; |
| 48 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.topology.rev20160708.ietftetopology.teterminationpointaugment.te.Config; |
| 49 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.topology.rev20160708.ietftetopology.teterminationpointaugment.te.DefaultConfig; |
| 50 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.topology.rev20160708.ietftetopology.teterminationpointaugment.te.DefaultState; |
| 51 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.topology.rev20160708.ietftetopology.teterminationpointaugment.te.State; |
| 52 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.types.rev20160705.ietftetypes.TeTpId; |
| 53 | |
| 54 | import com.google.common.collect.Lists; |
Yixiao Chen | 39828a6 | 2016-09-14 14:37:06 -0400 | [diff] [blame] | 55 | |
| 56 | /** |
| 57 | * The termination point translations. |
| 58 | */ |
| 59 | public final class TerminationPointConverter { |
| 60 | |
Henry Yu | 4b4a7eb | 2016-11-09 20:07:53 -0500 | [diff] [blame] | 61 | private static final String E_NULL_TE_SUBSYSTEM_TP = |
| 62 | "TeSubsystem terminationPoint object cannot be null"; |
| 63 | private static final String E_NULL_YANG_TP = |
| 64 | "YANG terminationPoint object cannot be null"; |
Yixiao Chen | 39828a6 | 2016-09-14 14:37:06 -0400 | [diff] [blame] | 65 | |
| 66 | // no instantiation |
| 67 | private TerminationPointConverter() { |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * TerminationPoint object translation from TE Topology subsystem to YANG. |
| 72 | * |
Henry Yu | 4b4a7eb | 2016-11-09 20:07:53 -0500 | [diff] [blame] | 73 | * @param teSubsystem TE Topology subsystem termination point |
| 74 | * @return Termination point in YANG Java data structure |
Yixiao Chen | 39828a6 | 2016-09-14 14:37:06 -0400 | [diff] [blame] | 75 | */ |
Hesam Rahimi | 39bdc00 | 2016-11-10 15:01:26 -0500 | [diff] [blame^] | 76 | public static TerminationPoint teSubsystem2YangTerminationPoint(org.onosproject.tetopology.management.api.node. |
| 77 | TerminationPoint teSubsystem) { |
| 78 | checkNotNull(teSubsystem, E_NULL_TE_SUBSYSTEM_TP); |
Yixiao Chen | 39828a6 | 2016-09-14 14:37:06 -0400 | [diff] [blame] | 79 | |
Hesam Rahimi | 39bdc00 | 2016-11-10 15:01:26 -0500 | [diff] [blame^] | 80 | TpId tpId = TpId.fromString(teSubsystem.tpId().toString()); |
| 81 | TerminationPoint.TerminationPointBuilder builder = |
| 82 | new DefaultTerminationPoint.TerminationPointBuilder().tpId(tpId); |
| 83 | |
| 84 | if (teSubsystem.supportingTpIds() != null) { |
| 85 | List<SupportingTerminationPoint> tps = Lists.newArrayList(); |
| 86 | SupportingTerminationPoint.SupportingTerminationPointBuilder |
| 87 | spTpBuilder = DefaultSupportingTerminationPoint.builder(); |
| 88 | for (TerminationPointKey tpKey : teSubsystem.supportingTpIds()) { |
| 89 | tps.add(spTpBuilder.networkRef(NetworkId.fromString(tpKey.networkId().toString())) |
| 90 | .nodeRef(NodeId.fromString(tpKey.nodeId().toString())) |
| 91 | .tpRef(TpId.fromString(tpKey.tpId().toString())) |
| 92 | .build()); |
| 93 | } |
| 94 | builder = builder.supportingTerminationPoint(tps); |
| 95 | } |
| 96 | |
| 97 | if (teSubsystem.teTpId() != null) { |
| 98 | AugmentedNtTerminationPoint.AugmentedNtTerminationPointBuilder |
| 99 | tpAugmentBuilder = DefaultAugmentedNtTerminationPoint.builder(); |
| 100 | |
| 101 | TeBuilder yangTeBuilder = DefaultTe.builder(); |
| 102 | |
| 103 | yangTeBuilder = yangTeBuilder.teTpId(TeTpId.fromString((String.valueOf(teSubsystem.teTpId())))); |
| 104 | |
| 105 | Config yConfig = teSubsystem2YangTeAugConfig(teSubsystem); |
| 106 | yangTeBuilder = yangTeBuilder.config(yConfig); |
| 107 | |
| 108 | State yState = teSubsystem2YangTeAugState(teSubsystem); |
| 109 | yangTeBuilder = yangTeBuilder.state(yState); |
| 110 | |
| 111 | tpAugmentBuilder = tpAugmentBuilder.te(yangTeBuilder.build()); |
| 112 | builder.addYangAugmentedInfo(tpAugmentBuilder.build(), AugmentedNtTerminationPoint.class); |
| 113 | } |
| 114 | |
| 115 | return builder.build(); |
Yixiao Chen | 39828a6 | 2016-09-14 14:37:06 -0400 | [diff] [blame] | 116 | } |
| 117 | |
Hesam Rahimi | 39bdc00 | 2016-11-10 15:01:26 -0500 | [diff] [blame^] | 118 | private static State teSubsystem2YangTeAugState(org.onosproject.tetopology.management.api.node. |
| 119 | TerminationPoint teSubsystemTe) { |
| 120 | State.StateBuilder yangStateBuilder = DefaultState.builder(); |
| 121 | // FIXME: interLayerLocks is a list in core but not in yang |
| 122 | // yangStateBuilder = yangStateBuilder.interLayerLockId(teLink.interLayerLocks().get(0)); |
| 123 | |
| 124 | return yangStateBuilder.build(); |
| 125 | } |
| 126 | |
| 127 | private static Config teSubsystem2YangTeAugConfig(org.onosproject.tetopology.management.api.node. |
| 128 | TerminationPoint teSubsystemTe) { |
| 129 | Config.ConfigBuilder yangConfigBuilder = DefaultConfig.builder(); |
| 130 | //FIXME: interLayerLocks is a list in core but not in yang |
| 131 | // yangConfigBuilder = |
| 132 | // yangConfigBuilder.interLayerLockId(teLink.interLayerLocks().get(0)); |
| 133 | |
| 134 | InterfaceSwitchingCapability.InterfaceSwitchingCapabilityBuilder isc = |
| 135 | DefaultInterfaceSwitchingCapability.builder(); |
| 136 | |
| 137 | MaxLspBandwidth.MaxLspBandwidthBuilder maxlspBW = DefaultMaxLspBandwidth |
| 138 | .builder(); |
| 139 | // for (float f : teLink.maxAvialLspBandwidth()) { |
| 140 | // // is converting from float to long ok? |
| 141 | // maxlspBW = maxlspBW.bandwidth(BigDecimal.valueOf((long) f)); |
| 142 | // isc = isc.addToMaxLspBandwidth(maxlspBW.build()); |
| 143 | // } |
| 144 | |
| 145 | yangConfigBuilder = yangConfigBuilder.addToInterfaceSwitchingCapability(isc.build()); |
| 146 | |
| 147 | return yangConfigBuilder.build(); |
| 148 | } |
Yixiao Chen | 39828a6 | 2016-09-14 14:37:06 -0400 | [diff] [blame] | 149 | |
| 150 | /** |
| 151 | * TerminationPoint object translation from YANG to TE Topology subsystem. |
| 152 | * |
Henry Yu | 4b4a7eb | 2016-11-09 20:07:53 -0500 | [diff] [blame] | 153 | * @param yangTp Termination point in YANG Java data structure |
| 154 | * @return TerminationPoint TE Topology subsystem termination point |
Yixiao Chen | 39828a6 | 2016-09-14 14:37:06 -0400 | [diff] [blame] | 155 | */ |
| 156 | public static org.onosproject.tetopology.management.api.node.TerminationPoint |
Hesam Rahimi | 39bdc00 | 2016-11-10 15:01:26 -0500 | [diff] [blame^] | 157 | yang2teSubsystemTerminationPoint(TerminationPoint yangTp) { |
| 158 | checkNotNull(yangTp, E_NULL_YANG_TP); |
Yixiao Chen | 39828a6 | 2016-09-14 14:37:06 -0400 | [diff] [blame] | 159 | |
Hesam Rahimi | 39bdc00 | 2016-11-10 15:01:26 -0500 | [diff] [blame^] | 160 | org.onosproject.tetopology.management.api.node.DefaultTerminationPoint tp = null; |
| 161 | List<org.onosproject.tetopology.management.api.node.TerminationPointKey> spTps = null; |
| 162 | KeyId teTpId = null; |
| 163 | |
| 164 | if (yangTp.supportingTerminationPoint() != null) { |
| 165 | spTps = Lists.newArrayList(); |
| 166 | for (SupportingTerminationPoint yangSptp : yangTp.supportingTerminationPoint()) { |
| 167 | org.onosproject.tetopology.management.api.node.TerminationPointKey tpKey = |
| 168 | new org.onosproject.tetopology.management.api.node.TerminationPointKey( |
| 169 | KeyId.keyId(yangSptp.networkRef().uri().string()), |
| 170 | KeyId.keyId(yangSptp.nodeRef().uri().string()), |
| 171 | KeyId.keyId(yangSptp.tpRef().uri().string())); |
| 172 | spTps.add(tpKey); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | if (yangTp.yangAugmentedInfoMap() != null && !yangTp.yangAugmentedInfoMap().isEmpty()) { |
| 177 | AugmentedNtTerminationPoint yangTpAugment = |
| 178 | (AugmentedNtTerminationPoint) yangTp.yangAugmentedInfo(AugmentedNtTerminationPoint.class); |
| 179 | if (yangTpAugment.te() != null && yangTpAugment.te().teTpId() != null) { |
| 180 | teTpId = KeyId.keyId(yangTpAugment.te().teTpId().toString()); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | tp = new org.onosproject.tetopology.management.api.node |
| 185 | .DefaultTerminationPoint(KeyId.keyId(yangTp.tpId().uri().string()), |
| 186 | spTps, |
| 187 | Long.valueOf(teTpId.toString())); |
| 188 | return tp; |
Yixiao Chen | 39828a6 | 2016-09-14 14:37:06 -0400 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | } |