/**
   * Stop and remove the router resource
   *
   * @throws ResourceException
   * @throws ProtocolException
   */
  protected void stopResource() throws ResourceException, ProtocolException {
    // Stop resource
    resourceManager.stopResource(routerResource.getResourceIdentifier());

    // Remove resource
    resourceManager.removeResource(routerResource.getResourceIdentifier());
  }
  public void startResource() throws ResourceException, ProtocolException {
    /* initialize model */
    List<CapabilityDescriptor> lCapabilityDescriptors = new ArrayList<CapabilityDescriptor>();

    CapabilityDescriptor greCapabilityDescriptor =
        ResourceHelper.newCapabilityDescriptor(
            TestsConstants.ACTION_NAME,
            TestsConstants.CAPABILIY_VERSION,
            TestsConstants.GRE_CAPABILITY_TYPE,
            TestsConstants.CAPABILITY_URI);
    lCapabilityDescriptors.add(greCapabilityDescriptor);

    // Add Queue Capability Descriptor
    CapabilityDescriptor queueCapabilityDescriptor = ResourceHelper.newQueueCapabilityDescriptor();
    lCapabilityDescriptors.add(queueCapabilityDescriptor);

    // Router Resource Descriptor
    ResourceDescriptor resourceDescriptor =
        ResourceHelper.newResourceDescriptor(
            lCapabilityDescriptors,
            TestsConstants.RESOURCE_TYPE,
            TestsConstants.RESOURCE_URI,
            RESOURCE_INFO_NAME);

    routerResource = resourceManager.createResource(resourceDescriptor);

    // If not exists the protocol session manager, it's created and add the session context
    InitializerTestHelper.addSessionContext(
        protocolManager,
        routerResource.getResourceIdentifier().getId(),
        TestsConstants.RESOURCE_URI);

    // Start resource
    resourceManager.startResource(routerResource.getResourceIdentifier());
  }
  private IResource startRouterResource(String routerName)
      throws ResourceException, ProtocolException {

    List<CapabilityDescriptor> lCapabilityDescriptors = new ArrayList<CapabilityDescriptor>();
    CapabilityDescriptor ospfCapabilityDescriptor =
        ResourceHelper.newCapabilityDescriptor("junos", "10.10", "ospf", CAPABILITY_URI);
    lCapabilityDescriptors.add(ospfCapabilityDescriptor);

    // Add Queue Capability Descriptor
    CapabilityDescriptor queueCapabilityDescriptor = ResourceHelper.newQueueCapabilityDescriptor();
    lCapabilityDescriptors.add(queueCapabilityDescriptor);

    // Router Resource Descriptor
    ResourceDescriptor resourceDescriptor =
        ResourceHelper.newResourceDescriptor(
            lCapabilityDescriptors, "router", RESOURCE_URI, routerName);

    IResource routerResource = resourceManager.createResource(resourceDescriptor);

    // If not exists the protocol session manager, it's created and add the session context
    addSessionContext(routerResource.getResourceIdentifier().getId());

    // Start resource
    resourceManager.startResource(routerResource.getResourceIdentifier());

    return routerResource;
  }
  @After
  public void clearRepo() {

    log.info("Clearing resource repo");

    IResource[] toRemove = new IResource[repository.listResources().size()];
    toRemove = repository.listResources().toArray(toRemove);

    for (IResource resource : toRemove) {
      if (resource.getState().equals(State.ACTIVE)) {
        try {
          repository.stopResource(resource.getResourceIdentifier().getId());
        } catch (ResourceException e) {
          log.error(
              "Failed to remove resource "
                  + resource.getResourceIdentifier().getId()
                  + " from repository.");
        }
      }
      try {
        repository.removeResource(resource.getResourceIdentifier().getId());
      } catch (ResourceException e) {
        log.error(
            "Failed to remove resource "
                + resource.getResourceIdentifier().getId()
                + " from repository.");
      }
    }

    log.info("Resource repo cleared!");
  }
  @Test
  public void repoIsPublishedInResourceManagerTest() throws ResourceException, ProtocolException {

    ResourceDescriptor resourceDescriptor = ResourceHelper.newResourceDescriptorProteus("roadm");

    IResource resource = resourceManger.createResource(resourceDescriptor);
    createProtocolForResource(resource.getResourceIdentifier().getId());
    resourceManger.startResource(resource.getResourceIdentifier());
    resourceManger.stopResource(resource.getResourceIdentifier());
    resourceManger.removeResource(resource.getResourceIdentifier());
  }
  @Test
  public void RemoveAndCreateResource() throws Exception {

    ResourceDescriptor resourceDescriptor = ResourceHelper.newResourceDescriptorProteus("roadm");

    IResource resource = repository.createResource(resourceDescriptor);
    Assert.assertFalse(repository.listResources().isEmpty());

    createProtocolForResource(resource.getResourceIdentifier().getId());

    repository.removeResource(resource.getResourceIdentifier().getId());
    Assert.assertTrue(repository.listResources().isEmpty());
  }
  /**
   * Stop and remove the network resource
   *
   * @throws ResourceException
   * @throws ProtocolException
   */
  @After
  public void stopResource() throws ResourceException, ProtocolException {
    resourceManager.stopResource(networkResource.getResourceIdentifier());
    resourceManager.removeResource(networkResource.getResourceIdentifier());

    // remove remaining if any
    List<IResource> toRemove = resourceManager.listResources();

    for (IResource resource : toRemove) {
      if (resource.getState().equals(State.ACTIVE)) {
        resourceManager.stopResource(resource.getResourceIdentifier());
      }
      resourceManager.removeResource(resource.getResourceIdentifier());
    }
  }
  /**
   * Start network resource with queue and ospf capability
   *
   * @throws ResourceException
   * @throws ProtocolException
   */
  @Before
  public void startResource() throws ResourceException, ProtocolException {

    List<CapabilityDescriptor> capDescriptors = new ArrayList<CapabilityDescriptor>();

    capDescriptors.add(
        ResourceHelper.newCapabilityDescriptor(
            ACTION_NAME, CAPABILIY_VERSION, BASIC_CAPABILIY_TYPE, CAPABILITY_URI));

    capDescriptors.add(
        ResourceHelper.newCapabilityDescriptor(
            ACTION_NAME, CAPABILIY_VERSION, OSPF_CAPABILIY_TYPE, CAPABILITY_URI));

    capDescriptors.add(
        ResourceHelper.newCapabilityDescriptor(
            ACTION_NAME, CAPABILIY_VERSION, QUEUE_CAPABILIY_TYPE, CAPABILITY_URI));

    // Network Resource Descriptor
    ResourceDescriptor resourceDescriptor =
        ResourceHelper.newResourceDescriptor(
            capDescriptors, RESOURCE_TYPE, RESOURCE_URI, RESOURCE_INFO_NAME);

    // Create resource
    networkResource = resourceManager.createResource(resourceDescriptor);

    // Start resource
    resourceManager.startResource(networkResource.getResourceIdentifier());
  }
  /**
   * FIXME this method should be part of refresh action. Remove it when moved!!!
   *
   * @param resource
   * @throws ResourceException
   */
  private void initVirtualResources(IResource resource) throws ResourceException {

    if (resource.getModel() != null) {

      List<String> nameLogicalRouters = resource.getModel().getChildren();

      if (!nameLogicalRouters.isEmpty()) {

        log.debug("Loading child logical routers");

        /* resource type for all child logical devices is the same as the parent (physical) one */
        String typeResource = resource.getResourceIdentifier().getType();
        IResourceManager resourceManager;
        try {
          resourceManager = Activator.getResourceManagerService();
        } catch (Exception e1) {
          throw new ResourceException("Could not get Resource Manager Service.");
        }

        /* initialize each resource */
        int createdResources = 0;
        for (String nameResource : nameLogicalRouters) {
          try {
            resourceManager.getIdentifierFromResourceName(typeResource, nameResource);
            log.info("A resource with this name already exists, omitting creation");
          } catch (ResourceNotFoundException e) {
            // TODO If the resource exists what it is our decision?
            log.error(e.getMessage());
            log.info("This resource is new, it have to be created");
            ResourceDescriptor newResourceDescriptor =
                newResourceDescriptor(resource.getResourceDescriptor(), nameResource);

            /* create new resources */
            resourceManager.createResource(newResourceDescriptor);
            createdResources++;
          }
        }
        log.debug("Loaded " + createdResources + " new logical routers");

        // FIXME If a resource is created, we have to delete the don't used resources
      }
    }
  }