blob: 94ed96598067cea6903d82bbe27b9a7c663fab47 [file] [log] [blame]
Yuta HIGUCHIa255bb42016-11-03 16:04:08 -07001/*
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 */
16package org.onosproject.net.behaviour.protection;
17
18import static com.google.common.base.Preconditions.checkNotNull;
19
20import java.util.List;
21
22import org.onosproject.net.DeviceId;
23import org.onosproject.net.config.BaseConfig;
24
25// FIXME Move this to Protection handling Intent related package?
26/**
27 * Config object for protection end-point.
28 * <p>
29 * Contains equivalent of {@link ProtectedTransportEndpointDescription}.
30 */
31public class ProtectionConfig
32 extends BaseConfig<DeviceId> {
33
34 /**
35 * {@value #CONFIG_KEY} : a netcfg ConfigKey for {@link ProtectionConfig}.
36 */
37 public static final String CONFIG_KEY = "protection";
38
39 /**
40 * JSON key for paths.
41 * <p>
42 * Value is list of {@link TransportEndpointDescription} in JSON.
43 */
44 private static final String PATHS = "paths";
45 /**
46 * JSON key for Peer {@link DeviceId}.
47 */
48 private static final String PEER = "peer";
49 private static final String FINGERPRINT = "fingerprint";
50
51
52 @Override
53 public boolean isValid() {
54 return isString(PEER, FieldPresence.MANDATORY) &&
55 isString(FINGERPRINT, FieldPresence.MANDATORY) &&
56 hasField(PATHS);
57 }
58
59
60 /**
61 * Returns List of underlying transport entity endpoints in priority order.
62 *
63 * @return the transport entity endpoint descriptions
64 */
65 public List<TransportEndpointDescription> paths() {
66 return getList(PATHS,
67 jsonStr -> decode(jsonStr, TransportEndpointDescription.class));
68 }
69
70 /**
71 * Sets the List of underlying transport entity endpoints in priority order.
72 *
73 * @param paths the transport entity endpoint descriptions
74 * @return self
75 */
76 public ProtectionConfig paths(List<TransportEndpointDescription> paths) {
77 setList(PATHS,
78 elm -> encode(elm, TransportEndpointDescription.class).toString(),
79 paths);
80 return this;
81 }
82
83 /**
84 * Returns DeviceId of remote peer of this endpoint.
85 *
86 * @return the peer
87 */
88 public DeviceId peer() {
89 return DeviceId.deviceId(get(PEER, ""));
90 }
91
92 /**
93 * Sets the DeviceId of remote peer of this endpoint.
94 *
95 * @param peer DeviceId
96 * @return self
97 */
98 public ProtectionConfig peer(DeviceId peer) {
99 setOrClear(PEER, peer.toString());
100 return this;
101 }
102
103 /**
104 * Returns fingerprint to identify this protected transport entity.
105 *
106 * @return the fingerprint
107 */
108 public String fingerprint() {
109 return get(FINGERPRINT, "");
110 }
111
112 /**
113 * Sets the fingerprint to identify this protected transport entity.
114 *
115 * @param fingerprint the fingerprint
116 * @return self
117 */
118 public ProtectionConfig fingerprint(String fingerprint) {
119 setOrClear(FINGERPRINT, checkNotNull(fingerprint));
120 return this;
121 }
122
123 /**
124 * Returns equivalent of this Config as {@link ProtectedTransportEndpointDescription}.
125 *
126 * @return {@link ProtectedTransportEndpointDescription}
127 */
128 public ProtectedTransportEndpointDescription asDescription() {
129 return ProtectedTransportEndpointDescription.of(paths(), peer(), fingerprint());
130 }
131
132 @Override
133 public String toString() {
134 return object.toString();
135 }
136}