libredfish 0.2.0

A redfish library. Useful for querying server hardware from a BMC.
Documentation
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
# Copyright Notice:
# Copyright 2016-2019 DMTF. All rights reserved.
# License: BSD 3-Clause License. For full text see link: https://github.com/DMTF/python-redfish-library/blob/main/LICENSE.md

import socket
import struct
import sys

# based on https://github.com/ZeWaren/python-upnp-ssdp-example/blob/master/lib/ssdp.py

class RfSSDPServer():
    def addSearchTarget(self, target):
        self.searchtargets.append(target)

    def __init__(self, root, location, ip=None, port=1900, timeout=5):
        """__init__

        Initialize an SSDP server

        :param root: /redfish/v1 payload
        :param location: http location of server
        :param ip: address to bind to (IPV4 only?)
        :param port: port for server to exist on, default port 1900
        :param timeout: int for packet timeout
        """
        ip = ip if ip is not None else "0.0.0.0"
        self.searchtargets = ['ssdp:all', 'upnp:rootdevice', 'urn:dmtf-org:service:redfish-rest:1']
        self.ip, self.port = ip, port
        self.timeout = timeout

        # setup payload info
        self.location = location
        self.UUID = root.get('UUID', 'nouuid')
        self.cachecontrol = 1800
        myVersion = root.get('RedfishVersion', '1.0.0')
        self.major, self.minor, self.errata = tuple(myVersion.split('.'))
        self.addSearchTarget('urn:dmtf-org:service:redfish-rest:1:{}'.format(self.minor))

        # initiate multicast socket
        # rf-spec:
        #   must use TTL 2
        #   must use port 1900
        #   optional MSEARCH messages: Notify, Alive, Shutdown
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
        sock.settimeout(timeout)

        # join the multicast group on any interface, and allow for the loopback address
        sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, socket.inet_aton('239.255.255.250') + struct.pack(b"@I", socket.INADDR_ANY))
        sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, 1)

        sock.bind(('', port))
        """
        Redfish Service Search Target (ST): "urn:dmtf-org:service:redfish-rest:1"
        For ssdp, "ssdp:all".
        For UPnP compatibility, the managed device should respond to MSEARCH
        queries searching for Search Target (ST) of "upnp:rootdevice"
        """
        self.sock = sock
        logger.info('SSDP Server Created')

    def start(self):
        logger.info('SSDP Server Running...')
        countTimeout = pcount = 0
        while True:
            try:
                if countTimeout % 5 == 0:
                    logger.info('Ssdp Poll... {} pings'.format(pcount))
                    pcount = 0
                    countTimeout = 1
                data, addr = self.sock.recvfrom(1024)
                pcount += 1
                self.check(data, addr)
            except socket.timeout:
                countTimeout += 1
                continue
            except Exception as e:
                logger.info('error occurred ' + str(e))
                pass
        pass

    def check(self, data, addr):
        logger.info('SSDP Packet received from {}'.format(addr))
        decoded = data.decode().replace('\r', '').split('\n')
        msgtype, decoded = decoded[0], decoded[1:]
        decodeddict = {x.split(':',  1)[0].upper(): x.split(':', 1)[1].strip(' ') for x in decoded if x != ''}

        if 'M-SEARCH' in msgtype:
            st = decodeddict.get('ST')
            if st in self.searchtargets:
                response = ['HTTP/1.1 200 OK',
                            'CACHE-CONTROL: max-age={}'.format(self.cachecontrol),
                            'ST:urn:dmtf-org:service:redfish-rest:1:{}'.format(self.minor),
                            'USN:uuid:{}::urn:dmtf-org:service:redfish-rest:1:{}'.format(self.UUID, self.minor),
                            'AL:{}'.format(self.location),
                            'EXT:']

                response.extend(('', ''))
                response = '\r\n'.join(response)

                self.sock.sendto(response.encode(), addr)
                logger.info('SSDP Packet sent to {}'.format(addr))

# Copyright Notice:
# Copyright 2016-2019 DMTF. All rights reserved.
# License: BSD 3-Clause License. For full text see link: https://github.com/DMTF/Redfish-Mockup-Server/blob/main/LICENSE.md

# redfishMockupServer.py
# tested and developed Python 3.4

import re
import sys
import argparse
import time
import collections.abc
import json
import threading
import datetime
import signal

import grequests

import os
import ssl
import logging
from http.server import BaseHTTPRequestHandler, HTTPServer
from urllib.parse import urlparse, urlunparse, parse_qs

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(logging.INFO)
logger.addHandler(ch)

tool_version = "1.2.8"

dont_send = ["connection", "keep-alive", "content-length", "transfer-encoding"]


