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.*;
15 import java.net.InetAddress;
16 import java.util.Properties;
22 public class MessageFactory implements IMessageFactory {
24 /** Default keep-alive timer value (secs) */
25 public static final short KA_TIMER_VALUE = 30;
26 /** Default accounting timer value (secs) */
27 public static final short ACCT_TIMER_VALUE = 0;
29 private static final Logger logger = LoggerFactory.getLogger(MessageFactory.class);
31 private static final MessageFactory instance = new MessageFactory();
33 private MessageFactory() {
36 public static MessageFactory getInstance() {
43 * @see pcmm.messages.IMessageFactory#create(pcmm.messages.MessageType)
45 public COPSMsg create(final byte messageType) {
46 return create(messageType, new Properties());
52 * @see org.pcmm.messages.IMessageFactory#create(org.pcmm.messages.IMessage.
53 * MessageType, java.util.Properties)
55 public COPSMsg create(final byte messageType, final Properties properties) {
56 logger.info("Creating message of type - " + messageType);
57 // return new PCMMMessage(messageType, content);
58 switch (messageType) {
59 case COPSHeader.COPS_OP_OPN:
60 return createOPNMessage(properties);
61 case COPSHeader.COPS_OP_REQ:
62 return createREQMessage(properties);
63 case COPSHeader.COPS_OP_CAT:
64 return createCATMessage(properties);
65 case COPSHeader.COPS_OP_CC:
66 return createCCMessage(properties);
67 case COPSHeader.COPS_OP_DEC:
68 return createDECMessage(properties);
69 case COPSHeader.COPS_OP_DRQ:
71 case COPSHeader.COPS_OP_KA:
72 return createKAMessage(properties);
73 case COPSHeader.COPS_OP_RPT:
75 case COPSHeader.COPS_OP_SSC:
77 case COPSHeader.COPS_OP_SSQ:
85 * @param prop - the properties
86 * @return - the message
88 protected COPSMsg createDECMessage(final Properties prop) {
89 final COPSDecisionMsg msg = new COPSDecisionMsg();
90 // ===common part between all gate control messages
91 final COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_DEC, IPCMMClient.CLIENT_TYPE);
94 final COPSContext context = new COPSContext(COPSContext.CONFIG, (short) 0);
96 final COPSDecision decision = new COPSDecision();
97 if (prop.get(MessageProperties.DECISION_CMD_CODE) != null)
98 decision.setCmdCode((byte) prop.get(MessageProperties.DECISION_CMD_CODE));
99 if (prop.get(MessageProperties.DECISION_FLAG) != null)
100 decision.setFlags((short) prop.get(MessageProperties.DECISION_FLAG));
102 final COPSClientSI si = new COPSClientSI(COPSObjHeader.COPS_DEC, (byte) 4);
103 if (prop.get(MessageProperties.GATE_CONTROL) != null)
104 si.setData((COPSData) prop.get(MessageProperties.GATE_CONTROL));
107 final COPSHandle handle;
108 if (prop.get(MessageProperties.CLIENT_HANDLE) != null) {
109 handle = new COPSHandle(new COPSData((String) prop.get(MessageProperties.CLIENT_HANDLE)));
112 // TODO - This smells wrong to have a null handle ID
113 handle = new COPSHandle(null);
116 msg.addDecision(decision, context);
119 // TODO - determine why this block has been commented out
121 // msg.dump(System.out);
122 // } catch (IOException unae) {
125 } catch (final COPSException e) {
126 logger.error(e.getMessage());
133 * creates a Client-Open message.
137 * @return COPS message
139 protected COPSMsg createOPNMessage(final Properties prop) {
140 final COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_OPN, IPCMMClient.CLIENT_TYPE);
141 final COPSPepId pepId = new COPSPepId();
142 // version infor object
143 short majorVersion = MMVersionInfo.DEFAULT_MAJOR_VERSION_INFO;
144 short minorVersion = MMVersionInfo.DEFAULT_MINOR_VERSION_INFO;
145 if (prop.get(MessageProperties.MM_MAJOR_VERSION_INFO) != null)
146 majorVersion = (Short) prop.get(MessageProperties.MM_MAJOR_VERSION_INFO);
147 if (prop.get(MessageProperties.MM_MINOR_VERSION_INFO) != null)
148 minorVersion = (Short) prop.get(MessageProperties.MM_MINOR_VERSION_INFO);
149 // Mandatory MM version.
150 final COPSClientSI clientSI = new COPSClientSI((byte) 1);
151 byte[] versionInfo = new MMVersionInfo(majorVersion, minorVersion).getAsBinaryArray();
152 clientSI.setData(new COPSData(versionInfo, 0, versionInfo.length));
153 final COPSClientOpenMsg msg = new COPSClientOpenMsg();
156 if (prop.get(MessageProperties.PEP_ID) != null)
157 d = new COPSData((String) prop.get(MessageProperties.PEP_ID));
159 d = new COPSData(InetAddress.getLocalHost().getHostName());
164 } catch (Exception e) {
165 logger.error("Error creating OPN message", e);
171 * creates a Client-Accept message.
172 * @param prop - properties
173 * @return COPS message
175 protected COPSMsg createCATMessage(final Properties prop) {
176 final COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_CAT, IPCMMClient.CLIENT_TYPE);
177 final COPSKATimer katimer;
178 if (prop.get(MessageProperties.KA_TIMER) != null)
179 katimer = new COPSKATimer((short) prop.get(MessageProperties.KA_TIMER));
181 katimer = new COPSKATimer(KA_TIMER_VALUE);
183 final COPSAcctTimer acctTimer;
184 if (prop.get(MessageProperties.ACCEPT_TIMER) != null)
185 acctTimer = new COPSAcctTimer((short) prop.get(MessageProperties.ACCEPT_TIMER));
187 acctTimer = new COPSAcctTimer(ACCT_TIMER_VALUE);
189 final COPSClientAcceptMsg acceptMsg = new COPSClientAcceptMsg();
192 acceptMsg.add(katimer);
193 if (acctTimer.getTimerVal() != 0)
194 acceptMsg.add(acctTimer);
195 } catch (COPSException e) {
196 logger.error(e.getMessage());
202 * creates a Client-Close message.
206 * @return COPS message
208 protected COPSMsg createCCMessage(final Properties prop) {
209 final COPSHeader cHdr = new COPSHeader(COPSHeader.COPS_OP_CC, IPCMMClient.CLIENT_TYPE);
211 if (prop.get(MessageProperties.ERR_MESSAGE) != null) {
212 short code = (short) 0;
213 final short error = (short) prop.get(MessageProperties.ERR_MESSAGE);
214 if (prop.get(MessageProperties.ERR_MESSAGE_SUB_CODE) != null)
215 code = (short) prop.get(MessageProperties.ERR_MESSAGE_SUB_CODE);
216 err = new COPSError(error, code);
218 err = new COPSError(COPSError.COPS_ERR_UNKNOWN, (short) 0);
220 final COPSClientCloseMsg closeMsg = new COPSClientCloseMsg();
224 } catch (COPSException e) {
225 logger.error(e.getMessage());
231 * creates a Request message
235 * @return Request message
237 protected COPSMsg createREQMessage(final Properties prop) {
238 final COPSHeader cHdr = new COPSHeader(COPSHeader.COPS_OP_REQ, IPCMMClient.CLIENT_TYPE);
239 final COPSReqMsg req = new COPSReqMsg();
242 if (prop.get(MessageProperties.R_TYPE) != null)
243 rType = (Short) prop.get(MessageProperties.R_TYPE);
244 else rType = ICMTS.DEFAULT_R_TYPE;
247 if (prop.get(MessageProperties.M_TYPE) != null)
248 mType = (Short) prop.get(MessageProperties.M_TYPE);
249 else mType = ICMTS.DEFAULT_M_TYPE;
251 final COPSContext copsContext = new COPSContext(rType, mType);
252 final COPSHandle copsHandle;
253 if (prop.get(MessageProperties.CLIENT_HANDLE) != null)
254 copsHandle = new COPSHandle(new COPSData((String) prop.get(MessageProperties.CLIENT_HANDLE)));
256 // just a random handle
257 copsHandle = new COPSHandle(new COPSData("" + Math.random() * 82730));
260 req.add(copsContext);
262 } catch (COPSException e) {
263 logger.error(e.getMessage());
269 * creates a Keep-Alive message.
273 * @return COPS message
274 * TODO - Why is there a timer being instantiated but never used?
276 protected COPSMsg createKAMessage(final Properties prop) {
277 final COPSHeader cHdr = new COPSHeader(COPSHeader.COPS_OP_KA, (short) 0);
278 final COPSKAMsg kaMsg = new COPSKAMsg();
279 final COPSKATimer timer;
280 if (prop.get(MessageProperties.KA_TIMER) != null)
281 timer = new COPSKATimer((Short) prop.get(MessageProperties.KA_TIMER));
283 timer = new COPSKATimer();
286 } catch (COPSException e) {
287 logger.error(e.getMessage());