001/* 002 * Copyright 2013-2020 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright 2013-2020 Ping Identity Corporation 007 * 008 * Licensed under the Apache License, Version 2.0 (the "License"); 009 * you may not use this file except in compliance with the License. 010 * You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, software 015 * distributed under the License is distributed on an "AS IS" BASIS, 016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 017 * See the License for the specific language governing permissions and 018 * limitations under the License. 019 */ 020/* 021 * Copyright (C) 2015-2020 Ping Identity Corporation 022 * 023 * This program is free software; you can redistribute it and/or modify 024 * it under the terms of the GNU General Public License (GPLv2 only) 025 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only) 026 * as published by the Free Software Foundation. 027 * 028 * This program is distributed in the hope that it will be useful, 029 * but WITHOUT ANY WARRANTY; without even the implied warranty of 030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 031 * GNU General Public License for more details. 032 * 033 * You should have received a copy of the GNU General Public License 034 * along with this program; if not, see <http://www.gnu.org/licenses>. 035 */ 036package com.unboundid.ldap.sdk.unboundidds.controls; 037 038 039 040import java.io.Serializable; 041import java.util.ArrayList; 042 043import com.unboundid.asn1.ASN1Element; 044import com.unboundid.asn1.ASN1Enumerated; 045import com.unboundid.asn1.ASN1Integer; 046import com.unboundid.asn1.ASN1Sequence; 047import com.unboundid.ldap.sdk.LDAPException; 048import com.unboundid.ldap.sdk.ResultCode; 049import com.unboundid.util.Debug; 050import com.unboundid.util.NotMutable; 051import com.unboundid.util.StaticUtils; 052import com.unboundid.util.ThreadSafety; 053import com.unboundid.util.ThreadSafetyLevel; 054 055import static com.unboundid.ldap.sdk.unboundidds.controls.ControlMessages.*; 056 057 058 059/** 060 * This class defines a data structure that provides information about the 061 * result of assured replication processing, either on a replication server (if 062 * that is all that is needed to satisfy the desired level of assurance) or 063 * on a directory server (if required by the desired level of assurance). 064 * <BR> 065 * <BLOCKQUOTE> 066 * <B>NOTE:</B> This class, and other classes within the 067 * {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only 068 * supported for use against Ping Identity, UnboundID, and 069 * Nokia/Alcatel-Lucent 8661 server products. These classes provide support 070 * for proprietary functionality or for external specifications that are not 071 * considered stable or mature enough to be guaranteed to work in an 072 * interoperable way with other types of LDAP servers. 073 * </BLOCKQUOTE> 074 */ 075@NotMutable() 076@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 077public final class AssuredReplicationServerResult 078 implements Serializable 079{ 080 /** 081 * The BER type for the result code element. 082 */ 083 private static final byte TYPE_RESULT_CODE = (byte) 0x80; 084 085 086 /** 087 * The BER type for the server ID element. 088 */ 089 private static final byte TYPE_SERVER_ID = (byte) 0x81; 090 091 092 /** 093 * The BER type for the replica ID element. 094 */ 095 private static final byte TYPE_REPLICA_ID = (byte) 0x82; 096 097 098 099 /** 100 * The serial version UID for this serializable class. 101 */ 102 private static final long serialVersionUID = 3015162215769386343L; 103 104 105 106 // The result code for this server result. 107 private final AssuredReplicationServerResultCode resultCode; 108 109 // The replica ID of the associated directory server. 110 private final Short replicaID; 111 112 // The server ID of the associated replication server. 113 private final Short replicationServerID; 114 115 116 117 /** 118 * Creates a new assured replication server result with the provided 119 * information. 120 * 121 * @param resultCode The result code that indicates the state of 122 * assurance processing for the associated 123 * replication server and/or directory server. 124 * It must not be {@code null}. 125 * @param replicationServerID The server ID of the replication server from 126 * which this server result was obtained. It may 127 * be {@code null} if no replication server ID is 128 * available for this result. 129 * @param replicaID The replica ID of the directory server with 130 * which this result is associated. It may be 131 * {@code null} if no replica ID is available 132 * for this result. 133 */ 134 public AssuredReplicationServerResult( 135 final AssuredReplicationServerResultCode resultCode, 136 final Short replicationServerID, 137 final Short replicaID) 138 { 139 this.resultCode = resultCode; 140 this.replicationServerID = replicationServerID; 141 this.replicaID = replicaID; 142 } 143 144 145 146 /** 147 * Retrieves the result code that indicates the state of assurance processing 148 * for this server result. 149 * 150 * @return The result code for this server result. 151 */ 152 public AssuredReplicationServerResultCode getResultCode() 153 { 154 return resultCode; 155 } 156 157 158 159 /** 160 * Retrieves the server ID for the replication server from which this server 161 * result was obtained, if available. 162 * 163 * @return The server ID for the replication server from which this server 164 * result was obtained, or {@code null} if no replication server ID 165 * is available. 166 */ 167 public Short getReplicationServerID() 168 { 169 return replicationServerID; 170 } 171 172 173 174 /** 175 * Retrieves the replica ID for the directory server with which this server 176 * result is associated, if applicable. 177 * 178 * @return The replica ID for the directory server with which this server 179 * result is associated, or {@code null} if there is no associated 180 * directory server. 181 */ 182 public Short getReplicaID() 183 { 184 return replicaID; 185 } 186 187 188 189 /** 190 * Encodes this assured replication server result to an ASN.1 element suitable 191 * for use in a {@link AssuredReplicationResponseControl}. 192 * 193 * @return The encoded representation of this assured replication server 194 * result. 195 */ 196 ASN1Element encode() 197 { 198 final ArrayList<ASN1Element> elements = new ArrayList<>(3); 199 200 elements.add(new ASN1Enumerated(TYPE_RESULT_CODE, resultCode.intValue())); 201 202 if (replicationServerID != null) 203 { 204 elements.add(new ASN1Integer(TYPE_SERVER_ID, replicationServerID)); 205 } 206 207 if (replicaID != null) 208 { 209 elements.add(new ASN1Integer(TYPE_REPLICA_ID, replicaID)); 210 } 211 212 return new ASN1Sequence(elements); 213 } 214 215 216 217 /** 218 * Decodes the provided ASN.1 element as an assured replication server 219 * result. 220 * 221 * @param element The ASN.1 element to be decoded. It must not be 222 * {@code null}. 223 * 224 * @return The decoded assured replication server result. 225 * 226 * @throws LDAPException If a problem is encountered while attempting to 227 * decode the provided ASN.1 element as an assured 228 * replication server result. 229 */ 230 static AssuredReplicationServerResult decode(final ASN1Element element) 231 throws LDAPException 232 { 233 AssuredReplicationServerResultCode resultCode = null; 234 Short serverID = null; 235 Short replicaID = null; 236 237 try 238 { 239 for (final ASN1Element e : 240 ASN1Sequence.decodeAsSequence(element).elements()) 241 { 242 switch (e.getType()) 243 { 244 case TYPE_RESULT_CODE: 245 final int rcValue = ASN1Enumerated.decodeAsEnumerated(e).intValue(); 246 resultCode = AssuredReplicationServerResultCode.valueOf(rcValue); 247 if (resultCode == null) 248 { 249 throw new LDAPException(ResultCode.DECODING_ERROR, 250 ERR_ASSURED_REPLICATION_SERVER_RESULT_INVALID_RESULT_CODE. 251 get(rcValue)); 252 } 253 break; 254 255 case TYPE_SERVER_ID: 256 serverID = (short) ASN1Integer.decodeAsInteger(e).intValue(); 257 break; 258 259 case TYPE_REPLICA_ID: 260 replicaID = (short) ASN1Integer.decodeAsInteger(e).intValue(); 261 break; 262 263 default: 264 throw new LDAPException(ResultCode.DECODING_ERROR, 265 ERR_ASSURED_REPLICATION_SERVER_RESULT_UNEXPECTED_ELEMENT_TYPE. 266 get(StaticUtils.toHex(e.getType()))); 267 } 268 } 269 } 270 catch (final LDAPException le) 271 { 272 Debug.debugException(le); 273 throw le; 274 } 275 catch (final Exception e) 276 { 277 Debug.debugException(e); 278 throw new LDAPException(ResultCode.DECODING_ERROR, 279 ERR_ASSURED_REPLICATION_SERVER_RESULT_CANNOT_DECODE.get( 280 StaticUtils.getExceptionMessage(e)), 281 e); 282 } 283 284 if (resultCode == null) 285 { 286 throw new LDAPException(ResultCode.DECODING_ERROR, 287 ERR_ASSURED_REPLICATION_SERVER_RESULT_NO_RESULT_CODE.get()); 288 } 289 290 return new AssuredReplicationServerResult(resultCode, serverID, replicaID); 291 } 292 293 294 295 /** 296 * Retrieves a string representation of this assured replication server 297 * result. 298 * 299 * @return A string representation of this assured replication server 300 * result. 301 */ 302 @Override() 303 public String toString() 304 { 305 final StringBuilder buffer = new StringBuilder(); 306 toString(buffer); 307 return buffer.toString(); 308 } 309 310 311 312 /** 313 * Appends a string representation of this assured replication server result 314 * to the provided buffer. 315 * 316 * @param buffer The buffer to which the information should be appended. 317 */ 318 public void toString(final StringBuilder buffer) 319 { 320 buffer.append("AssuredReplicationServerResult(resultCode="); 321 buffer.append(resultCode.name()); 322 323 if (replicationServerID != null) 324 { 325 buffer.append(", replicationServerID="); 326 buffer.append(replicationServerID); 327 } 328 329 if (replicaID != null) 330 { 331 buffer.append(", replicaID="); 332 buffer.append(replicaID); 333 } 334 335 buffer.append(')'); 336 } 337}