Skip to content

Commit b0760bd

Browse files
committed
Initial commit for child_fabric_module
1 parent f593092 commit b0760bd

File tree

6 files changed

+1344
-0
lines changed

6 files changed

+1344
-0
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# Copyright (c) 2025 Cisco and/or its affiliates.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
# pylint: disable=line-too-long
15+
from __future__ import absolute_import, division, print_function
16+
17+
__metaclass__ = type
18+
__author__ = "prabahal"
19+
20+
import logging
21+
22+
from ..fabrics import Fabrics
23+
24+
25+
class Msd(Fabrics):
26+
"""
27+
## api.v1.lan-fabric.rest.control.fabrics.Msd()
28+
29+
### Description
30+
Common methods and properties for Msd() subclasses.
31+
32+
### Path
33+
- ``/api/v1/lan-fabric/rest/control/fabrics/msd``
34+
"""
35+
36+
def __init__(self):
37+
super().__init__()
38+
self.class_name = self.__class__.__name__
39+
self.log = logging.getLogger(f"dcnm.{self.class_name}")
40+
self.msd = f"{self.fabrics}/msd"
41+
msg = f"ENTERED api.v1.lan_fabric.rest.control.fabrics.{self.class_name}"
42+
self.log.debug(msg)
43+
44+
45+
class EpFabricAssociations(Msd):
46+
"""
47+
## api.v1.lan-fabric.rest.control.fabrics.msd.EpFabricAssociations()
48+
49+
### Description
50+
Common methods and properties for EpFabricAssociations() subclasses.
51+
52+
### Path
53+
- ``/api/v1/lan-fabric/rest/control/fabrics/msd/fabric-associations``
54+
"""
55+
56+
def __init__(self):
57+
super().__init__()
58+
self.class_name = self.__class__.__name__
59+
self.log = logging.getLogger(f"dcnm.{self.class_name}")
60+
msg = f"ENTERED api.v1.lan_fabric.rest.control.fabrics.msd.{self.class_name}"
61+
self.log.debug(msg)
62+
63+
def _build_properties(self):
64+
super()._build_properties()
65+
self.properties["verb"] = "GET"
66+
67+
@property
68+
def path(self):
69+
"""
70+
Return endpoint path.
71+
"""
72+
return f"{self.msd}/fabric-associations"
73+
74+
75+
class EpChildFabricAdd(Msd):
76+
"""
77+
## api.v1.lan-fabric.rest.control.fabrics.msd.EpChildFabricAdd()
78+
79+
### Description
80+
Common methods and properties for EpChildFabricAdd() subclasses.
81+
82+
### Path
83+
- ``/api/v1/lan-fabric/rest/control/fabrics/msdAdd``
84+
"""
85+
86+
def __init__(self):
87+
super().__init__()
88+
self.class_name = self.__class__.__name__
89+
self.log = logging.getLogger(f"dcnm.{self.class_name}")
90+
self.msdAdd = f"{self.msd}Add"
91+
msg = f"ENTERED api.v1.lan_fabric.rest.control.fabrics.msd.{self.class_name}"
92+
self.log.debug(msg)
93+
94+
def _build_properties(self):
95+
super()._build_properties()
96+
self.properties["verb"] = "POST"
97+
98+
@property
99+
def path(self):
100+
"""
101+
Return endpoint path.
102+
"""
103+
return f"{self.msd}Add"
104+
105+
106+
class EpChildFabricExit(Msd):
107+
"""
108+
## api.v1.lan-fabric.rest.control.fabrics.msd.EpChildFabricExit()
109+
110+
### Description
111+
Common methods and properties for EpChildFabricExit() subclasses.
112+
113+
### Path
114+
- ``/api/v1/lan-fabric/rest/control/fabrics/msdExit``
115+
"""
116+
117+
def __init__(self):
118+
super().__init__()
119+
self.class_name = self.__class__.__name__
120+
self.log = logging.getLogger(f"dcnm.{self.class_name}")
121+
self.msdExit = f"{self.msd}Exit"
122+
msg = f"ENTERED api.v1.lan_fabric.rest.control.fabrics.msd.{self.class_name}"
123+
self.log.debug(msg)
124+
125+
def _build_properties(self):
126+
super()._build_properties()
127+
self.properties["verb"] = "POST"
128+
129+
@property
130+
def path(self):
131+
"""
132+
Return endpoint path.
133+
"""
134+
return f"{self.msd}Exit"
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#
2+
# Copyright (c) 2025 Cisco and/or its affiliates.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
from __future__ import absolute_import, division, print_function
17+
18+
__metaclass__ = type
19+
__author__ = "Prabahal"
20+
21+
import copy
22+
import inspect
23+
import json
24+
import logging
25+
26+
from ..common.api.v1.lan_fabric.rest.control.fabrics.msd.msd import \
27+
EpFabricAssociations
28+
29+
30+
# Import Results() only for the case where the user has not set Results()
31+
# prior to calling commit(). In this case, we instantiate Results()
32+
# in _validate_commit_parameters() so that we can register the failure
33+
# in commit().
34+
from ..common.results import Results
35+
36+
37+
class FabricAssociations():
38+
39+
def __init__(self):
40+
self.class_name = self.__class__.__name__
41+
self.log = logging.getLogger(f"dcnm.{self.class_name}")
42+
self.data = None
43+
self.results = Results()
44+
self.refreshed = False
45+
self.fabric_association_data = []
46+
47+
@property
48+
def all_data(self) -> dict:
49+
"""
50+
- Return raw fabric association data from the controller.
51+
- Raise ``ValueError`` if ``refresh()`` has not been called.
52+
"""
53+
method_name = inspect.stack()[0][3]
54+
try:
55+
self.verify_refresh_has_been_called(method_name)
56+
except ValueError as error:
57+
raise ValueError(error) from error
58+
return self.fabric_association_data
59+
60+
def verify_refresh_has_been_called(self, attempted_method_name):
61+
"""
62+
- raise ``ValueError`` if ``refresh()`` has not been called.
63+
"""
64+
if self.refreshed is True:
65+
return
66+
msg = f"{self.class_name}.refresh() must be called before accessing "
67+
msg += f"{self.class_name}.{attempted_method_name}."
68+
raise ValueError(msg)
69+
70+
def refresh(self):
71+
72+
self.ep_fabrics_associations = EpFabricAssociations()
73+
self.rest_send.path = self.ep_fabrics_associations.path
74+
self.rest_send.verb = self.ep_fabrics_associations.verb
75+
save_check_mode = self.rest_send.check_mode
76+
self.rest_send.check_mode = False
77+
self.rest_send.commit()
78+
self.rest_send.check_mode = save_check_mode
79+
self.fabric_association_data = copy.deepcopy(self.rest_send.response_current.get("DATA", {}))
80+
81+
msg = f"self.data: {json.dumps(self.data, indent=4, sort_keys=True)}"
82+
self.log.debug(msg)
83+
self.refreshed = True
84+
self.results.response_current = self.rest_send.response_current
85+
self.results.response = self.rest_send.response_current
86+
self.results.result_current = self.rest_send.result_current
87+
self.results.result = self.rest_send.result_current
88+
self.results.register_task_result()
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#
2+
# Copyright (c) 2025 Cisco and/or its affiliates.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
from __future__ import absolute_import, division, print_function
17+
18+
__metaclass__ = type
19+
__author__ = "Prabahal"
20+
21+
import copy
22+
import inspect
23+
import json
24+
import logging
25+
26+
from ..common.api.v1.lan_fabric.rest.control.fabrics.msd.msd import \
27+
EpChildFabricAdd
28+
29+
# Import Results() only for the case where the user has not set Results()
30+
# prior to calling commit(). In this case, we instantiate Results()
31+
# in _validate_commit_parameters() so that we can register the failure
32+
# in commit().
33+
from ..common.results import Results
34+
35+
36+
class childFabricAdd():
37+
"""
38+
methods and properties for adding Child fabric into MSD:
39+
40+
"""
41+
42+
def __init__(self):
43+
self.class_name = self.__class__.__name__
44+
self.action = "child_fabric_add"
45+
self.log = logging.getLogger(f"dcnm.{self.class_name}")
46+
self.ep_fabric_add = EpChildFabricAdd()
47+
msg = "ENTERED childFabricAdd()"
48+
self.log.debug(msg)
49+
50+
def commit(self, payload):
51+
"""
52+
### Summary
53+
- Add child fabrics to Mentioned Parent fabric.
54+
55+
### Raises
56+
- ``ValueError`` if:
57+
- ``_validate_commit_parameters`` raises ``ValueError``.
58+
59+
"""
60+
try:
61+
self._validate_commit_parameters()
62+
except ValueError as error:
63+
# pylint: disable=no-member
64+
self.results.action = self.action
65+
self.results.changed = False
66+
self.results.failed = True
67+
if self.rest_send is not None:
68+
self.results.check_mode = self.rest_send.check_mode
69+
self.results.state = self.rest_send.state
70+
else:
71+
self.results.check_mode = False
72+
self.results.state = "Added"
73+
self.results.register_task_result()
74+
raise ValueError(error) from error
75+
# pylint: enable=no-member
76+
77+
for payload_item in payload:
78+
try:
79+
self.rest_send.path = self.ep_fabric_add.path
80+
self.rest_send.verb = self.ep_fabric_add.verb
81+
self.rest_send.payload = payload_item
82+
self.rest_send.save_settings()
83+
self.rest_send.check_mode = False
84+
self.rest_send.timeout = 1
85+
self.rest_send.commit()
86+
self.rest_send.restore_settings()
87+
except (TypeError, ValueError) as error:
88+
raise ValueError(error) from error
89+
90+
if self.rest_send.result_current["success"] is False:
91+
self.results.diff_current = {}
92+
else:
93+
self.results.diff_current = copy.deepcopy(payload_item)
94+
self.results.action = self.action
95+
self.results.state = self.rest_send.state
96+
self.results.check_mode = self.rest_send.check_mode
97+
self.results.response_current = copy.deepcopy(
98+
self.rest_send.response_current
99+
)
100+
self.results.result_current = copy.deepcopy(self.rest_send.result_current)
101+
self.results.register_task_result()
102+
103+
msg = f"self.results.diff: {json.dumps(self.results.diff, indent=4, sort_keys=True)}"
104+
self.log.debug(msg)
105+
106+
def _validate_commit_parameters(self):
107+
"""
108+
- validate the parameters for commit
109+
- raise ``ValueError`` if ``fabric_names`` is not set
110+
"""
111+
method_name = inspect.stack()[0][3] # pylint: disable=unused-variable
112+
113+
if self.fabric_names is None:
114+
msg = f"{self.class_name}.{method_name}: "
115+
msg += "fabric_names must be set prior to calling commit."
116+
raise ValueError(msg)
117+
118+
# pylint: disable=no-member
119+
if self.rest_send is None:
120+
msg = f"{self.class_name}.{method_name}: "
121+
msg += "rest_send must be set prior to calling commit."
122+
raise ValueError(msg)
123+
124+
# pylint: disable=access-member-before-definition
125+
# pylint: disable=attribute-defined-outside-init
126+
if self.results is None:
127+
# Instantiate Results() only to register the failure
128+
self.results = Results()
129+
msg = f"{self.class_name}.{method_name}: "
130+
msg += "results must be set prior to calling commit."
131+
raise ValueError(msg)
132+
# pylint: enable=access-member-before-definition
133+
# pylint: enable=attribute-defined-outside-init

0 commit comments

Comments
 (0)