1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
|
# Copyright (c) 2014 The Johns Hopkins University/Applied Physics Laboratory
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from kmip.core import enums
from kmip.core import objects
from kmip.core import utils
from kmip.core.primitives import Struct
from kmip.core.primitives import Integer
from kmip.core.primitives import Enumeration
from kmip.core.primitives import Boolean
from kmip.core.primitives import TextString
from kmip.core.primitives import ByteString
from kmip.core.primitives import DateTime
# 6.1
class ProtocolVersion(Struct):
class ProtocolVersionMajor(Integer):
def __init__(self, value=None):
super(ProtocolVersion.ProtocolVersionMajor, self).\
__init__(value, enums.Tags.PROTOCOL_VERSION_MAJOR)
class ProtocolVersionMinor(Integer):
def __init__(self, value=None):
super(ProtocolVersion.ProtocolVersionMinor, self).\
__init__(value, enums.Tags.PROTOCOL_VERSION_MINOR)
def __init__(self,
protocol_version_major=None,
protocol_version_minor=None):
super(ProtocolVersion, self).__init__(enums.Tags.PROTOCOL_VERSION)
if protocol_version_major is None:
self.protocol_version_major = \
ProtocolVersion.ProtocolVersionMajor()
else:
self.protocol_version_major = protocol_version_major
if protocol_version_minor is None:
self.protocol_version_minor = \
ProtocolVersion.ProtocolVersionMinor()
else:
self.protocol_version_minor = protocol_version_minor
self.validate()
def read(self, istream):
super(ProtocolVersion, self).read(istream)
tstream = utils.BytearrayStream(istream.read(self.length))
# Read the major and minor portions of the version number
self.protocol_version_major.read(tstream)
self.protocol_version_minor.read(tstream)
self.is_oversized(tstream)
def write(self, ostream):
tstream = utils.BytearrayStream()
# Write the major and minor portions of the protocol version
self.protocol_version_major.write(tstream)
self.protocol_version_minor.write(tstream)
# Write the length and value of the protocol version
self.length = tstream.length()
super(ProtocolVersion, self).write(ostream)
ostream.write(tstream.buffer)
def validate(self):
self.__validate()
def __validate(self):
if not isinstance(self.protocol_version_major,
ProtocolVersion.ProtocolVersionMajor):
msg = "invalid protocol version major"
msg += "; expected {0}, received {1}".format(
ProtocolVersion.ProtocolVersionMajor,
self.protocol_version_major)
raise TypeError(msg)
if not isinstance(self.protocol_version_minor,
ProtocolVersion.ProtocolVersionMinor):
msg = "invalid protocol version minor"
msg += "; expected {0}, received {1}".format(
ProtocolVersion.ProtocolVersionMinor,
self.protocol_version_minor)
raise TypeError(msg)
def __eq__(self, other):
if isinstance(other, ProtocolVersion):
if ((self.protocol_version_major ==
other.protocol_version_major) and
(self.protocol_version_minor ==
other.protocol_version_minor)):
return True
else:
return False
else:
return NotImplemented
def __ne__(self, other):
if isinstance(other, ProtocolVersion):
return not self.__eq__(other)
else:
return NotImplemented
def __lt__(self, other):
if isinstance(other, ProtocolVersion):
if self.protocol_version_major < other.protocol_version_major:
return True
elif self.protocol_version_major > other.protocol_version_major:
return False
elif self.protocol_version_minor < other.protocol_version_minor:
return True
else:
return False
else:
return NotImplemented
def __gt__(self, other):
if isinstance(other, ProtocolVersion):
if self.protocol_version_major > other.protocol_version_major:
return True
elif self.protocol_version_major < other.protocol_version_major:
return False
elif self.protocol_version_minor > other.protocol_version_minor:
return True
else:
return False
else:
return NotImplemented
def __le__(self, other):
if isinstance(other, ProtocolVersion):
if self.protocol_version_major < other.protocol_version_major:
return True
elif self.protocol_version_major > other.protocol_version_major:
return False
elif self.protocol_version_minor <= other.protocol_version_minor:
return True
else:
return False
else:
return NotImplemented
def __ge__(self, other):
if isinstance(other, ProtocolVersion):
if self.protocol_version_major > other.protocol_version_major:
return True
elif self.protocol_version_major < other.protocol_version_major:
return False
elif self.protocol_version_minor >= other.protocol_version_minor:
return True
else:
return False
else:
return NotImplemented
def __repr__(self):
major = self.protocol_version_major.value
minor = self.protocol_version_minor.value
return "{0}.{1}".format(major, minor)
@classmethod
def create(cls, major, minor):
major = cls.ProtocolVersionMajor(major)
minor = cls.ProtocolVersionMinor(minor)
return ProtocolVersion(major, minor)
# 6.2
class Operation(Enumeration):
def __init__(self, value=None):
super(Operation, self).__init__(
enums.Operation, value, enums.Tags.OPERATION)
# 6.3
class MaximumResponseSize(Integer):
def __init__(self, value=None):
super(MaximumResponseSize, self).\
__init__(value, enums.Tags.MAXIMUM_RESPONSE_SIZE)
# 6.4
class UniqueBatchItemID(ByteString):
def __init__(self, value=None):
super(UniqueBatchItemID, self)\
.__init__(value, enums.Tags.UNIQUE_BATCH_ITEM_ID)
# 6.5
class TimeStamp(DateTime):
def __init__(self, value=None):
super(TimeStamp, self).__init__(value, enums.Tags.TIME_STAMP)
# 6.6
class Authentication(Struct):
def __init__(self, credential=None):
super(Authentication, self).__init__(enums.Tags.AUTHENTICATION)
self.credential = credential
def read(self, istream):
super(Authentication, self).read(istream)
tstream = utils.BytearrayStream(istream.read(self.length))
# Read the credential
self.credential = objects.Credential()
self.credential.read(tstream)
self.is_oversized(tstream)
def write(self, ostream):
tstream = utils.BytearrayStream()
# Write the credential
self.credential.write(tstream)
# Write the length and value of the protocol version
self.length = tstream.length()
super(Authentication, self).write(ostream)
ostream.write(tstream.buffer)
def validate(self):
# TODO (peter-hamilton) Finish implementation.
pass
# 6.7
class AsynchronousIndicator(Boolean):
def __init__(self, value=None):
super(AsynchronousIndicator, self).\
__init__(value, enums.Tags.ASYNCHRONOUS_INDICATOR)
# 6.8
class AsynchronousCorrelationValue(ByteString):
def __init__(self, value=None):
super(AsynchronousCorrelationValue, self).\
__init__(value, enums.Tags.ASYNCHRONOUS_CORRELATION_VALUE)
# 6.9
class ResultStatus(Enumeration):
def __init__(self, value=None):
super(ResultStatus, self).__init__(
enums.ResultStatus, value, enums.Tags.RESULT_STATUS)
# 6.10
class ResultReason(Enumeration):
def __init__(self, value=None):
super(ResultReason, self).__init__(
enums.ResultReason, value, enums.Tags.RESULT_REASON)
# 6.11
class ResultMessage(TextString):
def __init__(self, value=None):
super(ResultMessage, self).__init__(value, enums.Tags.RESULT_MESSAGE)
# 6.12
class BatchOrderOption(Boolean):
def __init__(self, value=None):
super(BatchOrderOption, self).\
__init__(value, enums.Tags.BATCH_ORDER_OPTION)
# 6.13
class BatchErrorContinuationOption(Enumeration):
def __init__(self, value=None):
super(BatchErrorContinuationOption, self).__init__(
enums.BatchErrorContinuationOption, value,
enums.Tags.BATCH_ERROR_CONTINUATION_OPTION)
# 6.14
class BatchCount(Integer):
def __init__(self, value=None):
super(BatchCount, self).__init__(value, enums.Tags.BATCH_COUNT)
# 6.16
class MessageExtension(Struct):
def __init__(self):
super(MessageExtension, self).__init__(enums.Tags.MESSAGE_EXTENSION)
# 9.1.3.2.2
class KeyCompressionType(Enumeration):
def __init__(self, value=None):
super(KeyCompressionType, self).__init__(
enums.KeyCompressionType, value, enums.Tags.KEY_COMPRESSION_TYPE)
|