ce60628d83ec332fdae1dbad9a049fe0cf6b5aac
[csit.git] / resources / libraries / python / honeycomb / HcAPIKwNSH.py
1 # Copyright (c) 2016 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 """This module implements keywords to manipulate NSH-SFC data structures using
15 Honeycomb REST API."""
16
17 from resources.libraries.python.HTTPRequest import HTTPCodes
18 from resources.libraries.python.honeycomb.HoneycombSetup import HoneycombError
19 from resources.libraries.python.honeycomb.HoneycombUtil \
20     import HoneycombUtil as HcUtil
21 from resources.libraries.python.honeycomb.HoneycombUtil \
22     import DataRepresentation
23
24
25 class NSHKeywords(object):
26     """Implementation of keywords which make it possible to:
27     - add and remove NSH entries,
28     - get operational data about NSH entries,
29     - add and remove NSH maps,
30     - get operational data about NSH maps.
31     """
32
33     def __init__(self):
34         pass
35
36     @staticmethod
37     def _set_nsh_properties(node, path, data=None):
38         """Set NSH properties and check the return code.
39
40         :param node: Honeycomb node.
41         :param path: Path which is added to the base path to identify the data.
42         :param data: The new data to be set. If None, the item will be removed.
43         :type node: dict
44         :type path: str
45         :type data: dict
46         :return: Content of response.
47         :rtype: bytearray
48         :raises HoneycombError: If the status code in response to PUT is not
49         OK or ACCEPTED.
50         """
51
52         if data:
53             status_code, resp = HcUtil. \
54                 put_honeycomb_data(node, "config_nsh", data, path,
55                                    data_representation=DataRepresentation.JSON)
56         else:
57             status_code, resp = HcUtil. \
58                 delete_honeycomb_data(node, "config_nsh", path)
59
60         if status_code not in (HTTPCodes.OK, HTTPCodes.ACCEPTED):
61             raise HoneycombError(
62                 "The configuration of NSH-SFC was not successful. "
63                 "Status code: {0}.".format(status_code))
64         return resp
65
66     @staticmethod
67     def add_nsh_entry(node, name, data):
68         """Add an NSH entry to the list of entries. The keyword does
69         not validate given data.
70
71         :param node: Honeycomb node.
72         :param name: Name for the NSH entry.
73         :param data: Settings for the new entry.
74         :type node: dict
75         :type name: str
76         :type data: dict
77         :return: Content of response.
78         :rtype: bytearray
79         """
80
81         path = "/nsh-entries/nsh-entry/{0}".format(name)
82
83         return NSHKeywords._set_nsh_properties(node, path, data)
84
85     @staticmethod
86     def add_nsh_map(node, name, data):
87         """Add an NSH map to the list of maps. The keyword does
88         not validate given data.
89
90         :param node: Honeycomb node.
91         :param name: Name for the NSH map.
92         :param data: Settings for the new map.
93         :type node: dict
94         :type name: str
95         :type data: dict
96         :return: Content of response.
97         :rtype: bytearray
98         """
99         path = "/nsh-maps/nsh-map/{0}".format(name)
100
101         return NSHKeywords._set_nsh_properties(node, path, data)
102
103     @staticmethod
104     def remove_nsh_entry(node, name):
105         """Remove an NSH entry from the list of entries.
106         :param node: Honeycomb node.
107         :param name: Name of the NSH entry.
108         :type node: dict
109         :type name: str
110         :return: Content of response.
111         :rtype: bytearray
112         """
113
114         path = "/nsh-entries/nsh-entry/{0}".format(name)
115         return NSHKeywords._set_nsh_properties(node, path)
116
117     @staticmethod
118     def remove_nsh_map(node, name):
119         """Remove an NSH map from the list of maps.
120         :param node: Honeycomb node.
121         :param name: Name of the NSH map.
122         :type node: dict
123         :type name: str
124         :return: Content of response.
125         :rtype: bytearray
126         """
127
128         path = "/nsh-maps/nsh-map/{0}".format(name)
129         return NSHKeywords._set_nsh_properties(node, path)
130
131     @staticmethod
132     def get_nsh_oper_data(node, entry_name=None, map_name=None):
133         """Get all NSH operational data present on the node. Optionally
134         filter out data for a specific entry or map.
135
136         :param node: Honeycomb node.
137         :param entry_name: Name of a specific NSH entry. Optional.
138         :param map_name: Name of a specific NSH map. Optional. Do not use
139         together with entry_name.
140         :type node: dict
141         :type entry_name: str
142         :type map_name: str
143         :return: List of classify tables.
144         :rtype: list
145         """
146         if entry_name:
147             path = "/nsh-entries/nsh-entry/{0}".format(entry_name)
148         elif map_name:
149             path = "/nsh-maps/nsh-map/{0}".format(map_name)
150         else:
151             path = ''
152
153         status_code, resp = HcUtil. \
154             get_honeycomb_data(node, "oper_nsh", path)
155
156         if status_code != HTTPCodes.OK:
157             raise HoneycombError(
158                 "Not possible to get operational information about the "
159                 "classify tables. Status code: {0}.".format(status_code))
160
161         return resp
162
163     @staticmethod
164     def clear_nsh_settings(node):
165         """Remove the entire NSH container with all of its entries and maps.
166
167         :param node: Honeycomb node.
168         :type node: dict
169         :return: Content of response.
170         :rtype: bytearray
171         """
172
173         return NSHKeywords._set_nsh_properties(node, '')