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