def _updateRemoteCSR(self, localCSE):
		Logging.logDebug('Updating remote CSR: %s' % remoteCSR.rn)
		csr = CSR.CSR()
		self._copyCSE2CSE(csr, localCSE)
		del csr['acpi']			# remove ACPI (don't provide ACPI in updates...a bit)
		data = json.dumps(csr.asJSON())

		(jsn, rc) = CSE.httpServer.sendUpdateRequest(self.remoteCSRURL, self.originator, data=data)
		if rc not in [C.rcUpdated, C.rcOK]:
			if rc != C.rcAlreadyExists:
				Logging.logDebug('Error updating remote CSR: %d' % rc)
			return (None, rc)
		Logging.logDebug('Remote CSR updated: %s' % self.remoteCseid)
		return (CSR.CSR(jsn), C.rcUpdated)
	def _updateCSRonRegistrarCSE(self, localCSE:Resource=None) -> Result:
		Logging.logDebug(f'Updating registrar CSR in CSE: {self.registrarCSI}')
		if localCSE is None:
			localCSE = Utils.getCSE().resource
		csr = CSR.CSR()
		self._copyCSE2CSR(csr, localCSE, isUpdate=True)
		del csr['acpi']			# remove ACPI (don't provide ACPI in updates...a bit)

		res = CSE.request.sendUpdateRequest(self.registrarCSRURL, CSE.cseCsi, data=csr.asDict(), ct=self.registrarSerialization) 	# own CSE.csi is the originator
		if res.rsc not in [ RC.updated, RC.OK ]:
			if res.rsc != RC.alreadyExists:
				Logging.logDebug(f'Error updating registrar CSR in CSE: {res.rsc:d}')
			return Result(rsc=res.rsc, dbg='cannot update remote CSR')
		Logging.logDebug(f'Registrar CSR updated in CSE: {self.registrarCSI}')
		return Result(resource=CSR.CSR(res.dict, pi=''), rsc=RC.updated)
    def _createRemoteCSR(self) -> Tuple[Resource, int, str]:
        Logging.logDebug('Creating remote CSR: %s' % self.remoteCsi)

        # get local CSEBase and copy relevant attributes
        localCSE, _, _ = Utils.getCSE()
        csr = CSR.CSR(rn=localCSE.ri)  # ri as name!
        self._copyCSE2CSE(csr, localCSE)
        csr['ri'] = self.cseCsi  # override ri with the own cseID
        csr['cb'] = Utils.getIdFromOriginator(localCSE.csi)  # only the stem
        for _ in ['ty', 'ri', 'ct', 'lt']:
            del (csr[_])  # remove a couple of attributes
        data = json.dumps(csr.asJSON())

        # Create the <remoteCSE> in the remote CSE
        Logging.logDebug('Creating remote CSR at: %s url: %s' %
                         (self.remoteCsi, self.remoteCSEURL))
        jsn, rc, msg = CSE.httpServer.sendCreateRequest(self.remoteCSEURL,
                                                        self.originator,
                                                        ty=C.tCSR,
                                                        data=data)
        if rc not in [C.rcCreated, C.rcOK]:
            if rc != C.rcAlreadyExists:
                Logging.logDebug('Error creating remote CSR: %d' % rc)
            return None, rc, 'cannot create remote CSR'
        Logging.logDebug('Remote CSR created: %s' % self.remoteCsi)

        return None, C.rcCreated, None
 def _retrieveRemoteCSR(self) -> Tuple[Resource, int, str]:
     Logging.logDebug('Retrieving remote CSR: %s' % self.remoteCsi)
     jsn, rc, msg = CSE.httpServer.sendRetrieveRequest(
         self.remoteCSRURL, self.originator)
     if rc not in [C.rcOK]:
         return None, rc, msg
     return CSR.CSR(jsn), C.rcOK, None
    def _updateRemoteCSR(self,
                         localCSE: Resource) -> Tuple[Resource, int, str]:
        Logging.logDebug('Updating remote CSR: %s' % self.remoteCsi)
        csr = CSR.CSR()
        self._copyCSE2CSE(csr, localCSE)
        del csr['acpi']  # remove ACPI (don't provide ACPI in updates...a bit)
        data = json.dumps(csr.asJSON())

        jsn, rc, msg = CSE.httpServer.sendUpdateRequest(self.remoteCSRURL,
                                                        self.originator,
                                                        data=data)
        if rc not in [C.rcUpdated, C.rcOK]:
            if rc != C.rcAlreadyExists:
                Logging.logDebug('Error updating remote CSR: %d' % rc)
            return None, rc, 'cannot update remote CSR'
        Logging.logDebug('Remote CSR updated: %s' % self.remoteCsi)
        return CSR.CSR(jsn), C.rcUpdated, None
	def _createRemoteCSR(self):
		Logging.logDebug('Creating remote CSR: %s' % self.remoteCseid)
		# get local CSEBase and copy relevant attributes

		(localCSE, _) = Utils.getCSE()
		csr = CSR.CSR()
		self._copyCSE2CSE(csr, localCSE)
		csr['ri'] = self.cseCsi
		data = json.dumps(csr.asJSON())

		(jsn, rc) = CSE.httpServer.sendCreateRequest(self.remoteCSEURL, self.originator, ty=C.tCSR, data=data)
		if rc not in [C.rcCreated, C.rcOK]:
			if rc != C.rcAlreadyExists:
				Logging.logDebug('Error creating remote CSR: %d' % rc)
			return (None, rc)
		Logging.logDebug('Remote CSR created: %s' % self.remoteCseid)
		return (CSR.CSR(jsn), C.rcCreated)
	def _createCSRonRegistrarCSE(self) -> Result:
		Logging.logDebug(f'Creating registrar CSR: {self.registrarCSI}')		
		# get local CSEBase and copy relevant attributes
		localCSE = Utils.getCSE().resource
		csr = CSR.CSR(rn=localCSE.ri) # ri as name!
		self._copyCSE2CSR(csr, localCSE)
		#csr['ri'] = CSE.cseCsi							# override ri with the own cseID
		#csr['cb'] = Utils.getIdFromOriginator(localCSE.csi)	# only the stem
		for _ in ['ty','ri', 'ct', 'lt']: csr.delAttribute(_, setNone=False)	# remove a couple of attributes
		#for _ in ['ty','ri', 'ct', 'lt']: del(csr[_])	# remove a couple of attributes

		# Create the <remoteCSE> in the remote CSE
		Logging.logDebug(f'Creating registrar CSR at: {self.registrarCSI} url: {self.registrarCSEURL}')	
		res = CSE.request.sendCreateRequest(self.registrarCSEURL, CSE.cseCsi, ty=T.CSR, data=csr.asDict(), ct=self.registrarSerialization) # own CSE.csi is the originator
		if res.rsc not in [ RC.created, RC.OK ]:
			if res.rsc != RC.alreadyExists:
				Logging.logDebug(f'Error creating registrar CSR: {res.rsc:d}')
			return Result(rsc=res.rsc, dbg='cannot create remote CSR')
		Logging.logDebug(f'Registrar CSR created: {self.registrarCSI}')
		return Result(resource=CSR.CSR(res.dict, pi=''), rsc=RC.created)
	def _createLocalCSR(self, remoteCSE: Resource) -> Result:
		Logging.logDebug(f'Creating local CSR: {remoteCSE.ri}')

		# copy local CSE attributes into a new CSR
		localCSE = Utils.getCSE().resource
		csr = CSR.CSR(pi=localCSE.ri, rn=remoteCSE.csi[1:])	# remoteCSE.csi as name!
		self._copyCSE2CSR(csr, remoteCSE)
		#csr['ri'] = remoteCSE.ri 						# set the ri to the remote CSE's ri
		csr['ri'] = remoteCSE.csi[1:] 						# set the ri to the remote CSE's ri
		# add local CSR and ACP's
		if (result := CSE.dispatcher.createResource(csr, localCSE)).resource is None:
			return result # Problem
    def _createLocalCSR(self,
                        remoteCSE: Resource) -> Tuple[Resource, int, str]:
        Logging.logDebug('Creating local CSR: %s' % remoteCSE.ri)

        # copy local CSE attributes into a new CSR
        localCSE, _, _ = Utils.getCSE()
        csr = CSR.CSR(pi=localCSE.ri, rn=remoteCSE.ri)  # remoteCSE.ri as name!
        self._copyCSE2CSE(csr, remoteCSE)
        csr['ri'] = remoteCSE.ri  # set the ri to the remote CSE's ri

        # add local CSR and ACP's
        if (res := CSE.dispatcher.createResource(csr, localCSE))[0] is None:
            return res  # Problem
	def _createLocalCSR(self, remoteCSE):
		Logging.logDebug('Creating local CSR: %s' % remoteCSE.ri)

		# copy attributes
		(localCSE, _) = Utils.getCSE()
		csr = CSR.CSR()
		# csr['pi'] = localCSE['ri']
		csr['pi'] = Configuration.get('cse.ri')
		self._copyCSE2CSE(csr, remoteCSE)
		csr['ri'] = remoteCSE.ri

		# add local CSR
		return CSE.dispatcher.createResource(csr, localCSE)
