Credentials Data Methods

import java.net.URL;
import java.rmi.RemoteException;

import org.apache.axis2.transport.http.HTTPConstants;
import org.apache.axis2.transport.http.HttpTransportProperties;

import com.hp.schemas.ucmdb._1.types.BytesProp;
import com.hp.schemas.ucmdb._1.types.BytesProps;
import com.hp.schemas.ucmdb._1.types.CIProperties;
import com.hp.schemas.ucmdb._1.types.CmdbContext;
import com.hp.schemas.ucmdb._1.types.StrProp;
import com.hp.schemas.ucmdb._1.types.StrProps;
import com.hp.schemas.ucmdb.discovery._1.params.AddCredentialsEntryRequest;
import com.hp.schemas.ucmdb.discovery._1.params.AddCredentialsEntryRequestDocument;
import com.hp.schemas.ucmdb.discovery._1.params.AddCredentialsEntryResponse;
import com.hp.schemas.ucmdb.discovery._1.params.AddCredentialsEntryResponseDocument;
import com.hp.schemas.ucmdb.discovery._1.params.GetCredentialsEntriesIDsRequest;
import com.hp.schemas.ucmdb.discovery._1.params.GetCredentialsEntriesIDsRequestDocument;
import com.hp.schemas.ucmdb.discovery._1.params.GetCredentialsEntriesIDsResponse;
import com.hp.schemas.ucmdb.discovery._1.params.GetCredentialsEntriesIDsResponseDocument;
import com.hp.schemas.ucmdb.discovery._1.params.GetCredentialsEntryRequest;
import com.hp.schemas.ucmdb.discovery._1.params.RemoveCredentialsEntryRequest;
import com.hp.schemas.ucmdb.discovery._1.params.RemoveCredentialsEntryRequestDocument;
import com.hp.schemas.ucmdb.discovery._1.params.UpdateCredentialsEntryRequest;
import com.hp.schemas.ucmdb.discovery._1.params.UpdateCredentialsEntryRequestDocument;
import com.hp.schemas.ucmdb.discovery._1.params.UpdateProbeScopeRequest;
import com.hp.schemas.ucmdb.discovery._1.params.UpdateProbeScopeRequestDocument;
import com.hp.ucmdb.generated.DiscoveryServiceStub;
import com.hp.ucmdb.generated.UcmdbFault;


public class TestCredentials{
	static final String HOST_NAME = "<my_hostname>";
    static final int PORT = 8080;
    private static final String PROTOCOL = "http";
    private static final String FILE = "/axis2/services/DiscoveryService";
    private static final String PASSWORD = "admin";
    private static final String USERNAME = "admin";
    public static CmdbContext cmdbContext = CmdbContext.Factory.newInstance();

    public static DiscoveryServiceStub getService() {

        DiscoveryServiceStub serviceStub=null;
        try {

            URL url = new URL(PROTOCOL, HOST_NAME, PORT, FILE);
            serviceStub = new DiscoveryServiceStub(url.toString());

            HttpTransportProperties.Authenticator auth = new HttpTransportProperties.Authenticator();
            auth.setUsername(USERNAME);
            auth.setPassword(PASSWORD);
            serviceStub._getServiceClient().getOptions()
                    .setProperty(HTTPConstants.AUTHENTICATE, auth);

        } catch (Exception e) {
            e.printStackTrace();

        }

        return serviceStub;
    }
	
	public static void addCreds(String user, String userlabel, String domain, String ipaddress,String protocol, String password) {

        DiscoveryServiceStub serviceStub = getService();
        cmdbContext.setCallerApplication("addCred");
        CIProperties newCredsProperties = CIProperties.Factory.newInstance();

        String [] args = {user,userlabel,domain,ipaddress};


        newCredsProperties.setStrProps(strAddProps(args));


        newCredsProperties.setBytesProps(addByteProp(password));


        AddCredentialsEntryRequest addReq = AddCredentialsEntryRequest.Factory
                .newInstance();
        AddCredentialsEntryRequestDocument addReqDoc = AddCredentialsEntryRequestDocument.Factory
                .newInstance();

        addReq.setCmdbContext(cmdbContext);

        addReq.setProtocolName(protocol);
        addReq.setDomainName("DefaultDomain");
        addReq.setCredentialsEntryParameters(newCredsProperties);
        addReqDoc.setAddCredentialsEntryRequest(addReq);

        try {
            AddCredentialsEntryResponseDocument  addResDoc = serviceStub.addCredentialsEntry(addReqDoc);
            AddCredentialsEntryResponse addRes = addResDoc.getAddCredentialsEntryResponse();
            System.out.println(addRes.getCredentialsEntryID().toString());
        } catch (RemoteException e) {

            e.printStackTrace();
        } catch (Exception e) {

            e.printStackTrace();
        }

    }

