Beispiel #1
0
def appendConfig(config, dic):
    """Append to dic values from config and also dates."""
    dic['hostname'] = config.get('agent', 'hostname')
    dic['ip'] = config.get('general', 'ip')
    dic['insertTime'] = getUTCnow()
    dic['updateTime'] = getUTCnow()
    return dic
Beispiel #2
0
 def _addDeltaStatesInModel(self, mainGraph, state, addition):
     """Add Delta States into Model."""
     # Issue details: https://github.com/sdn-sense/siterm/issues/73
     for conn in addition:
         if 'timestart' in list(conn.keys()) and state == 'committed':
             if conn['timestart'] < getUTCnow():
                 state = 'activating'
             else:
                 state = 'scheduled'
                 print('scheduled')
         elif 'timeend' in list(conn.keys(
         )) and conn['timeend'] < getUTCnow() and state == 'activated':
             state = 'deactivating'
         # Add new State under SwitchSubnet
         mainGraph.add(
             (self.prefixDB.genUriRef(custom=conn['connectionID']),
              self.prefixDB.genUriRef('mrs', 'tag'),
              self.prefixDB.genLiteral('monitor:status:%s' % state)))
         # If timed delta, add State under lifetime resource
         if 'timestart' in list(conn.keys()) or 'timeend' in list(
                 conn.keys()):
             mainGraph.add(
                 (self.prefixDB.genUriRef(custom="%s:lifetime" %
                                          conn['connectionID']),
                  self.prefixDB.genUriRef('mrs', 'tag'),
                  self.prefixDB.genLiteral('monitor:status:%s' % state)))
     return mainGraph
Beispiel #3
0
def timeendcheck(delta, logger):
    """ Check delta timeEnd. if passed, returns True. """
    conns = []
    # ------------------------------------
    # This is for backwards support. Can be deleted after all RMs deltas re-initiated.
    if isinstance(delta['addition'], dict):
        conns.append(delta['addition'])
    else:
        conns = delta['addition']
    # ------------------------------------
    connEnded = False
    for connDelta in conns:
        try:
            if 'timeend' in connDelta.keys():
                timeleft = getUTCnow() - int(connDelta['timeend'])
                logger.info('CurrentTime %s TimeStart %s. TimeLeft %s' %
                            (getUTCnow(), connDelta['timeend'], timeleft))
                if connDelta['timeend'] < getUTCnow():
                    logger.info('This delta already passed timeend mark. \
                                Setting state to cancel')
                    connEnded = True
                else:
                    logger.info('Time did not passed yet.')
        except IOError:
            logger.info('This delta had an error checking endtime. \
                        Leaving state as it is.')
    return connEnded
Beispiel #4
0
 def submitdebug(self, inputDict, **kwargs):
     """Submit new debug action request."""
     dbobj = getVal(self.dbI, **kwargs)
     out = {
         'hostname': inputDict['dtn'],
         'state': 'new',
         'requestdict': json.dumps(inputDict),
         'output': '',
         'insertdate': getUTCnow(),
         'updatedate': getUTCnow()
     }
     return dbobj.insert('debugrequests', [out])