Example #11
0
def resourceFromJSON(jsn: dict, pi: str = None, acpi: str = None, ty: int = None, create: bool = False, isImported: bool = False) -> Tuple[Resource.Resource, str]:
	""" Create a resource from a JSON structure.
		This will *not* call the activate method, therefore some attributes
		may be set separately.
	"""
	jsn, root = pureResource(jsn)	# remove optional "m2m:xxx" level
	typ = jsn['ty'] if 'ty' in jsn else ty
	if typ != None and ty != None and typ != ty:
		return None, 'type and resource specifier mismatch'
	mgd = jsn['mgd'] if 'mgd' in jsn else None		# for mgmtObj

	# Add extra acpi
	if acpi is not None:
		jsn['acpi'] = acpi if type(acpi) is list else [ acpi ]

	# store the import status in the original jsn
	if isImported:
		jsn[Resource.Resource._imported] = True	# Indicate that this is an imported resource


	# sorted by assumed frequency (small optimization)
	if typ == C.tCIN or root == C.tsCIN:
		return CIN.CIN(jsn, pi=pi, create=create), None
	elif typ == C.tCNT or root == C.tsCNT:
		return CNT.CNT(jsn, pi=pi, create=create), None
	elif typ == C.tGRP or root == C.tsGRP:
		return GRP.GRP(jsn, pi=pi, create=create), None
	elif typ == C.tGRP_FOPT or root == C.tsGRP_FOPT:
		return GRP_FOPT.GRP_FOPT(jsn, pi=pi, create=create), None
	elif typ == C.tACP or root == C.tsACP:
		return ACP.ACP(jsn, pi=pi, create=create), None
	elif typ == C.tFCNT:
		return FCNT.FCNT(jsn, pi=pi, fcntType=root, create=create), None
	elif typ == C.tFCI:
		return FCI.FCI(jsn, pi=pi, fcntType=root, create=create), None	
	elif typ == C.tAE or root == C.tsAE:
		return AE.AE(jsn, pi=pi, create=create), None
	elif typ == C.tSUB or root == C.tsSUB:
		return SUB.SUB(jsn, pi=pi, create=create), None
	elif typ == C.tCSR or root == C.tsCSR:
		return CSR.CSR(jsn, pi=pi, create=create), None
	elif typ == C.tNOD or root == C.tsNOD:
		return NOD.NOD(jsn, pi=pi, create=create), None
	elif (typ == C.tMGMTOBJ and mgd == C.mgdFWR) or root == C.tsFWR:
		return FWR.FWR(jsn, pi=pi, create=create), None
	elif (typ == C.tMGMTOBJ and mgd == C.mgdSWR) or root == C.tsSWR:
		return SWR.SWR(jsn, pi=pi, create=create), None
	elif (typ == C.tMGMTOBJ and mgd == C.mgdMEM) or root == C.tsMEM:
		return MEM.MEM(jsn, pi=pi, create=create), None
	elif (typ == C.tMGMTOBJ and mgd == C.mgdANI) or root == C.tsANI:
		return ANI.ANI(jsn, pi=pi, create=create), None
	elif (typ == C.tMGMTOBJ and mgd == C.mgdANDI) or root == C.tsANDI:
		return ANDI.ANDI(jsn, pi=pi, create=create), None
	elif (typ == C.tMGMTOBJ and mgd == C.mgdBAT) or root == C.tsBAT:
		return BAT.BAT(jsn, pi=pi, create=create), None
	elif (typ == C.tMGMTOBJ and mgd == C.mgdDVI) or root == C.tsDVI:
		return DVI.DVI(jsn, pi=pi, create=create), None
	elif (typ == C.tMGMTOBJ and mgd == C.mgdDVC) or root == C.tsDVC:
		return DVC.DVC(jsn, pi=pi, create=create), None
	elif (typ == C.tMGMTOBJ and mgd == C.mgdRBO) or root == C.tsRBO:
		return RBO.RBO(jsn, pi=pi, create=create), None
	elif (typ == C.tMGMTOBJ and mgd == C.mgdEVL) or root == C.tsEVL:
		return EVL.EVL(jsn, pi=pi, create=create), None
	elif typ == C.tCNT_LA or root == C.tsCNT_LA:
		return CNT_LA.CNT_LA(jsn, pi=pi, create=create), None
	elif typ == C.tCNT_OL or root == C.tsCNT_OL:
		return CNT_OL.CNT_OL(jsn, pi=pi, create=create), None
	elif typ == C.tFCNT_LA:
		return FCNT_LA.FCNT_LA(jsn, pi=pi, create=create), None
	elif typ == C.tFCNT_OL:
		return FCNT_OL.FCNT_OL(jsn, pi=pi, create=create), None
	elif typ == C.tCSEBase or root == C.tsCSEBase:
		return CSEBase.CSEBase(jsn, create=create), None

	return Unknown.Unknown(jsn, typ, root, pi=pi, create=create), None	# Capture-All resource
