Skip to content

Commit b1e9bc4

Browse files
Merge pull request #83 from networktocode/extend-readme
Extend Readme
2 parents 9d14c6f + c350a44 commit b1e9bc4

File tree

2 files changed

+152
-80
lines changed

2 files changed

+152
-80
lines changed

README.md

Lines changed: 152 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,31 @@ during a NANOG meeting that aimed to promote the usage of the iCalendar format.
1919
proposed iCalendar format, the parser is straight-forward and there is no need to define custom logic, but this library
2020
enables supporting other providers that are not using this proposed practice, getting the same outcome.
2121

22-
You can leverage on this library in your automation framework to process circuit maintenance notifications, and use the standarised output to handle your received circuit maintenance notifications in a simple way.
22+
You can leverage this library in your automation framework to process circuit maintenance notifications, and use the standardized [`Maintenance`](https://github.com/networktocode/circuit-maintenance-parser/blob/develop/circuit_maintenance_parser/output.py) to handle your received circuit maintenance notifications in a simple way. Every `maintenance` object contains, at least, the following attributes:
23+
24+
- **provider**: identifies the provider of the service that is the subject of the maintenance notification.
25+
- **account**: identifies an account associated with the service that is the subject of the maintenance notification.
26+
- **maintenance_id**: contains text that uniquely identifies the maintenance that is the subject of the notification.
27+
- **circuits**: list of circuits affected by the maintenance notification and their specific impact.
28+
- **status**: defines the overall status or confirmation for the maintenance.
29+
- **start**: timestamp that defines the start date of the maintenance in GMT.
30+
- **end**: timestamp that defines the end date of the maintenance in GMT.
31+
- **stamp**: timestamp that defines the update date of the maintenance in GMT.
32+
- **organizer**: defines the contact information included in the original notification.
33+
34+
> Please, refer to the [BCOP](https://github.com/jda/maintnote-std/blob/master/standard.md) to more details about these attributes.
2335
2436
## Workflow
2537

2638
1. We instantiate a `Provider`, directly or via the `init_provider` method, that depending on the selected type will return the corresponding instance.
27-
2. Each `Provider` have already defined multiple `Processors` that will be used to get the `Maintenances` when the `Provider.get_maintenances(data)` method is called.
28-
3. Each `Processor` class can have a pre defined logic to combine the data extracted from the notifications and create the final `Maintenance` object, and receives a `List` of multiple `Parsers` that will be to `parse` each type of data.
29-
4. Each `Parser` class supports one or more data types and implements the `Parser.parse()` method used to retrieve a `Dict` with the relevant key/values.
30-
5. When calling the `Provider.get_maintenances(data)`, the `data` argument is an instance of `NotificationData` (which is just a collection of multiple `DataParts`, each one with a `type` and a `content`) that will be used by the corresponding `Parser` when the `Processor` will try to match them.
39+
2. Get an instance of the `NotificationData` class. This instance groups together `DataParts` which each contain some content and a specific type (that will match a specific `Parser`). For example, a `NotificationData` might describe a received email message, with `DataParts` corresponding to the subject line and body of the email. There are factory methods to initialize a `NotificationData` describing a single chunk of binary data, as well as others to initialize one directly from a raw email message or `email.message.EmailMessage` instance.
40+
3. Each `Provider` uses one or more `Processors` that will be used to build `Maintenances` when the `Provider.get_maintenances(data)` method is called.
41+
4. Each `Processor` class uses one or more `Parsers` to process each type of data that it handles. It can have custom logic to combine the parsed data from multiple `Parsers` to create the final `Maintenance` object.
42+
5. Each `Parser` class supports one or a set of related data types, and implements the `Parser.parse()` method used to retrieve a `Dict` with the relevant keys/values.
43+
44+
<p align="center">
45+
<img src="https://raw.githubusercontent.com/nautobot/nautobot-plugin-circuit-maintenance/develop/docs/images/new_workflow.png" width="800" class="center">
46+
</p>
3147

3248
By default, there is a `GenericProvider` that support a `SimpleProcessor` using the standard `ICal` `Parser`, being the easiest path to start using the library in case the provider uses the reference iCalendar standard.
3349

@@ -66,15 +82,39 @@ By default, there is a `GenericProvider` that support a `SimpleProcessor` using
6682
The library is available as a Python package in pypi and can be installed with pip:
6783
`pip install circuit-maintenance-parser`
6884

69-
## Usage
85+
## How to use it?
7086

71-
> Please, refer to the [BCOP](https://github.com/jda/maintnote-std/blob/master/standard.md) to understand the meaning
72-
> of the output attributes.
87+
The library requires two things:
7388

74-
## Python Library
89+
- The `notificationdata`: this is the data that the library will check to extract the maintenance notifications. It can be simple (only one data type and content, such as an iCalendar notification) or more complex (with multiple data parts of different types, such as from an email).
90+
- The `provider` identifier: used to select the proper `Provider` which contains the `processor` logic to take the proper `Parsers` and use the data that they extract. By default, the `GenericProvider` (used when no other provider type is defined) will support parsing of `iCalendar` notifications using the recommended format.
91+
92+
### Python Library
93+
94+
First step is to define the `Provider` that we will use to parse the notifications. As commented, there is a `GenericProvider` that implements the gold standard format and can be reused for any notification matching the expectations.
7595

7696
```python
77-
from circuit_maintenance_parser import init_provider, NotificationData
97+
from circuit_maintenance_parser import init_provider
98+
99+
generic_provider = init_provider()
100+
101+
type(generic_provider)
102+
<class 'circuit_maintenance_parser.provider.GenericProvider'>
103+
```
104+
105+
However, usually some `Providers` don't fully implement the standard and maybe some information is missing, for example the `organizer` email or maybe a custom logic to combine information is required, so we allow custom `Providers`:
106+
107+
```python
108+
ntt_provider = init_provider("ntt")
109+
110+
type(ntt_provider)
111+
<class 'circuit_maintenance_parser.provider.NTT'>
112+
```
113+
114+
Once we have the `Provider` ready, we need to initialize the data to process, we call it `NotificationData` and can be initialized from a simple content and type or from more complex structures, such as an email.
115+
116+
```python
117+
from circuit_maintenance_parser import NotificationData
78118

79119
raw_data = b"""BEGIN:VCALENDAR
80120
VERSION:2.0
@@ -98,87 +138,57 @@ END:VEVENT
98138
END:VCALENDAR
99139
"""
100140

101-
ntt_provider = init_provider("ntt")
102-
103141
data_to_process = NotificationData.init_from_raw("ical", raw_data)
104142

105-
maintenances = ntt_provider.get_maintenances(data_to_process)
143+
type(data_to_process)
144+
<class 'circuit_maintenance_parser.data.NotificationData'>
145+
```
146+
147+
Finally, with we retrieve the maintenances (it is a `List` because a notification can contain multiple maintenances) from the data calling the `get_maintenances` method from the `Provider` instance:
148+
149+
```python
150+
maintenances = generic_provider.get_maintenances(data_to_process)
106151

107152
print(maintenances[0].to_json())
108153
{
109-
"account": "137.035999173",
110-
"circuits": [
111-
{
112-
"circuit_id": "acme-widgets-as-a-service",
113-
"impact": "NO-IMPACT"
114-
},
115-
{
116-
"circuit_id": "acme-widgets-as-a-service-2",
117-
"impact": "OUTAGE"
118-
}
119-
],
120-
"end": 1444471200,
121-
"maintenance_id": "WorkOrder-31415",
122-
"organizer": "mailto:[email protected]",
123-
"provider": "example.com",
124-
"sequence": 1,
125-
"stamp": 1444435800,
126-
"start": 1444464000,
127-
"status": "TENTATIVE",
128-
"summary": "Maint Note Example",
129-
"uid": "42"
154+
"account": "137.035999173",
155+
"circuits": [
156+
{
157+
"circuit_id": "acme-widgets-as-a-service",
158+
"impact": "NO-IMPACT"
159+
},
160+
{
161+
"circuit_id": "acme-widgets-as-a-service-2",
162+
"impact": "OUTAGE"
163+
}
164+
],
165+
"end": 1444471200,
166+
"maintenance_id": "WorkOrder-31415",
167+
"organizer": "mailto:[email protected]",
168+
"provider": "example.com",
169+
"sequence": 1,
170+
"stamp": 1444435800,
171+
"start": 1444464000,
172+
"status": "TENTATIVE",
173+
"summary": "Maint Note Example",
174+
"uid": "42"
130175
}
131176
```
132177

133-
## CLI
178+
Notice that, either with the `GenericProvider` or `NTT` provider, we get the same result from the same data, because they are using exactly the same `Processor` and `Parser`. The only difference is that `NTT` notifications come without `organizer` and `provider` in the notification, and this info is fulfilled with some default values for the `Provider`, but in this case the original notification contains all the necessary information, so the defaults are not used.
134179

135-
```bash
136-
$ circuit-maintenance-parser --data-file tests/unit/data/ical/ical1 --data-type ical
137-
Circuit Maintenance Notification #0
138-
{
139-
"account": "137.035999173",
140-
"circuits": [
141-
{
142-
"circuit_id": "acme-widgets-as-a-service",
143-
"impact": "NO-IMPACT"
144-
}
145-
],
146-
"end": 1444471200,
147-
"maintenance_id": "WorkOrder-31415",
148-
"organizer": "mailto:[email protected]",
149-
"provider": "example.com",
150-
"sequence": 1,
151-
"stamp": 1444435800,
152-
"start": 1444464000,
153-
"status": "TENTATIVE",
154-
"summary": "Maint Note Example",
155-
"uid": "42"
156-
}
180+
```python
181+
ntt_maintenances = ntt_provider.get_maintenances(data_to_process)
182+
assert maintenances_ntt == maintenances
157183
```
158184

159-
```bash
160-
$ circuit-maintenance-parser --data-file tests/unit/data/zayo/zayo1.html --data-type html --provider-type zayo
161-
Circuit Maintenance Notification #0
162-
{
163-
"account": "clientX",
164-
"circuits": [
165-
{
166-
"circuit_id": "/OGYX/000000/ /ZYO /",
167-
"impact": "OUTAGE"
168-
}
169-
],
170-
"end": 1601035200,
171-
"maintenance_id": "TTN-00000000",
172-
"organizer": "[email protected]",
173-
"provider": "zayo",
174-
"sequence": 1,
175-
"stamp": 1599436800,
176-
"start": 1601017200,
177-
"status": "CONFIRMED",
178-
"summary": "Zayo will implement planned maintenance to troubleshoot and restore degraded span",
179-
"uid": "0"
180-
}
181-
```
185+
### CLI
186+
187+
There is also a `cli` entrypoint `circuit-maintenance-parser` which offers easy access to the library using few arguments:
188+
189+
- `data-file`: file storing the notification.
190+
- `data-type`: `ical`, `html` or `email`, depending on the data type.
191+
- `provider-type`: to choose the right `Provider`. If empty, the `GenericProvider` is used.
182192

183193
```bash
184194
circuit-maintenance-parser --data-file "/tmp/___ZAYO TTN-00000000 Planned MAINTENANCE NOTIFICATION___.eml" --data-type email --provider-type zayo
@@ -204,6 +214,68 @@ Circuit Maintenance Notification #0
204214
}
205215
```
206216

217+
## How to Extend the Library?
218+
219+
Even though the library aims to include support for as many providers as possible, it's likely that not all the thousands of NSP are supported and you may need to add support for some new one. Adding a new `Provider` is quite straightforward, and in the following example we are adding support for an imaginary provider, ABCDE, that uses HTML notifications.
220+
221+
First step is creating a new file: `circuit_maintenance_parser/parsers/abcde.py`. This file will contain all the custom parsers needed for the provider and it will import the base classes for each parser type from `circuit_maintenance_parser.parser`. In the example, we only need to import `Html` and in the child class implement the methods required by the class, in this case `parse_html()` which will return a `dict` with all the data that this `Parser` can extract. In this case we have to helper methods, `_parse_bs` and `_parse_tables` that implement the logic to navigate the notification data.
222+
223+
```python
224+
from typing import Dict
225+
import bs4 # type: ignore
226+
from bs4.element import ResultSet # type: ignore
227+
from circuit_maintenance_parser.parser import Html
228+
229+
class HtmlParserABCDE1(Html):
230+
def parse_html(self, soup: ResultSet) -> Dict:
231+
data = {}
232+
self._parse_bs(soup.find_all("b"), data)
233+
self._parse_tables(soup.find_all("table"), data)
234+
return [data]
235+
236+
def _parse_bs(self, btags: ResultSet, data: Dict):
237+
...
238+
239+
def _parse_tables(self, tables: ResultSet, data: Dict):
240+
...
241+
```
242+
243+
Next step is to create the new `Provider` by defining a new class in `circuit_maintenance_parser/provider.py`. This class that inherits from `GenericProvider` only needs to define two attributes:
244+
245+
- `_processors`: is a `list` of `Processor` instances that uses several data `Parsers`. In this example, we don't need to create a new custom `Processor` because the combined logic serves well (the most likely case), and we only need to use the new defined `HtmlParserABCDE1` and also the generic `EmailDateParser` that extract the email date. Also notice that you could have multiple `Processors` with different `Parsers` in this list, supporting several formats.
246+
- `_default_organizer`: this is a default helper to fill the `organizer` attribute in the `Maintenance` if the information is not part of the original notification.
247+
248+
```python
249+
class ABCDE(GenericProvider):
250+
_processors: List[GenericProcessor] = [
251+
CombinedProcessor(data_parsers=[EmailDateParser, HtmlParserABCDE1]),
252+
]
253+
_default_organizer = "[email protected]"
254+
```
255+
256+
And expose the new `Provider` in `circuit_maintenance_parser/__init__.py`:
257+
258+
```python
259+
from .provider import (
260+
GenericProvider,
261+
ABCDE,
262+
...
263+
)
264+
265+
SUPPORTED_PROVIDERS = (
266+
GenericProvider,
267+
ABCDE,
268+
...
269+
)
270+
```
271+
272+
Last, but not least, you should update the tests!
273+
274+
- Test the new `Parser` in `tests/unit/test_parsers.py`
275+
- Test the new `Provider` logic in `tests/unit/test_e2e.py`
276+
277+
... adding the necessary data samples in `tests/unit/data/abcde/`.
278+
207279
# Contributing
208280

209281
Pull requests are welcomed and automatically built and tested against multiple versions of Python through Travis CI.

docs/images/new_workflow.png

49.2 KB
Loading

0 commit comments

Comments
 (0)