Fixed PublishEventBatch diagram in VES spec
[vnfrqts/requirements.git] / make_ids.py
1 # -*- coding: utf8 -*-
2 # org.onap.vnfrqts/requirements
3 # ============LICENSE_START====================================================
4 # Copyright Â© 2018 AT&T Intellectual Property. All rights reserved.
5 #
6 # Unless otherwise specified, all software contained herein is licensed
7 # under the Apache License, Version 2.0 (the "License");
8 # you may not use this software except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 #             http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 #
19 # Unless otherwise specified, all documentation contained herein is licensed
20 # under the Creative Commons License, Attribution 4.0 Intl. (the "License");
21 # you may not use this documentation except in compliance with the License.
22 # You may obtain a copy of the License at
23 #
24 #             https://creativecommons.org/licenses/by/4.0/
25 #
26 # Unless required by applicable law or agreed to in writing, documentation
27 # distributed under the License is distributed on an "AS IS" BASIS,
28 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29 # See the License for the specific language governing permissions and
30 # limitations under the License.
31 #
32 # ============LICENSE_END============================================
33 import argparse
34 import itertools
35 import json
36 import random
37 import requests
38 import urllib
39
40 REQUIREMENTS_FILE = "https://nexus.onap.org/content/sites/raw/org.onap.vnfrqts.requirements/master/needs.json"
41
42 def get_current_requirements():
43     """Read the current ONAP requirements from ONAP doc site"""
44     try:
45         r = requests.get(REQUIREMENTS_FILE)
46         if r.headers.get('content-type') == 'application/json':
47             data = r.content
48             return json.loads(data)
49         else:
50             warnings.warning("Unexpected content-type ({}) encountered downloading requirements.json, using last saved copy".format(r.headers.get('content-type')))
51     except requests.exceptions.RequestException as e:
52         warnings.warn("Error downloading latest JSON, using last saved copy.")
53         warnings.warn(UserWarning(e))
54
55 def load_all_ids():
56     """Loads the """
57     data = get_current_requirements()
58     result = set()
59     for version in data["versions"]:
60         result.update(data["versions"][version]["needs"].keys())
61     return result
62
63 def generate_ids():
64     """Generates a stream of unique requirement IDs"""
65     all_ids = load_all_ids()
66     while True:
67         new_id = "R-{:0>5d}".format(random.randint(0, 999999))
68         if new_id in all_ids:
69             continue  # skip this one and generate another one
70         all_ids.add(new_id)
71         yield new_id
72
73
74 if __name__ == "__main__":
75     parser = argparse.ArgumentParser(
76         description="""
77         Generate random, unique requirement IDs for use when adding new requirements
78         to the RST documentation.
79         """
80     )
81     parser.add_argument("num_ids", action="store", nargs="?", type=int, default=1,
82                         help="Number of IDs to generate")
83     args = parser.parse_args()
84     for req_id in itertools.islice(generate_ids(), args.num_ids):
85         print(req_id)
86