Yuta HIGUCHI | a255bb4 | 2016-11-03 16:04:08 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Yuta HIGUCHI | a255bb4 | 2016-11-03 16:04:08 -0700 | [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.net.behaviour.protection; |
| 17 | |
| 18 | import static com.google.common.base.Preconditions.checkNotNull; |
Yuta HIGUCHI | dcfa31a | 2016-12-09 19:37:41 -0800 | [diff] [blame] | 19 | import static org.onosproject.net.behaviour.protection.ProtectedTransportEndpointDescription.buildDescription; |
Yuta HIGUCHI | a255bb4 | 2016-11-03 16:04:08 -0700 | [diff] [blame] | 20 | |
| 21 | import java.util.List; |
| 22 | |
| 23 | import org.onosproject.net.DeviceId; |
| 24 | import org.onosproject.net.config.BaseConfig; |
Yuta HIGUCHI | 1d0d9cc | 2016-11-14 16:26:24 -0800 | [diff] [blame] | 25 | import com.fasterxml.jackson.databind.ObjectMapper; |
Yuta HIGUCHI | a255bb4 | 2016-11-03 16:04:08 -0700 | [diff] [blame] | 26 | |
| 27 | // FIXME Move this to Protection handling Intent related package? |
| 28 | /** |
| 29 | * Config object for protection end-point. |
| 30 | * <p> |
| 31 | * Contains equivalent of {@link ProtectedTransportEndpointDescription}. |
| 32 | */ |
| 33 | public class ProtectionConfig |
| 34 | extends BaseConfig<DeviceId> { |
| 35 | |
| 36 | /** |
| 37 | * {@value #CONFIG_KEY} : a netcfg ConfigKey for {@link ProtectionConfig}. |
| 38 | */ |
| 39 | public static final String CONFIG_KEY = "protection"; |
| 40 | |
| 41 | /** |
| 42 | * JSON key for paths. |
| 43 | * <p> |
| 44 | * Value is list of {@link TransportEndpointDescription} in JSON. |
| 45 | */ |
| 46 | private static final String PATHS = "paths"; |
| 47 | /** |
| 48 | * JSON key for Peer {@link DeviceId}. |
| 49 | */ |
| 50 | private static final String PEER = "peer"; |
| 51 | private static final String FINGERPRINT = "fingerprint"; |
| 52 | |
| 53 | |
| 54 | @Override |
| 55 | public boolean isValid() { |
| 56 | return isString(PEER, FieldPresence.MANDATORY) && |
| 57 | isString(FINGERPRINT, FieldPresence.MANDATORY) && |
| 58 | hasField(PATHS); |
| 59 | } |
| 60 | |
| 61 | |
| 62 | /** |
| 63 | * Returns List of underlying transport entity endpoints in priority order. |
| 64 | * |
| 65 | * @return the transport entity endpoint descriptions |
| 66 | */ |
| 67 | public List<TransportEndpointDescription> paths() { |
| 68 | return getList(PATHS, |
| 69 | jsonStr -> decode(jsonStr, TransportEndpointDescription.class)); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Sets the List of underlying transport entity endpoints in priority order. |
| 74 | * |
| 75 | * @param paths the transport entity endpoint descriptions |
| 76 | * @return self |
| 77 | */ |
| 78 | public ProtectionConfig paths(List<TransportEndpointDescription> paths) { |
| 79 | setList(PATHS, |
| 80 | elm -> encode(elm, TransportEndpointDescription.class).toString(), |
| 81 | paths); |
| 82 | return this; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Returns DeviceId of remote peer of this endpoint. |
| 87 | * |
| 88 | * @return the peer |
| 89 | */ |
| 90 | public DeviceId peer() { |
| 91 | return DeviceId.deviceId(get(PEER, "")); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Sets the DeviceId of remote peer of this endpoint. |
| 96 | * |
| 97 | * @param peer DeviceId |
| 98 | * @return self |
| 99 | */ |
| 100 | public ProtectionConfig peer(DeviceId peer) { |
| 101 | setOrClear(PEER, peer.toString()); |
| 102 | return this; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Returns fingerprint to identify this protected transport entity. |
| 107 | * |
| 108 | * @return the fingerprint |
| 109 | */ |
| 110 | public String fingerprint() { |
| 111 | return get(FINGERPRINT, ""); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Sets the fingerprint to identify this protected transport entity. |
| 116 | * |
| 117 | * @param fingerprint the fingerprint |
| 118 | * @return self |
| 119 | */ |
| 120 | public ProtectionConfig fingerprint(String fingerprint) { |
| 121 | setOrClear(FINGERPRINT, checkNotNull(fingerprint)); |
| 122 | return this; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Returns equivalent of this Config as {@link ProtectedTransportEndpointDescription}. |
| 127 | * |
| 128 | * @return {@link ProtectedTransportEndpointDescription} |
| 129 | */ |
| 130 | public ProtectedTransportEndpointDescription asDescription() { |
Yuta HIGUCHI | dcfa31a | 2016-12-09 19:37:41 -0800 | [diff] [blame] | 131 | return buildDescription(paths(), peer(), fingerprint()); |
Yuta HIGUCHI | a255bb4 | 2016-11-03 16:04:08 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public String toString() { |
| 136 | return object.toString(); |
| 137 | } |
Yuta HIGUCHI | 1d0d9cc | 2016-11-14 16:26:24 -0800 | [diff] [blame] | 138 | |
| 139 | /** |
| 140 | * Create a {@link ProtectionConfig}. |
| 141 | * <p> |
| 142 | * Note: created instance needs to be initialized by #init(..) before using. |
| 143 | */ |
| 144 | public ProtectionConfig() { |
| 145 | super(); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Create a {@link ProtectionConfig} for specified Device. |
| 150 | * <p> |
| 151 | * Note: created instance is not bound to NetworkConfigService, |
| 152 | * cannot use {@link #apply()}. Must be passed to the service |
| 153 | * using NetworkConfigService#applyConfig |
| 154 | * |
| 155 | * @param did DeviceId |
| 156 | */ |
| 157 | public ProtectionConfig(DeviceId did) { |
| 158 | ObjectMapper mapper = new ObjectMapper(); |
| 159 | init(did, CONFIG_KEY, mapper.createObjectNode(), mapper, null); |
| 160 | } |
Yuta HIGUCHI | a255bb4 | 2016-11-03 16:04:08 -0700 | [diff] [blame] | 161 | } |