Beispiel #5
0
 def committed(self, dbObj):
     """Committing state Check."""
     for delta in dbObj.get('deltas', search=[['state', 'committed']]):
         if delta['deltat'] in ['addition', 'modify'] and delta['addition']:
             delta['addition'] = evaldict(delta['addition'])
             # Check the times...
             delayStart = False
             for connDelta in delta['addition']:
                 try:
                     if 'timestart' in list(connDelta.keys()):
                         timeleft = int(
                             connDelta['timestart']) - getUTCnow()
                         self.logger.info(
                             'CurrentTime %s TimeStart %s. Seconds Left %s'
                             % (getUTCnow(), connDelta['timestart'],
                                timeleft))
                         if connDelta['timestart'] < getUTCnow():
                             self.logger.info(
                                 'This delta already passed timestart mark. \
                                              Setting state to activating')
                             delayStart = not delayStart
                         else:
                             self.logger.info(
                                 'This delta %s did not passed timestart mark. \
                                              Leaving state as it is' %
                                 delta['uid'])
                             delayStart = True
                 except:
                     self.logger.info(
                         'This delta %s had an error checking starttime. \
                                      Leaving state as it is.' %
                         delta['uid'])
                 if not delayStart:
                     self._stateChangerDelta(dbObj, 'activating', **delta)
                     self.connMgr.activating(dbObj, delta)
         else:
             self.logger.info(
                 'This delta %s is in committed. Setting state to activating.'
                 % delta['uid'])
             self._stateChangerDelta(dbObj, 'activating', **delta)
             if delta['deltat'] in ['reduction']:
                 for connid in evaldict(delta['connectionid']):
                     for dConn in dbObj.get(
                             'delta_connections',
                             search=[['connectionid', connid]]):
                         self.logger.info(
                             'This connection %s belongs to this delta: %s. Cancel delta'
                             % (connid, dConn['deltaid']))
                         deltaRem = {'uid': dConn['deltaid']}
                         self._stateChangerDelta(dbObj, 'remove',
                                                 **deltaRem)
Beispiel #6
0
 def addNewDelta(self, uploadContent, environ, **kwargs):
     """ Add new delta """
     dbobj = getVal(self.dbI, **kwargs)
     hashNum = uploadContent['id']
     if dbobj.get('deltas', search=[['uid', hashNum]], limit=1):
         # This needs to be supported as it can be re-initiated again. TODO
         msg = 'Something weird has happened... Check server logs; Same ID is already in DB'
         kwargs['http_respond'].ret_409('application/json',
                                        kwargs['start_response'], None)
         return getCustomOutMsg(errMsg=msg, errCode=409)
     tmpfd = NamedTemporaryFile(delete=False)
     tmpfd.close()
     self.getmodel(uploadContent['modelId'], **kwargs)
     outContent = {
         "ID": hashNum,
         "InsertTime": getUTCnow(),
         "UpdateTime": getUTCnow(),
         "Content": uploadContent,
         "State": "accepting",
         "modelId": uploadContent['modelId']
     }
     self.siteDB.saveContent(tmpfd.name, outContent)
     out = self.policer[kwargs['sitename']].acceptDelta(tmpfd.name)
     outDict = {
         'id': hashNum,
         'lastModified': convertTSToDatetime(outContent['UpdateTime']),
         'href': "%s/%s" % (environ['SCRIPT_URI'], hashNum),
         'modelId': out['modelId'],
         'state': out['State'],
         'reduction': out['ParsedDelta']['reduction'],
         'addition': out['ParsedDelta']['addition']
     }
     print 'Delta was %s. Returning info %s' % (out['State'], outDict)
     if out['State'] in ['accepted']:
         kwargs['http_respond'].ret_201(
             'application/json', kwargs['start_response'],
             [('Last-Modified', httpdate(out['UpdateTime'])),
              ('Location', outDict['href'])])
         return outDict
     else:
         kwargs['http_respond'].ret_500('application/json',
                                        kwargs['start_response'], None)
         if 'Error' in out.keys():
             errMsg = ""
             for key in ['errorNo', 'errorType', 'errMsg']:
                 if key in out['Error'].keys():
                     errMsg += " %s: %s" % (key, out['Error'][key])
         return getCustomOutMsg(errMsg=errMsg, exitCode=500)
Beispiel #7
0
def checkNewDelta(deltaIn):
    """Check if new delta can be provisioned"""
    # We check only timing is ok here. All other cases are checked
    # by deltas comparison (vlan, host, timings)
    if getUTCnow() >= deltaIn['times'][1]:
        raise BadRequestError(
            'New delta endtime is in the past. Nothing to configure.')
