4 package org.pcmm.messages.impl;
6 import org.pcmm.messages.IMessage.MessageProperties;
7 import org.pcmm.messages.IMessageFactory;
8 import org.pcmm.objects.MMVersionInfo;
9 import org.pcmm.rcd.ICMTS;
10 import org.pcmm.rcd.IPCMMClient;
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
13 import org.umu.cops.stack.*;
14 import org.umu.cops.stack.COPSObjHeader.CNum;
15 import org.umu.cops.stack.COPSObjHeader.CType;
17 import java.net.InetAddress;
18 import java.util.Properties;
24 public class MessageFactory implements IMessageFactory {
26 /** Default keep-alive timer value (secs) */
27 public static final short KA_TIMER_VALUE = 30;
28 /** Default accounting timer value (secs) */
29 public static final short ACCT_TIMER_VALUE = 0;
31 private static final Logger logger = LoggerFactory.getLogger(MessageFactory.class);
33 private static final MessageFactory instance = new MessageFactory();
35 private MessageFactory() {
38 public static MessageFactory getInstance() {
45 * @see pcmm.messages.IMessageFactory#create(pcmm.messages.MessageType)
47 public COPSMsg create(final byte messageType) {
48 return create(messageType, new Properties());
54 * @see org.pcmm.messages.IMessageFactory#create(org.pcmm.messages.IMessage.
55 * MessageType, java.util.Properties)
57 public COPSMsg create(final byte messageType, final Properties properties) {
58 logger.info("Creating message of type - " + messageType);
59 // return new PCMMMessage(messageType, content);
60 switch (messageType) {
61 case COPSHeader.COPS_OP_OPN:
62 return createOPNMessage(properties);
63 case COPSHeader.COPS_OP_REQ:
64 return createREQMessage(properties);
65 case COPSHeader.COPS_OP_CAT:
66 return createCATMessage(properties);
67 case COPSHeader.COPS_OP_CC:
68 return createCCMessage(properties);
69 case COPSHeader.COPS_OP_DEC:
70 return createDECMessage(properties);
71 case COPSHeader.COPS_OP_DRQ:
73 case COPSHeader.COPS_OP_KA:
74 return createKAMessage(properties);
75 case COPSHeader.COPS_OP_RPT:
77 case COPSHeader.COPS_OP_SSC:
79 case COPSHeader.COPS_OP_SSQ:
87 * @param prop - the properties
88 * @return - the message
90 protected COPSMsg createDECMessage(final Properties prop) {
91 final COPSDecisionMsg msg = new COPSDecisionMsg();
92 // ===common part between all gate control messages
93 final COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_DEC, IPCMMClient.CLIENT_TYPE);
96 final COPSContext context = new COPSContext(COPSContext.CONFIG, (short) 0);
98 final COPSDecision decision = new COPSDecision();
99 if (prop.get(MessageProperties.DECISION_CMD_CODE) != null)
100 decision.setCmdCode((byte) prop.get(MessageProperties.DECISION_CMD_CODE));
101 if (prop.get(MessageProperties.DECISION_FLAG) != null)
102 decision.setFlags((short) prop.get(MessageProperties.DECISION_FLAG));
104 final COPSClientSI si = new COPSClientSI((byte)CNum.DEC.ordinal(), (byte)CType.CSI.ordinal());
105 if (prop.get(MessageProperties.GATE_CONTROL) != null)
106 si.setData((COPSData) prop.get(MessageProperties.GATE_CONTROL));
109 final COPSHandle handle;
110 if (prop.get(MessageProperties.CLIENT_HANDLE) != null) {
111 handle = new COPSHandle(new COPSData((String) prop.get(MessageProperties.CLIENT_HANDLE)));
114 // TODO - This smells wrong to have a null handle ID
115 handle = new COPSHandle(null);
118 msg.addDecision(decision, context);
121 // TODO - determine why this block has been commented out
123 // msg.dump(System.out);
124 // } catch (IOException unae) {
127 } catch (final COPSException e) {
128 logger.error(e.getMessage());
135 * creates a Client-Open message.
139 * @return COPS message
141 protected COPSMsg createOPNMessage(final Properties prop) {
142 final COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_OPN, IPCMMClient.CLIENT_TYPE);
143 final COPSPepId pepId = new COPSPepId();
144 // version infor object
145 short majorVersion = MMVersionInfo.DEFAULT_MAJOR_VERSION_INFO;
146 short minorVersion = MMVersionInfo.DEFAULT_MINOR_VERSION_INFO;
147 if (prop.get(MessageProperties.MM_MAJOR_VERSION_INFO) != null)
148 majorVersion = (Short) prop.get(MessageProperties.MM_MAJOR_VERSION_INFO);
149 if (prop.get(MessageProperties.MM_MINOR_VERSION_INFO) != null)
150 minorVersion = (Short) prop.get(MessageProperties.MM_MINOR_VERSION_INFO);
151 // Mandatory MM version.
152 final COPSClientSI clientSI = new COPSClientSI((byte) 1);
153 byte[] versionInfo = new MMVersionInfo(majorVersion, minorVersion).getAsBinaryArray();
154 clientSI.setData(new COPSData(versionInfo, 0, versionInfo.length));
155 final COPSClientOpenMsg msg = new COPSClientOpenMsg();
158 if (prop.get(MessageProperties.PEP_ID) != null)
159 d = new COPSData((String) prop.get(MessageProperties.PEP_ID));
161 d = new COPSData(InetAddress.getLocalHost().getHostName());
166 } catch (Exception e) {
167 logger.error("Error creating OPN message", e);
173 * creates a Client-Accept message.
174 * @param prop - properties
175 * @return COPS message
177 protected COPSMsg createCATMessage(final Properties prop) {
178 final COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_CAT, IPCMMClient.CLIENT_TYPE);
179 final COPSKATimer katimer;
180 if (prop.get(MessageProperties.KA_TIMER) != null)
181 katimer = new COPSKATimer((short) prop.get(MessageProperties.KA_TIMER));
183 katimer = new COPSKATimer(KA_TIMER_VALUE);
185 final COPSAcctTimer acctTimer;
186 if (prop.get(MessageProperties.ACCEPT_TIMER) != null)
187 acctTimer = new COPSAcctTimer((short) prop.get(MessageProperties.ACCEPT_TIMER));
189 acctTimer = new COPSAcctTimer(ACCT_TIMER_VALUE);
191 final COPSClientAcceptMsg acceptMsg = new COPSClientAcceptMsg();
194 acceptMsg.add(katimer);
195 if (acctTimer.getTimerVal() != 0)
196 acceptMsg.add(acctTimer);
197 } catch (COPSException e) {
198 logger.error(e.getMessage());
204 * creates a Client-Close message.
208 * @return COPS message
210 protected COPSMsg createCCMessage(final Properties prop) {
211 final COPSHeader cHdr = new COPSHeader(COPSHeader.COPS_OP_CC, IPCMMClient.CLIENT_TYPE);
213 if (prop.get(MessageProperties.ERR_MESSAGE) != null) {
214 short code = (short) 0;
215 final short error = (short) prop.get(MessageProperties.ERR_MESSAGE);
216 if (prop.get(MessageProperties.ERR_MESSAGE_SUB_CODE) != null)
217 code = (short) prop.get(MessageProperties.ERR_MESSAGE_SUB_CODE);
218 err = new COPSError(error, code);
220 err = new COPSError(COPSError.COPS_ERR_UNKNOWN, (short) 0);
222 final COPSClientCloseMsg closeMsg = new COPSClientCloseMsg();
226 } catch (COPSException e) {
227 logger.error(e.getMessage());
233 * creates a Request message
237 * @return Request message
239 protected COPSMsg createREQMessage(final Properties prop) {
240 final COPSHeader cHdr = new COPSHeader(COPSHeader.COPS_OP_REQ, IPCMMClient.CLIENT_TYPE);
241 final COPSReqMsg req = new COPSReqMsg();
244 if (prop.get(MessageProperties.R_TYPE) != null)
245 rType = (Short) prop.get(MessageProperties.R_TYPE);
246 else rType = ICMTS.DEFAULT_R_TYPE;
249 if (prop.get(MessageProperties.M_TYPE) != null)
250 mType = (Short) prop.get(MessageProperties.M_TYPE);
251 else mType = ICMTS.DEFAULT_M_TYPE;
253 final COPSContext copsContext = new COPSContext(rType, mType);
254 final COPSHandle copsHandle;
255 if (prop.get(MessageProperties.CLIENT_HANDLE) != null)
256 copsHandle = new COPSHandle(new COPSData((String) prop.get(MessageProperties.CLIENT_HANDLE)));
258 // just a random handle
259 copsHandle = new COPSHandle(new COPSData("" + Math.random() * 82730));
262 req.add(copsContext);
264 } catch (COPSException e) {
265 logger.error(e.getMessage());
271 * creates a Keep-Alive message.
275 * @return COPS message
276 * TODO - Why is there a timer being instantiated but never used?
278 protected COPSMsg createKAMessage(final Properties prop) {
279 final COPSHeader cHdr = new COPSHeader(COPSHeader.COPS_OP_KA, (short) 0);
280 final COPSKAMsg kaMsg = new COPSKAMsg();
281 final COPSKATimer timer;
282 if (prop.get(MessageProperties.KA_TIMER) != null)
283 timer = new COPSKATimer((Short) prop.get(MessageProperties.KA_TIMER));
285 timer = new COPSKATimer();
288 } catch (COPSException e) {
289 logger.error(e.getMessage());