blob: 5c3fe70574f95653332899f8fa8934b3f3d929d7 [file] [log] [blame]
Rizwan Haider30b33792016-08-18 02:11:18 -04001import os
2import sys
3
4from synchronizers.base.syncstep import SyncStep
Rizwan Haidereb2cc772016-09-08 12:14:55 -04005from services.metronetwork.models import *
Rizwan Haider30b33792016-08-18 02:11:18 -04006from xos.logger import Logger, logging
7from synchronizers.metronetwork.providers.providerfactory import ProviderFactory
Rizwan Haider65baf552016-09-28 16:47:28 -04008from synchronizers.metronetwork.invokers.invokerfactory import InvokerFactory
Rizwan Haider30b33792016-08-18 02:11:18 -04009
10# metronetwork will be in steps/..
11parentdir = os.path.join(os.path.dirname(__file__), "..")
12sys.path.insert(0, parentdir)
13
14logger = Logger(level=logging.INFO)
15
16
Rizwan Haider65baf552016-09-28 16:47:28 -040017class SyncMetroNetworkSystem(SyncStep):
18 provides = [MetroNetworkSystem]
19 observes = MetroNetworkSystem
Rizwan Haider30b33792016-08-18 02:11:18 -040020 requested_interval = 0
21 initialized = False
22
23 def __init__(self, **args):
24 SyncStep.__init__(self, **args)
25
26 def fetch_pending(self, deletion=False):
27
28 # The general idea:
29 # We do one of two things in here:
30 # 1. Full Synchronization of the DBS (XOS <-> MetroONOS)
31 # 2. Look for updates between the two stores
32 # The first thing is potentially a much bigger
33 # operation and should not happen as often
34 #
35 # The Sync operation must take into account the 'deletion' flag
36
37 objs = []
38
Rizwan Haider65baf552016-09-28 16:47:28 -040039 # Get the NetworkSystem object - if it exists it will test us
Rizwan Haider30b33792016-08-18 02:11:18 -040040 # whether we should do a full sync or not - it all has our config
41 # information about the REST interface
42
Rizwan Haider65baf552016-09-28 16:47:28 -040043 metronetworksystem = self.get_metronetwork_system()
44 if not metronetworksystem:
Rizwan Haider30b33792016-08-18 02:11:18 -040045 logger.debug("No Service configured")
46 return objs
47
Rizwan Haider65baf552016-09-28 16:47:28 -040048 # Check to make sure the Metro Network System is enabled
49 metronetworksystem = self.get_metronetwork_system()
50 if metronetworksystem.administrativeState == 'disabled':
Rizwan Haider30b33792016-08-18 02:11:18 -040051 # Nothing to do
52 logger.debug("MetroService configured - state is Disabled")
53 return objs
54
55 # The Main Loop - retrieve all the NetworkDevice objects - for each of these
56 # Apply synchronization aspects
57 networkdevices = NetworkDevice.objects.all()
58
59 for dev in networkdevices:
60
61 # Set up the provider
62 provider = ProviderFactory.getprovider(dev)
63
64 # First check is for the AdminState of Disabled - do nothing
65 if dev.administrativeState == 'disabled':
66 # Nothing to do with this device
67 logger.debug("NetworkDevice %s: administrativeState set to Disabled - continuing" % dev.id)
68
69 # Now to the main options - are we syncing - deletion portion
70 elif dev.administrativeState == 'syncrequested' and deletion is True:
71
72 logger.info("NetworkDevice %s: administrativeState set to SyncRequested" % dev.id)
73
74 # Kill Links
75 networklinks = provider.get_network_links_for_deletion()
76 for link in networklinks:
77 objs.append(link)
78
79 # Kill Ports
80 allports = provider.get_network_ports_for_deletion()
81 for port in allports:
82 objs.append(port)
83
84 logger.info("NetworkDevice %s: Deletion part of Sync completed" % dev.id)
85 dev.administrativeState = 'syncinprogress'
86 dev.save(update_fields=['administrativeState'])
87
88 # Now to the main options - are we syncing - creation portion
89 elif dev.administrativeState == 'syncinprogress' and deletion is False:
90
91 logger.info("NetworkDevice %s: administrativeState set to SyncRequested" % dev.id)
92 # Reload objects in the reverse order of deletion
93
94 # Add Ports
95 networkports = provider.get_network_ports()
96 for port in networkports:
97 objs.append(port)
98
99 # Add Links
100 networklinks = provider.get_network_links()
101 for link in networklinks:
102 objs.append(link)
103
104 logger.info("NetworkDevice %s: Creation part of Sync completed" % dev.id)
105 dev.administrativeState = 'enabled'
106 dev.save(update_fields=['administrativeState'])
107
108 # If we are enabled - then check for events - in either direction and sync
109 elif dev.administrativeState == 'enabled' and deletion is False:
110 logger.debug("NetworkDevice: administrativeState set to Enabled - non deletion phase")
111
112 # This should be the 'normal running state' when we are not deleting - a few things to do in here
113
114 # Get the changed objects from the provider - deletions are handled separately
115 eventobjs = provider.get_updated_or_created_objects()
116 for eventobj in eventobjs:
117 # Simply put in the queue for update - this will handle both new and changed objects
118 objs.append(eventobj)
119
120 # Handle changes XOS -> ONOS
121 # Check for ConnectivityObjects that are in acticationequested state - creates to the backend
122 activatereqs = NetworkEdgeToEdgePointConnection.objects.filter(adminstate='activationrequested')
123 for activatereq in activatereqs:
124
125 # Call the XOS Interface to create the service
126 logger.debug("Attempting to create EdgePointToEdgePointConnectivity: %s" % activatereq.id)
127 if (provider.create_point_to_point_connectivity(activatereq)):
128 # Everyting is OK, lets let the system handle the persist
129 objs.append(activatereq)
130 else:
131 # In the case of an error we persist the state of the object directly to preserve
132 # the error code - and because that is how the base synchronizer is designed
133 activatereq.save()
134
135 # Check for ConnectivityObjects that are in deacticationequested state - deletes to the backend
136 deactivatereqs = NetworkEdgeToEdgePointConnection.objects.filter(adminstate='deactivationrequested')
137 for deactivatereq in deactivatereqs:
138
139 # Call the XOS Interface to delete the service
140 logger.debug("Attempting to delete EdgePointToEdgePointConnectivity: %s" % deactivatereq.id)
141 if provider.delete_point_to_point_connectivity(deactivatereq):
142 # Everyting is OK, lets let the system handle the persist
143 objs.append(deactivatereq)
144 else:
145 # In the case of an error we persist the state of the object directly to preserve
146 # the error code - and because that is how the base synchronizer is designed
147 deactivatereq.save()
148
149 # If we are enabled - and in our deletion pass then look for objects waiting for deletion
150 elif dev.administrativeState == 'enabled' and deletion is True:
151 logger.debug("NetworkDevice: administrativeState set to Enabled - deletion phase")
152
153 # Any object that is simply deleted in the model gets removed automatically - the synchronizer
154 # doesn't get involved - we just need to check for deleted objects in the domain and reflect that
155 # in the model
156 #
157 # Get the deleted objects from the provider
158 eventobjs = provider.get_deleted_objects()
159 for eventobj in eventobjs:
160 # Simply put in the queue for update - this will handle both new and changed objects
161 objs.append(eventobj)
162
163 # In add cases return the objects we are interested in
164 return objs
165
166 def sync_record(self, o):
Rizwan Haider65baf552016-09-28 16:47:28 -0400167
168 # First we call and see if there is an invoker for this object - the idea of the invoker
169 # is to wrap the save with a pre/post paradigm to handle special cases
170 # It will only exist for a subset of ojbects
171 invoker = InvokerFactory.getinvoker(o)
172
173 # Call Pre-save on the inovker (if it exists)
174 if invoker is not None:
175 invoker.presave(o)
176
Rizwan Haider30b33792016-08-18 02:11:18 -0400177 # Simply save the record to the DB - both updates and adds are handled the same way
178 o.save()
179
Rizwan Haider65baf552016-09-28 16:47:28 -0400180 # Call Post-save on the inovker (if it exists)
181 if invoker is not None:
182 invoker.postsave(o)
183
Rizwan Haider30b33792016-08-18 02:11:18 -0400184 def delete_record(self, o):
185 # Overriden to customize our behaviour - the core sync step for will remove the record directly
186 # We just log and return
187 logger.debug("deleting Object %s" % str(o), extra=o.tologdict())
188
Rizwan Haider65baf552016-09-28 16:47:28 -0400189 def get_metronetwork_system(self):
Rizwan Haider30b33792016-08-18 02:11:18 -0400190 # We only expect to have one of these objects in the system in the curent design
191 # So get the first element from the query
Rizwan Haider65baf552016-09-28 16:47:28 -0400192 metronetworksystem = MetroNetworkSystem.objects.all()
193 if not metronetworksystem:
Rizwan Haider30b33792016-08-18 02:11:18 -0400194 return None
195
Rizwan Haider65baf552016-09-28 16:47:28 -0400196 return metronetworksystem[0]