Yuta HIGUCHI | 66e35f4 | 2017-03-11 14:12:05 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Yuta HIGUCHI | 66e35f4 | 2017-03-11 14:12:05 -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 | package org.onosproject.driver.optical.config; |
| 17 | |
| 18 | import static org.slf4j.LoggerFactory.getLogger; |
| 19 | |
| 20 | import java.util.Optional; |
| 21 | |
| 22 | import org.onosproject.net.ChannelSpacing; |
| 23 | import org.onosproject.net.ConnectPoint; |
| 24 | import org.onosproject.net.GridType; |
| 25 | import org.onosproject.net.config.BaseConfig; |
| 26 | import org.slf4j.Logger; |
| 27 | |
| 28 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 29 | import com.google.common.annotations.Beta; |
| 30 | |
| 31 | /** |
| 32 | * Configuration about available lambda resource on a port. |
| 33 | */ |
| 34 | @Beta |
| 35 | public class LambdaConfig extends BaseConfig<ConnectPoint> { |
| 36 | |
| 37 | private static final Logger log = getLogger(LambdaConfig.class); |
| 38 | |
| 39 | /** |
| 40 | * Configuration key for {@link LambdaConfig}. ({@value #CONFIG_KEY}) |
| 41 | */ |
| 42 | public static final String CONFIG_KEY = "lambdas"; |
| 43 | |
| 44 | /** |
| 45 | * JSON key for GridType. {@value #GRID_TYPE}. |
| 46 | * Expects a string containing one of {@link GridType} element. |
| 47 | */ |
| 48 | private static final String GRID_TYPE = "gridType"; |
| 49 | |
| 50 | /** |
| 51 | * JSON key for DWDM ChannelSpacing. {@value #DWDM_SPACING}. |
| 52 | * Expects a string containing one of {@link ChannelSpacing} element. |
| 53 | */ |
| 54 | private static final String DWDM_SPACING = "dwdmSpacing"; |
| 55 | |
| 56 | /** |
| 57 | * JSON key for start value of slot index (inclusive). {@value #SLOT_START}. |
| 58 | * |
| 59 | * Expects an integer value. (default:1). |
| 60 | * @see OchSignal#newDwdmSlot(ChannelSpacing, int) |
| 61 | * @see OchSignal#newFlexGridSlot(int) |
| 62 | */ |
| 63 | private static final String SLOT_START = "slotStart"; |
| 64 | |
| 65 | /** |
| 66 | * JSON key for step value of slot index. {@value #SLOT_START}. |
| 67 | * |
| 68 | * Expects a positive integer value. (default:1). |
| 69 | * @see OchSignal#newDwdmSlot(ChannelSpacing, int) |
| 70 | * @see OchSignal#newFlexGridSlot(int) |
| 71 | */ |
| 72 | private static final String SLOT_STEP = "slotStep"; |
| 73 | |
| 74 | /** |
| 75 | * JSON key for end value of slot index (inclusive). {@value #SLOT_START}. |
| 76 | * |
| 77 | * Expects an integer value. (default:1). |
| 78 | * @see OchSignal#newDwdmSlot(ChannelSpacing, int) |
| 79 | * @see OchSignal#newFlexGridSlot(int) |
| 80 | */ |
| 81 | private static final String SLOT_END = "slotEnd"; |
| 82 | |
| 83 | @Override |
| 84 | public boolean isValid() { |
| 85 | if (!hasField(GRID_TYPE)) { |
| 86 | log.warn("GridType missing: {}", this); |
| 87 | return false; |
| 88 | } |
| 89 | try { |
| 90 | GridType type = gridType(); |
| 91 | Optional<ChannelSpacing> dwdmSpacing = dwdmSpacing(); |
| 92 | if (type == GridType.DWDM) { |
| 93 | if (!dwdmSpacing.isPresent()) { |
| 94 | log.warn("DWDM spacing missing: {}", this); |
| 95 | return false; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | return slotStep() > 0 && |
| 100 | slotStart() <= slotEnd(); |
| 101 | } catch (NullPointerException | IllegalArgumentException e) { |
| 102 | log.warn("Invalid netcfg: {}", this, e); |
| 103 | return false; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Returns available lambda resource type. |
| 109 | * |
| 110 | * @return grid type |
| 111 | */ |
| 112 | public GridType gridType() { |
| 113 | return GridType.valueOf(get(GRID_TYPE, null)); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Returns DWDM channel spacing. |
| 118 | * |
| 119 | * @return channel spacing if specified |
| 120 | */ |
| 121 | public Optional<ChannelSpacing> dwdmSpacing() { |
| 122 | return Optional.ofNullable(get(DWDM_SPACING, null)) |
| 123 | .map(ChannelSpacing::valueOf); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Returns start of slot index. (inclusive) |
| 128 | * |
| 129 | * @return start of slot index |
| 130 | */ |
| 131 | public int slotStart() { |
| 132 | return get(SLOT_START, 1); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Returns positive incremental step of slot index. |
| 137 | * |
| 138 | * @return step of slot index |
| 139 | */ |
| 140 | public int slotStep() { |
| 141 | return get(SLOT_STEP, 1); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Returns end of slot index. (inclusive) |
| 146 | * |
| 147 | * @return end of slot index |
| 148 | */ |
| 149 | public int slotEnd() { |
| 150 | return get(SLOT_END, 1); |
| 151 | } |
| 152 | |
| 153 | |
| 154 | /** |
| 155 | * Create a {@link LambdaConfig}. |
| 156 | * <p> |
| 157 | * Note: created instance needs to be initialized by #init(..) before using. |
| 158 | */ |
| 159 | public LambdaConfig() { |
| 160 | super(); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Create a {@link LambdaConfig} for specified Port. |
| 165 | * <p> |
| 166 | * Note: created instance is not bound to NetworkConfigService, |
| 167 | * cannot use {@link #apply()}. Must be passed to the service |
| 168 | * using NetworkConfigService#applyConfig |
| 169 | * |
| 170 | * @param cp ConnectPoint for a port |
| 171 | */ |
| 172 | public LambdaConfig(ConnectPoint cp) { |
| 173 | ObjectMapper mapper = new ObjectMapper(); |
| 174 | init(cp, CONFIG_KEY, mapper.createObjectNode(), mapper, null); |
| 175 | } |
| 176 | |
| 177 | } |