Beispiel #8
0
def reportServiceStatus(servicename, status, sitename, logger, hostname=""):
    """ Report service state to DB """
    try:
        if not hostname:
            hostname = socket.gethostname()
        dbOut = {
            'hostname': hostname,
            'servicestate': status,
            'servicename': servicename,
            'updatedate': getUTCnow()
        }
        dbI = getDBConn(servicename)
        dbobj = getVal(dbI, **{'sitename': sitename})
        services = dbobj.get('servicestates',
                             search=[['hostname', hostname],
                                     ['servicename', servicename]])
        if not services:
            dbobj.insert('servicestates', [dbOut])
        else:
            dbobj.update('servicestates', [dbOut])
    except Exception:
        excType, excValue = sys.exc_info()[:2]
        logger.critical(
            "Error details in reportServiceStatus. ErrorType: %s, ErrMsg: %s",
            str(excType.__name__), excValue)
Beispiel #9
0
 def remove(self, dbObj):
     """ Check on all remove state deltas """
     for delta in dbObj.get('deltas', search=[['state', 'remove']]):
         if delta['updatedate'] < int(getUTCnow() - 600):
             self._stateChangerDelta(dbObj, 'removed', **delta)
             self.modelstatecancel(dbObj, **delta)
     return
Beispiel #10
0
 def _deltaAddition(self, dbObj, delta, mainGraphName, updateState=True):
     """Delta addition lookup."""
     delta['content'] = evaldict(delta['content'])
     self.logger.info('Working on %s delta addition in state' %
                      delta['uid'])
     mainGraph = Graph()
     mainGraph.parse(mainGraphName, format='turtle')
     addition = delta['content']['addition']
     tmpFile = tempfile.NamedTemporaryFile(delete=False, mode="w+")
     tmpFile.write(addition)
     tmpFile.close()
     tmpGraph = Graph()
     tmpGraph.parse(tmpFile.name, format='turtle')
     os.unlink(tmpFile.name)
     self.logger.info('Main Graph len: %s Addition Len: %s', len(mainGraph),
                      len(tmpGraph))
     mainGraph += tmpGraph
     # Add delta states for that specific delta
     mainGraph = self._addDeltaStatesInModel(mainGraph, delta['state'],
                                             evaldict(delta['addition']))
     if updateState:
         dbObj.update('deltasmod', [{
             'uid': delta['uid'],
             'updatedate': getUTCnow(),
             'modadd': 'added'
         }])
     return mainGraph
Beispiel #11
0
def deltas_id(environ, **kwargs):
    """
    API Call associated with specific delta
    Method: GET
    Output: application/json
    Examples: https://server-host/sitefe/v1/deltas/([-_A-Za-z0-9]+) # Will return info about specific delta
    """
    # METHOD DELETE!!!!! TODO
    if environ['REQUEST_METHOD'].upper() == 'DELETE':
        kwargs['http_respond'].ret_405('application/json',
                                       kwargs['start_response'],
                                       ('Location', '/'))
        print('DELETE Method is not supported yet. Return 405')
        return [
            getCustomOutMsg(errMsg="Method %s is not supported in %s" %
                            environ['REQUEST_METHOD'].upper(),
                            errCode=405)
        ]
    modTime = getModTime(kwargs['headers'])
    print('Delta Status query for %s' % kwargs['mReg'][0])
    delta = DELTABACKEND.getdelta(kwargs['mReg'][0], **kwargs)
    if not delta:
        kwargs['http_respond'].ret_204(
            'application/json', kwargs['start_response'],
            [('Last-Modified', httpdate(getUTCnow()))])
        print('Return empty list. There are no deltas on the system')
        return []
    if modTime > delta['updatedate']:
        print(
            'Delta with ID %s was not updated so far. Time request comparison requested'
            % kwargs['mReg'][0])
        kwargs['http_respond'].ret_304(
            'application/json', kwargs['start_response'],
            ('Last-Modified', httpdate(delta['updatedate'])))
        return []
    if kwargs['urlParams']['oldview']:
        kwargs['http_respond'].ret_200(
            'application/json', kwargs['start_response'],
            [('Last-Modified', httpdate(delta['updatedate']))])
        return [delta]
    current = {}
    current = {
        "id": delta['uid'],
        "lastModified": convertTSToDatetime(delta['updatedate']),
        "state": delta['state'],
        "href": "%s" % environ['SCRIPT_URI'],
        "modelId": delta['modelid']
    }
    if not kwargs['urlParams']['summary']:
        current['addition'] = encodebase64(delta['addition'],
                                           kwargs['urlParams']['encode'])
        current['reduction'] = encodebase64(delta['reduction'],
                                            kwargs['urlParams']['encode'])
    print('Returning delta %s information. Few details: ModelID: %s, State: %s, LastModified: %s' % \
          (current["id"], current["modelId"], current["state"], current["lastModified"]))
    kwargs['http_respond'].ret_200(
        'application/json', kwargs['start_response'],
        [('Last-Modified', httpdate(delta['updatedate']))])
    return [current]
