Pier Ventre | 6b19e48 | 2016-11-07 16:21:04 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0947d7e | 2017-08-03 21:12:30 -0700 | [diff] [blame^] | 2 | * Copyright 2016-present Open Networking Foundation |
Pier Ventre | 6b19e48 | 2016-11-07 16:21:04 -0800 | [diff] [blame] | 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.segmentrouting.config; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import com.google.common.collect.ImmutableSet; |
| 22 | import org.onlab.packet.MplsLabel; |
| 23 | import org.onlab.packet.VlanId; |
| 24 | import org.onosproject.core.ApplicationId; |
| 25 | import org.onosproject.net.ConnectPoint; |
| 26 | import org.onosproject.net.config.Config; |
| 27 | import org.onosproject.segmentrouting.pwaas.DefaultL2Tunnel; |
| 28 | import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelDescription; |
| 29 | import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelPolicy; |
| 30 | import org.onosproject.segmentrouting.pwaas.L2Mode; |
| 31 | import org.slf4j.Logger; |
| 32 | import org.slf4j.LoggerFactory; |
| 33 | |
| 34 | import java.util.Set; |
| 35 | |
| 36 | /** |
| 37 | * App configuration object for Pwaas. |
| 38 | */ |
| 39 | public class PwaasConfig extends Config<ApplicationId> { |
| 40 | |
| 41 | private static Logger log = LoggerFactory |
| 42 | .getLogger(PwaasConfig.class); |
| 43 | |
| 44 | private static final String SRC_CP = "cP1"; |
| 45 | private static final String DST_CP = "cP2"; |
| 46 | private static final String SRC_OUTER_TAG = "cP1OuterTag"; |
| 47 | private static final String DST_OUTER_TAG = "cP2OuterTag"; |
| 48 | private static final String SRC_INNER_TAG = "cP1InnerTag"; |
| 49 | private static final String DST_INNER_TAG = "cP2InnerTag"; |
| 50 | private static final String MODE = "mode"; |
| 51 | private static final String ALL_VLAN = "allVlan"; |
| 52 | private static final String SD_TAG = "sdTag"; |
| 53 | private static final String PW_LABEL = "pwLabel"; |
| 54 | |
| 55 | /** |
| 56 | * Error message for missing parameters. |
| 57 | */ |
| 58 | private static final String MISSING_PARAMS = "Missing parameters in pseudo wire description"; |
| 59 | |
| 60 | /** |
| 61 | * Error message for invalid l2 mode. |
| 62 | */ |
| 63 | private static final String INVALID_L2_MODE = "Invalid pseudo wire mode"; |
| 64 | |
| 65 | /** |
| 66 | * Verify if the pwaas configuration block is valid. |
| 67 | * |
| 68 | * @return true, if the configuration block is valid. |
| 69 | * False otherwise. |
| 70 | */ |
| 71 | @Override |
| 72 | public boolean isValid() { |
| 73 | try { |
| 74 | getPwIds().forEach(this::getPwDescription); |
| 75 | } catch (IllegalArgumentException e) { |
| 76 | log.warn("{}", e.getMessage()); |
| 77 | return false; |
| 78 | } |
| 79 | return true; |
| 80 | |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Returns all pseudo wire keys. |
| 85 | * |
| 86 | * @return all keys (tunnels id) |
| 87 | * @throws IllegalArgumentException if wrong format |
| 88 | */ |
| 89 | public Set<Long> getPwIds() { |
| 90 | ImmutableSet.Builder<Long> builder = ImmutableSet.builder(); |
| 91 | object.fields().forEachRemaining(entry -> { |
| 92 | Long tunnelId = Long.parseLong(entry.getKey()); |
| 93 | builder.add(tunnelId); |
| 94 | }); |
| 95 | return builder.build(); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Returns pw description of given pseudo wire id. |
| 100 | * |
| 101 | * @param tunnelId pseudo wire key |
| 102 | * @return set of l2 tunnel descriptions |
| 103 | * @throws IllegalArgumentException if wrong format |
| 104 | */ |
| 105 | public DefaultL2TunnelDescription getPwDescription(Long tunnelId) { |
| 106 | JsonNode pwDescription = object.get(tunnelId.toString()); |
| 107 | if (!hasFields((ObjectNode) pwDescription, |
| 108 | SRC_CP, SRC_INNER_TAG, SRC_OUTER_TAG, |
| 109 | DST_CP, DST_INNER_TAG, DST_OUTER_TAG, |
| 110 | MODE, ALL_VLAN, SD_TAG, PW_LABEL)) { |
| 111 | throw new IllegalArgumentException(MISSING_PARAMS); |
| 112 | } |
| 113 | String tempString; |
| 114 | |
| 115 | tempString = pwDescription.get(SRC_CP).asText(); |
| 116 | ConnectPoint srcCp = ConnectPoint.deviceConnectPoint(tempString); |
| 117 | |
| 118 | tempString = pwDescription.get(DST_CP).asText(); |
| 119 | ConnectPoint dstCp = ConnectPoint.deviceConnectPoint(tempString); |
| 120 | |
| 121 | tempString = pwDescription.get(SRC_INNER_TAG).asText(); |
| 122 | VlanId srcInnerTag = VlanId.vlanId(tempString); |
| 123 | |
| 124 | tempString = pwDescription.get(SRC_OUTER_TAG).asText(); |
| 125 | VlanId srcOuterTag = VlanId.vlanId(tempString); |
| 126 | |
| 127 | tempString = pwDescription.get(DST_INNER_TAG).asText(); |
| 128 | VlanId dstInnerTag = VlanId.vlanId(tempString); |
| 129 | |
| 130 | tempString = pwDescription.get(DST_OUTER_TAG).asText(); |
| 131 | VlanId dstOuterTag = VlanId.vlanId(tempString); |
| 132 | |
| 133 | tempString = pwDescription.get(MODE).asText(); |
| 134 | |
| 135 | L2Mode l2Mode = L2Mode.valueOf(tempString); |
| 136 | |
| 137 | boolean allVlan = pwDescription.get(ALL_VLAN).asBoolean(); |
| 138 | |
| 139 | tempString = pwDescription.get(SD_TAG).asText(); |
| 140 | VlanId sdTag = VlanId.vlanId(tempString); |
| 141 | |
| 142 | tempString = pwDescription.get(PW_LABEL).asText(); |
| 143 | MplsLabel pwLabel = MplsLabel.mplsLabel(tempString); |
| 144 | |
| 145 | DefaultL2Tunnel l2Tunnel = new DefaultL2Tunnel( |
| 146 | l2Mode, |
| 147 | sdTag, |
| 148 | tunnelId, |
| 149 | pwLabel |
| 150 | ); |
| 151 | |
| 152 | DefaultL2TunnelPolicy l2TunnelPolicy = new DefaultL2TunnelPolicy( |
| 153 | tunnelId, |
| 154 | srcCp, |
| 155 | srcInnerTag, |
| 156 | srcOuterTag, |
| 157 | dstCp, |
| 158 | dstInnerTag, |
| 159 | dstOuterTag, |
| 160 | allVlan |
| 161 | ); |
| 162 | |
| 163 | return new DefaultL2TunnelDescription(l2Tunnel, l2TunnelPolicy); |
| 164 | } |
| 165 | |
| 166 | } |