    public static void updateCreds(String user, String userlabel, String domain, String ipaddress,String protocol, String password,
                                   String credid) {

        DiscoveryServiceStub serviceStub = getService();


        //set cmdbcontext
        cmdbContext.setCallerApplication("addCred");
        // create CI properties
        CIProperties newCredsProperties = CIProperties.Factory.newInstance();


        String [] args = {user,userlabel,domain,ipaddress,credid};

        newCredsProperties.setBytesProps(addByteProp(password));
        newCredsProperties.setStrProps(strAddProps(args));

        UpdateCredentialsEntryRequestDocument upReqDoc = UpdateCredentialsEntryRequestDocument.Factory
                .newInstance();
        UpdateCredentialsEntryRequest upReq = UpdateCredentialsEntryRequest.Factory
                .newInstance();

        upReq.setDomainName("DefaultDomain");
        upReq.setProtocolName("ntadminprotocol");
        upReq.setCmdbContext(cmdbContext);
        upReq.setCredentialsEntryID(credid);
        upReq.setCredentialsEntryParameters(newCredsProperties);

        upReqDoc.setUpdateCredentialsEntryRequest(upReq);

        try {
            serviceStub.updateCredentialsEntry(upReqDoc);
        } catch (RemoteException e) {

            e.printStackTrace();
        } catch (Exception e) {

            e.printStackTrace();
        }

    }



    public static StrProps strAddProps(String args[]) {
        StrProps sP = StrProps.Factory.newInstance();

        if (args.length ==5){
            StrProp proAddress = sP.addNewStrProp();
            proAddress.setName("cm_credential_id");
            proAddress.setValue(args[4]);
        }

        StrProp username = sP.addNewStrProp();
        username.setName("protocol_username");
        username.setValue(args[0]);

        StrProp userLabel = sP.addNewStrProp();
        userLabel.setName("user_label");
        userLabel.setValue(args[1]);

        StrProp proAddress = sP.addNewStrProp();
        proAddress.setName("protocol_netaddress");
        proAddress.setValue(args[3]);

        StrProp proDomain = sP.addNewStrProp();
        proDomain.setName("ntadminprotocol_ntdomain");
        proDomain.setValue(args[2]);





        return sP;

    }

    public static void getCreds(String user, String userlabel, String domain, String ipaddress,String protocol, String password) {

        DiscoveryServiceStub serviceStub = getService();
        cmdbContext.setCallerApplication("getCred");
        CIProperties newCredsProperties = CIProperties.Factory.newInstance();

        GetCredentialsEntryRequest addReq = GetCredentialsEntryRequest.Factory
                .newInstance();
        GetCredentialsEntriesIDsRequest getIDReq = GetCredentialsEntriesIDsRequest.Factory.newInstance();
        GetCredentialsEntriesIDsRequestDocument getIDReqDoc = GetCredentialsEntriesIDsRequestDocument.Factory.newInstance();

        addReq.setCmdbContext(cmdbContext);
        getIDReq.setCmdbContext(cmdbContext);

        addReq.setProtocolName(protocol);
        getIDReq.setProtocolName(protocol);

        addReq.setDomainName("DefaultDomain");
        getIDReq.setDomainName("DefaultDomain");

        getIDReqDoc.setGetCredentialsEntriesIDsRequest(getIDReq);

        try {
            GetCredentialsEntriesIDsResponseDocument getCredentialsEntriesIDsResponseDocument = serviceStub.getCredentialsEntriesIDs(getIDReqDoc);
            GetCredentialsEntriesIDsResponse getCredentialsEntriesIDsResponse =  getCredentialsEntriesIDsResponseDocument.getGetCredentialsEntriesIDsResponse();
            for (String id : getCredentialsEntriesIDsResponse.getCredentialsEntryIDs().getStrValueArray())  {
                System.out.println(id);
            }

        } catch (RemoteException e) {

            e.printStackTrace();
        } catch (Exception e) {

            e.printStackTrace();
        }

    }


    public static BytesProps addByteProp(String password){


        byte[] baytdizi = password.getBytes();

        BytesProps bProps = BytesProps.Factory.newInstance();
        BytesProp bprop =bProps.addNewBytesProp();

        bprop.setName("protocol_password");
        bprop.setValue(baytdizi);
        return bProps;

    }
    
    public static void removeCredentialsEntry(String domainName,String protocolName,String credentialsEntryID) throws RemoteException, UcmdbFault {
    	 DiscoveryServiceStub serviceStub = getService();
		 cmdbContext.setCallerApplication("RemoveCredentialsEntry");
		 RemoveCredentialsEntryRequest removeCredentialsEntryRequest = RemoveCredentialsEntryRequest.Factory.newInstance();
		 removeCredentialsEntryRequest.setCmdbContext(cmdbContext);
		 removeCredentialsEntryRequest.setDomainName(domainName);
		 removeCredentialsEntryRequest.setProtocolName(protocolName);
		 removeCredentialsEntryRequest.setCredentialsEntryID(credentialsEntryID);
		 RemoveCredentialsEntryRequestDocument removeCredentialsEntryRequestDocument = RemoveCredentialsEntryRequestDocument.Factory.newInstance();
		 removeCredentialsEntryRequestDocument.setRemoveCredentialsEntryRequest(removeCredentialsEntryRequest);
		 serviceStub.removeCredentialsEntry(removeCredentialsEntryRequestDocument);
    }
    public static void main(String[] args) throws Exception {
        getCreds("melo","userlabelll","DefaultDomain","11.11.11.11","ntadminprotocol","passw");
      //addCreds("melo","userlabelll","DefaultDomain","11.11.11.11","ntadminprotocol","passw");
        //100_1_CMS
       //updateCreds( "meli", "newlabel","DefaultDomain", "22.22.22.22", "ntadminprotocol", "passss","32_1_CMS");
        removeCredentialsEntry("DefaultDomain","ntadminprotocol","");




    }

}