| 1 | /** |
| 2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
| 3 | * contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. jclouds licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.jclouds.savvis.vpdc.util; |
| 20 | |
| 21 | import static com.google.common.base.Predicates.notNull; |
| 22 | import static com.google.common.collect.Iterables.concat; |
| 23 | import static com.google.common.collect.Iterables.filter; |
| 24 | import static com.google.common.collect.Iterables.transform; |
| 25 | |
| 26 | import java.net.URI; |
| 27 | import java.util.Map; |
| 28 | import java.util.Set; |
| 29 | |
| 30 | import org.jclouds.savvis.vpdc.domain.Link; |
| 31 | import org.jclouds.savvis.vpdc.domain.NetworkConfigSection; |
| 32 | import org.jclouds.savvis.vpdc.domain.NetworkConnectionSection; |
| 33 | import org.jclouds.savvis.vpdc.domain.Resource; |
| 34 | import org.jclouds.savvis.vpdc.domain.ResourceImpl; |
| 35 | import org.jclouds.savvis.vpdc.domain.VM; |
| 36 | import org.xml.sax.Attributes; |
| 37 | |
| 38 | import com.google.common.base.Function; |
| 39 | import com.google.common.collect.ImmutableMap; |
| 40 | import com.google.common.collect.ImmutableSet; |
| 41 | import com.google.common.collect.ImmutableMap.Builder; |
| 42 | |
| 43 | /** |
| 44 | * |
| 45 | * @author Adrian Cole |
| 46 | */ |
| 47 | public class Utils { |
| 48 | |
| 49 | public static Resource newResource(Map<String, String> attributes, String defaultType) { |
| 50 | String uri = attributes.get("href"); |
| 51 | String type = attributes.get("type"); |
| 52 | // savvis org has null href |
| 53 | String id = null; |
| 54 | URI href = null; |
| 55 | if (uri != null) { |
| 56 | href = URI.create(uri); |
| 57 | id = uri.substring(uri.lastIndexOf('/') + 1); |
| 58 | } |
| 59 | return (attributes.containsKey("rel")) ? new Link(id, attributes.get("name"), type != null ? type : defaultType, |
| 60 | href, attributes.get("rel")) : new ResourceImpl(id, attributes.get("name"), type != null ? type |
| 61 | : defaultType, href); |
| 62 | } |
| 63 | |
| 64 | public static Set<String> getIpsFromVM(VM vm) { |
| 65 | Iterable<String> ipFromConnections = transform(vm.getNetworkConnectionSections(), |
| 66 | new Function<NetworkConnectionSection, String>() { |
| 67 | @Override |
| 68 | public String apply(NetworkConnectionSection input) { |
| 69 | return input.getIpAddress(); |
| 70 | }; |
| 71 | }); |
| 72 | Iterable<String> ipsFromNat = concat(transform(vm.getNetworkConfigSections(), |
| 73 | new Function<NetworkConfigSection, Iterable<String>>() { |
| 74 | @Override |
| 75 | public Iterable<String> apply(NetworkConfigSection input) { |
| 76 | return concat(input.getInternalToExternalNATRules().keySet(), input |
| 77 | .getInternalToExternalNATRules().values()); |
| 78 | }; |
| 79 | })); |
| 80 | return ImmutableSet.copyOf(filter(concat(ipFromConnections, ipsFromNat), notNull())); |
| 81 | } |
| 82 | |
| 83 | public static Map<String, String> cleanseAttributes(Attributes in) { |
| 84 | Builder<String, String> attrs = ImmutableMap.<String, String> builder(); |
| 85 | for (int i = 0; i < in.getLength(); i++) { |
| 86 | String name = in.getQName(i); |
| 87 | if (name.indexOf(':') != -1) |
| 88 | name = name.substring(name.indexOf(':') + 1); |
| 89 | attrs.put(name, in.getValue(i)); |
| 90 | } |
| 91 | return attrs.build(); |
| 92 | } |
| 93 | |
| 94 | public static String currentOrNull(StringBuilder currentText) { |
| 95 | String returnVal = currentText.toString().trim(); |
| 96 | return returnVal.equals("") ? null : returnVal; |
| 97 | } |
| 98 | |
| 99 | public static Resource newResource(Map<String, String> attributes) { |
| 100 | return newResource(attributes, null); |
| 101 | } |
| 102 | } |