def dict_merge(dct, merge_dct):
    """
    https://gist.github.com/angstwad/bf22d1822c38a92ec0a9 modified
    Recursive dict merge. Inspired by :meth:``dict.update()``, instead of
    updating only top-level keys, dict_merge recurses down into dicts nested
    to an arbitrary depth, updating keys. The ``merge_dct`` is merged into
    ``dct``.
    :param dct: dict onto which the merge is executed
    :param merge_dct: dct merged into dct
    :return: None
    """
    for k in merge_dct:
        if k in dct and isinstance(dct[k], dict) and isinstance(merge_dct[k], collections.abc.Mapping):
            dict_merge(dct[k], merge_dct[k])
        else:
            dct[k] = merge_dct[k]


def clean_path(path, isShort):
    """clean_path

    :param path:
    :param isShort:
    """
    path = path.strip("/")
    path = path.split("?", 1)[0]
    path = path.split("#", 1)[0]
    if isShort:
        path = path.replace("redfish/v1", "").strip("/")
    return path


class RfMockupServer(BaseHTTPRequestHandler):
    """
    returns index.json file for Serverthe specified URL
    """

    patchedLinks = dict()

    def construct_path(self, path, filename):
        """construct_path

        :param path:
        :param filename:
        """
        apath = self.server.mockDir
        rpath = clean_path(path, self.server.shortForm)
        return "/".join([apath, rpath, filename]) if filename not in ["", None] else "/".join([apath, rpath])

    def get_cached_link(self, path):
        """get_cached_link

        :param path:
        """
        if path not in self.patchedLinks:
            if os.path.isfile(path):
                with open(path) as f:
                    jsonData = json.load(f)
                    f.close()
            else:
                jsonData = None
        else:
            jsonData = self.patchedLinks[path]
        return jsonData is not None and jsonData != "404", jsonData

    def try_to_sleep(self, method, path):
        """try_to_sleep

        :param method:
        :param path:
        """
        if self.server.timefromJson:
            responseTime = self.getResponseTime(method, path)
            try:
                time.sleep(float(responseTime))
            except ValueError:
                logger.info("Time is not a float value. Sleeping with default response time")
                time.sleep(float(self.server.responseTime))
        else:
            time.sleep(float(self.server.responseTime))

    def send_header_file(self, fpath):
        """send_header_file

        :param fpath:
        """
        with open(fpath) as headers_data:
            d = json.load(headers_data)
        if isinstance(d.get("GET"), dict):
            for k, v in d["GET"].items():
                if k.lower() not in dont_send:
                    self.send_header(k, v)

    def send_response_file(self, fpath):
        """send_response_file

        :param fpath:
        """
        with open(fpath) as response_data:
            d = json.load(response_data)
        if isinstance(d.get("status"), int):
            self.send_response(d.get("status"))
        else:
            self.send_response(200)
        if isinstance(d.get("headers"), dict):
            for k, v in d["headers"].items():
                if k.lower() not in dont_send:
                    self.send_header(k, v)
        body = d.get("body")
        self.send_header("Content-Length", len(body))
        self.end_headers()
        self.wfile.write(bytes(body, "utf8"))

    def add_new_member(self, payload, data_received):
        members = payload.get("Members")
        n = 1
        # Use an existing member ID if one exists
        if len(members):
            member_id = members[0].get("@odata.id").replace(self.path, "").strip("/")
            pattern = re.sub(r"\d+$", "{id}", member_id)
        else:
            pattern = "Member{id}"
        # If no {id} is in the pattern, append one to it
        pattern = pattern if re.search(r"\{id\}$", pattern) else pattern + "{id}"
        newpath_id = data_received.get("Id", pattern.format(id=n))
        # Default to standard pattern if the received ID already exists in members
        if data_received.get("Id") in [m.get("@odata.id").replace(self.path, "").strip("/") for m in members]:
            newpath_id = pattern.format(id=n)
        newpath = "/".join([self.path, newpath_id])
        while newpath in [m.get("@odata.id") for m in members]:
            n = n + 1
            newpath_id = pattern.format(id=n)
            newpath = "/".join([self.path, newpath_id])
        members.append({"@odata.id": newpath})
        data_received["@odata.id"] = newpath
        data_received["Id"] = newpath_id

        payload["Members"] = members
        payload["Members@odata.count"] = len(members)
        return newpath

    def handle_eventing(self, data_received):
        sub_path = self.construct_path("/redfish/v1/EventService/Subscriptions", "index.json")
        success, sub_payload = self.get_cached_link(sub_path)
        logger.info(sub_path)
        if not success:
            # Eventing not supported
            return 404
        else:
            # Check if all of the parameters are given
            if (
                ("EventType" not in data_received)
                or ("EventId" not in data_received)
                or ("EventTimestamp" not in data_received)
                or ("Severity" not in data_received)
                or ("Message" not in data_received)
                or ("MessageId" not in data_received)
                or ("MessageArgs" not in data_received)
                or ("OriginOfCondition" not in data_received)
            ):
                return 400
            else:
                # Need to reformat to make Origin Of Condition a proper link
                origin_of_cond = data_received["OriginOfCondition"]
                data_received["OriginOfCondition"] = {}
                data_received["OriginOfCondition"]["@odata.id"] = origin_of_cond
                event_payload = {}
                event_payload["@odata.type"] = "#Event.v1_2_1.Event"
                event_payload["Name"] = "Test Event"
                event_payload["Id"] = str(self.event_id)
                event_payload["Events"] = []
                event_payload["Events"].append(data_received)

                # Go through each subscriber
                events = []
                for member in sub_payload.get("Members", []):
                    entry = member["@odata.id"]
                    entrypath = self.construct_path(entry, "index.json")
                    success, subscription = self.get_cached_link(entrypath)
                    if not success:
                        logger.info("No such resource")
                    else:
                        # Sanity check the subscription for required properties
                        if ("Destination" in subscription) and ("EventTypes" in subscription):
                            logger.info(("Target", subscription["Destination"]))
                            logger.info((data_received["EventType"], subscription["EventTypes"]))

                            # If the EventType in the request is one of interest to the subscriber, build an event payload
                            if data_received["EventType"] in subscription["EventTypes"]:
                                http_headers = {}
                                http_headers["Content-Type"] = "application/json"

                                event_payload["Context"] = subscription.get("Context", "Default Context")

                                # Send the event
                                events.append(
                                    grequests.post(
                                        subscription["Destination"], timeout=20, data=json.dumps(event_payload), headers=http_headers
                                    )
                                )
                            else:
                                logger.info("event not in eventtypes")
                try:
                    threading.Thread(target=grequests.map, args=(events,)).start()
                except Exception as e:
                    logger.info("post error {}".format(str(e)))
                return 204
                self.event_id = self.event_id + 1

    def handle_telemetry(self, data_received):
        sub_path = self.construct_path("/redfish/v1/EventService/Subscriptions", "index.json")
        success, sub_payload = self.get_cached_link(sub_path)
        logger.info(sub_path)
        if not success:
            # Eventing not supported
            return 404
        else:
            # Check if all of the parameters are given
            if (
                (("MetricReportName" in data_received) and ("MetricReportValues" in data_received))
                or (("MetricReportName" in data_received) and ("GeneratedMetricReportValues" in data_received))
                or (("MetricName" in data_received) and ("MetricValues" in data_received))
            ):
                # If the EventType in the request is one of interest to the subscriber, build an event payload
                expected_keys = ["MetricId", "MetricValue", "Timestamp", "MetricProperty", "MetricDefinition"]
                my_name = data_received.get("MetricName", data_received.get("MetricReportName"))
                my_data = data_received.get(
                    "MetricValues", data_received.get("MetricReportValues", data_received.get("GeneratedMetricReportValues"))
                )
                event_payload = {}
                value_list = []
                # event_payload['@Redfish.Copyright'] = 'Copyright 2014-2016 Distributed Management Task Force, Inc. (DMTF). All rights reserved.'
                event_payload["@odata.context"] = "/redfish/v1/$metadata#MetricReport.MetricReport"
                event_payload["@odata.type"] = "#MetricReport.v1_0_0.MetricReport"
                event_payload["@odata.id"] = "/redfish/v1/TelemetryService/MetricReports/" + my_name
                event_payload["Id"] = my_name
                event_payload["Name"] = my_name
                event_payload["MetricReportDefinition"] = {"@odata.id": "/redfish/v1/TelemetryService/MetricReportDefinitions/" + my_name}
                now = datetime.datetime.now()
                event_payload["Timestamp"] = now.strftime("%Y-%m-%dT%H:%M:%S") + ("-%02d" % (now.microsecond / 10000))

                for tup in my_data:
                    if all(x in tup for x in expected_keys):
                        # uncomment for stricter payload check
                        # ex: if all(x in expected_keys + other_keys for x in tup):
                        value_list.append(tup)
                event_payload["MetricValues"] = value_list
                logger.info(event_payload)

                # construct path "mockdir/path/to/resource/<filename>"
                event_fpath = self.construct_path(event_payload["@odata.id"], "index.json")
                self.patchedLinks[event_fpath] = event_payload

                report_path = "/redfish/v1/TelemetryService/MetricReports"
                report_path = self.construct_path(report_path, "index.json")
                success, collection_payload = self.get_cached_link(report_path)

                if not success:
                    collection_payload = {"Members": []}
                    collection_payload["@odata.context"] = "/redfish/v1/$metadata#MetricReportCollection.MetricReportCollection"
                    collection_payload["@odata.type"] = "#MetricReportCollection.v1_0_0.MetricReportCollection"
                    collection_payload["@odata.id"] = "/redfish/v1/TelemetryService/MetricReports"
                    collection_payload["Name"] = "MetricReports"

                if event_payload["@odata.id"] not in [member.get("@odata.id") for member in collection_payload["Members"]]:
                    collection_payload["Members"].append({"@odata.id": event_payload["@odata.id"]})
                collection_payload["Members@odata.count"] = len(collection_payload["Members"])
                self.patchedLinks[report_path] = collection_payload

                # Go through each subscriber
                events = []
                for member in sub_payload.get("Members", []):
                    entry = member["@odata.id"]
                    entrypath = self.construct_path(entry, "index.json")
                    success, subscription = self.get_cached_link(entrypath)
                    if not success:
                        logger.info("No such resource")
                    else:
                        # Sanity check the subscription for required properties
                        if ("Destination" in subscription) and ("EventTypes" in subscription):
                            logger.info(("Target", subscription["Destination"]))
                            http_headers = {}
                            http_headers["Content-Type"] = "application/json"

                            # Send the event
                            events.append(
                                grequests.post(
                                    subscription["Destination"], timeout=20, data=json.dumps(event_payload), headers=http_headers
                                )
                            )
                        else:
                            logger.info("event not in eventtypes")
                try:
                    threading.Thread(target=grequests.map, args=(events,)).start()
                except Exception as e:
                    logger.info("post error {}".format(str(e)))
                self.event_id = self.event_id + 1
                return 204
            else:
                return 400

    server_version = "RedfishMockupHTTPD_v" + tool_version
    event_id = 1

    # Helper method to check if a dict is just an "@odata.id"
    # Returns value of odata_id_dict["@odata.id"]
    def __check_if_dict_is_odataid_only(self, odata_id_dict):
        if "@odata.id" in odata_id_dict and len(odata_id_dict) == 1:
            return odata_id_dict["@odata.id"]
        return None

    # Expand the data based on Redfish $expand spec
    # Note: Not handling '*' type for payload annotations
    def handle_expand_query(self, data, expand_type, levels):
        # Create a stack to handle diving deeper in the JSON data
        stack = [(data, levels)]
        while stack:
            current_data, expand_level = stack.pop()
            # If expand level is less than 1 then we've completed expansion
            if expand_level < 1:
                continue

            # For each key, value, expand dictionaries and list items
            # If an item is expanded, reduce the level of expansion to do
            # on item and add it to the stack
            for key, value in current_data.items():
                # Ignore values that aren't lists or dicts
                if not isinstance(value, list) and not isinstance(value, dict):
                    continue

                # Skipping "Links" based on expand type
                if expand_type == "." and key == "Links":
                    continue

                # If value is an object, check if just odata.id and expand
                if isinstance(value, dict):
                    # Track if value was expanded
                    expanded = False
                    odata_id = self.__check_if_dict_is_odataid_only(value)
                    if odata_id:
                        path = self.construct_path(odata_id, "index.json")
                        res, response_data = self.get_cached_link(path)
                        if res:
                            # Remove copyright information
                            response_data.pop("@Redfish.Copyright", None)
                            current_data[key] = response_data
                            # Mark value as expanded
                            expanded = True
                    # Add the current data to expand on
                    remove_level = 1 if expanded else 0
                    stack.append((current_data[key], expand_level - remove_level))
                else:
                    # Value is a list, expand each item if possible and add
                    # to stack

                    # Reserve space for replacement list
                    for index, array_item in enumerate(value):
                        if isinstance(array_item, dict):
                            # Track if array_item was expanded
                            expanded = False
                            odata_id = self.__check_if_dict_is_odataid_only(array_item)
                            if odata_id:
                                path = self.construct_path(odata_id, "index.json")
                                res, response_data = self.get_cached_link(path)
                                if res:
                                    # Remove copyright information
                                    response_data.pop("@Redfish.Copyright", None)
                                    value[index] = response_data
                                    expanded = True
                            # Add dictionary array items to stack to
                            # continue expand
                            # Remove a level if the item was expanded
                            remove_level = 1 if expanded else 0
                            stack.append((value[index], expand_level - remove_level))

                    # Copy current value back into the current data
                    current_data[key] = value

    # Headers only request
    def do_HEAD(self):
        """do_HEAD"""
        logger.info("Headers: ")
        logger.info(self.server.headers)

        # construct path "mockdir/path/to/resource/headers.json"
        fpath = self.construct_path(self.path, "index.json")
        fpath_xml = self.construct_path(self.path, "index.xml")
        fpath_headers = self.construct_path(self.path, "headers.json")
        fpath_direct = self.construct_path(self.path, "")

        # If bool headers is true and headers.json exists...
        # else, send normal headers for given resource
        if self.server.headers and (os.path.isfile(fpath_headers)):
            self.send_response(200)
            self.send_header_file(fpath_headers)
        elif (self.server.headers is False) or (os.path.isfile(fpath_headers) is False):
            if self.get_cached_link(fpath)[0]:
                self.send_response(200)
                self.send_header("Content-Type", "application/json")
                self.send_header("OData-Version", "4.0")
            elif os.path.isfile(fpath_xml) or os.path.isfile(fpath_direct):
                if os.path.isfile(fpath_xml):
                    file_extension = "xml"
                elif os.path.isfile(fpath_direct):
                    filename, file_extension = os.path.splitext(fpath_direct)
                    file_extension = file_extension.strip(".")
                self.send_response(200)
                self.send_header("Content-Type", "application/" + file_extension + ";charset=utf-8")
                self.send_header("OData-Version", "4.0")
            else:
                self.send_response(404)
        else:
            self.send_response(404)
        self.end_headers()

    def do_GET(self):
        """do_GET"""
        # for GETs always dump the request headers to the console
        # there is no request data, so no need to dump that
        logger.info(("GET", self.path))
        logger.info("   GET: Headers: {}".format(self.headers))

        # construct path "mockdir/path/to/resource/<filename>"
        fpath = self.construct_path(self.path, "index.json")
        fpath_xml = self.construct_path(self.path, "index.xml")
        fpath_headers = self.construct_path(self.path, "headers.json")
        fpath_custom = self.construct_path(self.path, "custom.json")
        fpath_direct = self.construct_path(self.path, "")

        success, payload = self.get_cached_link(fpath)

        scheme, netloc, path, params, query, fragment = urlparse(self.path)
        query_pieces = parse_qs(query, keep_blank_values=True)

        self.try_to_sleep("GET", self.path)

        # Handle custom responses written to custom.json
        if os.path.isfile(fpath_custom):
            self.send_response_file(fpath_custom)

        # handle resource paths that don't exist for shortForm
        # '/' and '/redfish'
        elif self.path == "/" and self.server.shortForm:
            self.send_response(404)
            self.end_headers()

        elif self.path in ["/redfish", "/redfish/"] and self.server.shortForm:
            self.send_response(200)
            if self.server.headers and (os.path.isfile(fpath_headers)):
                self.send_header_file(fpath_headers)
            else:
                self.send_header("Content-Type", "application/json")
                self.send_header("OData-Version", "4.0")

            encoded_data = json.dumps({"v1": "/redfish/v1"}, indent=4).encode()

            if not (self.server.headers and (os.path.isfile(fpath_headers))):
                self.send_header("Content-Length", len(encoded_data))
            self.end_headers()

            self.wfile.write(encoded_data)

        # if this location exists in memory or as file
        elif success:
            # if headers exist... send information (except for chunk info)
            # end headers here (always end headers after response)
            self.send_response(200)
            if self.server.headers and (os.path.isfile(fpath_headers)):
                self.send_header_file(fpath_headers)
            else:
                self.send_header("Content-Type", "application/json")
                self.send_header("OData-Version", "4.0")

            # Strip the @Redfish.Copyright property
            output_data = payload
            output_data.pop("@Redfish.Copyright", None)

            # Query Subscriptions should not return HttpHeaders.
            if "EventService/Subscriptions" in self.path:
                if output_data.get("HttpHeaders") is not None:
                    # This array is null or an empty array in responses.
                    # An empty array is the preferred return value on read operations.
                    output_data["HttpHeaders"] = []

            # Query evaluate
            if output_data.get("Members") is not None:
                my_members = output_data["Members"]
                top_count = int(query_pieces.get("$top", [str(len(my_members))])[0])
                top_skip = int(query_pieces.get("$skip", ["0"])[0])

                my_members = my_members[top_skip:]
                if top_count < len(my_members):
                    my_members = my_members[:top_count]
                    query_out = {"$skip": top_skip + top_count, "$top": top_count}
                    query_string = "&".join(["{}={}".format(k, v) for k, v in query_out.items()])
                    output_data["Members@odata.nextLink"] = urlunparse(("", "", path, "", query_string, ""))
                else:
                    pass

                output_data["Members"] = my_members
                pass

            # Handling expand
            expand_str = query_pieces.get("$expand", [""])[0]
            expand = re.match("([\\.~\\*])(\\(\\$levels=(\\d+)\\))?", expand_str)
            if expand:
                regex_groups = expand.groups()
                expand_type = regex_groups[0]
                levels = int(regex_groups[2]) if regex_groups[1] else 1
                self.handle_expand_query(output_data, expand_type, levels)

            encoded_data = json.dumps(output_data, sort_keys=True, indent=4, separators=(",", ": ")).encode()

            if not (self.server.headers and (os.path.isfile(fpath_headers))):
                self.send_header("Content-Length", len(encoded_data))
            self.end_headers()

            self.wfile.write(encoded_data)

        # if XML...
        elif os.path.isfile(fpath_xml):
            f = open(fpath_xml, "r")
            self.send_response(200)
            self.send_header("Content-Type", "application/xml;charset=utf-8")
            self.send_header("OData-Version", "4.0")
            self.end_headers()
            self.wfile.write(f.read().encode())
            f.close()

        elif os.path.isfile(fpath_direct):
            self.send_response(200)
            with open(fpath_direct, "rb") as f:
                content = f.read()

            try:
                decoded_content = content.decode()
            except ValueError:
                # If the file is binary, send it as octet-stream.
                self.send_header("Content-Type", "application/octet-stream")
                self.send_header("OData-Version", "4.0")
                self.end_headers()
                self.wfile.write(content)
            else:
                # The file is text.
                file_extension = os.path.splitext(fpath_direct)[1]
                if file_extension == "":
                    mime_type = "text/plain"
                else:
                    mime_type = "application/" + file_extension[1:]
                self.send_header("Content-Type", mime_type + ";charset=utf-8")
                self.send_header("OData-Version", "4.0")
                self.end_headers()
                self.wfile.write(decoded_content.encode("utf-8"))

        else:
            self.send_response(404)
            self.end_headers()

    def do_PATCH(self):
        logger.info("   PATCH: Headers: {}".format(self.headers))
        self.try_to_sleep("PATCH", self.path)

        if "content-length" in self.headers:
            lenn = int(self.headers["content-length"])
            try:
                data_received = json.loads(self.rfile.read(lenn).decode("utf-8"))
            except ValueError:
                print("Decoding JSON has failed, sending 400")
                data_received = None

        if data_received:
            logger.info("   PATCH: Data: {}".format(data_received))

            # construct path "mockdir/path/to/resource/<filename>"
            fpath = self.construct_path(self.path, "index.json")
            success, payload = self.get_cached_link(fpath)

            # check if resource exists, otherwise 404
            #   if it's a file, open it, if its in memory, grab it
            #   405 if Collection
            #   204 if patch success
            #   404 if payload DNE
            #   400 if no patch payload
            # end headers
            if success:
                # If this is a collection, throw a 405
                if payload.get("Members") is not None:
                    self.send_response(405)
                else:
                    # After getting resource, merge the data.
                    logger.info(self.headers.get("content-type"))
                    logger.info(data_received)
                    logger.info(payload)
                    dict_merge(payload, data_received)
                    logger.info(payload)
                    # put into self.patchedLinks
                    self.patchedLinks[fpath] = payload
                    self.send_response(204)
            else:
                self.send_response(404)
        else:
            self.send_response(400)

        self.end_headers()

    def do_PUT(self):
        logger.info("   PUT: Headers: {}".format(self.headers))
        self.try_to_sleep("PUT", self.path)

        if "content-length" in self.headers:
            lenn = int(self.headers["content-length"])
            try:
                data_received = json.loads(self.rfile.read(lenn).decode("utf-8"))
            except ValueError:
                print("Decoding JSON has failed, sending 400")
                data_received = None
            logger.info("   PUT: Data: {}".format(data_received))

        # we don't support this service
        #   405
        # end headers
        self.send_response(405)

        self.end_headers()

    def do_POST(self):
        logger.info("   POST: Headers: {}".format(self.headers))
        if "content-length" in self.headers:
            lenn = int(self.headers["content-length"])
            if lenn == 0:
                data_received = {}
            else:
                try:
                    data_received = json.loads(self.rfile.read(lenn).decode("utf-8"))
                except ValueError:
                    print("Decoding JSON has failed, sending 400")
                    data_received = None
        else:
            self.send_response(411)
            self.end_headers()
            return

        self.try_to_sleep("POST", self.path)

        if data_received is not None:
            logger.info("   POST: Data: {}".format(data_received))
            # construct path "mockdir/path/to/resource/<filename>"
            fpath = self.construct_path(self.path, "index.json")
            success, payload = self.get_cached_link(fpath)

            # don't bother if this item exists, otherwise, check if its an action or a file
            # if file
            #   405 if not Collection
            #   204 if success
            #   404 if no file present
            if success:
                if payload.get("Members") is None:
                    self.send_response(405)
                else:
                    logger.info(data_received)
                    logger.info(type(data_received))
                    # with members, form unique ID
                    #   must NOT exist in Members
                    #   add ID to members, change count
                    #   store as necessary in self.patchedLinks

                    newpath = self.add_new_member(payload, data_received)

                    newfpath = self.construct_path(newpath, "index.json")

                    logger.info(newfpath)

                    self.patchedLinks[newfpath] = data_received
                    self.patchedLinks[fpath] = payload
                    self.send_response(204)
                    self.send_header("Location", newpath)
                    self.send_header("Content-Length", "0")
                    if "SessionService/Sessions" in self.path:
                        self.send_header("X-Auth-Token", "1234567890ABCDEF")
                    self.end_headers()

            # Actions framework
            else:
                # SubmitTestEvent
                if "EventService/Actions/EventService.SubmitTestEvent" in self.path:
                    r_code = self.handle_eventing(data_received)
                    self.send_response(r_code)
                # SubmitTestMetricReport
                elif "TelemetryService/Actions/TelemetryService.SubmitTestMetricReport" in self.path:
                    r_code = self.handle_telemetry(data_received)
                    self.send_response(r_code)
                # All other actions (no data checking or response data)
                elif "/Actions/" in self.path:
                    fpath = self.construct_path(self.path.split("/Actions/", 1)[0], "index.json")
                    success, payload = self.get_cached_link(fpath)
                    if success:
                        action_found = False
                        try:
                            for action in payload["Actions"]:
                                if action == "Oem":
                                    for oem_action in payload["Actions"][action]:
                                        if payload["Actions"][action][oem_action]["target"] == self.path:
                                            action_found = True
                                else:
                                    if payload["Actions"][action]["target"] == self.path:
                                        action_found = True
                        except Exception:
                            pass
                        if action_found:
                            self.send_response(204)
                        else:
                            self.send_response(404)
                    else:
                        self.send_response(404)
                # Not found
                else:
                    self.send_response(404)
        else:
            self.send_response(400)
        self.end_headers()

    def do_DELETE(self):
        """
        Delete a resource
        """
        logger.info("DELETE: Headers: {}".format(self.headers))
        self.try_to_sleep("DELETE", self.path)

        fpath = self.construct_path(self.path, "index.json")
        ppath = "/".join(self.path.split("/")[:-1])
        parent_path = self.construct_path(ppath, "index.json")
        success, payload = self.get_cached_link(fpath)

        # 404 if file doesn't exist
        # 204 if success, override payload with 404
        #   modify payload to exclude expected URI, subtract count
        # 405 if parent is not Collection
        # end headers
        if success:
            success, parentData = self.get_cached_link(parent_path)
            if success and parentData.get("Members") is not None:
                self.patchedLinks[fpath] = "404"
                parentData["Members"] = [x for x in parentData["Members"] if not x["@odata.id"] == self.path]
                parentData["Members@odata.count"] = len(parentData["Members"])
                self.patchedLinks[parent_path] = parentData
                self.send_response(204)
            else:
                self.send_response(405)
        else:
            self.send_response(404)

        self.end_headers()

    # Response time calculation Algorithm
    def getResponseTime(self, method, path):
        fpath = self.construct_path(path, "time.json")
        success, item = self.get_cached_link(path)
        if not any(x in method for x in ("GET", "HEAD", "POST", "PATCH", "DELETE")):
            logger.info("Not a valid method")
            return 0

        if os.path.isfile(fpath):
            with open(fpath) as time_data:
                d = json.load(time_data)
                time_str = method + "_Time"
                if time_str in d:
                    try:
                        float(d[time_str])
                    except Exception:
                        logger.info("Time in the json file, not a float/int value. Reading the default time.")
                        return self.server.responseTime
                    return float(d[time_str])
        else:
            logger.info(("response time:", self.server.responseTime))
            return self.server.responseTime


