package ru.petrsu.nest.ldap;
import ru.petrsu.nest.san.LazyReference;
import java.lang.*;
import java.util.*;
import java.io.*;
/**
* Refernce to element in cache.
* @author Nuikin Alexander
*/
13 abstract class CacheReference<K, U> implements LazyReference<U> {
/** ID of referenced cache element */
private K ID;
private Class<U> type;
private StorageCache<K, ? super U> cache;
/**Creates reference to the specified cache element.
* Storage cache must be gived to constructor for realize possibility of
* parrallel using several storage systems.
* @param c storage cache.
* @param id element ID in cache ( storage system ).
*/
protected CacheReference
( StorageCache<K, ? super U> c, K id, Class<U> t ) {
cache = c;
ID = id;
type = t;
}
/**Get referenced cache element.
* @return referenced cache element.
*/
public U get( ) {
U el = null;
System.out.println( "CacheReference: get ["+ID+"]" );
try {
if( cache.isLoadedByID( ID ) ) {
el = type.cast( cache.get( ID ) );
}
}catch( UnknownIDException e ) {
System.out.println( e );
}
return el;
}
/**Implements LazyReference.getLoaded( ).
* @see LazyReference#getLoaded( )
*/
public U getLoaded( ) {
U el = null;
System.out.println( "CacheReference: getLoaded ["+ID+"]" );
try {
if( cache.isLoadedByID( ID ) )
el = type.cast( cache.get( ID ) );
}catch( UnknownIDException e ) {
System.out.println( e );
}
return el;
}
/**Two references are equal if referenced elements has equal ID in cache.
*/
public boolean equals( Object o ) {
if ( o instanceof CacheReference ) {
return ( ( CacheReference<?, ?> ) o ).ID == ID;
}
if ( o instanceof LazyReference ) {
return ( ( LazyReference<?> ) o ).getLoaded( ) == getLoaded( );
}
return false;
}
/**Returns cache ID of referenced object.
*/
K getID( ) {
return ID;
}
}
1 package ru.petrsu.nest.ldap;
/**Driver for testing some system class.
* @author Alexander Nuikin*/
5 abstract class DriverClass {
private boolean stopOnBad;
/**Standard label for begining subtest of class.*/
8 protected String label = "===> ";
/**Standard label for successfull finishing subtest of class.*/
10 protected String goodLabel = "OK";
/**Standard label for unsuccessfull finishing subtest of class.*/
12 protected String badLabel = "BAD";
/**Stop testing after first bad finished test if stopOnBad is true.
* Returns true if test finished successfully ( no bugs found ).
* */
17 abstract boolean test( boolean stopOnBad );
/**Set label for output.*/
20 public void setLabel( String l ) {
label = l;
}
}
/**Driver for testing some system interface.*/
/*
abstract class InterfaceDriver <T> extends ClassDriver {
T instance = null;
<U extends T> InterfaceDriver( ) {
instance = new U( );
}
}*/
package ru.petrsu.nest.ldap;
import ru.petrsu.nest.san.*;
import java.lang.*;
import java.util.*;
import ru.petrsu.nest.ldap.*;
import java.io.File;
import java.io.IOException;
10 class DriverLdapSanStorage extends DriverClass {
MasterHoleyCache <SanElement> cache;
File xmlF = new File( "san001.xml" );
XmlSanManager xSAN;
SAN san;
Collection<SanElement> sEl;
public DriverLdapSanStorage( ){
try {
xSAN = new XmlSanManager( xmlF );
} catch ( IOException ex ){}
san = xSAN.getSAN( );
sEl = xSAN.getElements( SanElement.class );
System.err.println( xSAN.getElements( SanElement.class ) );
}
boolean test( boolean StopOnBad ){
LdapSanStorage ldapSStorage = null;
try {
ldapSStorage = new LdapSanStorage( cache, "", "", "", "" );
cache = new MasterHoleyCache<SanElement>( ldapSStorage );
} catch ( StorageBadConnectionException ex ){}
Device rootDev = san.getRootDevice( );
CompositeOU rootOU = san.getRootOU( );
// Добавляем новые элементы в кэш и хранилище
Iterator<SanElement> i = sEl.iterator( );
while( i.hasNext( ) ){
SanElement s = i.next( );
cache.add( s );
//ldapSStorage.add( cache.IDOf( s ), s );
}
// Устанавливает rootID
try {
ldapSStorage.setRootDeviceID( cache.IDOf( rootDev ) );
} catch ( UnknownIDException ex ){}
// Теперь попробуем извлечь все, что добавили
System.err.println( ldapSStorage.getElements( SanElement.class ) );
// Проверяем правильность возращения ID главного устройства( rootDevice )
if ( ldapSStorage.getRootDeviceID( )== cache.IDOf( rootDev ) )
System.err.println( "OK rootDeviceID" );
else System.err.println( "BAD rootDeviceID" );
// Проверяем правильность возращения ID главного административного юника( rootOU )
if ( ldapSStorage.getRootOUID( ) == cache.IDOf( rootOU ) )
System.err.println( "OK rootOUID" );
else System.err.println( "BAD rootOUID" );
return true;
}
/*
public static void main( String args[] ){
DriverLdapSanStorage dlss = new DriverLdapSanStorage( );
System.err.println( dlss.xSAN.getElements( SanElement.class ) );
dlss.test( false );
}
*/
}
package ru.petrsu.nest.ldap;
import ru.petrsu.nest.san.*;
import java.lang.*;
import java.util.*;
import ru.petrsu.nest.ldap.*;
import java.io.File;
import java.io.IOException;
10 class DriverLdapSanStorageManager extends DriverClass {
MasterHoleyCache <SanElement> cache;
File xmlF;
XmlSanManager xSAN;
SAN san;
Collection<SanElement> sEl;
Device rootDev;
CompositeOU rootOU;
LdapSanStorageManager sManager;
public DriverLdapSanStorageManager( ){
File xmlF = new File( "san001.xml" );
try {
xSAN = new XmlSanManager( xmlF );
} catch ( IOException ex ){}
san = xSAN.getSAN( );
sEl = xSAN.getElements( SanElement.class );
rootDev = san.getRootDevice( );
rootOU = san.getRootOU( );
}
boolean test( boolean StopOnBad ){
int countElement = 0;
try {
sManager = new LdapSanStorageManager( "cn=Nest-Manager, dc=cs, dc=karelia, dc=ru", "test", "ldap://213.59.11.30:389", "ou=nest, dc=cs, dc=karelia, dc=ru" );
} catch ( StorageBadConnectionException ex ){}
System.out.println( sManager.getElements( SanElement.class ) );
SAN san = sManager.getSAN( );
return true;
}
public static void main( String args[] ){
DriverLdapSanStorageManager dsllm = new DriverLdapSanStorageManager( );
dsllm.test( false );
}
/*
// Добавляет элементы в кэш в правильно порядке
private void loadInLdap( ){
// Добавляем элементы в дерево ldap
Iterator<SanElement> i = xSAN.getElements( SanElement.class ).iterator( );
Collection<SanElement> clonesEl = xSAN.getElements( SanElement.class );
System.err.println( "--->> Добаляем элементы в LDAP" );
// Записываем сначало главные элементы ( rootOU, rootDev ) и здания
while( i.hasNext( ) ){
SanElement s = i.next( );
if ( rootOU.equals( s ) ){
System.err.println( "Add in ldap " + s+ " " + cache.IDOf( s ) );
ldapSStorage.add( cache.IDOf( s ), s );
clonesEl.remove( s );
}
if ( rootDev.equals( s ) ){
System.err.println( "Add in ldap " + s+ " " + cache.IDOf( s ) );
ldapSStorage.add( cache.IDOf( s ), s );
clonesEl.remove( s );
}
if ( s instanceof Building ){
System.err.println( "Add in ldap " + s+ " " + cache.IDOf( s ) );
ldapSStorage.add( cache.IDOf( s ), s );
clonesEl.remove( s );
}
}
sEl.clear( );
sEl.addAll( clonesEl );
i = sEl.iterator( );
// Теперь запишем этажи
while( i.hasNext( ) ){
SanElement s = i.next( );
if ( s instanceof Floor ){
System.err.println( "Add in ldap " + s+ " " + cache.IDOf( s ) );
ldapSStorage.add( cache.IDOf( s ), s );
clonesEl.remove( s );
}
}
sEl.clear( );
sEl.addAll( clonesEl );
i = sEl.iterator( );
// Записываем все остальное
while( i.hasNext( ) ){
SanElement s = i.next( );
System.err.println( "Add in ldap " + s+ " " + cache.IDOf( s ) );
ldapSStorage.add( cache.IDOf( s ), s );
}
}
// Удаляет элеметны из кэша в правильном порядке
public void rmByID( ){
Iterator<SanElement> i = xSAN.getElements( SanElement.class ).iterator( );
System.err.println( "-->>> Удаляем все элементы по ID" );
// Удалим структуру N, структуру A и здания
while( i.hasNext( ) ){
SanElement s = i.next( );
if ( cache.isLoaded( s ) && !( s instanceof Floor ) && !( s instanceof Building ) ){
System.err.println( "Delete " + s );
try {
cache.removeByID( cache.IDOf( s ) );
}catch ( UnknownIDException ex ){}
}
}
// Удалим все этажи
i = xSAN.getElements( SanElement.class ).iterator( );
while( i.hasNext( ) ){
SanElement s = i.next( );
if ( cache.isLoaded( s ) && s instanceof Floor ){
System.err.println( "Delete " + s );
try {
cache.removeByID( cache.IDOf( s ) );
}catch ( UnknownIDException ex ){}
}
}
// Удалим все остальное ( осталось только здания )
i = xSAN.getElements( SanElement.class ).iterator( );
while( i.hasNext( ) ){
SanElement s = i.next( );
if ( cache.isLoaded( s ) ){
System.err.println( "Delete " + s );
try {
cache.removeByID( cache.IDOf( s ) );
}catch ( UnknownIDException ex ){}
}
}
}
*/
}
package ru.petrsu.nest.ldap;
import ru.petrsu.nest.san.*;
import java.lang.*;
import java.util.*;
import ru.petrsu.nest.ldap.*;
import java.io.File;
import java.io.IOException;
10 class DriverMasterHoleyCache extends DriverClass {
MasterHoleyCache <SanElement> cache;
File xmlF;
XmlSanManager xSAN;
SAN san;
Collection<SanElement> sEl;
LdapSanStorage ldapSStorage;
Device rootDev;
CompositeOU rootOU;
MasterHoleyCache <SanElement> cc;
public static void main( String args[] ){
DriverMasterHoleyCache dmhc = new DriverMasterHoleyCache( );
dmhc.test( false );
}
public DriverMasterHoleyCache( ){
File xmlF = new File( "san001.xml" );
try {
xSAN = new XmlSanManager( xmlF );
} catch ( IOException ex ){}
san = xSAN.getSAN( );
sEl = xSAN.getElements( SanElement.class );
ldapSStorage = null;
rootDev = san.getRootDevice( );
rootOU = san.getRootOU( );
}
boolean test( boolean StopOnBad ){
int countElement = 0;
try {
cache = new MasterHoleyCache<SanElement>( );
ldapSStorage = new LdapSanStorage( cache, "cn=Nest-Manager, dc=cs, dc=karelia, dc=ru", "test", "ldap://213.59.11.30:389", "ou=nest, dc=cs, dc=karelia, dc=ru" );
cache.checkOut( );
} catch ( StorageBadConnectionException ex ){}
Iterator<SanElement> i = sEl.iterator( );
// Добавляем новые элементы в кэш
loadInCache( );
// Добавляем элементы в дерево Ldap
loadInLdap( xSAN.getElements( SanElement.class ) );
countElement = xSAN.getElements( SanElement.class ).size( );
System.out.println( "--->> Проверка подсчета количества элементов" );
// Проверяем количество ID в cache
if ( countElement == cache.countOfID( ) )
System.out.println( "countOfID : OK" );
else
System.out.println( "countOfID : BAD" );
// Проверяем количество элементов в дереве
if ( countElement == cache.countOfLoaded( ) )
System.out.println( "countOfLoaded : OK" );
else
System.out.println( "countOfLoaded : BAD" );
// Проверяем работы функции проверки на загруженность элемента в кэш
checkLoad( );
/* // Удаляем rootOU
i = xSAN.getElements( SanElement.class ).iterator( );
while ( i.hasNext( ) ){
SanElement s = i.next( );
if ( rootOU.equals( s ) ){
System.out.println( "Delete " + s );
cache.remove( s );
break;
}
}
*/
// теперь попробуем удалить каждый второй элемент структуры
/* i = xSAN.getElements( SanElement.class ).iterator( );
System.out.println( "-->>> Удаляем каждый второй элемент" );
while( true ){
// if ( i.hasNext( ) ) i.next( );
// else break;
if ( i.hasNext( ) ){
SanElement s = i.next( );
System.out.println( "Delete " + s );
cache.remove( s );
}else break;
}
*/
// Удалим теперь все, что осталось по ID
/* i = xSAN.getElements( SanElement.class ).iterator( );
System.out.println( "-->>> Удаляем все оставшиеся элементы по ID" );
while( i.hasNext( ) ){
SanElement s = i.next( );
if ( cache.isLoaded( s ) ){
System.out.println( "Delete " + s );
try {
cache.removeByID( cache.IDOf( s ) );
}catch ( UnknownIDException ex ){}
}
}
*/
// Удаляем элементы в правильно порядке функцией кэша removeByID
rmByID( );
// Проверяем работы функции проверки на загруженность элемента в кэш
checkLoad( );
System.out.println( "-->>>" );
// Проверяем количество ID в cache
if ( cache.countOfID( ) == 0 )
System.out.println( "countOfID = 0 : OK" );
else
System.out.println( "countOfID <> 0: BAD" );
// Проверяем количество элементов в дереве
if ( cache.countOfLoaded( ) == 0 )
System.out.println( "countOfLoaded = 0: OK" );
else
System.out.println( "countOfLoaded <> 0: BAD" );
// Добавляем новые элементы в кэш
loadInCache( );
// Добавляем элементы в дерево Ldap
loadInLdap( xSAN.getElements( SanElement.class ) );
// Удаляем элементы из cache remove
rm( );
// Проверяем работы функции проверки на загруженность элемента в кэш
checkLoad( );
System.out.println( "-->>>" );
// Проверяем количество ID в cache
if ( cache.countOfID( ) == 0 )
System.out.println( "countOfID = 0 : OK" );
else
System.out.println( "countOfID <> 0: BAD" );
// Проверяем количество элементов в дереве
if ( cache.countOfLoaded( ) == 0 )
System.out.println( "countOfLoaded = 0: OK" );
else
System.out.println( "countOfLoaded <> 0: BAD" );
// Добавляем новые элементы в кэш
loadInCache( );
// Добавляем элементы в дерево Ldap
loadInLdap( xSAN.getElements( SanElement.class ) );
System.out.println( "--->> Созданием новый кэш и LdapSanStorage и заполняем их из кэша" );
LdapSanStorage lss;
try {
cc = new MasterHoleyCache<SanElement>( );
lss = new LdapSanStorage( cc, "cn=Nest-Manager, dc=cs, dc=karelia, dc=ru", "test", "ldap://213.59.11.30:389", "ou=nest, dc=cs, dc=karelia, dc=ru" );
cc.checkOut( );
} catch ( StorageBadConnectionException ex ){
System.out.println( "StorageBadConnectException" );
}
// Посмотрим, что у нас теперь в кэше находится
sEl = cc.getElements( SanElement.class );
System.out.println( "Size of cache " + sEl.size( ) );
i = sEl.iterator( );
while ( i.hasNext( ) ){
SanElement s = i.next( );
System.out.println( "Exist in new cache " + s +" ID=" + cc.IDOf( s ) );
}
System.out.println( "--->> Берем ID нового кэша и смотрим есть ли такой в старом" );
i = sEl.iterator( );
while ( i.hasNext( ) ){
SanElement s = i.next( );
try {
if ( cache.isLoadedByID( cc.IDOf( s ) ) )
System.out.println( s +" содержится в старом кэше " + cc.IDOf( s ) );
else System.out.println( s +" не содержится в старом кэше " + cc.IDOf( s ) );
}catch ( UnknownIDException e ){}
}
System.out.println( "--->> Проверяем метод get. Берем ID из нового кэша и get его из старого" );
i = sEl.iterator( );
while ( i.hasNext( ) ){
SanElement s = i.next( );
try {
cache.get( cc.IDOf( s ) );
System.out.println( s +" get from old cache " );
}catch ( UnknownIDException e ){
System.out.println( "UnknownIDException " );
}
}
System.out.println( "--->> Проверяем метод get. Теперь берем ID из старого кэша и get его из нового" );
i = xSAN.getElements( SanElement.class ).iterator( );
while ( i.hasNext( ) ){
SanElement s = i.next( );
try {
cc.get( cache.IDOf( s ) );
System.out.println( s +" get from new cache " );
}catch ( UnknownIDException e ){
System.out.println( "UnknownIDException " );
}
}
//Теперь получим все элементы из кэша
System.out.println( "--->> Грузим все имеющиеся элементы в кэше getLoadedElements и выводим на экран" );
i = cache.getLoadedElements( ).iterator( );
while ( i.hasNext( ) ){
SanElement s = i.next( );
System.out.println( s );
}
// Удаляем все, чтобы не удалять вручную
rmByID( );
return true;
}
// Добавляем в кэш элементы
private void loadInCache( ){
Iterator<SanElement> i = xSAN.getElements( SanElement.class ).iterator( );
System.out.println( "--->> Добавляем элементы в кэш" );
while( i.hasNext( ) ){
SanElement s = i.next( );
System.out.println( "Add in cache " + s + " " + cache.add( s ) );
}
}
// Добавляет элементы в кэш в правильно порядке
private void loadInLdap( Collection<SanElement> els ){
// Добавляем элементы в дерево ldap
Iterator<SanElement> i = els.iterator( );
Collection<SanElement> clonesEl = xSAN.getElements( SanElement.class );
System.out.println( "--->> Добаляем элементы в LDAP" );
// Записываем сначало главные элементы ( rootOU, rootDev ) и здания
while( i.hasNext( ) ){
SanElement s = i.next( );
if ( rootOU.equals( s ) ){
System.out.println( "Add in ldap " + s+ " " + cache.IDOf( s ) );
ldapSStorage.add( cache.IDOf( s ), s );
clonesEl.remove( s );
}
if ( rootDev.equals( s ) ){
System.out.println( "Add in ldap " + s+ " " + cache.IDOf( s ) );
ldapSStorage.add( cache.IDOf( s ), s );
clonesEl.remove( s );
}
if ( s instanceof Building ){
System.out.println( "Add in ldap " + s+ " " + cache.IDOf( s ) );
ldapSStorage.add( cache.IDOf( s ), s );
clonesEl.remove( s );
}
}
els.clear( );
els.addAll( clonesEl );
i = els.iterator( );
// Теперь запишем этажи
while( i.hasNext( ) ){
SanElement s = i.next( );
if ( s instanceof Floor ){
System.out.println( "Add in ldap " + s+ " " + cache.IDOf( s ) );
ldapSStorage.add( cache.IDOf( s ), s );
clonesEl.remove( s );
}
}
els.clear( );
els.addAll( clonesEl );
i = els.iterator( );
// Записываем все остальное
while( i.hasNext( ) ){
SanElement s = i.next( );
System.out.println( "Add in ldap " + s+ " " + cache.IDOf( s ) );
ldapSStorage.add( cache.IDOf( s ), s );
}
}
// Удаляет элеметны из кэша в правильном порядке
private void rmByID( ){
Iterator<SanElement> i = xSAN.getElements( SanElement.class ).iterator( );
System.out.println( "-->>> Удаляем все элементы по ID" );
// Удалим структуру N, структуру A и здания
while( i.hasNext( ) ){
SanElement s = i.next( );
if ( cache.isLoaded( s ) && !( s instanceof Floor ) && !( s instanceof Building ) ){
System.out.println( "Delete " + s );
try {
cache.removeByID( cache.IDOf( s ) );
}catch ( UnknownIDException ex ){}
}
}
// Удалим все этажи
i = xSAN.getElements( SanElement.class ).iterator( );
while( i.hasNext( ) ){
SanElement s = i.next( );
if ( cache.isLoaded( s ) && s instanceof Floor ){
System.out.println( "Delete " + s );
try {
cache.removeByID( cache.IDOf( s ) );
}catch ( UnknownIDException ex ){}
}
}
// Удалим все остальное ( осталось только здания )
i = xSAN.getElements( SanElement.class ).iterator( );
while( i.hasNext( ) ){
SanElement s = i.next( );
if ( cache.isLoaded( s ) ){
System.out.println( "Delete " + s );
try {
cache.removeByID( cache.IDOf( s ) );
}catch ( UnknownIDException ex ){}
}
}
}
private void checkLoad( ){
System.out.println( "--->> Проверяем загруженность элементов" );
Iterator<SanElement> i = xSAN.getElements( SanElement.class ).iterator( );
while( i.hasNext( ) ){
SanElement s = i.next( );
if ( cache.isLoaded( s ) )
System.out.println( "isLoaded " + s + ": OK" );
else System.out.println( "isLoaded "+ s + ": BAD" );
if ( cache.IDOf( s ) != -1 )
System.out.println( "IDOF "+ s + ": OK" );
else System.out.println( "IDOF "+ s + ": BAD" );
try {
int id = cache.IDOf( s );
if ( cache.isLoadedByID( id ) )
System.out.println( "isLoadedByID "+s+ ": OK; ID = " + id );
else System.out.println( "idLoadedByID "+s+ ": BAD;" + " ID = " + id );
}catch ( UnknownIDException ex ){
System.out.println( "idLoadedByID "+s+ ": BAD" );
}
}
}
private void rm( ){
// Удалим элементы из cache
System.out.println( "-->>> Удаляем элементы, передавая сам элемент" );
Iterator<SanElement> i = xSAN.getElements( SanElement.class ).iterator( );
// Удалим структуру N, структуру A и здания
while( i.hasNext( ) ){
SanElement s = i.next( );
if ( cache.isLoaded( s ) && !( s instanceof Floor ) && !( s instanceof Building ) ){
System.out.println( "Delete " + s );
cache.remove( s );
}
}
// Удалим все этажи
i = xSAN.getElements( SanElement.class ).iterator( );
while( i.hasNext( ) ){
SanElement s = i.next( );
if ( cache.isLoaded( s ) && s instanceof Floor ){
System.out.println( "Delete " + s );
cache.remove( s );
}
}
// Удалим все остальное ( осталось только здания )
i = xSAN.getElements( SanElement.class ).iterator( );
while( i.hasNext( ) ){
SanElement s = i.next( );
if ( cache.isLoaded( s ) ){
System.out.println( "Delete " + s );
cache.remove( s );
}
}
}
}
1 package ru.petrsu.nest.ldap;
import ru.petrsu.nest.san.SanElement;
import java.util.*;
import java.io.*;
import java.lang.*;
import javax.naming.*;
10 class Driver_Ldap {
12 public static void main( String[] args ) {
try
{
Set<Integer> set = new HashSet<Integer>( );
List list = new ArrayList<String>( );
SanElement sanElement; // = new SanElement( );
// Ldap.setContextParams( "cn=Nest-Manager, dc=cs, dc=karelia, dc=ru",
// "test", "ldap://213.59.11.30:389",
// "ou=nest, dc=cs, dc=karelia, dc=ru" );
Ldap.setContextParams( "cn=Nest-Manager, dc=cs, dc=karelia, dc=ru",
"LaiphiS8", "ldap://10.0.51.17:389",
"ou=nest, dc=cs, dc=karelia, dc=ru" );
Ldap.initContext( );
// if ( Ldap.doesRecordExist( 10041 ) )
// System.out.println( "YES" );
// else
// System.out.println( "DOESN'T EXIST" );
// System.out.println( "\n\n\t\t"+Ldap.getRootDeviceID( )+"\n\n" );
// writeLdap.writeHeadDNs( );
List<Integer> freeID = new ArrayList<Integer>( );
// for ( int i=100; i<122; i++ )
// freeID.add( i );
//
// LdapCacheParams.setCacheParams( freeID, 567 );
//
LdapCacheParams.setCacheParams( freeID, 250 );
//
// freeID = LdapCacheParams.getFreeIDs( );
// if ( freeID.size( )==0 )
// System.out.println( "freeID pustoi" );
// for ( int i=0; i<freeID.size( ); i++ )
// System.out.println( ( i+1 )+" ) freeID="+freeID.get( i ) );
// System.out.println( "cacheSize="+LdapCacheParams.getSize( ) );
// String DN = "cn=Devices, cn=NetUnit, ou=nest, dc=cs, dc=karelia, dc=ru";
// String DN2= "cn=122, cn=111, cn=SpaceUnit, ou=nest, dc=cs, dc=karelia, dc=ru";
// LE.EraseLdapRecord( 28 );
// set = Ldap.ScopeSearch( "ou=nest, dc=cs, dc=karelia, dc=ru", "( cn=* )", ( -1 ) );
// set = ldap.ScopeSearch( 191, "( cn=* )", ( -1 ) );
// Iterator<Integer> iter = set.iterator( );
// for ( int i=0; i<set.size( ); i++ )
// System.out.print( ( i+1 )+"="+iter.next( )+" ; " );
// System.out.println( "" );
//
//sanElement = ldap.getElement( 193 );
}catch( Exception e ){
e.printStackTrace( );
}
}
}
1 package ru.petrsu.nest.ldap;
import ru.petrsu.nest.san.*;
import java.util.*;
import java.io.*;
import java.lang.*;
import ru.petrsu.nest.ldap.Cache;
import ru.petrsu.nest.ldap.*;
import javax.naming.NamingException;
12 class Driver_writeLdap {
14 public static void main( String[] args ) {
// Ldap.setContextParams( "cn=Nest-Manager, dc=cs, dc=karelia, dc=ru",
// "test", "ldap://213.59.11.30:389",
// "ou=nest, dc=cs, dc=karelia, dc=ru" );
Ldap.setContextParams( "cn=Nest-Manager, dc=cs, dc=karelia, dc=ru",
"LaiphiS8", "ldap://10.0.51.17:389",
"ou=nest, dc=cs, dc=karelia, dc=ru" );
try {
Ldap.initContext( );
} catch ( NamingException e ){}
writeLdap write = new writeLdap( );
SAN testSan = generateSAN( );
List<Integer> aIn = new ArrayList<Integer>( );
try {
Cache.create( 1000, aIn );
Device rootDev = testSan.getRootDevice( );
int id_rootDev = Cache.add( rootDev );
int[] id_bs = new int[1];
Building[] bs = testSan.getBuildings( );
id_bs[0] = Cache.add( bs[0] );
int[] id_fl = new int[1];
Floor[] fl = bs[0].getFloors( );
id_fl[0] = Cache.add( fl[0] );
int[] id_rs = new int[1];
Room[] rs = fl[0].getRooms( );
id_rs[0] = Cache.add( rs[0] );
CompositeOU rootOU = testSan.getRootOU( );
int id_rootOU = Cache.add( rootOU );
//AbstractOU[] simOUs = rootOU.getOUs( );
//SimpleOU[] simOUs = ( SimpleOU[] )rootOU.getOUs( );
//int[] id_simOU = new int[1];
//id_simOU[0] = Cache.add( simOUs[0] );
LinkInterface[] LIns = rootDev.getLinkInterfaces( );
int[] id_li = new int[2];
id_li[0] = Cache.add( LIns[0] );
id_li[1] = Cache.add( LIns[1] );
NetworkInterface[] NIns = LIns[0].getNetworkInterfaces( );
int[] id_ni = new int[1];
id_ni[0] = Cache.add( NIns[0] );
IPNetwork net = ( IPNetwork )NIns[0].getNetwork( );
int id_net = Cache.add( net );
SimpleOU ou2 = new SimpleOU( );
ou2.setName( "SUName" );
ou2.setDescription( "SUDesc" );
//ou2.addDevice( rootDev );
ou2.addRoom( rs[0] );
int id_simOU = Cache.add( ou2 );
rootOU.addOU( ou2 );
rs[0].addOU( ou2 );
rootDev.setOU( ou2 );
/*
IPNetwork net = new IPNetwork( new byte[] {
( byte )0x00, ( byte )0x80, ( byte )0xAD,
( byte )0x7C, ( byte )0x00, ( byte )0x32},
new byte[] {
( byte )0x00, ( byte )0x80, ( byte )0xAD,
( byte )0x7C, ( byte )0x00, ( byte )0x32} );
int id_net = Cache.add( net );
*/
write.writeHeadDNs( );
//write.writeElement( id_net );
write.writeElement( id_bs[0] ); // write Building
write.writeElement( id_fl[0] ); // write Floor
write.writeElement( id_rs[0] ); //write room
write.writeElement( id_rootOU ); //write CompositeOU
write.writeElement( id_simOU ); //write SimpleOU
write.writeElement( id_rootDev ); // write Device
write.writeElement( id_li[0] );
write.writeElement( id_net ); // write Network
write.writeElement( id_ni[0] ); // write networkInterface
//write.writeElement( id_li[1] );
//write.writeIPNetwork( net, id_net );
//write.writeBuilding( bs[0], id_bs[0] );
//write.writeFloor( fl[0], id_fl[0] );
//write.writeRoom( rs[0], id_rs[0] );
//write.writeCompositeOU( rootOU, id_rootOU );
//write.writeSimpleOU( sOU, id_sOU );
//write.writeDevice( rootDev, id_rootDev );
//write.writeLinkInterface( LIns[0], id_li[0] );
//write.writeLinkInterface( LIns[1], id_li[1] );
} catch( Exception e ) {
e.printStackTrace( );
}
}
120 private static SAN generateSAN( ) {
SAN san = new SAN( );
Device d1 = new Device( );
d1.setDescription( "Test device 1" );
d1.setName( "DevName1" );
EthernetInterface i11 = new EthernetInterface( d1 );
EthernetInterface i12 = new EthernetInterface( d1 );
i11.setMACAddress( new byte[] {
( byte )0x00, ( byte )0x00, ( byte )0x7C,
( byte )0x7C, ( byte )0x00, ( byte )0x32} );
i11.setState( LinkInterface.State.DOWN );
i11.setDevice( d1 );
i11.setMode( LinkInterface.Mode.DUPLEX );
/*
NetworkInterface ni = new NetworkInterface( );
ni.setName( "niName" );
ni.setDescription( "niDesc" );
ni.setLinkInterface( i11 );
ni.setInetAddress( new byte[] {
( byte )0x00, ( byte )0x80, ( byte )0xAD,
( byte )0x7C} );
*/
IPv4Interface ni = new IPv4Interface( );
ni.setName( "niName" );
ni.setDescription( "niDesc" );
ni.setLinkInterface( i11 );
ni.setInetAddress( new byte[] {
( byte )0x00, ( byte )0x80, ( byte )0xAD,
( byte )0x7C} );
IPNetwork net = new IPNetwork( );
net.setName( "netName" );
net.setDescription( "netDesc" );
net.addNetworkInterface( ni );
net.setAddress( new byte[] {
( byte )0x00, ( byte )0x00, ( byte )0x7C,
( byte )0x7C} );
net.setMask( new byte[] {
( byte )0x00, ( byte )0x00, ( byte )0x7C,
( byte )0x7C} );
ni.setNetwork( net );
Building b1 = new Building( );
b1.setDescription( "Miniluv" );
b1.setName( "BName" );
b1.setAddress( "Lenina" );
Floor f1 = new Floor( );
f1.setNumber( 1 );
b1.addFloor( f1 );
Room r1 = new Room( );
r1.setNumber( "217" );
r1.setDescription( "Rooommm" );
//r1.setName( "RooommmNaaaammme" );
r1.addDevice( d1 );
f1.addRoom( r1 );
d1.setRoom( r1 );
CompositeOU ou1 = new CompositeOU( );
ou1.setName( "OUName" );
ou1.setDescription( "OUDesc" );
SimpleOU ou2 = new SimpleOU( );
ou2.setName( "SUName" );
ou2.setDescription( "SUDesc" );
ou2.addDevice( d1 );
ou2.addRoom( r1 );
ou1.addOU( ou2 );
r1.addOU( ou2 );
d1.setOU( ou2 );
san.setRootDevice( d1 );
san.addBuilding( b1 );
san.setRootOU( ou1 );
return san;
}
}
package ru.petrsu.nest.ldap;
import ru.petrsu.nest.san.PropertyValueAddEvent;
import ru.petrsu.nest.san.PropertyValueRemoveEvent;
import java.lang.*;
import java.util.*;
import java.io.*;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import java.beans.beancontext.BeanContextChild;
13 class ElementListener<K, T>
//implements PropertyChangeListener
{
private K ID;
private StorageCache<K, T> cache;
private Class<T> rootType;
/*Constructor*/
ElementListener( StorageCache<K, T> c, K id, Class<T> t ) {
cache = c;
ID = id;
rootType = t;
}
/**Reaction for adding item to indexative property.
* Uses if property is other system element ( should have self listener ). */
final private void
addReferencedProperty( PropertyValueAddEvent event )
{
//BeanContextChild value = ( BeanContextChild ) event.getAddedValue( );
Object value = event.getAddedValue( );
K id = cache.add( rootType.cast( value ) );
value.addPropertyChangeListener(
new ElementListener<K, T>( cache, id, rootType ) );
}
/**Reaction for removing property from indexative property.
* Uses if property is other system element. */
final private void
removeReferencedProperty( PropertyValueRemoveEvent event )
{
cache.remove( rootType.cast( event.getRemovedValue( ) ) );
}
/**Reaction for changing property which id not reference to other
* system element. */
final private void modifySimpleProperty( PropertyChangeEvent event )
{
// storage modify
}
/**Reaction for changing property. */
public void propertyChange( PropertyChangeEvent event )
{
String name;
// add new property
if( event instanceof PropertyValueAddEvent ) {
name = event.getPropertyName( );
if( name == "linkInterfaces" ) {
LinkInterface value = ( LinkInterface )
( ( PropertyValueAddEvent )event ).getAddedValue( );
K id = cache.add( value );
value.addPropertyChangeListener(
new LinkInterfaceListener<K>( cache, id ) );
}
else throw new UnknownPropertyException( name );
} else
// remove property
if( event instanceof PropertyValueRemoveEvent ) {
name = event.getPropertyName( );
if( name == "linkInterfaces" ) {
LinkInterface value = ( LinkInterface )
( ( PropertyValueRemoveEvent )event ).getRemovedValue( );
//value.removePropertyChangeListener( name, null );
cache.remove( value );
}
else throw new UnknownPropertyException( name );
} else
// change property
if( event instanceof PropertyChangeEvent ) {
name = event.getPropertyName( );
//SanElement newValue = SanElement.class.cast( event.getNewValue( ) );
//SanElement oldValue = SanElement.class.cast( event.getOldValue( ) );
if( name == "room" ) {
// modify storage
System.out.println( "Device: modifing property: "+name );
}
if( name == "OU" ) {
// modify storage
System.out.println( "Device: modifing property: "+name );
}
}
}
}
package ru.petrsu.nest.ldap;
import java.util.Set;
import java.util.List;
import java.util.HashSet;
import java.util.ArrayList;
import java.util.Hashtable;
import java.lang.String ;
import java.lang.Integer ;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.NamingEnumeration;
import javax.naming.NameNotFoundException;
import javax.naming.NameAlreadyBoundException;
import javax.naming.directory.Attribute;
import javax.naming.directory.DirContext;
//import javax.naming.ldap.InitialLdapContext;
import javax.naming.directory.Attributes;
import javax.naming.directory.SearchResult;
import javax.naming.directory.SearchControls;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.NoSuchAttributeException;
/**
* Class Ldap - class for communication with LDAP server.
*
*
* @author Artemev Dmitry;
*/
38 class Ldap {
40 private DirContext context; // context of the server
42 private String rootPw; // password for root access
43 private String ldapUrl; // url of ldap server
44 private String rootDN; // DN for root access
46 String mainHeadDN; // DN of top for all element
47 String headDeviceDN; // list of top DN elements
48 String headNetworkDN;
49 String headLIDN;
50 String headNIDN;
51 String headAbstractOUDN;
52 String headNetUnitDN;
53 String headSpaceUnitDN;
54 String headPersonDN;
55 String headHistoryDN;
/**
*sets rootPw, rootDN, ldapUrl, mainHeadDN.
*/
60 Ldap( String rootUser, String pass, String url, String suffix ) {
rootDN = rootUser;
rootPw = pass;
ldapUrl = url;
mainHeadDN = suffix;
setDNs( );
}
/**
*set's top DN elements from mainHeadDN.
*/
74 private void setDNs( ) {
headNetUnitDN = "cn=NetUnit, " + mainHeadDN;
headDeviceDN = "cn=Devices, cn=NetUnit, " + mainHeadDN;
headNetworkDN = "cn=Networks, cn=NetUnit, " + mainHeadDN;
headLIDN = "cn=Link Interfaces, cn=NetUnit, " + mainHeadDN;
headNIDN = "cn=Network Interfaces, cn=NetUnit, " + mainHeadDN;
headAbstractOUDN = "cn=AbstractOU, " + mainHeadDN;
headPersonDN = "cn=People, cn=AbstractOU, " + mainHeadDN;
headSpaceUnitDN = "cn=SpaceUnit, " + mainHeadDN;
headHistoryDN = "cn=History, " + mainHeadDN;
}
/**
*connect's to LDAP & return's servers context.
*/
92 void initContext( )
throws StorageBadConnectionException {
try {
Hashtable<String, String> env = new Hashtable<String, String>( );
env.put( Context.SECURITY_PRINCIPAL, rootDN );
env.put( Context.SECURITY_CREDENTIALS, rootPw );
env.put( Context.PROVIDER_URL, ldapUrl );
// env.put( Context.SECURITY_PROTOCOL, "ssl" );
DirContext initial = new InitialDirContext( env );
// InitialLdapContext initial = new InitialLdapContext ( env, null );
context =( DirContext ) initial.lookup( ldapUrl );
}catch ( NamingException e ){
e.printStackTrace( );
throw new StorageBadConnectionException( "can't connect to server "+ldapUrl );
}
/*
Hashtable<String, String> env =
new Hashtable<String, String> ( );
env.put ( Context.SECURITY_PRINCIPAL, suffix );
env.put ( Context.SECURITY_CREDENTIALS, password );
env.put( Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory" );
env.put( Context.PROVIDER_URL, ldapUrl );
// env.put( Context.SECURITY_PROTOCOL, "ssl" );
LdapContext initial = new InitialLdapContext( env, null );
StartTlsResponse tls =
( StartTlsResponse )initial.extendedOperation
( new StartTlsRequest( ) );
tls.negotiate( );
LdapContext context =
( LdapContext ) initial.lookup ( ldapUrl );
*/
// return context;
}
/**
* Call's the same function with default head point.
*/
139 boolean isRecord( int ID ) {
return isRecord( ID, mainHeadDN );
}
/**
* Return's true if LDAP contains the record with ID.
*/
146 boolean isRecord( int ID, String someHeadDN ) {
boolean bool = false;
try{
String[] attrNames = {""}; // function will search
SearchControls ctls = new SearchControls( ); // only if record
ctls.setReturningAttributes( attrNames ); // of element
ctls.setSearchScope( SearchControls.SUBTREE_SCOPE );
NamingEnumeration element =
context.search( someHeadDN, ( "cn="+ID ), ctls );
if ( element.hasMore( ) )
bool = true;
}catch ( NamingException e ){}
return bool;
}
164 boolean isRecord( String DN ) {
boolean bool=false;
try {
context.lookup( DN );
bool = true;
}catch( NamingException e ) {
}
return bool;
}
/**
* Call's standart function to create LDAp record.
*/
178 void createLdapRecord( String name, Attributes attrs )
throws NameAlreadyBoundException {
try {
if ( context==null ) initContext( );
context.createSubcontext( name, attrs );
}catch ( NameAlreadyBoundException e ) {
throw new NameAlreadyBoundException( );
}
catch ( Exception e ) {
System.out.println( "createLdapRecord ERROR" );
System.out.println( "context="+context );
e.printStackTrace( );
}
}
/**
* Call's standart function that destroy's LDAP record.
*/
198 void destroyLdapRecord( String name ) {
try {
context.destroySubcontext( name );
}catch ( NameNotFoundException e ) {
}
catch ( NamingException e ) {
System.out.println( "can't destroy record:"+name );
}
}
/**
* Closes connection to server.
*/
213 void closeContext( )
throws StorageBadDisconnectionException {
try{
context.close( );
}catch ( NamingException e ) {
throw new StorageBadDisconnectionException( "can't close context." );
}
}
/**
*get's DN from ID and call's another ScopeSearch that works with DN.
*return's Set of Id's, with suitable filter, in some scope.
*/
Set<Integer> scopeSearch( int ID, String filter, int scope )
throws UnknownIDException {
return scopeSearch( IDtoDN( ID, mainHeadDN ), filter, scope );
}
/**
*return's Set of Id'd, gets them by DN, filter & scope
*/
Set<Integer> scopeSearch( String DN, String filter, int scope ) {
int cnValue; // value of cn attribute, actialy - ID for cache
Set<Integer> SetOfID = new HashSet<Integer>( );
try {
String[] attrNames = null; // setting any attribute
SearchControls ctls = new SearchControls( );
ctls.setReturningAttributes( attrNames ); //return all attrs
if ( scope == ( -1 ) )
ctls.setSearchScope( SearchControls.SUBTREE_SCOPE );
else
ctls.setSearchScope( scope ); // seting search scope
try { // testing if the context haven't been got.
if ( context == null ) initContext ( );
}catch ( Exception e ) {
System.out.println( "Ldap.java function scopeSearch: " );
System.out.println( "\t\t couldn't get context" );
}
// geting all suitable notes.
NamingEnumeration answer = context.search( DN, filter, ctls );
while ( answer.hasMore( ) ) {
SearchResult searchRes = ( SearchResult )answer.next( );
Attributes allAttrs = searchRes.getAttributes( );
Attribute attrCn = allAttrs.get( "cn" );
if ( attrCn != null ) {
NamingEnumeration cn = attrCn.getAll( );
try {
cnValue = Integer.parseInt( ( cn.next( ) ).toString( ) );
SetOfID.add( cnValue );
}catch( NumberFormatException e ) { }
}
}
}catch ( NamingException e ) {
System.out.println( "Ldap.java function scopeSearch: wrong ID" );
}
return SetOfID;
}
/**
* Return's the list of DN's that are on one level lower.
*/
List<String> OneLevelSearch( String DN ) {
List<String> list = new ArrayList<String>( );
SearchControls ctls = new SearchControls( );
ctls.setReturningAttributes( null );
ctls.setSearchScope( SearchControls.ONELEVEL_SCOPE );
try {
NamingEnumeration<SearchResult> answer =
context.search( DN, "cn=*", ctls );
while ( answer.hasMore( ) )
list.add( answer.next( ).getName( )+", "+DN );
}catch( NamingException e ) {
}
return list;
}
/**
*get's element by knowing it's ID.Return's SanElement.
*/
Attributes getAttributes( int ID )
throws UnknownIDException {
String DN = IDtoDN( ID, mainHeadDN );
return getAttributes( DN );
}
Attributes getAttributes( String DN ) {
Attributes attrs=null;
try {
attrs = context.getAttributes( DN );
}catch( NamingException e ) {
System.out.println( "Class Ldap: Can't get attributes." );
}
return attrs;
}
/**
* Return's values of specific attributes.
*/
Attributes getAttributes( String DN, String[] attrNames ) {
Attributes attrs = null;
try {
attrs = context.getAttributes( DN, attrNames );
}catch( NamingException e ) {
}
return attrs;
}
/**
* modify's sertan attributes of sertan element.
*/
void modifyAttributes( String DN, int modType, Attributes attrs ) {
try {
context.modifyAttributes( DN, modType, attrs );
}catch ( NoSuchAttributeException e ) {
}
catch( NamingException e ) {
}
}
/*
*get's DN from ID. For better search could change starting element.
*/
String IDtoDN( int ID )
throws UnknownIDException {
return IDtoDN( ID, mainHeadDN );
}
private String IDtoDN( int ID, String someHeadDN )
throws UnknownIDException {
String filter = "( cn="+ ID +" )"; // unique filter
String dn = null;
try {
if ( context == null ) initContext ( );
}catch ( Exception e ) {
System.out.println
( "Ldap.java function IDtoDN: problem in getting context" );
}
try {
String[] attrNames = {"cn"}; // function will return
SearchControls ctls = new SearchControls( ); // only cn record
ctls.setReturningAttributes( attrNames ); // of element
ctls.setSearchScope( SearchControls.SUBTREE_SCOPE );
NamingEnumeration element =
context.search( someHeadDN, filter, ctls );
if ( element.hasMore( ) ) {
SearchResult sr = ( SearchResult )element.next( );
dn = sr.getName( ) + ", " + someHeadDN;
}
}catch ( NamingException e ) {
System.out.println( "function IDtoDN: problem in getting dn" );
}
if ( dn==null ) throw new UnknownIDException( ""+ID );
return dn;
}
/**
*return's ID by it's DN.
*/
int DNtoID( String DN ) {
int ID = -1;
try {
NamingEnumeration NEID =
( ( ( context.getAttributes( DN ) ) ).get( "cn" ) ).getAll( );
ID = Integer.parseInt( ( NEID.next( ) ).toString( ) );
}catch ( NamingException e ) {
System.out.println( "Ldap.java. function DNtoID" );
}
return ID;
}
/**
* Write's root device ID in headDeviceDN.
*/
void setRootDeviceID( int ID ) {
Attributes attrs = new BasicAttributes( );
attrs.put( "rootDevice", ""+ID );
// try {
modifyAttributes( headDeviceDN, DirContext.REPLACE_ATTRIBUTE, attrs );
// }catch ( NamingException e ){
// throw new LdapException ( e );
// }
}
/**
* Returns ID of the root device or -1 if it is not set.
*/
int getRootDeviceID( ) {
int ID = -1;
try {
String[] str = {"rootDevice"};
String IDstr = null;
Attributes attrs = getAttributes( headDeviceDN, str );
IDstr = attrs.get( "rootDevice" ).getAll( ).next( ).toString( );
ID = Integer.parseInt( IDstr );
}catch ( NullPointerException e ) {}
catch ( NamingException e ) {
}
return ID;
}
}
package ru.petrsu.nest.ldap;
import ru.petrsu.nest.ldap.Cache;
import java.util.List;
import java.util.ArrayList;
import java.lang.String;
import java.lang.Integer;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.NamingEnumeration;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.InitialDirContext;
/////////////////////////////////////////////////////
/**
* Class LdapModifyer - class for editing LDAP elements server.
*
* @author Artemev Dmitry;
*/
31 class LdapCacheParams {
33 static final String CacheParams = "cn=CacheParams, "+Ldap.headHistoryDN;
36 static int getSize( ) {
String[] lastID = {"lastID"};
int answer = 0;
try {;
Attributes attrs = Ldap.getAttributes( CacheParams, lastID );
NamingEnumeration NE = attrs.get( "lastID" ).getAll( );
answer = Integer.parseInt( NE.next( ).toString( ) );
answer++;
} catch ( Exception e ) {}
return answer;
}
static List<Integer> getFreeIDs( ) {
List<Integer> list = new ArrayList<Integer>( );
String[] freeID = {"freeID"};
try {
Attributes attrs = Ldap.getAttributes( CacheParams, freeID );
NamingEnumeration NE = attrs.get( "freeID" ).getAll( );
while ( NE.hasMore( ) )
list.add( Integer.parseInt( NE.next( ).toString( ) ) );
}catch ( Exception e ){}
return list;
}
static void setCacheParams( List<Integer> freeID, int lastID ) {
try {
Ldap.destroyLdapRecord( CacheParams );
}catch( LdapException e ){}
Attributes attrs = new BasicAttributes( );
try {
attrs.put( "cn", "CacheParams" );
if ( freeID.size( )!=0 ){
Attribute free = new BasicAttribute( "freeID" );
for ( int i=0; i<freeID.size( ); i++ )
free.add( ""+freeID.get( i ) );
attrs.put( free );
}
attrs.put( "lastID", ""+lastID );
attrs.put( "objectClass", "nestHistory" );
Ldap.createLdapRecord( CacheParams, attrs );
}catch ( Exception e ) {}
}
1 package ru.petrsu.nest.ldap;
import ru.petrsu.nest.san.SanElement;
import java.util.List;
import java.util.ArrayList;
import java.lang.String;
import java.lang.Integer;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.NamingEnumeration;
import javax.naming.NameNotFoundException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.BasicAttributes;
/////////////////////////////////////////////////////
/**
* Class LdapEraser - class for removing elements from LDAP server.
*
* @author Artemev Dmitry;
*/
31 class LdapEraser {
///////////////////////////////////
34 final String[] DNattrs = {"deviceDN", "nifDN", "roomDN", "ouDN", "lifDN",
"networkDN", "personDN", "link"};
//////////////////////////////////
38 Ldap ldap;
/*Constructor*/
40 LdapEraser( Ldap l ) {
ldap = l;
}
/**
* Erases element in LDAP by it's ID
*/
46 void eraseLdapRecord( int ID )
throws UnknownIDException {
String DN = ldap.IDtoDN( ID );
eraseLdapRecord( DN );
}
/**
* Erases element in LDAP by it's DN
*/
56 void eraseLdapRecord( String DN ) {
try {
List<String> list = ldap.OneLevelSearch( DN );
if ( list.size( )==0 ) {
Attributes attrs= ldap.getAttributes( DN, DNattrs );
if ( attrs!=null ) {
NamingEnumeration<? extends Attribute> NE = attrs.getAll( );
NamingEnumeration NEattr;
List<NamingEnumeration> NEList = new ArrayList<NamingEnumeration>( );
String DNLink = DNLinkOfElement( DN );
Attributes basic = new BasicAttributes( DNLink, DN );
while ( NE.hasMore( ) ) {
NEattr=NE.next( ).getAll( );
while ( NEattr.hasMore( ) )
ldap.modifyAttributes( NEattr.next( ).toString( ),
DirContext.REMOVE_ATTRIBUTE,
basic );
}
if ( DNLink.equals( "lifDN" ) ) {
Attribute attr = ( attrs.get( "link" ) );
if ( attr!=null ){
NEattr=attr.getAll( );
basic = new BasicAttributes( "link", DN );
ldap.modifyAttributes( NEattr.next( ).toString( ),
DirContext.REMOVE_ATTRIBUTE,
basic );
}
}
}
}else {
for( int i=0; i<list.size( ); i++ )
eraseLdapRecord( list.get( i ) );
}
ldap.destroyLdapRecord( DN );
}catch ( Exception e ) {
System.err.println( "problem erasing element: "+DN );
}
}
/**
* Return's link erase attributes in other elements, that contain
* path to the element that is been erased.
*/
105 private String DNLinkOfElement( String DN ) {
String[] objClass = {"objectClass"};
String DNlink = null;
try {
Attributes attrs = ldap.getAttributes( DN, objClass );
NamingEnumeration NE = attrs.get( "objectClass" ).getAll( );
while ( NE.hasMore( ) ) {
DNlink = NE.next( ).toString( );
if ( DNlink.equals( "nestLinkInterface" )|
( DNlink.equals( "nestEthernetInterface" ) ) )
DNlink="lifDN";
else if ( ( DNlink.equals( "nestIPv4Interface" ) )|
( DNlink.equals( "nestNetworkInterface" ) ) )
DNlink="nifDN";
else if ( DNlink.equals( "nestRoom" ) )
DNlink="roomDN";
else if ( DNlink.equals( "nestSimpleOU" ) )
DNlink="ouDN";
else if ( DNlink.equals( "nestDevice" ) )
DNlink="deviceDN";
else if ( DNlink.equals( "nestNetwork" ) )
DNlink="networkDN";
else if ( DNlink.equals( "nestPerson" ) )
DNlink="personDN";
}
}catch ( NamingException e ) {
System.err.println( "Erase: can't identify element"+DN );
}
return DNlink;
}
}
package ru.petrsu.nest.ldap;
//import ru.petrsu.nest.san.LazyReference;
import ru.petrsu.nest.san.SanElement;
import java.lang.*;
import java.util.*;
import java.io.*;
/**
* @author Nuikin Alexander
*/
13 class LdapSanReference<U>
extends CacheReference<Integer, U> {
/*Constructor*/
public LdapSanReference
( StorageCache<Integer, ? super U> c, Integer id, Class<U> t ) {
super( c, id, t );
}
}
package ru.petrsu.nest.ldap;
import ru.petrsu.nest.san.NetworkInterface;
import ru.petrsu.nest.san.LinkInterface;
import ru.petrsu.nest.san.Building;
import ru.petrsu.nest.san.Room;
import ru.petrsu.nest.san.Floor;
import ru.petrsu.nest.san.SimpleOU;
import ru.petrsu.nest.san.CompositeOU;
import ru.petrsu.nest.san.Device;
import ru.petrsu.nest.san.Network;
import ru.petrsu.nest.san.IPNetwork;
import ru.petrsu.nest.san.SanElement;
import ru.petrsu.nest.san.EthernetInterface;
import ru.petrsu.nest.san.IPv4Interface;
import ru.petrsu.nest.san.References;
import ru.petrsu.nest.san.AbstractOU;
import java.lang.String;
import java.lang.Integer;
import java.util.Set;
import java.util.HashSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
/**LDAP storage of SAN-elements.
* Не контроллирует уникальность ИД.
* @author Alexander Nuikin
* @author Dmitry Artemev*/
32 class LdapSanStorage
33 extends SlaveStorage<Integer, SanElement> implements SanStorage<Integer> {
Ldap ldap;
// LdapEraser eraser;
LdapWriter writer;
SanElementCreator creator;
MasterCache<Integer, SanElement> cache;
/*===================================================================*/
/*---------------------- LDAP specific methods ----------------------*/
/*===================================================================*/
/*Constructor*/
LdapSanStorage( MasterCache<Integer, SanElement> c,
String rootUser, String passwd, String url, String suffix )
throws StorageBadConnectionException {
super( c );
ldap = new Ldap( rootUser, passwd, url, suffix );
// eraser = new LdapEraser( ldap );
writer = new LdapWriter( ldap, c );
creator= new SanElementCreator( ldap, c );
cache =c;
ldap.initContext( );
if ( !ldap.isRecord( ldap.headHistoryDN ) )
writer.writeHeadDNs( );
}
/**LDAP scope search.*/
Set<Integer> scopeSearch( Integer rootID, String filter, int scope )
throws UnknownIDException {
return ldap.scopeSearch( rootID, filter, scope );
}
/*===================================================================*/
/*------------- Implementation of SanStorage interface --------------*/
/*===================================================================*/
/**Set ID of root device in N-structure.*/
public void setRootDeviceID( Integer ID )
throws UnknownIDException {
if ( !ldap.isRecord( ID, ldap.headDeviceDN ) )
throw new UnknownIDException( ""+ID );
else
ldap.setRootDeviceID( ID );
}
/**Returns ID of root device in N-structure or -1 if its not sets.*/
public Integer getRootDeviceID( ) {
return ldap.getRootDeviceID( );
}
/**Returns ID of root organisation unit in A-structure
* or -1 if its not sets.*/
public Integer getRootOUID( ) {
int answer = -1;
Set<Integer> set = ldap.scopeSearch( ldap.headAbstractOUDN,
"objectClass=nestCompositeOU", 1 );
if ( set.size( )>0 ) {
Iterator<Integer> iter = set.iterator( );
answer = iter.next( );
}
return answer;
}
/*===================================================================*/
/*--------------- Implementation of Storage interface ---------------*/
/*===================================================================*/
/**Returns true if in storage exist element with given ID.*/
boolean isID( Integer ID ) {
return ldap.isRecord( ID );
}
/**Returns all elements from storage.*/
<U extends SanElement> Set<Integer> getElements( Class<U> type ){
//Set<Integer> getElements( SanElement SE ){
Set<Integer> set = new HashSet<Integer>( );
if ( type == LinkInterface.class )
set = ldap.scopeSearch( ldap.headLIDN, "cn=*", 1 );
else if ( type == NetworkInterface.class )
set = ldap.scopeSearch( ldap.headNIDN, "cn=*", 1 );
else if ( type == Device.class )
set = ldap.scopeSearch( ldap.headDeviceDN, "cn=*", 1 );
else if ( type == Network.class )
set = ldap.scopeSearch( ldap.headNetworkDN, "cn=*", 1 );
else if ( type == Floor.class )
set = ldap.scopeSearch( ldap.headSpaceUnitDN,
"objectClass=nestFloor", 2 );
else if ( type == Room.class )
set = ldap.scopeSearch( ldap.headSpaceUnitDN,
"objectClass=nestRoom", -1 );
else if ( type == SimpleOU.class )
set = ldap.scopeSearch( ldap.headAbstractOUDN,
"objectClass=nestSimpleOU", 1 );
else if ( type == CompositeOU.class )
set = ldap.scopeSearch( ldap.headAbstractOUDN,
"objectClass=nestCompositeOU", 1 );
else if ( type == Building.class )
set = ldap.scopeSearch( ldap.headSpaceUnitDN, "cn=*", 1 );
else if ( type == SanElement.class )
set = ldap.scopeSearch( ldap.mainHeadDN, "cn=*", -1 );
return set;
}
Set<Integer> getAllID( ) {
return ldap.scopeSearch( ldap.mainHeadDN, "cn=*", -1 );
}
/**Returns element from storage by its ID.
* Cache references to other elements will not be set.*/
SanElement getNotReferenced( Integer ID )
throws UnknownIDException {
return creator.getEmptyElement( ID );
}
/**Set references to other elements for given element.*/
void setReferences( Integer ID, SanElement el )
throws UnknownIDException, NullPointerException,
IDNotRespectiveTypeException {
creator.getElement( ID, el );
// throw new IDNotRespectiveTypeException
// ( el.getClass( ), Device.class );
}
/**Add new element to storage with given ID.*/
void add( Integer ID, SanElement el )
throws NullPointerException {
writer.writeElement( ID, el );
}
/**Removes element from storage by given ID.*/
void remove( Integer ID )
throws UnknownIDException {
String DN = ldap.IDtoDN( ID );
ldap.destroyLdapRecord( DN );
//eraser.eraseLdapRecord( ID );
}
/**Rewrites element by given ID*/
void modify( Integer ID )
throws UnknownIDException {
String DN = ldap.IDtoDN( ID );
ldap.destroyLdapRecord( DN );
writer.writeElement( ID, cache.get( ID ) );
}
/**Close connection with LDAP server.*/
void disconnect( )
throws StorageBadDisconnectionException {
ldap.closeContext( );
}
}
package ru.petrsu.nest.ldap;
import java.io.*;
import java.util.*;
import ru.petrsu.nest.san.SanManager;
import ru.petrsu.nest.san.SAN;
import ru.petrsu.nest.san.SanElement;
import ru.petrsu.nest.san.Device;
/**Manages SAN-elements storage system.
* @author Alexander Nuikin*/
11 public class LdapSanStorageManager extends SanStorageManager<Integer> {
/*================================================================*/
/*------------ Specific LdapSanStorageManger interface -----------*/
/*================================================================*/
LdapSanStorage lss;
/*Constructor*/
public LdapSanStorageManager
( String url, String rootUser, String passwd, String suffix )
throws StorageBadConnectionException {
cache = new MasterHoleyCache<SanElement>( );
lss = new LdapSanStorage( ( MasterCache<Integer, SanElement> )cache,
url, rootUser, passwd, suffix );
storage = lss;
cache.checkOut( );
cache.rootDeviceID = lss.getRootDeviceID( );
System.out.println( "Set cache root device: ["+cache.rootDeviceID+"]" );
// проверка на cache.rootDeviceID == -1 и выбор любого возм-го Device
// ...
try {
san.setRootDevice( Device.class.cast( cache.get( cache.rootDeviceID ) ) );
}catch( UnknownIDException e ){
throw new DebugException( );
}
}
/**LDAP scope search.*/
final public Collection<SanElement>
scopeSearch( Integer rootID, String filter, int scope )
throws UnknownIDException {
Collection<SanElement> setOfElements = new ArrayList<SanElement>( );
for( Integer id : lss.scopeSearch( rootID, filter, scope ) )
try {
setOfElements.add( cache.get( id ) );
}catch( UnknownIDException e ) {
System.out.println( e );
}
return setOfElements;
}
/**Retrieve the root element of the SAN structure.*/
final public SAN getSAN( ) {
Integer id = cache.add( san );
cache.rootID = id;
san.addPropertyChangeListener( new SANListener<Integer>( cache, lss, id ) );
return san;
}
/***/
final public void finalize( )
throws StorageBadDisconnectionException {
System.out.println( "Set storage root device: ["+cache.rootDeviceID+"]" );
try {
lss.setRootDeviceID( cache.rootDeviceID );
System.out.println( ": OK" );
}catch( UnknownIDException e ){
System.out.print( ": BAD: " );
System.out.println( e.toString( ) );
}
super.finalize( );
}
}
package ru.petrsu.nest.ldap;
import ru.petrsu.nest.san.NetworkInterface;
import ru.petrsu.nest.san.LinkInterface;
import ru.petrsu.nest.san.Building;
import ru.petrsu.nest.san.Room;
import ru.petrsu.nest.san.Floor;
import ru.petrsu.nest.san.SimpleOU;
import ru.petrsu.nest.san.CompositeOU;
import ru.petrsu.nest.san.Device;
import ru.petrsu.nest.san.Network;
import ru.petrsu.nest.san.IPNetwork;
import ru.petrsu.nest.san.SanElement;
import ru.petrsu.nest.san.EthernetInterface;
import ru.petrsu.nest.san.IPv4Interface;
import ru.petrsu.nest.san.References;
import ru.petrsu.nest.san.AbstractOU;
import ru.petrsu.nest.ldap.CacheReference;
import ru.petrsu.nest.ldap.Cache;
import java.io.IOException;
import java.util.Set;
import java.util.HashSet;
import java.util.Hashtable;
import java.lang.String;
import java.lang.Object;
import java.lang.Integer;
import java.lang.Exception;
import java.lang.StringBuffer;
import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.NamingEnumeration;
import javax.naming.NameAlreadyBoundException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.InitialDirContext;
/////////////////////////////////////////////////////
/**
* Class LdapWriter - class for writing to LDAP server.
*
* @author Artemev Dmitry;
*/
59 class LdapWriter {
61 Ldap ldap;
MasterCache<Integer, SanElement> cache;
LdapWriter( Ldap l, MasterCache<Integer, SanElement> c ) {
ldap = l;
cache =c;
}
////////////////////////////////////////////////////////////
void writeElement( int ID, SanElement SE )
throws NullPointerException {
if ( SE==null )
throw new NullPointerException( "Can't write to LDAP. ID="+ID );
else if ( SE instanceof LinkInterface )
writeLinkInterface( ( LinkInterface )SE, ID );
else if ( SE instanceof NetworkInterface )
writeNetworkInterface( ( NetworkInterface )SE, ID );
else if ( SE instanceof Device )
writeDevice( ( Device )SE, ID );
else if ( SE instanceof Room )
writeRoom( ( Room )SE, ID );
else if ( SE instanceof Floor )
writeFloor( ( Floor )SE, ID );
else if ( SE instanceof SimpleOU )
writeSimpleOU( ( SimpleOU )SE, ID );
else if ( SE instanceof IPNetwork )
writeIPNetwork( ( IPNetwork )SE, ID );
else if ( SE instanceof Building )
writeBuilding( ( Building )SE, ID );
else if ( SE instanceof CompositeOU )
writeCompositeOU( ( CompositeOU )SE, ID );
}
/**
*creates LI record in LDAP.
*/
void writeLinkInterface( LinkInterface LI, int ID )
throws ElementExistException {
try {
String name = LI.getName( );
String descr = LI.getDescription( );
String State = null;
String Mode = null;
StringBuilder DN = new StringBuilder( );
Attributes attrs = new BasicAttributes( );
attrs.put( "cn", ""+ID );
if ( name!=null && !name.trim( ).isEmpty( ) )
attrs.put( "name", name );
if ( descr!=null && !descr.trim( ).isEmpty( ) )
attrs.put( "description", descr );
Device device = LI.getDevice( );
if ( device!=null ) {
DN.append( "cn=" );
DN.append( cache.IDOf( device ) );
DN.append( ", " );
DN.append( ldap.headDeviceDN );
attrs.put( "deviceDN", DN.toString( ) );
}
LinkInterface link = LI.getLink( );
if ( link!=null ) {
DN.setLength( 0 );
DN.append( "cn=" );
DN.append( cache.IDOf( link ) );
DN.append( ", " );
DN.append( ldap.headLIDN );
attrs.put( "link", DN.toString( ) );
}
NetworkInterface[] NI = LI.getNetworkInterfaces( );
if ( NI.length!=0 ) {
Attribute nifDN = new BasicAttribute( "nifDN" );
for ( int i=0; i<NI.length; i++ ) {
DN.setLength( 0 );;
DN.append( "cn=" );
DN.append( cache.IDOf( NI[i] ) );
DN.append( ", " );
DN.append( ldap.headNIDN );
nifDN.add( DN.toString( ) );
}
if ( nifDN.size( )!=0 )
attrs.put( nifDN );
}
State = LI.getState( ).name( );
Mode = LI.getMode( ).name( );
if ( State!=null )
attrs.put( "state", State );
if ( Mode !=null )
attrs.put( "mode", Mode );
DN.setLength( 0 );
DN.append( "cn=" );
DN.append( ID );
DN.append( ", " );
DN.append( ldap.headLIDN );
if ( LI instanceof EthernetInterface ) {
StringBuilder MacString = new StringBuilder( 18 );
byte[] mac = ( ( EthernetInterface )LI ).getMACAddress( );
if ( mac!=null ) {
for ( int i=0; i<mac.length; i++ ) {
if ( i >0 )
MacString.append( ":" );
MacString.append( String.format( "%02X", mac[i] ) );
}
attrs.put( "macAddress", MacString.toString( ) );
}
attrs.put( "objectClass", "nestEthernetInterface" );
}
else attrs.put( "objectClass", "nestLinkInterface" );
ldap.createLdapRecord( DN.toString( ), attrs );
}catch ( NameAlreadyBoundException e ){
System.err.println( "element with ID= "+ID+" already exist." );
throw new ElementExistException( );
}
catch( NamingException e ) {
System.err.println( "writeEthernetInterface: ERROR." );
e.printStackTrace( );
}
}
/**
*creates NI record in LDAP.
*/
void writeNetworkInterface( NetworkInterface NI, int ID )
throws ElementExistException {
try {
String name = NI.getName( );
String descr = NI.getDescription( );
StringBuilder TCPIP = new StringBuilder( );
StringBuilder DN = new StringBuilder( );
int length;
Attributes attrs = new BasicAttributes( );
attrs.put( "cn", ""+ID );
if ( name!=null && !name.trim( ).isEmpty( ) )
attrs.put( "name", name );
if ( descr!=null && !descr.trim( ).isEmpty( ) )
attrs.put( "description", descr );
byte[] address = NI.getInetAddress( );
InetAddress ia =null;
try {
ia = InetAddress.getByAddress( address );
TCPIP.append( ia.getHostAddress( ) );
attrs.put( "ipHostNumber", TCPIP.toString( ) );
}catch ( UnknownHostException e ){}
// if ( length!=0 ) {
// for ( int i=0; i<length-1; i++ ) {
// TCPIP.append( address[i] );
// TCPIP.append( "." );
// }
// TCPIP.append( address[length-1] );
// length = TCPIP.length( );
// TCPIP = TCPIP.substring( 0, length-1 );
// }
Network network = NI.getNetwork( );
if ( network!=null ) {
DN.append( "cn=" );
DN.append( cache.IDOf( network ) );
DN.append( ", " );
DN.append( ldap.headNetworkDN );
attrs.put( "networkDN", DN.toString( ) );
}
DN.setLength( 0 );
LinkInterface LI = NI.getLinkInterface( );
if ( LI!=null ) {
DN.append( "cn=" );
DN.append( cache.IDOf( LI ) );
DN.append( ", " );
DN.append( ldap.headLIDN );
attrs.put( "lifDN", DN.toString( ) );
}
if ( NI instanceof IPv4Interface )
attrs.put( "objectClass", "nestIPv4Interface" );
else attrs.put( "objectClass", "nestNetworkInterface" );
DN.setLength( 0 );
DN.append( "cn=" );
DN.append( ID );
DN.append( ", " );
DN.append( ldap.headNIDN );
// System.out.println( "\n\n"+ attrs.toString( )+"\n\n" );
ldap.createLdapRecord( DN.toString( ), attrs );
}catch ( NameAlreadyBoundException e ){
System.err.println( "element with ID= "+ID+" already exist." );
throw new ElementExistException( );
}
catch( Exception e ) {
System.out.println( "writeNetworkInterface: ERROR." );
e.printStackTrace( );
}
}
/**
*creates Network record with ipNetmaskNumber and ipNetworkNUmber
* in LDAP.
*/
void writeIPNetwork( IPNetwork network, int ID )
throws ElementExistException {
try {
String name = network.getName( );
String descr = network.getDescription( );
StringBuilder DN = new StringBuilder( );
StringBuilder TCPIP = new StringBuilder( );
int length=0;
Attributes attrs = new BasicAttributes( );
attrs.put( "cn", ""+ID );
if ( name!=null && !name.trim( ).isEmpty( ) )
attrs.put( "name", name );
if ( descr!=null && !descr.trim( ).isEmpty( ) )
attrs.put( "description", descr );
byte[] mask = network.getMask( );
InetAddress ia = null;
try {
ia = InetAddress.getByAddress( mask );
TCPIP.append( ia.getHostAddress( ) );
attrs.put( "ipNetmaskNumber", TCPIP.toString( ) );
}catch ( UnknownHostException e ){}
byte[] address = network.getAddress( );
ia = null;
TCPIP.setLength( 0 );
try {
ia = InetAddress.getByAddress( address );
TCPIP.append( ia.getHostAddress( ) );
attrs.put( "ipNetworkNumber", TCPIP.toString( ) );
}catch ( UnknownHostException e ){}
Attribute nifDN = new BasicAttribute( "nifDN" );
NetworkInterface[] NI = network.getNetworkInterfaces( );
for ( int i=0; i<NI.length; i++ ) {
DN.setLength( 0 );
DN.append( "cn="+cache.IDOf( NI[i] ) );
DN.append( ", "+ldap.headNIDN );
nifDN.add( DN.toString( ) );
}
if ( nifDN.size( )!=0 )
attrs.put( nifDN );
DN.setLength( 0 );
DN.append( "cn="+ID );
DN.append( ", "+ldap.headNetworkDN );
attrs.put( "objectClass", "nestNetwork" );
ldap.createLdapRecord( DN.toString( ), attrs );
}catch ( NameAlreadyBoundException e ){
System.err.println( "element with ID= "+ID+" already exist." );
throw new ElementExistException( );
}
catch( Exception e ) {
System.out.println( "writeNetwork: ERROR." );
e.printStackTrace( );
}
}
/**
*creates Device record in LDAP.
*/
void writeDevice( Device device, int ID )
throws ElementExistException {
try {
String name = device.getName( );
String descr = device.getDescription( );
StringBuilder path = new StringBuilder( "cn="+ID );
StringBuilder DN = new StringBuilder( );
Attributes attrs = new BasicAttributes( );
attrs.put( "cn", ""+ID );
if ( name!=null && !name.trim( ).isEmpty( ) )
attrs.put( "name", name );
if ( descr!=null && !descr.trim( ).isEmpty( ) )
attrs.put( "description", descr );
path.append( ", "+ldap.headDeviceDN );
Room room = device.getRoom( );
if ( room != null ) {
DN.append( "cn="+cache.IDOf( room ) );
Floor floor = room.getFloor( );
DN.append( ", cn="+ cache.IDOf( floor ) );
Building building = floor.getBuilding( );
DN.append( ", cn="+ cache.IDOf( building )+", " );
DN.append( ldap.headSpaceUnitDN );
attrs.put( "roomDN", DN.toString( ) );
}
SimpleOU simple = device.getOU( );
if ( simple!=null ) {
DN.setLength( 0 );
DN.append( "cn="+cache.IDOf( simple ) );
CompositeOU composite = simple.getParent( );
while ( composite!=null ) {
DN.append( ", cn="+cache.IDOf( composite ) );
composite = composite.getParent( );
}
DN.append( ", "+ldap.headAbstractOUDN );
attrs.put( "ouDN", DN.toString( ) );
}
Attribute lifDN = new BasicAttribute( "lifDN" );
LinkInterface[] LI = device.getLinkInterfaces( );
for ( int i=0; i<LI.length; i++ ) {
DN.setLength( 0 );
DN.append( "cn="+ cache.IDOf( LI[i] ) );
DN.append( ", "+ldap.headLIDN );
lifDN.add( DN.toString( ) );
}
if ( lifDN.size( ) !=0 )
attrs.put( lifDN );
Attribute objClass = new BasicAttribute( "objectClass" );
objClass.add( "nestDevice" );
attrs.put( objClass );
ldap.createLdapRecord( path.toString( ), attrs );
}catch ( NameAlreadyBoundException e ){
System.err.println( "element with ID= "+ID+" already exist." );
throw new ElementExistException( );
}
catch( Exception e ) {
System.out.println( "writeDevice: ERROR." );
e.printStackTrace( );
}
}
/**
*creates Building record in LDAP.
*/
void writeBuilding( Building building, int ID )
throws ElementExistException {
try {
String street = building.getAddress( );
String buildingName = building.getName( );
String descr = building.getDescription( );
StringBuilder path = new StringBuilder( "cn=" );
Attributes attrs = new BasicAttributes( );
attrs.put( "cn", ""+ID );
if ( street != null && !street.trim( ).isEmpty( ) )
attrs.put( "street", street );
if ( buildingName != null && !buildingName.trim( ).isEmpty( ) )
attrs.put( "buildingName", buildingName );
if ( descr != null && !descr.trim( ).isEmpty( ) )
attrs.put( "description", descr );
path.append( ID );
path.append( ", " );
path.append( ldap.headSpaceUnitDN );
attrs.put( "objectClass", "nestBuilding" );
ldap.createLdapRecord( path.toString( ), attrs );
}catch ( NameAlreadyBoundException e ){
System.err.println( "element with ID= "+ID+" already exist." );
throw new ElementExistException( );
}
catch( Exception e ) {
System.out.println( "writeBuilding: ERROR." );
e.printStackTrace( );
}
}
/**
*creates Floor record in LDAP.
*/
void writeFloor( Floor floor, int ID )
throws ElementExistException {
try {
int bID = cache.IDOf( floor.getBuilding( ) );
String descr = floor.getDescription( );
String name = floor.getName( );
StringBuilder path = new StringBuilder( "cn=" );
Attributes attrs = new BasicAttributes( );
attrs.put( "cn", ""+ID );
attrs.put( "floorNumber", ( ""+ floor.getNumber( ) ) );
if ( name!=null && !name.trim( ).isEmpty( ) )
attrs.put( "name", name );
if ( descr != null && !descr.trim( ).isEmpty( ) )
attrs.put( "description", descr );
attrs.put( "objectClass", "nestFloor" );
path.append( ID );
path.append( ", cn=" );
path.append( bID );
path.append( ", " );
path.append( ldap.headSpaceUnitDN );
ldap.createLdapRecord( path.toString( ), attrs );
}catch ( NameAlreadyBoundException e ){
System.err.println( "element with ID= "+ID+" already exist." );
throw new ElementExistException( );
}
catch( Exception e ) {
System.out.println( "writeFloor: ERROR." );
e.printStackTrace( );
}
}
/**
*creates Room record in LDAP.
*/
void writeRoom( Room room, int ID ) {
try {
String number = room.getNumber( );
String descr = room.getDescription( );
String name = room.getName( );
StringBuilder path = new StringBuilder( "cn=" );
StringBuilder someDN = new StringBuilder( );
Attributes attrs = new BasicAttributes( );
attrs.put( "cn", ""+ID );
Floor floor = room.getFloor( );
path.append( ID );
path.append( ", cn=" );
path.append( cache.IDOf( floor ) );
path.append( ", cn=" );
path.append( cache.IDOf( floor.getBuilding( ) ) );
path.append( ", " );
path.append( ldap.headSpaceUnitDN );
if ( number!=null && !number.trim( ).isEmpty( ) )
attrs.put( "roomNumber", number );
if ( descr!=null && !descr.trim( ).isEmpty( ) )
attrs.put( "description", descr );
if ( name!=null && !name.trim( ).isEmpty( ) )
attrs.put( "name", name );
Attribute attrDN = new BasicAttribute( "ouDN" );
SimpleOU[] simple = room.getOUs( );
CompositeOU composite=null;
for ( int i=0; i<simple.length; i++ ) {
someDN.setLength( 0 );
composite = simple[i].getParent( );
someDN.append( "cn=" );
someDN.append( cache.IDOf( simple[i] ) );
someDN.append( ", " );
while ( composite != null ) {
someDN.append( "cn=" );
someDN.append( cache.IDOf( composite ) );
someDN.append( ", " );
composite = composite.getParent( );
}
someDN.append( ldap.headAbstractOUDN );
attrDN.add( someDN.toString( ) );
}
if ( attrDN.size( )!=0 )
attrs.put( attrDN );
Device[] device = room.getDevices( );
attrDN = new BasicAttribute( "deviceDN" );
for ( int i=0; i<device.length; i++ ) {
someDN.setLength( 0 );
someDN.append( "cn=" );
someDN.append( cache.IDOf( device[i] ) );
someDN.append( ", " );
someDN.append( ldap.headDeviceDN );
attrDN.add( someDN.toString( ) );
}
if ( attrDN.size( )!=0 )
attrs.put( attrDN );
attrs.put( "objectClass", "nestRoom" );
ldap.createLdapRecord( path.toString( ), attrs );
}catch ( NameAlreadyBoundException e ){
System.err.println( "element with ID= "+ID+" already exist." );
throw new ElementExistException( );
}
catch( Exception e ) {
System.out.println( "writeRoom: ERROR." );
e.printStackTrace( );
}
}
/**
*creates CompositeOU record in LDAP.
*/
void writeCompositeOU( CompositeOU composite, int ID )
throws ElementExistException {
try {
String descr = composite.getDescription( );
String name = composite.getName( );
StringBuilder DN = new StringBuilder( "cn=" );
Attributes attrs = new BasicAttributes( );
attrs.put( "cn", ""+ID );
DN.append( ID );
DN.append( ", " );
if ( descr!=null && !descr.trim( ).isEmpty( ) )
attrs.put( "description", descr );
if ( name != null && !name.trim( ).isEmpty( ) )
attrs.put( "ou", name );
while ( composite != null ) {
composite = composite.getParent( );
if ( composite != null ) {
DN.append( "cn=" );
DN.append( cache.IDOf( composite ) );
DN.append( ", " );
}
}
DN.append( ldap.headAbstractOUDN );
attrs.put( "objectClass", "nestCompositeOU" );
ldap.createLdapRecord( DN.toString( ), attrs );
}catch ( NameAlreadyBoundException e ){
System.err.println( "element with ID= "+ID+" already exist." );
throw new ElementExistException( );
}
catch( Exception e ) {
System.out.println( "writeCompositeOU: ERROR." );
e.printStackTrace( );
}
}
/**
*creates SimpleOU record in LDAP.
*/
void writeSimpleOU( SimpleOU simple, int ID )
throws ElementExistException {
try {
String name = simple.getName( );
String descr = simple.getDescription( );
StringBuilder DN = new StringBuilder( );
StringBuilder path = new StringBuilder( "cn="+ID );
Attributes attrs = new BasicAttributes( );
attrs.put( "cn", ""+ID );
if ( name!=null && !name.trim( ).isEmpty( ) )
attrs.put( "ou", name );
if ( descr!=null && !descr.trim( ).isEmpty( ) )
attrs.put( "description", descr );
CompositeOU composite = simple.getParent( );
while ( composite!=null ) {
path.append( ", cn="+cache.IDOf( composite ) );
composite = composite.getParent( );
}
path.append( ", " );
path.append( ldap.headAbstractOUDN );
Attribute someDN = new BasicAttribute( "roomDN" );
Room[] room = simple.getRooms( );
Floor floor = null;
Building building = null;
for ( int i=0; i<room.length; i++ ) {
DN.setLength( 0 );
DN.append( "cn=" );
DN.append( cache.IDOf( room[i] ) );
floor = room[i].getFloor( );
DN.append( ", cn=" );
DN.append( cache.IDOf( floor ) );
building = floor.getBuilding( );
DN.append( ", cn=" );
DN.append( cache.IDOf( building ) );
DN.append( ", " );
DN.append( ldap.headSpaceUnitDN );
someDN.add( DN.toString( ) );
}
if ( someDN.size( )!=0 )
attrs.put( someDN );
someDN = new BasicAttribute( "deviceDN" );
Device[] device = simple.getDevices( );
for ( int i=0; i<device.length; i++ ) {
DN.setLength( 0 );
DN.append( "cn=" );
DN.append( cache.IDOf( device[i] ) );
DN.append( ", " );
DN.append( ldap.headDeviceDN );
someDN.add( DN.toString( ) );
}
if ( someDN.size( )!=0 )
attrs.put( someDN );
attrs.put( "objectClass", "nestSimpleOU" );
ldap.createLdapRecord( path.toString( ), attrs );
}catch ( NameAlreadyBoundException e ){
System.err.println( "element with ID= "+ID+" already exist." );
throw new ElementExistException( );
}
catch( Exception e ) {
System.out.println( "writeSimpleOU: ERROR." );
e.printStackTrace( );
}
}
/**
*creates All head records for elements in LDAP.
*/
void writeHeadDNs( ) {
try {
Attributes attrs = new BasicAttributes( );
try{
attrs.put( "cn", "AbstractOU" );
attrs.put( "description", "Head DN for Administration unit" );
attrs.put( "objectClass", "nestAbstractOU" );
ldap.createLdapRecord( ldap.headAbstractOUDN, attrs );
} catch( NameAlreadyBoundException e ) {}
try {
attrs = new BasicAttributes( );
attrs.put( "cn", "People" );
attrs.put( "description", "Head DN for people" );
attrs.put( "objectClass", "nestPeople" );
ldap.createLdapRecord( ldap.headPersonDN, attrs );
} catch( NameAlreadyBoundException e ) {}
try {
attrs = new BasicAttributes( );
attrs.put( "cn", "SpaceUnit" );
attrs.put( "description", "Head DN for Space Unit" );
attrs.put( "objectClass", "nestSpaceUnit" );
ldap.createLdapRecord( ldap.headSpaceUnitDN, attrs );
} catch( NameAlreadyBoundException e ) {}
try {
attrs = new BasicAttributes( );
attrs.put( "cn", "NetUnit" );
attrs.put( "description", "Head DN for Net Unit" );
attrs.put( "objectClass", "nestNetUnit" );
ldap.createLdapRecord( ldap.headNetUnitDN, attrs );
} catch( NameAlreadyBoundException e ) {}
try {
attrs = new BasicAttributes( );
attrs.put( "cn", "History" );
attrs.put( "description", "Head DN for tranzaction History" );
attrs.put( "objectClass", "nestHistory" );
ldap.createLdapRecord( ldap.headHistoryDN, attrs );
} catch( NameAlreadyBoundException e ) {}
try {
attrs = new BasicAttributes( );
attrs.put( "cn", "Devices" );
attrs.put( "description", "Head DN for all devices" );
attrs.put( "objectClass", "nestNetUnit" );
ldap.createLdapRecord( ldap.headDeviceDN, attrs );
} catch( NameAlreadyBoundException e ) {}
try{
attrs = new BasicAttributes( );
attrs.put( "cn", "Networks" );
attrs.put( "description", "Head DN for all networks" );
attrs.put( "objectClass", "nestNetUnit" );
ldap.createLdapRecord( ldap.headNetworkDN, attrs );
} catch( NameAlreadyBoundException e ) {}
try {
attrs = new BasicAttributes( );
attrs.put( "cn", "Link Interfaces" );
attrs.put( "description", "Head DN for all link interfaces" );
attrs.put( "objectClass", "nestNetUnit" );
ldap.createLdapRecord( ldap.headLIDN, attrs );
} catch( NameAlreadyBoundException e ) {}
try {
attrs = new BasicAttributes( );
attrs.put( "cn", "Network Interfaces" );
attrs.put( "description", "Head DN for all network interfaces" );
attrs.put( "objectClass", "nestNetUnit" );
ldap.createLdapRecord( ldap.headNIDN, attrs );
} catch( NameAlreadyBoundException e ) {}
}catch( Exception e ) {
System.out.println
( "LdapWriter, writeHeadDNs: problems in writing headDNs" );
}
}
}
package ru.petrsu.nest.ldap;
/**"Ведущий" кэш.
* Контроллирует уникальнось ИД.*/
5 abstract class MasterCache<K, T> extends StorageCache<K, T> {
protected SlaveStorage<K, T> storage;
void setStorage( SlaveStorage<K, T> s ) {
storage = s;
}
}
package ru.petrsu.nest.ldap;
import java.lang.*;
import java.util.*;
import java.io.*;
import java.beans.PropertyChangeListener;
import ru.petrsu.nest.san.*;
/**"Дырявый" кэш. Implementation of StorageCache.
* Кэш - массив с "дырками" - удаленными ИД, которые заполняются при добавлении
* новых элементов. Реализут абстрактный класс StorageCache.
* Конроллирует уникальность ИД.
* @author Nuikin Alexander
*/
15 class MasterHoleyCache<T> extends MasterCache<Integer, T> {
private List<T> elements = new ArrayList<T>( );
/**Free IDs.*/
private List<Integer> holesIDs = new ArrayList<Integer>( );
/**IDs of elements added to storage.
* Actually added to cache and marked as added.
* Really addition to storage will be on commit( ). */
private List<Integer> addedIDs = new ArrayList<Integer>( );//ИД добавленных
/**IDs of elements loaded from storage.*/
private List<Integer> loadedIDs = new ArrayList<Integer>( );
/**IDs of elements loaded from storage and removed.
* Actually marked as removed.
* Really removing in storage will when on commit( ) will be called.*/
private List<Integer> loadedRemovedIDs = new ArrayList<Integer>( );
/**IDs of elements loaded from storage and modified.
* Actually marked as modified.
* Really modifing in storage will when on commit( ) will be called.*/
private List<Integer> loadedModifiedIDs = new ArrayList<Integer>( );
private int maxCount = 30;
int rootDeviceID;
/*Constructor*/
MasterHoleyCache( ) {
badID = -1;
rootDeviceID = -15555;
}
/**Initialize cache from storage.*/
void checkOut( ) {
System.out.println( "==> CheckOut START" );
if( storage == null )
throw new NullPointerException( "Cache have not storage." );
Set<Integer> antiHoles = storage.getAllID( ); // антидырки
System.out.println( "Busy ID size: "+antiHoles.size( ) );
if( antiHoles.size( ) <= maxCount )
System.out.println( "Busy ID: "+antiHoles.toString( ) );
// System.out.println( "AntiHoles size: "+antiHoles.size( ) );
// Ищем максимальный ИД
int max = -1;
for( int id : antiHoles )
if( id > max )
max = id;
System.out.println( "Max ID: "+max );
// System.out.println( "New cache size: "+elements.size( ) );
for( int id = 0; id < max+1; id++ ) {
elements.add( null );
if( !antiHoles.contains( id ) )
holesIDs.add( id );
}
for( int i = 0; i < elements.size( ); i++ ) // на всякий случай
elements.set( i, null );
// System.out.println( "New cache size: "+elements.size( ) );
System.out.println( "Free ID size: "+holesIDs.size( ) );
if( holesIDs.size( ) <= maxCount )
System.out.println( "Free ID: "+holesIDs.toString( ) );
System.out.println( "==> CheckOut END" );
}
/*Returns all ID.*/
/*
Collection<Integer> getAllID( ) {
Collection<Integer> col = new ArrayList<Integer>( );
for( int i = 0; i < elements.size( ); i++ )
if( !holesIDs.contains( i ) )
col.add( i );
return col;
}*/
/**Returns all elements in cache.*/
Set<T> getLoadedElements( ) {
Set<T> res = new HashSet<T>( );
for( Integer i = 0; i < elements.size( ); i++ )
if( elements.get( i ) != null && !holesIDs.contains( i ) )
res.add( elements.get( i ) );
return res;
}
/**Returns all elements by given type.*/
<U extends T> Set<U> getElements( Class<U> type ) {
Set<U> res = new HashSet<U>( );
System.out.println( "Get elements from ldap..." );
Set<Integer> setOfID = storage.getElements( type );
System.err.println( "size:" + setOfID.size( ) );
for( Integer id : setOfID )
if( !holesIDs.contains( id ) )
try {
System.out.println( "\tload & add to result ["+id+"]" );
if( type.isInstance( get( id ) ) )
res.add( type.cast( get( id ) ) );
}catch( UnknownIDException e ) {
System.err.println( "UnknownIDException" );
}
System.out.println( "Get elements from added..." );
for( Integer id : addedIDs )
if( type.isInstance( elements.get( id ) ) ) {
System.out.println( "\tadd to result ["+id+"]" );
res.add( type.cast( elements.get( id ) ) );
}
return res;
}
/**Returns element from cache by given ID.
* Always return not-null element or generate exception.*/
T get( Integer ID )
throws UnknownIDException {
if( ID < 0 || ID > elements.size( )
|| holesIDs.contains( ID )
|| loadedRemovedIDs.contains( ID ) )
throw new UnknownIDException( ID.toString( ) );
T el = elements.get( ID );
if( el == null ) { // element not loaded from storage yet
el = storage.getNotReferenced( ID );
elements.set( ID, el );
try {
storage.setReferences( ID, el );
}catch( IDNotRespectiveTypeException e ) {
System.out.println( e );
}
}
if( !loadedIDs.contains( ID ) )
loadedIDs.add( ID );
return el;
}
/**Add new element to cache and storage.
* Returns ID ( new unique ID ) of added element */
Integer add( T el )
throws ElementExistException, NullPointerException {
if( isLoaded( el ) )
throw new ElementExistException( );
Integer ID;
if( holesIDs.isEmpty( ) ) {
ID = elements.size( );
elements.add( el );
}
else {
ID = holesIDs.get( 0 );
holesIDs.remove( 0 );
elements.set( ID, el );
}
if( !addedIDs.contains( ID ) )
addedIDs.add( ID );
//storage.add( ID, el ); // !!! ошибка при записи по ссылкам
return ID;
}
/**Removes element from cache by gived ID.*/
void removeByID( Integer ID )
throws UnknownIDException {
if( ID < 0 || ID > elements.size( ) || holesIDs.contains( ID ) )
throw new UnknownIDException( ID.toString( ) );
//storage.remove( ID );
if( loadedIDs.contains( ID ) ) {
loadedRemovedIDs.add( ID );
Integer ind = loadedModifiedIDs.indexOf( ID );
if( ind != -1 )
loadedModifiedIDs.remove( ind );
}else if( addedIDs.contains( ID ) ) {
Integer ind = addedIDs.indexOf( ID );
holesIDs.add( ID );
elements.set( ID, null );
addedIDs.remove( ind );
}else
throw new DebugException( );
}
/**Removes element from cache by gived ID.*/
void remove( T el )
throws UnknownElementException, NullPointerException {
if( isLoaded( el ) ){
Integer id = IDOf( el );
try {
removeByID( id );
}catch( UnknownIDException e ) {
throw new DebugException( e );
}
}
else throw new UnknownElementException( );
}
/**Marked element as modified.*/
void modify( Integer ID )
throws UnknownIDException {
if( isLoadedByID( ID ) ) {
if( !loadedModifiedIDs.contains( ID ) ) // еще не изменен
loadedModifiedIDs.add( ID );
}else
throw new DebugException( );
}
/**Returns true if given element loaded from storage.*/
boolean isLoaded( T el )
throws NullPointerException {
if( el == null )
throw new NullPointerException( );
Integer id = IDOf( el );
if( id == -1 ) {
return false;
}
//if( !loadedIDs.contains( id ) && !addedIDs.contains( id ) )
// throw new DebugException( );
return true;
}
/**Returns true if loaded from storage element with gived ID.*/
boolean isLoadedByID( Integer ID )
throws UnknownIDException {
if( ID < 0 || ID > elements.size( )
|| holesIDs.contains( ID )
|| loadedRemovedIDs.contains( ID ) )
throw new UnknownIDException( ID.toString( ) );
if( elements.get( ID ) == null )
return false;
//if( !loadedIDs.contains( ID ) && !addedIDs.contains( ID ) )
// throw new DebugException( );
return true;
}
/**Returns true if in system ( cache/storage ) exist element with given ID.*/
boolean isID( Integer ID ) {
if( ID < 0 || ID > elements.size( )
|| holesIDs.contains( ID )
|| loadedRemovedIDs.contains( ID ) )
return false;
return true;
}
/**Returns ID of element
* or -1 if no given element in cache.*/
Integer IDOf( T el )
throws NullPointerException {
if( el == null )
return badID;
//throw new NullPointerException( );
Integer id = elements.indexOf( el );
if( loadedRemovedIDs.contains( id ) )
return badID;
return id;
}
/**Returns count of ID.*/
int countOfID( ) {
return elements.size( ) - holesIDs.size( ) - loadedRemovedIDs.size( );
}
/**Returns count of loaded objects.*/
int countOfLoaded( ) {
return loadedIDs.size( );
}
/**Write all unsaved changes to storage.*/
void commit( ) {
try {
System.out.println( "==> Commit all START" );
//1. Storage.remove( id ) ( удаляем обратных без обратных ссылок ) для всех loadedRemovedIDs
commit( loadedRemovedIDs, REM );
//2. Storage.modify( id ) ( переписываем сверху без обратных ссылок ) для всех loadedModifiedIDs
System.out.println( "==> Remove added from modified" );
for( int i=0; i<addedIDs.size( ); i++ ) {
int ind = loadedModifiedIDs.indexOf( addedIDs.get( i ) );
if( ind != -1 )
loadedModifiedIDs.remove( ind );
}
commit( loadedModifiedIDs, MOD );
// Storage.add( id ) для всех addedIDs
commit( addedIDs, ADD );
System.out.println( "=> Cleaning out" );
loadedModifiedIDs.clear( );
addedIDs.clear( );
// Все loadedRemovedIDs добавить в holesIDs
for( Integer ind=0; ind<loadedRemovedIDs.size( ); ind++ )
holesIDs.add( loadedRemovedIDs.get( ind ) );
loadedRemovedIDs.clear( );
// set root device
/*
System.out.println(
"Storage set root Device: ["+rootDeviceID+"]" );
try {
SanStorage.class.cast( storage ).setRootDeviceID( rootDeviceID );
System.out.println( ": OK" );
}catch( UnknownIDException e ){
System.out.print( ": BAD: " );
System.out.println( e.toString( ) );
}*/
System.out.println( "==> Commit all END" );
}catch( UnknownIDException e ) {
throw new DebugException( e );
}
}
private final int ADD = 1;
private final int REM = 2;
private final int MOD = 3;
/**Correct commit*/
private void commit( List<Integer> listIDs, int Operation )
throws UnknownIDException {
switch( Operation ) {
case REM: System.out.println( "=> Commit loaded & removed" ); break;
case MOD: System.out.println( "=> Commit loaded & modified" ); break;
case ADD: System.out.println( "=> Commit added" ); break;
default: throw new DebugException( "Bad operation" );
}
System.out.println( "Count of IDs: "+listIDs.size( ) );
if( listIDs.size( ) == 1 && Operation == ADD ) { // SAN
System.out.println( "==> Commit end" );
return;
}
if( listIDs.size( ) <= maxCount )
System.out.println( listIDs.toString( ) );
List<Class<? extends SanElement>> types =
new ArrayList<Class<? extends SanElement>>( );
types.add( Device.class );
types.add( LinkInterface.class );
types.add( NetworkInterface.class );
types.add( IPNetwork.class );
types.add( Building.class );
types.add( Floor.class );
types.add( Room.class );
types.add( CompositeOU.class );
types.add( SimpleOU.class );
// по всем типам
for( Class<? extends SanElement> t : types ) {
System.out.println( "type: "+t.getSimpleName( )+":" );
// по всем элементам данного типа
for( int i=0; i < listIDs.size( ); i++ ) {
if( t.isInstance( get( listIDs.get( i ) ) ) ) {
System.out.println( "\t"+
get( listIDs.get( i ) ).getClass( ).getSimpleName( )+"["+listIDs.get( i )+"]" );
switch( Operation ) {
case REM: storage.remove( listIDs.get( i ) );
break;
case MOD: storage.modify( listIDs.get( i ) );
break;
case ADD: storage.add( listIDs.get( i ), get( listIDs.get( i ) ) );
break;
default: throw new DebugException( "Bad operation" );
}
}
}
}
System.out.println( "==> Commit end" );
}
/**Print out cache.*/
void print( ) {
System.out.println( "> Loaded: " );
System.out.println( loadedIDs.toString( ) );
System.out.println( "> Loaded & removed: " );
System.out.println( loadedRemovedIDs.toString( ) );
System.out.println( "> Loaded & modified: " );
System.out.println( loadedModifiedIDs.toString( ) );
System.out.println( "> Added: " );
System.out.println( addedIDs.toString( ) );
System.out.print( "> elements: \n[" );
for( int i=0; i<elements.size( ); i++ )
if( elements.get( i ) != null )
System.out.print( i+", " );
System.out.println( "]" );
System.out.println( "> Holes: " );
System.out.println( holesIDs.toString( ) );
}
}
package ru.petrsu.nest.ldap;
import java.lang.*;
import java.util.*;
/**"Ведущее" хранилище.
* Контролирует уникальность ИД элеметов.
* @author Alexander Nuikin*/
8 abstract class MasterStorage<K, T> extends Storage<K, T> {
SlaveCache<K, T> cache;
/*Constructor*/
MasterStorage( SlaveCache<K, T> c )
throws StorageBadConnectionException {
cache = c;
cache.setStorage( this );
}
/**Add new element to storage.
* Returns ID of added element.*/
abstract K add( T el )
throws NullPointerException;
}
package ru.petrsu.nest.ldap;
import ru.petrsu.nest.san.SanElement;
import ru.petrsu.nest.san.SAN;
import ru.petrsu.nest.san.Device;
import ru.petrsu.nest.san.CompositeOU;
import ru.petrsu.nest.san.PropertyValueAddEvent;
import ru.petrsu.nest.san.PropertyValueRemoveEvent;
import java.lang.*;
import java.util.*;
import java.io.*;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
16 class SANListener<K> extends SanElementListener<K>
{
private SanStorage<K> sanStorage;
private boolean rootOUSetted = false;
/*Constructor*/
SANListener( StorageCache<K, SanElement> c, SanStorage<K> s, K id ) {
super( c, id );
sanStorage = s;
}
/**Reaction for changing property.*/
public void propertyChange( PropertyChangeEvent event )
{
super.propertyChange( event );
try {
SanElement value = SanElement.class.cast( event.getNewValue( ) );
// SAN.setRootDevice( ) called
if( value instanceof Device ) {
K id = cache.IDOf( value );
cache.rootDeviceID = id;
System.out.println( "Cache set root Device: ["+id+"]" );
}
// SAN.setRootOU( ) called
if( value instanceof CompositeOU ) {
if( !rootOUSetted ) {
System.out.print( "SAN: set root OU: " );
rootOUSetted = true;
}else{
System.out.print(
"SAN: affective set root OU. Current RootOU: " );
}
K id = sanStorage.getRootOUID( );
System.out.println( "["+id+"]" );
}
}catch( ClassCastException e ) {
// do nothing
}
}
}
package ru.petrsu.nest.ldap;
import ru.petrsu.nest.san.NetworkInterface;
import ru.petrsu.nest.san.LinkInterface;
import ru.petrsu.nest.san.Building;
import ru.petrsu.nest.san.Room;
import ru.petrsu.nest.san.Floor;
import ru.petrsu.nest.san.SimpleOU;
import ru.petrsu.nest.san.CompositeOU;
import ru.petrsu.nest.san.Device;
import ru.petrsu.nest.san.Network;
import ru.petrsu.nest.san.IPNetwork;
import ru.petrsu.nest.san.SanElement;
import ru.petrsu.nest.san.EthernetInterface;
import ru.petrsu.nest.san.IPv4Interface;
import ru.petrsu.nest.san.References;
import ru.petrsu.nest.san.AbstractOU;
import ru.petrsu.nest.ldap.CacheReference;
//import java.io.IOException;
import java.util.Set;
import java.util.List;
import java.util.HashSet;
import java.util.Iterator;
import java.util.ArrayList;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.lang.String;
import java.lang.Integer;
import javax.naming.NamingException;
import javax.naming.NamingEnumeration;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
/**
* Class SanElementCreator - class for creating SanElement from
* attributes.
*
* @author Artemev Dmitry;
*/
46 class SanElementCreator
{
48 String DN = null;
49 Attributes attrs;
50 Ldap ldap;
MasterCache<Integer, SanElement> cache;
SanElementCreator( Ldap l, MasterCache<Integer, SanElement> c ) {
ldap = l;
cache = c;
}
SanElement getEmptyElement( int ID )
throws UnknownIDException {
SanElement toSanElement = null;
String objClass = null;
attrs = ldap.getAttributes( ID );
DN = ldap.IDtoDN( ID );
try{
NamingEnumeration NEobjClass =
( attrs.get( "objectClass" ) ).getAll( );
objClass = ( NEobjClass.next( ) ).toString( );
if ( ( objClass ).equals( "nestBuilding" ) )
toSanElement = new Building( );
else if ( ( objClass ).equals( "nestFloor" ) )
toSanElement = new Floor( );
else if ( ( objClass ).equals( "nestRoom" ) )
toSanElement = new Room( );
else if ( ( objClass ).equals( "nestCompositeOU" ) )
toSanElement = new CompositeOU( );
else if ( ( objClass ).equals( "nestSimpleOU" ) )
toSanElement = new SimpleOU( );
else if ( ( objClass ).equals( "nestDevice" ) )
toSanElement = new Device( );
else if ( ( objClass ).equals( "nestEthernetInterface" )|
( ( objClass ).equals( "nestLinkInterface" ) ) )
toSanElement = new LinkInterface( );
else if ( ( objClass ).equals( "nestIPv4Interface" )|
( objClass ).equals( "nestNetworkInterface" ) )
toSanElement = new NetworkInterface( );
else if ( ( objClass ).equals( "nestNetwork" ) )
toSanElement = new IPNetwork( );
}
catch( NamingException e ){
System.out.println( "Couldn't create empty element" +ID );
}
return toSanElement;
}
/**
* Get's element by knowing it's ID.Return's SanElement.
*/
void getElement( int ID, SanElement el )
throws NullPointerException, IDNotRespectiveTypeException {
if ( el==null )
throw new NullPointerException( "Can't read from LDAP. ID="+ID );
else {
String obj=null;
try {
NamingEnumeration NE = attrs.get( "objectClass" ).getAll( );
obj = NE.next( ).toString( );
}catch( NamingException e ){
System.err.println
( "Couldn't get objectClass from Ldap record with ID: "+ID );
}
if( ( el instanceof Building )&&( obj.equals( "nestBuilding" ) ) )
getBuilding( attrs, DN, ID, Building.class.cast( el ) );
else if( ( el instanceof Floor )&&( obj.equals( "nestFloor" ) ) )
getFloor( attrs, DN, ID, Floor.class.cast( el ) );
else if( ( el instanceof Room )&&( obj.equals( "nestRoom" ) ) )
getRoom( attrs, DN , ID, Room.class.cast( el ) );
else if( ( el instanceof CompositeOU )&&
( obj.equals( "nestCompositeOU" ) ) )
getCompositeOU( attrs, DN, ID, CompositeOU.class.cast( el ) );
else if( ( el instanceof SimpleOU )&&( obj.equals( "nestSimpleOU" ) ) )
getSimpleOU( attrs, DN, ID, SimpleOU.class.cast( el ) );
else if( ( el instanceof Device )&&( obj.equals( "nestDevice" ) ) )
getDevice( attrs, ID , Device.class.cast( el ) );
else if( ( el instanceof LinkInterface )&&
( ( obj.equals( "nestLinkInterface" ) )||
( obj.equals( "nestEthernetInterface" ) ) ) )
getLI( attrs, ID, LinkInterface.class.cast( el ) );
else if( ( el instanceof NetworkInterface )&&
( ( obj.equals( "nestNetworkInterface" ) )||
( obj.equals( "nestIPv4Interface" ) ) ) )
getNI( attrs, ID, NetworkInterface.class.cast( el ) );
else if( ( el instanceof IPNetwork )&&( obj.equals( "nestNetwork" ) ) )
getNetwork( attrs, ID, IPNetwork.class.cast( el ) );
else
throw new IDNotRespectiveTypeException
( el.getClass( ), obj.substring( 4 ) );
}
}
/* public Set<SanElement> getElements( String filterString ) {
Set<SanElement> elements = new HashSet<SanElement>( );
try
{
int id;
Set<Integer> IDS = ldap.scopeSearch( ldap.mainHeadDN, filterString, -1 );
Iterator<Integer> i = IDS.iterator( );
while ( i.hasNext( ) )
{
id = i.next( );
elements.add( getElement( id, getEmptyElement( id ) ) );
}
}
catch ( Exception e ) {
}
return elements;
}
*/
/**
*return's SanElement of building;
*/
private void getBuilding( Attributes attrs, String DN, int ID,
Building building ) {
try {
Attribute descrAttr = attrs.get( "description" );
Attribute name = attrs.get( "buildingName" );
Attribute street = attrs.get( "street" );
if ( name!=null )
building.setName( name.getAll( ).next( ).toString( ) );
if ( street!=null )
building.setAddress( street.getAll( ).next( ).toString( ) );
if ( descrAttr!=null ) {
NamingEnumeration NEDescript = descrAttr.getAll( );
building.setDescription( ( NEDescript.next( ) ).toString( ) );
}
Set<Integer> set = ldap.scopeSearch( DN, "( objectClass=nestFloor )", 1 );
if ( set.size( ) != 0 ) {
List<LdapSanReference<Floor>> floorDNs=
new ArrayList<LdapSanReference<Floor>>( );
Iterator<Integer> iter = set.iterator( );
for ( int i=0; i<set.size( ); i++ )
{
LdapSanReference<Floor> crf =
new LdapSanReference<Floor>
( cache, iter.next( ), Floor.class );
floorDNs.add( crf );
}
building.setFloorsReference
( References.referenceCollectionToArray( floorDNs ) );
/* // Загрузка этажа по ссылке.( пример отладки - отлажено )
Iterator<CacheReference<Floor>> iii = floorDNs.iterator( );
while ( iii.hasNext( ) )
{
iii.next( ).get( );
}
*/
}
}catch ( Exception e ) {
System.err.println( "getBuilding Couldn't fill element ID="+ID );
}
}
/**
*return's SanElement of Floor;
*/
private void getFloor( Attributes attrs, String DN, int ID,
Floor floor ) {
try {
Attribute descrAttr = attrs.get( "description" );
Attribute name = attrs.get( "name" );
Attribute number = attrs.get( "floorNumber" );
if( descrAttr!=null ) {
NamingEnumeration NEDescript = descrAttr.getAll( );
floor.setDescription( NEDescript.next( ).toString( ) );
}
if ( name!=null ) {
NamingEnumeration NEName = name.getAll( );
floor.setName( NEName.toString( ) );
}
if ( number!=null )
floor.setNumber( Integer.parseInt
( number.getAll( ).next( ).toString( ) ) );
String parentDN = DN.substring( ( ""+ID ).length( ) + 4 );
Set<Integer> set = ldap.scopeSearch( DN, "( objectClass=nestRoom )", 1 );
if ( set.size( ) != 0 ) {
List<LdapSanReference<Room>> roomDNs=
new ArrayList<LdapSanReference<Room>>( );
Iterator<Integer> iter = set.iterator( );
for ( int i=0; i<set.size( ); i++ )
{
LdapSanReference<Room> crr = new LdapSanReference<Room>
( cache, iter.next( ), Room.class );
roomDNs.add( crr );
}
floor.setRoomsReference
( References.referenceCollectionToArray( roomDNs ) );
}
LdapSanReference<Building> crb =
new LdapSanReference<Building>
( cache, ldap.DNtoID( parentDN ), Building.class );
floor.setBuildingReference( crb );
}catch ( Exception e ) {
System.err.println( "getFloor Couldn't fill element ID="+ID );
}
}
/**
*return's SanElement of Room;
*/
private void getRoom( Attributes attrs, String DN, int ID, Room room ) {
try {
Attribute descrAttr = attrs.get( "description" );
Attribute name = attrs.get( "name" );
Attribute someAttr = attrs.get( "ouDN" );
Attribute number = attrs.get( "roomNumber" );
if( descrAttr!=null ) {
NamingEnumeration NEDescript = descrAttr.getAll( );
room.setDescription( NEDescript.next( ).toString( ) );
}
if( name!=null ) {
NamingEnumeration NEName = name.getAll( );
room.setName( NEName.next( ).toString( ) );
}
if ( number!=null )
room.setNumber( ( number.getAll( ).next( ) ).toString( ) );
String parentDN = DN.substring( ( ""+ID ).length( ) + 4 );
if ( someAttr != null ) {
NamingEnumeration NEOuDN = someAttr.getAll( );
List<LdapSanReference<SimpleOU>> OU =
new ArrayList<LdapSanReference<SimpleOU>>( );
while ( NEOuDN.hasMore( ) )
{
LdapSanReference<SimpleOU> crsou =
new LdapSanReference<SimpleOU>
( cache, ldap.DNtoID( NEOuDN.next( ).toString( ) ),
SimpleOU.class );
OU.add( crsou );
}
room.setOUsReference
( References.referenceCollectionToArray( OU ) );
}
someAttr = attrs.get( "deviceDN" );
if ( someAttr != null ) {
NamingEnumeration NEDevDN = someAttr.getAll( );
List<LdapSanReference<Device>> Dev =
new ArrayList<LdapSanReference<Device>>( );
while ( NEDevDN.hasMore( ) )
{
LdapSanReference<Device> crd =
new LdapSanReference<Device>
( cache, ldap.DNtoID( NEDevDN.next( ).toString( ) ),
Device.class );
Dev.add( crd );
}
room.setDevicesReference
( References.referenceCollectionToArray( Dev ) );
}
LdapSanReference<Floor> crf = new LdapSanReference<Floor>
( cache, ldap.DNtoID( parentDN ), Floor.class );
room.setFloorReference( crf );
}catch ( Exception e ) {
System.err.println( "getRoom Couldn't fill element ID="+ID );
}
}
/**
*return's SanElement of CompositeOU;
*/
private void getCompositeOU( Attributes attrs, String DN,
int ID, CompositeOU composite ) {
try {
Attribute attr = attrs.get( "description" );
Attribute name = attrs.get( "ou" );
if ( attr!=null ) {
NamingEnumeration NEDescript = attr.getAll( );
composite.setDescription( ( NEDescript.next( ) ).toString( ) );
}
String parentDN = DN.substring( ( ""+ID ).length( ) + 4 );
Set<Integer> set = ldap.scopeSearch( DN,
"( ( objectClass=nestSimpleOU )|( objectClass=nestCompositeOU ) )", 1 );
List<LdapSanReference<AbstractOU>> OU=
new ArrayList<LdapSanReference<AbstractOU>>( );
Iterator<Integer> iter = set.iterator( );
for ( int i=0; i<set.size( ); i++ )
{
LdapSanReference<AbstractOU> ceaou =
new LdapSanReference<AbstractOU>
( cache, iter.next( ), AbstractOU.class );
OU.add( ceaou );
}
composite.setOUsReference
( References.referenceCollectionToArray( OU ) );
if ( name!=null ) {
NamingEnumeration NEOu = name.getAll( );
composite.setName( NEOu.next( ).toString( ) );
}
if ( parentDN.equals( ldap.headAbstractOUDN )==false ) {
LdapSanReference<CompositeOU> crcou =
new LdapSanReference<CompositeOU>
( cache, ldap.DNtoID( parentDN ), CompositeOU.class );
composite.setParentReference( crcou );
}
}catch ( Exception e ) {
System.err.println( "getCompositeOU Couldn't fill element ID="+ID );
}
}
/**
*return's SanElement of SimpleOU;
*/
private void getSimpleOU( Attributes attrs, String DN,
int ID, SimpleOU simple ) {
try {
Attribute descrAttr = attrs.get( "description" );
Attribute name = attrs.get( "ou" );
Attribute roomDN = attrs.get( "roomDN" );
// NamingEnumeration NEPersonDN = descrAttr.getAll( );
if ( descrAttr!=null ) {
NamingEnumeration NEDescript = descrAttr.getAll( );
simple.setDescription( NEDescript.next( ).toString( ) );
}
if ( name!=null ) {
NamingEnumeration NEOu = name.getAll( );
simple.setName( ( NEOu.next( ) ).toString( ) );
}
String parentDN = DN.substring( ( ""+ID ).length( ) + 4 );
/* if ( NEPersonDN != null )
{
List<CacheReference<Person>> Person =
new ArrayList<CacheReference<Person>>( );
while ( NEPersonDN.hasMore( ) )
Person.add( new CacheReference<Person>
( DNtoID( NEPersonDN.next( ).toString( ) ) ) );
simple.setPersonsReference
( Person.toArray( new CacheReference[Person.size( )] ) );
}
*/
if ( roomDN != null ) {
NamingEnumeration NERoomDN = roomDN.getAll( );
List<LdapSanReference<Room>> Room =
new ArrayList<LdapSanReference<Room>>( );
while ( NERoomDN.hasMore( ) )
{
LdapSanReference<Room> crr = new LdapSanReference<Room>
( cache, ldap.DNtoID( NERoomDN.next( ).toString( ) ),
Room.class );
Room.add( crr );
}
simple.setRoomsReference
( References.referenceCollectionToArray( Room ) );
}
if ( parentDN.indexOf( "cn=AbstractOU" ) != 0 ) {
LdapSanReference<CompositeOU> crcou =
new LdapSanReference<CompositeOU>
( cache, ldap.DNtoID( parentDN ), CompositeOU.class );
simple.setParentReference( crcou );
}
else
{
LdapSanReference<CompositeOU> crcou =
new LdapSanReference<CompositeOU>
( cache, -1, CompositeOU.class );
}
}catch ( Exception e ) {
System.err.println( "getSimpleOU Couldn't fill"+ID );
}
}
/**
*return's SanElement of Device;
*/
private void getDevice( Attributes attrs, int ID, Device device ) {
try {
Attribute descrAttr = attrs.get( "description" );
Attribute someAttr = attrs.get( "roomDN" );
Attribute name = attrs.get( "name" );
if( descrAttr!=null ) {
NamingEnumeration Descr = descrAttr.getAll( );
device.setDescription( ( Descr.next( ) ).toString( ) );
}
if ( someAttr != null ) {
NamingEnumeration RmDN = someAttr.getAll( );
LdapSanReference<Room> crd = new LdapSanReference<Room>
( cache, ldap.DNtoID( RmDN.next( ).toString( ) ), Room.class );
device.setRoomReference( crd );
}
someAttr = attrs.get( "ouDN" );
if ( someAttr != null ) {
NamingEnumeration OuDN = someAttr.getAll( );
LdapSanReference<SimpleOU> crsou =
new LdapSanReference<SimpleOU>
( cache, ldap.DNtoID( OuDN.next( ).toString( ) ),
SimpleOU.class );
device.setOUReference( crsou );
}
someAttr = attrs.get( "lifDN" );
if ( someAttr != null ) {
NamingEnumeration LifDN = someAttr.getAll( );
List<LdapSanReference<LinkInterface>> Lif =
new ArrayList<LdapSanReference<LinkInterface>>( );
while ( LifDN.hasMore( ) )
{
LdapSanReference<LinkInterface> crlif =
new LdapSanReference<LinkInterface>
( cache, ldap.DNtoID( LifDN.next( ).toString( ) ),
LinkInterface.class );
Lif.add( crlif );
}
device.setLinkInterfacesReference
( References.referenceCollectionToArray( Lif ) );
}
}catch ( Exception e ) {
System.err.println( "getDevice Couldn't fill element ID="+ID );
}
}
/**
*return's SanElement of LinkInterface;
*/
private void getLI( Attributes attrs, int ID, LinkInterface li ) {
try
{
// System.out.println( attrs.get( "cn" ).getAll( ).next( ).toString( ) );
Attribute descrAttr = attrs.get( "description" );
Attribute someAttr = attrs.get( "macAddress" );
Attribute name = attrs.get( "name" );
Attribute DevDN = attrs.get( "deviceDN" );
if ( descrAttr!=null ) {
NamingEnumeration Descr = descrAttr.getAll( );
li.setDescription( ( Descr.next( ) ).toString( ) );
}
if ( name != null ) {
NamingEnumeration NEName = name.getAll( );
li.setName( NEName.next( ).toString( ) );
}
if ( DevDN != null ) {
LdapSanReference<Device> crd =new LdapSanReference<Device>
( cache, ldap.DNtoID( DevDN.getAll( ).next( ).toString( ) )
, Device.class );
li.setDeviceReference( crd );
}
if ( someAttr!=null ) {
NamingEnumeration Mac = someAttr.getAll( );
EthernetInterface EI = new EthernetInterface( );
String macAddressString = Mac.next( ).toString( );
byte[] macAddressByte = new byte[6];
int one = 0;
for ( int i=0; i<6; i++ ) {
one = i*3;
String b = macAddressString.substring( one, one+2 );
macAddressByte[i] = ( byte ) Integer.parseInt( b, 16 );
}
try {
EI.setMACAddress( macAddressByte );
li = EI;
}catch ( Exception e ) {
System.out.println( "Ldap.java function getNI" );
System.out.println( "\t\t Can not get MacAddress address" );
e.printStackTrace( );
}
}
someAttr = attrs.get( "mode" );
if ( someAttr!=null ) {
NamingEnumeration Mode = someAttr.getAll( );
LinkInterface.Mode Md = null;
Md = Md.valueOf( Mode.next( ).toString( ) );
li.setMode( Md );
}
someAttr = attrs.get( "state" );
if ( someAttr!=null ) {
NamingEnumeration State = someAttr.getAll( );
LinkInterface.State St = null;
St = St.valueOf( State.next( ).toString( ) );
li.setState( St );
}
someAttr = attrs.get( "nifDN" );
if ( someAttr != null ) {
NamingEnumeration NifDN = someAttr.getAll( );
List<LdapSanReference<NetworkInterface>> Nif =
new ArrayList<LdapSanReference<NetworkInterface>>( );
while ( NifDN.hasMore( ) )
{
LdapSanReference<NetworkInterface> crnif =
new LdapSanReference<NetworkInterface>
( cache, ldap.DNtoID( NifDN.next( ).toString( ) ),
NetworkInterface.class );
Nif.add( crnif );
}
li.setNetworkInterfacesReference
( References.referenceCollectionToArray( Nif ) );
}
someAttr = attrs.get( "link" );
if ( someAttr != null ) {
NamingEnumeration Link = someAttr.getAll( );
LdapSanReference<LinkInterface> crlif =
new LdapSanReference<LinkInterface>
( cache, ldap.DNtoID( Link.next( ).toString( ) ),
LinkInterface.class );
li.setLinkReference( crlif );
}
// if ( attrs.get( "cn" ).getAll( ).next( ).toString( ).equals( "33" ) )
// System.out.println( attrs.get( "cn" ).getAll( ).next( ).toString( ) );
}catch ( Exception e ) {
System.err.println( "getLIouldn't fill element ID="+ID );
}
}
/**
*return's SanElement of NetworkInterface;
*/
private void getNI( Attributes attrs, int ID, NetworkInterface ni ) {
try {
Attribute descrAttr = attrs.get( "description" );
Attribute name = attrs.get( "name" );
Attribute someAttr = attrs.get( "ipHostNumber" );
NamingEnumeration LifDN = attrs.get( "lifDN" ).getAll( );
NamingEnumeration NetDN = attrs.get( "NetworkDN" ).getAll( );
if ( someAttr!=null ) {
NamingEnumeration IP = someAttr.getAll( );
IPv4Interface v4 = new IPv4Interface( );
String IPv4 = IP.next( ).toString( );
InetAddress inAddr;
try {
inAddr = InetAddress.getByName( IPv4 );
v4.setInetAddress( inAddr.getAddress( ) );
ni = v4;
}catch ( UnknownHostException e ) {
System.out.println( "Ldap.java function getNI" );
System.out.println( "\t\t Can not get IP address" );
e.printStackTrace( );
}
}
if ( descrAttr != null ) {
NamingEnumeration Descr = descrAttr.getAll( );
ni.setDescription( ( Descr.next( ) ).toString( ) );
}
if ( name!=null ) {
NamingEnumeration NEName = name.getAll( );
ni.setName( NEName.next( ).toString( ) );
}
if ( LifDN != null ) {
LdapSanReference<LinkInterface> crlif =
new LdapSanReference<LinkInterface>
( cache, ldap.DNtoID( LifDN.next( ).toString( ) ),
LinkInterface.class );
ni.setLinkInterfaceReference( crlif );
}
if ( NetDN != null ) {
LdapSanReference<Network> crn = new LdapSanReference<Network>
( cache, ldap.DNtoID( NetDN.next( ).toString( ) ), Network.class );
ni.setNetworkReference( crn );
}
}catch ( Exception e ) {
System.err.println( "getNI couldn't fill element ID="+ID );
}
}
/**
*return's SanElement of Network;
*/
private void getNetwork( Attributes attrs, int ID, IPNetwork net ) {
try {
Attribute descrAttr = attrs.get( "description" );
Attribute mask = attrs.get( "ipNetmaskNumber" );
Attribute number = attrs.get( "ipNetworkNumber" );
Attribute name = attrs.get( "Name" );
Attribute ni = attrs.get( "nifDN" );
if ( ( mask == null ) | ( number == null ) ) {
if ( descrAttr != null ) {
NamingEnumeration Descr = descrAttr.getAll( );
net.setDescription( ( Descr.next( ) ).toString( ) );
}
if ( name!=null ) {
NamingEnumeration NEName = name.getAll( );
net.setName( NEName.toString( ) );
}
if ( ni != null ) {
NamingEnumeration NifDN = ni.getAll( );
List<LdapSanReference<NetworkInterface>> Nif =
new ArrayList<LdapSanReference<NetworkInterface>>( );
while ( NifDN.hasMore( ) )
{
LdapSanReference<NetworkInterface> crnif =
new LdapSanReference<NetworkInterface>
( cache, ldap.DNtoID( NifDN.next( ).toString( ) ),
NetworkInterface.class );
Nif.add( crnif );
}
net.setNetworkInterfacesReference
( References.referenceCollectionToArray( Nif ) );
}
}else {
IPNetwork ipnet = new IPNetwork( );
if ( descrAttr != null ) {
NamingEnumeration Descr =attrs.get( "description" ).getAll( );
ipnet.setDescription( ( Descr.next( ) ).toString( ) );
}
if ( name!=null ) {
NamingEnumeration NEName = name.getAll( );
ipnet.setName( NEName.toString( ) );
}
NamingEnumeration NetN =attrs.get( "ipNetworkNumber" ).getAll( );
NamingEnumeration MaskN =attrs.get( "ipNetmaskNumber" ).getAll( );
String Mask = MaskN.next( ).toString( );
String Net = NetN.next( ).toString( );
InetAddress MaskAddr;
InetAddress NetAddr;
try {
MaskAddr = InetAddress.getByName( Mask );
NetAddr = InetAddress.getByName( Net );
ipnet.setAddress( NetAddr.getAddress( ) );
ipnet.setMask( MaskAddr.getAddress( ) );
}catch ( UnknownHostException e ) {
System.out.println( "Ldap.java function getNetwork" );
System.out.println
( "\t\t Can not get MAsk or Net address" );
e.printStackTrace( );
}
if ( ni != null ) {
NamingEnumeration NifDN = ni.getAll( );
List<LdapSanReference<NetworkInterface>> Nif =
new ArrayList<LdapSanReference<NetworkInterface>>( );
while ( NifDN.hasMore( ) )
{
LdapSanReference<NetworkInterface> crnif =
new LdapSanReference<NetworkInterface>
( cache, ldap.DNtoID( NifDN.next( ).toString( ) ),
NetworkInterface.class );
Nif.add( crnif );
}
ipnet.setNetworkInterfacesReference
( References.referenceCollectionToArray( Nif ) );
}
}
}catch ( Exception e ) {
System.err.println( "getNetwork Couldn't fill element ID="+ID );
}
}
}
package ru.petrsu.nest.ldap;
import ru.petrsu.nest.san.PropertyValueAddEvent;
import ru.petrsu.nest.san.PropertyValueRemoveEvent;
import ru.petrsu.nest.san.SanElement;
import ru.petrsu.nest.san.SAN;
import java.lang.*;
import java.util.*;
import java.io.*;
import java.beans.*;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
//import java.beans.PropertyChangeListener;
//import java.beans.PropertyChangeEvent;
17 class SanElementListener<K> implements PropertyChangeListener
{
private K ID;
protected StorageCache<K, SanElement> cache;
/*Constructor*/
SanElementListener( StorageCache<K, SanElement> c, K id ) {
cache = c;
ID = id;
}
/**Check inheritation.*/
private static boolean isSubclass
( Class<?> superclass, Class<?> subclass ) {
try {
subclass.asSubclass( superclass );
} catch ( ClassCastException e ) {
return false;
}
return true;
}
/**Add all not added properties to cache.*/
final protected void addProperties( SanElement el ) {
//System.out.println( "\nAdd properties for "+el.getClass( ).getName( ) );
BeanInfo beanInfo = null;
try {
beanInfo = Introspector.getBeanInfo( el.getClass( ) );
} catch ( IntrospectionException ex ) {
ex.printStackTrace( );
return;
}
PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors( );
//boolean hasProperties = false;
for ( PropertyDescriptor desc : pds ) {
Method read = desc.getReadMethod( );
if ( read == null )
continue;
SanElement[] values = null;
try {
if ( isSubclass( SanElement.class, desc.getPropertyType( ) ) ) {
values = new SanElement[] {
( SanElement ) read.invoke( el ) };
} else if ( isSubclass( SanElement[].class,
desc.getPropertyType( ) ) ) {
values = ( SanElement[] ) read.invoke( el );
} else {
continue;
}
} catch ( IllegalArgumentException ex ) {
ex.printStackTrace( );
continue;
} catch ( IllegalAccessException ex ) {
ex.printStackTrace( );
continue;
} catch ( InvocationTargetException ex ) {
ex.printStackTrace( );
continue;
}
if ( values == null )
continue;
for ( SanElement val : values ) {
if( val == null ) {
// hasProperties = true;
continue;
}
K newID = cache.IDOf( val );
if( newID == cache.badID ) {
//новый или удаленный. Без listener'а
newID = cache.add( val );
val.addPropertyChangeListener(
new SanElementListener<K>( cache, newID ) );
addProperties( val );
}
}
}
}
/**Reaction for adding item to indexative property.
* Uses if property is other system element ( should have self listener ). */
final protected void
addElementProperty( String name, SanElement value )
{
System.out.print(
"Element "+ID+": add element property: "+name );
K newID = cache.IDOf( value );
if( newID == cache.badID ) {
//новый или удаленный. Без listener'а
newID = cache.add( value );
value.addPropertyChangeListener(
new SanElementListener<K>( cache, newID ) );
}
addProperties( value );
System.out.print( " ["+newID+"]" );
System.out.println( ": OK" );
}
/**Reaction for removing property from indexative property.
* Uses if property is other system element. */
final protected void
removeElementProperty( String name, SanElement value )
{
System.out.print(
"Element "+ID+": remove element property: "+name );
try {
cache.modify( ID );
}catch( UnknownIDException e ) {
throw new DebugException( e );
}
K id = cache.IDOf( value );
System.out.print( " ["+id+"]" );
System.out.println( ": OK" );
// Если Nest в данном случае явно не вызовет SanManager.remove( el ), то:
// Проверка на отсутствие связей, у данного элемента.
// Если так, то cache.remove( ID ) и удаляем listener ( this )
/*
try {
if( isReferenced( cache.get( ID ) ) ) {
cache.get( ID ).removePropertyChangeListener( this );
cache.removeByID( ID );
}
}catch( UnknownIDException e ){
throw new DebugException( );
}*/
// Если SanManager.remove( el ) вызывается явно, то
// НУЖНО ХРАНИТЬ listener в Cache !!!
// ...
}
/**Add all not added properties to cache.*/
final protected boolean isReferenced( SanElement el ) {
BeanInfo beanInfo = null;
try {
beanInfo = Introspector.getBeanInfo( el.getClass( ) );
} catch ( IntrospectionException ex ) {
ex.printStackTrace( );
//return;
}
PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors( );
boolean referenced = false;
for ( PropertyDescriptor desc : pds ) {
Method read = desc.getReadMethod( );
if ( read == null )
continue;
SanElement[] values = null;
try {
if ( isSubclass( SanElement.class, desc.getPropertyType( ) ) ) {
values = new SanElement[] {
( SanElement ) read.invoke( el ) };
} else if ( isSubclass( SanElement[].class,
desc.getPropertyType( ) ) ) {
values = ( SanElement[] ) read.invoke( el );
} else {
continue;
}
} catch ( IllegalArgumentException ex ) {
ex.printStackTrace( );
continue;
} catch ( IllegalAccessException ex ) {
ex.printStackTrace( );
continue;
} catch ( InvocationTargetException ex ) {
ex.printStackTrace( );
continue;
}
if( values == null )
continue;
else{
referenced = true;
break;
}
}
return referenced;
}
/**Reaction for changing property which id not reference to other
* system element.*/
final protected void modifyElementProperty( String name,
SanElement oldValue, SanElement newValue )
{
System.out.print(
"Element "+ID+": modifing element property: "+name );
try {
cache.modify( ID );
}catch( UnknownIDException e ) {
throw new DebugException( e );
}
K oldID = cache.IDOf( oldValue );
K newID = null;
if( newValue != null ) {
newID = cache.IDOf( newValue );
if( newID == cache.badID ) {
//новый или удаленный. Без listener'а
newID = cache.add( newValue );
newValue.addPropertyChangeListener(
new SanElementListener<K>( cache, newID ) );
}
addProperties( newValue );
}
System.out.print( " ["+oldID+"] -> ["+newID+"]" );
System.out.println( ": OK" );
}
/**Reaction for changing property which id not reference to other
* system element. */
final protected void modifySimpleProperty( String name )
{
System.out.print(
"Element "+ID+": modifing simple property: "+name );
try {
cache.modify( ID );
}catch( UnknownIDException e ) {
throw new DebugException( e );
}
System.out.println( ": OK" );
}
/**Reaction for changing property.*/
public void propertyChange( PropertyChangeEvent event )
{
if ( event instanceof PropertyValueAddEvent &&
( ( PropertyValueAddEvent )event ).getAddedValue( )
instanceof SanElement ) {
addElementProperty( event.getPropertyName( ),
SanElement.class.cast(
( ( PropertyValueAddEvent )event ).getAddedValue( ) ) );
} else if
( event instanceof PropertyValueRemoveEvent &&
( ( PropertyValueRemoveEvent )event ).getRemovedValue( )
instanceof SanElement ) {
removeElementProperty( event.getPropertyName( ),
SanElement.class.cast(
( ( PropertyValueRemoveEvent )event ).getRemovedValue( ) ) );
} else // if event is instance of PropertyChangeEvent
if( event.getNewValue( ) instanceof SanElement
|| event.getNewValue( ) == null )
modifyElementProperty( event.getPropertyName( ),
SanElement.class.cast( event.getOldValue( ) ),
SanElement.class.cast( event.getNewValue( ) ) );
else
modifySimpleProperty( event.getPropertyName( ) );
cache.print( );
}
}
package ru.petrsu.nest.ldap;
import java.io.*;
import java.util.*;
import ru.petrsu.nest.san.SanManager;
import ru.petrsu.nest.san.SAN;
import ru.petrsu.nest.san.SanElement;
/**Manages SAN-elements storage system.
* @author Alexander Nuikin*/
11 public abstract class SanStorageManager<K> extends SanManager {
protected SAN san = new SAN( );
protected Storage<K, SanElement> storage;
protected StorageCache<K, SanElement> cache;
/*================================================================*/
/*-------------- Specific SanStorageManger interface -------------*/
/*================================================================*/
public void finalize( )
throws StorageBadDisconnectionException {
storage.disconnect( );
}
/*================================================================*/
/*------------ Implementation of SanManger interface -------------*/
/*================================================================*/
/**Clear all element properties ( reset to default values, remove all
* references to and from other elements ) and then remove element
* from the store.*/
final public void clearElement( SanElement e ) {
super.clearElement( e );
}
/*Write all unsaved changes to the storage.*/
final public void commit( )
throws IOException {
cache.commit( );
}
/**Retrieves all available elements of the specified type.*/
final public <T extends SanElement>
Set<T> getElements( Class<T> type ) {
return cache.getElements( type );
}
/**Remove element from the store.*/
final public void removeElement( SanElement el ) {
//cache.get( ID ).removePropertyChangeListener( ??? );
cache.remove( el );
}
}
package ru.petrsu.nest.ldap;
/**"Подчиненный" кэш.
* Не контроллирует уникальнось ИД.*/
5 abstract class SlaveCache<K, T> extends StorageCache<K, T> {
protected MasterStorage<K, T> storage;
void setStorage( MasterStorage<K, T> s ) {
storage = s;
}
}
package ru.petrsu.nest.ldap;
import java.util.*;
/** Map realisation of internal cache for store system.
* @author Alexander Nuikin*/
7 class SlaveMapCache <K, T> extends SlaveCache <K, T> {
private Map<K, T> elements = new HashMap<K, T>( );
/*Constructor*/
SlaveMapCache( MasterStorage<K, T> s ) {
super( s );
}
/**Returns all elements by given type.*/
public <U extends T> Collection<U> getElements( Class<U> type ) {
return storage.getElements( type );
}
/**Returns element by given ID or null if no such element.*/
public T get( K ID ) {
T el = elements.get( ID );
if( el == null ) // element not loaded from storage yet
try {
el = storage.getNotReferenced( ID );
elements.put( ID, el );
storage.setReferences( ID, el );
}catch( UnknownIDException e ) {
System.err.println( "Unknown ID: "+ID.toString( ) );
}catch( IDNotRespectiveTypeException e ) {
System.err.println( e );
}
return el;
}
/**Add new element to cache and storage.
* Returns ID ( new unique ID ) of added element.*/
public K add( T el )
throws ElementExistException, NullPointerException {
if( elements.containsValue( el ) )
throw new ElementExistException( );
K ID = storage.add( el );
elements.put( ID, el );
return ID;
}
/**Removes element from system ( cache and storage ) by gived ID.*/
void removeByID( K ID )
throws UnknownIDException {
}
/**Removes element from system.*/
public void remove( T el )
throws UnknownElementException {
if( !elements.containsValue( el ) )
throw new UnknownElementException( );
K id = IDOf( el );
try{
storage.remove( id );
elements.remove( id );
}catch( UnknownIDException e ) {
System.err.println( "Unknown ID: "+id.toString( ) );
}
}
/**Returns true if given element loaded from storage.*/
boolean isLoadedElement( T el )
throws NullPointerException {
return elements.containsValue( el );
}
/**Returns true if element with gived ID exist in cache.*/
public boolean isLoadedByID( K ID )
throws NullPointerException {
return elements.containsKey( ID );
}
/**Returns true if element exist in cache.*/
public boolean isLoaded( T el )
throws NullPointerException {
return elements.containsValue( el );
}
/**Returns true if in system ( cache/storage ) exist element with given ID.*/
public boolean isID( K ID ) {
//return elements.containsKey( ID );
throw new RuntimeException( "Not realized yet." );
}
/**Returns ID of element or null if no element.*/
public K IDOf( T el ) {
K ID = null;
for( K id : elements.keySet( ) )
if( elements.get( id ) == el ) {
ID = id;
break;
}
return ID;
}
/**Returns count of ID.*/
public int countOfID( ) {
throw new RuntimeException( "Not realized yet." );
}
/**Returns count of loaded elements.*/
public int countOfLoaded( ) {
return elements.size( );
}
}
package ru.petrsu.nest.ldap;
import java.lang.*;
import java.util.*;
/**"Подчиненное" хранилище.
* Не контроллирует уникальность ИД элеметов.
* @author Alexander Nuikin*/
8 abstract class SlaveStorage<K, T> extends Storage<K, T> {
MasterCache<K, T> cache;
SlaveStorage( MasterCache<K, T> c )
throws StorageBadConnectionException {
cache = c;
cache.setStorage( this );
}
/**Add new element to storage with given ID.*/
abstract void add( K ID, T el )
throws NullPointerException;
}
package ru.petrsu.nest.ldap;
import java.lang.*;
import java.util.*;
/**External storage of elements.
* Интерфейс для внешенего ( относительно медленного ) хранилища элементов.
* Данный интерфейс должен использоваться только кэшем.
* Остальные подсистем должны использовать интерфейс кэша.
* Элементы - представители от общего суперкласса.
* Элементы связаны между собой посредством ссылок, которые храятся внутри
* самих объектов.
* Элементы в хранилище находятся под определенным ИД, уникальность которого
* контролирует само хранилище ( LDAP-дерево, база данных ).
*
* При использовании интерфейсов с четко определенными и продуманными методами
* ( их аргументами, возвращаующими значениями и спецификацией исключений ) будет
* проще тестировать классы как реализации интерфейсов. Кроме того, написанный
* однажды класс-драйвер практически не потребует изменений при тестировании
* новой реализации интерфейса.
* @author Alexander Nuikin*/
21 abstract class Storage<K, T> {
/**Returns all elements from storage.*/
abstract <U extends T>
Set<K> getElements( Class<U> type );
/**Returns element from storage by its ID.
* Cache references to other SAN-elements will not be set.*/
abstract T getNotReferenced( K ID )
throws UnknownIDException;
/**Set references to other elements for gived element.*/
abstract void setReferences( K ID, T el )
throws UnknownIDException, NullPointerException,
IDNotRespectiveTypeException;
/**Returns element from storage by its ID.
* Cache references to other elements will be set.*/
T get( K ID )
throws UnknownIDException {
return null;
}
/**Removes element from storage by gived ID.
* References will be not cleaned up.*/
abstract void remove( K ID )
throws UnknownIDException;
/**Modiry element at storage by givedi ID.
* Actually rewriting elemnt. */
abstract void modify( K ID )
throws UnknownIDException;
/**Returns true if in storage exist element with given ID.*/
abstract boolean isID( K ID );
/**Returns count of elements in storage.*/
int size( ) {
return -1;
}
/**Returns sorted set of all ID of elements in storage.*/
abstract Set<K> getAllID( );
/**Close connection with storage entity.
* For example with LDAP server, SQl sever.
* Before it connection should be opened in constructor of class,
* extends Storage.*/
abstract void disconnect( )
throws StorageBadDisconnectionException;
}
package ru.petrsu.nest.ldap;
import java.util.*;
/**Internal cache for store system.
* Кэш ( т.е. внутренний, быстрый ) элементов для хранилища.
* Кэш - прослойка между хранилищем и остальнами элементами системы хранения.
* Основная работа с хранилищем осуществляется через кэш ( за исключением
* специфичных для реализации хранилища функций ( scopesearch, например ) ).
* Аккумулирует в себе запрашиваемые элементы.
* В кэше элементы хранятся под своими ИД, соответствующими ИД в хранилище
* У кэша существует понятие "Множество ИД".
* При удалении элемента "удалятеся" и его ИД. После этого кэш должен говорить,
* что у него нет такого ИД, пока он не будет восстановлен ( сгенерирован ) во
* время добавления нового элемента.
* @author Alexander Nuikin*/
17 abstract class StorageCache <K, T> {
K badID;
K rootID; // SAN
K rootDeviceID;
/**Print out cache.*/
abstract void print( );
/**Initialization.*/
abstract void checkOut( )
throws NullPointerException;
/**Initialization.*/
abstract void commit( );
/**Returns all elements in cache.*/
abstract Set<T> getLoadedElements( );
/**Returns all elements from by given type.*/
abstract <U extends T> Set<U> getElements( Class<U> type );
/**Returns element from cache by given ID.
* Всегда либо вернет элемент, либо исключение об отсутствии заданного ИД.*/
abstract T get( K ID )
throws UnknownIDException;
/**Add new element to cache and storage.
* Returns ID ( new unique ID ) of added element.*/
abstract K add( T el )
throws ElementExistException, NullPointerException;
/**Removes element from system ( cache and storage ) by gived ID.*/
abstract void removeByID( K ID )
throws UnknownIDException;
/**Removes element from system ( cache and storage ) by gived ID.*/
abstract void remove( T el )
throws UnknownElementException, NullPointerException;
/**Marked element as modified.*/
abstract void modify( K ID )
throws UnknownIDException;
/**Returns true if given element loaded from storage.*/
abstract boolean isLoaded( T el )
throws NullPointerException;
/**Returns true if loaded from storage element with gived ID.*/
abstract boolean isLoadedByID( K ID )
throws UnknownIDException;
/**Returns true if in system ( cache/storage ) exist element with given ID.*/
abstract boolean isID( K ID );
/**Returns ID of element or -1 if no element.*/
abstract K IDOf( T el )
throws NullPointerException;
/**Returns count of ID.*/
abstract int countOfID( );
/**Returns count of loaded elements.*/
abstract int countOfLoaded( );
}
1 package ru.petrsu.nest.ldap;
/*Имена исключений нужно выбирать так, чтобы они несли максимум полезной
* информации о ситуации в которой исключение возникает.*/
/**Это исключение не следует использовать. Оно временно нужно только для
* корректной трансляции этого файла ( StorageException.java ).*/
8 class StorageException extends Exception{}
/*===========================================================================*/
/*------------------------------ Exceptions. --------------------------------*/
/*===========================================================================*/
/**Ошибка при подключении к серверу.*/
13 class StorageBadConnectionException extends Exception{
14 StorageBadConnectionException( String msg ) {
super( msg );
}
}
/**Ошибка при отсоединении от сервера.*/
19 class StorageBadDisconnectionException extends Exception{
20 StorageBadDisconnectionException( String msg ) {
super( msg );
}
}
/**Нет запрашиваемого ИД ( неизвестный ИД ).*/
25 class UnknownIDException extends Exception{
26 UnknownIDException( String ID ) {
super( "Unknown ID: "+ID );
}
}
/**По указанному ИД в хранилище находится элемент, по типу не соответствующий
* заданому элементу. Например запрашивается устройство с ИД=k, когда в
* хранилище с этим ИД находится здание.
* @author Alexander Nuikin*/
34 class IDNotRespectiveTypeException extends Exception{
35 IDNotRespectiveTypeException( Class<?> given, Class<?> stored ) {
super( "\nClass of given element: "+given.getName( )
+"\nClass of stored element: "+stored.getName( ) );
}
39 IDNotRespectiveTypeException( Class<?> given, String stored ) {
super( "\nClass of given element: "+given.getName( )
+"\nClass of stored element: "+stored );
}
}
/*===========================================================================*/
/*---------------------- Runtime exceptions. --------------------------------*/
/*===========================================================================*/
/**Исключение используемое для отладки.
* Говорит об ошибке разработчиков.*/
52 class DebugException extends RuntimeException {
53 DebugException( ) {
super( );
}
56 DebugException( Exception e ) {
super( e );
}
59 DebugException( String name ) {
super( name );
}
}
/**Некоторая ошибка специфичная для конкретной реализации хранилища.*/
64 class StorageInternalException extends RuntimeException {
65 StorageInternalException( Exception e ) {
super( e );
}
}
/**Нет данного элемента ( неизвестный элемент ).*/
70 class UnknownElementException extends IllegalArgumentException{}
/**Элемент уже существует в системе хранения.*/
72 class ElementExistException extends IllegalArgumentException{}
/**Beans: Неизвестное свойство.*/
74 class UnknownPropertyException extends RuntimeException {
75 UnknownPropertyException( Exception e ) {
super( e );
}
78 UnknownPropertyException( String name ) {
super( name );
}
81 UnknownPropertyException( String name, Exception e ) {
super( name, e );
}
}
package ru.petrsu.nest.ldap;
import java.lang.*;
import java.util.*;
import java.io.*;
/**Driver for testing all units in tested system.
* @author Alexander Nuikin*/
9 class SystemDriver {
private boolean stopOnBad;
/**Standard label for begining subtest of class.*/
12 private String label = ">>>>>> ";
/**Standard label for successfull finishing subtest of class.*/
14 private String goodLabel = "OK";
/**Standard label for unsuccessfull finishing subtest of class.*/
16 private String badLabel = "BAD";
private List<DriverClass> drivers = new ArrayList<DriverClass>( );
/**Add class driver.*/
public void add( DriverClass d ) {
drivers.add( d );
// проверка на вхождение двух драйверов одного класса
// ...
}
/**Stop testing after first bad finished test if stopOnBad is true.*/
public void setStopOnBad( boolean sob ) {
stopOnBad = sob;
}
/**Set label for output.*/
public void setLabel( String l ) {
label = l;
}
/**Test all classes.*/
public void test( ) {
int goodCount = 0;
System.out.println(
label+"Testing Begin: "+drivers.size( )+
" tested classes." );
for( int i=0; i<drivers.size( ); i++ ) {
System.out.println(
label+"["+i+"]"+" Testing "
+drivers.get( i ).getClass( ).getName( )+": Begin" );
if( drivers.get( i ).test( stopOnBad ) ) {
goodCount++;
System.out.println(
label+"["+i+"]"+" Testing "
+drivers.get( i ).getClass( ).getName( )+": "+goodLabel );
}
else if( stopOnBad ) {
System.out.println(
label+"["+i+"]"+" Testing "
+drivers.get( i ).getClass( ).getName( )+": "+badLabel );
break;
}
}
System.out.println(
label+"Testing results: "+goodCount+" / "+drivers.size( )+
" classes tests finished successfully." );
}
}
1 package ru.petrsu.nest.ldap;
/**Тестирование всех классов в системе.
* @author Alexander Nuikin*/
5 class SystemTest {
6 public static void main( String[] args ) {
SystemDriver sysd = new SystemDriver( );
sysd.add( new DriverLdapSanStorage( ) );
sysd.test( );
}
}
1 package ru.petrsu.nest.ldap;
import ru.petrsu.nest.san.NetworkInterface;
import ru.petrsu.nest.san.LinkInterface;
import ru.petrsu.nest.san.Building;
import ru.petrsu.nest.san.Room;
import ru.petrsu.nest.san.Floor;
import ru.petrsu.nest.san.SimpleOU;
import ru.petrsu.nest.san.CompositeOU;
import ru.petrsu.nest.san.Device;
import ru.petrsu.nest.san.Network;
import ru.petrsu.nest.san.IPNetwork;
import ru.petrsu.nest.san.SanElement;
import ru.petrsu.nest.san.EthernetInterface;
import ru.petrsu.nest.san.IPv4Interface;
import ru.petrsu.nest.san.References;
import ru.petrsu.nest.san.AbstractOU;
import ru.petrsu.nest.ldap.CacheReference;
import ru.petrsu.nest.ldap.Cache;
import java.io.IOException;
import java.util.Set;
import java.util.HashSet;
import java.util.Hashtable;
import java.lang.String;
import java.lang.Object;
import java.lang.Integer;
import java.lang.Exception;
import java.lang.StringBuffer;
import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.NamingEnumeration;
import javax.naming.NameAlreadyBoundException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.InitialDirContext;
/////////////////////////////////////////////////////
/**
* Class writeLdap - class for writing to LDAP server.
* it is temporary class.
*
* @author Artemev Dmitry;
*/
60 class writeLdap {
62 static private String mainHeadDN = Ldap.mainHeadDN;
63 static private String headDeviceDN = Ldap.headDeviceDN;
64 static private String headNetworkDN = Ldap.headNetworkDN;
65 static private String headLIDN = Ldap.headLIDN;
66 static private String headNIDN = Ldap.headNIDN;
67 static private String headAbstractOUDN = Ldap.headAbstractOUDN;
68 static private String headNetUnitDN = Ldap.headNetUnitDN;
69 static private String headSpaceUnitDN = Ldap.headSpaceUnitDN;
70 static private String headPersonDN = Ldap.headPersonDN;
71 static private String headHistoryDN = Ldap.headHistoryDN;
////////////////////////////////////////////////////////////
76 static void writeTest( )
throws WriteLdapException {
try {
Attributes attrs = new BasicAttributes( );
attrs.put( "ou", "writeTest" );
attrs.put( "objectClass", "organizationalUnit" );
Ldap.createLdapRecord( mainHeadDN, attrs );
}catch( Exception e ) {
System.out.println( "writeTest: ERROR." );
throw new WriteLdapException( e );
}
}
91 static void writeElement( int ID )
throws WriteLdapException {
try {
SanElement SE = Cache.get( ID );
if ( SE instanceof LinkInterface )
writeLinkInterface( ( LinkInterface )SE, ID );
else if ( SE instanceof NetworkInterface )
writeNetworkInterface( ( NetworkInterface )SE, ID );
else if ( SE instanceof Device )
writeDevice( ( Device )SE, ID );
else if ( SE instanceof Room )
writeRoom( ( Room )SE, ID );
else if ( SE instanceof Floor )
writeFloor( ( Floor )SE, ID );
else if ( SE instanceof SimpleOU )
writeSimpleOU( ( SimpleOU )SE, ID );
else if ( SE instanceof IPNetwork )
writeIPNetwork( ( IPNetwork )SE, ID );
else if ( SE instanceof Building )
writeBuilding( ( Building )SE, ID );
else if ( SE instanceof CompositeOU )
writeCompositeOU( ( CompositeOU )SE, ID );
}catch ( Exception e ) {
throw new WriteLdapException( e );
}
}
/**
*creates LI record in LDAP.
*/
133 static void writeLinkInterface( LinkInterface LI, int ID )
throws WriteLdapException {
try {
String name = LI.getName( );
String descr = LI.getDescription( );
String State = null;
String Mode = null;
StringBuilder DN = new StringBuilder( );
Attributes attrs = new BasicAttributes( );
attrs.put( "cn", ""+ID );
if ( name!=null )
attrs.put( "name", name );
if ( descr!=null && !descr.trim( ).isEmpty( ) )
attrs.put( "description", descr );
Device device = LI.getDevice( );
if ( device!=null ) {
DN.append( "cn=" );
DN.append( Cache.indexOf( device ) );
DN.append( ", " );
DN.append( headDeviceDN );
attrs.put( "deviceDN", DN.toString( ) );
}
LinkInterface link = LI.getLink( );
if ( link!=null ) {
DN.setLength( 0 );
DN.append( "cn=" );
DN.append( Cache.indexOf( link ) );
DN.append( ", " );
DN.append( headLIDN );
attrs.put( "link", DN.toString( ) );
}
NetworkInterface[] NI = LI.getNetworkInterfaces( );
if ( NI.length!=0 ) {
Attribute nifDN = new BasicAttribute( "nifDN" );
for ( int i=0; i<NI.length; i++ ) {
DN.setLength( 0 );;
DN.append( "cn=" );
DN.append( Cache.indexOf( NI[i] ) );
DN.append( ", " );
DN.append( headNIDN );
nifDN.add( DN.toString( ) );
}
if ( nifDN.size( )!=0 )
attrs.put( nifDN );
}
State = LI.getState( ).name( );
Mode = LI.getMode( ).name( );
if ( State!=null )
attrs.put( "state", State );
if ( Mode !=null )
attrs.put( "mode", Mode );
DN.setLength( 0 );
DN.append( "cn=" );
DN.append( ID );
DN.append( ", " );
DN.append( headLIDN );
if ( LI instanceof EthernetInterface ) {
StringBuilder MacString = new StringBuilder( 18 );
byte[] mac = ( ( EthernetInterface )LI ).getMACAddress( );
for ( int i=0; i<mac.length; i++ ) {
if ( i >0 )
MacString.append( ":" );
MacString.append( String.format( "%02X", mac[i] ) );
}
attrs.put( "macAddress", MacString.toString( ) );
attrs.put( "objectClass", "nestEthernetInterface" );
}
else attrs.put( "objectClass", "nestLinkInterface" );
Ldap.createLdapRecord( DN.toString( ), attrs );
}catch( Exception e ) {
System.out.println( "writeEthernetInterface: ERROR." );
throw new WriteLdapException( e );
}
}
/**
*creates NI record in LDAP.
*/
229 static void writeNetworkInterface( NetworkInterface NI, int ID )
throws WriteLdapException {
try {
String name = NI.getName( );
String descr = NI.getDescription( );
StringBuilder TCPIP = new StringBuilder( );
StringBuilder DN = new StringBuilder( );
int length;
Attributes attrs = new BasicAttributes( );
attrs.put( "cn", ""+ID );
if ( name!=null )
attrs.put( "name", name );
if ( descr!=null && !descr.trim( ).isEmpty( ) )
attrs.put( "description", descr );
byte[] address = NI.getInetAddress( );
InetAddress ia =null;
try {
ia = InetAddress.getByAddress( address );
TCPIP.append( ia.getHostAddress( ) );
attrs.put( "ipHostNumber", TCPIP.toString( ) );
}catch ( UnknownHostException e ){}
// if ( length!=0 ) {
// for ( int i=0; i<length-1; i++ ) {
// TCPIP.append( address[i] );
// TCPIP.append( "." );
// }
// TCPIP.append( address[length-1] );
// length = TCPIP.length( );
// TCPIP = TCPIP.substring( 0, length-1 );
// }
Network network = NI.getNetwork( );
if ( network!=null ) {
DN.append( "cn=" );
DN.append( Cache.indexOf( network ) );
DN.append( ", " );
DN.append( headNetworkDN );
attrs.put( "networkDN", DN.toString( ) );
}
DN.setLength( 0 );
LinkInterface LI = NI.getLinkInterface( );
if ( LI!=null ) {
DN.append( "cn=" );
DN.append( Cache.indexOf( LI ) );
DN.append( ", " );
DN.append( headLIDN );
attrs.put( "lifDN", DN.toString( ) );
}
if ( NI instanceof IPv4Interface )
attrs.put( "objectClass", "nestIPv4Interface" );
else attrs.put( "objectClass", "nestNetworkInterface" );
DN.setLength( 0 );
DN.append( "cn=" );
DN.append( ID );
DN.append( ", " );
DN.append( headNIDN );
System.out.println( "\n\n"+ attrs.toString( )+"\n\n" );
Ldap.createLdapRecord( DN.toString( ), attrs );
}catch( Exception e ) {
System.out.println( "writeNetworkInterface: ERROR." );
throw new WriteLdapException( e );
}
}
/**
*creates Network record with ipNetmaskNumber and ipNetworkNUmber
* in LDAP.
*/
311 static void writeIPNetwork( IPNetwork network, int ID )
throws WriteLdapException {
try {
String name = network.getName( );
String descr = network.getDescription( );
StringBuilder DN = new StringBuilder( );
StringBuilder TCPIP = new StringBuilder( );
int length=0;
Attributes attrs = new BasicAttributes( );
attrs.put( "cn", ""+ID );
if ( name!=null )
attrs.put( "name", name );
if ( descr!=null )
attrs.put( "description", descr );
byte[] mask = network.getMask( );
InetAddress ia = null;
try {
ia = InetAddress.getByAddress( mask );
TCPIP.append( ia.getHostAddress( ) );
attrs.put( "ipNetmaskNumber", TCPIP.toString( ) );
}catch ( UnknownHostException e ){}
byte[] address = network.getAddress( );
ia = null;
TCPIP.setLength( 0 );
try {
ia = InetAddress.getByAddress( address );
TCPIP.append( ia.getHostAddress( ) );
attrs.put( "ipNetworkNumber", TCPIP.toString( ) );
}catch ( UnknownHostException e ){}
Attribute nifDN = new BasicAttribute( "nifDN" );
NetworkInterface[] NI = network.getNetworkInterfaces( );
for ( int i=0; i<NI.length; i++ ) {
DN.setLength( 0 );
DN.append( "cn="+Cache.indexOf( NI[i] ) );
DN.append( ", "+headNIDN );
nifDN.add( DN.toString( ) );
}
if ( nifDN.size( )!=0 )
attrs.put( nifDN );
DN.setLength( 0 );
DN.append( "cn="+ID );
DN.append( ", "+headNetworkDN );
attrs.put( "objectClass", "nestNetwork" );
Ldap.createLdapRecord( DN.toString( ), attrs );
}catch( Exception e ) {
System.out.println( "writeNetwork: ERROR." );
throw new WriteLdapException( e );
}
}
/**
*creates Device record in LDAP.
*/
375 static void writeDevice( Device device, int ID )
throws WriteLdapException {
try {
String name = device.getName( );
String descr = device.getDescription( );
StringBuilder path = new StringBuilder( "cn="+ID );
StringBuilder DN = new StringBuilder( );
Attributes attrs = new BasicAttributes( );
attrs.put( "cn", ""+ID );
if ( name!=null )
attrs.put( "name", name );
if ( descr!=null )
attrs.put( "description", descr );
path.append( ", "+headDeviceDN );
Room room = device.getRoom( );
if ( room != null ) {
DN.append( "cn="+Cache.indexOf( room ) );
Floor floor = room.getFloor( );
DN.append( ", cn="+ Cache.indexOf( floor ) );
Building building = floor.getBuilding( );
DN.append( ", cn="+ Cache.indexOf( building )+", " );
DN.append( headSpaceUnitDN );
attrs.put( "roomDN", DN.toString( ) );
}
SimpleOU simple = device.getOU( );
if ( simple!=null ) {
DN.setLength( 0 );
DN.append( "cn="+Cache.indexOf( simple ) );
CompositeOU composite = simple.getParent( );
while ( composite!=null ) {
DN.append( ", cn="+Cache.indexOf( composite ) );
composite = composite.getParent( );
}
DN.append( ", "+headAbstractOUDN );
attrs.put( "ouDN", DN.toString( ) );
}
Attribute lifDN = new BasicAttribute( "lifDN" );
LinkInterface[] LI = device.getLinkInterfaces( );
for ( int i=0; i<LI.length; i++ ) {
DN.setLength( 0 );
DN.append( "cn="+ Cache.indexOf( LI[i] ) );
DN.append( ", "+headLIDN );
lifDN.add( DN.toString( ) );
}
if ( lifDN.size( ) !=0 )
attrs.put( lifDN );
Attribute objClass = new BasicAttribute( "objectClass" );
objClass.add( "nestDevice" );
attrs.put( objClass );
Ldap.createLdapRecord( path.toString( ), attrs );
}catch( Exception e ) {
System.out.println( "writeDevice: ERROR." );
throw new WriteLdapException( e );
}
}
/**
*creates Building record in LDAP.
*/
444 static void writeBuilding( Building building, int ID )
throws WriteLdapException {
try {
String street = building.getAddress( );
String buildingName = building.getName( );
String descr = building.getDescription( );
StringBuilder path = new StringBuilder( "cn=" );
Attributes attrs = new BasicAttributes( );
attrs.put( "cn", ""+ID );
if ( street != null )
attrs.put( "street", street );
if ( buildingName != null )
attrs.put( "buildingName", buildingName );
if ( descr != null )
attrs.put( "description", descr );
path.append( ID );
path.append( ", " );
path.append( headSpaceUnitDN );
attrs.put( "objectClass", "nestBuilding" );
Ldap.createLdapRecord( path.toString( ), attrs );
}catch( Exception e ) {
System.out.println( "writeBuilding: ERROR." );
throw new WriteLdapException( e );
}
}
/**
*creates Floor record in LDAP.
*/
480 static void writeFloor( Floor floor, int ID )
throws WriteLdapException {
try {
int bID = Cache.indexOf( floor.getBuilding( ) );
String descr = floor.getDescription( );
String name = floor.getName( );
StringBuilder path = new StringBuilder( "cn=" );
Attributes attrs = new BasicAttributes( );
attrs.put( "cn", ""+ID );
attrs.put( "floorNumber", ( ""+ floor.getNumber( ) ) );
if ( name!=null )
attrs.put( "name", name );
if ( descr != null )
attrs.put( "description", descr );
attrs.put( "objectClass", "nestFloor" );
path.append( ID );
path.append( ", cn=" );
path.append( bID );
path.append( ", " );
path.append( headSpaceUnitDN );
Ldap.createLdapRecord( path.toString( ), attrs );
}catch( Exception e ) {
System.out.println( "writeFloor: ERROR." );
throw new WriteLdapException( e );
}
}
/**
*creates Room record in LDAP.
*/
517 static void writeRoom( Room room, int ID )
throws WriteLdapException {
try {
String number = room.getNumber( );
String descr = room.getDescription( );
String name = room.getName( );
StringBuilder path = new StringBuilder( "cn=" );
StringBuilder someDN = new StringBuilder( );
Attributes attrs = new BasicAttributes( );
attrs.put( "cn", ""+ID );
Floor floor = room.getFloor( );
path.append( ID );
path.append( ", cn=" );
path.append( Cache.indexOf( floor ) );
path.append( ", cn=" );
path.append( Cache.indexOf( floor.getBuilding( ) ) );
path.append( ", " );
path.append( headSpaceUnitDN );
if ( number!=null )
attrs.put( "roomNumber", number );
if ( descr!=null )
attrs.put( "description", descr );
if ( name!=null )
attrs.put( "name", name );
Attribute attrDN = new BasicAttribute( "ouDN" );
SimpleOU[] simple = room.getOUs( );
CompositeOU composite=null;
for ( int i=0; i<simple.length; i++ ) {
someDN.setLength( 0 );
composite = simple[i].getParent( );
someDN.append( "cn=" );
someDN.append( Cache.indexOf( simple[i] ) );
someDN.append( ", " );
while ( composite != null ) {
someDN.append( "cn=" );
someDN.append( Cache.indexOf( composite ) );
someDN.append( ", " );
composite = composite.getParent( );
}
someDN.append( headAbstractOUDN );
attrDN.add( someDN.toString( ) );
}
if ( attrDN.size( )!=0 )
attrs.put( attrDN );
Device[] device = room.getDevices( );
attrDN = new BasicAttribute( "deviceDN" );
for ( int i=0; i<device.length; i++ ) {
someDN.setLength( 0 );
someDN.append( "cn=" );
someDN.append( Cache.indexOf( device[i] ) );
someDN.append( ", " );
someDN.append( headDeviceDN );
attrDN.add( someDN.toString( ) );
}
if ( attrDN.size( )!=0 )
attrs.put( attrDN );
attrs.put( "objectClass", "nestRoom" );
Ldap.createLdapRecord( path.toString( ), attrs );
}catch( Exception e ) {
System.out.println( "writeRoom: ERROR." );
throw new WriteLdapException( e );
}
}
/**
*creates CompositeOU record in LDAP.
*/
597 static void writeCompositeOU( CompositeOU composite, int ID )
throws WriteLdapException {
try {
String descr = composite.getDescription( );
String name = composite.getName( );
StringBuilder DN = new StringBuilder( "cn=" );
Attributes attrs = new BasicAttributes( );
attrs.put( "cn", ""+ID );
DN.append( ID );
DN.append( ", " );
if ( descr!=null )
attrs.put( "description", descr );
if ( name != null )
attrs.put( "ou", name );
while ( composite != null ) {
composite = composite.getParent( );
if ( composite != null ) {
DN.append( "cn=" );
DN.append( Cache.indexOf( composite ) );
DN.append( ", " );
}
}
DN.append( headAbstractOUDN );
attrs.put( "objectClass", "nestCompositeOU" );
Ldap.createLdapRecord( DN.toString( ), attrs );
}catch( Exception e ) {
System.out.println( "writeCompositeOU: ERROR." );
throw new WriteLdapException( e );
}
}
/**
*creates SimpleOU record in LDAP.
*/
640 static void writeSimpleOU( SimpleOU simple, int ID )
throws WriteLdapException {
try {
String name = simple.getName( );
String descr = simple.getDescription( );
StringBuilder DN = new StringBuilder( );
StringBuilder path = new StringBuilder( "cn="+ID );
Attributes attrs = new BasicAttributes( );
attrs.put( "cn", ""+ID );
if ( name!=null )
attrs.put( "ou", name );
if ( descr!=null )
attrs.put( "description", descr );
CompositeOU composite = simple.getParent( );
while ( composite!=null ) {
path.append( ", cn="+Cache.indexOf( composite ) );
composite = composite.getParent( );
}
path.append( ", " );
path.append( headAbstractOUDN );
Attribute someDN = new BasicAttribute( "roomDN" );
Room[] room = simple.getRooms( );
Floor floor = null;
Building building = null;
for ( int i=0; i<room.length; i++ ) {
DN.setLength( 0 );
DN.append( "cn=" );
DN.append( Cache.indexOf( room[i] ) );
floor = room[i].getFloor( );
DN.append( ", cn=" );
DN.append( Cache.indexOf( floor ) );
building = floor.getBuilding( );
DN.append( ", cn=" );
DN.append( Cache.indexOf( building ) );
DN.append( ", " );
DN.append( headSpaceUnitDN );
someDN.add( DN.toString( ) );
}
if ( someDN.size( )!=0 )
attrs.put( someDN );
someDN = new BasicAttribute( "deviceDN" );
Device[] device = simple.getDevices( );
for ( int i=0; i<device.length; i++ ) {
DN.setLength( 0 );
DN.append( "cn=" );
DN.append( Cache.indexOf( device[i] ) );
DN.append( ", " );
DN.append( headDeviceDN );
someDN.add( DN.toString( ) );
}
if ( someDN.size( )!=0 )
attrs.put( someDN );
attrs.put( "objectClass", "nestSimpleOU" );
Ldap.createLdapRecord( path.toString( ), attrs );
}catch( Exception e ) {
System.out.println( "writeSimpleOU: ERROR." );
throw new WriteLdapException( e );
}
}
/**
*creates All head records for elements in LDAP.
*/
715 static void writeHeadDNs( )
throws WriteLdapException {
try {
Attributes attrs = new BasicAttributes( );
try{
attrs.put( "cn", "AbstractOU" );
attrs.put( "description", "Head DN for Administration unit" );
attrs.put( "objectClass", "nestAbstractOU" );
Ldap.createLdapRecord( headAbstractOUDN, attrs );
} catch( NameAlreadyBoundException e ) {}
try {
attrs = new BasicAttributes( );
attrs.put( "cn", "People" );
attrs.put( "description", "Head DN for people" );
attrs.put( "objectClass", "nestPeople" );
Ldap.createLdapRecord( headPersonDN, attrs );
} catch( NameAlreadyBoundException e ) {}
try {
attrs = new BasicAttributes( );
attrs.put( "cn", "SpaceUnit" );
attrs.put( "description", "Head DN for Space Unit" );
attrs.put( "objectClass", "nestSpaceUnit" );
Ldap.createLdapRecord( headSpaceUnitDN, attrs );
} catch( NameAlreadyBoundException e ) {}
try {
attrs = new BasicAttributes( );
attrs.put( "cn", "NetUnit" );
attrs.put( "description", "Head DN for Net Unit" );
attrs.put( "objectClass", "nestNetUnit" );
Ldap.createLdapRecord( headNetUnitDN, attrs );
} catch( NameAlreadyBoundException e ) {}
try {
attrs = new BasicAttributes( );
attrs.put( "cn", "History" );
attrs.put( "description", "Head DN for tranzaction History" );
attrs.put( "objectClass", "nestHistory" );
Ldap.createLdapRecord( headHistoryDN, attrs );
} catch( NameAlreadyBoundException e ) {}
try {
attrs = new BasicAttributes( );
attrs.put( "cn", "Devices" );
attrs.put( "description", "Head DN for all devices" );
attrs.put( "objectClass", "nestNetUnit" );
Ldap.createLdapRecord( headDeviceDN, attrs );
} catch( NameAlreadyBoundException e ) {}
try{
attrs = new BasicAttributes( );
attrs.put( "cn", "Networks" );
attrs.put( "description", "Head DN for all networks" );
attrs.put( "objectClass", "nestNetUnit" );
Ldap.createLdapRecord( headNetworkDN, attrs );
} catch( NameAlreadyBoundException e ) {}
try {
attrs = new BasicAttributes( );
attrs.put( "cn", "Link Interfaces" );
attrs.put( "description", "Head DN for all link interfaces" );
attrs.put( "objectClass", "nestNetUnit" );
Ldap.createLdapRecord( headLIDN, attrs );
} catch( NameAlreadyBoundException e ) {}
try {
attrs = new BasicAttributes( );
attrs.put( "cn", "Network Interfaces" );
attrs.put( "description", "Head DN for all network interfaces" );
attrs.put( "objectClass", "nestNetUnit" );
Ldap.createLdapRecord( headNIDN, attrs );
} catch( NameAlreadyBoundException e ) {}
}catch( LdapException e ) {
System.out.println
( "writeLdap, writeHeadDNs: problems in writing headDNs" );
throw new WriteLdapException( e );
}
}
}
806 class WriteLdapException extends Exception {
807 WriteLdapException( Exception e ) {
super ( e );
}
}