001: /*
002:
003: * <copyright>
004: *
005: * Copyright 2002-2007 BBNT Solutions, LLC
006: * under sponsorship of the Defense Advanced Research Projects
007: * Agency (DARPA).
008: *
009: * You can redistribute this software and/or modify it under the
010: * terms of the Cougaar Open Source License as published on the
011: * Cougaar Open Source Website (www.cougaar.org).
012: *
013: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
014: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
015: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
016: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
017: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
018: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
019: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
020: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
021: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
022: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
023: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
024: *
025: * </copyright>
026:
027: */
028:
029: package org.cougaar.qos.qrs;
030:
031: import java.net.InetAddress;
032:
033: import org.cougaar.util.log.Logger;
034:
035: /**
036: * A sample ResourceContext which looks for ip-flow capacity data on any feed,
037: * by using a Remos-style key with an IntegraterDS. The available formulas are
038: * 'CapacityMax' and 'CapacityUnused'.
039: */
040: public class IpFlowDS extends ResourceContext {
041: static void register() {
042: ContextInstantiater cinst = new AbstractContextInstantiater() {
043: public ResourceContext instantiateContext(
044: String[] parameters, ResourceContext parent)
045: throws ResourceContext.ParameterError {
046: return new IpFlowDS(parameters, parent);
047: }
048:
049: public Object identifyParameters(String[] parameters) {
050: if (parameters == null || parameters.length != 2) {
051: return null;
052: }
053: String x = parameters[0];
054: String y = parameters[1];
055: String result;
056: try {
057: String a1 = InetAddress.getByName(x)
058: .getHostAddress();
059: String a2 = InetAddress.getByName(y)
060: .getHostAddress();
061: result = a1 + "=>" + a2;
062: } catch (java.net.UnknownHostException ex) {
063: result = x + "=>" + y;
064: }
065: return result.intern();
066: }
067:
068: };
069: registerContextInstantiater("IpFlow", cinst);
070: }
071:
072: private static final String SOURCE = "source";
073: private static final String DESTINATION = "destination";
074:
075: // IpFlow ResourceContexts can be the first element in a path
076: protected ResourceContext preferredParent(RSS root) {
077: return root;
078: }
079:
080: protected DataFormula instantiateFormula(String kind) {
081: if (kind.equals("CapacityMax")) {
082: return new CapacityMax();
083: } else if (kind.equals("CapacityUnused")) {
084: return new CapacityUnused();
085: } else {
086: return null;
087: }
088: }
089:
090: /**
091: * The parameters should contain two strings, the source and destination ip
092: * of the flow.
093: */
094: protected void verifyParameters(String[] parameters)
095: throws ParameterError {
096: // should be two strings (ip addresses)
097: if (parameters == null || parameters.length != 2) {
098: throw new ParameterError("IpFlowDS ...");
099: }
100:
101: String src = parameters[0];
102: String dst = parameters[1];
103:
104: try {
105: src = InetAddress.getByName(src).getHostAddress();
106: dst = InetAddress.getByName(dst).getHostAddress();
107: } catch (java.net.UnknownHostException ex) {
108: Logger logger = Logging.getLogger(IpFlowDS.class);
109: logger.error(null, ex);
110: }
111:
112: bindSymbolValue(SOURCE, src);
113: bindSymbolValue(DESTINATION, dst);
114:
115: // Clobbering the arglist is not right.
116: // parameters = new Object[2];
117: // parameters[0] = src;
118: // parameters[1] = dst;
119: }
120:
121: private IpFlowDS(String[] parameters, ResourceContext parent)
122: throws ParameterError {
123: super (parameters, parent);
124: }
125:
126: public String toString() {
127: String src = (String) getValue(SOURCE);
128: String dst = (String) getValue(DESTINATION);
129: return "<IpFlowDS " + src + ", " + dst + ">";
130: }
131:
132: abstract static class Formula extends DataFormula implements
133: Constants {
134: private DataValue[] values;
135: private Logger logger;
136:
137: abstract String getKey();
138:
139: abstract String getSiteFormula();
140:
141: protected void initialize(ResourceContext context) {
142: super .initialize(context);
143: logger = Logging.getLogger(IpFlowDS.class);
144: String source = (String) context.getValue(SOURCE);
145: String destination = (String) context.getValue(DESTINATION);
146:
147: values = new DataValue[3];
148:
149: // Make a default IP flow with compile-time dependancy
150: if (source.equals(destination)) {
151: // 10 Gigbit bus
152: values[0] = new DataValue(10000000,
153: HOURLY_MEAS_CREDIBILITY);
154: } else {
155: // ethernet
156: values[0] = new DataValue(10000, DEFAULT_CREDIBILITY);
157: }
158:
159: // Get IP FLOW from the DataFeeds
160: String key = "Ip" + KEY_SEPR + "Flow" + KEY_SEPR + source
161: + KEY_SEPR + destination + KEY_SEPR + getKey();
162: String[] parameters = { key };
163: ResourceContext dependency = RSS.instance().resolveSpec(
164: "Integrater", parameters);
165: registerDependency(dependency, "Formula");
166:
167: SitesDB sites = RSS.instance().getSitesDB();
168:
169: // As Backup Case get the Site Flow for this IP Flow
170: SiteAddress from_site = sites.lookup(source);
171: SiteAddress to_site = sites.lookup(destination);
172:
173: if (from_site == null) {
174: logger.error("IP FLOW No from site matching " + source);
175: return;
176: }
177:
178: if (to_site == null) {
179: logger.error("IPFLOW No to site matching "
180: + destination);
181: return;
182: }
183:
184: String[] parameters2 = { from_site.toString(),
185: to_site.toString() };
186: ResourceContext dependency2 = RSS.instance().resolveSpec(
187: "SiteFlow", parameters2);
188: registerDependency(dependency2, getSiteFormula());
189:
190: }
191:
192: protected DataValue doCalculation(DataFormula.Values values) {
193: // ipflow should be dominate when credibility is the same
194:
195: this .values[2] = values.get("Formula");
196: this .values[1] = values.get(getSiteFormula());
197: DataValue result = DataValue.maxCredibility(this .values);
198: if (logger.isDebugEnabled()) {
199: logger.debug("Recalculating " + getKey() + " values="
200: + this .values[2] + " (ipflow) " + ", "
201: + this .values[1] + " (siteflow) " + ", "
202: + this .values[0] + " (default)");
203: logger.debug("Result=" + result);
204: }
205:
206: return result;
207: }
208:
209: }
210:
211: public static class CapacityMax extends Formula {
212:
213: String getSiteFormula() {
214: return "CapacityMax";
215: }
216:
217: String getKey() {
218: return "Capacity" + KEY_SEPR + "Max";
219: }
220:
221: }
222:
223: public static class CapacityUnused extends Formula {
224:
225: String getSiteFormula() {
226: return "CapacityUnused";
227: }
228:
229: String getKey() {
230: return "Capacity" + KEY_SEPR + "Unused";
231: }
232:
233: }
234:
235: }
|