Beispiel #12
0
 def _modelstatechanger(dbObj, newState, **kwargs):
     """Model State change."""
     tNow = getUTCnow()
     dbObj.update('deltasmod', [{
         'uid': kwargs['uid'],
         'modadd': newState,
         'updatedate': tNow
     }])
Beispiel #13
0
 def updatedebug(self, inputDict, **kwargs):
     """Update debug action information."""
     dbobj = getVal(self.dbI, **kwargs)
     out = {
         'id': kwargs['mReg'][1],
         'state': inputDict['state'],
         'output': inputDict['output'],
         'updatedate': getUTCnow()
     }
     return dbobj.update('debugrequests', [out])
Beispiel #14
0
 def committed(self, dbObj):
     """ Committing state Check """
     for delta in dbObj.get('deltas', search=[['state', 'committed']]):
         if delta['deltat'] == 'addition' and delta['addition']:
             delta['addition'] = evaldict(delta['addition'])
             # Check the times...
             delayStart = False
             for connDelta in delta['addition']:
                 try:
                     if 'timestart' in connDelta.keys():
                         timeleft = int(
                             connDelta['timestart']) - getUTCnow()
                         self.logger.info(
                             'CurrentTime %s TimeStart %s. Seconds Left %s'
                             % (getUTCnow(), connDelta['timestart'],
                                timeleft))
                         if connDelta['timestart'] < getUTCnow():
                             self.logger.info(
                                 'This delta already passed timestart mark. \
                                              Setting state to activating')
                             delayStart = False if delayStart is not True else True
                         else:
                             self.logger.info(
                                 'This delta %s did not passed timestart mark. \
                                              Leaving state as it is' %
                                 delta['uid'])
                             delayStart = True
                 except:
                     self.logger.info(
                         'This delta %s had an error checking starttime. \
                                      Leaving state as it is.' %
                         delta['uid'])
                 if not delayStart:
                     self._stateChangerDelta(dbObj, 'activating', **delta)
                     self.connMgr.activating(dbObj, delta)
         else:
             self.logger.info(
                 'This delta %s is in committed. Setting state to activating.'
                 % delta['uid'])
             self._stateChangerDelta(dbObj, 'activating', **delta)
Beispiel #15
0
 def activated(self, dbObj):
     """Check on all activated state deltas."""
     for delta in dbObj.get('deltas', search=[['state', 'activated']]):
         # Reduction
         if delta['deltat'] in ['reduction']:
             if delta['updatedate'] < int(getUTCnow() - 600):
                 self._stateChangerDelta(dbObj, 'removing', **delta)
             continue
         # Addition
         delta['addition'] = evaldict(delta['addition'])
         if timeendcheck(delta, self.logger):
             self._stateChangerDelta(dbObj, 'cancel', **delta)
             self.modelstatecancel(dbObj, **delta)
Beispiel #16
0
 def _stateChangerHost(self, dbObj, hid, **kwargs):
     """Change state for host."""
     tNow = getUTCnow()
     self.logger.info(
         'Changing delta %s hoststate %s to %s' %
         (kwargs['deltaid'], kwargs['hostname'], kwargs['state']))
     dbObj.update('hoststates', [{
         'deltaid': kwargs['deltaid'],
         'state': kwargs['state'],
         'updatedate': tNow,
         'id': hid
     }])
     dbObj.insert('hoststateshistory', [kwargs])
