2 * (c) 2015 Cable Television Laboratories, Inc. All rights reserved.
5 package org.pcmm.rcd.impl;
7 import org.pcmm.gates.IGateSpec.Direction;
8 import org.pcmm.gates.IPCMMError;
9 import org.pcmm.gates.impl.GateID;
10 import org.pcmm.gates.impl.PCMMError;
11 import org.pcmm.gates.impl.PCMMGateReq;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14 import org.umu.cops.prpep.COPSPepException;
15 import org.umu.cops.prpep.COPSPepMsgSender;
16 import org.umu.cops.prpep.COPSPepReqStateMan;
17 import org.umu.cops.stack.*;
18 import org.umu.cops.stack.COPSDecision.DecisionFlag;
19 import org.umu.cops.stack.COPSObjHeader.CNum;
20 import org.umu.cops.stack.COPSObjHeader.CType;
21 import org.umu.cops.stack.COPSReportType.ReportType;
23 import java.io.IOException;
24 import java.net.Socket;
28 * PEP State manager implementation for use in a CMTS.
30 public class CmtsPepReqStateMan extends COPSPepReqStateMan {
32 private final static Logger logger = LoggerFactory.getLogger(CmtsPepReqStateMan.class);
35 * The configured gates
37 private final Map<Direction, Set<String>> gateConfig;
40 * The connected CMTSs and whether or not they are up
42 private final Map<String, Boolean> cmStatus;
45 * Contains the gates that have been set where the key is the gate name and the value is a Set of subIds
46 * that are using this gate
48 private final Map<String, Set<String>> gatesSetMap;
51 * Create a State Request Manager
53 * @param clientType - the client type for this connection
54 * @param clientHandle - the client-handle for this connection
55 * @param process - the data processor
56 * @param socket - the socket connection
57 * @param gateConfig - the configured service class names (gates)
59 public CmtsPepReqStateMan(final short clientType, final COPSHandle clientHandle, final CmtsDataProcessor process,
60 final Socket socket, final Map<Direction, Set<String>> gateConfig,
61 final Map<String, Boolean> cmStatus) {
62 super(clientType, clientHandle, process, socket, new COPSPepMsgSender(clientType, clientHandle, socket));
63 this.gateConfig = Collections.unmodifiableMap(gateConfig);
64 this.cmStatus = Collections.unmodifiableMap(cmStatus);
66 this.gatesSetMap = new HashMap<>();
67 for (final Set<String> gateIdSet: gateConfig.values()) {
68 for (final String gateId : gateIdSet) {
69 gatesSetMap.put(gateId, new HashSet<String>());
75 protected void processDecision(final COPSDecisionMsg dMsg) throws COPSException {
76 logger.info("Processing decision message - " + dMsg);
77 final Map<COPSContext, Set<COPSDecision>> decisions = dMsg.getDecisions();
79 final Map<String, String> removeDecs = new HashMap<>();
80 final Map<String, String> installDecs = new HashMap<>();
82 for (final Set<COPSDecision> copsDecisions: decisions.values()) {
83 final COPSDecision cmddecision = copsDecisions.iterator().next();
84 switch (cmddecision.getCommand()) {
86 for (final COPSDecision decision : copsDecisions) {
87 if (decision.getFlag().equals(DecisionFlag.REQERROR)) {
88 logger.info("processing decision");
89 // This is assuming a gate set right or wrong
90 if (dMsg.getDecisions().size() == 1 && dMsg.getDecSI() != null) {
91 final PCMMGateReq gateReq = new PCMMGateReq(dMsg.getDecSI().getData().getData());
92 if (gateReq.getGateSpec() != null) {
93 processGateReq(gateReq, _socket);
100 for (final COPSDecision decision : copsDecisions) {
101 // TODO - implement gate delete
108 //** Apply decisions to the configuration
109 // TODO - why is this collection never getting populated???
110 final Map<String, String> errorDecs = new HashMap<>();
111 _process.setDecisions(this, removeDecs, installDecs, errorDecs);
112 _status = Status.ST_DECS;
115 if (_process.isFailReport(this)) {
116 // COPSDebug.out(getClass().getName(),"Sending FAIL Report\n");
117 _sender.sendFailReport(_process.getReportData(this));
119 // COPSDebug.out(getClass().getName(),"Sending SUCCESS Report\n");
120 _sender.sendSuccessReport(_process.getReportData(this));
122 _status = Status.ST_REPORT;
125 _sender.sendSyncComplete();
127 _status = Status.ST_SYNCALL;
131 private void processGateReq(final PCMMGateReq gateReq, final Socket socket) throws COPSException {
132 // TODO - Check and/or Set state here
133 // Gate ADD gateReq.getTrafficProfile() != null
134 // Gate REMOVE gateReq.getTrafficProfile() == null
135 final String subId = gateReq.getSubscriberID().getSourceIPAddress().getHostAddress();
137 // Get direction here
138 final Direction gateDir = gateReq.getGateSpec().getDirection();
139 final Set<String> gateNames = gateConfig.get(gateDir);
140 final String gateName = gateReq.getTrafficProfile().getData().str();
142 IPCMMError error = new PCMMError();
143 if (subId == null || gateDir == null || gateNames == null || gateName == null) {
144 // Missing required object
145 error.setErrorCode((short)3);
146 } else if (!cmStatus.keySet().contains(subId)
147 || (cmStatus.keySet().contains(subId) && !cmStatus.get(subId))) {
149 error.setErrorCode((short)13);
150 } else if (!gateNames.contains(gateName.trim())) {
151 error.setErrorCode((short)11);
154 gatesSetMap.get(gateName.trim()).add(subId);
156 gateReq.setError(error);
158 logger.info("Processing gate request [" + gateName + "] with direction [" + gateDir + ']');
163 final List<Byte> data = new ArrayList<>();
164 for (final byte val : gateReq.getTransactionID().getAsBinaryArray())
166 for (final byte val : gateReq.getAMID().getAsBinaryArray())
168 for (final byte val : gateReq.getSubscriberID().getAsBinaryArray())
170 if (error != null) for (final byte val : gateReq.getError().getAsBinaryArray())
174 final GateID gateID = new GateID();
175 gateID.setGateID(UUID.randomUUID().hashCode());
176 for (final byte val : gateID.getAsBinaryArray())
180 final byte[] csiArr = new byte[data.size()];
181 for (int i = 0; i < data.size(); i++) {
182 csiArr[i] = data.get(i);
184 final COPSClientSI si = new COPSClientSI(CNum.CSI, CType.DEF, new COPSData(csiArr, 0, csiArr.length));
186 final ReportType reportType;
187 if (gateReq.getError() == null) reportType = ReportType.SUCCESS; else reportType = ReportType.FAILURE;
189 logger.info("Returning " + reportType + " for gate request [" + gateName + "] direction [" + gateDir
190 + "] for host - " + subId);
191 final COPSReportMsg reportMsg = new COPSReportMsg(_clientType, getClientHandle(),
192 new COPSReportType(reportType), si, null);
194 reportMsg.writeData(socket);
195 } catch (IOException e) {
196 throw new COPSPepException("Error writing gate set SUCCESS Report", e);