I am trying to create jenkins views (ListView) under a given parent view. I am able to create the view at the top level, but not able to find a way to create a view under a given parent view. I am using groovy to achieve this.Below is the use case:
1. I am using Nested View plugin to organize the jobs.
2. Leaf level views are the Jenkins ListView and all the jobs are placed under leaf level views.

In order to automate the view creation process, I need ability to create views at a given level (or under a given parent view) in a view hierarchy. I am able to create the views, but it always gets created at as the top level view. Below is the script i am working on for the same. With this script, even if I am passing the parent view, it gets created at the top level.

```
import jenkins.model.* 
import hudson.model.ListView
import hudson.model.*

def name = 'Top_Level_View'
def viewObj = Jenkins.instance.getView(name)
def parentObj = viewObj.getOwner()

parentObj.getViews().each { 
   if( it.name == 'Top_Level_View'){
    it.getViews().each{
                if( it.name == "DevTools_View"){
                      it.getViews().each{
                           println "Views under DevTools_View: " + it.name
                           if(it.name == "1_Build_Triggers"){
                                  println "Parent Name: " + it.getOwner().name
                  //Create a view under a given parent.
                                  Jenkins.instance.addView(new ListView('Hello',it.getOwner()))
                                  it.getOwner().getViews().each{
                                    println "print view Name: " + it.name
                                  }
                           }
                      } 
                }
    }
  }
} 

```

Here is the further analysis done. There are two main classes participate in the view creation process: hudson.model.ViewGroupMixIn and jenkins.model.Jenkins. Jenkins.addView(View view) method has to be called in order to create the view. This method inturn calls addView() method of ViewGroupMixIn class. 

addView from Jenkins class
--------------------------

```
public void addView(View v) throws IOException {
    viewGroupMixIn.addView(v);
}

```

addView from ViewGroupMixIn
---------------------------

```
public void addView(View v) throws IOException {
   v.owner = owner;
   views().add(v);
   owner.save();
}

```

Note that owner of the view gets overridden by the addView method. So even if we pass owner of the view, default will be taken.
viewGroupMixIn instance is declared as final in the Jenkins class here is the declaration for viewGroupMixIn:

```
private transient final ViewGroupMixIn viewGroupMixIn = new ViewGroupMixIn(this) {        
    protected List<View> views() { return views; }      
    protected String primaryView() { return primaryView; }        
    protected void primaryView(String name) { primaryView=name; }    
};

```

Constructor for ViewGroupMixIn:
-------------------------------

```
public abstract class ViewGroupMixIn {
    private final ViewGroup owner;

    /**
     * Returns all the views. This list must be concurrently iterable.
     */
    protected abstract List<View> views();
    protected abstract String primaryView();
    protected abstract void primaryView(String newName);

    protected ViewGroupMixIn(ViewGroup owner) {
        this.owner = owner;
    }

```

There is no way we can change the owner without modifying the Jenkins class. Above snippet is from jenkins.model.Jenkins class. Note that while creating ViewGrouopMixIn object, "this" reference is passed which represents Jenkins object. This means there is no way one can create the nested views programmatically. It has to be created through UI only. Apart form this, NestedView class of the NestedView-Plugin, doesn't support passing owner to the constructor. Doesn't look like if views can be created this way. Not sure if there is any other way to do so.