Beispiel #17
0
 def _stateChangerDelta(self, dbObj, newState, **kwargs):
     """Delta State change."""
     tNow = getUTCnow()
     self.logger.info('Changing delta %s to %s' % (kwargs['uid'], newState))
     dbObj.update('deltas', [{
         'uid': kwargs['uid'],
         'state': newState,
         'updatedate': tNow
     }])
     dbObj.insert('states', [{
         'deltaid': kwargs['uid'],
         'state': newState,
         'insertdate': tNow
     }])
Beispiel #18
0
def timeendcheck(delta, logger):
    """Check delta timeEnd.

    if passed, returns True.
    """
    conns = []
    connEnded = False
    for connDelta in conns:
        try:
            if 'timeend' in list(connDelta.keys()):
                timeleft = getUTCnow() - int(connDelta['timeend'])
                logger.info('CurrentTime %s TimeStart %s. TimeLeft %s' %
                            (getUTCnow(), connDelta['timeend'], timeleft))
                if connDelta['timeend'] < getUTCnow():
                    logger.info('This delta already passed timeend mark. \
                                Setting state to cancel')
                    connEnded = True
                else:
                    logger.info('Time did not passed yet.')
        except IOError:
            logger.info('This delta had an error checking endtime. \
                        Leaving state as it is.')
    return connEnded
Beispiel #19
0
 def _deltaReduction(self, dbObj, delta, mainGraphName):
     """ Delta reduction """
     delta['content'] = evaldict(delta['content'])
     self.logger.info('Working on %s delta reduction in state' % delta['uid'])
     mainGraph = Graph()
     mainGraph.parse(mainGraphName, format='turtle')
     reduction = delta['content']['reduction']
     tmpFile = tempfile.NamedTemporaryFile(delete=False)
     tmpFile.write(reduction)
     tmpFile.close()
     tmpGraph = Graph()
     tmpGraph.parse(tmpFile.name, format='turtle')
     os.unlink(tmpFile.name)
     self.logger.info('Main Graph len: %s Reduction Len: %s', len(mainGraph), len(tmpGraph))
     mainGraph -= tmpGraph
     dbObj.update('deltasmod', [{'uid': delta['uid'], 'updatedate': getUTCnow(), 'modadd': 'removed'}])
     return mainGraph
Beispiel #20
0
 def getServiceStates(self, registry, **kwargs):
     """Get all Services states."""
     serviceState = Enum(
         'service_state',
         'Description of enum',
         labelnames=['servicename'],
         states=['OK', 'UNKNOWN', 'FAILED', 'KEYBOARDINTERRUPT'],
         registry=registry)
     services = self.dbI[kwargs['sitename']].get('servicestates')
     # {'servicestate': u'OK', 'hostname': u'4df8c7b989d1',
     #  'servicename': u'LookUpService', 'id': 1, 'updatedate': 1601047007}
     timenow = int(getUTCnow())
     for service in services:
         state = 'UNKNOWN'
         if int(timenow - service['updatedate']) < 120:
             # If we are not getting service state for 2 mins, leave state as unknown
             state = service['servicestate']
         serviceState.labels(
             servicename=service['servicename']).state(state)