def main():

    logger.info("Redfish Mockup Server, version {}".format(tool_version))

    parser = argparse.ArgumentParser(description="Serve a static Redfish mockup.")
    parser.add_argument("-H", "--host", "--Host", default="127.0.0.1", help="hostname or IP address (default 127.0.0.1)")
    parser.add_argument("-p", "--port", "--Port", default=8000, type=int, help="host port (default 8000)")
    parser.add_argument("-D", "--dir", "--Dir", help="path to mockup dir (may be relative to CWD)")
    parser.add_argument("-E", "--test-etag", "--TestEtag", action="store_true", help="(unimplemented) etag testing")
    parser.add_argument("-X", "--headers", action="store_true", help="load headers from headers.json files in mockup")
    parser.add_argument("-t", "--time", default=0, help="delay in seconds added to responses (float or int)")
    parser.add_argument("-T", action="store_true", help="delay response based on times in time.json files in mockup")
    parser.add_argument("-s", "--ssl", action="store_true", help="place server in SSL (HTTPS) mode; requires a cert and key")
    parser.add_argument("--cert", help="the certificate for SSL")
    parser.add_argument("--key", help="the key for SSL")
    parser.add_argument("-S", "--short-form", "--shortForm", action="store_true", help="apply short form to mockup (omit filepath /redfish/v1)")
    parser.add_argument("-P", "--ssdp", action="store_true", help="make mockup SSDP discoverable")

    args = parser.parse_args()
    hostname = args.host
    port = args.port
    mockDirPath = args.dir
    testEtagFlag = args.test_etag
    headers = args.headers
    responseTime = args.time
    timefromJson = args.T
    sslMode = args.ssl
    sslCert = args.cert
    sslKey = args.key
    shortForm = args.short_form
    ssdpStart = args.ssdp

    # check if mockup path was specified.  If not, use the built-in mockup
    if mockDirPath is None:
        mockDirPath = "public-rackmount1"
        shortForm = True

    logger.info("Hostname: {}".format(hostname))
    logger.info("Port: {}".format(port))
    logger.info("Mockup directory path specified: {}".format(mockDirPath))
    logger.info("Response time: {} seconds".format(responseTime))

    # create the full path to the top directory holding the Mockup
    mockDir = os.path.realpath(mockDirPath)  # creates real full path including path for CWD to the -D<mockDir> dir path
    logger.info("Serving Mockup in absolute path: {}".format(mockDir))

    # check that we have a valid tall mockup--with /redfish in mockDir before proceeding
    if not shortForm:
        slashRedfishDir = os.path.join(mockDir, "redfish")
        if os.path.isdir(slashRedfishDir) is not True:
            logger.info("ERROR: Invalid Mockup Directory--no /redfish directory at top. Aborting")
            sys.stderr.flush()
            sys.exit(1)

    if shortForm:
        if os.path.isdir(mockDir) is not True or os.path.isfile(os.path.join(mockDir, "index.json")) is not True:
            logger.info("ERROR: Invalid Mockup Directory--dir or index.json does not exist")
            sys.stderr.flush()
            sys.exit(1)

    myServer = HTTPServer((hostname, port), RfMockupServer)

    def sigterm_handler(signal_number, frame):
        logger.info("SIGTERM: Shutting down http server")
        myServer.server_close()
        sys.exit(0)

    signal.signal(signal.SIGTERM, sigterm_handler)

    if sslMode:
        logger.info("Using SSL with certfile: {}".format(sslCert))
        context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
        context.load_cert_chain(certfile=sslCert, keyfile=sslKey)
        myServer.socket = context.wrap_socket(myServer.socket, server_side=True)

    # save the test flag, and real path to the mockup dir for the handler to use
    myServer.mockDir = mockDir
    myServer.testEtagFlag = testEtagFlag
    myServer.headers = headers
    myServer.timefromJson = timefromJson
    myServer.shortForm = shortForm
    try:
        myServer.responseTime = float(responseTime)
    except ValueError:
        logger.info("Enter an integer or float value")
        sys.exit(2)
    # myServer.me="HELLO"

    mySSDP = None
    if ssdpStart:
        from gevent import monkey

        monkey.patch_all()
        # construct path "mockdir/path/to/resource/<filename>"
        path, filename, jsonData = "/redfish/v1", "index.json", None
        apath = myServer.mockDir
        rpath = clean_path(path, myServer.shortForm)
        fpath = os.path.join(apath, rpath, filename) if filename not in ["", None] else os.path.join(apath, rpath)
        if os.path.isfile(fpath):
            with open(fpath) as f:
                jsonData = json.load(f)
                f.close()
        else:
            jsonData = None
        protocol = "{}://".format("https" if sslMode else "http")
        mySSDP = RfSSDPServer(jsonData, "{}{}:{}{}".format(protocol, hostname, port, "/redfish/v1"), hostname)

    logger.info("Serving Redfish mockup on port: {}".format(port))
    try:
        if mySSDP is not None:
            t2 = threading.Thread(target=mySSDP.start)
            t2.daemon = True
            t2.start()
        logger.info("running Server...")
        myServer.serve_forever()

    except KeyboardInterrupt:
        pass

    myServer.server_close()
    logger.info("Shutting down http server")


# the below is only executed if the program is run as a script
if __name__ == "__main__":
    main()

"""
TODO:
1. add -L option to load json and dump output from python dictionary
2. add authentication support -- note that in redfish some api don't require auth
3. add https support


"""