Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parser for Apple peering notifications #227

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions circuit_maintenance_parser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from .errors import NonexistentProviderError, ProviderError
from .provider import (
GenericProvider,
Apple,
AquaComms,
AWS,
BSO,
Expand All @@ -31,6 +32,7 @@

SUPPORTED_PROVIDERS = (
GenericProvider,
Apple,
AquaComms,
AWS,
BSO,
Expand Down
58 changes: 58 additions & 0 deletions circuit_maintenance_parser/parsers/apple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import email
import re

from typing import Dict, List

from circuit_maintenance_parser.output import Impact, Status
from circuit_maintenance_parser.parser import EmailSubjectParser, Text, CircuitImpact

class SubjectParserApple(EmailSubjectParser):
def parser_hook(self, raw: bytes):
message = email.message_from_string(self.bytes_to_string(raw))
return self.parse_subject(message['subject'])

def parse_subject(self, subject: str) -> List[Dict]:
return [{'summary': subject}]


class TextParserApple(Text):
def parse_text(self, text: str) -> List[Dict]:
data = {
'circuits': self._circuits(text),
'maintenance_id': self._maintenance_id(text),
'start': self._start_time(text),
'end': self._end_time(text),
'status': Status.CONFIRMED, # Have yet to see anything but confirmation.
'organizer': '[email protected]',
'provider': 'apple',
}
return [data]

def _circuits(self, text):
pattern = r'Peer AS: (\d*)'
m = re.search(pattern, text)
return [CircuitImpact(
circuit_id=f'AS{m.group(1)}',
impact=Impact.NO_IMPACT)]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you would like to mark the circuit state during the maintenance as OUTAGE? NO_IMPACT is supposed for a circuit that it's not being affected during the maintenance period


def _maintenance_id(self, text):
# Apple ticket numbers always starts with "CHG".
pattern = r'CHG(\d*)'
m = re.search(pattern, text)
return m.group(0)

def _get_time(self, pattern, text):
# Apple sends timestamps as RFC2822, misused
# email module to convert to datetime.
m = re.search(pattern, text)
rfc2822 = m.group(1)
time_tuple = email.utils.parsedate_tz(rfc2822)
return email.utils.mktime_tz(time_tuple)

def _start_time(self, text):
pattern = 'Start Time: ([a-zA-Z0-9 :]*)'
return self._get_time(pattern, text)

def _end_time(self, text):
pattern = 'End Time: ([a-zA-Z0-9 :]*)'
return self._get_time(pattern, text)
9 changes: 9 additions & 0 deletions circuit_maintenance_parser/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from circuit_maintenance_parser.processor import CombinedProcessor, SimpleProcessor, GenericProcessor
from circuit_maintenance_parser.constants import EMAIL_HEADER_SUBJECT

from circuit_maintenance_parser.parsers.apple import SubjectParserApple, TextParserApple
from circuit_maintenance_parser.parsers.aquacomms import HtmlParserAquaComms1, SubjectParserAquaComms1
from circuit_maintenance_parser.parsers.aws import SubjectParserAWS1, TextParserAWS1
from circuit_maintenance_parser.parsers.bso import HtmlParserBSO1
Expand Down Expand Up @@ -164,6 +165,14 @@ def get_provider_type(cls) -> str:
# PROVIDERS #
####################

class Apple(GenericProvider):
"""Apple provider custom class."""

_processors: List[GenericProcessor] = [
CombinedProcessor(data_parsers=[TextParserApple, SubjectParserApple]),
]
_default_organizer = "[email protected]"


class AquaComms(GenericProvider):
"""AquaComms provider custom class."""
Expand Down
49 changes: 49 additions & 0 deletions tests/unit/data/apple/apple1.eml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
Date-warning: Date header was inserted by rn-mailsvcp-relay-lapp01.rno.apple.com
Date: Mon, 01 May 2023 10:38:11 -0700 (PDT)
Message-id: <[email protected]>
Received: from prz3-cstools-hyp001.corp.apple.com
(prz3-cstools-hyp001.corp.apple.com [10.35.7.133])
by csmail.corp.apple.com (Postfix) with ESMTP id 1E23115EDE4; Mon,
1 May 2023 17:38:11 +0000 (UTC)
Content-type: multipart/mixed; boundary="===============1255676001481217459=="
MIME-version: 1.0
From: [email protected]
Subject: Apple (AS714) Net Maintenance Notice for AS12345 in Chicago
To: recipients not specified: ;

--===============1255676001481217459==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit

Apple (AS714) Network Maintenance Notification
Reference Internal Ticket: CHG000123456
Message sent to: [email protected]

Details:
Start Time: 2 May 2023 14:00 UTC
End Time: 16 May 2023 23:59 UTC

Peering Details:
Apple AS: 714
Peer AS: 12345


Apple IP: ['10.0.0.1', '2000:0000:0000:0000:0000:0000:0000:0001']
Peer IP: ['10.0.0.2', '2000:0000:0000:0000:0000:0000:0000:0002']

There is no change required on your end as we will gracefully drain the traffic prior to maintenance.

When the maintenance is completed, Apple will automatically re-route traffic back.
For reference, our current max prefix setting recommendation is: IPv4 = 10000 and IPv6 = 1000.
Questions about this event can be sent to [email protected].

Thank you for peering with Apple!

Regards,

Apple Peering NOC (AS714)
[email protected]
as714.peeringdb.com

--===============1255676001481217459==--
17 changes: 17 additions & 0 deletions tests/unit/data/apple/apple1_result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[
{
"circuits": [
{
"circuit_id": "AS12345",
"impact": "OUTAGE"
}
],
"end": 1684281540,
"maintenance_id": "CHG000123456",
"start": 1683036000,
"status": "CONFIRMED",
"summary": "Apple (AS714) Net Maintenance Notice for AS12345 in Chicago",
"organizer": "[email protected]",
"provider": "apple"
}
]
16 changes: 16 additions & 0 deletions tests/unit/data/apple/apple1_text_parser_result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[
{
"circuits": [
{
"circuit_id": "AS12345",
"impact": "NO-IMPACT"
}
],
"end": 1684281540,
"maintenance_id": "CHG000123456",
"start": 1683036000,
"status": "CONFIRMED",
"organizer": "[email protected]",
"provider": "apple"
}
]
5 changes: 5 additions & 0 deletions tests/unit/data/apple/apple2_subject_parser_result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
{
"summary": "Apple (AS714) Net Maintenance Notice for AS12345 in Chicago"
}
]
11 changes: 11 additions & 0 deletions tests/unit/test_e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from circuit_maintenance_parser.provider import (
Equinix,
GenericProvider,
Apple,
AquaComms,
Arelion,
AWS,
Expand Down Expand Up @@ -54,6 +55,16 @@
GENERIC_ICAL_RESULT_PATH,
],
),
# Apple
(
Apple,
[
("email", Path(dir_path, "data", "apple", "apple1.eml")),
],
[
Path(dir_path, "data", "apple", "apple1_result.json"),
],
),
# AquaComms
(
AquaComms,
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/test_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from circuit_maintenance_parser.errors import ParserError
from circuit_maintenance_parser.parser import ICal, EmailDateParser
from circuit_maintenance_parser.parsers.apple import SubjectParserApple, TextParserApple
from circuit_maintenance_parser.parsers.aquacomms import HtmlParserAquaComms1, SubjectParserAquaComms1
from circuit_maintenance_parser.parsers.aws import SubjectParserAWS1, TextParserAWS1
from circuit_maintenance_parser.parsers.bso import HtmlParserBSO1
Expand Down Expand Up @@ -68,6 +69,17 @@
Path(dir_path, "data", "ical", "ical6"),
Path(dir_path, "data", "ical", "ical6_result.json"),
),
# Apple
(
TextParserApple,
Path(dir_path, "data", "apple", "apple1.eml"),
Path(dir_path, "data", "apple", "apple1_text_parser_result.json"),
),
(
SubjectParserApple,
Path(dir_path, "data", "apple", "apple1.eml"),
Path(dir_path, "data", "apple", "apple2_subject_parser_result.json"),
),
# AquaComms
(
HtmlParserAquaComms1,
Expand Down