Beispiel #21
0
 def commitdelta(self,
                 deltaID,
                 newState='UNKNOWN',
                 internal=False,
                 hostname=None,
                 **kwargs):
     """Change delta state."""
     dbobj = getVal(self.dbI, **kwargs)
     if internal:
         out = dbobj.get('hoststates',
                         search=[['deltaid', deltaID],
                                 ['hostname', hostname]])
         if not out:
             msg = 'This query did not returned any host states for %s %s' % (
                 deltaID, hostname)
             raise WrongDeltaStatusTransition(msg)
         self.stateM._stateChangerHost(
             dbobj, out[0]['id'], **{
                 'deltaid': deltaID,
                 'state': newState,
                 'insertdate': getUTCnow(),
                 'hostname': hostname
             })
         if newState == 'remove':
             # Remove state comes only from modify and by agent itself
             self.stateM.stateChange(dbobj, {
                 'uid': deltaID,
                 'state': newState
             })
         return getCustomOutMsg(msg='Internal State change approved',
                                exitCode=200)
     delta = self.getdelta(deltaID, **kwargs)
     print('Commit Action for delta %s' % delta)
     # Now we go directly to commited in case of commit
     if delta['state'] != 'accepted':
         msg = "Delta    state in the system is not in accepted state. \
               State on the system: %s. Not allowed to change." % delta[
             'state']
         print(msg)
         raise WrongDeltaStatusTransition(msg)
     self.stateM.commit(dbobj, {'uid': deltaID, 'state': 'committing'})
     return {'status': 'OK'}
Beispiel #22
0
 def updatehost(self, inputDict, **kwargs):
     """ Update Host in DB.
         Must provide dictionary with:
             ip       -> ip of new host
         Example:
             GOOD: {"ip": "1.2.3.4", "site": "T2_US_Caltech", "status": "benchhmark",
                    "hostype": "gridftp", "port": 11234}"""
     # Validate that these entries are known...
     dbobj = getVal(self.dbI, **kwargs)
     host = dbobj.get('hosts', limit=1, search=[['ip', inputDict['ip']]])
     if not host:
         raise NotFoundError(
             'This IP %s is not registered at all. Call addhost' %
             inputDict['ip'])
     out = {
         'id': host[0]['id'],
         'hostname': inputDict['hostname'],
         'ip': inputDict['ip'],
         'updatedate': getUTCnow(),
         'hostinfo': str(inputDict)
     }
     dbobj.update('hosts', [out])
     return
Beispiel #23
0
def deltas(environ, **kwargs):
    """
    API Call associated with deltas
    Method: GET
    Output: application/json
    Examples: https://server-host/sitefe/v1/deltas/ # Will return info about all deltas
    Method: POST
    Output: application/json
    Examples: https://server-host/sitefe/v1/deltas/ # Will add new delta and returns it's ID
    """
    # ======================================================
    # GET
    if environ['REQUEST_METHOD'].upper() == 'GET':
        modTime = getModTime(kwargs['headers'])
        outdeltas = DELTABACKEND.getdelta(None, **kwargs)
        if kwargs['urlParams']['oldview']:
            print 'Return All deltas. 200 OK'
            kwargs['http_respond'].ret_200('application/json',
                                           kwargs['start_response'], None)
            return outdeltas
        outM = {"deltas": []}
        if not outdeltas:
            kwargs['http_respond'].ret_204(
                'application/json', kwargs['start_response'],
                [('Last-Modified', httpdate(getUTCnow()))])
            print 'Return empty list. There are no deltas on the system'
            return []
        updateTimestamp = 0
        for delta in outdeltas:
            if modTime > delta['updatedate']:
                continue
            updateTimestamp = updateTimestamp if updateTimestamp > delta[
                'updatedate'] else delta['updatedate']
            current = {
                "id": delta['uid'],
                "lastModified": convertTSToDatetime(delta['updatedate']),
                "state": delta['state'],
                "href": "%s/%s" % (environ['SCRIPT_URI'], delta['uid']),
                "modelId": delta['modelid']
            }
            if not kwargs['urlParams']['summary']:
                # Doing here not encode, because we are decoding. So it is opposite.
                current["addition"] = decodebase64(
                    delta['addition'], not kwargs['urlParams']['encode'])
                current["reduction"] = decodebase64(
                    delta['reduction'], not kwargs['urlParams']['encode'])
            outM["deltas"].append(current)
        if not outM["deltas"]:
            kwargs['http_respond'].ret_304(
                'application/json', kwargs['start_response'],
                ('Last-Modified', httpdate(modTime)))
            return []
        kwargs['http_respond'].ret_200(
            'application/json', kwargs['start_response'],
            [('Last-Modified', httpdate(updateTimestamp))])
        print 'Return Last Delta. 200 OK'
        return outM["deltas"]
    # ======================================================
    # POST
    out = {}
    postRequest = False
    if environ['REQUEST_METHOD'].upper() == 'POST':
        postRequest = is_post_request(environ)
    if not postRequest:
        if is_application_json(environ):
            out = get_json_post_form(environ)
        else:
            kwargs['http_respond'].ret_400('application/json',
                                           kwargs['start_response'], None)
            customErr = getCustomOutMsg(
                errMsg=
                'You did POST method, but provided CONTENT_TYPE is not correct',
                errCode=400)
            print 'Return 400. More details: %s' % customErr
            return customErr
    if not out:
        out = get_post_form(environ)
    newDelta = {}
    for key in out.keys():
        newDelta[key] = out.get(key, "")
    for key in ['modelId', 'id']:
        if not newDelta[key]:
            customErr = getCustomOutMsg(
                errMsg='You did POST method, %s is not specified' % key,
                errCode=400)
            print 'Wrong delta: %s. Parsed:%s Error: %s' % (out, newDelta,
                                                            customErr)
            kwargs['http_respond'].ret_400('application/json',
                                           kwargs['start_response'], None)
            return customErr
    if not newDelta['reduction'] and not newDelta['addition']:
        customErr = getCustomOutMsg(
            errMsg=
            'You did POST method, but nor reduction, nor addition is present',
            errCode=400)
        print 'Wrong delta: %s. Parsed:%s Error: %s' % (out, newDelta,
                                                        customErr)
        kwargs['http_respond'].ret_400('application/json',
                                       kwargs['start_response'], None)
        return customErr
    return DELTABACKEND.addNewDelta(newDelta, environ, **kwargs)
