Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2013-2015 Dell, Inc ==================================================================== Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ====================================================================
 
 
 package org.dasein.cloud.azure.network;
 
 
 
 import  javax.annotation.Nonnull;
 import  javax.annotation.Nullable;
 
 
 public class AzureVPNSupport implements VPNSupport {	
     static private final Logger logger = Azure.getLogger(AzureVPNSupport.class);
 
 	static private final String NETWORKING_SERVICES = "/services/networking";
Azure gateway, vlan does not have an Id. Therefore,gateway id would be local network name + _&_ + gateway endpoint
 
 	static private final String OBJECT_NAME_SPLIT = "_&_";
 	static public final String VPN_ID_KEY = "VPNID";
The enstratus default VPN is used when create VLANs, subnet, local network and gateway as all of the operations on VLAN, subnet etc requires an VPN;
 
 	static public final String ENSTRATUS_DEFAULT_VPN = "Enstratus_VPN";
 	
     private Azure provider;
 
     public AzureVPNSupport(Azure provider) { this. = provider; }
 	
 
 	public String[] mapServiceAction(ServiceAction action) {
 		return new String[0];
 	}

Vlan was created when the VPN created
 
 	public void attachToVLAN(String providerVpnIdString providerVlanIdthrows CloudExceptionInternalException {
 		// TODO Auto-generated method stub
 
 	}
 
 	public void connectToGateway(String providerVpnIdString toGatewayIdthrows CloudExceptionInternalException {
         if.isTraceEnabled() ) {
             .trace("ENTER: " + AzureVPNSupport.class.getName() + ".connectToGateway()");
         }
         try {
             ProviderContext ctx = .getContext();
 
             ifctx == null ) {
                 throw new AzureConfigException("No context was specified for this request");
             }
            
             
             AzureMethod method = new AzureMethod();
                   
            String resourceDir =  + "/" + providerVpnId + "/gateway";
           
            method.post(ctx.getAccountNumber(), resourceDirnull);
     
        }
        finally {
            if.isTraceEnabled() ) {
                .trace("EXIT: " + AzureVPNSupport.class.getName() + ".connectToGateway()");
            }
        }
	}
	public VPN createVPN(String inProviderDataCenterIdString name,String descriptionVPNProtocol protocolthrows CloudExceptionInternalException {
        if.isTraceEnabled() ) {
            .trace("ENTER: " + AzureVPNSupport.class.getName() + ".createVPN()");
        }
        
        //Require a default cidr
        String cidr = "10.0.0.0/8";
        
        try {        	
            ProviderContext ctx = .getContext();
            ifctx == null ) {
                throw new AzureConfigException("No context was specified for this request");
            }
                       
            AzureMethod method = new AzureMethod();
            StringBuilder xml = new StringBuilder();
           
            xml.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");             
            xml.append("<NetworkConfiguration xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns=\"http://schemas.microsoft.com/ServiceHosting/2011/07/NetworkConfiguration\">");
            xml.append("<VirtualNetworkConfiguration>");
            xml.append("<VirtualNetworkSites>");
            xml.append("VirtualNetworkSite name=\"" + name"\" Location=\"" +  ctx.getRegionId() +"\">");
            xml.append("<AddressSpace>");
            xml.append("<AddressPrefix>"cidr +"</AddressPrefix>");
            xml.append("</AddressSpace>");
            xml.append("</VirtualNetworkSite>");
            xml.append("</VirtualNetworkSites>");                        
            xml.append("</VirtualNetworkConfiguration>");
            xml.append("</NetworkConfiguration>");
            if.isDebugEnabled() ) {
                try {
                    method.parseResponse(xml.toString(), false);
                }
                catchException e ) {
                    .warn("Unable to parse outgoing XML locally: " + e.getMessage());
                    .warn("XML:");
                    .warn(xml.toString());
                }
            }
            String resourceDir =  + "/media";
            method.post(ctx.getAccountNumber(),resourceDirxml.toString());
            // TODO: return VLAN
            return null;
        }
        finally {
            if.isTraceEnabled() ) {
                .trace("EXIT: " + AzureVlanSupport.class.getName() + ".createVlan()");
            }
        }
	}
	private String getAffinityGroup(String vlanName){
		//TODO
		return "Group1";
	}
	// Create local network for connecting VPN?
	public VPNGateway createVPNGateway(String endpointString name,String descriptionVPNProtocol protocolString bgpAsn)throws CloudExceptionInternalException {
        if.isTraceEnabled() ) {
            .trace("ENTER: " + AzureVPNSupport.class.getName() + ".createVPNGateway()");
        }
        try {
            ProviderContext ctx = .getContext();
            ifctx == null ) {
                throw new AzureConfigException("No context was specified for this request");
            }
           
            
            AzureMethod method = new AzureMethod();
            StringBuilder xml = new StringBuilder();
            xml.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
            xml.append("<NetworkConfiguration  xmlns=\"http://schemas.microsoft.com/windowsazure\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">");
            xml.append("<VirtualNetworkConfiguration>");            
            xml.append("<LocalNetworkSites>");
            
            xml.append("<LocalNetworkSite name=\"" + name +  "\">");
            xml.append("<AddressSpace>");
            xml.append("<AddressPrefix>"endpoint +"/32" + "</AddressPrefix>");
            xml.append("</AddressSpace>");
            xml.append("<VPNGatewayAddress>"endpoint + "</VPNGatewayAddress>");
            xml.append("</LocalNetworkSite>");
            
            xml.append("</LocalNetworkSites>");
            xml.append("</VirtualNetworkConfiguration>");
            xml.append("</NetworkConfiguration>");
               
            if.isDebugEnabled() ) {
                try {
                    method.parseResponse(xml.toString(), false);
                }
                catchException e ) {
                    .warn("Unable to parse outgoing XML locally: " + e.getMessage());
                    .warn("XML:");
                    .warn(xml.toString());
                }
            }
            String resourceDir =  + "/media";
            method.post(ctx.getAccountNumber(),resourceDir , xml.toString());
            // TODO: VPNGateway
            return null;
        }
        finally {
            if.isTraceEnabled() ) {
                .trace("EXIT: " + AzureVPNSupport.class.getName() + ".createVPNGateway()");
            }
        }
	}
	public void deleteVPN(String providerVpnIdthrows CloudExceptionInternalException {
		// TODO Auto-generated method stub
	}
	public void deleteVPNGateway(String providerVPNGatewayIdthrows CloudExceptionInternalException {
        if.isTraceEnabled() ) {
            .trace("ENTER: " + AzureVPNSupport.class.getName() + ".deleteVPNGateway(" + providerVPNGatewayId+")");
        }
        try {
            ProviderContext ctx = .getContext();
            ifctx == null ) {
                throw new AzureConfigException("No context was specified for this request");
            }
            
            if(!providerVPNGatewayId.contains()){
            	.trace(" The GateWay ID does not have the patern ->" +  );
            	return;
            }
           	String[] VPNAndGatewayName = providerVPNGatewayId.split();
         	String resourceDir =  + "/" + VPNAndGatewayName[0] + "/gateway" ;
         	                                
            AzureMethod method = new AzureMethod();
    
            method.invoke("DELETE",ctx.getAccountNumber(), resourceDirnull);
        }
        finally {
            if.isTraceEnabled() ) {
                .trace("EXIT: " + AzureDisk.class.getName() + ".deattach()");
            }
        }
	}
	public void detachFromVLAN(String providerVpnIdString providerVlanIdthrows CloudExceptionInternalException {
		// TODO Auto-generated method stub
	}
	public void disconnectFromGateway(String providerVpnIdString fromGatewayIdthrows CloudExceptionInternalException {
		// TODO Auto-generated method stub
	}
    private transient volatile AzureVPNCapabilities capabilities;
    @Nonnull
    @Override
        if == null ) {
             = new AzureVPNCapabilities();
        }
        return ;
    }
    @Override
		if(list != null){ 
			for(VPNGateway gatewaylist){
				if(gateway.getProviderVpnGatewayId().equals(gatewayId)){
					return gateway;
				}			
			}
		}
		return null;		
	}
	public VPN getVPN(String providerVpnIdthrows CloudException,InternalException {
		if(list != null){ 
			for(VPN vpnlist){
				if(vpn.getProviderVpnId().equals(providerVpnId)){
					return vpn;
				}			
			}
		}
		return null;		
	}
		// TODO Auto-generated method stub
		return null;
	}
		// TODO Auto-generated method stub
		return null;
	}
    @Nonnull
    @Override
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }
    @Override
	    ArrayList<VPNGatewaygateways = new ArrayList<VPNGateway>();
		if(list != null){ 
			for(VPN vpnlist){
				 if(items != null){
					 gateways.addAll(items);
				 }
			}
		}		
		return gateways;
	}
		// TODO Auto-generated method stub
        ProviderContext ctx = .getContext();
        ifctx == null ) {
            throw new AzureConfigException("No context was specified for this request");
        }
        String resourceDir =  + "/" + vpnId + "/gateway";
        AzureMethod method = new AzureMethod();
        Document doc = method.getAsXML(ctx.getAccountNumber(), resourceDir);
                
        NodeList entries = doc.getElementsByTagName("Gateway");
        ArrayList<VPNGatewaylist = new ArrayList<VPNGateway>();
        forint i=0; i<entries.getLength(); i++ ) {
            Node entry = entries.item(i);
            VPNGateway gatewaytoVPNGateway(ctxentry,vpnId );
            
            ifgateway != null ) {
            	list.add(gateway);
            }
        }        
        return list;
	}
		// TODO Auto-generated method stub
		return null;
	}
		// TODO Auto-generated method stub
		return null;
	}
    @Nonnull
    @Override
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }
    @Override
        ProviderContext ctx = .getContext();
        ifctx == null ) {
            throw new AzureConfigException("No context was specified for this request");
        }
        AzureMethod method = new AzureMethod();
        Document doc = method.getAsXML(ctx.getAccountNumber(), +"/virtualnetwork");
                
        NodeList entries = doc.getElementsByTagName("VirtualNetworkSite");
        ArrayList<VPNlist = new ArrayList<VPN>();
        forint i=0; i<entries.getLength(); i++ ) {
            Node entry = entries.item(i);
            VPN vpn = toVPN(ctxentry);
            ifvpn != null ) {
            	list.add(vpn);
            }
        }        
        return list;
	}
		// TODO Auto-generated method stub
		return null;
	}
	public boolean isSubscribed() throws CloudExceptionInternalException {
		return true;
	}
    private @Nullable VPN toVPN(@Nonnull ProviderContext ctx, @Nullable Node entrythrows CloudExceptionInternalException {
        ifentry == null ) {
            return null;
        }
        VPN vpnnew VPN();
        vpn.setCurrentState(.);
        HashMap<String,Stringtags = new HashMap<StringString>();
      
        NodeList attributes = entry.getChildNodes();
        forint i=0; i<attributes.getLength(); i++ ) {
            Node attribute = attributes.item(i);
            if(attribute.getNodeType() == .continue;
            String nodeName = attribute.getNodeName();
            
            ifnodeName.equalsIgnoreCase("name") && attribute.hasChildNodes() ) {
            	vpn.setProviderVpnId(attribute.getFirstChild().getNodeValue().trim());
            	//vpn.setName(attribute.getFirstChild().getNodeValue().trim());
            }
            else ifnodeName.equalsIgnoreCase("label") && attribute.hasChildNodes() ) {
            	vpn.setDescription(attribute.getFirstChild().getNodeValue().trim());
            }
            else ifnodeName.equalsIgnoreCase("Id") && attribute.hasChildNodes() ) {
            	//vpn.setProviderVpnId(attribute.getFirstChild().getNodeValue().trim());
            }
            else ifnodeName.equalsIgnoreCase("State") && attribute.hasChildNodes() ) {
            	String status = attribute.getFirstChild().getNodeValue().trim();
            	
            	if("Created".equalsIgnoreCase(status)){
            		vpn.setCurrentState(.);
            	}        
            }            
            else ifnodeName.equalsIgnoreCase("AffinityGroup") && attribute.hasChildNodes() ) {
            	tags.put("AffinityGroup"attribute.getFirstChild().getNodeValue().trim());
            }
            else ifnodeName.equalsIgnoreCase("AddressSpace") && attribute.hasChildNodes() ) {
                NodeList addressSpaces = attribute.getChildNodes();
                
                forint k=0; k<addressSpaces.getLength(); k++ ) {
                    Node addressSpace = addressSpaces.item(k);
                    
                    ifaddressSpace.getNodeName().equalsIgnoreCase("AddressPrefixes") && addressSpace.hasChildNodes() ) {
                       
                    	NodeList addressPrefixes  = addressSpace.getChildNodes();
                    	
                    	ArrayList<StringvlanIds = new ArrayList<String>();
                    	
                        forint l=0; l<addressPrefixes.getLength(); l++ ) {
                            Node addressPrefix = addressPrefixes.item(l);                               
                            ifaddressPrefix.getNodeName().equalsIgnoreCase("AddressPrefix") && addressPrefix.hasChildNodes() ) {
                            	vlanIds.add(addressPrefix.getFirstChild().getNodeValue().trim());
                            }                            
                        }
                        vpn.setProviderVlanIds(vlanIds.toArray(new String[vlanIds.size()]));
                        
                    }
                }
            }
        }
        ifvpn.getProviderVpnId() == null ) {
            return null;
        }
        ifvpn.getName() == null ) {
        	vpn.setName(vpn.getProviderVpnId());
        }
        ifvpn.getDescription() == null ) {
        	vpn.setDescription(vpn.getName());
        }       
        vpn.setTags(tags);
        
        return vpn;
    }
    
    private @Nullable VPNGateway toVPNGateway(@Nonnull ProviderContext ctx, @Nullable Node entryString vpnIdthrows CloudExceptionInternalException {
        ifentry == null ) {
            return null;
        }      
        VPNGateway gatewaynew VPNGateway();       
     
        gateway.setCurrentState(.);
        HashMap<String,Stringtags = new HashMap<StringString>();
        tags.put(vpnId);
        NodeList attributes = entry.getChildNodes();
       
        forint i=0; i<attributes.getLength(); i++ ) {
            Node attribute = attributes.item(i);
            if(attribute.getNodeType() == .continue;
            String nodeName = attribute.getNodeName();
            
            ifnodeName.equalsIgnoreCase("State") && attribute.hasChildNodes() ) { 
            	String status = attribute.getFirstChild().getNodeValue().trim();
            	
            	if("Provisioning".equalsIgnoreCase(status) || "Provisioned".equalsIgnoreCase(status)){
            		gateway.setCurrentState(.);
            	}
            	else if("Deprovisioning".equalsIgnoreCase(status)){
            		gateway.setCurrentState(.);
            	}
            	else if("NotProvisioned".equalsIgnoreCase(status)){
            		gateway.setCurrentState(.);
            	}
            }           
            else ifnodeName.equalsIgnoreCase("VIPAddress") && attribute.hasChildNodes() ) {
            	gateway.setEndpoint(attribute.getFirstChild().getNodeValue().trim());            	
            }
        }
        ifvpnId == null || gateway.getEndpoint() == null ) {        	
        	return null;
        }else{
        	gateway.setProviderVpnGatewayId(vpnId +  + gateway.getEndpoint());
        	
        }
        ifgateway.getName() == null ) {
        	gateway.setName(gateway.getEndpoint());
        }
        ifgateway.getDescription() == null ) {
        	gateway.setDescription(gateway.getName());
        }       
        gateway.setTags(tags);
        
        return gateway;
    }	
New to GrepCode? Check out our FAQ X