brooklyn-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ahgittin <...@git.apache.org>
Subject [GitHub] brooklyn-server pull request: SoftLayer VLAN customizer
Date Thu, 10 Mar 2016 16:16:46 GMT
Github user ahgittin commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/53#discussion_r55704096
  
    --- Diff: locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/softlayer/SoftLayerSameVlanLocationCustomizer.java
---
    @@ -0,0 +1,192 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you 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.apache.brooklyn.location.jclouds.softlayer;
    +
    +import java.util.concurrent.Semaphore;
    +import java.util.concurrent.TimeUnit;
    +
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import com.google.common.cache.Cache;
    +import com.google.common.cache.CacheBuilder;
    +
    +import org.jclouds.compute.ComputeService;
    +import org.jclouds.compute.options.TemplateOptions;
    +import org.jclouds.softlayer.SoftLayerApi;
    +import org.jclouds.softlayer.compute.options.SoftLayerTemplateOptions;
    +import org.jclouds.softlayer.domain.VirtualGuest;
    +import org.jclouds.softlayer.features.VirtualGuestApi;
    +import org.jclouds.softlayer.reference.SoftLayerConstants;
    +
    +import org.apache.brooklyn.api.entity.Entity;
    +import org.apache.brooklyn.config.ConfigKey;
    +import org.apache.brooklyn.core.config.ConfigKeys;
    +import org.apache.brooklyn.core.location.LocationConfigKeys;
    +import org.apache.brooklyn.location.jclouds.BasicJcloudsLocationCustomizer;
    +import org.apache.brooklyn.location.jclouds.JcloudsLocation;
    +import org.apache.brooklyn.location.jclouds.JcloudsMachineLocation;
    +import org.apache.brooklyn.util.core.task.Tasks;
    +import org.apache.brooklyn.util.javalang.Threads;
    +import org.apache.brooklyn.util.text.Strings;
    +import org.apache.brooklyn.util.time.Duration;
    +
    +/**
    + * Configures SoftLayer provisioned VMs with the same VLAN ids across an application.
    + */
    +public class SoftLayerSameVlanLocationCustomizer extends BasicJcloudsLocationCustomizer
{
    +
    +    private static final Logger LOG = LoggerFactory.getLogger(SoftLayerSameVlanLocationCustomizer.class);
    +
    +    public static final ConfigKey<String> SCOPE_UID = ConfigKeys.newStringConfigKey("softlayer.vlan.scopeUid",
    +            "The unique identifier for a Softlayer location scope that will have VMs
created in the same VLAN");
    +
    +    public static final ConfigKey<Duration> SCOPE_TIMEOUT = ConfigKeys.newDurationConfigKey("softlayer.vlan.timeout",
    +            "The length of time to wait for a Softlayer VLAN ID", Duration.minutes(10));
    +
    +    /** Caches the semaphore object for locking access to the scope. */
    +    private static final Cache<String, Semaphore> semaphoreCache = CacheBuilder.newBuilder().build();
    +
    +    /** Caches the public VLAN id that should be shared between all entities in the same
scope. */
    +    private static final Cache<String, Integer> publicVlanIdCache = CacheBuilder.newBuilder().build();
    +
    +    /** Caches the private VLAN id that should be shared between all entities in the
same scope. */
    +    private static final Cache<String, Integer> privateVlanIdCache = CacheBuilder.newBuilder().build();
    +
    +    /**
    +     * Used to obtain the VLANs being used by the first created {@link JcloudsMachineLocation}.
    +     */
    +    @Override
    +    public void customize(JcloudsLocation location, ComputeService computeService, JcloudsMachineLocation
machine) {
    +        String provider = location.getProvider();
    +        if (!(provider.equals(SoftLayerConstants.SOFTLAYER_PROVIDER_NAME))) {
    +            String message = String.format("Invalid location provider: %s", provider);
    +            LOG.warn(message);
    +            throw new IllegalArgumentException(message);
    +        }
    +
    +        Semaphore semaphore = null;
    +        String scopeUid = getScopeUid(location);
    +
    +        synchronized (SoftLayerSameVlanLocationCustomizer.class) {
    +            semaphore = semaphoreCache.getIfPresent(scopeUid);
    +            if (semaphore == null) {
    +                throw new IllegalStateException("No semaphore available for scope: "
+ scopeUid);
    +            }
    +        }
    +
    +        Integer publicVlanId = publicVlanIdCache.getIfPresent(scopeUid);
    +        Integer privateVlanId = privateVlanIdCache.getIfPresent(scopeUid);
    +        if (privateVlanId != null || publicVlanId != null) {
    +            LOG.debug("SoftLayer VLANs already configured for application: {}", scopeUid);
    +            return;
    +        }
    +
    +        // Ask SoftLayer API for the VLAN details for this VM
    +        VirtualGuestApi api = computeService.getContext().unwrapApi(SoftLayerApi.class).getVirtualGuestApi();
    +        Long serverId = Long.parseLong(machine.getJcloudsId());
    +        VirtualGuest guest = api.getVirtualGuestFiltered(serverId,
    +                "primaryNetworkComponent;" +
    +                "primaryNetworkComponent.networkVlan;" +
    +                "primaryBackendNetworkComponent;" +
    +                "primaryBackendNetworkComponent.networkVlan");
    +        publicVlanId = guest.getPrimaryNetworkComponent().getNetworkVlan().getVlanNumber();
    +        privateVlanId = guest.getPrimaryBackendNetworkComponent().getNetworkVlan().getVlanNumber();
    +
    +        // Save the VLAN ids and release the semaphore
    +        publicVlanIdCache.put(scopeUid, publicVlanId);
    +        privateVlanIdCache.put(scopeUid, privateVlanId);
    +        semaphore.release();
    +    }
    +
    +    /**
    +     * Configure the {@link org.jclouds.compute.options.TemplateOptions} that will
    +     * be used by {@link JcloudsLocation} to obtain machines.
    +     */
    +    @Override
    +    public void customize(JcloudsLocation location, ComputeService computeService, TemplateOptions
templateOptions) {
    +        String provider = location.getProvider();
    +        if (!(provider.equals(SoftLayerConstants.SOFTLAYER_PROVIDER_NAME) &&
    +                templateOptions instanceof SoftLayerTemplateOptions)) {
    +            String message = String.format("Invalid location provider or template options:
%s/%s",
    +                    provider, templateOptions.getClass().getSimpleName());
    +            LOG.warn(message);
    +            throw new IllegalArgumentException(message);
    +        }
    +
    +        SoftLayerTemplateOptions softLayerOptions = (SoftLayerTemplateOptions) templateOptions;
    +        if (softLayerOptions.getPrimaryNetworkComponentNetworkVlanId() != null ||
    +                softLayerOptions.getPrimaryBackendNetworkComponentNetworkVlanId() !=
null) {
    +            LOG.debug("SoftLayer VLANs already configured in template options, skipping");
    +            return;
    +        }
    +
    +        Semaphore semaphore = null;
    +        String scopeUid = getScopeUid(location);
    +
    +        synchronized (SoftLayerSameVlanLocationCustomizer.class) {
    +            semaphore = semaphoreCache.getIfPresent(scopeUid);
    +            if (semaphore == null) {
    +                // We are first, so create a new semaphore and return
    +                LOG.debug("Creating semaphore for scope: {}", scopeUid);
    +                semaphore = new Semaphore(1);
    +                semaphoreCache.put(scopeUid, semaphore);
    --- End diff --
    
    shouldn't this also acquire the semaphore?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message