Beispiel #24
0
    def startwork(self):
        """Main start """
        self.logger.info('Started LookupService work')
        dbObj = getVal(self.dbI, **{'sitename': self.sitename})
        workDir = self.config.get(self.sitename, 'privatedir') + "/LookUpService/"
        createDirs(workDir)
        self.newGraph = Graph()
        jOut = getAllHosts(self.sitename, self.logger)
        # ==================================================================================
        # 1. Define Basic MRML Prefixes
        # ==================================================================================
        self.defineMRMLPrefixes()
        # ==================================================================================
        # 2. Define Basic MRML Definition
        # ==================================================================================
        self.defineMRMLServices()
        self.hosts = {}

        for _, nodeDict in jOut.items():
            # ==================================================================================
            # 3. Define Node inside yaml
            # ==================================================================================
            self.defineNodeInformation(nodeDict)
            # ==================================================================================
            # 4. Define Routing Service information
            # ==================================================================================
            self.defineLayer3MRML(nodeDict)
            # ==================================================================================
            # 5. Define Host Information and all it's interfaces.
            # ==================================================================================
            self.defineHostInfo(nodeDict)
        # ==================================================================================
        # 6. Define Switch information from Switch Lookup Plugin
        # ==================================================================================
        self.addSwitchInfo(jOut)

        saveName = self.getModelSavePath()
        with open(saveName, "w") as fd:
            fd.write(self.newGraph.serialize(format='turtle'))
        hashNum = generateHash(self.newGraph.serialize(format='turtle'))

        # Append all deltas to the model
        self.appendDeltas(dbObj, saveName)
        if dbObj.get('models', limit=1, search=[['uid', hashNum]]):
            raise Exception('hashNum %s is already in database...' % hashNum)

        self.logger.info('Checking if new model is different from previous')
        modelsEqual, modelinDB = self.checkForModelDiff(dbObj, saveName)
        lastKnownModel = {'uid': hashNum, 'insertdate': getUTCnow(),
                          'fileloc': saveName, 'content': str(self.newGraph.serialize(format='turtle'))}
        if modelsEqual:
            if modelinDB[0]['insertdate'] < int(getUTCnow() - 3600):
                # Force to update model every hour, Even there is no update;
                self.logger.info('Forcefully update model in db as it is older than 1h')
                dbObj.insert('models', [lastKnownModel])
            else:
                self.logger.info('Models are equal.')
                lastKnownModel = modelinDB[0]
                os.unlink(saveName)
        else:
            self.logger.info('Models are different. Update DB')
            dbObj.insert('models', [lastKnownModel])

        self.logger.debug('Last Known Model: %s' % str(lastKnownModel))
        # Clean Up old models (older than 24h.)
        for model in dbObj.get('models', limit=100, orderby=['insertdate', 'ASC']):
            if model['insertdate'] < int(getUTCnow() - 86400):
                self.logger.debug('delete %s', model)
                try:
                    os.unlink(model['fileloc'])
                except OSError as ex:
                    self.logger.debug('Got OS Error removing this model %s. Exc: %s' % (model, str(ex)))
                dbObj.delete('models', [['id', model['id']]])