Example #12
0
def resourceFromJSON(jsn, pi=None, acpi=None, tpe=None, create=False):
    (jsn, root) = pureResource(jsn)  # remove optional "m2m:xxx" level
    ty = jsn['ty'] if 'ty' in jsn else tpe
    if ty != None and tpe != None and ty != tpe:
        return None
    mgd = jsn['mgd'] if 'mgd' in jsn else None  # for mgmtObj

    # Add extra acpi
    if acpi is not None:
        jsn['acpi'] = acpi if type(acpi) is list else [acpi]

    # sorted by assumed frequency (small optimization)
    if ty == C.tCIN or root == C.tsCIN:
        return CIN.CIN(jsn, pi=pi, create=create)
    elif ty == C.tCNT or root == C.tsCNT:
        return CNT.CNT(jsn, pi=pi, create=create)
    elif ty == C.tGRP or root == C.tsGRP:
        return GRP.GRP(jsn, pi=pi, create=create)
    elif ty == C.tGRP_FOPT or root == C.tsGRP_FOPT:
        return GRP_FOPT.GRP_FOPT(jsn, pi=pi, create=create)
    elif ty == C.tACP or root == C.tsACP:
        return ACP.ACP(jsn, pi=pi, create=create)
    elif ty == C.tFCNT:
        return FCNT.FCNT(jsn, pi=pi, fcntType=root, create=create)
    elif ty == C.tFCI:
        return FCI.FCI(jsn, pi=pi, fcntType=root, create=create)
    elif ty == C.tAE or root == C.tsAE:
        return AE.AE(jsn, pi=pi, create=create)
    elif ty == C.tSUB or root == C.tsSUB:
        return SUB.SUB(jsn, pi=pi, create=create)
    elif ty == C.tCSR or root == C.tsCSR:
        return CSR.CSR(jsn, pi=pi, create=create)
    elif ty == C.tNOD or root == C.tsNOD:
        return NOD.NOD(jsn, pi=pi, create=create)
    elif (ty == C.tMGMTOBJ and mgd == C.mgdFWR) or root == C.tsFWR:
        return FWR.FWR(jsn, pi=pi, create=create)
    elif (ty == C.tMGMTOBJ and mgd == C.mgdSWR) or root == C.tsSWR:
        return SWR.SWR(jsn, pi=pi, create=create)
    elif (ty == C.tMGMTOBJ and mgd == C.mgdMEM) or root == C.tsMEM:
        return MEM.MEM(jsn, pi=pi, create=create)
    elif (ty == C.tMGMTOBJ and mgd == C.mgdANI) or root == C.tsANI:
        return ANI.ANI(jsn, pi=pi, create=create)
    elif (ty == C.tMGMTOBJ and mgd == C.mgdANDI) or root == C.tsANDI:
        return ANDI.ANDI(jsn, pi=pi, create=create)
    elif (ty == C.tMGMTOBJ and mgd == C.mgdBAT) or root == C.tsBAT:
        return BAT.BAT(jsn, pi=pi, create=create)
    elif (ty == C.tMGMTOBJ and mgd == C.mgdDVI) or root == C.tsDVI:
        return DVI.DVI(jsn, pi=pi, create=create)
    elif (ty == C.tMGMTOBJ and mgd == C.mgdDVC) or root == C.tsDVC:
        return DVC.DVC(jsn, pi=pi, create=create)
    elif (ty == C.tMGMTOBJ and mgd == C.mgdRBO) or root == C.tsRBO:
        return RBO.RBO(jsn, pi=pi, create=create)
    elif (ty == C.tMGMTOBJ and mgd == C.mgdEVL) or root == C.tsEVL:
        return EVL.EVL(jsn, pi=pi, create=create)
    elif ty == C.tCNT_LA or root == C.tsCNT_LA:
        return CNT_LA.CNT_LA(jsn, pi=pi, create=create)
    elif ty == C.tCNT_OL or root == C.tsCNT_OL:
        return CNT_OL.CNT_OL(jsn, pi=pi, create=create)
    elif ty == C.tFCNT_LA:
        return FCNT_LA.FCNT_LA(jsn, pi=pi, create=create)
    elif ty == C.tFCNT_OL:
        return FCNT_OL.FCNT_OL(jsn, pi=pi, create=create)

    elif ty == C.tCSEBase or root == C.tsCSEBase:
        return CSEBase.CSEBase(jsn, create=create)
    else:
        return Unknown.Unknown(jsn, ty, root, pi=pi,
                               create=create)  # Capture-All resource
    return None
	def _retrieveCSRfromRegistrarCSE(self) -> Result:
		Logging.logDebug(f'Retrieving remote CSR: {self.registrarCSI}')
		result = CSE.request.sendRetrieveRequest(self.registrarCSRURL, CSE.cseCsi, ct=self.registrarSerialization)	# own CSE.csi is the originator
		if result.rsc not in [ RC.OK ]:
			return result.errorResult()
		return Result(resource=CSR.CSR(result.dict, pi=''), rsc=RC.OK)
	def _retrieveRemoteCSR(self):
		#Logging.logDebug('Retrieving remote CSR: %s' % self.remoteCseid)
		(jsn, rc) = CSE.httpServer.sendRetrieveRequest(self.remoteCSRURL, self.originator)
		if rc not in [C.rcOK]:
			return (None, rc)
		return (CSR.CSR(jsn), C.rcOK)