Beispiel #25
0
def models(environ, **kwargs):
    """
    Returns a collection of available model resources within the Resource Manager
    Method: GET
    Output: application/json
    Examples: https://server-host/sitefe/v1/models/ # Returns list of all models;
    """
    # Get IF_MODIFIED_SINCE modification time in timestamp
    modTime = getModTime(kwargs['headers'])
    outmodels = DELTABACKEND.getmodel(**kwargs)
    if not outmodels:
        kwargs['http_respond'].ret_500('application/json',
                                       kwargs['start_response'], None)
        print 'LastModel does not exist in dictionary. First time run? See documentation'
        return getCustomOutMsg(errMsg="No models are available...",
                               errCode=500)
    outmodels = [outmodels] if isinstance(outmodels, dict) else outmodels
    outM = {"models": []}
    current = {
        "id": outmodels[0]['uid'],
        "creationTime": convertTSToDatetime(outmodels[0]['insertdate']),
        "href": "%s/%s" % (environ['SCRIPT_URI'], outmodels[0]['uid'])
    }
    print outmodels[0]['insertdate'], modTime, getUTCnow()
    if outmodels[0]['insertdate'] < modTime:
        print '%s and %s' % (outmodels[0]['insertdate'], modTime)
        kwargs['http_respond'].ret_304(
            'application/json', kwargs['start_response'],
            ('Last-Modified', httpdate(outmodels[0]['insertdate'])))
        return []
    kwargs['http_respond'].ret_200(
        'application/json', kwargs['start_response'],
        [('Last-Modified', httpdate(outmodels[0]['insertdate']))])
    if kwargs['urlParams']['oldview']:
        print 'Requested oldview model output. Return 200'
        return outmodels
    elif kwargs['urlParams']['current']:
        if not kwargs['urlParams']['summary']:
            current['model'] = encodebase64(
                DELTABACKEND.getmodel(outmodels[0]['uid'],
                                      content=True,
                                      **kwargs), kwargs['urlParams']['encode'])
        outM['models'].append(current)
        print 'Requested only current model. Return 200. Last Model %s' % outmodels[
            0]['uid']
        return [current]
    elif not kwargs['urlParams']['current']:
        for model in outmodels:
            tmpDict = {
                "id": model['uid'],
                "creationTime": convertTSToDatetime(model['insertdate']),
                "href": "%s/%s" % (environ['SCRIPT_URI'], model['uid'])
            }
            if not kwargs['urlParams']['summary']:
                tmpDict['model'] = encodebase64(
                    DELTABACKEND.getmodel(model['uid'], content=True,
                                          **kwargs),
                    kwargs['urlParams']['encode'])
            outM['models'].append(tmpDict)
        print 'Returning all models known to the system. Return 200'
        return outM['models']
Beispiel #26
0
 def _newhoststate(dbObj, **kwargs):
     """Private to add new host states."""
     tNow = getUTCnow()
     kwargs['insertdate'] = tNow
     kwargs['updatedate'] = tNow
     dbObj.insert('hoststates', [kwargs])